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

2024-01-20

我正在尝试使用多重采样MTKView。我有一个MTKView与一名代表。我设置了视图sampleCount属性为 4。我使用以下命令创建一个管道状态描述符rasterSampleCount设置为 4,并使用它来创建我在渲染时使用的渲染管道状态。

在代表处draw(in:)方法中,我通过获取视图的当前渲染通道描述符并设置storeAction to multisampleResolve。我也设置过尝试过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)!

这就是我的绘画方式:

    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()

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

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


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

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

我不知道是否MTKView设置其currentRenderPassDescriptor当有一个时自动这样sampleCount> 1. 理想情况下,会的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过 MTKView 使用多重采样? 的相关文章

随机推荐