From 1d0ea78f36312d6efdd00293f78d74828229ca8b Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Tue, 20 Feb 2024 17:25:25 +0100 Subject: [PATCH] Save 16 bytes per MeshUniform in uniform/storage buffers (#11999) # Objective - Save 16 bytes per MeshUniform in uniform/storage buffers. ## Solution - Reorder members of MeshUniform to capitalise on alignment and size rules for tighter data packing. Before the size of a MeshUniform was 160 bytes, and after it is 144 bytes, saving 16 bytes of unused padding for alignment. --- ## Changelog - Reduced the size of MeshUniform by 16 bytes. --- crates/bevy_pbr/src/render/mesh.rs | 14 +++++++------- crates/bevy_pbr/src/render/mesh_types.wgsl | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 7ea546749f744..ab89ceb407eb3 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -201,6 +201,13 @@ pub struct MeshUniform { // Affine 4x3 matrices transposed to 3x4 pub transform: [Vec4; 3], pub previous_transform: [Vec4; 3], + // 3x3 matrix packed in mat2x4 and f32 as: + // [0].xyz, [1].x, + // [1].yz, [2].xy + // [2].z + pub inverse_transpose_model_a: [Vec4; 2], + pub inverse_transpose_model_b: f32, + pub flags: u32, // Four 16-bit unsigned normalized UV values packed into a `UVec2`: // // <--- MSB LSB ---> @@ -211,13 +218,6 @@ pub struct MeshUniform { // // (MSB: most significant bit; LSB: least significant bit.) pub lightmap_uv_rect: UVec2, - // 3x3 matrix packed in mat2x4 and f32 as: - // [0].xyz, [1].x, - // [1].yz, [2].xy - // [2].z - pub inverse_transpose_model_a: [Vec4; 2], - pub inverse_transpose_model_b: f32, - pub flags: u32, } impl MeshUniform { diff --git a/crates/bevy_pbr/src/render/mesh_types.wgsl b/crates/bevy_pbr/src/render/mesh_types.wgsl index 89b73be2bd6f1..a3aec0b9ebe70 100644 --- a/crates/bevy_pbr/src/render/mesh_types.wgsl +++ b/crates/bevy_pbr/src/render/mesh_types.wgsl @@ -5,7 +5,6 @@ struct Mesh { // Use bevy_render::maths::affine_to_square to unpack model: mat3x4, previous_model: mat3x4, - lightmap_uv_rect: vec2, // 3x3 matrix packed in mat2x4 and f32 as: // [0].xyz, [1].x, // [1].yz, [2].xy @@ -15,6 +14,7 @@ struct Mesh { inverse_transpose_model_b: f32, // 'flags' is a bit field indicating various options. u32 is 32 bits so we have up to 32 options. flags: u32, + lightmap_uv_rect: vec2, }; #ifdef SKINNED