Skip to content

Commit

Permalink
Use an index for buffer side
Browse files Browse the repository at this point in the history
  • Loading branch information
nicopap committed Oct 9, 2023
1 parent e6a83eb commit 52892b6
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions crates/bevy_pbr/src/render/double_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@ use bevy_render::{
use bytemuck::Pod;

#[derive(Clone, Copy, PartialEq, Eq)]
enum Side {
A,
B,
}
struct Side(usize);
impl Side {
fn flip(self) -> Self {
match self {
Side::A => Side::B,
Side::B => Side::A,
}
Side((self.0 + 1) % 2)
}
fn toggle(&mut self) {
*self = self.flip();
}
}
pub struct DoubleBufferVec<T: Pod> {
a: BufferVec<T>,
b: BufferVec<T>,
buffers: [BufferVec<T>; 2],
current: Side,
}
impl<T: Pod> Extend<T> for DoubleBufferVec<T> {
Expand All @@ -34,22 +27,15 @@ impl<T: Pod> Extend<T> for DoubleBufferVec<T> {
impl<T: Pod> DoubleBufferVec<T> {
pub const fn new(buffer_usage: BufferUsages) -> Self {
DoubleBufferVec {
a: BufferVec::new(buffer_usage),
b: BufferVec::new(buffer_usage),
current: Side::A,
buffers: [BufferVec::new(buffer_usage), BufferVec::new(buffer_usage)],
current: Side(0),
}
}
const fn buffer_side(&self, side: Side) -> &BufferVec<T> {
match side {
Side::A => &self.a,
Side::B => &self.b,
}
&self.buffers[side.0]
}
pub(crate) fn current_buffer_mut(&mut self) -> &mut BufferVec<T> {
match self.current {
Side::A => &mut self.a,
Side::B => &mut self.b,
}
&mut self.buffers[self.current.0]
}
pub fn current(&self) -> Option<&Buffer> {
self.buffer_side(self.current).buffer()
Expand Down

0 comments on commit 52892b6

Please sign in to comment.