首页 > 解决方案 > 在 draw() 循环中重新使用 currentDrawable.texture 时的 MTKView 混合问题

问题描述

我正在开发一个金属支持的绘画应用程序,在该应用程序中,我将笔画的绘制分为两个步骤:第一步将笔画的前沿绘制到屏幕上,并通过以下方式将整个捕获到 MTLTexture:

metalTextureComposite = self.currentDrawable!.texture

第二步绘制前进笔划的更新前边缘,并在使用最后保存的 metalTextureComposite 纹理的多边形上进行合成。

这种方法允许我在不牺牲性能的情况下绘制无限长的笔划,因为这两个步骤在绘制周期的每一帧中都重复。

我遇到的问题是,使用所需的源复合模式(见下面的代码),我只看到笔划的前沿被绘制到屏幕上。这告诉我要么我没有从 currentDrawable 充分捕获 metalTextureComposite,要么我对要使用的混合模式做出了错误的假设,顺便说一下如下:

renderPipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
renderPipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha       
renderPipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha                 
renderPipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha

如果我使用不同的混合模式,我会看到整个笔画被绘制,但不一定是我想要的外观。下面是我包含在 MTKView 的 draw() 方法中的部分代码。

func metalRenderStampArray() {

    // first encode uber-stamp with previous loop's  metalTextureComposite in a background polygon
    // note metalTexture= metalTextureComposite contains previous loop's drawable contents
    metalCommandEncode(renderCommandEncoder: renderCommandEncoder, stampLayer: stampLayerMode.stampLayerBG, vertexArrayStamps: vertexArrayStampsBG, metalTexture: metalTextureComposite) // background uber-stamp

    // empty out uber-stamp in preparation for the next cycle
    initializeStampArrays(stampLayer: stampLayerMode.stampLayerBG) 

    // next, encode current subCurve chunk polygons in foreground
    // note metalTexture=brushTexture.texture is a round brush texture with alpha
    metalCommandEncode(renderCommandEncoder: renderCommandEncoder, stampLayer: stampLayerMode.stampLayerFG, vertexArrayStamps: vertexArrayStampsFG, metalTexture: brushTexture.texture) // foreground sub-curve chunk

    renderCommandEncoder?.endEncoding() // finalize renderEncoder set up

    // now present bg + fg composite which is where I see the problem
    commandBuffer?.present(self.currentDrawable!)

    // 7b. Render to pipeline
    commandBuffer?.commit() // commit and send task to gpu

    metalTextureComposite = nil // empty out before re-populating
    metalTextureComposite = self.currentDrawable!.texture // set up bg texture for next iteration 

    metalStampComputeComposite() // compute coordinates for the background composite stamp for the next iteration

 } // end of func metalRenderStampArray()

我是否应该以不同的方式处理 metalTextureComposite(因为它是以 1/fps 写入的),如果是这样,我应该如何处理它?目标是为背景多边形和前导笔划多边形使用单一混合模式。任何帮助,将不胜感激。

标签: swift4metalrender-to-texturemetalkitmtlbuffer

解决方案


您似乎正在存储对可绘制纹理的引用,然后在下一帧使用它。

我可以看到两个可能的问题: - 在下一帧绘制周期,前一帧的纹理可能尚未被图形处理器绘制 - 系统和应用程序的代码可能同时使用可绘制的纹理

我建议尝试复制可绘制的纹理供您使用commandBuffer?.addCompletedHandler { }


推荐阅读