Skip to content

Instantly share code, notes, and snippets.

@rb6502
Created June 13, 2023 01:32
Show Gist options
  • Save rb6502/efca2e3d2e9ad35b520412b523b43d7a to your computer and use it in GitHub Desktop.
Save rb6502/efca2e3d2e9ad35b520412b523b43d7a to your computer and use it in GitHub Desktop.
diff --git a/3rdparty/bgfx/src/renderer_mtl.mm b/3rdparty/bgfx/src/renderer_mtl.mm
index 2317c16d6b5..db81e227290 100644
--- a/3rdparty/bgfx/src/renderer_mtl.mm
+++ b/3rdparty/bgfx/src/renderer_mtl.mm
@@ -14,6 +14,12 @@
# include <Cocoa/Cocoa.h>
#endif
+#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 110000
+#undef OLD_MACOS
+#else
+#define OLD_MACOS
+#endif
+
#import <Foundation/Foundation.h>
#define UNIFORM_BUFFER_SIZE (8*1024*1024)
@@ -212,18 +218,31 @@ inline void setViewType(ViewId _view, const bx::StringView _str)
MTLPixelFormat m_fmt;
MTLPixelFormat m_fmtSrgb;
MTLReadWriteTextureTier m_rwTier;
- MTLTextureSwizzleChannels m_mapping;
+#ifdef OLD_MACOS
+ uint8_t m_mapping[4];
+#else
+ MTLTextureSwizzleChannels m_mapping;
+#endif
bool m_autoGetMipmap;
};
static TextureFormatInfo s_textureFormat[] =
{
+#ifdef OLD_MACOS
+#define $0 0
+#define $1 1
+#define $R 2
+#define $G 3
+#define $B 4
+#define $A 5
+#else
#define $0 MTLTextureSwizzleZero
#define $1 MTLTextureSwizzleOne
#define $R MTLTextureSwizzleRed
#define $G MTLTextureSwizzleGreen
#define $B MTLTextureSwizzleBlue
#define $A MTLTextureSwizzleAlpha
+#endif
{ MTLPixelFormat(130/*BC1_RGBA*/), MTLPixelFormat(131/*BC1_RGBA_sRGB*/), MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, false }, // BC1
{ MTLPixelFormat(132/*BC2_RGBA*/), MTLPixelFormat(133/*BC2_RGBA_sRGB*/), MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, false }, // BC2
{ MTLPixelFormat(134/*BC3_RGBA*/), MTLPixelFormat(135/*BC3_RGBA_sRGB*/), MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, false }, // BC3
@@ -320,12 +339,21 @@ inline void setViewType(ViewId _view, const bx::StringView _str)
{ MTLPixelFormatRGBA32Sint, MTLPixelFormatInvalid, MTLReadWriteTextureTier2, { $R, $G, $B, $A }, true }, // RGBA32I
{ MTLPixelFormatRGBA32Uint, MTLPixelFormatInvalid, MTLReadWriteTextureTier2, { $R, $G, $B, $A }, true }, // RGBA32U
{ MTLPixelFormatRGBA32Float, MTLPixelFormatInvalid, MTLReadWriteTextureTier2, { $R, $G, $B, $A }, true }, // RGBA32F
+#ifdef OLD_MACOS
+ { MTLPixelFormatInvalid, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // BGR5A1
+ { MTLPixelFormatInvalid, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $B, $G, $R, $A }, true }, // RGB5A1
+ { MTLPixelFormatInvalid, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // BGR5A1
+ { MTLPixelFormatInvalid, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $B, $G, $R, $A }, true }, // RGB5A1
+ { MTLPixelFormatInvalid, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // BGR5A1
+ { MTLPixelFormatInvalid, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $B, $G, $R, $A }, true }, // RGB5A1
+#else
{ MTLPixelFormatB5G6R5Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // B5G6R5
{ MTLPixelFormatB5G6R5Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $B, $G, $R, $A }, true }, // R5G6B5
{ MTLPixelFormatABGR4Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $G, $B, $A, $R }, true }, // BGRA4
{ MTLPixelFormatABGR4Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $A, $B, $G, $R }, true }, // RGBA4
{ MTLPixelFormatBGR5A1Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // BGR5A1
{ MTLPixelFormatBGR5A1Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $B, $G, $R, $A }, true }, // RGB5A1
+#endif
{ MTLPixelFormatRGB10A2Unorm, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // RGB10A2
{ MTLPixelFormatRG11B10Float, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, true }, // RG11B10F
{ MTLPixelFormatInvalid, MTLPixelFormatInvalid, MTLReadWriteTextureTierNone, { $R, $G, $B, $A }, false }, // UnknownDepth
@@ -2917,8 +2945,9 @@ void writeString(bx::WriterI* _writer, const char* _str)
desc.mipmapLevelCount = ti.numMips;
desc.sampleCount = 1;
desc.arrayLength = ti.numLayers;
+#ifndef OLD_MACOS
desc.swizzle = tfi.m_mapping;
-
+#endif
if (s_renderMtl->m_iOS9Runtime
|| s_renderMtl->m_macOS11Runtime)
{
@rb6502
Copy link
Author

rb6502 commented Jun 13, 2023

Ok, that's progress. It's checking the GPU type for Apple Silicon there, so not surprising that it doesn't exist prior to Big Sur. Try this:

https://gist.github.com/rb6502/b3091421d0fd4f75c648359ee590ab9c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment