Does atomic functions on same memory location cause an performance issue? is it worse to do for each invocation an atomicAdd on the same memory location than doing for each invocation an atomicAdd to a unique memory location? (I know, atomicAdd does not make sense when each invocation writes to another location... but this is theoretically)
I am asking, because I wrote the marching cubes algorithm and while creating the mesh I additionally want to calculate the center of mass as well.
I am doing this by having four integers in the SSBO (centerX, centerY, centerZ and countTriangles)
each of these 4 integers will be increased by the atomicAdd function within the GeometryShader.
When everything is done, I only need to divide the centerX/Y/Z variable by countTriangles and I am done.
if these atomics on one memory location would slow down my shader, I would outsource it to another maybe CPU function.
Thanks in advance
Will having all invocations incrementing the same atomic counter be slower than not doing that? Sure. How much slower? That depends on many factors, but it's hardly going to be a crippling performance issue.
The whole point of atomic memory operations is that there is some hardware mechanism that allows them to work reasonably efficiently. Not as efficiently as bumping a local variable of course, but they shouldn't be assumed to cause a massive performance penalty.
On some hardware, atomic counter operations aren't even memory operations. AMD implements them as specialized hardware within their shader system, such that atomic counter operations don't touch actual memory storage until after the rendering process that uses them is complete.
Obviously, the best thing you can do is profile on your hardware-of-choice, but failing that, I would see no reason not to use this mechanism as you see fit.