Skip to content

Commit a443350

Browse files
authored
Allow implicitly casting enum types to bool. (shader-slang#4739)
* Allow implicitly casting enum types to bool. * Fix.
1 parent 7ea47f9 commit a443350

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

source/slang/core.meta.slang

+8
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,14 @@ extension bool : IRangedValue
751751
__intrinsic_op($(kIROp_CastPtrToBool))
752752
__init(Ptr<T> ptr);
753753

754+
__generic<T : __EnumType>
755+
__implicit_conversion($(kConversionCost_IntegerTruncate))
756+
[__unsafeForceInlineEarly]
757+
__init(T v)
758+
{
759+
return __slang_noop_cast<T.__Tag>(v) != __intCast<T.__Tag>(0);
760+
}
761+
754762
static const bool maxValue = true;
755763
static const bool minValue = false;
756764
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Test enums can be implicitly casted to bool.
2+
3+
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
4+
5+
[Flags]
6+
enum BitFlags
7+
{
8+
One,
9+
Two,
10+
Three
11+
}
12+
13+
int test<let n : bool>()
14+
{
15+
return n;
16+
}
17+
18+
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
19+
RWStructuredBuffer<int> outputBuffer;
20+
21+
[numthreads(4, 1, 1)]
22+
void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
23+
{
24+
BitFlags x = BitFlags.One | BitFlags.Two;
25+
if (x & BitFlags.One)
26+
outputBuffer[0] = 1;
27+
else
28+
outputBuffer[0] = 0;
29+
if (x & BitFlags.Three)
30+
outputBuffer[1] = 1;
31+
else
32+
outputBuffer[1] = 0;
33+
if (x & BitFlags.Two)
34+
outputBuffer[2] = 1;
35+
else
36+
outputBuffer[2] = 0;
37+
38+
outputBuffer[3] = test<BitFlags.One & BitFlags.One>();
39+
// CHECK: 1
40+
// CHECK: 0
41+
// CHECK: 1
42+
// CHECK: 1
43+
}

0 commit comments

Comments
 (0)