MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
BackendPipelineManager.cpp
Go to the documentation of this file.
2
4#include "VKContext.hpp"
7#include "VKShaderModule.hpp"
8
11
12namespace MayaFlux::Core {
13
15 : m_context(context)
16{
17}
18
19void BackendPipelineManager::setup_backend_service(const std::shared_ptr<Registry::Service::ComputeService>& compute_service)
20{
21 compute_service->create_shader_module = [this](const std::string& path, uint32_t stage) -> std::shared_ptr<void> {
22 return std::static_pointer_cast<void>(this->create_shader_module(path, stage));
23 };
24 compute_service->create_descriptor_manager = [this](uint32_t pool_size) -> std::shared_ptr<void> {
25 return std::static_pointer_cast<void>(this->create_descriptor_manager(pool_size));
26 };
27 compute_service->create_descriptor_layout = [this](const std::shared_ptr<void>& mgr,
28 const std::vector<std::pair<uint32_t, uint32_t>>& bindings) -> void* {
29 auto manager = std::static_pointer_cast<VKDescriptorManager>(mgr);
30 vk::DescriptorSetLayout layout = this->create_descriptor_layout(manager, bindings);
31 return reinterpret_cast<void*>(static_cast<VkDescriptorSetLayout>(layout));
32 };
33 compute_service->create_compute_pipeline = [this](const std::shared_ptr<void>& shdr,
34 const std::vector<void*>& layouts,
35 uint32_t push_size) -> std::shared_ptr<void> {
36 auto shader = std::static_pointer_cast<VKShaderModule>(shdr);
37 std::vector<vk::DescriptorSetLayout> vk_layouts;
38
39 vk_layouts.reserve(layouts.size());
40 for (auto* ptr : layouts) {
41 vk_layouts.emplace_back(reinterpret_cast<VkDescriptorSetLayout>(ptr));
42 }
43
44 return std::static_pointer_cast<void>(this->create_compute_pipeline(shader, vk_layouts, push_size));
45 };
46 compute_service->cleanup_resource = [this](const std::shared_ptr<void>& res) {
47 this->cleanup_compute_resource(res.get());
48 };
49}
50
51std::shared_ptr<VKShaderModule> BackendPipelineManager::create_shader_module(const std::string& spirv_path, uint32_t stage)
52{
53 auto shader = std::make_shared<VKShaderModule>();
54 if (!shader->create_from_spirv_file(m_context.get_device(), spirv_path, vk::ShaderStageFlagBits(stage))) {
56 "Failed to create shader module from '{}'", spirv_path);
57 return nullptr;
58 }
59
60 track_shader(shader);
61 return shader;
62}
63
64std::shared_ptr<VKDescriptorManager> BackendPipelineManager::create_descriptor_manager(uint32_t pool_size)
65{
66 auto manager = std::make_shared<VKDescriptorManager>();
67 manager->initialize(m_context.get_device(), pool_size);
68
70 return manager;
71}
72
74 const std::shared_ptr<VKDescriptorManager>& manager,
75 const std::vector<std::pair<uint32_t, uint32_t>>& bindings)
76{
77 DescriptorSetLayoutConfig vk_bindings {};
78 for (const auto& [binding, type] : bindings) {
79 vk_bindings.add_binding(binding, vk::DescriptorType(type), vk::ShaderStageFlagBits::eCompute);
80 }
81
82 return manager->create_layout(m_context.get_device(), vk_bindings);
83}
84
85std::shared_ptr<VKComputePipeline> BackendPipelineManager::create_compute_pipeline(
86 const std::shared_ptr<VKShaderModule>& shader,
87 const std::vector<vk::DescriptorSetLayout>& layouts,
88 uint32_t push_constant_size)
89{
90 auto pipeline = std::make_shared<VKComputePipeline>();
92 config.shader = shader;
93 config.set_layouts = layouts;
94 if (push_constant_size > 0) {
95 config.add_push_constant(vk::ShaderStageFlagBits::eCompute, push_constant_size);
96 }
97
98 if (!pipeline->create(m_context.get_device(), config)) {
100 "Failed to create compute pipeline");
101 return nullptr;
102 }
103
104 track_compute_pipeline(pipeline);
105 return pipeline;
106}
107
108std::shared_ptr<VKGraphicsPipeline> BackendPipelineManager::create_graphics_pipeline(
109 const GraphicsPipelineConfig& config)
110{
111 auto pipeline = std::make_shared<VKGraphicsPipeline>();
112
113 if (!pipeline->create(m_context.get_device(), config)) {
115 "Failed to create graphics pipeline");
116 return nullptr;
117 }
118
119 track_graphics_pipeline(pipeline);
120
122 "Created graphics pipeline");
123
124 return pipeline;
125}
126
128{
129 // Resource will be cleaned up via shared_ptr destruction
130 // If you need explicit cleanup, add it here
132 "Cleanup compute resource requested (handled by shared_ptr)");
133}
134
145
146void BackendPipelineManager::track_shader(const std::shared_ptr<VKShaderModule>& shader)
147{
148 m_managed_shaders.push_back(shader);
149}
150
151void BackendPipelineManager::track_descriptor_manager(const std::shared_ptr<VKDescriptorManager>& manager)
152{
153 m_managed_descriptor_managers.push_back(manager);
154}
155
156void BackendPipelineManager::track_compute_pipeline(const std::shared_ptr<VKComputePipeline>& pipeline)
157{
158 m_managed_compute_pipelines.push_back(pipeline);
159}
160
161void BackendPipelineManager::track_graphics_pipeline(const std::shared_ptr<VKGraphicsPipeline>& pipeline)
162{
163 m_managed_graphics_pipelines.push_back(pipeline);
164}
165}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
vk::PhysicalDeviceType type
Definition VKDevice.cpp:126
const uint8_t * ptr
void setup_backend_service(const std::shared_ptr< Registry::Service::ComputeService > &compute_service)
std::vector< std::shared_ptr< VKComputePipeline > > m_managed_compute_pipelines
vk::DescriptorSetLayout create_descriptor_layout(const std::shared_ptr< VKDescriptorManager > &manager, const std::vector< std::pair< uint32_t, uint32_t > > &bindings)
Create a descriptor set layout.
void track_shader(const std::shared_ptr< VKShaderModule > &shader)
void cleanup_compute_resource(void *resource)
Cleanup a compute resource allocated by the backend.
std::vector< std::shared_ptr< VKDescriptorManager > > m_managed_descriptor_managers
std::shared_ptr< VKDescriptorManager > create_descriptor_manager(uint32_t pool_size)
Create a descriptor manager for managing descriptor sets.
std::vector< std::shared_ptr< VKGraphicsPipeline > > m_managed_graphics_pipelines
void track_descriptor_manager(const std::shared_ptr< VKDescriptorManager > &manager)
std::vector< std::shared_ptr< VKShaderModule > > m_managed_shaders
void track_graphics_pipeline(const std::shared_ptr< VKGraphicsPipeline > &pipeline)
std::shared_ptr< VKShaderModule > create_shader_module(const std::string &spirv_path, uint32_t stage)
Create a shader module from SPIR-V binary.
std::shared_ptr< VKComputePipeline > create_compute_pipeline(const std::shared_ptr< VKShaderModule > &shader, const std::vector< vk::DescriptorSetLayout > &layouts, uint32_t push_constant_size)
Create a compute pipeline.
void track_compute_pipeline(const std::shared_ptr< VKComputePipeline > &pipeline)
std::shared_ptr< VKGraphicsPipeline > create_graphics_pipeline(const GraphicsPipelineConfig &config)
Create graphics pipeline.
vk::Device get_device() const
Get logical device.
Definition VKContext.hpp:49
High-level wrapper for Vulkan instance and device.
Definition VKContext.hpp:16
@ GraphicsBackend
Graphics/visual rendering backend (Vulkan, OpenGL)
@ Core
Core engine, backend, subsystems.
std::vector< vk::DescriptorSetLayout > set_layouts
Descriptor layouts.
std::shared_ptr< VKShaderModule > shader
Compute shader.
void add_push_constant(vk::ShaderStageFlags stages, uint32_t size, uint32_t offset=0)
Configuration for creating a compute pipeline.
void add_binding(uint32_t binding, vk::DescriptorType type, vk::ShaderStageFlags stages, uint32_t count=1)
Configuration for creating a descriptor set layout.
Configuration for creating a graphics pipeline.