Nameless Engine
Loading...
Searching...
No Matches
PipelineManager.h
1#pragma once
2
3// Standard.
4#include <array>
5#include <mutex>
6#include <unordered_map>
7#include <memory>
8#include <unordered_set>
9
10// Custom.
11#include "render/general/pipeline/Pipeline.h"
12#include "shader/general/ShaderMacro.h"
13#include "render/general/pipeline/PipelineSharedPtr.h"
14#include "shader/ComputeShaderInterface.h"
15#include "render/general/pipeline/PipelineConfiguration.h"
16#include "render/general/pipeline/PipelineRegistry.hpp"
17#include "render/general/resource/frame/FrameResourceManager.h"
18
19// External.
20#include "vulkan/vulkan_core.h"
21
22namespace ne {
23 class Renderer;
24 class Material;
25 class MeshNode;
26 class ComputeShaderInterface;
27
45 std::array<
46 std::array<
47 std::unordered_map<Pipeline*, std::unordered_set<ComputeShaderInterface*>>,
48 static_cast<size_t>(ComputeExecutionGroup::SIZE)>,
49 static_cast<size_t>(ComputeExecutionStage::SIZE)>
51 };
52
55 // Pipeline notifies the manager when a material stops referencing it.
56 friend class Pipeline;
57
58 // Releases/restores internal Pipeline's resources.
59 friend class DelayedPipelineResourcesCreation;
60
61 // Compute interfaces request compute pipelines and queue for execution.
62 friend class ComputeShaderInterface;
63
64 public:
71
73 virtual ~PipelineManager();
74
75 PipelineManager() = delete;
76 PipelineManager(const PipelineManager&) = delete;
77 PipelineManager& operator=(const PipelineManager&) = delete;
78
89 [[nodiscard]] std::optional<Error> bindBuffersToAllVulkanPipelinesIfUsed(
90 const std::array<GpuResource*, FrameResourceManager::getFrameResourceCount()>& vResources,
91 const std::string& sShaderResourceName,
92 VkDescriptorType descriptorType);
93
106 [[nodiscard]] std::optional<Error> bindImageToAllVulkanPipelinesIfUsed(
107 GpuResource* pImageResourceToBind,
108 const std::string& sShaderResourceName,
109 VkDescriptorType descriptorType,
110 VkImageLayout imageLayout,
111 VkSampler pSampler);
112
129 [[nodiscard]] std::optional<Error> recreateGraphicsPipelinesResources();
130
145 std::variant<PipelineSharedPtr, Error> getGraphicsPipelineForMaterial(
146 std::unique_ptr<PipelineConfiguration> pPipelineConfiguration, Material* pMaterial);
147
155 std::pair<std::recursive_mutex*, QueuedForExecutionComputeShaders*>
158 }
159
169 std::pair<std::recursive_mutex, GraphicsPipelineRegistry>* getGraphicsPipelines() {
170 return &mtxGraphicsPipelines;
171 }
172
179
186
192 Renderer* getRenderer() const;
193
194 private:
198 struct Resources {
200 std::unordered_map<std::string, std::shared_ptr<Pipeline>> pipelines;
201
204 };
205
206 ComputePipelines() = default;
207
208 ComputePipelines(const ComputePipelines&) = delete;
209 ComputePipelines& operator=(const ComputePipelines&) = delete;
210
224 std::variant<PipelineSharedPtr, Error> getComputePipelineForShader(
225 PipelineManager* pPipelineManager, ComputeShaderInterface* pComputeShaderInterface);
226
240 [[nodiscard]] std::optional<Error> onPipelineNoLongerUsedByComputeShaderInterface(
241 const std::string& sComputeShaderName, ComputeShaderInterface* pComputeShaderInterface);
242
255 [[nodiscard]] std::optional<Error>
257
264
272 std::pair<std::recursive_mutex*, QueuedForExecutionComputeShaders*>
274 return std::make_pair(&mtxResources.first, &mtxResources.second.queuedComputeShaders);
275 }
276
277 private:
288 [[nodiscard]] static std::optional<Error> queueComputeShaderInterfaceForExecution(
289 std::unordered_map<Pipeline*, std::unordered_set<ComputeShaderInterface*>>& pipelineShaders,
290 ComputeShaderInterface* pComputeShaderInterface);
291
293 std::pair<std::recursive_mutex, Resources> mtxResources;
294 };
295
309 std::variant<PipelineSharedPtr, Error> createGraphicsPipelineForMaterial(
310 std::unordered_map<std::string, ShaderPipelines>& pipelines,
311 const std::string& sShaderNames,
312 const std::set<ShaderMacro>& macrosToUse,
313 std::unique_ptr<PipelineConfiguration> pPipelineConfiguration,
314 Material* pMaterial);
315
322 void onPipelineNoLongerUsedByMaterial(const std::string& sPipelineIdentifier);
323
331 const std::string& sComputeShaderName, ComputeShaderInterface* pComputeShaderInterface);
332
344 std::variant<PipelineSharedPtr, Error> findOrCreatePipeline(
345 std::unordered_map<std::string, ShaderPipelines>& pipelines,
346 const std::string& sKeyToLookFor,
347 const std::set<ShaderMacro>& macrosToLookFor,
348 std::unique_ptr<PipelineConfiguration> pPipelineConfiguration,
349 Material* pMaterial);
350
352 std::pair<std::recursive_mutex, GraphicsPipelineRegistry> mtxGraphicsPipelines;
353
356
358 Renderer* const pRenderer = nullptr;
359 };
360} // namespace ne
Definition: ComputeShaderInterface.h:46
static constexpr unsigned int getFrameResourceCount()
Definition: FrameResourceManager.h:74
Definition: GpuResource.h:16
Definition: Material.h:63
Definition: PipelineManager.h:54
void onPipelineNoLongerUsedByComputeShaderInterface(const std::string &sComputeShaderName, ComputeShaderInterface *pComputeShaderInterface)
Definition: PipelineManager.cpp:437
void onPipelineNoLongerUsedByMaterial(const std::string &sPipelineIdentifier)
Definition: PipelineManager.cpp:407
Renderer * getRenderer() const
Definition: PipelineManager.cpp:316
std::optional< Error > bindImageToAllVulkanPipelinesIfUsed(GpuResource *pImageResourceToBind, const std::string &sShaderResourceName, VkDescriptorType descriptorType, VkImageLayout imageLayout, VkSampler pSampler)
Definition: PipelineManager.cpp:51
size_t getCurrentGraphicsPipelineCount()
Definition: PipelineManager.cpp:297
std::variant< PipelineSharedPtr, Error > getGraphicsPipelineForMaterial(std::unique_ptr< PipelineConfiguration > pPipelineConfiguration, Material *pMaterial)
Definition: PipelineManager.cpp:113
Renderer *const pRenderer
Definition: PipelineManager.h:358
ComputePipelines computePipelines
Definition: PipelineManager.h:355
std::optional< Error > bindBuffersToAllVulkanPipelinesIfUsed(const std::array< GpuResource *, FrameResourceManager::getFrameResourceCount()> &vResources, const std::string &sShaderResourceName, VkDescriptorType descriptorType)
Definition: PipelineManager.cpp:17
std::variant< PipelineSharedPtr, Error > findOrCreatePipeline(std::unordered_map< std::string, ShaderPipelines > &pipelines, const std::string &sKeyToLookFor, const std::set< ShaderMacro > &macrosToLookFor, std::unique_ptr< PipelineConfiguration > pPipelineConfiguration, Material *pMaterial)
Definition: PipelineManager.cpp:87
std::variant< PipelineSharedPtr, Error > createGraphicsPipelineForMaterial(std::unordered_map< std::string, ShaderPipelines > &pipelines, const std::string &sShaderNames, const std::set< ShaderMacro > &macrosToUse, std::unique_ptr< PipelineConfiguration > pPipelineConfiguration, Material *pMaterial)
Definition: PipelineManager.cpp:227
virtual ~PipelineManager()
Definition: PipelineManager.cpp:175
std::pair< std::recursive_mutex, GraphicsPipelineRegistry > * getGraphicsPipelines()
Definition: PipelineManager.h:169
std::optional< Error > recreateGraphicsPipelinesResources()
Definition: PipelineManager.cpp:318
size_t getCurrentComputePipelineCount()
Definition: PipelineManager.cpp:312
std::pair< std::recursive_mutex, GraphicsPipelineRegistry > mtxGraphicsPipelines
Definition: PipelineManager.h:352
std::pair< std::recursive_mutex *, QueuedForExecutionComputeShaders * > getComputeShadersForGraphicsQueueExecution()
Definition: PipelineManager.h:156
Definition: Pipeline.h:20
Definition: Renderer.h:43
Definition: PipelineManager.h:198
QueuedForExecutionComputeShaders queuedComputeShaders
Definition: PipelineManager.h:203
std::unordered_map< std::string, std::shared_ptr< Pipeline > > pipelines
Definition: PipelineManager.h:200
Definition: PipelineManager.h:196
size_t getComputePipelineCount()
Definition: PipelineManager.cpp:449
std::variant< PipelineSharedPtr, Error > getComputePipelineForShader(PipelineManager *pPipelineManager, ComputeShaderInterface *pComputeShaderInterface)
Definition: PipelineManager.cpp:454
std::optional< Error > onPipelineNoLongerUsedByComputeShaderInterface(const std::string &sComputeShaderName, ComputeShaderInterface *pComputeShaderInterface)
Definition: PipelineManager.cpp:485
static std::optional< Error > queueComputeShaderInterfaceForExecution(std::unordered_map< Pipeline *, std::unordered_set< ComputeShaderInterface * > > &pipelineShaders, ComputeShaderInterface *pComputeShaderInterface)
Definition: PipelineManager.cpp:547
std::optional< Error > queueShaderExecutionOnGraphicsQueue(ComputeShaderInterface *pComputeShaderInterface)
Definition: PipelineManager.cpp:523
std::pair< std::recursive_mutex, Resources > mtxResources
Definition: PipelineManager.h:293
std::pair< std::recursive_mutex *, QueuedForExecutionComputeShaders * > getComputeShadersForGraphicsQueueExecution()
Definition: PipelineManager.h:273
Definition: PipelineManager.h:36
std::array< std::array< std::unordered_map< Pipeline *, std::unordered_set< ComputeShaderInterface * > >, static_cast< size_t >(ComputeExecutionGroup::SIZE)>, static_cast< size_t >(ComputeExecutionStage::SIZE)> vGraphicsQueueStagesGroups
Definition: PipelineManager.h:50