MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextureProcessor.cpp
Go to the documentation of this file.
2
8#include "TextureBuffer.hpp"
9
10namespace MayaFlux::Buffers {
11
17
19
20void TextureProcessor::on_attach(std::shared_ptr<Buffer> buffer)
21{
22 if (!m_buffer_service) {
25 }
26
27 if (!m_buffer_service) {
28 error<std::runtime_error>(
31 std::source_location::current(),
32 "TextureProcessor requires a valid buffer service");
33 }
34
35 auto tex_buffer = std::dynamic_pointer_cast<TextureBuffer>(buffer);
36 if (!tex_buffer) {
37 return;
38 }
39
40 m_texture_buffer = tex_buffer;
41
42 if (!tex_buffer->is_initialized()) {
43 try {
45 } catch (const std::exception& e) {
46 error_rethrow(
49 std::source_location::current(),
50 "Failed to initialize texture buffer: {}", e.what());
51 }
52 }
53
55
57 "TextureProcessor attached to {}x{} TextureBuffer",
58 tex_buffer->get_width(), tex_buffer->get_height());
59}
60
61void TextureProcessor::on_detach(std::shared_ptr<Buffer> /*buffer*/)
62{
63 m_texture_buffer.reset();
64}
65
66void TextureProcessor::processing_function(std::shared_ptr<Buffer> /*buffer*/)
67{
68 if (!m_texture_buffer) {
69 return;
70 }
71
73
75}
76
77// =========================================================================
78// Initialization
79// =========================================================================
80
82{
83 if (!m_texture_buffer) {
84 return;
85 }
86
87 m_texture_buffer->m_gpu_texture = create_gpu_texture();
88
90
92
93 m_texture_buffer->m_texture_dirty = false;
94 m_texture_buffer->m_geometry_dirty = false;
95
97 "TextureProcessor: GPU resources initialized");
98}
99
101{
102 if (!m_texture_buffer || m_texture_buffer->m_vertex_bytes.empty()) {
103 return;
104 }
105
106 try {
108 m_texture_buffer->m_vertex_bytes.data(),
109 m_texture_buffer->m_vertex_bytes.size(),
111
113 "TextureProcessor: uploaded {} bytes of geometry data",
114 m_texture_buffer->m_vertex_bytes.size());
115 } catch (const std::exception& e) {
117 "Failed to upload initial geometry: {}", e.what());
118 }
119}
120
122{
123 if (!m_texture_buffer || !m_texture_buffer->has_texture()) {
124 return;
125 }
126
127 const auto& pixel_data = m_texture_buffer->m_pixel_data;
128 if (pixel_data.empty()) {
130 "TextureProcessor: no pixel data to upload (uninitialized texture)");
131 return;
132 }
133
135 loom.upload_data(
136 m_texture_buffer->get_texture(),
137 pixel_data.data(),
138 pixel_data.size());
139
141 "TextureProcessor: uploaded {} bytes of pixel data", pixel_data.size());
142}
143
144// =========================================================================
145// Per-Frame Updates
146// =========================================================================
147
149{
150 if (!m_texture_buffer || !m_texture_buffer->m_geometry_dirty) {
151 return;
152 }
153
154 m_texture_buffer->generate_quad_with_transform();
155
156 try {
158 m_texture_buffer->m_vertex_bytes.data(),
159 m_texture_buffer->m_vertex_bytes.size(),
161
162 m_texture_buffer->m_geometry_dirty = false;
163
165 "TextureProcessor: geometry updated and uploaded");
166 } catch (const std::exception& e) {
168 "Failed to update geometry: {}", e.what());
169 }
170}
171
173{
174 if (!m_texture_buffer || !m_texture_buffer->m_texture_dirty) {
175 return;
176 }
177
178 if (!m_texture_buffer->has_texture()) {
179 m_texture_buffer->m_gpu_texture = create_gpu_texture();
180 }
181
182 const auto& pixel_data = m_texture_buffer->m_pixel_data;
183 if (pixel_data.empty()) {
184 return;
185 }
186
188 loom.upload_data(
189 m_texture_buffer->get_texture(),
190 pixel_data.data(),
191 pixel_data.size());
192
193 m_texture_buffer->m_texture_dirty = false;
194
196 "TextureProcessor: pixel data updated ({} bytes)", pixel_data.size());
197}
198
199// =========================================================================
200// GPU Resource Creation
201// =========================================================================
202
203std::shared_ptr<Core::VKImage> TextureProcessor::create_gpu_texture()
204{
205 if (!m_texture_buffer) {
206 return nullptr;
207 }
208
210
211 auto texture = loom.create_2d(
212 m_texture_buffer->get_width(),
213 m_texture_buffer->get_height(),
214 m_texture_buffer->get_format(),
215 nullptr,
216 1);
217
218 if (!texture) {
219 error<std::runtime_error>(
222 std::source_location::current(),
223 "Failed to create GPU texture via TextureLoom");
224 }
225
227 "TextureProcessor: created GPU VKImage {}x{}",
228 m_texture_buffer->get_width(), m_texture_buffer->get_height());
229
230 return texture;
231}
232
233void TextureProcessor::generate_quad_vertices(std::vector<uint8_t>& out_bytes)
234{
235 // Helper for future enhancements (actual transform application)
236 // For now, this is called by TextureBuffer::generate_quad_with_transform()
237 // Future: apply m_position, m_scale, m_rotation here
238}
239
240} // namespace MayaFlux::Buffers
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
std::shared_ptr< Core::VKImage > create_gpu_texture()
Create VKImage for texture storage.
void on_attach(std::shared_ptr< Buffer > buffer) override
Called when this processor is attached to a buffer.
void update_geometry_if_dirty()
Regenerate quad vertices if transform changed, upload if needed.
void initialize_gpu_resources()
Initialize all GPU resources:
void update_pixels_if_dirty()
Re-upload pixels to GPU if they changed.
void upload_initial_pixels()
Upload initial pixel data to GPU texture.
void on_detach(std::shared_ptr< Buffer > buffer) override
Called when this processor is detached from a buffer.
void processing_function(std::shared_ptr< Buffer > buffer) override
The core processing function that must be implemented by derived classes.
void generate_quad_vertices(std::vector< uint8_t > &out_bytes)
Generate quad vertices respecting current transform Handles both default quad and custom vertices.
std::shared_ptr< TextureBuffer > m_texture_buffer
void upload_initial_geometry()
Upload initial quad geometry based on default or custom vertices.
Registry::Service::BufferService * m_buffer_service
Definition VKBuffer.hpp:468
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
@ GRAPHICS_BACKEND
Standard graphics processing backend configuration.
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.
MAYAFLUX_API TextureLoom & get_texture_manager()
Get the global texture manager instance.
std::function< void(const std::shared_ptr< void > &)> initialize_buffer
Initialize a buffer object.
Backend buffer management service interface.