143 lines
5.2 KiB
C++
143 lines
5.2 KiB
C++
#include "XCEngine/RHI/D3D12/D3D12RootSignature.h"
|
|
|
|
namespace XCEngine {
|
|
namespace RHI {
|
|
|
|
D3D12RootSignature::D3D12RootSignature() {
|
|
}
|
|
|
|
D3D12RootSignature::~D3D12RootSignature() {
|
|
Shutdown();
|
|
}
|
|
|
|
bool D3D12RootSignature::Initialize(ID3D12Device* device, const D3D12_ROOT_SIGNATURE_DESC& desc) {
|
|
ID3DBlob* signature = nullptr;
|
|
ID3DBlob* error = nullptr;
|
|
|
|
HRESULT hResult = D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error);
|
|
if (FAILED(hResult)) {
|
|
if (error) {
|
|
error->Release();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
hResult = device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&m_rootSignature));
|
|
|
|
signature->Release();
|
|
|
|
if (FAILED(hResult)) {
|
|
return false;
|
|
}
|
|
|
|
m_parameterCount = desc.NumParameters;
|
|
|
|
return true;
|
|
}
|
|
|
|
void D3D12RootSignature::Shutdown() {
|
|
m_rootSignature.Reset();
|
|
}
|
|
|
|
D3D12_ROOT_SIGNATURE_DESC D3D12RootSignature::CreateDesc(
|
|
D3D12_ROOT_PARAMETER* parameters,
|
|
uint32_t parameterCount,
|
|
D3D12_STATIC_SAMPLER_DESC* samplers,
|
|
uint32_t samplerCount,
|
|
D3D12_ROOT_SIGNATURE_FLAGS flags) {
|
|
D3D12_ROOT_SIGNATURE_DESC desc = {};
|
|
desc.NumParameters = parameterCount;
|
|
desc.pParameters = parameters;
|
|
desc.NumStaticSamplers = samplerCount;
|
|
desc.pStaticSamplers = samplers;
|
|
desc.Flags = flags;
|
|
return desc;
|
|
}
|
|
|
|
D3D12_ROOT_PARAMETER D3D12RootSignature::CreateCBV(uint32_t shaderRegister, ShaderVisibility visibility, uint32_t registerSpace) {
|
|
D3D12_ROOT_PARAMETER param = {};
|
|
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
|
|
param.ShaderVisibility = ToD3D12(visibility);
|
|
param.Descriptor.RegisterSpace = registerSpace;
|
|
param.Descriptor.ShaderRegister = shaderRegister;
|
|
return param;
|
|
}
|
|
|
|
D3D12_ROOT_PARAMETER D3D12RootSignature::CreateSRV(uint32_t shaderRegister, ShaderVisibility visibility, uint32_t registerSpace) {
|
|
D3D12_ROOT_PARAMETER param = {};
|
|
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;
|
|
param.ShaderVisibility = ToD3D12(visibility);
|
|
param.Descriptor.RegisterSpace = registerSpace;
|
|
param.Descriptor.ShaderRegister = shaderRegister;
|
|
return param;
|
|
}
|
|
|
|
D3D12_ROOT_PARAMETER D3D12RootSignature::CreateUAV(uint32_t shaderRegister, ShaderVisibility visibility, uint32_t registerSpace) {
|
|
D3D12_ROOT_PARAMETER param = {};
|
|
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV;
|
|
param.ShaderVisibility = ToD3D12(visibility);
|
|
param.Descriptor.RegisterSpace = registerSpace;
|
|
param.Descriptor.ShaderRegister = shaderRegister;
|
|
return param;
|
|
}
|
|
|
|
D3D12_ROOT_PARAMETER D3D12RootSignature::Create32BitConstants(uint32_t shaderRegister, uint32_t num32BitValues, ShaderVisibility visibility, uint32_t registerSpace) {
|
|
D3D12_ROOT_PARAMETER param = {};
|
|
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
|
|
param.ShaderVisibility = ToD3D12(visibility);
|
|
param.Constants.RegisterSpace = registerSpace;
|
|
param.Constants.ShaderRegister = shaderRegister;
|
|
param.Constants.Num32BitValues = num32BitValues;
|
|
return param;
|
|
}
|
|
|
|
D3D12_ROOT_PARAMETER D3D12RootSignature::CreateDescriptorTable(uint32_t numRanges, const D3D12_DESCRIPTOR_RANGE* ranges, ShaderVisibility visibility) {
|
|
D3D12_ROOT_PARAMETER param = {};
|
|
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
|
|
param.ShaderVisibility = ToD3D12(visibility);
|
|
param.DescriptorTable.NumDescriptorRanges = numRanges;
|
|
param.DescriptorTable.pDescriptorRanges = ranges;
|
|
return param;
|
|
}
|
|
|
|
D3D12_STATIC_SAMPLER_DESC D3D12RootSignature::CreateStaticSampler(uint32_t shaderRegister, const D3D12_SAMPLER_DESC& desc, ShaderVisibility visibility) {
|
|
D3D12_STATIC_SAMPLER_DESC samplerDesc = {};
|
|
samplerDesc.Filter = desc.Filter;
|
|
samplerDesc.AddressU = desc.AddressU;
|
|
samplerDesc.AddressV = desc.AddressV;
|
|
samplerDesc.AddressW = desc.AddressW;
|
|
samplerDesc.MipLODBias = desc.MipLODBias;
|
|
samplerDesc.MaxAnisotropy = desc.MaxAnisotropy;
|
|
samplerDesc.ComparisonFunc = desc.ComparisonFunc;
|
|
samplerDesc.BorderColor = D3D12_STATIC_BORDER_COLOR_OPAQUE_BLACK;
|
|
samplerDesc.MinLOD = desc.MinLOD;
|
|
samplerDesc.MaxLOD = desc.MaxLOD;
|
|
samplerDesc.RegisterSpace = 0;
|
|
samplerDesc.ShaderRegister = shaderRegister;
|
|
samplerDesc.ShaderVisibility = ToD3D12(visibility);
|
|
return samplerDesc;
|
|
}
|
|
|
|
D3D12_SAMPLER_DESC D3D12RootSignature::CreateSamplerDesc(FilterMode filter, TextureAddressMode address, float maxLOD) {
|
|
D3D12_SAMPLER_DESC desc = {};
|
|
desc.Filter = ToD3D12(filter);
|
|
desc.AddressU = ToD3D12(address);
|
|
desc.AddressV = ToD3D12(address);
|
|
desc.AddressW = ToD3D12(address);
|
|
desc.MaxLOD = maxLOD;
|
|
return desc;
|
|
}
|
|
|
|
D3D12_DESCRIPTOR_RANGE D3D12RootSignature::CreateDescriptorRange(D3D12_DESCRIPTOR_RANGE_TYPE type, uint32_t baseShaderRegister, uint32_t numDescriptors, uint32_t registerSpace) {
|
|
D3D12_DESCRIPTOR_RANGE range = {};
|
|
range.RangeType = type;
|
|
range.NumDescriptors = numDescriptors;
|
|
range.BaseShaderRegister = baseShaderRegister;
|
|
range.RegisterSpace = registerSpace;
|
|
range.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
|
|
return range;
|
|
}
|
|
|
|
} // namespace RHI
|
|
} // namespace XCEngine
|