From 99e566289d74f40e93781cd6453b8f2910d8c1a4 Mon Sep 17 00:00:00 2001 From: nanoqsh Date: Wed, 17 Apr 2024 20:56:22 +0500 Subject: [PATCH] More math functions --- dunge_shader/src/math.rs | 73 ++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/dunge_shader/src/math.rs b/dunge_shader/src/math.rs index 9874ff7..649c07d 100644 --- a/dunge_shader/src/math.rs +++ b/dunge_shader/src/math.rs @@ -2,7 +2,7 @@ use { crate::{ eval::{Eval, EvalTuple, Evaluated, Expr, GetEntry}, op::Ret, - types::Number, + types::{Number, Vec3, Vector}, }, naga::{Expression, MathFunction}, std::marker::PhantomData, @@ -13,105 +13,142 @@ where X: Eval, X::Out: Number, { - Ret::new(Math::new((x,), Func(MathFunction::Abs))) + Ret::new(Math::new((x,), MathFunction::Abs)) } pub const fn acos(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Acos))) + Ret::new(Math::new((x,), MathFunction::Acos)) } pub const fn acosh(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Acosh))) + Ret::new(Math::new((x,), MathFunction::Acosh)) } pub const fn asin(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Asin))) + Ret::new(Math::new((x,), MathFunction::Asin)) } pub const fn asinh(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Asinh))) + Ret::new(Math::new((x,), MathFunction::Asinh)) } pub const fn atan(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Atan))) + Ret::new(Math::new((x,), MathFunction::Atan)) } pub const fn atanh(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Atanh))) + Ret::new(Math::new((x,), MathFunction::Atanh)) +} + +pub const fn atan2(y: Y, x: X) -> Ret, f32> +where + Y: Eval, + X: Eval, +{ + Ret::new(Math::new((y, x), MathFunction::Atan2)) } pub const fn ceil(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Ceil))) + Ret::new(Math::new((x,), MathFunction::Ceil)) +} + +pub const fn clamp(x: X, lo: L, hi: H) -> Ret, f32> +where + X: Eval, + X::Out: Number, + L: Eval, + H: Eval, +{ + Ret::new(Math::new((x, lo, hi), MathFunction::Clamp)) } pub const fn cos(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Cos))) + Ret::new(Math::new((x,), MathFunction::Cos)) } pub const fn cosh(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Cosh))) + Ret::new(Math::new((x,), MathFunction::Cosh)) +} + +pub const fn cross(x: X, y: Y) -> Ret, Vec3> +where + X: Eval>, + Y: Eval>, +{ + Ret::new(Math::new((x, y), MathFunction::Cross)) +} + +#[allow(clippy::type_complexity)] +pub const fn dot(x: X, y: Y) -> Ret, ::Scalar> +where + X: Eval, + X::Out: Vector, + ::Scalar: Number, + Y: Eval, +{ + Ret::new(Math::new((x, y), MathFunction::Dot)) } pub const fn floor(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Floor))) + Ret::new(Math::new((x,), MathFunction::Floor)) } pub const fn sin(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Sin))) + Ret::new(Math::new((x,), MathFunction::Sin)) } pub const fn sinh(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Sinh))) + Ret::new(Math::new((x,), MathFunction::Sinh)) } pub const fn tan(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Tan))) + Ret::new(Math::new((x,), MathFunction::Tan)) } pub const fn tanh(x: X) -> Ret, f32> where X: Eval, { - Ret::new(Math::new((x,), Func(MathFunction::Tanh))) + Ret::new(Math::new((x,), MathFunction::Tanh)) } pub struct Math { @@ -121,10 +158,10 @@ pub struct Math { } impl Math { - const fn new(args: A, func: Func) -> Self { + const fn new(args: A, func: MathFunction) -> Self { Self { args, - func, + func: Func(func), e: PhantomData, } }