Skip to content

Commit 4d21790

Browse files
jkwak-workcsyonghe
andauthored
Fix incorrect behavior of operator% (shader-slang#3470)
* Fix incorrect behavior of operator% Fixes shader-slang#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 <yonghe@outlook.com>
1 parent af91e77 commit 4d21790

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

source/slang/slang-emit-spirv.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4398,7 +4398,7 @@ struct SPIRVEmitContext
43984398
opCode = isSigned ? SpvOpSRem : SpvOpUMod;
43994399
break;
44004400
case kIROp_FRem:
4401-
opCode = SpvOpFMod;
4401+
opCode = SpvOpFRem;
44024402
break;
44034403
case kIROp_Less:
44044404
opCode = isFloatingPoint ? SpvOpFOrdLessThan

tests/compute/frem.slang

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// frem.slang
22

3-
//TEST(compute):COMPARE_COMPUTE: -shaderobj
4-
//TEST(compute):COMPARE_COMPUTE:-cpu -shaderobj
5-
//TEST(compute):COMPARE_COMPUTE:-vk -shaderobj
3+
//TEST(compute):COMPARE_COMPUTE: -shaderobj -output-using-type
4+
//TEST(compute):COMPARE_COMPUTE:-cpu -shaderobj -output-using-type
5+
//TEST(compute):COMPARE_COMPUTE:-vk -shaderobj -emit-spirv-directly -output-using-type
66

77
// Test uses of floating-point `%` operator.
88

99
RWStructuredBuffer<float> gInput;
10-
//TEST_INPUT:ubuffer(data=[2.0 1.0 5.0 2.0 2.0 -4.0 -5.0 2.0], stride=4):name=gInput
10+
//TEST_INPUT:ubuffer(data=[2.0 1.0 5.0 2.0 1.0 -4.0 -5.0 4.0], stride=4):name=gInput
1111

1212
int test(int inVal)
1313
{
@@ -17,7 +17,7 @@ int test(int inVal)
1717
}
1818

1919
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
20-
RWStructuredBuffer<int> outputBuffer;
20+
RWStructuredBuffer<float> outputBuffer;
2121

2222
[numthreads(4, 1, 1)]
2323
void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
@@ -26,4 +26,4 @@ void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
2626
int inVal = tid;
2727
int outVal = test(inVal);
2828
outputBuffer[tid] = outVal;
29-
}
29+
}
+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
0
2-
1
3-
FFFFFFFE
4-
1
1+
type: float
2+
0.000000
3+
1.000000
4+
1.000000
5+
-1.000000

tests/compute/frem.slang.expected.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
0
2-
1
3-
2
4-
FFFFFFFF
1+
type: float
2+
0.000000
3+
1.000000
4+
1.000000
5+
-1.000000

0 commit comments

Comments
 (0)