Metal makeBuffer Question

Hi,

I was looking at the documentation for Metal and saw there are 3 instance methods for creating a Metal buffer:

  1. makeBuffer(length:)
  2. makeBuffer(bytes:, length:)
  3. makeBuffer(bytesNoCopy:, length:)

Though, I was wondering what the purpose is of using the bytes alternative when the bytesNoCopy method exists. Especially when working within the unified memory model of the iPhone and newer Mac models.

I would assume the noCopy method has less overhead since it just wraps an existing contiguous memory block, while the normal bytes version creates a full copy (and thus, more overhead?).

Is there a simple answer for them both existing, or could you point me to some documentation explaining their difference in greater detail than the Apple developer docs?

Thanks in advance!

It's mostly about who own the buffer memory: you or the Metal runtime. The no-copy method won't copy the data, but you are responsible for the memory allocation, plus there are some additional requirements your allocations has to fulfil (such as being page-aligned). So it's not like you can take any CPU memory allocation and turn it into a Metal buffer. The bytes method will make a new allocation and copy the data, but this is fast enough in practice, so if all you need is to upload some data to the GPU in advance it can be a reasonable default choice.

I think for a lot of practical applications, both these method variants are a bit niche as it's not that often that you start with some block of CPU data that you want to expose to Metal. The most frequent case is that you allocate a buffer and then upload the data into the buffer memory directly (e.g. from a file or some other data-producing algorithm). Apple just gives you a bunch of options so the you can pick what best suits your use case.

Metal makeBuffer Question
 
 
Q