From 6ff725d89a38a09a63edfc2edf7cf3b581c1a30f Mon Sep 17 00:00:00 2001 From: Lain-dono Date: Mon, 1 Feb 2021 08:28:17 +0300 Subject: [PATCH] [spv-out] Add missing math functions (#401) * [spv-out] Add missing builtins * [spv-out] remove a non-existent integer version of mix --- src/back/spv/writer.rs | 65 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/src/back/spv/writer.rs b/src/back/spv/writer.rs index a7d450441..3feb3727d 100644 --- a/src/back/spv/writer.rs +++ b/src/back/spv/writer.rs @@ -1609,10 +1609,13 @@ impl Writer { } // trigonometry Mf::Sin => MathOp::Single(spirv::GLOp::Sin), + Mf::Sinh => MathOp::Single(spirv::GLOp::Sinh), Mf::Asin => MathOp::Single(spirv::GLOp::Asin), Mf::Cos => MathOp::Single(spirv::GLOp::Cos), + Mf::Cosh => MathOp::Single(spirv::GLOp::Cosh), Mf::Acos => MathOp::Single(spirv::GLOp::Acos), Mf::Tan => MathOp::Single(spirv::GLOp::Tan), + Mf::Tanh => MathOp::Single(spirv::GLOp::Tanh), Mf::Atan => MathOp::Single(spirv::GLOp::Atan), Mf::Atan2 => MathOp::Double(spirv::GLOp::Atan2), // decomposition @@ -1621,6 +1624,9 @@ impl Writer { Mf::Floor => MathOp::Single(spirv::GLOp::Floor), Mf::Fract => MathOp::Single(spirv::GLOp::Fract), Mf::Trunc => MathOp::Single(spirv::GLOp::Trunc), + Mf::Modf => MathOp::Double(spirv::GLOp::Modf), + Mf::Frexp => MathOp::Double(spirv::GLOp::Frexp), + Mf::Ldexp => MathOp::Double(spirv::GLOp::Ldexp), // geometry Mf::Dot => { let result_lookup_ty = @@ -1693,8 +1699,41 @@ impl Writer { MathOp::Other(inst, result_lookup_ty) } Mf::Normalize => MathOp::Single(spirv::GLOp::Normalize), + Mf::FaceForward => MathOp::Triple(spirv::GLOp::FaceForward), + Mf::Reflect => MathOp::Double(spirv::GLOp::Reflect), + // exponent + Mf::Exp => MathOp::Single(spirv::GLOp::Exp), + Mf::Exp2 => MathOp::Single(spirv::GLOp::Exp2), + Mf::Log => MathOp::Single(spirv::GLOp::Log), + Mf::Log2 => MathOp::Single(spirv::GLOp::Log2), Mf::Pow => MathOp::Double(spirv::GLOp::Pow), // computational + Mf::Sign => { + let op = match self + .get_type_inner(&ir_module.types, arg0_lookup_ty) + .scalar_kind() + { + Some(crate::ScalarKind::Float) => spirv::GLOp::FSign, + Some(crate::ScalarKind::Sint) => spirv::GLOp::SSign, + other => unimplemented!("Unexpected sign({:?})", other), + }; + MathOp::Single(op) + } + Mf::Fma => MathOp::Triple(spirv::GLOp::Fma), + Mf::Mix => { + let op = match self + .get_type_inner(&ir_module.types, arg0_lookup_ty) + .scalar_kind() + { + Some(crate::ScalarKind::Float) => spirv::GLOp::FMix, + other => unimplemented!("Unexpected mix({:?})", other), + }; + MathOp::Triple(op) + } + Mf::Step => MathOp::Triple(spirv::GLOp::Step), + Mf::SmoothStep => MathOp::Triple(spirv::GLOp::SmoothStep), + Mf::Sqrt => MathOp::Single(spirv::GLOp::Sqrt), + Mf::InverseSqrt => MathOp::Single(spirv::GLOp::InverseSqrt), Mf::Transpose => { let result_lookup_ty = match *self.get_type_inner(&ir_module.types, arg0_lookup_ty) { @@ -1720,7 +1759,31 @@ impl Writer { ); MathOp::Other(inst, result_lookup_ty) } - _ => { + Mf::Determinant => { + let result_lookup_ty = + match *self.get_type_inner(&ir_module.types, arg0_lookup_ty) { + crate::TypeInner::Matrix { width, .. } => { + LookupType::Local(LocalType::Scalar { + kind: crate::ScalarKind::Float, + width, + }) + } + _ => unreachable!(), + }; + let result_type_id = + self.get_type_id(&ir_module.types, result_lookup_ty)?; + + let inst = super::instructions::instruction_ext_inst( + self.gl450_ext_inst_id, + spirv::GLOp::Determinant, + result_type_id, + id, + &[arg0_id], + ); + MathOp::Other(inst, result_lookup_ty) + } + + Mf::Outer | Mf::ReverseBits | Mf::CountOneBits => { log::error!("unimplemented math function {:?}", fun); return Err(Error::FeatureNotImplemented("math function")); }