diff options
| author | Ray <[email protected]> | 2021-10-31 12:28:04 +0100 |
|---|---|---|
| committer | Ray <[email protected]> | 2021-10-31 12:28:04 +0100 |
| commit | 1fac09d0f4d409d33c0fe529b46bc8132b18ccd9 (patch) | |
| tree | 9d71b687118b39c0880e0772da50ef22c4582360 /examples/others/resources/shaders/glsl430/gol.glsl | |
| parent | f090f5444c274ce81bd2abcc51d18e516e352081 (diff) | |
| download | raylib-1fac09d0f4d409d33c0fe529b46bc8132b18ccd9.tar.gz raylib-1fac09d0f4d409d33c0fe529b46bc8132b18ccd9.zip | |
REVIEWED: example: Compute shader Game-of-life
Diffstat (limited to 'examples/others/resources/shaders/glsl430/gol.glsl')
| -rw-r--r-- | examples/others/resources/shaders/glsl430/gol.glsl | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/others/resources/shaders/glsl430/gol.glsl b/examples/others/resources/shaders/glsl430/gol.glsl new file mode 100644 index 00000000..c5dfe06b --- /dev/null +++ b/examples/others/resources/shaders/glsl430/gol.glsl @@ -0,0 +1,41 @@ +#version 430 + +// Game of Life logic shader + +#define GOL_WIDTH 768 + +layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in; + +layout(std430, binding = 1) readonly restrict buffer golLayout { + uint golBuffer[]; // golBuffer[x, y] = golBuffer[x + gl_NumWorkGroups.x * y] +}; + +layout(std430, binding = 2) writeonly restrict buffer golLayout2 { + uint golBufferDest[]; // golBufferDest[x, y] = golBufferDest[x + gl_NumWorkGroups.x * y] +}; + +#define fetchGol(x, y) ((((x) < 0) || ((y) < 0) || ((x) > GOL_WIDTH) || ((y) > GOL_WIDTH)) \ + ? (0) \ + : golBuffer[(x) + GOL_WIDTH * (y)]) + +#define setGol(x, y, value) golBufferDest[(x) + GOL_WIDTH*(y)] = value + +void main() +{ + uint neighbourCount = 0; + uint x = gl_GlobalInvocationID.x; + uint y = gl_GlobalInvocationID.y; + + neighbourCount += fetchGol(x - 1, y - 1); // Top left + neighbourCount += fetchGol(x, y - 1); // Top middle + neighbourCount += fetchGol(x + 1, y - 1); // Top right + neighbourCount += fetchGol(x - 1, y); // Left + neighbourCount += fetchGol(x + 1, y); // Right + neighbourCount += fetchGol(x - 1, y + 1); // Bottom left + neighbourCount += fetchGol(x, y + 1); // Bottom middle + neighbourCount += fetchGol(x + 1, y + 1); // Bottom right + + if (neighbourCount == 3) setGol(x, y, 1); + else if (neighbourCount == 2) setGol(x, y, fetchGol(x, y)); + else setGol(x, y, 0); +} |
