2026-03-16 16:07:12 +08:00
|
|
|
#define GLFW_INCLUDE_NONE
|
2026-03-16 17:22:45 +08:00
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
|
2026-03-16 16:07:12 +08:00
|
|
|
#include <glad/glad.h>
|
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace RHI {
|
|
|
|
|
|
|
|
|
|
OpenGLDevice::OpenGLDevice()
|
|
|
|
|
: m_window(nullptr)
|
|
|
|
|
, m_initialized(false)
|
|
|
|
|
, m_ownsWindow(false) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpenGLDevice::~OpenGLDevice() {
|
|
|
|
|
Shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 17:45:01 +08:00
|
|
|
bool OpenGLDevice::Initialize(const RHIDeviceDesc& desc) {
|
|
|
|
|
if (m_initialized) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (desc.windowHandle) {
|
|
|
|
|
return InitializeWithExistingWindow(static_cast<GLFWwindow*>(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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 17:22:45 +08:00
|
|
|
bool OpenGLDevice::CreateRenderWindow(int width, int height, const char* title, bool enableDebug) {
|
2026-03-16 16:07:12 +08:00
|
|
|
if (m_initialized) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 18:06:57 +08:00
|
|
|
static bool glfwInitialized = false;
|
|
|
|
|
if (!glfwInitialized) {
|
|
|
|
|
glfwInit();
|
|
|
|
|
glfwInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 16:07:12 +08:00
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
|
|
|
|
|
|
m_window = glfwCreateWindow(width, height, title, nullptr, nullptr);
|
|
|
|
|
if (!m_window) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 18:06:57 +08:00
|
|
|
glfwSetWindowShouldClose(m_window, GLFW_FALSE);
|
|
|
|
|
|
2026-03-16 16:07:12 +08:00
|
|
|
m_ownsWindow = true;
|
|
|
|
|
return InitializeWithExistingWindow(m_window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenGLDevice::InitializeWithExistingWindow(GLFWwindow* window) {
|
|
|
|
|
if (m_initialized) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_window = window;
|
|
|
|
|
if (!m_window) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glfwMakeContextCurrent(m_window);
|
|
|
|
|
|
|
|
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 19:35:51 +08:00
|
|
|
const char* vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
|
|
|
|
|
const char* renderer = reinterpret_cast<const char*>(glGetString(GL_RENDERER));
|
|
|
|
|
const char* version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
|
|
|
|
|
|
|
|
|
|
m_deviceInfo.vendor = std::wstring(vendor ? vendor : "", vendor ? vendor + strlen(vendor) : nullptr);
|
|
|
|
|
m_deviceInfo.renderer = std::wstring(renderer ? renderer : "", renderer ? renderer + strlen(renderer) : nullptr);
|
|
|
|
|
m_deviceInfo.version = std::wstring(version ? version : "", version ? version + strlen(version) : nullptr);
|
2026-03-16 16:07:12 +08:00
|
|
|
|
2026-03-17 19:44:50 +08:00
|
|
|
GLint majorVersion = 0, minorVersion = 0;
|
|
|
|
|
glGetIntegerv(GL_MAJOR_VERSION, &majorVersion);
|
|
|
|
|
glGetIntegerv(GL_MINOR_VERSION, &minorVersion);
|
|
|
|
|
m_deviceInfo.majorVersion = static_cast<uint32_t>(majorVersion);
|
|
|
|
|
m_deviceInfo.minorVersion = static_cast<uint32_t>(minorVersion);
|
|
|
|
|
|
2026-03-16 16:07:12 +08:00
|
|
|
m_initialized = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDevice::Shutdown() {
|
|
|
|
|
if (m_ownsWindow && m_window) {
|
|
|
|
|
glfwDestroyWindow(m_window);
|
|
|
|
|
}
|
|
|
|
|
m_window = nullptr;
|
|
|
|
|
m_initialized = false;
|
|
|
|
|
m_ownsWindow = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDevice::SwapBuffers() {
|
|
|
|
|
if (m_window) {
|
|
|
|
|
glfwSwapBuffers(m_window);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenGLDevice::PollEvents() {
|
|
|
|
|
glfwPollEvents();
|
|
|
|
|
return !glfwWindowShouldClose(m_window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDevice::SetShouldClose(bool shouldClose) {
|
|
|
|
|
if (m_window) {
|
|
|
|
|
glfwSetWindowShouldClose(m_window, shouldClose ? GLFW_TRUE : GLFW_FALSE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenGLDevice::ShouldClose() const {
|
|
|
|
|
return m_window && glfwWindowShouldClose(m_window) == GLFW_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 17:45:01 +08:00
|
|
|
RHIBuffer* OpenGLDevice::CreateBuffer(const BufferDesc& desc) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RHITexture* OpenGLDevice::CreateTexture(const TextureDesc& desc) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RHISwapChain* OpenGLDevice::CreateSwapChain(const SwapChainDesc& desc) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RHICommandList* OpenGLDevice::CreateCommandList(const CommandListDesc& desc) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RHICommandQueue* OpenGLDevice::CreateCommandQueue(const CommandQueueDesc& desc) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RHIShader* OpenGLDevice::CompileShader(const ShaderCompileDesc& desc) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RHIPipelineState* OpenGLDevice::CreatePipelineState(const PipelineStateDesc& desc) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RHIFence* OpenGLDevice::CreateFence(const FenceDesc& desc) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RHISampler* OpenGLDevice::CreateSampler(const SamplerDesc& desc) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const RHICapabilities& OpenGLDevice::GetCapabilities() const {
|
|
|
|
|
return m_capabilities;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const RHIDeviceInfo& OpenGLDevice::GetDeviceInfo() const {
|
2026-03-17 19:35:51 +08:00
|
|
|
return m_deviceInfo;
|
2026-03-17 17:45:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* OpenGLDevice::GetNativeDevice() {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* OpenGLDevice::GetNativeHandle() const {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 16:07:12 +08:00
|
|
|
} // namespace RHI
|
|
|
|
|
} // namespace XCEngine
|