代码之家  ›  专栏  ›  技术社区  ›  user1118321

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

  •  3
  • user1118321  · 技术社区  · 7 年前

    我正试着用多重取样法 MTKView . 我有一个 sampleCount rasterSampleCount 设置为4,并使用它生成渲染管道状态,我在渲染时使用该状态。

    在代表的 draw(in:) 方法,通过获取视图的当前渲染过程描述符并设置 storeAction multisampleResolve . 我也试过了 storeAndMultisampleResolve 无济于事。

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

    我需要做什么才能让渲染进入屏幕?

    下面是如何设置视图、管道状态和“解析纹理”(resolve texture):

        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 回复  |  直到 7 年前
        1
  •  6
  •   Ken Thomases    7 年前

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

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

    我不知道 MTKView 设置其 currentRenderPassDescriptor 当有一个 sampleCount

    推荐文章