1 /*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/gpu/d3d/GrD3DRootSignature.h"
9
10 #include "src/gpu/GrSPIRVUniformHandler.h"
11 #include "src/gpu/d3d/GrD3DGpu.h"
12
Make(GrD3DGpu * gpu,int numTextureSamplers,int numUAVs)13 sk_sp<GrD3DRootSignature> GrD3DRootSignature::Make(GrD3DGpu* gpu, int numTextureSamplers,
14 int numUAVs) {
15 // Just allocate enough space for 3 in case we need it.
16 D3D12_ROOT_PARAMETER parameters[3];
17
18 // The first will always be our uniforms
19 parameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
20 parameters[0].Descriptor.ShaderRegister = 0;
21 parameters[0].Descriptor.RegisterSpace = GrSPIRVUniformHandler::kUniformDescriptorSet;
22 parameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
23 int parameterCount = 1;
24
25 int numShaderViews = numTextureSamplers + numUAVs;
26 SkAutoTArray<D3D12_DESCRIPTOR_RANGE> samplerRanges(numTextureSamplers);
27 SkAutoTArray<D3D12_DESCRIPTOR_RANGE> shaderViewRanges(numShaderViews);
28 if (numTextureSamplers) {
29 // Now handle the textures and samplers. We need a range for each sampler because of the
30 // interaction between how we set bindings and spirv-cross. Each binding value is used for
31 // the register value in the HLSL shader. So setting a binding of i for a texture will give
32 // it register t[i] in HLSL. We set the bindings of textures and samplers in pairs with the
33 // sampler at i and the corresponding texture at i+1. Thus no textures or samplers will have
34 // a contiguous range of HLSL registers so we must define a different range for each.
35 for (int i = 0; i < numTextureSamplers; ++i) {
36 samplerRanges[i].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER;
37 samplerRanges[i].NumDescriptors = 1;
38 samplerRanges[i].BaseShaderRegister = 2 * i;
39 // Spirv-Cross uses the descriptor set as the space in HLSL
40 samplerRanges[i].RegisterSpace = GrSPIRVUniformHandler::kSamplerTextureDescriptorSet;
41 // In the descriptor table the descriptors will all be contiguous.
42 samplerRanges[i].OffsetInDescriptorsFromTableStart =
43 D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
44
45 shaderViewRanges[i].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
46 shaderViewRanges[i].NumDescriptors = 1;
47 shaderViewRanges[i].BaseShaderRegister = 2 * i + 1;
48 // Spirv-Cross uses the descriptor set as the space in HLSL
49 shaderViewRanges[i].RegisterSpace = GrSPIRVUniformHandler::kSamplerTextureDescriptorSet;
50 // In the descriptor table the descriptors will all be contiguous.
51 shaderViewRanges[i].OffsetInDescriptorsFromTableStart =
52 D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
53 }
54 }
55 if (numUAVs) {
56 shaderViewRanges[numTextureSamplers].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
57 shaderViewRanges[numTextureSamplers].NumDescriptors = numUAVs;
58 // The assigned register range for the texture SRVs and samplers is from 0 to
59 // 2*(numTextureSamplers-1) + 1, so we start with the next register, 2*numTextureSamplers
60 shaderViewRanges[numTextureSamplers].BaseShaderRegister = 2 * numTextureSamplers;
61 // We share texture descriptor set
62 shaderViewRanges[numTextureSamplers].RegisterSpace =
63 GrSPIRVUniformHandler::kSamplerTextureDescriptorSet;
64 // In the descriptor table the descriptors will all be contiguous.
65 shaderViewRanges[numTextureSamplers].OffsetInDescriptorsFromTableStart =
66 D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
67 }
68
69 if (numShaderViews) {
70 unsigned numDescriptorRanges = numUAVs ? numTextureSamplers + 1 : numTextureSamplers;
71 parameters[parameterCount].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
72 parameters[parameterCount].DescriptorTable.NumDescriptorRanges = numDescriptorRanges;
73 parameters[parameterCount].DescriptorTable.pDescriptorRanges = shaderViewRanges.get();
74 parameters[parameterCount].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
75 parameterCount++;
76 }
77
78 if (numTextureSamplers) {
79 parameters[parameterCount].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
80 parameters[parameterCount].DescriptorTable.NumDescriptorRanges = numTextureSamplers;
81 parameters[parameterCount].DescriptorTable.pDescriptorRanges = samplerRanges.get();
82 parameters[parameterCount].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
83 parameterCount++;
84 }
85
86 D3D12_ROOT_SIGNATURE_DESC rootDesc{};
87 rootDesc.NumParameters = parameterCount;
88 rootDesc.pParameters = parameters;
89 rootDesc.NumStaticSamplers = 0;
90 rootDesc.pStaticSamplers = nullptr;
91 rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
92
93 gr_cp<ID3DBlob> rootSigBinary;
94 gr_cp<ID3DBlob> error;
95 // TODO: D3D Static Function
96 HRESULT hr = D3D12SerializeRootSignature(&rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0,
97 &rootSigBinary, &error);
98
99 if (!SUCCEEDED(hr)) {
100 SkDebugf("Failed to serialize root signature. Error: %s\n",
101 reinterpret_cast<char*>(error->GetBufferPointer()));
102 return nullptr;
103 }
104
105 gr_cp<ID3D12RootSignature> rootSig;
106
107 hr = gpu->device()->CreateRootSignature(0, rootSigBinary->GetBufferPointer(),
108 rootSigBinary->GetBufferSize(), IID_PPV_ARGS(&rootSig));
109 if (!SUCCEEDED(hr)) {
110 SkDebugf("Failed to create root signature.\n");
111 return nullptr;
112 }
113
114 return sk_sp<GrD3DRootSignature>(new GrD3DRootSignature(std::move(rootSig),
115 numTextureSamplers,
116 numUAVs));
117 }
118
GrD3DRootSignature(gr_cp<ID3D12RootSignature> rootSig,int numTextureSamplers,int numUAVs)119 GrD3DRootSignature::GrD3DRootSignature(gr_cp<ID3D12RootSignature> rootSig, int numTextureSamplers,
120 int numUAVs)
121 : fRootSignature(std::move(rootSig))
122 , fNumTextureSamplers(numTextureSamplers)
123 , fNumUAVs(numUAVs) {
124 }
125
isCompatible(int numTextureSamplers,int numUAVs) const126 bool GrD3DRootSignature::isCompatible(int numTextureSamplers, int numUAVs) const {
127 return fNumTextureSamplers == numTextureSamplers && fNumUAVs == numUAVs;
128 }
129