From 6ef351de4ba506e7f0f285569aa0e22255bb68c6 Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:23 +0100 Subject: refactor(render): slight leveldrawpass refactors --- stockton-render/src/draw/draw_passes/util.rs | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 stockton-render/src/draw/draw_passes/util.rs (limited to 'stockton-render/src/draw/draw_passes/util.rs') diff --git a/stockton-render/src/draw/draw_passes/util.rs b/stockton-render/src/draw/draw_passes/util.rs new file mode 100644 index 0000000..a42f870 --- /dev/null +++ b/stockton-render/src/draw/draw_passes/util.rs @@ -0,0 +1,41 @@ +//! Utility structs & functions + +use anyhow::Result; + +/// Keeps a given resource for each swapchain image +pub struct TargetSpecificResources { + elements: Vec, + next_idx: usize, +} + +impl TargetSpecificResources { + /// Create a new set of resources, given a function to generate them and the count + /// In most cases, count should be swapchain_properties.image_count + pub fn new(generator: F, count: usize) -> Result + where + F: FnMut() -> Result, + { + let mut elements = Vec::with_capacity(count); + for _ in 0..count { + elements.push(generator()?); + } + + Ok(TargetSpecificResources { + elements, + next_idx: 0, + }) + } + + /// Get the next resource, wrapping around if necessary. + pub fn get_next<'a>(&'a mut self) -> &'a T { + let el = &self.elements[self.next_idx]; + self.next_idx = (self.next_idx + 1) % self.elements.len(); + el + } + + /// Dissolve the resource set, returning an iterator over each item. + /// In most cases, each item will need deactivated. + pub fn dissolve(self) -> impl Iterator { + self.elements.into_iter() + } +} -- cgit v1.2.3