Skip to content

Commit

Permalink
[wgsl-in] Allow sign() to take int argument
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Sep 2, 2023
1 parent 5329aa2 commit 41d9b7e
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 101 deletions.
17 changes: 16 additions & 1 deletion src/valid/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,6 @@ impl super::Validator {
| Mf::Log
| Mf::Log2
| Mf::Length
| Mf::Sign
| Mf::Sqrt
| Mf::InverseSqrt => {
if arg1_ty.is_some() | arg2_ty.is_some() | arg3_ty.is_some() {
Expand All @@ -992,6 +991,22 @@ impl super::Validator {
_ => return Err(ExpressionError::InvalidArgumentType(fun, 0, arg)),
}
}
Mf::Sign => {
if arg1_ty.is_some() | arg2_ty.is_some() | arg3_ty.is_some() {
return Err(ExpressionError::WrongArgumentCount(fun));
}
match *arg_ty {
Ti::Scalar {
kind: Sk::Float | Sk::Sint,
..
}
| Ti::Vector {
kind: Sk::Float | Sk::Sint,
..
} => {}
_ => return Err(ExpressionError::InvalidArgumentType(fun, 0, arg)),
}
}
Mf::Atan2 | Mf::Pow | Mf::Distance | Mf::Step => {
let arg1_ty = match (arg1_ty, arg2_ty, arg3_ty) {
(Some(ty1), None, None) => ty1,
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 @@ -8,6 +8,7 @@ fn main() {
let d = radians(v);
let e = saturate(v);
let g = refract(v, v, f);
let h = sign(-1);
let const_dot = dot(vec2<i32>(), vec2<i32>());
let first_leading_bit_abs = firstLeadingBit(abs(0u));
let flb_a = firstLeadingBit(-1);
Expand Down
5 changes: 3 additions & 2 deletions tests/out/glsl/math-functions.main.Fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void main() {
vec4 d = radians(v);
vec4 e = clamp(v, vec4(0.0), vec4(1.0));
vec4 g = refract(v, v, 1.0);
int h = sign(-1);
int const_dot = ( + ivec2(0).x * ivec2(0).x + ivec2(0).y * ivec2(0).y);
uint first_leading_bit_abs = uint(findMSB(uint(abs(int(0u)))));
int flb_a = findMSB(-1);
Expand All @@ -81,8 +82,8 @@ void main() {
ivec2 ctz_h = ivec2(min(uvec2(findLSB(ivec2(1))), uvec2(32u)));
int clz_a = (-1 < 0 ? 0 : 31 - findMSB(-1));
uint clz_b = uint(31 - findMSB(1u));
ivec2 _e58 = ivec2(-1);
ivec2 clz_c = mix(ivec2(31) - findMSB(_e58), ivec2(0), lessThan(_e58, ivec2(0)));
ivec2 _e60 = ivec2(-1);
ivec2 clz_c = mix(ivec2(31) - findMSB(_e60), ivec2(0), lessThan(_e60, ivec2(0)));
uvec2 clz_d = uvec2(ivec2(31) - findMSB(uvec2(1u)));
float lde_a = ldexp(1.0, 2);
vec2 lde_b = ldexp(vec2(1.0, 2.0), ivec2(3, 4));
Expand Down
5 changes: 3 additions & 2 deletions tests/out/hlsl/math-functions.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void main()
float4 d = radians(v);
float4 e = saturate(v);
float4 g = refract(v, v, 1.0);
int h = sign(-1);
int const_dot = dot((int2)0, (int2)0);
uint first_leading_bit_abs = firstbithigh(abs(0u));
int flb_a = asint(firstbithigh(-1));
Expand All @@ -91,8 +92,8 @@ void main()
int2 ctz_h = asint(min((32u).xx, firstbitlow((1).xx)));
int clz_a = (-1 < 0 ? 0 : 31 - asint(firstbithigh(-1)));
uint clz_b = (31u - firstbithigh(1u));
int2 _expr58 = (-1).xx;
int2 clz_c = (_expr58 < (0).xx ? (0).xx : (31).xx - asint(firstbithigh(_expr58)));
int2 _expr60 = (-1).xx;
int2 clz_c = (_expr60 < (0).xx ? (0).xx : (31).xx - asint(firstbithigh(_expr60)));
uint2 clz_d = ((31u).xx - firstbithigh((1u).xx));
float lde_a = ldexp(1.0, 2);
float2 lde_b = ldexp(float2(1.0, 2.0), int2(3, 4));
Expand Down
13 changes: 7 additions & 6 deletions tests/out/msl/math-functions.msl
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ fragment void main_(
metal::float4 d = ((v) * 0.017453292519943295474);
metal::float4 e = metal::saturate(v);
metal::float4 g = metal::refract(v, v, 1.0);
int h = metal::sign(-1);
int const_dot = ( + metal::int2 {}.x * metal::int2 {}.x + metal::int2 {}.y * metal::int2 {}.y);
uint _e13 = metal::abs(0u);
uint first_leading_bit_abs = metal::select(31 - metal::clz(_e13), uint(-1), _e13 == 0 || _e13 == -1);
uint _e15 = metal::abs(0u);
uint first_leading_bit_abs = metal::select(31 - metal::clz(_e15), uint(-1), _e15 == 0 || _e15 == -1);
int flb_a = metal::select(31 - metal::clz(metal::select(-1, ~-1, -1 < 0)), int(-1), -1 == 0 || -1 == -1);
metal::int2 _e18 = metal::int2(-1);
metal::int2 flb_b = metal::select(31 - metal::clz(metal::select(_e18, ~_e18, _e18 < 0)), int2(-1), _e18 == 0 || _e18 == -1);
metal::uint2 _e21 = metal::uint2(1u);
metal::uint2 flb_c = metal::select(31 - metal::clz(_e21), uint2(-1), _e21 == 0 || _e21 == -1);
metal::int2 _e20 = metal::int2(-1);
metal::int2 flb_b = metal::select(31 - metal::clz(metal::select(_e20, ~_e20, _e20 < 0)), int2(-1), _e20 == 0 || _e20 == -1);
metal::uint2 _e23 = metal::uint2(1u);
metal::uint2 flb_c = metal::select(31 - metal::clz(_e23), uint2(-1), _e23 == 0 || _e23 == -1);
int ftb_a = (((metal::ctz(-1) + 1) % 33) - 1);
uint ftb_b = (((metal::ctz(1u) + 1) % 33) - 1);
metal::int2 ftb_c = (((metal::ctz(metal::int2(-1)) + 1) % 33) - 1);
Expand Down
181 changes: 91 additions & 90 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: 127
; Bound: 128
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
Expand Down Expand Up @@ -32,13 +32,13 @@ OpMemberDecorate %13 1 Offset 16
%16 = OpTypeFunction %2
%17 = OpConstant %4 1.0
%18 = OpConstant %4 0.0
%19 = OpConstantNull %5
%20 = OpTypeInt 32 0
%21 = OpConstant %20 0
%22 = OpConstant %6 -1
%23 = OpConstant %20 1
%19 = OpConstant %6 -1
%20 = OpConstantNull %5
%21 = OpTypeInt 32 0
%22 = OpConstant %21 0
%23 = OpConstant %21 1
%24 = OpConstant %6 0
%25 = OpConstant %20 4294967295
%25 = OpConstant %21 4294967295
%26 = OpConstant %6 1
%27 = OpConstant %6 2
%28 = OpConstant %4 2.0
Expand All @@ -47,12 +47,12 @@ OpMemberDecorate %13 1 Offset 16
%31 = OpConstant %4 1.5
%39 = OpConstantComposite %3 %18 %18 %18 %18
%40 = OpConstantComposite %3 %17 %17 %17 %17
%43 = OpConstantNull %6
%56 = OpTypeVector %20 2
%66 = OpConstant %20 32
%76 = OpConstantComposite %56 %66 %66
%88 = OpConstant %6 31
%94 = OpConstantComposite %5 %88 %88
%44 = OpConstantNull %6
%57 = OpTypeVector %21 2
%67 = OpConstant %21 32
%77 = OpConstantComposite %57 %67 %67
%89 = OpConstant %6 31
%95 = OpConstantComposite %5 %89 %89
%15 = OpFunction %2 None %16
%14 = OpLabel
OpBranch %32
Expand All @@ -64,84 +64,85 @@ OpBranch %32
%37 = OpExtInst %3 %1 Radians %33
%38 = OpExtInst %3 %1 FClamp %33 %39 %40
%41 = OpExtInst %3 %1 Refract %33 %33 %17
%44 = OpCompositeExtract %6 %19 0
%45 = OpCompositeExtract %6 %19 0
%46 = OpIMul %6 %44 %45
%47 = OpIAdd %6 %43 %46
%48 = OpCompositeExtract %6 %19 1
%49 = OpCompositeExtract %6 %19 1
%50 = OpIMul %6 %48 %49
%42 = OpIAdd %6 %47 %50
%51 = OpCopyObject %20 %21
%52 = OpExtInst %20 %1 FindUMsb %51
%53 = OpExtInst %6 %1 FindSMsb %22
%54 = OpCompositeConstruct %5 %22 %22
%55 = OpExtInst %5 %1 FindSMsb %54
%57 = OpCompositeConstruct %56 %23 %23
%58 = OpExtInst %56 %1 FindUMsb %57
%59 = OpExtInst %6 %1 FindILsb %22
%60 = OpExtInst %20 %1 FindILsb %23
%61 = OpCompositeConstruct %5 %22 %22
%62 = OpExtInst %5 %1 FindILsb %61
%63 = OpCompositeConstruct %56 %23 %23
%64 = OpExtInst %56 %1 FindILsb %63
%67 = OpExtInst %20 %1 FindILsb %21
%65 = OpExtInst %20 %1 UMin %66 %67
%69 = OpExtInst %6 %1 FindILsb %24
%68 = OpExtInst %6 %1 UMin %66 %69
%71 = OpExtInst %20 %1 FindILsb %25
%70 = OpExtInst %20 %1 UMin %66 %71
%73 = OpExtInst %6 %1 FindILsb %22
%72 = OpExtInst %6 %1 UMin %66 %73
%74 = OpCompositeConstruct %56 %21 %21
%77 = OpExtInst %56 %1 FindILsb %74
%75 = OpExtInst %56 %1 UMin %76 %77
%78 = OpCompositeConstruct %5 %24 %24
%80 = OpExtInst %5 %1 FindILsb %78
%79 = OpExtInst %5 %1 UMin %76 %80
%81 = OpCompositeConstruct %56 %23 %23
%83 = OpExtInst %56 %1 FindILsb %81
%82 = OpExtInst %56 %1 UMin %76 %83
%84 = OpCompositeConstruct %5 %26 %26
%86 = OpExtInst %5 %1 FindILsb %84
%85 = OpExtInst %5 %1 UMin %76 %86
%89 = OpExtInst %6 %1 FindUMsb %22
%87 = OpISub %6 %88 %89
%91 = OpExtInst %6 %1 FindUMsb %23
%90 = OpISub %20 %88 %91
%92 = OpCompositeConstruct %5 %22 %22
%95 = OpExtInst %5 %1 FindUMsb %92
%93 = OpISub %5 %94 %95
%96 = OpCompositeConstruct %56 %23 %23
%98 = OpExtInst %5 %1 FindUMsb %96
%97 = OpISub %56 %94 %98
%99 = OpExtInst %4 %1 Ldexp %17 %27
%100 = OpCompositeConstruct %7 %17 %28
%101 = OpCompositeConstruct %5 %29 %30
%102 = OpExtInst %7 %1 Ldexp %100 %101
%103 = OpExtInst %8 %1 ModfStruct %31
%42 = OpExtInst %6 %1 SSign %19
%45 = OpCompositeExtract %6 %20 0
%46 = OpCompositeExtract %6 %20 0
%47 = OpIMul %6 %45 %46
%48 = OpIAdd %6 %44 %47
%49 = OpCompositeExtract %6 %20 1
%50 = OpCompositeExtract %6 %20 1
%51 = OpIMul %6 %49 %50
%43 = OpIAdd %6 %48 %51
%52 = OpCopyObject %21 %22
%53 = OpExtInst %21 %1 FindUMsb %52
%54 = OpExtInst %6 %1 FindSMsb %19
%55 = OpCompositeConstruct %5 %19 %19
%56 = OpExtInst %5 %1 FindSMsb %55
%58 = OpCompositeConstruct %57 %23 %23
%59 = OpExtInst %57 %1 FindUMsb %58
%60 = OpExtInst %6 %1 FindILsb %19
%61 = OpExtInst %21 %1 FindILsb %23
%62 = OpCompositeConstruct %5 %19 %19
%63 = OpExtInst %5 %1 FindILsb %62
%64 = OpCompositeConstruct %57 %23 %23
%65 = OpExtInst %57 %1 FindILsb %64
%68 = OpExtInst %21 %1 FindILsb %22
%66 = OpExtInst %21 %1 UMin %67 %68
%70 = OpExtInst %6 %1 FindILsb %24
%69 = OpExtInst %6 %1 UMin %67 %70
%72 = OpExtInst %21 %1 FindILsb %25
%71 = OpExtInst %21 %1 UMin %67 %72
%74 = OpExtInst %6 %1 FindILsb %19
%73 = OpExtInst %6 %1 UMin %67 %74
%75 = OpCompositeConstruct %57 %22 %22
%78 = OpExtInst %57 %1 FindILsb %75
%76 = OpExtInst %57 %1 UMin %77 %78
%79 = OpCompositeConstruct %5 %24 %24
%81 = OpExtInst %5 %1 FindILsb %79
%80 = OpExtInst %5 %1 UMin %77 %81
%82 = OpCompositeConstruct %57 %23 %23
%84 = OpExtInst %57 %1 FindILsb %82
%83 = OpExtInst %57 %1 UMin %77 %84
%85 = OpCompositeConstruct %5 %26 %26
%87 = OpExtInst %5 %1 FindILsb %85
%86 = OpExtInst %5 %1 UMin %77 %87
%90 = OpExtInst %6 %1 FindUMsb %19
%88 = OpISub %6 %89 %90
%92 = OpExtInst %6 %1 FindUMsb %23
%91 = OpISub %21 %89 %92
%93 = OpCompositeConstruct %5 %19 %19
%96 = OpExtInst %5 %1 FindUMsb %93
%94 = OpISub %5 %95 %96
%97 = OpCompositeConstruct %57 %23 %23
%99 = OpExtInst %5 %1 FindUMsb %97
%98 = OpISub %57 %95 %99
%100 = OpExtInst %4 %1 Ldexp %17 %27
%101 = OpCompositeConstruct %7 %17 %28
%102 = OpCompositeConstruct %5 %29 %30
%103 = OpExtInst %7 %1 Ldexp %101 %102
%104 = OpExtInst %8 %1 ModfStruct %31
%105 = OpCompositeExtract %4 %104 0
%106 = OpExtInst %8 %1 ModfStruct %31
%107 = OpCompositeExtract %4 %106 1
%108 = OpCompositeConstruct %7 %31 %31
%109 = OpExtInst %9 %1 ModfStruct %108
%110 = OpCompositeConstruct %3 %31 %31 %31 %31
%111 = OpExtInst %10 %1 ModfStruct %110
%112 = OpCompositeExtract %3 %111 1
%113 = OpCompositeExtract %4 %112 0
%114 = OpCompositeConstruct %7 %31 %31
%115 = OpExtInst %9 %1 ModfStruct %114
%116 = OpCompositeExtract %7 %115 0
%117 = OpCompositeExtract %4 %116 1
%118 = OpExtInst %11 %1 FrexpStruct %31
%105 = OpExtInst %8 %1 ModfStruct %31
%106 = OpCompositeExtract %4 %105 0
%107 = OpExtInst %8 %1 ModfStruct %31
%108 = OpCompositeExtract %4 %107 1
%109 = OpCompositeConstruct %7 %31 %31
%110 = OpExtInst %9 %1 ModfStruct %109
%111 = OpCompositeConstruct %3 %31 %31 %31 %31
%112 = OpExtInst %10 %1 ModfStruct %111
%113 = OpCompositeExtract %3 %112 1
%114 = OpCompositeExtract %4 %113 0
%115 = OpCompositeConstruct %7 %31 %31
%116 = OpExtInst %9 %1 ModfStruct %115
%117 = OpCompositeExtract %7 %116 0
%118 = OpCompositeExtract %4 %117 1
%119 = OpExtInst %11 %1 FrexpStruct %31
%120 = OpCompositeExtract %4 %119 0
%121 = OpExtInst %11 %1 FrexpStruct %31
%122 = OpCompositeExtract %6 %121 1
%123 = OpCompositeConstruct %3 %31 %31 %31 %31
%124 = OpExtInst %13 %1 FrexpStruct %123
%125 = OpCompositeExtract %12 %124 1
%126 = OpCompositeExtract %6 %125 0
%120 = OpExtInst %11 %1 FrexpStruct %31
%121 = OpCompositeExtract %4 %120 0
%122 = OpExtInst %11 %1 FrexpStruct %31
%123 = OpCompositeExtract %6 %122 1
%124 = OpCompositeConstruct %3 %31 %31 %31 %31
%125 = OpExtInst %13 %1 FrexpStruct %124
%126 = OpCompositeExtract %12 %125 1
%127 = OpCompositeExtract %6 %126 0
OpReturn
OpFunctionEnd
1 change: 1 addition & 0 deletions tests/out/wgsl/math-functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fn main() {
let d = radians(v);
let e = saturate(v);
let g = refract(v, v, 1.0);
let h = sign(-1);
let const_dot = dot(vec2<i32>(), vec2<i32>());
let first_leading_bit_abs = firstLeadingBit(abs(0u));
let flb_a = firstLeadingBit(-1);
Expand Down

0 comments on commit 41d9b7e

Please sign in to comment.