FIXED: GLSL MAT unable to use Sampler array index 0

Windows 7 Ult 64-bit SP1
TD 088 build 62160
GLSL version 4.00

This is what I expect to work in the GLSL MAT pixel shader, with my sampler array starting at index of 0. There is no compilation error or TD error doing this, but the sampler I assign to index 0 is just invisible as if i don’t have a sampler at all, RGBA = 0000. While Indexes 1-3 sample and render just fine.

[code]// Init instanced normal map array
const int numInstances = 4;
uniform sampler2D sNormalMap[numInstances];
sampler2D sNormalIndex;

void main(){
// omitted unrelated code //

// Assign normal map with instance texture index 
for (int i = 0; i < numInstances; i++ )
{
		if ( vVert.texIndex == i)
		{
			sNormalIndex = sNormalMap[i];
		}
}

vec4 normalMap = texture(sampler2D(sNormalIndex), texCoord0.st);

[/code]
td_glslmat_sampler0.png

This is the workaround I’m using to avoid the issue --increasing the array size by 1, with no sampler assigned to index 0, offsetting the for loop, so I am able to use indexes 1-4.

[code]// Init instanced normal map array
const int numInstances = 5;
uniform sampler2D sNormalMap[numInstances];
sampler2D sNormalIndex;

void main(){
// omitted unrelated code //

// Assign normal map with instance texture index 
for (int i = 0; i < numInstances-1; i++ )
{
		if ( vVert.texIndex == i)
		{
			sNormalIndex = sNormalMap[i+1];
		}
}

vec4 normalMap = texture(sampler2D(sNormalIndex), texCoord0.st);

[/code]
td_glslmat_sampler1.png

Seems like a bug… but maybe there is some reason for this behaviour or some GLSL limitation with Sampler arrays.

P.S. The reason I am instancing Normal map textures this way is because am I already instancing Color map textures with TDInstanceTexture2D().
I am also unable to stack both Color and Normal maps into one texture split out in the shader by UV for instancing because I need nearest neighbour for the Color map (alpha channel is a vertex displacement map) but linear filtering for the Normal map.

Thanks

Also, I found this for reference that for GLSL 4.00 and above it is “legal to loop over an array of samplers, so long as the loop index is based on constants and uniforms.”

Khronos’ example being rather straightforward and no mention of caveats with the array index starting at the default of 0.

[url]Data Type (GLSL) - OpenGL Wiki

Hey,
I can confirm there is a bug where sampler arrays assigned like this won’t work for index 0, its just a bug in my code. However I’ll mention I don’t think this code counts as a “Dynamically Uniform Expression” required for sampler lookup into an array. Since the value of vVert.texIndex comes from gl_InstanceID (I presume), which doesn’t count as a Dynamically Uniform Expression.
From https://www.khronos.org/opengl/wiki/Core_Language_(GLSL)#Dynamically_uniform_expression

A work around for both cases would be to instead name your samplers sNormalMap0, sNormalMap1 etc. (avoid arrays), and use a switch(int(vVert.texIndex)) statement to select which sampler you want to sample from. I do this in some of my shaders and it seems to work well across different GPUs on windows. However on macOS it does not work, and code needs to sample all of the samplers and then select which color to use after the fact.

Thanks for the report

The issue with setting a sampler array at index 0 in the GLSL MAT will be fixed in build 2017.2060 or later.

Thanks, Malcolm!

And I appreciate the heads up regarding Dynamically Uniform Expressions. :ugeek:

  • Landon Thomas