I’ve been using FNA for a while in my project and recently switched back to MonoGame for 3.3. This caused my pixel shaders to stop working and I figured the textures I was setting to GraphicsDevice.Textures[] were no longer available in the shader. Here’s a brief example of what I’m doing:
C#
GraphicsDevice.Textures[1] = _renderTarget1;
GraphicsDevice.Textures[2] = Game.Content.Load<Texture2D>("Texture1");
GraphicsDevice.Textures[3] = _renderTarget2;
GraphicsDevice.Textures[4] = Game.Content.Load<Texture2D>("Texture2");
HLSL
sampler2D textureA : register(s1);
sampler2D textureB : register(s2);
sampler2D textureC : register(s3);
sampler2D textureD : register(s4);
I couldn’t reference the textures in the shader - tex2D returned nothing. I found that in EffectPass.SetShaderSamplers() the elements within the TextureCollection collection were being overwritten with NULL as the loaded Effect object didn’t have a texture in the Data property of the corresponding parameter. By placing a temporary NULL check around the texture assignment I’ve fixed my problem - the textures are no longer wiped and are available in the shader. I’ve not yet found the real cause of the issue but maybe someone who knows this area could advise on what’s happening here.
private void SetShaderSamplers(Shader shader, TextureCollection textures, SamplerStateCollection samplerStates)
{
foreach (var sampler in shader.Samplers)
{
var param = _effect.Parameters[sampler.parameter];
var texture = param.Data as Texture;
textures[sampler.textureSlot] = texture;
// If there is a sampler state set it.
if (sampler.state != null)
samplerStates[sampler.samplerSlot] = sampler.state;
}
}