MayaFlux 0.4.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 Query the Vulkan device address of an initialized BDA-capable buffer
48 * @param buffer Buffer with Usage::UNIFORM_BDA or Usage::STORAGE_BDA
49 * @return vk::DeviceAddress, or 0 if the buffer was not created with eShaderDeviceAddress
50 */
51 [[nodiscard]] vk::DeviceAddress get_buffer_device_address(
52 const std::shared_ptr<Buffers::VKBuffer>& buffer) const;
53
54 /**
55 * @brief Flush any pending buffer operations (e.g., uploads/downloads)
56 */
57 void flush_pending_buffer_operations();
58
59 // ========================================================================
60 // Buffer management
61 // ========================================================================
62
63 /**
64 * @brief Initialize a VKImage (allocate VkImage, memory, and create image view)
65 * @param image VKImage to initialize
66 *
67 * Follows the same pattern as initialize_buffer:
68 * 1. Create VkImage
69 * 2. Allocate VkDeviceMemory
70 * 3. Bind memory to image
71 * 4. Create VkImageView
72 * 5. Store handles in VKImage
73 */
74 void initialize_image(const std::shared_ptr<VKImage>& image);
75
76 /**
77 * @brief Cleanup a VKImage (destroy view, image, and free memory)
78 * @param image VKImage to cleanup
79 */
80 void cleanup_image(const std::shared_ptr<VKImage>& image);
81
82 /**
83 * @brief Transition image layout using a pipeline barrier
84 * @param image VkImage handle
85 * @param old_layout Current layout
86 * @param new_layout Target layout
87 * @param mip_levels Number of mip levels to transition
88 * @param array_layers Number of array layers to transition
89 *
90 * Executes immediately on graphics queue. Use for initial setup and
91 * one-off transitions. For rendering, prefer manual barriers.
92 */
93 void transition_image_layout(
94 vk::Image image,
95 vk::ImageLayout old_layout,
96 vk::ImageLayout new_layout,
97 uint32_t mip_levels = 1,
98 uint32_t array_layers = 1,
99 vk::ImageAspectFlags aspect_flags = vk::ImageAspectFlagBits::eColor);
100
101 /**
102 * @brief Upload data to an image (creates staging buffer internally)
103 * @param image Target VKImage
104 * @param data Pixel data pointer
105 * @param size Data size in bytes
106 */
107 void upload_image_data(
108 std::shared_ptr<VKImage> image,
109 const void* data,
110 size_t size);
111
112 /**
113 * @brief Upload image data using a caller-supplied persistent staging buffer.
114 * Identical to upload_image_data() but skips the per-call VkBuffer
115 * allocation. The staging buffer must be host-visible and at least
116 * @p size bytes. Intended for high-frequency streaming uploads.
117 * @param image Target VKImage.
118 * @param data Source pixel data pointer.
119 * @param size Byte count.
120 * @param staging Pre-allocated host-visible staging buffer.
121 */
122 void upload_image_data_with_staging(
123 std::shared_ptr<VKImage> image,
124 const void* data,
125 size_t size,
126 const std::shared_ptr<Buffers::VKBuffer>& staging);
127
128 /**
129 * @brief Download data from an image into a caller-supplied buffer.
130 *
131 * Transitions the image to eTransferSrcOptimal, copies to a staging buffer,
132 * then restores it to restore_layout using restore_stage.
133 *
134 * The defaults cover the common case of a device-local texture in shader
135 * read-only layout. Pass ePresentSrcKHR / eBottomOfPipe for swapchain images.
136 *
137 * @param image Source image.
138 * @param data Destination host pointer (must be at least size bytes).
139 * @param size Byte count to read.
140 * @param restore_layout Layout to transition back to after the copy.
141 * Defaults to eShaderReadOnlyOptimal.
142 * @param restore_stage Pipeline stage that will consume the image after
143 * restore. Defaults to eFragmentShader.
144 */
145 void download_image_data(
146 std::shared_ptr<VKImage> image,
147 void* data,
148 size_t size,
149 vk::ImageLayout restore_layout = vk::ImageLayout::eShaderReadOnlyOptimal,
150 vk::PipelineStageFlags restore_stage = vk::PipelineStageFlagBits::eFragmentShader);
151
152 // ========================================================================
153 // Sampler management
154 // ========================================================================
155
156 /**
157 * @brief Create sampler
158 * @param filter Mag/min filter
159 * @param address_mode Texture address mode (wrap, clamp, etc.)
160 * @param anisotropy Max anisotropy (0 = disabled)
161 * @return Sampler handle
162 */
163 vk::Sampler create_sampler(
164 vk::Filter filter = vk::Filter::eLinear,
165 vk::SamplerAddressMode address_mode = vk::SamplerAddressMode::eRepeat,
166 float max_anisotropy = 0.0F);
167
168 /**
169 * @brief Destroy sampler
170 */
171 void destroy_sampler(vk::Sampler sampler);
172
173 // ========================================================================
174 // Memory management
175 // ========================================================================
176
177 /**
178 * @brief Find a suitable memory type for Vulkan buffer allocation
179 * @param type_filter Memory type bits filter
180 * @param properties Desired memory property flags
181 * @return Index of the suitable memory type
182 */
183 uint32_t find_memory_type(uint32_t type_filter, vk::MemoryPropertyFlags properties) const;
184
185 // ========================================================================
186 // Command management
187 // ========================================================================
188
189 /**
190 * @brief Execute immediate command recording for buffer operations
191 * @param recorder Command recording function
192 */
193 void execute_immediate_commands(const std::function<void(vk::CommandBuffer)>& recorder);
194
195 /**
196 * @brief Record deferred command recording for buffer operations
197 * @param recorder Command recording function
198 */
199 void record_deferred_commands(const std::function<void(vk::CommandBuffer)>& recorder);
200
201 // ========================================================================
202 // Cleanup
203 // ========================================================================
204
205 void cleanup();
206
207private:
210
211 std::vector<std::shared_ptr<Buffers::VKBuffer>> m_managed_buffers;
212 std::unordered_map<size_t, vk::Sampler> m_sampler_cache;
213
214 size_t compute_sampler_hash(vk::Filter filter, vk::SamplerAddressMode address_mode, float max_anisotropy) const;
215};
216
217} // namespace MayaFlux::Core
IO::ImageData image
Range size
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
std::shared_ptr< Kriya::SamplingPipeline > create_sampler(const std::string &filepath, uint32_t num_samples, bool truncate, uint32_t channel, uint64_t max_dur_ms)
Construct a built SamplingPipeline from an audio file.
Definition Rigs.cpp:15