如何加载 Metal iOS & Mac 的 HDR 文件纹理?

How to load HDR file texture for Metal iOS & Mac?

对于OpenGL,我们可以简单地使用stb_image。也许,它也适用于 Metal,需要一些额外的工作。

我不想包含更多库。所以我尝试了 MTKTextureLoader,但没有成功。

MTKTextureLoaderErrorKey=Image decoding failed

在 Metal API 中使用 HDR 纹理文件的正确方法是什么?

既然没有人回答这个问题,我会post下面的解决方案。

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];