floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
6.63k stars 472 forks source link

sokol_gfx failing to compile on macOS 12.4 with `use of undeclared identifier 'MTLGPUFamilyApple8'` #914

Closed kkukshtel closed 9 months ago

kkukshtel commented 9 months ago

As the title says — trying to compile sokol_gfx to a library (using zig build) on macOS 12.4 on a 2021 M1 Pro laptop and getting the following error:

clang failed with stderr: In file included from /Users/kyle/Workspace/Dinghy/Dinghy.Bootstrapper/libs/sokol/build/sokol.c:16:
/Users/kyle/Workspace/Dinghy/Dinghy.Bootstrapper/libs/sokol/build/../src/sokol/sokol_gfx.h:10760:80: error: use of undeclared identifier 'MTLGPUFamilyApple8'

My sokol.c:

#define SOKOL_IMPL
#define SOKOL_DLL
#define SOKOL_NO_ENTRY

#define USE_SOKOL_APP

#if defined(_WIN32)
#define SOKOL_LOG(s) OutputDebugStringA(s)
#endif
#include "../src/sokol/sokol_app.h"
#include "../src/sokol/sokol_args.h"
#include "../src/sokol/sokol_audio.h"
#include "../src/sokol/sokol_fetch.h"
#include "../src/sokol/sokol_gfx.h"

I was getting a similar error recently but for MTLGPUFamilyApple7 but then I went to latest sokol master commit and that one went away. Not sure what's up but it seems like maybe an assumption of Metal versions available for OSs?

floooh commented 9 months ago

Strange indeed, according to here MTLGPUFamilyApple8 should be available since macOS SDK 10.15+

https://developer.apple.com/documentation/metal/mtlgpufamily/mtlgpufamilyapple8

What does xcrun --sdk macosx --show-sdk-path say? (to check what SDK version is used, for instance here it says: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk)

I was hoping that the way I'm conditionally compiling this code inside ifdefs would fix the issue now for older SDK version, but looks I did something wrong there:

https://github.com/floooh/sokol/blob/5bfa84212b7428ed8ca3b55ffce214aff9e4954b/sokol_gfx.h#L10757-L10770

kkukshtel commented 9 months ago

Just ran:

xcrun --sdk macosx --show-sdk-path

And it's showing:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk

I assume this means I could fix this by upgrading Xcode then? Though I would definitely expect yeah that it should still work with older versions.

floooh commented 9 months ago

Hmm ok, getting closer. On my old Intel Mac with SDK 11.3 this whole code block is skipped, so it builds just fine.

But the code block is compiled with SDK 12.x, but I guess this MTLGPUFamily8 is not yet defined in SDK 12.3 (which means the documentation is wrong, but that's not unusual unfortunately).

Upgrading the SDK or even Xcode would workaround the problem, but I'd better find a solution that also works with the 12.3 SDK, I just need to add a couple more ifdefs I guess.

I'll try to look into this in the evening, but it'll be a bit hard for me to test.

floooh commented 9 months ago

Btw, this define shouldn't be needed:

#define USE_SOKOL_APP

...also SOKOL_LOG has been replaced with the new runtime logging callback and sokol_log.h :)

floooh commented 9 months ago

It's really strange, there's nothing in the Metal headers (of SDK 14.0 at least) which would indicate when MTLGPUFamilyApple8 became available:

image
floooh commented 9 months ago

Can you check what this enum MTLGPUFamily looks like on your SDK version? The path for your SDK version should be:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Metal.framework/Headers/MTLDevice.h

floooh commented 9 months ago

...I'm actually wondering why I explicitely added the MTLGPUFamily8 check, I would expect that this is a superset of MTLGPUFamily7... need to dig around a bit in the git history...

floooh commented 9 months ago

...yeah I simply removed the MTLGPUFamily8 check since the clamp_to_border_color feature should be covered by MTLGPUFamily7.

Can you check if this works for you now?

kkukshtel commented 9 months ago

Just tried latest and it seems to compile without issue! However the program crashes on sokol_gfx setup but I think that's an issue for me and bindings.

Just for reference though, here's the enum for me:

Can you check what this enum MTLGPUFamily looks like on your SDK version? The path for your SDK version should be:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Metal.framework/Headers/MTLDevice.h

typedef NS_ENUM(NSInteger, MTLGPUFamily)
{
    MTLGPUFamilyApple1 = 1001,
    MTLGPUFamilyApple2 = 1002,
    MTLGPUFamilyApple3 = 1003,
    MTLGPUFamilyApple4 = 1004,
    MTLGPUFamilyApple5 = 1005,
    MTLGPUFamilyApple6 = 1006,
    MTLGPUFamilyApple7 = 1007,
    MTLGPUFamilyMac1 = 2001,
    MTLGPUFamilyMac2 = 2002,
    MTLGPUFamilyCommon1 = 3001,
    MTLGPUFamilyCommon2 = 3002,
    MTLGPUFamilyCommon3 = 3003,
    MTLGPUFamilyMacCatalyst1 = 4001,
    MTLGPUFamilyMacCatalyst2 = 4002,
} API_AVAILABLE(macos(10.15), ios(13.0));
floooh commented 9 months ago

Ok! I'll close this issue then. Please open another issue for that crash if you're stuck!