Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic committed Jun 3, 2024
1 parent 3195593 commit 530a558
Showing 1 changed file with 66 additions and 46 deletions.
112 changes: 66 additions & 46 deletions src/renderer_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -420,38 +420,47 @@ static uint32_t getEntryProperty(io_registry_entry_t _entry, CFStringRef _proper
}
#endif // BX_PLATFORM_OSX


static const char* s_accessNames[] = {
"Access::Read",
"Access::Write",
"Access::ReadWrite",
};

BX_STATIC_ASSERT(BX_COUNTOF(s_accessNames) == Access::Count, "Invalid s_accessNames count");

#ifndef __IPHONE_OS_VERSION_MAX_ALLOWED
#define __IPHONE_OS_VERSION_MAX_ALLOWED 0
# define __IPHONE_OS_VERSION_MAX_ALLOWED 0
#endif
#ifndef __MAC_OS_X_VERSION_MAX_ALLOWED
#define __MAC_OS_X_VERSION_MAX_ALLOWED 0
# define __MAC_OS_X_VERSION_MAX_ALLOWED 0
#endif

#ifndef BX_XCODE_15
# define BX_XCODE_15 ((__MAC_OS_X_VERSION_MAX_ALLOWED >= 140000) || \
(__IPHONE_OS_VERSION_MAX_ALLOWED >= 170000))
#endif
# define BX_XCODE_15 (0 \
|| (__MAC_OS_X_VERSION_MAX_ALLOWED >= 140000) \
|| (__IPHONE_OS_VERSION_MAX_ALLOWED >= 170000) \
)
#endif // BX_XCODE_15

#ifndef BX_XCODE_14
# define BX_XCODE_14 ((__MAC_OS_X_VERSION_MAX_ALLOWED >= 130000) || \
(__IPHONE_OS_VERSION_MAX_ALLOWED >= 160000))
#endif
# define BX_XCODE_14 (0 \
|| (__MAC_OS_X_VERSION_MAX_ALLOWED >= 130000) \
|| (__IPHONE_OS_VERSION_MAX_ALLOWED >= 160000) \
)
#endif // BX_XCODE_14

#ifndef BX_XCODE_13
# define BX_XCODE_13 ((__MAC_OS_X_VERSION_MAX_ALLOWED >= 120000) || \
(__IPHONE_OS_VERSION_MAX_ALLOWED >= 150000))
#endif
# define BX_XCODE_13 (0 \
|| (__MAC_OS_X_VERSION_MAX_ALLOWED >= 120000) \
|| (__IPHONE_OS_VERSION_MAX_ALLOWED >= 150000) \
)
#endif // BX_XCODE_13

#ifndef BX_XCODE_12
# define BX_XCODE_12 ((__MAC_OS_X_VERSION_MAX_ALLOWED >= 110000) || \
(__IPHONE_OS_VERSION_MAX_ALLOWED >= 140000))
#endif
# define BX_XCODE_12 (0 \
|| (__MAC_OS_X_VERSION_MAX_ALLOWED >= 110000) \
|| (__IPHONE_OS_VERSION_MAX_ALLOWED >= 140000) \
)
#endif // BX_XCODE_12

#if BX_XCODE_15
# define VISION_OS_MINIMUM visionOS 1.0,
Expand Down Expand Up @@ -524,11 +533,11 @@ bool init(const Init& _init)
);
m_numWindows = 1;

#if BX_PLATFORM_VISIONOS
#if BX_PLATFORM_VISIONOS
if (NULL == m_mainFrameBuffer.m_swapChain->m_layerRenderer)
#else // BX_PLATFORM_VISIONOS
#else
if (NULL == m_mainFrameBuffer.m_swapChain->m_metalLayer)
#endif // BX_PLATFORM_VISIONOS
#endif // BX_PLATFORM_VISIONOS
{
release(m_device);
return false;
Expand Down Expand Up @@ -1029,13 +1038,14 @@ void updateTextureEnd() override
{
}

MTLPixelFormat getSwapChainPixelFormat(SwapChainMtl *swapChain) {
MTLPixelFormat getSwapChainPixelFormat(SwapChainMtl *swapChain)
{
#if BX_PLATFORM_VISIONOS
return MTLPixelFormatBGRA8Unorm_sRGB;
return MTLPixelFormatBGRA8Unorm_sRGB;
#else
return swapChain->m_metalLayer.pixelFormat;
#endif
}
return swapChain->m_metalLayer.pixelFormat;
#endif // BX_PLATFORM_VISIONOS
}

void readTexture(TextureHandle _handle, void* _data, uint8_t _mip) override
{
Expand Down Expand Up @@ -1424,7 +1434,7 @@ void flip() override
cp_frame_start_submission(frameBuffer.m_swapChain->m_frame);
cp_drawable_encode_present(frameBuffer.m_swapChain->m_drawable, m_commandBuffer);
cp_frame_end_submission(frameBuffer.m_swapChain->m_frame);
#else // BX_PLATFORM_VISIONOS
#else
m_commandBuffer.presentDrawable(frameBuffer.m_swapChain->m_drawable);
MTL_RELEASE(frameBuffer.m_swapChain->m_drawable);
#endif // BX_PLATFORM_VISIONOS
Expand Down Expand Up @@ -3306,10 +3316,11 @@ void writeString(bx::WriterI* _writer, const char* _str)
{
#if BX_PLATFORM_VISIONOS
MTL_RELEASE(m_layerRenderer);
#else // BX_PLATFORM_VISIONOS
#else
MTL_RELEASE(m_metalLayer);
MTL_RELEASE(m_drawable);
#endif // BX_PLATFORM_VISIONOS

MTL_RELEASE(m_drawableTexture);

MTL_RELEASE(m_backBufferDepth);
Expand All @@ -3324,15 +3335,24 @@ void writeString(bx::WriterI* _writer, const char* _str)

void SwapChainMtl::init(void* _nwh)
{
#if !BX_PLATFORM_VISIONOS
#if BX_PLATFORM_VISIONOS
{
cp_layer_renderer_t layerRenderer = (cp_layer_renderer_t)_nwh;
m_layerRenderer = layerRenderer;
retain(m_layerRenderer);
}
#else
if (m_metalLayer)
{
release(m_metalLayer);
}

if (NULL != NSClassFromString(@"MTKView") )
{
MTKView *view = (MTKView *)_nwh;
if (NULL != view && [view isKindOfClass:NSClassFromString(@"MTKView")])

if (NULL != view
&& [view isKindOfClass:NSClassFromString(@"MTKView")])
{
m_metalLayer = (CAMetalLayer *)view.layer;
}
Expand All @@ -3341,7 +3361,7 @@ void writeString(bx::WriterI* _writer, const char* _str)
if (NULL != NSClassFromString(@"CAMetalLayer") )
{
if (NULL == m_metalLayer)
#if BX_PLATFORM_IOS
# if BX_PLATFORM_IOS
{
CAMetalLayer* metalLayer = (CAMetalLayer*)_nwh;
if (NULL == metalLayer
Expand All @@ -3353,7 +3373,7 @@ void writeString(bx::WriterI* _writer, const char* _str)

m_metalLayer = metalLayer;
}
#elif BX_PLATFORM_OSX
# elif BX_PLATFORM_OSX
{
NSObject* nvh = (NSObject*)_nwh;
if ([nvh isKindOfClass:[CAMetalLayer class]])
Expand Down Expand Up @@ -3413,7 +3433,7 @@ void writeString(bx::WriterI* _writer, const char* _str)
}
}
}
#endif // BX_PLATFORM_*
# endif // BX_PLATFORM_*
}

if (NULL == m_metalLayer)
Expand All @@ -3426,13 +3446,8 @@ void writeString(bx::WriterI* _writer, const char* _str)
m_metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
m_metalLayer.magnificationFilter = kCAFilterNearest;
retain(m_metalLayer);
#else // BX_PLATFORM_VISIONOS
{
cp_layer_renderer_t layerRenderer = (cp_layer_renderer_t)_nwh;
m_layerRenderer = layerRenderer;
retain(m_layerRenderer);
}
#endif // BX_PLATFORM_VISIONOS

m_nwh = _nwh;
}

Expand Down Expand Up @@ -3460,7 +3475,7 @@ void writeString(bx::WriterI* _writer, const char* _str)

#if !BX_PLATFORM_VISIONOS
m_metalLayer.drawableSize = CGSizeMake(_width, _height);
m_metalLayer.pixelFormat = (_flags & BGFX_RESET_SRGB_BACKBUFFER)
m_metalLayer.pixelFormat = (_flags & BGFX_RESET_SRGB_BACKBUFFER)
? MTLPixelFormatBGRA8Unorm_sRGB
: MTLPixelFormatBGRA8Unorm
;
Expand Down Expand Up @@ -3528,7 +3543,7 @@ void writeString(bx::WriterI* _writer, const char* _str)
desc.pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
#else
desc.pixelFormat = m_metalLayer.pixelFormat;
#endif
#endif // BX_PLATFORM_VISIONOS
m_backBufferColorMsaa = s_renderMtl->m_device.newTextureWithDescriptor(desc);
}

Expand All @@ -3537,7 +3552,7 @@ void writeString(bx::WriterI* _writer, const char* _str)
murmur.add(1);
#if !BX_PLATFORM_VISIONOS
murmur.add( (uint32_t)m_metalLayer.pixelFormat);
#endif // BX_PLATFORM_VISIONOS
#endif // !BX_PLATFORM_VISIONOS
murmur.add( (uint32_t)m_backBufferDepth.pixelFormat() );
murmur.add( (uint32_t)m_backBufferStencil.pixelFormat() );
murmur.add( (uint32_t)sampleCount);
Expand All @@ -3550,33 +3565,38 @@ void writeString(bx::WriterI* _writer, const char* _str)
{
#if BX_PLATFORM_VISIONOS
m_frame = cp_layer_renderer_query_next_frame(m_layerRenderer);
if (m_frame) {
if (m_frame)
{
m_drawable = cp_frame_query_drawable(m_frame);
}
#else // BX_PLATFORM_VISIONOS
#else
m_drawable = m_metalLayer.nextDrawable;
#endif // BX_PLATFORM_VISIONOS

if (m_drawable != NULL)
{
#if BX_PLATFORM_VISIONOS
m_drawableTexture = cp_drawable_get_color_texture(m_drawable, 0);
#else // BX_PLATFORM_VISIONOS
#else
m_drawableTexture = m_drawable.texture;
retain(m_drawable); // keep alive to be useable at 'flip'
#endif // BX_PLATFORM_VISIONOS

retain(m_drawableTexture);
}
else
{
TextureDescriptor desc = s_renderMtl->m_textureDescriptor;
desc.textureType = MTLTextureType2D;
#if !BX_PLATFORM_VISIONOS

#if BX_PLATFORM_VISIONOS
desc.pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
#else
desc.pixelFormat = m_metalLayer.pixelFormat;
desc.width = m_metalLayer.drawableSize.width;
desc.height = m_metalLayer.drawableSize.height;
#else // BX_PLATFORM_VISIONOS
desc.pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
#endif // BX_PLATFORM_VISIONOS

desc.depth = 1;
desc.mipmapLevelCount = 1;
desc.sampleCount = 1;
Expand Down

0 comments on commit 530a558

Please sign in to comment.