Nameless Engine
Loading...
Searching...
No Matches
RootSignatureGenerator.h
1#pragma once
2
3// Standard.
4#include <variant>
5#include <optional>
6#include <unordered_map>
7#include <stdexcept>
8#include <array>
9#include <set>
10
11// Custom.
12#include "misc/Error.h"
13#include "SpecialRootParameterSlot.hpp"
14
15// External.
16#include "directx/d3dx12.h"
17#include <DirectXShaderCompiler/inc/d3d12shader.h>
18
19// OS.
20#include <wrl/client.h>
21
22namespace ne {
23 class HlslShader;
24 class DirectXRenderer;
25
26 using namespace Microsoft::WRL;
27
29 enum class SamplerType {
30 BASIC, //< Usual `SamplerState` type in HLSL.
31 COMPARISON //< `SamplerComparisonState` type in HLSL.
32 };
33
38 public:
39 RootSignatureGenerator() = delete;
41 RootSignatureGenerator& operator=(const RootSignatureGenerator&) = delete;
42
45 public:
47 enum class Type { CONSTANTS, CBV, SRV, UAV };
48
50 RootParameter() = default;
51
63 RootParameter(UINT iBindPoint, UINT iSpace, Type type, bool bIsTable = false, UINT iCount = 1);
64
72 CD3DX12_ROOT_PARAMETER generateSingleDescriptorDescription() const;
73
81 CD3DX12_DESCRIPTOR_RANGE generateTableRange() const;
82
88 D3D12_SHADER_VISIBILITY getVisibility() const;
89
96 bool isTable() const;
97
98 private:
100 UINT iBindPoint = 0;
101
103 UINT iSpace = 0;
104
107
113 UINT iCount = 0;
114
116 bool bIsTable = false;
117
119 D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL;
120 };
121
125 std::set<SamplerType> staticSamplers;
126
128 std::vector<RootParameter> vRootParameters;
129
135 std::unordered_map<std::string, std::pair<UINT, RootParameter>> rootParameterIndices;
136
145 std::unordered_map<std::string, size_t> rootConstantOffsets;
146 };
147
149 struct Generated {
151 ComPtr<ID3D12RootSignature> pRootSignature;
152
160 std::unordered_map<std::string, UINT> rootParameterIndices;
161
172 std::array<UINT, static_cast<unsigned int>(SpecialRootParameterSlot::SIZE)>
174
183 std::unordered_map<std::string, size_t> rootConstantOffsets;
184 };
185
194 static std::variant<CollectedInfo, Error> collectInfoFromReflection(
195 ID3D12Device* pDevice, const ComPtr<ID3D12ShaderReflection>& pShaderReflection);
196
215 static std::variant<Generated, Error>
216 generateGraphics(DirectXRenderer* pRenderer, HlslShader* pVertexShader, HlslShader* pPixelShader);
217
229 static std::variant<Generated, Error>
230 generateCompute(DirectXRenderer* pRenderer, HlslShader* pComputeShader);
231
232 private:
240 static std::variant<SamplerType, Error>
241 findStaticSamplerForSamplerResource(const D3D12_SHADER_INPUT_BIND_DESC& samplerResourceDescription);
242
257 std::unordered_map<std::string, std::pair<UINT, RootSignatureGenerator::RootParameter>>&
258 shaderRootParameterIndices,
259 std::vector<CD3DX12_ROOT_PARAMETER>& vRootParameters,
260 std::vector<CD3DX12_DESCRIPTOR_RANGE>& vTableRanges,
261 std::set<std::string>& addedRootParameterNames,
262 std::unordered_map<std::string, UINT>& rootParameterIndices,
263 std::array<UINT, static_cast<unsigned int>(SpecialRootParameterSlot::SIZE)>&
264 vSpecialRootParameterIndices);
265
278 static std::optional<Error> addUniquePairResourceNameRootParameterIndex(
279 std::unordered_map<std::string, std::pair<UINT, RootParameter>>& mapToAddTo,
280 const std::string& sResourceName,
281 UINT iRootParameterIndex,
282 const RootParameter& parameter);
283
293 static std::optional<Error> addCbufferRootParameter(
294 std::vector<RootParameter>& vRootParameters,
295 std::unordered_map<std::string, std::pair<UINT, RootParameter>>& rootParameterIndices,
296 const D3D12_SHADER_INPUT_BIND_DESC& resourceDescription);
297
308 static std::optional<Error> addTexture2DRootParameter(
309 std::vector<RootParameter>& vRootParameters,
310 std::unordered_map<std::string, std::pair<UINT, RootParameter>>& rootParameterIndices,
311 const D3D12_SHADER_INPUT_BIND_DESC& resourceDescription,
312 bool bIsReadWrite);
313
324 static std::optional<Error> addStructuredBufferRootParameter(
325 std::vector<RootParameter>& vRootParameters,
326 std::unordered_map<std::string, std::pair<UINT, RootParameter>>& rootParameterIndices,
327 const D3D12_SHADER_INPUT_BIND_DESC& resourceDescription,
328 bool bIsReadWrite);
329
343 static std::variant<bool, Error> processRootConstantsIfFound(
344 const ComPtr<ID3D12ShaderReflection>& pShaderReflection,
345 const D3D12_SHADER_INPUT_BIND_DESC& resourceDescription,
346 std::unordered_map<std::string, size_t>& rootConstantOffsets,
347 std::vector<RootParameter>& vRootParameters,
348 std::unordered_map<std::string, std::pair<UINT, RootParameter>>& rootParameterIndices);
349
351 static inline const std::string sRootConstantsVariableName = "constants";
352
354 static inline const std::string sRootConstantsTypeName = "RootConstants";
355 };
356} // namespace ne
Definition: DirectXRenderer.h:36
Definition: HlslShader.h:31
Definition: RootSignatureGenerator.h:44
bool bIsTable
Definition: RootSignatureGenerator.h:116
bool isTable() const
Definition: RootSignatureGenerator.cpp:1107
CD3DX12_ROOT_PARAMETER generateSingleDescriptorDescription() const
Definition: RootSignatureGenerator.cpp:1017
Type
Definition: RootSignatureGenerator.h:47
UINT iBindPoint
Definition: RootSignatureGenerator.h:100
UINT iSpace
Definition: RootSignatureGenerator.h:103
Type type
Definition: RootSignatureGenerator.h:106
UINT iCount
Definition: RootSignatureGenerator.h:113
CD3DX12_DESCRIPTOR_RANGE generateTableRange() const
Definition: RootSignatureGenerator.cpp:1057
D3D12_SHADER_VISIBILITY getVisibility() const
Definition: RootSignatureGenerator.cpp:1103
D3D12_SHADER_VISIBILITY visibility
Definition: RootSignatureGenerator.h:119
Definition: RootSignatureGenerator.h:37
static std::optional< Error > addCbufferRootParameter(std::vector< RootParameter > &vRootParameters, std::unordered_map< std::string, std::pair< UINT, RootParameter > > &rootParameterIndices, const D3D12_SHADER_INPUT_BIND_DESC &resourceDescription)
Definition: RootSignatureGenerator.cpp:798
static const std::string sRootConstantsTypeName
Definition: RootSignatureGenerator.h:354
static std::variant< Generated, Error > generateCompute(DirectXRenderer *pRenderer, HlslShader *pComputeShader)
Definition: RootSignatureGenerator.cpp:506
static std::variant< CollectedInfo, Error > collectInfoFromReflection(ID3D12Device *pDevice, const ComPtr< ID3D12ShaderReflection > &pShaderReflection)
Definition: RootSignatureGenerator.cpp:16
static std::variant< Generated, Error > generateGraphics(DirectXRenderer *pRenderer, HlslShader *pVertexShader, HlslShader *pPixelShader)
Definition: RootSignatureGenerator.cpp:193
static std::variant< bool, Error > processRootConstantsIfFound(const ComPtr< ID3D12ShaderReflection > &pShaderReflection, const D3D12_SHADER_INPUT_BIND_DESC &resourceDescription, std::unordered_map< std::string, size_t > &rootConstantOffsets, std::vector< RootParameter > &vRootParameters, std::unordered_map< std::string, std::pair< UINT, RootParameter > > &rootParameterIndices)
Definition: RootSignatureGenerator.cpp:882
static void addSpecialResourceRootParametersIfUsed(std::unordered_map< std::string, std::pair< UINT, RootSignatureGenerator::RootParameter > > &shaderRootParameterIndices, std::vector< CD3DX12_ROOT_PARAMETER > &vRootParameters, std::vector< CD3DX12_DESCRIPTOR_RANGE > &vTableRanges, std::set< std::string > &addedRootParameterNames, std::unordered_map< std::string, UINT > &rootParameterIndices, std::array< UINT, static_cast< unsigned int >(SpecialRootParameterSlot::SIZE)> &vSpecialRootParameterIndices)
Definition: RootSignatureGenerator.cpp:669
static std::optional< Error > addTexture2DRootParameter(std::vector< RootParameter > &vRootParameters, std::unordered_map< std::string, std::pair< UINT, RootParameter > > &rootParameterIndices, const D3D12_SHADER_INPUT_BIND_DESC &resourceDescription, bool bIsReadWrite)
Definition: RootSignatureGenerator.cpp:824
static std::optional< Error > addStructuredBufferRootParameter(std::vector< RootParameter > &vRootParameters, std::unordered_map< std::string, std::pair< UINT, RootParameter > > &rootParameterIndices, const D3D12_SHADER_INPUT_BIND_DESC &resourceDescription, bool bIsReadWrite)
Definition: RootSignatureGenerator.cpp:854
static const std::string sRootConstantsVariableName
Definition: RootSignatureGenerator.h:351
static std::variant< SamplerType, Error > findStaticSamplerForSamplerResource(const D3D12_SHADER_INPUT_BIND_DESC &samplerResourceDescription)
Definition: RootSignatureGenerator.cpp:615
static std::optional< Error > addUniquePairResourceNameRootParameterIndex(std::unordered_map< std::string, std::pair< UINT, RootParameter > > &mapToAddTo, const std::string &sResourceName, UINT iRootParameterIndex, const RootParameter &parameter)
Definition: RootSignatureGenerator.cpp:777
Definition: RootSignatureGenerator.h:123
std::vector< RootParameter > vRootParameters
Definition: RootSignatureGenerator.h:128
std::set< SamplerType > staticSamplers
Definition: RootSignatureGenerator.h:125
std::unordered_map< std::string, std::pair< UINT, RootParameter > > rootParameterIndices
Definition: RootSignatureGenerator.h:135
std::unordered_map< std::string, size_t > rootConstantOffsets
Definition: RootSignatureGenerator.h:145
Definition: RootSignatureGenerator.h:149
std::array< UINT, static_cast< unsigned int >(SpecialRootParameterSlot::SIZE)> vSpecialRootParameterIndices
Definition: RootSignatureGenerator.h:173
ComPtr< ID3D12RootSignature > pRootSignature
Definition: RootSignatureGenerator.h:151
std::unordered_map< std::string, size_t > rootConstantOffsets
Definition: RootSignatureGenerator.h:183
std::unordered_map< std::string, UINT > rootParameterIndices
Definition: RootSignatureGenerator.h:160