refactor(RHI): 将窗口职责从RHI移到Platform层

- RHIDeviceDesc 删除 windowHandle/width/height/appName
- SwapChainDesc 添加 windowHandle 字段
- RHISwapChain 删除 PollEvents/ShouldClose/SetFullscreen 等窗口相关接口
- OpenGLDevice 删除 CreateRenderWindow,改用 InitializeWithExistingWindow
- 更新所有集成测试使用新API
- 273个单元测试 + 8个集成测试全部通过
This commit is contained in:
2026-03-24 23:00:49 +08:00
parent 9fae910854
commit 7a66913f2b
22 changed files with 66 additions and 379 deletions

View File

@@ -335,7 +335,7 @@ RHIFence* D3D12Device::CreateFence(const FenceDesc& desc) {
RHISwapChain* D3D12Device::CreateSwapChain(const SwapChainDesc& desc) {
auto* swapChain = new D3D12SwapChain();
HWND hwnd = static_cast<HWND>(m_deviceDesc.windowHandle);
HWND hwnd = static_cast<HWND>(desc.windowHandle);
if (swapChain->Initialize(m_factory.Get(), m_commandQueue.Get(), hwnd,
desc.width, desc.height, desc.bufferCount)) {
return swapChain;

View File

@@ -5,8 +5,7 @@ namespace XCEngine {
namespace RHI {
D3D12SwapChain::D3D12SwapChain()
: m_windowHandle(nullptr)
, m_width(0)
: m_width(0)
, m_height(0)
, m_bufferCount(2) {
}
@@ -40,7 +39,6 @@ bool D3D12SwapChain::Initialize(IDXGIFactory4* factory, ID3D12CommandQueue* comm
}
m_commandQueue = commandQueue;
m_windowHandle = windowHandle;
m_width = width;
m_height = height;
m_bufferCount = bufferCount;
@@ -102,15 +100,6 @@ void D3D12SwapChain::Resize(uint32_t width, uint32_t height) {
m_height = height;
}
void D3D12SwapChain::SetFullscreen(bool fullscreen) {
m_fullscreen = fullscreen;
m_swapChain->SetFullscreenState(fullscreen, nullptr);
}
bool D3D12SwapChain::IsFullscreen() const {
return m_fullscreen;
}
void* D3D12SwapChain::GetNativeHandle() {
return reinterpret_cast<void*>(m_swapChain.Get());
}
@@ -119,16 +108,5 @@ RHITexture* D3D12SwapChain::GetCurrentBackBuffer() {
return &GetBackBuffer(GetCurrentBackBufferIndex());
}
bool D3D12SwapChain::ShouldClose() const {
return m_shouldClose;
}
void D3D12SwapChain::SetShouldClose(bool shouldClose) {
m_shouldClose = shouldClose;
}
void D3D12SwapChain::PollEvents() {
}
} // namespace RHI
} // namespace XCEngine

View File

@@ -20,9 +20,6 @@
#include "XCEngine/RHI/OpenGL/OpenGLResourceView.h"
#include "XCEngine/Debug/Logger.h"
static bool s_windowClassRegistered = false;
static const wchar_t kWindowClassName[] = L"XCEngine_OpenGL_WindowClass";
typedef const char* (WINAPI* PFNWGLGETEXTENSIONSSTRINGARBPROC)(HDC hdc);
typedef BOOL (WINAPI* PFNWGLCHOOSEPIXELFORMATARBPROC)(HDC hdc, const int* piAttribIList, const FLOAT* pfAttribFList, UINT nMaxFormats, int* piFormats, UINT* nNumFormats);
typedef HGLRC (WINAPI* PFNWGLCREATECONTEXTATTRIBSARBPROC)(HDC hdc, HGLRC hShareContext, const int* attribList);
@@ -45,9 +42,7 @@ OpenGLDevice::OpenGLDevice()
: m_hwnd(nullptr)
, m_hdc(nullptr)
, m_hglrc(nullptr)
, m_initialized(false)
, m_ownsWindow(false)
, m_shouldClose(false) {
, m_initialized(false) {
m_textureUnitAllocator = std::make_unique<OpenGLTextureUnitAllocator>();
m_uniformBufferManager = std::make_unique<OpenGLUniformBufferManager>();
}
@@ -61,16 +56,7 @@ bool OpenGLDevice::Initialize(const RHIDeviceDesc& desc) {
return true;
}
if (desc.windowHandle) {
return InitializeWithExistingWindow(static_cast<HWND>(desc.windowHandle));
}
std::string titleStr = "XCEngine";
if (!desc.appName.empty()) {
titleStr = std::string(desc.appName.begin(), desc.appName.end());
}
return CreateRenderWindow(desc.width, desc.height, titleStr.c_str(), desc.enableDebugLayer);
return false;
}
bool OpenGLDevice::InitializeWithExistingWindow(HWND hwnd) {
@@ -248,52 +234,6 @@ bool OpenGLDevice::InitializeWithExistingWindow(HWND hwnd) {
return true;
}
bool OpenGLDevice::CreateRenderWindow(int width, int height, const char* title, bool enableDebug) {
if (m_initialized) {
return true;
}
if (!s_windowClassRegistered) {
WNDCLASSEXW wc = {};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = DefWindowProcW;
wc.hInstance = GetModuleHandleW(nullptr);
wc.lpszClassName = kWindowClassName;
if (!RegisterClassExW(&wc)) {
return false;
}
s_windowClassRegistered = true;
}
std::wstring titleW(title ? std::wstring(title, title + strlen(title)) : L"XCEngine");
HWND hwnd = CreateWindowExW(
0,
kWindowClassName,
titleW.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
width, height,
nullptr,
nullptr,
GetModuleHandleW(nullptr),
nullptr
);
if (!hwnd) {
return false;
}
m_hwnd = hwnd;
m_ownsWindow = true;
ShowWindow(m_hwnd, SW_SHOWNORMAL);
UpdateWindow(m_hwnd);
return InitializeWithExistingWindow(m_hwnd);
}
void OpenGLDevice::Shutdown() {
if (m_hglrc) {
wglMakeCurrent(nullptr, nullptr);
@@ -304,10 +244,6 @@ void OpenGLDevice::Shutdown() {
if (m_hdc && m_hwnd) {
ReleaseDC(m_hwnd, m_hdc);
m_hdc = nullptr;
}
if (m_ownsWindow && m_hwnd) {
DestroyWindow(m_hwnd);
m_hwnd = nullptr;
}
@@ -319,8 +255,6 @@ void OpenGLDevice::Shutdown() {
}
m_initialized = false;
m_ownsWindow = false;
m_shouldClose = false;
}
void OpenGLDevice::SwapBuffers() {
@@ -329,30 +263,6 @@ void OpenGLDevice::SwapBuffers() {
}
}
bool OpenGLDevice::PollEvents() {
MSG msg;
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
m_shouldClose = true;
return false;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return !m_shouldClose;
}
void OpenGLDevice::SetShouldClose(bool shouldClose) {
m_shouldClose = shouldClose;
if (m_hwnd && shouldClose) {
PostMessageW(m_hwnd, WM_CLOSE, 0, 0);
}
}
bool OpenGLDevice::ShouldClose() const {
return m_shouldClose;
}
RHIBuffer* OpenGLDevice::CreateBuffer(const BufferDesc& desc) {
auto* buffer = new OpenGLBuffer();
OpenGLBufferType bufferType = OpenGLBufferType::Vertex;
@@ -416,8 +326,9 @@ RHITexture* OpenGLDevice::CreateTexture(const TextureDesc& desc) {
RHISwapChain* OpenGLDevice::CreateSwapChain(const SwapChainDesc& desc) {
auto* swapChain = new OpenGLSwapChain();
if (m_hwnd) {
swapChain->Initialize(this, m_hwnd, desc.width, desc.height);
HWND hwnd = static_cast<HWND>(desc.windowHandle);
if (hwnd) {
swapChain->Initialize(this, hwnd, desc.width, desc.height);
}
return swapChain;
}

View File

@@ -6,9 +6,6 @@
#include "XCEngine/RHI/OpenGL/OpenGLSwapChain.h"
typedef void (APIENTRY* PFNWGLSWAPINTERVALEXTPROC)(int);
static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = nullptr;
namespace XCEngine {
namespace RHI {
@@ -17,12 +14,6 @@ OpenGLSwapChain::OpenGLSwapChain()
, m_hwnd(nullptr)
, m_width(0)
, m_height(0)
, m_framebufferWidth(0)
, m_framebufferHeight(0)
, m_vsync(true)
, m_shouldClose(false)
, m_fullscreen(false)
, m_presentMode(PresentMode::VSync)
, m_backBufferTexture(nullptr) {
}
@@ -35,23 +26,6 @@ bool OpenGLSwapChain::Initialize(OpenGLDevice* device, HWND window, int width, i
m_hwnd = window;
m_width = width;
m_height = height;
m_presentMode = PresentMode::VSync;
m_vsync = true;
m_shouldClose = false;
if (!wglSwapIntervalEXT) {
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
}
if (wglSwapIntervalEXT) {
wglSwapIntervalEXT(m_vsync ? 1 : 0);
}
RECT rect = { 0, 0, width, height };
::AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
::SetWindowPos(m_hwnd, nullptr, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE | SWP_NOZORDER);
m_framebufferWidth = width;
m_framebufferHeight = height;
if (!m_backBufferTexture) {
m_backBufferTexture = new OpenGLTexture();
@@ -76,41 +50,6 @@ void OpenGLSwapChain::SwapBuffers() {
}
}
void OpenGLSwapChain::SetVSync(bool enabled) {
m_vsync = enabled;
if (wglSwapIntervalEXT) {
wglSwapIntervalEXT(enabled ? 1 : 0);
}
}
void OpenGLSwapChain::SetFramebufferSize(int width, int height) {
m_framebufferWidth = width;
m_framebufferHeight = height;
}
bool OpenGLSwapChain::ShouldClose() const {
return m_shouldClose;
}
void OpenGLSwapChain::SetShouldClose(bool shouldClose) {
m_shouldClose = shouldClose;
if (m_hwnd && shouldClose) {
PostMessageW(m_hwnd, WM_CLOSE, 0, 0);
}
}
void OpenGLSwapChain::PollEvents() {
MSG msg;
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
m_shouldClose = true;
break;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
void OpenGLSwapChain::Present(uint32_t syncInterval, uint32_t flags) {
if (m_device) {
::SwapBuffers(m_device->GetPresentationDC());
@@ -120,21 +59,6 @@ void OpenGLSwapChain::Present(uint32_t syncInterval, uint32_t flags) {
void OpenGLSwapChain::Resize(uint32_t width, uint32_t height) {
m_width = width;
m_height = height;
if (m_hwnd) {
RECT rect = { 0, 0, width, height };
::AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
::SetWindowPos(m_hwnd, nullptr, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE | SWP_NOZORDER);
}
m_framebufferWidth = width;
m_framebufferHeight = height;
}
void OpenGLSwapChain::SetFullscreen(bool fullscreen) {
m_fullscreen = fullscreen;
}
bool OpenGLSwapChain::IsFullscreen() const {
return m_fullscreen;
}
uint32_t OpenGLSwapChain::GetCurrentBackBufferIndex() const {