Search code examples
swifttexturesmetalmetalkit

Repitition of texture on pan gesture in Metal Shading Language


I am new to metal shading language.So your kind guidance would be so helpful. I just repeated texture using metal as shown on below image (Image sequence). But it seems not smoothy.How can I make it to be appeared as smoothy line as done in following link for metal points.(overlapping Metal Point primitives and blending)

At the moment what I do is allocating memory for vertex buffer (MTLBuffer) at each touch point in my gesture.

code:

func allocateMemoryForVetexBuffer(vertices: Array<Vertex>){
        var vertexData = Array<Float>()
        for vertex in vertices{
            vertexData += vertex.floatBuffer()
        }

        let dataSize = vertexData.count * MemoryLayout.size(ofValue: vertexData[0])
        print("dataSize:\(dataSize)")
        vertexBuffer = device.makeBuffer(bytes: vertexData, length: dataSize, options: [])

        vertexCount = vertices.count

    }

I am calling this method in my pan gesture recognizer.But it causes to drop FPS.So my problem is without knowing how many textures will add on single pan on metal layer, how may I allocate memory for Frame Buffer.


Solution

  • You're unnecessarily using instancing.

    Change this:

    renderEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: vertexCount, 
                                 instanceCount: vertexCount / 3)
    

    to this:

    renderEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: vertexCount)