OpenGL: Refactor integration test and enable CTest framework
- Simplify OpenGL integration test structure - Enable CTest registration for OpenGL tests - Refactor test fixtures and device enumeration - Minor code cleanup and improvements
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
|
||||
#include <stdio.h>
|
||||
#include <glad/glad.h>
|
||||
#include <windows.h>
|
||||
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLBuffer.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLTexture.h"
|
||||
@@ -9,16 +15,20 @@
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLCommandList.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLCommandQueue.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLSwapChain.h"
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
static bool s_windowClassRegistered = false;
|
||||
static const wchar_t kWindowClassName[] = L"XCEngine_OpenGL_WindowClass";
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
|
||||
OpenGLDevice::OpenGLDevice()
|
||||
: m_window(nullptr)
|
||||
: m_hwnd(nullptr)
|
||||
, m_hdc(nullptr)
|
||||
, m_hglrc(nullptr)
|
||||
, m_initialized(false)
|
||||
, m_ownsWindow(false) {
|
||||
, m_ownsWindow(false)
|
||||
, m_shouldClose(false) {
|
||||
}
|
||||
|
||||
OpenGLDevice::~OpenGLDevice() {
|
||||
@@ -31,7 +41,7 @@ bool OpenGLDevice::Initialize(const RHIDeviceDesc& desc) {
|
||||
}
|
||||
|
||||
if (desc.windowHandle) {
|
||||
return InitializeWithExistingWindow(static_cast<GLFWwindow*>(desc.windowHandle));
|
||||
return InitializeWithExistingWindow(static_cast<HWND>(desc.windowHandle));
|
||||
}
|
||||
|
||||
std::string titleStr = "XCEngine";
|
||||
@@ -47,42 +57,121 @@ bool OpenGLDevice::CreateRenderWindow(int width, int height, const char* title,
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool glfwInitialized = false;
|
||||
if (!glfwInitialized) {
|
||||
glfwInit();
|
||||
glfwInitialized = 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;
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
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
|
||||
);
|
||||
|
||||
m_window = glfwCreateWindow(width, height, title, nullptr, nullptr);
|
||||
if (!m_window) {
|
||||
if (!hwnd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
glfwSetWindowShouldClose(m_window, GLFW_FALSE);
|
||||
|
||||
m_hwnd = hwnd;
|
||||
m_ownsWindow = true;
|
||||
return InitializeWithExistingWindow(m_window);
|
||||
|
||||
ShowWindow(m_hwnd, SW_SHOWNORMAL);
|
||||
UpdateWindow(m_hwnd);
|
||||
|
||||
return InitializeWithExistingWindow(m_hwnd);
|
||||
}
|
||||
|
||||
bool OpenGLDevice::InitializeWithExistingWindow(GLFWwindow* window) {
|
||||
bool OpenGLDevice::InitializeWithExistingWindow(HWND hwnd) {
|
||||
if (m_initialized) {
|
||||
return true;
|
||||
}
|
||||
|
||||
m_window = window;
|
||||
if (!m_window) {
|
||||
if (!hwnd) {
|
||||
fprintf(stderr, "[OpenGLDevice] ERROR: hwnd is null\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(m_window);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
m_hwnd = hwnd;
|
||||
m_hdc = ::GetDC(m_hwnd);
|
||||
if (!m_hdc) {
|
||||
fprintf(stderr, "[OpenGLDevice] ERROR: GetDC failed\n");
|
||||
return false;
|
||||
}
|
||||
fprintf(stderr, "[OpenGLDevice] GetDC succeeded\n");
|
||||
|
||||
PIXELFORMATDESCRIPTOR pfd = {};
|
||||
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
|
||||
pfd.nVersion = 1;
|
||||
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
||||
pfd.iPixelType = PFD_TYPE_RGBA;
|
||||
pfd.cColorBits = 32;
|
||||
pfd.cDepthBits = 24;
|
||||
pfd.cStencilBits = 8;
|
||||
pfd.iLayerType = PFD_MAIN_PLANE;
|
||||
|
||||
int pixelFormat = ChoosePixelFormat(m_hdc, &pfd);
|
||||
if (!pixelFormat) {
|
||||
fprintf(stderr, "[OpenGLDevice] ERROR: ChoosePixelFormat failed\n");
|
||||
ReleaseDC(m_hwnd, m_hdc);
|
||||
m_hdc = nullptr;
|
||||
return false;
|
||||
}
|
||||
fprintf(stderr, "[OpenGLDevice] ChoosePixelFormat succeeded\n");
|
||||
|
||||
if (!SetPixelFormat(m_hdc, pixelFormat, &pfd)) {
|
||||
fprintf(stderr, "[OpenGLDevice] ERROR: SetPixelFormat failed\n");
|
||||
ReleaseDC(m_hwnd, m_hdc);
|
||||
m_hdc = nullptr;
|
||||
return false;
|
||||
}
|
||||
fprintf(stderr, "[OpenGLDevice] SetPixelFormat succeeded\n");
|
||||
|
||||
m_hglrc = wglCreateContext(m_hdc);
|
||||
if (!m_hglrc) {
|
||||
fprintf(stderr, "[OpenGLDevice] ERROR: wglCreateContext failed\n");
|
||||
ReleaseDC(m_hwnd, m_hdc);
|
||||
m_hdc = nullptr;
|
||||
return false;
|
||||
}
|
||||
fprintf(stderr, "[OpenGLDevice] wglCreateContext succeeded\n");
|
||||
|
||||
if (!wglMakeCurrent(m_hdc, m_hglrc)) {
|
||||
fprintf(stderr, "[OpenGLDevice] ERROR: wglMakeCurrent failed\n");
|
||||
wglDeleteContext(m_hglrc);
|
||||
ReleaseDC(m_hwnd, m_hdc);
|
||||
m_hglrc = nullptr;
|
||||
m_hdc = nullptr;
|
||||
return false;
|
||||
}
|
||||
fprintf(stderr, "[OpenGLDevice] wglMakeCurrent succeeded\n");
|
||||
|
||||
if (!gladLoadGL()) {
|
||||
fprintf(stderr, "[OpenGLDevice] ERROR: gladLoadGL failed\n");
|
||||
wglMakeCurrent(nullptr, nullptr);
|
||||
wglDeleteContext(m_hglrc);
|
||||
ReleaseDC(m_hwnd, m_hdc);
|
||||
m_hglrc = nullptr;
|
||||
m_hdc = nullptr;
|
||||
return false;
|
||||
}
|
||||
fprintf(stderr, "[OpenGLDevice] gladLoadGL succeeded\n");
|
||||
|
||||
const char* vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
|
||||
const char* renderer = reinterpret_cast<const char*>(glGetString(GL_RENDERER));
|
||||
@@ -135,33 +224,55 @@ bool OpenGLDevice::InitializeWithExistingWindow(GLFWwindow* window) {
|
||||
}
|
||||
|
||||
void OpenGLDevice::Shutdown() {
|
||||
if (m_ownsWindow && m_window) {
|
||||
glfwDestroyWindow(m_window);
|
||||
if (m_hglrc) {
|
||||
wglMakeCurrent(nullptr, nullptr);
|
||||
wglDeleteContext(m_hglrc);
|
||||
m_hglrc = nullptr;
|
||||
}
|
||||
m_window = nullptr;
|
||||
|
||||
if (m_hdc && m_hwnd) {
|
||||
ReleaseDC(m_hwnd, m_hdc);
|
||||
m_hdc = nullptr;
|
||||
}
|
||||
|
||||
if (m_ownsWindow && m_hwnd) {
|
||||
DestroyWindow(m_hwnd);
|
||||
m_hwnd = nullptr;
|
||||
}
|
||||
|
||||
m_initialized = false;
|
||||
m_ownsWindow = false;
|
||||
m_shouldClose = false;
|
||||
}
|
||||
|
||||
void OpenGLDevice::SwapBuffers() {
|
||||
if (m_window) {
|
||||
glfwSwapBuffers(m_window);
|
||||
if (m_hdc) {
|
||||
::SwapBuffers(m_hdc);
|
||||
}
|
||||
}
|
||||
|
||||
bool OpenGLDevice::PollEvents() {
|
||||
glfwPollEvents();
|
||||
return !glfwWindowShouldClose(m_window);
|
||||
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) {
|
||||
if (m_window) {
|
||||
glfwSetWindowShouldClose(m_window, shouldClose ? GLFW_TRUE : GLFW_FALSE);
|
||||
m_shouldClose = shouldClose;
|
||||
if (m_hwnd && shouldClose) {
|
||||
PostMessageW(m_hwnd, WM_CLOSE, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
bool OpenGLDevice::ShouldClose() const {
|
||||
return m_window && glfwWindowShouldClose(m_window) == GLFW_TRUE;
|
||||
return m_shouldClose;
|
||||
}
|
||||
|
||||
RHIBuffer* OpenGLDevice::CreateBuffer(const BufferDesc& desc) {
|
||||
@@ -169,10 +280,10 @@ RHIBuffer* OpenGLDevice::CreateBuffer(const BufferDesc& desc) {
|
||||
OpenGLBufferType bufferType = OpenGLBufferType::Vertex;
|
||||
|
||||
switch (desc.bufferType) {
|
||||
case 1: // Index buffer
|
||||
case 1:
|
||||
bufferType = OpenGLBufferType::Index;
|
||||
break;
|
||||
case 2: // Constant buffer
|
||||
case 2:
|
||||
bufferType = OpenGLBufferType::Uniform;
|
||||
break;
|
||||
default:
|
||||
@@ -210,8 +321,8 @@ RHITexture* OpenGLDevice::CreateTexture(const TextureDesc& desc) {
|
||||
|
||||
RHISwapChain* OpenGLDevice::CreateSwapChain(const SwapChainDesc& desc) {
|
||||
auto* swapChain = new OpenGLSwapChain();
|
||||
if (m_window) {
|
||||
swapChain->Initialize(m_window, desc.width, desc.height);
|
||||
if (m_hwnd) {
|
||||
swapChain->Initialize(m_hwnd, desc.width, desc.height);
|
||||
}
|
||||
return swapChain;
|
||||
}
|
||||
@@ -272,4 +383,4 @@ void* OpenGLDevice::GetNativeHandle() const {
|
||||
}
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user