From 221a9b023d8c9ca55c093823e9efd6d13d0a54a2 Mon Sep 17 00:00:00 2001 From: Kevin Hartman Date: Fri, 29 Aug 2014 22:23:12 -0700 Subject: Viewport scaling and display density independence The view is scaled to be as large as possible, without changing the aspect, within the bounds of the window. On "retina" displays, or other displays where window units != pixels, the view should no longer draw incorrectly. --- src/video_core/renderer_opengl/renderer_opengl.cpp | 36 +++++++++++++++++++++- src/video_core/renderer_opengl/renderer_opengl.h | 15 +++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) (limited to 'src/video_core') diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index 8483f79be..3757482db 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -191,7 +191,8 @@ void RendererOpenGL::DrawSingleScreenRotated(const TextureInfo& texture, float x * Draws the emulated screens to the emulator window. */ void RendererOpenGL::DrawScreens() { - glViewport(0, 0, resolution_width, resolution_height); + UpdateViewportExtent(); + glViewport(viewport_extent.x, viewport_extent.y, viewport_extent.width, viewport_extent.height); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(program_id); @@ -228,6 +229,39 @@ void RendererOpenGL::SetWindow(EmuWindow* window) { render_window = window; } +void RendererOpenGL::UpdateViewportExtent() { + int width_in_pixels; + int height_in_pixels; + + render_window->GetFramebufferSize(&width_in_pixels, &height_in_pixels); + + // No update needed if framebuffer size hasn't changed + if (width_in_pixels == framebuffer_size.width && height_in_pixels == framebuffer_size.height) { + return; + } + + framebuffer_size.width = width_in_pixels; + framebuffer_size.height = height_in_pixels; + + float window_aspect_ratio = static_cast(height_in_pixels) / width_in_pixels; + float emulation_aspect_ratio = static_cast(resolution_height) / resolution_width; + + if (window_aspect_ratio > emulation_aspect_ratio) { + // If the window is more narrow than the emulation content, borders are applied on the + // top and bottom of the window. + viewport_extent.width = width_in_pixels; + viewport_extent.height = emulation_aspect_ratio * viewport_extent.width; + viewport_extent.x = 0; + viewport_extent.y = (height_in_pixels - viewport_extent.height) / 2; + } else { + // Otherwise, borders are applied on the left and right sides of the window. + viewport_extent.height = height_in_pixels; + viewport_extent.width = (1 / emulation_aspect_ratio) * viewport_extent.height; + viewport_extent.x = (width_in_pixels - viewport_extent.width) / 2; + viewport_extent.y = 0; + } +} + /// Initialize the renderer void RendererOpenGL::Init() { render_window->MakeCurrent(); diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h index eed201a95..d440e2bc7 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.h +++ b/src/video_core/renderer_opengl/renderer_opengl.h @@ -52,12 +52,27 @@ private: static void LoadFBToActiveGLTexture(const GPU::Regs::FramebufferConfig& framebuffer, const TextureInfo& texture); + /// Updates the viewport rectangle + void UpdateViewportExtent(); + EmuWindow* render_window; ///< Handle to render window u32 last_mode; ///< Last render mode int resolution_width; ///< Current resolution width int resolution_height; ///< Current resolution height + struct { + int width; + int height; + } framebuffer_size; ///< Current framebuffer size + + struct { + int x; + int y; + int width; + int height; + } viewport_extent; ///< Current viewport rectangle + // OpenGL object IDs GLuint vertex_array_handle; GLuint vertex_buffer_handle; -- cgit v1.2.3 From bd8f491e4c08e9b9a7b852de0b50c144da8ac8c8 Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Sun, 12 Oct 2014 18:14:57 +0200 Subject: Fixup EmuWindow interface and implementations thereof. --- src/citra/emu_window/emu_window_glfw.cpp | 51 ++++++++++++------ src/citra/emu_window/emu_window_glfw.h | 13 +++-- src/citra_qt/bootmanager.cpp | 53 ++++++++++++------- src/citra_qt/bootmanager.hxx | 19 ++++--- src/common/emu_window.h | 61 ++++++++++++---------- src/video_core/renderer_opengl/renderer_opengl.cpp | 6 +-- 6 files changed, 127 insertions(+), 76 deletions(-) (limited to 'src/video_core') diff --git a/src/citra/emu_window/emu_window_glfw.cpp b/src/citra/emu_window/emu_window_glfw.cpp index d0f6e9a9e..28aa3450c 100644 --- a/src/citra/emu_window/emu_window_glfw.cpp +++ b/src/citra/emu_window/emu_window_glfw.cpp @@ -2,6 +2,8 @@ // Licensed under GPLv2 // Refer to the license.txt file included. +#include + #include "common/common.h" #include "video_core/video_core.h" @@ -10,22 +12,21 @@ #include "citra/emu_window/emu_window_glfw.h" +EmuWindow_GLFW* EmuWindow_GLFW::GetEmuWindow(GLFWwindow* win) { + return static_cast(glfwGetWindowUserPointer(win)); +} + /// Called by GLFW when a key event occurs void EmuWindow_GLFW::OnKeyEvent(GLFWwindow* win, int key, int scancode, int action, int mods) { - if (!VideoCore::g_emu_window) { - return; - } - - int keyboard_id = ((EmuWindow_GLFW*)VideoCore::g_emu_window)->keyboard_id; + int keyboard_id = GetEmuWindow(win)->keyboard_id; if (action == GLFW_PRESS) { EmuWindow::KeyPressed({key, keyboard_id}); - } - - if (action == GLFW_RELEASE) { + } else if (action == GLFW_RELEASE) { EmuWindow::KeyReleased({key, keyboard_id}); } + HID_User::PadUpdateComplete(); } @@ -34,8 +35,18 @@ const bool EmuWindow_GLFW::IsOpen() { return glfwWindowShouldClose(m_render_window) == 0; } -void EmuWindow_GLFW::GetFramebufferSize(int* fbWidth, int* fbHeight) { - glfwGetFramebufferSize(m_render_window, fbWidth, fbHeight); +void EmuWindow_GLFW::OnFramebufferResizeEvent(GLFWwindow* win, int width, int height) { + _dbg_assert_(GUI, width > 0); + _dbg_assert_(GUI, height > 0); + + GetEmuWindow(win)->NotifyFramebufferSizeChanged(std::pair(width, height)); +} + +void EmuWindow_GLFW::OnClientAreaResizeEvent(GLFWwindow* win, int width, int height) { + _dbg_assert_(GUI, width > 0); + _dbg_assert_(GUI, height > 0); + + GetEmuWindow(win)->NotifyClientAreaSizeChanged(std::pair(width, height)); } /// EmuWindow_GLFW constructor @@ -54,20 +65,30 @@ EmuWindow_GLFW::EmuWindow_GLFW() { // GLFW on OSX requires these window hints to be set to create a 3.2+ GL context. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - + m_render_window = glfwCreateWindow(VideoCore::kScreenTopWidth, (VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight), - m_window_title.c_str(), NULL, NULL); + GetWindowTitle().c_str(), NULL, NULL); if (m_render_window == NULL) { printf("Failed to create GLFW window! Exiting..."); exit(1); } - - // Setup callbacks + glfwSetWindowUserPointer(m_render_window, this); - glfwSetKeyCallback(m_render_window, OnKeyEvent); + // Notify base interface about window state + int width, height; + glfwGetFramebufferSize(m_render_window, &width, &height); + OnFramebufferResizeEvent(m_render_window, width, height); + + glfwGetWindowSize(m_render_window, &width, &height); + OnClientAreaResizeEvent(m_render_window, width, height); + + // Setup callbacks + glfwSetKeyCallback(m_render_window, OnKeyEvent); + glfwSetFramebufferSizeCallback(m_render_window, OnFramebufferResizeEvent); + glfwSetWindowSizeCallback(m_render_window, OnClientAreaResizeEvent); DoneCurrent(); } diff --git a/src/citra/emu_window/emu_window_glfw.h b/src/citra/emu_window/emu_window_glfw.h index e96228765..0da688a54 100644 --- a/src/citra/emu_window/emu_window_glfw.h +++ b/src/citra/emu_window/emu_window_glfw.h @@ -4,10 +4,10 @@ #pragma once -#include - #include "common/emu_window.h" +struct GLFWwindow; + class EmuWindow_GLFW : public EmuWindow { public: EmuWindow_GLFW(); @@ -30,12 +30,15 @@ public: /// Whether the window is still open, and a close request hasn't yet been sent const bool IsOpen(); - void ReloadSetKeymaps() override; + static void OnClientAreaResizeEvent(GLFWwindow* win, int width, int height); - /// Gets the size of the window in pixels - void GetFramebufferSize(int* fbWidth, int* fbHeight); + static void OnFramebufferResizeEvent(GLFWwindow* win, int width, int height); + + void ReloadSetKeymaps() override; private: + static EmuWindow_GLFW* GetEmuWindow(GLFWwindow* win); + GLFWwindow* m_render_window; ///< Internal GLFW render window /// Device id of keyboard for use with KeyMap diff --git a/src/citra_qt/bootmanager.cpp b/src/citra_qt/bootmanager.cpp index 516e115fd..34a79b306 100644 --- a/src/citra_qt/bootmanager.cpp +++ b/src/citra_qt/bootmanager.cpp @@ -88,20 +88,20 @@ void EmuThread::Stop() class GGLWidgetInternal : public QGLWidget { public: - GGLWidgetInternal(QGLFormat fmt, GRenderWindow* parent) : QGLWidget(fmt, parent) - { - parent_ = parent; + GGLWidgetInternal(QGLFormat fmt, GRenderWindow* parent) + : QGLWidget(fmt, parent), parent(parent) { } - void paintEvent(QPaintEvent* ev) override - { + void paintEvent(QPaintEvent* ev) override { } + void resizeEvent(QResizeEvent* ev) override { - parent_->SetClientAreaWidth(size().width()); - parent_->SetClientAreaHeight(size().height()); + parent->OnClientAreaResized(ev->size().width(), ev->size().height()); + parent->OnFramebufferSizeChanged(); } + private: - GRenderWindow* parent_; + GRenderWindow* parent; }; EmuThread& GRenderWindow::GetEmuThread() @@ -120,16 +120,23 @@ GRenderWindow::GRenderWindow(QWidget* parent) : QWidget(parent), emu_thread(this fmt.setProfile(QGLFormat::CoreProfile); // Requests a forward-compatible context, which is required to get a 3.2+ context on OS X fmt.setOption(QGL::NoDeprecatedFunctions); - + child = new GGLWidgetInternal(fmt, this); QBoxLayout* layout = new QHBoxLayout(this); resize(VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight); layout->addWidget(child); layout->setMargin(0); setLayout(layout); - QObject::connect(&emu_thread, SIGNAL(started()), this, SLOT(moveContext())); + connect(&emu_thread, SIGNAL(started()), this, SLOT(moveContext())); + + OnFramebufferSizeChanged(); + NotifyClientAreaSizeChanged(std::pair(child->width(), child->height())); BackupGeometry(); + +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) + connect(this->windowHandle(), SIGNAL(screenChanged(QScreen*)), this, SLOT(OnFramebufferSizeChanged())); +#endif } void GRenderWindow::moveContext() @@ -182,22 +189,26 @@ void GRenderWindow::PollEvents() { */ } -// On Qt 5.1+, this correctly gets the size of the framebuffer (pixels). +// On Qt 5.0+, this correctly gets the size of the framebuffer (pixels). // // Older versions get the window size (density independent pixels), // and hence, do not support DPI scaling ("retina" displays). // The result will be a viewport that is smaller than the extent of the window. -void GRenderWindow::GetFramebufferSize(int* fbWidth, int* fbHeight) +void GRenderWindow::OnFramebufferSizeChanged() { -#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) - int pixelRatio = child->QPaintDevice::devicePixelRatio(); - - *fbWidth = child->QPaintDevice::width() * pixelRatio; - *fbHeight = child->QPaintDevice::height() * pixelRatio; + // Screen changes potentially incur a change in screen DPI, hence we should update the framebuffer size +#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) + // windowHandle() might not be accessible until the window is displayed to screen. + auto pixel_ratio = windowHandle() ? (windowHandle()->screen()->devicePixelRatio()) : 1.0; + + unsigned width = child->QPaintDevice::width() * pixel_ratio; + unsigned height = child->QPaintDevice::height() * pixel_ratio; #else - *fbWidth = child->QPaintDevice::width(); - *fbHeight = child->QPaintDevice::height(); + unsigned width = child->QPaintDevice::width(); + unsigned height = child->QPaintDevice::height(); #endif + + NotifyFramebufferSizeChanged(std::make_pair(width, height)); } void GRenderWindow::BackupGeometry() @@ -260,3 +271,7 @@ void GRenderWindow::ReloadSetKeymaps() KeyMap::SetKeyMapping({Settings::values.pad_sdown_key, keyboard_id}, HID_User::PAD_CIRCLE_DOWN); } +void GRenderWindow::OnClientAreaResized(unsigned width, unsigned height) +{ + NotifyClientAreaSizeChanged(std::make_pair(width, height)); +} diff --git a/src/citra_qt/bootmanager.hxx b/src/citra_qt/bootmanager.hxx index ec3e1fe71..3a18f98fd 100644 --- a/src/citra_qt/bootmanager.hxx +++ b/src/citra_qt/bootmanager.hxx @@ -1,12 +1,16 @@ +#include + #include #include -#include + #include "common/common.h" #include "common/emu_window.h" -class GRenderWindow; +class QScreen; class QKeyEvent; +class GRenderWindow; + class EmuThread : public QThread { Q_OBJECT @@ -74,7 +78,7 @@ private: signals: /** * Emitted when CPU when we've finished processing a single Gekko instruction - * + * * @warning This will only be emitted when the CPU is not running (SetCpuRunning(false)) * @warning When connecting to this signal from other threads, make sure to specify either Qt::QueuedConnection (invoke slot within the destination object's message thread) or even Qt::BlockingQueuedConnection (additionally block source thread until slot returns) */ @@ -96,12 +100,11 @@ public: void MakeCurrent() override; void DoneCurrent() override; void PollEvents() override; - void GetFramebufferSize(int* fbWidth, int* fbHeight) override; void BackupGeometry(); void RestoreGeometry(); void restoreGeometry(const QByteArray& geometry); // overridden - QByteArray saveGeometry(); // overridden + QByteArray saveGeometry(); // overridden EmuThread& GetEmuThread(); @@ -110,8 +113,12 @@ public: void ReloadSetKeymaps() override; + void OnClientAreaResized(unsigned width, unsigned height); + + void OnFramebufferSizeChanged(); + public slots: - void moveContext(); + void moveContext(); // overridden private: QGLWidget* child; diff --git a/src/common/emu_window.h b/src/common/emu_window.h index ba9d4fa76..72c40c6a4 100644 --- a/src/common/emu_window.h +++ b/src/common/emu_window.h @@ -43,54 +43,59 @@ public: static void KeyReleased(KeyMap::HostDeviceKey key); WindowConfig GetConfig() const { - return m_config; + return config; } void SetConfig(const WindowConfig& val) { - m_config = val; + config = val; } - /// Gets the size of the window in pixels - virtual void GetFramebufferSize(int* fbWidth, int* fbHeight) = 0; - - int GetClientAreaWidth() const { - return m_client_area_width; - } - - void SetClientAreaWidth(const int val) { - m_client_area_width = val; + /** + * Gets the size of the framebuffer in pixels + */ + const std::pair GetFramebufferSize() const { + return framebuffer_size; } - int GetClientAreaHeight() const { - return m_client_area_height; + /** + * Gets window client area width in logical coordinates + */ + std::pair GetClientAreaSize() const { + return std::make_pair(client_area_width, client_area_height); } - void SetClientAreaHeight(const int val) { - m_client_area_height = val; + std::string GetWindowTitle() const { + return window_title; } - std::string GetWindowTitle() const { - return m_window_title; - } - - void SetWindowTitle(std::string val) { - m_window_title = val; + void SetWindowTitle(const std::string& val) { + window_title = val; } protected: - EmuWindow(): - m_window_title(Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc)), + EmuWindow() : // TODO: What the hell... -.- - don't hardcode dimensions here without applying them in a sensible manner... + window_title(Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc)) m_client_area_width(640), - m_client_area_height(480) + m_client_area_height(480), {} virtual ~EmuWindow() {} - std::string m_window_title; ///< Current window title, should be used by window impl. + std::pair NotifyFramebufferSizeChanged(const std::pair& size) { + framebuffer_size = size; + } - int m_client_area_width; ///< Current client width, should be set by window impl. - int m_client_area_height; ///< Current client height, should be set by window impl. + void NotifyClientAreaSizeChanged(std::pair size) { + client_area_width = size.first; + client_area_height = size.second; + } private: - WindowConfig m_config; ///< Internal configuration + std::string window_title; ///< Current window title, should be used by window impl. + + std::pair framebuffer_size; + + unsigned client_area_width; ///< Current client width, should be set by window impl. + unsigned client_area_height; ///< Current client height, should be set by window impl. + WindowConfig config; ///< Internal configuration }; diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index 3757482db..082a464b3 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -230,10 +230,10 @@ void RendererOpenGL::SetWindow(EmuWindow* window) { } void RendererOpenGL::UpdateViewportExtent() { - int width_in_pixels; - int height_in_pixels; + unsigned width_in_pixels; + unsigned height_in_pixels; - render_window->GetFramebufferSize(&width_in_pixels, &height_in_pixels); + std::tie(width_in_pixels, height_in_pixels) = render_window->GetFramebufferSize(); // No update needed if framebuffer size hasn't changed if (width_in_pixels == framebuffer_size.width && height_in_pixels == framebuffer_size.height) { -- cgit v1.2.3 From c5c6e095f037a0128de156103f5b98d5bf2b417c Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Thu, 13 Nov 2014 19:33:29 +0100 Subject: OpenGL Renderer: Cleanup viewport extent calculation. --- src/video_core/renderer_opengl/renderer_opengl.cpp | 49 ++++++++++------------ src/video_core/renderer_opengl/renderer_opengl.h | 24 ++++------- 2 files changed, 29 insertions(+), 44 deletions(-) (limited to 'src/video_core') diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index 082a464b3..729e8e73d 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -191,8 +191,8 @@ void RendererOpenGL::DrawSingleScreenRotated(const TextureInfo& texture, float x * Draws the emulated screens to the emulator window. */ void RendererOpenGL::DrawScreens() { - UpdateViewportExtent(); - glViewport(viewport_extent.x, viewport_extent.y, viewport_extent.width, viewport_extent.height); + auto viewport_extent = GetViewportExtent(); + glViewport(viewport_extent.left, viewport_extent.top, viewport_extent.GetWidth(), viewport_extent.GetHeight()); // TODO: Or bottom? glClear(GL_COLOR_BUFFER_BIT); glUseProgram(program_id); @@ -229,37 +229,32 @@ void RendererOpenGL::SetWindow(EmuWindow* window) { render_window = window; } -void RendererOpenGL::UpdateViewportExtent() { - unsigned width_in_pixels; - unsigned height_in_pixels; +MathUtil::Rectangle RendererOpenGL::GetViewportExtent() { + unsigned framebuffer_width; + unsigned framebuffer_height; + std::tie(framebuffer_width, framebuffer_height) = render_window->GetFramebufferSize(); - std::tie(width_in_pixels, height_in_pixels) = render_window->GetFramebufferSize(); - - // No update needed if framebuffer size hasn't changed - if (width_in_pixels == framebuffer_size.width && height_in_pixels == framebuffer_size.height) { - return; - } - - framebuffer_size.width = width_in_pixels; - framebuffer_size.height = height_in_pixels; - - float window_aspect_ratio = static_cast(height_in_pixels) / width_in_pixels; + float window_aspect_ratio = static_cast(framebuffer_height) / framebuffer_width; float emulation_aspect_ratio = static_cast(resolution_height) / resolution_width; + MathUtil::Rectangle viewport_extent; if (window_aspect_ratio > emulation_aspect_ratio) { - // If the window is more narrow than the emulation content, borders are applied on the - // top and bottom of the window. - viewport_extent.width = width_in_pixels; - viewport_extent.height = emulation_aspect_ratio * viewport_extent.width; - viewport_extent.x = 0; - viewport_extent.y = (height_in_pixels - viewport_extent.height) / 2; + // Window is narrower than the emulation content => apply borders to the top and bottom + unsigned viewport_height = emulation_aspect_ratio * framebuffer_width; + viewport_extent.left = 0; + viewport_extent.top = (framebuffer_height - viewport_height) / 2; + viewport_extent.right = viewport_extent.left + framebuffer_width; + viewport_extent.bottom = viewport_extent.top + viewport_height; } else { - // Otherwise, borders are applied on the left and right sides of the window. - viewport_extent.height = height_in_pixels; - viewport_extent.width = (1 / emulation_aspect_ratio) * viewport_extent.height; - viewport_extent.x = (width_in_pixels - viewport_extent.width) / 2; - viewport_extent.y = 0; + // Otherwise, apply borders to the left and right sides of the window. + unsigned viewport_width = framebuffer_height / emulation_aspect_ratio; + viewport_extent.left = (framebuffer_width - viewport_width) / 2; + viewport_extent.top = 0; + viewport_extent.right = viewport_extent.left + viewport_width; + viewport_extent.bottom = viewport_extent.top + framebuffer_height; } + + return viewport_extent; } /// Initialize the renderer diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h index d440e2bc7..7fdcec731 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.h +++ b/src/video_core/renderer_opengl/renderer_opengl.h @@ -4,13 +4,15 @@ #pragma once +#include + #include "generated/gl_3_2_core.h" -#include "common/common.h" +#include "common/math_util.h" + #include "core/hw/gpu.h" -#include "video_core/renderer_base.h" -#include +#include "video_core/renderer_base.h" class EmuWindow; @@ -52,8 +54,8 @@ private: static void LoadFBToActiveGLTexture(const GPU::Regs::FramebufferConfig& framebuffer, const TextureInfo& texture); - /// Updates the viewport rectangle - void UpdateViewportExtent(); + /// Computes the viewport rectangle + MathUtil::Rectangle GetViewportExtent(); EmuWindow* render_window; ///< Handle to render window u32 last_mode; ///< Last render mode @@ -61,18 +63,6 @@ private: int resolution_width; ///< Current resolution width int resolution_height; ///< Current resolution height - struct { - int width; - int height; - } framebuffer_size; ///< Current framebuffer size - - struct { - int x; - int y; - int width; - int height; - } viewport_extent; ///< Current viewport rectangle - // OpenGL object IDs GLuint vertex_array_handle; GLuint vertex_buffer_handle; -- cgit v1.2.3