Fix RHI swap chain queue binding and restore minimal GT checks

This commit is contained in:
2026-03-25 21:50:57 +08:00
parent 8f76564ded
commit 30b5f93157
13 changed files with 127 additions and 74 deletions

View File

@@ -65,7 +65,7 @@ public:
RHIBuffer* CreateBuffer(const BufferDesc& desc) override;
RHITexture* CreateTexture(const TextureDesc& desc) override;
RHISwapChain* CreateSwapChain(const SwapChainDesc& desc) override;
RHISwapChain* CreateSwapChain(const SwapChainDesc& desc, RHICommandQueue* presentQueue) override;
RHICommandList* CreateCommandList(const CommandListDesc& desc) override;
RHICommandQueue* CreateCommandQueue(const CommandQueueDesc& desc) override;
RHIShader* CreateShader(const ShaderCompileDesc& desc) override;
@@ -114,8 +114,6 @@ private:
ComPtr<ID3D12Device> m_device;
ComPtr<IDXGIFactory4> m_factory;
ComPtr<IDXGIAdapter1> m_adapter;
ComPtr<ID3D12CommandQueue> m_commandQueue;
AdapterInfo m_adapterInfo;
RHICapabilities m_capabilities;
RHIDeviceInfo m_deviceInfo;

View File

