Jump to content
  • Advertisement
Sign in to follow this  

Compute shader atomic counter help with barriers

This topic is 1007 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a compute shader that reads an image. For simplicity here, I count all black pixels with an atomic counter, then divide that count by the texture resolution. That result is the output image.

The code:

#version 430

#extension GL_ARB_compute_shader : enable
#extension GL_ARB_compute_variable_group_size : enable

layout (binding = 0, rgba8) readonly uniform image2D intex;
layout (binding = 1, rgba8) writeonly uniform image2D outtex;

layout(binding = 2, offset = 0) uniform atomic_uint a_count;

layout(local_size_variable) in;

const float num_fragments = 1024.0 * 1024.0;

void main()
    ivec2 uv = ivec2(gl_GlobalInvocationID.xy);

    vec4 color = imageLoad(intex, uv);

    if(color.r == 0.0)


    uint count = atomicCounter(a_count);

    float c = float(count);

    color = vec4(c / num_fragments);

    imageStore(outtex, uv, color);

When this executes, the output image is black at the bottom and light at the top (like printing out a y texture coordinate from 0 to 1)

I thought the 'barrier' would wait until all invocations have completed and then I could continue with the correct count.


So obviously my understanding of 'barrier' is wrong.


If I just do a count in the compute shader, then pass the count to a fragment shader where I divide the count by the number of fragments, the output is correct.


I was hoping to do this in one pass. How do I correctly implement a barrier to get the correct count in one pass? (If that is possible)





Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!