sdfgi_fields.glsl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* clang-format off */
  2. [compute]
  3. #version 450
  4. VERSION_DEFINES
  5. layout(local_size_x = OCT_RES, local_size_y = OCT_RES, local_size_z = 1) in;
  6. /* clang-format on */
  7. #define MAX_CASCADES 8
  8. layout(rgba16f, set = 0, binding = 1) uniform restrict image2DArray irradiance_texture;
  9. layout(rg16f, set = 0, binding = 2) uniform restrict image2DArray depth_texture;
  10. layout(rgba32ui, set = 0, binding = 3) uniform restrict uimage2DArray irradiance_history_texture;
  11. layout(rg32ui, set = 0, binding = 4) uniform restrict uimage2DArray depth_history_texture;
  12. struct CascadeData {
  13. vec3 offset; //offset of (0,0,0) in world coordinates
  14. float to_cell; // 1/bounds * grid_size
  15. };
  16. layout(set = 0, binding = 5, std140) uniform Cascades {
  17. CascadeData data[MAX_CASCADES];
  18. }
  19. cascades;
  20. #define DEPTH_HISTORY_BITS 24
  21. #define IRRADIANCE_HISTORY_BITS 16
  22. layout(push_constant, binding = 0, std430) uniform Params {
  23. vec3 grid_size;
  24. uint max_cascades;
  25. uint probe_axis_size;
  26. uint cascade;
  27. uint history_size;
  28. uint pad0;
  29. ivec3 scroll; //scroll in probes
  30. uint pad1;
  31. }
  32. params;
  33. void main() {
  34. ivec2 local = ivec2(gl_LocalInvocationID.xy);
  35. ivec2 probe = ivec2(gl_WorkGroupID.xy);
  36. ivec3 probe_cell;
  37. probe_cell.x = probe.x % int(params.probe_axis_size);
  38. probe_cell.y = probe.y;
  39. probe_cell.z = probe.x / int(params.probe_axis_size);
  40. #ifdef MODE_SCROLL_BEGIN
  41. ivec3 read_cell = probe_cell - params.scroll;
  42. uint src_layer = (params.history_size + 1) * params.cascade;
  43. uint dst_layer = (params.history_size + 1) * params.max_cascades;
  44. for (uint i = 0; i <= params.history_size; i++) {
  45. ivec3 write_pos = ivec3(probe * OCT_RES + local, int(i));
  46. if (any(lessThan(read_pos, ivec3(0))) || any(greaterThanEqual(read_pos, ivec3(params.probe_axis_size)))) {
  47. // nowhere to read from for scrolling, try finding the value from upper probes
  48. #ifdef MODE_IRRADIANCE
  49. imageStore(irradiance_history_texture, write_pos, uvec4(0));
  50. #endif
  51. #ifdef MODE_DEPTH
  52. imageStore(depth_history_texture, write_pos, uvec4(0));
  53. #endif
  54. } else {
  55. ivec3 read_pos;
  56. read_pos.xy = read_cell.xy;
  57. read_pos.x += read_cell.z * params.probe_axis_size;
  58. read_pos.xy = read_pos.xy * OCT_RES + local;
  59. read_pos.z = int(i);
  60. #ifdef MODE_IRRADIANCE
  61. uvec4 value = imageLoad(irradiance_history_texture, read_pos);
  62. imageStore(irradiance_history_texture, write_pos, value);
  63. #endif
  64. #ifdef MODE_DEPTH
  65. uvec2 value = imageLoad(depth_history_texture, read_pos);
  66. imageStore(depth_history_texture, write_pos, value);
  67. #endif
  68. }
  69. }
  70. #endif // MODE_SCROLL_BEGIN
  71. #ifdef MODE_SCROLL_END
  72. uint src_layer = (params.history_size + 1) * params.max_cascades;
  73. uint dst_layer = (params.history_size + 1) * params.cascade;
  74. for (uint i = 0; i <= params.history_size; i++) {
  75. ivec3 pos = ivec3(probe * OCT_RES + local, int(i));
  76. #ifdef MODE_IRRADIANCE
  77. uvec4 value = imageLoad(irradiance_history_texture, read_pos);
  78. imageStore(irradiance_history_texture, write_pos, value);
  79. #endif
  80. #ifdef MODE_DEPTH
  81. uvec2 value = imageLoad(depth_history_texture, read_pos);
  82. imageStore(depth_history_texture, write_pos, value);
  83. #endif
  84. }
  85. #endif //MODE_SCROLL_END
  86. #ifdef MODE_STORE
  87. uint src_layer = (params.history_size + 1) * params.cascade + params.history_size;
  88. ivec3 read_pos = ivec3(probe * OCT_RES + local, int(src_layer));
  89. ivec3 write_pos = ivec3(probe * (OCT_RES + 2) + ivec2(1), int(params.cascade));
  90. ivec3 copy_to[4] = ivec3[](write_pos, ivec3(-2, -2, -2), ivec3(-2, -2, -2), ivec3(-2, -2, -2));
  91. #ifdef MODE_IRRADIANCE
  92. uvec4 average = imageLoad(irradiance_history_texture, read_pos);
  93. vec4 light_accum = vec4(average / params.history_size) / float(1 << IRRADIANCE_HISTORY_BITS);
  94. #endif
  95. #ifdef MODE_DEPTH
  96. uvec2 value = imageLoad(depth_history_texture, read_pos);
  97. vec2 depth_accum = vec4(average / params.history_size) / float(1 << IRRADIANCE_HISTORY_BITS);
  98. float probe_cell_size = float(params.grid_size / float(params.probe_axis_size - 1)) / cascades.data[params.cascade].to_cell;
  99. float max_depth = length(params.grid_size / cascades.data[params.max_cascades - 1].to_cell);
  100. max_depth /= probe_cell_size;
  101. depth_value = (vec2(average / params.history_size) / float(1 << DEPTH_HISTORY_BITS)) * vec2(max_depth, max_depth * max_depth);
  102. #endif
  103. /* Fill the border if required */
  104. if (local == ivec2(0, 0)) {
  105. copy_to[1] = texture_pos + ivec3(OCT_RES - 1, -1, 0);
  106. copy_to[2] = texture_pos + ivec3(-1, OCT_RES - 1, 0);
  107. copy_to[3] = texture_pos + ivec3(OCT_RES, OCT_RES, 0);
  108. } else if (local == ivec2(OCT_RES - 1, 0)) {
  109. copy_to[1] = texture_pos + ivec3(0, -1, 0);
  110. copy_to[2] = texture_pos + ivec3(OCT_RES, OCT_RES - 1, 0);
  111. copy_to[3] = texture_pos + ivec3(-1, OCT_RES, 0);
  112. } else if (local == ivec2(0, OCT_RES - 1)) {
  113. copy_to[1] = texture_pos + ivec3(-1, 0, 0);
  114. copy_to[2] = texture_pos + ivec3(OCT_RES - 1, OCT_RES, 0);
  115. copy_to[3] = texture_pos + ivec3(OCT_RES, -1, 0);
  116. } else if (local == ivec2(OCT_RES - 1, OCT_RES - 1)) {
  117. copy_to[1] = texture_pos + ivec3(0, OCT_RES, 0);
  118. copy_to[2] = texture_pos + ivec3(OCT_RES, 0, 0);
  119. copy_to[3] = texture_pos + ivec3(-1, -1, 0);
  120. } else if (local.y == 0) {
  121. copy_to[1] = texture_pos + ivec3(OCT_RES - local.x - 1, local.y - 1, 0);
  122. } else if (local.x == 0) {
  123. copy_to[1] = texture_pos + ivec3(local.x - 1, OCT_RES - local.y - 1, 0);
  124. } else if (local.y == OCT_RES - 1) {
  125. copy_to[1] = texture_pos + ivec3(OCT_RES - local.x - 1, local.y + 1, 0);
  126. } else if (local.x == OCT_RES - 1) {
  127. copy_to[1] = texture_pos + ivec3(local.x + 1, OCT_RES - local.y - 1, 0);
  128. }
  129. for (int i = 0; i < 4; i++) {
  130. if (copy_to[i] == ivec3(-2, -2, -2)) {
  131. continue;
  132. }
  133. #ifdef MODE_IRRADIANCE
  134. imageStore(irradiance_texture, copy_to[i], light_accum);
  135. #endif
  136. #ifdef MODE_DEPTH
  137. imageStore(depth_texture, copy_to[i], vec4(depth_value, 0.0, 0.0));
  138. #endif
  139. }
  140. #endif // MODE_STORE
  141. }