Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/smaa_edge_detection.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[3];
34
35 layout(push_constant, std430) uniform Params {
36 vec2 inv_size;
37 float threshold;
38 float reserved;
39 }
40 params;
41
42 void main() {
43 vec2 vertex_base;
44 if (gl_VertexIndex == 0) {
45 vertex_base = vec2(-1.0, -1.0);
46 } else if (gl_VertexIndex == 1) {
47 vertex_base = vec2(-1.0, 3.0);
48 } else {
49 vertex_base = vec2(3.0, -1.0);
50 }
51 gl_Position = vec4(vertex_base, 0.0, 1.0);
52 tex_coord = clamp(vertex_base, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
53
54 offset[0] = fma(params.inv_size.xyxy, vec4(-1.0, 0.0, 0.0, -1.0), tex_coord.xyxy);
55 offset[1] = fma(params.inv_size.xyxy, vec4(1.0, 0.0, 0.0, 1.0), tex_coord.xyxy);
56 offset[2] = fma(params.inv_size.xyxy, vec4(-2.0, 0.0, 0.0, -2.0), tex_coord.xyxy);
57 }
58
59 #[fragment]
60 #version 450
61
62 layout(location = 0) in vec2 tex_coord;
63 layout(location = 1) in vec4 offset[3];
64
65 layout(set = 0, binding = 0) uniform sampler2D color_tex;
66
67 layout(location = 0) out vec2 edges;
68
69 layout(push_constant, std430) uniform Params {
70 vec2 inv_size;
71 float threshold;
72 float reserved;
73 }
74 params;
75
76 #define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0
77
78 void main() {
79 vec2 threshold = vec2(params.threshold);
80
81 vec4 delta;
82 vec3 C = texture(color_tex, tex_coord).rgb;
83
84 vec3 Cleft = texture(color_tex, offset[0].xy).rgb;
85 vec3 t = abs(C - Cleft);
86 delta.x = max(max(t.r, t.g), t.b);
87
88 vec3 Ctop = texture(color_tex, offset[0].zw).rgb;
89 t = abs(C - Ctop);
90 delta.y = max(max(t.r, t.g), t.b);
91
92 edges = step(threshold, delta.xy);
93
94 if (dot(edges, vec2(1.0, 1.0)) == 0.0) {
95 discard;
96 }
97
98 vec3 Cright = texture(color_tex, offset[1].xy).rgb;
99 t = abs(C - Cright);
100 delta.z = max(max(t.r, t.g), t.b);
101
102 vec3 Cbottom = texture(color_tex, offset[1].zw).rgb;
103 t = abs(C - Cbottom);
104 delta.w = max(max(t.r, t.g), t.b);
105
106 vec2 max_delta = max(delta.xy, delta.zw);
107
108 vec3 Cleftleft = texture(color_tex, offset[2].xy).rgb;
109 t = abs(Cleft - Cleftleft);
110 delta.z = max(max(t.r, t.g), t.b);
111
112 vec3 Ctoptop = texture(color_tex, offset[2].zw).rgb;
113 t = abs(Ctop - Ctoptop);
114 delta.w = max(max(t.r, t.g), t.b);
115
116 max_delta = max(max_delta.xy, delta.zw);
117 float final_delta = max(max_delta.x, max_delta.y);
118
119 edges.xy *= step(final_delta, SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy);
120 }