0

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?

2
  • Where does the texture class come from? Did you set the min/mag filters correctly for the float textures? Commented Sep 27, 2016 at 16:39
  • It's a custom one I created myself. Both values are set to GL_LINEAR. Commented Sep 27, 2016 at 16:42

1 Answer 1

1
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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, my data is 12 bit DICOM data, is there some way to convert it to float in a shader?
@Schnigges: What's wrong with your current method of casting it to a float?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.