@@ -36,7 +36,7 @@ public:
RHIBuffer* CreateBuffer(const BufferDesc& desc) override;
RHITexture* CreateTexture(const TextureDesc& desc) override;
RHISwapChain* CreateSwapChain(const SwapChainDesc& desc) override;
RHISwapChain* CreateSwapChain(const SwapChainDesc& desc, RHICommandQueue* presentQueue) override;
RHICommandList* CreateCommandList(const CommandListDesc& desc) override;
RHICommandQueue* CreateCommandQueue(const CommandQueueDesc& desc) override;
RHIShader* CreateShader(const ShaderCompileDesc& desc) override;
@@ -90,4 +90,4 @@ private:
};
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -34,7 +34,7 @@ public:
virtual RHIBuffer* CreateBuffer(const BufferDesc& desc) = 0;
virtual RHITexture* CreateTexture(const TextureDesc& desc) = 0;
virtual RHISwapChain* CreateSwapChain(const SwapChainDesc& desc) = 0;
virtual RHISwapChain* CreateSwapChain(const SwapChainDesc& desc, RHICommandQueue* presentQueue) = 0;
virtual RHICommandList* CreateCommandList(const CommandListDesc& desc) = 0;
virtual RHICommandQueue* CreateCommandQueue(const CommandQueueDesc& desc) = 0;
virtual RHIShader* CreateShader(const ShaderCompileDesc& desc) = 0;
@@ -69,4 +69,4 @@ public:
};
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -78,24 +78,12 @@ bool D3D12Device::Initialize(const RHIDeviceDesc& desc) {
return false;
}
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
queueDesc.Priority = 0;
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
queueDesc.NodeMask = 0;
if (FAILED(m_device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&m_commandQueue)))) {
return false;
}
QueryAdapterInfo();
m_initialized = true;
return true;
}
void D3D12Device::Shutdown() {
if (m_commandQueue) {
m_commandQueue.Reset();
}
if (m_device) {
m_device.Reset();
}
@@ -398,10 +386,19 @@ RHIFence* D3D12Device::CreateFence(const FenceDesc& desc) {
return nullptr;
}
RHISwapChain* D3D12Device::CreateSwapChain(const SwapChainDesc& desc) {
RHISwapChain* D3D12Device::CreateSwapChain(const SwapChainDesc& desc, RHICommandQueue* presentQueue) {
if (presentQueue == nullptr) {
return nullptr;
}
auto* nativeQueue = static_cast<ID3D12CommandQueue*>(presentQueue->GetNativeHandle());
if (nativeQueue == nullptr) {
return nullptr;
}
auto* swapChain = new D3D12SwapChain();
HWND hwnd = static_cast<HWND>(desc.windowHandle);
if (swapChain->Initialize(m_factory.Get(), m_commandQueue.Get(), hwnd,
if (swapChain->Initialize(m_factory.Get(), nativeQueue, hwnd,
desc.width, desc.height, desc.bufferCount)) {
return swapChain;
}

View File

@@ -334,13 +334,19 @@ RHITexture* OpenGLDevice::CreateTexture(const TextureDesc& desc) {
return texture;
}
RHISwapChain* OpenGLDevice::CreateSwapChain(const SwapChainDesc& desc) {
RHISwapChain* OpenGLDevice::CreateSwapChain(const SwapChainDesc& desc, RHICommandQueue* presentQueue) {
if (presentQueue == nullptr) {
return nullptr;
}
auto* swapChain = new OpenGLSwapChain();
HWND hwnd = static_cast<HWND>(desc.windowHandle);
if (hwnd) {
swapChain->Initialize(this, hwnd, desc.width, desc.height);
if (hwnd && swapChain->Initialize(this, hwnd, desc.width, desc.height)) {
return swapChain;
}
return swapChain;
delete swapChain;
return nullptr;
}
RHICommandList* OpenGLDevice::CreateCommandList(const CommandListDesc& desc) {
@@ -579,4 +585,4 @@ void* OpenGLDevice::GetNativeHandle() const {
}
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -21,6 +21,29 @@ namespace XCEngine {
namespace RHI {
namespace Integration {
namespace {
std::filesystem::path GetExecutableDirectory() {
char exePath[MAX_PATH] = {};
DWORD length = GetModuleFileNameA(nullptr, exePath, MAX_PATH);
if (length == 0 || length >= MAX_PATH) {
return std::filesystem::current_path();
}
return std::filesystem::path(exePath).parent_path();
}
std::filesystem::path ResolveRuntimePath(const char* path) {
std::filesystem::path resolved(path);
if (resolved.is_absolute()) {
return resolved;
}
return GetExecutableDirectory() / resolved;
}
} // namespace
void Log(const char* format, ...) {
char buffer[1024];
va_list args;
@@ -71,19 +94,19 @@ void RHIIntegrationFixture::SetUp() {
}
ASSERT_TRUE(initResult);
CommandQueueDesc queueDesc = {};
queueDesc.queueType = static_cast<uint32_t>(CommandQueueType::Direct);
mCommandQueue = mDevice->CreateCommandQueue(queueDesc);
ASSERT_NE(mCommandQueue, nullptr);
SwapChainDesc swapDesc = {};
swapDesc.windowHandle = mWindow;
swapDesc.width = width;
swapDesc.height = height;
swapDesc.bufferCount = 2;
mSwapChain = mDevice->CreateSwapChain(swapDesc);
mSwapChain = mDevice->CreateSwapChain(swapDesc, mCommandQueue);
ASSERT_NE(mSwapChain, nullptr);
CommandQueueDesc queueDesc = {};
queueDesc.queueType = static_cast<uint32_t>(CommandQueueType::Direct);
mCommandQueue = mDevice->CreateCommandQueue(queueDesc);
ASSERT_NE(mCommandQueue, nullptr);
CommandListDesc cmdDesc = {};
cmdDesc.commandListType = static_cast<uint32_t>(CommandQueueType::Direct);
mCommandList = mDevice->CreateCommandList(cmdDesc);
@@ -112,11 +135,12 @@ void RHIIntegrationFixture::SetUp() {
mRTVs.push_back(rtv);
}
D3D12Texture depthStencil;
depthStencil.InitializeDepthStencil(device, width, height);
mDepthStencilTexture = new D3D12Texture();
ASSERT_NE(mDepthStencilTexture, nullptr);
ASSERT_TRUE(mDepthStencilTexture->InitializeDepthStencil(device, width, height));
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12ResourceView::CreateDepthStencilDesc(Format::D24_UNorm_S8_UInt, D3D12_DSV_DIMENSION_TEXTURE2D);
auto* d3d12DSV = new D3D12ResourceView();
d3d12DSV->InitializeAsDepthStencil(device, depthStencil.GetResource(), &dsvDesc, mDSVHeap, 0);
d3d12DSV->InitializeAsDepthStencil(device, mDepthStencilTexture->GetResource(), &dsvDesc, mDSVHeap, 0);
mDSV = d3d12DSV;
}
@@ -178,6 +202,12 @@ void RHIIntegrationFixture::TearDown() {
mDSV = nullptr;
}
if (mDepthStencilTexture) {
mDepthStencilTexture->Shutdown();
delete mDepthStencilTexture;
mDepthStencilTexture = nullptr;
}
if (mRTVHeap) {
mRTVHeap->Shutdown();
delete mRTVHeap;
@@ -255,8 +285,11 @@ bool RHIIntegrationFixture::TakeScreenshot(const char* filename) {
(void*)mScreenshot, (void*)mDevice, (void*)mSwapChain);
return false;
}
Log("[TEST] TakeScreenshot: capturing to %s", filename);
bool result = mScreenshot->Capture(mDevice, mSwapChain, filename);
const std::filesystem::path outputPath = ResolveRuntimePath(filename);
const std::string outputPathString = outputPath.string();
Log("[TEST] TakeScreenshot: capturing to %s", outputPathString.c_str());
bool result = mScreenshot->Capture(mDevice, mSwapChain, outputPathString.c_str());
Log("[TEST] TakeScreenshot: result=%d", result);
return result;
}
@@ -264,9 +297,10 @@ bool RHIIntegrationFixture::TakeScreenshot(const char* filename) {
bool RHIIntegrationFixture::CompareWithGoldenTemplate(const char* outputPpm, const char* gtPpm, float threshold) {
namespace fs = std::filesystem;
fs::path exeDir = fs::current_path();
fs::path outputPath = exeDir / outputPpm;
fs::path gtPath = exeDir / gtPpm;
fs::path exeDir = GetExecutableDirectory();
fs::path outputPath = ResolveRuntimePath(outputPpm);
fs::path gtPath = ResolveRuntimePath(gtPpm);
fs::path compareScriptPath = exeDir / "compare_ppm.py";
if (!fs::exists(outputPath)) {
std::cerr << "Output file not found: " << outputPath << std::endl;
@@ -278,7 +312,12 @@ bool RHIIntegrationFixture::CompareWithGoldenTemplate(const char* outputPpm, con
return false;
}
std::string cmd = "python compare_ppm.py \"" + outputPath.string() + "\" \"" + gtPath.string() + "\" " + std::to_string(static_cast<int>(threshold));
if (!fs::exists(compareScriptPath)) {
std::cerr << "Compare script not found: " << compareScriptPath << std::endl;
return false;
}
std::string cmd = "python \"" + compareScriptPath.string() + "\" \"" + outputPath.string() + "\" \"" + gtPath.string() + "\" " + std::to_string(static_cast<int>(threshold));
int result = system(cmd.c_str());
return result == 0;
}

View File

@@ -66,6 +66,7 @@ private:
#if defined(XCENGINE_SUPPORT_D3D12)
D3D12DescriptorHeap* mRTVHeap = nullptr;
D3D12DescriptorHeap* mDSVHeap = nullptr;
D3D12Texture* mDepthStencilTexture = nullptr;
std::vector<RHIResourceView*> mRTVs;
RHIResourceView* mDSV = nullptr;
#endif
@@ -73,4 +74,4 @@ private:
} // namespace Integration
} // namespace RHI
} // namespace XCEngine
} // namespace XCEngine

View File

@@ -46,14 +46,11 @@ add_custom_command(TARGET rhi_integration_minimal POST_BUILD
$<TARGET_FILE_DIR:rhi_integration_minimal>/
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/tests/RHI/D3D12/integration/minimal/GT.ppm
$<TARGET_FILE_DIR:rhi_integration_minimal>/GT_D3D12.ppm
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/tests/RHI/OpenGL/integration/minimal/GT.ppm
$<TARGET_FILE_DIR:rhi_integration_minimal>/GT_OpenGL.ppm
$<TARGET_FILE_DIR:rhi_integration_minimal>/GT.ppm
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ENGINE_ROOT_DIR}/third_party/renderdoc/renderdoc.dll
$<TARGET_FILE_DIR:rhi_integration_minimal>/
)
include(GoogleTest)
gtest_discover_tests(rhi_integration_minimal)
gtest_discover_tests(rhi_integration_minimal)

View File

@@ -19,6 +19,14 @@ protected:
void RenderFrame() override;
};
const char* GetScreenshotFilename(RHIType type) {
return type == RHIType::D3D12 ? "minimal_d3d12.ppm" : "minimal_opengl.ppm";
}
int GetComparisonThreshold(RHIType type) {
return type == RHIType::OpenGL ? 5 : 0;
}
void MinimalTest::RenderFrame() {
RHICommandList* cmdList = GetCommandList();
RHICommandQueue* cmdQueue = GetCommandQueue();
@@ -55,6 +63,8 @@ TEST_P(MinimalTest, RenderClear) {
RHICommandQueue* cmdQueue = GetCommandQueue();
RHISwapChain* swapChain = GetSwapChain();
const int targetFrameCount = 30;
const char* screenshotFilename = GetScreenshotFilename(GetBackendType());
const int comparisonThreshold = GetComparisonThreshold(GetBackendType());
for (int frameCount = 0; frameCount <= targetFrameCount; ++frameCount) {
if (frameCount > 0) {
@@ -67,8 +77,10 @@ TEST_P(MinimalTest, RenderClear) {
if (frameCount >= targetFrameCount) {
cmdQueue->WaitForIdle();
Log("[TEST] MainLoop: frame %d reached, test complete", frameCount);
// Screenshot temporarily disabled due to device state issue
Log("[TEST] MainLoop: frame %d reached, capturing %s", frameCount, screenshotFilename);
ASSERT_TRUE(TakeScreenshot(screenshotFilename));
ASSERT_TRUE(CompareWithGoldenTemplate(screenshotFilename, "GT.ppm", static_cast<float>(comparisonThreshold)));
Log("[TEST] MainLoop: frame %d compare passed", frameCount);
break;
}

View File

@@ -41,25 +41,27 @@ void RHITestFixture::SetUp() {
desc.enableDebugLayer = false;
initResult = mDevice->Initialize(desc);
ASSERT_TRUE(initResult);
} else if (GetParam() == RHIType::OpenGL) {
auto* oglDevice = static_cast<OpenGLDevice*>(mDevice);
initResult = oglDevice->InitializeWithExistingWindow(mWindow);
ASSERT_TRUE(initResult);
}
CommandQueueDesc queueDesc = {};
queueDesc.queueType = static_cast<uint32_t>(CommandQueueType::Direct);
queueDesc.priority = 0;
queueDesc.nodeMask = 0;
queueDesc.flags = 0;
mCommandQueue = mDevice->CreateCommandQueue(queueDesc);
ASSERT_NE(mCommandQueue, nullptr);
CommandQueueDesc queueDesc = {};
queueDesc.queueType = static_cast<uint32_t>(CommandQueueType::Direct);
queueDesc.priority = 0;
queueDesc.nodeMask = 0;
queueDesc.flags = 0;
mCommandQueue = mDevice->CreateCommandQueue(queueDesc);
ASSERT_NE(mCommandQueue, nullptr);
if (GetParam() == RHIType::D3D12) {
FenceDesc fenceDesc = {};
fenceDesc.initialValue = 0;
fenceDesc.flags = 0;
mFence = mDevice->CreateFence(fenceDesc);
ASSERT_NE(mFence, nullptr);
mFenceValue = 0;
} else if (GetParam() == RHIType::OpenGL) {
auto* oglDevice = static_cast<OpenGLDevice*>(mDevice);
initResult = oglDevice->InitializeWithExistingWindow(mWindow);
ASSERT_TRUE(initResult);
}
}

View File

@@ -31,6 +31,7 @@ protected:
void WaitForGPU();
RHIDevice* GetDevice() { return mDevice; }
RHICommandQueue* GetCommandQueue() { return mCommandQueue; }
RHIType GetBackendType() const { return GetParam(); }
HWND GetWindowHandle() const { return mWindow; }

View File

@@ -20,7 +20,7 @@ TEST_P(RHITestFixture, Screenshot_Capture_Basic) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
if (swapChain == nullptr) {
return;
}
@@ -44,7 +44,7 @@ TEST_P(RHITestFixture, Screenshot_Capture_WithPresent) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
if (swapChain == nullptr) {
return;
}
@@ -60,4 +60,4 @@ TEST_P(RHITestFixture, Screenshot_Capture_WithPresent) {
swapChain->Shutdown();
delete swapChain;
}
}

View File

@@ -11,7 +11,7 @@ TEST_P(RHITestFixture, SwapChain_Create) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
swapChain->Shutdown();
@@ -26,7 +26,7 @@ TEST_P(RHITestFixture, SwapChain_GetCurrentBackBufferIndex) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
uint32_t index = swapChain->GetCurrentBackBufferIndex();
@@ -44,7 +44,7 @@ TEST_P(RHITestFixture, SwapChain_GetCurrentBackBuffer) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
RHITexture* backBuffer = swapChain->GetCurrentBackBuffer();
@@ -62,7 +62,7 @@ TEST_P(RHITestFixture, SwapChain_Resize) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
swapChain->Resize(1024, 768);
@@ -79,7 +79,7 @@ TEST_P(RHITestFixture, SwapChain_Present_Basic) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
swapChain->Present(0, 0);
@@ -96,7 +96,7 @@ TEST_P(RHITestFixture, SwapChain_Present_WithSyncInterval) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
swapChain->Present(1, 0);
@@ -114,7 +114,7 @@ TEST_P(RHITestFixture, SwapChain_Present_Multiple) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
for (int i = 0; i < 10; ++i) {
@@ -133,7 +133,7 @@ TEST_P(RHITestFixture, SwapChain_GetNativeHandle) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
void* handle = swapChain->GetNativeHandle();
@@ -151,7 +151,7 @@ TEST_P(RHITestFixture, SwapChain_Resize_WithPresent) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
swapChain->Present(0, 0);
@@ -170,7 +170,7 @@ TEST_P(RHITestFixture, SwapChain_TripleBuffering) {
desc.bufferCount = 3;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
uint32_t index0 = swapChain->GetCurrentBackBufferIndex();
@@ -193,7 +193,7 @@ TEST_P(RHITestFixture, SwapChain_DoubleShutdown) {
desc.bufferCount = 2;
desc.format = Format::R8G8B8A8_UNorm;
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc);
RHISwapChain* swapChain = GetDevice()->CreateSwapChain(desc, GetCommandQueue());
ASSERT_NE(swapChain, nullptr);
swapChain->Shutdown();