[Solved] Why is my shader rotating and tiling the texture?

In the first image below, you can see what is rendered if I set effect in the SpriteBatch.Begin() to null, basically how it should be, and the second image shows what happens if I apply my effect. Why does this happen? Does the SpriteBatch.Begin() method pass weird texture coordinates to the shader, cause it seems the origin is somehow centered. If I turn on wrap in the texture sampler in the shader, the image is also repeated four times.

The effect code:

#if OPENGL
	#define SV_POSITION POSITION
	#define VS_SHADERMODEL vs_3_0
	#define PS_SHADERMODEL ps_3_0
#else
	#define VS_SHADERMODEL vs_4_0_level_9_1
	#define PS_SHADERMODEL ps_4_0_level_9_1
#endif

texture Tex;

sampler2D texSampler = sampler_state
{
	Texture = <Tex>;
	AddressU = clamp;
	AddressV = clamp;
	Filter = linear;
};

float4 MainPS(float2 TexCoord : TEXCOORD0) : COLOR0
{
	float4 col = tex2D(texSampler, TexCoord);
	float4 sub = { 0.5f, 0.5f, 0.5f, 0};
	col -= sub;
	return col;
}

technique BasicColorDrawing
{
	pass P0
	{
		PixelShader = compile PS_SHADERMODEL MainPS();
	}
};

The draw code:

            GraphicsDevice.SetRenderTarget(_screenTarget);

            foreach(EffectPass pass in _basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, numTriangles);
            }
        
            GraphicsDevice.SetRenderTarget(null);
            
            _spriteBatch.Begin(SpriteSortMode.Immediate,
                BlendState.AlphaBlend,
                SamplerState.PointClamp,
                DepthStencilState.Default,
                RasterizerState.CullNone,
                _testEffect);
            _spriteBatch.Draw(_screenTarget, new Rectangle(0, 0, _graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight), null, Color.White);
            _spriteBatch.End();
            

            base.Draw(gameTime);

It looks like you’re getting the vertex position fed into TexCoord.
The vertex shader output for SpriteBatch looks like this:

struct VertexShaderOutput
{
	float4 Position : SV_POSITION;
	float4 Color : COLOR0;
	float2 TexCoord : TEXCOORD0;
};

You should take that as input to your pixel shader:

float4 MainPS(VertexShaderOutput input) : COLOR0
{
    float4 col = tex2D(texSampler, input.TexCoord);
1 Like

Thanks alot, once again, for your help