MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextureProcessor.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Core {
6class VKImage;
7}
8
9namespace MayaFlux::Buffers {
10
11class TextureBuffer;
12
13/**
14 * @class TextureProcessor
15 * @brief Internal processor: handles CPU→GPU transfers for TextureBuffer
16 *
17 * TextureProcessor is automatically created and attached by TextureBuffer.
18 * Users never instantiate or interact with it directly.
19 *
20 * Responsibilities:
21 * - Initialize GPU texture (VKImage)
22 * - Upload pixel data to GPU (initial + dirty updates)
23 * - Generate and upload quad geometry respecting transform
24 * - Detect changes and re-upload as needed
25 *
26 * All work is invisible to the user. They just modify TextureBuffer
27 * (set_pixel_data, set_position, etc.) and it "just works."
28 */
29class MAYAFLUX_API TextureProcessor : public VKBufferProcessor {
30public:
31 /** @enum PixelSource
32 * @brief Source of pixel data for GPU uploads
33 *
34 * - BUFFER_STORAGE: Read from m_texture_buffer->m_pixel_data (default)
35 * - EXTERNAL_DATA: Call get_variant_source(), expects DataVariant supply
36 */
37 enum class PixelSource : uint8_t {
38 BUFFER_STORAGE,
39 EXTERNAL_DATA
40 };
41
44
45 /**
46 * @brief Enable or disable streaming mode for pixel uploads
47 *
48 * When enabled, TextureProcessor will reuse a persistent host-visible staging buffer
49 * for all pixel uploads, eliminating the per-frame Vulkan object churn that causes
50 * VK_ERROR_DEVICE_LOST under sustained updates (e.g. video playback).
51 *
52 * The staging buffer is allocated on first dirty update via TextureLoom::create_streaming_staging()
53 * and reused every subsequent frame. This is only used for pixel data uploads, not geometry.
54 *
55 * @param enabled True to enable streaming mode, false to disable (default)
56 */
57 void set_streaming_mode(bool enabled) { m_streaming_mode = enabled; }
58
59 /**
60 * @brief Check if streaming mode is enabled
61 * @return True if streaming mode is enabled, false otherwise
62 */
63 [[nodiscard]] bool is_streaming_mode() const { return m_streaming_mode; }
64
65 /**
66 * @brief Invalidate the persistent streaming staging buffer.
67 *
68 * Forces reallocation on the next dirty upload cycle, sized to the
69 * current GPU texture footprint. Call after resize_texture().
70 */
71 void invalidate_staging() { m_stream_staging.reset(); }
72
73protected:
74 void on_attach(const std::shared_ptr<Buffer>& buffer) override;
75 void on_detach(const std::shared_ptr<Buffer>& buffer) override;
76 void processing_function(const std::shared_ptr<Buffer>& buffer) override;
77
78 virtual std::optional<Kakshya::DataVariant> get_variant_source() { return std::nullopt; }
79
80 PixelSource m_pixel_source { PixelSource::BUFFER_STORAGE };
81 std::shared_ptr<TextureBuffer> m_texture_buffer;
82
83private:
84 /**
85 * @brief Persistent host-visible staging buffer for streaming pixel uploads.
86 * Allocated on first dirty update via TextureLoom::create_streaming_staging().
87 * Reused every subsequent frame — no per-frame Vulkan object allocation.
88 */
89 std::shared_ptr<Buffers::VKBuffer> m_stream_staging;
90
91 bool m_streaming_mode {};
92
93 // =========================================================================
94 // Initialization (on_attach)
95 // =========================================================================
96
97 /**
98 * Initialize all GPU resources:
99 * - VKImage for texture
100 * - Vertex buffer with quad geometry
101 * - Pixel upload
102 */
103 void initialize_gpu_resources();
104
105 /**
106 * Upload initial quad geometry based on default or custom vertices
107 */
108 void upload_initial_geometry();
109
110 /**
111 * Upload initial pixel data to GPU texture
112 */
113 void upload_initial_pixels();
114
115 // =========================================================================
116 // Per-Frame Updates (processing_function)
117 // =========================================================================
118
119 /**
120 * Regenerate quad vertices if transform changed, upload if needed
121 */
122 void update_geometry_if_dirty();
123
124 /**
125 * Re-upload pixels to GPU if they changed
126 */
127 void update_pixels_if_dirty();
128
129 // =========================================================================
130 // GPU Resource Creation
131 // =========================================================================
132
133 /**
134 * Create VKImage for texture storage
135 */
136 std::shared_ptr<Core::VKImage> create_gpu_texture();
137
138 /**
139 * Generate quad vertices respecting current transform
140 * Handles both default quad and custom vertices
141 */
142 void generate_quad_vertices(std::vector<uint8_t>& out_bytes);
143};
144
145} // namespace MayaFlux::Buffers
PixelSource
Source of pixel data for GPU uploads.
bool is_streaming_mode() const
Check if streaming mode is enabled.
void invalidate_staging()
Invalidate the persistent streaming staging buffer.
virtual std::optional< Kakshya::DataVariant > get_variant_source()
void set_streaming_mode(bool enabled)
Enable or disable streaming mode for pixel uploads.
std::shared_ptr< TextureBuffer > m_texture_buffer
std::shared_ptr< Buffers::VKBuffer > m_stream_staging
Persistent host-visible staging buffer for streaming pixel uploads.
Internal processor: handles CPU→GPU transfers for TextureBuffer.