Chapter 21 Shader Attributes

In Chapter 21 when writing the shaders for the ray tracer it seems, for the first time in the book, the arguments to the shaders are not being qualified with attributes. E.g.

kernel void accumulateKernel(constant Uniforms &uniforms,
                             texture2d<float> renderTex,
                             texture2d<float, access:: read_write> t,
                             uint2 tid [[ thread_position_in_grid ]])

That is, uniforms doesn’t have [[ buffer(0) ]] and the textures don’t have [[ texture(0) ]] or [[ texture(1) ]] qualifying them. Are these implicitly added based on their order? I’m having trouble finding documentation confirming this.


@cocheret - Yes, they would be implicitly added, although I haven’t seen any documentation confirming this either.

The buffers are presented in the correct order on the Swift side:

computeEncoder?.setBuffer(uniformBuffer, offset: uniformBufferOffset,
                              index: 0)
computeEncoder?.setTexture(renderTarget, index: 0)
computeEncoder?.setTexture(accumulationTarget, index: 1)

You do have to be very careful doing that implicitly obviously. With the vertex rendering, because we’re using odd number buffers, eg Uniforms is buffer 11, you have to specify the buffer number. And because Model I/O adds buffers sometimes (as it did with the tangent and bi-tangent), it’s best to explicitly number your own buffers.

Btw - congratulations on almost reaching the end of the book! :trophy:


Thanks for the answer and congrats. I’ve done a ton of independent work to reinforce the great content here. I’m trying to implement screen space ambient occlusion and subsurface scattering and a utility for line/curve drawing with arbitrary thickness. Lots more I want to do. This has been a great way to bootstrap.

1 Like