Refactor: use engine helpers for RootSignature and PSO creation

This commit is contained in:
2026-03-17 01:16:39 +08:00
parent 73627f62f4
commit 64bd8c5074
3 changed files with 38 additions and 74 deletions

View File

@@ -213,17 +213,11 @@ public:
// 定义GPU资源绑定规则: CBV(常量缓冲) / SRV(着色器资源) / DescriptorTable
//=================================================================================
ID3D12RootSignature* InitRootSignature() {
D3D12_ROOT_PARAMETER rootParameters[4];
rootParameters[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
rootParameters[1].ShaderVisibility = ToD3D12(ShaderVisibility::Vertex);
rootParameters[1].Constants.RegisterSpace = 0;
rootParameters[1].Constants.ShaderRegister = 0;
rootParameters[1].Constants.Num32BitValues = 4;
using namespace XCEngine::RHI;
rootParameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParameters[0].ShaderVisibility = ToD3D12(ShaderVisibility::All);
rootParameters[0].Descriptor.RegisterSpace = 0;
rootParameters[0].Descriptor.ShaderRegister = 1;
D3D12_ROOT_PARAMETER rootParameters[4];
rootParameters[0] = D3D12RootSignature::CreateCBV(1, ShaderVisibility::All, 0);
rootParameters[1] = D3D12RootSignature::Create32BitConstants(0, 4, ShaderVisibility::Vertex, 0);
D3D12_DESCRIPTOR_RANGE descriptorRange[1];
descriptorRange[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
@@ -231,35 +225,20 @@ ID3D12RootSignature* InitRootSignature() {
descriptorRange[0].BaseShaderRegister = 0;
descriptorRange[0].NumDescriptors = 1;
descriptorRange[0].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
rootParameters[2] = D3D12RootSignature::CreateDescriptorTable(1, descriptorRange, ShaderVisibility::Pixel);
rootParameters[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParameters[2].ShaderVisibility = ToD3D12(ShaderVisibility::Pixel);
rootParameters[2].DescriptorTable.pDescriptorRanges = descriptorRange;
rootParameters[2].DescriptorTable.NumDescriptorRanges = _countof(descriptorRange);
rootParameters[3] = D3D12RootSignature::CreateSRV(0, ShaderVisibility::All, 1);
rootParameters[3].ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;
rootParameters[3].ShaderVisibility = ToD3D12(ShaderVisibility::All);
rootParameters[3].Descriptor.RegisterSpace = 1;
rootParameters[3].Descriptor.ShaderRegister = 0;
D3D12_SAMPLER_DESC samplerDesc = {};
samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
samplerDesc.MaxLOD = D3D12_FLOAT32_MAX;
D3D12_STATIC_SAMPLER_DESC staticSamplerDesc = D3D12RootSignature::CreateStaticSampler(0, samplerDesc, ShaderVisibility::Pixel);
D3D12_STATIC_SAMPLER_DESC samplerDesc[1];
memset(samplerDesc, 0, sizeof(D3D12_STATIC_SAMPLER_DESC) * _countof(samplerDesc));
samplerDesc[0].Filter = ToD3D12(FilterMode::Linear);
samplerDesc[0].AddressU = ToD3D12(TextureAddressMode::Clamp);
samplerDesc[0].AddressV = ToD3D12(TextureAddressMode::Clamp);
samplerDesc[0].AddressW = ToD3D12(TextureAddressMode::Clamp);
samplerDesc[0].BorderColor = ToD3D12(BorderColor::OpaqueBlack);
samplerDesc[0].MaxLOD = D3D12_FLOAT32_MAX;
samplerDesc[0].RegisterSpace = 0;
samplerDesc[0].ShaderRegister = 0;
samplerDesc[0].ShaderVisibility = ToD3D12(ShaderVisibility::Pixel);
D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc = {};
rootSignatureDesc.NumParameters = _countof(rootParameters);
rootSignatureDesc.pParameters = rootParameters;
rootSignatureDesc.NumStaticSamplers = _countof(samplerDesc);
rootSignatureDesc.pStaticSamplers = samplerDesc;
rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc = D3D12RootSignature::CreateDesc(
rootParameters, 4, &staticSamplerDesc, 1);
gRootSignature.Initialize(gD3D12Device.GetDevice(), rootSignatureDesc);
@@ -275,48 +254,23 @@ ID3D12RootSignature* InitRootSignature() {
ID3D12PipelineState* CreatePSO(ID3D12RootSignature* inID3D12RootSignature,
D3D12_SHADER_BYTECODE inVertexShader, D3D12_SHADER_BYTECODE inPixelShader,
D3D12_SHADER_BYTECODE inGSShader) {
using namespace XCEngine::RHI;
D3D12_INPUT_ELEMENT_DESC vertexDataElementDesc[] = {
{"POSITION",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,0,D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,0},
{"TEXCOORD",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,sizeof(float) * 4,D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,0},
{"NORMAL",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,sizeof(float) * 8,D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,0},
{"TANGENT",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,sizeof(float) * 12,D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,0}
D3D12PipelineState::CreateInputElement("POSITION", 0, Format::R32G32B32A32_Float, 0, 0),
D3D12PipelineState::CreateInputElement("TEXCOORD", 0, Format::R32G32B32A32_Float, 0, sizeof(float) * 4),
D3D12PipelineState::CreateInputElement("NORMAL", 0, Format::R32G32B32A32_Float, 0, sizeof(float) * 8),
D3D12PipelineState::CreateInputElement("TANGENT", 0, Format::R32G32B32A32_Float, 0, sizeof(float) * 12)
};
D3D12_INPUT_LAYOUT_DESC vertexDataLayoutDesc = {};
vertexDataLayoutDesc.NumElements = 4;
vertexDataLayoutDesc.pInputElementDescs = vertexDataElementDesc;
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.pRootSignature = inID3D12RootSignature;
psoDesc.VS = inVertexShader;
psoDesc.GS = inGSShader;
psoDesc.PS = inPixelShader;
psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
psoDesc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
psoDesc.SampleDesc.Count = 1;
psoDesc.SampleDesc.Quality = 0;
psoDesc.SampleMask = 0xffffffff;
psoDesc.InputLayout = vertexDataLayoutDesc;
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = D3D12PipelineState::CreateDesc(
inID3D12RootSignature,
inVertexShader,
inPixelShader,
inGSShader,
4,
vertexDataElementDesc);
psoDesc.RasterizerState.FillMode = ToD3D12(FillMode::Solid);
psoDesc.RasterizerState.CullMode = ToD3D12(CullMode::Back);
psoDesc.RasterizerState.DepthClipEnable = TRUE;
psoDesc.DepthStencilState.DepthEnable = true;
psoDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
psoDesc.DepthStencilState.DepthFunc = ToD3D12(ComparisonFunc::LessEqual);
psoDesc.BlendState = { 0 };
D3D12_RENDER_TARGET_BLEND_DESC rtBlendDesc = {
FALSE,FALSE,
ToD3D12(BlendFactor::SrcAlpha),ToD3D12(BlendFactor::InvSrcAlpha),ToD3D12(BlendOp::Add),
ToD3D12(BlendFactor::SrcAlpha),ToD3D12(BlendFactor::InvSrcAlpha),ToD3D12(BlendOp::Add),
ToD3D12(LogicOp::Noop),
static_cast<UINT8>(ColorWriteMask::All),
};
for (int i = 0; i < D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i)
psoDesc.BlendState.RenderTarget[i] = rtBlendDesc;
psoDesc.NumRenderTargets = 1;
ID3D12PipelineState* d3d12PSO = nullptr;
gPipelineState.Initialize(gD3D12Device.GetDevice(), psoDesc);