[Chapter 8] Error in renderEncoder.setFragmentBytes() calls

In the Chapter 8 code, there is the following code in Model.swift:

encoder.setVertexBytes(
    &uniforms,
    length: MemoryLayout<Uniforms>.stride,
    index: UniformsBuffer.index)

encoder.setFragmentBytes(
    &params,
    length: MemoryLayout<Uniforms>.stride,
    index: ParamsBuffer.index)

That length parameter in the second call should be MemoryLayout<Params>.stride, not MemoryLayout<Uniforms>.stride. I also think a similar error exists in Chapter 7 code as well, where the actual text says:

In draw(in:) , before calling the methods to render the model or quad, send the parameters to the fragment function:

renderEncoder.setFragmentBytes(
  &params,
  length: MemoryLayout<Uniforms>.stride,
  index: 12)

Great first post, thank you :smiley: .

It works because the stride of Uniforms is larger than the stride of Params, so thereโ€™s extra unused allocated space.

1 Like