Schnigges Schnigges - 2 months ago 28
C++ Question

OpenGL: isampler2DArray vs. sampler2DArray vs. sampler3DArray

I'm currently facing an issue with OpenGL samplers. I have a running fragment shader which uses a

isampler2DArray
variable to sample a stack of 2D images and an integer index which is used to retrieve the desired slice from the stack.

The fragment shader looks like this:

#version 430

/* Per Fragment Input Attributes */
in vec2 texCoord;

/* Per Fragment Uniform Attributes */
layout (binding = 0) uniform isampler2DArray textureSampler;

/* Per Fragment Output Values */
layout (location = 0) out vec3 out_color;

uniform float windowCenter;
uniform float windowWidth;

uniform int texIndex;

void main()
{
float value = float(texture(textureSampler, vec3(texCoord, texIndex)).r);

float min = windowCenter - windowWidth / 2;

value = (value - min) / windowWidth;

out_color = vec3(value);
}


When I tried to change the sampler to
sampler2DArray
and the uniform variable to retrieve the data to
float
, I only get a black screen instead of the sampled texture. Similar for a
sampler3D
.

The texture setup code looks as follows:

m_texture= std::make_unique<Texture>(GL_TEXTURE_2D_ARRAY, nullptr, sequence->width(), sequence->height(), sequence->images().size(), GL_R16I, GL_RED_INTEGER, 1, 0, GL_SHORT);

glPixelStorei(GL_UNPACK_ROW_LENGTH, sequence->width());

for (int i = 0; i < sequence->images().size(); i++)
{
m_texture->UpdateTexture(sequence->images()[i]->data(), 0, sequence->images()[i]->width(), 0, sequence->images()[i]->height(), i, 1);
}

glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);


For
sampler2DArray
and
sampler3D
, I uploaded my
float
uniform the following way, according to this stackoverflow question (OpenGL 3D Texture Coordinates):

prg_texture3D->SetUniform("currentSliceTexCoord", (1.0f + 2.0f * currentSlice) / (2.0f * sequence->images().size()));


I'm puzzled why the program runs with the
isampler2DArray
and not with the other two. Can you please make clear to me the difference between them, especially between
isampler2DArray
and
sampler2DArray
?

Answer
GL_R16I, GL_RED_INTEGER

If you use non-integer samplers, then your texture's image format must match that. It must be a floating point or normalized integer format. And GL_R16I is an integer format, not a float/normalized format.