【发布时间】:2019-03-27 18:57:06
【问题描述】:
我有一个 MTKView 设置为使用 MTLPixelFormat.rgba16Float。我遇到的显示问题可以用下图来最好地描述:
因此,预期的 UIColor 会被褪色,但仅在它显示在 MTKView 中时。当我通过 CIIMage 将可绘制纹理转换回图像以在 UIView 中显示时,我恢复了原始颜色。以下是我创建该输出的方式:
let colorSpace = CGColorSpaceCreateDeviceRGB()
let kciOptions = [kCIImageColorSpace: colorSpace,
kCIContextOutputPremultiplied: true,
kCIContextUseSoftwareRenderer: false] as [String : Any]
let strokeCIImage = CIImage(mtlTexture: metalTextureComposite, options: kciOptions)!.oriented(CGImagePropertyOrientation.downMirrored)
let imageCropCG = cicontext.createCGImage(strokeCIImage, from: bbox, format: kCIFormatABGR8, colorSpace: colorSpace)
其他相关设置:
uiColorBrushDefault: UIColor = UIColor(red: 0.92, green: 0.79, blue: 0.18, alpha: 1.0)
self.colorPixelFormat = MTLPixelFormat.rgba16Float
renderPipelineDescriptor.colorAttachments[0].pixelFormat = self.colorPixelFormat
// below is the colorspace for the texture which is tinted with UIColor
let colorSpace = CGColorSpaceCreateDeviceRGB()
let texDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: MTLPixelFormat.rgba8Unorm, width: Int(width), height: Int(height), mipmapped: isMipmaped)
target = texDescriptor.textureType
texture = device.makeTexture(descriptor: texDescriptor)
一些帖子暗示 sRGB 被假定在某处,但没有具体说明如何禁用它。
我希望我在 MTKView 上显示的颜色与输入相匹配(尽可能接近它)并且仍然能够将该纹理转换为我可以在 ImageView 中显示的东西。我已经在 iPad Air 和新的 iPad Pro 上对此进行了测试。相同的行为。任何帮助将不胜感激。
【问题讨论】:
-
因此,您应该做的第一件事是从处理链中删除 CoreImage,以确保您实际上将正确的颜色值输入到 MKTView 中。一种简单的方法是通过 MKTView 中的 Metal 直接使用硬编码的清晰颜色。我建议你先让它与 sRGB 一起工作,然后尝试将工作代码转换为支持线性 16 位颜色。
-
我认为
MTKView在内部将颜色空间转换为colorspace属性中设置的颜色空间,因此您没有得到想要的结果。 -
我不知道swift,但通常类似的问题是由于:颜色空间:Imageview可能会将数据显示为sRGB(更明智的选择,如果没有颜色配置文件),但你选择
CGColorSpaceCreateDeviceRGB,现在 [现代机器] 可以是 DCI-P3 而不是 sRGB。第二:线性/vs伽玛校正。浮点数通常用于线性空间。 -
谢谢@MoDJ。非常明智的建议。将 CoreImage 排除在等式之外只会让我得到 brighter 颜色。听起来我需要提供 corrected UIColors,当显示时,它将与原始颜色匹配......你知道我应该应用什么样的更正吗? (数学方面)
-
sRGB 颜色是伽马编码的字节范围值,当您使用线性 16 位值定义颜色时,这些颜色不是伽马编码的(因为它们是线性的)。这可能是您看到的问题的原因。您应该做的是确保将颜色指示为 sRGB,然后将这些 sRGB 值写入使用 16 位像素存储的纹理中。 Metal 通常会为您完成转换。
标签: swift metal color-space cgcolorspace mtlbuffer