Skip to content

Commit

Permalink
Impl instance
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 13, 2024
1 parent 7189aa4 commit 7e7d08c
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 73 deletions.
2 changes: 1 addition & 1 deletion dunge/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub struct Binder<'a> {
}

impl<'a> Binder<'a> {
pub(crate) fn new<V>(state: &'a State, shader: &'a Shader<V>) -> Self {
pub(crate) fn new<V, I>(state: &'a State, shader: &'a Shader<V, I>) -> Self {
let layout = shader.groups();
Self {
shader_id: shader.id(),
Expand Down
10 changes: 5 additions & 5 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ impl Context {
&self.0
}

pub fn make_shader<M, A>(&self, module: M) -> Shader<M::Vertex>
pub fn make_shader<M, A>(&self, module: M) -> Shader<M::Vertex, M::Instance>
where
M: IntoModule<A>,
{
Shader::new(&self.0, module)
}

pub fn make_binder<'a, V>(&'a self, shader: &'a Shader<V>) -> Binder<'a> {
pub fn make_binder<'a, V, I>(&'a self, shader: &'a Shader<V, I>) -> Binder<'a> {
Binder::new(&self.0, shader)
}

pub fn make_uniform<V>(&self, val: V) -> Uniform<V>
pub fn make_uniform<U>(&self, val: U) -> Uniform<U>
where
V: Value,
U: Value,
{
Uniform::new(&self.0, val.value().as_ref())
}

pub fn make_layer<V>(&self, format: Format, shader: &Shader<V>) -> Layer<V> {
pub fn make_layer<V, I>(&self, format: Format, shader: &Shader<V, I>) -> Layer<V, I> {
Layer::new(&self.0, format, shader)
}

Expand Down
10 changes: 5 additions & 5 deletions dunge/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ impl BoundLayer<'_, '_, ()> {
}
}

pub struct Layer<V> {
pub struct Layer<V, I> {
shader_id: usize,
no_bindings: bool,
format: Format,
render: RenderPipeline,
vert: PhantomData<V>,
ty: PhantomData<(V, I)>,
}

impl<V> Layer<V> {
pub(crate) fn new(state: &State, format: Format, shader: &Shader<V>) -> Self {
impl<V, I> Layer<V, I> {
pub(crate) fn new(state: &State, format: Format, shader: &Shader<V, I>) -> Self {
use wgpu::*;

let targets = [Some(ColorTargetState {
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<V> Layer<V> {
no_bindings: shader.groups().is_empty(),
format,
render,
vert: PhantomData,
ty: PhantomData,
}
}

Expand Down
39 changes: 26 additions & 13 deletions dunge/src/shader.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
use {
crate::{
bind::TypedGroup,
sl::{InstInfo, IntoModule, Module, Stages, VertInfo},
sl::{InputInfo, InstInfo, IntoModule, Module, Stages, VertInfo},
state::State,
types::{MemberType, VectorType},
},
std::marker::PhantomData,
std::{cell::Cell, marker::PhantomData},
wgpu::{BufferAddress, PipelineLayout, ShaderModule, VertexAttribute, VertexBufferLayout},
};

pub struct Shader<V> {
pub struct Shader<V, I> {
inner: Inner,
vert: PhantomData<V>,
ty: PhantomData<(V, I)>,
}

impl<V> Shader<V> {
impl<V, I> Shader<V, I> {
pub(crate) fn new<M, A>(state: &State, module: M) -> Self
where
M: IntoModule<A, Vertex = V>,
{
Self {
inner: Inner::new(state, module.into_module()),
vert: PhantomData,
ty: PhantomData,
}
}

Expand Down Expand Up @@ -164,20 +164,26 @@ impl Inner {
VectorType::Vec4i => VertexFormat::Sint32x4,
};

let mut vertex = Vec::with_capacity(cx.count_verts());
let next_location = {
let location = Cell::default();
move || {
let current = location.get();
location.set(current + 1);
current
}
};

let vert = |info: VertInfo| {
let mut offset = 0;
let mut location = info.start_location;
let attr = |vecty| {
let format = to_format(vecty);
let attr = VertexAttribute {
format,
offset,
shader_location: location,
shader_location: next_location(),
};

offset += format.size();
location += 1;
attr
};

Expand All @@ -187,13 +193,12 @@ impl Inner {
}
};

vertex.extend(cx.verts().map(vert));
let inst = |info: InstInfo| {
let format = to_format(info.vecty);
let attr = VertexAttribute {
format,
offset: 0,
shader_location: info.location,
shader_location: next_location(),
};

Vertex {
Expand All @@ -202,7 +207,15 @@ impl Inner {
}
};

vertex.extend(cx.insts().map(inst));
let mut vertex = Vec::with_capacity(cx.count_input());
for input in cx.input() {
match input {
InputInfo::Vert(v) => vertex.push(vert(v)),
InputInfo::Inst(i) => vertex.push(inst(i)),
InputInfo::Index => {}
}
}

Self {
id: state.next_shader_id(),
module,
Expand Down
2 changes: 1 addition & 1 deletion dunge/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Frame<'_, '_> {
self.encoders.make(self.device, view)
}

pub fn layer<'p, V, O>(&'p mut self, layer: &'p Layer<V>, opts: O) -> SetLayer<'p, V>
pub fn layer<'p, V, I, O>(&'p mut self, layer: &'p Layer<V, I>, opts: O) -> SetLayer<'p, V>
where
O: Into<Options>,
{
Expand Down
12 changes: 6 additions & 6 deletions dunge/src/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use {
wgpu::Buffer,
};

pub struct Uniform<V> {
pub struct Uniform<U> {
buf: Buffer,
vert: PhantomData<V>,
ty: PhantomData<U>,
}

impl<V> Uniform<V> {
impl<U> Uniform<U> {
pub(crate) fn new(state: &State, contents: &[u8]) -> Self {
use wgpu::{
util::{BufferInitDescriptor, DeviceExt},
Expand All @@ -32,13 +32,13 @@ impl<V> Uniform<V> {

Self {
buf,
vert: PhantomData,
ty: PhantomData,
}
}

pub fn update(&self, cx: &Context, val: V)
pub fn update(&self, cx: &Context, val: U)
where
V: Value,
U: Value,
{
let queue = cx.state().queue();
let data = val.value();
Expand Down
52 changes: 10 additions & 42 deletions dunge_shader/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,25 @@ impl Stages {
}
}

pub(crate) enum InputInfo {
#[doc(hidden)]
#[derive(Clone, Copy)]
pub enum InputInfo {
Vert(VertInfo),
Inst(InstInfo),
Index,
}

impl InputInfo {
fn as_vert(&self) -> Option<VertInfo> {
match self {
Self::Vert(info) => Some(*info),
_ => None,
}
}

fn as_inst(&self) -> Option<InstInfo> {
match self {
Self::Inst(info) => Some(*info),
_ => None,
}
}
}

#[doc(hidden)]
#[derive(Clone, Copy)]
pub struct VertInfo {
pub def: Define<VectorType>,
pub size: usize,
pub start_location: u32,
}

#[doc(hidden)]
#[derive(Clone, Copy)]
pub struct InstInfo {
pub vecty: VectorType,
pub location: u32,
}

pub(crate) struct GroupEntry {
Expand Down Expand Up @@ -97,7 +83,6 @@ fn countdown(v: &mut u8, msg: &str) {
pub struct Context {
pub(crate) inputs: Vec<InputInfo>,
pub(crate) groups: Vec<GroupEntry>,
location: u32,
limits: Limits,
}

Expand All @@ -106,7 +91,6 @@ impl Context {
Self {
inputs: vec![],
groups: vec![],
location: 0,
limits: Limits {
index: 1,
verts: 1,
Expand All @@ -126,26 +110,15 @@ impl Context {
fn add_vertex(&mut self, def: Define<VectorType>, size: usize) -> u32 {
countdown(&mut self.limits.verts, "too many vertices in the shader");
let id = self.inputs.len() as u32;
let info = VertInfo {
def,
size,
start_location: self.location,
};

self.location += def.len() as u32;
let info = VertInfo { def, size };
self.inputs.push(InputInfo::Vert(info));
id
}

fn add_instance(&mut self, vec: VectorType) -> u32 {
countdown(&mut self.limits.insts, "too many instances in the shader");
let id = self.inputs.len() as u32;
let info = InstInfo {
vecty: vec,
location: self.location,
};

self.location += 1;
let info = InstInfo { vecty: vec };
self.inputs.push(InputInfo::Inst(info));
id
}
Expand All @@ -165,21 +138,16 @@ impl Context {
}

#[doc(hidden)]
pub fn count_verts(&self) -> usize {
pub fn count_input(&self) -> usize {
self.inputs
.iter()
.filter(|info| matches!(info, InputInfo::Vert(_) | InputInfo::Inst(_)))
.count()
}

#[doc(hidden)]
pub fn verts(&self) -> impl Iterator<Item = VertInfo> + '_ {
self.inputs.iter().filter_map(InputInfo::as_vert)
}

#[doc(hidden)]
pub fn insts(&self) -> impl Iterator<Item = InstInfo> + '_ {
self.inputs.iter().filter_map(InputInfo::as_inst)
pub fn input(&self) -> impl Iterator<Item = InputInfo> + '_ {
self.inputs.iter().copied()
}

#[doc(hidden)]
Expand Down
3 changes: 3 additions & 0 deletions dunge_shader/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{

pub trait IntoModule<A> {
type Vertex;
type Instance;
fn into_module(self) -> Module;
}

Expand All @@ -15,6 +16,7 @@ where
O: Output,
{
type Vertex = ();
type Instance = ();

fn into_module(self) -> Module {
let cx = Context::new();
Expand All @@ -34,6 +36,7 @@ macro_rules! impl_into_module {
)*
{
type Vertex = A::Vertex;
type Instance = A::Instance;

#[allow(non_snake_case)]
fn into_module(self) -> Module {
Expand Down

0 comments on commit 7e7d08c

Please sign in to comment.