MTKTextureLoader grayscale png turn red

Is there a way to correctly load png grayscale image types with the MTKTextureLoader?


When I load them (with newTexture(URL:options:)), the loaded texture has a red tint.


When I convert them to the png rgb image type before loading with MTKTextureLoader, the texture is correct.


Chris

Replies

This is sort of an optimzation.


MTK chooses MTLPixelFormatR8Unform for grayscale images because it is the only single channel pixel format. So you can get the grayscale by using my_texture_sample.xxxx in your fragment shader. If MTK didn't do this, your texture would need to use MTLPixelFomatRGBA8Unorm which uses 4x the memory (and bandwidth to the GPU)

Thanks Dan,


I didn't realize the different pixel formats, the MTKTextureLoader choosed, but there is something, I still don't understand:


A) This is the wanted result:


https://www.dropbox.com/s/fczgtktt43uk56h/A-RGB.png?dl=0

(achived with converting the grayscale image to rgb, then loading the rgb image and using my_texture_sample in the fragment shader)


B) This is the result when loading a grayscale image and using my_texture_sample in the fragment shader:


https://www.dropbox.com/s/2xa42xwkpbbxv2a/B-Gray.png?dl=0


C) But this is the result when loading a grayscale image and using my_texture_sample.xxxx in the fragment shader:


https://www.dropbox.com/s/9896xzurkqcwu2w/C-Gray%20xxxx.png?dl=0

Why is it too bright?



This is the rgb png:


https://www.dropbox.com/s/rr1r9b8kdgc1lp0/RGB%20version.png?dl=0



This is the grayscale png:


https://www.dropbox.com/s/29rvqwe7rnrbhol/Grayscale%20version.png?dl=0