From c84ed938ff08aaa40887f748de3a65bc096e8a35 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Sat, 28 Oct 2023 23:29:17 +0200 Subject: [PATCH] [Window] Fixed resize + vsync support. --- source/client/core/Window.cpp | 20 +++++++++++++++----- source/client/core/Window.hpp | 2 ++ source/client/graphics/BgfxView.hpp | 10 +++++----- source/client/graphics/Framebuffer.cpp | 14 ++++++++------ source/client/graphics/Framebuffer.hpp | 1 + source/client/states/GameState.cpp | 2 ++ 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/source/client/core/Window.cpp b/source/client/core/Window.cpp index 3f4d6cc9..47ba45ea 100644 --- a/source/client/core/Window.cpp +++ b/source/client/core/Window.cpp @@ -30,6 +30,7 @@ #include #include +#include "Config.hpp" #include "BgfxView.hpp" #include "CoreApplication.hpp" #include "Window.hpp" @@ -64,6 +65,16 @@ void Window::close() { void Window::clear() { bgfx::touch(0); + + if (m_hasChanged) { + uint32_t flags = (m_isVerticalSyncEnabled) ? BGFX_RESET_VSYNC : BGFX_RESET_NONE; + bgfx::reset(m_size.x, m_size.y, flags); + + for (bgfx::ViewId i = 0; i < BgfxView::Count; ++i) + bgfx::setViewRect(i, 0, 0, (u16)m_size.x, (u16)m_size.y); + + m_hasChanged = false; + } } void Window::display() { @@ -72,11 +83,10 @@ void Window::display() { void Window::onEvent(const SDL_Event &event) { if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - for (bgfx::ViewId i = 0; i < BgfxView::Count; ++i) - bgfx::setViewRect(i, 0, 0, (uint16_t)event.window.data1, (uint16_t)event.window.data2); - m_size.x = (unsigned int)event.window.data1; m_size.y = (unsigned int)event.window.data2; + + m_hasChanged = true; } if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE) { @@ -87,7 +97,7 @@ void Window::onEvent(const SDL_Event &event) { void Window::setVerticalSyncEnabled(bool isVerticalSyncEnabled) { m_isVerticalSyncEnabled = isVerticalSyncEnabled; - gkWarning() << "Vsync not implemented yet"; + m_hasChanged = true; } void Window::setWindowMode(Mode mode) { @@ -142,7 +152,7 @@ void Window::initBGFX() { bgfx::Init init; init.resolution.width = m_size.x; init.resolution.height = m_size.y; - init.resolution.reset = BGFX_RESET_VSYNC; + init.resolution.reset = (Config::isVerticalSyncEnabled) ? BGFX_RESET_VSYNC : BGFX_RESET_NONE; init.type = bgfx::RendererType::OpenGL; init.callback = &m_callback; diff --git a/source/client/core/Window.hpp b/source/client/core/Window.hpp index ec90288d..4f299dbb 100644 --- a/source/client/core/Window.hpp +++ b/source/client/core/Window.hpp @@ -88,6 +88,8 @@ class Window : public RenderTarget { bool m_isVerticalSyncEnabled = false; BgfxCallback m_callback; + + bool m_hasChanged = false; }; #endif // WINDOW_HPP_ diff --git a/source/client/graphics/BgfxView.hpp b/source/client/graphics/BgfxView.hpp index 736e8278..28f9be81 100644 --- a/source/client/graphics/BgfxView.hpp +++ b/source/client/graphics/BgfxView.hpp @@ -29,11 +29,11 @@ namespace BgfxView { enum Enum { - Sky, - World, - Effect, - HUD, - UI, + Sky = 0, + World = 1, + Effect = 2, + HUD = 3, + UI = 4, Count }; diff --git a/source/client/graphics/Framebuffer.cpp b/source/client/graphics/Framebuffer.cpp index 332770f5..6d071766 100644 --- a/source/client/graphics/Framebuffer.cpp +++ b/source/client/graphics/Framebuffer.cpp @@ -78,6 +78,9 @@ void Framebuffer::init(u16 width, u16 height) { if (!bgfx::isValid(m_textures[1])) throw EXCEPTION("Framebuffer error: Unable to create depth texture with format D24S8"); + if (bgfx::isValid(m_handle)) + bgfx::destroy(m_handle); + m_handle = bgfx::createFrameBuffer(2, m_textures, true); if (!bgfx::isValid(m_handle)) @@ -85,15 +88,9 @@ void Framebuffer::init(u16 width, u16 height) { bgfx::setViewRect(BgfxView::Effect, 0, 0, width, height); bgfx::setViewClear(BgfxView::Effect, BGFX_CLEAR_COLOR); - - bgfx::setViewFrameBuffer(BgfxView::Sky, m_handle); - bgfx::setViewFrameBuffer(BgfxView::World, m_handle); } void Framebuffer::free() { - bgfx::setViewFrameBuffer(BgfxView::Sky, BGFX_INVALID_HANDLE); - bgfx::setViewFrameBuffer(BgfxView::World, BGFX_INVALID_HANDLE); - if (bgfx::isValid(m_handle)) { bgfx::destroy(m_handle); m_handle.idx = bgfx::kInvalidHandle; @@ -124,6 +121,11 @@ void Framebuffer::loadShader(const std::string &name) { m_shader.loadFromFile(name); } +void Framebuffer::prepareDraw() const { + bgfx::setViewFrameBuffer(BgfxView::Sky, m_handle); + bgfx::setViewFrameBuffer(BgfxView::World, m_handle); +} + void Framebuffer::draw() const { bgfx::setTexture(0, m_colorTextureSampler, m_textures[0]); bgfx::setTexture(1, m_depthTextureSampler, m_textures[1]); diff --git a/source/client/graphics/Framebuffer.hpp b/source/client/graphics/Framebuffer.hpp index 44a7a5e7..94af65df 100644 --- a/source/client/graphics/Framebuffer.hpp +++ b/source/client/graphics/Framebuffer.hpp @@ -45,6 +45,7 @@ class Framebuffer : public gk::NonCopyable { void loadShader(const std::string &name); + void prepareDraw() const; void draw() const; private: diff --git a/source/client/states/GameState.cpp b/source/client/states/GameState.cpp index a686c987..a67a087c 100644 --- a/source/client/states/GameState.cpp +++ b/source/client/states/GameState.cpp @@ -258,6 +258,8 @@ void GameState::draw(RenderTarget &target, RenderStates states) const { target.setView(m_camera); + m_fbo.prepareDraw(); + states.view = BgfxView::Sky; { target.draw(m_skybox, states);