Debian Codegraph @

servers/rendering/renderer_rd/shaders/blit.glsl

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