core: Define VK_USE_PLATFORM_XCB_KHR before including vkd3d_utils.h.
[vkmodelviewer.git] / Core / RootSignature.h
blob62a6ad6c68f4237085d2b08d8402471382e19acb
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // This code is licensed under the MIT License (MIT).
4 // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5 // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6 // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7 // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
8 //
9 // Developed by Minigraph
11 // Author: James Stanard
14 #pragma once
16 #include "pch.h"
18 class DescriptorCache;
20 class RootParameter
22 friend class RootSignature;
23 public:
25 RootParameter() : m_RootParam({})
27 m_RootParam.ParameterType = (D3D12_ROOT_PARAMETER_TYPE)0xFFFFFFFF;
30 ~RootParameter()
32 Clear();
35 void Clear()
37 if (m_RootParam.ParameterType == D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE)
38 delete [] m_RootParam.DescriptorTable.pDescriptorRanges;
40 m_RootParam.ParameterType = (D3D12_ROOT_PARAMETER_TYPE)0xFFFFFFFF;
43 void InitAsConstants( UINT Register, UINT NumDwords, D3D12_SHADER_VISIBILITY Visibility = D3D12_SHADER_VISIBILITY_ALL )
45 m_RootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
46 m_RootParam.ShaderVisibility = Visibility;
47 m_RootParam.Constants.Num32BitValues = NumDwords;
48 m_RootParam.Constants.ShaderRegister = Register;
49 m_RootParam.Constants.RegisterSpace = 0;
52 void InitAsConstantBuffer( UINT Register, D3D12_SHADER_VISIBILITY Visibility = D3D12_SHADER_VISIBILITY_ALL )
54 m_RootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
55 m_RootParam.ShaderVisibility = Visibility;
56 m_RootParam.Descriptor.ShaderRegister = Register;
57 m_RootParam.Descriptor.RegisterSpace = 0;
60 void InitAsBufferSRV( UINT Register, D3D12_SHADER_VISIBILITY Visibility = D3D12_SHADER_VISIBILITY_ALL )
62 m_RootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;
63 m_RootParam.ShaderVisibility = Visibility;
64 m_RootParam.Descriptor.ShaderRegister = Register;
65 m_RootParam.Descriptor.RegisterSpace = 0;
68 void InitAsBufferUAV( UINT Register, D3D12_SHADER_VISIBILITY Visibility = D3D12_SHADER_VISIBILITY_ALL )
70 m_RootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV;
71 m_RootParam.ShaderVisibility = Visibility;
72 m_RootParam.Descriptor.ShaderRegister = Register;
73 m_RootParam.Descriptor.RegisterSpace = 0;
76 void InitAsDescriptorRange( D3D12_DESCRIPTOR_RANGE_TYPE Type, UINT Register, UINT Count, D3D12_SHADER_VISIBILITY Visibility = D3D12_SHADER_VISIBILITY_ALL )
78 InitAsDescriptorTable(1, Visibility);
79 SetTableRange(0, Type, Register, Count);
82 void InitAsDescriptorTable( UINT RangeCount, D3D12_SHADER_VISIBILITY Visibility = D3D12_SHADER_VISIBILITY_ALL )
84 m_RootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
85 m_RootParam.ShaderVisibility = Visibility;
86 m_RootParam.DescriptorTable.NumDescriptorRanges = RangeCount;
87 m_RootParam.DescriptorTable.pDescriptorRanges = new D3D12_DESCRIPTOR_RANGE[RangeCount];
90 void SetTableRange( UINT RangeIndex, D3D12_DESCRIPTOR_RANGE_TYPE Type, UINT Register, UINT Count, UINT Space = 0 )
92 D3D12_DESCRIPTOR_RANGE* range = const_cast<D3D12_DESCRIPTOR_RANGE*>(m_RootParam.DescriptorTable.pDescriptorRanges + RangeIndex);
93 range->RangeType = Type;
94 range->NumDescriptors = Count;
95 range->BaseShaderRegister = Register;
96 range->RegisterSpace = Space;
97 range->OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
100 const D3D12_ROOT_PARAMETER& operator() ( void ) const { return m_RootParam; }
103 protected:
105 D3D12_ROOT_PARAMETER m_RootParam;
108 // Maximum 64 DWORDS divied up amongst all root parameters.
109 // Root constants = 1 DWORD * NumConstants
110 // Root descriptor (CBV, SRV, or UAV) = 2 DWORDs each
111 // Descriptor table pointer = 1 DWORD
112 // Static samplers = 0 DWORDS (compiled into shader)
113 class RootSignature
115 friend class DynamicDescriptorHeap;
117 public:
119 RootSignature( UINT NumRootParams = 0, UINT NumStaticSamplers = 0 ) : m_Finalized(FALSE), m_NumParameters(NumRootParams)
121 Reset(NumRootParams, NumStaticSamplers);
124 ~RootSignature()
128 static void DestroyAll(void);
130 void Reset( UINT NumRootParams, UINT NumStaticSamplers = 0 )
132 if (NumRootParams > 0)
133 m_ParamArray.reset(new RootParameter[NumRootParams]);
134 else
135 m_ParamArray = nullptr;
136 m_NumParameters = NumRootParams;
138 if (NumStaticSamplers > 0)
139 m_SamplerArray.reset(new D3D12_STATIC_SAMPLER_DESC[NumStaticSamplers]);
140 else
141 m_SamplerArray = nullptr;
142 m_NumSamplers = NumStaticSamplers;
143 m_NumInitializedStaticSamplers = 0;
146 RootParameter& operator[] ( size_t EntryIndex )
148 ASSERT(EntryIndex < m_NumParameters);
149 return m_ParamArray.get()[EntryIndex];
152 const RootParameter& operator[] ( size_t EntryIndex ) const
154 ASSERT(EntryIndex < m_NumParameters);
155 return m_ParamArray.get()[EntryIndex];
158 void InitStaticSampler( UINT Register, const D3D12_SAMPLER_DESC& NonStaticSamplerDesc,
159 D3D12_SHADER_VISIBILITY Visibility = D3D12_SHADER_VISIBILITY_ALL );
161 void Finalize(const std::wstring& name, D3D12_ROOT_SIGNATURE_FLAGS Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE);
163 ID3D12RootSignature* GetSignature() const { return m_Signature; }
165 protected:
167 BOOL m_Finalized;
168 UINT m_NumParameters;
169 UINT m_NumSamplers;
170 UINT m_NumInitializedStaticSamplers;
171 uint32_t m_DescriptorTableBitMap; // One bit is set for root parameters that are non-sampler descriptor tables
172 uint32_t m_SamplerTableBitMap; // One bit is set for root parameters that are sampler descriptor tables
173 uint32_t m_DescriptorTableSize[16]; // Non-sampler descriptor tables need to know their descriptor count
174 std::unique_ptr<RootParameter[]> m_ParamArray;
175 std::unique_ptr<D3D12_STATIC_SAMPLER_DESC[]> m_SamplerArray;
176 ID3D12RootSignature* m_Signature;