Sign in to follow this  
h3ro

Help getting shaders up and running in XNA

Recommended Posts

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,

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this