Can you change textures and buffers between drawing primitives?

Is this safe? Can you replace the texture/buffer at a given "index", and then draw different primitive shapes, or does this set up a race condition? Ie, could this end up drawing all 10 shapes with the texture and vertices of the last one?


let enc = [ make render encoder ]
for shape in tenDifferentShapes {
   enc.setFragmentTexture([texture for shape], index: 0)
   enc.setVertexBytes([verts for shape], index: 0)
   enc.drawPrimitives(...)
}
enc.endEncoding()

Replies

This is fine.


However, if you've ever heard of someone talk about reducing the number of draw calls and texture switching, this is what they are refering to.


It will be expected that the more draw calls you have and the more times you send data, the longer it will take for the code to run.


Often, you can optimize this by using drawPrimitives(...)instanceCount, and combining draw calls, and using texture arrays. (Btw texture arrays are not supported by the MTLTextureLoader wrapper, so you'll need to load them by hand yourself https://developer.apple.com/documentation/metal/mtltexture/1515679-replaceregion)


Doing these optimizations has a big effect especially for your app where you likely would want to render a lot of shapes.


Edit: I meant Instanced drawing, not indexed.