diff --git a/Components/Bites/src/OgreApplicationContextBase.cpp b/Components/Bites/src/OgreApplicationContextBase.cpp index 016adbcc9c9..f97428f472d 100644 --- a/Components/Bites/src/OgreApplicationContextBase.cpp +++ b/Components/Bites/src/OgreApplicationContextBase.cpp @@ -452,21 +452,19 @@ void ApplicationContextBase::_destroyWindow(const NativeWindowPair& win) void ApplicationContextBase::_fireInputEvent(const Event& event, uint32_t windowID) const { Event scaled = event; -#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE + // events arrive in view points, while Ogre uses pixels throughout + // (assumes all windows have the same scale) + float viewScale = getRenderWindow()->getViewPointToPixelScale(); if (event.type == MOUSEMOTION) { - // assumes all windows have the same scale - float viewScale = getRenderWindow()->getViewPointToPixelScale(); scaled.motion.x *= viewScale; scaled.motion.y *= viewScale; } else if(event.type == MOUSEBUTTONDOWN || event.type == MOUSEBUTTONUP) { - float viewScale = getRenderWindow()->getViewPointToPixelScale(); scaled.button.x *= viewScale; scaled.button.y *= viewScale; } -#endif for(const auto & li : mInputListeners) { diff --git a/Components/Bites/src/OgreApplicationContextQt.cpp b/Components/Bites/src/OgreApplicationContextQt.cpp index 26cfab7e82f..45b550bc1ca 100644 --- a/Components/Bites/src/OgreApplicationContextQt.cpp +++ b/Components/Bites/src/OgreApplicationContextQt.cpp @@ -172,6 +172,9 @@ namespace OgreBites p.miscParams["externalWlDisplay"] = Ogre::StringConverter::toString(size_t(display)); p.miscParams["externalWlSurface"] = Ogre::StringConverter::toString(size_t(surface)); + // an explicit value from the render system options or the caller wins + if (p.miscParams.find("contentScalingFactor") == p.miscParams.end()) + p.miscParams["contentScalingFactor"] = Ogre::StringConverter::toString(window->devicePixelRatio()); #endif } if (!mWindows.empty()) diff --git a/Components/Bites/src/OgreApplicationContextSDL.cpp b/Components/Bites/src/OgreApplicationContextSDL.cpp index 57a9da1de60..d5ad3da1609 100644 --- a/Components/Bites/src/OgreApplicationContextSDL.cpp +++ b/Components/Bites/src/OgreApplicationContextSDL.cpp @@ -21,6 +21,21 @@ namespace OgreBites { +// returns the window size in view points and the scale to its pixel size +static void getWindowSize(SDL_Window* win, int& width, int& height, float& scale) +{ + SDL_GetWindowSize(win, &width, &height); + scale = 1.0f; +#if SDL_VERSION_ATLEAST(2, 26, 0) + int pixelWidth, pixelHeight; + SDL_GetWindowSizeInPixels(win, &pixelWidth, &pixelHeight); + if (width > 0 && pixelWidth > 0) + scale = float(pixelWidth) / width; + else if (height > 0 && pixelHeight > 0) + scale = float(pixelHeight) / height; +#endif +} + ApplicationContextSDL::ApplicationContextSDL(const Ogre::String& appName) : ApplicationContextBase(appName) { } @@ -70,6 +85,12 @@ NativeWindowPair ApplicationContextSDL::createWindow(const Ogre::String& name, O } int flags = p.useFullScreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE; +#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX + // ALLOW_HIGHDPI makes SDL size the Wayland drawable in physical pixels and + // enables wp_fractional_scale. Ogre still gets view point dimensions and + // allocates the backbuffer in pixels using contentScalingFactor. + flags |= SDL_WINDOW_ALLOW_HIGHDPI; +#endif int d = Ogre::StringConverter::parseInt(miscParams["monitorIndex"], 1) - 1; ret.native = SDL_CreateWindow(p.name.c_str(), SDL_WINDOWPOS_UNDEFINED_DISPLAY(d), @@ -91,6 +112,15 @@ NativeWindowPair ApplicationContextSDL::createWindow(const Ogre::String& name, O Ogre::LogManager::getSingleton().logMessage("[SDL] Creating Wayland window"); p.miscParams["externalWlDisplay"] = Ogre::StringConverter::toString(size_t(wmInfo.info.wl.display)); p.miscParams["externalWlSurface"] = Ogre::StringConverter::toString(size_t(wmInfo.info.wl.surface)); + + int widthPt, heightPt; + float scale; + getWindowSize(ret.native, widthPt, heightPt, scale); + p.width = widthPt; + p.height = heightPt; + // an explicit value from the render system options or the caller wins + if (p.miscParams.find("contentScalingFactor") == p.miscParams.end()) + p.miscParams["contentScalingFactor"] = Ogre::StringConverter::toString(scale); #endif } else if (wmInfo.subsystem == SDL_SYSWM_X11) diff --git a/OgreMain/include/OgreRenderSystem.h b/OgreMain/include/OgreRenderSystem.h index 8d3000cb8cf..d4c3c5634c1 100644 --- a/OgreMain/include/OgreRenderSystem.h +++ b/OgreMain/include/OgreRenderSystem.h @@ -237,7 +237,7 @@ namespace Ogre | FSAA | 0 | concatenation of "FSAA" and "FSAAHint" as in @ref _createRenderWindow | | Video Mode | - | Window resolution | | Display Frequency | - | "displayFrequency" in @ref _createRenderWindow | - | Content Scaling Factor | 1.0 | "contentScalingFactor" in @ref _createRenderWindow | + | Content Scaling Factor | Auto | pixels per view point; "contentScalingFactor" in @ref _createRenderWindow. "Auto" detects the scale of the monitor the window is created on | @param name The name of the option to alter. @param @@ -414,7 +414,7 @@ namespace Ogre | useNVPerfHUD | true, false | false | Enable the use of nVidia NVPerfHUD | D3D | | depthBuffer | true, false | true | Use depth buffer | D3D | | NSOpenGLCPSurfaceOrder | -1 or 1 | 1 | [NSOpenGLCPSurfaceOrder](https://developer.apple.com/documentation/appkit/nsopenglcpsurfaceorder) | OSX | - | contentScalingFactor | Positive Float | The default content scaling factor of the screen | On IOS specifies the CAEAGLLayer content scaling factor. This can be useful to limit the resolution of the OpenGL ES backing store. For example, the iPhone 4's native resolution is 960 x 640\. Windows are always 320 x 480, if you would like to limit the display to 720 x 480, specify 1.5 as the scaling factor. | OSX, iOS, Android | + | contentScalingFactor | Positive Float | The default content scaling factor of the screen | On IOS specifies the CAEAGLLayer content scaling factor. This can be useful to limit the resolution of the OpenGL ES backing store. For example, the iPhone 4's native resolution is 960 x 640\. Windows are always 320 x 480, if you would like to limit the display to 720 x 480, specify 1.5 as the scaling factor. On Wayland it is the number of physical pixels per view point, which the backends use to allocate the backbuffer at native resolution. | OSX, iOS, Android, Wayland | | externalViewHandle | UIView pointer as an integer | 0 | External view handle, for rendering OGRE render in an existing view | iOS | | externalViewControllerHandle | UIViewController pointer as an integer | 0 | External view controller handle, for embedding OGRE in an existing view controller | iOS | | externalSharegroup | EAGLSharegroup pointer as an integer | 0 | External sharegroup, used to shared GL resources between contexts | iOS | diff --git a/OgreMain/src/OgreRenderSystem.cpp b/OgreMain/src/OgreRenderSystem.cpp index 8f8aef086e7..0bba12587c9 100644 --- a/OgreMain/src/OgreRenderSystem.cpp +++ b/OgreMain/src/OgreRenderSystem.cpp @@ -224,7 +224,9 @@ namespace Ogre { if((opt = mOptions.find("Display Frequency")) != end) miscParams.emplace("displayFrequency", opt->second.currentValue); - if((opt = mOptions.find("Content Scaling Factor")) != end) + // "Auto" means the window provider detects the scale of the monitor the + // window is created on; there is no usable value to pass along. + if ((opt = mOptions.find("Content Scaling Factor")) != end && opt->second.currentValue != "Auto") miscParams["contentScalingFactor"] = opt->second.currentValue; if((opt = mOptions.find("Rendering Device")) != end) @@ -886,6 +888,20 @@ namespace Ogre { optHDRDisplay.currentValue = optHDRDisplay.possibleValues[0]; mOptions[optHDRDisplay.name] = optHDRDisplay; + // Windows are sized in view points while the backbuffer is allocated in + // pixels. "Auto" lets the window provider detect the scale of the monitor + // the window is created on; the list is for discoverability only, the + // value can be anything the backend understands. + if (mOptions.find("Content Scaling Factor") == mOptions.end()) + { + ConfigOption optContentScaling; + optContentScaling.name = "Content Scaling Factor"; + optContentScaling.immutable = false; + optContentScaling.possibleValues = {"Auto", "1.0", "1.25", "1.5", "1.75", "2.0", "3.0"}; + optContentScaling.currentValue = optContentScaling.possibleValues.front(); + mOptions[optContentScaling.name] = optContentScaling; + } + #if OGRE_NO_QUAD_BUFFER_STEREO == 0 ConfigOption optStereoMode; optStereoMode.name = "Frame Sequential Stereo"; diff --git a/RenderSystems/GLES2/src/EAGL/OgreEAGL2Support.mm b/RenderSystems/GLES2/src/EAGL/OgreEAGL2Support.mm index f73672e4acb..64fc7386f66 100644 --- a/RenderSystems/GLES2/src/EAGL/OgreEAGL2Support.mm +++ b/RenderSystems/GLES2/src/EAGL/OgreEAGL2Support.mm @@ -57,7 +57,6 @@ of this software and associated documentation files (the "Software"), to deal ConfigOptionMap EAGL2Support::getConfigOptions(void) { ConfigOptionMap mOptions; - ConfigOption optContentScalingFactor; // Get the application frame size. On all iPhones(including iPhone 4) this will be 320 x 480 // The iPad, at least with iPhone OS 3.2 will report 768 x 1024 @@ -67,21 +66,11 @@ of this software and associated documentation files (the "Software"), to deal mVideoModes.push_back({320, 568, 0}); mVideoModes.push_back({768, 1024, 0}); - optContentScalingFactor.name = "Content Scaling Factor"; - optContentScalingFactor.possibleValues.push_back( "1.0" ); - optContentScalingFactor.possibleValues.push_back( "1.33" ); - optContentScalingFactor.possibleValues.push_back( "1.5" ); - optContentScalingFactor.possibleValues.push_back( "2.0" ); - optContentScalingFactor.currentValue = StringConverter::toString([UIScreen mainScreen].scale); - optContentScalingFactor.immutable = false; - mFSAALevels.push_back(0); mFSAALevels.push_back(2); mFSAALevels.push_back(4); mFSAALevels.push_back(8); - mOptions[optContentScalingFactor.name] = optContentScalingFactor; - return mOptions; } diff --git a/RenderSystems/GLES2/src/EAGL/OgreEAGL2Window.mm b/RenderSystems/GLES2/src/EAGL/OgreEAGL2Window.mm index bf6c9cfbcac..d8ced738570 100644 --- a/RenderSystems/GLES2/src/EAGL/OgreEAGL2Window.mm +++ b/RenderSystems/GLES2/src/EAGL/OgreEAGL2Window.mm @@ -38,6 +38,7 @@ of this software and associated documentation files (the "Software"), to deal #import #import +#import #include "ARCMacros.h" #include "OgreDepthBuffer.h" @@ -59,7 +60,7 @@ of this software and associated documentation files (the "Software"), to deal mUsingExternalView(false), mUsingExternalViewController(false), mIsContentScalingSupported(EAGLCurrentOSVersion >= 4.0), - mContentScalingFactor(1.0), + mContentScalingFactor([[UIScreen mainScreen] scale]), mGLSupport(glsupport), mContext(NULL), mWindow(nil), diff --git a/RenderSystems/GLSupport/include/EGL/Wayland/OgreWaylandEGLWindow.h b/RenderSystems/GLSupport/include/EGL/Wayland/OgreWaylandEGLWindow.h index 295a7184b4c..33e5c38e000 100644 --- a/RenderSystems/GLSupport/include/EGL/Wayland/OgreWaylandEGLWindow.h +++ b/RenderSystems/GLSupport/include/EGL/Wayland/OgreWaylandEGLWindow.h @@ -16,6 +16,7 @@ class _OgrePrivate WaylandEGLWindow : public EGLWindow protected: WaylandEGLSupport* mGLSupport; wl_surface* mWlSurface; + float mContentScalingFactor = 1.0f; void initNativeCreatedWindow(const NameValuePairList* miscParams); void createNativeWindow(uint& width, uint& height); @@ -28,6 +29,8 @@ class _OgrePrivate WaylandEGLWindow : public EGLWindow void create(const String& name, unsigned int width, unsigned int height, bool fullScreen, const NameValuePairList* miscParams) override; + + float getViewPointToPixelScale() override { return mContentScalingFactor; } }; } // namespace Ogre diff --git a/RenderSystems/GLSupport/src/EGL/Wayland/OgreWaylandEGLWindow.cpp b/RenderSystems/GLSupport/src/EGL/Wayland/OgreWaylandEGLWindow.cpp index 043ca4cbaf0..c34373e8d26 100644 --- a/RenderSystems/GLSupport/src/EGL/Wayland/OgreWaylandEGLWindow.cpp +++ b/RenderSystems/GLSupport/src/EGL/Wayland/OgreWaylandEGLWindow.cpp @@ -14,6 +14,7 @@ #include #include +#include #include namespace Ogre @@ -75,14 +76,18 @@ void WaylandEGLWindow::createNativeWindow(uint& width, uint& height) wl_display_flush(reinterpret_cast(mNativeDisplay)); } -void WaylandEGLWindow::resize(uint width, uint height) +void WaylandEGLWindow::resize(uint widthPt, uint heightPt) { - if (mClosed) { return; } + // the compositor is told a size in view points, but the backbuffer has to + // be allocated in physical pixels for a 1:1 mapping (HiDPI / fractional scaling) + const uint width = std::lround(widthPt * mContentScalingFactor); + const uint height = std::lround(heightPt * mContentScalingFactor); + if (mWidth == width && mHeight == height) { return; @@ -109,9 +114,19 @@ void WaylandEGLWindow::windowMovedOrResized() if (mClosed || !mWindow) return; + // the attached size is already the physical backbuffer size int width, height; wl_egl_window_get_attached_size(reinterpret_cast(mWindow), &width, &height); - resize(width, height); + if (width <= 0 || height <= 0) + return; + + const uint oldWidth = mWidth; + const uint oldHeight = mHeight; + RenderWindow::resize(width, height); + wl_surface_damage(mWlSurface, 0, 0, oldWidth, oldHeight); + wl_surface_commit(mWlSurface); + wl_display_dispatch_pending(reinterpret_cast(mNativeDisplay)); + wl_display_flush(reinterpret_cast(mNativeDisplay)); } void WaylandEGLWindow::create(const String& name, uint width, uint height, bool fullScreen, @@ -192,6 +207,13 @@ void WaylandEGLWindow::create(const String& name, uint width, uint height, bool mHwGamma = StringConverter::parseBool(opt->second); } + if ((opt = miscParams->find("contentScalingFactor")) != end) + { + const float scale = StringConverter::parseReal(opt->second); + if (scale > 0.0f) + mContentScalingFactor = scale; + } + if ((opt = miscParams->find("hdrDisplay")) != end) { mHdrDisplay = StringConverter::parseBool(opt->second); @@ -260,7 +282,12 @@ void WaylandEGLWindow::create(const String& name, uint width, uint height, bool mGLSupport->switchMode(width, height, frequency); } - createNativeWindow(width, height); + // width/height are view points, while the EGL window and RenderWindow use + // the physical pixel size the compositor will map 1:1 to the display + uint pixelWidth = std::lround(width * mContentScalingFactor); + uint pixelHeight = std::lround(height * mContentScalingFactor); + + createNativeWindow(pixelWidth, pixelHeight); mEglSurface = createSurfaceFromWindow(mGLSupport->getGLDisplay(), mWindow); mContext = createEGLContext(eglContext); @@ -270,8 +297,8 @@ void WaylandEGLWindow::create(const String& name, uint width, uint height, bool setVSyncEnabled(vsync); mName = name; - mWidth = width; - mHeight = height; + mWidth = pixelWidth; + mHeight = pixelHeight; finaliseWindow(); wl_surface_commit(mWlSurface); diff --git a/RenderSystems/GLSupport/src/OSX/OgreOSXCocoaWindow.mm b/RenderSystems/GLSupport/src/OSX/OgreOSXCocoaWindow.mm index 1e45bfaef1e..989cec36aa6 100644 --- a/RenderSystems/GLSupport/src/OSX/OgreOSXCocoaWindow.mm +++ b/RenderSystems/GLSupport/src/OSX/OgreOSXCocoaWindow.mm @@ -71,7 +71,7 @@ - (BOOL)acceptsFirstResponder CocoaWindow::CocoaWindow() : GLWindow(), mWindow(nil), mView(nil), mGLContext(nil), mGLPixelFormat(nil), mWindowOriginPt(NSZeroPoint), mHasResized(false), mWindowTitle(""), - mUseOgreGLView(true), mContentScalingFactor(1.0), mStyleMask(NSResizableWindowMask|NSTitledWindowMask) + mUseOgreGLView(true), mContentScalingFactor([[NSScreen mainScreen] backingScaleFactor]), mStyleMask(NSResizableWindowMask|NSTitledWindowMask) { // Set vsync by default to save battery and reduce tearing } diff --git a/RenderSystems/GLSupport/src/OSX/OgreOSXGL3PlusSupport.mm b/RenderSystems/GLSupport/src/OSX/OgreOSXGL3PlusSupport.mm index bc2eca44674..8af88990b3a 100644 --- a/RenderSystems/GLSupport/src/OSX/OgreOSXGL3PlusSupport.mm +++ b/RenderSystems/GLSupport/src/OSX/OgreOSXGL3PlusSupport.mm @@ -39,7 +39,6 @@ of this software and associated documentation files (the "Software"), to deal #import #import -#import namespace Ogre { @@ -52,20 +51,12 @@ of this software and associated documentation files (the "Software"), to deal { ConfigOptionMap mOptions; ConfigOption optBitDepth; - ConfigOption optContentScalingFactor; optBitDepth.name = "Colour Depth"; optBitDepth.currentValue = "32"; optBitDepth.immutable = false; - optContentScalingFactor.name = "Content Scaling Factor"; - optContentScalingFactor.possibleValues.push_back( "1.0" ); - optContentScalingFactor.possibleValues.push_back( "2.0" ); - optContentScalingFactor.currentValue = StringConverter::toString((float)[NSScreen mainScreen].backingScaleFactor); - optContentScalingFactor.immutable = false; - mOptions[ optBitDepth.name ] = optBitDepth; - mOptions[ optContentScalingFactor.name ] = optContentScalingFactor; CGLRendererInfoObj rend; GLint nrend = 0, maxSamples = 0; diff --git a/RenderSystems/Vulkan/include/OgreVulkanWindow.h b/RenderSystems/Vulkan/include/OgreVulkanWindow.h index 780d1a66a70..e99de94d10f 100644 --- a/RenderSystems/Vulkan/include/OgreVulkanWindow.h +++ b/RenderSystems/Vulkan/include/OgreVulkanWindow.h @@ -62,6 +62,7 @@ namespace Ogre bool mLowestLatencyVSync; bool mHdrDisplay; + float mContentScalingFactor = 1.0f; bool mVisible; bool mHidden; @@ -133,6 +134,8 @@ namespace Ogre void windowMovedOrResized() override; + float getViewPointToPixelScale() override { return mContentScalingFactor; } + void _setDevice( VulkanDevice *device ); void create(const String& name, unsigned int widthPt, unsigned int heightPt, bool fullScreen, diff --git a/RenderSystems/Vulkan/src/OgreVulkanWindow.cpp b/RenderSystems/Vulkan/src/OgreVulkanWindow.cpp index 2e0cdc2dfaa..46fda90aa66 100644 --- a/RenderSystems/Vulkan/src/OgreVulkanWindow.cpp +++ b/RenderSystems/Vulkan/src/OgreVulkanWindow.cpp @@ -37,6 +37,9 @@ THE SOFTWARE. #include "OgrePixelFormat.h" #include "OgreVulkanTextureGpuManager.h" #include "OgreHardwarePixelBuffer.h" + +#include + #if OGRE_PLATFORM == OGRE_PLATFORM_ANDROID #include #elif OGRE_PLATFORM == OGRE_PLATFORM_WIN32 @@ -348,14 +351,17 @@ namespace Ogre mTexture->_setCurrentImage( mSwapchainImages[imageIdx], imageIdx ); } - void VulkanWindow::resize(uint width, uint height) + void VulkanWindow::resize(uint widthPt, uint heightPt) { if (mClosed) return; - if (width != 0 && height != 0) + if (widthPt != 0 && heightPt != 0) { - RenderWindow::resize(width, height); + // the compositor is told a size in view points, but the swapchain has to be + // allocated in physical pixels for a 1:1 mapping + RenderWindow::resize(std::lround(widthPt * mContentScalingFactor), + std::lround(heightPt * mContentScalingFactor)); } } @@ -478,8 +484,6 @@ namespace Ogre mVisible = true; mClosed = false; mHwGamma = false; - mWidth = width; - mHeight = height; mFSAA = 1; size_t wlDisplay = 0; @@ -519,8 +523,18 @@ namespace Ogre mHdrDisplay = StringConverter::parseBool( opt->second ); mHwGamma = mHdrDisplay; // hdr implies hw gamma } + opt = miscParams->find("contentScalingFactor"); + if (opt != end) + { + const float scale = StringConverter::parseReal(opt->second); + if (scale > 0.0f) + mContentScalingFactor = scale; + } } + mWidth = std::lround(width * mContentScalingFactor); + mHeight = std::lround(height * mContentScalingFactor); + #ifdef VK_USE_PLATFORM_WAYLAND_KHR OgreAssert( mWindowHandle && wlDisplay, "externalWlSurface and externalWlDisplay required"); createSurface(mWindowHandle, wlDisplay);