0
\$\begingroup\$

I'm new to shaders, HLSL and XNA in general. I'm trying to write a pass-through pixel shader but I'm experiencing some odd behaviour. I created a simple texture, shown below:

1

Here's what I see when I run my game:

2

Drawing the texture without passing effect in spriteBatch.Begin() works fine, for instance:

3

Drawing the texture at (40, 10) instead of (0, 0) gives different results, too (the green stripe isn't as thick):

4

My shader file:

sampler input;

float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0
{
    float4 color = tex2D(input, coords);
    return color;
}

technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
    }
}

My game code:

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D tex;
    Effect effect;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        tex = Content.Load<Texture2D>("tex");
        effect = Content.Load<Effect>("effect");
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin(sortMode: SpriteSortMode.Immediate, effect: effect);
        spriteBatch.Draw(tex, new Vector2(0, 0), Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

What am I doing wrong? I'm expecting my pass-through shader to simply draw the texture on the screen as-is (appearing as in the second screenshot above), but it looks totally wrong. (For what it's worth, I was trying to follow this tutorial: http://blog.josack.com/2011/07/my-first-2d-pixel-shaders-part-1.html)

\$\endgroup\$
  • \$\begingroup\$ Are you using the screen coordinates as texture coordinates? (I don't speak HLSL, but that's what it looks like from your screenshots) \$\endgroup\$ – immibis Aug 2 '15 at 23:15
1
\$\begingroup\$

Since you override SpriteBatch's default effect with your own, you are now responsible for setting effect parameters, especially matrices. You probably would like to replicate default SpriteBatch behaviour, so set effect.World to identity, and for projection default SpriteBatch effect uses effect.Projection = Matrix.CreateTranslation(-0.5f, -0.5f, 0) * Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.