blit.glsl 2.7 KB

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