byte-engine 0.1.0

A composable Rust game engine focused on graphics, input, audio, physics, and retained UI.
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use std::{alloc::Allocator, hash::Hash, marker::PhantomData, usize};

use utils::{
	hash::{HashMap, HashMapExt as _},
	StableVec, StableVecHandle,
};

use crate::core::factory::Handle;

pub struct MeshStats {
	vertex_count: usize,
	index_count: usize,
}

impl MeshStats {
	pub fn new(vertex_count: usize, index_count: usize) -> Self {
		Self {
			vertex_count,
			index_count,
		}
	}
}

pub struct AddMeshResponse {
	id: usize,
	base_vertex: usize,
	base_index: usize,
}

impl AddMeshResponse {
	pub fn id(&self) -> usize {
		self.id
	}

	pub fn vertex_offset(&self) -> usize {
		self.base_vertex
	}

	pub fn index_offset(&self) -> usize {
		self.base_index
	}
}

struct Mesh {
	vertex_count: usize,
	index_count: usize,
	base_index: usize,
	base_vertex: usize,
}

pub struct MeshBuffersStats<I> {
	vertex_count: usize,
	index_count: usize,

	meshes: HashMap<usize, Mesh>,

	instances: StableVec<(usize, I)>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InstanceBatch {
	base_index: usize,
	base_vertex: usize,
	instance_count: usize,
	index_count: usize,
	base_instance: usize,
}

impl InstanceBatch {
	pub fn base_index(&self) -> usize {
		self.base_index
	}

	pub fn base_vertex(&self) -> usize {
		self.base_vertex
	}

	pub fn instance_count(&self) -> usize {
		self.instance_count
	}

	pub fn index_count(&self) -> usize {
		self.index_count
	}

	pub fn base_instance(&self) -> usize {
		self.base_instance
	}
}

impl<I> MeshBuffersStats<I> {
	pub fn does_mesh_exist(&self, hash: u64) -> Option<usize> {
		if self.meshes.contains_key(&(hash as usize)) {
			Some(hash as usize)
		} else {
			None
		}
	}

	pub fn add_mesh(&mut self, mesh: MeshStats, hash: u64) -> AddMeshResponse {
		if let Some(existing_mesh) = self.meshes.get(&(hash as usize)) {
			assert_eq!(
				existing_mesh.vertex_count, mesh.vertex_count,
				"Tried to add a mesh with a hash which already exists but their vertex counts don't match."
			);
			assert_eq!(
				existing_mesh.index_count, mesh.index_count,
				"Tried to add a mesh with a hash which already exists but their index counts don't match."
			);

			return AddMeshResponse {
				id: hash as _,
				base_vertex: existing_mesh.base_vertex,
				base_index: existing_mesh.base_index,
			};
		}

		let vertex_offset = self.vertex_offset();
		let index_offset = self.index_offset();

		self.vertex_count += mesh.vertex_count;
		self.index_count += mesh.index_count;

		let mesh_id = hash as usize;

		self.meshes.insert(
			hash as usize,
			Mesh {
				base_vertex: vertex_offset,
				base_index: index_offset,
				vertex_count: mesh.vertex_count,
				index_count: mesh.index_count,
			},
		);

		AddMeshResponse {
			id: mesh_id,
			base_vertex: vertex_offset,
			base_index: index_offset,
		}
	}

	pub fn add_instance(&mut self, mesh_id: usize, instance_data: I) -> StableVecHandle {
		assert!(
			self.meshes.contains_key(&mesh_id),
			"Provided mesh_id for instance does not exist!"
		);
		self.instances.push((mesh_id, instance_data))
	}

	/// Removes an instance without shifting the remaining instance indices.
	pub fn remove_instance(&mut self, instance_id: StableVecHandle) -> Option<I> {
		self.instances.remove(instance_id).map(|(_, instance)| instance)
	}

	pub fn get_instance_batches(&self) -> InstanceBatches<'_, I> {
		InstanceBatches {
			batches: self.collect_instance_batches_in(std::alloc::Global),
			_marker: PhantomData,
		}
	}

