MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
FormaProcessor.cpp
Go to the documentation of this file.
1#include "FormaProcessor.hpp"
2
7
8namespace MayaFlux::Buffers {
9
30
31void FormaProcessor::set_bytes(std::vector<uint8_t> bytes)
32{
33 m_pending_geometry = std::move(bytes);
34 m_geometry_dirty.test_and_set(std::memory_order_release);
35}
36
37bool FormaProcessor::has_pending() const noexcept
38{
39 return m_geometry_dirty.test(std::memory_order_acquire);
40}
41
42void FormaProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
43{
44 auto vk = std::dynamic_pointer_cast<VKBuffer>(buffer);
45 if (!vk) {
46 error<std::invalid_argument>(
49 std::source_location::current(),
50 "FormaProcessor requires a VKBuffer");
51 }
52
53 m_staging = create_staging_buffer(vk->get_size_bytes());
54
55 switch (m_topology) {
57 vk->set_vertex_layout(Kakshya::VertexLayout::for_points());
58 break;
61 vk->set_vertex_layout(Kakshya::VertexLayout::for_lines());
62 break;
65 default:
66 vk->set_vertex_layout(Kakshya::VertexLayout::for_meshes());
67 break;
68 }
69}
70
71void FormaProcessor::on_detach(const std::shared_ptr<Buffer>& /*buffer*/)
72{
73 m_staging.reset();
74 m_pending_geometry.clear();
75 m_active.clear();
76}
77
78void FormaProcessor::set_texture(std::shared_ptr<Core::VKImage> image, std::string binding)
79{
80 m_pending_texture = PendingTexture { .image = std::move(image), .binding = std::move(binding) };
81 m_texture_dirty.test_and_set(std::memory_order_release);
82}
83
84void FormaProcessor::processing_function(const std::shared_ptr<Buffer>& buffer)
85{
86 if (m_texture_dirty.test(std::memory_order_acquire)) {
87 m_texture_dirty.clear(std::memory_order_release);
88
89 auto vk = std::dynamic_pointer_cast<VKBuffer>(buffer);
90 if (vk) {
91 if (auto rp = vk->get_render_processor()) {
93 rp->bind_texture(m_pending_texture->binding,
94 m_pending_texture->image);
95 }
96 }
97 }
98 m_pending_texture.reset();
99 }
100
101 if (!m_geometry_dirty.test(std::memory_order_acquire))
102 return;
103
104 m_geometry_dirty.clear(std::memory_order_release);
105 std::swap(m_active, m_pending_geometry);
106
107 if (m_active.empty())
108 return;
109
110 auto vk = std::dynamic_pointer_cast<VKBuffer>(buffer);
111 if (!vk)
112 return;
113
114 const size_t required = m_active.size();
115 const size_t available = vk->get_size_bytes();
116
117 if (required > available) {
118 vk->resize(static_cast<size_t>(required * 1.5F), false);
119 m_staging = create_staging_buffer(vk->get_size_bytes());
120 }
121
123 m_active.data(),
124 std::min<size_t>(required, vk->get_size_bytes()),
125 vk,
126 m_staging);
127
128 auto layout = vk->get_vertex_layout();
129 if (layout) {
130 auto updated = *layout;
131 updated.vertex_count = vertex_count(m_active.size());
132 vk->set_vertex_layout(updated);
133 }
134}
135
136uint32_t FormaProcessor::vertex_count(size_t byte_count) const noexcept
137{
138 if (m_stride == 0)
139 return 0;
140 return static_cast<uint32_t>(byte_count / m_stride);
141}
142
143} // namespace MayaFlux::Buffers
IO::ImageData image
Definition Decoder.cpp:57
Portal::Graphics::PrimitiveTopology m_topology
void set_bytes(std::vector< uint8_t > bytes)
Supply new vertex bytes for the next graphics tick.
void set_texture(std::shared_ptr< Core::VKImage > image, std::string binding)
Supply a texture to bind on the next graphics tick.
std::vector< uint8_t > m_pending_geometry
FormaProcessor(Portal::Graphics::PrimitiveTopology topology)
std::shared_ptr< VKBuffer > m_staging
uint32_t vertex_count(size_t byte_count) const noexcept
std::optional< PendingTexture > m_pending_texture
void processing_function(const std::shared_ptr< Buffer > &buffer) override
The core processing function that must be implemented by derived classes.
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is detached from a buffer.
@ GRAPHICS_BACKEND
Standard graphics processing backend configuration.
std::shared_ptr< VKBuffer > create_staging_buffer(size_t size)
Create staging buffer for transfers.
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.
PrimitiveTopology
Vertex assembly primitive topology.
uint32_t stride_bytes
Total bytes per vertex (stride in Vulkan terms) e.g., 3 floats (position) + 3 floats (normal) = 24 by...
static VertexLayout for_lines(uint32_t stride=60)
Factory: layout for LineVertex (position, color, thickness, uv, normal, tangent)
static VertexLayout for_meshes(uint32_t stride=60)
Factory: layout for MeshVertex (position, color, weight, uv, normal, tangent)
static VertexLayout for_points(uint32_t stride=60)
Factory: layout for PointVertex (position, color, size, uv, normal, tangent)