Hey! Let me start by saying I intensely enjoy going through the tutorials in this book. I have graphics background, yet in each chapter learn something new or solidify my knowledge. Great!
I have two questions with regards to Chapter 10:
- In the
fragment_main
shader function definition:
fragment float4 fragment_main(
constant Params ¶ms [[buffer(ParamsBuffer)]],
constant Light *lights [[buffer(LightBuffer)]])
{
// ...
}
Why do we use reference for params
and pointer to lights
? I have some lightweight C++ knowledge and AFAIK in Metal Shading Language they have the same semantics, i.e. references and pointers can be interchanged?
Furthermore, both are set in Swift via the renderEncoder.setFragmentBytes
method via UnsafeRawPointer
var lights = scene.lighting.lights
renderEncoder.setFragmentBytes(
&lights,
length: MemoryLayout<Light>.stride * lights.count,
index: LightBuffer.index
)
encoder.setFragmentBytes(
¶ms,
length: MemoryLayout<Params>.stride,
index: ParamsBuffer.index)
- When we construct the separate 3x3 normal world matrix, in the book this code snippet is used:
However this does not transpose and inverse the uniforms.modelMatrix
. In fact, in MathLibrary.swift
there exists this method, which seems correct:
extension float3x3 {
init(normalFrom4x4 matrix: float4x4) {
self.init()
columns = matrix.upperLeft.inverse.transpose.columns
}
}
However it is not used anywhere in this lesson.
Thanks in advance!