summaryrefslogtreecommitdiff
path: root/src/instance.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/instance.rs')
-rw-r--r--src/instance.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/instance.rs b/src/instance.rs
index 2bff797..2d1808f 100644
--- a/src/instance.rs
+++ b/src/instance.rs
@@ -2,9 +2,11 @@ use std::mem::size_of;
use bytemuck::{Pod, Zeroable};
+/// The ID for an instance
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct InstanceId(usize);
+/// A sprite, that can be used by the alligator shader
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)]
pub struct Instance {
@@ -59,6 +61,7 @@ impl Instance {
}
}
+/// A buffer of sprites, for both CPU and GPU memory
pub struct InstanceBuffer {
instances: Vec<Instance>,
instance_buffer: wgpu::Buffer,
@@ -75,6 +78,7 @@ fn create_buffer(device: &wgpu::Device, instances: &Vec<Instance>) -> wgpu::Buff
}
impl InstanceBuffer {
+ /// Create a new buffer with the given capacity
pub(crate) fn new(device: &wgpu::Device, capacity: usize) -> Self {
let instances = Vec::with_capacity(capacity);
let instance_buffer_size = instances.capacity();
@@ -87,6 +91,7 @@ impl InstanceBuffer {
}
}
+ /// The number of sprites
pub fn len(&self) -> u32 {
self.instances
.len()
@@ -94,41 +99,51 @@ impl InstanceBuffer {
.expect("expected less than 3 billion instances")
}
+ /// Returns `true` if there are no sprites
pub fn is_empty(&self) -> bool {
self.instances.is_empty()
}
+ /// The capacity of the buffer
pub const fn buffer_size(&self) -> usize {
self.instance_buffer_size
}
+ /// Get a slice containing the entire buffer
pub(crate) fn buffer_slice(&self) -> wgpu::BufferSlice {
self.instance_buffer.slice(..)
}
+ /// Add a new sprite. The new sprite's `InstanceId` is returned. This ID
+ /// becomes invalid if the buffer is cleared.
pub fn push_instance(&mut self, instance: Instance) -> InstanceId {
let index = self.instances.len();
self.instances.push(instance);
InstanceId(index)
}
+ /// Get a specific instance
pub fn get_instance(&self, id: InstanceId) -> Option<&Instance> {
self.instances.get(id.0)
}
+ /// Get a mutable reference to a specific sprite
pub fn get_instance_mut(&mut self, id: InstanceId) -> Option<&mut Instance> {
self.instances.get_mut(id.0)
}
+ /// Clear the instance buffer. This invalidates all `InstanceId`'s
pub fn clear(&mut self) {
self.instances.clear();
}
+ /// Increase the capacity of the buffer
fn expand_buffer(&mut self, device: &wgpu::Device) {
self.instance_buffer_size = self.instances.capacity();
self.instance_buffer = create_buffer(device, &self.instances);
}
+ /// Fill the GPU buffer with the sprites in the CPU buffer.
#[profiling::function]
pub(crate) fn fill_buffer(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
if self.instances.len() > self.instance_buffer_size {