1
votes

I'm trying to build a compute shader to create a noise texture to use as a height map. gl_GlobalInvocationID is always (0, 0) from my debugging, and I'm not sure why. The good news is that something is being written inside of the compute shader, since I am able to retrieve an all-red texture, but I cannot write more than a solid color. I tried moving around the image binding as well as the dispatching command of the compute shader, but I haven't had any luck so far.

Not pictured is my shader program initializations, but no errors are being generated from compilation or linking, so I don't think the issue lies there. I believe that the image and texture formats are consistent and correct as well.

Thank you for any insight you could provide to this issue!

Compute Shader:

#version 430
layout(local_size_x = 1, local_size_y = 1) in;
layout(rgba32f, binding = 0) uniform image2D noiseImage;
void main() {
    vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
    ivec2 pixel = ivec2(gl_GlobalInvocationID.xy);
    // Debugging
    if (pixel.x == 0 && pixel.y == 0) {
        color = vec4(1.0, 0.0, 0.0, 1.0);
    } else {
        color = vec4(0.0, 1.0, 0.0, 1.0);
    }

    imageStore(noiseImage, pixel, color);
}

OpenGL Code:

width = 512;
height = 512;
noiseTexture = 0;

// Snipped (runs once)
glGenTextures(1, &noiseTexture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, noiseTexture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
glBindImageTexture(0, noiseTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);

// Snipped (runs every frame)
glUseProgram(computeShader);
glDispatchCompute(width, height, 1);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, noiseTexture);

glUseProgram(vertex/fragment);
render();
1

1 Answers

0
votes

Hmm, so you're rendering the resulting image afterwards? Do you perhaps need the GL_TEXTURE_FETCH_BARRIER_BIT barrier too, so that the results of the compute shader rendering are visible to the texture fetch?