From fe9b2c32eea3f87f80d62431c3290b23ed8f6fc4 Mon Sep 17 00:00:00 2001 From: Riley S Date: Tue, 23 Feb 2021 20:45:48 -0500 Subject: [PATCH 1/5] Add basic OpenGL DPI Scaling --- src/camera.cpp | 6 ++++-- src/engine.cpp | 1 + src/gl/gl3device.cpp | 19 ++++++++++++++++--- src/rwengine.h | 8 ++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/camera.cpp b/src/camera.cpp index 4a87ed3..35b03d0 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -216,8 +216,10 @@ cameraSync(ObjectWithFrame *obj) inv.at.x = -inv.at.x; inv.pos.x = -inv.pos.x; - float32 xscl = 1.0f/(2.0f*cam->viewWindow.x); - float32 yscl = 1.0f/(2.0f*cam->viewWindow.y); + V2d dpiScale = engine->device.dpiScale(cam->frameBuffer->width, cam->frameBuffer->height); + + float32 xscl = 1.0f/(2.0f*cam->viewWindow.x*dpiScale.x); + float32 yscl = 1.0f/(2.0f*cam->viewWindow.y*dpiScale.y); proj.flags = 0; proj.right.x = xscl; diff --git a/src/engine.cpp b/src/engine.cpp index 74dae02..cb36bec 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -559,6 +559,7 @@ Device renderdevice = { null::im3DRenderPrimitive, null::im3DRenderIndexedPrimitive, null::im3DEnd, + null::dpiScale, null::deviceSystem }; diff --git a/src/gl/gl3device.cpp b/src/gl/gl3device.cpp index 6d1f95a..a0ed4b9 100644 --- a/src/gl/gl3device.cpp +++ b/src/gl/gl3device.cpp @@ -1253,8 +1253,9 @@ beginUpdate(Camera *cam) setViewMatrix(view); // Projection Matrix - float32 invwx = 1.0f/cam->viewWindow.x; - float32 invwy = 1.0f/cam->viewWindow.y; + V2d dpiScale = engine->device.dpiScale(cam->frameBuffer->width, cam->frameBuffer->height); + float32 invwx = 1.0f/cam->viewWindow.x/dpiScale.x; + float32 invwy = 1.0f/cam->viewWindow.y/dpiScale.y; float32 invz = 1.0f/(cam->farPlane-cam->nearPlane); proj[0] = invwx; @@ -1381,6 +1382,16 @@ showRaster(Raster *raster, uint32 flags) #endif } +static V2d dpiScale(float x, float y) +{ + V2d v; + int w = 0; + int h = 0; + glfwGetFramebufferSize(glGlobals.window, &w, &h); + v.set(w / x, h / y); + return v; +} + static bool32 rasterRenderFast(Raster *raster, int32 x, int32 y) { @@ -1388,6 +1399,7 @@ rasterRenderFast(Raster *raster, int32 x, int32 y) Raster *dst = Raster::getCurrentContext(); Gl3Raster *natdst = PLUGINOFFSET(Gl3Raster, dst, nativeRasterOffset); Gl3Raster *natsrc = PLUGINOFFSET(Gl3Raster, src, nativeRasterOffset); + V2d dpiScale = engine->device.dpiScale(src->width, src->height); switch(dst->type){ case Raster::NORMAL: @@ -1398,7 +1410,7 @@ rasterRenderFast(Raster *raster, int32 x, int32 y) setActiveTexture(0); glBindTexture(GL_TEXTURE_2D, natdst->texid); glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y, - 0, 0, src->width, src->height); + src->width/dpiScale.x, src->height/dpiScale.y, src->width, src->height); glBindTexture(GL_TEXTURE_2D, boundTexture[0]); return 1; } @@ -2032,6 +2044,7 @@ Device renderdevice = { gl3::im3DRenderPrimitive, gl3::im3DRenderIndexedPrimitive, gl3::im3DEnd, + gl3::dpiScale, #ifdef LIBRW_SDL2 gl3::deviceSystemSDL2 #else diff --git a/src/rwengine.h b/src/rwengine.h index a84aa3a..97eaea4 100644 --- a/src/rwengine.h +++ b/src/rwengine.h @@ -66,6 +66,7 @@ struct Device void (*im3DRenderIndexedPrimitive)(PrimitiveType primType, void *indices, int32 numIndices); void (*im3DEnd)(void); + V2d (*dpiScale)(float x, float y); DeviceSystem *system; }; @@ -255,6 +256,13 @@ namespace null { void im3DRenderIndexedPrimitive(PrimitiveType primType, void *indices, int32 numIndices); void im3DEnd(void); + inline V2d dpiScale(float,float) + { + V2d s; + s.set(1.f, 1.f); + return s; + } + int deviceSystem(DeviceReq req, void *arg0, int32 n); extern Device renderdevice; From 9629666136020bae98e2717cce184e83c7369e99 Mon Sep 17 00:00:00 2001 From: Riley S Date: Wed, 24 Feb 2021 00:23:06 -0500 Subject: [PATCH 2/5] Fix missing / hung image on 1.0 dpi --- src/gl/gl3device.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/gl/gl3device.cpp b/src/gl/gl3device.cpp index a0ed4b9..c5756c3 100644 --- a/src/gl/gl3device.cpp +++ b/src/gl/gl3device.cpp @@ -1388,7 +1388,10 @@ static V2d dpiScale(float x, float y) int w = 0; int h = 0; glfwGetFramebufferSize(glGlobals.window, &w, &h); - v.set(w / x, h / y); + if (w && h) + v.set(w / x, h / y); + else + v.set(1,1); return v; } @@ -1409,8 +1412,12 @@ rasterRenderFast(Raster *raster, int32 x, int32 y) case Raster::CAMERA: setActiveTexture(0); glBindTexture(GL_TEXTURE_2D, natdst->texid); - glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y, - src->width/dpiScale.x, src->height/dpiScale.y, src->width, src->height); + if(dpiScale.x != 1 || dpiScale.y != 1) + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y, + src->width/dpiScale.x, src->height/dpiScale.y, src->width, src->height); + else + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y, + 0, 0, src->width, src->height); glBindTexture(GL_TEXTURE_2D, boundTexture[0]); return 1; } From b89ab48201dfe66623bf2b791879b29664857ebe Mon Sep 17 00:00:00 2001 From: Riley S Date: Wed, 24 Feb 2021 00:37:31 -0500 Subject: [PATCH 3/5] Better non-braching check --- src/gl/gl3device.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/gl/gl3device.cpp b/src/gl/gl3device.cpp index c5756c3..47e54f8 100644 --- a/src/gl/gl3device.cpp +++ b/src/gl/gl3device.cpp @@ -1412,12 +1412,11 @@ rasterRenderFast(Raster *raster, int32 x, int32 y) case Raster::CAMERA: setActiveTexture(0); glBindTexture(GL_TEXTURE_2D, natdst->texid); - if(dpiScale.x != 1 || dpiScale.y != 1) - glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y, - src->width/dpiScale.x, src->height/dpiScale.y, src->width, src->height); - else - glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y, - 0, 0, src->width, src->height); + + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y, + src->width - src->width/dpiScale.x, src->height - src->height/dpiScale.y, + src->width, src->height); + glBindTexture(GL_TEXTURE_2D, boundTexture[0]); return 1; } From a968ea83644f51e24a4f3e410adb45576195f466 Mon Sep 17 00:00:00 2001 From: Riley S Date: Thu, 25 Feb 2021 16:11:34 -0500 Subject: [PATCH 4/5] Add default/null dpiScale for other apis --- src/d3d/d3ddevice.cpp | 1 + src/ps2/ps2device.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/src/d3d/d3ddevice.cpp b/src/d3d/d3ddevice.cpp index 922530c..ce53ad3 100644 --- a/src/d3d/d3ddevice.cpp +++ b/src/d3d/d3ddevice.cpp @@ -2003,6 +2003,7 @@ Device renderdevice = { d3d::im3DRenderPrimitive, d3d::im3DRenderIndexedPrimitive, d3d::im3DEnd, + null::dpiScale, d3d::deviceSystem, }; diff --git a/src/ps2/ps2device.cpp b/src/ps2/ps2device.cpp index 98f9914..07f017c 100644 --- a/src/ps2/ps2device.cpp +++ b/src/ps2/ps2device.cpp @@ -40,6 +40,7 @@ Device renderdevice = { null::im3DRenderPrimitive, null::im3DRenderIndexedPrimitive, null::im3DEnd, + null::dpiScale, null::deviceSystem }; From 396ec95f220f69ae1d1b78e5df0b4f1fda87014b Mon Sep 17 00:00:00 2001 From: Riley S Date: Thu, 25 Feb 2021 16:20:15 -0500 Subject: [PATCH 5/5] SDL2 checking --- src/gl/gl3device.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/gl/gl3device.cpp b/src/gl/gl3device.cpp index 47e54f8..7cdab78 100644 --- a/src/gl/gl3device.cpp +++ b/src/gl/gl3device.cpp @@ -1384,6 +1384,12 @@ showRaster(Raster *raster, uint32 flags) static V2d dpiScale(float x, float y) { +//TODO SDL2 version of this +#ifdef LIBRW_SDL2 + V2d v; + v.set(1.f, 1.f); + return v; +#else V2d v; int w = 0; int h = 0; @@ -1393,6 +1399,7 @@ static V2d dpiScale(float x, float y) else v.set(1,1); return v; +#endif } static bool32