	pub fn get_instance_batches_in<'a>(&self, allocator: &'a bumpalo::Bump) -> Vec<InstanceBatch, &'a bumpalo::Bump> {
		self.collect_instance_batches_in(allocator)
	}

	/// Collects contiguous mesh batches with the caller-selected allocation strategy.
	fn collect_instance_batches_in<A: Allocator>(&self, allocator: A) -> Vec<InstanceBatch, A> {
		let mut batches = Vec::with_capacity_in(self.instances.len(), allocator);
		let mut current_batch: Option<(usize, InstanceBatch)> = None;

		for instance_id in 0..self.instances.slots_len() {
			let Some((mesh_id, _)) = self.instances.get_slot(instance_id) else {
				if let Some((_, batch)) = current_batch.take() {
					batches.push(batch);
				}
				continue;
			};

			let mesh = &self.meshes.get(mesh_id).unwrap();
			match &mut current_batch {
				Some((current_mesh_id, batch)) if current_mesh_id == mesh_id => {
					batch.instance_count += 1;
				}
				Some(_) => {
					let (_, batch) = current_batch
						.replace((
							*mesh_id,
							InstanceBatch {
								index_count: mesh.index_count,
								instance_count: 1,
								base_vertex: mesh.base_vertex,
								base_index: mesh.base_index,
								base_instance: instance_id,
							},
						))
						.unwrap();
					batches.push(batch);
				}
				None => {
					current_batch = Some((
						*mesh_id,
						InstanceBatch {
							index_count: mesh.index_count,
							instance_count: 1,
							base_vertex: mesh.base_vertex,
							base_index: mesh.base_index,
							base_instance: instance_id,
						},
					));
				}
			}
		}

		if let Some((_, batch)) = current_batch {
			batches.push(batch);
		}

		batches
	}

	pub fn vertex_offset(&self) -> usize {
		self.vertex_count
	}

	pub fn index_offset(&self) -> usize {
		self.index_count
	}

	pub fn get_instance_id(&self, handle: I) -> Option<StableVecHandle>
	where
		I: Eq,
	{
		self.instances
			.handled_iter()
			.find_map(|(instance_handle, (_, h))| (*h == handle).then_some(instance_handle))
	}
}

impl<I> Default for MeshBuffersStats<I> {
	fn default() -> Self {
		Self {
			vertex_count: 0,
			index_count: 0,
			meshes: HashMap::with_capacity(4096),
			instances: StableVec::new(),
		}
	}
}

pub struct InstanceBatches<'a, I> {
	batches: Vec<InstanceBatch>,
	_marker: PhantomData<&'a I>,
}

impl<'a, I> InstanceBatches<'a, I> {
	pub fn iter(&self) -> InstanceBatchesIterator<'_, I> {
		InstanceBatchesIterator {
			batches: self.batches.iter(),
			_marker: PhantomData,
		}
	}
}

#[derive(Clone)]
pub struct InstanceBatchesIterator<'a, I> {
	batches: std::slice::Iter<'a, InstanceBatch>,
	_marker: PhantomData<I>,
}

impl<'a, I> InstanceBatchesIterator<'a, I> {
	pub fn into_vec(self) -> Vec<InstanceBatch> {
		self.batches.copied().collect()
	}
}

impl<'a, I: 'a> Iterator for InstanceBatchesIterator<'a, I> {
	type Item = BatchInstancesIterator<'a, I>;

	fn next(&mut self) -> Option<Self::Item> {
		self.batches.next().map(|b| BatchInstancesIterator {
			batch: *b,
			index: 0,
			_marker: PhantomData,
		})
	}
}

pub struct BatchInstancesIterator<'a, I> {
	batch: InstanceBatch,
	index: usize,
	_marker: PhantomData<&'a I>,
}

impl<'a, I> BatchInstancesIterator<'a, I> {
	pub fn index_count(&self) -> usize {
		self.batch.index_count()
	}

	pub fn instance_count(&self) -> usize {
		self.batch.instance_count()
	}

	pub fn base_vertex(&self) -> usize {
		self.batch.base_vertex()
	}

	pub fn base_index(&self) -> usize {
		self.batch.base_index()
	}

	pub fn base_instance(&self) -> usize {
		self.batch.base_instance()
	}
}

impl<'a, I> Iterator for BatchInstancesIterator<'a, I> {
	type Item = usize;

	fn next(&mut self) -> Option<Self::Item> {
		if self.index < self.batch.instance_count {
			let i = self.batch.base_instance + self.index;
			self.index += 1;
			Some(i)
		} else {
			None
		}
	}
}

#[cfg(test)]
mod tests {
	use crate::rendering::utils::{MeshBuffersStats, MeshStats};

	#[test]
	fn test_one_mesh_and_instance() {
		let mut mesh_buffer_stats = MeshBuffersStats::default();

		let mesh = mesh_buffer_stats.add_mesh(MeshStats::new(32, 96), 1);

		assert_eq!(mesh.vertex_offset(), 0);
		assert_eq!(mesh.index_offset(), 0);

		let mesh_instance = mesh_buffer_stats.add_instance(mesh.id(), ());

		let batches = mesh_buffer_stats.get_instance_batches();
		let mut batches = batches.iter();

		let batch = batches.next().unwrap();
		assert_eq!(batch.index_count(), 96);
		assert_eq!(batch.instance_count(), 1);
		assert_eq!(batch.base_vertex(), 0);
		assert_eq!(batch.base_index(), 0);
		assert_eq!(batch.base_instance(), 0);
	}

