13using Buffers::VKBuffer;
17 constexpr uint32_t WORKGROUP = 256;
18 constexpr uint32_t ABSENT = 0xFFFFFFFFU;
21 constexpr size_t RUN_WORDS = 3;
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 };
40 bool offsets_from(
const Kakshya::VertexLayout& layout, MillOffsets& out)
42 if (layout.stride_bytes == 0 || layout.stride_bytes %
sizeof(uint32_t) != 0) {
51 out.stride_words = layout.stride_bytes /
static_cast<uint32_t
>(
sizeof(uint32_t));
52 out.position = *position;
70 uint32_t stride_words;
71 uint32_t position_offset;
72 uint32_t scalar_offset;
78 float fallback_extent;
83 uint32_t tangent_offset;
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");
91 assemble.function(
"uint",
"find_run",
"uint g, uint 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"
100 assemble.function(
"uint",
"vertex_at",
"uint k, uint ix",
101 " return ix == 1u ? indices[k] : k;\n");
103 assemble.function(
"vec2",
"wire_uv",
"uint c",
104 " return vec2(c == 0u ? 21.0 : 20.0, c == 1u ? 1.0 : 0.0);\n");
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");
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");
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");
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");
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");
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");
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");
150 assemble.function(
"vec3",
"dir_in",
"uint s, uint lo, uint sw, uint po",
151 " vec3 p = read_pos(s, sw, po);\n"
153 " for (uint k = 0u; k < 4u; ++k) {\n"
154 " if (c <= lo) { break; }\n"
156 " vec3 q = read_pos(c, sw, po);\n"
157 " if (length(p - q) > 1e-6) { return normalize(p - q); }\n"
159 " return vec3(0.0);\n");
162 assemble.function(
"vec3",
"dir_out",
"uint s, uint hi, uint sw, uint po",
163 " vec3 p = read_pos(s, sw, po);\n"
165 " for (uint k = 0u; k < 4u; ++k) {\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"
171 " return vec3(0.0);\n");
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"
191 " for (uint k = 0u; k < 4u; ++k) {\n"
192 " if (c <= lo) { break; }\n"
194 " sum += read_extent(c, sw, so, fb);\n"
196 " if (length(read_pos(c, sw, po) - p) > 1e-6) { break; }\n"
199 " for (uint k = 0u; k < 4u; ++k) {\n"
201 " if (c >= hi) { break; }\n"
202 " sum += read_extent(c, sw, so, fb);\n"
204 " if (length(read_pos(c, sw, po) - p) > 1e-6) { break; }\n"
206 " return sum / cnt;\n");
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"
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"
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");
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"
252 " float l = length(d);\n"
253 " return l < 1e-6 ? vec3(0.0) : d / l;\n");
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"
263 " float l = length(d);\n"
264 " return l < 1e-6 ? vec3(0.0) : d / l;\n");
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");
316 add_helpers(assemble);
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";
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";
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";
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";
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";
351 body +=
" uint corner = local % 6u;\n";
352 body +=
" uint quad = local / 6u;\n";
353 body +=
" vec2 uv;\n";
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";
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";
385 body +=
" if (synth_uv == 1u) { write_uv(i, sw, uv_offset, uv); }\n";
386 body +=
" return;\n";
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";
399 body +=
" return;\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";
431 body +=
" e0 = offset_from(p0, din, dn, dn, eye, h0);\n";
432 body +=
" e1 = offset_from(p1, dn, dout, dn, eye, h1);\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";
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";
453 assemble.kernel(
KernelSource { .body = std::move(body) });
455 return assemble.build();
461 const MillOffsets& off,
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,
474 .
mode = spec.ribbon ==
MillSpec::Ribbon::WorldPlane ? 1U : 0
U,
475 .synth_uv = spec.synthesize_uv ? 1U : 0
U,
476 .width_scale = spec.width_scale,
477 .point_scale = spec.point_scale,
478 .fallback_extent = spec.fallback_extent,
482 .indexed = indexed ? 1U : 0
U,
483 .tangent_offset = billboard ? off.tangent : ABSENT
496 void build_prefix(std::span<const DrawRun> runs, std::vector<uint32_t>& prefix)
498 prefix.assign(runs.size() + 1, 0U);
499 for (
size_t r = 0; r < runs.size(); ++r) {
500 prefix[r + 1] = prefix[r]
529 for (
const auto&
run : runs) {
541 const auto spec = build_mill_spec();
546 "PrimitiveMill: kernel expects {} push constant bytes but MillPC is {}",
555 "PrimitiveMill: kernel failed to compile");
563 "PrimitiveMill: pipeline creation failed");
571 const std::shared_ptr<VKBuffer>& source,
580 "PrimitiveMill: BufferService unavailable");
585 const auto grown =
static_cast<uint32_t
>(
static_cast<float>(total) * 1.5F);
587 auto milled_layout = layout;
592 for (
const auto& buffer : outgoing) {
593 svc->destroy_buffer(buffer);
600 slot = std::make_shared<VKBuffer>(
602 VKBuffer::Usage::VERTEX,
603 source->get_modality());
604 slot->set_vertex_layout(milled_layout);
605 svc->initialize_buffer(slot);
612 auto milled_layout = *slot->get_vertex_layout();
613 milled_layout.vertex_count = total;
614 slot->set_vertex_layout(milled_layout);
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) {
624 arena = std::make_unique<SlotArena>();
627 static_cast<size_t>(
static_cast<float>(bytes) * 1.5F),
631 fit_arena(
m_run_arena, std::max<size_t>(run_count * RUN_WORDS *
sizeof(uint32_t),
sizeof(uint32_t)));
656 foundry.wait_for_fence(fence);
657 foundry.release_fence(fence);
663 const std::shared_ptr<VKBuffer>& source,
664 std::span<const DrawRun> runs,
666 std::optional<Kakshya::VertexLayout> layout)
672 if (!source || runs.empty()) {
677 layout = source->get_vertex_layout();
678 if (!layout.has_value()) {
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);
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) {
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");
711 const uint32_t total =
m_prefix.back();
725 for (
size_t r = 0; r < runs.size(); ++r) {
728 const bool emulated = is_triangle_topology(runs[r].topology)
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;
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");
750 const auto pc = make_pc(
m_spec, view, off, total, runs.size(), indexed, billboard);
759 foundry.buffer_barrier(
762 vk::AccessFlagBits2::eVertexAttributeRead,
763 vk::AccessFlagBits2::eShaderWrite,
764 vk::PipelineStageFlagBits2::eVertexInput,
765 vk::PipelineStageFlagBits2::eComputeShader);
767 const auto storage_write = [](uint32_t binding,
const std::shared_ptr<VKBuffer>& buf) {
770 write.
type = vk::DescriptorType::eStorageBuffer;
771 write.
buffer = vk::DescriptorBufferInfo { buf->get_buffer(), 0, buf->get_size_bytes() };
775 const auto slot_write = [](uint32_t binding,
const vk::DescriptorBufferInfo& info) {
778 write.
type = vk::DescriptorType::eStorageBuffer;
786 index_write.
buffer = vk::DescriptorBufferInfo {
787 source->get_index_buffer(), 0, source->get_index_buffer_size()
791 const std::array<SlotLease, 2> leases {
797 storage_write(0, source),
798 slot_write(1, run_info),
804 press.push_constants(cmd_id,
m_pipeline, &pc,
sizeof(MillPC));
805 press.dispatch(cmd_id, (total + WORKGROUP - 1) / WORKGROUP, 1, 1);
807 foundry.buffer_barrier(
810 vk::AccessFlagBits2::eShaderWrite,
811 vk::AccessFlagBits2::eVertexAttributeRead,
812 vk::PipelineStageFlagBits2::eComputeShader,
813 vk::PipelineStageFlagBits2::eVertexInput);
815 const FenceID fence = foundry.submit_async(cmd_id);
818 "PrimitiveMill: dispatch submission failed");
827 "PrimitiveMill: milled {} runs into {} vertices", runs.size(), total);
876 uint32_t stride_words;
878 [[nodiscard]] glm::vec3 read_pos(uint32_t v, uint32_t po)
const
880 const uint32_t* w = src +
static_cast<size_t>(v) * stride_words + po;
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));
888 [[nodiscard]]
float read_extent(uint32_t v, uint32_t so,
float fb)
const
894 std::memcpy(&f, src +
static_cast<size_t>(v) * stride_words + so,
sizeof(
float));
898 void copy_vertex(uint32_t d, uint32_t s)
const
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));
906 void write_pos(uint32_t d, uint32_t po,
const glm::vec3& p)
const
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));
914 void write_uv(uint32_t d, uint32_t uo,
const glm::vec2& t)
const
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));
924 void write_tangent(uint32_t d, uint32_t to,
const glm::vec4& t)
const
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));
934 glm::vec3 host_view_normal(
const glm::vec3& p,
const glm::vec3& eye)
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;
941 glm::vec3 host_side_at(
943 const glm::vec3& seg,
944 const glm::vec3& eye,
949 const float dl = glm::length(dir);
951 return glm::vec3(0.0F);
955 glm::vec3 s = world_plane
956 ? glm::vec3(-dir.y, dir.x, 0.0F)
957 : glm::cross(dir, host_view_normal(p, eye));
959 const float sl = glm::length(s);
960 return (sl < 1e-6F ? glm::vec3(0.0F, 1.0F, 0.0F) : s / sl) * half_w;
966 std::span<const uint8_t> src,
968 std::span<const DrawRun> runs,
971 std::vector<uint8_t>& dst,
972 std::span<const uint32_t> indices)
974 const auto vertex_at = [&indices](uint32_t k) {
975 return indices.empty() ? k : indices[k];
978 auto result = layout;
983 if (!offsets_from(layout, off)) {
987 std::vector<uint32_t> prefix;
988 build_prefix(runs, prefix);
989 const uint32_t total = prefix.empty() ? 0U : prefix.back();
994 dst.assign(
static_cast<size_t>(total) * layout.
stride_bytes, 0);
995 result.vertex_count = total;
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
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;
1013 for (uint32_t local = 0; local < emitted; ++local) {
1014 const uint32_t out = base + local;
1016 const bool is_tri = is_triangle_topology(
run.topology);
1022 const auto wire_uv = [](uint32_t c) {
1023 return glm::vec2(c == 0U ? 21.0F : 20.0F, c == 1U ? 1.0F : 0.0F);
1027 rec.copy_vertex(out, vertex_at(voff + local));
1029 rec.write_uv(out, off.uv, wire_uv(local % 3U));
1037 const uint32_t tri = local / 3U;
1038 const uint32_t c = local - tri * 3U;
1040 uint32_t b1 = tri + 1U;
1041 uint32_t b2 = tri + 2U;
1044 if ((tri & 1U) == 1U) {
1048 const uint32_t pick = c == 0U ? a : (c == 1U ? b1 : b2);
1049 rec.copy_vertex(out, vertex_at(voff + pick));
1051 rec.write_uv(out, off.uv, wire_uv(c));
1056 const uint32_t corner = local % 6U;
1057 const uint32_t quad = local / 6U;
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)
1070 rx = glm::vec3(h, 0.0F, 0.0F);
1071 ry = glm::vec3(0.0F, h, 0.0F);
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;
1086 uv = { 1.0F, 0.0F };
1091 uv = { 1.0F, 1.0F };
1095 uv = { 0.0F, 1.0F };
1099 uv = { 0.0F, 0.0F };
1103 rec.copy_vertex(out, s);
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));
1109 rec.write_pos(out, off.position, p + o);
1112 rec.write_uv(out, off.uv, uv + glm::vec2(10.0F));
1120 const uint32_t s1 = s0 + 1U;
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;
1126 if (glm::length(d) < 1e-6F) {
1127 rec.write_pos(out, off.position, p0);
1129 rec.write_tangent(out, off.tangent, glm::vec4(0.0F));
1134 const float h0 = rec.read_extent(s0, scalar_offset, spec.
fallback_extent)
1136 const float h1 = rec.read_extent(s1, scalar_offset, spec.
fallback_extent)
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);
1150 uv = { 0.0F, 0.0F };
1156 uv = { 1.0F, 0.0F };
1161 uv = { 1.0F, 1.0F };
1165 uv = { 0.0F, 1.0F };
1169 rec.copy_vertex(out, pick);
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));
1177 rec.write_pos(out, off.position, pos);
1180 rec.write_uv(out, off.uv, wire_uv(corner % 3U));
1182 rec.write_uv(out, off.uv, uv);
#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
std::vector< uint32_t > m_prefix
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.
size_t m_push_constant_size
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.
uint32_t m_output_capacity
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.
ComputePipelineID m_pipeline
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.
std::vector< uint32_t > m_run_words
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.
@ Rendering
GPU rendering operations (graphics pipeline, frame rendering).
@ Portal
High-level user-facing API layer.
@ SCALAR_F32
Single-channel float data.
@ 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.
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.
vk::DescriptorBufferInfo buffer
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.