summaryrefslogtreecommitdiffhomepage
path: root/examples/shaders/resources
diff options
context:
space:
mode:
authorAstie Teddy <[email protected]>2021-10-31 11:46:38 +0100
committerGitHub <[email protected]>2021-10-31 11:46:38 +0100
commitf090f5444c274ce81bd2abcc51d18e516e352081 (patch)
tree4e89303995c941a6d481b1f51cfa703aab287679 /examples/shaders/resources
parent2bb4d36c43a8061ab2ede5729be23a6d2ceba2be (diff)
downloadraylib-f090f5444c274ce81bd2abcc51d18e516e352081.tar.gz
raylib-f090f5444c274ce81bd2abcc51d18e516e352081.zip
Add Conway's Game of Life compute shader example. (#2088)
* Add Conway's Game of Life compute shader example. * Fix various shaders problems, and tune command buffer size. * Various coding convention changes.
Diffstat (limited to 'examples/shaders/resources')
-rw-r--r--examples/shaders/resources/shaders/glsl430/gol.glsl64
-rw-r--r--examples/shaders/resources/shaders/glsl430/gol_render.glsl34
-rw-r--r--examples/shaders/resources/shaders/glsl430/gol_transfert.glsl54
3 files changed, 152 insertions, 0 deletions
diff --git a/examples/shaders/resources/shaders/glsl430/gol.glsl b/examples/shaders/resources/shaders/glsl430/gol.glsl
new file mode 100644
index 00000000..47b1a55f
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl430/gol.glsl
@@ -0,0 +1,64 @@
+// Game of Life logic shader
+#version 430
+
+#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 neighbour_count = 0;
+ uint x = gl_GlobalInvocationID.x;
+ uint y = gl_GlobalInvocationID.y;
+
+ // Top left
+ neighbour_count += fetchGol(x - 1, y - 1);
+
+ // Top middle
+ neighbour_count += fetchGol(x, y - 1);
+
+ // Top right
+ neighbour_count += fetchGol(x + 1, y - 1);
+
+ // Left
+ neighbour_count += fetchGol(x - 1, y);
+
+ // Right
+ neighbour_count += fetchGol(x + 1, y);
+
+ // Bottom left
+ neighbour_count += fetchGol(x - 1, y + 1);
+
+ // Bottom middle
+ neighbour_count += fetchGol(x, y + 1);
+
+ // Bottom right
+ neighbour_count += fetchGol(x + 1, y + 1);
+
+ if (neighbour_count == 3)
+ {
+ setGol(x, y, 1);
+ }
+ else if (neighbour_count == 2)
+ {
+ setGol(x, y, fetchGol(x, y));
+ }
+ else
+ {
+ setGol(x, y, 0);
+ }
+}
diff --git a/examples/shaders/resources/shaders/glsl430/gol_render.glsl b/examples/shaders/resources/shaders/glsl430/gol_render.glsl
new file mode 100644
index 00000000..bbc16eed
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl430/gol_render.glsl
@@ -0,0 +1,34 @@
+// Game of Life rendering shader
+// Just renders the content of the ssbo at binding 1 to screen.
+#version 430
+
+#define GOL_WIDTH 768
+
+// Input vertex attributes (from vertex shader)
+in vec2 fragTexCoord;
+
+// Output fragment color
+out vec4 finalColor;
+
+// Input game of life grid.
+layout(std430, binding = 1) readonly buffer golLayout
+{
+ uint golBuffer[];
+};
+
+// Output resolution
+uniform vec2 res;
+
+void main()
+{
+ ivec2 coords = ivec2(fragTexCoord * res);
+
+ if (golBuffer[coords.x + coords.y * uvec2(res).x] == 1)
+ {
+ finalColor = vec4(1.0);
+ }
+ else
+ {
+ finalColor = vec4(0.0, 0.0, 0.0, 1.0);
+ }
+}
diff --git a/examples/shaders/resources/shaders/glsl430/gol_transfert.glsl b/examples/shaders/resources/shaders/glsl430/gol_transfert.glsl
new file mode 100644
index 00000000..40d54458
--- /dev/null
+++ b/examples/shaders/resources/shaders/glsl430/gol_transfert.glsl
@@ -0,0 +1,54 @@
+// Game of life transfert shader.
+#version 430
+#define GOL_WIDTH 768
+
+// Structure definitions
+struct GolUpdateCmd {
+ uint x; // x coordinate of the gol command
+ uint y; // y coordinate of the gol command
+ uint w; // width of the filled zone
+ uint enabled; // whether to enable or disable zone
+};
+
+// Local compute unit size.
+layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+
+// Output game of life grid buffer.
+layout(std430, binding = 1) buffer golBufferLayout
+{
+ uint golBuffer[]; // golBuffer[x, y] = golBuffer[x + GOL_WIDTH * y]
+};
+
+// Command buffer
+layout(std430, binding = 3) readonly restrict buffer golUpdateLayout
+{
+ uint count;
+ GolUpdateCmd commands[];
+};
+
+#define isInside(x, y) (((x) >= 0) && ((y) >= 0) && ((x) < GOL_WIDTH) && ((y) < GOL_WIDTH))
+#define getBufferIndex(x, y) ((x) + GOL_WIDTH * (y))
+
+void main()
+{
+ uint cmd_index = gl_GlobalInvocationID.x;
+ GolUpdateCmd cmd = commands[cmd_index];
+
+ for (uint x = cmd.x; x < (cmd.x + cmd.w); x++)
+ {
+ for (uint y = cmd.y; y < (cmd.y + cmd.w); y++)
+ {
+ if (isInside(x, y))
+ {
+ if (cmd.enabled != 0)
+ {
+ atomicOr(golBuffer[getBufferIndex(x, y)], 1);
+ }
+ else
+ {
+ atomicAnd(golBuffer[getBufferIndex(x, y)], 0);
+ }
+ }
+ }
+ }
+}