summaryrefslogtreecommitdiffhomepage
path: root/examples/others
diff options
context:
space:
mode:
authorRay <[email protected]>2021-10-31 12:28:04 +0100
committerRay <[email protected]>2021-10-31 12:28:04 +0100
commit1fac09d0f4d409d33c0fe529b46bc8132b18ccd9 (patch)
tree9d71b687118b39c0880e0772da50ef22c4582360 /examples/others
parentf090f5444c274ce81bd2abcc51d18e516e352081 (diff)
downloadraylib-1fac09d0f4d409d33c0fe529b46bc8132b18ccd9.tar.gz
raylib-1fac09d0f4d409d33c0fe529b46bc8132b18ccd9.zip
REVIEWED: example: Compute shader Game-of-life
Diffstat (limited to 'examples/others')
-rw-r--r--examples/others/resources/shaders/glsl430/gol.glsl41
-rw-r--r--examples/others/resources/shaders/glsl430/gol_render.glsl29
-rw-r--r--examples/others/resources/shaders/glsl430/gol_transfert.glsl51
-rw-r--r--examples/others/rlgl_compute_shader.c173
4 files changed, 294 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);
+}
diff --git a/examples/others/resources/shaders/glsl430/gol_render.glsl b/examples/others/resources/shaders/glsl430/gol_render.glsl
new file mode 100644
index 00000000..97a1e99e
--- /dev/null
+++ b/examples/others/resources/shaders/glsl430/gol_render.glsl
@@ -0,0 +1,29 @@
+#version 430
+
+// Game of Life rendering shader
+// Just renders the content of the ssbo at binding 1 to screen
+
+#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 resolution;
+
+void main()
+{
+ ivec2 coords = ivec2(fragTexCoord*resolution);
+
+ if ((golBuffer[coords.x + coords.y*uvec2(resolution).x]) == 1) finalColor = vec4(1.0);
+ else finalColor = vec4(0.0, 0.0, 0.0, 1.0);
+}
diff --git a/examples/others/resources/shaders/glsl430/gol_transfert.glsl b/examples/others/resources/shaders/glsl430/gol_transfert.glsl
new file mode 100644
index 00000000..a202338b
--- /dev/null
+++ b/examples/others/resources/shaders/glsl430/gol_transfert.glsl
@@ -0,0 +1,51 @@
+#version 430
+
+// Game of life transfert shader
+
+#define GOL_WIDTH 768
+
+// Game Of Life Update Command
+// NOTE: matches the structure defined on main program
+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 cmdIndex = gl_GlobalInvocationID.x;
+ GolUpdateCmd cmd = commands[cmdIndex];
+
+ 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);
+ }
+ }
+ }
+}
diff --git a/examples/others/rlgl_compute_shader.c b/examples/others/rlgl_compute_shader.c
new file mode 100644
index 00000000..abc58934
--- /dev/null
+++ b/examples/others/rlgl_compute_shader.c
@@ -0,0 +1,173 @@
+/*******************************************************************************************
+*
+* raylib [rlgl] example - compute shader - Conway's Game of Life
+*
+* NOTE: This example requires raylib OpenGL 4.3 versions for compute shaders support,
+* shaders used in this example are #version 430 (OpenGL 4.3)
+*
+* This example has been created using raylib 4.0 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Example contributed by Teddy Astie (@tsnake41) and reviewed by Ramon Santamaria (@raysan5)
+*
+* Copyright (c) 2021 Teddy Astie (@tsnake41)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+#include "rlgl.h"
+
+#include <stdlib.h>
+
+// IMPORTANT: This must match gol*.glsl GOL_WIDTH constant.
+// This must be a multiple of 16 (check golLogic compute dispatch).
+#define GOL_WIDTH 768
+
+// Maximum amount of queued draw commands (squares draw from mouse down events).
+#define MAX_BUFFERED_TRANSFERTS 48
+
+// Game Of Life Update Command
+typedef struct GolUpdateCmd {
+ unsigned int x; // x coordinate of the gol command
+ unsigned int y; // y coordinate of the gol command
+ unsigned int w; // width of the filled zone
+ unsigned int enabled; // whether to enable or disable zone
+} GolUpdateCmd;
+
+// Game Of Life Update Commands SSBO
+typedef struct GolUpdateSSBO {
+ unsigned int count;
+ GolUpdateCmd commands[MAX_BUFFERED_TRANSFERTS];
+} GolUpdateSSBO;
+
+int main(void)
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ InitWindow(GOL_WIDTH, GOL_WIDTH, "raylib [rlgl] example - compute shader - game of life");
+
+ const Vector2 resolution = { GOL_WIDTH, GOL_WIDTH };
+ unsigned int brushSize = 8;
+
+ // Game of Life logic compute shader
+ char *golLogicCode = LoadFileText("resources/shaders/glsl430/gol.glsl");
+ unsigned int golLogicShader = rlCompileShader(golLogicCode, RL_COMPUTE_SHADER);
+ unsigned int golLogicProgram = rlLoadComputeShaderProgram(golLogicShader);
+ UnloadFileText(golLogicCode);
+
+ // Game of Life logic compute shader
+ Shader golRenderShader = LoadShader(NULL, "resources/shaders/glsl430/gol_render.glsl");
+ int resUniformLoc = GetShaderLocation(golRenderShader, "resolution");
+
+ // Game of Life transfert shader
+ char *golTransfertCode = LoadFileText("resources/shaders/glsl430/gol_transfert.glsl");
+ unsigned int golTransfertShader = rlCompileShader(golTransfertCode, RL_COMPUTE_SHADER);
+ unsigned int golTransfertProgram = rlLoadComputeShaderProgram(golTransfertShader);
+ UnloadFileText(golTransfertCode);
+
+ // SSBOs
+ unsigned int ssboA = rlLoadShaderBuffer(GOL_WIDTH*GOL_WIDTH*sizeof(unsigned int), NULL, RL_DYNAMIC_COPY);
+ unsigned int ssboB = rlLoadShaderBuffer(GOL_WIDTH*GOL_WIDTH*sizeof(unsigned int), NULL, RL_DYNAMIC_COPY);
+
+ struct GolUpdateSSBO transfertBuffer;
+ transfertBuffer.count = 0;
+
+ int transfertSSBO = rlLoadShaderBuffer(sizeof(struct GolUpdateSSBO), NULL, RL_DYNAMIC_COPY);
+
+ // Create a white texture of the size of the window to update
+ // each pixel of the window using the fragment shader
+ Image whiteImage = GenImageColor(GOL_WIDTH, GOL_WIDTH, WHITE);
+ Texture whiteTex = LoadTextureFromImage(whiteImage);
+ UnloadImage(whiteImage);
+ //--------------------------------------------------------------------------------------
+
+ // Main game loop
+ while (!WindowShouldClose())
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+ brushSize += (int)GetMouseWheelMove();
+
+ if ((IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
+ && (transfertBuffer.count < MAX_BUFFERED_TRANSFERTS))
+ {
+ // Buffer a new command
+ transfertBuffer.commands[transfertBuffer.count].x = GetMouseX() - brushSize/2;
+ transfertBuffer.commands[transfertBuffer.count].y = GetMouseY() - brushSize/2;
+ transfertBuffer.commands[transfertBuffer.count].w = brushSize;
+ transfertBuffer.commands[transfertBuffer.count].enabled = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
+ transfertBuffer.count++;
+ }
+ else if (transfertBuffer.count > 0)
+ {
+ // Process transfert buffer
+
+ // Send SSBO buffer to GPU
+ rlUpdateShaderBufferElements(transfertSSBO, &transfertBuffer, sizeof(struct GolUpdateSSBO), 0);
+
+ // Process ssbo command
+ rlEnableShader(golTransfertProgram);
+ rlBindShaderBuffer(ssboA, 1);
+ rlBindShaderBuffer(transfertSSBO, 3);
+ rlComputeShaderDispatch(transfertBuffer.count, 1, 1); // each GPU unit will process a command
+ rlDisableShader();
+
+ transfertBuffer.count = 0;
+ }
+ else
+ {
+ // Process game of life logic
+ rlEnableShader(golLogicProgram);
+ rlBindShaderBuffer(ssboA, 1);
+ rlBindShaderBuffer(ssboB, 2);
+ rlComputeShaderDispatch(GOL_WIDTH/16, GOL_WIDTH/16, 1);
+ rlDisableShader();
+
+ // ssboA <-> ssboB
+ int temp = ssboA;
+ ssboA = ssboB;
+ ssboB = temp;
+ }
+
+ rlBindShaderBuffer(ssboA, 1);
+ SetShaderValue(golRenderShader, resUniformLoc, &resolution, SHADER_UNIFORM_VEC2);
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(BLANK);
+
+ BeginShaderMode(golRenderShader);
+ DrawTexture(whiteTex, 0, 0, WHITE);
+ EndShaderMode();
+
+ DrawRectangleLines(GetMouseX() - brushSize/2, GetMouseY() - brushSize/2, brushSize, brushSize, RED);
+
+ DrawText("Use Mouse wheel to increase/decrease brush size", 10, 10, 20, WHITE);
+ DrawFPS(GetScreenWidth() - 100, 10);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ // Unload shader buffers objects.
+ rlUnloadShaderBuffer(ssboA);
+ rlUnloadShaderBuffer(ssboB);
+ rlUnloadShaderBuffer(transfertSSBO);
+
+ // Unload compute shader programs
+ rlUnloadShaderProgram(golTransfertProgram);
+ rlUnloadShaderProgram(golLogicProgram);
+
+ UnloadTexture(whiteTex); // Unload white texture
+ UnloadShader(golRenderShader); // Unload rendering fragment shader
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}