How to use C++ modules with Xcode 11/12 in Apple Metal shaders?

Viewed 355

Xcode 11 and 12 have option "Metal Compiler - Build Options" with "Enable Modules (Metal)", "Header Search Paths" and other LLVM options for compiling *.metal shaders by Xcode build system. But, when I selecting "Enable Modules - YES", in base Multiplatform game project: Swift + Metal with shader code located at any NEW .metal file with default code like

#include <metal_stdlib>
using namespace metal;

all nice compiling, but when replace it to

import <metal_stdlib>
export module test;

I have an errors:

  • error: no template named 'import'
  • error: use of undeclared identifier 'metal_stdlib'
  • error: expected unqualified-id

compile Test.metal output like

export SDKROOT\=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/metal -c
-target air64-apple-macos10.15 -gline-tables-only -MO
-ItestMetalGame/Build/Products/Debug/include
-FtestMetalGame/Build/Products/Debug
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk
-ffast-math -fmodules -serialize-diagnostics
testMetalGame\ macOS.build/Metal/Test.dia -o testMetalGame\
macOS.build/Metal/Test.air -index-store-path
testMetalGame/.build/testMetalGame/Index/DataStore
-MMD -MT dependencies -MF testMetalGame\ macOS.build/Metal/Test.dat
testMetalGame/testMetalGame\ Shared/Test.metal

How to write Metal shaders in Xcode 12+ with Metal 2+ with C++ modules, according Metal Specification ?

0 Answers
Related