Debian Codegraph @

Data/Sys/Shaders/asciiart.glsl

raw
1 /*
2 [configuration]
3
4 [OptionBool]
5 GUIName = Use target window resolution
6 OptionName = USE_WINDOW_RES
7 DefaultValue = true
8
9 [OptionBool]
10 GUIName = Debug: Calculate only one character per subgroup
11 OptionName = DEBUG_ONLY_ONE_CHAR
12 DefaultValue = false
13
14 [/configuration]
15 */
16
17 const uint MAX_CHARS = 96u; // max 96, must be a multiple of 32
18 const bool HAVE_FULL_FEATURE_FALLBACK = false; // terrible slow, can easily softlock the GPU
19 const uint UNROLL_FALLBACK = 4;
20 const uint UNROLL_SIMD = 3; // max MAX_CHARS / 32
21
22 // #undef SUPPORTS_SUBGROUP_REDUCTION
23
24 #ifdef API_VULKAN
25 // By default, subgroupBroadcast only supports compile time constants as index.
26 // However we need an uniform instead. This is always supported in OpenGL,
27 // but in Vulkan only in SPIR-V >= 1.5.
28 // So fall back to subgroupShuffle on Vulkan instead.
29 #define subgroupBroadcast subgroupShuffle
30 #endif
31
32 /*
33 The header-only font
34 We have 96 (ASCII) characters, each of them is 12 pixels high and 8 pixels wide.
35 To store the boolean value per pixel, 96 bits per character is needed.
36 So three 32 bit integers are used per character.
37 This takes in total roughly 1 kB of constant buffer.
38 The first character must be all-one for the optimized implementation below.
39 */
40 const uint char_width = 8;
41 const uint char_height = 12;
42 const uint char_count = 96;
43 const uint char_pixels = char_width * char_height;
44 const float2 char_dim = float2(char_width, char_height);
45
46 const uint rasters[char_count][(char_pixels + 31) / 32] = {
47 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}, {0x18181818, 0x00181818, 0x00181800},
48 {0x6c6c6c6c, 0x00000000, 0x00000000}, {0x66660000, 0xff6666ff, 0x00006666},
49 {0x1bff7e18, 0xd8f87e1f, 0x00187eff}, {0x6edb1b0e, 0x760c1830, 0x0070d8db},
50 {0x3333361c, 0x1b0e0e1b, 0x00fe63f3}, {0x18383070, 0x00000000, 0x00000000},
51 {0x0c0c1830, 0x0c0c0c0c, 0x0030180c}, {0x3030180c, 0x30303030, 0x000c1830},
52 {0x5a990000, 0x5a3cff3c, 0x00000099}, {0x18180000, 0x18ffff18, 0x00001818},
53 {0x00000000, 0x38000000, 0x000c1838}, {0x00000000, 0x00ffff00, 0x00000000},
54 {0x00000000, 0x00000000, 0x00001c1c}, {0x6060c0c0, 0x18183030, 0x06060c0c},
55 {0xe3c3663c, 0xc7cfdbf3, 0x003c66c3}, {0x181e1c18, 0x18181818, 0x007e1818},
56 {0x60c0e77e, 0x060c1830, 0x00ff0303}, {0xc0c0e77e, 0xc0e07ee0, 0x007ee7c0},
57 {0x363c3830, 0x3030ff33, 0x00303030}, {0x030303ff, 0xc0e07f03, 0x007ee7c0},
58 {0x0303e77e, 0xc3e37f03, 0x007ee7c3}, {0xc0c0c0ff, 0x0c183060, 0x000c0c0c},
59 {0xc3c3e77e, 0xc3e77ee7, 0x007ee7c3}, {0xc3c3e77e, 0xc0c0fee7, 0x007ee7c0},
60 {0x00000000, 0x00001c1c, 0x00001c1c}, {0x38000000, 0x38000038, 0x000c1838},
61 {0x0c183060, 0x0c060306, 0x00603018}, {0x00000000, 0xff00ffff, 0x000000ff},
62 {0x30180c06, 0x3060c060, 0x00060c18}, {0xc0c3c37e, 0x18183060, 0x00180000},
63 {0x7e000000, 0xdbcbbbc3, 0x00fc06f3}, {0xc3663c18, 0xc3ffc3c3, 0x00c3c3c3},
64 {0xc3c3e37f, 0xc3e37fe3, 0x007fe3c3}, {0x0303e77e, 0x03030303, 0x007ee703},
65 {0xc3e3733f, 0xc3c3c3c3, 0x003f73e3}, {0x030303ff, 0x03033f03, 0x00ff0303},
66 {0x030303ff, 0x0303033f, 0x00030303}, {0x0303e77e, 0xc3f30303, 0x007ee7c3},
67 {0xc3c3c3c3, 0xc3c3ffc3, 0x00c3c3c3}, {0x1818187e, 0x18181818, 0x007e1818},
68 {0x60606060, 0x60606060, 0x003e7763}, {0x1b3363c3, 0x1b0f070f, 0x00c36333},
69 {0x03030303, 0x03030303, 0x00ff0303}, {0xffffe7c3, 0xc3c3c3db, 0x00c3c3c3},
70 {0xcfcfc7c7, 0xf3fbdbdf, 0x00e3e3f3}, {0xc3c3e77e, 0xc3c3c3c3, 0x007ee7c3},
71 {0xc3c3e37f, 0x03037fe3, 0x00030303}, {0xc3c3663c, 0xdbc3c3c3, 0x00fc76fb},
72 {0xc3c3e37f, 0x1b0f7fe3, 0x00c36333}, {0x0303e77e, 0xc0e07e07, 0x007ee7c0},
73 {0x181818ff, 0x18181818, 0x00181818}, {0xc3c3c3c3, 0xc3c3c3c3, 0x007ee7c3},
74 {0xc3c3c3c3, 0x6666c3c3, 0x00183c3c}, {0xc3c3c3c3, 0xffdbdbc3, 0x00c3e7ff},
75 {0x3c6666c3, 0x3c3c183c, 0x00c36666}, {0x3c6666c3, 0x1818183c, 0x00181818},
76 {0x60c0c0ff, 0x060c7e30, 0x00ff0303}, {0x0c0c0c3c, 0x0c0c0c0c, 0x003c0c0c},
77 {0x0c0c0606, 0x30301818, 0xc0c06060}, {0x3030303c, 0x30303030, 0x003c3030},
78 {0xc3663c18, 0x00000000, 0x00000000}, {0x00000000, 0x00000000, 0xff000000},
79 {0x181c0c0e, 0x00000000, 0x00000000}, {0x00000000, 0xfec0c37e, 0x00fec3c3},
80 {0x03030303, 0xc3c37f03, 0x007fc3c3}, {0x00000000, 0x0303c37e, 0x007ec303},
81 {0xc0c0c0c0, 0xc3c3fec0, 0x00fec3c3}, {0x00000000, 0x7fc3c37e, 0x00fe0303},
82 {0x0c0ccc78, 0x0c0c3f0c, 0x000c0c0c}, {0x00000000, 0xc3c3c37e, 0xc3c0c0fe},
83 {0x03030303, 0xc3c3c37f, 0x00c3c3c3}, {0x00001800, 0x18181818, 0x00181818},
84 {0x00003000, 0x30303030, 0x36303030}, {0x03030303, 0x0f1b3363, 0x0063331f},
85 {0x1818181e, 0x18181818, 0x007e1818}, {0x00000000, 0xdbdbdb7f, 0x00dbdbdb},
86 {0x00000000, 0x6363633f, 0x00636363}, {0x00000000, 0x6363633e, 0x003e6363},
87 {0x00000000, 0xc3c3c37f, 0x03037fc3}, {0x00000000, 0xc3c3c3fe, 0xc0c0fec3},
88 {0x00000000, 0x0303077f, 0x00030303}, {0x00000000, 0x7e0303fe, 0x007fc0c0},
89 {0x0c0c0c00, 0x0c0c0c3f, 0x00386c0c}, {0x00000000, 0x63636363, 0x007e6363},
90 {0x00000000, 0x6666c3c3, 0x00183c3c}, {0x00000000, 0xdbc3c3c3, 0x00c3e7ff},
91 {0x00000000, 0x183c66c3, 0x00c3663c}, {0x00000000, 0x3c6666c3, 0x06060c18},
92 {0x00000000, 0x183060ff, 0x00ff060c}, {0x181818f0, 0x181c0f1c, 0x00f01818},
93 {0x18181818, 0x18181818, 0x18181818}, {0x1818180f, 0x1838f038, 0x000f1818},
94 {0x06000000, 0x0060f18f, 0x00000000}, {0x00000000, 0x00000000, 0x00000000}};
95
96 // Precalculated sum of all pixels per character
97 const uint raster_active_pixels[char_count] = {
98 96, 18, 16, 40, 56, 42, 46, 10, 22, 22, 32, 28, 10, 16, 6, 24, 52, 29, 36, 44, 35, 42, 50, 28,
99 58, 51, 12, 16, 22, 32, 22, 26, 41, 46, 57, 38, 52, 38, 32, 46, 48, 30, 31, 43, 28, 56, 64, 52,
100 42, 52, 52, 44, 28, 48, 42, 58, 42, 32, 38, 26, 24, 26, 14, 8, 10, 34, 40, 26, 40, 32, 30, 33,
101 39, 16, 20, 37, 28, 43, 30, 30, 34, 34, 20, 28, 27, 30, 26, 36, 26, 24, 26, 30, 24, 30, 14, 0};
102
103 // Get one sample of the font: (pixel index, character index)
104 float SampleFont(uint2 pos)
105 {
106 return (rasters[pos.y][pos.x / 32] >> (pos.x % 32)) & uint(1);
107 }
108
109 // Get one sample of the framebuffer: (character position in screen space, pixel index)
110 float3 SampleTex(uint2 char_pos, uint pixel)
111 {
112 float2 inv_resoltion =
113 OptionEnabled(USE_WINDOW_RES) ? GetInvWindowResolution() : GetInvResolution();
114 float2 tex_pos = char_pos * char_dim + float2(pixel % char_width, pixel / char_width) + 0.5;
115 return SampleLocation(tex_pos * inv_resoltion).xyz;
116 }
117
118 struct CharResults
119 {
120 float3 fg; // font color
121 float3 bg; // background color
122 float err; // MSE of this configuration
123 uint c; // character index
124 };
125
126 // Calculate the font and background color and the MSE for a given character
127 CharResults CalcCharRes(uint c, float3 t, float3 ft)
128 {
129 CharResults o;
130 o.c = c;
131
132 // Inputs:
133 // tt: sum of all texture samples squared
134 // t: sum of all texture samples
135 // ff: sum of all font samples squared
136 // f: sum of all font samples
137 // ft: sum of all font samples * texture samples
138
139 // The font is either 1.0 or 0.0, so ff == f
140 // As the font is constant, this is pre-calculated
141 float f = raster_active_pixels[c];
142 float ff = f;
143
144 // The calculation isn't stable if the font is all-one. Return max err
145 // instead.
146 if (f == char_pixels)
147 {
148 o.err = char_pixels * char_pixels;
149 return o;
150 }
151
152 // tt is only used as constant offset for the error, define it as zero
153 float3 tt = float3(0.0, 0.0, 0.0);
154
155 // The next lines are a bit harder, hf :-)
156
157 // The idea is to find the perfect char with the perfect background color
158 // and the perfect font color. As this is an equation with three unknowns,
159 // we can't just try all chars and color combinations.
160
161 // As criterion how "perfect" the selection is, we compare the "mean
162 // squared error" of the resulted colors of all chars. So, now the big
163 // issue: how to calculate the MSE without knowing the two colors ...
164
165 // In the next steps, "a" is the font color, "b" is the background color,
166 // "f" is the font value at this pixel, "t" is the texture value
167
168 // So the square error of one pixel is:
169 // e = ( t - a⋅f - b⋅(1-f) ) ^ 2
170
171 // In longer:
172 // e = a^2⋅f^2 - 2⋅a⋅b⋅f^2 + 2⋅a⋅b⋅f - 2⋅a⋅f⋅t + b^2⋅f^2 - 2⋅b^2⋅f + b^2 +
173 // 2⋅b⋅f⋅t - 2⋅b⋅t + t^2
174
175 // The sum of all errors is: (as shortcut, ff,f,ft,t,tt are now the sums
176 // like declared above, sum(1) is the count of pixels) sum(e) = a^2⋅ff -
177 // 2⋅a^2⋅ff + 2⋅a⋅b⋅f - 2⋅a⋅ft + b^2⋅ff - 2⋅b^2⋅f + b^2⋅sum(1) + 2⋅b⋅ft -
178 // 2⋅b⋅t + tt
179
180 // tt is only used as a constant offset, so its value has no effect on a,b or
181 // on the relative error. So it can be completely dropped.
182
183 // To find the minimum, we have to derive this by "a" and "b":
184 // d/da sum(e) = 2⋅a⋅ff + 2⋅b⋅f - 2⋅b⋅ff - 2⋅ft
185 // d/db sum(e) = 2⋅a⋅f - 2⋅a⋅ff - 4⋅b⋅f + 2⋅b⋅ff + 2⋅b⋅sum(1) + 2⋅ft - 2⋅t
186
187 // So, both equations must be zero at minimum and there is only one
188 // solution.
189
190 float3 a = (ft * (f - float(char_pixels)) + t * (f - ff)) / (f * f - ff * float(char_pixels));
191 float3 b = (ft * f - t * ff) / (f * f - ff * float(char_pixels));
192
193 float3 e = a * a * ff + 2.0 * a * b * (f - ff) - 2.0 * a * ft +
194 b * b * (-2.0 * f + ff + float(char_pixels)) + 2.0 * b * ft - 2.0 * b * t + tt;
195 o.err = dot(e, float3(1.0, 1.0, 1.0));
196
197 o.fg = a;
198 o.bg = b;
199 o.c = c;
200
201 return o;
202 }
203
204 // Get the color of the pixel of this invocation based on the character details
205 float3 GetFinalPixel(CharResults char_out)
206 {
207 float2 resolution = OptionEnabled(USE_WINDOW_RES) ? GetWindowResolution() : GetResolution();
208 uint2 char_pos = uint2(floor(GetCoordinates() * resolution / char_dim));
209 uint2 pixel_offset = uint2(floor(GetCoordinates() * resolution) - char_pos * char_dim);
210 float font = SampleFont(int2(pixel_offset.x + char_width * pixel_offset.y, char_out.c));
211 return char_out.fg * font + char_out.bg * (1.0 - font);
212 }
213
214 /*
215 This shader performs some kind of brute force evaluation, which character fits best.
216
217 for c in characters:
218 for p in pixels:
219 ft += font(c,p) * texture(p)
220 res = CalcCharRes(ft)
221 min(res.err)
222
223 Terrible in performance, only for reference.
224 */
225 CharResults CalcCharTrivial(uint2 char_pos)
226 {
227 float3 t;
228 CharResults char_out;
229 char_out.err = char_pixels * char_pixels;
230 for (uint c = 0; c < MAX_CHARS; c += 1)
231 {
232 float3 ft = float3(0.0, 0.0, 0.0);
233 for (uint pixel = 0; pixel < char_pixels; pixel += 1)
234 {
235 float3 tex = SampleTex(char_pos, pixel);
236 float font = SampleFont(uint2(pixel, c));
237 ft += font * tex;
238 }
239 if (c == 0)
240 t = ft;
241 CharResults res = CalcCharRes(c, t, ft);
242 if (res.err < char_out.err)
243 char_out = res;
244 }
245 return char_out;
246 }
247
248 /*
249 However for better performance, some characters are tested at once. This saves some expensive
250 texture() calls. Also split the loop over the pixels in groups of 32 for only fetching the uint32
251 of the font once.
252 */
253 CharResults CalcCharFallback(uint2 char_pos)
254 {
255 float3 t;
256 CharResults char_out;
257 char_out.err = char_pixels * char_pixels;
258 for (uint c = 0; c < MAX_CHARS; c += UNROLL_FALLBACK)
259 {
260 // Declare ft
261 float3 ft[UNROLL_FALLBACK];
262 for (uint i = 0; i < UNROLL_FALLBACK; i++)
263 ft[i] = float3(0.0, 0.0, 0.0);
264
265 // Split `for p : pixels` in groups of 32. This makes accessing the texture (bit in uint32)
266 // easier.
267 for (uint pixel = 0; pixel < char_pixels; pixel += 32)
268 {
269 uint font_i[UNROLL_FALLBACK];
270 for (uint i = 0; i < UNROLL_FALLBACK; i++)
271 font_i[i] = rasters[c + i][pixel / 32];
272
273 for (uint pixel_offset = 0; pixel_offset < 32; pixel_offset += 1)
274 {
275 float3 tex = SampleTex(char_pos, pixel + pixel_offset);
276
277 // Inner kernel of `ft += font * tex`. Most time is spend in here.
278 for (uint i = 0; i < UNROLL_FALLBACK; i++)
279 {
280 float font = (font_i[i] >> pixel_offset) & uint(1);
281
282 ft[i] += font * tex;
283 }
284 }
285 }
286 if (c == 0)
287 {
288 // First char has font := 1, so t = ft. Cache this value for the next iterations.
289 t = ft[0];
290 }
291
292 // Check if this character fits better than the last one.
293 for (uint i = 0; i < UNROLL_FALLBACK; i++)
294 {
295 CharResults res = CalcCharRes(c + i, t, ft[i]);
296 if (res.err < char_out.err)
297 char_out = res;
298 }
299 }
300
301 return char_out;
302 }
303
304 /*
305 SIMD optimized version with subgroup intrinsics
306 - distribute all characters over the lanes and check for them in parallel
307 - distribute the uniform texture access and broadcast each back to each lane
308 */
309 CharResults CalcCharSIMD(uint2 char_pos, uint simd_width)
310 {
311 // Font color, bg color, character, error -- of character with minimum error
312 CharResults char_out;
313 char_out.err = char_pixels * char_pixels;
314 float3 t;
315 #ifdef SUPPORTS_SUBGROUP_REDUCTION
316
317 // Hack: Work in hard-codeded fixed SIMD mode
318 if (gl_SubgroupInvocationID < simd_width)
319 {
320 // Loop over all characters
321 for (uint c = 0; c < MAX_CHARS; c += UNROLL_SIMD * simd_width)
322 {
323 // registers for "sum of font * texture"
324 float3 ft[UNROLL_SIMD];
325 for (uint i = 0; i < UNROLL_SIMD; i++)
326 ft[i] = float3(0.0, 0.0, 0.0);
327
328 for (uint pixel = 0; pixel < char_pixels; pixel += 32)
329 {
330 // Preload the font uint32 for the next 32 pixels
331 uint font_i[UNROLL_SIMD];
332 for (uint i = 0; i < UNROLL_SIMD; i++)
333 font_i[i] = rasters[c + UNROLL_SIMD * gl_SubgroupInvocationID + i][pixel / 32];
334
335 for (uint pixel_offset = 0; pixel_offset < 32; pixel_offset += simd_width)
336 {
337 // Copy one full WRAP of textures into registers and shuffle them around for later usage.
338 // This avoids one memory transaction per tested pixel & character.
339 float3 tex_simd = SampleTex(char_pos, pixel + pixel_offset + gl_SubgroupInvocationID);
340
341 for (uint k = 0; k < simd_width; k += 1)
342 {
343 float3 tex = subgroupBroadcast(tex_simd, k);
344
345 // Note: As pixel iterates based on power-of-two gl_SubgroupSize,
346 // the const memory access to rasters is CSE'd and the inner loop
347 // after unrolling only contains: testing one bit + shuffle +
348 // conditional add
349 for (uint i = 0; i < UNROLL_SIMD; i++)
350 {
351 float font = (font_i[i] >> (k + pixel_offset % 32)) & uint(1);
352 ft[i] += font * tex;
353 }
354 }
355 }
356 }
357 if (c == 0)
358 {
359 // font[0] is a hardcoded 1 font, so t = ft
360 t = subgroupBroadcast(ft[0], 0);
361 }
362
363 for (uint i = 0; i < UNROLL_SIMD; i++)
364 {
365 CharResults res = CalcCharRes(c + UNROLL_SIMD * gl_SubgroupInvocationID + i, t, ft[i]);
366 if (res.err < char_out.err)
367 char_out = res;
368 }
369 }
370 }
371
372 // Broadcast to get the best character of all threads
373 float err_min = subgroupMin(char_out.err);
374 uint smallest = subgroupBallotFindLSB(subgroupBallot(err_min == char_out.err));
375 char_out.fg = subgroupBroadcast(char_out.fg, smallest);
376 char_out.bg = subgroupBroadcast(char_out.bg, smallest);
377 char_out.c = subgroupBroadcast(char_out.c, smallest);
378 char_out.err = err_min;
379
380 #endif
381 return char_out;
382 }
383
384 bool supportsSIMD(uint simd_width)
385 {
386 #ifdef SUPPORTS_SUBGROUP_REDUCTION
387 const uint mask = simd_width == 32u ? 0xFFFFFFFFu : (1u << simd_width) - 1;
388 return (subgroupBallot(true)[0] & mask) == mask;
389 #else
390 return false;
391 #endif
392 }
393
394 // "Error: The AsciiArt shader requires the missing GPU extension KHR_shader_subgroup."
395 const uint missing_subgroup_warning_len = 82;
396 const uint missing_subgroup_warning[missing_subgroup_warning_len] = {
397 37, 82, 82, 79, 82, 26, 95, 52, 72, 69, 95, 33, 83, 67, 73, 73, 33, 82, 84, 95, 83,
398 72, 65, 68, 69, 82, 95, 82, 69, 81, 85, 73, 82, 69, 83, 95, 84, 72, 69, 95, 77, 73,
399 83, 83, 73, 78, 71, 95, 39, 48, 53, 95, 69, 88, 84, 69, 78, 84, 73, 79, 78, 95, 43,
400 40, 50, 63, 83, 72, 65, 68, 69, 82, 63, 83, 85, 66, 71, 82, 79, 85, 80, 14};
401
402 float3 ShowWarning(uint2 char_pos)
403 {
404 CharResults char_out;
405 char_out.fg = float3(1.0, 1.0, 1.0);
406 char_out.bg = float3(0.0, 0.0, 0.0);
407 char_out.c = 95u; // just background
408
409 if (char_pos.y == 0u && char_pos.x < missing_subgroup_warning_len)
410 {
411 char_out.c = missing_subgroup_warning[char_pos.x];
412 }
413
414 return GetFinalPixel(char_out);
415 }
416
417 void main()
418 {
419 // Calculate the character position of this pixel
420 float2 resolution = OptionEnabled(USE_WINDOW_RES) ? GetWindowResolution() : GetResolution();
421 uint2 char_pos_self = uint2(floor(GetCoordinates() * resolution / char_dim));
422
423 float3 color_out;
424
425 #ifdef SUPPORTS_SUBGROUP_REDUCTION
426 if (supportsSIMD(8))
427 {
428 // Loop over all character positions covered by this wave
429 bool pixel_active = !gl_HelperInvocation;
430 CharResults char_out;
431 while (true)
432 {
433 // Fetch the next active character position
434 uint4 active_lanes = subgroupBallot(pixel_active);
435 if (active_lanes == uint4(0, 0, 0, 0))
436 {
437 break;
438 }
439 uint2 char_pos = subgroupBroadcast(char_pos_self, subgroupBallotFindLSB(active_lanes));
440
441 // And calculate everything for this character position
442 if (supportsSIMD(32))
443 {
444 char_out = CalcCharSIMD(char_pos, 32);
445 }
446 else if (supportsSIMD(16))
447 {
448 char_out = CalcCharSIMD(char_pos, 16);
449 }
450 else if (supportsSIMD(8))
451 {
452 char_out = CalcCharSIMD(char_pos, 8);
453 }
454
455 // Draw the character on screen
456 if (char_pos == char_pos_self)
457 {
458 color_out = GetFinalPixel(char_out);
459 pixel_active = false;
460 }
461 if (OptionEnabled(DEBUG_ONLY_ONE_CHAR))
462 {
463 break;
464 }
465 }
466 }
467 else
468 #else
469 if (char_pos_self.y <= 1u)
470 {
471 color_out = ShowWarning(char_pos_self);
472 }
473 else
474 #endif
475 if (HAVE_FULL_FEATURE_FALLBACK)
476 {
477 color_out = GetFinalPixel(CalcCharFallback(char_pos_self));
478 }
479 else
480 {
481 color_out = Sample().xyz;
482 }
483
484 SetOutput(float4(color_out, 1.0));
485 }