MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextureProcessor.cpp
Go to the documentation of this file.
2
4#include "TextureBuffer.hpp"
5
7
10
12
14
15namespace MayaFlux::Buffers {
16
22
24
25void TextureProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
26{
27 if (!m_buffer_service) {
30 }
31
32 if (!m_buffer_service) {
33 error<std::runtime_error>(
36 std::source_location::current(),
37 "TextureProcessor requires a valid buffer service");
38 }
39
40 auto tex_buffer = std::dynamic_pointer_cast<TextureBuffer>(buffer);
41 if (!tex_buffer) {
42 return;
43 }
44
45 m_texture_buffer = tex_buffer;
46
47 if (!tex_buffer->is_initialized()) {
48 try {
50 } catch (const std::exception& e) {
51 error_rethrow(
54 std::source_location::current(),
55 "Failed to initialize texture buffer: {}", e.what());
56 }
57 }
58
60
62 "TextureProcessor attached to {}x{} TextureBuffer",
63 tex_buffer->get_width(), tex_buffer->get_height());
64}
65
66void TextureProcessor::on_detach(const std::shared_ptr<Buffer>& /*buffer*/)
67{
68 m_texture_buffer.reset();
69 m_stream_staging.reset();
70}
71
72void TextureProcessor::processing_function(const std::shared_ptr<Buffer>& /*buffer*/)
73{
74 if (!m_texture_buffer) {
75 return;
76 }
77
79
81}
82
83// =========================================================================
84// Initialization
85// =========================================================================
86
88{
89 if (!m_texture_buffer) {
90 return;
91 }
92
93 m_texture_buffer->m_gpu_texture = create_gpu_texture();
94
96
98
99 m_texture_buffer->m_texture_dirty = false;
100 m_texture_buffer->m_geometry_dirty = false;
101
103 "TextureProcessor: GPU resources initialized");
104}
105
107{
108 if (!m_texture_buffer || m_texture_buffer->m_vertex_bytes.empty()) {
109 return;
110 }
111
112 try {
114 m_texture_buffer->m_vertex_bytes.data(),
115 m_texture_buffer->m_vertex_bytes.size(),
117
119 "TextureProcessor: uploaded {} bytes of geometry data",
120 m_texture_buffer->m_vertex_bytes.size());
121 } catch (const std::exception& e) {
123 "Failed to upload initial geometry: {}", e.what());
124 }
125}
126
128{
129 if (!m_texture_buffer || !m_texture_buffer->has_texture()) {
130 return;
131 }
132
133 const auto& pixel_data = m_texture_buffer->m_pixel_data;
134 if (pixel_data.empty()) {
136 "TextureProcessor: no pixel data to upload (uninitialized texture)");
137 return;
138 }
139
141 loom.upload_data(
142 m_texture_buffer->get_texture(),
143 pixel_data.data(),
144 pixel_data.size());
145
147 "TextureProcessor: uploaded {} bytes of pixel data", pixel_data.size());
148}
149
150// =========================================================================
151// Per-Frame Updates
152// =========================================================================
153
155{
156 if (!m_texture_buffer || !m_texture_buffer->m_geometry_dirty) {
157 return;
158 }
159
160 m_texture_buffer->generate_quad_with_transform();
161
162 try {
164 m_texture_buffer->m_vertex_bytes.data(),
165 m_texture_buffer->m_vertex_bytes.size(),
167
168 m_texture_buffer->m_geometry_dirty = false;
169
171 "TextureProcessor: geometry updated and uploaded");
172 } catch (const std::exception& e) {
174 "Failed to update geometry: {}", e.what());
175 }
176}
177
179{
180 if (!m_texture_buffer || !m_texture_buffer->m_texture_dirty) {
181 return;
182 }
183
184 if (!m_texture_buffer->has_texture()) {
185 m_texture_buffer->m_gpu_texture = create_gpu_texture();
186 }
187
188 const auto& pixel_data = m_texture_buffer->m_pixel_data;
189 if (pixel_data.empty()) {
190 return;
191 }
192
194
197 m_texture_buffer->get_texture()->get_size_bytes());
198 }
199
201 loom.upload_data(
202 m_texture_buffer->get_texture(),
203 pixel_data.data(),
204 pixel_data.size(),
206 } else {
207 loom.upload_data(
208 m_texture_buffer->get_texture(),
209 pixel_data.data(),
210 pixel_data.size());
211 }
212
213 m_texture_buffer->m_texture_dirty = false;
214
216 "TextureProcessor: pixel data updated ({} bytes)", pixel_data.size());
217}
218
219// =========================================================================
220// GPU Resource Creation
221// =========================================================================
222
223std::shared_ptr<Core::VKImage> TextureProcessor::create_gpu_texture()
224{
225 if (!m_texture_buffer) {
226 return nullptr;
227 }
228
230
231 auto texture = loom.create_2d(
232 m_texture_buffer->get_width(),
233 m_texture_buffer->get_height(),
234 m_texture_buffer->get_format(),
235 nullptr,
236 1);
237
238 if (!texture) {
239 error<std::runtime_error>(
242 std::source_location::current(),
243 "Failed to create GPU texture via TextureLoom");
244 }
245
247 "TextureProcessor: created GPU VKImage {}x{}",
248 m_texture_buffer->get_width(), m_texture_buffer->get_height());
249
250 return texture;
251}
252
253void TextureProcessor::generate_quad_vertices(std::vector<uint8_t>& out_bytes)
254{
255 // Helper for future enhancements (actual transform application)
256 // For now, this is called by TextureBuffer::generate_quad_with_transform()
257 // Future: apply m_position, m_scale, m_rotation here
258}
259
260} // 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_detach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is detached from a buffer.
void update_geometry_if_dirty()
Regenerate quad vertices if transform changed, upload if needed.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
The core processing function that must be implemented by derived classes.
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_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
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
std::shared_ptr< Buffers::VKBuffer > m_stream_staging
Persistent host-visible staging buffer for streaming pixel uploads.
void upload_initial_geometry()
Upload initial quad geometry based on default or custom vertices.
Registry::Service::BufferService * m_buffer_service
Definition VKBuffer.hpp:555
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
std::shared_ptr< VKBuffer > create_image_staging_buffer(size_t size)
Allocate a persistent host-visible staging buffer sized for repeated streaming uploads to an image of...
@ 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.