Why I can't init a MPSMatrix successfully?

331 views Asked by At

I wonder why I can't init an MPSMatrix successfully? The object method initWith just return a nil(which means unsuccessfully init). My codes are shown below, and I want to create an MPSMatrix named matrixA but just get a nil back.

id<MTLDevice> device = MTLCreateSystemDefaultDevice();
id<MTLCommandQueue> commandQueue = [device newCommandQueue];
id<MTLCommandBuffer> mpsBuffer = commandQueue.commandBuffer;
float arrayA[5*6] =
    {1,1,1,1,1,1,
     2,2,2,2,2,2,
     3,3,3,3,3,3,
     4,4,4,4,4,4,
     5,5,5,5,5,5};
float arrayB[6*5] =
    {1,1,1,1,1,
     2,2,2,2,2,
     3,3,3,3,3,
     4,4,4,4,4,
     5,5,5,5,5,
     6,6,6,6,6};
float arrayC[5*5] = {0};
id<MTLBuffer> bufferA = [device newBufferWithBytes:arrayA length:5*6*4 options:MTLResourceCPUCacheModeDefaultCache];
id<MTLBuffer> bufferB = [device newBufferWithBytes:arrayB length:6*5*4 options:MTLResourceCPUCacheModeDefaultCache];
id<MTLBuffer> bufferC = [device newBufferWithBytes:arrayC length:5*5*4 options:MTLResourceCPUCacheModeDefaultCache];

MPSMatrixDescriptor * descA = [MPSMatrixDescriptor matrixDescriptorWithDimensions:5 columns:6 rowBytes:6*4 dataType:MPSDataTypeFloat32];
MPSMatrixDescriptor * descB = [MPSMatrixDescriptor matrixDescriptorWithDimensions:6 columns:5 rowBytes:5*4 dataType:MPSDataTypeFloat32];
MPSMatrixDescriptor * descC = [MPSMatrixDescriptor matrixDescriptorWithDimensions:5 columns:5 rowBytes:5*4 dataType:MPSDataTypeFloat32];

MPSMatrix * matrixA = [[MPSMatrix alloc] initWithBuffer:bufferA descriptor:descA];
MPSMatrix * matrixB = [[MPSMatrix alloc] initWithBuffer:bufferB descriptor:descB];
MPSMatrix * matrixC = [[MPSMatrix alloc] initWithBuffer:bufferC descriptor:descC];
2

There are 2 answers

1
javen929 On

I find that was because the device I used to debug is an 5s, and it just support metal performance shaders foundation partly. It can get a MPSMatrixDescriptor however it can't get a MPSMatrix. sigh.

0
冯剑龙 On

Yes.Use MPSSupportsMTLDevice to Determine whether a MetalPerformanceShaders.framework supports a MTLDevice. In fact, My iPad mini2 don't support the MPS too. As below:

Code Screenshot