Skip to content

Commit

Permalink
feat: base visionOS bgfx implementation
Browse files Browse the repository at this point in the history
Co-authored-by: mani3xis <mariusz.pas+dev@protonmail.com>

fix: properly set storageMode

cleanup: remove unused variables

fix crash while releasing m_drawable on visionOS

fix: remove unused timing variable

fix: file name cases, cleanup

feat: integrate visionOS into bgfx examples
  • Loading branch information
okwasniewski committed May 13, 2024
1 parent 3a116a9 commit 6d69822
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/common/entry/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void openUrl(const bx::StringView& _url)
#if BX_PLATFORM_WINDOWS
void* result = ShellExecuteA(NULL, NULL, tmp, NULL, NULL, false);
BX_UNUSED(result);
#elif !BX_PLATFORM_IOS
#elif !BX_PLATFORM_IOS && !BX_PLATFORM_VISIONOS
int32_t result = system(tmp);
BX_UNUSED(result);
#endif // BX_PLATFORM_*
Expand Down
2 changes: 1 addition & 1 deletion examples/common/entry/entry_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace entry
else
# endif // ENTRY_CONFIG_USE_WAYLAND
return (void*)wmi.info.x11.window;
# elif BX_PLATFORM_OSX || BX_PLATFORM_IOS
# elif BX_PLATFORM_OSX || BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
return wmi.info.cocoa.window;
# elif BX_PLATFORM_WINDOWS
return wmi.info.win.window;
Expand Down
2 changes: 2 additions & 0 deletions include/bgfx/embedded_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
|| BX_PLATFORM_LINUX \
|| BX_PLATFORM_OSX \
|| BX_PLATFORM_RPI \
|| BX_PLATFORM_VISIONOS \
|| BX_PLATFORM_WINDOWS \
)
#define BGFX_PLATFORM_SUPPORTS_GLSL (0 \
Expand All @@ -44,6 +45,7 @@
#define BGFX_PLATFORM_SUPPORTS_METAL (0 \
|| BX_PLATFORM_IOS \
|| BX_PLATFORM_OSX \
|| BX_PLATFORM_VISIONOS \
)
#define BGFX_PLATFORM_SUPPORTS_NVN (0 \
|| BX_PLATFORM_NX \
Expand Down
12 changes: 6 additions & 6 deletions src/bgfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "topology.h"

#if BX_PLATFORM_OSX || BX_PLATFORM_IOS
#if BX_PLATFORM_OSX || BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
# include <objc/message.h>
#elif BX_PLATFORM_WINDOWS
# ifndef WIN32_LEAN_AND_MEAN
Expand Down Expand Up @@ -2413,7 +2413,7 @@ namespace bgfx
}
}

