Difference between revisions of "Atomic Counters"

From TouchDesigner Documentation
Jump to: navigation, search
(Created page with "TBD")
 
(Tag: 2018.28070)
 
Line 1: Line 1:
TBD
+
Atomic counters are buffer objects in OpenGL that can have atomic operations performed on them. They are of a separate variable type called <code>atomic_uint</code> (atomic unsigned integer). There are a number of atomic operations that can be applied on them, namely <code>atomicCounterIncrement</code> and <code>atomicCounterDecrement</code>.
 +
 
 +
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);
 +
  }

Latest revision as of 10:46, 8 March 2019

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);
 }