Skip to content

Commit 1f92e70

Browse files
committed
Initial porting of arithmetic shaders to HLSL
1 parent 8f8899f commit 1f92e70

18 files changed

+1343
-314
lines changed

include/nbl/builtin/hlsl/atomics.hlsl

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (C) 2023 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_BUILTIN_HLSL_ATOMICS_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_ATOMICS_INCLUDED_
6+
7+
namespace nbl
8+
{
9+
namespace hlsl
10+
{
11+
namespace atomics
12+
{
13+
template<typename T>
14+
T atomicAdd(inout T mem, T data)
15+
{
16+
T orig;
17+
InterlockedAdd(mem, data, orig);
18+
return orig;
19+
}
20+
21+
template<typename T>
22+
T atomicAnd(inout T mem, T data)
23+
{
24+
T orig;
25+
InterlockedAnd(mem, data, orig);
26+
return orig;
27+
}
28+
29+
template<typename T>
30+
T atomicOr(inout T mem, T data)
31+
{
32+
T orig;
33+
InterlockedOr(mem, data, orig);
34+
return orig;
35+
}
36+
37+
template<typename T>
38+
T atomicXor(inout T mem, T data)
39+
{
40+
T orig;
41+
InterlockedXor(mem, data, orig);
42+
return orig;
43+
}
44+
45+
template<typename T>
46+
T atomicMin(inout T mem, T data)
47+
{
48+
T orig;
49+
InterlockedMin(mem, data, orig);
50+
return orig;
51+
}
52+
53+
template<typename T>
54+
T atomicMax(inout T mem, T data)
55+
{
56+
T orig;
57+
InterlockedMax(mem, data, orig);
58+
return orig;
59+
}
60+
61+
template<typename T>
62+
T atomicExchange(inout T mem, T data)
63+
{
64+
T orig;
65+
InterlockedExchange(mem, data, orig);
66+
return orig;
67+
}
68+
69+
template<typename T>
70+
T atomicCompSwap(inout T mem, T compare, T data)
71+
{
72+
T orig;
73+
InterlockedCompareExchange(mem, compare, data, orig);
74+
return orig;
75+
}
76+
}
77+
}
78+
}
79+
#endif

include/nbl/builtin/hlsl/binops.hlsl

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -80,88 +80,70 @@ struct mul
8080
}
8181
};
8282

83-
template<typename T, class Comparator>
84-
struct min
85-
{
86-
T operator()(const T lhs, const T rhs, inout Comparator comp)
87-
{
88-
return comp(lhs, rhs) ? lhs : rhs;
89-
}
90-
};
91-
9283
template<typename T>
93-
struct min
94-
{
95-
T operator()(const T lhs, const T rhs)
96-
{
97-
comparator_lt_t comp;
98-
return bitwise_min(lhs, rhs, comp);
99-
}
100-
101-
T identity()
102-
{
103-
return ~0;
104-
}
105-
};
106-
107-
template<typename T, class Comparator>
108-
struct max
84+
struct comparator_lt_t
10985
{
110-
T operator()(const T lhs, const T rhs, inout Comparator comp)
86+
bool operator()(const T lhs, const T rhs)
11187
{
112-
return comp(lhs, rhs) ? lhs : rhs;
88+
return lhs<rhs;
11389
}
11490
};
11591

11692
template<typename T>
117-
struct max
93+
struct comparator_gt_t
11894
{
119-
T operator()(const T lhs, const T rhs)
95+
bool operator()(const T lhs, const T rhs)
12096
{
121-
comparator_gt_t comp;
122-
return bitwise_max(lhs, rhs, comp);
97+
return lhs>rhs;
12398
}
124-
125-
T identity()
126-
{
127-
return 0; // REVIEW: This assumes T = unsigned but what if we got T = signed ?
128-
}
12999
};
130100

131101
template<typename T>
132-
struct comparator_lt_t
102+
struct comparator_lte_t
133103
{
134104
bool operator()(const T lhs, const T rhs)
135105
{
136-
return lhs<rhs;
106+
return lhs<=rhs;
137107
}
138108
};
139109

140110
template<typename T>
141-
struct comparator_gt_t
111+
struct comparator_gte_t
142112
{
143113
bool operator()(const T lhs, const T rhs)
144114
{
145-
return lhs>rhs;
115+
return lhs>=rhs;
146116
}
147117
};
148118

149119
template<typename T>
150-
struct comparator_lte_t
120+
struct min
151121
{
152-
bool operator()(const T lhs, const T rhs)
122+
T operator()(const T lhs, const T rhs)
153123
{
154-
return lhs<=rhs;
124+
comparator_lt_t<T> comp;
125+
return comp(lhs, rhs) ? lhs : rhs;
155126
}
127+
128+
T identity()
129+
{
130+
return ~0;
131+
}
156132
};
157133

158134
template<typename T>
159-
struct comparator_gte_t
135+
struct max
160136
{
161-
bool operator()(const T lhs, const T rhs)
137+
T operator()(const T lhs, const T rhs)
162138
{
163-
return lhs>=rhs;
139+
comparator_gt_t<T> comp;
140+
return comp(lhs, rhs) ? lhs : rhs;
164141
}
142+
143+
T identity()
144+
{
145+
return 0;
146+
}
165147
};
166148

167149
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
// choerent -> globallycoherent

include/nbl/builtin/hlsl/scan/virtual_workgroup.hlsl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
const uint gl_LocalInvocationIndex: SV_GroupIndex;
1313

14+
#if 0
1415
namespace nbl
1516
{
1617
namespace hlsl
@@ -24,20 +25,21 @@ namespace scan
2425
const Parameters_t params = getParameters();
2526
const uint levelInvocationIndex = localWorkgroupIndex * _NBL_HLSL_WORKGROUP_SIZE_ + gl_LocalInvocationIndex;
2627
const bool lastInvocationInGroup = gl_LocalInvocationIndex == (_NBL_HLSL_WORKGROUP_SIZE_ - 1);
27-
28+
2829
const uint lastLevel = params.topLevel << 1u;
2930
const uint pseudoLevel = levelInvocationIndex <= params.lastElement[pseudoLevel];
30-
31+
3132
const bool inRange = levelInvocationIndex <= params.lastElement[pseudoLevel];
32-
33+
3334
Storage_t data = binop.identity();
3435
if(inRange)
3536
{
3637
getData(data, levelInvocationIndex, localWorkgroupIndex, treeLevel, pseudoLevel);
3738
}
38-
39+
3940
if(treeLevel < params.topLevel)
4041
{
42+
#error "Must also define some scratch accessor when calling operation()"
4143
data = workgroup::reduction<binop>()(data);
4244
}
4345
// REVIEW: missing _TYPE_ check and extra case here
@@ -85,6 +87,8 @@ namespace scan
8587
}
8688
}
8789
}
90+
#endif
91+
8892
#define _NBL_HLSL_SCAN_MAIN_DEFINED_
8993
#endif
9094

0 commit comments

Comments
 (0)