blit.glsl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(push_constant, std140) uniform Pos {
  5. vec4 src_rect;
  6. vec4 dst_rect;
  7. vec2 eye_center;
  8. float k1;
  9. float k2;
  10. float upscale;
  11. float aspect_ratio;
  12. uint layer;
  13. uint pad1;
  14. }
  15. data;
  16. layout(location = 0) out vec2 uv;
  17. void main() {
  18. 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));
  19. uv = data.src_rect.xy + base_arr[gl_VertexIndex] * data.src_rect.zw;
  20. vec2 vtx = data.dst_rect.xy + base_arr[gl_VertexIndex] * data.dst_rect.zw;
  21. gl_Position = vec4(vtx * 2.0 - 1.0, 0.0, 1.0);
  22. }
  23. #[fragment]
  24. #version 450
  25. #VERSION_DEFINES
  26. layout(push_constant, std140) uniform Pos {
  27. vec4 src_rect;
  28. vec4 dst_rect;
  29. vec2 eye_center;
  30. float k1;
  31. float k2;
  32. float upscale;
  33. float aspect_ratio;
  34. uint layer;
  35. bool convert_to_srgb;
  36. }
  37. data;
  38. layout(location = 0) in vec2 uv;
  39. layout(location = 0) out vec4 color;
  40. #ifdef USE_LAYER
  41. layout(binding = 0) uniform sampler2DArray src_rt;
  42. #else
  43. layout(binding = 0) uniform sampler2D src_rt;
  44. #endif
  45. vec3 linear_to_srgb(vec3 color) {
  46. // If going to srgb, clamp from 0 to 1.
  47. color = clamp(color, vec3(0.0), vec3(1.0));
  48. const vec3 a = vec3(0.055f);
  49. 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)));
  50. }
  51. void main() {
  52. #ifdef APPLY_LENS_DISTORTION
  53. vec2 coords = uv * 2.0 - 1.0;
  54. vec2 offset = coords - data.eye_center;
  55. // take aspect ratio into account
  56. offset.y /= data.aspect_ratio;
  57. // distort
  58. vec2 offset_sq = offset * offset;
  59. float radius_sq = offset_sq.x + offset_sq.y;
  60. float radius_s4 = radius_sq * radius_sq;
  61. float distortion_scale = 1.0 + (data.k1 * radius_sq) + (data.k2 * radius_s4);
  62. offset *= distortion_scale;
  63. // reapply aspect ratio
  64. offset.y *= data.aspect_ratio;
  65. // add our eye center back in
  66. coords = offset + data.eye_center;
  67. coords /= data.upscale;
  68. // and check our color
  69. if (coords.x < -1.0 || coords.y < -1.0 || coords.x > 1.0 || coords.y > 1.0) {
  70. color = vec4(0.0, 0.0, 0.0, 1.0);
  71. } else {
  72. // layer is always used here
  73. coords = (coords + vec2(1.0)) / vec2(2.0);
  74. color = texture(src_rt, vec3(coords, data.layer));
  75. }
  76. #elif defined(USE_LAYER)
  77. color = texture(src_rt, vec3(uv, data.layer));
  78. #else
  79. color = texture(src_rt, uv);
  80. #endif
  81. if (data.convert_to_srgb) {
  82. color.rgb = linear_to_srgb(color.rgb); // Regular linear -> SRGB conversion.
  83. }
  84. }