Game Development Community

TGEA 1.8.x possible bug - get Shader Const Handle to a sampler2D shader constant

by Jay Verba · in Torque Game Engine Advanced · 02/13/2009 (12:33 pm) · 2 replies

Put a break point on line 147 of clipMapBlenderCache.cpp to see this in action. All of the shader constant handles that reference sampler2D constants are 0 after the getShaderConstHandle() call.

I believe these should be returning something valid.

#1
02/13/2009 (1:13 pm)
In D3D they do not, and should not, return valid handles. In OpenGL they do, and should, return valid handles.

HLSL hardcodes the sampler number directly in the shader and you are not allowed to change it. In GLSL you cannot put the sampler number in the shader and you must set it.
#2
07/24/2009 (12:28 am)
I'd just like to add a quick "tutorial" to this thread to drive the point home. I wasted nearly 6 hours today on this tiny issue! (before I saw this thread - doh)

When converting your DirectX/HLSL shaders to OpenGL/GLSL, the texture samplers need special attention. Failure to do this will result in ALL your sampler2D's referencing the same texture.

1. Create a GFXShaderConstHandle for your sampler2D variable
GFXShaderConstHandle* myTextureSampler1SC;
GFXShaderConstHandle* myTextureSampler2SC;

2. Link it with your GLSL sampler2D variable
myTextureSampler1SC = myShaderData->getShader()->getShaderConstHandle("$MyTextureSampler1");
myTextureSampler2SC = myShaderData->getShader()->getShaderConstHandle("$MyTextureSampler2");

3. Finally, set it to the sampler number you're using
mySCB->set(myTextureSampler1SC, (S32)0); //Corresponds to GFX->setTexture( 0, ...
mySCB->set(myTextureSampler2SC, (S32)1); //Corresponds to GFX->setTexture( 1, ...

Enjoy your working texture sampler in OpenGL/GLSL :)

Lorne