Close

Atomic Counters

Atomic counters are buffer objects in OpenGL that can have atomic operations performed on them. They are of a separate variable type called atomic_uint (atomic unsigned integer). There are a number of atomic operations that can be applied on them, namely atomicCounterIncrement and atomicCounterDecrement.

For more info and a full list of available functions see: https://www.khronos.org/opengl/wiki/Atomic_Counter

Atomic Counters are available in TouchDesigner through the GLSL TOP. See also: Write a GLSL TOP. They can be used in the shaders at any stage of the pipeline (vertex, fragment/pixel, compute) and can be used to track all sorts of things such as number of vertices, number of red pixels, and more.

Below is a simple example of a pixel shader where the atomic counter is incremented each time (ie. once per pixel), converted to a float, and then put into the red channel. Visually, pixels that are rendered first will be a darker red than those rendered last.

 layout(binding = 0, offset = 0) uniform atomic_uint ac;
 out vec4 fragColor;
 void main()
 {
    uint c = atomicCounterIncrement(ac);
    float r = (c/255)/255.f;
    fragColor = vec4(r,0,0,1);
 }