-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaders.metal
More file actions
368 lines (303 loc) · 14 KB
/
Shaders.metal
File metadata and controls
368 lines (303 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <metal_stdlib>
using namespace metal;
// Simple Weyl hash function
// Source: https://gist.github.com/Marc-B-Reynolds/5a939f71fc7237c7af63
uint hash2(uint2 c)
{
c.x *= 0x3504f333;
c.y *= 0xf1bbcdcb;
c.x ^= c.y;
c.x *= 741103597;
return c.x;
}
// Modified from the above. Only single channel
uint hash1(uint c)
{
c *= 0x3504f333;
return c;
}
constexpr constant uint BUFFER_THREAD_GROUP_SIZE = 256;
constexpr constant uint TEXTURE_THREAD_GROUP_SIZE = 16;
enum class LoadType { Invariant, Linear, Random, RandomLarge };
struct LoadConstants {
uint elementsMask; // Runtime address mask. Needed to prevent compiler combining narrow raw buffer loads from single thread.
uint writeIndex; // Runtime write mask. Always 0xffffffff (= never write). But the compiler doesn't know this :)
};
struct LoadConstantsWithArray {
uint elementsMask; // Runtime address mask. Needed to prevent compiler combining narrow raw buffer loads from single thread.
uint writeIndex; // Runtime write mask. Always 0xffffffff (= never write). But the compiler doesn't know this :)
float4 benchmarkArray[1024];
};
template <LoadType Load>
uint getIndex(uint lid) {
switch (Load) {
case LoadType::Invariant:
// All threads load from same address. Index is wave invariant.
return 0;
case LoadType::Linear:
// Linearly increasing starting address to allow memory coalescing
return lid;
case LoadType::Random:
// Randomize start address offset (0-15) to prevent memory coalescing
return (hash1(lid) & 0xf);
case LoadType::RandomLarge:
// Randomize start address offset (0-63) to prevent memory coalescing
return hash1(lid) & 0x3f;
}
}
template <LoadType Load>
uint2 getIndex(uint2 lid) {
switch (Load) {
case LoadType::Invariant:
// All threads load from same address. Index is wave invariant.
return 0;
case LoadType::Linear:
// Linearly increasing starting address to allow memory coalescing
return lid;
case LoadType::Random:
// Randomize start address offset (0-3, 0-3) to prevent memory coalescing
return uint2(hash1(lid.x) & 3, hash1(lid.y) & 3);
case LoadType::RandomLarge:
// Randomize start address offset (0-7, 0-7) to prevent memory coalescing
return uint2(hash1(lid.x) & 7, hash1(lid.y) & 7);
}
}
float4 expand(vec<float, 1> val) { return val.xxxx; }
float4 expand(vec<float, 2> val) { return val.xyxy; }
float4 expand(vec<float, 3> val) { return val.xyzx; }
float4 expand(vec<float, 4> val) { return val.xyzw; }
float4 expand(packed_vec<float, 1> val) { return val.xxxx; }
float4 expand(packed_vec<float, 2> val) { return val.xyxy; }
float4 expand(packed_vec<float, 3> val) { return val.xyzx; }
float4 expand(packed_vec<float, 4> val) { return val.xyzw; }
half4 expand(vec<half, 1> val) { return val.xxxx; }
half4 expand(vec<half, 2> val) { return val.xyxy; }
half4 expand(vec<half, 3> val) { return val.xyzx; }
half4 expand(vec<half, 4> val) { return val.xyzw; }
template <LoadType Load> kernel void loadConstants(
uint2 gid [[thread_position_in_grid]],
uint lid [[thread_index_in_threadgroup]],
constant LoadConstantsWithArray& constants [[buffer(0)]],
device float* output [[buffer(1)]])
{
threadgroup float dummyLDS[BUFFER_THREAD_GROUP_SIZE];
float4 value = 0;
uint htid = getIndex<Load>(lid);
for (int i = 0; i < 256; ++i) {
// Mask with runtime constant to prevent unwanted compiler optimizations
uint elemIdx = (htid + i) | constants.elementsMask;
value += constants.benchmarkArray[elemIdx].xyzw;
}
// Linear write to LDS (no bank conflicts). Significantly faster than memory loads.
dummyLDS[lid] = value.x + value.y + value.z + value.w;
threadgroup_barrier(mem_flags::mem_threadgroup);
if (constants.writeIndex != 0xffffffff) {
output[gid.x + gid.y] = dummyLDS[constants.writeIndex];
}
}
template <LoadType Load, typename T> kernel void loadConstantBuffer(
uint2 gid [[thread_position_in_grid]],
uint lid [[thread_index_in_threadgroup]],
constant LoadConstants& constants [[buffer(0)]],
device float* output [[buffer(1)]],
constant T* buffer [[buffer(2)]])
{
threadgroup float dummyLDS[BUFFER_THREAD_GROUP_SIZE];
float4 value = 0;
uint htid = getIndex<Load>(lid);
for (int i = 0; i < 256; ++i) {
// Mask with runtime constant to prevent unwanted compiler optimizations
uint elemIdx = (htid + i) | constants.elementsMask;
value += expand(buffer[elemIdx]);
}
// Linear write to LDS (no bank conflicts). Significantly faster than memory loads.
dummyLDS[lid] = value.x + value.y + value.z + value.w;
threadgroup_barrier(mem_flags::mem_threadgroup);
if (constants.writeIndex != 0xffffffff) {
output[gid.x + gid.y] = dummyLDS[constants.writeIndex];
}
}
template <LoadType Load, typename T, typename U> kernel void loadBuffer(
uint2 gid [[thread_position_in_grid]],
uint lid [[thread_index_in_threadgroup]],
constant LoadConstants& constants [[buffer(0)]],
device U* output [[buffer(1)]],
device const T* buffer [[buffer(2)]])
{
threadgroup U dummyLDS[BUFFER_THREAD_GROUP_SIZE];
vec<U, 4> value = 0;
uint htid = getIndex<Load>(lid);
for (int i = 0; i < 256; ++i) {
// Mask with runtime constant to prevent unwanted compiler optimizations
uint elemIdx = (htid + i) | constants.elementsMask;
value += expand(buffer[elemIdx]);
}
// Linear write to LDS (no bank conflicts). Significantly faster than memory loads.
dummyLDS[lid] = value.x + value.y + value.z + value.w;
threadgroup_barrier(mem_flags::mem_threadgroup);
if (constants.writeIndex != 0xffffffff) {
output[gid.x + gid.y] = dummyLDS[constants.writeIndex];
}
}
template <LoadType Load, uint Size, typename T> kernel void loadTextureBuffer(
uint2 gid [[thread_position_in_grid]],
uint lid [[thread_index_in_threadgroup]],
constant LoadConstants& constants [[buffer(0)]],
device T* output [[buffer(1)]],
texture_buffer<T, access::read> texture [[texture(0)]])
{
threadgroup T dummyLDS[BUFFER_THREAD_GROUP_SIZE];
vec<T, 4> value = 0;
uint htid = getIndex<Load>(lid);
for (int i = 0; i < 256; ++i) {
// Mask with runtime constant to prevent unwanted compiler optimizations
uint elemIdx = (htid + i) | constants.elementsMask;
if (Size == 1)
value += texture.read(elemIdx).xxxx;
else if (Size == 2)
value += texture.read(elemIdx).xyxy;
else
value += texture.read(elemIdx).xyzw;
}
// Linear write to LDS (no bank conflicts). Significantly faster than memory loads.
dummyLDS[lid] = value.x + value.y + value.z + value.w;
threadgroup_barrier(mem_flags::mem_threadgroup);
if (constants.writeIndex != 0xffffffff) {
output[gid.x + gid.y] = dummyLDS[constants.writeIndex];
}
}
template <LoadType Load, uint Size, typename T> kernel void loadTexture(
uint2 gid [[thread_position_in_grid]],
uint2 lid [[thread_position_in_threadgroup]],
constant LoadConstants& constants [[buffer(0)]],
device T* output [[buffer(1)]],
texture2d<T, access::read> texture [[texture(0)]])
{
threadgroup T dummyLDS[TEXTURE_THREAD_GROUP_SIZE][TEXTURE_THREAD_GROUP_SIZE];
vec<T, 4> value = 0;
uint2 htid = getIndex<Load>(lid);
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 16; ++j) {
// Mask with runtime constant to prevent unwanted compiler optimizations
uint2 elemIdx = (htid + uint2(i, j)) | constants.elementsMask;
if (Size == 1)
value += texture.read(elemIdx).xxxx;
else if (Size == 2)
value += texture.read(elemIdx).xyxy;
else
value += texture.read(elemIdx).xyzw;
}
}
// Linear write to LDS (no bank conflicts). Significantly faster than memory loads.
dummyLDS[lid.y][lid.x] = value.x + value.y + value.z + value.w;
threadgroup_barrier(mem_flags::mem_threadgroup);
if (constants.writeIndex != 0xffffffff) {
output[gid.x + gid.y] = dummyLDS[(constants.writeIndex >> 8) & 0xff][constants.writeIndex & 0xff];
}
}
template <LoadType Load, uint Size, typename T> kernel void sampleTexture(
uint2 gid [[thread_position_in_grid]],
uint2 lid [[thread_position_in_threadgroup]],
constant LoadConstants& constants [[buffer(0)]],
device T* output [[buffer(1)]],
texture2d<T, access::sample> texture [[texture(0)]],
sampler samp [[sampler(0)]])
{
threadgroup T dummyLDS[TEXTURE_THREAD_GROUP_SIZE][TEXTURE_THREAD_GROUP_SIZE];
vec<T, 4> value = 0;
uint2 htid = getIndex<Load>(lid);
const float2 invTextureDims = 1.0f / float2(32.0f, 32.0f);
const float2 texCenter = invTextureDims * 0.5;
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 16; ++j) {
// Mask with runtime constant to prevent unwanted compiler optimizations
uint2 elemIdx = (htid + uint2(i, j)) | constants.elementsMask;
float2 uv = float2(elemIdx) * invTextureDims + texCenter;
if (Size == 1)
value += texture.sample(samp, uv).xxxx;
else if (Size == 2)
value += texture.sample(samp, uv).xyxy;
else
value += texture.sample(samp, uv).xyzw;
}
}
// Linear write to LDS (no bank conflicts). Significantly faster than memory loads.
dummyLDS[lid.y][lid.x] = value.x + value.y + value.z + value.w;
threadgroup_barrier(mem_flags::mem_threadgroup);
if (constants.writeIndex != 0xffffffff) {
output[gid.x + gid.y] = dummyLDS[(constants.writeIndex >> 8) & 0xff][constants.writeIndex & 0xff];
}
}
template <LoadType Load, typename T> kernel void gatherTexture(
uint2 gid [[thread_position_in_grid]],
uint2 lid [[thread_position_in_threadgroup]],
constant LoadConstants& constants [[buffer(0)]],
device T* output [[buffer(1)]],
texture2d<T, access::sample> texture [[texture(0)]],
sampler samp [[sampler(0)]])
{
threadgroup T dummyLDS[TEXTURE_THREAD_GROUP_SIZE][TEXTURE_THREAD_GROUP_SIZE];
vec<T, 4> value = 0;
uint2 htid = getIndex<Load>(lid);
const float2 invTextureDims = 1.0f / float2(32.0f, 32.0f);
const float2 texCenter = invTextureDims * 0.5;
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 16; ++j) {
// Mask with runtime constant to prevent unwanted compiler optimizations
uint2 elemIdx = (htid + uint2(i, j)) | constants.elementsMask;
float2 uv = float2(elemIdx) * invTextureDims + texCenter;
value += texture.gather(samp, uv);
}
}
// Linear write to LDS (no bank conflicts). Significantly faster than memory loads.
dummyLDS[lid.y][lid.x] = value.x + value.y + value.z + value.w;
threadgroup_barrier(mem_flags::mem_threadgroup);
if (constants.writeIndex != 0xffffffff) {
output[gid.x + gid.y] = dummyLDS[(constants.writeIndex >> 8) & 0xff][constants.writeIndex & 0xff];
}
}
#define TEMPLATE_ALL_LOADS(macro, name, ...) \
macro(name "Uniform", LoadType::Invariant, ##__VA_ARGS__) \
macro(name "Linear", LoadType::Linear, ##__VA_ARGS__) \
macro(name "Random", LoadType::Random, ##__VA_ARGS__) \
macro(name "RandomLarge", LoadType::RandomLarge, ##__VA_ARGS__) \
#define TEMPLATE_124(macro, name, ...) \
TEMPLATE_ALL_LOADS(macro, name "1d", 1, ##__VA_ARGS__) \
TEMPLATE_ALL_LOADS(macro, name "2d", 2, ##__VA_ARGS__) \
TEMPLATE_ALL_LOADS(macro, name "4d", 4, ##__VA_ARGS__) \
#define TEMPLATE_1234(macro, name, ...) \
TEMPLATE_ALL_LOADS(macro, name "1d", 1, ##__VA_ARGS__) \
TEMPLATE_ALL_LOADS(macro, name "2d", 2, ##__VA_ARGS__) \
TEMPLATE_ALL_LOADS(macro, name "3d", 3, ##__VA_ARGS__) \
TEMPLATE_ALL_LOADS(macro, name "4d", 4, ##__VA_ARGS__) \
#define TEMPLATE_CONSTANT(name, type) \
template [[host_name(name)]] kernel void loadConstants<type>(uint2, uint, constant LoadConstantsWithArray&, device float*);
#define TEMPLATE_BUFFER(name, type, size, fn, loadtype, qualifier, buffer_type) \
template [[host_name(name)]] kernel void fn<type, buffer_type<float, size>>(uint2, uint, constant LoadConstants&, device loadtype*, qualifier buffer_type<float, size>*);
#define TEMPLATE_UNORM_BUFFER(name, type, fn, loadtype, qualifier, buffer_type) \
template [[host_name(name)]] kernel void fn<type, buffer_type>(uint2, uint, constant LoadConstants&, device loadtype*, qualifier buffer_type*);
#define TEMPLATE_GATHER(name, type, fn, loadtype) \
template [[host_name(name)]] kernel void fn<type, loadtype>(uint2, uint2, constant LoadConstants&, device loadtype*, texture2d<loadtype, access::sample>, sampler);
#define TEMPLATE_OTHER(name, type, size, fn, loadtype, lid, ...) \
template [[host_name(name)]] kernel void fn<type, size, loadtype>(uint2, lid, constant LoadConstants&, device loadtype*, ##__VA_ARGS__);
using half1 = vec<half, 1>;
TEMPLATE_ALL_LOADS(TEMPLATE_CONSTANT, "loadConstant")
TEMPLATE_ALL_LOADS(TEMPLATE_UNORM_BUFFER, "loadUnormBuffer2d", loadBuffer, float, device const, rg8unorm<float2>)
TEMPLATE_ALL_LOADS(TEMPLATE_UNORM_BUFFER, "loadUnormBuffer4d", loadBuffer, float, device const, rgba8unorm<float4>)
TEMPLATE_ALL_LOADS(TEMPLATE_UNORM_BUFFER, "loadUnormHalfBuffer2d", loadBuffer, half, device const, rg8unorm<half2>)
TEMPLATE_ALL_LOADS(TEMPLATE_UNORM_BUFFER, "loadUnormHalfBuffer4d", loadBuffer, half, device const, rgba8unorm<half4>)
TEMPLATE_ALL_LOADS(TEMPLATE_UNORM_BUFFER, "loadHalfBuffer1d", loadBuffer, half, device const, half1)
TEMPLATE_ALL_LOADS(TEMPLATE_UNORM_BUFFER, "loadHalfBuffer2d", loadBuffer, half, device const, half2)
TEMPLATE_ALL_LOADS(TEMPLATE_UNORM_BUFFER, "loadHalfBuffer4d", loadBuffer, half, device const, half4)
TEMPLATE_124(TEMPLATE_BUFFER, "loadBuffer", loadBuffer, float, device const, vec)
TEMPLATE_1234(TEMPLATE_BUFFER, "loadPackedBuffer", loadBuffer, float, device const, packed_vec)
TEMPLATE_124(TEMPLATE_BUFFER, "loadConstantBuffer", loadConstantBuffer, float, constant, vec)
TEMPLATE_1234(TEMPLATE_BUFFER, "loadConstantPackedBuffer", loadConstantBuffer, float, constant, packed_vec)
TEMPLATE_124(TEMPLATE_OTHER, "loadTextureBuffer", loadTextureBuffer, float, uint, texture_buffer<float, access::read>)
TEMPLATE_124(TEMPLATE_OTHER, "loadTexture", loadTexture, float, uint2, texture2d<float, access::read>)
TEMPLATE_124(TEMPLATE_OTHER, "sampleTexture", sampleTexture, float, uint2, texture2d<float, access::sample>, sampler)
TEMPLATE_ALL_LOADS(TEMPLATE_GATHER, "gatherTexture", gatherTexture, float)
TEMPLATE_124(TEMPLATE_OTHER, "loadTextureBufferHalf", loadTextureBuffer, half, uint, texture_buffer<half, access::read>)
TEMPLATE_124(TEMPLATE_OTHER, "loadTextureHalf", loadTexture, half, uint2, texture2d<half, access::read>)
TEMPLATE_124(TEMPLATE_OTHER, "sampleTextureHalf", sampleTexture, half, uint2, texture2d<half, access::sample>, sampler)
TEMPLATE_ALL_LOADS(TEMPLATE_GATHER, "gatherTextureHalf", gatherTexture, half)