Fix RHI D3D12 RTV creation and GetFormat bug

1. Add ALLOW_RENDER_TARGET flag for color textures in CreateTexture
   - This was the root cause of 5 failing RTV-related tests
   - Without this flag, creating RTV caused device removal

2. Add FromD3D12() reverse conversion for Format enum
   - GetFormat() was incorrectly casting DXGI_FORMAT to Format
   - DXGI_FORMAT_R8G8B8A8_UNORM=28 but Format::R8G8B8A8_UNorm=3
   - Added FromD3D12() to properly convert back

3. Update RHITestFixture to pre-create CommandQueue and Fence
   - Prevents potential timing issues with GPU synchronization

4. Update RHITestFixture tests to pass correct format in ResourceViewDesc
   - Previously passed empty desc.format=0 which caused issues

All 234 RHI unit tests now pass (117 D3D12 + 117 OpenGL)
This commit is contained in:
2026-03-25 18:12:50 +08:00
parent b0d0576763
commit b11f59e144
5 changed files with 72 additions and 41 deletions

View File

@@ -158,6 +158,32 @@ inline DXGI_FORMAT ToD3D12(Format format) {
return DXGI_FORMAT_UNKNOWN; return DXGI_FORMAT_UNKNOWN;
} }
inline Format FromD3D12(DXGI_FORMAT format) {
switch (format) {
case DXGI_FORMAT_UNKNOWN: return Format::Unknown;
case DXGI_FORMAT_R8_UNORM: return Format::R8_UNorm;
case DXGI_FORMAT_R8G8_UNORM: return Format::R8G8_UNorm;
case DXGI_FORMAT_R8G8B8A8_UNORM: return Format::R8G8B8A8_UNorm;
case DXGI_FORMAT_R16G16B16A16_FLOAT: return Format::R16G16B16A16_Float;
case DXGI_FORMAT_R32G32B32A32_FLOAT: return Format::R32G32B32A32_Float;
case DXGI_FORMAT_R16_FLOAT: return Format::R16_Float;
case DXGI_FORMAT_R32_FLOAT: return Format::R32_Float;
case DXGI_FORMAT_D16_UNORM: return Format::D16_UNorm;
case DXGI_FORMAT_D24_UNORM_S8_UINT: return Format::D24_UNorm_S8_UInt;
case DXGI_FORMAT_D32_FLOAT: return Format::D32_Float;
case DXGI_FORMAT_BC1_UNORM: return Format::BC1_UNorm;
case DXGI_FORMAT_BC2_UNORM: return Format::BC2_UNorm;
case DXGI_FORMAT_BC3_UNORM: return Format::BC3_UNorm;
case DXGI_FORMAT_BC4_UNORM: return Format::BC4_UNorm;
case DXGI_FORMAT_BC5_UNORM: return Format::BC5_UNorm;
case DXGI_FORMAT_BC6H_UF16: return Format::BC6H_UF16;
case DXGI_FORMAT_BC7_UNORM: return Format::BC7_UNorm;
case DXGI_FORMAT_R32G32B32A32_UINT: return Format::R32G32B32A32_UInt;
case DXGI_FORMAT_R32_UINT: return Format::R32_UInt;
}
return Format::Unknown;
}
inline D3D12_RESOURCE_STATES ToD3D12(ResourceStates state) { inline D3D12_RESOURCE_STATES ToD3D12(ResourceStates state) {
switch (state) { switch (state) {
case ResourceStates::Common: return D3D12_RESOURCE_STATE_COMMON; case ResourceStates::Common: return D3D12_RESOURCE_STATE_COMMON;

View File

@@ -43,7 +43,7 @@ public:
void SetName(const std::string& name) override { m_name = name; } void SetName(const std::string& name) override { m_name = name; }
uint32_t GetArraySize() const { return GetDesc().DepthOrArraySize; } uint32_t GetArraySize() const { return GetDesc().DepthOrArraySize; }
Format GetFormat() const override { return static_cast<Format>(GetDesc().Format); } Format GetFormat() const override { return FromD3D12(GetDesc().Format); }
TextureType GetTextureType() const override { TextureType GetTextureType() const override {
switch (GetDesc().Dimension) { switch (GetDesc().Dimension) {
case D3D12_RESOURCE_DIMENSION_TEXTURE1D: return TextureType::Texture1D; case D3D12_RESOURCE_DIMENSION_TEXTURE1D: return TextureType::Texture1D;

View File

@@ -297,12 +297,14 @@ RHITexture* D3D12Device::CreateTexture(const TextureDesc& desc) {
if (format == Format::D24_UNorm_S8_UInt || format == Format::D32_Float || if (format == Format::D24_UNorm_S8_UInt || format == Format::D32_Float ||
format == Format::D16_UNorm) { format == Format::D16_UNorm) {
d3d12Desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; d3d12Desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
} else if (d3d12Desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D && desc.width > 0 && desc.height > 0) {
d3d12Desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
} }
d3d12Desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; d3d12Desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
{ {
FILE* f = fopen("D:\\Xuanchi\\Main\\XCEngine\\debug_rhi.log", "a"); FILE* f = fopen("D:\\Xuanchi\\Main\\XCEngine\\debug_rhi.log", "a");
if (f) { fprintf(f, "[CreateTexture] Calling Initialize, device=%p, format=%d\n", m_device.Get(), (int)d3d12Desc.Format); fclose(f); } if (f) { fprintf(f, "[CreateTexture] Calling Initialize, device=%p, format=%d, flags=0x%X\n", m_device.Get(), (int)d3d12Desc.Format, d3d12Desc.Flags); fclose(f); }
} }
if (texture->Initialize(m_device.Get(), d3d12Desc)) { if (texture->Initialize(m_device.Get(), d3d12Desc)) {
FILE* f = fopen("D:\\Xuanchi\\Main\\XCEngine\\debug_rhi.log", "a"); FILE* f = fopen("D:\\Xuanchi\\Main\\XCEngine\\debug_rhi.log", "a");

View File

@@ -4,6 +4,9 @@
#include <windows.h> #include <windows.h>
#include "XCEngine/RHI/D3D12/D3D12Device.h" #include "XCEngine/RHI/D3D12/D3D12Device.h"
#include "XCEngine/RHI/D3D12/D3D12CommandQueue.h"
#include "XCEngine/RHI/D3D12/D3D12CommandAllocator.h"
#include "XCEngine/RHI/D3D12/D3D12CommandList.h"
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h" #include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
namespace XCEngine { namespace XCEngine {
@@ -37,57 +40,54 @@ void RHITestFixture::SetUp() {
RHIDeviceDesc desc = {}; RHIDeviceDesc desc = {};
desc.enableDebugLayer = false; desc.enableDebugLayer = false;
initResult = mDevice->Initialize(desc); initResult = mDevice->Initialize(desc);
} else if (GetParam() == RHIType::OpenGL) {
auto* oglDevice = static_cast<OpenGLDevice*>(mDevice);
initResult = oglDevice->InitializeWithExistingWindow(mWindow);
}
ASSERT_TRUE(initResult); ASSERT_TRUE(initResult);
}
void RHITestFixture::WaitForGPU() {
if (mDevice == nullptr) {
return;
}
if (GetParam() == RHIType::D3D12) {
auto* device = static_cast<D3D12Device*>(mDevice);
CommandQueueDesc queueDesc = {}; CommandQueueDesc queueDesc = {};
queueDesc.queueType = static_cast<uint32_t>(CommandQueueType::Direct); queueDesc.queueType = static_cast<uint32_t>(CommandQueueType::Direct);
queueDesc.priority = 0; queueDesc.priority = 0;
queueDesc.nodeMask = 0; queueDesc.nodeMask = 0;
queueDesc.flags = 0; queueDesc.flags = 0;
auto* commandQueue = device->CreateCommandQueue(queueDesc); mCommandQueue = mDevice->CreateCommandQueue(queueDesc);
if (commandQueue) { ASSERT_NE(mCommandQueue, nullptr);
FenceDesc fenceDesc = {}; FenceDesc fenceDesc = {};
fenceDesc.initialValue = 0; fenceDesc.initialValue = 0;
fenceDesc.flags = 0; fenceDesc.flags = 0;
auto* fence = mDevice->CreateFence(fenceDesc); mFence = mDevice->CreateFence(fenceDesc);
if (fence) { ASSERT_NE(mFence, nullptr);
commandQueue->Signal(fence, 1); mFenceValue = 0;
fence->Wait(1); } else if (GetParam() == RHIType::OpenGL) {
auto* oglDevice = static_cast<OpenGLDevice*>(mDevice);
for (int i = 0; i < 100; i++) { initResult = oglDevice->InitializeWithExistingWindow(mWindow);
if (fence->GetCompletedValue() >= 1) { ASSERT_TRUE(initResult);
break;
} }
Sleep(10);
} }
fence->Shutdown(); void RHITestFixture::WaitForGPU() {
delete fence; if (mDevice == nullptr || mCommandQueue == nullptr || mFence == nullptr) {
} return;
commandQueue->Shutdown();
delete commandQueue;
} }
Sleep(100); if (GetParam() == RHIType::D3D12) {
mFenceValue++;
mCommandQueue->Signal(mFence, mFenceValue);
mFence->Wait(mFenceValue);
} }
} }
void RHITestFixture::TearDown() { void RHITestFixture::TearDown() {
WaitForGPU(); WaitForGPU();
if (mFence != nullptr) {
mFence->Shutdown();
delete mFence;
mFence = nullptr;
}
if (mCommandQueue != nullptr) {
mCommandQueue->Shutdown();
delete mCommandQueue;
mCommandQueue = nullptr;
}
if (mDevice != nullptr) { if (mDevice != nullptr) {
mDevice->Shutdown(); mDevice->Shutdown();
delete mDevice; delete mDevice;

View File

@@ -37,6 +37,9 @@ protected:
private: private:
RHIDevice* mDevice = nullptr; RHIDevice* mDevice = nullptr;
HWND mWindow = nullptr; HWND mWindow = nullptr;
RHICommandQueue* mCommandQueue = nullptr;
RHIFence* mFence = nullptr;
uint64_t mFenceValue = 0;
}; };
} // namespace RHI } // namespace RHI