-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.rs
222 lines (185 loc) · 7.29 KB
/
mesh.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//! Memory usage tracking for Bevy's [`Mesh`] type.
//!
//! The behavior can be customized using the [`MemoryConfig`].
use bevy::{
app::Plugin,
asset::Assets,
ecs::system::Res,
render::mesh::{Mesh, VertexAttributeValues},
utils::HashSet,
};
use crate::{
systems, DataSize, DataSizeEstimator, MemoryConfig, MemoryStats, MemoryUsage,
RegisterSizedTypes,
};
/// Adds memory tracking for [`Mesh`] assets.
#[derive(Debug, Default)]
pub struct MeshMemoryUsagePlugin;
impl Plugin for MeshMemoryUsagePlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.register_sized_type::<Mesh, _, _>(update_stats_for_meshes);
}
}
#[derive(Debug)]
struct MeshSizeEstimator<'a> {
additional_vertex_attributes: &'a [&'static str],
}
impl<'a> Default for MeshSizeEstimator<'a> {
fn default() -> Self {
static NO_ADDITIONAL_ATTRIBUTES: [&str; 0] = [];
Self {
additional_vertex_attributes: &NO_ADDITIONAL_ATTRIBUTES[..],
}
}
}
impl<'a> DataSizeEstimator<Mesh> for MeshSizeEstimator<'a> {
/// Sums up the sizes of the mesh's vertex attribute lists.
fn estimate_heap_size(&self, mesh: &Mesh) -> usize {
const DEFAULT_ATTRIBUTES: [&str; 7] = [
Mesh::ATTRIBUTE_COLOR,
Mesh::ATTRIBUTE_NORMAL,
Mesh::ATTRIBUTE_TANGENT,
Mesh::ATTRIBUTE_POSITION,
Mesh::ATTRIBUTE_UV_0,
Mesh::ATTRIBUTE_JOINT_WEIGHT,
Mesh::ATTRIBUTE_JOINT_INDEX,
];
let attributes: HashSet<&str> = DEFAULT_ATTRIBUTES
.into_iter()
.chain(self.additional_vertex_attributes.iter().copied())
.collect();
let total_size_of_attributes: usize = attributes
.into_iter()
.filter_map(|attribute_name| mesh.attribute(attribute_name))
.map(|attributes| MemoryStats::total_size_of_with_estimator(attributes, self))
.sum();
total_size_of_attributes
}
}
impl<'a> DataSizeEstimator<VertexAttributeValues> for MeshSizeEstimator<'a> {
#[inline]
fn estimate_heap_size(&self, values: &VertexAttributeValues) -> usize {
use VertexAttributeValues::*;
match values {
Float32(v) => v.estimate_heap_size(),
Sint32(v) => v.estimate_heap_size(),
Uint32(v) => v.estimate_heap_size(),
Float32x2(v) => v.estimate_heap_size(),
Sint32x2(v) => v.estimate_heap_size(),
Uint32x2(v) => v.estimate_heap_size(),
Float32x3(v) => v.estimate_heap_size(),
Sint32x3(v) => v.estimate_heap_size(),
Uint32x3(v) => v.estimate_heap_size(),
Float32x4(v) => v.estimate_heap_size(),
Sint32x4(v) => v.estimate_heap_size(),
Uint32x4(v) => v.estimate_heap_size(),
Sint16x2(v) => v.estimate_heap_size(),
Snorm16x2(v) => v.estimate_heap_size(),
Uint16x2(v) => v.estimate_heap_size(),
Unorm16x2(v) => v.estimate_heap_size(),
Sint16x4(v) => v.estimate_heap_size(),
Snorm16x4(v) => v.estimate_heap_size(),
Uint16x4(v) => v.estimate_heap_size(),
Unorm16x4(v) => v.estimate_heap_size(),
Sint8x2(v) => v.estimate_heap_size(),
Snorm8x2(v) => v.estimate_heap_size(),
Uint8x2(v) => v.estimate_heap_size(),
Unorm8x2(v) => v.estimate_heap_size(),
Sint8x4(v) => v.estimate_heap_size(),
Snorm8x4(v) => v.estimate_heap_size(),
Uint8x4(v) => v.estimate_heap_size(),
Unorm8x4(v) => v.estimate_heap_size(),
}
}
}
/// This system updates the [`MemoryStats`] for the [`Mesh`] type.
pub fn update_stats_for_meshes(
memory_config: Res<MemoryConfig>,
memory_usage: Res<MemoryUsage>,
meshes: Res<Assets<Mesh>>,
) {
let additional_vertex_attributes = &memory_config.additional_mesh_vertex_attributes[..];
let estimator = MeshSizeEstimator {
additional_vertex_attributes,
};
systems::update_stats::<Mesh, _>(&*memory_config, &*memory_usage, || {
MemoryStats::from_values_with_estimator(
meshes.iter().map(|(_handle, mesh)| mesh),
&estimator,
)
})
}
/***************************************************************************************************
dMMMMMMP dMMMMMP .dMMMb dMMMMMMP .dMMMb
dMP dMP dMP" VP dMP dMP" VP
dMP dMMMP VMMMb dMP VMMMb
dMP dMP dP .dMP dMP dP .dMP
dMP dMMMMMP VMMMP" dMP VMMMP"
***************************************************************************************************/
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
use bevy::render::render_resource::PrimitiveTopology;
use maplit::hashmap;
const ATTRIBUTE_STACK_SIZE: usize = std::mem::size_of::<VertexAttributeValues>();
fn create_mesh(attributes: HashMap<&'static str, usize>) -> Mesh {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
for (name, bytes) in attributes.iter() {
if bytes % 2 != 0 {
panic!("Must specify a multiple of 2 bytes");
}
let values = VertexAttributeValues::Uint8x2(vec![[0, 0]; *bytes / 2]);
mesh.set_attribute(*name, values);
}
mesh
}
#[test]
fn counts_default_attributes() {
let mesh = create_mesh(hashmap! {
Mesh::ATTRIBUTE_POSITION => 100,
Mesh::ATTRIBUTE_NORMAL => 100,
});
let estimator = MeshSizeEstimator::default();
let estimated_heap_size = MemoryStats::heap_size_of_with_estimator(&mesh, &estimator);
assert_eq!(estimated_heap_size, 200 + ATTRIBUTE_STACK_SIZE * 2);
}
#[test]
fn does_not_count_extra_attributes_if_not_configured() {
let mesh = create_mesh(hashmap! {
Mesh::ATTRIBUTE_POSITION => 100,
Mesh::ATTRIBUTE_NORMAL => 100,
"foo" => 100,
"bar" => 100,
});
let estimator = MeshSizeEstimator::default();
let estimated_heap_size = MemoryStats::heap_size_of_with_estimator(&mesh, &estimator);
assert_eq!(estimated_heap_size, 200 + ATTRIBUTE_STACK_SIZE * 2);
}
#[test]
fn counts_extra_attributes_if_configured() {
let mesh = create_mesh(hashmap! {
Mesh::ATTRIBUTE_POSITION => 100,
Mesh::ATTRIBUTE_NORMAL => 100,
"foo" => 100,
"bar" => 100,
});
let estimator = MeshSizeEstimator {
additional_vertex_attributes: &["foo", "bar"],
};
let estimated_heap_size = MemoryStats::heap_size_of_with_estimator(&mesh, &estimator);
assert_eq!(estimated_heap_size, 400 + ATTRIBUTE_STACK_SIZE * 4);
}
#[test]
fn does_not_count_attribute_twice() {
let mesh = create_mesh(hashmap! {
Mesh::ATTRIBUTE_POSITION => 1000,
Mesh::ATTRIBUTE_NORMAL => 1000,
});
let estimator = MeshSizeEstimator {
additional_vertex_attributes: &[Mesh::ATTRIBUTE_POSITION],
};
let estimated_size = MemoryStats::total_size_of_with_estimator(&mesh, &estimator);
assert!(estimated_size < 3000);
}
}