bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Add inverse model matrix to Mesh uniform buffer

Open pbaja opened this issue 3 years ago • 3 comments

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_WorldToObject functionality from Unity

Solution

  • Add model_inverse field 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_model field.
  • Additional change was to rename the field from transform to model in the MeshUniform and Mesh2dUniform structs, to match structs in shaders and reduce confusion. I can revert it if this is unwanted.

pbaja avatar Nov 14 '22 05:11 pbaja

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.

james7132 avatar Nov 14 '22 12:11 james7132

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.

pbaja avatar Nov 14 '22 14:11 pbaja

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.

james7132 avatar Nov 14 '22 19:11 james7132

@pbaja do you mind trying the alternative I proposed above? If it shows that it's as performant, we can probably merge the change.

james7132 avatar Dec 04 '22 07:12 james7132

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.

atlv24 avatar May 04 '24 00:05 atlv24

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.

superdump avatar May 04 '24 20:05 superdump