cube_to_dp.glsl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #[vertex]
  2. #version 450
  3. VERSION_DEFINES
  4. layout(push_constant, binding = 1, std430) uniform Params {
  5. float z_far;
  6. float z_near;
  7. bool z_flip;
  8. uint pad;
  9. vec4 screen_rect;
  10. }
  11. params;
  12. layout(location = 0) out vec2 uv_interp;
  13. void main() {
  14. vec2 base_arr[4] = vec2[](vec2(0.0, 0.0), vec2(0.0, 1.0), vec2(1.0, 1.0), vec2(1.0, 0.0));
  15. uv_interp = base_arr[gl_VertexIndex];
  16. vec2 screen_pos = uv_interp * params.screen_rect.zw + params.screen_rect.xy;
  17. gl_Position = vec4(screen_pos * 2.0 - 1.0, 0.0, 1.0);
  18. }
  19. #[fragment]
  20. #version 450
  21. VERSION_DEFINES
  22. layout(location = 0) in vec2 uv_interp;
  23. layout(set = 0, binding = 0) uniform samplerCube source_cube;
  24. layout(push_constant, binding = 1, std430) uniform Params {
  25. float z_far;
  26. float z_near;
  27. bool z_flip;
  28. uint pad;
  29. vec4 screen_rect;
  30. }
  31. params;
  32. void main() {
  33. vec2 uv = uv_interp;
  34. vec3 normal = vec3(uv * 2.0 - 1.0, 0.0);
  35. normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y));
  36. normal = normalize(normal);
  37. normal.y = -normal.y; //needs to be flipped to match projection matrix
  38. if (!params.z_flip) {
  39. normal.z = -normal.z;
  40. }
  41. float depth = texture(source_cube, normal).r;
  42. // absolute values for direction cosines, bigger value equals closer to basis axis
  43. vec3 unorm = abs(normal);
  44. if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) {
  45. // x code
  46. unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0);
  47. } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) {
  48. // y code
  49. unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0);
  50. } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) {
  51. // z code
  52. unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0);
  53. } else {
  54. // oh-no we messed up code
  55. // has to be
  56. unorm = vec3(1.0, 0.0, 0.0);
  57. }
  58. float depth_fix = 1.0 / dot(normal, unorm);
  59. depth = 2.0 * depth - 1.0;
  60. float linear_depth = 2.0 * params.z_near * params.z_far / (params.z_far + params.z_near - depth * (params.z_far - params.z_near));
  61. depth = (linear_depth * depth_fix) / params.z_far;
  62. gl_FragDepth = depth;
  63. }