For OpenGL, we can simply use stb_image. Maybe, it also work for Metal with some extra work.
I don't want include more libs. So I tried MTKTextureLoader
, but it didn't work with error.
MTKTextureLoaderErrorKey=Image decoding failed
What is the correct way to use HDR texture file with Metal API?
Since nobody is answering this question, I will post me solution below.
MTKTextureLoader *loader = [[MTKTextureLoader alloc] initWithDevice: _device];
NSDictionary *textureLoaderOptions = @ {
MTKTextureLoaderOptionSRGB: @NO,
MTKTextureLoaderOptionAllocateMipmaps: @YES,
MTKTextureLoaderOptionGenerateMipmaps: @YES,
MTKTextureLoaderOptionTextureUsage : @(MTLTextureUsageShaderRead),
MTKTextureLoaderOptionTextureStorageMode : @(MTLStorageModePrivate),
MTKTextureLoaderOptionOrigin: MTKTextureLoaderOriginFlippedVertically };
let pathHDR = [NSBundle.mainBundle pathForResource:@"filename" ofType:@"hdr"];
#if TARGET_OS_OSX
let urlHDR = [[NSURL alloc] initFileURLWithPath:pathHDR];
let imageData = [[[NSImage alloc] initWithContentsOfURL:urlHDR] TIFFRepresentation];
#else
//let imageData = [[NSData alloc] initWithContentsOfFile:pathHDR];
let image = [[UIImage alloc] initWithContentsOfFile:pathHDR];
let imageData = UIImageJPEGRepresentation(image, 1.0);
//UIImagePNGRepresentation(image);
#endif
_textureHDR = [loader newTextureWithData:imageData options:textureLoaderOptions error:&ERROR];