Cocoa 中的 MTKView 无法在 Retina 下绘制。

4
我在Cocoa中有一个MTKView。
我使用drawable.texture显示纹理,但是纹理只在非Retina屏幕上的四分之一显示。
我需要进行其他设置才能使其正常工作吗?
class MyView : MTKView

下面的代码没有任何效果:
let scale = NSScreen.mainScreen()!.backingScaleFactor
var s = frame.size
s.width *= scale
s.height *= scale

metalLayer.contentsScale = scale
metalLayer.framebufferOnly = false
metalLayer.drawableSize = s

更新:
以下是我用来从图像中加载纹理的方法:
func textureFromImage(image: NSImage, inout tex:MTLTexture?) {


    let textureLoader = MTKTextureLoader(device: self.device!)
    do {
        tex = try textureLoader.newTextureWithCGImage(image.CGImage, options: nil)
    }
    catch {
        fatalError("Can't load texture")
    }
}

并展示代码的代码:

let commandBuffer = commandQueue.commandBuffer()
    let commandEncoder = commandBuffer.computeCommandEncoder()
    commandEncoder.setComputePipelineState(pipelineState)
    commandEncoder.setTexture(originalTexture, atIndex: 0)
    commandEncoder.setTexture(drawable.texture, atIndex: 1)
    commandEncoder.dispatchThreadgroups(threadgroupsPerGrid, threadsPerThreadgroup: threadsPerThreadgroup)
    commandEncoder.endEncoding()
    commandBuffer.presentDrawable(drawable)
    commandBuffer.commit();

原始输入图像和纹理的所有尺寸都与视图相同。


你能告诉我们更多关于你是如何将纹理内容传输到可绘制对象中的吗(绘制、贴图等)?源纹理和可绘制对象之间有尺寸差异吗? - warrenm
没问题,我添加了一些代码。 - Chris
2个回答

0

虽然我来晚了5年,但我刚遇到这个问题,因为我调用了
renderEncoder.setViewport(MTLViewport(originX: 0, originY: 0, width: Double(viewport.x), height: Double(viewport.y), znear: -1.0, zfar: 1.0)),其中viewport.x/y是以点为单位而不是像素。

为了正确渲染,我将x/y乘以屏幕比例(视网膜屏幕为2倍)。


0
尝试将视图的 contentScaleFactor 更改为 1。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接