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

@@ -17,6 +17,8 @@ namespace RHI {
class RHIPipelineState;
class D3D12ResourceView;
class D3D12Shader;
class D3D12PipelineLayout;
class D3D12CommandList : public RHICommandList {
public:
@@ -31,6 +33,21 @@ public:
ID3D12GraphicsCommandList* GetCommandList() const { return m_commandList.Get(); }
void SetShader(RHIShader* shader) override;
void SetUniformInt(const char* name, int value) override;
void SetUniformFloat(const char* name, float value) override;
void SetUniformVec3(const char* name, float x, float y, float z) override;
void SetUniformVec4(const char* name, float x, float y, float z, float w) override;
void SetUniformMat4(const char* name, const float* value) override;
void SetGlobalInt(const char* name, int value) override;
void SetGlobalFloat(const char* name, float value) override;
void SetGlobalVec3(const char* name, float x, float y, float z) override;
void SetGlobalVec4(const char* name, float x, float y, float z, float w) override;
void SetGlobalMat4(const char* name, const float* value) override;
void SetGlobalTexture(const char* name, RHIResourceView* texture) override;
void TransitionBarrier(RHIResourceView* resource, ResourceStates stateBefore, ResourceStates stateAfter) override;
void TransitionBarrier(ID3D12Resource* resource, ResourceStates stateBefore, ResourceStates stateAfter);
void TransitionBarrierInternal(ID3D12Resource* resource, ResourceStates stateBefore, ResourceStates stateAfter, uint32_t subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES);
@@ -42,6 +59,7 @@ public:
void SetPipelineState(RHIPipelineState* pso) override;
void SetPipelineState(ID3D12PipelineState* pso);
void SetPipelineStateInternal(ID3D12PipelineState* pso);
void SetPipelineLayout(D3D12PipelineLayout* layout);
void SetRootSignature(ID3D12RootSignature* signature);
void SetViewport(const Viewport& viewport) override;
void SetViewports(uint32_t count, const Viewport* viewports) override;
@@ -131,7 +149,16 @@ private:
std::vector<D3D12_CPU_DESCRIPTOR_HANDLE> m_boundRenderTargets;
D3D12_CPU_DESCRIPTOR_HANDLE m_boundDepthStencil;
bool m_depthStencilBound = false;
D3D12Shader* m_currentShader;
class D3D12PipelineLayout* m_currentPipelineLayout;
std::unordered_map<std::string, int> m_globalIntCache;
std::unordered_map<std::string, float> m_globalFloatCache;
std::unordered_map<std::string, std::vector<float>> m_globalVec3Cache;
std::unordered_map<std::string, std::vector<float>> m_globalVec4Cache;
std::unordered_map<std::string, std::vector<float>> m_globalMat4Cache;
std::unordered_map<std::string, RHIResourceView*> m_globalTextureCache;
};
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -10,6 +10,7 @@
#include "../RHIEnums.h"
#include "../RHITypes.h"
#include "D3D12Enums.h"
#include "D3D12PipelineLayout.h"
using Microsoft::WRL::ComPtr;
@@ -69,6 +70,7 @@ public:
RHICommandQueue* CreateCommandQueue(const CommandQueueDesc& desc) override;
RHIShader* CompileShader(const ShaderCompileDesc& desc) override;
RHIPipelineState* CreatePipelineState(const GraphicsPipelineDesc& desc) override;
RHIPipelineLayout* CreatePipelineLayout(const RHIPipelineLayoutDesc& desc) override;
RHIFence* CreateFence(const FenceDesc& desc) override;
RHISampler* CreateSampler(const SamplerDesc& desc) override;

View File

@@ -0,0 +1,42 @@
#pragma once
#include <d3d12.h>
#include <wrl/client.h>
#include <unordered_map>
#include "../RHIPipelineLayout.h"
#include "D3D12RootSignature.h"
using Microsoft::WRL::ComPtr;
namespace XCEngine {
namespace RHI {
class D3D12Device;
class D3D12PipelineLayout : public RHIPipelineLayout {
public:
D3D12PipelineLayout();
~D3D12PipelineLayout() override;
bool InitializeWithDevice(D3D12Device* device, const RHIPipelineLayoutDesc& desc);
void Shutdown() override;
bool Initialize(const RHIPipelineLayoutDesc& desc) override { return false; }
void* GetNativeHandle() override { return m_rootSignature.Get(); }
ID3D12RootSignature* GetRootSignature() const { return m_rootSignature.Get(); }
uint32_t GetRootParameterIndex(uint32_t shaderRegister) const;
bool HasRootParameter(uint32_t shaderRegister) const;
private:
bool InitializeInternal(D3D12Device* device, const RHIPipelineLayoutDesc& desc);
ComPtr<ID3D12RootSignature> m_rootSignature;
D3D12Device* m_device;
std::unordered_map<uint32_t, uint32_t> m_registerToRootIndex;
std::vector<D3D12_ROOT_PARAMETER> m_rootParameters;
};
} // namespace RHI
} // namespace XCEngine

View File

@@ -4,6 +4,7 @@
#include <dxgi1_4.h>
#include <wrl/client.h>
#include <string>
#include <vector>
#include "../RHIShader.h"
#include "D3D12Enums.h"
@@ -32,21 +33,19 @@ public:
void* GetNativeHandle() override { return m_bytecode.Get(); }
bool IsValid() const override { return m_bytecode != nullptr; }
void Bind() override { }
void Unbind() override { }
void SetInt(const char* name, int value) override { }
void SetFloat(const char* name, float value) override { }
void SetVec3(const char* name, float x, float y, float z) override { }
void SetVec4(const char* name, float x, float y, float z, float w) override { }
void SetMat4(const char* name, const float* value) override { }
const std::vector<UniformInfo>& GetUniformInfos() const override;
const UniformInfo* GetUniformInfo(const char* name) const override;
private:
void CacheUniformInfos() const;
ComPtr<ID3DBlob> m_bytecode;
ComPtr<ID3DBlob> m_error;
ShaderType m_type;
InputLayoutDesc m_inputLayout;
mutable std::vector<UniformInfo> m_uniformInfos;
mutable bool m_uniformsCached = false;
};
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -2,6 +2,7 @@
#include <vector>
#include <cstdint>
#include <unordered_map>
#include "../RHICommandList.h"
@@ -51,6 +52,21 @@ public:
void ClearStencil(int stencil);
void ClearDepthStencil(float depth, int stencil);
void SetShader(RHIShader* shader) override;
void SetUniformInt(const char* name, int value) override;
void SetUniformFloat(const char* name, float value) override;
void SetUniformVec3(const char* name, float x, float y, float z) override;
void SetUniformVec4(const char* name, float x, float y, float z, float w) override;
void SetUniformMat4(const char* name, const float* value) override;
void SetGlobalInt(const char* name, int value) override;
void SetGlobalFloat(const char* name, float value) override;
void SetGlobalVec3(const char* name, float x, float y, float z) override;
void SetGlobalVec4(const char* name, float x, float y, float z, float w) override;
void SetGlobalMat4(const char* name, const float* value) override;
void SetGlobalTexture(const char* name, RHIResourceView* texture) override;
void SetPipelineState(RHIPipelineState* pipelineState) override;
void SetVertexBuffer(uint32_t slot, RHIResourceView* buffer, uint64_t offset) override;
void SetVertexBuffers(uint32_t startSlot, uint32_t count, RHIResourceView** buffers, const uint64_t* offsets, const uint32_t* strides) override;
@@ -176,7 +192,14 @@ private:
unsigned int m_primitiveType;
unsigned int m_currentVAO;
unsigned int m_currentProgram;
OpenGLShader* m_currentShader;
std::unordered_map<std::string, int> m_globalIntCache;
std::unordered_map<std::string, float> m_globalFloatCache;
std::unordered_map<std::string, std::vector<float>> m_globalVec3Cache;
std::unordered_map<std::string, std::vector<float>> m_globalVec4Cache;
std::unordered_map<std::string, std::vector<float>> m_globalMat4Cache;
std::unordered_map<std::string, RHIResourceView*> m_globalTextureCache;
};
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -50,6 +50,7 @@ public:
RHICommandQueue* CreateCommandQueue(const CommandQueueDesc& desc) override;
RHIShader* CompileShader(const ShaderCompileDesc& desc) override;
RHIPipelineState* CreatePipelineState(const GraphicsPipelineDesc& desc) override;
RHIPipelineLayout* CreatePipelineLayout(const RHIPipelineLayoutDesc& desc) override;
RHIFence* CreateFence(const FenceDesc& desc) override;
RHISampler* CreateSampler(const SamplerDesc& desc) override;

