Group Group Group Group Group Group Group Group Group

When to use stride or size

Hi,

As far as I know, MemoryLayout<float3>.stride is used for making buffer and setting bytes for vertex/fragment functions.
I understand what stride and size are, but still have confusion when to use stride or size.

For example, in Chapter 4, p.119, is like:

let originalBuffer = device.makeBuffer(
      bytes: &vertices, 
      length: MemoryLayout.stride * vertices.count, 
      options: [])

However, in Chapter 14, p.655, is like:

quadVerticesBuffer = 
    Renderer.device.makeBuffer(bytes: quadVertices, 
      length: MemoryLayout.size * quadVertices.count, 
      options: [])

Which one should I follow and why?

stride within an array or array held in buffer. Greg Heo wrote a lovely illustrated article on why: Size, Stride, Alignment - Swift Unboxed

size works there because size and stride are the same in that one case.

Thank you for raising this topic. It does need to be mentioned in the book.

2 Likes

What a nice article. It helps me a lot.
Thank you.

1 Like