Files
XCEngine/tests/RHI/D3D12/fixtures/D3D12TestFixture.cpp
ssdfasd 11ea2a4fc5 Fix GPU state issue - make device non-static per test
- Each test now creates its own D3D12 device, command queue, allocator, and command list
- Properly cleanup in TearDown to avoid GPU state issues
- All 29 tests now pass
2026-03-17 03:54:50 +08:00

72 lines
2.0 KiB
C++

#include "D3D12TestFixture.h"
void D3D12TestFixture::SetUpTestSuite() {
}
void D3D12TestFixture::TearDownTestSuite() {
}
void D3D12TestFixture::SetUp() {
HRESULT hr = D3D12CreateDevice(
nullptr,
D3D_FEATURE_LEVEL_12_0,
IID_PPV_ARGS(&mDevice)
);
if (FAILED(hr)) {
GTEST_SKIP() << "Failed to create D3D12 device";
return;
}
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
hr = mDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&mCommandQueue));
if (FAILED(hr)) {
GTEST_SKIP() << "Failed to create command queue";
return;
}
hr = mDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&mCommandAllocator));
if (FAILED(hr)) {
GTEST_SKIP() << "Failed to create command allocator";
return;
}
hr = mDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, mCommandAllocator.Get(), nullptr, IID_PPV_ARGS(&mCommandList));
if (FAILED(hr)) {
GTEST_SKIP() << "Failed to create command list";
return;
}
}
void D3D12TestFixture::TearDown() {
if (mCommandQueue) {
WaitForGPU();
}
mCommandList.Reset();
mCommandAllocator.Reset();
mCommandQueue.Reset();
mDevice.Reset();
}
void D3D12TestFixture::WaitForGPU() {
if (!mCommandQueue || !mDevice) return;
ComPtr<ID3D12Fence> fence;
UINT64 fenceValue = 1;
HRESULT hr = mDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence));
if (SUCCEEDED(hr)) {
mCommandQueue->Signal(fence.Get(), fenceValue);
if (fence->GetCompletedValue() < fenceValue) {
HANDLE eventHandle = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (eventHandle) {
fence->SetEventOnCompletion(fenceValue, eventHandle);
WaitForSingleObject(eventHandle, INFINITE);
CloseHandle(eventHandle);
}
}
}
}