sdfgi_debug_probes.glsl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #[vertex]
  2. #version 450
  3. #if defined(USE_MULTIVIEW) && defined(has_VK_KHR_multiview)
  4. #extension GL_EXT_multiview : enable
  5. #endif
  6. #ifdef USE_MULTIVIEW
  7. #ifdef has_VK_KHR_multiview
  8. #define ViewIndex gl_ViewIndex
  9. #else // has_VK_KHR_multiview
  10. // !BAS! This needs to become an input once we implement our fallback!
  11. #define ViewIndex 0
  12. #endif // has_VK_KHR_multiview
  13. #else // USE_MULTIVIEW
  14. // Set to zero, not supported in non stereo
  15. #define ViewIndex 0
  16. #endif //USE_MULTIVIEW
  17. #VERSION_DEFINES
  18. #define MAX_CASCADES 8
  19. #define MAX_VIEWS 2
  20. layout(push_constant, std430) uniform Params {
  21. uint band_power;
  22. uint sections_in_band;
  23. uint band_mask;
  24. float section_arc;
  25. vec3 grid_size;
  26. uint cascade;
  27. uint pad;
  28. float y_mult;
  29. uint probe_debug_index;
  30. int probe_axis_size;
  31. }
  32. params;
  33. // https://in4k.untergrund.net/html_articles/hugi_27_-_coding_corner_polaris_sphere_tessellation_101.htm
  34. vec3 get_sphere_vertex(uint p_vertex_id) {
  35. float x_angle = float(p_vertex_id & 1u) + (p_vertex_id >> params.band_power);
  36. float y_angle =
  37. float((p_vertex_id & params.band_mask) >> 1) + ((p_vertex_id >> params.band_power) * params.sections_in_band);
  38. x_angle *= params.section_arc * 0.5f; // remember - 180AA x rot not 360
  39. y_angle *= -params.section_arc;
  40. vec3 point = vec3(sin(x_angle) * sin(y_angle), cos(x_angle), sin(x_angle) * cos(y_angle));
  41. return point;
  42. }
  43. #ifdef MODE_PROBES
  44. layout(location = 0) out vec3 normal_interp;
  45. layout(location = 1) out flat uint probe_index;
  46. #endif
  47. #ifdef MODE_VISIBILITY
  48. layout(location = 0) out float visibility;
  49. #endif
  50. struct CascadeData {
  51. vec3 offset; //offset of (0,0,0) in world coordinates
  52. float to_cell; // 1/bounds * grid_size
  53. ivec3 probe_world_offset;
  54. uint pad;
  55. vec4 pad2;
  56. };
  57. layout(set = 0, binding = 1, std140) uniform Cascades {
  58. CascadeData data[MAX_CASCADES];
  59. }
  60. cascades;
  61. layout(set = 0, binding = 4) uniform texture3D occlusion_texture;
  62. layout(set = 0, binding = 3) uniform sampler linear_sampler;
  63. layout(set = 0, binding = 5, std140) uniform SceneData {
  64. mat4 projection[MAX_VIEWS];
  65. }
  66. scene_data;
  67. void main() {
  68. #ifdef MODE_PROBES
  69. probe_index = gl_InstanceIndex;
  70. normal_interp = get_sphere_vertex(gl_VertexIndex);
  71. vec3 vertex = normal_interp * 0.2;
  72. float probe_cell_size = float(params.grid_size / float(params.probe_axis_size - 1)) / cascades.data[params.cascade].to_cell;
  73. ivec3 probe_cell;
  74. probe_cell.x = int(probe_index % params.probe_axis_size);
  75. probe_cell.y = int(probe_index / (params.probe_axis_size * params.probe_axis_size));
  76. probe_cell.z = int((probe_index / params.probe_axis_size) % params.probe_axis_size);
  77. vertex += (cascades.data[params.cascade].offset + vec3(probe_cell) * probe_cell_size) / vec3(1.0, params.y_mult, 1.0);
  78. gl_Position = scene_data.projection[ViewIndex] * vec4(vertex, 1.0);
  79. #endif
  80. #ifdef MODE_VISIBILITY
  81. int probe_index = int(params.probe_debug_index);
  82. vec3 vertex = get_sphere_vertex(gl_VertexIndex) * 0.01;
  83. float probe_cell_size = float(params.grid_size / float(params.probe_axis_size - 1)) / cascades.data[params.cascade].to_cell;
  84. ivec3 probe_cell;
  85. probe_cell.x = int(probe_index % params.probe_axis_size);
  86. probe_cell.y = int((probe_index % (params.probe_axis_size * params.probe_axis_size)) / params.probe_axis_size);
  87. probe_cell.z = int(probe_index / (params.probe_axis_size * params.probe_axis_size));
  88. vertex += (cascades.data[params.cascade].offset + vec3(probe_cell) * probe_cell_size) / vec3(1.0, params.y_mult, 1.0);
  89. int probe_voxels = int(params.grid_size.x) / int(params.probe_axis_size - 1);
  90. int occluder_index = int(gl_InstanceIndex);
  91. int diameter = probe_voxels * 2;
  92. ivec3 occluder_pos;
  93. occluder_pos.x = int(occluder_index % diameter);
  94. occluder_pos.y = int(occluder_index / (diameter * diameter));
  95. occluder_pos.z = int((occluder_index / diameter) % diameter);
  96. float cell_size = 1.0 / cascades.data[params.cascade].to_cell;
  97. ivec3 occluder_offset = occluder_pos - ivec3(diameter / 2);
  98. vertex += ((vec3(occluder_offset) + vec3(0.5)) * cell_size) / vec3(1.0, params.y_mult, 1.0);
  99. ivec3 global_cell = probe_cell + cascades.data[params.cascade].probe_world_offset;
  100. uint occlusion_layer = 0;
  101. if ((global_cell.x & 1) != 0) {
  102. occlusion_layer |= 1;
  103. }
  104. if ((global_cell.y & 1) != 0) {
  105. occlusion_layer |= 2;
  106. }
  107. if ((global_cell.z & 1) != 0) {
  108. occlusion_layer |= 4;
  109. }
  110. ivec3 tex_pos = probe_cell * probe_voxels + occluder_offset;
  111. const vec4 layer_axis[4] = vec4[](
  112. vec4(1, 0, 0, 0),
  113. vec4(0, 1, 0, 0),
  114. vec4(0, 0, 1, 0),
  115. vec4(0, 0, 0, 1));
  116. tex_pos.z += int(params.cascade) * int(params.grid_size);
  117. if (occlusion_layer >= 4) {
  118. tex_pos.x += int(params.grid_size.x);
  119. occlusion_layer &= 3;
  120. }
  121. visibility = dot(texelFetch(sampler3D(occlusion_texture, linear_sampler), tex_pos, 0), layer_axis[occlusion_layer]);
  122. gl_Position = scene_data.projection[ViewIndex] * vec4(vertex, 1.0);
  123. #endif
  124. }
  125. #[fragment]
  126. #version 450
  127. #if defined(USE_MULTIVIEW) && defined(has_VK_KHR_multiview)
  128. #extension GL_EXT_multiview : enable
  129. #endif
  130. #ifdef USE_MULTIVIEW
  131. #ifdef has_VK_KHR_multiview
  132. #define ViewIndex gl_ViewIndex
  133. #else // has_VK_KHR_multiview
  134. // !BAS! This needs to become an input once we implement our fallback!
  135. #define ViewIndex 0
  136. #endif // has_VK_KHR_multiview
  137. #else // USE_MULTIVIEW
  138. // Set to zero, not supported in non stereo
  139. #define ViewIndex 0
  140. #endif //USE_MULTIVIEW
  141. #VERSION_DEFINES
  142. #define MAX_VIEWS 2
  143. layout(location = 0) out vec4 frag_color;
  144. layout(set = 0, binding = 2) uniform texture2DArray lightprobe_texture;
  145. layout(set = 0, binding = 3) uniform sampler linear_sampler;
  146. layout(push_constant, std430) uniform Params {
  147. uint band_power;
  148. uint sections_in_band;
  149. uint band_mask;
  150. float section_arc;
  151. vec3 grid_size;
  152. uint cascade;
  153. uint pad;
  154. float y_mult;
  155. uint probe_debug_index;
  156. int probe_axis_size;
  157. }
  158. params;
  159. #ifdef MODE_PROBES
  160. layout(location = 0) in vec3 normal_interp;
  161. layout(location = 1) in flat uint probe_index;
  162. #endif
  163. #ifdef MODE_VISIBILITY
  164. layout(location = 0) in float visibility;
  165. #endif
  166. vec2 octahedron_wrap(vec2 v) {
  167. vec2 signVal;
  168. signVal.x = v.x >= 0.0 ? 1.0 : -1.0;
  169. signVal.y = v.y >= 0.0 ? 1.0 : -1.0;
  170. return (1.0 - abs(v.yx)) * signVal;
  171. }
  172. vec2 octahedron_encode(vec3 n) {
  173. // https://twitter.com/Stubbesaurus/status/937994790553227264
  174. n /= (abs(n.x) + abs(n.y) + abs(n.z));
  175. n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);
  176. n.xy = n.xy * 0.5 + 0.5;
  177. return n.xy;
  178. }
  179. void main() {
  180. #ifdef MODE_PROBES
  181. ivec3 tex_pos;
  182. tex_pos.x = int(probe_index) % params.probe_axis_size; //x
  183. tex_pos.y = int(probe_index) / (params.probe_axis_size * params.probe_axis_size);
  184. tex_pos.x += params.probe_axis_size * ((int(probe_index) / params.probe_axis_size) % params.probe_axis_size); //z
  185. tex_pos.z = int(params.cascade);
  186. vec3 tex_pos_ofs = vec3(octahedron_encode(normal_interp) * float(OCT_SIZE), 0.0);
  187. vec3 tex_posf = vec3(vec2(tex_pos.xy * (OCT_SIZE + 2) + ivec2(1)), float(tex_pos.z)) + tex_pos_ofs;
  188. tex_posf.xy /= vec2(ivec2(params.probe_axis_size * params.probe_axis_size * (OCT_SIZE + 2), params.probe_axis_size * (OCT_SIZE + 2)));
  189. vec4 indirect_light = textureLod(sampler2DArray(lightprobe_texture, linear_sampler), tex_posf, 0.0);
  190. frag_color = indirect_light;
  191. #endif
  192. #ifdef MODE_VISIBILITY
  193. frag_color = vec4(vec3(1, visibility, visibility), 1.0);
  194. #endif
  195. }