IsUpdateTilegrid.glsl 3.1 KB

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