blit.glsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 eye_center;
  10. float k1;
  11. float k2;
  12. float upscale;
  13. float aspect_ratio;
  14. uint layer;
  15. bool convert_to_srgb;
  16. bool use_debanding;
  17. float pad;
  18. }
  19. data;
  20. layout(location = 0) out vec2 uv;
  21. void main() {
  22. mat4 swapchain_transform = mat4(1.0);
  23. swapchain_transform[0][0] = data.rotation_cos;
  24. swapchain_transform[0][1] = -data.rotation_sin;
  25. swapchain_transform[1][0] = data.rotation_sin;
  26. swapchain_transform[1][1] = data.rotation_cos;
  27. 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));
  28. uv = data.src_rect.xy + base_arr[gl_VertexIndex] * data.src_rect.zw;
  29. vec2 vtx = data.dst_rect.xy + base_arr[gl_VertexIndex] * data.dst_rect.zw;
  30. gl_Position = swapchain_transform * vec4(vtx * 2.0 - 1.0, 0.0, 1.0);
  31. }
  32. #[fragment]
  33. #version 450
  34. #VERSION_DEFINES
  35. layout(push_constant, std140) uniform Pos {
  36. vec4 src_rect;
  37. vec4 dst_rect;
  38. float rotation_sin;
  39. float rotation_cos;
  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. bool use_debanding;
  48. float pad;
  49. }
  50. data;
  51. layout(location = 0) in vec2 uv;
  52. layout(location = 0) out vec4 color;
  53. #ifdef USE_LAYER
  54. layout(binding = 0) uniform sampler2DArray src_rt;
  55. #else
  56. layout(binding = 0) uniform sampler2D src_rt;
  57. #endif
  58. vec3 linear_to_srgb(vec3 color) {
  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. // From https://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
  63. // and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom)
  64. // NOTE: `frag_coord` is in pixels (i.e. not normalized UV).
  65. // This dithering must be applied after encoding changes (linear/nonlinear) have been applied
  66. // as the final step before quantization from floating point to integer values.
  67. vec3 screen_space_dither(vec2 frag_coord) {
  68. // Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR.
  69. // Removed the time component to avoid passing time into this shader.
  70. vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord));
  71. dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0));
  72. // Subtract 0.5 to avoid slightly brightening the whole viewport.
  73. // Use a dither strength of 100% rather than the 37.5% suggested by the original source.
  74. // Divide by 255 to align to 8-bit quantization.
  75. return (dither.rgb - 0.5) / 255.0;
  76. }
  77. void main() {
  78. #ifdef APPLY_LENS_DISTORTION
  79. vec2 coords = uv * 2.0 - 1.0;
  80. vec2 offset = coords - data.eye_center;
  81. // take aspect ratio into account
  82. offset.y /= data.aspect_ratio;
  83. // distort
  84. vec2 offset_sq = offset * offset;
  85. float radius_sq = offset_sq.x + offset_sq.y;
  86. float radius_s4 = radius_sq * radius_sq;
  87. float distortion_scale = 1.0 + (data.k1 * radius_sq) + (data.k2 * radius_s4);
  88. offset *= distortion_scale;
  89. // reapply aspect ratio
  90. offset.y *= data.aspect_ratio;
  91. // add our eye center back in
  92. coords = offset + data.eye_center;
  93. coords /= data.upscale;
  94. // and check our color
  95. if (coords.x < -1.0 || coords.y < -1.0 || coords.x > 1.0 || coords.y > 1.0) {
  96. color = vec4(0.0, 0.0, 0.0, 1.0);
  97. } else {
  98. // layer is always used here
  99. coords = (coords + vec2(1.0)) / vec2(2.0);
  100. color = texture(src_rt, vec3(coords, data.layer));
  101. }
  102. #elif defined(USE_LAYER)
  103. color = texture(src_rt, vec3(uv, data.layer));
  104. #else
  105. color = texture(src_rt, uv);
  106. #endif
  107. if (data.convert_to_srgb) {
  108. color.rgb = linear_to_srgb(color.rgb); // Regular linear -> SRGB conversion.
  109. // Even if debanding was applied earlier in the rendering process, it must
  110. // be reapplied after the linear_to_srgb floating point operations.
  111. // When the linear_to_srgb operation was not performed, the source is
  112. // already an 8-bit format and debanding cannot be effective. In this
  113. // case, GPU driver rounding error can add noise so debanding should be
  114. // skipped entirely.
  115. if (data.use_debanding) {
  116. color.rgb += screen_space_dither(gl_FragCoord.xy);
  117. }
  118. color.rgb = clamp(color.rgb, vec3(0.0), vec3(1.0));
  119. }
  120. }