sdfgi_debug.glsl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  5. #define MAX_CASCADES 8
  6. layout(set = 0, binding = 1) uniform texture3D sdf_cascades[MAX_CASCADES];
  7. layout(set = 0, binding = 2) uniform texture3D light_cascades[MAX_CASCADES];
  8. layout(set = 0, binding = 3) uniform texture3D aniso0_cascades[MAX_CASCADES];
  9. layout(set = 0, binding = 4) uniform texture3D aniso1_cascades[MAX_CASCADES];
  10. layout(set = 0, binding = 5) uniform texture3D occlusion_texture;
  11. layout(set = 0, binding = 8) uniform sampler linear_sampler;
  12. struct CascadeData {
  13. vec3 offset; //offset of (0,0,0) in world coordinates
  14. float to_cell; // 1/bounds * grid_size
  15. ivec3 probe_world_offset;
  16. uint pad;
  17. vec4 pad2;
  18. };
  19. layout(set = 0, binding = 9, std140) uniform Cascades {
  20. CascadeData data[MAX_CASCADES];
  21. }
  22. cascades;
  23. layout(rgba16f, set = 0, binding = 10) uniform restrict writeonly image2D screen_buffer;
  24. layout(set = 0, binding = 11) uniform texture2DArray lightprobe_texture;
  25. layout(push_constant, std430) uniform Params {
  26. vec3 grid_size;
  27. uint max_cascades;
  28. ivec2 screen_size;
  29. float y_mult;
  30. float z_near;
  31. mat3x4 inv_projection;
  32. // We pack these more tightly than mat3 and vec3, which will require some reconstruction trickery.
  33. float cam_basis[3][3];
  34. float cam_origin[3];
  35. }
  36. params;
  37. vec3 linear_to_srgb(vec3 color) {
  38. //if going to srgb, clamp from 0 to 1.
  39. color = clamp(color, vec3(0.0), vec3(1.0));
  40. const vec3 a = vec3(0.055f);
  41. return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f)));
  42. }
  43. vec2 octahedron_wrap(vec2 v) {
  44. vec2 signVal;
  45. signVal.x = v.x >= 0.0 ? 1.0 : -1.0;
  46. signVal.y = v.y >= 0.0 ? 1.0 : -1.0;
  47. return (1.0 - abs(v.yx)) * signVal;
  48. }
  49. vec2 octahedron_encode(vec3 n) {
  50. // https://twitter.com/Stubbesaurus/status/937994790553227264
  51. n /= (abs(n.x) + abs(n.y) + abs(n.z));
  52. n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);
  53. n.xy = n.xy * 0.5 + 0.5;
  54. return n.xy;
  55. }
  56. void main() {
  57. // Pixel being shaded
  58. ivec2 screen_pos = ivec2(gl_GlobalInvocationID.xy);
  59. if (any(greaterThanEqual(screen_pos, params.screen_size))) { //too large, do nothing
  60. return;
  61. }
  62. vec3 ray_pos;
  63. vec3 ray_dir;
  64. {
  65. ray_pos = vec3(params.cam_origin[0], params.cam_origin[1], params.cam_origin[2]);
  66. ray_dir.xy = ((vec2(screen_pos) / vec2(params.screen_size)) * 2.0 - 1.0);
  67. ray_dir.z = params.z_near;
  68. ray_dir = (vec4(ray_dir, 1.0) * mat4(params.inv_projection)).xyz;
  69. mat3 cam_basis;
  70. {
  71. vec3 c0 = vec3(params.cam_basis[0][0], params.cam_basis[0][1], params.cam_basis[0][2]);
  72. vec3 c1 = vec3(params.cam_basis[1][0], params.cam_basis[1][1], params.cam_basis[1][2]);
  73. vec3 c2 = vec3(params.cam_basis[2][0], params.cam_basis[2][1], params.cam_basis[2][2]);
  74. cam_basis = mat3(c0, c1, c2);
  75. }
  76. ray_dir = normalize(cam_basis * ray_dir);
  77. }
  78. ray_pos.y *= params.y_mult;
  79. ray_dir.y *= params.y_mult;
  80. ray_dir = normalize(ray_dir);
  81. vec3 pos_to_uvw = 1.0 / params.grid_size;
  82. vec3 light = vec3(0.0);
  83. float blend = 0.0;
  84. #if 1
  85. // No interpolation
  86. vec3 inv_dir = 1.0 / ray_dir;
  87. float rough = 0.5;
  88. bool hit = false;
  89. for (uint i = 0; i < params.max_cascades; i++) {
  90. //convert to local bounds
  91. vec3 pos = ray_pos - cascades.data[i].offset;
  92. pos *= cascades.data[i].to_cell;
  93. // Should never happen for debug, since we start mostly at the bounds center,
  94. // but add anyway.
  95. //if (any(lessThan(pos,vec3(0.0))) || any(greaterThanEqual(pos,params.grid_size))) {
  96. // continue; //already past bounds for this cascade, goto next
  97. //}
  98. //find maximum advance distance (until reaching bounds)
  99. vec3 t0 = -pos * inv_dir;
  100. vec3 t1 = (params.grid_size - pos) * inv_dir;
  101. vec3 tmax = max(t0, t1);
  102. float max_advance = min(tmax.x, min(tmax.y, tmax.z));
  103. float advance = 0.0;
  104. vec3 uvw;
  105. hit = false;
  106. while (advance < max_advance) {
  107. //read how much to advance from SDF
  108. uvw = (pos + ray_dir * advance) * pos_to_uvw;
  109. float distance = texture(sampler3D(sdf_cascades[i], linear_sampler), uvw).r * 255.0 - 1.7;
  110. if (distance < 0.001) {
  111. //consider hit
  112. hit = true;
  113. break;
  114. }
  115. advance += distance;
  116. }
  117. if (!hit) {
  118. pos += ray_dir * min(advance, max_advance);
  119. pos /= cascades.data[i].to_cell;
  120. pos += cascades.data[i].offset;
  121. ray_pos = pos;
  122. continue;
  123. }
  124. //compute albedo, emission and normal at hit point
  125. const float EPSILON = 0.001;
  126. vec3 hit_normal = normalize(vec3(
  127. texture(sampler3D(sdf_cascades[i], linear_sampler), uvw + vec3(EPSILON, 0.0, 0.0)).r - texture(sampler3D(sdf_cascades[i], linear_sampler), uvw - vec3(EPSILON, 0.0, 0.0)).r,
  128. texture(sampler3D(sdf_cascades[i], linear_sampler), uvw + vec3(0.0, EPSILON, 0.0)).r - texture(sampler3D(sdf_cascades[i], linear_sampler), uvw - vec3(0.0, EPSILON, 0.0)).r,
  129. texture(sampler3D(sdf_cascades[i], linear_sampler), uvw + vec3(0.0, 0.0, EPSILON)).r - texture(sampler3D(sdf_cascades[i], linear_sampler), uvw - vec3(0.0, 0.0, EPSILON)).r));
  130. vec3 hit_light = texture(sampler3D(light_cascades[i], linear_sampler), uvw).rgb;
  131. vec4 aniso0 = texture(sampler3D(aniso0_cascades[i], linear_sampler), uvw);
  132. vec3 hit_aniso0 = aniso0.rgb;
  133. vec3 hit_aniso1 = vec3(aniso0.a, texture(sampler3D(aniso1_cascades[i], linear_sampler), uvw).rg);
  134. hit_light *= (dot(max(vec3(0.0), (hit_normal * hit_aniso0)), vec3(1.0)) + dot(max(vec3(0.0), (-hit_normal * hit_aniso1)), vec3(1.0)));
  135. light = hit_light;
  136. break;
  137. }
  138. #endif
  139. imageStore(screen_buffer, screen_pos, vec4(linear_to_srgb(light), 1.0));
  140. }