View File

@@ -8,6 +8,7 @@ namespace RHI {
class RHIResourceView;
class RHIPipelineState;
class RHIShader;
struct DepthStencilState {
bool depthEnable = true;
@@ -54,6 +55,21 @@ public:
virtual void TransitionBarrier(RHIResourceView* resource, ResourceStates stateBefore, ResourceStates stateAfter) = 0;
virtual void SetShader(RHIShader* shader) = 0;
virtual void SetUniformInt(const char* name, int value) = 0;
virtual void SetUniformFloat(const char* name, float value) = 0;
virtual void SetUniformVec3(const char* name, float x, float y, float z) = 0;
virtual void SetUniformVec4(const char* name, float x, float y, float z, float w) = 0;
virtual void SetUniformMat4(const char* name, const float* value) = 0;
virtual void SetGlobalInt(const char* name, int value) = 0;
virtual void SetGlobalFloat(const char* name, float value) = 0;
virtual void SetGlobalVec3(const char* name, float x, float y, float z) = 0;
virtual void SetGlobalVec4(const char* name, float x, float y, float z, float w) = 0;
virtual void SetGlobalMat4(const char* name, const float* value) = 0;
virtual void SetGlobalTexture(const char* name, RHIResourceView* texture) = 0;
virtual void SetPipelineState(RHIPipelineState* pso) = 0;
virtual void SetPrimitiveTopology(PrimitiveTopology topology) = 0;
virtual void SetViewport(const Viewport& viewport) = 0;
@@ -86,4 +102,4 @@ public:
};
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -14,6 +14,7 @@ class RHICommandList;
class RHICommandQueue;
class RHIShader;
class RHIPipelineState;
class RHIPipelineLayout;
class RHIFence;
class RHISampler;
class RHIResourceView;
@@ -32,6 +33,7 @@ public:
virtual RHICommandQueue* CreateCommandQueue(const CommandQueueDesc& desc) = 0;
virtual RHIShader* CompileShader(const ShaderCompileDesc& desc) = 0;
virtual RHIPipelineState* CreatePipelineState(const GraphicsPipelineDesc& desc) = 0;
virtual RHIPipelineLayout* CreatePipelineLayout(const RHIPipelineLayoutDesc& desc) = 0;
virtual RHIFence* CreateFence(const FenceDesc& desc) = 0;
virtual RHISampler* CreateSampler(const SamplerDesc& desc) = 0;
@@ -47,4 +49,4 @@ public:
};
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -28,6 +28,7 @@ public:
uint32_t size;
uint32_t type;
uint32_t arraySize;
uint32_t offset;
};
virtual const std::vector<UniformInfo>& GetUniformInfos() const = 0;