File: imageblit_kernels.cl

package info (click to toggle)
rocr-runtime 6.4.3%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,888 kB
  • sloc: cpp: 126,824; ansic: 41,837; lisp: 1,225; asm: 905; sh: 452; python: 117; makefile: 59
file content (657 lines) | stat: -rw-r--r-- 22,634 bytes parent folder | download | duplicates (4)
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
////////////////////////////////////////////////////////////////////////////////
//
// The University of Illinois/NCSA
// Open Source License (NCSA)
//
// Copyright (c) 2014-2020, Advanced Micro Devices, Inc. All rights reserved.
//
// Developed by:
//
//                 AMD Research and AMD HSA Software Development
//
//                 Advanced Micro Devices, Inc.
//
//                 www.amd.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal with 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:
//
//  - Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimers.
//  - Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimers in
//    the documentation and/or other materials provided with the distribution.
//  - Neither the names of Advanced Micro Devices, Inc,
//    nor the names of its contributors may be used to endorse or promote
//    products derived from this Software without specific prior written
//    permission.
//
// 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 CONTRIBUTORS 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 WITH THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

/// Kernel code for HSA image import/export/copy/clear in OpenCL C form.

uint4 read_image(__read_only image1d_t src1d,
                 __read_only image2d_t src2d,
                 __read_only image3d_t src3d,
                 __read_only image1d_array_t src1da,
                 __read_only image2d_array_t src2da,
                 uint format,
                 int4 coords) {
  switch (format) {
    case 0:  // 1D
      return read_imageui(src1d, coords.x);
      break;
    case 1:  // 2D
      return read_imageui(src2d, coords.xy);
      break;
    case 2:  // 3D
      return read_imageui(src3d, coords);
      break;
    case 3:  // 1DA
      return read_imageui(src1da, coords.xy);
      break;
    case 4:  // 2DA
      return read_imageui(src2da, coords);
      break;
    // case 5: //1DB
    //  return read_imageui(src1db, coords.x);
    //  break;
    default:  // Critical failure.
      return 0;
  }
}

void write_image(__write_only image1d_t src1d,
                 __write_only image2d_t src2d,
                 __write_only image3d_t src3d,
                 __write_only image1d_array_t src1da,
                 __write_only image2d_array_t src2da,
                 uint format,
                 int4 coords,
                 uint4 texel) {
  switch (format) {
    case 0:  // 1D
      write_imageui(src1d, coords.x, texel);
      break;
    case 1:  // 2D
      write_imageui(src2d, coords.xy, texel);
      break;
    case 2:  // 3D
      write_imageui(src3d, coords, texel);
      break;
    case 3:  // 1DA
      write_imageui(src1da, coords.xy, texel);
      break;
    case 4:  // 2DA
      write_imageui(src2da, coords, texel);
      break;
    // case 5: //1DB
    //  write_imageui(src1db, coords.x, texel);
    //  break;
    default:  // Critical failure.
      return;
  }
}

float4 read_image_float(__read_only image1d_t src1d,
                        __read_only image2d_t src2d,
                        __read_only image3d_t src3d,
                        __read_only image1d_array_t src1da,
                        __read_only image2d_array_t src2da,
                        uint format,
                        int4 coords) {
  switch (format) {
    case 0:  // 1D
      return read_imagef(src1d, coords.x);
      break;
    case 1:  // 2D
      return read_imagef(src2d, coords.xy);
      break;
    case 2:  // 3D
      return read_imagef(src3d, coords);
      break;
    case 3:  // 1DA
      return read_imagef(src1da, coords.xy);
      break;
    case 4:  // 2DA
      return read_imagef(src2da, coords);
      break;
    default:  // Critical failure.
      return 0;
  }
}

void write_image_float(__write_only image1d_t src1d,
                       __write_only image2d_t src2d,
                       __write_only image3d_t src3d,
                       __write_only image1d_array_t src1da,
                       __write_only image2d_array_t src2da,
                       uint format,
                       int4 coords,
                       float4 texel) {
  switch (format) {
    case 0:  // 1D
      write_imagef(src1d, coords.x, texel);
      break;
    case 1:  // 2D
      write_imagef(src2d, coords.xy, texel);
      break;
    case 2:  // 3D
      write_imagef(src3d, coords, texel);
      break;
    case 3:  // 1DA
      write_imagef(src1da, coords.xy, texel);
      break;
    case 4:  // 2DA
      write_imagef(src2da, coords, texel);
      break;
    default:  // Critical failure.
      return;
  }
}

