summaryrefslogtreecommitdiff
path: root/src/renderer.rs
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2022-09-18 18:16:19 -0400
committerMicha White <botahamec@outlook.com>2022-09-18 18:16:19 -0400
commitcd7d74507fa54d026547b7dda9e1498a81a395b5 (patch)
tree7d5435eec9c84dead9cdc36a4ba87dcfe520f5c3 /src/renderer.rs
parent83295e01008bdf25e03f6b5aa18b93b735a5e326 (diff)
An actual instancing API
Diffstat (limited to 'src/renderer.rs')
-rw-r--r--src/renderer.rs22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/renderer.rs b/src/renderer.rs
index 75e6d1e..81e1a3f 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -1,6 +1,6 @@
use std::convert::TryInto;
-use crate::{vertex::SQUARE, Instance, RenderWindowConfig, Vertex};
+use crate::{instance::InstanceId, vertex::SQUARE, Instance, RenderWindowConfig, Vertex};
use pollster::FutureExt;
use thiserror::Error;
use wgpu::{include_wgsl, util::DeviceExt};
@@ -172,7 +172,7 @@ impl Renderer {
usage: wgpu::BufferUsages::VERTEX,
});
- let instances = Vec::new();
+ let instances = Vec::with_capacity(config.instance_capacity);
Ok(Self {
surface,
@@ -199,6 +199,24 @@ impl Renderer {
self.surface.configure(&self.device, &self.surface_config);
}
+ /// Add an instance to the renderer, and returns an `InstanceId` to the
+ /// instance. This id becomes invalid if the instances are cleared.
+ pub fn push_instance(&mut self, instance: Instance) -> InstanceId {
+ let index = self.instances.len();
+ self.instances.push(instance);
+ InstanceId(index)
+ }
+
+ /// Get a mutable reference to an instance
+ pub fn instance_mut(&mut self, id: InstanceId) -> Option<&mut Instance> {
+ self.instances.get_mut(id.0)
+ }
+
+ /// Clears the list of instances, making all instance ID's invalid
+ pub fn clear_instances(&mut self) {
+ self.instances.clear();
+ }
+
/// Renders a new frame to the window
///
/// # Errors