The situation: I'm developing an iOS Metal App in Objective-C. I need to use a 3rd-party library VkFFT that provides Metal FFTs based on Metal-Cpp.
What I need: pass a id<MTLCommandQueue>
instance from Objective-C world to C++ world as a MTL::CommandQueue
object so that I can push compute commands to it. I don't want to create a seperate commmand queue in Metal-Cpp because using a single command queue can let me keep pushing all commands to it without waitUntilCompleted
.
With the help of both Hamid Yusifli's answer and this GitHub Issue, I solved my problem.
From Objective-C code (.m
), cast the id<MTLCommandQueue>
into void*
:
id<MTLCommandQueue> objcQueue = /* Your Objective-c queue */;
void* commandQueuePtr = (__bridge void*)objcQueue;
Pass this opaque pointer to C++ world.
In C++ code, "decode" the void*
back to a MTL::CommandQueue*
:
void foo(void* commandQueuePtr) {
MTL::CommandQueue* commandQueue = (MTL::CommandQueue*) commandQueuePtr;
// submit GPU works to commandQueue...
}