From 875348fc8da06a32c246ceba3b4e8e0500554aa7 Mon Sep 17 00:00:00 2001 From: nanoqsh Date: Wed, 3 Jan 2024 08:18:58 +0600 Subject: [PATCH] Operators --- dunge/src/context.rs | 9 +++- dunge/tests/triangle.rs | 2 +- dunge_shader/src/eval.rs | 109 ++++++++++++++++++++++++++++++++------- 3 files changed, 98 insertions(+), 22 deletions(-) diff --git a/dunge/src/context.rs b/dunge/src/context.rs index cde3b18..4549ba1 100644 --- a/dunge/src/context.rs +++ b/dunge/src/context.rs @@ -20,7 +20,14 @@ pub struct Context(Arc); impl Context { pub async fn new() -> Result { - let instance = wgpu::Instance::default(); + use wgpu::{Backends, Instance, InstanceDescriptor}; + + let desc = InstanceDescriptor { + backends: Backends::PRIMARY, + ..Default::default() + }; + + let instance = Instance::new(desc); let state = State::new(&instance).await?; Ok(Self(Arc::new(state))) } diff --git a/dunge/tests/triangle.rs b/dunge/tests/triangle.rs index 223be55..4d764f1 100644 --- a/dunge/tests/triangle.rs +++ b/dunge/tests/triangle.rs @@ -20,7 +20,7 @@ fn render() -> Result<(), Error> { const THIRD: f32 = consts::TAU / 3.; let triangle = |Index(index): Index| { - let [x, y] = sl::share(sl::f32(index) * THIRD); + let [x, y] = sl::thunk(sl::f32(index) * THIRD); Out { place: sl::vec4(sl::cos(x), sl::sin(y), 0., 1.), color: Vec4::new(1., 0., 0., 1.), diff --git a/dunge_shader/src/eval.rs b/dunge_shader/src/eval.rs index f28282a..c53c77f 100644 --- a/dunge_shader/src/eval.rs +++ b/dunge_shader/src/eval.rs @@ -125,19 +125,75 @@ const fn ret(a: A) -> Ret { Ret { a, t: PhantomData } } +impl ops::Add> for f32 { + type Output = Ret>, Self>; + + fn add(self, b: Ret) -> Self::Output { + ret(Binary { + a: self, + b, + op: Op::Add, + }) + } +} + +impl ops::Add for Ret { + type Output = Ret, f32>; + + fn add(self, b: f32) -> Self::Output { + ret(Binary { + a: self, + b, + op: Op::Add, + }) + } +} + +impl ops::Sub> for f32 { + type Output = Ret>, Self>; + + fn sub(self, b: Ret) -> Self::Output { + ret(Binary { + a: self, + b, + op: Op::Sub, + }) + } +} + +impl ops::Sub for Ret { + type Output = Ret, f32>; + + fn sub(self, b: f32) -> Self::Output { + ret(Binary { + a: self, + b, + op: Op::Sub, + }) + } +} + impl ops::Mul> for f32 { - type Output = Ret>, Self>; + type Output = Ret>, Self>; fn mul(self, b: Ret) -> Self::Output { - ret(Mul { a: self, b }) + ret(Binary { + a: self, + b, + op: Op::Mul, + }) } } impl ops::Mul for Ret { - type Output = Ret, f32>; + type Output = Ret, f32>; fn mul(self, b: f32) -> Self::Output { - ret(Mul { a: self, b }) + ret(Binary { + a: self, + b, + op: Op::Mul, + }) } } @@ -145,10 +201,14 @@ impl ops::Mul for Ret where O: types::Vector, { - type Output = Ret, O>; + type Output = Ret, O>; fn mul(self, b: f32) -> Self::Output { - ret(Mul { a: self, b }) + ret(Binary { + a: self, + b, + op: Op::Mul, + }) } } @@ -156,19 +216,30 @@ impl ops::Mul> for f32 where O: types::Vector, { - type Output = Ret>, O>; + type Output = Ret>, O>; fn mul(self, b: Ret) -> Self::Output { - ret(Mul { a: self, b }) + ret(Binary { + a: self, + b, + op: Op::Mul, + }) } } -pub struct Mul { +enum Op { + Add, + Sub, + Mul, +} + +pub struct Binary { a: A, b: B, + op: Op, } -impl Eval for Ret, O> +impl Eval for Ret, O> where A: Eval, B: Eval, @@ -179,7 +250,7 @@ where fn eval(self, en: &mut E) -> Expr { let x = self.a.a.eval(en); let y = self.a.b.eval(en); - en.get().binary(Op::Mul, x, y) + en.get().binary(self.a.op, x, y) } } @@ -445,22 +516,22 @@ where } } -pub fn share(a: A) -> [Ret, A::Out>; N] +pub fn thunk(a: A) -> [Ret, A::Out>; N] where A: Eval, { let state = State::Eval(a); let inner = Rc::new(Cell::new(state)); - array::from_fn(|_| ret(Share(Rc::clone(&inner)))) + array::from_fn(|_| ret(Thunk(Rc::clone(&inner)))) } -pub struct Share(Rc>>); +pub struct Thunk(Rc>>); -impl Eval for Ret, O> +impl Eval for Ret, O> where A: Eval, { - type Out = A::Out; + type Out = O; fn eval(self, en: &mut E) -> Expr { match self.a.0.replace(State::None) { @@ -931,10 +1002,6 @@ impl Argument { } } -enum Op { - Mul, -} - type Args<'a> = dyn Iterator + 'a; pub struct Entry { @@ -999,6 +1066,8 @@ impl Entry { fn binary(&mut self, op: Op, a: Expr, b: Expr) -> Expr { let op = match op { + Op::Add => BinaryOperator::Add, + Op::Sub => BinaryOperator::Subtract, Op::Mul => BinaryOperator::Multiply, };