refactor(RHI): 完成 Shader uniform 设置迁移到 CommandList

- 删除 RHIShader 的 OpenGL 风格 SetMat4/SetVec3/SetInt 等方法
- 添加 UniformInfo 结构体和 GetUniformInfos/GetUniformInfo 接口
- D3D12Shader 和 OpenGLShader 实现 CacheUniformInfos
- RHICommandList 添加 SetUniform*/SetGlobal* 统一接口
- D3D12 实现 D3D12PipelineLayout 管理 root signature 映射
- 修复 D3D12CommandList::SetPipelineStateInternal 在 Reset 后未重新应用 root signature 的问题
- 更新 OpenGL 集成测试使用新的 SetUniform* API
- 所有单元测试和集成测试通过 (8/8 integration tests)
This commit is contained in:
2026-03-24 19:47:22 +08:00
parent 135fe9145b
commit 0f5d018c1a
21 changed files with 578 additions and 54 deletions

View File

@@ -1,11 +1,15 @@
#include "XCEngine/RHI/D3D12/D3D12Shader.h"
#include <d3dcompiler.h>
static const IID IID_ID3D12ShaderReflection = {
0x8e5c5a69, 0x5c6a, 0x427d, {0xb0, 0xdc, 0x27, 0x63, 0xae, 0xac, 0xe3, 0x75}
};
namespace XCEngine {
namespace RHI {
D3D12Shader::D3D12Shader()
: m_type(ShaderType::Vertex) {
: m_type(ShaderType::Vertex), m_uniformsCached(false) {
}
D3D12Shader::~D3D12Shader() {
@@ -35,6 +39,7 @@ bool D3D12Shader::CompileFromFile(const wchar_t* filePath, const char* entryPoin
m_type = ShaderType::Compute;
}
m_uniformsCached = false;
return true;
}
@@ -50,12 +55,87 @@ bool D3D12Shader::Compile(const void* sourceData, size_t sourceSize, const char*
return false;
}
m_uniformsCached = false;
return true;
}
void D3D12Shader::Shutdown() {
m_bytecode.Reset();
m_error.Reset();
m_uniformInfos.clear();
m_uniformsCached = false;
}
void D3D12Shader::CacheUniformInfos() const {
if (m_uniformsCached || !m_bytecode) {
return;
}
m_uniformInfos.clear();
ComPtr<ID3D12ShaderReflection> pReflection;
HRESULT hr = D3DReflect(m_bytecode->GetBufferPointer(), m_bytecode->GetBufferSize(),
IID_ID3D12ShaderReflection, (void**)&pReflection);
if (FAILED(hr)) {
return;
}
D3D12_SHADER_DESC shaderDesc;
pReflection->GetDesc(&shaderDesc);
for (UINT i = 0; i < shaderDesc.BoundResources; ++i) {
D3D12_SHADER_INPUT_BIND_DESC bindDesc;
pReflection->GetResourceBindingDesc(i, &bindDesc);
UniformInfo info;
info.name = bindDesc.Name;
info.bindPoint = bindDesc.BindPoint;
info.arraySize = bindDesc.NumSamples;
switch (bindDesc.Type) {
case D3D_SIT_CBUFFER:
info.type = static_cast<uint32_t>(D3D_SIT_CBUFFER);
break;
case D3D_SIT_TEXTURE:
info.type = static_cast<uint32_t>(D3D_SIT_TEXTURE);
break;
case D3D_SIT_SAMPLER:
info.type = static_cast<uint32_t>(D3D_SIT_SAMPLER);
break;
default:
info.type = static_cast<uint32_t>(bindDesc.Type);
break;
}
D3D12_SHADER_BUFFER_DESC bufferDesc;
if (bindDesc.Type == D3D_SIT_CBUFFER) {
ID3D12ShaderReflectionConstantBuffer* pCB = pReflection->GetConstantBufferByIndex(bindDesc.BindPoint);
pCB->GetDesc(&bufferDesc);
info.size = bufferDesc.Size;
} else {
info.size = 0;
}
m_uniformInfos.push_back(info);
}
m_uniformsCached = true;
}
const std::vector<RHIShader::UniformInfo>& D3D12Shader::GetUniformInfos() const {
CacheUniformInfos();
return m_uniformInfos;
}
const RHIShader::UniformInfo* D3D12Shader::GetUniformInfo(const char* name) const {
CacheUniformInfos();
for (const auto& info : m_uniformInfos) {
if (info.name == name) {
return &info;
}
}
return nullptr;
}
const D3D12_SHADER_BYTECODE D3D12Shader::GetD3D12Bytecode() const {
@@ -90,4 +170,4 @@ ShaderType D3D12Shader::GetType() const {
}
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine