如何在 MTKView 中使用多重采样?

use*_*321 7 multisampling swift metal metalkit

我正在尝试使用MTKView. 我有MTKView一个代表。我将视图的sampleCount属性设置为 4。我创建了一个rasterSampleCount设置为 4的管道状态描述符,并使用它来创建渲染时使用的渲染管道状态。

在代理的draw(in:)方法,我通过获取该视图的当前渲染通道描述符和设置创建渲染过程描述storeActionmultisampleResolve。我也设置了尝试storeAndMultisampleResolve无济于事。

我为渲染通道描述符创建了一个解析纹理,它与视图具有相同的宽度和高度以及相同的像素格式。

鉴于上述情况,我在渲染过程中得到了一个完整的红框。我已经使用金属调试器来查看纹理,并且视图的纹理和解析纹理都有正确的渲染。我在 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)!
Run Code Online (Sandbox Code Playgroud)

这就是我的绘画方式:

    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!);
Run Code Online (Sandbox Code Playgroud)

然后我进行绘制调用,最后调用:

    renderEncoder?.endEncoding()
    commandBuffer?.present(metalView.currentDrawable!)
    commandBuffer?.commit()
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

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

Ken*_*ses 6

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

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

我不知道当 a > 1 时是否会自动MTKView设置它。理想情况下,它会。currentRenderPassDescriptorsampleCount