I'm new to Metal and I'm baffled...
I have a set of vertices which I'm "tiling" out from a master texture. I'm using both a vertex shader and a fragment sampling shader to do this.
I wanted to overlay the first drawing pass with an additional drawing pass using a different texture. I was getting all stressed out about having to do Load/Store actions and such. Alas, no -- below is what I did in my render loop in Swift. I should say that the amount of drawing in the overlay obliterates less than 5% of that in the first pass.
This works. Is it really just this easy?
I just have this nagging feeling that I'm missing something. On the other hand, I may have been under the mistaken impression that one only gets one chance in a render loop, say, to set the vertex buffer of the render encoder.
let renderEncoder : MTLRenderCommandEncoder = (commandBuffer?.makeRenderCommandEncoder(descriptor: renderPassDescriptor!))!
renderEncoder.label = "MyRenderEncoder"
renderEncoder.setViewport(MTLViewport.init(originX: 0.0, originY: 0.0, width: Double(self.drawableSize.width), height: Double(self.drawableSize.height), znear: -1.0, zfar: 1.0))
renderEncoder.setRenderPipelineState(self.pipelineState)
// draw main content
let vertices = ...
let vCount = vertices?.count
let vSize = vCount!*MemoryLayout.size(ofValue: vertices?[0])
let mBuff = self.device!.makeBuffer(bytes: vertices!, length: vSize, options: [])
renderEncoder.setVertexBuffer(mBuff, offset: 0, at: 0)
renderEncoder.setVertexBytes(&self.viewportSize, length: MemoryLayout.size(ofValue: self.viewportSize), at: 1)
renderEncoder.setFragmentTexture(inputTexture, at: 0)
renderEncoder.drawPrimitives(type: MTLPrimitiveType.triangle, vertexStart: 0, vertexCount: vCount!)
// draw overlaying content
let vertices2 = ...
let vCount2 = vertices2?.count
let vSize2 = vCount2!*MemoryLayout.size(ofValue: vertices2?[0])
let mBuff2 = self.device!.makeBuffer(bytes: vertices2!, length: vSize2, options: [])
renderEncoder.setVertexBuffer(mBuff2, offset: 0, at: 0)
renderEncoder.setVertexBytes(&self.viewportSize, length: MemoryLayout.size(ofValue: self.viewportSize), at: 1)
renderEncoder.setFragmentTexture(inputTexture2, at: 0)
renderEncoder.drawPrimitives(type: MTLPrimitiveType.triangle, vertexStart: 0, vertexCount: vCount2!)
// done drawing
renderEncoder.endEncoding()
commandBuffer?.present(view.currentDrawable!)
commandBuffer?.commit()