	#[test]
	fn test_one_mesh_and_two_instances() {
		let mut mesh_buffer_stats = MeshBuffersStats::default();

		let mesh = mesh_buffer_stats.add_mesh(MeshStats::new(32, 96), 1);

		assert_eq!(mesh.vertex_offset(), 0);
		assert_eq!(mesh.index_offset(), 0);

		let mesh_instance1 = mesh_buffer_stats.add_instance(mesh.id(), ());
		let mesh_instance2 = mesh_buffer_stats.add_instance(mesh.id(), ());

		let batches = mesh_buffer_stats.get_instance_batches();
		let mut batches = batches.iter();

		let batch = batches.next().unwrap();
		assert_eq!(batch.index_count(), 96);
		assert_eq!(batch.instance_count(), 2);
		assert_eq!(batch.base_vertex(), 0);
		assert_eq!(batch.base_index(), 0);
		assert_eq!(batch.base_instance(), 0);
	}

	#[test]
	fn test_two_meshes_and_two_instances() {
		let mut mesh_buffer_stats = MeshBuffersStats::default();

		let mesh1 = mesh_buffer_stats.add_mesh(MeshStats::new(32, 96), 1);
		let mesh2 = mesh_buffer_stats.add_mesh(MeshStats::new(64, 192), 2);

		assert_eq!(mesh1.vertex_offset(), 0);
		assert_eq!(mesh1.index_offset(), 0);
		assert_eq!(mesh2.vertex_offset(), 32);
		assert_eq!(mesh2.index_offset(), 96);

		let mesh1_instance1 = mesh_buffer_stats.add_instance(mesh1.id(), ());
		let mesh2_instance2 = mesh_buffer_stats.add_instance(mesh2.id(), ());

		let batches = mesh_buffer_stats.get_instance_batches();
		let mut batches = batches.iter().collect::<Vec<_>>();
		batches.sort_by_key(|e| e.batch.base_instance);
		let mut batches = batches.iter();

		let batch = batches.next().unwrap();
		assert_eq!(batch.index_count(), 96);
		assert_eq!(batch.instance_count(), 1);
		assert_eq!(batch.base_vertex(), 0);
		assert_eq!(batch.base_index(), 0);
		assert_eq!(batch.base_instance(), 0);

		let batch = batches.next().unwrap();
		assert_eq!(batch.index_count(), 192);
		assert_eq!(batch.instance_count(), 1);
		assert_eq!(batch.base_vertex(), 32);
		assert_eq!(batch.base_index(), 96);
		assert_eq!(batch.base_instance(), 1);
	}

	#[test]
	fn test_removed_instance_does_not_shift_or_batch_through_hole() {
		let mut mesh_buffer_stats = MeshBuffersStats::default();

		let mesh = mesh_buffer_stats.add_mesh(MeshStats::new(32, 96), 1);
		let first = mesh_buffer_stats.add_instance(mesh.id(), "first");
		let second = mesh_buffer_stats.add_instance(mesh.id(), "second");
		let third = mesh_buffer_stats.add_instance(mesh.id(), "third");

		assert_eq!(mesh_buffer_stats.remove_instance(second), Some("second"));
		assert_eq!(mesh_buffer_stats.get_instance_id("first"), Some(first));
		assert_eq!(mesh_buffer_stats.get_instance_id("third"), Some(third));

		let batches = mesh_buffer_stats.get_instance_batches();
		let batches = batches.iter().into_vec();

		assert_eq!(batches.len(), 2);
		assert_eq!(batches[0].base_instance(), first.index());
		assert_eq!(batches[0].instance_count(), 1);
		assert_eq!(batches[1].base_instance(), third.index());
		assert_eq!(batches[1].instance_count(), 1);
	}

	#[test]
	fn heap_and_frame_allocators_preserve_mesh_switch_and_hole_batches() {
		let mut mesh_buffer_stats = MeshBuffersStats::default();
		let first_mesh = mesh_buffer_stats.add_mesh(MeshStats::new(10, 30), 1);
		let second_mesh = mesh_buffer_stats.add_mesh(MeshStats::new(20, 60), 2);

		mesh_buffer_stats.add_instance(first_mesh.id(), "first");
		let removed = mesh_buffer_stats.add_instance(first_mesh.id(), "removed");
		mesh_buffer_stats.add_instance(second_mesh.id(), "second-a");
		mesh_buffer_stats.add_instance(second_mesh.id(), "second-b");
		mesh_buffer_stats.add_instance(first_mesh.id(), "last");
		assert_eq!(mesh_buffer_stats.remove_instance(removed), Some("removed"));

		let heap_batches = mesh_buffer_stats.get_instance_batches();
		let frame_allocator = bumpalo::Bump::new();
		let frame_batches = mesh_buffer_stats.get_instance_batches_in(&frame_allocator);

		assert_eq!(heap_batches.batches.as_slice(), frame_batches.as_slice());
		assert_eq!(frame_batches.len(), 3);
		assert_eq!((frame_batches[0].index_count(), frame_batches[0].base_instance()), (30, 0));
		assert_eq!((frame_batches[1].index_count(), frame_batches[1].instance_count()), (60, 2));
		assert_eq!((frame_batches[2].index_count(), frame_batches[2].base_instance()), (30, 4));
	}
}