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
25 enum class Direction : uint8_t {
26 INPUT,
27 OUTPUT,
29 } direction { Direction::INPUT };
30
31 /**
32 * @brief Element type the shader expects in this buffer.
33 *
34 * FLOAT32 — cast double channels to float (default).
35 * UINT32 — reinterpret variant bytes as uint32_t.
36 * INT32 — reinterpret variant bytes as int32_t.
37 * PASSTHROUGH — upload raw variant bytes with no cast; caller
38 * must pre-stage via stage_passthrough() for
39 * INPUT / INPUT_OUTPUT bindings.
40 * IMAGE_STORAGE, — writeonly/readonly image2D — eStorageImage descriptor
41 * IMAGE_SAMPLED — sampler2D — eCombinedImageSampler descriptor
42 */
43 enum class ElementType : uint8_t {
44 FLOAT32,
45 UINT32,
46 INT32,
50 } element_type { ElementType::FLOAT32 };
51};
52
53struct GpuResourceManagerImpl;
54
55/**
56 * @class GpuResourceManager
57 * @brief Encapsulates all Vulkan resource lifecycle behind Portal facades.
58 *
59 * Manages storage buffers, pipeline, shader, and descriptor sets.
60 * PIMPL hides vk:: types from the header entirely.
61 */
63public:
66
71
72 bool initialise(const GpuShaderConfig& config,
73 const std::vector<GpuBufferBinding>& bindings);
74
75 void ensure_buffer(size_t index, size_t required_bytes);
76 void upload(size_t index, const float* data, size_t byte_size);
77 void upload_raw(size_t index, const uint8_t* data, size_t byte_size);
78 void download(size_t index, float* dest, size_t byte_size);
79 void bind_descriptor(size_t index, const GpuBufferBinding& spec);
80
81 /**
82 * @brief Bind a storage image descriptor at the given slot index.
83 * @param index Slot index matching the binding declaration.
84 * @param image VKImage to bind. Must be initialised and in eGeneral layout.
85 * @param spec Binding declaration. element_type must be IMAGE_STORAGE.
86 */
87 void bind_image_storage(size_t index,
88 const std::shared_ptr<Core::VKImage>& image,
89 const GpuBufferBinding& spec);
90
91 /**
92 * @brief Bind a combined image+sampler descriptor at the given slot index.
93 * @param index Slot index matching the binding declaration.
94 * @param image VKImage to bind. Must be in eShaderReadOnlyOptimal layout.
95 * @param sampler Vulkan sampler handle from SamplerForge.
96 * @param spec Binding declaration. element_type must be IMAGE_SAMPLED.
97 */
98 void bind_image_sampled(size_t index,
99 const std::shared_ptr<Core::VKImage>& image,
100 vk::Sampler sampler,
101 const GpuBufferBinding& spec);
102
103 /**
104 * @brief Transition a VKImage layout via an immediate command submission.
105 * @param image Image to transition.
106 * @param old_layout Source layout.
107 * @param new_layout Target layout.
108 */
109 void transition_image(const std::shared_ptr<Core::VKImage>& image,
110 vk::ImageLayout old_layout,
111 vk::ImageLayout new_layout);
112
113 void dispatch(const std::array<uint32_t, 3>& groups,
114 const std::vector<GpuBufferBinding>& bindings,
115 const uint8_t* push_constant_data,
116 size_t push_constant_size);
117
118 void dispatch_batched(
119 uint32_t pass_count,
120 const std::array<uint32_t, 3>& groups,
121 const std::vector<GpuBufferBinding>& bindings,
122 const std::function<void(uint32_t pass, std::vector<uint8_t>&)>& push_constant_updater,
123 size_t push_constant_size,
124 const std::unordered_map<std::string, std::any>& execution_metadata = {});
125
126 void cleanup();
127
128 [[nodiscard]] bool is_ready() const { return m_ready; }
129 [[nodiscard]] size_t buffer_allocated_bytes(size_t index) const;
130
131private:
134 std::vector<Portal::Graphics::DescriptorSetID> m_descriptor_set_ids;
135
136 std::unique_ptr<GpuResourceManagerImpl> m_impl;
137
138 struct BufferSlot {
140 };
141 std::vector<BufferSlot> m_buffer_slots;
142 std::vector<std::shared_ptr<Core::VKImage>> m_image_slots;
143
144 bool m_ready {};
145}; // class GpuResourceManager
146
147} // namespace MayaFlux::Yantra
IO::ImageData image
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
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.