aboutsummaryrefslogtreecommitdiff
path: root/src/core/GLES_COMPUTE/cs_shaders/helpers.h
blob: 86dedf5a9cd66fa232e8724f22f360dbdc867c32 (plain)
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
 * Copyright (c) 2017 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef ARM_COMPUTE_HELPER_H
#define ARM_COMPUTE_HELPER_H

#define CLAMP(x, min_val, max_val) min(max(x, min_val), max_val)

#define VEC_DATA_TYPE_STR(type, size) type##size
#define VEC_DATA_TYPE(type, size) VEC_DATA_TYPE_STR(type, size)

#define CONVERT(x, type) type(x)

#define PACK(value, stype, dtype) \
    pack_##stype##_##dtype(value)

#define UNPACK(value, stype, dtype) \
    unpack_##stype##_##dtype(value)

#define BUFFER_DECLARATION(name, location, type, access)          \
    layout(std430, binding = location) access buffer name##Buffer \
    {                                                             \
        type name##_ptr[];                                        \
    }

#define VECTOR_PARAM_DECLARATION(name)         \
    uint name##_stride_x;                      \
    uint name##_step_x;                        \
    uint name##_offset_first_element_in_bytes; \
    uint name##_buffer_data_type_size

#define IMAGE_PARAM_DECLARATION(name)          \
    uint name##_stride_x;                      \
    uint name##_step_x;                        \
    uint name##_stride_y;                      \
    uint name##_step_y;                        \
    uint name##_offset_first_element_in_bytes; \
    uint name##_buffer_data_type_size

#define TENSOR3D_PARAM_DECLARATION(name)       \
    uint name##_stride_x;                      \
    uint name##_step_x;                        \
    uint name##_stride_y;                      \
    uint name##_step_y;                        \
    uint name##_stride_z;                      \
    uint name##_step_z;                        \
    uint name##_offset_first_element_in_bytes; \
    uint name##_buffer_data_type_size

/** Structure to hold Vector information */
struct Vector
{
    uint current_offset;                /**< Current offset of vector */
    uint offset_first_element_in_bytes; /**< The offset of the first element in the source image */
    uint stride_x;                      /**< Stride of the image in X dimension (in bytes) */
};

/** Structure to hold Image information */
struct Image
{
    uint current_offset;                /**< Current offset of image */
    uint offset_first_element_in_bytes; /**< The offset of the first element in the source image */
    uint stride_x;                      /**< Stride of the image in X dimension (in bytes) */
    uint stride_y;                      /**< Stride of the image in Y dimension (in bytes) */
};

/** Structure to hold 3D tensor information */
struct Tensor3D
{
    uint current_offset;                /**< Current offset of tensor */
    uint offset_first_element_in_bytes; /**< The offset of the first element in the source image */
    uint stride_x;                      /**< Stride of the image in X dimension (in bytes) */
    uint stride_y;                      /**< Stride of the image in Y dimension (in bytes) */
    uint stride_z;                      /**< Stride of the image in Z dimension (in bytes) */
};

/////////////////////////////////////////////////////////////
// TODO: old to be removed

#define CONVERT_TO_VECTOR_STRUCT(name) \
    update_vector_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x)

#define CONVERT_TO_VECTOR_STRUCT_FP16(name) \
    update_vector_workitem_offset_fp16(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x)

#define CONVERT_TO_VECTOR_STRUCT_NO_STEP(name) \
    update_vector_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, uint(0))

#define CONVERT_TO_VECTOR_STRUCT_NO_STEP_FP16(name) \
    update_vector_workitem_offset_fp16(name##_offset_first_element_in_bytes, name##_stride_x, uint(0))

#define CONVERT_TO_IMAGE_STRUCT(name) \
    update_image_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y)

#define CONVERT_TO_IMAGE_STRUCT_FP16(name) \
    update_image_workitem_offset_fp16(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y)

#define CONVERT_TO_IMAGE_STRUCT_NO_STEP(name) \
    update_image_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, uint(0), name##_stride_y, uint(0))

