MayaFlux 0.1.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 Download data from an image (creates staging buffer internally)
106 * @param image Source VKImage
107 * @param data Destination buffer pointer
108 * @param size Buffer size in bytes
109 */
110 void download_image_data(
111 std::shared_ptr<VKImage> image,
112 void* data,
113 size_t size);
114
115 // ========================================================================
116 // Sampler management
117 // ========================================================================
118
119 /**
120 * @brief Create sampler
121 * @param filter Mag/min filter
122 * @param address_mode Texture address mode (wrap, clamp, etc.)
123 * @param anisotropy Max anisotropy (0 = disabled)
124 * @return Sampler handle
125 */
126 vk::Sampler create_sampler(
127 vk::Filter filter = vk::Filter::eLinear,
128 vk::SamplerAddressMode address_mode = vk::SamplerAddressMode::eRepeat,
129 float max_anisotropy = 0.0F);
130
131 /**
132 * @brief Destroy sampler
133 */
134 void destroy_sampler(vk::Sampler sampler);
135
136 // ========================================================================
137 // Memory management
138 // ========================================================================
139
140 /**
141 * @brief Find a suitable memory type for Vulkan buffer allocation
142 * @param type_filter Memory type bits filter
143 * @param properties Desired memory property flags
144 * @return Index of the suitable memory type
145 */
146 uint32_t find_memory_type(uint32_t type_filter, vk::MemoryPropertyFlags properties) const;
147
148 // ========================================================================
149 // Command management
150 // ========================================================================
151
152 /**
153 * @brief Execute immediate command recording for buffer operations
154 * @param recorder Command recording function
155 */
156 void execute_immediate_commands(const std::function<void(vk::CommandBuffer)>& recorder);
157
158 /**
159 * @brief Record deferred command recording for buffer operations
160 * @param recorder Command recording function
161 */
162 void record_deferred_commands(const std::function<void(vk::CommandBuffer)>& recorder);
163
164 // ========================================================================
165 // Cleanup
166 // ========================================================================
167
168 void cleanup();
169
170private:
173
174 std::vector<std::shared_ptr<Buffers::VKBuffer>> m_managed_buffers;
175 std::unordered_map<size_t, vk::Sampler> m_sampler_cache;
176
177 size_t compute_sampler_hash(vk::Filter filter, vk::SamplerAddressMode address_mode, float max_anisotropy) const;
178};
179
180} // 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