File: IntelExtensions.hlsl

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (460 lines) | stat: -rw-r--r-- 14,457 bytes parent folder | download | duplicates (3)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*========================== begin_copyright_notice ============================

Copyright (C) 2019-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

//
// Intel extension buffer structure, generic interface for
//   0-operand extensions (e.g. sync opcodes)
//   1-operand unary operations (e.g. render target writes)
//   2-operand binary operations (future extensions)
//   3-operand ternary operations (future extensions)
//
struct IntelExtensionStruct
{
    uint   opcode;  // opcode to execute
    uint   rid;     // resource ID
    uint   sid;     // sampler ID

    float4 src0f;   // float source operand  0
    float4 src1f;   // float source operand  0
    float4 src2f;   // float source operand  0
    float4 dst0f;   // float destination operand

    uint4  src0u;
    uint4  src1u;
    uint4  src2u;
    uint4  dst0u;

    float  pad[181]; // total lenght 864
};

//
// extension opcodes
//
#define INTEL_EXT_WAVE_SHUFFLE              17
#define INTEL_EXT_WAVE_LANEINDEX            18
#define INTEL_EXT_WAVE_WAVEACTIVEBALLOT     19
#define INTEL_EXT_WAVE_WAVEPREFIXOP         20
#define INTEL_EXT_WAVE_WAVEALL              21
#define INTEL_EXT_SIMDSIZE                  22


// Define RW buffer for Intel extensions.
// Application should bind null resource, operations will be ignored.
// If application needs to use slot other than u63, it needs to
// define INTEL_SHADER_EXT_UAV_SLOT as a unused slot. This should be
// defined before including this file in shader as:
// #define INTEL_SHADER_EXT_UAV_SLOT u8

#ifdef INTEL_SHADER_EXT_UAV_SLOT
RWStructuredBuffer<IntelExtensionStruct> g_IntelExt : register( INTEL_SHADER_EXT_UAV_SLOT );
#else
RWStructuredBuffer<IntelExtensionStruct> g_IntelExt : register( u63 );
#endif


//
// Initialize Intel HSLS Extensions
// This method should be called before any other extension function
//
void IntelExt_Init()
{
    uint4 init = { 0x63746e69, 0x6c736c68, 0x6e747865, 0x0 }; // intc hlsl extn
    g_IntelExt[0].src0u = init;
}

// Extension matching DirectX12 Wave instructions
// DX12 behavior is described here:
// https://github.com/Microsoft/DirectXShaderCompiler/wiki/Wave-Intrinsics
#define INTEL_EXT_WAVEOPS_SUM     0
#define INTEL_EXT_WAVEOPS_PROD    1
#define INTEL_EXT_WAVEOPS_UMIN    2
#define INTEL_EXT_WAVEOPS_UMAX    3
#define INTEL_EXT_WAVEOPS_IMIN    4
#define INTEL_EXT_WAVEOPS_IMAX    5
#define INTEL_EXT_WAVEOPS_OR      6
#define INTEL_EXT_WAVEOPS_XOR     7
#define INTEL_EXT_WAVEOPS_AND     8
#define INTEL_EXT_WAVEOPS_FSUM    9
#define INTEL_EXT_WAVEOPS_FPROD   10
#define INTEL_EXT_WAVEOPS_FMIN    11
#define INTEL_EXT_WAVEOPS_FMAX    12

#define INTEL_EXT_UINT64_ATOMIC      24

#define INTEL_EXT_ATOMIC_ADD          0
#define INTEL_EXT_ATOMIC_MIN          1
#define INTEL_EXT_ATOMIC_MAX          2
#define INTEL_EXT_ATOMIC_CMPXCHG      3
#define INTEL_EXT_ATOMIC_XCHG         4
#define INTEL_EXT_ATOMIC_AND          5
#define INTEL_EXT_ATOMIC_OR           6
#define INTEL_EXT_ATOMIC_XOR          7

float IntelExt_WaveReadLaneAt(float input, uint lane)
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_WAVE_SHUFFLE;
    g_IntelExt[opcode].src0f.x = input;
    g_IntelExt[opcode].src1u.x = lane;
    return g_IntelExt[opcode].dst0f.x;
}

