2 years ago

#65781

test-img

Karly

Unexpected results from compute shader after call to dispatch and glMemoryBarrier

I am trying to set up a compute shader (first time I do this). And would like to see some simple results, computed by the shader. To do this I have a uniform imageBuffer, which the shader code reads from, using imageLoad. Then it increments the value in the shader by 1. Finally it stores the changed value via imageStore.

Then, to verify the results, in the c++ side I map the buffer to a local array using glMapBufferRange and print what's inside.

With this setup, I would expect all fields of the buffer to have been incremented by 1, per invocation of the compute shader.

But I observe the increment only for each third invocation of the shader. For the other two I observe the old value from the prior iteration.

For glMemoryBarrier I have attempted to use

GL_SHADER_IMAGE_ACCESS_BARRIER_BIT
GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT
GL_ALL_BARRIER_BITS

But all options show the same wrong result.

Is there anything obvious that I'm missing? I am dimly aware that the GLSL functions imageLoad/Store operate 'memory-incoherently', but shouldn't the CPU-side call to glMemoryBarrier after the dispatch take care of making all increments visible?

i.e. the output for a specific buffer index, looks like this:

val: 0
val: 0
val: 1

val: 1
val: 1
val: 2

val: 2
val: 2
val: 3
...

my compute shader looks like this

#version 430 core

layout (local_size_x = 1024) in;
layout (rgba32f, binding = 0) uniform imageBuffer position_buffer;

void main(void)
{
  int index = int(gl_GlobalInvocationID.x);

  vec4 u_curr_vec = imageLoad(position_buffer, index);

  barrier();
  
  u_curr_vec.x += 1;
    
  imageStore(position_buffer, index, u_curr_vec);
}

The buffer image is instantiated like so:

glGenTextures(1, tbo_name_union_instance.tbos);
glBindTexture(GL_TEXTURE_BUFFER, tbo_name_union_instance.tbos[0]);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, buffer_name_union_instance.buffers[0]);

and then, after shader activation, it is bound:

glBindImageTexture(0, tbo_name_union_instance.tbo_pair_name.position_tbo, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);

c++

opengl

glsl

compute-shader

0 Answers

Your Answer

Accepted video resources