Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/smaa_weight_calculation.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 vec2 pix_coord;
34 layout(location = 2) out vec4 offset[3];
35
36 layout(push_constant, std430) uniform Params {
37 vec2 inv_size;
38 ivec2 size;
39
40 vec4 subsample_indices;
41 }
42 params;
43
44 #define SMAA_MAX_SEARCH_STEPS 32
45
46 void main() {
47 vec2 vertex_base;
48 if (gl_VertexIndex == 0) {
49 vertex_base = vec2(-1.0, -1.0);
50 } else if (gl_VertexIndex == 1) {
51 vertex_base = vec2(-1.0, 3.0);
52 } else {
53 vertex_base = vec2(3.0, -1.0);
54 }
55 gl_Position = vec4(vertex_base, 0.0, 1.0);
56 tex_coord = clamp(vertex_base, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
57 pix_coord = tex_coord * params.size.xy;
58
59 offset[0] = fma(params.inv_size.xyxy, vec4(-0.25, -0.125, 1.25, -0.125), tex_coord.xyxy);
60 offset[1] = fma(params.inv_size.xyxy, vec4(-0.125, -0.25, -0.125, 1.25), tex_coord.xyxy);
61 offset[2] = fma(params.inv_size.xxyy,
62 vec4(-2.0, 2.0, -2.0, 2.0) * SMAA_MAX_SEARCH_STEPS,
63 vec4(offset[0].xz, offset[1].yw));
64 }
65
66 #[fragment]
67 #version 450
68
69 layout(location = 0) in vec2 tex_coord;
70 layout(location = 1) in vec2 pix_coord;
71 layout(location = 2) in vec4 offset[3];
72 layout(set = 0, binding = 0) uniform sampler2D edges_tex;
73 layout(set = 1, binding = 0) uniform sampler2D area_tex;
74 layout(set = 1, binding = 1) uniform sampler2D search_tex;
75 layout(location = 0) out vec4 weights;
76
77 layout(push_constant, std430) uniform Params {
78 vec2 inv_size;
79 ivec2 size;
80
81 vec4 subsample_indices;
82 }
83 params;
84
85 #define SMAA_MAX_SEARCH_STEPS 32
86 #define SMAA_MAX_SEARCH_STEPS_DIAG 16
87 #define SMAA_CORNER_ROUNDING 25
88
89 #ifndef SMAA_AREATEX_SELECT
90 #define SMAA_AREATEX_SELECT(sample) sample.rg
91 #endif
92
93 #ifndef SMAA_SEARCHTEX_SELECT
94 #define SMAA_SEARCHTEX_SELECT(sample) sample.r
95 #endif
96
97 #define SMAA_AREATEX_MAX_DISTANCE 16
98 #define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
99 #define SMAA_AREATEX_PIXEL_SIZE (1.0 / vec2(160.0, 560.0))
100 #define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 7.0)
101 #define SMAA_SEARCHTEX_SIZE vec2(66.0, 33.0)
102 #define SMAA_SEARCHTEX_PACKED_SIZE vec2(64.0, 16.0)
103 #define SMAA_CORNER_ROUNDING_NORM (float(SMAA_CORNER_ROUNDING) / 100.0)
104
105 void SMAAMovc(bvec2 cond, inout vec2 variable, vec2 value) {
106 if (cond.x) {
107 variable.x = value.x;
108 }
109 if (cond.y) {
110 variable.y = value.y;
111 }
112 }
113
114 vec2 SMAADecodeDiagBilinearAccess(vec2 e) {
115 e.r = e.r * abs(5.0 * e.r - 5.0 * 0.75);
116 return round(e);
117 }
118
119 vec4 SMAADecodeDiagBilinearAccess(vec4 e) {
120 e.rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75);
121 return round(e);
122 }
123
124 vec2 SMAASearchDiag1(vec2 tex_coord, vec2 dir, out vec2 e) {
125 vec4 coord = vec4(tex_coord, -1.0, 1.0);
126 vec3 t = vec3(params.inv_size.xy, 1.0);
127 while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) &&
128 coord.w > 0.9) {
129 coord.xyz = fma(t, vec3(dir, 1.0), coord.xyz);
130 e = textureLod(edges_tex, coord.xy, 0.0).rg;
131 coord.w = dot(e, vec2(0.5, 0.5));
132 }
133 return coord.zw;
134 }
135
136 vec2 SMAASearchDiag2(vec2 tex_coord, vec2 dir, out vec2 e) {
137 vec4 coord = vec4(tex_coord, -1.0, 1.0);
138 coord.x += 0.25 * params.inv_size.x;
139 vec3 t = vec3(params.inv_size.xy, 1.0);
140 while (coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) &&
141 coord.w > 0.9) {
142 coord.xyz = fma(t, vec3(dir, 1.0), coord.xyz);
143
144 e = textureLod(edges_tex, coord.xy, 0.0).rg;
145 e = SMAADecodeDiagBilinearAccess(e);
146
147 coord.w = dot(e, vec2(0.5, 0.5));
148 }
149 return coord.zw;
150 }
151
152 vec2 SMAAAreaDiag(vec2 dist, vec2 e, float offset) {
153 vec2 coord = fma(vec2(SMAA_AREATEX_MAX_DISTANCE_DIAG, SMAA_AREATEX_MAX_DISTANCE_DIAG), e, dist);
154
155 coord = fma(SMAA_AREATEX_PIXEL_SIZE, coord, 0.5 * SMAA_AREATEX_PIXEL_SIZE);
156
157 coord.x += 0.5;
158
159 coord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
160
161 return SMAA_AREATEX_SELECT(textureLod(area_tex, coord, 0.0));
162 }
163
164 vec2 SMAACalculateDiagWeights(vec2 tex_coord, vec2 e, vec4 subsample_indices) {
165 vec2 weights = vec2(0.0, 0.0);
166
167 vec4 d;
168 vec2 end;
169 if (e.r > 0.0) {
170 d.xz = SMAASearchDiag1(tex_coord, vec2(-1.0, 1.0), end);
171 d.x += float(end.y > 0.9);
172 } else {
173 d.xz = vec2(0.0, 0.0);
174 }
175 d.yw = SMAASearchDiag1(tex_coord, vec2(1.0, -1.0), end);
176
177 if (d.x + d.y > 2.0) {
178 vec4 coords = fma(vec4(-d.x + 0.25, d.x, d.y, -d.y - 0.25), params.inv_size.xyxy, tex_coord.xyxy);
179 vec4 c;
180 c.xy = textureLodOffset(edges_tex, coords.xy, 0.0, ivec2(-1, 0)).rg;
181 c.zw = textureLodOffset(edges_tex, coords.zw, 0.0, ivec2(1, 0)).rg;
182 c.yxwz = SMAADecodeDiagBilinearAccess(c.xyzw);
183
184 vec2 cc = fma(vec2(2.0, 2.0), c.xz, c.yw);
185
186 SMAAMovc(bvec2(step(0.9, d.zw)), cc, vec2(0.0, 0.0));
187
188 weights += SMAAAreaDiag(d.xy, cc, subsample_indices.z);
189 }
190
191 d.xz = SMAASearchDiag2(tex_coord, vec2(-1.0, -1.0), end);
192 if (textureLodOffset(edges_tex, tex_coord, 0.0, ivec2(1, 0)).r > 0.0) {
193 d.yw = SMAASearchDiag2(tex_coord, vec2(1.0, 1.0), end);
194 d.y += float(end.y > 0.9);
195 } else {
196 d.yw = vec2(0.0, 0.0);
197 }
198
199 if (d.x + d.y > 2.0) {
200 vec4 coords = fma(vec4(-d.x, -d.x, d.y, d.y), params.inv_size.xyxy, tex_coord.xyxy);
201 vec4 c;
202 c.x = textureLodOffset(edges_tex, coords.xy, 0.0, ivec2(-1, 0)).g;
203 c.y = textureLodOffset(edges_tex, coords.xy, 0.0, ivec2(0, -1)).r;
204 c.zw = textureLodOffset(edges_tex, coords.zw, 0.0, ivec2(1, 0)).gr;
205 vec2 cc = fma(vec2(2.0, 2.0), c.xz, c.yw);
206
207 SMAAMovc(bvec2(step(0.9, d.zw)), cc, vec2(0.0, 0.0));
208
209 weights += SMAAAreaDiag(d.xy, cc, subsample_indices.w).gr;
210 }
211
212 return weights;
213 }
214
215 float SMAASearchLength(vec2 e, float offset) {
216 vec2 scale = SMAA_SEARCHTEX_SIZE * vec2(0.5, -1.0);
217 vec2 bias = SMAA_SEARCHTEX_SIZE * vec2(offset, 1.0);
218
219 scale += vec2(-1.0, 1.0);
220 bias += vec2(0.5, -0.5);
221
222 scale *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
223 bias *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
224
225 return SMAA_SEARCHTEX_SELECT(textureLod(search_tex, fma(scale, e, bias), 0.0));
226 }
227
228 float SMAASearchXLeft(vec2 tex_coord, float end) {
229 vec2 e = vec2(0.0, 1.0);
230 while (tex_coord.x > end &&
231 e.g > 0.8281 &&
232 e.r == 0.0) {
233 e = textureLod(edges_tex, tex_coord, 0.0).rg;
234 tex_coord = fma(-vec2(2.0, 0.0), params.inv_size.xy, tex_coord);
235 }
236
237 float offset = fma(-(255.0 / 127.0), SMAASearchLength(e, 0.0), 3.25);
238 return fma(params.inv_size.x, offset, tex_coord.x);
239 }
240
241 float SMAASearchXRight(vec2 tex_coord, float end) {
242 vec2 e = vec2(0.0, 1.0);
243 while (tex_coord.x < end &&
244 e.g > 0.8281 &&
245 e.r == 0.0) {
246 e = textureLod(edges_tex, tex_coord, 0.0).rg;
247 tex_coord = fma(vec2(2.0, 0.0), params.inv_size.xy, tex_coord);
248 }
249
250 float offset = fma(-(255.0 / 127.0), SMAASearchLength(e, 0.5), 3.25);
251 return fma(-params.inv_size.x, offset, tex_coord.x);
252 }
253
254 float SMAASearchYUp(vec2 tex_coord, float end) {
255 vec2 e = vec2(1.0, 0.0);
256 while (tex_coord.y > end &&
257 e.r > 0.8281 &&
258 e.g == 0.0) {
259 e = textureLod(edges_tex, tex_coord, 0.0).rg;
260 tex_coord = fma(-vec2(0.0, 2.0), params.inv_size.xy, tex_coord);
261 }
262
263 float offset = fma(-(255.0 / 127.0), SMAASearchLength(e.gr, 0.0), 3.25);
264 return fma(params.inv_size.y, offset, tex_coord.y);
265 }
266
267 float SMAASearchYDown(vec2 tex_coord, float end) {
268 vec2 e = vec2(1.0, 0.0);
269 while (tex_coord.y < end &&
270 e.r > 0.8281 &&
271 e.g == 0.0) {
272 e = textureLod(edges_tex, tex_coord, 0.0).rg;
273 tex_coord = fma(vec2(0.0, 2.0), params.inv_size.xy, tex_coord);
274 }
275
276 float offset = fma(-(255.0 / 127.0), SMAASearchLength(e.gr, 0.5), 3.25);
277 return fma(-params.inv_size.y, offset, tex_coord.y);
278 }
279
280 vec2 SMAAArea(vec2 dist, float e1, float e2, float offset) {
281 vec2 tex_coord = fma(vec2(SMAA_AREATEX_MAX_DISTANCE, SMAA_AREATEX_MAX_DISTANCE), round(4.0 * vec2(e1, e2)), dist);
282
283 tex_coord = fma(SMAA_AREATEX_PIXEL_SIZE, tex_coord, 0.5 * SMAA_AREATEX_PIXEL_SIZE);
284
285 tex_coord.y = fma(SMAA_AREATEX_SUBTEX_SIZE, offset, tex_coord.y);
286
287 return SMAA_AREATEX_SELECT(textureLod(area_tex, tex_coord, 0.0));
288 }
289
290 void SMAADetectHorizontalCornerPattern(inout vec2 weights, vec4 coord, vec2 d) {
291 vec2 left_right = step(d.xy, d.yx);
292 vec2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * left_right;
293
294 rounding /= left_right.x + left_right.y;
295
296 vec2 factor = vec2(1.0, 1.0);
297 factor.x -= rounding.x * textureLodOffset(edges_tex, coord.xy, 0.0, ivec2(0, 1)).r;
298 factor.x -= rounding.y * textureLodOffset(edges_tex, coord.zw, 0.0, ivec2(1, 1)).r;
299 factor.y -= rounding.x * textureLodOffset(edges_tex, coord.xy, 0.0, ivec2(0, -2)).r;
300 factor.y -= rounding.y * textureLodOffset(edges_tex, coord.zw, 0.0, ivec2(1, -2)).r;
301
302 weights *= clamp(factor, 0.0, 1.0);
303 }
304
305 void SMAADetectVerticalCornerPattern(inout vec2 weights, vec4 coord, vec2 d) {
306 vec2 left_right = step(d.xy, d.yx);
307 vec2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * left_right;
308
309 rounding /= left_right.x + left_right.y;
310
311 vec2 factor = vec2(1.0, 1.0);
312 factor.x -= rounding.x * textureLodOffset(edges_tex, coord.xy, 0.0, ivec2(1, 0)).g;
313 factor.x -= rounding.y * textureLodOffset(edges_tex, coord.zw, 0.0, ivec2(1, 1)).g;
314 factor.y -= rounding.x * textureLodOffset(edges_tex, coord.xy, 0.0, ivec2(-2, 0)).g;
315 factor.y -= rounding.y * textureLodOffset(edges_tex, coord.zw, 0.0, ivec2(-2, 1)).g;
316
317 weights *= clamp(factor, 0.0, 1.0);
318 }
319
320 void main() {
321 weights = vec4(0.0, 0.0, 0.0, 0.0);
322 vec2 e = textureLod(edges_tex, tex_coord, 0.0).rg;
323
324 if (e.g > 0.0) { // Edge at north.
325 weights.rg = SMAACalculateDiagWeights(tex_coord, e, params.subsample_indices);
326 if (weights.r == -weights.g) {
327 vec2 d;
328 vec3 coords;
329 coords.x = SMAASearchXLeft(offset[0].xy, offset[2].x);
330 coords.y = offset[1].y;
331 d.x = coords.x;
332
333 float e1 = textureLod(edges_tex, coords.xy, 0.0).r;
334
335 coords.z = SMAASearchXRight(offset[0].zw, offset[2].y);
336 d.y = coords.z;
337
338 d = abs(round(fma(params.size.xx, d, -pix_coord.xx)));
339
340 vec2 sqrt_d = sqrt(d);
341
342 float e2 = textureLodOffset(edges_tex, coords.zy, 0.0, ivec2(1, 0)).r;
343
344 weights.rg = SMAAArea(sqrt_d, e1, e2, params.subsample_indices.y);
345
346 coords.y = tex_coord.y;
347 SMAADetectHorizontalCornerPattern(weights.rg, coords.xyzy, d);
348 } else {
349 e.r = 0.0;
350 }
351 }
352
353 if (e.r > 0.0) { // Edge at west.
354 vec2 d;
355 vec3 coords;
356 coords.y = SMAASearchYUp(offset[1].xy, offset[2].z);
357 coords.x = offset[0].x;
358 d.x = coords.y;
359
360 float e1 = textureLod(edges_tex, coords.xy, 0.0).g;
361
362 coords.z = SMAASearchYDown(offset[1].zw, offset[2].w);
363 d.y = coords.z;
364
365 d = abs(round(fma(params.size.yy, d, -pix_coord.yy)));
366
367 vec2 sqrt_d = sqrt(d);
368
369 float e2 = textureLodOffset(edges_tex, coords.xz, 0.0, ivec2(0, 1)).g;
370
371 weights.ba = SMAAArea(sqrt_d, e1, e2, params.subsample_indices.x);
372
373 coords.x = tex_coord.x;
374 SMAADetectVerticalCornerPattern(weights.ba, coords.xyxz, d);
375 }
376 }