From 4d217907665180537ea217f9f2213c9d53a22b99 Mon Sep 17 00:00:00 2001 From: Jay Kwak <82421531+jkwak-work@users.noreply.github.com> Date: Tue, 23 Jan 2024 16:57:44 -0800 Subject: Fix incorrect behavior of operator% (#3470) * Fix incorrect behavior of operator% Fixes #1059. This change fixes the incorrect translation of "operator%" from HLSL to SPIRV. The issue stems from the fact that the behavior of "operator%" in GLSL differs from that in HLSL. In HLSL it behaves as "remainder" where as it behaves as "modulus" in GLSL. We have been using SpvOpFMod for operator% when Slang compiles from HLSL to SPRIV, which is incorrect. This change switches it to SpvOpFRem. The tests are slightly modified to reveal any potential issues. * Change output type of test/compute/frem For testing the operator%, we are using "int" as the output type of the test, "test/compute/frem.slang". Since the operands are in float type, it is more preferable to have a float type as the resulting type. This can be done with an option, "-output-using-type". --------- Co-authored-by: Yong He --- source/slang/slang-emit-spirv.cpp | 2 +- tests/compute/frem.slang | 12 ++++++------ tests/compute/frem.slang.2.expected.txt | 9 +++++---- tests/compute/frem.slang.expected.txt | 9 +++++---- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp index a600d4b10..46ce1d0b5 100644 --- a/source/slang/slang-emit-spirv.cpp +++ b/source/slang/slang-emit-spirv.cpp @@ -4398,7 +4398,7 @@ struct SPIRVEmitContext opCode = isSigned ? SpvOpSRem : SpvOpUMod; break; case kIROp_FRem: - opCode = SpvOpFMod; + opCode = SpvOpFRem; break; case kIROp_Less: opCode = isFloatingPoint ? SpvOpFOrdLessThan diff --git a/tests/compute/frem.slang b/tests/compute/frem.slang index 7890b9c9a..893f29794 100644 --- a/tests/compute/frem.slang +++ b/tests/compute/frem.slang @@ -1,13 +1,13 @@ // frem.slang -//TEST(compute):COMPARE_COMPUTE: -shaderobj -//TEST(compute):COMPARE_COMPUTE:-cpu -shaderobj -//TEST(compute):COMPARE_COMPUTE:-vk -shaderobj +//TEST(compute):COMPARE_COMPUTE: -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE:-cpu -shaderobj -output-using-type +//TEST(compute):COMPARE_COMPUTE:-vk -shaderobj -emit-spirv-directly -output-using-type // Test uses of floating-point `%` operator. RWStructuredBuffer gInput; -//TEST_INPUT:ubuffer(data=[2.0 1.0 5.0 2.0 2.0 -4.0 -5.0 2.0], stride=4):name=gInput +//TEST_INPUT:ubuffer(data=[2.0 1.0 5.0 2.0 1.0 -4.0 -5.0 4.0], stride=4):name=gInput int test(int inVal) { @@ -17,7 +17,7 @@ int test(int inVal) } //TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer -RWStructuredBuffer outputBuffer; +RWStructuredBuffer outputBuffer; [numthreads(4, 1, 1)] void computeMain(int3 dispatchThreadID : SV_DispatchThreadID) @@ -26,4 +26,4 @@ void computeMain(int3 dispatchThreadID : SV_DispatchThreadID) int inVal = tid; int outVal = test(inVal); outputBuffer[tid] = outVal; -} \ No newline at end of file +} diff --git a/tests/compute/frem.slang.2.expected.txt b/tests/compute/frem.slang.2.expected.txt index f22b9c805..15ca0db0b 100644 --- a/tests/compute/frem.slang.2.expected.txt +++ b/tests/compute/frem.slang.2.expected.txt @@ -1,4 +1,5 @@ -0 -1 -FFFFFFFE -1 +type: float +0.000000 +1.000000 +1.000000 +-1.000000 diff --git a/tests/compute/frem.slang.expected.txt b/tests/compute/frem.slang.expected.txt index bafada4d1..15ca0db0b 100644 --- a/tests/compute/frem.slang.expected.txt +++ b/tests/compute/frem.slang.expected.txt @@ -1,4 +1,5 @@ -0 -1 -2 -FFFFFFFF +type: float +0.000000 +1.000000 +1.000000 +-1.000000 -- cgit v1.2.3