From df14f78b016faaee0574ab11812cc03172c4ba9e Mon Sep 17 00:00:00 2001 From: Evan Mark Hopkins Date: Sun, 22 Jan 2023 14:24:47 -0500 Subject: [PATCH 1/3] [wgsl] Add refract built-in --- src/back/wgsl/writer.rs | 1 + src/front/wgsl/conv.rs | 1 + tests/in/math-functions.wgsl | 1 + .../out/glsl/math-functions.main.Vertex.glsl | 1 + tests/out/hlsl/math-functions.hlsl | 1 + tests/out/msl/math-functions.msl | 1 + tests/out/spv/math-functions.spvasm | 21 ++++++++++--------- tests/out/wgsl/math-functions.wgsl | 1 + 8 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/back/wgsl/writer.rs b/src/back/wgsl/writer.rs index b2cec92da3..74c0bf3e88 100644 --- a/src/back/wgsl/writer.rs +++ b/src/back/wgsl/writer.rs @@ -1582,6 +1582,7 @@ impl Writer { Mf::Normalize => Function::Regular("normalize"), Mf::FaceForward => Function::Regular("faceForward"), Mf::Reflect => Function::Regular("reflect"), + Mf::Refract => Function::Regular("refract"), // computational Mf::Sign => Function::Regular("sign"), Mf::Fma => Function::Regular("fma"), diff --git a/src/front/wgsl/conv.rs b/src/front/wgsl/conv.rs index 645f90b011..e4c29f7939 100644 --- a/src/front/wgsl/conv.rs +++ b/src/front/wgsl/conv.rs @@ -178,6 +178,7 @@ pub fn map_standard_fun(word: &str) -> Option { "normalize" => Mf::Normalize, "faceForward" => Mf::FaceForward, "reflect" => Mf::Reflect, + "refract" => Mf::Refract, // computational "sign" => Mf::Sign, "fma" => Mf::Fma, diff --git a/tests/in/math-functions.wgsl b/tests/in/math-functions.wgsl index 9ecac80499..13b080e927 100644 --- a/tests/in/math-functions.wgsl +++ b/tests/in/math-functions.wgsl @@ -7,6 +7,7 @@ fn main() { let c = degrees(v); let d = radians(v); let e = saturate(v); + let g = refract(v, v, f); let const_dot = dot(vec2(), vec2()); let first_leading_bit_abs = firstLeadingBit(abs(0u)); } diff --git a/tests/out/glsl/math-functions.main.Vertex.glsl b/tests/out/glsl/math-functions.main.Vertex.glsl index f0954772e6..5efa8f27ca 100644 --- a/tests/out/glsl/math-functions.main.Vertex.glsl +++ b/tests/out/glsl/math-functions.main.Vertex.glsl @@ -11,6 +11,7 @@ void main() { vec4 c = degrees(v); vec4 d = radians(v); vec4 e = clamp(v, vec4(0.0), vec4(1.0)); + vec4 g = refract(v, v, 1.0); int const_dot = ( + ivec2(0, 0).x * ivec2(0, 0).x + ivec2(0, 0).y * ivec2(0, 0).y); uint first_leading_bit_abs = uint(findMSB(uint(abs(int(0u))))); } diff --git a/tests/out/hlsl/math-functions.hlsl b/tests/out/hlsl/math-functions.hlsl index 827da71b4a..64c2e87eab 100644 --- a/tests/out/hlsl/math-functions.hlsl +++ b/tests/out/hlsl/math-functions.hlsl @@ -7,6 +7,7 @@ void main() float4 c = degrees(v); float4 d = radians(v); float4 e = saturate(v); + float4 g = refract(v, v, 1.0); int const_dot = dot(int2(0, 0), int2(0, 0)); uint first_leading_bit_abs = firstbithigh(abs(0u)); } diff --git a/tests/out/msl/math-functions.msl b/tests/out/msl/math-functions.msl index 559d464681..d097be47a8 100644 --- a/tests/out/msl/math-functions.msl +++ b/tests/out/msl/math-functions.msl @@ -14,6 +14,7 @@ vertex void main_( metal::float4 c = ((v) * 57.295779513082322865); metal::float4 d = ((v) * 0.017453292519943295474); metal::float4 e = metal::saturate(v); + metal::float4 g = metal::refract(v, v, 1.0); int const_dot = ( + const_type_1_.x * const_type_1_.x + const_type_1_.y * const_type_1_.y); uint first_leading_bit_abs = (((metal::clz(metal::abs(0u)) + 1) % 33) - 1); } diff --git a/tests/out/spv/math-functions.spvasm b/tests/out/spv/math-functions.spvasm index 194024b769..4fada56c7f 100644 --- a/tests/out/spv/math-functions.spvasm +++ b/tests/out/spv/math-functions.spvasm @@ -1,7 +1,7 @@ ; SPIR-V ; Version: 1.1 ; Generator: rspirv -; Bound: 36 +; Bound: 37 OpCapability Shader %1 = OpExtInstImport "GLSL.std.450" OpMemoryModel Logical GLSL450 @@ -18,7 +18,7 @@ OpEntryPoint Vertex %14 "main" %11 = OpTypeVector %7 2 %12 = OpConstantComposite %11 %6 %6 %15 = OpTypeFunction %2 -%26 = OpConstantNull %7 +%27 = OpConstantNull %7 %14 = OpFunction %2 None %15 %13 = OpLabel OpBranch %16 @@ -31,15 +31,16 @@ OpBranch %16 %23 = OpCompositeConstruct %10 %5 %5 %5 %5 %24 = OpCompositeConstruct %10 %3 %3 %3 %3 %22 = OpExtInst %10 %1 FClamp %17 %23 %24 -%27 = OpCompositeExtract %7 %12 0 +%25 = OpExtInst %10 %1 Refract %17 %17 %3 %28 = OpCompositeExtract %7 %12 0 -%29 = OpIMul %7 %27 %28 -%30 = OpIAdd %7 %26 %29 -%31 = OpCompositeExtract %7 %12 1 +%29 = OpCompositeExtract %7 %12 0 +%30 = OpIMul %7 %28 %29 +%31 = OpIAdd %7 %27 %30 %32 = OpCompositeExtract %7 %12 1 -%33 = OpIMul %7 %31 %32 -%25 = OpIAdd %7 %30 %33 -%34 = OpCopyObject %9 %8 -%35 = OpExtInst %9 %1 FindUMsb %34 +%33 = OpCompositeExtract %7 %12 1 +%34 = OpIMul %7 %32 %33 +%26 = OpIAdd %7 %31 %34 +%35 = OpCopyObject %9 %8 +%36 = OpExtInst %9 %1 FindUMsb %35 OpReturn OpFunctionEnd \ No newline at end of file diff --git a/tests/out/wgsl/math-functions.wgsl b/tests/out/wgsl/math-functions.wgsl index 2643879ee3..004d9cf5be 100644 --- a/tests/out/wgsl/math-functions.wgsl +++ b/tests/out/wgsl/math-functions.wgsl @@ -6,6 +6,7 @@ fn main() { let c = degrees(v); let d = radians(v); let e = saturate(v); + let g = refract(v, v, 1.0); let const_dot = dot(vec2(0, 0), vec2(0, 0)); let first_leading_bit_abs = firstLeadingBit(abs(0u)); } From 2ca00e391eff781cec9d3c5f3a5ef69e37999f1f Mon Sep 17 00:00:00 2001 From: Evan Mark Hopkins Date: Sun, 22 Jan 2023 14:29:15 -0500 Subject: [PATCH 2/3] [wgsl] Update inverse hyperbolic built-ins --- src/back/wgsl/writer.rs | 29 ++----------------- src/front/wgsl/conv.rs | 3 ++ .../wgsl/inv-hyperbolic-trig-functions.wgsl | 6 ++-- 3 files changed, 9 insertions(+), 29 deletions(-) diff --git a/src/back/wgsl/writer.rs b/src/back/wgsl/writer.rs index 74c0bf3e88..e91024c2e0 100644 --- a/src/back/wgsl/writer.rs +++ b/src/back/wgsl/writer.rs @@ -1529,13 +1529,9 @@ impl Writer { use crate::MathFunction as Mf; enum Function { - Asincosh { is_sin: bool }, - Atanh, Regular(&'static str), } - // NOTE: If https://github.com/gpuweb/gpuweb/issues/1622 ever is - // accepted, replace this with the builtin functions let function = match fun { Mf::Abs => Function::Regular("abs"), Mf::Min => Function::Regular("min"), @@ -1553,9 +1549,9 @@ impl Writer { Mf::Asin => Function::Regular("asin"), Mf::Atan => Function::Regular("atan"), Mf::Atan2 => Function::Regular("atan2"), - Mf::Asinh => Function::Asincosh { is_sin: true }, - Mf::Acosh => Function::Asincosh { is_sin: false }, - Mf::Atanh => Function::Atanh, + Mf::Asinh => Function::Regular("asinh"), + Mf::Acosh => Function::Regular("acosh"), + Mf::Atanh => Function::Regular("atanh"), Mf::Radians => Function::Regular("radians"), Mf::Degrees => Function::Regular("degrees"), // decomposition @@ -1618,25 +1614,6 @@ impl Writer { }; match function { - Function::Asincosh { is_sin } => { - write!(self.out, "log(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " + sqrt(")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, " * ")?; - self.write_expr(module, arg, func_ctx)?; - match is_sin { - true => write!(self.out, " + 1.0))")?, - false => write!(self.out, " - 1.0))")?, - } - } - Function::Atanh => { - write!(self.out, "0.5 * log((1.0 + ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, ") / (1.0 - ")?; - self.write_expr(module, arg, func_ctx)?; - write!(self.out, "))")?; - } Function::Regular(fun_name) => { write!(self.out, "{}(", fun_name)?; self.write_expr(module, arg, func_ctx)?; diff --git a/src/front/wgsl/conv.rs b/src/front/wgsl/conv.rs index e4c29f7939..7aa4cbc07d 100644 --- a/src/front/wgsl/conv.rs +++ b/src/front/wgsl/conv.rs @@ -149,8 +149,11 @@ pub fn map_standard_fun(word: &str) -> Option { "tan" => Mf::Tan, "tanh" => Mf::Tanh, "acos" => Mf::Acos, + "acosh" => Mf::Acosh, "asin" => Mf::Asin, + "asinh" => Mf::Asinh, "atan" => Mf::Atan, + "atanh" => Mf::Atanh, "atan2" => Mf::Atan2, "radians" => Mf::Radians, "degrees" => Mf::Degrees, diff --git a/tests/out/wgsl/inv-hyperbolic-trig-functions.wgsl b/tests/out/wgsl/inv-hyperbolic-trig-functions.wgsl index e7bee8d858..2ac357020a 100644 --- a/tests/out/wgsl/inv-hyperbolic-trig-functions.wgsl +++ b/tests/out/wgsl/inv-hyperbolic-trig-functions.wgsl @@ -6,11 +6,11 @@ fn main_1() { var d: f32; let _e8 = a; - b = log(_e8 + sqrt(_e8 * _e8 + 1.0)); + b = asinh(_e8); let _e10 = a; - c = log(_e10 + sqrt(_e10 * _e10 - 1.0)); + c = acosh(_e10); let _e12 = a; - d = 0.5 * log((1.0 + _e12) / (1.0 - _e12)); + d = atanh(_e12); return; } From 956579e0fa636db6faa23598037b65b81daa98e8 Mon Sep 17 00:00:00 2001 From: Evan Mark Hopkins Date: Sun, 22 Jan 2023 18:43:24 -0500 Subject: [PATCH 3/3] [wgsl] Removes isFinite and isNormal --- src/back/wgsl/writer.rs | 2 -- src/front/wgsl/conv.rs | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/back/wgsl/writer.rs b/src/back/wgsl/writer.rs index e91024c2e0..da3f75d7e2 100644 --- a/src/back/wgsl/writer.rs +++ b/src/back/wgsl/writer.rs @@ -1685,8 +1685,6 @@ impl Writer { use crate::RelationalFunction as Rf; let fun_name = match fun { - Rf::IsFinite => "isFinite", - Rf::IsNormal => "isNormal", Rf::All => "all", Rf::Any => "any", _ => return Err(Error::UnsupportedRelationalFunction(fun)), diff --git a/src/front/wgsl/conv.rs b/src/front/wgsl/conv.rs index 7aa4cbc07d..ca4cc5519e 100644 --- a/src/front/wgsl/conv.rs +++ b/src/front/wgsl/conv.rs @@ -126,8 +126,6 @@ pub fn map_relational_fun(word: &str) -> Option { match word { "any" => Some(crate::RelationalFunction::Any), "all" => Some(crate::RelationalFunction::All), - "isFinite" => Some(crate::RelationalFunction::IsFinite), - "isNormal" => Some(crate::RelationalFunction::IsNormal), _ => None, } }