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