ios – 采用多次采样的金属离屏绘图

ios – 采用多次采样的金属离屏绘图,第1张

概述如何将基元渲染到屏幕外纹理,而不是直接进入屏幕? 我有一组三角形和相应的颜色,我只想绘制它们的方式与屏幕相同,但进入屏幕外纹理,我可以保存到文件中. 任何人都可以给我看一个代码示例吗? 好的,我自己意识到了.这段代码完成了这项工作,唯一的例外是它绘制了太大的三角形,但这是Vertex函数的另一个主题. 这是我的代码: let fragmentProgram = defaultLibrary.new 如何将基元渲染到屏幕外纹理,而不是直接进入屏幕?

我有一组三角形和相应的颜色,我只想绘制它们的方式与屏幕相同,但进入屏幕外纹理,我可以保存到文件中.

任何人都可以给我看一个代码示例吗?

解决方法 好的,我自己意识到了.这段代码完成了这项工作,唯一的例外是它绘制了太大的三角形,但这是Vertex函数的另一个主题.

这是我的代码:

let fragmentProgram = defaultlibrary.newFunctionWithname("image_fragmentT")    let vertexProgram = defaultlibrary.newFunctionWithname("image_vertexT")    struct VertexT {        var x,y,z,w : float        var r,g,b,a : float    }    let vertexDescriptor = MTLVertexDescriptor()    vertexDescriptor.attributes[0].offset = 0    vertexDescriptor.attributes[0].format = .float4    vertexDescriptor.attributes[0].bufferIndex = 0    vertexDescriptor.attributes[1].offset = 0    vertexDescriptor.attributes[1].format = .float4    vertexDescriptor.attributes[1].bufferIndex = 0    vertexDescriptor.layouts[0].stepFunction = .PerVertex    vertexDescriptor.layouts[0].strIDe = sizeof(VertexT)    let pipelinestateDescriptor = MTLRenderPipelineDescriptor()    pipelinestateDescriptor.vertexDescriptor = vertexDescriptor    pipelinestateDescriptor.vertexFunction = vertexProgram    pipelinestateDescriptor.fragmentFunction = fragmentProgram    pipelinestateDescriptor.colorAttachments[0].pixelFormat = .RGBA8Unorm;    pipelinestateDescriptor.colorAttachments[0].blendingEnabled = true    pipelinestateDescriptor.sampleCount = 4    pipelinestateDescriptor.colorAttachments[0].rgbBlendOperation =    MTLBlendOperation.Add    pipelinestateDescriptor.colorAttachments[0].AlphaBlendOperation = MTLBlendOperation.Add    pipelinestateDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactor.sourceAlpha    pipelinestateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactor.sourceAlpha    pipelinestateDescriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactor.OneMinusSourceAlpha    pipelinestateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactor.OneMinusSourceAlpha    let sampleDesc = MTLTextureDescriptor()    sampleDesc.textureType = MTLTextureType.Type2DMultisample    sampleDesc.wIDth = inTexture.wIDth    sampleDesc.height = inTexture.height    sampleDesc.sampleCount = 4    sampleDesc.pixelFormat = .RGBA8Unorm    sampleDesc.storageMode = .Private    sampleDesc.usage = .rendertarget    let sampletex = device.device.newTextureWithDescriptor(sampleDesc)    let renderPassDescriptor = MTLRenderPassDescriptor()    renderPassDescriptor.colorAttachments[0].texture = sampletex    renderPassDescriptor.colorAttachments[0].resolveTexture = outTexture    renderPassDescriptor.colorAttachments[0].loadAction = .Clear    renderPassDescriptor.colorAttachments[0].clearcolor = MTLClearcolor(red: 0.0,green: 0.0,blue: 0.0,Alpha: 0.0)    renderPassDescriptor.colorAttachments[0].storeAction = .MultisampleResolve    let renderCB = commandQueue.commandBuffer()    let renderCommandEncoder = renderCB.renderCommandEncoderWithDescriptor(renderPassDescriptor)    let pipelinestate = try! device.device.neWrenderPipelinestateWithDescriptor(pipelinestateDescriptor)    renderCommandEncoder.setRenderPipelinestate(pipelinestate)    let vertexBuf = device.device.newBufferWithLength(triangles.count * 3 * sizeof(VertexT),options: .cpuCacheModeDefaultCache)    var vBufPointer = [VertexT]()    for i in 0..<triangles.count {        // create buffer here    }    memcpy(vertexBuf.contents(),&vBufPointer,triangles.count * 3 * sizeof(VertexT))    renderCommandEncoder.setVertexBuffer(vertexBuf,offset: 0,atIndex: 0)    renderCommandEncoder.drawPrimitives(.Triangle,vertexStart: 0,vertexCount: triangles.count * 3)    renderCommandEncoder.endEnCoding()    renderCB.commit()    renderCB.waitUntilCompleted()

您现在的图像是在outTexture变量中.

总结

以上是内存溢出为你收集整理的ios – 采用多次采样的金属离屏绘图全部内容,希望文章能够帮你解决ios – 采用多次采样的金属离屏绘图所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1070707.html

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

发表评论

登录后才能评论

评论列表(0条)

保存