int IntelExt_WaveReadLaneAt(int input, uint lane)
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_WAVE_SHUFFLE;
    g_IntelExt[opcode].src0u.x = input;
    g_IntelExt[opcode].src1u.x = lane;
    return g_IntelExt[opcode].dst0u.x;
}

uint IntelExt_WaveGetLaneIndex()
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_WAVE_LANEINDEX;
    return g_IntelExt[opcode].dst0u.x;
}

float IntelExt_QuadReadAcrossDiagonal(float localValue)
{
    uint laneID = IntelExt_WaveGetLaneIndex();
    return IntelExt_WaveReadLaneAt(localValue, (laneID & 0xFC) + ((laneID & 3) ^ 3));
}

int IntelExt_QuadReadAcrossDiagonal(int localValue)
{
    uint laneID = IntelExt_WaveGetLaneIndex();
    return IntelExt_WaveReadLaneAt(localValue, (laneID & 0xFC) + ((laneID & 3) ^ 3));
}

float IntelExt_QuadReadLaneAt(float sourceValue, uint quadLaneID)
{
    uint laneID = IntelExt_WaveGetLaneIndex();
    return IntelExt_WaveReadLaneAt(sourceValue, (laneID & 0xFC) + quadLaneID);
}

int IntelExt_QuadReadLaneAt(int sourceValue, uint quadLaneID)
{
    uint laneID = IntelExt_WaveGetLaneIndex();
    return IntelExt_WaveReadLaneAt(sourceValue, (laneID & 0xFC) + quadLaneID);
}

float IntelExt_QuadReadAcrossX(float localValue)
{
    uint laneID = IntelExt_WaveGetLaneIndex();
    return IntelExt_WaveReadLaneAt(localValue, (laneID & 0xFC) + ((laneID & 3) ^ 1));
}

int IntelExt_QuadReadAcrossX(int localValue)
{
    uint laneID = IntelExt_WaveGetLaneIndex();
    return IntelExt_WaveReadLaneAt(localValue, (laneID & 0xFC) + ((laneID & 3) ^ 1));
}

float IntelExt_QuadReadAcrossY(float localValue)
{
    uint laneID = IntelExt_WaveGetLaneIndex();
    return IntelExt_WaveReadLaneAt(localValue, (laneID & 0xFC) + ((laneID & 3) ^ 2));
}

int IntelExt_QuadReadAcrossY(int localValue)
{
    uint laneID = IntelExt_WaveGetLaneIndex();
    return IntelExt_WaveReadLaneAt(localValue, (laneID & 0xFC) + ((laneID & 3) ^ 2));
}

uint4 IntelExt_WaveActiveBallot(bool localValue)
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_WAVE_WAVEACTIVEBALLOT;
    g_IntelExt[opcode].src1u.x = localValue;
    return uint4(g_IntelExt[opcode].dst0u.x, 0, 0, 0);
}

float IntelExt_WaveReadLaneFirst(float input)
{
    uint firstLaneID = firstbitlow(IntelExt_WaveActiveBallot(true).x);
    return IntelExt_WaveReadLaneAt(input, firstLaneID);
}

int IntelExt_WaveReadLaneFirst(int input)
{
    uint firstLaneID = firstbitlow(IntelExt_WaveActiveBallot(true).x);
    return IntelExt_WaveReadLaneAt(input, firstLaneID);
}

bool IntelExt_WaveActiveAllTrue(bool expr)
{
    return (IntelExt_WaveActiveBallot(expr).x == IntelExt_WaveActiveBallot(true).x);
}

bool IntelExt_WaveActiveAllEqual(float localValue)
{
    return IntelExt_WaveActiveAllTrue(IntelExt_WaveReadLaneFirst(localValue) == localValue);
}

bool IntelExt_WaveActiveAllEqual(int localValue)
{
    return IntelExt_WaveActiveAllTrue(IntelExt_WaveReadLaneFirst(localValue) == localValue);
}

float IntelExt_WaveAll(float localValue, uint opType)
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_WAVE_WAVEALL;
    g_IntelExt[opcode].src0f.x = localValue;
    g_IntelExt[opcode].src1u.x = opType;
    return g_IntelExt[opcode].dst0f.x;
}

int IntelExt_WaveAll(int localValue, uint opType)
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_WAVE_WAVEALL;
    g_IntelExt[opcode].src0u.x = localValue;
    g_IntelExt[opcode].src1u.x = opType;
    return g_IntelExt[opcode].dst0u.x;
}

