MPSImageScale throws BAD ACCESS

I've got the following code that attempts to use MPSImageScale shader to flip and convert a texture:

    /// mtlDevice and mtlCommandBuffer are obtained earlier
    /// srcTex and dstTex are valid and existing MTLTexture objects with the same descriptors

    MPSScaleTransform scale{};
    scale.scaleX = 1;
    scale.scaleY = -1;
    auto scaleShader = [[MPSImageScale alloc] initWithDevice:mtlDevice];
    if ( scaleShader == nil ) {
        return ErrorType::OUT_OF_MEMORY;
    }
    scaleShader.scaleTransform = &scale;
    
    [scaleShader encodeToCommandBuffer:mtlCommandBuffer
                         sourceTexture:srcTex
                    destinationTexture:dstTex;

No matter what I do, I keep getting the EXC_BAD_ACCESS with the last line with the assembly stopping before endEncoding:

   0x7ff81492d804 <+1078>: callq  0x7ff81492ce5b            ; ___lldb_unnamed_symbol373$$MPSImage
->  0x7ff81492d809 <+1083>: movq   -0x98(%rbp), %rdi
    0x7ff81492d810 <+1090>: movq   0x3756f991(%rip), %rsi    ; "endEncoding"

All Metal objects are valid and I did all that I could to ensure that they are not culprits here, including making sure that the pixel format of both textures is the same, even if this is not required for MPS shaders. What am I missing?

Answered by BartW in 736238022

Switching from MPSImageScale to MPSImageLanczosScale solved this error.

Does it mean that MPSImageScale is an abstract base class? Why can it be instantiated then and it's not mentioned in the docs?

Accepted Answer

Switching from MPSImageScale to MPSImageLanczosScale solved this error.

Does it mean that MPSImageScale is an abstract base class? Why can it be instantiated then and it's not mentioned in the docs?

Based on the description in this header file, it certainly sounds like you can either use MPSImageLanczosScale or MPSImageBilinearScale, and MPSImageScale is not really an option:

https://github.com/xybp888/iOS-SDKs/blob/fa4a2d866231985fa9538fafc800421aa99b83f2/iPhoneOS13.0.sdk/System/Library/Frameworks/MetalPerformanceShaders.framework/Headers/MetalPerformanceShaders.h#L647

MPSImageScale throws BAD ACCESS
 
 
Q