Skip to content

Commit

Permalink
Matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 11, 2024
1 parent 430ceb3 commit e040a09
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 220 deletions.
22 changes: 12 additions & 10 deletions dunge/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,18 @@ impl Inner {
entries.clear();
for (binding, member) in iter::zip(0.., info.decl) {
let entry = match member {
MemberType::Scalar(_) | MemberType::Vector(_) => BindGroupLayoutEntry {
binding,
visibility: visibility(info.stages),
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
MemberType::Scalar(_) | MemberType::Vector(_) | MemberType::Matrix(_) => {
BindGroupLayoutEntry {
binding,
visibility: visibility(info.stages),
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}
}
MemberType::Tx2df => BindGroupLayoutEntry {
binding,
visibility: visibility(info.stages),
Expand Down
23 changes: 1 addition & 22 deletions dunge_shader/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
group::DeclareGroup,
module::{Module, Out, Output},
ret::Ret,
types::{self, IntoVector, ScalarType, Vector, VectorType},
types::{self, ScalarType, VectorType},
},
naga::{
AddressSpace, Arena, BinaryOperator, Binding, Block, BuiltIn, EntryPoint, Expression,
Expand Down Expand Up @@ -150,27 +150,6 @@ where
}
}

impl<V, E> Eval<E> for V
where
V: IntoVector,
<V::Vector as Vector>::Scalar: Eval<E>,
E: GetEntry,
{
type Out = V::Vector;

fn eval(self, en: &mut E) -> Expr {
let mut components = Vec::with_capacity(V::Vector::TYPE.dims());
self.into_vector(|scalar| {
let v = scalar.eval(en).get();
components.push(v);
});

let en = en.get_entry();
let ty = en.new_type(V::Vector::TYPE.ty());
en.compose(ty, Exprs(components))
}
}

#[derive(Clone, Copy)]
pub struct ReadIndex {
id: u32,
Expand Down
1 change: 1 addition & 0 deletions dunge_shader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod convert;
mod eval;
pub mod group;
mod math;
mod matrix;
mod module;
mod ret;
mod texture;
Expand Down
72 changes: 72 additions & 0 deletions dunge_shader/src/matrix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::{
eval::{Eval, Expr, Exprs, GetEntry},
types::{self, Matrix},
};

macro_rules! impl_eval_mat {
($g:ty => $t:ty) => {
impl<E> Eval<E> for $g
where
E: GetEntry,
{
type Out = $t;

fn eval(self, en: &mut E) -> Expr {
let mut components = Vec::with_capacity(<$t>::TYPE.dims());
self.into_matrix(|vector| {
let v = vector.eval(en).get();
components.push(v);
});

let en = en.get_entry();
let ty = en.new_type(<$t>::TYPE.ty());
en.compose(ty, Exprs(components))
}
}
};
}

impl_eval_mat!(glam::Mat2 => types::Mat2);
impl_eval_mat!(glam::Mat3 => types::Mat3);
impl_eval_mat!(glam::Mat4 => types::Mat4);

trait IntoMatrix {
type Vector;

fn into_matrix<F>(self, f: F)
where
F: FnMut(Self::Vector);
}

impl IntoMatrix for glam::Mat2 {
type Vector = glam::Vec2;

fn into_matrix<F>(self, mut f: F)
where
F: FnMut(Self::Vector),
{
self.to_cols_array_2d().map(|v| f(Self::Vector::from(v)));
}
}

impl IntoMatrix for glam::Mat3 {
type Vector = glam::Vec3;

fn into_matrix<F>(self, mut f: F)
where
F: FnMut(Self::Vector),
{
self.to_cols_array_2d().map(|v| f(Self::Vector::from(v)));
}
}

impl IntoMatrix for glam::Mat4 {
type Vector = glam::Vec4;

fn into_matrix<F>(self, mut f: F)
where
F: FnMut(Self::Vector),
{
self.to_cols_array_2d().map(|v| f(Self::Vector::from(v)));
}
}
89 changes: 29 additions & 60 deletions dunge_shader/src/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ macro_rules! impl_op {
}

impl<A> ops::$o<$a> for Ret<A, $b> {
type Output = Ret<Binary<Ret<A, $a>, $b>, $r>;
type Output = Ret<Binary<Ret<A, $b>, $a>, $r>;

fn $f(self, b: $a) -> Self::Output {
Ret::new(Binary {
Expand Down Expand Up @@ -110,62 +110,31 @@ impl_op!(Mul::mul(u32, u32) -> u32);
impl_op!(Div::div(u32, u32) -> u32);
impl_op!(Rem::rem(u32, u32) -> u32);

impl<A, O> ops::Mul<f32> for Ret<A, O>
where
O: types::Vector<Scalar = f32>,
{
type Output = Ret<Binary<Self, f32>, O>;

fn mul(self, b: f32) -> Self::Output {
Ret::new(Binary {
a: self,
b,
op: Op::Mul,
})
}
}

impl<A, O> ops::Mul<Ret<A, O>> for f32
where
O: types::Vector<Scalar = Self>,
{
type Output = Ret<Binary<Self, Ret<A, O>>, O>;

fn mul(self, b: Ret<A, O>) -> Self::Output {
Ret::new(Binary {
a: self,
b,
op: Op::Mul,
})
}
}

impl<A, B, O> ops::Add<Ret<B, O>> for Ret<A, O>
where
O: types::Vector,
{
type Output = Ret<Binary<Self, Ret<B, O>>, O>;

fn add(self, b: Ret<B, O>) -> Self::Output {
Ret::new(Binary {
a: self,
b,
op: Op::Add,
})
}
}

impl<A, B, O> ops::Sub<Ret<B, O>> for Ret<A, O>
where
O: types::Vector,
{
type Output = Ret<Binary<Self, Ret<B, O>>, O>;

fn sub(self, b: Ret<B, O>) -> Self::Output {
Ret::new(Binary {
a: self,
b,
op: Op::Sub,
})
}
}
impl_op!(Add::add(types::Vec2<f32>, types::Vec2<f32>) -> types::Vec2<f32>);
impl_op!(Add::add(types::Vec3<f32>, types::Vec3<f32>) -> types::Vec3<f32>);
impl_op!(Add::add(types::Vec4<f32>, types::Vec4<f32>) -> types::Vec4<f32>);
impl_op!(Sub::sub(types::Vec2<f32>, types::Vec2<f32>) -> types::Vec2<f32>);
impl_op!(Sub::sub(types::Vec3<f32>, types::Vec3<f32>) -> types::Vec3<f32>);
impl_op!(Sub::sub(types::Vec4<f32>, types::Vec4<f32>) -> types::Vec4<f32>);
impl_op!(Mul::mul(f32, types::Vec2<f32>) -> types::Vec2<f32>);
impl_op!(Mul::mul(types::Vec2<f32>, f32) -> types::Vec2<f32>);
impl_op!(Mul::mul(f32, types::Vec3<f32>) -> types::Vec3<f32>);
impl_op!(Mul::mul(types::Vec3<f32>, f32) -> types::Vec3<f32>);
impl_op!(Mul::mul(f32, types::Vec4<f32>) -> types::Vec4<f32>);
impl_op!(Mul::mul(types::Vec4<f32>, f32) -> types::Vec4<f32>);

impl_op!(Add::add(types::Mat2, types::Mat2) -> types::Mat2);
impl_op!(Add::add(types::Mat3, types::Mat3) -> types::Mat3);
impl_op!(Add::add(types::Mat4, types::Mat4) -> types::Mat4);
impl_op!(Sub::sub(types::Mat2, types::Mat2) -> types::Mat2);
impl_op!(Sub::sub(types::Mat3, types::Mat3) -> types::Mat3);
impl_op!(Sub::sub(types::Mat4, types::Mat4) -> types::Mat4);
impl_op!(Mul::mul(types::Mat2, types::Mat2) -> types::Mat2);
impl_op!(Mul::mul(types::Mat3, types::Mat3) -> types::Mat3);
impl_op!(Mul::mul(types::Mat4, types::Mat4) -> types::Mat4);
impl_op!(Mul::mul(f32, types::Mat2) -> types::Mat2);
impl_op!(Mul::mul(f32, types::Mat3) -> types::Mat3);
impl_op!(Mul::mul(f32, types::Mat4) -> types::Mat4);
impl_op!(Mul::mul(types::Mat2, types::Vec2<f32>) -> types::Vec2<f32>);
impl_op!(Mul::mul(types::Mat3, types::Vec3<f32>) -> types::Vec3<f32>);
impl_op!(Mul::mul(types::Mat4, types::Vec4<f32>) -> types::Vec4<f32>);
Loading

0 comments on commit e040a09

Please sign in to comment.