#if BX_PLATFORM_OSX || BX_PLATFORM_IOS
#if BX_PLATFORM_OSX || BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
struct NSAutoreleasePoolScope
{
NSAutoreleasePoolScope()
Expand All @@ -2437,7 +2437,7 @@ namespace bgfx
{
BGFX_PROFILER_SCOPE("bgfx::renderFrame", 0xff2040ff);

#if BX_PLATFORM_OSX || BX_PLATFORM_IOS
#if BX_PLATFORM_OSX || BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
NSAutoreleasePoolScope pool;
#endif // BX_PLATFORM_OSX

Expand Down Expand Up @@ -2635,11 +2635,11 @@ namespace bgfx
{ d3d11::rendererCreate, d3d11::rendererDestroy, BGFX_RENDERER_DIRECT3D11_NAME, !!BGFX_CONFIG_RENDERER_DIRECT3D11 }, // Direct3D11
{ d3d12::rendererCreate, d3d12::rendererDestroy, BGFX_RENDERER_DIRECT3D12_NAME, !!BGFX_CONFIG_RENDERER_DIRECT3D12 }, // Direct3D12
{ gnm::rendererCreate, gnm::rendererDestroy, BGFX_RENDERER_GNM_NAME, !!BGFX_CONFIG_RENDERER_GNM }, // GNM
#if BX_PLATFORM_OSX || BX_PLATFORM_IOS
#if BX_PLATFORM_OSX || BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
{ mtl::rendererCreate, mtl::rendererDestroy, BGFX_RENDERER_METAL_NAME, !!BGFX_CONFIG_RENDERER_METAL }, // Metal
#else
{ noop::rendererCreate, noop::rendererDestroy, BGFX_RENDERER_NOOP_NAME, false }, // Noop
#endif // BX_PLATFORM_OSX || BX_PLATFORM_IOS
#endif // BX_PLATFORM_OSX || BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
{ nvn::rendererCreate, nvn::rendererDestroy, BGFX_RENDERER_NVN_NAME, !!BGFX_CONFIG_RENDERER_NVN }, // NVN
{ gl::rendererCreate, gl::rendererDestroy, BGFX_RENDERER_OPENGL_NAME, !!BGFX_CONFIG_RENDERER_OPENGLES }, // OpenGLES
{ gl::rendererCreate, gl::rendererDestroy, BGFX_RENDERER_OPENGL_NAME, !!BGFX_CONFIG_RENDERER_OPENGL }, // OpenGL
Expand Down Expand Up @@ -2737,7 +2737,7 @@ namespace bgfx
score += RendererType::Metal == renderer ? 20 : 0;
score += RendererType::Vulkan == renderer ? 10 : 0;
}
else if (BX_ENABLED(BX_PLATFORM_IOS) )
else if (BX_ENABLED(BX_PLATFORM_IOS) || BX_ENABLED(BX_PLATFORM_VISIONOS))
{
score += RendererType::Metal == renderer ? 20 : 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
# define BGFX_CONFIG_RENDERER_METAL (0 \
|| BX_PLATFORM_IOS \
|| BX_PLATFORM_OSX \
|| BX_PLATFORM_VISIONOS \
? 1 : 0)
# endif // BGFX_CONFIG_RENDERER_METAL

Expand Down
28 changes: 22 additions & 6 deletions src/renderer_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
#import <Metal/Metal.h>
#import <MetalKit/MetalKit.h>

#if BX_PLATFORM_IOS
#if BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
# import <UIKit/UIKit.h>
#endif // BX_PLATFORM_*

#if BX_PLATFORM_VISIONOS
#import <CompositorServices/CompositorServices.h>
#endif

#define BGFX_MTL_PROFILER_BEGIN(_view, _abgr) \
BX_MACRO_BLOCK_BEGIN \
BGFX_PROFILER_BEGIN(s_viewName[view], _abgr); \
Expand All @@ -38,7 +42,7 @@ namespace bgfx { namespace mtl
//runtime os check
inline bool iOSVersionEqualOrGreater(const char* _version)
{
#if BX_PLATFORM_IOS
#if BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
return ([[[UIDevice currentDevice] systemVersion] compare:@(_version) options:NSNumericSearch] != NSOrderedAscending);
#else
BX_UNUSED(_version);
Expand Down Expand Up @@ -386,19 +390,19 @@ namespace bgfx { namespace mtl

bool supportsTextureSampleCount(int sampleCount)
{
if (BX_ENABLED(BX_PLATFORM_IOS) && !iOSVersionEqualOrGreater("9.0.0") )
if (BX_ENABLED(BX_PLATFORM_VISIONOS) || (BX_ENABLED(BX_PLATFORM_IOS) && !iOSVersionEqualOrGreater("9.0.0")) )
return sampleCount == 1 || sampleCount == 2 || sampleCount == 4;
else
return [m_obj supportsTextureSampleCount:sampleCount];
}

bool depth24Stencil8PixelFormatSupported()
{
#if BX_PLATFORM_IOS
#if BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
return false;
#else
return m_obj.depth24Stencil8PixelFormatSupported;
#endif // BX_PLATFORM_IOS
#endif // BX_PLATFORM_IOS || BX_PLATFORM_VISIONOS
}
MTL_CLASS_END

Expand Down Expand Up @@ -1032,7 +1036,12 @@ namespace bgfx { namespace mtl
struct SwapChainMtl
{
SwapChainMtl()
#if BX_PLATFORM_VISIONOS
: m_layerRenderer(NULL)
, m_frame(NULL)
#else
: m_metalLayer(nil)
#endif
, m_drawable(nil)
, m_drawableTexture(nil)
, m_backBufferColorMsaa()
Expand All @@ -1049,8 +1058,15 @@ namespace bgfx { namespace mtl

id <MTLTexture> currentDrawableTexture();


#if BX_PLATFORM_VISIONOS
cp_layer_renderer_t m_layerRenderer;
cp_frame_t m_frame;
cp_drawable_t m_drawable;
#else
CAMetalLayer* m_metalLayer;
id <CAMetalDrawable> m_drawable;
id <CAMetalDrawable> m_drawable;
#endif
id <MTLTexture> m_drawableTexture;
Texture m_backBufferColorMsaa;
Texture m_backBufferDepth;
Expand Down

0 comments on commit 6d69822

Please sign in to comment.