IsUpdateTilegrid.glsl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // This compute shader sets the planes of the tilegrid
  2. #pragma anki start computeShader
  3. layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
  4. #define TILES_COUNT (TILES_X_COUNT * TILES_Y_COUNT)
  5. // The total number of planes. See the cpp code for details
  6. #define PLANES_COUNT (TILES_X_COUNT - 1 + TILES_Y_COUNT - 1 + TILES_COUNT)
  7. #define TILE_W (DEPTHMAP_WIDTH / TILES_X_COUNT)
  8. #define TILE_H (DEPTHMAP_HEIGHT / TILES_Y_COUNT)
  9. #define tileX gl_WorkGroupID.x
  10. #define tileY gl_WorkGroupID.y
  11. // Offsets of different plane groups in
  12. #define PLANES_X_OFFSET 0
  13. #define PLANES_Y_OFFSET (PLANES_X_OFFSET + )
  14. #define PLANES_NEAR_OFFSET (PLANES_Y_OFFSET + TILES_Y_COUNT - 1)
  15. #define PLANES_FAR_OFFSET (PLANES_NEAR_OFFSET + TILES_COUNT)
  16. uniform highp sampler2D depthMap;
  17. struct Plane
  18. {
  19. vec4 normalOffset;
  20. };
  21. struct Tilegrid
  22. {
  23. Plane planesX[TILES_X_COUNT - 1];
  24. Plane planesY[TILES_Y_COUNT - 1];
  25. Plane planesNear[TILES_Y_COUNT][TILES_X_COUNT];
  26. Plane planesFar[TILES_Y_COUNT][TILES_X_COUNT];
  27. };
  28. layout(std140, binding = 0) buffer tilegridBuffer
  29. {
  30. Tilegrid tilegrid;
  31. };
  32. layout(std140, binding = 1) uniform uniformBuffer
  33. {
  34. vec4 fovXfovYNearFar;
  35. bvec4 frustumChanged_;
  36. }
  37. #define frustumChanged frustumChanged_.x
  38. #define fovX fovXfovYNearFar.x
  39. #define fovY fovXfovYNearFar.y
  40. #define near fovXfovYNearFar.z
  41. #define far fovXfovYNearFar.w
  42. void main()
  43. {
  44. //
  45. // First get the min max depth of the tile. This reads from memory so do
  46. // it first
  47. //
  48. vec2 minMaxDepth = vec2(-10000.0, 100000.0);
  49. uvec2 coord = uvec2(uvec2(tileX, tileY) * uvec2(TILE_W, TILE_H));
  50. for(int x = 0; x < TILE_W; x++)
  51. {
  52. for(uint y = 0; y < TILE_H; i++)
  53. {
  54. float depth = texelFetch(depthMap, coord + uvec2(x, y)).r;
  55. minMaxDepth.x = min(depth, minMaxDepth.x);
  56. minMaxDepth.y = max(depth, minMaxDepth.y);
  57. }
  58. }
  59. //
  60. // Update top and right looking planes only when the fovs have changed
  61. //
  62. if(frustumChanged)
  63. {
  64. float near2 = 2.0 * near;
  65. float l = near2 * tan(fovX * 0.5);
  66. float l6 = l * (1.0 / float(TILES_X_COUNT));
  67. float o = near2 * tan(fovY * 0.5);
  68. float o6 = o * (1.0 / float(TILES_Y_COUNT));
  69. // First the right looking planes in one working thread
  70. if(tileY == 0U)
  71. {
  72. vec3 a, b;
  73. a = vec3(
  74. float(int(tileX + 1) - (int(TILES_X_COUNT) / 2)) * l6,
  75. 0.0,
  76. -near);
  77. b = cross(a, vec3(0.0, 1.0, 0.0));
  78. normalize(b);
  79. planesX[tileX].normalOffset = vec4(b, 0.0)
  80. }
  81. // Then the top looking planes in one working thread
  82. if(tileX == 0U)
  83. {
  84. vec3 a, b;
  85. a = vec3(
  86. 0.0,
  87. float(int(tileY + 1) - (int(TILES_Y_COUNT) / 2)) * o6,
  88. -near);
  89. b = cross(vec3(1.0, 0.0, 0.0), a);
  90. normalize(b);
  91. planesY[tileY].normalOffset = vec4(b, 0.0);
  92. }
  93. }
  94. //
  95. // Update the far and near planes
  96. //
  97. {
  98. // Calc the Z in view space
  99. vec2 minMaxZ =
  100. vec2(far * near) / (minMaxDepth * vec2(near - far) + vec2(near));
  101. // Set planes
  102. planesNear[tileY][tileX].normalOffset =
  103. vec4(0.0, 0.0, -1.0, minMaxZ.x);
  104. planesFar[tileY][tileX].normalOffset =
  105. vec4(0.0, 0.0, 1.0, minMaxZ.y);
  106. }
  107. }