Skip to content

Commit d1a8cd2

Browse files
author
Tim Foley
authored
Add != operator for enum types (shader-slang#1394)
This was an oversight in the stdlib, and the `!=` definition follows the `==` in a straightforward fashion.
1 parent cd7f01b commit d1a8cd2

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

source/slang/core.meta.slang

+4
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,10 @@ __generic<E : __EnumType>
18081808
__intrinsic_op($(kIROp_Eql))
18091809
bool operator==(E left, E right);
18101810

1811+
__generic<E : __EnumType>
1812+
__intrinsic_op($(kIROp_Neq))
1813+
bool operator!=(E left, E right);
1814+
18111815
// Binding Attributes
18121816

18131817
__attributeTarget(DeclBase)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// enum-equality.slang
2+
3+
// Test that equality (and inequality) of `enum`
4+
// types works as expected.
5+
6+
//TEST(compute):COMPARE_COMPUTE:
7+
8+
enum Channel
9+
{
10+
Red,
11+
Green,
12+
Blue,
13+
Alpha,
14+
}
15+
16+
int test(int val)
17+
{
18+
Channel channel= Channel(val);
19+
20+
int result = 0;
21+
if(channel == Channel.Red) result += 1;
22+
if(channel != Channel.Green) result += 16;
23+
if(channel == Channel.Blue) result += 16*16;
24+
if(channel != Channel.Alpha) result += 16*16*16;
25+
26+
return result;
27+
}
28+
29+
30+
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
31+
RWStructuredBuffer<int> outputBuffer;
32+
33+
[numthreads(4, 1, 1)]
34+
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
35+
{
36+
uint tid = dispatchThreadID.x;
37+
int inVal = tid;
38+
int outVal = test(inVal);
39+
outputBuffer[tid] = outVal;
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1011
2+
1000
3+
1110
4+
10

0 commit comments

Comments
 (0)