int IntelExt_WaveActiveBitAnd(int localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_AND);
}

int IntelExt_WaveActiveBitOr(int localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_OR);
}

int IntelExt_WaveActiveBitXor(int localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_XOR);
}

uint IntelExt_WaveActiveCountBits(bool bBit)
{
    return countbits(IntelExt_WaveActiveBallot(bBit).x);
}

float IntelExt_WaveActiveMax(float localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_FMAX);
}

int IntelExt_WaveActiveMax(int localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_IMAX);
}

uint IntelExt_WaveActiveMax(uint localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_UMAX);
}

float IntelExt_WaveActiveMin(float localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_FMIN);
}

int IntelExt_WaveActiveMin(int localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_IMIN);
}

uint IntelExt_WaveActiveMin(uint localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_UMIN);
}

float IntelExt_WaveActiveProduct(float localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_FPROD);
}

int IntelExt_WaveActiveProduct(int localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_PROD);
}

float IntelExt_WaveActiveSum(float localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_FSUM);
}

int IntelExt_WaveActiveSum(int localValue)
{
    return IntelExt_WaveAll(localValue, INTEL_EXT_WAVEOPS_SUM);
}

bool IntelExt_WaveActiveAnyTrue(bool expr)
{
    return (IntelExt_WaveActiveBallot(expr).x != 0);
}

uint IntelExt_WaveGetLaneCount()
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_SIMDSIZE;
    return g_IntelExt[opcode].dst0u.x;
}

bool IntelExt_WaveIsFirstLane()
{
    uint firstLaneID = firstbitlow(IntelExt_WaveActiveBallot(true).x);
    uint index = IntelExt_WaveGetLaneIndex();
    return (firstLaneID == index);
}

uint IntelExt_WavePrefixCountBits(bool bBit)
{
    uint ballot = IntelExt_WaveActiveBallot(bBit).x;
    uint index = IntelExt_WaveGetLaneIndex();
    return countbits(ballot & ((1 << index) - 1));
}

float IntelExt_WavePrefixProduct(float value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_WAVE_WAVEPREFIXOP;
    g_IntelExt[opcode].src0f.x = value;
    g_IntelExt[opcode].src1u.x = INTEL_EXT_WAVEOPS_FPROD;
    return g_IntelExt[opcode].dst0f.x;
}

int IntelExt_WavePrefixProduct(int value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_WAVE_WAVEPREFIXOP;
    g_IntelExt[opcode].src0u.x = value;
    g_IntelExt[opcode].src1u.x = INTEL_EXT_WAVEOPS_PROD;
    return g_IntelExt[opcode].dst0u.x;
}

float IntelExt_WavePrefixSum(float value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_WAVE_WAVEPREFIXOP;
    g_IntelExt[opcode].src0f.x = value;
    g_IntelExt[opcode].src1u.x = INTEL_EXT_WAVEOPS_FSUM;
    return g_IntelExt[opcode].dst0f.x;
}

int IntelExt_WavePrefixSum(int value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    g_IntelExt[opcode].opcode = INTEL_EXT_WAVE_WAVEPREFIXOP;
    g_IntelExt[opcode].src0u.x = value;
    g_IntelExt[opcode].src1u.x = INTEL_EXT_WAVEOPS_SUM;
    return g_IntelExt[opcode].dst0u.x;
}

// uint64 typed atomics
// Interlocked max
uint2 IntelExt_InterlockedMaxUint64(RWTexture2D<uint2> uav, uint2 address, uint2 value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    uav[uint2(opcode, opcode)] = uint2(0, 0); //dummy instruction to get the resource handle
    g_IntelExt[opcode].opcode = INTEL_EXT_UINT64_ATOMIC;
    g_IntelExt[opcode].src0u.xy = address;
    g_IntelExt[opcode].src1u.xy = value;
    g_IntelExt[opcode].src2u.x = INTEL_EXT_ATOMIC_MAX;

    return g_IntelExt[opcode].dst0u.xy;
}

