Extra2D/src/renderer/rhi/opengl/gl_context.cpp

85 lines
2.2 KiB
C++
Raw Normal View History

#include <cstring>
#include <renderer/rhi/opengl/gl_context.h>
#include <renderer/rhi/opengl/gl_framebuffer.h>
#include <utils/logger.h>
namespace extra2d {
GLContext::GLContext(GLDevice *device)
: device_(device), defaultFramebuffer_(nullptr) {}
GLContext::~GLContext() = default;
bool GLContext::initialize() {
E2D_LOG_INFO("Initializing OpenGL context...");
// 创建默认帧缓冲(窗口帧缓冲)
defaultFramebuffer_ = std::make_unique<GLFramebuffer>();
defaultFramebuffer_->setSize(800, 600); // 默认大小,会被更新
E2D_LOG_INFO("OpenGL context initialized");
return true;
}
void GLContext::shutdown() {
E2D_LOG_INFO("Shutting down OpenGL context...");
defaultFramebuffer_.reset();
E2D_LOG_INFO("OpenGL context shutdown complete");
}
void GLContext::beginFrame() {
// 重置每帧的统计
// device_->resetStats(); // 需要在 GLDevice 中添加此方法
}
void GLContext::endFrame() {
// 交换缓冲区
// 通过 SDL 获取当前窗口并交换缓冲区
SDL_Window* window = SDL_GL_GetCurrentWindow();
if (window) {
SDL_GL_SwapWindow(window);
}
}
void GLContext::setViewport(int32_t x, int32_t y, uint32_t width,
uint32_t height) {
glViewport(x, y, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
// 更新默认帧缓冲大小
if (defaultFramebuffer_) {
defaultFramebuffer_->setSize(width, height);
}
}
void GLContext::bindDefaultFramebuffer() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
RHIFramebuffer *GLContext::getDefaultFramebuffer() {
return defaultFramebuffer_.get();
}
bool GLContext::isFeatureSupported(const char *feature) const {
// 检查 OpenGL 扩展或版本
if (std::strcmp(feature, "instancing") == 0) {
return glad_glDrawArraysInstanced != nullptr;
}
if (std::strcmp(feature, "compute") == 0) {
return glad_glDispatchCompute != nullptr;
}
if (std::strcmp(feature, "sRGB") == 0) {
return GLAD_GL_EXT_texture_sRGB_R8;
}
if (std::strcmp(feature, "anisotropic") == 0) {
return GLAD_GL_EXT_texture_filter_anisotropic;
}
if (std::strcmp(feature, "debug") == 0) {
return GLAD_GL_KHR_debug;
}
return false;
}
} // namespace extra2d