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 <atomic>
9#include <unordered_set>
10
11// Custom.
12#include "render/general/pipeline/Pipeline.h"
13#include "shader/general/ShaderMacro.h"
14#include "render/general/pipeline/PipelineSharedPtr.h"
15#include "shader/ComputeShaderInterface.h"
16#include "render/general/pipeline/PipelineType.hpp"
17#include "render/general/pipeline/PipelineConfiguration.h"
18#include "render/general/pipeline/PipelineRegistry.hpp"
19#include "render/general/resource/frame/FrameResourceManager.h"
20
21// External.
22#include "vulkan/vulkan_core.h"
23
24namespace ne {
25 class Renderer;
26 class Material;
27 class MeshNode;
28 class ComputeShaderInterface;
29
39 public:
41
44
47 operator=(DelayedPipelineResourcesCreation&& other) noexcept = delete;
48
55 this->pPipelineManager = pPipelineManager;
56 initialize();
57 }
58
60
61 private:
63 void initialize();
64
66 void destroy();
67
70 };
71
89 std::array<
90 std::array<
91 std::unordered_map<Pipeline*, std::unordered_set<ComputeShaderInterface*>>,
92 static_cast<size_t>(ComputeExecutionGroup::SIZE)>,
93 static_cast<size_t>(ComputeExecutionStage::SIZE)>
95 };
96
99 // Pipeline notifies the manager when a material stops referencing it.
100 friend class Pipeline;
101
102 // Releases/restores internal Pipeline's resources.
104
105 // Compute interfaces request compute pipelines and queue for execution.
106 friend class ComputeShaderInterface;
107
108 public:
115
117 virtual ~PipelineManager();
118
119 PipelineManager() = delete;
120 PipelineManager(const PipelineManager&) = delete;
121 PipelineManager& operator=(const PipelineManager&) = delete;
122
133 [[nodiscard]] std::optional<Error> bindBuffersToAllVulkanPipelinesIfUsed(
134 const std::array<GpuResource*, FrameResourceManager::getFrameResourceCount()>& vResources,
135 const std::string& sShaderResourceName,
136 VkDescriptorType descriptorType);
137
150 [[nodiscard]] std::optional<Error> bindImageToAllVulkanPipelinesIfUsed(
151 GpuResource* pImageResourceToBind,
152 const std::string& sShaderResourceName,
153 VkDescriptorType descriptorType,
154 VkImageLayout imageLayout,
155 VkSampler pSampler);
156
166
181 std::variant<PipelineSharedPtr, Error> getGraphicsPipelineForMaterial(
182 std::unique_ptr<PipelineConfiguration> pPipelineConfiguration, Material* pMaterial);
183
191 inline std::pair<std::recursive_mutex*, QueuedForExecutionComputeShaders*>
194 }
195
205 inline std::pair<std::recursive_mutex, GraphicsPipelineRegistry>* getGraphicsPipelines() {
206 return &mtxGraphicsPipelines;
207 }
208
215
222
228 Renderer* getRenderer() const;
229
230 private:
234 struct Resources {
236 std::unordered_map<std::string, std::shared_ptr<Pipeline>> pipelines;
237
240 };
241
242 ComputePipelines() = default;
243
244 ComputePipelines(const ComputePipelines&) = delete;
245 ComputePipelines& operator=(const ComputePipelines&) = delete;
246
260 std::variant<PipelineSharedPtr, Error> getComputePipelineForShader(
261 PipelineManager* pPipelineManager, ComputeShaderInterface* pComputeShaderInterface);
262
276 [[nodiscard]] std::optional<Error> onPipelineNoLongerUsedByComputeShaderInterface(
277 const std::string& sComputeShaderName, ComputeShaderInterface* pComputeShaderInterface);
278
291 [[nodiscard]] std::optional<Error>
293
300
308 inline std::pair<std::recursive_mutex*, QueuedForExecutionComputeShaders*>
310 return std::make_pair(&mtxResources.first, &mtxResources.second.queuedComputeShaders);
311 }
312
313 private:
324 [[nodiscard]] static std::optional<Error> queueComputeShaderInterfaceForExecution(
325 std::unordered_map<Pipeline*, std::unordered_set<ComputeShaderInterface*>>& pipelineShaders,
326 ComputeShaderInterface* pComputeShaderInterface);
327
329 std::pair<std::recursive_mutex, Resources> mtxResources;
330 };
331
348 [[nodiscard]] std::optional<Error> releaseInternalGraphicsPipelinesResources();
349
359 [[nodiscard]] std::optional<Error> restoreInternalGraphicsPipelinesResources();
360
374 std::variant<PipelineSharedPtr, Error> createGraphicsPipelineForMaterial(
375 std::unordered_map<std::string, ShaderPipelines>& pipelines,
376 const std::string& sShaderNames,
377 const std::set<ShaderMacro>& macrosToUse,
378 std::unique_ptr<PipelineConfiguration> pPipelineConfiguration,
379 Material* pMaterial);
380
387 void onPipelineNoLongerUsedByMaterial(const std::string& sPipelineIdentifier);
388
396 const std::string& sComputeShaderName, ComputeShaderInterface* pComputeShaderInterface);
397
409 std::variant<PipelineSharedPtr, Error> findOrCreatePipeline(
410 std::unordered_map<std::string, ShaderPipelines>& pipelines,
411 const std::string& sKeyToLookFor,
412 const std::set<ShaderMacro>& macrosToLookFor,
413 std::unique_ptr<PipelineConfiguration> pPipelineConfiguration,
414 Material* pMaterial);
415
417 std::pair<std::recursive_mutex, GraphicsPipelineRegistry> mtxGraphicsPipelines;
418
421
423 Renderer* const pRenderer = nullptr;
424 };
425} // namespace ne
Definition: ComputeShaderInterface.h:46
Definition: PipelineManager.h:38
PipelineManager * pPipelineManager
Definition: PipelineManager.h:69
void initialize()
Definition: PipelineManager.cpp:473
DelayedPipelineResourcesCreation(PipelineManager *pPipelineManager)
Definition: PipelineManager.h:54
void destroy()
Definition: PipelineManager.cpp:494
static constexpr unsigned int getFrameResourceCount()
Definition: FrameResourceManager.h:74
Definition: GpuResource.h:16
Definition: Material.h:63
Definition: PipelineManager.h:98
void onPipelineNoLongerUsedByComputeShaderInterface(const std::string &sComputeShaderName, ComputeShaderInterface *pComputeShaderInterface)
Definition: PipelineManager.cpp:461
void onPipelineNoLongerUsedByMaterial(const std::string &sPipelineIdentifier)
Definition: PipelineManager.cpp:431
Renderer * getRenderer() const
Definition: PipelineManager.cpp:314
std::optional< Error > bindImageToAllVulkanPipelinesIfUsed(GpuResource *pImageResourceToBind, const std::string &sShaderResourceName, VkDescriptorType descriptorType, VkImageLayout imageLayout, VkSampler pSampler)
Definition: PipelineManager.cpp:51
std::optional< Error > restoreInternalGraphicsPipelinesResources()
Definition: PipelineManager.cpp:340
DelayedPipelineResourcesCreation clearGraphicsPipelinesInternalResourcesAndDelayRestoring()
Definition: PipelineManager.cpp:88
size_t getCurrentGraphicsPipelineCount()
Definition: PipelineManager.cpp:295
std::variant< PipelineSharedPtr, Error > getGraphicsPipelineForMaterial(std::unique_ptr< PipelineConfiguration > pPipelineConfiguration, Material *pMaterial)
Definition: PipelineManager.cpp:118
Renderer *const pRenderer
Definition: PipelineManager.h:423
ComputePipelines computePipelines
Definition: PipelineManager.h:420
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:92
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:225
virtual ~PipelineManager()
Definition: PipelineManager.cpp:173
std::optional< Error > releaseInternalGraphicsPipelinesResources()
Definition: PipelineManager.cpp:316
std::pair< std::recursive_mutex, GraphicsPipelineRegistry > * getGraphicsPipelines()
Definition: PipelineManager.h:205
size_t getCurrentComputePipelineCount()
Definition: PipelineManager.cpp:310
std::pair< std::recursive_mutex, GraphicsPipelineRegistry > mtxGraphicsPipelines
Definition: PipelineManager.h:417
std::pair< std::recursive_mutex *, QueuedForExecutionComputeShaders * > getComputeShadersForGraphicsQueueExecution()
Definition: PipelineManager.h:192
Definition: Pipeline.h:20
Definition: Renderer.h:43
Definition: PipelineManager.h:234
QueuedForExecutionComputeShaders queuedComputeShaders
Definition: PipelineManager.h:239
std::unordered_map< std::string, std::shared_ptr< Pipeline > > pipelines
Definition: PipelineManager.h:236
Definition: PipelineManager.h:232
size_t getComputePipelineCount()
Definition: PipelineManager.cpp:505
std::variant< PipelineSharedPtr, Error > getComputePipelineForShader(PipelineManager *pPipelineManager, ComputeShaderInterface *pComputeShaderInterface)
Definition: PipelineManager.cpp:510
std::optional< Error > onPipelineNoLongerUsedByComputeShaderInterface(const std::string &sComputeShaderName, ComputeShaderInterface *pComputeShaderInterface)
Definition: PipelineManager.cpp:541
static std::optional< Error > queueComputeShaderInterfaceForExecution(std::unordered_map< Pipeline *, std::unordered_set< ComputeShaderInterface * > > &pipelineShaders, ComputeShaderInterface *pComputeShaderInterface)
Definition: PipelineManager.cpp:603
std::optional< Error > queueShaderExecutionOnGraphicsQueue(ComputeShaderInterface *pComputeShaderInterface)
Definition: PipelineManager.cpp:579
std::pair< std::recursive_mutex, Resources > mtxResources
Definition: PipelineManager.h:329
std::pair< std::recursive_mutex *, QueuedForExecutionComputeShaders * > getComputeShadersForGraphicsQueueExecution()
Definition: PipelineManager.h:309
Definition: PipelineManager.h:80
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:94