MayaFlux 0.3.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:
33
34 /**
35 * @brief Enable or disable streaming mode for pixel uploads
36 *
37 * When enabled, TextureProcessor will reuse a persistent host-visible staging buffer
38 * for all pixel uploads, eliminating the per-frame Vulkan object churn that causes
39 * VK_ERROR_DEVICE_LOST under sustained updates (e.g. video playback).
40 *
41 * The staging buffer is allocated on first dirty update via TextureLoom::create_streaming_staging()
42 * and reused every subsequent frame. This is only used for pixel data uploads, not geometry.
43 *
44 * @param enabled True to enable streaming mode, false to disable (default)
45 */
46 void set_streaming_mode(bool enabled) { m_streaming_mode = enabled; }
47
48 /**
49 * @brief Check if streaming mode is enabled
50 * @return True if streaming mode is enabled, false otherwise
51 */
52 [[nodiscard]] bool is_streaming_mode() const { return m_streaming_mode; }
53
54protected:
55 void on_attach(const std::shared_ptr<Buffer>& buffer) override;
56 void on_detach(const std::shared_ptr<Buffer>& buffer) override;
57 void processing_function(const std::shared_ptr<Buffer>& buffer) override;
58
59private:
60 std::shared_ptr<TextureBuffer> m_texture_buffer;
61
62 /**
63 * @brief Persistent host-visible staging buffer for streaming pixel uploads.
64 * Allocated on first dirty update via TextureLoom::create_streaming_staging().
65 * Reused every subsequent frame — no per-frame Vulkan object allocation.
66 */
67 std::shared_ptr<Buffers::VKBuffer> m_stream_staging;
68
69 bool m_streaming_mode {};
70
71 // =========================================================================
72 // Initialization (on_attach)
73 // =========================================================================
74
75 /**
76 * Initialize all GPU resources:
77 * - VKImage for texture
78 * - Vertex buffer with quad geometry
79 * - Pixel upload
80 */
81 void initialize_gpu_resources();
82
83 /**
84 * Upload initial quad geometry based on default or custom vertices
85 */
86 void upload_initial_geometry();
87
88 /**
89 * Upload initial pixel data to GPU texture
90 */
91 void upload_initial_pixels();
92
93 // =========================================================================
94 // Per-Frame Updates (processing_function)
95 // =========================================================================
96
97 /**
98 * Regenerate quad vertices if transform changed, upload if needed
99 */
100 void update_geometry_if_dirty();
101
102 /**
103 * Re-upload pixels to GPU if they changed
104 */
105 void update_pixels_if_dirty();
106
107 // =========================================================================
108 // GPU Resource Creation
109 // =========================================================================
110
111 /**
112 * Create VKImage for texture storage
113 */
114 std::shared_ptr<Core::VKImage> create_gpu_texture();
115
116 /**
117 * Generate quad vertices respecting current transform
118 * Handles both default quad and custom vertices
119 */
120 void generate_quad_vertices(std::vector<uint8_t>& out_bytes);
121};
122
123} // namespace MayaFlux::Buffers
bool is_streaming_mode() const
Check if streaming mode is enabled.
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.