aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Weissflog <floooh@gmail.com>2021-04-01 20:02:42 +0200
committerAndre Weissflog <floooh@gmail.com>2021-04-01 20:02:42 +0200
commit6183c725fe5c2aa4d930863773074f822b9a3888 (patch)
tree7f7a9f7e72ad00f7f86383e0ac35d654a31c1e2a
parent1d747aaf56d73f6b27411bcdc8c270f9ba5ed83e (diff)
sokol_app.h iOS: low- vs high-dpi fixes for Metal and GL.
MTKView.contentScaleFactor seems to be ignored now, which means that rendering on iOS was always in HighDPI. Instead of relying on MTKView contentScaleFactor, the drawableSize is now set explicitly. When high_dpi is enabled, the dpi_scale factor is now queried from iOS (via UIScreen.mainScreen.nativeScale). MSAA is now supported for the iOS GL backend.
-rw-r--r--sokol_app.h61
1 files changed, 37 insertions, 24 deletions
diff --git a/sokol_app.h b/sokol_app.h
index b9d774e5..98e84f59 100644
--- a/sokol_app.h
+++ b/sokol_app.h
@@ -2492,6 +2492,10 @@ _SOKOL_PRIVATE void _sapp_macos_app_event(sapp_event_type type) {
}
}
+/* NOTE: unlike the iOS version of this function, the macOS version
+ can dynamically update the DPI scaling factor when a window is moved
+ between HighDPI / LowDPI screens.
+*/
_SOKOL_PRIVATE void _sapp_macos_update_dimensions(void) {
#if defined(SOKOL_METAL)
const NSRect fb_rect = [_sapp.macos.view bounds];
@@ -2519,7 +2523,11 @@ _SOKOL_PRIVATE void _sapp_macos_update_dimensions(void) {
}
_sapp.dpi_scale = (float)_sapp.framebuffer_width / (float)_sapp.window_width;
- /* also make sure the MTKView drawable size is uptodate */
+ /* NOTE: _sapp_macos_update_dimensions() isn't called each frame, but only
+ when the window size actually changes, so resizing the MTKView's
+ in each call is fine even when MTKView doesn't ignore setting an
+ identical drawableSize.
+ */
#if defined(SOKOL_METAL)
CGSize drawable_size = { (CGFloat) _sapp.framebuffer_width, (CGFloat) _sapp.framebuffer_height };
_sapp.macos.view.drawableSize = drawable_size;
@@ -3197,8 +3205,10 @@ _SOKOL_PRIVATE void _sapp_ios_touch_event(sapp_event_type type, NSSet<UITouch *>
_SOKOL_PRIVATE void _sapp_ios_update_dimensions(void) {
CGRect screen_rect = UIScreen.mainScreen.bounds;
- _sapp.window_width = (int) screen_rect.size.width;
- _sapp.window_height = (int) screen_rect.size.height;
+ _sapp.framebuffer_width = (int)(screen_rect.size.width * _sapp.dpi_scale);
+ _sapp.framebuffer_height = (int)(screen_rect.size.height * _sapp.dpi_scale);
+ _sapp.window_width = (int)screen_rect.size.width;
+ _sapp.window_height = (int)screen_rect.size.height;
int cur_fb_width, cur_fb_height;
#if defined(SOKOL_METAL)
const CGSize fb_size = _sapp.ios.view.drawableSize;
@@ -3208,15 +3218,18 @@ _SOKOL_PRIVATE void _sapp_ios_update_dimensions(void) {
cur_fb_width = (int) _sapp.ios.view.drawableWidth;
cur_fb_height = (int) _sapp.ios.view.drawableHeight;
#endif
- const bool dim_changed =
- (_sapp.framebuffer_width != cur_fb_width) ||
- (_sapp.framebuffer_height != cur_fb_height);
- _sapp.framebuffer_width = cur_fb_width;
- _sapp.framebuffer_height = cur_fb_height;
- SOKOL_ASSERT((_sapp.framebuffer_width > 0) && (_sapp.framebuffer_height > 0));
- _sapp.dpi_scale = (float)_sapp.framebuffer_width / (float) _sapp.window_width;
- if (dim_changed && !_sapp.first_frame) {
- _sapp_ios_app_event(SAPP_EVENTTYPE_RESIZED);
+ const bool dim_changed = (_sapp.framebuffer_width != cur_fb_width) ||
+ (_sapp.framebuffer_height != cur_fb_height);
+ if (dim_changed) {
+ #if defined(SOKOL_METAL)
+ const CGSize drawable_size = { (CGFloat) _sapp.framebuffer_width, (CGFloat) _sapp.framebuffer_height };
+ _sapp.ios.view.drawableSize = drawable_size;
+ #else
+ // nothing to do here, GLKView correctly respects the view's contentScaleFactor
+ #endif
+ if (!_sapp.first_frame) {
+ _sapp_ios_app_event(SAPP_EVENTTYPE_RESIZED);
+ }
}
}
@@ -3266,14 +3279,13 @@ _SOKOL_PRIVATE void _sapp_ios_show_keyboard(bool shown) {
_sapp.window_width = screen_rect.size.width;
_sapp.window_height = screen_rect.size.height;
if (_sapp.desc.high_dpi) {
- _sapp.framebuffer_width = 2 * _sapp.window_width;
- _sapp.framebuffer_height = 2 * _sapp.window_height;
+ _sapp.dpi_scale = (float) UIScreen.mainScreen.nativeScale;
}
else {
- _sapp.framebuffer_width = _sapp.window_width;
- _sapp.framebuffer_height = _sapp.window_height;
+ _sapp.dpi_scale = 1.0f;
}
- _sapp.dpi_scale = (float)_sapp.framebuffer_width / (float) _sapp.window_width;
+ _sapp.framebuffer_width = _sapp.window_width * _sapp.dpi_scale;
+ _sapp.framebuffer_height = _sapp.window_height * _sapp.dpi_scale;
#if defined(SOKOL_METAL)
_sapp.ios.mtl_device = MTLCreateSystemDefaultDevice();
_sapp.ios.view = [[_sapp_ios_view alloc] init];
@@ -3282,12 +3294,11 @@ _SOKOL_PRIVATE void _sapp_ios_show_keyboard(bool shown) {
_sapp.ios.view.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
_sapp.ios.view.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
_sapp.ios.view.sampleCount = (NSUInteger)_sapp.sample_count;
- if (_sapp.desc.high_dpi) {
- _sapp.ios.view.contentScaleFactor = 2.0;
- }
- else {
- _sapp.ios.view.contentScaleFactor = 1.0;
- }
+ /* NOTE: iOS MTKView seems to ignore thew view's contentScaleFactor
+ and automatically renders at Retina resolution. We'll disable
+ autoResize and instead do the resizing in _sapp_ios_update_dimensions()
+ */
+ _sapp.ios.view.autoResizeDrawable = false;
_sapp.ios.view.userInteractionEnabled = YES;
_sapp.ios.view.multipleTouchEnabled = YES;
_sapp.ios.view_ctrl = [[UIViewController alloc] init];
@@ -3310,11 +3321,13 @@ _SOKOL_PRIVATE void _sapp_ios_show_keyboard(bool shown) {
_sapp.ios.view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
_sapp.ios.view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
_sapp.ios.view.drawableStencilFormat = GLKViewDrawableStencilFormatNone;
- _sapp.ios.view.drawableMultisample = GLKViewDrawableMultisampleNone; /* FIXME */
+ GLKViewDrawableMultisample msaa = _sapp.sample_count > 1 ? GLKViewDrawableMultisample4X : GLKViewDrawableMultisampleNone;
+ _sapp.ios.view.drawableMultisample = msaa;
_sapp.ios.view.context = _sapp.ios.eagl_ctx;
_sapp.ios.view.enableSetNeedsDisplay = NO;
_sapp.ios.view.userInteractionEnabled = YES;
_sapp.ios.view.multipleTouchEnabled = YES;
+ // on GLKView, contentScaleFactor appears to work just fine!
if (_sapp.desc.high_dpi) {
_sapp.ios.view.contentScaleFactor = 2.0;
}