如果 Metal origin 是左上角,为什么这个图像显示在左下角?

If Metal origin is upper left corner, why does this image show it on the lower left?

我得到的图像与这个问题中的图像一样:

Confused about thread_position_in_grid

深黑色在左下角,这意味着 gid.xgid.y 在图像的那个部分都是 0。然而 Metal 文档(以及像这样的答案:)指出原点在左上角,这意味着应该是你看到深黑色角的地方。

为什么情况似乎并非如此?

我使用 CoreImage 查看纹理。我像这样制作 CIImage

CIImage(mtlTexture: kernelOutputTexture,
                       options: [CIImageOption.colorSpace: colorSpace])!

然后我在 NSImageView 中这样查看它:

let rep = NSCIImageRep(ciImage: image)
let nsImage = NSImage(size: rep.size)
nsImage.addRepresentation(rep)

Core Image 使用原点在图像左下角的坐标系。尽管您可能怀疑它会在包装 MTLTexture 时解释 Metal 的左上角原点,但事实并非如此。

要创建方向与原始纹理匹配的CIImage,您可以请求垂直翻转的新图像:

image = image.transformed(by: image.orientationTransform(for: .downMirrored))

orientationTransform(for:) 方法是在 iOS 11.0 中引入的 和 macOS 10.13。使用它可能比编写您自己的仿射变换更健壮,并且也非常有效,因为 Core Image 使用惰性评估而不是物理移动或复制底层数据。