vrs.glsl 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. #ifdef USE_MULTIVIEW
  5. #ifdef has_VK_KHR_multiview
  6. #extension GL_EXT_multiview : enable
  7. #define ViewIndex gl_ViewIndex
  8. #else // has_VK_KHR_multiview
  9. #define ViewIndex 0
  10. #endif // has_VK_KHR_multiview
  11. #endif //USE_MULTIVIEW
  12. #ifdef USE_MULTIVIEW
  13. layout(location = 0) out vec3 uv_interp;
  14. #else
  15. layout(location = 0) out vec2 uv_interp;
  16. #endif
  17. layout(push_constant, std430) uniform Params {
  18. float max_texel_factor;
  19. float res1;
  20. float res2;
  21. float res3;
  22. }
  23. params;
  24. void main() {
  25. vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
  26. gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
  27. uv_interp.xy = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
  28. #ifdef USE_MULTIVIEW
  29. uv_interp.z = ViewIndex;
  30. #endif
  31. }
  32. #[fragment]
  33. #version 450
  34. #VERSION_DEFINES
  35. #ifdef USE_MULTIVIEW
  36. #ifdef has_VK_KHR_multiview
  37. #extension GL_EXT_multiview : enable
  38. #define ViewIndex gl_ViewIndex
  39. #else // has_VK_KHR_multiview
  40. #define ViewIndex 0
  41. #endif // has_VK_KHR_multiview
  42. #endif //USE_MULTIVIEW
  43. #ifdef USE_MULTIVIEW
  44. layout(location = 0) in vec3 uv_interp;
  45. layout(set = 0, binding = 0) uniform sampler2DArray source_color;
  46. #else /* USE_MULTIVIEW */
  47. layout(location = 0) in vec2 uv_interp;
  48. layout(set = 0, binding = 0) uniform sampler2D source_color;
  49. #endif /* USE_MULTIVIEW */
  50. layout(location = 0) out uint frag_color;
  51. layout(push_constant, std430) uniform Params {
  52. float max_texel_factor;
  53. float res1;
  54. float res2;
  55. float res3;
  56. }
  57. params;
  58. void main() {
  59. #ifdef USE_MULTIVIEW
  60. vec3 uv = uv_interp;
  61. #else
  62. vec2 uv = uv_interp;
  63. #endif
  64. // Input is standardized. R for X, G for Y, 0.0 (0) = 1, 0.33 (85) = 2, 0.66 (170) = 3, 1.0 (255) = 8
  65. vec4 color = textureLod(source_color, uv, 0.0);
  66. // Output image shading rate image for VRS according to VK_KHR_fragment_shading_rate.
  67. color.r = clamp(floor(color.r * params.max_texel_factor + 0.1), 0.0, params.max_texel_factor);
  68. color.g = clamp(floor(color.g * params.max_texel_factor + 0.1), 0.0, params.max_texel_factor);
  69. // Note 1x4, 4x1, 1x8, 8x1, 2x8 and 8x2 are not supported:
  70. if (color.r < (color.g - 1.0)) {
  71. color.r = color.g - 1.0;
  72. }
  73. if (color.g < (color.r - 1.0)) {
  74. color.g = color.r - 1.0;
  75. }
  76. // Encode to frag_color;
  77. frag_color = int(color.r + 0.1) << 2;
  78. frag_color += int(color.g + 0.1);
  79. }