Shader - On OpenGL removes Texture2D

Hi,

I’m trying to get deferred 2D-Light working.
The shader I’m using to combine the textures doesn’t work for OpenGL, though.
Using the same Shader on DirectX works perfectly fine.

When trying to set the parameters in OpenGL it can’t find “LightMap” and “NormalMap” in parameters.
Why is that and how can I solve this?

This is the Shader:

#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

float ambient;
float4 ambientColor;
float lightAmbient;

sampler ColorMapSampler: register (s0); //Set by spritebatch

Texture2D LightMap;
sampler LightMapSampler: register (s1)
{
    Texture = (LightMap);
    magfilter = LINEAR;
    minfilter = LINEAR;
    mipfilter = LINEAR;
    AddressU = wrap;
    AddressV = wrap;
};

Texture2D NormalMap;
sampler NormalMapSampler: register (s2)
{
    Texture = (NormalMap);
    magfilter = LINEAR;
    minfilter = LINEAR;
    mipfilter = LINEAR;
    AddressU = wrap;
    AddressV = wrap;
};

float4 DeferredLightPixelShader(float4 pos : SV_POSITION, float4 color1 : COLOR0, float2 texCoord : TEXCOORD0) : SV_TARGET0
{    
    float4 col= tex2D(ColorMapSampler, texCoord.xy);
    float4 shading = LightMap.Sample(LightMapSampler, texCoord.xy);
    float3 normal = NormalMap.Sample(NormalMapSampler, texCoord.xy).rgb;

    if(!any(normal))
    {
        return float4(0, 0, 0, 0);
    }

    return (col * ambientColor *ambient)+((shading * col) * lightAmbient);
}

technique DeferredLightEffect
{
    pass Pass1
    {
        PixelShader = compile PS_SHADERMODEL DeferredLightPixelShader();
    }
}

Well…
That moment when after hours of research you didn’t come up with anything. So you create a topic. And minutes after find the solution.
Changing

float4 shading = LightMap.Sample(LightMapSampler, texCoord.xy);
float3 normal = NormalMap.Sample(NormalMapSampler, texCoord.xy).rgb;

To

float4 shading = tex2D(LightMapSampler, texCoord.xy);
float3 normal = tex2D(NormalMapSampler, texCoord.xy).rgb;

fixed the issue.

Great that you’ve solved it.

Just as a tip, you can use macros for sampling to solve this kind of problem as well, eg:

#if OPENGL
    #define SAMPLE_TEXTURE(Name, texCoord)  tex2D(Name, texCoord)
#else
    #define SAMPLE_TEXTURE(name, texCoord)  name.Sample(name##Sampler, texCoord)
#endif

The main problem is that the parameters are named differently in the GL context:

2 Likes