Fast way to copy memory between buffers, with a destination offset

What would be the fastest way to copy elements from the source buffer, to a destination buffer, with an offset in the destination buffer?

I’d like to start a job, which would copy the contents of several, equally sized buffers, to a destination buffer in parallel. Each source buffer would be mapped to a different starting index in the destination buffer, based on their jobIndex:

Maybe that’s word soup. Here’s a visual example:

Source Buffers:

SourceBufferA: [A0, A1, A2]
SourceBufferB: [B0, B1, B2]
SourceBufferC: [C0, C1, C2]

Destination Buffer (after copy job):

DestBuffer: [A0, A1, A2, B0, B1, B2, C0, C1, C2]


The destination buffer can be assumed to already be the correct size.

This seems like something basic I should be able to figure out. But since UsafeUtility.MemCpy takes a void* for the destination buffer, I’m not sure how to do the destination offset.

Thank you for any advice. I have my face-palming hand ready!

something like this?

                if (chunk.DidChange(LocalToWorldType, LastSystemVersion))
                {
                    unsafe
                    {
                        var sourcePtr = chunk.GetNativeArray(LocalToWorldType).GetUnsafeReadOnlyPtr();
                        var destinationPtr = (byte*) LtwBuffer.GetUnsafePtr() + UnsafeUtility.SizeOf<LocalToWorld>() * firstEntityIndex;
                        var copySizeInBytes = UnsafeUtility.SizeOf<LocalToWorld>() * chunk.Count;
                       
                        UnsafeUtility.MemCpy(destinationPtr, sourcePtr, copySizeInBytes);
                        value.y = chunk.Count;
                    }
                }
3 Likes

Facepalm

Thank you! :slight_smile: