MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
GpuResourceManager.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Yantra {
7
10
12
13/**
14 * @class GpuResourceManager
15 * @brief Encapsulates all Vulkan resource lifecycle behind Portal facades.
16 *
17 * Manages a keyed set of pipelines, each with its own storage buffers,
18 * pipeline, shader, and descriptor sets. There is no distinct single-
19 * pipeline mode: a caller using exactly one shader simply uses one key
20 * throughout. PIMPL hides vk:: types from the header entirely.
21 */
22class MAYAFLUX_API GpuResourceManager {
23public:
26
31
32 /**
33 * @brief Create (or confirm existing) pipeline for the given key.
34 *
35 * Create-if-absent: if a unit already exists and is ready for this
36 * key, returns true immediately without recreating anything. Call
37 * release(key) first to force recreation with a different config.
38 */
39 bool initialise(const std::string& key,
40 const GpuComputeConfig& config,
41 const std::vector<GpuBufferBinding>& bindings);
42
43 void ensure_buffer(const std::string& key, size_t index, size_t required_bytes,
44 Portal::Graphics::BufferUsageHint usage_hint = Portal::Graphics::BufferUsageHint::COMPUTE_STORAGE);
45 void upload(const std::string& key, size_t index, const float* data, size_t byte_size);
46 void upload_raw(const std::string& key, size_t index, const uint8_t* data, size_t byte_size);
47 void download(const std::string& key, size_t index, float* dest, size_t byte_size);
48 void bind_descriptor(const std::string& key, size_t index, const GpuBufferBinding& spec);
49
50 void ensure_shared_buffer(uint32_t set, size_t binding_index, size_t element_count,
52 Portal::Graphics::BufferUsageHint usage_hint = Portal::Graphics::BufferUsageHint::COMPUTE_STORAGE);
53 void bind_shared_descriptor(const std::string& key, uint32_t set, size_t binding_index, const GpuBufferBinding& spec);
54 void download_shared(uint32_t set, size_t binding_index, void* dest, size_t byte_size);
55 void upload_shared_raw(uint32_t set, size_t binding_index, const uint8_t* data, size_t byte_size);
56
57 [[nodiscard]] Portal::Graphics::HazardResource make_shared_buffer_hazard(
58 const GpuBufferBinding& spec) const;
59
60 /**
61 * @brief Bind a storage image descriptor at the given slot index.
62 * @param key Pipeline unit to bind into.
63 * @param index Slot index matching the binding declaration.
64 * @param image VKImage to bind. Must be initialised and in eGeneral layout.
65 * @param spec Binding declaration. element_type must be IMAGE_STORAGE.
66 */
67 void bind_image_storage(const std::string& key, size_t index,
68 const std::shared_ptr<Core::VKImage>& image,
69 const GpuBufferBinding& spec);
70
71 /**
72 * @brief Bind a combined image+sampler descriptor at the given slot index.
73 * @param key Pipeline unit to bind into.
74 * @param index Slot index matching the binding declaration.
75 * @param image VKImage to bind. Must be in eShaderReadOnlyOptimal layout.
76 * @param sampler Vulkan sampler handle from SamplerForge.
77 * @param spec Binding declaration. element_type must be IMAGE_SAMPLED.
78 */
79 void bind_image_sampled(const std::string& key, size_t index,
80 const std::shared_ptr<Core::VKImage>& image,
81 vk::Sampler sampler,
82 const GpuBufferBinding& spec);
83
84 /**
85 * @brief Transition a VKImage layout via an immediate command submission.
86 *
87 * Not tied to any pipeline unit; operates on the image directly.
88 * @param image Image to transition.
89 * @param old_layout Source layout.
90 * @param new_layout Target layout.
91 */
92 void transition_image(const std::shared_ptr<Core::VKImage>& image,
93 vk::ImageLayout old_layout,
94 vk::ImageLayout new_layout);
95
96 void dispatch(const std::string& key,
97 const std::array<uint32_t, 3>& groups,
98 const std::vector<GpuBufferBinding>& bindings,
99 const uint8_t* push_constant_data,
100 size_t push_constant_size);
101
102 void dispatch_batched(const std::string& key,
103 const std::array<uint32_t, 3>& groups,
104 const std::vector<GpuBufferBinding>& bindings,
105 size_t push_constant_size,
106 const ExecutionContext& ctx);
107
108 void dispatch_batched_indirect(const std::string& key,
109 uint32_t indirect_set, size_t indirect_binding,
110 const std::array<uint32_t, 3>& groups,
111 const std::vector<GpuBufferBinding>& bindings,
112 size_t push_constant_size,
113 const ExecutionContext& ctx);
114
115 /**
116 * @brief Submit a compute dispatch without blocking.
117 *
118 * Records the same command sequence as dispatch() but submits via
119 * ShaderFoundry::submit_async instead of submit_and_wait. Returns a
120 * FenceID immediately. Readback must be deferred until
121 * ShaderFoundry::is_fence_signaled returns true for the returned ID.
122 *
123 * @param key Pipeline unit to dispatch.
124 * @param groups Workgroup counts {x, y, z}.
125 * @param bindings Binding declarations; output barriers inserted.
126 * @param push_constant_data Pointer to push constant bytes, or nullptr.
127 * @param push_constant_size Byte count of push constant data.
128 * @return FenceID to poll with ShaderFoundry::is_fence_signaled.
129 * Returns INVALID_FENCE if not ready or submission fails.
130 */
131 [[nodiscard]] Portal::Graphics::FenceID dispatch_async(const std::string& key,
132 const std::array<uint32_t, 3>& groups,
133 const std::vector<GpuBufferBinding>& bindings,
134 const uint8_t* push_constant_data,
135 size_t push_constant_size);
136
137 /**
138 * @brief Record a dispatch for each requested key into one command
139 * buffer via ComputePress::record_sequence, submitted once.
140 *
141 * Each key must already be initialise()'d. Vectors are parallel,
142 * indexed by position, one entry per key in the same order as keys.
143 */
144 void dispatch_sequence(
145 const std::vector<std::string>& keys,
146 const std::vector<std::array<uint32_t, 3>>& groups_per_key,
147 const std::vector<std::vector<uint8_t>>& push_constants_per_key,
148 const std::vector<std::vector<Portal::Graphics::HazardResource>>& hazards_per_key);
149
150 /**
151 * @brief Destroy the pipeline, shader, descriptor sets, and buffers
152 * for a single key, without affecting any other key.
153 */
154 void release(const std::string& key);
155
156 /**
157 * @brief Destroy every key currently held. Called from the destructor.
158 */
159 void cleanup();
160
161 [[nodiscard]] bool is_ready(const std::string& key) const;
162 [[nodiscard]] size_t buffer_allocated_bytes(const std::string& key, size_t index) const;
163
164private:
166 Portal::Graphics::ShaderID shader_id { Portal::Graphics::INVALID_SHADER };
167 Portal::Graphics::ComputePipelineID pipeline_id { Portal::Graphics::INVALID_COMPUTE_PIPELINE };
168 std::vector<Portal::Graphics::DescriptorSetID> descriptor_set_ids;
169
170 std::unique_ptr<GpuResourceManagerImpl> impl;
171
172 struct BufferSlot {
173 size_t allocated_bytes {};
174 };
175 std::vector<BufferSlot> buffer_slots;
176 std::vector<std::shared_ptr<Core::VKImage>> image_slots;
177
178 bool ready {};
179 };
180
181 struct SharedBuffers;
182 std::unique_ptr<SharedBuffers> m_shared;
183
184 [[nodiscard]] PipelineUnit& unit_for(const std::string& key);
185 [[nodiscard]] const PipelineUnit* find_unit(const std::string& key) const;
186
187 std::unordered_map<std::string, std::unique_ptr<PipelineUnit>> m_units;
188}; // class GpuResourceManager
189
190} // namespace MayaFlux::Yantra
IO::ImageData image
Definition Decoder.cpp:64
GpuResourceManager & operator=(GpuResourceManager &&)=delete
std::unique_ptr< SharedBuffers > m_shared
GpuResourceManager(GpuResourceManager &&)=delete
GpuResourceManager & operator=(const GpuResourceManager &)=delete
std::unordered_map< std::string, std::unique_ptr< PipelineUnit > > m_units
GpuResourceManager(const GpuResourceManager &)=delete
Encapsulates all Vulkan resource lifecycle behind Portal facades.
BufferUsageHint
Semantic usage hint for buffer allocation and memory properties.
ElementType
Element type the shader expects in this binding.
Declares a single storage buffer or image binding a compute shader expects.
Plain-data description of the compute shader to dispatch.
One resource this stage's dispatch reads/writes that a later stage in the sequence depends on,...
Context information controlling how a compute operation executes.
std::vector< Portal::Graphics::DescriptorSetID > descriptor_set_ids
std::unique_ptr< GpuResourceManagerImpl > impl
std::vector< std::shared_ptr< Core::VKImage > > image_slots