Qt: Fix HiDPI scaling on non-software renderers

This commit is contained in:
Cacodemon345
2025-03-30 21:03:46 +06:00
parent 512154e4a8
commit 8f94ccec80
6 changed files with 35 additions and 27 deletions

View File

@@ -37,7 +37,7 @@ void
HardwareRenderer::resizeGL(int w, int h) HardwareRenderer::resizeGL(int w, int h)
{ {
m_context->makeCurrent(this); m_context->makeCurrent(this);
glViewport(0, 0, qRound(w * devicePixelRatio()), qRound(h * devicePixelRatio())); glViewport(0, 0, qRound(w * devicePixelRatioF()), qRound(h * devicePixelRatioF()));
} }
#define PROGRAM_VERTEX_ATTRIBUTE 0 #define PROGRAM_VERTEX_ATTRIBUTE 0
@@ -220,14 +220,17 @@ HardwareRenderer::onBlit(int buf_idx, int x, int y, int w, int h)
#endif #endif
buf_usage[buf_idx].clear(); buf_usage[buf_idx].clear();
source.setRect(x, y, w, h); source.setRect(x, y, w, h);
if (origSource != source) if (origSource != source) {
this->pixelRatio = devicePixelRatioF();
onResize(this->width(), this->height()); onResize(this->width(), this->height());
}
update(); update();
} }
void void
HardwareRenderer::resizeEvent(QResizeEvent *event) HardwareRenderer::resizeEvent(QResizeEvent *event)
{ {
this->pixelRatio = devicePixelRatioF();
onResize(width(), height()); onResize(width(), height());
QOpenGLWindow::resizeEvent(event); QOpenGLWindow::resizeEvent(event);

View File

@@ -1154,14 +1154,15 @@ OpenGLRenderer::onBlit(int buf_idx, int x, int y, int w, int h)
buf_usage[buf_idx].clear(); buf_usage[buf_idx].clear();
source.setRect(x, y, w, h); source.setRect(x, y, w, h);
this->pixelRatio = devicePixelRatio();
onResize(this->width(), this->height()); onResize(this->width(), this->height());
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
glw.glViewport( glw.glViewport(
destination.x() * devicePixelRatio(), destination.x(),
destination.y() * devicePixelRatio(), destination.y(),
destination.width() * devicePixelRatio(), destination.width(),
destination.height() * devicePixelRatio()); destination.height());
#endif #endif
if (video_framerate == -1) if (video_framerate == -1)
@@ -1187,6 +1188,7 @@ OpenGLRenderer::exposeEvent(QExposeEvent *event)
if (!isInitialized) if (!isInitialized)
initialize(); initialize();
this->pixelRatio = devicePixelRatio();
onResize(size().width(), size().height()); onResize(size().width(), size().height());
} }
@@ -1195,6 +1197,7 @@ OpenGLRenderer::resizeEvent(QResizeEvent *event)
{ {
Q_UNUSED(event); Q_UNUSED(event);
this->pixelRatio = devicePixelRatio();
onResize(event->size().width(), event->size().height()); onResize(event->size().width(), event->size().height());
if (notReady()) if (notReady())
@@ -1203,10 +1206,10 @@ OpenGLRenderer::resizeEvent(QResizeEvent *event)
context->makeCurrent(this); context->makeCurrent(this);
glw.glViewport( glw.glViewport(
destination.x() * devicePixelRatio(), destination.x(),
destination.y() * devicePixelRatio(), destination.y(),
destination.width() * devicePixelRatio(), destination.width(),
destination.height() * devicePixelRatio()); destination.height());
} }
void void
@@ -1386,10 +1389,10 @@ OpenGLRenderer::render()
uint32_t x, y, w, h; uint32_t x, y, w, h;
} window_rect; } window_rect;
window_rect.x = destination.x() * devicePixelRatio(); window_rect.x = destination.x();
window_rect.y = destination.y() * devicePixelRatio(); window_rect.y = destination.y();
window_rect.w = destination.width() * devicePixelRatio(); window_rect.w = destination.width();
window_rect.h = destination.height() * devicePixelRatio(); window_rect.h = destination.height();
glw.glBindTexture(GL_TEXTURE_2D, scene_texture.id); glw.glBindTexture(GL_TEXTURE_2D, scene_texture.id);
scene_texture.min_filter = scene_texture.mag_filter = video_filter_method ? GL_LINEAR : GL_NEAREST; scene_texture.min_filter = scene_texture.mag_filter = video_filter_method ? GL_LINEAR : GL_NEAREST;
@@ -1652,7 +1655,7 @@ OpenGLRenderer::render()
} }
if (monitors[r_monitor_index].mon_screenshots) { if (monitors[r_monitor_index].mon_screenshots) {
int width = destination.width() * devicePixelRatio(), height = destination.height() * devicePixelRatio(); int width = destination.width(), height = destination.height();
char path[1024]; char path[1024];
char fn[256]; char fn[256];

View File

@@ -59,6 +59,9 @@ RendererCommon::onResize(int width, int height)
bool main_max = main_window->isMaximized(); bool main_max = main_window->isMaximized();
bool main_is_max = (main_is_ancestor && main_max == false); bool main_is_max = (main_is_ancestor && main_max == false);
width = round(pixelRatio * width);
height = round(pixelRatio * height);
if (is_fs && (video_fullscreen_scale_maximized ? (parent_max && main_is_max) : 1)) if (is_fs && (video_fullscreen_scale_maximized ? (parent_max && main_is_max) : 1))
destination.setRect(0, 0, width, height); destination.setRect(0, 0, width, height);
else { else {

View File

@@ -49,5 +49,7 @@ protected:
QRect destination; QRect destination;
QWidget *parentWidget { nullptr }; QWidget *parentWidget { nullptr };
double pixelRatio = 1.0;
std::vector<std::atomic_flag> buf_usage; std::vector<std::atomic_flag> buf_usage;
}; };

View File

@@ -970,17 +970,11 @@ VulkanRenderer2::startNextFrame()
m_devFuncs->vkCmdBindVertexBuffers(cb, 0, 1, &m_buf, &vbOffset); m_devFuncs->vkCmdBindVertexBuffers(cb, 0, 1, &m_buf, &vbOffset);
VkViewport viewport; VkViewport viewport;
if (dpi_scale) { viewport.x = destination.x();
viewport.x = destination.x() * m_window->devicePixelRatio(); viewport.y = destination.y();
viewport.y = destination.y() * m_window->devicePixelRatio(); viewport.width = destination.width();
viewport.width = destination.width() * m_window->devicePixelRatio(); viewport.height = destination.height();
viewport.height = destination.height() * m_window->devicePixelRatio();
} else {
viewport.x = destination.x();
viewport.y = destination.y();
viewport.width = destination.width();
viewport.height = destination.height();
}
viewport.minDepth = 0; viewport.minDepth = 0;
viewport.maxDepth = 1; viewport.maxDepth = 1;
m_devFuncs->vkCmdSetViewport(cb, 0, 1, &viewport); m_devFuncs->vkCmdSetViewport(cb, 0, 1, &viewport);

View File

@@ -846,6 +846,7 @@ VulkanWindowRenderer::createRenderer()
void void
VulkanWindowRenderer::resizeEvent(QResizeEvent *event) VulkanWindowRenderer::resizeEvent(QResizeEvent *event)
{ {
this->pixelRatio = devicePixelRatio();
onResize(width(), height()); onResize(width(), height());
QVulkanWindow::resizeEvent(event); QVulkanWindow::resizeEvent(event);
@@ -868,8 +869,10 @@ VulkanWindowRenderer::onBlit(int buf_idx, int x, int y, int w, int h)
if (isExposed()) if (isExposed())
requestUpdate(); requestUpdate();
buf_usage[0].clear(); buf_usage[0].clear();
if (origSource != source) if (origSource != source) {
this->pixelRatio = devicePixelRatio();
onResize(this->width(), this->height()); onResize(this->width(), this->height());
}
} }
uint32_t uint32_t