Know Your Spaces

Removing unnecessary transformations between spaces can help optimise shaders

A common mistake in vertex shaders is to perform unnecessary transformations between model space, world space, view space, and clip space. If the model-world transformation is a rigid body transformation (only consisting of of rotations, translations, mirroring, lighting, or similar) then calculations can be performed directly in model space.

Transforming uniforms such as light positions and directions to model space is a per-mesh operation. This is as opposed to transforming the vertex position to world or view space once per vertex, which is an optimisation. In cases where a particular space must be used, for example, for cube map reflections, it is often best to use this single space throughout.