【发布时间】:2019-07-22 10:18:01
【问题描述】:
我正在尝试将我的 OpenGL 代码移植到 Metal one。作为其中的一部分,我需要绘制 PNG 纹理,并且我正在使用 MTKTextureLoader 来加载它们。这是我的管道代码:
texturePipelineDescriptor.vertexFunction = textureVertexFunc;
texturePipelineDescriptor.fragmentFunction = textureFragmentFunc;
texturePipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm;
texturePipelineDescriptor.colorAttachments[0].blendingEnabled = YES;
texturePipelineDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
texturePipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
texturePipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
这是我的加载器代码:
NSData* imageData = [NSData dataWithBytes:imageBuffer length:imageBufferSize];
id<MTLTexture> newMTLTexture = [m_metal_renderer.metalTextureLoader newTextureWithData:imageData options:nil error:&error];
结果如下。整个图像应该是橙色的,就像小部分一样。
如果我改变这一行:
texturePipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm;
到这一行:
texturePipelineDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
我会得到以下结果:
如你所见,图像改变了 R 和 B 通道,当我加载纹理时,在所有这些中
newMTLTexture.pixelFormat = MTLPixelFormatBGRA8Unorm;
无论它们是否正确绘制。 我怀疑 PNG 文件有问题,但在任何 PNG 查看器中它们似乎都是正常的。
任何线索我可以做些什么来解决它?它是 MTLTextureLoader 中的错误吗?还有其他加载PNG图片的方法吗?
附:该软件是为 MacOS 而不是 iOS 编写的。
【问题讨论】:
标签: objective-c macos metal