Skip to content

Commit

Permalink
Update Thunk
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Feb 8, 2024
1 parent 174f3f4 commit c0a6ec1
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 139 deletions.
6 changes: 3 additions & 3 deletions dunge/tests/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ fn shader_calc() -> Result<(), Error> {

let compute = || {
let m = -sl::mat2(sl::vec2(1., 0.), sl::vec2(0., 1.));
let [m0, m1, m3] = sl::thunk(m);
let v = m0.x() + m1.y();
let mt = sl::thunk(m);
let v = mt.clone().x() + mt.clone().y();
let z = sl::splat_vec3(1.).z();

Out {
place: sl::vec4_concat(m3.x(), v) * sl::f32(1) * z,
place: sl::vec4_concat(mt.x(), v) * sl::f32(1) * z,
color: sl::vec4(0., 0., 1., 1.) + Vec4::splat(0.),
}
};
Expand Down
4 changes: 2 additions & 2 deletions dunge/tests/triangle_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ fn render() -> Result<(), Error> {
const Y_OFFSET: f32 = 0.25;

let triangle = |Index(index): Index| {
let [x, y] = sl::thunk(sl::f32(index) * THIRD + R_OFFSET);
let i = sl::thunk(sl::f32(index) * THIRD + R_OFFSET);
Out {
place: sl::vec4(sl::cos(x), sl::sin(y) + Y_OFFSET, 0., 1.),
place: sl::vec4(sl::cos(i.clone()), sl::sin(i) + Y_OFFSET, 0., 1.),
color: COLOR,
}
};
Expand Down
4 changes: 2 additions & 2 deletions dunge/tests/triangle_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ fn render() -> Result<(), Error> {
struct Transform(Row<[f32; 2]>, Row<[f32; 3]>);

let triangle = |t: InInstance<Transform>, Index(index): Index| {
let [x, y] = sl::thunk(sl::f32(index) * THIRD + R_OFFSET);
let p = sl::vec2(sl::cos(x), sl::sin(y)) * TRIANGLE_SIZE + t.0;
let i = sl::thunk(sl::f32(index) * THIRD + R_OFFSET);
let p = sl::vec2(sl::cos(i.clone()), sl::sin(i)) * TRIANGLE_SIZE + t.0;
Out {
place: sl::vec4_concat(p, Vec2::new(0., 1.)),
color: sl::vec4_with(sl::fragment(t.1), 1.),
Expand Down
137 changes: 26 additions & 111 deletions dunge_shader/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ use {
crate::{
context::{Context, InputInfo, InstInfo, Stages, VertInfo},
define::Define,
math::Func,
module::{Module, Out, Output},
ret::Ret,
ret::{Bi, Ret, Un},
texture::Sampled,
types::{self, MemberType, ScalarType, ValueType, VectorType},
},
naga::{
AddressSpace, Arena, BinaryOperator, Binding, BuiltIn, EntryPoint, Expression, Function,
FunctionArgument, FunctionResult, GlobalVariable, Handle, Literal, LocalVariable, Range,
ResourceBinding, SampleLevel, ShaderStage, Span, Statement, StructMember, Type, TypeInner,
UnaryOperator, UniqueArena,
AddressSpace, Arena, Binding, BuiltIn, EntryPoint, Expression, Function, FunctionArgument,
FunctionResult, GlobalVariable, Handle, Literal, LocalVariable, Range, ResourceBinding,
ShaderStage, Span, Statement, StructMember, Type, TypeInner, UniqueArena,
},
std::{array, cell::Cell, collections::HashMap, iter, marker::PhantomData, mem, rc::Rc},
std::{cell::Cell, collections::HashMap, iter, marker::PhantomData, mem, rc::Rc},
};

pub(crate) fn make<O>(cx: Context, output: O) -> Module
Expand Down Expand Up @@ -173,13 +174,13 @@ pub struct ReadVertex {
}

impl ReadVertex {
pub const fn new<T>(id: u32, index: u32) -> Ret<Self, T> {
pub const fn new<O>(id: u32, index: u32) -> Ret<Self, O> {
Ret::new(Self { id, index })
}
}

impl<T> Eval<Vs> for Ret<ReadVertex, T> {
type Out = T;
impl<O> Eval<Vs> for Ret<ReadVertex, O> {
type Out = O;

fn eval(self, en: &mut Vs) -> Expr {
let en = &mut en.inner;
Expand All @@ -194,24 +195,24 @@ pub struct ReadInstance {
}

impl ReadInstance {
pub const fn new<T>(id: u32) -> Ret<Self, T> {
pub const fn new<O>(id: u32) -> Ret<Self, O> {
Ret::new(Self { id })
}
}

impl<T> Eval<Vs> for Ret<ReadInstance, T>
impl<O> Eval<Vs> for Ret<ReadInstance, O>
where
T: types::Value,
O: types::Value,
{
type Out = T;
type Out = O;

fn eval(self, en: &mut Vs) -> Expr {
let en = &mut en.inner;
let id = self.get().id;
match T::VALUE_TYPE {
match O::VALUE_TYPE {
ValueType::Scalar(_) | ValueType::Vector(_) => en.argument(id),
ValueType::Matrix(mat) => {
let ty = en.new_type(T::VALUE_TYPE.ty());
let ty = en.new_type(O::VALUE_TYPE.ty());
let arg = en.argument(id);
let exprs = (0..mat.dims())
.map(|index| en.access_index(arg, index))
Expand Down Expand Up @@ -255,11 +256,11 @@ impl ReadGlobal {
}
}

impl<T, E> Eval<E> for Ret<ReadGlobal, T>
impl<O, E> Eval<E> for Ret<ReadGlobal, O>
where
E: GetEntry,
{
type Out = T;
type Out = O;

fn eval(self, en: &mut E) -> Expr {
let ReadGlobal {
Expand Down Expand Up @@ -308,14 +309,11 @@ where
}
}

pub fn thunk<A, E, const N: usize>(a: A) -> [Ret<Thunk<A, E>, A::Out>; N]
pub fn thunk<A, E>(a: A) -> Ret<Thunk<A, E>, A::Out>
where
A: Eval<E>,
{
let state = State::Eval(a);
let inner = Rc::new(Cell::new(state));
let thunk = Thunk::new(Rc::clone(&inner));
array::from_fn(|_| Ret::new(thunk.clone()))
Ret::new(Thunk::new(a))
}

pub struct Thunk<A, E> {
Expand All @@ -324,17 +322,20 @@ pub struct Thunk<A, E> {
}

impl<A, E> Thunk<A, E> {
fn new(a: Rc<Cell<State<A>>>) -> Self {
fn new(a: A) -> Self {
Self {
s: a,
s: Rc::new(Cell::new(State::Eval(a))),
e: PhantomData,
}
}
}

impl<A, E> Clone for Thunk<A, E> {
fn clone(&self) -> Self {
Self::new(self.s.clone())
Self {
s: Rc::clone(&self.s),
e: PhantomData,
}
}
}

Expand Down Expand Up @@ -607,92 +608,6 @@ impl Argument {
}
}

pub(crate) enum Un {
Neg,
}

impl Un {
fn operator(self) -> UnaryOperator {
match self {
Self::Neg => UnaryOperator::Negate,
}
}
}

pub(crate) enum Bi {
Add,
Sub,
Mul,
Div,
Rem,
}

impl Bi {
fn operator(self) -> BinaryOperator {
match self {
Self::Add => BinaryOperator::Add,
Self::Sub => BinaryOperator::Subtract,
Self::Mul => BinaryOperator::Multiply,
Self::Div => BinaryOperator::Divide,
Self::Rem => BinaryOperator::Modulo,
}
}
}

pub(crate) enum Func {
Cos,
Cosh,
Sin,
Sinh,
Tan,
Tanh,
}

impl Func {
fn expr(self, ev: Evaluated) -> Expression {
use naga::MathFunction;

let fun = match self {
Self::Cos => MathFunction::Cos,
Self::Cosh => MathFunction::Cosh,
Self::Sin => MathFunction::Sin,
Self::Sinh => MathFunction::Sinh,
Self::Tan => MathFunction::Tan,
Self::Tanh => MathFunction::Tanh,
};

let mut exprs = ev.into_iter().map(Expr::get);
Expression::Math {
fun,
arg: exprs.next().expect("first argument"),
arg1: exprs.next(),
arg2: exprs.next(),
arg3: exprs.next(),
}
}
}

pub(crate) struct Sampled {
pub tex: Expr,
pub sam: Expr,
pub crd: Expr,
}

impl Sampled {
fn expr(self) -> Expression {
Expression::ImageSample {
image: self.tex.0,
sampler: self.sam.0,
gather: None,
coordinate: self.crd.0,
array_index: None,
offset: None,
level: SampleLevel::Auto,
depth_ref: None,
}
}
}

pub struct Entry {
compl: Compiler,
locls: Arena<LocalVariable>,
Expand Down
34 changes: 33 additions & 1 deletion dunge_shader/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use {
crate::{
eval::{Eval, EvalTuple, Evaluated, Expr, Func, GetEntry},
eval::{Eval, EvalTuple, Evaluated, Expr, GetEntry},
ret::Ret,
},
naga::{Expression, MathFunction},
std::marker::PhantomData,
};

Expand Down Expand Up @@ -78,3 +79,34 @@ where
en.get_entry().math(func, o)
}
}

pub(crate) enum Func {
Cos,
Cosh,
Sin,
Sinh,
Tan,
Tanh,
}

impl Func {
pub fn expr(self, ev: Evaluated) -> Expression {
let fun = match self {
Self::Cos => MathFunction::Cos,
Self::Cosh => MathFunction::Cosh,
Self::Sin => MathFunction::Sin,
Self::Sinh => MathFunction::Sinh,
Self::Tan => MathFunction::Tan,
Self::Tanh => MathFunction::Tanh,
};

let mut exprs = ev.into_iter().map(Expr::get);
Expression::Math {
fun,
arg: exprs.next().expect("first argument"),
arg1: exprs.next(),
arg2: exprs.next(),
arg3: exprs.next(),
}
}
}
43 changes: 38 additions & 5 deletions dunge_shader/src/ret.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use {
crate::eval::{Bi, Eval, Expr, GetEntry, Un, Vs},
crate::eval::{Eval, Expr, GetEntry, Vs},
naga::{BinaryOperator, UnaryOperator},
std::{marker::PhantomData, ops},
};

pub struct Ret<A, T> {
pub struct Ret<A, O> {
a: A,
t: PhantomData<T>,
t: PhantomData<O>,
}

impl<A, T> Ret<A, T> {
Expand All @@ -18,7 +19,7 @@ impl<A, T> Ret<A, T> {
}
}

impl<A, T> Clone for Ret<A, T>
impl<A, O> Clone for Ret<A, O>
where
A: Clone,
{
Expand All @@ -27,7 +28,7 @@ where
}
}

impl<A, T> Copy for Ret<A, T> where A: Copy {}
impl<A, O> Copy for Ret<A, O> where A: Copy {}

type Operand<A, B> = Ret<A, <B as Eval<Vs>>::Out>;

Expand Down Expand Up @@ -179,3 +180,35 @@ impl_binary!(Mul::mul(f32, glam::Mat4) -> glam::Mat4);
impl_binary!(Mul::mul(glam::Mat2, glam::Vec2) -> glam::Vec2);
impl_binary!(Mul::mul(glam::Mat3, glam::Vec3) -> glam::Vec3);
impl_binary!(Mul::mul(glam::Mat4, glam::Vec4) -> glam::Vec4);

pub(crate) enum Un {
Neg,
}

impl Un {
pub fn operator(self) -> UnaryOperator {
match self {
Self::Neg => UnaryOperator::Negate,
}
}
}

pub(crate) enum Bi {
Add,
Sub,
Mul,
Div,
Rem,
}

impl Bi {
pub fn operator(self) -> BinaryOperator {
match self {
Self::Add => BinaryOperator::Add,
Self::Sub => BinaryOperator::Subtract,
Self::Mul => BinaryOperator::Multiply,
Self::Div => BinaryOperator::Divide,
Self::Rem => BinaryOperator::Modulo,
}
}
}
Loading

0 comments on commit c0a6ec1

Please sign in to comment.