2
0

voxel_gi_debug.glsl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. struct CellData {
  5. uint position; // xyz 10 bits
  6. uint albedo; //rgb albedo
  7. uint emission; //rgb normalized with e as multiplier
  8. uint normal; //RGB normal encoded
  9. };
  10. layout(set = 0, binding = 1, std140) buffer CellDataBuffer {
  11. CellData data[];
  12. }
  13. cell_data;
  14. layout(set = 0, binding = 2) uniform texture3D color_tex;
  15. layout(set = 0, binding = 3) uniform sampler tex_sampler;
  16. layout(push_constant, binding = 0, std430) uniform Params {
  17. mat4 projection;
  18. uint cell_offset;
  19. float dynamic_range;
  20. float alpha;
  21. uint level;
  22. ivec3 bounds;
  23. uint pad;
  24. }
  25. params;
  26. layout(location = 0) out vec4 color_interp;
  27. void main() {
  28. const vec3 cube_triangles[36] = vec3[](
  29. vec3(-1.0f, -1.0f, -1.0f),
  30. vec3(-1.0f, -1.0f, 1.0f),
  31. vec3(-1.0f, 1.0f, 1.0f),
  32. vec3(1.0f, 1.0f, -1.0f),
  33. vec3(-1.0f, -1.0f, -1.0f),
  34. vec3(-1.0f, 1.0f, -1.0f),
  35. vec3(1.0f, -1.0f, 1.0f),
  36. vec3(-1.0f, -1.0f, -1.0f),
  37. vec3(1.0f, -1.0f, -1.0f),
  38. vec3(1.0f, 1.0f, -1.0f),
  39. vec3(1.0f, -1.0f, -1.0f),
  40. vec3(-1.0f, -1.0f, -1.0f),
  41. vec3(-1.0f, -1.0f, -1.0f),
  42. vec3(-1.0f, 1.0f, 1.0f),
  43. vec3(-1.0f, 1.0f, -1.0f),
  44. vec3(1.0f, -1.0f, 1.0f),
  45. vec3(-1.0f, -1.0f, 1.0f),
  46. vec3(-1.0f, -1.0f, -1.0f),
  47. vec3(-1.0f, 1.0f, 1.0f),
  48. vec3(-1.0f, -1.0f, 1.0f),
  49. vec3(1.0f, -1.0f, 1.0f),
  50. vec3(1.0f, 1.0f, 1.0f),
  51. vec3(1.0f, -1.0f, -1.0f),
  52. vec3(1.0f, 1.0f, -1.0f),
  53. vec3(1.0f, -1.0f, -1.0f),
  54. vec3(1.0f, 1.0f, 1.0f),
  55. vec3(1.0f, -1.0f, 1.0f),
  56. vec3(1.0f, 1.0f, 1.0f),
  57. vec3(1.0f, 1.0f, -1.0f),
  58. vec3(-1.0f, 1.0f, -1.0f),
  59. vec3(1.0f, 1.0f, 1.0f),
  60. vec3(-1.0f, 1.0f, -1.0f),
  61. vec3(-1.0f, 1.0f, 1.0f),
  62. vec3(1.0f, 1.0f, 1.0f),
  63. vec3(-1.0f, 1.0f, 1.0f),
  64. vec3(1.0f, -1.0f, 1.0f));
  65. vec3 vertex = cube_triangles[gl_VertexIndex] * 0.5 + 0.5;
  66. #ifdef MODE_DEBUG_LIGHT_FULL
  67. uvec3 posu = uvec3(gl_InstanceIndex % params.bounds.x, (gl_InstanceIndex / params.bounds.x) % params.bounds.y, gl_InstanceIndex / (params.bounds.y * params.bounds.x));
  68. #else
  69. uint cell_index = gl_InstanceIndex + params.cell_offset;
  70. uvec3 posu = uvec3(cell_data.data[cell_index].position & 0x7FF, (cell_data.data[cell_index].position >> 11) & 0x3FF, cell_data.data[cell_index].position >> 21);
  71. #endif
  72. #ifdef MODE_DEBUG_EMISSION
  73. color_interp.xyz = vec3(uvec3(cell_data.data[cell_index].emission & 0x1ff, (cell_data.data[cell_index].emission >> 9) & 0x1ff, (cell_data.data[cell_index].emission >> 18) & 0x1ff)) * pow(2.0, float(cell_data.data[cell_index].emission >> 27) - 15.0 - 9.0);
  74. #endif
  75. #ifdef MODE_DEBUG_COLOR
  76. color_interp.xyz = unpackUnorm4x8(cell_data.data[cell_index].albedo).xyz;
  77. #endif
  78. #ifdef MODE_DEBUG_LIGHT
  79. #ifdef USE_ANISOTROPY
  80. #define POS_X 0
  81. #define POS_Y 1
  82. #define POS_Z 2
  83. #define NEG_X 3
  84. #define NEG_Y 4
  85. #define NEG_Z 5
  86. const uint triangle_aniso[12] = uint[](
  87. NEG_X,
  88. NEG_Z,
  89. NEG_Y,
  90. NEG_Z,
  91. NEG_X,
  92. NEG_Y,
  93. POS_Z,
  94. POS_X,
  95. POS_X,
  96. POS_Y,
  97. POS_Y,
  98. POS_Z);
  99. color_interp.xyz = texelFetch(sampler3D(color_tex, tex_sampler), ivec3(posu), int(params.level)).xyz * params.dynamic_range;
  100. vec3 aniso_pos = texelFetch(sampler3D(aniso_pos_tex, tex_sampler), ivec3(posu), int(params.level)).xyz;
  101. vec3 aniso_neg = texelFetch(sampler3D(aniso_neg_tex, tex_sampler), ivec3(posu), int(params.level)).xyz;
  102. uint side = triangle_aniso[gl_VertexIndex / 3];
  103. float strength = 0.0;
  104. switch (side) {
  105. case POS_X:
  106. strength = aniso_pos.x;
  107. break;
  108. case POS_Y:
  109. strength = aniso_pos.y;
  110. break;
  111. case POS_Z:
  112. strength = aniso_pos.z;
  113. break;
  114. case NEG_X:
  115. strength = aniso_neg.x;
  116. break;
  117. case NEG_Y:
  118. strength = aniso_neg.y;
  119. break;
  120. case NEG_Z:
  121. strength = aniso_neg.z;
  122. break;
  123. }
  124. color_interp.xyz *= strength;
  125. #else
  126. color_interp = texelFetch(sampler3D(color_tex, tex_sampler), ivec3(posu), int(params.level));
  127. color_interp.xyz *params.dynamic_range;
  128. #endif
  129. #endif
  130. float scale = (1 << params.level);
  131. gl_Position = params.projection * vec4((vec3(posu) + vertex) * scale, 1.0);
  132. #ifdef MODE_DEBUG_LIGHT_FULL
  133. if (color_interp.a == 0.0) {
  134. gl_Position = vec4(0.0); //force clip and not draw
  135. }
  136. #else
  137. color_interp.a = params.alpha;
  138. #endif
  139. }
  140. #[fragment]
  141. #version 450
  142. #VERSION_DEFINES
  143. layout(location = 0) in vec4 color_interp;
  144. layout(location = 0) out vec4 frag_color;
  145. void main() {
  146. frag_color = color_interp;
  147. #ifdef MODE_DEBUG_LIGHT_FULL
  148. //there really is no alpha, so use dither
  149. int x = int(gl_FragCoord.x) % 4;
  150. int y = int(gl_FragCoord.y) % 4;
  151. int index = x + y * 4;
  152. float limit = 0.0;
  153. if (x < 8) {
  154. if (index == 0)
  155. limit = 0.0625;
  156. if (index == 1)
  157. limit = 0.5625;
  158. if (index == 2)
  159. limit = 0.1875;
  160. if (index == 3)
  161. limit = 0.6875;
  162. if (index == 4)
  163. limit = 0.8125;
  164. if (index == 5)
  165. limit = 0.3125;
  166. if (index == 6)
  167. limit = 0.9375;
  168. if (index == 7)
  169. limit = 0.4375;
  170. if (index == 8)
  171. limit = 0.25;
  172. if (index == 9)
  173. limit = 0.75;
  174. if (index == 10)
  175. limit = 0.125;
  176. if (index == 11)
  177. limit = 0.625;
  178. if (index == 12)
  179. limit = 1.0;
  180. if (index == 13)
  181. limit = 0.5;
  182. if (index == 14)
  183. limit = 0.875;
  184. if (index == 15)
  185. limit = 0.375;
  186. }
  187. if (frag_color.a < limit) {
  188. discard;
  189. }
  190. #endif
  191. }