1
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ I've had similar things cripple performance on CPUs so I would not bet on it not being a crippling performance issue. Especially as GPUs have so many more cores all fighting for the same memory location. With that said, you're right that the best thing to do is measure it. $\endgroup$ – Olivier Jul 18 at 0:13
  • 1
    $\begingroup$ @Olivier: And yet, there are many algorithms and rendering patterns that do this all the time. Linked-list-based order independent transparency is ultimately based on everyone bumping the same atomic counter to get unique locations for their list nodes. That seems to work adequately performance-wise. Also, atomic counters can be specialized hardware; on AMD platforms, they're something that lives inside the rendering context, not a memory location. I'm not saying that it's cheap, but it shouldn't be looked on as an a priori show-stopper. $\endgroup$ – Nicol Bolas Jul 19 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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