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