opengl-escompute-shader3d-texture

OpenGL mixes colors for GL_TEXTURE_3D in compute shader


I am not sure whether it is a driver bug but following happens:

compute-shader.glsl:

#version 320 es
precision mediump float;

layout(local_size_x = 16, local_size_y = 8, local_size_z = 1) in;
layout(rgba8ui, binding = 0) writeonly uniform mediump uimage2D image;
layout(binding = 1) uniform mediump usampler3D grid;

void main() {
    uint i = gl_GlobalInvocationID.y;
    uint j = gl_GlobalInvocationID.x;

    uvec4 val = texture(grid, vec3(float(i)/480.0,float(j)/640.0,0.5));

    imageStore(image, ivec2(j,i), val);
}

The color channels of val are completely scrambled:

main.cpp:

    glActiveTexture(GL_TEXTURE1);
    glEnable(GL_TEXTURE_3D);
    glBindTexture(GL_TEXTURE_3D, grid_tex_id);
    glTexStorage3D(GL_TEXTURE_3D, 1, GL_RGBA8, 256, 256, 256);
    glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, 256, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, grid.data);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

Is there any reasonable explanation for this? Note: I do not want to use the hack val.ragb (if that even exists).


Solution

  • layout(binding = 1) uniform mediump usampler3D grid;
    ...
    glTexStorage3D(GL_TEXTURE_3D, 1, GL_RGBA8, 256, 256, 256);
    

    GL_RGBA8 is not an integer format; it's a normalized format. That means it is equivalent to a floating-point format. So you should use sampler3D, not usampler3D. Or you should upload your data to an integer format like GL_RGBA8UI, using the GL_RGBA_INTEGER pixel transfer format.

    You probably got away with it using Image Load/Store by re-specifying the format in the shader itself. You can't do that with textures.