Source browser
examples/shaders/blur.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 3x3 image filter with explicit edge clamping, identical on CPU and GPU.
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.
5
6layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; // Cover a 8 by 8 group of output coordinates.
7layout(r32f, binding = 0) readonly uniform highp image2D input_image; // Read the r32f image at this image binding.
8layout(r32f, binding = 1) writeonly uniform highp image2D output_image; // Write the r32f image at this image binding.
9
10void main() { // Average the clamped three-by-three neighborhood of one image pixel.
11 ivec2 position = ivec2(gl_GlobalInvocationID.xy); // Address the pixel assigned to this two-dimensional invocation.
12 ivec2 dimensions = imageSize(input_image); // Read the bound image extent before accessing its pixels.
13 if (position.x >= dimensions.x || position.y >= dimensions.y) { // Guard invocations outside the image in the partial final tile.
14 return; // Stop this invocation before performing another resource access.
15 } // End this block.
16 float result = 0.0; // Start the nine-sample sum at zero.
17 for (int y = -1; y <= 1; ++y) { // Visit the three neighboring rows.
18 for (int x = -1; x <= 1; ++x) { // Visit the three neighboring columns.
19 ivec2 sample_position = ivec2(clamp(position.x + x, 0, dimensions.x - 1), // Clamp the horizontal neighbor to the nearest valid image edge.
20 clamp(position.y + y, 0, dimensions.y - 1)); // Clamp the vertical neighbor to the nearest valid image edge.
21 result += imageLoad(input_image, sample_position).x; // Accumulate the red component of this clamped scalar image sample.
22 } // End this block.
23 } // End this block.
24 imageStore(output_image, position, vec4(result / 9.0, 0.0, 0.0, 1.0)); // Divide the sum by nine and store the averaged scalar pixel.
25} // End this block.