Reputation: 3075
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?
If I have several techniques in an effect file - how do I call a particular one? Because at the moment what I do is: shaders.CurrentTechnique.Passes[0].Apply();
and it works for one technique but I would like to have many.
If technique has several passes how do I apply all of them?
If I already applied a shader how do I cancel it? I can end the current sprite batch of course and start another one. But I don't really know if that's how it should be done for most efficiency.
Any other tips for working with pixel shaders?
Upvotes: 1
Views: 1664
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