ioscore-graphicsmetaluigraphicsimagerenderer

How do I determine the maximum allowed size of an MTLTextureDescriptor


I am performing image manipulation using UIGraphicsImageRenderer(size: size, format: format). If the image is too large, it will fail with an assertion:

failed assertion `MTLTextureDescriptor has width (15488) greater than the maximum allowed size of 8192.'

I'd like to prevent this by first checking the image does not exceed the maximum allowed size. I'm not sure that 8192 is a constant for all devices, and would like to obtain this programmatically instead of hard coding it.


Solution

  • It is programmatically not possible to get the maximum texture size supported by the device. However, the below code will give you the hardcoded size based on the device type.

    int maxTexSize = 4096;
    
    if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily4_v1] || [mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v1]) {
        maxTexSize = 16384;
    else if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v2] || [mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v2]) {
        maxTexSize = 8192;
    } else {
        maxTexSize = 4096;
    }