Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

174 řádky
7.2 KiB

před 11 měsíci
  1. /*******************************************************************************************
  2. *
  3. * raylib [rlgl] example - compute shader - Conway's Game of Life
  4. *
  5. * NOTE: This example requires raylib OpenGL 4.3 versions for compute shaders support,
  6. * shaders used in this example are #version 430 (OpenGL 4.3)
  7. *
  8. * Example originally created with raylib 4.0, last time updated with raylib 2.5
  9. *
  10. * Example contributed by Teddy Astie (@tsnake41) and reviewed by Ramon Santamaria (@raysan5)
  11. *
  12. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  13. * BSD-like license that allows static linking with closed source software
  14. *
  15. * Copyright (c) 2021-2024 Teddy Astie (@tsnake41)
  16. *
  17. ********************************************************************************************/
  18. #include "raylib.h"
  19. #include "rlgl.h"
  20. #include <stdlib.h>
  21. // IMPORTANT: This must match gol*.glsl GOL_WIDTH constant.
  22. // This must be a multiple of 16 (check golLogic compute dispatch).
  23. #define GOL_WIDTH 768
  24. // Maximum amount of queued draw commands (squares draw from mouse down events).
  25. #define MAX_BUFFERED_TRANSFERTS 48
  26. // Game Of Life Update Command
  27. typedef struct GolUpdateCmd {
  28. unsigned int x; // x coordinate of the gol command
  29. unsigned int y; // y coordinate of the gol command
  30. unsigned int w; // width of the filled zone
  31. unsigned int enabled; // whether to enable or disable zone
  32. } GolUpdateCmd;
  33. // Game Of Life Update Commands SSBO
  34. typedef struct GolUpdateSSBO {
  35. unsigned int count;
  36. GolUpdateCmd commands[MAX_BUFFERED_TRANSFERTS];
  37. } GolUpdateSSBO;
  38. //------------------------------------------------------------------------------------
  39. // Program main entry point
  40. //------------------------------------------------------------------------------------
  41. int main(void)
  42. {
  43. // Initialization
  44. //--------------------------------------------------------------------------------------
  45. InitWindow(GOL_WIDTH, GOL_WIDTH, "raylib [rlgl] example - compute shader - game of life");
  46. const Vector2 resolution = { GOL_WIDTH, GOL_WIDTH };
  47. unsigned int brushSize = 8;
  48. // Game of Life logic compute shader
  49. char *golLogicCode = LoadFileText("resources/shaders/glsl430/gol.glsl");
  50. unsigned int golLogicShader = rlCompileShader(golLogicCode, RL_COMPUTE_SHADER);
  51. unsigned int golLogicProgram = rlLoadComputeShaderProgram(golLogicShader);
  52. UnloadFileText(golLogicCode);
  53. // Game of Life logic render shader
  54. Shader golRenderShader = LoadShader(NULL, "resources/shaders/glsl430/gol_render.glsl");
  55. int resUniformLoc = GetShaderLocation(golRenderShader, "resolution");
  56. // Game of Life transfert shader (CPU<->GPU download and upload)
  57. char *golTransfertCode = LoadFileText("resources/shaders/glsl430/gol_transfert.glsl");
  58. unsigned int golTransfertShader = rlCompileShader(golTransfertCode, RL_COMPUTE_SHADER);
  59. unsigned int golTransfertProgram = rlLoadComputeShaderProgram(golTransfertShader);
  60. UnloadFileText(golTransfertCode);
  61. // Load shader storage buffer object (SSBO), id returned
  62. unsigned int ssboA = rlLoadShaderBuffer(GOL_WIDTH*GOL_WIDTH*sizeof(unsigned int), NULL, RL_DYNAMIC_COPY);
  63. unsigned int ssboB = rlLoadShaderBuffer(GOL_WIDTH*GOL_WIDTH*sizeof(unsigned int), NULL, RL_DYNAMIC_COPY);
  64. unsigned int ssboTransfert = rlLoadShaderBuffer(sizeof(GolUpdateSSBO), NULL, RL_DYNAMIC_COPY);
  65. GolUpdateSSBO transfertBuffer = { 0 };
  66. // Create a white texture of the size of the window to update
  67. // each pixel of the window using the fragment shader: golRenderShader
  68. Image whiteImage = GenImageColor(GOL_WIDTH, GOL_WIDTH, WHITE);
  69. Texture whiteTex = LoadTextureFromImage(whiteImage);
  70. UnloadImage(whiteImage);
  71. //--------------------------------------------------------------------------------------
  72. // Main game loop
  73. while (!WindowShouldClose())
  74. {
  75. // Update
  76. //----------------------------------------------------------------------------------
  77. brushSize += (int)GetMouseWheelMove();
  78. if ((IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
  79. && (transfertBuffer.count < MAX_BUFFERED_TRANSFERTS))
  80. {
  81. // Buffer a new command
  82. transfertBuffer.commands[transfertBuffer.count].x = GetMouseX() - brushSize/2;
  83. transfertBuffer.commands[transfertBuffer.count].y = GetMouseY() - brushSize/2;
  84. transfertBuffer.commands[transfertBuffer.count].w = brushSize;
  85. transfertBuffer.commands[transfertBuffer.count].enabled = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
  86. transfertBuffer.count++;
  87. }
  88. else if (transfertBuffer.count > 0) // Process transfert buffer
  89. {
  90. // Send SSBO buffer to GPU
  91. rlUpdateShaderBuffer(ssboTransfert, &transfertBuffer, sizeof(GolUpdateSSBO), 0);
  92. // Process SSBO commands on GPU
  93. rlEnableShader(golTransfertProgram);
  94. rlBindShaderBuffer(ssboA, 1);
  95. rlBindShaderBuffer(ssboTransfert, 3);
  96. rlComputeShaderDispatch(transfertBuffer.count, 1, 1); // Each GPU unit will process a command!
  97. rlDisableShader();
  98. transfertBuffer.count = 0;
  99. }
  100. else
  101. {
  102. // Process game of life logic
  103. rlEnableShader(golLogicProgram);
  104. rlBindShaderBuffer(ssboA, 1);
  105. rlBindShaderBuffer(ssboB, 2);
  106. rlComputeShaderDispatch(GOL_WIDTH/16, GOL_WIDTH/16, 1);
  107. rlDisableShader();
  108. // ssboA <-> ssboB
  109. int temp = ssboA;
  110. ssboA = ssboB;
  111. ssboB = temp;
  112. }
  113. rlBindShaderBuffer(ssboA, 1);
  114. SetShaderValue(golRenderShader, resUniformLoc, &resolution, SHADER_UNIFORM_VEC2);
  115. //----------------------------------------------------------------------------------
  116. // Draw
  117. //----------------------------------------------------------------------------------
  118. BeginDrawing();
  119. ClearBackground(BLANK);
  120. BeginShaderMode(golRenderShader);
  121. DrawTexture(whiteTex, 0, 0, WHITE);
  122. EndShaderMode();
  123. DrawRectangleLines(GetMouseX() - brushSize/2, GetMouseY() - brushSize/2, brushSize, brushSize, RED);
  124. DrawText("Use Mouse wheel to increase/decrease brush size", 10, 10, 20, WHITE);
  125. DrawFPS(GetScreenWidth() - 100, 10);
  126. EndDrawing();
  127. //----------------------------------------------------------------------------------
  128. }
  129. // De-Initialization
  130. //--------------------------------------------------------------------------------------
  131. // Unload shader buffers objects.
  132. rlUnloadShaderBuffer(ssboA);
  133. rlUnloadShaderBuffer(ssboB);
  134. rlUnloadShaderBuffer(ssboTransfert);
  135. // Unload compute shader programs
  136. rlUnloadShaderProgram(golTransfertProgram);
  137. rlUnloadShaderProgram(golLogicProgram);
  138. UnloadTexture(whiteTex); // Unload white texture
  139. UnloadShader(golRenderShader); // Unload rendering fragment shader
  140. CloseWindow(); // Close window and OpenGL context
  141. //--------------------------------------------------------------------------------------
  142. return 0;
  143. }