Debian Codegraph @

drivers/gles3/shaders/canvas_sdf.glsl

raw
1 /* clang-format off */
2 #[modes]
3
4 mode_load = #define MODE_LOAD
5 mode_load_shrink = #define MODE_LOAD_SHRINK
6 mode_process = #define MODE_PROCESS
7 mode_store = #define MODE_STORE
8 mode_store_shrink = #define MODE_STORE_SHRINK
9
10 #[specializations]
11
12 #[vertex]
13
14 layout(location = 0) in vec2 vertex_attrib;
15
16 /* clang-format on */
17
18 uniform ivec2 size;
19 uniform int stride;
20 uniform int shift;
21 uniform ivec2 base_size;
22
23 void main() {
24 gl_Position = vec4(vertex_attrib, 1.0, 1.0);
25 }
26
27 /* clang-format off */
28 #[fragment]
29
30 #define SDF_MAX_LENGTH 16384.0
31
32 #if defined(MODE_LOAD) || defined(MODE_LOAD_SHRINK)
33 uniform lowp sampler2D src_pixels;//texunit:0
34 #else
35 uniform highp isampler2D src_process;//texunit:0
36 #endif
37
38 uniform ivec2 size;
39 uniform int stride;
40 uniform int shift;
41 uniform ivec2 base_size;
42
43 #if defined(MODE_LOAD) || defined(MODE_LOAD_SHRINK) || defined(MODE_PROCESS)
44 layout(location = 0) out ivec4 distance_field;
45 #else
46 layout(location = 0) out vec4 distance_field;
47 #endif
48
49 vec4 float_to_vec4(float p_float) {
50 highp vec4 comp = fract(p_float * vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0));
51 comp -= comp.xxyz * vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
52 return comp;
53 }
54
55 void main() {
56 ivec2 pos = ivec2(gl_FragCoord.xy);
57
58 #ifdef MODE_LOAD
59
60 bool solid = texelFetch(src_pixels, pos, 0).r > 0.5;
61 distance_field = solid ? ivec4(ivec2(-32767), 0, 0) : ivec4(ivec2(32767), 0, 0);
62 #endif
63
64 #ifdef MODE_LOAD_SHRINK
65
66 int s = 1 << shift;
67 ivec2 base = pos << shift;
68 ivec2 center = base + ivec2(shift);
69
70 ivec2 rel = ivec2(32767);
71 float d = 1e20;
72 int found = 0;
73 int solid_found = 0;
74 for (int i = 0; i < s; i++) {
75 for (int j = 0; j < s; j++) {
76 ivec2 src_pos = base + ivec2(i, j);
77 if (any(greaterThanEqual(src_pos, base_size))) {
78 continue;
79 }
80 bool solid = texelFetch(src_pixels, src_pos, 0).r > 0.5;
81 if (solid) {
82 float dist = length(vec2(src_pos - center));
83 if (dist < d) {
84 d = dist;
85 rel = src_pos;
86 }
87 solid_found++;
88 }
89 found++;
90 }
91 }
92
93 if (solid_found == found) {
94 //mark solid only if all are solid
95 rel = ivec2(-32767);
96 }
97
98 distance_field = ivec4(rel, 0, 0);
99 #endif
100
101 #ifdef MODE_PROCESS
102
103 ivec2 base = pos << shift;
104 ivec2 center = base + ivec2(shift);
105
106 ivec2 rel = texelFetch(src_process, pos, 0).xy;
107
108 bool solid = rel.x < 0;
109
110 if (solid) {
111 rel = -rel - ivec2(1);
112 }
113
114 if (center != rel) {
115 //only process if it does not point to itself
116 const int ofs_table_size = 8;
117 const ivec2 ofs_table[ofs_table_size] = ivec2[](
118 ivec2(-1, -1),
119 ivec2(0, -1),
120 ivec2(+1, -1),
121
122 ivec2(-1, 0),
123 ivec2(+1, 0),
124
125 ivec2(-1, +1),
126 ivec2(0, +1),
127 ivec2(+1, +1));
128
129 float dist = length(vec2(rel - center));
130 for (int i = 0; i < ofs_table_size; i++) {
131 ivec2 src_pos = pos + ofs_table[i] * stride;
132 if (any(lessThan(src_pos, ivec2(0))) || any(greaterThanEqual(src_pos, size))) {
133 continue;
134 }
135 ivec2 src_rel = texelFetch(src_process, src_pos, 0).xy;
136 bool src_solid = src_rel.x < 0;
137 if (src_solid) {
138 src_rel = -src_rel - ivec2(1);
139 }
140
141 if (src_solid != solid) {
142 src_rel = ivec2(src_pos << shift); //point to itself if of different type
143 }
144
145 float src_dist = length(vec2(src_rel - center));
146 if (src_dist < dist) {
147 dist = src_dist;
148 rel = src_rel;
149 }
150 }
151 }
152
153 if (solid) {
154 rel = -rel - ivec2(1);
155 }
156
157 distance_field = ivec4(rel, 0, 0);
158 #endif
159
160 #ifdef MODE_STORE
161
162 ivec2 rel = texelFetch(src_process, pos, 0).xy;
163
164 bool solid = rel.x < 0;
165
166 if (solid) {
167 rel = -rel - ivec2(1);
168 }
169
170 float d = length(vec2(rel - pos));
171
172 if (solid) {
173 d = -d;
174 }
175
176 d /= SDF_MAX_LENGTH;
177 d = clamp(d, -1.0, 1.0);
178 distance_field = float_to_vec4(d*0.5+0.5);
179
180 #endif
181
182 #ifdef MODE_STORE_SHRINK
183
184 ivec2 base = pos << shift;
185 ivec2 center = base + ivec2(shift);
186
187 ivec2 rel = texelFetch(src_process, pos, 0).xy;
188
189 bool solid = rel.x < 0;
190
191 if (solid) {
192 rel = -rel - ivec2(1);
193 }
194
195 float d = length(vec2(rel - center));
196
197 if (solid) {
198 d = -d;
199 }
200 d /= SDF_MAX_LENGTH;
201 d = clamp(d, -1.0, 1.0);
202 distance_field = float_to_vec4(d*0.5+0.5);
203
204 #endif
205 }