Source browser
examples/shaders/sum.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// A small deterministic reduction: one invocation reads the preceding stage.
2// For large buffers, split the reduction into several independently run groups.
3#ifdef __cplusplus // Expose host declarations only when a C++ compiler reads this shader.
4#include "shdr.h" // Include shdr.h declarations.
5#endif // Finish this conditional compilation branch.
6
7layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; // Group 1 independent invocations along x.
8layout(std430, binding = 0) readonly buffer Storage_input_values { // Read the preceding stage's input elements through buffer binding 0.
9 float input_values[]; // Store the preceding stage's input elements.
10}; // Finish this declaration.
11layout(std430, binding = 1) writeonly buffer Storage_output_values { // Write this stage's output elements through buffer binding 1.
12 float output_values[]; // Store this stage's output elements.
13}; // Finish this declaration.
14uniform uint count; // Limit work to the valid element count, including a partial final workgroup.
15
16void main() { // Reduce the entire input into one deterministic sum.
17 float result = 0.0; // Start the ordered accumulation at zero.
18 for (uint i = 0u; i < count; ++i) { // Visit every input in the same order on both backends.
19 result += input_values[i]; // Add this input element to the running total.
20 } // End this block.
21 output_values[0] = result; // Publish the one-element result for the following stage or host.
22} // End this block.