sdfgi_debug_probes.glsl 6.1 KB

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