3D Graphics with Metal · Function Specialization | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1258241-3d-graphics-with-metal/lessons/30
1 Like

So if I understand correctly, the if/else statement in the fragment function here is in fact not an if/else statement, and that this setup creates two different fragment functions based on the condition. Such two fragment functions will both be compiled before drawing begins, and assigned as specified here:

let fragmentFunction = try! Renderer.library.makeFunction(name: “fragment_main”, constantValues: functionConstants)

Is that correct?

Effectively, yes. I don’t know what happens internally, whether it’s actually two separate functions, but it’s two different code paths, rather than a conditional.

This is Apple’s sample code: Apple Developer Documentation with a beautiful PBR-ready fire engine model. (has Physically Based Rendering Textures such as metallic and roughness)

You should try and avoid conditionals when you can in shader functions, but if it’s too hard, then don’t optimize prematurely.

Thanks for the clarity. Understood