Appending (Write-Only) to Managed Buffers

I've been trying to figure out if my planned usage of managed buffers breaks any rules, but didn't find anything, so apologies if this has already been answered.

If I have a single Metal buffer which:

* is marked as managed and uses -didModifyRange:

* is only written to by the CPU and read by the GPU

* is only appended to

Can I keep a single copy of it around regardless of other n-buffering I do? E.g. can I do triple buffering elsewhere but keep one of this buffer around and append to it between draw loops when necessary?

A trivial example might be a buffer of int32s which contains the current item index as its value (bytes[i] = i;). If the buffer is over-allocated initially, (e.g. 1,000 items) but I only use the first 100 (and use -[id<MTLBuffer> didModifyRange:NSRange(0, 100 * sizeof(int32_t)]), can I later populate indices 100-200 and call -didModifyRange: on that portion in the next loop without tearing? Are there rules about this?

Replies

Yes, you should be able to do this. But just so we're on the same page...


You can write to a portion of a buffer with the CPU so long as the GPU isn't reading from that same portion (and you can gaurantee that it won't read from it until the CPU is done writing). The GPU can read from other portions of the buffer.


Another example: the triple buffering scheme we use in our samples can be implmented with a single buffer that's split into 3 portions.