From cd7d74507fa54d026547b7dda9e1498a81a395b5 Mon Sep 17 00:00:00 2001 From: Micha White Date: Sun, 18 Sep 2022 18:16:19 -0400 Subject: An actual instancing API --- src/renderer.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/renderer.rs') 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 -- cgit v1.2.3