Skip to content

Commit

Permalink
Update instance
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 15, 2024
1 parent 5bf0d2d commit 9cc480d
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 191 deletions.
10 changes: 5 additions & 5 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ use {
bind::{self, Binder, GroupHandler, UniqueBinding, Update, Visit},
draw::Draw,
format::Format,
instance::Row,
layer::Layer,
mesh::{self, Mesh},
shader::Shader,
sl::IntoModule,
state::{Render, State},
table::{self, Table},
texture::{
self, CopyBuffer, CopyBufferView, DrawTexture, Filter, Make, MapResult, Mapped, Sampler,
},
uniform::{Uniform, Value},
Instance, Vertex,
Vertex,
},
std::{error, fmt, future::IntoFuture, sync::Arc},
};
Expand Down Expand Up @@ -59,11 +59,11 @@ impl Context {
Mesh::new(&self.0, data)
}

pub fn make_table<I>(&self, data: table::Data<I>) -> Table<I>
pub fn make_row<U>(&self, data: &[U]) -> Row<U>
where
I: Instance,
U: Value,
{
Table::new(&self.0, data)
Row::new(&self.0, data)
}

pub fn make_texture<M>(&self, data: M) -> M::Out
Expand Down
94 changes: 94 additions & 0 deletions dunge/src/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use {
crate::{
state::State,
uniform::{self, Value},
Instance,
},
std::marker::PhantomData,
wgpu::{Buffer, RenderPass},
};

pub use dunge_shader::instance::Projection;

pub trait Set: Instance {
fn set<'p>(&'p self, setter: &mut Setter<'_, 'p>);
}

pub struct Setter<'s, 'p> {
len: Option<u32>,
slot: u32,
pass: &'s mut RenderPass<'p>,
}

impl<'s, 'p> Setter<'s, 'p> {
pub(crate) fn new(slot: u32, pass: &'s mut RenderPass<'p>) -> Self {
Self {
len: None,
slot,
pass,
}
}

pub(crate) fn len(&self) -> u32 {
self.len.unwrap_or_default()
}

fn next_slot(&mut self) -> u32 {
let next = self.slot;
self.slot += 1;
next
}

fn update_len(&mut self, len: u32) {
let current = self.len.get_or_insert(len);
*current = u32::min(*current, len);
}
}

pub trait SetMember<'p> {
fn set_member(&'p self, setter: &mut Setter<'_, 'p>);
}

impl<'p, U> SetMember<'p> for Row<U> {
fn set_member(&'p self, setter: &mut Setter<'_, 'p>) {
setter.update_len(self.len);
let slot = setter.next_slot();
let slice = self.buf.slice(..);
setter.pass.set_vertex_buffer(slot, slice);
}
}

pub struct Row<U> {
buf: Buffer,
len: u32,
ty: PhantomData<U>,
}

impl<U> Row<U> {
pub(crate) fn new(state: &State, data: &[U]) -> Self
where
U: Value,
{
use wgpu::{
util::{BufferInitDescriptor, DeviceExt},
BufferUsages,
};

let buf = {
let desc = BufferInitDescriptor {
label: None,
contents: uniform::values_as_bytes(data),
usage: BufferUsages::VERTEX,
};

state.device().create_buffer_init(&desc)
};

let len = data.len() as u32;
Self {
buf,
len,
ty: PhantomData,
}
}
}
19 changes: 11 additions & 8 deletions dunge/src/layer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::table::Table;

use {
crate::{
bind::Binding,
format::Format,
instance::{Set, Setter},
mesh::Mesh,
shader::{Shader, Slots},
state::State,
Expand Down Expand Up @@ -59,10 +58,14 @@ impl<'s, 'p, V, I> SetBinding<'s, 'p, V, I> {
}
}

pub fn instance(&'s mut self, table: &'p Table<I>) -> SetInstance<'s, 'p, V> {
table.set(self.pass, self.slots.instance);
pub fn instance(&'s mut self, instance: &'p I) -> SetInstance<'s, 'p, V>
where
I: Set,
{
let mut setter = Setter::new(self.slots.instance, self.pass);
instance.set(&mut setter);
SetInstance {
count: table.count(),
len: setter.len(),
slots: self.slots,
pass: self.pass,
ty: PhantomData,
Expand All @@ -83,21 +86,21 @@ impl SetBinding<'_, '_, (), ()> {
}

pub struct SetInstance<'s, 'p, V> {
count: u32,
len: u32,
slots: Slots,
pass: &'s mut RenderPass<'p>,
ty: PhantomData<V>,
}

impl<'p, V> SetInstance<'_, 'p, V> {
pub fn draw(&mut self, mesh: &'p Mesh<V>) {
mesh.draw(self.pass, self.slots.vertex, self.count);
mesh.draw(self.pass, self.slots.vertex, self.len);
}
}

impl SetInstance<'_, '_, ()> {
pub fn draw_triangles(&mut self, count: u32) {
self.pass.draw(0..count * 3, 0..self.count);
self.pass.draw(0..count * 3, 0..self.len);
}
}

Expand Down
6 changes: 1 addition & 5 deletions dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ pub mod draw;
pub mod format;
pub mod group;
mod init;
pub mod instance;
pub mod layer;
pub mod mesh;
pub mod shader;
pub mod state;
pub mod table;
pub mod texture;
pub mod uniform;
pub mod vertex;
Expand All @@ -30,9 +30,5 @@ pub use {
glam,
};

pub mod instance {
pub use dunge_shader::instance::Projection;
}

#[cfg(feature = "winit")]
pub use {crate::init::window, el::Control};
117 changes: 0 additions & 117 deletions dunge/src/table.rs

This file was deleted.

Loading

0 comments on commit 9cc480d

Please sign in to comment.