Files
XCEngine/docs/api/rhi/opengl/opengl-swap-chain.md

2.7 KiB
Raw Blame History

OpenGLSwapChain

OpenGL 交换链实现,基于 GLFW 窗口和双缓冲。

头文件

#include <XCEngine/RHI/OpenGL/OpenGLSwapChain.h>

枚举

PresentMode

描述
Immediate 立即呈现,无垂直同步
VSync 等待垂直同步
Mailbox 邮箱模式(渲染时不阻塞)
Fifo 标准 FIFO 队列(默认)

SurfaceFormat

描述
RGBA8 8-bit RGBA
RGBA16F 16-bit float RGBA
RGBA32F 32-bit float RGBA
BGRA8 BGRA 格式

继承关系

RHISwapChain (interface)
└── OpenGLSwapChain (implementation)

公共成员函数

构造函数与析构函数

OpenGLSwapChain()

~OpenGLSwapChain() override

初始化

bool Initialize(GLFWwindow* window, bool vsync = true)

初始化交换链。

bool Initialize(GLFWwindow* window, int width, int height, PresentMode mode = PresentMode::VSync)

void Shutdown() override

呈现操作

void Present(uint32_t syncInterval = 1, uint32_t flags = 0) override

呈现帧。

  • syncInterval: 垂直同步间隔

void SwapBuffers()

直接交换缓冲区。

尺寸管理

void Resize(uint32_t width, uint32_t height) override

void SetFramebufferSize(int width, int height)

垂直同步

void SetVSync(bool enabled)

设置垂直同步。

bool IsVSync() const

检查是否启用垂直同步。

全屏

void SetFullscreen(bool fullscreen) override

bool IsFullscreen() const override

窗口事件

bool ShouldClose() const override

void SetShouldClose(bool shouldClose) override

void PollEvents() override

缓冲区查询

uint32_t GetCurrentBackBufferIndex() const override

返回 0OpenGL 单缓冲区索引)。

RHITexture* GetCurrentBackBuffer() override

返回 nullptrOpenGL 使用默认 framebuffer

属性

int GetWidth() const

int GetHeight() const

int GetFramebufferWidth() const

int GetFramebufferHeight() const

GLFWwindow* GetWindow() const

void* GetNativeHandle() override

内部成员

成员 类型 描述
m_window GLFWwindow* GLFW 窗口
m_width/height int 窗口尺寸
m_framebufferWidth/height int 帧缓冲尺寸 (DPI aware)
m_vsync bool 是否垂直同步
m_presentMode PresentMode 呈现模式

使用示例

OpenGLSwapChain swapChain;
swapChain.Initialize(window, true);

while (!swapChain.ShouldClose()) {
    swapChain.PollEvents();
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // Render...
    
    swapChain.Present(1);
}