REACTIVITY
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
use std::{
|
||||
marker::PhantomData,
|
||||
ops::{Deref, DerefMut, Range},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
layout::{Color, TextureHandle, UiRegion},
|
||||
render::{ArrBuf, data::PrimitiveInstance},
|
||||
@@ -7,33 +12,37 @@ use wgpu::*;
|
||||
|
||||
pub struct Primitives {
|
||||
pub(super) instances: Vec<PrimitiveInstance>,
|
||||
pub(super) run: Vec<PrimitiveInstance>,
|
||||
pub(super) data: PrimitiveData,
|
||||
// ensure drawn textures don't get freed
|
||||
pub(crate) drawn_textures: Vec<TextureHandle>,
|
||||
pub updated: bool,
|
||||
run_start: usize,
|
||||
}
|
||||
|
||||
impl Default for Primitives {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
instances: Default::default(),
|
||||
run: Default::default(),
|
||||
data: Default::default(),
|
||||
drawn_textures: Default::default(),
|
||||
updated: true,
|
||||
run_start: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Primitive: Pod {
|
||||
const BINDING: u32;
|
||||
fn vec(data: &mut PrimitiveData) -> &mut Vec<Self>;
|
||||
fn vec(data: &mut PrimitiveData) -> &mut PrimitiveVec<Self>;
|
||||
}
|
||||
|
||||
macro_rules! primitives {
|
||||
($($name:ident: $ty:ty => $binding:expr,)*) => {
|
||||
#[derive(Default)]
|
||||
pub struct PrimitiveData {
|
||||
$($name: Vec<$ty>,)*
|
||||
$($name: PrimitiveVec<$ty>,)*
|
||||
}
|
||||
|
||||
pub struct PrimitiveBuffers {
|
||||
@@ -68,6 +77,12 @@ macro_rules! primitives {
|
||||
pub fn clear(&mut self) {
|
||||
$(self.$name.clear();)*
|
||||
}
|
||||
pub fn free(&mut self, binding: u32, idx: usize) {
|
||||
match binding {
|
||||
$(<$ty>::BINDING => self.$name.free(idx),)*
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$(
|
||||
@@ -75,7 +90,7 @@ macro_rules! primitives {
|
||||
unsafe impl bytemuck::Zeroable for $ty {}
|
||||
impl Primitive for $ty {
|
||||
const BINDING: u32 = $binding;
|
||||
fn vec(data: &mut PrimitiveData) -> &mut Vec<Self> {
|
||||
fn vec(data: &mut PrimitiveData) -> &mut PrimitiveVec<Self> {
|
||||
&mut data.$name
|
||||
}
|
||||
}
|
||||
@@ -86,23 +101,72 @@ macro_rules! primitives {
|
||||
}
|
||||
|
||||
impl Primitives {
|
||||
pub fn write<P: Primitive>(&mut self, data: P, region: UiRegion) {
|
||||
pub fn write<P: Primitive>(&mut self, data: P, region: UiRegion) -> PrimitiveHandle<P> {
|
||||
let vec = P::vec(&mut self.data);
|
||||
let i = vec.len() as u32;
|
||||
self.instances.push(PrimitiveInstance {
|
||||
let i = vec.add(data);
|
||||
self.run.push(PrimitiveInstance {
|
||||
region,
|
||||
idx: i,
|
||||
idx: i as u32,
|
||||
binding: P::BINDING,
|
||||
});
|
||||
vec.push(data);
|
||||
PrimitiveHandle::new(i)
|
||||
}
|
||||
|
||||
pub(crate) fn clear(&mut self) {
|
||||
self.updated = true;
|
||||
self.instances.clear();
|
||||
self.run.clear();
|
||||
self.data.clear();
|
||||
self.drawn_textures.clear();
|
||||
}
|
||||
|
||||
pub fn set<P: Primitive>(&mut self, handle: &PrimitiveHandle<P>, data: P) {
|
||||
P::vec(&mut self.data)[handle.idx] = data;
|
||||
}
|
||||
|
||||
pub fn prepare(&mut self, span: Range<usize>) {
|
||||
self.run_start = span.start;
|
||||
for instance in &self.instances[span] {
|
||||
self.data.free(instance.binding, instance.idx as usize);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply(&mut self, span: Range<usize>) -> isize {
|
||||
let delta = self.run.len() as isize - span.len() as isize;
|
||||
self.instances.splice(span, self.run.drain(..));
|
||||
delta
|
||||
}
|
||||
|
||||
pub fn replace(&mut self) {
|
||||
std::mem::swap(&mut self.run, &mut self.instances);
|
||||
self.run.clear();
|
||||
}
|
||||
|
||||
pub fn cur_pos(&self) -> usize {
|
||||
self.run_start + self.run.len()
|
||||
}
|
||||
}
|
||||
|
||||
/// primitives but only exposes set for redrawing
|
||||
pub struct PrimitiveEditor<'a>(&'a mut Primitives);
|
||||
impl PrimitiveEditor<'_> {
|
||||
pub fn set<P: Primitive>(&mut self, handle: &PrimitiveHandle<P>, data: P) {
|
||||
self.0.set(handle, data);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PrimitiveHandle<P: Primitive> {
|
||||
idx: usize,
|
||||
_pd: PhantomData<P>,
|
||||
}
|
||||
|
||||
impl<P: Primitive> PrimitiveHandle<P> {
|
||||
fn new(idx: usize) -> Self {
|
||||
Self {
|
||||
idx,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
primitives!(
|
||||
@@ -125,3 +189,54 @@ pub struct TexturePrimitive {
|
||||
pub view_idx: u32,
|
||||
pub sampler_idx: u32,
|
||||
}
|
||||
|
||||
pub struct PrimitiveVec<T> {
|
||||
vec: Vec<T>,
|
||||
free: Vec<usize>,
|
||||
}
|
||||
|
||||
impl<T> PrimitiveVec<T> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
vec: Vec::new(),
|
||||
free: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn add(&mut self, t: T) -> usize {
|
||||
if let Some(i) = self.free.pop() {
|
||||
self.vec[i] = t;
|
||||
i
|
||||
} else {
|
||||
let i = self.vec.len();
|
||||
self.vec.push(t);
|
||||
i
|
||||
}
|
||||
}
|
||||
pub fn free(&mut self, i: usize) {
|
||||
self.free.push(i);
|
||||
}
|
||||
pub fn clear(&mut self) {
|
||||
self.free.clear();
|
||||
self.vec.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for PrimitiveVec<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for PrimitiveVec<T> {
|
||||
type Target = Vec<T>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.vec
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for PrimitiveVec<T> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.vec
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user