void write_image_int(__write_only image1d_t src1d,
                     __write_only image2d_t src2d,
                     __write_only image3d_t src3d,
                     __write_only image1d_array_t src1da,
                     __write_only image2d_array_t src2da,
                     uint format,
                     int4 coords,
                     int4 texel) {
  switch (format) {
    case 0:  // 1D
      write_imagei(src1d, coords.x, texel);
      break;
    case 1:  // 2D
      write_imagei(src2d, coords.xy, texel);
      break;
    case 2:  // 3D
      write_imagei(src3d, coords, texel);
      break;
    case 3:  // 1DA
      write_imagei(src1da, coords.xy, texel);
      break;
    case 4:  // 2DA
      write_imagei(src2da, coords, texel);
      break;
    default:  // Critical failure.
      return;
  }
}

//image handle is repeated since OCL doesn't allow pointers to or casting of images.
//dst is start of output pixel in destination buffer
//format.x is element count
//format.y is element size
//format.z is max(dword per pixel, 1)
//format.w is texture type.
//srcOrigin is start pixel address.
//No export for 64, 96, 128 bit formats
__kernel void copy_image_to_buffer(
    __read_only image1d_t src1d,
    __read_only image2d_t src2d,
    __read_only image3d_t src3d,
    __read_only image1d_array_t src1da,
    __read_only image2d_array_t src2da,
    __global void* const dst,
    int4        srcOrigin,
    uint4       format,
    ulong       pitch,
    ulong       slice_pitch)
{
    ulong    idxDst;
    int4     coordsSrc;
    uint4    texel;

    __global uchar* const dstUChar = (__global uchar* const)dst;
    __global ushort* const dstUShort = (__global ushort* const)dst;
    __global uint* const dstUInt = (__global uint* const)dst;

    coordsSrc.x = get_global_id(0);
    coordsSrc.y = get_global_id(1);
    coordsSrc.z = get_global_id(2);
    coordsSrc.w = 0;

    idxDst = (coordsSrc.z * slice_pitch + coordsSrc.y * pitch +
        coordsSrc.x) * format.z;

    coordsSrc.x += srcOrigin.x;
    coordsSrc.y += srcOrigin.y;
    coordsSrc.z += srcOrigin.z;

    texel = read_image(src1d, src2d, src3d, src1da, src2da, format.w, coordsSrc);

    // Check components
    switch (format.x) {
    case 1:
        // Check size
        switch (format.y) {
        case 1:
            dstUChar[idxDst] = texel.x;
            break;
        case 2:
            dstUShort[idxDst] = texel.x;
            break;
        case 4:
            dstUInt[idxDst] = texel.x;
            break;
        }
    break;
    case 2:
        // Check size
        switch (format.y) {
        case 1:
            dstUShort[idxDst] = texel.x |
               (texel.y << 8);
            break;
        case 2:
            dstUInt[idxDst] = texel.x | (texel.y << 16);
            break;
        case 4:
            dstUInt[idxDst++] = texel.x;
            dstUInt[idxDst] = texel.y;
            break;
        }
    break;
    case 4:
        // Check size
        switch (format.y) {
        case 1:
            dstUInt[idxDst] = texel.x |
               (texel.y << 8) |
               (texel.z << 16) |
               (texel.w << 24);
            break;
        case 2:
            dstUInt[idxDst++] = texel.x | (texel.y << 16);
            dstUInt[idxDst] = texel.z | (texel.w << 16);
            break;
        case 4:
            dstUInt[idxDst++] = texel.x;
            dstUInt[idxDst++] = texel.y;
            dstUInt[idxDst++] = texel.z;
            dstUInt[idxDst] = texel.w;
            break;
        }
    break;
    }
}

