Lightspaceboundingbox.comp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. uniform sampler2D depth;
  2. uniform float split0;
  3. uniform float split1;
  4. uniform float split2;
  5. uniform float splitmax;
  6. uniform mat4 SunCamMatrix;
  7. layout (local_size_x = 8, local_size_y = 8) in;
  8. struct CascadeBoundingBox
  9. {
  10. int xmin;
  11. int xmax;
  12. int ymin;
  13. int ymax;
  14. int zmin;
  15. int zmax;
  16. };
  17. layout (std430) buffer BoundingBoxes
  18. {
  19. CascadeBoundingBox BB[4];
  20. };
  21. vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
  22. shared int xmin[4];
  23. shared int xmax[4];
  24. shared int ymin[4];
  25. shared int ymax[4];
  26. shared int zmin[4];
  27. shared int zmax[4];
  28. void main()
  29. {
  30. if (gl_LocalInvocationIndex < 4) {
  31. xmin[gl_LocalInvocationIndex] = ymin[gl_LocalInvocationIndex] = zmin[gl_LocalInvocationIndex] = 1000;
  32. xmax[gl_LocalInvocationIndex] = ymax[gl_LocalInvocationIndex] = zmax[gl_LocalInvocationIndex] = -1000;
  33. }
  34. barrier();
  35. ivec3 lmax0 = ivec3(-1000);
  36. ivec3 lmin0 = ivec3(1000);
  37. ivec3 lmax1 = ivec3(-1000);
  38. ivec3 lmin1 = ivec3(1000);
  39. ivec3 lmax2 = ivec3(-1000);
  40. ivec3 lmin2 = ivec3(1000);
  41. ivec3 lmax3 = ivec3(-1000);
  42. ivec3 lmin3 = ivec3(1000);
  43. vec2 start_xy = gl_LocalInvocationID.xy + gl_WorkGroupID.xy * gl_WorkGroupSize.xy * 8;
  44. for (int i = 0; i < 8; i++) {
  45. for (int j = 0; j < 8; j++) {
  46. vec2 uv = (start_xy + vec2(i, j) * gl_WorkGroupID.xy) / screen;
  47. float z = texture(depth, uv).x;
  48. vec4 xpos = getPosFromUVDepth(vec3(uv, z), InverseProjectionMatrix);
  49. vec4 lightcoord = InverseViewMatrix * xpos;
  50. lightcoord /= lightcoord.w;
  51. lightcoord = SunCamMatrix * lightcoord;
  52. lightcoord /= lightcoord.w;
  53. ivec3 lc = ivec3(lightcoord.xyz) * 4;
  54. if (xpos.z < split0) {
  55. lmax0 = max(lmax0, lc);
  56. lmin0 = min(lmin0, lc);
  57. } else if (xpos.z < split1) {
  58. lmax1 = max(lmax1, lc);
  59. lmin1 = min(lmin1, lc);
  60. } else if (xpos.z < split2) {
  61. lmax2 = max(lmax2, lc);
  62. lmin2 = min(lmin2, lc);
  63. } else if (xpos.z < splitmax) {
  64. lmax3 = max(lmax3, lc);
  65. lmin3 = min(lmin3, lc);
  66. }
  67. }
  68. }
  69. atomicMax(xmax[0], lmax0.x);
  70. atomicMax(ymax[0], lmax0.y);
  71. atomicMax(zmax[0], lmax0.z);
  72. atomicMin(xmin[0], lmin0.x);
  73. atomicMin(ymin[0], lmin0.y);
  74. atomicMin(zmin[0], lmin0.z);
  75. atomicMax(xmax[1], lmax1.x);
  76. atomicMax(ymax[1], lmax1.y);
  77. atomicMax(zmax[1], lmax1.z);
  78. atomicMin(xmin[1], lmin1.x);
  79. atomicMin(ymin[1], lmin1.y);
  80. atomicMin(zmin[1], lmin1.z);
  81. atomicMax(xmax[2], lmax2.x);
  82. atomicMax(ymax[2], lmax2.y);
  83. atomicMax(zmax[2], lmax2.z);
  84. atomicMin(xmin[2], lmin2.x);
  85. atomicMin(ymin[2], lmin2.y);
  86. atomicMin(zmin[2], lmin2.z);
  87. atomicMax(xmax[3], lmax3.x);
  88. atomicMax(ymax[3], lmax3.y);
  89. atomicMax(zmax[3], lmax3.z);
  90. atomicMin(xmin[3], lmin3.x);
  91. atomicMin(ymin[3], lmin3.y);
  92. atomicMin(zmin[3], lmin3.z);
  93. barrier();
  94. if (gl_LocalInvocationIndex == 0) {
  95. atomicMax(BB[0].xmax, xmax[0]);
  96. atomicMax(BB[0].ymax, ymax[0]);
  97. atomicMax(BB[0].zmax, zmax[0]);
  98. atomicMin(BB[0].xmin, xmin[0]);
  99. atomicMin(BB[0].ymin, ymin[0]);
  100. atomicMin(BB[0].zmin, zmin[0]);
  101. atomicMax(BB[1].xmax, xmax[1]);
  102. atomicMax(BB[1].ymax, ymax[1]);
  103. atomicMax(BB[1].zmax, zmax[1]);
  104. atomicMin(BB[1].xmin, xmin[1]);
  105. atomicMin(BB[1].ymin, ymin[1]);
  106. atomicMin(BB[1].zmin, zmin[1]);
  107. atomicMax(BB[2].xmax, xmax[2]);
  108. atomicMax(BB[2].ymax, ymax[2]);
  109. atomicMax(BB[2].zmax, zmax[2]);
  110. atomicMin(BB[2].xmin, xmin[2]);
  111. atomicMin(BB[2].ymin, ymin[2]);
  112. atomicMin(BB[2].zmin, zmin[2]);
  113. atomicMax(BB[3].xmax, xmax[3]);
  114. atomicMax(BB[3].ymax, ymax[3]);
  115. atomicMax(BB[3].zmax, zmax[3]);
  116. atomicMin(BB[3].xmin, xmin[3]);
  117. atomicMin(BB[3].ymin, ymin[3]);
  118. atomicMin(BB[3].zmin, zmin[3]);
  119. }
  120. }