Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wgsl] Partially Update Built-in Functions #2218

Merged
merged 3 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 4 additions & 28 deletions src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1529,13 +1529,9 @@ impl<W: Write> Writer<W> {
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"),
Expand All @@ -1553,9 +1549,9 @@ impl<W: Write> Writer<W> {
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
Expand All @@ -1582,6 +1578,7 @@ impl<W: Write> Writer<W> {
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"),
Expand Down Expand Up @@ -1617,25 +1614,6 @@ impl<W: Write> Writer<W> {
};

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)?;
Expand Down Expand Up @@ -1707,8 +1685,6 @@ impl<W: Write> Writer<W> {
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)),
Expand Down
6 changes: 4 additions & 2 deletions src/front/wgsl/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ pub fn map_relational_fun(word: &str) -> Option<crate::RelationalFunction> {
match word {
"any" => Some(crate::RelationalFunction::Any),
"all" => Some(crate::RelationalFunction::All),
"isFinite" => Some(crate::RelationalFunction::IsFinite),
"isNormal" => Some(crate::RelationalFunction::IsNormal),
_ => None,
}
}
Expand All @@ -149,8 +147,11 @@ pub fn map_standard_fun(word: &str) -> Option<crate::MathFunction> {
"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,
Expand Down Expand Up @@ -178,6 +179,7 @@ pub fn map_standard_fun(word: &str) -> Option<crate::MathFunction> {
"normalize" => Mf::Normalize,
"faceForward" => Mf::FaceForward,
"reflect" => Mf::Reflect,
"refract" => Mf::Refract,
// computational
"sign" => Mf::Sign,
"fma" => Mf::Fma,
Expand Down
1 change: 1 addition & 0 deletions tests/in/math-functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>(), vec2<i32>());
let first_leading_bit_abs = firstLeadingBit(abs(0u));
}
1 change: 1 addition & 0 deletions tests/out/glsl/math-functions.main.Vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))));
}
Expand Down
1 change: 1 addition & 0 deletions tests/out/hlsl/math-functions.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
1 change: 1 addition & 0 deletions tests/out/msl/math-functions.msl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
21 changes: 11 additions & 10 deletions tests/out/spv/math-functions.spvasm
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
6 changes: 3 additions & 3 deletions tests/out/wgsl/inv-hyperbolic-trig-functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions tests/out/wgsl/math-functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>(0, 0), vec2<i32>(0, 0));
let first_leading_bit_abs = firstLeadingBit(abs(0u));
}