Help getting shaders up and running in XNA

Started by
3 comments, last by Daaark 14 years, 2 months ago
Hallo, I am having some problems getting shaders to work and was wondering if anyone could point out what I was doing wrong. The sprite is being rendered correctly, but the shader has no effect Here is the draw code:

m_spriteEffect.Begin();

spriteBatch.Draw(texture, pos, getRect(), Color.White);

m_spriteEffect.End();

Here is the spriteEffect begin/end

public override void Begin()
{
   m_effect.Begin();
   m_effect.CurrentTechnique.Passes[0].Begin();
}

public override void End()
{
   m_effect.CurrentTechnique.Passes[0].End();
   m_effect.End();  
}

Here is my shader code:

float4 PixelShaderFunction(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0  
{  
    float4 returnColor =  float4(0,0,1,1);
    return returnColor;  
}  
 
technique Default  
{  
    pass p0  
    {  
        PixelShader = compile ps_3_0 PixelShaderFunction();  
    }  
}  

Other info: The game is all 2d, no 3d meshes or vertexes. The spritebatch is started with Begin() Regards,
Advertisement
That would be because you're calling spriteBatch.Draw, which I assume is a variable of type SpriteBatch, which is internally coded to use its own built in shader (for which you can get the source to at creators.xna.com). So no, wrapping your spriteBatch.Draw call in an effect.Begin() will do nothing.
Quote:The spritebatch is started with Begin()


There's your problem. You can't use the default arguments for SpriteBatch.Begin() with shaders, because when you do, all the actual rendering takes place inside the SpriteBatch.End() call, instead of in the draw calls you put between your shader. That's because SpriteBatch actually batches and sorts the draw calls together by default.

You need to set the SpriteSortMode to Immediate in the Begin() arguments, and that should get things going.
Thanks to both of you for the really quick answer.
There is a new developer talk posted at the creators.xna.com site where Shawn Hargreaves goes over basic shader use (with SpriteBatch).

This topic is closed to new replies.

Advertisement