I am writing a game engine, and the engine creates a MTLBuffer and update the contents every frame. And the MTLBuffer is used in many draw calls, the codes looks like:
// draw call 1
memcpy(mtlbuffer,.contents + offset1, data, size);
[_mtlRenderEncoder drawIndexedPrimitives:toMTLPrimitive(primitiveType)
indexCount:count
indexType:toMTLIndexType(indexType)
indexBuffer:mtlbuffer
indexBufferOffset:offset1];
// draw call 2
memcpy(mtlbuffer,.contents + offset2, data, size);
[_mtlRenderEncoder drawIndexedPrimitives:toMTLPrimitive(primitiveType)
indexCount:count
indexType:toMTLIndexType(indexType)
indexBuffer:mtlbuffer
indexBufferOffset:offset2];
[_mtlCommandBuffer commit];
So i have two questions:
1. Can use the same mtlbuffer in two draw calls with offset?
2. Should update mtlbuffer after all commands are executed by GPU? If not, then the data will be modified when GPU use the mtlbuffer?
Thanks in advanced.