Source browser

examples/shaders/rle_count.cpp

Read the complete file below or download its original bytes. Line numbers provide stable links to a declaration or processing step.

File fingerprint
1// Count compact (value,run length) pairs independently within each fixed-size tile.
2#ifdef __cplusplus // Expose host declarations only when a C++ compiler reads this shader.
3#include "shdr.h" // Include shdr.h declarations.
4#endif // Finish this conditional compilation branch.
5layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in; // Group 64 independent invocations along x.
6layout(std430, binding = 0) readonly buffer Values { // Read the original unsigned values to encode through buffer binding 0.
7    uint values[]; // Store the original unsigned values to encode.
8}; // Finish this declaration.
9layout(std430, binding = 1) writeonly buffer Sizes { // Write the size of each independently processed tile through buffer binding 1.
10    uint sizes[]; // Store the size of each independently processed tile.
11}; // Finish this declaration.
12uniform uint count; // Limit work to the valid element count, including a partial final workgroup.
13uniform uint block_size; // Limit each independent encoder or decoder to this many original values.
14void main() { // Count the encoded payload size of one independent input tile.
15    uint block = gl_GlobalInvocationID.x; // Assign one independent input tile to this invocation.
16    uint begin = block * block_size; // Locate the first original value in this tile.
17    if (begin >= count) // Ignore workgroup padding after the final tile.
18        return; // Stop this invocation before performing another resource access.
19    uint end = min(begin + block_size, count); // Stop the last tile at the actual input length.
20    uint runs = 1u; // Count the first value as the start of one run.
21    for (uint i = begin + 1u; i < end; ++i) // Inspect the remaining values within this tile.
22        if (values[i] != values[i - 1u]) // Start another run whenever the original value changes.
23            ++runs; // Count this transition as one additional encoded pair.
24    sizes[block] = runs * 2u; // Reserve two payload words for each value and run-length pair.
25} // End this block.