Debian Codegraph @

servers/rendering/renderer_rd/shaders/canvas_sdf.glsl

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
8
9 layout(r8, set = 0, binding = 1) uniform restrict readonly image2D src_pixels;
10 layout(r16_snorm, set = 0, binding = 2) uniform restrict writeonly image2D dst_sdf;
11
12 layout(rg16i, set = 0, binding = 3) uniform restrict readonly iimage2D src_process;
13 layout(rg16i, set = 0, binding = 4) uniform restrict writeonly iimage2D dst_process;
14
15 layout(push_constant, std430) uniform Params {
16 ivec2 size;
17 int stride;
18 int shift;
19 ivec2 base_size;
20 uvec2 pad;
21 }
22 params;
23
24 #define SDF_MAX_LENGTH 16384.0
25
26 void main() {
27 ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
28 if (any(greaterThanEqual(pos, params.size))) { //too large, do nothing
29 return;
30 }
31
32 #ifdef MODE_LOAD
33
34 bool solid = imageLoad(src_pixels, pos).r > 0.5;
35 imageStore(dst_process, pos, solid ? ivec4(ivec2(-32767), 0, 0) : ivec4(ivec2(32767), 0, 0));
36 #endif
37
38 #ifdef MODE_LOAD_SHRINK
39
40 int s = 1 << params.shift;
41 ivec2 base = pos << params.shift;
42 ivec2 center = base + ivec2(params.shift);
43
44 ivec2 rel = ivec2(32767);
45 float d = 1e20;
46 int found = 0;
47 int solid_found = 0;
48 for (int i = 0; i < s; i++) {
49 for (int j = 0; j < s; j++) {
50 ivec2 src_pos = base + ivec2(i, j);
51 if (any(greaterThanEqual(src_pos, params.base_size))) {
52 continue;
53 }
54 bool solid = imageLoad(src_pixels, src_pos).r > 0.5;
55 if (solid) {
56 float dist = length(vec2(src_pos - center));
57 if (dist < d) {
58 d = dist;
59 rel = src_pos;
60 }
61 solid_found++;
62 }
63 found++;
64 }
65 }
66
67 if (solid_found == found) {
68 //mark solid only if all are solid
69 rel = ivec2(-32767);
70 }
71
72 imageStore(dst_process, pos, ivec4(rel, 0, 0));
73 #endif
74
75 #ifdef MODE_PROCESS
76
77 ivec2 base = pos << params.shift;
78 ivec2 center = base + ivec2(params.shift);
79
80 ivec2 rel = imageLoad(src_process, pos).xy;
81
82 bool solid = rel.x < 0;
83
84 if (solid) {
85 rel = -rel - ivec2(1);
86 }
87
88 if (center != rel) {
89 //only process if it does not point to itself
90 const int ofs_table_size = 8;
91 const ivec2 ofs_table[ofs_table_size] = ivec2[](
92 ivec2(-1, -1),
93 ivec2(0, -1),
94 ivec2(+1, -1),
95
96 ivec2(-1, 0),
97 ivec2(+1, 0),
98
99 ivec2(-1, +1),
100 ivec2(0, +1),
101 ivec2(+1, +1));
102
103 float dist = length(vec2(rel - center));
104 for (int i = 0; i < ofs_table_size; i++) {
105 ivec2 src_pos = pos + ofs_table[i] * params.stride;
106 if (any(lessThan(src_pos, ivec2(0))) || any(greaterThanEqual(src_pos, params.size))) {
107 continue;
108 }
109 ivec2 src_rel = imageLoad(src_process, src_pos).xy;
110 bool src_solid = src_rel.x < 0;
111 if (src_solid) {
112 src_rel = -src_rel - ivec2(1);
113 }
114
115 if (src_solid != solid) {
116 src_rel = ivec2(src_pos << params.shift); //point to itself if of different type
117 }
118
119 float src_dist = length(vec2(src_rel - center));
120 if (src_dist < dist) {
121 dist = src_dist;
122 rel = src_rel;
123 }
124 }
125 }
126
127 if (solid) {
128 rel = -rel - ivec2(1);
129 }
130
131 imageStore(dst_process, pos, ivec4(rel, 0, 0));
132 #endif
133
134 #ifdef MODE_STORE
135
136 ivec2 rel = imageLoad(src_process, pos).xy;
137
138 bool solid = rel.x < 0;
139
140 if (solid) {
141 rel = -rel - ivec2(1);
142 }
143
144 float d = length(vec2(rel - pos));
145
146 if (solid) {
147 d = -d;
148 }
149
150 d /= SDF_MAX_LENGTH;
151 d = clamp(d, -1.0, 1.0);
152 imageStore(dst_sdf, pos, vec4(d));
153
154 #endif
155
156 #ifdef MODE_STORE_SHRINK
157
158 ivec2 base = pos << params.shift;
159 ivec2 center = base + ivec2(params.shift);
160
161 ivec2 rel = imageLoad(src_process, pos).xy;
162
163 bool solid = rel.x < 0;
164
165 if (solid) {
166 rel = -rel - ivec2(1);
167 }
168
169 float d = length(vec2(rel - center));
170
171 if (solid) {
172 d = -d;
173 }
174 d /= SDF_MAX_LENGTH;
175 d = clamp(d, -1.0, 1.0);
176 imageStore(dst_sdf, pos, vec4(d));
177
178 #endif
179 }