MayaFlux 0.1.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 shader->create_from_spirv_file(m_context.get_device(), spirv_path, vk::ShaderStageFlagBits(stage));
55
56 track_shader(shader);
57 return shader;
58}
59
60std::shared_ptr<VKDescriptorManager> BackendPipelineManager::create_descriptor_manager(uint32_t pool_size)
61{
62 auto manager = std::make_shared<VKDescriptorManager>();
63 manager->initialize(m_context.get_device(), pool_size);
64
66 return manager;
67}
68
70 const std::shared_ptr<VKDescriptorManager>& manager,
71 const std::vector<std::pair<uint32_t, uint32_t>>& bindings)
72{
73 DescriptorSetLayoutConfig vk_bindings {};
74 for (const auto& [binding, type] : bindings) {
75 vk_bindings.add_binding(binding, vk::DescriptorType(type), vk::ShaderStageFlagBits::eCompute);
76 }
77
78 return manager->create_layout(m_context.get_device(), vk_bindings);
79}
80
81std::shared_ptr<VKComputePipeline> BackendPipelineManager::create_compute_pipeline(
82 const std::shared_ptr<VKShaderModule>& shader,
83 const std::vector<vk::DescriptorSetLayout>& layouts,
84 uint32_t push_constant_size)
85{
86 auto pipeline = std::make_shared<VKComputePipeline>();
88 config.shader = shader;
89 config.set_layouts = layouts;
90 if (push_constant_size > 0) {
91 config.add_push_constant(vk::ShaderStageFlagBits::eCompute, push_constant_size);
92 }
93
94 pipeline->create(m_context.get_device(), config);
95
96 track_compute_pipeline(pipeline);
97 return pipeline;
98}
99
100std::shared_ptr<VKGraphicsPipeline> BackendPipelineManager::create_graphics_pipeline(
101 const GraphicsPipelineConfig& config)
102{
103 auto pipeline = std::make_shared<VKGraphicsPipeline>();
104
105 if (!pipeline->create(m_context.get_device(), config)) {
107 "Failed to create graphics pipeline");
108 return nullptr;
109 }
110
111 track_graphics_pipeline(pipeline);
112
114 "Created graphics pipeline");
115
116 return pipeline;
117}
118
120{
121 // Resource will be cleaned up via shared_ptr destruction
122 // If you need explicit cleanup, add it here
124 "Cleanup compute resource requested (handled by shared_ptr)");
125}
126
137
138void BackendPipelineManager::track_shader(const std::shared_ptr<VKShaderModule>& shader)
139{
140 m_managed_shaders.push_back(shader);
141}
142
143void BackendPipelineManager::track_descriptor_manager(const std::shared_ptr<VKDescriptorManager>& manager)
144{
145 m_managed_descriptor_managers.push_back(manager);
146}
147
148void BackendPipelineManager::track_compute_pipeline(const std::shared_ptr<VKComputePipeline>& pipeline)
149{
150 m_managed_compute_pipelines.push_back(pipeline);
151}
152
153void BackendPipelineManager::track_graphics_pipeline(const std::shared_ptr<VKGraphicsPipeline>& pipeline)
154{
155 m_managed_graphics_pipelines.push_back(pipeline);
156}
157}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
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.