MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
BackendResoureManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "vulkan/vulkan.hpp"
4
6struct BufferService;
7}
8
9namespace MayaFlux::Buffers {
10class VKBuffer;
11}
12
13namespace MayaFlux::Core {
14
15class VKContext;
16class VKImage;
17class VKCommandManager;
18
19/**
20 * @class BackendResourceManager
21 * @brief Manages Vulkan resources (buffers, images, samplers) for the graphics backend
22 */
23class MAYAFLUX_API BackendResourceManager {
24public:
25 BackendResourceManager(VKContext& context, VKCommandManager& command_manager);
27
28 void setup_backend_service(const std::shared_ptr<Registry::Service::BufferService>& buffer_service);
29
30 // ========================================================================
31 // Buffer management
32 // ========================================================================
33
34 /**
35 * @brief Initialize a buffer for use with the graphics backend
36 * @param buffer Shared pointer to the buffer to initialize
37 */
38 void initialize_buffer(const std::shared_ptr<Buffers::VKBuffer>& buffer);
39
40 /**
41 * @brief Cleanup a buffer and release associated resources
42 * @param buffer Shared pointer to the buffer to cleanup
43 */
44 void cleanup_buffer(const std::shared_ptr<Buffers::VKBuffer>& buffer);
45
46 /**
47 * @brief Flush any pending buffer operations (e.g., uploads/downloads)
48 */
49 void flush_pending_buffer_operations();
50
51 // ========================================================================
52 // Buffer management
53 // ========================================================================
54
55 /**
56 * @brief Initialize a VKImage (allocate VkImage, memory, and create image view)
57 * @param image VKImage to initialize
58 *
59 * Follows the same pattern as initialize_buffer:
60 * 1. Create VkImage
61 * 2. Allocate VkDeviceMemory
62 * 3. Bind memory to image
63 * 4. Create VkImageView
64 * 5. Store handles in VKImage
65 */
66 void initialize_image(const std::shared_ptr<VKImage>& image);
67
68 /**
69 * @brief Cleanup a VKImage (destroy view, image, and free memory)
70 * @param image VKImage to cleanup
71 */
72 void cleanup_image(const std::shared_ptr<VKImage>& image);
73
74 /**
75 * @brief Transition image layout using a pipeline barrier
76 * @param image VkImage handle
77 * @param old_layout Current layout
78 * @param new_layout Target layout
79 * @param mip_levels Number of mip levels to transition
80 * @param array_layers Number of array layers to transition
81 *
82 * Executes immediately on graphics queue. Use for initial setup and
83 * one-off transitions. For rendering, prefer manual barriers.
84 */
85 void transition_image_layout(
86 vk::Image image,
87 vk::ImageLayout old_layout,
88 vk::ImageLayout new_layout,
89 uint32_t mip_levels = 1,
90 uint32_t array_layers = 1,
91 vk::ImageAspectFlags aspect_flags = vk::ImageAspectFlagBits::eColor);
92
93 /**
94 * @brief Upload data to an image (creates staging buffer internally)
95 * @param image Target VKImage
96 * @param data Pixel data pointer
97 * @param size Data size in bytes
98 */
99 void upload_image_data(
100 std::shared_ptr<VKImage> image,
101 const void* data,
102 size_t size);
103
104 /**
105 * @brief Upload image data using a caller-supplied persistent staging buffer.
106 * Identical to upload_image_data() but skips the per-call VkBuffer
107 * allocation. The staging buffer must be host-visible and at least
108 * @p size bytes. Intended for high-frequency streaming uploads.
109 * @param image Target VKImage.
110 * @param data Source pixel data pointer.
111 * @param size Byte count.
112 * @param staging Pre-allocated host-visible staging buffer.
113 */
114 void upload_image_data_with_staging(
115 std::shared_ptr<VKImage> image,
116 const void* data,
117 size_t size,
118 const std::shared_ptr<Buffers::VKBuffer>& staging);
119
120 /**
121 * @brief Download data from an image into a caller-supplied buffer.
122 *
123 * Transitions the image to eTransferSrcOptimal, copies to a staging buffer,
124 * then restores it to restore_layout using restore_stage.
125 *
126 * The defaults cover the common case of a device-local texture in shader
127 * read-only layout. Pass ePresentSrcKHR / eBottomOfPipe for swapchain images.
128 *
129 * @param image Source image.
130 * @param data Destination host pointer (must be at least size bytes).
131 * @param size Byte count to read.
132 * @param restore_layout Layout to transition back to after the copy.
133 * Defaults to eShaderReadOnlyOptimal.
134 * @param restore_stage Pipeline stage that will consume the image after
135 * restore. Defaults to eFragmentShader.
136 */
137 void download_image_data(
138 std::shared_ptr<VKImage> image,
139 void* data,
140 size_t size,
141 vk::ImageLayout restore_layout = vk::ImageLayout::eShaderReadOnlyOptimal,
142 vk::PipelineStageFlags restore_stage = vk::PipelineStageFlagBits::eFragmentShader);
143
144 // ========================================================================
145 // Sampler management
146 // ========================================================================
147
148 /**
149 * @brief Create sampler
150 * @param filter Mag/min filter
151 * @param address_mode Texture address mode (wrap, clamp, etc.)
152 * @param anisotropy Max anisotropy (0 = disabled)
153 * @return Sampler handle
154 */
155 vk::Sampler create_sampler(
156 vk::Filter filter = vk::Filter::eLinear,
157 vk::SamplerAddressMode address_mode = vk::SamplerAddressMode::eRepeat,
158 float max_anisotropy = 0.0F);
159
160 /**
161 * @brief Destroy sampler
162 */
163 void destroy_sampler(vk::Sampler sampler);
164
165 // ========================================================================
166 // Memory management
167 // ========================================================================
168
169 /**
170 * @brief Find a suitable memory type for Vulkan buffer allocation
171 * @param type_filter Memory type bits filter
172 * @param properties Desired memory property flags
173 * @return Index of the suitable memory type
174 */
175 uint32_t find_memory_type(uint32_t type_filter, vk::MemoryPropertyFlags properties) const;
176
177 // ========================================================================
178 // Command management
179 // ========================================================================
180
181 /**
182 * @brief Execute immediate command recording for buffer operations
183 * @param recorder Command recording function
184 */
185 void execute_immediate_commands(const std::function<void(vk::CommandBuffer)>& recorder);
186
187 /**
188 * @brief Record deferred command recording for buffer operations
189 * @param recorder Command recording function
190 */
191 void record_deferred_commands(const std::function<void(vk::CommandBuffer)>& recorder);
192
193 // ========================================================================
194 // Cleanup
195 // ========================================================================
196
197 void cleanup();
198
199private:
202
203 std::vector<std::shared_ptr<Buffers::VKBuffer>> m_managed_buffers;
204 std::unordered_map<size_t, vk::Sampler> m_sampler_cache;
205
206 size_t compute_sampler_hash(vk::Filter filter, vk::SamplerAddressMode address_mode, float max_anisotropy) const;
207};
208
209} // namespace MayaFlux::Core
std::unordered_map< size_t, vk::Sampler > m_sampler_cache
std::vector< std::shared_ptr< Buffers::VKBuffer > > m_managed_buffers
Manages Vulkan resources (buffers, images, samplers) for the graphics backend.
Manages Vulkan command pools and command buffers.
High-level wrapper for Vulkan instance and device.
Definition VKContext.hpp:16