Add inverse model matrix to Mesh uniform buffer
Objective
- Enable users to convert positions from world to object local space in shaders. This is often useful when f.eg. ray marching.
- Match
unity_WorldToObjectfunctionality from Unity
Solution
- Add
model_inversefield to Mesh and Mesh2d structs - Compute and assign the variable to MeshUniform and Mesh2dUniform. This is free because we are already computing the inverse model matrix for the
inverse_transpose_modelfield. - Additional change was to rename the field from
transformtomodelin the MeshUniform and Mesh2dUniform structs, to match structs in shaders and reduce confusion. I can revert it if this is unwanted.
I'd like to see some performance numbers here. MeshUniform's size in memory is notorious for impacting end-to-end rendering performance. Particularly since IIRC, matrix transposes are basically free on modern GPU hardware.
I have run many_cubes test with cargo run --example many_cubes --release sphere.
AdapterInfo { name: "NVIDIA GeForce RTX 3060 Laptop GPU", vendor: 4318, device: 9504, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "517.48", backend: Vulkan }
| Diagnostic | Before change | After Change |
|---|---|---|
| Frametime | 23 - 26 ms | 24 - 27 ms |
| Framerate | 38 - 42 FPS | 34 - 40 FPS |
Not great.
In this case, I guess to get a model inverse matrix, one should do let inverse_model = transpose(mesh.inverse_transpose_model); which should be pretty cheap.
I guess this PR can be closed.
It may have worth to move the transpose explicitly to the GPU. If it's "free" on the GPU and has a cost on the CPU, makes more sense to just provide the inverse and transpose it in the shader. It's also a simpler mental model for developers.
@pbaja do you mind trying the alternative I proposed above? If it shows that it's as performant, we can probably merge the change.
Now that #12773 is merged, this can be closed. The model inverse transpose is computed on the GPU anyways due to bandwidth savings. Getting inverse model is just a different unpacking of it and should be pretty cheap.
I wouldn't say that it can be closed only because of #12773 because that is only addresses platforms with compute. That said, the inverse transpose is available and can be transposed back and reconstructed into the full inverse model matrix on all platforms, and various performance optimisations have and continue to be done to make performance as good as it reasonably can be.