133 lines
4.1 KiB
Markdown
133 lines
4.1 KiB
Markdown
# D3D12RootSignature
|
|
|
|
DirectX 12 根签名的 D3D12 实现,封装了 `ID3D12RootSignature`。
|
|
|
|
## 头文件
|
|
|
|
```cpp
|
|
#include <XCEngine/RHI/D3D12/D3D12RootSignature.h>
|
|
```
|
|
|
|
## 命名空间
|
|
|
|
`XCEngine::RHI` (不继承 RHI 抽象接口)
|
|
|
|
## 公共成员函数
|
|
|
|
### 构造函数与析构函数
|
|
|
|
#### `D3D12RootSignature()`
|
|
默认构造函数。
|
|
|
|
#### `~D3D12RootSignature()`
|
|
析构函数,确保调用 `Shutdown()`。
|
|
|
|
### 生命周期
|
|
|
|
#### `bool Initialize(ID3D12Device* device, const D3D12_ROOT_SIGNATURE_DESC& desc)`
|
|
初始化根签名。
|
|
- `device`: D3D12 设备
|
|
- `desc`: 根签名描述
|
|
- 返回: 初始化是否成功
|
|
|
|
#### `void Shutdown()`
|
|
释放根签名。
|
|
|
|
### 原生接口
|
|
|
|
#### `ID3D12RootSignature* GetRootSignature() const`
|
|
获取底层 `ID3D12RootSignature` 指针。
|
|
|
|
#### `void* GetNativeHandle() const`
|
|
返回原生句柄。
|
|
|
|
#### `uint32_t GetParameterCount() const`
|
|
获取根参数数量。
|
|
|
|
### 静态辅助函数
|
|
|
|
#### `static D3D12_ROOT_SIGNATURE_DESC CreateDesc(...)`
|
|
创建根签名描述。
|
|
|
|
参数:
|
|
- `D3D12_ROOT_PARAMETER* parameters`: 根参数数组
|
|
- `uint32_t parameterCount`: 参数数量
|
|
- `D3D12_STATIC_SAMPLER_DESC* samplers`: 静态采样器数组
|
|
- `uint32_t samplerCount`: 静态采样器数量
|
|
- `D3D12_ROOT_SIGNATURE_FLAGS flags`: 标志
|
|
|
|
#### `static D3D12_ROOT_PARAMETER CreateCBV(uint32_t shaderRegister, ShaderVisibility visibility = ShaderVisibility::All, uint32_t registerSpace = 0)`
|
|
创建常量缓冲区视图根参数。
|
|
|
|
#### `static D3D12_ROOT_PARAMETER CreateSRV(uint32_t shaderRegister, ShaderVisibility visibility = ShaderVisibility::All, uint32_t registerSpace = 0)`
|
|
创建着色器资源视图根参数。
|
|
|
|
#### `static D3D12_ROOT_PARAMETER CreateUAV(uint32_t shaderRegister, ShaderVisibility visibility = ShaderVisibility::All, uint32_t registerSpace = 0)`
|
|
创建无序访问视图根参数。
|
|
|
|
#### `static D3D12_ROOT_PARAMETER Create32BitConstants(uint32_t shaderRegister, uint32_t num32BitValues, ShaderVisibility visibility = ShaderVisibility::All, uint32_t registerSpace = 0)`
|
|
创建 32 位常量根参数。
|
|
|
|
#### `static D3D12_ROOT_PARAMETER CreateDescriptorTable(uint32_t numRanges, const D3D12_DESCRIPTOR_RANGE* ranges, ShaderVisibility visibility = ShaderVisibility::All)`
|
|
创建描述符表根参数。
|
|
|
|
#### `static D3D12_STATIC_SAMPLER_DESC CreateStaticSampler(...)`
|
|
创建静态采样器描述。
|
|
|
|
#### `static D3D12_SAMPLER_DESC CreateSamplerDesc(FilterMode filter, TextureAddressMode address, float maxLOD = D3D12_FLOAT32_MAX)`
|
|
创建采样器描述。
|
|
|
|
#### `static D3D12_DESCRIPTOR_RANGE CreateDescriptorRange(...)`
|
|
创建描述符范围。
|
|
|
|
参数:
|
|
- `type`: 描述符类型 (CBV, SRV, UAV, Sampler)
|
|
- `baseShaderRegister`: 基寄存器编号
|
|
- `numDescriptors`: 描述符数量
|
|
- `registerSpace`: 寄存器空间
|
|
|
|
## 内部成员
|
|
|
|
| 成员 | 类型 | 描述 |
|
|
|------|------|------|
|
|
| `m_rootSignature` | `ComPtr<ID3D12RootSignature>` | D3D12 根签名 |
|
|
| `m_parameterCount` | `uint32_t` | 参数数量 |
|
|
|
|
## 使用示例
|
|
|
|
```cpp
|
|
// Create root parameters
|
|
D3D12_ROOT_PARAMETER params[3];
|
|
|
|
// Parameter 0: CBV
|
|
params[0] = D3D12RootSignature::CreateCBV(0);
|
|
|
|
// Parameter 1: 32-bit constants (e.g., 4x4 matrix = 16 floats)
|
|
params[1] = D3D12RootSignature::Create32BitConstants(0, 16);
|
|
|
|
// Parameter 2: Descriptor table
|
|
D3D12_DESCRIPTOR_RANGE ranges[1];
|
|
ranges[0] = D3D12RootSignature::CreateDescriptorRange(
|
|
D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 0, 10);
|
|
params[2] = D3D12RootSignature::CreateDescriptorTable(1, ranges);
|
|
|
|
// Create static samplers
|
|
D3D12_SAMPLER_DESC sampDesc = D3D12RootSignature::CreateSamplerDesc(
|
|
FilterMode::Linear, TextureAddressMode::Wrap);
|
|
D3D12_STATIC_SAMPLER_DESC staticSamp = D3D12RootSignature::CreateStaticSampler(0, sampDesc);
|
|
|
|
// Create root signature
|
|
D3D12_ROOT_SIGNATURE_DESC rsDesc = D3D12RootSignature::CreateDesc(
|
|
params, 3, &staticSamp, 1);
|
|
D3D12RootSignature rootSig;
|
|
rootSig.Initialize(device->GetDevice(), rsDesc);
|
|
```
|
|
|
|
## 备注
|
|
|
|
- 根签名定义 GPU 能访问的资源布局
|
|
- 根参数有三种类型: 描述符表、常量、描述符
|
|
- 描述符表使用描述符范围引用描述符堆中的描述符
|
|
- 静态采样器嵌入根签名,无需描述符堆
|
|
- 根签名需要与 PSO 匹配
|