MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
PrimitiveMill.cpp
Go to the documentation of this file.
1#include "PrimitiveMill.hpp"
2
3#include "ShaderFoundry.hpp"
4#include "ShaderSpec.hpp"
5
10
12
13using Buffers::VKBuffer;
14
15namespace {
16
17 constexpr uint32_t WORKGROUP = 256;
18 constexpr uint32_t ABSENT = 0xFFFFFFFFU;
19
20 /** @brief Words per DrawRun in the run table: topology, offset, count. */
21 constexpr size_t RUN_WORDS = 3;
22
23 /**
24 * @struct MillOffsets
25 * @brief Word offsets into one source vertex, resolved from its layout.
26 */
27 struct MillOffsets {
28 uint32_t stride_words { 0 };
29 uint32_t position { ABSENT };
30 uint32_t scalar { ABSENT };
31 uint32_t tangent { ABSENT };
32 uint32_t uv { ABSENT };
33 };
34
35 /**
36 * @brief Resolve @p layout into word offsets.
37 * @return false when the stride is not word aligned or no position
38 * attribute is addressable, which makes the layout unmillable.
39 */
40 bool offsets_from(const Kakshya::VertexLayout& layout, MillOffsets& out)
41 {
42 if (layout.stride_bytes == 0 || layout.stride_bytes % sizeof(uint32_t) != 0) {
43 return false;
44 }
45
46 const auto position = layout.find_word_offset(Kakshya::DataModality::VERTEX_POSITIONS_3D);
47 if (!position) {
48 return false;
49 }
50
51 out.stride_words = layout.stride_bytes / static_cast<uint32_t>(sizeof(uint32_t));
52 out.position = *position;
53 out.scalar = layout.find_word_offset(Kakshya::DataModality::SCALAR_F32).value_or(ABSENT);
54 out.tangent = layout.find_word_offset(Kakshya::DataModality::VERTEX_TANGENTS_4D).value_or(ABSENT);
55 out.uv = layout.find_word_offset(Kakshya::DataModality::TEXTURE_COORDS_2D).value_or(ABSENT);
56
57 return true;
58 }
59
60 /**
61 * @struct MillPC
62 * @brief Push constants for the mill kernel.
63 *
64 * Field order and widths must match build_mill_spec()'s pc() declaration
65 * order, which is how ShaderSpec assigns offsets.
66 */
67 struct MillPC {
68 uint32_t total;
69 uint32_t run_count;
70 uint32_t stride_words;
71 uint32_t position_offset;
72 uint32_t scalar_offset;
73 uint32_t uv_offset;
74 uint32_t mode;
75 uint32_t synth_uv;
76 float width_scale;
77 float point_scale;
78 float fallback_extent;
79 float eye_x;
80 float eye_y;
81 float eye_z;
82 uint32_t indexed;
83 uint32_t tangent_offset;
84 };
85
86 static_assert(sizeof(MillPC) == 16 * sizeof(uint32_t),
87 "MillPC must be tightly packed 4-byte fields matching build_mill_spec's pc() list");
88
89 void add_helpers(ShaderSpec::Assemble& assemble)
90 {
91 assemble.function("uint", "find_run", "uint g, uint n",
92 " uint lo = 0u;\n"
93 " uint hi = n;\n"
94 " while (lo + 1u < hi) {\n"
95 " uint mid = (lo + hi) >> 1u;\n"
96 " if (prefix[mid] <= g) { lo = mid; } else { hi = mid; }\n"
97 " }\n"
98 " return lo;\n");
99
100 assemble.function("uint", "vertex_at", "uint k, uint ix",
101 " return ix == 1u ? indices[k] : k;\n");
102
103 assemble.function("vec2", "wire_uv", "uint c",
104 " return vec2(c == 0u ? 21.0 : 20.0, c == 1u ? 1.0 : 0.0);\n");
105
106 assemble.function("vec3", "read_pos", "uint v, uint sw, uint po",
107 " uint b = v * sw + po;\n"
108 " return vec3(uintBitsToFloat(src[b]), uintBitsToFloat(src[b + 1u]), "
109 "uintBitsToFloat(src[b + 2u]));\n");
110
111 assemble.function("float", "read_extent", "uint v, uint sw, uint so, float fb",
112 " if (so == 0xffffffffu) { return fb; }\n"
113 " return uintBitsToFloat(src[v * sw + so]);\n");
114
115 assemble.function("void", "copy_vertex", "uint d, uint s, uint sw",
116 " uint db = d * sw;\n"
117 " uint sb = s * sw;\n"
118 " for (uint k = 0u; k < sw; ++k) { dst[db + k] = src[sb + k]; }\n");
119
120 assemble.function("void", "write_pos", "uint d, uint sw, uint po, vec3 p",
121 " uint b = d * sw + po;\n"
122 " dst[b] = floatBitsToUint(p.x);\n"
123 " dst[b + 1u] = floatBitsToUint(p.y);\n"
124 " dst[b + 2u] = floatBitsToUint(p.z);\n");
125
126 assemble.function("void", "write_uv", "uint d, uint sw, uint uo, vec2 t",
127 " if (uo == 0xffffffffu) { return; }\n"
128 " uint b = d * sw + uo;\n"
129 " dst[b] = floatBitsToUint(t.x);\n"
130 " dst[b + 1u] = floatBitsToUint(t.y);\n");
131
132 assemble.function("void", "write_tangent", "uint d, uint sw, uint to, vec4 t",
133 " uint b = d * sw + to;\n"
134 " dst[b] = floatBitsToUint(t.x);\n"
135 " dst[b + 1u] = floatBitsToUint(t.y);\n"
136 " dst[b + 2u] = floatBitsToUint(t.z);\n"
137 " dst[b + 3u] = floatBitsToUint(t.w);\n");
138
139 assemble.function("vec3", "view_normal", "vec3 p, vec3 eye",
140 " vec3 n = eye - p;\n"
141 " float l = length(n);\n"
142 " return l < 1e-8 ? vec3(0.0, 0.0, 1.0) : n / l;\n");
143
144 /**
145 * Direction arriving at vertex s from the nearest preceding vertex at a
146 * different position, within [lo, s]. Skipping coincident vertices lets
147 * one path serve both a true strip and a strip emitted as duplicated
148 * pairs. Zero when s starts the run.
149 */
150 assemble.function("vec3", "dir_in", "uint s, uint lo, uint sw, uint po",
151 " vec3 p = read_pos(s, sw, po);\n"
152 " uint c = s;\n"
153 " for (uint k = 0u; k < 4u; ++k) {\n"
154 " if (c <= lo) { break; }\n"
155 " c = c - 1u;\n"
156 " vec3 q = read_pos(c, sw, po);\n"
157 " if (length(p - q) > 1e-6) { return normalize(p - q); }\n"
158 " }\n"
159 " return vec3(0.0);\n");
160
161 /** Direction leaving vertex s toward the next distinct vertex below hi. */
162 assemble.function("vec3", "dir_out", "uint s, uint hi, uint sw, uint po",
163 " vec3 p = read_pos(s, sw, po);\n"
164 " uint c = s;\n"
165 " for (uint k = 0u; k < 4u; ++k) {\n"
166 " c = c + 1u;\n"
167 " if (c >= hi) { break; }\n"
168 " vec3 q = read_pos(c, sw, po);\n"
169 " if (length(q - p) > 1e-6) { return normalize(q - p); }\n"
170 " }\n"
171 " return vec3(0.0);\n");
172
173 /**
174 * Width at vertex s, averaged over every vertex sharing its position
175 * and the nearest distinct vertex on each side.
176 *
177 * A producer that emits each interior sample twice can give the two
178 * copies different thickness, which makes the ribbon step at a point
179 * where it should be continuous: one quad ends at one width and the
180 * next begins at another. Averaging over position rather than over
181 * vertex index makes both copies agree, and folding in the neighbours
182 * damps per-sample jitter that reads as serration once the ribbon is
183 * more than a pixel wide.
184 */
185 assemble.function("float", "extent_at",
186 "uint s, uint lo, uint hi, uint sw, uint po, uint so, float fb",
187 " vec3 p = read_pos(s, sw, po);\n"
188 " float sum = read_extent(s, sw, so, fb);\n"
189 " float cnt = 1.0;\n"
190 " uint c = s;\n"
191 " for (uint k = 0u; k < 4u; ++k) {\n"
192 " if (c <= lo) { break; }\n"
193 " c = c - 1u;\n"
194 " sum += read_extent(c, sw, so, fb);\n"
195 " cnt += 1.0;\n"
196 " if (length(read_pos(c, sw, po) - p) > 1e-6) { break; }\n"
197 " }\n"
198 " c = s;\n"
199 " for (uint k = 0u; k < 4u; ++k) {\n"
200 " c = c + 1u;\n"
201 " if (c >= hi) { break; }\n"
202 " sum += read_extent(c, sw, so, fb);\n"
203 " cnt += 1.0;\n"
204 " if (length(read_pos(c, sw, po) - p) > 1e-6) { break; }\n"
205 " }\n"
206 " return sum / cnt;\n");
207
208 /**
209 * Offset from vertex s to the ribbon edge, mitred.
210 *
211 * The side vector follows the bisector of the segments meeting at s,
212 * so both quads sharing s place their corners identically and the
213 * ribbon stays continuous. Scaling by the reciprocal of the bisector's
214 * projection onto the segment normal keeps the width constant through
215 * the turn; the clamp is the usual miter limit, past which a very sharp
216 * corner would otherwise throw the corner out to infinity.
217 */
218 assemble.function("vec3", "offset_from",
219 "vec3 p, vec3 din, vec3 dout, vec3 seg, vec3 eye, float half_w",
220 " vec3 vn = view_normal(p, eye);\n"
221 " vec3 ns = cross(seg, vn);\n"
222 " float nsl = length(ns);\n"
223 " if (nsl < 1e-6) { return vec3(0.0); }\n"
224 " ns = ns / nsl;\n"
225 " vec3 t = din + dout;\n"
226 " float tl = length(t);\n"
227 " if (tl < 1e-6) { return ns * half_w; }\n"
228 " vec3 nm = cross(t / tl, vn);\n"
229 " float nml = length(nm);\n"
230 " if (nml < 1e-6) { return ns * half_w; }\n"
231 " nm = nm / nml;\n"
232 " float proj = dot(nm, ns);\n"
233 " if (abs(proj) < 0.25) { return ns * half_w; }\n"
234 " return nm * (half_w / proj);\n");
235
236 /**
237 * Direction of the segment preceding a LINE_LIST pair that starts at
238 * @p s, or zero when the previous pair ends somewhere else.
239 *
240 * Pairs that meet at a shared position are a polyline written as
241 * disconnected segments, which is what a path producer emits when it
242 * duplicates each interior sample. Pairs that do not meet are genuinely
243 * separate edges and must not be joined.
244 */
245 assemble.function("vec3", "pair_dir_in", "uint s, uint lo, uint sw, uint po",
246 " if (s < lo + 2u) { return vec3(0.0); }\n"
247 " vec3 p = read_pos(s, sw, po);\n"
248 " vec3 b = read_pos(s - 1u, sw, po);\n"
249 " if (length(b - p) > 1e-6) { return vec3(0.0); }\n"
250 " vec3 a = read_pos(s - 2u, sw, po);\n"
251 " vec3 d = b - a;\n"
252 " float l = length(d);\n"
253 " return l < 1e-6 ? vec3(0.0) : d / l;\n");
254
255 /** Direction of the segment following a LINE_LIST pair ending at s. */
256 assemble.function("vec3", "pair_dir_out", "uint s, uint hi, uint sw, uint po",
257 " if (s + 2u >= hi) { return vec3(0.0); }\n"
258 " vec3 p = read_pos(s, sw, po);\n"
259 " vec3 a = read_pos(s + 1u, sw, po);\n"
260 " if (length(a - p) > 1e-6) { return vec3(0.0); }\n"
261 " vec3 b = read_pos(s + 2u, sw, po);\n"
262 " vec3 d = b - a;\n"
263 " float l = length(d);\n"
264 " return l < 1e-6 ? vec3(0.0) : d / l;\n");
265
266 /** Width at s averaged with a coincident neighbour at @p o, if any. */
267 assemble.function("float", "pair_extent",
268 "uint s, uint o, uint lo, uint hi, uint sw, uint po, uint so, float fb",
269 " float e = read_extent(s, sw, so, fb);\n"
270 " if (o < lo || o >= hi) { return e; }\n"
271 " if (length(read_pos(o, sw, po) - read_pos(s, sw, po)) > 1e-6) { return e; }\n"
272 " return 0.5 * (e + read_extent(o, sw, so, fb));\n");
273 }
274
275 /**
276 * @brief Kernel milling one output vertex per invocation.
277 *
278 * src and dst are declared UINT32 so an attribute copy is bit-exact
279 * whatever the attribute's real type; only position and texture coordinate
280 * words are reinterpreted. Topology codes follow PrimitiveTopology's
281 * declaration order.
282 *
283 * A zero-length segment collapses all six corners onto one position and
284 * skips the attribute copy: the triangles have no area, so nothing
285 * interpolates them and the untouched words are never read. That is the
286 * common case for a producer emitting a strip as duplicated vertex pairs,
287 * where every second segment is a seam.
288 */
289 ShaderSpec build_mill_spec()
290 {
291 ShaderSpec::Assemble assemble;
292 assemble
300 .pc("stride_words", Kakshya::GpuDataFormat::UINT32)
301 .pc("position_offset", Kakshya::GpuDataFormat::UINT32)
302 .pc("scalar_offset", Kakshya::GpuDataFormat::UINT32)
306 .pc("width_scale", Kakshya::GpuDataFormat::FLOAT32)
307 .pc("point_scale", Kakshya::GpuDataFormat::FLOAT32)
308 .pc("fallback_extent", Kakshya::GpuDataFormat::FLOAT32)
313 .pc("tangent_offset", Kakshya::GpuDataFormat::UINT32)
314 .workgroup(WORKGROUP);
315
316 add_helpers(assemble);
317
318 std::string body;
319 body += " if (i >= total) { return; }\n";
320 body += " uint sw = stride_words;\n";
321 body += " uint po = position_offset;\n";
322 body += " vec3 eye = vec3(eye_x, eye_y, eye_z);\n";
323 body += " uint r = find_run(i, run_count);\n";
324 body += " uint rb = r * 3u;\n";
325 body += " uint topo = runs[rb] & 255u;\n";
326 body += " uint fill = runs[rb] >> 8u;\n";
327 body += " uint voff = runs[rb + 1u];\n";
328 body += " uint local = i - prefix[r];\n";
329
330 body += " if (topo == 3u && fill != 1u) {\n";
331 body += " copy_vertex(i, vertex_at(voff + local, indexed), sw);\n";
332 body += " if (fill == 2u) { write_uv(i, sw, uv_offset, wire_uv(local % 3u)); }\n";
333 body += " return;\n";
334 body += " }\n";
335
336 body += " if ((topo == 4u || topo == 5u) && fill != 1u) {\n";
337 body += " uint tri = local / 3u;\n";
338 body += " uint c = local - tri * 3u;\n";
339 body += " uint a; uint b1; uint b2;\n";
340 body += " if (topo == 5u) { a = 0u; b1 = tri + 1u; b2 = tri + 2u; }\n";
341 body += " else {\n";
342 body += " a = tri; b1 = tri + 1u; b2 = tri + 2u;\n";
343 body += " if ((tri & 1u) == 1u) { uint t = a; a = b1; b1 = t; }\n";
344 body += " }\n";
345 body += " uint pick = c == 0u ? a : (c == 1u ? b1 : b2);\n";
346 body += " copy_vertex(i, vertex_at(voff + pick, indexed), sw);\n";
347 body += " if (fill == 2u) { write_uv(i, sw, uv_offset, wire_uv(c)); }\n";
348 body += " return;\n";
349 body += " }\n";
350
351 body += " uint corner = local % 6u;\n";
352 body += " uint quad = local / 6u;\n";
353 body += " vec2 uv;\n";
354
355 body += " if (topo == 0u || fill == 1u) {\n";
356 body += " uint s = topo == 0u ? voff + quad : vertex_at(voff + quad, indexed);\n";
357 body += " vec3 p = read_pos(s, sw, po);\n";
358 body += " float h = read_extent(s, sw, scalar_offset, fallback_extent)\n";
359 body += " * point_scale * 0.5;\n";
360 body += " vec3 rx; vec3 ry;\n";
361 body += " if (mode == 1u) {\n";
362 body += " rx = vec3(h, 0.0, 0.0);\n";
363 body += " ry = vec3(0.0, h, 0.0);\n";
364 body += " } else {\n";
365 body += " vec3 n = view_normal(p, eye);\n";
366 body += " vec3 up = abs(n.y) < 0.99 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0);\n";
367 body += " rx = normalize(cross(up, n)) * h;\n";
368 body += " ry = normalize(cross(n, rx)) * h;\n";
369 body += " }\n";
370 body += " vec3 o;\n";
371 body += " if (corner == 0u) { o = -rx - ry; uv = vec2(10.0, 10.0); }\n";
372 body += " else if (corner == 1u) { o = rx - ry; uv = vec2(11.0, 10.0); }\n";
373 body += " else if (corner == 2u) { o = rx + ry; uv = vec2(11.0, 11.0); }\n";
374 body += " else if (corner == 3u) { o = -rx - ry; uv = vec2(10.0, 10.0); }\n";
375 body += " else if (corner == 4u) { o = rx + ry; uv = vec2(11.0, 11.0); }\n";
376 body += " else { o = -rx + ry; uv = vec2(10.0, 11.0); }\n";
377 body += " copy_vertex(i, s, sw);\n";
378 body += " if (tangent_offset != 0xffffffffu) {\n";
379 body += " write_pos(i, sw, po, p);\n";
380 body += " write_tangent(i, sw, tangent_offset,\n";
381 body += " vec4(((uv - vec2(10.0)) * 2.0 - 1.0) * h, 0.0, 0.0));\n";
382 body += " } else {\n";
383 body += " write_pos(i, sw, po, p + o);\n";
384 body += " }\n";
385 body += " if (synth_uv == 1u) { write_uv(i, sw, uv_offset, uv); }\n";
386 body += " return;\n";
387 body += " }\n";
388
389 body += " uint s0 = topo == 1u ? voff + quad * 2u : voff + quad;\n";
390 body += " uint s1 = s0 + 1u;\n";
391 body += " vec3 p0 = read_pos(s0, sw, po);\n";
392 body += " vec3 p1 = read_pos(s1, sw, po);\n";
393 body += " vec3 d = p1 - p0;\n";
394 body += " if (length(d) < 1e-6) {\n";
395 body += " write_pos(i, sw, po, p0);\n";
396 body += " if (tangent_offset != 0xffffffffu) {\n";
397 body += " write_tangent(i, sw, tangent_offset, vec4(0.0));\n";
398 body += " }\n";
399 body += " return;\n";
400 body += " }\n";
401 body += " float dl = length(d);\n";
402 body += " vec3 dn = d / dl;\n";
403 body += " float h0 = read_extent(s0, sw, scalar_offset, fallback_extent)\n";
404 body += " * width_scale * 0.5;\n";
405 body += " float h1 = read_extent(s1, sw, scalar_offset, fallback_extent)\n";
406 body += " * width_scale * 0.5;\n";
407 body += " vec3 e0; vec3 e1;\n";
408 body += " if (mode == 1u) {\n";
409 body += " vec3 sv = vec3(-dn.y, dn.x, 0.0);\n";
410 body += " e0 = sv * h0;\n";
411 body += " e1 = sv * h1;\n";
412 body += " } else {\n";
413 body += " uint lo = voff;\n";
414 body += " uint hi = voff + runs[rb + 2u];\n";
415 body += " vec3 din; vec3 dout;\n";
416 body += " if (topo == 2u) {\n";
417 body += " din = dir_in(s0, lo, sw, po);\n";
418 body += " dout = dir_out(s1, hi, sw, po);\n";
419 body += " h0 = extent_at(s0, lo, hi, sw, po, scalar_offset, fallback_extent)\n";
420 body += " * width_scale * 0.5;\n";
421 body += " h1 = extent_at(s1, lo, hi, sw, po, scalar_offset, fallback_extent)\n";
422 body += " * width_scale * 0.5;\n";
423 body += " } else {\n";
424 body += " din = pair_dir_in(s0, lo, sw, po);\n";
425 body += " dout = pair_dir_out(s1, hi, sw, po);\n";
426 body += " h0 = pair_extent(s0, s0 - 1u, lo, hi, sw, po, scalar_offset, fallback_extent)\n";
427 body += " * width_scale * 0.5;\n";
428 body += " h1 = pair_extent(s1, s1 + 1u, lo, hi, sw, po, scalar_offset, fallback_extent)\n";
429 body += " * width_scale * 0.5;\n";
430 body += " }\n";
431 body += " e0 = offset_from(p0, din, dn, dn, eye, h0);\n";
432 body += " e1 = offset_from(p1, dn, dout, dn, eye, h1);\n";
433 body += " }\n";
434 body += " vec3 pos; uint pick;\n";
435 body += " if (corner == 0u) { pos = p0 - e0; pick = s0; uv = vec2(0.0, 1.0); }\n";
436 body += " else if (corner == 1u) { pos = p0 + e0; pick = s0; uv = vec2(0.0, 0.0); }\n";
437 body += " else if (corner == 2u) { pos = p1 + e1; pick = s1; uv = vec2(1.0, 0.0); }\n";
438 body += " else if (corner == 3u) { pos = p0 - e0; pick = s0; uv = vec2(0.0, 1.0); }\n";
439 body += " else if (corner == 4u) { pos = p1 + e1; pick = s1; uv = vec2(1.0, 0.0); }\n";
440 body += " else { pos = p1 - e1; pick = s1; uv = vec2(1.0, 1.0); }\n";
441 body += " copy_vertex(i, pick, sw);\n";
442 body += " if (tangent_offset != 0xffffffffu) {\n";
443 body += " float sg = (corner == 0u || corner == 3u || corner == 5u) ? -1.0 : 1.0;\n";
444 body += " float hh = (corner == 0u || corner == 1u || corner == 3u) ? h0 : h1;\n";
445 body += " write_pos(i, sw, po, pick == s0 ? p0 : p1);\n";
446 body += " write_tangent(i, sw, tangent_offset, vec4(dn, sg * hh));\n";
447 body += " } else {\n";
448 body += " write_pos(i, sw, po, pos);\n";
449 body += " }\n";
450 body += " if (fill == 2u) { write_uv(i, sw, uv_offset, wire_uv(corner % 3u)); }\n";
451 body += " else if (synth_uv == 1u) { write_uv(i, sw, uv_offset, uv); }\n";
452
453 assemble.kernel(KernelSource { .body = std::move(body) });
454
455 return assemble.build();
456 }
457
458 MillPC make_pc(
459 const MillSpec& spec,
460 const MillView& view,
461 const MillOffsets& off,
462 uint32_t total,
463 size_t run_count,
464 bool indexed,
465 bool billboard)
466 {
467 return MillPC {
468 .total = total,
469 .run_count = static_cast<uint32_t>(run_count),
470 .stride_words = off.stride_words,
471 .position_offset = off.position,
472 .scalar_offset = spec.use_vertex_extent ? off.scalar : ABSENT,
473 .uv_offset = off.uv,
474 .mode = spec.ribbon == MillSpec::Ribbon::WorldPlane ? 1U : 0U,
475 .synth_uv = spec.synthesize_uv ? 1U : 0U,
476 .width_scale = spec.width_scale,
477 .point_scale = spec.point_scale,
478 .fallback_extent = spec.fallback_extent,
479 .eye_x = view.eye.x,
480 .eye_y = view.eye.y,
481 .eye_z = view.eye.z,
482 .indexed = indexed ? 1U : 0U,
483 .tangent_offset = billboard ? off.tangent : ABSENT
484 };
485 }
486
487 /** @brief Whether a span of this topology is addressed through an index buffer when the source has one. */
488 constexpr bool is_triangle_topology(PrimitiveTopology topology) noexcept
489 {
490 return topology == PrimitiveTopology::TRIANGLE_LIST
492 || topology == PrimitiveTopology::TRIANGLE_FAN;
493 }
494
495 /** @brief Exclusive prefix of milled counts, with the total appended. */
496 void build_prefix(std::span<const DrawRun> runs, std::vector<uint32_t>& prefix)
497 {
498 prefix.assign(runs.size() + 1, 0U);
499 for (size_t r = 0; r < runs.size(); ++r) {
500 prefix[r + 1] = prefix[r]
501 + triangle_vertex_count(runs[r].topology, runs[r].vertex_count, runs[r].fill);
502 }
503 }
504
505} // namespace
506
508 : m_spec(spec)
509 , m_output_ring(std::max(output_ring, 1U))
510{
511}
512
517
518std::shared_ptr<VKBuffer> PrimitiveMill::output() const
519{
520 if (m_outputs.empty()) {
521 return nullptr;
522 }
523 return m_outputs[m_output_slot];
524}
525
526uint32_t PrimitiveMill::milled_vertex_count(std::span<const DrawRun> runs)
527{
528 uint32_t total = 0;
529 for (const auto& run : runs) {
530 total += triangle_vertex_count(run.topology, run.vertex_count, run.fill);
531 }
532 return total;
533}
534
536{
538 return true;
539 }
540
541 const auto spec = build_mill_spec();
542 m_push_constant_size = spec.push_constant_bytes;
543
544 if (m_push_constant_size != sizeof(MillPC)) {
546 "PrimitiveMill: kernel expects {} push constant bytes but MillPC is {}",
547 m_push_constant_size, sizeof(MillPC));
548 return false;
549 }
550
551 auto& foundry = get_shader_foundry();
552 m_shader = foundry.load_shader(spec);
553 if (m_shader == INVALID_SHADER) {
555 "PrimitiveMill: kernel failed to compile");
556 return false;
557 }
558
559 auto& press = get_compute_press();
560 m_pipeline = press.create_pipeline_auto(m_shader, m_push_constant_size, 0);
563 "PrimitiveMill: pipeline creation failed");
564 return false;
565 }
566
567 return true;
568}
569
571 const std::shared_ptr<VKBuffer>& source,
572 const Kakshya::VertexLayout& layout,
573 uint32_t total,
574 size_t run_count)
575{
578 if (!svc) {
580 "PrimitiveMill: BufferService unavailable");
581 return false;
582 }
583
584 if (total > m_output_capacity || m_outputs.size() != m_output_ring) {
585 const auto grown = static_cast<uint32_t>(static_cast<float>(total) * 1.5F);
586
587 auto milled_layout = layout;
588 milled_layout.vertex_count = total;
589
590 if (!m_outputs.empty()) {
591 get_shader_foundry().release_after_submit([svc, outgoing = std::move(m_outputs)] {
592 for (const auto& buffer : outgoing) {
593 svc->destroy_buffer(buffer);
594 }
595 });
596 }
597
598 m_outputs.assign(m_output_ring, nullptr);
599 for (auto& slot : m_outputs) {
600 slot = std::make_shared<VKBuffer>(
601 static_cast<size_t>(grown) * layout.stride_bytes,
602 VKBuffer::Usage::VERTEX,
603 source->get_modality());
604 slot->set_vertex_layout(milled_layout);
605 svc->initialize_buffer(slot);
606 }
607
608 m_output_capacity = grown;
609 m_output_slot = 0;
610 } else {
611 for (auto& slot : m_outputs) {
612 auto milled_layout = *slot->get_vertex_layout();
613 milled_layout.vertex_count = total;
614 slot->set_vertex_layout(milled_layout);
615 }
616 }
617
618 const auto arena_slots = static_cast<size_t>(m_output_ring) + 1;
619 const auto fit_arena = [arena_slots](std::unique_ptr<SlotArena>& arena, size_t bytes) {
620 if (arena && arena->slot_bytes() >= bytes) {
621 return;
622 }
623 if (!arena) {
624 arena = std::make_unique<SlotArena>();
625 }
626 arena->initialize(
627 static_cast<size_t>(static_cast<float>(bytes) * 1.5F),
628 BufferUsageHint::HOST_STORAGE, arena_slots);
629 };
630
631 fit_arena(m_run_arena, std::max<size_t>(run_count * RUN_WORDS * sizeof(uint32_t), sizeof(uint32_t)));
632 fit_arena(m_prefix_arena, std::max<size_t>(m_prefix.size() * sizeof(uint32_t), sizeof(uint32_t)));
633
634 return !m_outputs.empty()
635 && m_run_arena && m_run_arena->is_initialized()
636 && m_prefix_arena && m_prefix_arena->is_initialized();
637}
638
640{
641 auto& foundry = get_shader_foundry();
642
643 size_t done = 0;
644 while (done < m_in_flight.size() && foundry.is_fence_signaled(m_in_flight[done])) {
645 foundry.release_fence(m_in_flight[done]);
646 ++done;
647 }
648 m_in_flight.erase(m_in_flight.begin(), m_in_flight.begin() + static_cast<std::ptrdiff_t>(done));
649}
650
652{
653 auto& foundry = get_shader_foundry();
654
655 for (const FenceID fence : m_in_flight) {
656 foundry.wait_for_fence(fence);
657 foundry.release_fence(fence);
658 }
659 m_in_flight.clear();
660}
661
663 const std::shared_ptr<VKBuffer>& source,
664 std::span<const DrawRun> runs,
665 const MillView& view,
666 std::optional<Kakshya::VertexLayout> layout)
667{
669
670 m_milled_count = 0;
671
672 if (!source || runs.empty()) {
673 return 0;
674 }
675
676 if (!layout)
677 layout = source->get_vertex_layout();
678 if (!layout.has_value()) {
679 return 0;
680 }
681
682 MillOffsets off;
683 if (!offsets_from(*layout, off)) {
685 "PrimitiveMill: layout is not millable, stride {} needs word alignment "
686 "and an addressable position attribute",
687 layout->stride_bytes);
688 return 0;
689 }
690
691 const bool indexed = source->has_index_buffer();
692 const auto index_count = static_cast<uint32_t>(source->get_index_buffer_size() / sizeof(uint32_t));
693 const uint32_t source_vertices = layout->vertex_count;
694 for (const auto& run : runs) {
695 if (run.vertex_count == 0) {
696 continue;
697 }
698 const bool by_index = indexed && is_triangle_topology(run.topology);
699 const uint32_t limit = by_index ? index_count : source_vertices;
700 if (run.vertex_offset > limit
701 || run.vertex_count > limit - run.vertex_offset) {
703 "PrimitiveMill: run [{}, {}) exceeds the source's {} {}",
704 run.vertex_offset, run.vertex_offset + run.vertex_count, limit,
705 by_index ? "indices" : "vertices");
706 return 0;
707 }
708 }
709
710 build_prefix(runs, m_prefix);
711 const uint32_t total = m_prefix.back();
712 if (total == 0) {
713 return 0;
714 }
715
716 if (!ensure_kernel() || !ensure_buffers(source, *layout, total, runs.size())) {
717 return 0;
718 }
719
720 m_output_slot = (m_output_slot + 1) % m_outputs.size();
721
722 m_run_words.assign(runs.size() * RUN_WORDS, 0U);
723 auto* run_ptr = m_run_words.data();
724
725 for (size_t r = 0; r < runs.size(); ++r) {
726 const bool is_line = runs[r].topology == PrimitiveTopology::LINE_LIST
727 || runs[r].topology == PrimitiveTopology::LINE_STRIP;
728 const bool emulated = is_triangle_topology(runs[r].topology)
729 ? (runs[r].fill == PolygonMode::POINT || runs[r].fill == PolygonMode::LINE)
730 : (is_line && (runs[r].fill == PolygonMode::POINT || runs[r].fill == PolygonMode::LINE));
731 const uint32_t fill_code = !emulated ? 0U : (runs[r].fill == PolygonMode::POINT ? 1U : 2U);
732 run_ptr[r * RUN_WORDS + 0] = static_cast<uint32_t>(runs[r].topology) | (fill_code << 8U);
733 run_ptr[r * RUN_WORDS + 1] = runs[r].vertex_offset;
734 run_ptr[r * RUN_WORDS + 2] = runs[r].vertex_count;
735 }
736
737 m_run_slot = m_run_arena->acquire();
738 m_prefix_slot = m_prefix_arena->acquire();
739 m_run_arena->write(m_run_slot, m_run_words.data(), m_run_words.size() * sizeof(uint32_t));
740 m_prefix_arena->write(m_prefix_slot, m_prefix.data(), m_prefix.size() * sizeof(uint32_t));
741
742 const bool billboard = m_spec.billboard_instances
744 && off.tangent != ABSENT;
745 if (m_spec.billboard_instances && off.tangent == ABSENT) {
747 "PrimitiveMill: billboard_instances needs a tangent attribute in the vertex layout");
748 }
749
750 const auto pc = make_pc(m_spec, view, off, total, runs.size(), indexed, billboard);
751
752 auto& foundry = get_shader_foundry();
753 auto& press = get_compute_press();
754
755 const auto destination = m_outputs[m_output_slot]->get_buffer();
756
757 auto cmd_id = foundry.begin_commands(ShaderFoundry::CommandBufferType::GRAPHICS);
758
759 foundry.buffer_barrier(
760 cmd_id,
761 destination,
762 vk::AccessFlagBits2::eVertexAttributeRead,
763 vk::AccessFlagBits2::eShaderWrite,
764 vk::PipelineStageFlagBits2::eVertexInput,
765 vk::PipelineStageFlagBits2::eComputeShader);
766
767 const auto storage_write = [](uint32_t binding, const std::shared_ptr<VKBuffer>& buf) {
769 write.binding = binding;
770 write.type = vk::DescriptorType::eStorageBuffer;
771 write.buffer = vk::DescriptorBufferInfo { buf->get_buffer(), 0, buf->get_size_bytes() };
772 return write;
773 };
774
775 const auto slot_write = [](uint32_t binding, const vk::DescriptorBufferInfo& info) {
777 write.binding = binding;
778 write.type = vk::DescriptorType::eStorageBuffer;
779 write.buffer = info;
780 return write;
781 };
782
783 const auto run_info = m_run_arena->info(m_run_slot);
784 PushDescriptorWrite index_write = slot_write(4, run_info);
785 if (indexed) {
786 index_write.buffer = vk::DescriptorBufferInfo {
787 source->get_index_buffer(), 0, source->get_index_buffer_size()
788 };
789 }
790
791 const std::array<SlotLease, 2> leases {
793 };
794
795 press.bind_pipeline(cmd_id, m_pipeline);
796 press.push_descriptors(cmd_id, m_pipeline, {
797 storage_write(0, source),
798 slot_write(1, run_info),
799 slot_write(2, m_prefix_arena->info(m_prefix_slot)),
800 storage_write(3, m_outputs[m_output_slot]),
801 index_write,
802 },
803 leases);
804 press.push_constants(cmd_id, m_pipeline, &pc, sizeof(MillPC));
805 press.dispatch(cmd_id, (total + WORKGROUP - 1) / WORKGROUP, 1, 1);
806
807 foundry.buffer_barrier(
808 cmd_id,
809 destination,
810 vk::AccessFlagBits2::eShaderWrite,
811 vk::AccessFlagBits2::eVertexAttributeRead,
812 vk::PipelineStageFlagBits2::eComputeShader,
813 vk::PipelineStageFlagBits2::eVertexInput);
814
815 const FenceID fence = foundry.submit_async(cmd_id);
816 if (fence == INVALID_FENCE) {
818 "PrimitiveMill: dispatch submission failed");
819 return 0;
820 }
821
822 m_in_flight.push_back(fence);
823
824 m_milled_count = total;
825
827 "PrimitiveMill: milled {} runs into {} vertices", runs.size(), total);
828
829 return total;
830}
831
833{
835
836 auto& foundry = get_shader_foundry();
837 auto& press = get_compute_press();
838
840 press.destroy_pipeline(m_pipeline);
842 }
843
844 if (m_shader != INVALID_SHADER) {
845 foundry.destroy_shader(m_shader);
847 }
848
849 m_outputs.clear();
850 m_output_slot = 0;
851 if (m_run_arena) {
852 m_run_arena->release();
853 }
854 if (m_prefix_arena) {
855 m_prefix_arena->release();
856 }
857 m_run_arena.reset();
858 m_prefix_arena.reset();
859 m_prefix.clear();
860 m_run_words.clear();
862 m_milled_count = 0;
864}
865
866// ===========================================================================
867// Host path
868// ===========================================================================
869
870namespace {
871
872 /** @brief Reads and writes one vertex record as words, mirroring the kernel. */
873 struct HostRecords {
874 const uint32_t* src;
875 uint32_t* dst;
876 uint32_t stride_words;
877
878 [[nodiscard]] glm::vec3 read_pos(uint32_t v, uint32_t po) const
879 {
880 const uint32_t* w = src + static_cast<size_t>(v) * stride_words + po;
881 glm::vec3 p;
882 std::memcpy(&p.x, w, sizeof(float));
883 std::memcpy(&p.y, w + 1, sizeof(float));
884 std::memcpy(&p.z, w + 2, sizeof(float));
885 return p;
886 }
887
888 [[nodiscard]] float read_extent(uint32_t v, uint32_t so, float fb) const
889 {
890 if (so == ABSENT) {
891 return fb;
892 }
893 float f = 0.0F;
894 std::memcpy(&f, src + static_cast<size_t>(v) * stride_words + so, sizeof(float));
895 return f;
896 }
897
898 void copy_vertex(uint32_t d, uint32_t s) const
899 {
900 std::memcpy(
901 dst + static_cast<size_t>(d) * stride_words,
902 src + static_cast<size_t>(s) * stride_words,
903 static_cast<size_t>(stride_words) * sizeof(uint32_t));
904 }
905
906 void write_pos(uint32_t d, uint32_t po, const glm::vec3& p) const
907 {
908 uint32_t* w = dst + static_cast<size_t>(d) * stride_words + po;
909 std::memcpy(w, &p.x, sizeof(float));
910 std::memcpy(w + 1, &p.y, sizeof(float));
911 std::memcpy(w + 2, &p.z, sizeof(float));
912 }
913
914 void write_uv(uint32_t d, uint32_t uo, const glm::vec2& t) const
915 {
916 if (uo == ABSENT) {
917 return;
918 }
919 uint32_t* w = dst + static_cast<size_t>(d) * stride_words + uo;
920 std::memcpy(w, &t.x, sizeof(float));
921 std::memcpy(w + 1, &t.y, sizeof(float));
922 }
923
924 void write_tangent(uint32_t d, uint32_t to, const glm::vec4& t) const
925 {
926 uint32_t* w = dst + static_cast<size_t>(d) * stride_words + to;
927 std::memcpy(w, &t.x, sizeof(float));
928 std::memcpy(w + 1, &t.y, sizeof(float));
929 std::memcpy(w + 2, &t.z, sizeof(float));
930 std::memcpy(w + 3, &t.w, sizeof(float));
931 }
932 };
933
934 glm::vec3 host_view_normal(const glm::vec3& p, const glm::vec3& eye)
935 {
936 const glm::vec3 n = eye - p;
937 const float l = glm::length(n);
938 return l < 1e-8F ? glm::vec3(0.0F, 0.0F, 1.0F) : n / l;
939 }
940
941 glm::vec3 host_side_at(
942 const glm::vec3& p,
943 const glm::vec3& seg,
944 const glm::vec3& eye,
945 float half_w,
946 bool world_plane)
947 {
948 glm::vec3 dir = seg;
949 const float dl = glm::length(dir);
950 if (dl < 1e-8F) {
951 return glm::vec3(0.0F);
952 }
953 dir /= dl;
954
955 glm::vec3 s = world_plane
956 ? glm::vec3(-dir.y, dir.x, 0.0F)
957 : glm::cross(dir, host_view_normal(p, eye));
958
959 const float sl = glm::length(s);
960 return (sl < 1e-6F ? glm::vec3(0.0F, 1.0F, 0.0F) : s / sl) * half_w;
961 }
962
963} // namespace
964
966 std::span<const uint8_t> src,
967 const Kakshya::VertexLayout& layout,
968 std::span<const DrawRun> runs,
969 const MillSpec& spec,
970 const MillView& view,
971 std::vector<uint8_t>& dst,
972 std::span<const uint32_t> indices)
973{
974 const auto vertex_at = [&indices](uint32_t k) {
975 return indices.empty() ? k : indices[k];
976 };
977
978 auto result = layout;
979 result.vertex_count = 0;
980 dst.clear();
981
982 MillOffsets off;
983 if (!offsets_from(layout, off)) {
984 return result;
985 }
986
987 std::vector<uint32_t> prefix;
988 build_prefix(runs, prefix);
989 const uint32_t total = prefix.empty() ? 0U : prefix.back();
990 if (total == 0) {
991 return result;
992 }
993
994 dst.assign(static_cast<size_t>(total) * layout.stride_bytes, 0);
995 result.vertex_count = total;
996
997 const HostRecords rec {
998 .src = reinterpret_cast<const uint32_t*>(src.data()),
999 .dst = reinterpret_cast<uint32_t*>(dst.data()),
1000 .stride_words = off.stride_words
1001 };
1002
1003 const bool world_plane = spec.ribbon == MillSpec::Ribbon::WorldPlane;
1004 const bool billboard = spec.billboard_instances && !world_plane && off.tangent != ABSENT;
1005 const uint32_t scalar_offset = spec.use_vertex_extent ? off.scalar : ABSENT;
1006
1007 for (size_t r = 0; r < runs.size(); ++r) {
1008 const auto& run = runs[r];
1009 const uint32_t emitted = prefix[r + 1] - prefix[r];
1010 const uint32_t base = prefix[r];
1011 const uint32_t voff = run.vertex_offset;
1012
1013 for (uint32_t local = 0; local < emitted; ++local) {
1014 const uint32_t out = base + local;
1015
1016 const bool is_tri = is_triangle_topology(run.topology);
1017 const bool as_point = run.topology == PrimitiveTopology::POINT_LIST
1018 || (run.fill == PolygonMode::POINT
1019 && (is_tri || run.topology == PrimitiveTopology::LINE_LIST
1020 || run.topology == PrimitiveTopology::LINE_STRIP));
1021 const bool wire = is_tri && run.fill == PolygonMode::LINE;
1022 const auto wire_uv = [](uint32_t c) {
1023 return glm::vec2(c == 0U ? 21.0F : 20.0F, c == 1U ? 1.0F : 0.0F);
1024 };
1025
1026 if (run.topology == PrimitiveTopology::TRIANGLE_LIST && !as_point) {
1027 rec.copy_vertex(out, vertex_at(voff + local));
1028 if (wire) {
1029 rec.write_uv(out, off.uv, wire_uv(local % 3U));
1030 }
1031 continue;
1032 }
1033
1034 if ((run.topology == PrimitiveTopology::TRIANGLE_STRIP
1035 || run.topology == PrimitiveTopology::TRIANGLE_FAN)
1036 && !as_point) {
1037 const uint32_t tri = local / 3U;
1038 const uint32_t c = local - tri * 3U;
1039 uint32_t a = 0;
1040 uint32_t b1 = tri + 1U;
1041 uint32_t b2 = tri + 2U;
1042 if (run.topology == PrimitiveTopology::TRIANGLE_STRIP) {
1043 a = tri;
1044 if ((tri & 1U) == 1U) {
1045 std::swap(a, b1);
1046 }
1047 }
1048 const uint32_t pick = c == 0U ? a : (c == 1U ? b1 : b2);
1049 rec.copy_vertex(out, vertex_at(voff + pick));
1050 if (wire) {
1051 rec.write_uv(out, off.uv, wire_uv(c));
1052 }
1053 continue;
1054 }
1055
1056 const uint32_t corner = local % 6U;
1057 const uint32_t quad = local / 6U;
1058
1059 if (as_point) {
1060 const uint32_t s = run.topology == PrimitiveTopology::POINT_LIST
1061 ? voff + quad
1062 : vertex_at(voff + quad);
1063 const glm::vec3 p = rec.read_pos(s, off.position);
1064 const float h = rec.read_extent(s, scalar_offset, spec.fallback_extent)
1065 * spec.point_scale * 0.5F;
1066
1067 glm::vec3 rx;
1068 glm::vec3 ry;
1069 if (world_plane) {
1070 rx = glm::vec3(h, 0.0F, 0.0F);
1071 ry = glm::vec3(0.0F, h, 0.0F);
1072 } else {
1073 const glm::vec3 n = host_view_normal(p, view.eye);
1074 const glm::vec3 up = std::abs(n.y) < 0.99F
1075 ? glm::vec3(0.0F, 1.0F, 0.0F)
1076 : glm::vec3(1.0F, 0.0F, 0.0F);
1077 rx = glm::normalize(glm::cross(up, n)) * h;
1078 ry = glm::normalize(glm::cross(n, rx)) * h;
1079 }
1080
1081 glm::vec3 o;
1082 glm::vec2 uv;
1083 switch (corner) {
1084 case 1:
1085 o = rx - ry;
1086 uv = { 1.0F, 0.0F };
1087 break;
1088 case 2:
1089 case 4:
1090 o = rx + ry;
1091 uv = { 1.0F, 1.0F };
1092 break;
1093 case 5:
1094 o = -rx + ry;
1095 uv = { 0.0F, 1.0F };
1096 break;
1097 default:
1098 o = -rx - ry;
1099 uv = { 0.0F, 0.0F };
1100 break;
1101 }
1102
1103 rec.copy_vertex(out, s);
1104 if (billboard) {
1105 rec.write_pos(out, off.position, p);
1106 rec.write_tangent(out, off.tangent,
1107 glm::vec4((uv * 2.0F - glm::vec2(1.0F)) * h, 0.0F, 0.0F));
1108 } else {
1109 rec.write_pos(out, off.position, p + o);
1110 }
1111 if (spec.synthesize_uv) {
1112 rec.write_uv(out, off.uv, uv + glm::vec2(10.0F));
1113 }
1114 continue;
1115 }
1116
1117 const uint32_t s0 = run.topology == PrimitiveTopology::LINE_LIST
1118 ? voff + quad * 2U
1119 : voff + quad;
1120 const uint32_t s1 = s0 + 1U;
1121
1122 const glm::vec3 p0 = rec.read_pos(s0, off.position);
1123 const glm::vec3 p1 = rec.read_pos(s1, off.position);
1124 const glm::vec3 d = p1 - p0;
1125
1126 if (glm::length(d) < 1e-6F) {
1127 rec.write_pos(out, off.position, p0);
1128 if (billboard) {
1129 rec.write_tangent(out, off.tangent, glm::vec4(0.0F));
1130 }
1131 continue;
1132 }
1133
1134 const float h0 = rec.read_extent(s0, scalar_offset, spec.fallback_extent)
1135 * spec.width_scale * 0.5F;
1136 const float h1 = rec.read_extent(s1, scalar_offset, spec.fallback_extent)
1137 * spec.width_scale * 0.5F;
1138
1139 const glm::vec3 e0 = host_side_at(
1140 p0, d, view.eye, h0, world_plane);
1141 const glm::vec3 e1 = host_side_at(
1142 p1, d, view.eye, h1, world_plane);
1143
1144 glm::vec3 pos;
1145 uint32_t pick = s0;
1146 glm::vec2 uv;
1147 switch (corner) {
1148 case 1:
1149 pos = p0 + e0;
1150 uv = { 0.0F, 0.0F };
1151 break;
1152 case 2:
1153 case 4:
1154 pos = p1 + e1;
1155 pick = s1;
1156 uv = { 1.0F, 0.0F };
1157 break;
1158 case 5:
1159 pos = p1 - e1;
1160 pick = s1;
1161 uv = { 1.0F, 1.0F };
1162 break;
1163 default:
1164 pos = p0 - e0;
1165 uv = { 0.0F, 1.0F };
1166 break;
1167 }
1168
1169 rec.copy_vertex(out, pick);
1170 if (billboard) {
1171 const float sign = (corner == 0U || corner == 3U || corner == 5U) ? -1.0F : 1.0F;
1172 const float half_w = (corner == 0U || corner == 1U || corner == 3U) ? h0 : h1;
1173 rec.write_pos(out, off.position, pick == s0 ? p0 : p1);
1174 rec.write_tangent(out, off.tangent,
1175 glm::vec4(d / glm::length(d), sign * half_w));
1176 } else {
1177 rec.write_pos(out, off.position, pos);
1178 }
1179 if (run.fill == PolygonMode::LINE) {
1180 rec.write_uv(out, off.uv, wire_uv(corner % 3U));
1181 } else if (spec.synthesize_uv) {
1182 rec.write_uv(out, off.uv, uv);
1183 }
1184 }
1185 }
1186
1187 return result;
1188}
1189
1190} // namespace MayaFlux::Portal::Graphics
#define MF_ERROR(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
#define MF_RT_TRACE(comp, ctx,...)
PrimitiveMill(MillSpec spec={}, uint32_t output_ring=2)
std::shared_ptr< Buffers::VKBuffer > output() const
The milled triangles: the ring slot the last mill() wrote.
std::unique_ptr< SlotArena > m_prefix_arena
uint32_t mill(const std::shared_ptr< Buffers::VKBuffer > &source, std::span< const DrawRun > runs, const MillView &view, std::optional< Kakshya::VertexLayout > layout=std::nullopt)
Mill runs out of source into the owned buffer.
static uint32_t milled_vertex_count(std::span< const DrawRun > runs)
Vertices runs would mill to.
bool ensure_buffers(const std::shared_ptr< Buffers::VKBuffer > &source, const Kakshya::VertexLayout &layout, uint32_t total, size_t run_count)
Grows the destination ring, run and prefix arenas to fit.
void release()
Destroy the kernel, pipeline and buffers.
const MillSpec & spec() const
The current shaping parameters.
void collect_completed()
Reclaim every dispatch that has finished, without waiting.
std::unique_ptr< SlotArena > m_run_arena
bool ensure_kernel()
Compiles the kernel and creates its pipeline with a push descriptor set, once.
std::vector< std::shared_ptr< Buffers::VKBuffer > > m_outputs
Milled buffers rotated between dispatches, all at m_output_capacity.
std::vector< FenceID > m_in_flight
Dispatches submitted and not yet observed complete, oldest first.
void resolve_pending()
Wait on and reclaim every outstanding dispatch.
void release_after_submit(std::function< void()> release)
Run a release action once every submission made so far has completed.
Assemble & pc(std::string name, Kakshya::GpuDataFormat format)
Declare a push constant field with explicit format.
Assemble & ssbo(std::string name, BindingDirection direction, Kakshya::GpuDataFormat format, Kakshya::DataModality modality=Kakshya::DataModality::SCALAR_F32)
Declare an SSBO binding.
Assemble & workgroup(uint32_t x, uint32_t y=1, uint32_t z=1)
Override workgroup size.
Fluent assembler producing a ShaderSpec.
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
void run()
Definition main.cpp:21
@ Rendering
GPU rendering operations (graphics pipeline, frame rendering).
@ Portal
High-level user-facing API layer.
@ SCALAR_F32
Single-channel float data.
Definition NDData.hpp:155
@ FLOAT32
32-bit float (standard GPU)
std::vector< double > mode(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Mode per window via tolerance-bucketed frequency count.
Definition Analysis.cpp:580
Kakshya::VertexLayout mill_on_host(std::span< const uint8_t > src, const Kakshya::VertexLayout &layout, std::span< const DrawRun > runs, const MillSpec &spec, const MillView &view, std::vector< uint8_t > &dst, std::span< const uint32_t > indices)
Host equivalent of PrimitiveMill::mill, over raw bytes.
constexpr ShaderID INVALID_SHADER
constexpr FenceID INVALID_FENCE
constexpr uint32_t triangle_vertex_count(PrimitiveTopology topology, uint32_t n, PolygonMode fill=PolygonMode::FILL) noexcept
Vertices a span of n at topology yields once reduced to a TRIANGLE_LIST, with points becoming quads a...
MAYAFLUX_API ShaderFoundry & get_shader_foundry()
Get the global shader compiler instance.
constexpr ComputePipelineID INVALID_COMPUTE_PIPELINE
@ HOST_STORAGE
Host-visible storage buffer (eStorageBuffer + eHostVisible|eHostCoherent).
PrimitiveTopology
Vertex assembly primitive topology.
MAYAFLUX_API ComputePress & get_compute_press()
uint32_t stride_bytes
Total bytes per vertex (stride in Vulkan terms) e.g., 3 floats (position) + 3 floats (normal) = 24 by...
uint32_t vertex_count
Total number of vertices in this buffer.
Complete description of vertex data layout in a buffer.
Parsed representation of a user-supplied kernel lambda.
@ WorldFacing
Turned to face the viewpoint, in world space.
@ WorldPlane
Held in the XY plane, viewpoint ignored.
bool synthesize_uv
Write 0..1 UVs across each ribbon and quad instead of copying the source.
float fallback_extent
Extent used when the layout carries no scalar attribute.
float width_scale
World units of total ribbon width per unit of a vertex's scalar.
bool billboard_instances
Keep point quads and ribbons unexpanded for an instanced draw.
bool use_vertex_extent
Take ribbon width and point size from each vertex's own scalar (LineVertex::thickness,...
float point_scale
World units per unit of a vertex's own size, on point spans.
How a PrimitiveMill shapes spans.
One binding written straight into a command buffer through a push descriptor set.
Complete declarative description of a generated compute shader.
Backend buffer management service interface.