#define CONVERT_TO_IMAGE_STRUCT_NO_STEP_FP16(name) \
    update_image_workitem_offset_fp16(name##_offset_first_element_in_bytes, name##_stride_x, uint(0), name##_stride_y, uint(0))

#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(name) \
    update_image_from_tensor3D_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, uint(0), name##_stride_y, uint(0), name##_stride_z, name##_step_z)

#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP_FP16(name) \
    update_image_from_tensor3D_workitem_offset_fp16(name##_offset_first_element_in_bytes, name##_stride_x, uint(0), name##_stride_y, uint(0), name##_stride_z, name##_step_z)

#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
    update_image_from_tensor3D_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, name##_stride_z, name##_step_z)

#define CONVERT_TENSOR3D_TO_IMAGE_STRUCT_FP16(name) \
    update_image_from_tensor3D_workitem_offset_fp16(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, name##_stride_z, name##_step_z)

#define CONVERT_TO_TENSOR3D_STRUCT(name)                                                                                                  \
    update_tensor3D_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
                                    name##_stride_z, name##_step_z)

#define CONVERT_TO_TENSOR3D_STRUCT_FP16(name)                                                                                                  \
    update_tensor3D_workitem_offset_fp16(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
                                         name##_stride_z, name##_step_z)

#define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \
    update_tensor3D_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, uint(0), name##_stride_y, uint(0), name##_stride_z, uint(0))

#define CONVERT_TO_TENSOR3D_STRUCT_NO_STEP_FP16(name) \
    update_tensor3D_workitem_offset_fp16(name##_offset_first_element_in_bytes, name##_stride_x, uint(0), name##_stride_y, uint(0), name##_stride_z, uint(0))

// FIXME: Redesign the macros if different data types are supported.
#define LOAD4(name, offset) \
    name##_ptr[offset]

#define STORE4(name, offset, value) \
    name##_ptr[offset] = value

// Load 1 element, which size is determined by ssbo type.
#define LOAD1(r, name, offset) \
    r = name##_ptr[offset]

#define STORE1(name, offset, value) \
    name##_ptr[offset] = value

#define LOAD2(r, name, offset) \
    LOAD1(r[0], name, offset); \
    LOAD1(r[1], name, (offset) + uint(1))

#define STORE2(name, offset, value)            \
    name##_ptr[offset]             = value[0]; \
    name##_ptr[(offset) + uint(1)] = value[1]

#define LOAD3(r, name, offset)             \
    LOAD1(r[0], name, offset);             \
    LOAD1(r[1], name, (offset) + uint(1)); \
    LOAD1(r[2], name, (offset) + uint(2))

#define CURRENT_OFFSET(name) \
    name.current_offset

/** Wrap vector information into an Vector structure, and make the offset to be this workitem's position.
 *
 * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector
 * @param[in] stride_x                      Stride of the vector in X dimension (in bytes)
 * @param[in] step_x                        stride_x * number of elements along X processed per workitem(in bytes)
 *
 * @return An vector object
 */
Vector update_vector_workitem_offset(uint offset_first_element_in_bytes, uint stride_x, uint step_x)
{
    Vector vector;
    vector.offset_first_element_in_bytes = offset_first_element_in_bytes;
    vector.stride_x                      = stride_x;
    vector.current_offset                = (vector.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x) >> 2;

    return vector;
}

/** Wrap vector information into an Vector structure, and make the offset to be this workitem's position.
 *
 * @param[in] offset_first_element_in_bytes The offset of the first element in the source vector
 * @param[in] stride_x                      Stride of the vector in X dimension (in bytes)
 * @param[in] step_x                        stride_x * number of elements along X processed per workitem(in bytes)
 *
 * @return An vector object
 */
Vector update_vector_workitem_offset_fp16(uint offset_first_element_in_bytes, uint stride_x, uint step_x)
{
    Vector vector;
    vector.offset_first_element_in_bytes = offset_first_element_in_bytes;
    vector.stride_x                      = stride_x;
    vector.current_offset                = vector.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x;

    return vector;
}

/** Wrap image information into an Image structure, and make the offset to be this workitem's position.
 *
 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in] stride_x                      Stride of the image in X dimension (in bytes)
 * @param[in] step_x                        stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in] stride_y                      Stride of the image in Y dimension (in bytes)
 * @param[in] step_y                        stride_y * number of elements along Y processed per workitem(in bytes)
 *
 * @return An image object
 */
Image update_image_workitem_offset(uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y)
{
    Image img;
    img.offset_first_element_in_bytes = offset_first_element_in_bytes;
    img.stride_x                      = stride_x;
    img.stride_y                      = stride_y;
    img.current_offset                = (img.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x + gl_GlobalInvocationID.y * step_y) >> 2;

    return img;
}

/** Wrap image information into an Image structure, and make the offset to be this workitem's position.
 *
 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in] stride_x                      Stride of the image in X dimension (in bytes)
 * @param[in] step_x                        stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in] stride_y                      Stride of the image in Y dimension (in bytes)
 * @param[in] step_y                        stride_y * number of elements along Y processed per workitem(in bytes)
 *
 * @return An image object
 */
Image update_image_workitem_offset_fp16(uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y)
{
    Image img;
    img.offset_first_element_in_bytes = offset_first_element_in_bytes;
    img.stride_x                      = stride_x;
    img.stride_y                      = stride_y;
    img.current_offset                = img.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x + gl_GlobalInvocationID.y * step_y;

    return img;
}

/** Wrap 3D tensor information into an image structure, and make the offset to be this workitem's position.
 *
 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in] stride_x                      Stride of the image in X dimension (in bytes)
 * @param[in] step_x                        stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in] stride_y                      Stride of the image in Y dimension (in bytes)
 * @param[in] step_y                        stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in] stride_z                      Stride of the image in Z dimension (in bytes)
 * @param[in] step_z                        stride_z * number of elements along Z processed per workitem(in bytes)
 *
 * @return A 2D Image object
 */
Image update_image_from_tensor3D_workitem_offset(uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y, uint stride_z, uint step_z)
{
    Image img;
    img.offset_first_element_in_bytes = offset_first_element_in_bytes;
    img.stride_x                      = stride_x;
    img.stride_y                      = stride_y;
    img.current_offset                = (img.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x + gl_GlobalInvocationID.y * step_y + gl_GlobalInvocationID.z * step_z) >> 2;

    return img;
}

/** Wrap 3D tensor information into an image structure, and make the offset to be this workitem's position.
 *
 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in] stride_x                      Stride of the image in X dimension (in bytes)
 * @param[in] step_x                        stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in] stride_y                      Stride of the image in Y dimension (in bytes)
 * @param[in] step_y                        stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in] stride_z                      Stride of the image in Z dimension (in bytes)
 * @param[in] step_z                        stride_z * number of elements along Z processed per workitem(in bytes)
 *
 * @return A 2D Image object
 */
Image update_image_from_tensor3D_workitem_offset_fp16(uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y, uint stride_z, uint step_z)
{
    Image img;
    img.offset_first_element_in_bytes = offset_first_element_in_bytes;
    img.stride_x                      = stride_x;
    img.stride_y                      = stride_y;
    img.current_offset                = img.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x + gl_GlobalInvocationID.y * step_y + gl_GlobalInvocationID.z * step_z;

    return img;
}

/** Wrap 3D tensor information into an tensor structure, and make the offset to be this workitem's position.
 *
 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in] stride_x                      Stride of the image in X dimension (in bytes)
 * @param[in] step_x                        stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in] stride_y                      Stride of the image in Y dimension (in bytes)
 * @param[in] step_y                        stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in] stride_z                      Stride of the image in Z dimension (in bytes)
 * @param[in] step_z                        stride_z * number of elements along Z processed per workitem(in bytes)
 *
 * @return A 3D tensor object
 */
Tensor3D update_tensor3D_workitem_offset(uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y, uint stride_z, uint step_z)
{
    Tensor3D tensor;
    tensor.offset_first_element_in_bytes = offset_first_element_in_bytes;
    tensor.stride_x                      = stride_x;
    tensor.stride_y                      = stride_y;
    tensor.stride_z                      = stride_z;
    tensor.current_offset                = (tensor.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x + gl_GlobalInvocationID.y * step_y + gl_GlobalInvocationID.z * step_z) >> 2;

    return tensor;
}

/** Wrap 3D tensor information into an tensor structure, and make the offset to be this workitem's position.
 *
 * @param[in] offset_first_element_in_bytes The offset of the first element in the source image
 * @param[in] stride_x                      Stride of the image in X dimension (in bytes)
 * @param[in] step_x                        stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in] stride_y                      Stride of the image in Y dimension (in bytes)
 * @param[in] step_y                        stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in] stride_z                      Stride of the image in Z dimension (in bytes)
 * @param[in] step_z                        stride_z * number of elements along Z processed per workitem(in bytes)
 *
 * @return A 3D tensor object
 */
Tensor3D update_tensor3D_workitem_offset_fp16(uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y, uint stride_z, uint step_z)
{
    Tensor3D tensor;
    tensor.offset_first_element_in_bytes = offset_first_element_in_bytes;
    tensor.stride_x                      = stride_x;
    tensor.stride_y                      = stride_y;
    tensor.stride_z                      = stride_z;
    tensor.current_offset                = tensor.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x + gl_GlobalInvocationID.y * step_y + gl_GlobalInvocationID.z * step_z;

    return tensor;
}

/** Get the pointer position of a Vector
 *
 * @param[in] vec Pointer to the starting position of the buffer
 * @param[in] x   Relative X position
 */
uint vector_offset(Vector vec, int x)
{
    return CONVERT(CONVERT(vec.current_offset << 2, int) + x * CONVERT(vec.stride_x, int), uint) >> 2;
}

/** Get the pointer position of a Vector
 *
 * @param[in] vec Pointer to the starting position of the buffer
 * @param[in] x   Relative X position
 */
uint vector_offset_fp16(Vector vec, int x)
{
    return CONVERT(CONVERT(vec.current_offset, int) + x * CONVERT(vec.stride_x, int), uint);
}

/** Get the pointer position of a Image
 *
 * @param[in] img Pointer to the starting position of the buffer
 * @param[in] x   Relative X position
 * @param[in] y   Relative Y position
 */
uint offset(Image img, int x, int y)
{
    return CONVERT(CONVERT(img.current_offset << 2, int) + x * CONVERT(img.stride_x, int) + y * CONVERT(img.stride_y, int), uint) >> 2;
}

/** Get the pointer position of a Image
 *
 * @param[in] img Pointer to the starting position of the buffer
 * @param[in] x   Relative X position
 * @param[in] y   Relative Y position
 */
uint offset_fp16(Image img, int x, int y)
{
    return CONVERT(CONVERT(img.current_offset, int) + x * CONVERT(img.stride_x, int) + y * CONVERT(img.stride_y, int), uint);
}

/** Get the pointer position of a Tensor3D
 *
 * @param[in] tensor Pointer to the starting postion of the buffer
 * @param[in] x      Relative X position
 * @param[in] y      Relative Y position
 * @param[in] z      Relative Z position
 */
uint tensor3D_offset(Tensor3D tensor, int x, int y, int z)
{
    return CONVERT(CONVERT(tensor.current_offset << 2, int) + x * CONVERT(tensor.stride_x, int) + y * CONVERT(tensor.stride_y, int) + z * CONVERT(tensor.stride_z, int), uint) >> 2;
}

/** Get the pointer position of a Tensor3D
 *
 * @param[in] tensor Pointer to the starting postion of the buffer
 * @param[in] x      Relative X position
 * @param[in] y      Relative Y position
 * @param[in] z      Relative Z position
 */
uint tensor3D_offset_fp16(Tensor3D tensor, int x, int y, int z)
{
    return CONVERT(CONVERT(tensor.current_offset, int) + x * CONVERT(tensor.stride_x, int) + y * CONVERT(tensor.stride_y, int) + z * CONVERT(tensor.stride_z, int), uint);
}

/////////////////////////////////////////////////////////////
// new one

#define GC_CONVERT_TO_VECTOR_STRUCT(name) \
    gc_update_vector_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x)

#define GC_CONVERT_TO_VECTOR_STRUCT_NO_STEP(name) \
    gc_update_vector_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, uint(0))

#define GC_CONVERT_TO_IMAGE_STRUCT(name) \
    gc_update_image_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y)

#define GC_CONVERT_TO_IMAGE_STRUCT_NO_STEP(name) \
    gc_update_image_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, uint(0), name##_stride_y, uint(0))

#define GC_CONVERT_TO_TENSOR3D_STRUCT(name)                                                                                                  \
    gc_update_tensor3D_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, \
                                       name##_stride_z, name##_step_z)

#define GC_CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(name) \
    gc_update_tensor3D_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, uint(0), name##_stride_y, uint(0), name##_stride_z, uint(0))

#define GC_CONVERT_TENSOR3D_TO_IMAGE_STRUCT(name) \
    gc_update_image_from_tensor3D_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, name##_step_x, name##_stride_y, name##_step_y, name##_stride_z, name##_step_z)

#define GC_CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(name) \
    gc_update_image_from_tensor3D_workitem_offset(name##_offset_first_element_in_bytes, name##_stride_x, uint(0), name##_stride_y, uint(0), name##_stride_z, name##_step_z)

Vector gc_update_vector_workitem_offset(uint offset_first_element_in_bytes, uint stride_x, uint step_x)
{
    Vector vector;
    vector.offset_first_element_in_bytes = offset_first_element_in_bytes;
    vector.stride_x                      = stride_x;
    vector.current_offset                = vector.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x;

    return vector;
}

Image gc_update_image_workitem_offset(uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y)
{
    Image img;
    img.offset_first_element_in_bytes = offset_first_element_in_bytes;
    img.stride_x                      = stride_x;
    img.stride_y                      = stride_y;
    img.current_offset                = img.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x + gl_GlobalInvocationID.y * step_y;

    return img;
}

Tensor3D gc_update_tensor3D_workitem_offset(uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y, uint stride_z, uint step_z)
{
    Tensor3D tensor;
    tensor.offset_first_element_in_bytes = offset_first_element_in_bytes;
    tensor.stride_x                      = stride_x;
    tensor.stride_y                      = stride_y;
    tensor.stride_z                      = stride_z;
    tensor.current_offset                = tensor.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x + gl_GlobalInvocationID.y * step_y + gl_GlobalInvocationID.z * step_z;

    return tensor;
}

Image gc_update_image_from_tensor3D_workitem_offset(uint offset_first_element_in_bytes, uint stride_x, uint step_x, uint stride_y, uint step_y, uint stride_z, uint step_z)
{
    Image img;
    img.offset_first_element_in_bytes = offset_first_element_in_bytes;
    img.stride_x                      = stride_x;
    img.stride_y                      = stride_y;
    img.current_offset                = img.offset_first_element_in_bytes + gl_GlobalInvocationID.x * step_x + gl_GlobalInvocationID.y * step_y + gl_GlobalInvocationID.z * step_z;

    return img;
}

#define GC_CURRENT_OFFSET(name) \
    name.current_offset

uint gc_vector_offset(Vector vec, int x)
{
    return CONVERT(CONVERT(vec.current_offset, int) + x * CONVERT(vec.stride_x, int), uint);
}

uint gc_image_offset(Image img, int x, int y)
{
    return CONVERT(CONVERT(img.current_offset, int) + x * CONVERT(img.stride_x, int) + y * CONVERT(img.stride_y, int), uint);
}

uint gc_tensor3D_offset(Tensor3D tensor, int x, int y, int z)
{
    return CONVERT(CONVERT(tensor.current_offset, int) + x * CONVERT(tensor.stride_x, int) + y * CONVERT(tensor.stride_y, int) + z * CONVERT(tensor.stride_z, int), uint);
}

// load/store number of element depends on buffer type
#define GC_LOAD1(r, name, offset) \
    r = name##_ptr[offset]

#define GC_LOAD2(r, name, offset) \
    GC_LOAD1(r[0], name, offset); \
    GC_LOAD1(r[1], name, (offset) + uint(1))

#define GC_LOAD3(r, name, offset)             \
    GC_LOAD1(r[0], name, offset);             \
    GC_LOAD1(r[1], name, (offset) + uint(1)); \
    GC_LOAD1(r[2], name, (offset) + uint(2))

#define GC_STORE1(value, name, offset) \
    name##_ptr[offset] = value

#define GC_STORE2(value, name, offset) \
    GC_STORE1(value[0], name, offset); \
    GC_STORE1(value[1], name, (offset) + uint(1))

#define GC_STORE3(value, name, offset)             \
    GC_STORE1(value[0], name, offset);             \
    GC_STORE1(value[1], name, (offset) + uint(1)); \
    GC_STORE1(value[2], name, (offset) + uint(2))

// has to manually expand them since not supported by compiler
#define GC_LOAD1_1D_OFFSET(r, name, x) \
    GC_LOAD1(r, name, gc_vector_offset(name, int(x)) >> name##_buffer_data_type_size)

#define GC_LOAD1_2D_OFFSET(r, name, x, y) \
    GC_LOAD1(r, name, gc_image_offset(name, int(x), int(y)) >> name##_buffer_data_type_size)

#define GC_LOAD1_3D_OFFSET(r, name, x, y, z) \
    GC_LOAD1(r, name, gc_tensor3D_offset(name, int(x), int(y), int(z)) >> name##_buffer_data_type_size)

#define GC_STORE1_1D_OFFSET(value, name, x) \
    GC_STORE1(value, name, gc_vector_offset(name, int(x)) >> name##_buffer_data_type_size)

#define GC_STORE1_2D_OFFSET(value, name, x, y) \
    GC_STORE1(value, name, gc_image_offset(name, int(x), int(y)) >> name##_buffer_data_type_size)

#define GC_STORE1_3D_OFFSET(value, name, x, y, z) \
    GC_STORE1(value, name, gc_tensor3D_offset(name, int(x), int(y), int(z)) >> name##_buffer_data_type_size)

#define GC_LOAD2_1D_OFFSET(r, name, x) \
    GC_LOAD2(r, name, gc_vector_offset(name, int(x)) >> name##_buffer_data_type_size)

#define GC_LOAD2_2D_OFFSET(r, name, x, y) \
    GC_LOAD2(r, name, gc_image_offset(name, int(x), int(y)) >> name##_buffer_data_type_size)

#define GC_LOAD2_3D_OFFSET(r, name, x, y, z) \
    GC_LOAD2(r, name, gc_tensor3D_offset(name, int(x), int(y), int(z)) >> name##_buffer_data_type_size)

#define GC_STORE2_1D_OFFSET(value, name, x) \
    GC_STORE2(value, name, gc_vector_offset(name, int(x)) >> name##_buffer_data_type_size)

#define GC_STORE2_2D_OFFSET(value, name, x, y) \
    GC_STORE2(value, name, gc_image_offset(name, int(x), int(y)) >> name##_buffer_data_type_size)

#define GC_STORE2_3D_OFFSET(value, name, x, y, z) \
    GC_STORE2(value, name, gc_tensor3D_offset(name, int(x), int(y), int(z)) >> name##_buffer_data_type_size)

#define GC_LOAD3_1D_OFFSET(r, name, x) \
    GC_LOAD3(r, name, gc_vector_offset(name, int(x)) >> name##_buffer_data_type_size)

#define GC_LOAD3_2D_OFFSET(r, name, x, y) \
    GC_LOAD3(r, name, gc_image_offset(name, int(x), int(y)) >> name##_buffer_data_type_size)

#define GC_LOAD3_3D_OFFSET(r, name, x, y, z) \
    GC_LOAD3(r, name, gc_tensor3D_offset(name, int(x), int(y), int(z)) >> name##_buffer_data_type_size)

/////////////////////////////////////////////////////////////

#endif // _HELPER_H