Nameless Engine
Loading...
Searching...
No Matches
DirectXPso.h
1#pragma once
2
3// Custom.
4#include "render/general/pipeline/Pipeline.h"
5#include "shader/hlsl/SpecialRootParameterSlot.hpp"
6#include "render/general/resource/frame/FrameResourceManager.h"
7#include "render/directx/resource/DirectXResource.h"
8
9// External.
10#include "directx/d3dx12.h"
11
12// OS.
13#include <wrl.h>
14
15namespace ne {
16 using namespace Microsoft::WRL;
17
18 class DirectXResource;
19 class DirectXRenderer;
20
22 class DirectXPso : public Pipeline {
23 // Renderer will ask us to bind global shader resource views.
24 friend class DirectXRenderer;
25
26 public:
30 ComPtr<ID3D12RootSignature> pRootSignature;
31
33 ComPtr<ID3D12PipelineState> pPso;
34
42 std::unordered_map<std::string, UINT> rootParameterIndices;
43
54 std::array<UINT, static_cast<unsigned int>(SpecialRootParameterSlot::SIZE)>
56
67 std::unordered_map<
68 UINT,
71
78 std::unordered_map<
79 UINT,
82
88 std::unordered_map<UINT, std::shared_ptr<ContinuousDirectXDescriptorRange>>
90
92 bool bIsReadyForUsage = false;
93 };
94
95 DirectXPso() = delete;
96 DirectXPso(const DirectXPso&) = delete;
97 DirectXPso& operator=(const DirectXPso&) = delete;
98
99 virtual ~DirectXPso() override;
100
111 static std::variant<std::shared_ptr<DirectXPso>, Error> createGraphicsPso(
114 std::unique_ptr<PipelineConfiguration> pPipelineConfiguration);
115
126 static std::variant<std::shared_ptr<DirectXPso>, Error> createComputePso(
127 Renderer* pRenderer, PipelineManager* pPipelineManager, const std::string& sComputeShaderName);
128
137 std::variant<unsigned int, Error> getRootParameterIndex(const std::string& sShaderResourceName);
138
144 std::pair<std::recursive_mutex, InternalResources>* getInternalResources() {
145 return &mtxInternalResources;
146 }
147
148 protected:
165 [[nodiscard]] virtual std::optional<Error> recreateInternalResources() override;
166
167 private:
175 explicit DirectXPso(
178 std::unique_ptr<PipelineConfiguration> pPipelineConfiguration);
179
190 const ComPtr<ID3D12GraphicsCommandList>& pCommandList, size_t iCurrentFrameResourceIndex) const {
191 // No need to lock internal resources mutex since the caller is expected to lock that mutex
192 // because this function is expected to be called inside of the `draw` function.
193
194 // Bind global CBVs.
195 for (const auto& [iRootParameterIndex, vResourcesToBind] :
196 mtxInternalResources.second.globalShaderResourceCbvs) {
197 pCommandList->SetGraphicsRootConstantBufferView(
198 iRootParameterIndex,
199 vResourcesToBind[iCurrentFrameResourceIndex]
200 ->getInternalResource()
201 ->GetGPUVirtualAddress());
202 }
203
204 // Bind global SRVs.
205 for (const auto& [iRootParameterIndex, vResourcesToBind] :
206 mtxInternalResources.second.globalShaderResourceSrvs) {
207 // Set view.
208 pCommandList->SetGraphicsRootShaderResourceView(
209 iRootParameterIndex,
210 vResourcesToBind[iCurrentFrameResourceIndex]
211 ->getInternalResource()
212 ->GetGPUVirtualAddress());
213 }
214 }
215
225 [[nodiscard]] std::optional<Error> generateGraphicsPso();
226
236 [[nodiscard]] std::optional<Error> generateComputePso();
237
242 std::pair<std::recursive_mutex, InternalResources> mtxInternalResources;
243 };
244} // namespace ne
Definition: DirectXPso.h:22
static std::variant< std::shared_ptr< DirectXPso >, Error > createGraphicsPso(Renderer *pRenderer, PipelineManager *pPipelineManager, std::unique_ptr< PipelineConfiguration > pPipelineConfiguration)
Definition: DirectXPso.cpp:35
std::optional< Error > generateGraphicsPso()
Definition: DirectXPso.cpp:146
static std::variant< std::shared_ptr< DirectXPso >, Error > createComputePso(Renderer *pRenderer, PipelineManager *pPipelineManager, const std::string &sComputeShaderName)
Definition: DirectXPso.cpp:53
std::pair< std::recursive_mutex, InternalResources > mtxInternalResources
Definition: DirectXPso.h:242
std::pair< std::recursive_mutex, InternalResources > * getInternalResources()
Definition: DirectXPso.h:144
std::variant< unsigned int, Error > getRootParameterIndex(const std::string &sShaderResourceName)
Definition: DirectXPso.cpp:70
void bindGlobalShaderResourceViews(const ComPtr< ID3D12GraphicsCommandList > &pCommandList, size_t iCurrentFrameResourceIndex) const
Definition: DirectXPso.h:189
virtual std::optional< Error > recreateInternalResources() override
Definition: DirectXPso.cpp:85
std::optional< Error > generateComputePso()
Definition: DirectXPso.cpp:369
Definition: DirectXRenderer.h:36
Definition: DirectXResource.h:32
Definition: Error.h:27
static constexpr unsigned int getFrameResourceCount()
Definition: FrameResourceManager.h:74
Definition: PipelineManager.h:54
Definition: Pipeline.h:20
const std::unique_ptr< PipelineConfiguration > pPipelineConfiguration
Definition: Pipeline.h:266
Renderer *const pRenderer
Definition: Pipeline.h:272
PipelineManager *const pPipelineManager
Definition: Pipeline.h:269
Definition: Renderer.h:43
Definition: DirectXPso.h:28
bool bIsReadyForUsage
Definition: DirectXPso.h:92
std::unordered_map< UINT, std::shared_ptr< ContinuousDirectXDescriptorRange > > descriptorRangesToBind
Definition: DirectXPso.h:89
std::array< UINT, static_cast< unsigned int >(SpecialRootParameterSlot::SIZE)> vSpecialRootParameterIndices
Definition: DirectXPso.h:55
std::unordered_map< std::string, UINT > rootParameterIndices
Definition: DirectXPso.h:42
std::unordered_map< UINT, std::array< DirectXResource *, FrameResourceManager::getFrameResourceCount()> > globalShaderResourceCbvs
Definition: DirectXPso.h:70
ComPtr< ID3D12RootSignature > pRootSignature
Definition: DirectXPso.h:30
std::unordered_map< UINT, std::array< DirectXResource *, FrameResourceManager::getFrameResourceCount()> > globalShaderResourceSrvs
Definition: DirectXPso.h:81
ComPtr< ID3D12PipelineState > pPso
Definition: DirectXPso.h:33