Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7651159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:26:17+00:00 2026-05-31T11:26:17+00:00

A fragment shader uses two atomic counters. It may or may not increment the

  • 0

A fragment shader uses two atomic counters. It may or may not increment the first and may or may not increment the second (but never both). Before so modifying the counters, however, their current values are always read and –if the counters are then later modified– those previously read values used for some custom logic. All this happens in a (most likely unrollable) loop.

Envision a flow roughly like this:

  • in some small unrollable loop, say FOR 0-20 (compile-time resolvable const)…
  • get counter values for AC1 and AC2
  • check some value:
  • if x: set texel in uimage1D_A at index AC1, increment AC1
  • else: set texel in uimage1D_B at index (imgwidth-AC2-1), increment AC2

Question: the shader queries the current counter value — does it always get the “most current” value? Do I lose the massive parallelism of fragment shaders here (speaking in terms of of current-generation and future GPUs and drivers only)?

As for the branching (if x) — I compare a texel in another (readonly restrict uniform) uimage1D to a (uniform) uint. So one operand is definitely a uniform scalar, but the other is an imageLoad().x although the image is uniform — is this sort of branching still “fully parallelized”? You can see both branches are each exactly two, almost identical instructions. Assuming a “perfectly optimizing” GLSL compiler, is this kind of branching likely introducing a stall?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:26:18+00:00Added an answer on May 31, 2026 at 11:26 am

    Atomic counters are atomic. But each atomic operation is atomic only for that operation.

    So, if you want to ensure that every shader gets a unique value from a counter, then every shader must access that counter only with atomicCounterIncrement (or Decrement, but they must all use the same one).

    The correct way to do what you’re suggesting is:

    1. check some value:
    2. if x:
      1. atomicCounterIncrement(AC1), storing the value returned.
      2. Use the stored value as the texel at which to set something into uimage1D_A.
    3. else:
      1. atomicCounterIncrement(AC2), storing the value returned.
      2. Use the stored value to compute the texel (imgwidth – val – 1) at which to set something into uimage1D_B.

    Your “fetch and later increment” strategy is a race condition waiting to happen. It doesn’t matter if it’s “fully parallelized” because it’s broken. You need it to work before wondering if it’s going to be fast.

    I would strongly advise getting familiar with atomics and threading on CPUs before trying to tackle GPU stuff. This is a common mistake made by novices when working with atomics. You need to be a threading expert (or at least intermediate-level) if you want to use successfully GLSL atomics and image load/store.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two textures generated using a fragment shader. I want to be able
In a Qt based application I want to execute a fragment shader on two
I'm doing ray casting in the fragment shader. I can think of a couple
I have a HTML fragment which contains two anchor tags in various parts of
I'm trying to create a wireframe vertex/fragment shader in Unity. It seems possible according
I am working on a project writing a GLSL fragment shader, and it just
If I have a few Program3D objects (each with it's own Vertex-Shader and Fragment-Shader
I'm trying to write a simple geometry shader what just passes through vertices before
I've got a fragment shader that essentially reads the color alpha and translates it
I have a very basic fragment shader which I want to output 'gl_PrimitiveID' to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.