Help: HLSL Sampler cannot set texture


I’m very new to the whole shader business, but I’ve looked everywhere and could not find the problem with my code.

I’m trying to create a shader which overrides the transparency original texture with another texture.

My shader code is:

sampler s0;

texture IceTexture;
sampler ice_sampler = sampler_state
	Texture = <IceTexture>;

float4 PixelShaderFunction(float4 position : SV_Position, float4 color : COLOR0, float2 coords : TEXCOORD0) : COLOR0
	return tex2D(ice_sampler, coords);

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

I then applied the shader in my C#:

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

And here’s where the weirdness begins. Any function I write in PixelShaderFunction works on the base texture (i.e. if I change the shader to inverse the pixel color, or set everything to red), but when I use tex2D(ice_sampler, coords);, the result is still taken from the texture from the spritebatch, not the IceTexture I set value to in C#.

Is there something I’m doing wrong? Many thanks for any help!


On the off chance that this can help any passer by, the problem was that the sampler s0 was not used in the shader function and seemed to be optimized out, leaving ice_sampler the first available sampler and automatically assigned to the original texture. Using sampler s0 in a way that affects the end pixel color in PixelShaderFunction solved the problem.