diff options
author | 2019-10-13 13:50:38 +0200 | |
---|---|---|
committer | 2019-10-13 07:50:38 -0400 | |
commit | 0837e7451a84f95c29dbdb9bd6b8b931fee1635d (patch) | |
tree | 626bcbd250a7fbebdf5958a288d2f438d4f9fb5a /3rdparty/bgfx/examples/common/entry/entry_osx.mm | |
parent | c913ccb59d713ce0b91135520719ac5385e54358 (diff) |
WIP: sync bgfx, bx and bimg with latest upstream (#5723)
* Sync with bgfx upstream revision b91d0b6
* Sync with bx upstream revision d60912b
* Sync with bimg upstream revision bd81f60
* Add astc-codec decoder
* Rename VertexDecl to VertexLayout
* Rename UniformType enum Int1 to Sampler.
* Add NVN stub
* Fix unused-const-variable error on macOS
* Drop redundant explicit language parameters
buildoptions_cpp are only applied to c++ files and buildoptions_objcpp are only
applied to objective c++ files. As such, hardcoding -x offers no benefit while
preventing overrides (such as one needed by 3rdparty/bgfx/src/renderer_vk.cpp on
macOS) from working.
* Re-introduce -x c++ in places where C code is compiled as C++ to prevent clang from throwing a warning
* Build bgfx as Objective-C++ on macOS
It is needed due to included headers
* Enable Direct3D12 and Vulkan bgfx rendering backends
* Enable building of spirv shaders
* Properly escape /c in cmd call
* Comment out dx12 bgfx renderer
* Honor VERBOSE setting during shaders build
* Only invert hlsl shader XYZ_TO_sRGB matrix for opengl
* Add spirv shaders
* OpenGL ES needs transposed matrix too
* Metal needs transposed matrix as well
Diffstat (limited to '3rdparty/bgfx/examples/common/entry/entry_osx.mm')
-rw-r--r-- | 3rdparty/bgfx/examples/common/entry/entry_osx.mm | 269 |
1 files changed, 127 insertions, 142 deletions
diff --git a/3rdparty/bgfx/examples/common/entry/entry_osx.mm b/3rdparty/bgfx/examples/common/entry/entry_osx.mm index 9bd53fb1362..7b5959a43bd 100644 --- a/3rdparty/bgfx/examples/common/entry/entry_osx.mm +++ b/3rdparty/bgfx/examples/common/entry/entry_osx.mm @@ -1,5 +1,5 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. + * Copyright 2011-2019 Branimir Karadzic. All rights reserved. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause */ @@ -30,7 +30,6 @@ @interface Window : NSObject<NSWindowDelegate> { - uint32_t windowCount; } + (Window*)sharedDelegate; @@ -86,21 +85,21 @@ namespace entry } MainThreadEntry* self = (MainThreadEntry*)_userData; - return main(self->m_argc, self->m_argv); + uint32_t result = main(self->m_argc, self->m_argv); + [NSApp terminate:nil]; + return result; } }; struct Context { Context() - : m_windowsCreated(0) - , m_scrollf(0.0f) + : m_scrollf(0.0f) , m_mx(0) , m_my(0) , m_scroll(0) , m_style(0) , m_exit(false) - , m_fullscreen(false) { s_translateKey[27] = Key::Esc; s_translateKey[uint8_t('\r')] = Key::Return; @@ -151,6 +150,11 @@ namespace entry s_translateKey[uint8_t(ch)] = s_translateKey[uint8_t(ch - ' ')] = Key::KeyA + (ch - 'a'); } + + for(int ii=0; ii<ENTRY_CONFIG_MAX_WINDOWS; ++ii) + { + m_window[ii] = NULL; + } } NSEvent* waitEvent() @@ -193,10 +197,14 @@ namespace entry uint8_t translateModifiers(int flags) { return 0 - | (0 != (flags & NSEventModifierFlagShift ) ) ? Modifier::LeftShift | Modifier::RightShift : 0 - | (0 != (flags & NSEventModifierFlagOption ) ) ? Modifier::LeftAlt | Modifier::RightAlt : 0 - | (0 != (flags & NSEventModifierFlagControl) ) ? Modifier::LeftCtrl | Modifier::RightCtrl : 0 - | (0 != (flags & NSEventModifierFlagCommand) ) ? Modifier::LeftMeta | Modifier::RightMeta : 0 + | (0 != (flags & NX_DEVICELSHIFTKEYMASK ) ) ? Modifier::LeftShift : 0 + | (0 != (flags & NX_DEVICERSHIFTKEYMASK ) ) ? Modifier::RightShift : 0 + | (0 != (flags & NX_DEVICELALTKEYMASK ) ) ? Modifier::LeftAlt : 0 + | (0 != (flags & NX_DEVICERALTKEYMASK ) ) ? Modifier::RightAlt : 0 + | (0 != (flags & NX_DEVICELCTLKEYMASK ) ) ? Modifier::LeftCtrl : 0 + | (0 != (flags & NX_DEVICERCTLKEYMASK ) ) ? Modifier::RightCtrl : 0 + | (0 != (flags & NX_DEVICELCMDKEYMASK) ) ? Modifier::LeftMeta : 0 + | (0 != (flags & NX_DEVICERCMDKEYMASK) ) ? Modifier::RightMeta : 0 ; } @@ -258,8 +266,18 @@ namespace entry { NSEventType eventType = [event type]; - NSWindow *window = [NSApp keyWindow]; - WindowHandle handle = handleFromWindow(window); + NSWindow *window = [event window]; + WindowHandle handle = {UINT16_MAX}; + if (nil != window) + { + handle = findHandle(window); + } + if (!isValid(handle)) + { + [NSApp sendEvent:event]; + [NSApp updateWindows]; + return true; + } switch (eventType) { @@ -367,7 +385,7 @@ namespace entry void windowDidResize(NSWindow *window) { - WindowHandle handle = handleFromWindow(window); + WindowHandle handle = findHandle(window); NSRect originalFrame = [window frame]; NSRect rect = [window contentRectForFrameRect: originalFrame]; uint32_t width = uint32_t(rect.size.width); @@ -381,14 +399,14 @@ namespace entry void windowDidBecomeKey(NSWindow *window) { - WindowHandle handle = handleFromWindow(window); + WindowHandle handle = findHandle(window); m_eventQueue.postSuspendEvent(handle, Suspend::WillResume); m_eventQueue.postSuspendEvent(handle, Suspend::DidResume); } void windowDidResignKey(NSWindow *window) { - WindowHandle handle = handleFromWindow(window); + WindowHandle handle = findHandle(window); m_eventQueue.postSuspendEvent(handle, Suspend::WillSuspend); m_eventQueue.postSuspendEvent(handle, Suspend::DidSuspend); } @@ -459,13 +477,13 @@ namespace entry while (!(m_exit = [dg applicationHasTerminated]) ) { - @autoreleasepool - { - bgfx::renderFrame(); - } + bgfx::renderFrame(); - while (dispatchEvent(peekEvent() ) ) + @autoreleasepool { + while (dispatchEvent(peekEvent() ) ) + { + } } } @@ -477,31 +495,28 @@ namespace entry return 0; } - bool isValid(WindowHandle _handle) - { - return m_windowAlloc.isValid(_handle.idx); - } - - WindowHandle handleFromWindow(NSWindow *window) + WindowHandle findHandle(NSWindow *_window) { - uint16_t windowIdx = 0; - for (uint16_t i = 0; i < m_windowsCreated; i++) + bx::MutexScope scope(m_lock); + for (uint16_t ii = 0, num = m_windowAlloc.getNumHandles(); ii < num; ++ii) { - if (window == m_window[i]) + uint16_t idx = m_windowAlloc.getHandleAt(ii); + if (_window == m_window[idx]) { - windowIdx = i; - break; + WindowHandle handle = { idx }; + return handle; } } - WindowHandle handle = { windowIdx }; - return handle; + + WindowHandle invalid = { UINT16_MAX }; + return invalid; } EventQueue m_eventQueue; + bx::Mutex m_lock; bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc; NSWindow* m_window[ENTRY_CONFIG_MAX_WINDOWS]; - SInt32 m_windowsCreated; NSRect m_windowFrame; float m_scrollf; @@ -510,7 +525,6 @@ namespace entry int32_t m_scroll; int32_t m_style; bool m_exit; - bool m_fullscreen; }; static Context s_ctx; @@ -534,104 +548,111 @@ namespace entry { BX_UNUSED(_flags); - uint16_t handleIdx = IncrementAtomic(&s_ctx.m_windowsCreated); + bx::MutexScope scope(s_ctx.m_lock); + WindowHandle handle = { s_ctx.m_windowAlloc.alloc() }; - if (handleIdx >= ENTRY_CONFIG_MAX_WINDOWS) + if (UINT16_MAX != handle.idx) { - return { UINT16_MAX }; - } + void (^createWindowBlock)(void) = ^(void) { + NSRect rect = NSMakeRect(_x, _y, _width, _height); + NSWindow* window = [ + [NSWindow alloc] + initWithContentRect:rect + styleMask:s_ctx.m_style + backing:NSBackingStoreBuffered defer:NO + ]; + NSString* appName = [NSString stringWithUTF8String:_title]; + [window setTitle:appName]; + [window makeKeyAndOrderFront:window]; + [window setAcceptsMouseMovedEvents:YES]; + [window setBackgroundColor:[NSColor blackColor]]; + [[Window sharedDelegate] windowCreated:window]; + + s_ctx.m_window[handle.idx] = window; - WindowHandle handle = { handleIdx }; - - void (^createWindowBlock)(void) = ^(void) { - s_ctx.m_windowAlloc.alloc(); - NSRect rect = NSMakeRect(_x, _y, _width, _height); - NSWindow* window = [[NSWindow alloc] - initWithContentRect:rect - styleMask:s_ctx.m_style - backing:NSBackingStoreBuffered defer:NO - ]; - NSString* appName = [NSString stringWithUTF8String:_title]; - [window setTitle:appName]; - [window makeKeyAndOrderFront:window]; - [window setAcceptsMouseMovedEvents:YES]; - [window setBackgroundColor:[NSColor blackColor]]; - [[Window sharedDelegate] windowCreated:window]; - - s_ctx.m_window[handleIdx] = window; - - if(s_ctx.m_windowsCreated > 1) - { s_ctx.m_eventQueue.postSizeEvent(handle, _width, _height); s_ctx.m_eventQueue.postWindowEvent(handle, window); - } - }; + }; - if ([NSThread isMainThread]) - { - createWindowBlock(); - } - else - { - dispatch_async(dispatch_get_main_queue(), createWindowBlock); + if ([NSThread isMainThread]) + { + createWindowBlock(); + } + else + { + dispatch_async(dispatch_get_main_queue(), createWindowBlock); + } } return handle; } - void destroyWindow(WindowHandle _handle) + void destroyWindow(WindowHandle _handle, bool _closeWindow) { - if (s_ctx.isValid(_handle) ) + if (isValid(_handle)) { dispatch_async(dispatch_get_main_queue() - , ^{ - [s_ctx.m_window[_handle.idx] performClose: nil]; - }); + , ^(void){ + NSWindow *window = s_ctx.m_window[_handle.idx]; + if ( NULL != window) + { + s_ctx.m_eventQueue.postWindowEvent(_handle); + s_ctx.m_window[_handle.idx] = NULL; + if ( _closeWindow ) + { + [window close]; + } + + if (0 == _handle.idx) + { + [NSApp terminate:nil]; + } + } + }); + + bx::MutexScope scope(s_ctx.m_lock); + s_ctx.m_windowAlloc.free(_handle.idx); } } + void destroyWindow(WindowHandle _handle) + { + destroyWindow(_handle, true); + } + void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y) { - if (s_ctx.isValid(_handle) ) - { - NSWindow* window = s_ctx.m_window[_handle.idx]; - NSScreen* screen = [window screen]; + dispatch_async(dispatch_get_main_queue() + , ^{ + NSWindow* window = s_ctx.m_window[_handle.idx]; + NSScreen* screen = [window screen]; - NSRect screenRect = [screen frame]; - CGFloat menuBarHeight = [[[NSApplication sharedApplication] mainMenu] menuBarHeight]; + NSRect screenRect = [screen frame]; + CGFloat menuBarHeight = [[[NSApplication sharedApplication] mainMenu] menuBarHeight]; - NSPoint position = { float(_x), screenRect.size.height - menuBarHeight - float(_y) }; + NSPoint position = { float(_x), screenRect.size.height - menuBarHeight - float(_y) }; - dispatch_async(dispatch_get_main_queue() - , ^{ [window setFrameTopLeftPoint: position]; }); - } } void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height) { - if (s_ctx.isValid(_handle) ) - { - NSSize size = { float(_width), float(_height) }; - dispatch_async(dispatch_get_main_queue() + NSSize size = { float(_width), float(_height) }; + dispatch_async(dispatch_get_main_queue() , ^{ [s_ctx.m_window[_handle.idx] setContentSize: size]; }); - } } void setWindowTitle(WindowHandle _handle, const char* _title) { - if (s_ctx.isValid(_handle) ) - { - NSString* title = [[NSString alloc] initWithCString:_title encoding:1]; - dispatch_async(dispatch_get_main_queue() + NSString* title = [[NSString alloc] initWithCString:_title encoding:1]; + dispatch_async(dispatch_get_main_queue() , ^{ [s_ctx.m_window[_handle.idx] setTitle: title]; + [title release]; }); - [title release]; - } } void setWindowFlags(WindowHandle _handle, uint32_t _flags, bool _enabled) @@ -641,37 +662,11 @@ namespace entry void toggleFullscreen(WindowHandle _handle) { - if (s_ctx.isValid(_handle) ) - { - NSWindow* window = s_ctx.m_window[_handle.idx]; - NSScreen* screen = [window screen]; - NSRect screenRect = [screen frame]; - - if (!s_ctx.m_fullscreen) - { - s_ctx.m_style &= ~NSWindowStyleMaskTitled; - dispatch_async(dispatch_get_main_queue() - , ^{ - [NSMenu setMenuBarVisible: false]; - [window setStyleMask: s_ctx.m_style]; - [window setFrame:screenRect display:YES]; - }); - - s_ctx.m_fullscreen = true; - } - else - { - s_ctx.m_style |= NSWindowStyleMaskTitled; - dispatch_async(dispatch_get_main_queue() - , ^{ - [NSMenu setMenuBarVisible: true]; - [window setStyleMask: s_ctx.m_style]; - [window setFrame:s_ctx.m_windowFrame display:YES]; - }); - - s_ctx.m_fullscreen = false; - } - } + dispatch_async(dispatch_get_main_queue() + , ^{ + NSWindow* window = s_ctx.m_window[_handle.idx]; + [window toggleFullScreen:nil]; + }); } void setMouseLock(WindowHandle _handle, bool _lock) @@ -732,7 +727,6 @@ namespace entry return nil; } - self->windowCount = 0; return self; } @@ -741,31 +735,22 @@ namespace entry assert(window); [window setDelegate:self]; - - assert(self->windowCount < ~0u); - self->windowCount += 1; } - (void)windowWillClose:(NSNotification*)notification { BX_UNUSED(notification); + NSWindow *window = [notification object]; + + [window setDelegate:nil]; + + destroyWindow(entry::s_ctx.findHandle(window), false); } - (BOOL)windowShouldClose:(NSWindow*)window { assert(window); - - [window setDelegate:nil]; - - assert(self->windowCount); - self->windowCount -= 1; - - if (self->windowCount == 0) - { - [NSApp terminate:self]; - return false; - } - + BX_UNUSED(window); return true; } |