Search code examples
rustsimdmetalobjective-c-runtimemetalkit

What is the proper way to encode SIMD types and functions that use them for the Objective-C Runtime?


I'm developing an app in a language other than Objective-C and I came across newBoxWithDimensions. It uses the vector_float3 type which comes from the SIMD API.

I can not encode this function because of the vector_float3 type.

I've found Why can't gcc or clang properly @encode SIMD vector types?; the problem here is when @encode does not encode the SIMD types, then it can not create a proper form for those functions that use SIMD types and then the message sending verification fails. How can I bypass this encoding problem in message sending verification?


Solution

  • As an experiment, I requested the method signature for +newBoxWithDimensions:segments:geometryType:inwardNormals:allocator:, using:

    NSMethodSignature* sig = [MDLMesh methodSignatureForSelector:@selector(newBoxWithDimensions:segments:geometryType:inwardNormals:allocator:)];
    

    I then enumerated its arguments and their type encodings. It turns out that the signature just skips the two vector arguments. It shows a total of 5 arguments, which includes the implicit self and _cmd arguments, when there should be 7. The encodings are "@", ":", "q", "c", "@", the first two of which correspond to self and _cmd and the last three of which match the last three arguments of the method.

    I think your safest approach is to write an Objective-C module that exports a function wrapping this method but where the vector components are passed separately (i.e. three float arguments for the dimensions and three unsigned int arguments for the segments). It would construct the vector arguments from those individual arguments and call through to the class method, returning its result.