金属多采样锯齿边(iOS)

金属多采样锯齿边(iOS),第1张

概述我正在尝试绘制一个将使用 Swift中的Metal进行动画处理的图形.我已成功绘制了一帧图形.从这张图片中可以看出,图形很简单.我无法弄清楚如何MultiSample绘图.一般来说,对Metal的引用似乎很少,特别是在Swift语法方面. self.metalLayer = CAMetalLayer()self.metalLayer.device = self.deviceself.metal 我正在尝试绘制一个将使用 Swift中的Metal进行动画处理的图形.我已成功绘制了一帧图形.从这张图片中可以看出,图形很简单.我无法弄清楚如何MultiSample绘图.一般来说,对Metal的引用似乎很少,特别是在Swift语法方面.

self.MetalLayer = cametallayer()self.MetalLayer.device = self.deviceself.MetalLayer.pixelFormat = .BGRA8Unormself.MetalLayer.framebufferOnly = trueself.MetalLayer.frame = self.vIEw.frameself.vIEw.layer.addSublayer(self.MetalLayer)self.renderer = SunRenderer(device: self.device,frame: self.vIEw.frame)let defaultlibrary = self.device.newDefaultlibrary()let fragmentProgram = defaultlibrary!.newFunctionWithname("basic_fragment")let vertexProgram = defaultlibrary!.newFunctionWithname("basic_vertex")let pipelinestateDescriptor = MTLRenderPipelineDescriptor()pipelinestateDescriptor.vertexFunction = vertexProgrampipelinestateDescriptor.fragmentFunction = fragmentProgrampipelinestateDescriptor.colorAttachments[0].pixelFormat = .BGRA8UnormpipelinestateDescriptor.colorAttachments[0].blendingEnabled = truepipelinestateDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperation.AddpipelinestateDescriptor.colorAttachments[0].AlphaBlendOperation = MTLBlendOperation.AddpipelinestateDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactor.sourceAlphapipelinestateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactor.sourceAlphapipelinestateDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlphapipelinestateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlpha

问题是,我如何平滑这些边缘?

更新:

所以我实现了一个MultiSample纹理,并将sampleCount设置为4.我没有注意到任何差异所以我怀疑我做错了什么.

最后:

所以最终确实出现了多次采样的工作原理.最初我有顶点用0 Alpha包裹这些“光线”.这是一个使边缘更平滑的技巧.使用这些顶点,多重采样似乎没有改善边缘.当我恢复为每条光线有4个顶点时,多次采样改善了它们的边缘.

let defaultlibrary = self.device.newDefaultlibrary()let fragmentProgram = defaultlibrary!.newFunctionWithname("basic_fragment")let vertexProgram = defaultlibrary!.newFunctionWithname("basic_vertex")let pipelinestateDescriptor = MTLRenderPipelineDescriptor()pipelinestateDescriptor.vertexFunction = vertexProgrampipelinestateDescriptor.fragmentFunction = fragmentProgrampipelinestateDescriptor.colorAttachments[0].pixelFormat = .BGRA8UnormpipelinestateDescriptor.colorAttachments[0].blendingEnabled = truepipelinestateDescriptor.sampleCount = 4pipelinestateDescriptor.colorAttachments[0].rgbBlendOperation =    MTLBlendOperation.AddpipelinestateDescriptor.colorAttachments[0].AlphaBlendOperation = MTLBlendOperation.AddpipelinestateDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactor.sourceAlphapipelinestateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactor.sourceAlphapipelinestateDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlphapipelinestateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlphalet desc = MTLTextureDescriptor()desc.textureType = MTLTextureType.Type2DMultisampledesc.wIDth = Int(self.vIEw.frame.wIDth)desc.height = Int(self.vIEw.frame.height)desc.sampleCount = 4desc.pixelFormat = .BGRA8Unormself.sampletex = self.device.newTextureWithDescriptor(desc)// When renderinglet renderPassDescriptor = MTLRenderPassDescriptor()renderPassDescriptor.colorAttachments[0].texture = sampletexrenderPassDescriptor.colorAttachments[0].resolveTexture = drawable.texturerenderPassDescriptor.colorAttachments[0].loadAction = .ClearrenderPassDescriptor.colorAttachments[0].clearcolor = MTLClearcolor(red: 23/255.0,green: 26/255.0,blue: 31/255.0,Alpha: 0.0)renderPassDescriptor.colorAttachments[0].storeAction = .MultisampleResolvelet commandBuffer = commandQueue.commandBuffer()let renderEncoder = commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor)renderEncoder.setRenderPipelinestate(pipelinestate)
解决方法 这对MTKVIEw来说简单得多(只需将sampleCount设置为视图和管道描述符上所需的MSAA样本数),但以下是滚动自己的步骤.

>创建渲染管道状态时,将渲染管道状态描述符的sampleCount设置为多重采样计数.>在启动时,每当图层调整大小时,通过创建纹理描述符(其textureType为MTLTextureType2DMultisample且其sampleCount为多重采样计数),创建尺寸等于图层可绘制尺寸的多重采样纹理.如果您使用的是深度和/或模板缓冲区,请在其描述符上设置这些属性.>渲染时,将MSAA纹理设置为渲染过程描述符的主要颜色附件的纹理,并将当前可绘制的纹理设置为resolveTexture.>将颜色附件的storeAction设置为MTLStoreActionMultisampleResolve,以便在传递结束时将MSAA纹理解析为渲染缓冲区.>像往常一样画画和呈现.

总结

以上是内存溢出为你收集整理的金属多采样/锯齿边(iOS)全部内容,希望文章能够帮你解决金属多采样/锯齿边(iOS)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1103430.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存