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:
Here's what I see when I run my game:
Drawing the texture without passing effect
in spriteBatch.Begin()
works fine, for instance:
Drawing the texture at (40, 10) instead of (0, 0) gives different results, too (the green stripe isn't as thick):
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)