__kernel void copy_buffer_to_image(__global uint* src,
                                   __write_only image1d_t dst1d,
                                   __write_only image2d_t dst2d,
                                   __write_only image3d_t dst3d,
                                   __write_only image1d_array_t dst1da,
                                   __write_only image2d_array_t dst2da,
                                   int4 dstOrigin,
                                   uint4 format,
                                   ulong pitch,
                                   ulong slice_pitch) {
  ulong idxSrc;
  int4 coordsDst;
  uint4 texel;

  __global uint* srcUInt = src;
  __global ushort* srcUShort = (__global ushort*)src;
  __global uchar* srcUChar = (__global uchar*)src;

  ushort tmpUShort;
  uint tmpUInt;

  coordsDst.x = get_global_id(0);
  coordsDst.y = get_global_id(1);
  coordsDst.z = get_global_id(2);
  coordsDst.w = 0;

  idxSrc = (coordsDst.z * slice_pitch + coordsDst.y * pitch + coordsDst.x) * format.z;

  coordsDst.x += dstOrigin.x;
  coordsDst.y += dstOrigin.y;
  coordsDst.z += dstOrigin.z;

  // Check components
  switch (format.x) {
    case 1:
        // Check size
        switch (format.y) {
          case 1:
            texel.x = (uint)srcUChar[idxSrc];
            break;
          case 2:
            texel.x = (uint)srcUShort[idxSrc];
            break;
          case 4:
            texel.x = srcUInt[idxSrc];
            break;
        }
    break;
    case 2:
        // Check size
        switch (format.y) {
          case 1:
            tmpUShort = srcUShort[idxSrc];
            texel.x = (uint)(tmpUShort & 0xff);
            texel.y = (uint)(tmpUShort >> 8);
            break;
          case 2:
            tmpUInt = srcUInt[idxSrc];
            texel.x = (tmpUInt & 0xffff);
            texel.y = (tmpUInt >> 16);
            break;
          case 4:
            texel.x = srcUInt[idxSrc++];
            texel.y = srcUInt[idxSrc];
            break;
        }
    break;
    case 4:
        // Check size
        switch (format.y) {
          case 1:
            tmpUInt = srcUInt[idxSrc];
            texel.x = tmpUInt & 0xff;
            texel.y = (tmpUInt >> 8) & 0xff;
            texel.z = (tmpUInt >> 16) & 0xff;
            texel.w = (tmpUInt >> 24) & 0xff;
            break;
          case 2:
            tmpUInt = srcUInt[idxSrc++];
            texel.x = tmpUInt & 0xffff;
            texel.y = (tmpUInt >> 16);
            tmpUInt = srcUInt[idxSrc];
            texel.z = tmpUInt & 0xffff;
            texel.w = (tmpUInt >> 16);
            break;
          case 4:
            texel.x = srcUInt[idxSrc++];
            texel.y = srcUInt[idxSrc++];
            texel.z = srcUInt[idxSrc++];
            texel.w = srcUInt[idxSrc];
            break;
        }
        break;
    }
    // Write the final pixel
    write_image(dst1d, dst2d, dst3d, dst1da, dst2da, format.w, coordsDst, texel);
}

__kernel void copy_image_default(__read_only image1d_t src1d,
                                 __read_only image2d_t src2d,
                                 __read_only image3d_t src3d,
                                 __read_only image1d_array_t src1da,
                                 __read_only image2d_array_t src2da,
                                 __write_only image1d_t dst1d,
                                 __write_only image2d_t dst2d,
                                 __write_only image3d_t dst3d,
                                 __write_only image1d_array_t dst1da,
                                 __write_only image2d_array_t dst2da,
                                 int4 srcOrigin,
                                 int4 dstOrigin,
                                 int srcFormat,
                                 int dstFormat) {
  int4 coordsDst;
  int4 coordsSrc;

  coordsDst.x = get_global_id(0);
  coordsDst.y = get_global_id(1);
  coordsDst.z = get_global_id(2);
  coordsDst.w = 0;

  coordsSrc = srcOrigin + coordsDst;
  coordsDst += dstOrigin;

  uint4 texel;
  texel = read_image(src1d, src2d, src3d, src1da, src2da, srcFormat, coordsSrc);
  write_image(dst1d, dst2d, dst3d, dst1da, dst2da, dstFormat, coordsDst, texel);
}

