0
\$\begingroup\$

My Monogame HLSL shaders only successfully sample one texture (besides the screen). Any additional textures I try to sample end up with the same texture data as the screen.

I've found people with similar issues as me, such as this thread. However, I'm unsure of what they've done to remedy the problem. Any solutions I've seen in other threads don't work.

C#

Lighting.Parameters["NightTexture"].SetValue(night);
Lighting.Parameters["SunsetTexture"].SetValue(sunset);

HLSL

float4x4 World;
float4x4 View;
float4x4 Projection;

Texture2D NightTexture;
sampler2D NightSampler = sampler_state
{
    Texture = <NightTexture>;
};

Texture2D SunsetTexture;
sampler2D SunsetSampler = sampler_state
{
    Texture = <SunsetTexture>;
};

sampler TextureSampler : register(s0);

float4 main(float4 position : SV_Position, float4 color : COLOR0, float2 
texCoord : TEXCOORD0) : COLOR0
{
    // Different Grading
    float4 day = tex2D(TextureSampler, texCoord);
    float4 night = tex2D(NightSampler, texCoord);
    float4 sunset = tex2D(SunsetSampler, texCoord);

    float4 col = lerp(day, night, .5);
    col = lerp(sunset, col, .5);

    return col;
}

technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_5_0  main();
    }
}

Note: The code is abbreviated to show the issue. When executed, the SunsetTexture and TextureSampler both have correct data, but the NightTexture copies the TextureSampler's data.

Any ideas how to fix this?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Looks like it's just a matter of using textures in the right order.

float4x4 World;
float4x4 View;
float4x4 Projection;

sampler TextureSampler : register(s0);

Texture2D NightTexture;
sampler2D NightSampler = sampler_state
{
    Texture = <NightTexture>;
};

Texture2D SunsetTexture;
sampler2D SunsetSampler = sampler_state
{
    Texture = <SunsetTexture>;
};

float4 main(float4 position : SV_Position, float4 color : COLOR0, float2 
texCoord : TEXCOORD0) : COLOR0
{
    float4 day = tex2D(TextureSampler, texCoord);
    float4 night = tex2D(NightSampler, texCoord);
    float4 sunset = tex2D(SunsetSampler, texCoord);

    float4 col = lerp(day, night, .5);
    col = lerp(sunset, col, .5);

    return col;
}

technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_5_0  main();
    }
}

I moved TextureSampler to the top.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .