Refactor RHI ResourceView abstraction layer for unified cross-platform interface

- Create unified RHIResourceView base interface with type-specific Initialize methods
- Implement D3D12ResourceView with RTV/DSV/SRV/UAV/CBV support
- Implement OpenGL ResourceView simulation layer using FBO, texture units, and UBO
- Add OpenGLTextureUnitAllocator for managing texture unit bindings
- Add OpenGLUniformBufferManager for UBO binding points
- Add OpenGLFramebuffer for FBO management
- Remove deprecated D3D12 view classes (RenderTargetView, DepthStencilView, etc.)
- Fix ExecuteCommandLists type confusion bug by adding GetNativeHandle to RHICommandList
- Fix test bug where Close() was called before ExecuteCommandLists
- Update all integration tests to use new D3D12ResourceView class
- All tests pass: 144 unit tests + 8 integration tests
This commit is contained in:
2026-03-24 03:49:13 +08:00
parent 86b6d6b042
commit 0a3fe842b9
32 changed files with 1288 additions and 233 deletions

View File

@@ -18,8 +18,7 @@
#include "XCEngine/RHI/D3D12/D3D12SwapChain.h"
#include "XCEngine/RHI/D3D12/D3D12Buffer.h"
#include "XCEngine/RHI/D3D12/D3D12Texture.h"
#include "XCEngine/RHI/D3D12/D3D12RenderTargetView.h"
#include "XCEngine/RHI/D3D12/D3D12DepthStencilView.h"
#include "XCEngine/RHI/D3D12/D3D12ResourceView.h"
#include "XCEngine/RHI/D3D12/D3D12Screenshot.h"
#include "XCEngine/Debug/Logger.h"
#include "XCEngine/Debug/ConsoleLogSink.h"
@@ -48,8 +47,8 @@ D3D12CommandList gCommandList;
D3D12Texture gDepthStencil;
D3D12DescriptorHeap gRTVHeap;
D3D12DescriptorHeap gDSVHeap;
D3D12RenderTargetView gRTVs[2];
D3D12DepthStencilView gDSV;
D3D12ResourceView gRTVs[2];
D3D12ResourceView gDSV;
UINT gRTVDescriptorSize = 0;
UINT gDSVDescriptorSize = 0;
@@ -128,14 +127,13 @@ bool InitD3D12() {
CPUDescriptorHandle rtvCpuHandle = gRTVHeap.GetCPUDescriptorHandle(i);
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = { rtvCpuHandle.ptr };
gRTVs[i].InitializeAt(device, backBuffer.GetResource(), rtvHandle, nullptr);
D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = D3D12ResourceView::CreateRenderTargetDesc(Format::R8G8B8A8_UNorm, D3D12_RTV_DIMENSION_TEXTURE2D);
gRTVs[i].InitializeAsRenderTarget(device, backBuffer.GetResource(), &rtvDesc, &gRTVHeap, i);
}
// Create DSV
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12DepthStencilView::CreateDesc(Format::D24_UNorm_S8_UInt);
CPUDescriptorHandle dsvCpuHandle = gDSVHeap.GetCPUDescriptorHandle(0);
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = { dsvCpuHandle.ptr };
gDSV.InitializeAt(device, gDepthStencil.GetResource(), dsvHandle, &dsvDesc);
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12ResourceView::CreateDepthStencilDesc(Format::D24_UNorm_S8_UInt, D3D12_DSV_DIMENSION_TEXTURE2D);
gDSV.InitializeAsDepthStencil(device, gDepthStencil.GetResource(), &dsvDesc, &gDSVHeap, 0);
// Create command allocator and list
gCommandAllocator.Initialize(device, CommandQueueType::Direct);

View File

