MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ mill()

uint32_t MayaFlux::Portal::Graphics::PrimitiveMill::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.

Parameters
sourceVertex buffer the spans index into. Must carry a vertex layout unless layout is supplied. The stride must be a multiple of four bytes and the position attribute word aligned. With an index buffer, triangle spans count and offset indices; point and line spans stay vertex addressed.
runsSpans to mill, in output order.
viewViewpoint, used only by Ribbon::WorldFacing.
layoutOptional layout describing the source data. Read from source when omitted.
Returns
Vertices written, or 0 when there was nothing to mill or the source could not be used.

Does not wait on its own dispatch. It resolves the previous call's submission, submits this one and returns. output() can be recorded against immediately, but it is not valid to read from the host until a later mill() or release() has resolved the fence.

Runs on the graphics queue, so a draw submitted afterwards is ordered after the write. The destination grows to fit and never shrinks.

Definition at line 662 of file PrimitiveMill.cpp.

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) {
768 PushDescriptorWrite write;
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) {
776 PushDescriptorWrite write;
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}
#define MF_RT_ERROR(comp, ctx,...)
#define MF_RT_TRACE(comp, ctx,...)
std::unique_ptr< SlotArena > m_prefix_arena
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 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 run()
Definition main.cpp:21
@ Rendering
GPU rendering operations (graphics pipeline, frame rendering).
@ Portal
High-level user-facing API layer.
Source source()
Begin a Source chain.
Definition Plot.hpp:127
PolygonMode
Rasterization polygon mode.
constexpr FenceID INVALID_FENCE
MAYAFLUX_API ShaderFoundry & get_shader_foundry()
Get the global shader compiler instance.
MAYAFLUX_API ComputePress & get_compute_press()
std::shared_ptr< Buffers::TextBuffer > press(std::string_view text, const PressParams &params)
Composite a UTF-8 string into a new TextBuffer.
Definition InkPress.cpp:344
@ WorldFacing
Turned to face the viewpoint, in world space.

References MayaFlux::Portal::Graphics::PushDescriptorWrite::binding, MayaFlux::Portal::Graphics::PushDescriptorWrite::buffer, collect_completed(), ensure_buffers(), ensure_kernel(), MayaFlux::Portal::Graphics::get_compute_press(), MayaFlux::Portal::Graphics::get_shader_foundry(), MayaFlux::Portal::Graphics::ShaderFoundry::GRAPHICS, MayaFlux::Portal::Graphics::INVALID_FENCE, MayaFlux::Portal::Graphics::LINE, MayaFlux::Portal::Graphics::LINE_LIST, MayaFlux::Portal::Graphics::LINE_STRIP, m_in_flight, m_milled_count, m_output_slot, m_outputs, m_pipeline, m_prefix, m_prefix_arena, m_prefix_slot, m_run_arena, m_run_slot, m_run_words, m_spec, MF_RT_ERROR, MF_RT_TRACE, MayaFlux::Portal::Graphics::POINT, MayaFlux::Journal::Portal, MayaFlux::Journal::Rendering, run(), MayaFlux::Portal::Graphics::PushDescriptorWrite::type, and MayaFlux::Portal::Graphics::MillSpec::WorldFacing.

Referenced by MayaFlux::Buffers::RenderProcessor::mill_runs(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function: