voxel_gi_debug.glsl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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, 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. color_interp = texelFetch(sampler3D(color_tex, tex_sampler), ivec3(posu), int(params.level));
  80. color_interp.xyz *params.dynamic_range;
  81. #endif
  82. float scale = (1 << params.level);
  83. gl_Position = params.projection * vec4((vec3(posu) + vertex) * scale, 1.0);
  84. #ifdef MODE_DEBUG_LIGHT_FULL
  85. if (color_interp.a == 0.0) {
  86. gl_Position = vec4(0.0); //force clip and not draw
  87. }
  88. #else
  89. color_interp.a = params.alpha;
  90. #endif
  91. }
  92. #[fragment]
  93. #version 450
  94. #VERSION_DEFINES
  95. layout(location = 0) in vec4 color_interp;
  96. layout(location = 0) out vec4 frag_color;
  97. void main() {
  98. frag_color = color_interp;
  99. #ifdef MODE_DEBUG_LIGHT_FULL
  100. //there really is no alpha, so use dither
  101. int x = int(gl_FragCoord.x) % 4;
  102. int y = int(gl_FragCoord.y) % 4;
  103. int index = x + y * 4;
  104. float limit = 0.0;
  105. if (x < 8) {
  106. if (index == 0)
  107. limit = 0.0625;
  108. if (index == 1)
  109. limit = 0.5625;
  110. if (index == 2)
  111. limit = 0.1875;
  112. if (index == 3)
  113. limit = 0.6875;
  114. if (index == 4)
  115. limit = 0.8125;
  116. if (index == 5)
  117. limit = 0.3125;
  118. if (index == 6)
  119. limit = 0.9375;
  120. if (index == 7)
  121. limit = 0.4375;
  122. if (index == 8)
  123. limit = 0.25;
  124. if (index == 9)
  125. limit = 0.75;
  126. if (index == 10)
  127. limit = 0.125;
  128. if (index == 11)
  129. limit = 0.625;
  130. if (index == 12)
  131. limit = 1.0;
  132. if (index == 13)
  133. limit = 0.5;
  134. if (index == 14)
  135. limit = 0.875;
  136. if (index == 15)
  137. limit = 0.375;
  138. }
  139. if (frag_color.a < limit) {
  140. discard;
  141. }
  142. #endif
  143. }