Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I originally asked this on plain old stack but it was closed for being too open-ended. (Which was a good call as this is a better place for such questions.)

Original Link: http://stackoverflow.com/questions/15132135/opengl-image-load-store-atomics-applications

So, I've been spending the last couple months or so getting familiarized with the latest OpenGL 4 features when I find myself with free time. (Things like transform feedback, tessellation, compute shaders, etc..) I've finally taken time to mess around with using Image Load-Store textures, and while the idea of being able to atomically read/write to any texture from any GPU core regardless of shader stage seems incredibly powerful, I could not think of interesting applications utilizing the unique facilities these textures provide. Even after consulting "the google" for interesting applications, I was left empty handed. So, are there any WOW-THATS-COOL or even DAMN-THATS-HANDY applications you can think of?

Application 1 (Suggested By Nicol Bolas): "Search for order-independent transparency. That requires atomic image operations."

share|improve this question
add comment

1 Answer

As far as I can tell, you got it somewhat wrong. You're looking in the wrong direction, cool and handy stuff here is in code, not in applications.

This is explained in OpenGL wiki, although one needs to apply certain effort to to get what's so cool about it:

Consider a shader that reads from a location, adds 1 to it, and then writes to it. It is theoretically possible for two such shaders to read from and and write to the same location in the same image at the same time. Because of the way memory accesses are handled, it is entirely possible that this sequence of events works like this:

  • Shader A reads the image value, say 0.
  • Shader B reads the image value, also 0.
  • Shader B adds 1 to its local value of 0, becoming 1.
  • Shader B writes its local value to the image. The image now has 1.
  • Shader A adds 1 to its local value of 0, becoming 1.
  • Shader A writes its local value to the image. The image now has 1.

Do you see what's wrong with above? Look, image value is intended to go 0 then 1 then 2 but it goes only 1: this means you'd get light-gray where you intended to be dark-gray or maybe you'll get green if you're really unlucky - this is indeterministic behavior.


Well, you won't see bad stuff like that in real applications even before atomics were introduced, but what did that cost to programmer? Just think about it, to avoid side effects, programmer would have to manually synchronize shaders, so that their code was looking like that:

  • enter synchronized section
    • Shader A reads the image value, say 0.
    • Shader A adds 1 to its local value of 0, becoming 1.
    • Shader A writes its local value to the image. The image now has 1.
  • quit synchronized section
  • enter synchronized section
    • Shader B reads the image value, it is guaranteed to be 1 now, how nice
      (if we forget that coder had to write 3 extra statements to get it right).
    • Shader B adds 1 to its local value of 1, becoming 2.
    • Shader B writes its local value to the image. The image now has 2.
  • quit synchronized section

Now, as wiki state,

Atomic operations prevent this possibility entirely. Each shader's independent atomic operation will fully complete before the next one starts.

What would that mean to coder? Let's see, we still want our two shaders to add that 1 twice so that 0 becomes 2. Okie dokie, here we go...

  • Shader A reads-modifies-writes the image value in one step, 0 is guaranteed to become 1.
  • Shader B reads-modifies-writes the image value in one step, 1 is guaranteed to become 2.

...and that's all. That's it. That's it!!! We've got to two statements (that are quite easy to read btw) from 10 that mixed rendering and synchronization in not particularly apparent way.

Have you ever been through code changes like this? I've been and, well, this is one of the coolest, handiest, sexiest experiences one can ever imagine.

Oh applications... you asked about these, right? Well, applications will look and behave the same. There will only be 5 times less code there, if that matters to you.

share|improve this answer
    
Very good explanation of atomics! –  Sir Digby Chicken Caesar Mar 2 '13 at 1:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.