From 41d9b7ef9613b8277a281f87f929cdba98eb37ab Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Fri, 1 Sep 2023 14:20:14 +0200 Subject: [PATCH] [wgsl-in] Allow sign() to take int argument --- src/valid/expression.rs | 17 +- tests/in/math-functions.wgsl | 1 + .../glsl/math-functions.main.Fragment.glsl | 5 +- tests/out/hlsl/math-functions.hlsl | 5 +- tests/out/msl/math-functions.msl | 13 +- tests/out/spv/math-functions.spvasm | 181 +++++++++--------- tests/out/wgsl/math-functions.wgsl | 1 + 7 files changed, 122 insertions(+), 101 deletions(-) diff --git a/src/valid/expression.rs b/src/valid/expression.rs index 548b6c1451..ddef1aaea0 100644 --- a/src/valid/expression.rs +++ b/src/valid/expression.rs @@ -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() { @@ -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, diff --git a/tests/in/math-functions.wgsl b/tests/in/math-functions.wgsl index 408f8a74f8..6e7bccf7f6 100644 --- a/tests/in/math-functions.wgsl +++ b/tests/in/math-functions.wgsl @@ -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(), vec2()); let first_leading_bit_abs = firstLeadingBit(abs(0u)); let flb_a = firstLeadingBit(-1); diff --git a/tests/out/glsl/math-functions.main.Fragment.glsl b/tests/out/glsl/math-functions.main.Fragment.glsl index be81715ce1..595235bdd1 100644 --- a/tests/out/glsl/math-functions.main.Fragment.glsl +++ b/tests/out/glsl/math-functions.main.Fragment.glsl @@ -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); @@ -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)); diff --git a/tests/out/hlsl/math-functions.hlsl b/tests/out/hlsl/math-functions.hlsl index afdc6f4671..6a2e4e6787 100644 --- a/tests/out/hlsl/math-functions.hlsl +++ b/tests/out/hlsl/math-functions.hlsl @@ -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)); @@ -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)); diff --git a/tests/out/msl/math-functions.msl b/tests/out/msl/math-functions.msl index 14824ec30f..8a885374e9 100644 --- a/tests/out/msl/math-functions.msl +++ b/tests/out/msl/math-functions.msl @@ -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); diff --git a/tests/out/spv/math-functions.spvasm b/tests/out/spv/math-functions.spvasm index 260c3b4bd4..50fcc08a93 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: 127 +; Bound: 128 OpCapability Shader %1 = OpExtInstImport "GLSL.std.450" OpMemoryModel Logical GLSL450 @@ -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 @@ -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 @@ -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 \ No newline at end of file diff --git a/tests/out/wgsl/math-functions.wgsl b/tests/out/wgsl/math-functions.wgsl index 149ebff8e0..488eadf3e9 100644 --- a/tests/out/wgsl/math-functions.wgsl +++ b/tests/out/wgsl/math-functions.wgsl @@ -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(), vec2()); let first_leading_bit_abs = firstLeadingBit(abs(0u)); let flb_a = firstLeadingBit(-1);