|
| 1 | +#include "entry_p.h" |
| 2 | + |
| 3 | +#if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_VISIONOS |
| 4 | + |
| 5 | +#include "swift_adapter.hpp" |
| 6 | + |
| 7 | +#include <bgfx/platform.h> |
| 8 | +#include <bx/uint32_t.h> |
| 9 | +#include <bx/thread.h> |
| 10 | + |
| 11 | +namespace entry |
| 12 | +{ |
| 13 | + struct MainThreadEntry |
| 14 | + { |
| 15 | + int m_argc; |
| 16 | + const char* const* m_argv; |
| 17 | + |
| 18 | + static int32_t threadFunc(bx::Thread* _thread, void* _userData); |
| 19 | + }; |
| 20 | + |
| 21 | + static WindowHandle s_defaultWindow = { 0 }; |
| 22 | + |
| 23 | + struct Context |
| 24 | + { |
| 25 | + Context(uint32_t _width, uint32_t _height) |
| 26 | + { |
| 27 | + static const char* const argv[] = { "visionos" }; |
| 28 | + m_mte.m_argc = BX_COUNTOF(argv); |
| 29 | + m_mte.m_argv = argv; |
| 30 | + |
| 31 | + m_eventQueue.postSizeEvent(s_defaultWindow, _width, _height); |
| 32 | + |
| 33 | + |
| 34 | + // Prevent render thread creation. |
| 35 | + bgfx::renderFrame(); |
| 36 | + |
| 37 | + m_thread.init(MainThreadEntry::threadFunc, &m_mte); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + ~Context() |
| 42 | + { |
| 43 | + m_thread.shutdown(); |
| 44 | + } |
| 45 | + |
| 46 | + MainThreadEntry m_mte; |
| 47 | + bx::Thread m_thread; |
| 48 | + void* m_window; |
| 49 | + |
| 50 | + EventQueue m_eventQueue; |
| 51 | + }; |
| 52 | + |
| 53 | + static Context* s_ctx; |
| 54 | + |
| 55 | + int32_t MainThreadEntry::threadFunc(bx::Thread* _thread, void* _userData) |
| 56 | + { |
| 57 | + BX_UNUSED(_thread); |
| 58 | + |
| 59 | + if (_thread != NULL) { |
| 60 | + _thread->setThreadName("Main Thread BGFX"); |
| 61 | + } |
| 62 | + |
| 63 | + CFBundleRef mainBundle = CFBundleGetMainBundle(); |
| 64 | + if (mainBundle != nil) |
| 65 | + { |
| 66 | + CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle); |
| 67 | + if (resourcesURL != nil) |
| 68 | + { |
| 69 | + char path[PATH_MAX]; |
| 70 | + if (CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8*)path, PATH_MAX) ) |
| 71 | + { |
| 72 | + chdir(path); |
| 73 | + } |
| 74 | + |
| 75 | + CFRelease(resourcesURL); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + MainThreadEntry* self = (MainThreadEntry*)_userData; |
| 80 | + int32_t result = main(self->m_argc, self->m_argv); |
| 81 | + return result; |
| 82 | + } |
| 83 | + |
| 84 | + const Event* poll() |
| 85 | + { |
| 86 | + return s_ctx->m_eventQueue.poll(); |
| 87 | + } |
| 88 | + |
| 89 | + const Event* poll(WindowHandle _handle) |
| 90 | + { |
| 91 | + return s_ctx->m_eventQueue.poll(_handle); |
| 92 | + } |
| 93 | + |
| 94 | + void release(const Event* _event) |
| 95 | + { |
| 96 | + s_ctx->m_eventQueue.release(_event); |
| 97 | + } |
| 98 | + |
| 99 | + WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title) |
| 100 | + { |
| 101 | + BX_UNUSED(_x, _y, _width, _height, _flags, _title); |
| 102 | + WindowHandle handle = { UINT16_MAX }; |
| 103 | + return handle; |
| 104 | + } |
| 105 | + |
| 106 | + void destroyWindow(WindowHandle _handle) |
| 107 | + { |
| 108 | + BX_UNUSED(_handle); |
| 109 | + } |
| 110 | + |
| 111 | + void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y) |
| 112 | + { |
| 113 | + BX_UNUSED(_handle, _x, _y); |
| 114 | + } |
| 115 | + |
| 116 | + void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height) |
| 117 | + { |
| 118 | + BX_UNUSED(_handle, _width, _height); |
| 119 | + } |
| 120 | + |
| 121 | + void setWindowTitle(WindowHandle _handle, const char* _title) |
| 122 | + { |
| 123 | + BX_UNUSED(_handle, _title); |
| 124 | + } |
| 125 | + |
| 126 | + void setWindowFlags(WindowHandle _handle, uint32_t _flags, bool _enabled) |
| 127 | + { |
| 128 | + BX_UNUSED(_handle, _flags, _enabled); |
| 129 | + } |
| 130 | + |
| 131 | + void toggleFullscreen(WindowHandle _handle) |
| 132 | + { |
| 133 | + BX_UNUSED(_handle); |
| 134 | + } |
| 135 | + |
| 136 | + void setMouseLock(WindowHandle _handle, bool _lock) |
| 137 | + { |
| 138 | + BX_UNUSED(_handle, _lock); |
| 139 | + } |
| 140 | + |
| 141 | + void* getNativeWindowHandle(WindowHandle _handle) |
| 142 | + { |
| 143 | + if (kDefaultWindowHandle.idx == _handle.idx) |
| 144 | + { |
| 145 | + return s_ctx->m_window; |
| 146 | + } |
| 147 | + |
| 148 | + return NULL; |
| 149 | + } |
| 150 | + |
| 151 | + void* getNativeDisplayHandle() |
| 152 | + { |
| 153 | + return NULL; |
| 154 | + } |
| 155 | + |
| 156 | + bgfx::NativeWindowHandleType::Enum getNativeWindowHandleType() |
| 157 | + { |
| 158 | + return bgfx::NativeWindowHandleType::Default; |
| 159 | + } |
| 160 | + |
| 161 | +} // namespace entry |
| 162 | + |
| 163 | +using namespace entry; |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | +bool BgfxAdapter::initialize(void) { |
| 168 | + if (!m_initialized) { |
| 169 | + // Set context width and height to default visionOS resolution. It's different for the headset and device. |
| 170 | +#if TARGET_OS_SIMULATOR |
| 171 | + s_ctx = new Context(2732, 2048); |
| 172 | +#else |
| 173 | + s_ctx = new Context(1920, 1824); |
| 174 | +#endif |
| 175 | + s_ctx->m_window = m_layerRenderer; |
| 176 | + m_initialized = true; |
| 177 | + } |
| 178 | + |
| 179 | + return m_initialized; |
| 180 | +} |
| 181 | + |
| 182 | +void BgfxAdapter::shutdown(void) { |
| 183 | + if (m_initialized) { |
| 184 | + s_ctx->m_eventQueue.postExitEvent(); |
| 185 | + s_ctx = NULL; |
| 186 | + } |
| 187 | + |
| 188 | + m_initialized = false; |
| 189 | +} |
| 190 | + |
| 191 | +void BgfxAdapter::render() { |
| 192 | + if (!m_initialized) { |
| 193 | + return; |
| 194 | + } |
| 195 | + bgfx::renderFrame(); |
| 196 | +} |
| 197 | + |
| 198 | +void BgfxAdapter::renderLoop() { |
| 199 | + bool isRendering = true; |
| 200 | + while (isRendering) { |
| 201 | + switch (cp_layer_renderer_get_state(m_layerRenderer)) { |
| 202 | + case cp_layer_renderer_state_paused: |
| 203 | + cp_layer_renderer_wait_until_running(m_layerRenderer); |
| 204 | + break; |
| 205 | + |
| 206 | + case cp_layer_renderer_state_running: |
| 207 | + this->initialize(); |
| 208 | + this->render(); |
| 209 | + break; |
| 210 | + |
| 211 | + case cp_layer_renderer_state_invalidated: |
| 212 | + isRendering = false; |
| 213 | + break; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + this->shutdown(); |
| 218 | +} |
| 219 | + |
| 220 | +#endif // BX_PLATFORM_VISIONOS |
0 commit comments