float linear_to_standard_rgba(float l_val) {
  float s_val = l_val;

  if (isnan(s_val)) s_val = 0.0f;

  if (s_val > 1.0f) {
    s_val = 1.0f;
  } else if (s_val < 0.0f) {
    s_val = 0.0f;
  } else if (s_val < 0.0031308f) {
    s_val = 12.92f * s_val;
  } else {
    s_val = (1.055f * pow(s_val, 5.0f / 12.0f)) - 0.055f;
  }

  return s_val;
}

__kernel void copy_image_linear_to_standard(
                                            __read_only image1d_t src1d,
                                            __read_only image2d_t src2d,
                                            __read_only image3d_t src3d,
                                            __read_only image1d_array_t src1da,
                                            __read_only image2d_array_t src2da,
                                            int srcFormat,
                                            __write_only image1d_t dst1d,
                                            __write_only image2d_t dst2d,
                                            __write_only image3d_t dst3d,
                                            __write_only image1d_array_t dst1da,
                                            __write_only image2d_array_t dst2da,
                                            int dstFormat,
                                            int4 srcOrigin,
                                            int4 dstOrigin) {
  int4 coordsDst;
  int4 coordsSrc;

  coordsDst.x = get_global_id(0);
  coordsDst.y = get_global_id(1);
  coordsDst.z = get_global_id(2);
  coordsDst.w = 0;

  coordsSrc = srcOrigin + coordsDst;
  coordsDst += dstOrigin;

  float4 texel;
  texel = read_image_float(src1d, src2d, src3d, src1da, src2da, srcFormat, coordsSrc);

  texel.x = linear_to_standard_rgba(texel.x);
  texel.y = linear_to_standard_rgba(texel.y);
  texel.z = linear_to_standard_rgba(texel.z);

  write_image_float(dst1d, dst2d, dst3d, dst1da, dst2da, dstFormat, coordsDst, texel);
}

__kernel void copy_image_standard_to_linear(
                                            __read_only image1d_t src1d,
                                            __read_only image2d_t src2d,
                                            __read_only image3d_t src3d,
                                            __read_only image1d_array_t src1da,
                                            __read_only image2d_array_t src2da,
                                            int srcFormat,
                                            __write_only image1d_t dst1d,
                                            __write_only image2d_t dst2d,
                                            __write_only image3d_t dst3d,
                                            __write_only image1d_array_t dst1da,
                                            __write_only image2d_array_t dst2da,
                                            int dstFormat,
                                            int4 srcOrigin,
                                            int4 dstOrigin) {
  int4 coordsDst;
  int4 coordsSrc;

  coordsDst.x = get_global_id(0);
  coordsDst.y = get_global_id(1);
  coordsDst.z = get_global_id(2);
  coordsDst.w = 0;

  coordsSrc = srcOrigin + coordsDst;
  coordsDst += dstOrigin;

  float4 texel;
  texel = read_image_float(src1d, src2d, src3d, src1da, src2da, srcFormat, coordsSrc);
  write_image_float(dst1d, dst2d, dst3d, dst1da, dst2da, dstFormat, coordsDst, texel);
}

__kernel void copy_image_1db(
                                            __read_only image1d_buffer_t src1d,
                                            __read_only image2d_t src2d,
                                            __read_only image3d_t src3d,
                                            __read_only image1d_array_t src1da,
                                            __read_only image2d_array_t src2da,
                                            int srcFormat,
                                            __write_only image1d_t dst1d,
                                            __write_only image2d_t dst2d,
                                            __write_only image3d_t dst3d,
                                            __write_only image1d_array_t dst1da,
                                            __write_only image2d_array_t dst2da,
                                            int dstFormat,
                                            int4 srcOrigin,
                                            int4 dstOrigin)
{
    int    coordDst;
    int    coordSrc;

    coordDst = get_global_id(0);

    coordSrc = srcOrigin.x + coordDst;
    coordDst += dstOrigin.x;

    uint4  texel;
    texel = read_imageui(src1d, coordSrc);
    write_imageui(dst1d, coordDst, texel);
}

