gol.glsl 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #version 430
  2. // Game of Life logic shader
  3. #define GOL_WIDTH 768
  4. layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
  5. layout(std430, binding = 1) readonly restrict buffer golLayout {
  6. uint golBuffer[]; // golBuffer[x, y] = golBuffer[x + gl_NumWorkGroups.x * y]
  7. };
  8. layout(std430, binding = 2) writeonly restrict buffer golLayout2 {
  9. uint golBufferDest[]; // golBufferDest[x, y] = golBufferDest[x + gl_NumWorkGroups.x * y]
  10. };
  11. #define fetchGol(x, y) ((((x) < 0) || ((y) < 0) || ((x) > GOL_WIDTH) || ((y) > GOL_WIDTH)) \
  12. ? (0) \
  13. : golBuffer[(x) + GOL_WIDTH * (y)])
  14. #define setGol(x, y, value) golBufferDest[(x) + GOL_WIDTH*(y)] = value
  15. void main()
  16. {
  17. uint neighbourCount = 0;
  18. uint x = gl_GlobalInvocationID.x;
  19. uint y = gl_GlobalInvocationID.y;
  20. neighbourCount += fetchGol(x - 1, y - 1); // Top left
  21. neighbourCount += fetchGol(x, y - 1); // Top middle
  22. neighbourCount += fetchGol(x + 1, y - 1); // Top right
  23. neighbourCount += fetchGol(x - 1, y); // Left
  24. neighbourCount += fetchGol(x + 1, y); // Right
  25. neighbourCount += fetchGol(x - 1, y + 1); // Bottom left
  26. neighbourCount += fetchGol(x, y + 1); // Bottom middle
  27. neighbourCount += fetchGol(x + 1, y + 1); // Bottom right
  28. if (neighbourCount == 3) setGol(x, y, 1);
  29. else if (neighbourCount == 2) setGol(x, y, fetchGol(x, y));
  30. else setGol(x, y, 0);
  31. }