如何通过MTKView使用多重采样?

时间:2018-10-17 02:09:18

标签: swift metal metalkit multisampling

我正在尝试使用MTKView进行多重采样。我有一个MTKView和一位代表。我将视图的sampleCount属性设置为4。我创建了rasterSampleCount设置为4的管道状态描述符,并使用该描述符来创建渲染时使用的渲染管道状态。

在委托人的draw(in:)方法中,我通过获取视图的当前渲染过程描述符并将storeAction设置为multisampleResolve来创建渲染过程描述符。我也将尝试storeAndMultisampleResolve设置为无效。

我为渲染过程描述符创建了一个解析纹理,它的宽度和高度与视图相同,并且像素格式相同。

鉴于上述情况,渲染过程中出现了一个完整的红色框。我已经使用了金属调试器来查看纹理,并且视图的纹理和resolve纹理都在其中具有正确的渲染。我在AMD机器上,其中全红色的纹理通常表示未初始化的纹理。

我需要做些什么才能将渲染显示到屏幕上吗?

这是我设置视图,管道状态和解析纹理的方式:

    metalView = newMetalView
    metalView.sampleCount = 4
    metalView.clearColor = MTLClearColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
    device = newMetalView.device!

    let metalLibrary = device.makeDefaultLibrary()!
    let vertexFunction =  metalLibrary.makeFunction(name: "vertexShader")
    let fragmentFunction = metalLibrary.makeFunction(name: "fragmentShader")

    let pipelineStateDescriptor = MTLRenderPipelineDescriptor.init()
    pipelineStateDescriptor.label = "Particle Renderer"
    pipelineStateDescriptor.vertexFunction = vertexFunction
    pipelineStateDescriptor.fragmentFunction = fragmentFunction
    pipelineStateDescriptor.colorAttachments [ 0 ].pixelFormat = metalView.colorPixelFormat
    pipelineStateDescriptor.rasterSampleCount = 4

    do {
        try pipelineState = device.makeRenderPipelineState(descriptor: pipelineStateDescriptor)
    } catch {
        NSLog("Unable to create pipeline state")
        pipelineState = nil
    }

    let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: metalView.colorPixelFormat, width: Int(metalView.bounds.width), height: Int(metalView.bounds.height), mipmapped: false)
    resolveTexture = device.makeTexture(descriptor: textureDescriptor)!

这是我的绘画方式:

    let commandBuffer = commandQueue.makeCommandBuffer()
    commandBuffer?.label = "Partcle Command Buffer"
    let renderPassDescriptor = metalView.currentRenderPassDescriptor
    renderPassDescriptor?.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 0.0)
    renderPassDescriptor?.colorAttachments[0].loadAction = MTLLoadAction.clear
    renderPassDescriptor?.colorAttachments[0].storeAction = MTLStoreAction.multisampleResolve
    renderPassDescriptor?.colorAttachments[0].resolveTexture = resolveTexture

    let renderEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: renderPassDescriptor!)
    renderEncoder?.label = "Particle Render Encoder"
    renderEncoder?.setViewport(MTLViewport(originX: 0.0, originY: 0.0, width: Double(viewportSize.x), height: Double(viewportSize.y), znear: -1.0, zfar: 1.0))
    renderEncoder?.setRenderPipelineState(pipelineState!);

然后我进行抽奖,然后通过调用结束:

    renderEncoder?.endEncoding()
    commandBuffer?.present(metalView.currentDrawable!)
    commandBuffer?.commit()

这是调试器在我的纹理中显示的内容:

enter image description here

奇怪的是,在进行调试时,我不小心隐藏了Xcode,并且在1帧内,视图显示了正确的纹理。

1 个答案:

答案 0 :(得分:2)

renderPassDescriptor(从metalView.currentRenderPassDescriptor返回的初始配置是什么?

我相信您希望将颜色附件的texture设置为metalView.multisampleColorTexture,并将其颜色resolveTexture设置为metalView.currentDrawable.texture。也就是说,它应该对多样本纹理进行主要的多采样渲染,然后解析为可绘制纹理,以在视图中实际绘制它。

我不知道MTKView> 1时currentRenderPassDescriptor是否会像这样自动设置其sampleCount