__kernel void copy_image_1db_to_reg(
                                            __read_only image1d_buffer_t src1d,
                                            __read_only image2d_t src2d,
                                            __read_only image3d_t src3d,
                                            __read_only image1d_array_t src1da,
                                            __read_only image2d_array_t src2da,
                                            int srcFormat,
                                            __write_only image1d_t dst1d,
                                            __write_only image2d_t dst2d,
                                            __write_only image3d_t dst3d,
                                            __write_only image1d_array_t dst1da,
                                            __write_only image2d_array_t dst2da,
                                            int dstFormat,
                                            int4 srcOrigin,
                                            int4 dstOrigin)
{
    int4    coordsDst;
    int    coordSrc;

    coordsDst.x = get_global_id(0);
    coordsDst.y = get_global_id(1);
    coordsDst.z = get_global_id(2);
    coordsDst.w = 0;

    coordSrc = srcOrigin.x + coordsDst.x;
    coordsDst += dstOrigin;

    uint4  texel;
    texel = read_imageui(src1d, coordSrc);
    write_imageui(dst1d, coordsDst.x, texel);
}

__kernel void copy_image_reg_to_1db(
                                            __read_only image1d_t src1d,
                                            __read_only image2d_t src2d,
                                            __read_only image3d_t src3d,
                                            __read_only image1d_array_t src1da,
                                            __read_only image2d_array_t src2da,
                                            int srcFormat,
                                            __write_only image1d_buffer_t dst1d,
                                            __write_only image2d_t dst2d,
                                            __write_only image3d_t dst3d,
                                            __write_only image1d_array_t dst1da,
                                            __write_only image2d_array_t dst2da,
                                            int dstFormat,
                                            int4 srcOrigin,
                                            int4 dstOrigin)
{
    int    coordDst;
    int4    coordsSrc;

    coordsSrc.x = get_global_id(0);
    coordsSrc.y = get_global_id(1);
    coordsSrc.z = get_global_id(2);
    coordsSrc.w = 0;

    coordDst = dstOrigin.x + coordsSrc.x;
    coordsSrc += srcOrigin;

    uint4  texel;
    texel = read_imageui(src1d, coordsSrc.x);
    write_imageui(dst1d, coordDst, texel);
}

__kernel void clear_image(__write_only image1d_t dst1d,
                          __write_only image2d_t dst2d,
                          __write_only image3d_t dst3d,
                          __write_only image1d_array_t dst1da,
                          __write_only image2d_array_t dst2da,
                          int dstFormat,
                          uint type,
                          uint4 fill_data,
                          int4 origin) {
  int4 coords;

  coords.x = get_global_id(0);
  coords.y = get_global_id(1);
  coords.z = get_global_id(2);
  coords.w = 0;

  coords += origin;

  // Check components
  switch (type) {
    case 0:
      write_image_float(dst1d, dst2d, dst3d, dst1da, dst2da, dstFormat, coords, *(float4*)&fill_data);
      break;
    case 1:
      write_image_int(dst1d, dst2d, dst3d, dst1da, dst2da, dstFormat, coords, *(int4*)&fill_data);
      break;
    case 2:
      write_image(dst1d, dst2d, dst3d, dst1da, dst2da, dstFormat, coords, fill_data);
      break;
    }
}

__kernel void clear_image_1db(__write_only image1d_buffer_t dst1d,
                              __write_only image2d_t dst2d,
                              __write_only image3d_t dst3d,
                              __write_only image1d_array_t dst1da,
                              __write_only image2d_array_t dst2da,
                              int dstFormat,
                              uint4 fill_data,
                              int4 origin,
                              uint type) {
  int4 coords;

  coords.x = get_global_id(0);

  coords += origin;

  // Check components
  switch (type) {
    case 0:
      write_imagef(dst1d, coords.x, *(float4*)&fill_data);
      break;
    case 1:
      write_imagei(dst1d, coords.x, *(int4*)&fill_data);
      break;
    case 2:
      write_imageui(dst1d, coords.x, fill_data);
      break;
    }
}