Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/smaa_blending.glsl

raw
1 /**
2 * Copyright (C) 2013 Jorge Jimenez ([email protected])
3 * Copyright (C) 2013 Jose I. Echevarria ([email protected])
4 * Copyright (C) 2013 Belen Masia ([email protected])
5 * Copyright (C) 2013 Fernando Navarro ([email protected])
6 * Copyright (C) 2013 Diego Gutierrez ([email protected])
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * this software and associated documentation files (the "Software"), to deal in
10 * the Software without restriction, including without limitation the rights to
11 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is furnished to
13 * do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software. As clarification, there
17 * is no requirement that the copyright notice and permission be included in
18 * binary distributions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #[vertex]
30 #version 450
31
32 layout(location = 0) out vec2 tex_coord;
33 layout(location = 1) out vec4 offset;
34
35 layout(push_constant, std430) uniform Params {
36 vec2 inv_size;
37 vec2 pad;
38 }
39 params;
40
41 void main() {
42 vec2 vertex_base;
43 if (gl_VertexIndex == 0) {
44 vertex_base = vec2(-1.0, -1.0);
45 } else if (gl_VertexIndex == 1) {
46 vertex_base = vec2(-1.0, 3.0);
47 } else {
48 vertex_base = vec2(3.0, -1.0);
49 }
50 gl_Position = vec4(vertex_base, 0.0, 1.0);
51 tex_coord = clamp(vertex_base, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
52 offset = fma(params.inv_size.xyxy, vec4(1.0, 0.0, 0.0, 1.0), tex_coord.xyxy);
53 }
54
55 #[fragment]
56 #version 450
57
58 layout(location = 0) in vec2 tex_coord;
59 layout(location = 1) in vec4 offset;
60 layout(set = 0, binding = 0) uniform sampler2D color_tex;
61 layout(set = 1, binding = 0) uniform sampler2D blend_tex;
62
63 layout(location = 0) out vec4 out_color;
64
65 #define FLAG_USE_8_BIT_DEBANDING (1 << 0)
66 #define FLAG_USE_10_BIT_DEBANDING (1 << 1)
67
68 layout(push_constant, std430) uniform Params {
69 vec2 inv_size;
70 uint use_debanding;
71 float pad;
72 }
73 params;
74
75 #define textureLinear(tex, uv) srgb_to_linear(textureLod(tex, uv, 0.0).rgb)
76
77 vec3 linear_to_srgb(vec3 color) {
78 // If going to srgb, clamp from 0 to 1.
79 color = clamp(color, vec3(0.0), vec3(1.0));
80 const vec3 a = vec3(0.055f);
81 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)));
82 }
83
84 vec3 srgb_to_linear(vec3 color) {
85 return mix(pow((color.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), color.rgb * (1.0 / 12.92), lessThan(color.rgb, vec3(0.04045)));
86 }
87
88 void SMAAMovc(bvec2 cond, inout vec2 variable, vec2 value) {
89 if (cond.x) {
90 variable.x = value.x;
91 }
92 if (cond.y) {
93 variable.y = value.y;
94 }
95 }
96
97 void SMAAMovc(bvec4 cond, inout vec4 variable, vec4 value) {
98 SMAAMovc(cond.xy, variable.xy, value.xy);
99 SMAAMovc(cond.zw, variable.zw, value.zw);
100 }
101
102 // From https://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
103 // and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom)
104 // NOTE: `frag_coord` is in pixels (i.e. not normalized UV).
105 // This dithering must be applied after encoding changes (linear/nonlinear) have been applied
106 // as the final step before quantization from floating point to integer values.
107 vec3 screen_space_dither(vec2 frag_coord, float bit_alignment_diviser) {
108 // Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR.
109 // Removed the time component to avoid passing time into this shader.
110 vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord));
111 dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0));
112
113 // Subtract 0.5 to avoid slightly brightening the whole viewport.
114 // Use a dither strength of 100% rather than the 37.5% suggested by the original source.
115 return (dither.rgb - 0.5) / bit_alignment_diviser;
116 }
117
118 void main() {
119 vec4 a;
120 a.x = texture(blend_tex, offset.xy).a;
121 a.y = texture(blend_tex, offset.zw).g;
122 a.wz = texture(blend_tex, tex_coord).xz;
123
124 if (dot(a, vec4(1.0, 1.0, 1.0, 1.0)) < 1e-5) {
125 out_color = textureLod(color_tex, tex_coord, 0.0);
126 } else {
127 bool h = max(a.x, a.z) > max(a.y, a.w);
128
129 vec4 blending_offset = vec4(0.0, a.y, 0.0, a.w);
130 vec2 blending_weight = a.yw;
131
132 SMAAMovc(bvec4(h, h, h, h), blending_offset, vec4(a.x, 0.0, a.z, 0.0));
133 SMAAMovc(bvec2(h, h), blending_weight, a.xz);
134 blending_weight /= dot(blending_weight, vec2(1.0, 1.0));
135
136 vec4 blending_coord = fma(blending_offset, vec4(params.inv_size.xy, -params.inv_size.xy), tex_coord.xyxy);
137
138 out_color.rgb = blending_weight.x * textureLinear(color_tex, blending_coord.xy);
139 out_color.rgb += blending_weight.y * textureLinear(color_tex, blending_coord.zw);
140 out_color.rgb = linear_to_srgb(out_color.rgb);
141 out_color.a = texture(color_tex, tex_coord).a;
142 }
143 if (bool(params.use_debanding)) {
144 // Divide by 255 to align to 8-bit quantization.
145 out_color.rgb += screen_space_dither(gl_FragCoord.xy, 255.0);
146 }
147 }