NewProger
NewProger

Reputation: 3075

Use multiple pixel shaders?

I just started using pixel shaders with xna, but I can't wrap my head around several things, and it seems there is no clear answer anywhere...

I use spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); and I would like to apply shaders to a particular sprite I'm drawing and then cancel it back to a default shader or no shader.

So, can you help me with following?

Any other tips for working with pixel shaders?

Upvotes: 1

Views: 1664

Answers (1)

marc wellman
marc wellman

Reputation: 5886

You can define different techniques in your effects file like this:

// shading code ...

technique Technique1
{
    pass Pass1
    {
        // VertexShader = ...
        // PixelShader = ...
    }

    pass Pass2
    {
        // VertexShader = ...
        // PixelShader = ...
    }

    // more passes if you want
}

technique Technique2
{
    pass Pass1
    {
        // VertexShader = ...
        // PixelShader = ...
    }

    // more Passes if you want ...
}

From your C# code use your effects file as follows:

// declar your variable by loading the effect file from the content pipeline
Effect effect = ContentManager.Load<Effect>("NameOfMyEffectFile");

// use a particular technique
effect.CurrentTechnique = effect.Techniques["Technique1"];

// apply a particular pass
effect.CurrentTechnique.Passes[1].Apply();

// begin some drawing
effect.Begin();

// draw ...

// end some drawing
effect.End();

If you want to apply multiple passes, simply iterate over all of them like this:

foreach(EffectPass p in effect.CurrentTechnique.Passes)
{
    // begin some drawing
    p.Begin();

    // draw ...

    // end some drawing
    p.End();
}

Upvotes: 3

Related Questions