@@ -18,13 +18,11 @@
#include "XCEngine/RHI/D3D12/D3D12SwapChain.h"
#include "XCEngine/RHI/D3D12/D3D12Buffer.h"
#include "XCEngine/RHI/D3D12/D3D12Texture.h"
#include "XCEngine/RHI/D3D12/D3D12RenderTargetView.h"
#include "XCEngine/RHI/D3D12/D3D12DepthStencilView.h"
#include "XCEngine/RHI/D3D12/D3D12Shader.h"
#include "XCEngine/RHI/D3D12/D3D12RootSignature.h"
#include "XCEngine/RHI/D3D12/D3D12PipelineState.h"
#include "XCEngine/RHI/D3D12/D3D12Screenshot.h"
#include "XCEngine/RHI/D3D12/D3D12ShaderResourceView.h"
#include "XCEngine/RHI/D3D12/D3D12ResourceView.h"
#include "XCEngine/Debug/Logger.h"
#include "XCEngine/Debug/ConsoleLogSink.h"
#include "XCEngine/Debug/FileLogSink.h"
@@ -51,8 +49,8 @@ D3D12Texture gDepthStencil;
D3D12DescriptorHeap gRTVHeap;
D3D12DescriptorHeap gDSVHeap;
D3D12DescriptorHeap gSRVHeap;
D3D12RenderTargetView gRTVs[2];
D3D12DepthStencilView gDSV;
D3D12ResourceView gRTVs[2];
D3D12ResourceView gDSV;
D3D12Shader gVertexShader;
D3D12Shader gPixelShader;
@@ -60,7 +58,7 @@ D3D12RootSignature gRootSignature;
D3D12PipelineState gPipelineState;
D3D12Buffer gVertexBuffer;
D3D12Texture gDiffuseTexture;
D3D12ShaderResourceView gDiffuseSRV;
D3D12ResourceView gDiffuseSRV;
UINT gRTVDescriptorSize = 0;
UINT gDSVDescriptorSize = 0;
@@ -88,7 +86,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
return DefWindowProc(hwnd, msg, wParam, lParam);
}
bool LoadTexture(const char* filename, D3D12Texture& texture, D3D12ShaderResourceView& srv, ID3D12Device* device, D3D12DescriptorHeap& srvHeap, ID3D12GraphicsCommandList* commandList, D3D12CommandAllocator& allocator, D3D12CommandQueue& queue) {
bool LoadTexture(const char* filename, D3D12Texture& texture, D3D12ResourceView& srv, ID3D12Device* device, D3D12DescriptorHeap& srvHeap, ID3D12GraphicsCommandList* commandList, D3D12CommandAllocator& allocator, D3D12CommandQueue& queue) {
int width, height, channels;
stbi_uc* pixels = stbi_load(filename, &width, &height, &channels, STBI_rgb_alpha);
if (!pixels) {
@@ -113,8 +111,8 @@ bool LoadTexture(const char* filename, D3D12Texture& texture, D3D12ShaderResourc
texture.SetName(filename);
srvHeap.Initialize(device, DescriptorHeapType::CBV_SRV_UAV, 1, true);
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = D3D12ShaderResourceView::CreateDesc(Format::R8G8B8A8_UNorm, D3D12_SRV_DIMENSION_TEXTURE2D);
srv.InitializeAt(device, texture.GetResource(), srvHeap.GetCPUDescriptorHandleForHeapStart(), &srvDesc);
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = D3D12ResourceView::CreateShaderResourceDesc(Format::R8G8B8A8_UNorm, D3D12_SRV_DIMENSION_TEXTURE2D);
srv.InitializeAsShaderResource(device, texture.GetResource(), &srvDesc, &srvHeap, 0);
return true;
}
@@ -156,15 +154,12 @@ bool InitD3D12() {
for (int i = 0; i < 2; i++) {
D3D12Texture& backBuffer = gSwapChain.GetBackBuffer(i);
CPUDescriptorHandle rtvCpuHandle = gRTVHeap.GetCPUDescriptorHandle(i);
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = { rtvCpuHandle.ptr };
gRTVs[i].InitializeAt(device, backBuffer.GetResource(), rtvHandle, nullptr);
D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = D3D12ResourceView::CreateRenderTargetDesc(Format::R8G8B8A8_UNorm, D3D12_RTV_DIMENSION_TEXTURE2D);
gRTVs[i].InitializeAsRenderTarget(device, backBuffer.GetResource(), &rtvDesc, &gRTVHeap, i);
}
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12DepthStencilView::CreateDesc(Format::D24_UNorm_S8_UInt);
CPUDescriptorHandle dsvCpuHandle = gDSVHeap.GetCPUDescriptorHandle(0);
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = { dsvCpuHandle.ptr };
gDSV.InitializeAt(device, gDepthStencil.GetResource(), dsvHandle, &dsvDesc);
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12ResourceView::CreateDepthStencilDesc(Format::D24_UNorm_S8_UInt, D3D12_DSV_DIMENSION_TEXTURE2D);
gDSV.InitializeAsDepthStencil(device, gDepthStencil.GetResource(), &dsvDesc, &gDSVHeap, 0);
gCommandAllocator.Initialize(device, CommandQueueType::Direct);
gCommandList.Initialize(device, CommandQueueType::Direct, gCommandAllocator.GetCommandAllocator());

View File

@@ -20,12 +20,10 @@
#include "XCEngine/RHI/D3D12/D3D12SwapChain.h"
#include "XCEngine/RHI/D3D12/D3D12Buffer.h"
#include "XCEngine/RHI/D3D12/D3D12Texture.h"
#include "XCEngine/RHI/D3D12/D3D12RenderTargetView.h"
#include "XCEngine/RHI/D3D12/D3D12DepthStencilView.h"
#include "XCEngine/RHI/D3D12/D3D12Shader.h"
#include "XCEngine/RHI/D3D12/D3D12RootSignature.h"
#include "XCEngine/RHI/D3D12/D3D12PipelineState.h"
#include "XCEngine/RHI/D3D12/D3D12ShaderResourceView.h"
#include "XCEngine/RHI/D3D12/D3D12ResourceView.h"
#include "XCEngine/RHI/D3D12/D3D12Screenshot.h"
#include "XCEngine/Debug/Logger.h"
#include "XCEngine/Debug/ConsoleLogSink.h"
@@ -56,8 +54,8 @@ D3D12Texture gDepthStencil;
D3D12DescriptorHeap gRTVHeap;
D3D12DescriptorHeap gDSVHeap;
D3D12DescriptorHeap gSRVHeap;
D3D12RenderTargetView gRTVs[2];
D3D12DepthStencilView gDSV;
D3D12ResourceView gRTVs[2];
D3D12ResourceView gDSV;
D3D12Shader gVertexShader;
D3D12Shader gPixelShader;
@@ -67,7 +65,7 @@ D3D12Buffer gVertexBuffer;
D3D12Buffer gIndexBuffer;
D3D12Buffer gMVPBuffer;
D3D12Texture gDiffuseTexture;
D3D12ShaderResourceView gDiffuseSRV;
D3D12ResourceView gDiffuseSRV;
UINT gRTVDescriptorSize = 0;
UINT gDSVDescriptorSize = 0;
@@ -155,7 +153,7 @@ void GenerateSphere(std::vector<Vertex>& vertices, std::vector<UINT32>& indices,
}
}
bool LoadTexture(const char* filename, D3D12Texture& texture, D3D12ShaderResourceView& srv, ID3D12Device* device, D3D12DescriptorHeap& srvHeap, ID3D12GraphicsCommandList* commandList, D3D12CommandAllocator& allocator, D3D12CommandQueue& queue) {
bool LoadTexture(const char* filename, D3D12Texture& texture, D3D12ResourceView& srv, ID3D12Device* device, D3D12DescriptorHeap& srvHeap, ID3D12GraphicsCommandList* commandList, D3D12CommandAllocator& allocator, D3D12CommandQueue& queue) {
int width, height, channels;
stbi_uc* pixels = stbi_load(filename, &width, &height, &channels, STBI_rgb_alpha);
if (!pixels) {
@@ -180,8 +178,8 @@ bool LoadTexture(const char* filename, D3D12Texture& texture, D3D12ShaderResourc
texture.SetName(filename);
srvHeap.Initialize(device, DescriptorHeapType::CBV_SRV_UAV, 1, true);
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = D3D12ShaderResourceView::CreateDesc(Format::R8G8B8A8_UNorm, D3D12_SRV_DIMENSION_TEXTURE2D);
srv.InitializeAt(device, texture.GetResource(), srvHeap.GetCPUDescriptorHandleForHeapStart(), &srvDesc);
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = D3D12ResourceView::CreateShaderResourceDesc(Format::R8G8B8A8_UNorm, D3D12_SRV_DIMENSION_TEXTURE2D);
srv.InitializeAsShaderResource(device, texture.GetResource(), &srvDesc, &srvHeap, 0);
return true;
}
@@ -223,15 +221,12 @@ bool InitD3D12() {
for (int i = 0; i < 2; i++) {
D3D12Texture& backBuffer = gSwapChain.GetBackBuffer(i);
CPUDescriptorHandle rtvCpuHandle = gRTVHeap.GetCPUDescriptorHandle(i);
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = { rtvCpuHandle.ptr };
gRTVs[i].InitializeAt(device, backBuffer.GetResource(), rtvHandle, nullptr);
D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = D3D12ResourceView::CreateRenderTargetDesc(Format::R8G8B8A8_UNorm, D3D12_RTV_DIMENSION_TEXTURE2D);
gRTVs[i].InitializeAsRenderTarget(device, backBuffer.GetResource(), &rtvDesc, &gRTVHeap, i);
}
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12DepthStencilView::CreateDesc(Format::D24_UNorm_S8_UInt);
CPUDescriptorHandle dsvCpuHandle = gDSVHeap.GetCPUDescriptorHandle(0);
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = { dsvCpuHandle.ptr };
gDSV.InitializeAt(device, gDepthStencil.GetResource(), dsvHandle, &dsvDesc);
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12ResourceView::CreateDepthStencilDesc(Format::D24_UNorm_S8_UInt, D3D12_DSV_DIMENSION_TEXTURE2D);
gDSV.InitializeAsDepthStencil(device, gDepthStencil.GetResource(), &dsvDesc, &gDSVHeap, 0);
gCommandAllocator.Initialize(device, CommandQueueType::Direct);
gCommandList.Initialize(device, CommandQueueType::Direct, gCommandAllocator.GetCommandAllocator());

View File

@@ -18,8 +18,7 @@
#include "XCEngine/RHI/D3D12/D3D12SwapChain.h"
#include "XCEngine/RHI/D3D12/D3D12Buffer.h"
#include "XCEngine/RHI/D3D12/D3D12Texture.h"
#include "XCEngine/RHI/D3D12/D3D12RenderTargetView.h"
#include "XCEngine/RHI/D3D12/D3D12DepthStencilView.h"
#include "XCEngine/RHI/D3D12/D3D12ResourceView.h"
#include "XCEngine/RHI/D3D12/D3D12Shader.h"
#include "XCEngine/RHI/D3D12/D3D12RootSignature.h"
#include "XCEngine/RHI/D3D12/D3D12PipelineState.h"
@@ -48,8 +47,8 @@ D3D12CommandList gCommandList;
D3D12Texture gDepthStencil;
D3D12DescriptorHeap gRTVHeap;
D3D12DescriptorHeap gDSVHeap;
D3D12RenderTargetView gRTVs[2];
D3D12DepthStencilView gDSV;
D3D12ResourceView gRTVs[2];
D3D12ResourceView gDSV;
D3D12Shader gVertexShader;
D3D12Shader gPixelShader;
@@ -120,15 +119,12 @@ bool InitD3D12() {
for (int i = 0; i < 2; i++) {
D3D12Texture& backBuffer = gSwapChain.GetBackBuffer(i);
CPUDescriptorHandle rtvCpuHandle = gRTVHeap.GetCPUDescriptorHandle(i);
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = { rtvCpuHandle.ptr };
gRTVs[i].InitializeAt(device, backBuffer.GetResource(), rtvHandle, nullptr);
D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = D3D12ResourceView::CreateRenderTargetDesc(Format::R8G8B8A8_UNorm, D3D12_RTV_DIMENSION_TEXTURE2D);
gRTVs[i].InitializeAsRenderTarget(device, backBuffer.GetResource(), &rtvDesc, &gRTVHeap, i);
}
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12DepthStencilView::CreateDesc(Format::D24_UNorm_S8_UInt);
CPUDescriptorHandle dsvCpuHandle = gDSVHeap.GetCPUDescriptorHandle(0);
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = { dsvCpuHandle.ptr };
gDSV.InitializeAt(device, gDepthStencil.GetResource(), dsvHandle, &dsvDesc);
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12ResourceView::CreateDepthStencilDesc(Format::D24_UNorm_S8_UInt, D3D12_DSV_DIMENSION_TEXTURE2D);
gDSV.InitializeAsDepthStencil(device, gDepthStencil.GetResource(), &dsvDesc, &gDSVHeap, 0);
gCommandAllocator.Initialize(device, CommandQueueType::Direct);
gCommandList.Initialize(device, CommandQueueType::Direct, gCommandAllocator.GetCommandAllocator());

View File

@@ -19,11 +19,11 @@ TEST_P(RHITestFixture, CommandQueue_ExecuteCommandLists) {
ASSERT_NE(cmdList, nullptr);
cmdList->Reset();
cmdList->Close();
void* cmdLists[] = { cmdList };
queue->ExecuteCommandLists(1, cmdLists);
cmdList->Close();
cmdList->Shutdown();
delete cmdList;
queue->Shutdown();