MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
GpuResourceManager.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Yantra {
6
7/**
8 * @struct GpuShaderConfig
9 * @brief Plain-data description of the compute shader to dispatch.
10 */
12 std::string shader_path;
13 std::array<uint32_t, 3> workgroup_size { 256, 1, 1 };
14 size_t push_constant_size { 0 };
15};
16
17/**
18 * @struct GpuBufferBinding
19 * @brief Declares a single storage buffer the shader expects.
20 */
22 uint32_t set { 0 };
23 uint32_t binding { 0 };
24 bool skip_auto_readback { false };
25
26 enum class Direction : uint8_t {
27 INPUT,
28 OUTPUT,
30 } direction { Direction::INPUT };
31
32 /**
33 * @brief Element type the shader expects in this buffer.
34 *
35 * FLOAT32 — cast double channels to float (default).
36 * UINT32 — reinterpret variant bytes as uint32_t.
37 * INT32 — reinterpret variant bytes as int32_t.
38 * PASSTHROUGH — upload raw variant bytes with no cast; caller
39 * must pre-stage via stage_passthrough() for
40 * INPUT / INPUT_OUTPUT bindings.
41 * IMAGE_STORAGE, — writeonly/readonly image2D — eStorageImage descriptor
42 * IMAGE_SAMPLED — sampler2D — eCombinedImageSampler descriptor
43 */
44 enum class ElementType : uint8_t {
45 FLOAT32,
46 UINT32,
47 INT32,
51 } element_type { ElementType::FLOAT32 };
52};
53
54struct GpuResourceManagerImpl;
55
56/**
57 * @class GpuResourceManager
58 * @brief Encapsulates all Vulkan resource lifecycle behind Portal facades.
59 *
60 * Manages storage buffers, pipeline, shader, and descriptor sets.
61 * PIMPL hides vk:: types from the header entirely.
62 */
64public:
67
72
73 bool initialise(const GpuShaderConfig& config,
74 const std::vector<GpuBufferBinding>& bindings);
75
76 void ensure_buffer(size_t index, size_t required_bytes);
77 void upload(size_t index, const float* data, size_t byte_size);
78 void upload_raw(size_t index, const uint8_t* data, size_t byte_size);
79 void download(size_t index, float* dest, size_t byte_size);
80 void bind_descriptor(size_t index, const GpuBufferBinding& spec);
81
82 /**
83 * @brief Bind a storage image descriptor at the given slot index.
84 * @param index Slot index matching the binding declaration.
85 * @param image VKImage to bind. Must be initialised and in eGeneral layout.
86 * @param spec Binding declaration. element_type must be IMAGE_STORAGE.
87 */
88 void bind_image_storage(size_t index,
89 const std::shared_ptr<Core::VKImage>& image,
90 const GpuBufferBinding& spec);
91
92 /**
93 * @brief Bind a combined image+sampler descriptor at the given slot index.
94 * @param index Slot index matching the binding declaration.
95 * @param image VKImage to bind. Must be in eShaderReadOnlyOptimal layout.
96 * @param sampler Vulkan sampler handle from SamplerForge.
97 * @param spec Binding declaration. element_type must be IMAGE_SAMPLED.
98 */
99 void bind_image_sampled(size_t index,
100 const std::shared_ptr<Core::VKImage>& image,
101 vk::Sampler sampler,
102 const GpuBufferBinding& spec);
103
104 /**
105 * @brief Transition a VKImage layout via an immediate command submission.
106 * @param image Image to transition.
107 * @param old_layout Source layout.
108 * @param new_layout Target layout.
109 */
110 void transition_image(const std::shared_ptr<Core::VKImage>& image,
111 vk::ImageLayout old_layout,
112 vk::ImageLayout new_layout);
113
114 void dispatch(const std::array<uint32_t, 3>& groups,
115 const std::vector<GpuBufferBinding>& bindings,
116 const uint8_t* push_constant_data,
117 size_t push_constant_size);
118
119 void dispatch_batched(
120 uint32_t pass_count,
121 const std::array<uint32_t, 3>& groups,
122 const std::vector<GpuBufferBinding>& bindings,
123 const std::function<void(uint32_t pass, std::vector<uint8_t>&)>& push_constant_updater,
124 size_t push_constant_size,
125 const std::unordered_map<std::string, std::any>& execution_metadata = {});
126
127 /**
128 * @brief Submit a compute dispatch without blocking.
129 *
130 * Records the same command sequence as dispatch() but submits via
131 * ShaderFoundry::submit_async instead of submit_and_wait. Returns a
132 * FenceID immediately. Readback must be deferred until
133 * ShaderFoundry::is_fence_signaled returns true for the returned ID.
134 *
135 * @param groups Workgroup counts {x, y, z}.
136 * @param bindings Binding declarations; output barriers inserted.
137 * @param push_constant_data Pointer to push constant bytes, or nullptr.
138 * @param push_constant_size Byte count of push constant data.
139 * @return FenceID to poll with ShaderFoundry::is_fence_signaled.
140 * Returns INVALID_FENCE if not ready or submission fails.
141 */
143 const std::array<uint32_t, 3>& groups,
144 const std::vector<GpuBufferBinding>& bindings,
145 const uint8_t* push_constant_data,
146 size_t push_constant_size);
147
148 void cleanup();
149
150 [[nodiscard]] bool is_ready() const { return m_ready; }
151 [[nodiscard]] size_t buffer_allocated_bytes(size_t index) const;
152
153private:
156 std::vector<Portal::Graphics::DescriptorSetID> m_descriptor_set_ids;
157
158 std::unique_ptr<GpuResourceManagerImpl> m_impl;
159
160 struct BufferSlot {
162 };
163 std::vector<BufferSlot> m_buffer_slots;
164 std::vector<std::shared_ptr<Core::VKImage>> m_image_slots;
165
166 bool m_ready {};
167}; // class GpuResourceManager
168
169} // namespace MayaFlux::Yantra
IO::ImageData image
Definition Decoder.cpp:57
size_t buffer_allocated_bytes(size_t index) const
GpuResourceManager & operator=(GpuResourceManager &&)=delete
void upload_raw(size_t index, const uint8_t *data, size_t byte_size)
void upload(size_t index, const float *data, size_t byte_size)
GpuResourceManager(GpuResourceManager &&)=delete
Portal::Graphics::ComputePipelineID m_pipeline_id
std::vector< std::shared_ptr< Core::VKImage > > m_image_slots
Portal::Graphics::FenceID dispatch_async(const std::array< uint32_t, 3 > &groups, const std::vector< GpuBufferBinding > &bindings, const uint8_t *push_constant_data, size_t push_constant_size)
Submit a compute dispatch without blocking.
std::vector< Portal::Graphics::DescriptorSetID > m_descriptor_set_ids
void download(size_t index, float *dest, size_t byte_size)
void bind_image_storage(size_t index, const std::shared_ptr< Core::VKImage > &image, const GpuBufferBinding &spec)
Bind a storage image descriptor at the given slot index.
void dispatch_batched(uint32_t pass_count, const std::array< uint32_t, 3 > &groups, const std::vector< GpuBufferBinding > &bindings, const std::function< void(uint32_t pass, std::vector< uint8_t > &)> &push_constant_updater, size_t push_constant_size, const std::unordered_map< std::string, std::any > &execution_metadata={})
bool initialise(const GpuShaderConfig &config, const std::vector< GpuBufferBinding > &bindings)
void bind_image_sampled(size_t index, const std::shared_ptr< Core::VKImage > &image, vk::Sampler sampler, const GpuBufferBinding &spec)
Bind a combined image+sampler descriptor at the given slot index.
GpuResourceManager & operator=(const GpuResourceManager &)=delete
void transition_image(const std::shared_ptr< Core::VKImage > &image, vk::ImageLayout old_layout, vk::ImageLayout new_layout)
Transition a VKImage layout via an immediate command submission.
void ensure_buffer(size_t index, size_t required_bytes)
void dispatch(const std::array< uint32_t, 3 > &groups, const std::vector< GpuBufferBinding > &bindings, const uint8_t *push_constant_data, size_t push_constant_size)
void bind_descriptor(size_t index, const GpuBufferBinding &spec)
GpuResourceManager(const GpuResourceManager &)=delete
std::unique_ptr< GpuResourceManagerImpl > m_impl
Encapsulates all Vulkan resource lifecycle behind Portal facades.
constexpr ShaderID INVALID_SHADER
constexpr ComputePipelineID INVALID_COMPUTE_PIPELINE
enum MayaFlux::Yantra::GpuBufferBinding::ElementType FLOAT32
enum MayaFlux::Yantra::GpuBufferBinding::Direction INPUT
ElementType
Element type the shader expects in this buffer.
Declares a single storage buffer the shader expects.
std::array< uint32_t, 3 > workgroup_size
Plain-data description of the compute shader to dispatch.