MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
GeometryWriteProcessor.cpp
Go to the documentation of this file.
2
5
6namespace MayaFlux::Buffers {
7
12
14{
15 m_pending_data = std::move(variant);
16 m_data_dirty.test_and_set(std::memory_order_release);
17}
18
20 const void* data, size_t byte_count,
21 const Kakshya::VertexLayout& layout)
22{
23 VertexSnapshot snap;
24 snap.bytes.resize(byte_count);
25 std::memcpy(snap.bytes.data(), data, byte_count);
26 snap.layout = layout;
27
28 m_pending_vertices = std::move(snap);
29 m_vertices_dirty.test_and_set(std::memory_order_release);
30}
31
33{
34 return m_data_dirty.test(std::memory_order_acquire);
35}
36
37void GeometryWriteProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
38{
39 auto vk = std::dynamic_pointer_cast<VKBuffer>(buffer);
40 if (!vk) {
41 error<std::invalid_argument>(
44 std::source_location::current(),
45 "GeometryWriteProcessor requires a VKBuffer");
46 }
47
48 if (!vk->is_host_visible()) {
49 m_staging = create_staging_buffer(vk->get_size_bytes());
50 }
51}
52
53void GeometryWriteProcessor::on_detach(const std::shared_ptr<Buffer>& /*buffer*/)
54{
55 m_staging.reset();
56 m_active_data.reset();
57 m_active_vertices.reset();
58 m_pending_data.reset();
59 m_pending_vertices.reset();
60}
61
62void GeometryWriteProcessor::processing_function(const std::shared_ptr<Buffer>& buffer)
63{
64 auto vk = std::dynamic_pointer_cast<VKBuffer>(buffer);
65 if (!vk) {
67 "GeometryWriteProcessor attached to non-VKBuffer");
68 return;
69 }
70
71 if (m_vertices_dirty.test(std::memory_order_acquire)) {
72 m_vertices_dirty.clear(std::memory_order_release);
74
75 if (m_active_vertices.has_value()) {
76 const size_t required = m_active_vertices->bytes.size();
77 const size_t available = vk->get_size_bytes();
78
79 if (required > available) {
80 vk->resize(static_cast<size_t>((float)required * 1.5F), false);
81 if (m_staging) {
82 m_staging = create_staging_buffer(vk->get_size_bytes());
83 }
84 }
85
86 upload_to_gpu(m_active_vertices->bytes.data(),
87 std::min<size_t>(required, vk->get_size_bytes()),
88 vk, m_staging);
89
90 vk->set_vertex_layout(m_active_vertices->layout);
91 return;
92 }
93 }
94
95 if (!m_data_dirty.test(std::memory_order_acquire)) {
96 return;
97 }
98
99 m_data_dirty.clear(std::memory_order_release);
100 std::swap(m_active_data, m_pending_data);
101
102 if (!m_active_data.has_value()) {
103 return;
104 }
105
106 std::optional<Kakshya::VertexAccess> access;
107 switch (m_mode) {
110 break;
113 break;
116 break;
117 }
118
119 if (!access.has_value()) {
121 "GeometryWriteProcessor: as_vertex_access returned nullopt — unsupported variant type");
122 return;
123 }
124
125 upload_resizing(access->data_ptr, access->byte_count, vk, m_staging);
126
127 vk->set_vertex_layout(access->layout);
128}
129
130} // namespace MayaFlux::Buffers
#define MF_RT_ERROR(comp, ctx,...)
void set_data(Kakshya::DataVariant variant)
Supply vertex data for the next cycle.
bool has_pending() const noexcept
Returns true if a snapshot has been set and not yet consumed.
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is detached from a buffer.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
The core processing function that must be implemented by derived classes.
void set_vertices(const void *data, size_t byte_count, const Kakshya::VertexLayout &layout)
Supply pre-resolved vertex bytes for the next cycle.
std::optional< Kakshya::DataVariant > m_pending_data
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
std::optional< Kakshya::DataVariant > m_active_data
std::optional< VertexSnapshot > m_pending_vertices
std::optional< VertexSnapshot > m_active_vertices
void upload_resizing(const void *data, size_t size, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging, float growth_factor)
Upload size bytes to target, growing both buffers first if needed.
@ GRAPHICS_BACKEND
Standard graphics processing backend configuration.
std::shared_ptr< VKBuffer > create_staging_buffer(size_t size)
Create staging buffer for transfers.
@ LINE
Interpret vertex data as Nodes::LineVertex.
@ MESH
Interpret vertex data as Nodes::MeshVertex.
@ POINT
Interpret vertex data as Nodes::PointVertex.
void upload_to_gpu(const void *data, size_t size, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging)
Upload raw data to GPU buffer (auto-detects host-visible vs device-local)
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Buffers
Buffers, Managers, processors and processing chains.
std::optional< VertexAccess > as_line_vertex_access(const DataVariant &variant, const VertexAccessConfig &config)
Convert DataVariant to line-vertex-compatible bytes.
std::optional< VertexAccess > as_mesh_vertex_access(const DataVariant &variant, const VertexAccessConfig &config)
Convert DataVariant to mesh-vertex-compatible bytes.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
Definition NDData.hpp:76
std::optional< VertexAccess > as_point_vertex_access(const DataVariant &variant, const VertexAccessConfig &config)
Convert DataVariant to point-vertex-compatible bytes.
Owned copy of pre-resolved vertex bytes and their layout.
Complete description of vertex data layout in a buffer.