// Interlocked Min
uint2 IntelExt_InterlockedMinUint64(RWTexture2D<uint2> uav, uint2 address, uint2 value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    uav[uint2(opcode, opcode)] = uint2(0, 0); //dummy instruction to get the resource handle
    g_IntelExt[opcode].opcode = INTEL_EXT_UINT64_ATOMIC;
    g_IntelExt[opcode].src0u.xy = address;
    g_IntelExt[opcode].src1u.xy = value;
    g_IntelExt[opcode].src2u.x = INTEL_EXT_ATOMIC_MIN;

    return g_IntelExt[opcode].dst0u.xy;
}

// Interlocked and
uint2 IntelExt_InterlockedAndUint64(RWTexture2D<uint2> uav, uint2 address, uint2 value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    uav[uint2(opcode, opcode)] = uint2(0, 0); //dummy instruction to get the resource handle
    g_IntelExt[opcode].opcode = INTEL_EXT_UINT64_ATOMIC;
    g_IntelExt[opcode].src0u.xy = address;
    g_IntelExt[opcode].src1u.xy = value;
    g_IntelExt[opcode].src2u.x = INTEL_EXT_ATOMIC_AND;

    return g_IntelExt[opcode].dst0u.xy;
}

// Interlocked or
uint2 IntelExt_InterlockedOrUint64(RWTexture2D<uint2> uav, uint2 address, uint2 value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    uav[uint2(opcode, opcode)] = uint2(0, 0); //dummy instruction to get the resource handle
    g_IntelExt[opcode].opcode = INTEL_EXT_UINT64_ATOMIC;
    g_IntelExt[opcode].src0u.xy = address;
    g_IntelExt[opcode].src1u.xy = value;
    g_IntelExt[opcode].src2u.x = INTEL_EXT_ATOMIC_OR;

    return g_IntelExt[opcode].dst0u.xy;
}

// Interlocked add
uint2 IntelExt_InterlockedAddUint64(RWTexture2D<uint2> uav, uint2 address, uint2 value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    uav[uint2(opcode, opcode)] = uint2(0, 0); //dummy instruction to get the resource handle
    g_IntelExt[opcode].opcode = INTEL_EXT_UINT64_ATOMIC;
    g_IntelExt[opcode].src0u.xy = address;
    g_IntelExt[opcode].src1u.xy = value;
    g_IntelExt[opcode].src2u.x = INTEL_EXT_ATOMIC_ADD;

    return g_IntelExt[opcode].dst0u.xy;
}

// Interlocked xor
uint2 IntelExt_InterlockedXorUint64(RWTexture2D<uint2> uav, uint2 address, uint2 value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    uav[uint2(opcode, opcode)] = uint2(0, 0); //dummy instruction to get the resource handle
    g_IntelExt[opcode].opcode = INTEL_EXT_UINT64_ATOMIC;
    g_IntelExt[opcode].src0u.xy = address;
    g_IntelExt[opcode].src1u.xy = value;
    g_IntelExt[opcode].src2u.x = INTEL_EXT_ATOMIC_XOR;

    return g_IntelExt[opcode].dst0u.xy;
}

// Interlocked exchange
uint2 IntelExt_InterlockedExchangeUint64(RWTexture2D<uint2> uav, uint2 address, uint2 value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    uav[uint2(opcode, opcode)] = uint2(0, 0); //dummy instruction to get the resource handle
    g_IntelExt[opcode].opcode = INTEL_EXT_UINT64_ATOMIC;
    g_IntelExt[opcode].src0u.xy = address;
    g_IntelExt[opcode].src1u.xy = value;
    g_IntelExt[opcode].src2u.x = INTEL_EXT_ATOMIC_XCHG;

    return g_IntelExt[opcode].dst0u.xy;
}

// Interlocked compare exchange
uint2 IntelExt_InterlockedCompareExchangeUint64(RWTexture2D<uint2> uav, uint2 address, uint2 cmp_value, uint2 xchg_value)
{
    uint opcode = g_IntelExt.IncrementCounter();
    uav[uint2(opcode, opcode)] = uint2(0, 0); //dummy instruction to get the resource handle
    g_IntelExt[opcode].opcode = INTEL_EXT_UINT64_ATOMIC;
    g_IntelExt[opcode].src0u.xy = address;
    g_IntelExt[opcode].src1u.xy = cmp_value;
    g_IntelExt[opcode].src1u.zw = xchg_value;
    g_IntelExt[opcode].src2u.x = INTEL_EXT_ATOMIC_CMPXCHG;

    return g_IntelExt[opcode].dst0u.xy;
}