feat(RHI): 实现 RHISwapChain 抽象基类

This commit is contained in:
2026-03-17 18:05:40 +08:00
parent 55865a0252
commit 354b6a5cfc
6 changed files with 163 additions and 17 deletions

View File

@@ -5,6 +5,7 @@
#include <wrl/client.h>
#include <vector>
#include "../RHISwapChain.h"
#include "D3D12Enum.h"
#include "D3D12Texture.h"
@@ -13,22 +14,27 @@ using Microsoft::WRL::ComPtr;
namespace XCEngine {
namespace RHI {
class D3D12SwapChain {
class D3D12SwapChain : public RHISwapChain {
public:
D3D12SwapChain();
~D3D12SwapChain();
~D3D12SwapChain() override;
bool Initialize(IDXGIFactory4* factory, ID3D12CommandQueue* commandQueue, HWND windowHandle, uint32_t width, uint32_t height, uint32_t bufferCount = 2);
bool Initialize(IDXGISwapChain* swapChain, uint32_t width, uint32_t height);
void Shutdown();
void Shutdown() override;
uint32_t GetCurrentBackBufferIndex() const;
uint32_t GetCurrentBackBufferIndex() const override;
RHITexture* GetCurrentBackBuffer() override;
D3D12Texture* GetBackBuffer(uint32_t index) const;
void Present(uint32_t syncInterval, uint32_t flags);
void Resize(uint32_t width, uint32_t height);
void SetFullscreen(bool fullscreen);
bool IsFullscreen() const;
void* GetNativeHandle() const;
void Present(uint32_t syncInterval = 1, uint32_t flags = 0) override;
void Resize(uint32_t width, uint32_t height) override;
void SetFullscreen(bool fullscreen) override;
bool IsFullscreen() const override;
void* GetNativeHandle() override;
bool ShouldClose() const override;
void SetShouldClose(bool shouldClose) override;
void PollEvents() override;
IDXGISwapChain3* GetSwapChain() const { return m_swapChain.Get(); }

View File

@@ -3,6 +3,8 @@
#include <GLFW/glfw3.h>
#include <cstdint>
#include "../RHISwapChain.h"
namespace XCEngine {
namespace RHI {
@@ -20,20 +22,22 @@ enum class SurfaceFormat {
BGRA8
};
class OpenGLSwapChain {
class OpenGLSwapChain : public RHISwapChain {
public:
OpenGLSwapChain();
~OpenGLSwapChain();
~OpenGLSwapChain() override;
bool Initialize(GLFWwindow* window, bool vsync = true);
bool Initialize(GLFWwindow* window, int width, int height, PresentMode mode = PresentMode::VSync);
void Shutdown();
void Shutdown() override;
void Present();
void Present(uint32_t syncInterval = 1, uint32_t flags = 0) override;
void SwapBuffers();
void Resize(int width, int height);
void Resize(uint32_t width, uint32_t height) override;
void SetVSync(bool enabled);
bool IsVSync() const { return m_vsync; }
void SetFullscreen(bool fullscreen) override;
bool IsFullscreen() const override;
void SetFramebufferSize(int width, int height);
int GetWidth() const { return m_width; }
@@ -43,9 +47,13 @@ public:
GLFWwindow* GetWindow() const { return m_window; }
bool ShouldClose() const;
void SetShouldClose(bool shouldClose);
void PollEvents();
bool ShouldClose() const override;
void SetShouldClose(bool shouldClose) override;
void PollEvents() override;
uint32_t GetCurrentBackBufferIndex() const override;
RHITexture* GetCurrentBackBuffer() override;
void* GetNativeHandle() override;
private:
GLFWwindow* m_window;

View File

@@ -0,0 +1,31 @@
#pragma once
#include "RHITypes.h"
namespace XCEngine {
namespace RHI {
class RHITexture;
class RHISwapChain {
public:
virtual ~RHISwapChain() = default;
virtual void Shutdown() = 0;
virtual uint32_t GetCurrentBackBufferIndex() const = 0;
virtual RHITexture* GetCurrentBackBuffer() = 0;
virtual void Present(uint32_t syncInterval = 1, uint32_t flags = 0) = 0;
virtual void Resize(uint32_t width, uint32_t height) = 0;
virtual void SetFullscreen(bool fullscreen) = 0;
virtual bool IsFullscreen() const = 0;
virtual bool ShouldClose() const = 0;
virtual void SetShouldClose(bool shouldClose) = 0;
virtual void PollEvents() = 0;
virtual void* GetNativeHandle() = 0;
};
} // namespace RHI
} // namespace XCEngine

View File

@@ -105,5 +105,19 @@ void* D3D12SwapChain::GetNativeHandle() const {
return reinterpret_cast<void*>(m_swapChain.Get());
}
RHITexture* D3D12SwapChain::GetCurrentBackBuffer() {
return GetBackBuffer(GetCurrentBackBufferIndex());
}
bool D3D12SwapChain::ShouldClose() const {
return false;
}
void D3D12SwapChain::SetShouldClose(bool shouldClose) {
}
void D3D12SwapChain::PollEvents() {
}
} // namespace RHI
} // namespace XCEngine

View File

@@ -116,5 +116,34 @@ void OpenGLSwapChain::PollEvents() {
glfwPollEvents();
}
void OpenGLSwapChain::Present(uint32_t syncInterval, uint32_t flags) {
if (m_window) {
glfwSwapBuffers(m_window);
}
}
void OpenGLSwapChain::Resize(uint32_t width, uint32_t height) {
Resize(static_cast<int>(width), static_cast<int>(height));
}
void OpenGLSwapChain::SetFullscreen(bool fullscreen) {
}
bool OpenGLSwapChain::IsFullscreen() const {
return false;
}
uint32_t OpenGLSwapChain::GetCurrentBackBufferIndex() const {
return 0;
}
RHITexture* OpenGLSwapChain::GetCurrentBackBuffer() {
return nullptr;
}
void* OpenGLSwapChain::GetNativeHandle() {
return nullptr;
}
} // namespace RHI
} // namespace XCEngine