Offline shader compilation for iOS

Hi,

I am moving from compiling my Metal shader in the runtime (the app) into the toolchain, as I read the compiler may do more optimisation when performed in the toolchain.

When compiling for macos, I used to following command:

    xcrun -sdk macos metal -ffast-math  -o

When compiling for iOS:

    xcrun -sdk iphoneos metal -ffast-math  -o


Everything compile fine for the macos platform. However, when compiling for iOS, shaders that use "texturecube_array" fail to compile. The error message is:


/tmp/noxMaterialDefConverter/temp_DI3EEf.metal:17:168: error: unknown type name 'texturecube_array';
   did you mean 'texture_cube_array_t'?

fragment main0_out main0(constant UDefaultFs& _UDefaultFs_0 [[buffer(0)]], texture2d g_samples [[texture(0)]], 
   texturecube g_radianceMap [[texture(1)]], texturecube_array g_sh2Weights [[texture(2)]], 
   sampler g_samplesSmplr [[sampler(0)]], sampler g_radianceMapSmplr [[sampler(1)]],
   sampler g_sh2WeightsSmplr [[sampler(2)]], float4 gl_FragCoord [[position]])
                                                                                                                                                                       ^~~~~~~~~~~~~~~~~
                                                                                                                                                                      texture_cube_array_t
note: 'texture_cube_array_t' declared here


The shader language specification indicates that texturecube_array should be supported. Is that i am not compiling for the correct shader language version? How can I specify the shader language version I would like to use from the command line "xcrun -sdk iphoneos metal" ?

Thanks.

Replies

I found the info in the docs: https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
In my case, I needed:

xcrun -sdk iphoneos metal -std=ios-metal2.0 -ffast-math inputFile -o outputFile


Cheers.

Just an FYI, texture cube arrays are not supported on all iOS devices. They are supported on iPhone 8, iPhone 8+, and iPhone X. Check for MTLFeatureSet_iOS_GPUFamily4_v1 to determine if your app should use them.