-
Notifications
You must be signed in to change notification settings - Fork 191
/
imageHelpers.cpp
4134 lines (3720 loc) · 145 KB
/
imageHelpers.cpp
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2017,2021 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "imageHelpers.h"
#include <limits.h>
#include <assert.h>
#if defined(__APPLE__)
#include <sys/mman.h>
#endif
#if !defined(_WIN32) && !defined(__APPLE__)
#include <malloc.h>
#endif
#include <algorithm>
#include <cinttypes>
#include <iterator>
#if !defined(_WIN32)
#include <cmath>
#endif
RoundingMode gFloatToHalfRoundingMode = kDefaultRoundingMode;
cl_device_type gDeviceType = CL_DEVICE_TYPE_DEFAULT;
bool gTestRounding = false;
double sRGBmap(float fc)
{
double c = (double)fc;
#if !defined(_WIN32)
if (std::isnan(c)) c = 0.0;
#else
if (_isnan(c)) c = 0.0;
#endif
if (c > 1.0)
c = 1.0;
else if (c < 0.0)
c = 0.0;
else if (c < 0.0031308)
c = 12.92 * c;
else
c = (1055.0 / 1000.0) * pow(c, 5.0 / 12.0) - (55.0 / 1000.0);
return c * 255.0;
}
double sRGBunmap(float fc)
{
double c = (double)fc;
double result;
if (c <= 0.04045)
result = c / 12.92;
else
result = pow((c + 0.055) / 1.055, 2.4);
return result;
}
uint32_t get_format_type_size(const cl_image_format *format)
{
return get_channel_data_type_size(format->image_channel_data_type);
}
uint32_t get_channel_data_type_size(cl_channel_type channelType)
{
switch (channelType)
{
case CL_SNORM_INT8:
case CL_UNORM_INT8:
case CL_SIGNED_INT8:
case CL_UNSIGNED_INT8: return 1;
case CL_SNORM_INT16:
case CL_UNORM_INT16:
case CL_SIGNED_INT16:
case CL_UNSIGNED_INT16:
case CL_HALF_FLOAT:
#ifdef CL_SFIXED14_APPLE
case CL_SFIXED14_APPLE:
#endif
return sizeof(cl_short);
case CL_SIGNED_INT32:
case CL_UNSIGNED_INT32: return sizeof(cl_int);
case CL_UNORM_SHORT_565:
case CL_UNORM_SHORT_555: return 2;
case CL_UNORM_INT_101010: return 4;
case CL_FLOAT: return sizeof(cl_float);
default: return 0;
}
}
uint32_t get_format_channel_count(const cl_image_format *format)
{
return get_channel_order_channel_count(format->image_channel_order);
}
uint32_t get_channel_order_channel_count(cl_channel_order order)
{
switch (order)
{
case CL_R:
case CL_A:
case CL_Rx:
case CL_INTENSITY:
case CL_LUMINANCE:
case CL_DEPTH:
case CL_DEPTH_STENCIL: return 1;
case CL_RG:
case CL_RA:
case CL_RGx: return 2;
case CL_RGB:
case CL_RGBx:
case CL_sRGB:
case CL_sRGBx: return 3;
case CL_RGBA:
case CL_ARGB:
case CL_BGRA:
case CL_sRGBA:
case CL_sBGRA:
case CL_ABGR:
#ifdef CL_1RGB_APPLE
case CL_1RGB_APPLE:
#endif
#ifdef CL_BGR1_APPLE
case CL_BGR1_APPLE:
#endif
#ifdef CL_ABGR_APPLE
case CL_ABGR_APPLE:
#endif
return 4;
default:
log_error("%s does not support 0x%x\n", __FUNCTION__, order);
return 0;
}
}
cl_channel_type get_channel_type_from_name(const char *name)
{
struct
{
cl_channel_type type;
const char *name;
} typeNames[] = { { CL_SNORM_INT8, "CL_SNORM_INT8" },
{ CL_SNORM_INT16, "CL_SNORM_INT16" },
{ CL_UNORM_INT8, "CL_UNORM_INT8" },
{ CL_UNORM_INT16, "CL_UNORM_INT16" },
{ CL_UNORM_INT24, "CL_UNORM_INT24" },
{ CL_UNORM_SHORT_565, "CL_UNORM_SHORT_565" },
{ CL_UNORM_SHORT_555, "CL_UNORM_SHORT_555" },
{ CL_UNORM_INT_101010, "CL_UNORM_INT_101010" },
{ CL_SIGNED_INT8, "CL_SIGNED_INT8" },
{ CL_SIGNED_INT16, "CL_SIGNED_INT16" },
{ CL_SIGNED_INT32, "CL_SIGNED_INT32" },
{ CL_UNSIGNED_INT8, "CL_UNSIGNED_INT8" },
{ CL_UNSIGNED_INT16, "CL_UNSIGNED_INT16" },
{ CL_UNSIGNED_INT32, "CL_UNSIGNED_INT32" },
{ CL_HALF_FLOAT, "CL_HALF_FLOAT" },
{ CL_FLOAT, "CL_FLOAT" },
#ifdef CL_SFIXED14_APPLE
{ CL_SFIXED14_APPLE, "CL_SFIXED14_APPLE" }
#endif
};
for (size_t i = 0; i < sizeof(typeNames) / sizeof(typeNames[0]); i++)
{
if (strcmp(typeNames[i].name, name) == 0
|| strcmp(typeNames[i].name + 3, name) == 0)
return typeNames[i].type;
}
return (cl_channel_type)-1;
}
cl_channel_order get_channel_order_from_name(const char *name)
{
const struct
{
cl_channel_order order;
const char *name;
} orderNames[] = {
{ CL_R, "CL_R" },
{ CL_A, "CL_A" },
{ CL_Rx, "CL_Rx" },
{ CL_RG, "CL_RG" },
{ CL_RA, "CL_RA" },
{ CL_RGx, "CL_RGx" },
{ CL_RGB, "CL_RGB" },
{ CL_RGBx, "CL_RGBx" },
{ CL_RGBA, "CL_RGBA" },
{ CL_BGRA, "CL_BGRA" },
{ CL_ARGB, "CL_ARGB" },
{ CL_INTENSITY, "CL_INTENSITY" },
{ CL_LUMINANCE, "CL_LUMINANCE" },
{ CL_DEPTH, "CL_DEPTH" },
{ CL_DEPTH_STENCIL, "CL_DEPTH_STENCIL" },
{ CL_sRGB, "CL_sRGB" },
{ CL_sRGBx, "CL_sRGBx" },
{ CL_sRGBA, "CL_sRGBA" },
{ CL_sBGRA, "CL_sBGRA" },
{ CL_ABGR, "CL_ABGR" },
#ifdef CL_1RGB_APPLE
{ CL_1RGB_APPLE, "CL_1RGB_APPLE" },
#endif
#ifdef CL_BGR1_APPLE
{ CL_BGR1_APPLE, "CL_BGR1_APPLE" },
#endif
};
for (size_t i = 0; i < sizeof(orderNames) / sizeof(orderNames[0]); i++)
{
if (strcmp(orderNames[i].name, name) == 0
|| strcmp(orderNames[i].name + 3, name) == 0)
return orderNames[i].order;
}
return (cl_channel_order)-1;
}
int is_format_signed(const cl_image_format *format)
{
switch (format->image_channel_data_type)
{
case CL_SNORM_INT8:
case CL_SIGNED_INT8:
case CL_SNORM_INT16:
case CL_SIGNED_INT16:
case CL_SIGNED_INT32:
case CL_HALF_FLOAT:
case CL_FLOAT:
#ifdef CL_SFIXED14_APPLE
case CL_SFIXED14_APPLE:
#endif
return 1;
default: return 0;
}
}
uint32_t get_pixel_size(const cl_image_format *format)
{
switch (format->image_channel_data_type)
{
case CL_SNORM_INT8:
case CL_UNORM_INT8:
case CL_SIGNED_INT8:
case CL_UNSIGNED_INT8: return get_format_channel_count(format);
case CL_SNORM_INT16:
case CL_UNORM_INT16:
case CL_SIGNED_INT16:
case CL_UNSIGNED_INT16:
case CL_HALF_FLOAT:
#ifdef CL_SFIXED14_APPLE
case CL_SFIXED14_APPLE:
#endif
return get_format_channel_count(format) * sizeof(cl_ushort);
case CL_SIGNED_INT32:
case CL_UNSIGNED_INT32:
return get_format_channel_count(format) * sizeof(cl_int);
case CL_UNORM_SHORT_565:
case CL_UNORM_SHORT_555: return 2;
case CL_UNORM_INT_101010: return 4;
case CL_FLOAT:
return get_format_channel_count(format) * sizeof(cl_float);
case CL_UNORM_INT_101010_2: return 4;
case CL_UNSIGNED_INT_RAW10_EXT:
case CL_UNSIGNED_INT_RAW12_EXT: return 2;
default: return 0;
}
}
uint32_t next_power_of_two(uint32_t v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
uint32_t get_pixel_alignment(const cl_image_format *format)
{
return next_power_of_two(get_pixel_size(format));
}
int get_8_bit_image_format(cl_context context, cl_mem_object_type objType,
cl_mem_flags flags, size_t channelCount,
cl_image_format *outFormat)
{
cl_image_format formatList[128];
unsigned int outFormatCount, i;
int error;
/* Make sure each image format is supported */
if ((error = clGetSupportedImageFormats(context, flags, objType, 128,
formatList, &outFormatCount)))
return error;
/* Look for one that is an 8-bit format */
for (i = 0; i < outFormatCount; i++)
{
if (formatList[i].image_channel_data_type == CL_SNORM_INT8
|| formatList[i].image_channel_data_type == CL_UNORM_INT8
|| formatList[i].image_channel_data_type == CL_SIGNED_INT8
|| formatList[i].image_channel_data_type == CL_UNSIGNED_INT8)
{
if (!channelCount
|| (channelCount
&& (get_format_channel_count(&formatList[i])
== channelCount)))
{
*outFormat = formatList[i];
return 0;
}
}
}
return -1;
}
int get_32_bit_image_format(cl_context context, cl_mem_object_type objType,
cl_mem_flags flags, size_t channelCount,
cl_image_format *outFormat)
{
cl_image_format formatList[128];
unsigned int outFormatCount, i;
int error;
/* Make sure each image format is supported */
if ((error = clGetSupportedImageFormats(context, flags, objType, 128,
formatList, &outFormatCount)))
return error;
/* Look for one that is an 8-bit format */
for (i = 0; i < outFormatCount; i++)
{
if (formatList[i].image_channel_data_type == CL_UNORM_INT_101010
|| formatList[i].image_channel_data_type == CL_FLOAT
|| formatList[i].image_channel_data_type == CL_SIGNED_INT32
|| formatList[i].image_channel_data_type == CL_UNSIGNED_INT32)
{
if (!channelCount
|| (channelCount
&& (get_format_channel_count(&formatList[i])
== channelCount)))
{
*outFormat = formatList[i];
return 0;
}
}
}
return -1;
}
void print_first_pixel_difference_error(size_t where, const char *sourcePixel,
const char *destPixel,
image_descriptor *imageInfo, size_t y,
size_t thirdDim)
{
size_t pixel_size = get_pixel_size(imageInfo->format);
log_error("ERROR: Scanline %d did not verify for image size %d,%d,%d "
"pitch %d (extra %d bytes)\n",
(int)y, (int)imageInfo->width, (int)imageInfo->height,
(int)thirdDim, (int)imageInfo->rowPitch,
(int)imageInfo->rowPitch
- (int)imageInfo->width * (int)pixel_size);
log_error("Failed at column: %zu ", where);
switch (pixel_size)
{
case 1:
log_error("*0x%2.2x vs. 0x%2.2x\n", ((cl_uchar *)sourcePixel)[0],
((cl_uchar *)destPixel)[0]);
break;
case 2:
log_error("*0x%4.4x vs. 0x%4.4x\n", ((cl_ushort *)sourcePixel)[0],
((cl_ushort *)destPixel)[0]);
break;
case 3:
log_error("*{0x%2.2x, 0x%2.2x, 0x%2.2x} vs. "
"{0x%2.2x, 0x%2.2x, 0x%2.2x}\n",
((cl_uchar *)sourcePixel)[0],
((cl_uchar *)sourcePixel)[1],
((cl_uchar *)sourcePixel)[2], ((cl_uchar *)destPixel)[0],
((cl_uchar *)destPixel)[1], ((cl_uchar *)destPixel)[2]);
break;
case 4:
log_error("*0x%8.8x vs. 0x%8.8x\n", ((cl_uint *)sourcePixel)[0],
((cl_uint *)destPixel)[0]);
break;
case 6:
log_error(
"*{0x%4.4x, 0x%4.4x, 0x%4.4x} vs. "
"{0x%4.4x, 0x%4.4x, 0x%4.4x}\n",
((cl_ushort *)sourcePixel)[0], ((cl_ushort *)sourcePixel)[1],
((cl_ushort *)sourcePixel)[2], ((cl_ushort *)destPixel)[0],
((cl_ushort *)destPixel)[1], ((cl_ushort *)destPixel)[2]);
break;
case 8:
log_error("*0x%16.16" PRIx64 " vs. 0x%16.16" PRIx64 "\n",
((cl_ulong *)sourcePixel)[0], ((cl_ulong *)destPixel)[0]);
break;
case 12:
log_error("*{0x%8.8x, 0x%8.8x, 0x%8.8x} vs. "
"{0x%8.8x, 0x%8.8x, 0x%8.8x}\n",
((cl_uint *)sourcePixel)[0], ((cl_uint *)sourcePixel)[1],
((cl_uint *)sourcePixel)[2], ((cl_uint *)destPixel)[0],
((cl_uint *)destPixel)[1], ((cl_uint *)destPixel)[2]);
break;
case 16:
log_error("*{0x%8.8x, 0x%8.8x, 0x%8.8x, 0x%8.8x} vs. "
"{0x%8.8x, 0x%8.8x, 0x%8.8x, 0x%8.8x}\n",
((cl_uint *)sourcePixel)[0], ((cl_uint *)sourcePixel)[1],
((cl_uint *)sourcePixel)[2], ((cl_uint *)sourcePixel)[3],
((cl_uint *)destPixel)[0], ((cl_uint *)destPixel)[1],
((cl_uint *)destPixel)[2], ((cl_uint *)destPixel)[3]);
break;
default:
log_error("Don't know how to print pixel size of %zu\n",
pixel_size);
break;
}
}
size_t compare_scanlines(const image_descriptor *imageInfo, const char *aPtr,
const char *bPtr)
{
size_t pixel_size = get_pixel_size(imageInfo->format);
size_t column;
for (column = 0; column < imageInfo->width; column++)
{
switch (imageInfo->format->image_channel_data_type)
{
// If the data type is 101010, then ignore bits 31 and 32 when
// comparing the row
case CL_UNORM_INT_101010: {
cl_uint aPixel = *(cl_uint *)aPtr;
cl_uint bPixel = *(cl_uint *)bPtr;
if ((aPixel & 0x3fffffff) != (bPixel & 0x3fffffff))
return column;
}
break;
// If the data type is 555, ignore bit 15 when comparing the row
case CL_UNORM_SHORT_555: {
cl_ushort aPixel = *(cl_ushort *)aPtr;
cl_ushort bPixel = *(cl_ushort *)bPtr;
if ((aPixel & 0x7fff) != (bPixel & 0x7fff)) return column;
}
break;
default:
if (memcmp(aPtr, bPtr, pixel_size) != 0) return column;
break;
}
aPtr += pixel_size;
bPtr += pixel_size;
}
// If we didn't find a difference, return the width of the image
return column;
}
int random_log_in_range(int minV, int maxV, MTdata d)
{
double v = log2(((double)genrand_int32(d) / (double)0xffffffff) + 1);
int iv = (int)((float)(maxV - minV) * v);
return iv + minV;
}
// Define the addressing functions
typedef int (*AddressFn)(int value, size_t maxValue);
int NoAddressFn(int value, size_t maxValue) { return value; }
int RepeatAddressFn(int value, size_t maxValue)
{
if (value < 0)
value += (int)maxValue;
else if (value >= (int)maxValue)
value -= (int)maxValue;
return value;
}
int MirroredRepeatAddressFn(int value, size_t maxValue)
{
if (value < 0)
value = 0;
else if ((size_t)value >= maxValue)
value = (int)(maxValue - 1);
return value;
}
int ClampAddressFn(int value, size_t maxValue)
{
return (value < -1) ? -1
: ((value > (cl_long)maxValue) ? (int)maxValue : value);
}
int ClampToEdgeNearestFn(int value, size_t maxValue)
{
return (value < 0)
? 0
: (((size_t)value > maxValue - 1) ? (int)maxValue - 1 : value);
}
AddressFn ClampToEdgeLinearFn = ClampToEdgeNearestFn;
// Note: normalized coords get repeated in normalized space, not unnormalized
// space! hence the special case here
volatile float gFloatHome;
float RepeatNormalizedAddressFn(float fValue, size_t maxValue)
{
#ifndef _MSC_VER // Use original if not the VS compiler.
// General computation for repeat
return (fValue - floorf(fValue)) * (float)maxValue; // Reduce to [0, 1.f]
#else // Otherwise, use this instead:
// Home the subtraction to a float to break up the sequence of x87
// instructions emitted by the VS compiler.
gFloatHome = fValue - floorf(fValue);
return gFloatHome * (float)maxValue;
#endif
}
float MirroredRepeatNormalizedAddressFn(float fValue, size_t maxValue)
{
// Round to nearest multiple of two.
// Note halfway values flip flop here due to rte, but they both end up
// pointing the same place at the end of the day.
float s_prime = 2.0f * rintf(fValue * 0.5f);
// Reduce to [-1, 1], Apply mirroring -> [0, 1]
s_prime = fabsf(fValue - s_prime);
// un-normalize
return s_prime * (float)maxValue;
}
struct AddressingTable
{
AddressingTable()
{
static_assert(CL_ADDRESS_MIRRORED_REPEAT - CL_ADDRESS_NONE < 6, "");
static_assert(CL_FILTER_NEAREST - CL_FILTER_LINEAR < 2, "");
mTable[CL_ADDRESS_NONE - CL_ADDRESS_NONE]
[CL_FILTER_NEAREST - CL_FILTER_NEAREST] = NoAddressFn;
mTable[CL_ADDRESS_NONE - CL_ADDRESS_NONE]
[CL_FILTER_LINEAR - CL_FILTER_NEAREST] = NoAddressFn;
mTable[CL_ADDRESS_REPEAT - CL_ADDRESS_NONE]
[CL_FILTER_NEAREST - CL_FILTER_NEAREST] = RepeatAddressFn;
mTable[CL_ADDRESS_REPEAT - CL_ADDRESS_NONE]
[CL_FILTER_LINEAR - CL_FILTER_NEAREST] = RepeatAddressFn;
mTable[CL_ADDRESS_CLAMP_TO_EDGE - CL_ADDRESS_NONE]
[CL_FILTER_NEAREST - CL_FILTER_NEAREST] = ClampToEdgeNearestFn;
mTable[CL_ADDRESS_CLAMP_TO_EDGE - CL_ADDRESS_NONE]
[CL_FILTER_LINEAR - CL_FILTER_NEAREST] = ClampToEdgeLinearFn;
mTable[CL_ADDRESS_CLAMP - CL_ADDRESS_NONE]
[CL_FILTER_NEAREST - CL_FILTER_NEAREST] = ClampAddressFn;
mTable[CL_ADDRESS_CLAMP - CL_ADDRESS_NONE]
[CL_FILTER_LINEAR - CL_FILTER_NEAREST] = ClampAddressFn;
mTable[CL_ADDRESS_MIRRORED_REPEAT - CL_ADDRESS_NONE]
[CL_FILTER_NEAREST - CL_FILTER_NEAREST] = MirroredRepeatAddressFn;
mTable[CL_ADDRESS_MIRRORED_REPEAT - CL_ADDRESS_NONE]
[CL_FILTER_LINEAR - CL_FILTER_NEAREST] = MirroredRepeatAddressFn;
}
AddressFn operator[](image_sampler_data *sampler)
{
return mTable[(int)sampler->addressing_mode - CL_ADDRESS_NONE]
[(int)sampler->filter_mode - CL_FILTER_NEAREST];
}
AddressFn mTable[6][2];
};
static AddressingTable sAddressingTable;
bool is_sRGBA_order(cl_channel_order image_channel_order)
{
switch (image_channel_order)
{
case CL_sRGB:
case CL_sRGBx:
case CL_sRGBA:
case CL_sBGRA: return true;
default: return false;
}
}
// Format helpers
int has_alpha(const cl_image_format *format)
{
switch (format->image_channel_order)
{
case CL_R: return 0;
case CL_A: return 1;
case CL_Rx: return 0;
case CL_RG: return 0;
case CL_RA: return 1;
case CL_RGx: return 0;
case CL_RGB:
case CL_sRGB: return 0;
case CL_RGBx:
case CL_sRGBx: return 0;
case CL_RGBA: return 1;
case CL_BGRA: return 1;
case CL_ARGB: return 1;
case CL_ABGR: return 1;
case CL_INTENSITY: return 1;
case CL_LUMINANCE: return 0;
#ifdef CL_BGR1_APPLE
case CL_BGR1_APPLE: return 1;
#endif
#ifdef CL_1RGB_APPLE
case CL_1RGB_APPLE: return 1;
#endif
case CL_sRGBA:
case CL_sBGRA: return 1;
case CL_DEPTH: return 0;
default:
log_error("Invalid image channel order: %d\n",
format->image_channel_order);
return 0;
}
}
#define PRINT_MAX_SIZE_LOGIC 0
#define SWAP(_a, _b) \
do \
{ \
_a ^= _b; \
_b ^= _a; \
_a ^= _b; \
} while (0)
void get_max_sizes(
size_t *numberOfSizes, const int maxNumberOfSizes, size_t sizes[][3],
size_t maxWidth, size_t maxHeight, size_t maxDepth, size_t maxArraySize,
const cl_ulong maxIndividualAllocSize, // CL_DEVICE_MAX_MEM_ALLOC_SIZE
const cl_ulong maxTotalAllocSize, // CL_DEVICE_GLOBAL_MEM_SIZE
cl_mem_object_type image_type, const cl_image_format *format,
int usingMaxPixelSizeBuffer)
{
bool is3D = (image_type == CL_MEM_OBJECT_IMAGE3D);
bool isArray = (image_type == CL_MEM_OBJECT_IMAGE1D_ARRAY
|| image_type == CL_MEM_OBJECT_IMAGE2D_ARRAY);
// Validate we have a reasonable max depth for 3D
if (is3D && maxDepth < 2)
{
log_error("ERROR: Requesting max image sizes for 3D images when max "
"depth is < 2.\n");
*numberOfSizes = 0;
return;
}
// Validate we have a reasonable max array size for 1D & 2D image arrays
if (isArray && maxArraySize < 2)
{
log_error("ERROR: Requesting max image sizes for an image array when "
"max array size is < 1.\n");
*numberOfSizes = 0;
return;
}
// Reduce the maximum because we are trying to test the max image
// dimensions, not the memory allocation
cl_ulong adjustedMaxTotalAllocSize = maxTotalAllocSize / 4;
cl_ulong adjustedMaxIndividualAllocSize = maxIndividualAllocSize / 4;
log_info("Note: max individual allocation adjusted down from %gMB to %gMB "
"and max total allocation adjusted down from %gMB to %gMB.\n",
maxIndividualAllocSize / (1024.0 * 1024.0),
adjustedMaxIndividualAllocSize / (1024.0 * 1024.0),
maxTotalAllocSize / (1024.0 * 1024.0),
adjustedMaxTotalAllocSize / (1024.0 * 1024.0));
// Cap our max allocation to 1.0GB.
// FIXME -- why? In the interest of not taking a long time? We should
// still test this stuff...
if (adjustedMaxTotalAllocSize > (cl_ulong)1024 * 1024 * 1024)
{
adjustedMaxTotalAllocSize = (cl_ulong)1024 * 1024 * 1024;
log_info("Limiting max total allocation size to %gMB (down from %gMB) "
"for test.\n",
adjustedMaxTotalAllocSize / (1024.0 * 1024.0),
maxTotalAllocSize / (1024.0 * 1024.0));
}
cl_ulong maxAllocSize = adjustedMaxIndividualAllocSize;
if (adjustedMaxTotalAllocSize < adjustedMaxIndividualAllocSize * 2)
maxAllocSize = adjustedMaxTotalAllocSize / 2;
size_t raw_pixel_size = get_pixel_size(format);
// If the test will be creating input (src) buffer of type int4 or float4,
// number of pixels will be governed by sizeof(int4 or float4) and not
// sizeof(dest fomat) Also if pixel size is 12 bytes i.e. RGB or RGBx, we
// adjust it to 16 bytes as GPUs has no concept of 3 channel images. GPUs
// expand these to four channel RGBA.
if (usingMaxPixelSizeBuffer || raw_pixel_size == 12) raw_pixel_size = 16;
size_t max_pixels = (size_t)maxAllocSize / raw_pixel_size;
log_info("Maximums: [%zu x %zu x %zu], raw pixel size %zu bytes, "
"per-allocation limit %gMB.\n",
maxWidth, maxHeight, isArray ? maxArraySize : maxDepth,
raw_pixel_size, (maxAllocSize / (1024.0 * 1024.0)));
// Keep track of the maximum sizes for each dimension
size_t maximum_sizes[] = { maxWidth, maxHeight, maxDepth };
switch (image_type)
{
case CL_MEM_OBJECT_IMAGE1D_ARRAY:
maximum_sizes[1] = maxArraySize;
maximum_sizes[2] = 1;
break;
case CL_MEM_OBJECT_IMAGE2D_ARRAY:
maximum_sizes[2] = maxArraySize;
break;
}
// Given one fixed sized dimension, this code finds one or two other
// dimensions, both with very small size, such that the size does not
// exceed the maximum passed to this function
#if defined(__x86_64) || defined(__arm64__) || defined(__ppc64__)
size_t other_sizes[] = { 2, 3, 5, 6, 7, 9, 10, 11, 13, 15 };
#else
size_t other_sizes[] = { 2, 3, 5, 6, 7, 9, 11, 13 };
#endif
static size_t other_size = 0;
enum
{
num_other_sizes = sizeof(other_sizes) / sizeof(size_t)
};
(*numberOfSizes) = 0;
if (image_type == CL_MEM_OBJECT_IMAGE1D
|| image_type == CL_MEM_OBJECT_IMAGE1D_BUFFER)
{
size_t M = maximum_sizes[0];
size_t A = max_pixels;
M = static_cast<size_t>(fmax(1, fmin(A / M, M)));
// Store the size
sizes[(*numberOfSizes)][0] = M;
sizes[(*numberOfSizes)][1] = 1;
sizes[(*numberOfSizes)][2] = 1;
++(*numberOfSizes);
}
else if (image_type == CL_MEM_OBJECT_IMAGE1D_ARRAY
|| image_type == CL_MEM_OBJECT_IMAGE2D)
{
for (int fixed_dim = 0; fixed_dim < 2; ++fixed_dim)
{
// Determine the size of the fixed dimension
size_t M = maximum_sizes[fixed_dim];
size_t A = max_pixels;
int x0_dim = !fixed_dim;
size_t x0 = static_cast<size_t>(
fmin(fmin(other_sizes[(other_size++) % num_other_sizes], A / M),
maximum_sizes[x0_dim]));
// Store the size
sizes[(*numberOfSizes)][fixed_dim] = M;
sizes[(*numberOfSizes)][x0_dim] = x0;
sizes[(*numberOfSizes)][2] = 1;
++(*numberOfSizes);
}
}
else if (image_type == CL_MEM_OBJECT_IMAGE2D_ARRAY
|| image_type == CL_MEM_OBJECT_IMAGE3D)
{
// Iterate over dimensions, finding sizes for the non-fixed dimension
for (int fixed_dim = 0; fixed_dim < 3; ++fixed_dim)
{
// Determine the size of the fixed dimension
size_t M = maximum_sizes[fixed_dim];
size_t A = max_pixels;
// Find two other dimensions, x0 and x1
int x0_dim = (fixed_dim == 0) ? 1 : 0;
int x1_dim = (fixed_dim == 2) ? 1 : 2;
// Choose two other sizes for these dimensions
size_t x0 = static_cast<size_t>(
fmin(fmin(A / M, maximum_sizes[x0_dim]),
other_sizes[(other_size++) % num_other_sizes]));
// GPUs have certain restrictions on minimum width (row alignment)
// of images which has given us issues testing small widths in this
// test (say we set width to 3 for testing, and compute size based
// on this width and decide it fits within vram ... but GPU driver
// decides that, due to row alignment requirements, it has to use
// width of 16 which doesnt fit in vram). For this purpose we are
// not testing width < 16 for this test.
if (x0_dim == 0 && x0 < 16) x0 = 16;
size_t x1 = static_cast<size_t>(
fmin(fmin(A / M / x0, maximum_sizes[x1_dim]),
other_sizes[(other_size++) % num_other_sizes]));
// Valid image sizes cannot be below 1. Due to the workaround for
// the xo_dim where x0 is overidden to 16 there might not be enough
// space left for x1 dimension. This could be a fractional 0.x size
// that when cast to integer would result in a value 0. In these
// cases we clamp the size to a minimum of 1.
if (x1 < 1) x1 = 1;
// M and x0 cannot be '0' as they derive from clDeviceInfo calls
assert(x0 > 0 && M > 0);
// Store the size
sizes[(*numberOfSizes)][fixed_dim] = M;
sizes[(*numberOfSizes)][x0_dim] = x0;
sizes[(*numberOfSizes)][x1_dim] = x1;
++(*numberOfSizes);
}
}
// Log the results
for (int j = 0; j < (int)(*numberOfSizes); j++)
{
switch (image_type)
{
case CL_MEM_OBJECT_IMAGE1D_BUFFER:
case CL_MEM_OBJECT_IMAGE1D:
log_info(" size[%d] = [%zu] (%g MB image)\n", j, sizes[j][0],
raw_pixel_size * sizes[j][0] * sizes[j][1]
* sizes[j][2] / (1024.0 * 1024.0));
break;
case CL_MEM_OBJECT_IMAGE1D_ARRAY:
case CL_MEM_OBJECT_IMAGE2D:
log_info(" size[%d] = [%zu %zu] (%g MB image)\n", j,
sizes[j][0], sizes[j][1],
raw_pixel_size * sizes[j][0] * sizes[j][1]
* sizes[j][2] / (1024.0 * 1024.0));
break;
case CL_MEM_OBJECT_IMAGE2D_ARRAY:
case CL_MEM_OBJECT_IMAGE3D:
log_info(" size[%d] = [%zu %zu %zu] (%g MB image)\n", j,
sizes[j][0], sizes[j][1], sizes[j][2],
raw_pixel_size * sizes[j][0] * sizes[j][1]
* sizes[j][2] / (1024.0 * 1024.0));
break;
}
}
}
float get_max_absolute_error(const cl_image_format *format,
image_sampler_data *sampler)
{
if (sampler->filter_mode == CL_FILTER_NEAREST) return 0.0f;
switch (format->image_channel_data_type)
{
case CL_SNORM_INT8: return 1.0f / 127.0f;
case CL_UNORM_INT8: return 1.0f / 255.0f;
case CL_UNORM_INT16: return 1.0f / 65535.0f;
case CL_SNORM_INT16: return 1.0f / 32767.0f;
case CL_FLOAT: return CL_FLT_MIN;
#ifdef CL_SFIXED14_APPLE
case CL_SFIXED14_APPLE: return 0x1.0p-14f;
#endif
case CL_UNORM_SHORT_555:
case CL_UNORM_SHORT_565: return 1.0f / 31.0f;
default: return 0.0f;
}
}
float get_max_relative_error(const cl_image_format *format,
image_sampler_data *sampler, int is3D,
int isLinearFilter)
{
float maxError = 0.0f;
float sampleCount = 1.0f;
if (isLinearFilter) sampleCount = is3D ? 8.0f : 4.0f;
// Note that the ULP is defined here as the unit in the last place of the
// maximum magnitude sample used for filtering.
// Section 8.3
switch (format->image_channel_data_type)
{
// The spec allows 2 ulps of error for normalized formats
case CL_SNORM_INT8:
case CL_UNORM_INT8:
case CL_SNORM_INT16:
case CL_UNORM_INT16:
case CL_UNORM_SHORT_565:
case CL_UNORM_SHORT_555:
case CL_UNORM_INT_101010:
// Maximum sampling error for round to zero normalization based on
// multiplication by reciprocal (using reciprocal generated in
// round to +inf mode, so that 1.0 matches spec)
maxError = 2 * FLT_EPSILON * sampleCount;
break;
// If the implementation supports these formats then it will have to
// allow rounding error here too, because not all 32-bit ints are
// exactly representable in float
case CL_SIGNED_INT32:
case CL_UNSIGNED_INT32: maxError = 1 * FLT_EPSILON; break;
}
// Section 8.2
if (sampler->addressing_mode == CL_ADDRESS_REPEAT
|| sampler->addressing_mode == CL_ADDRESS_MIRRORED_REPEAT
|| sampler->filter_mode != CL_FILTER_NEAREST
|| sampler->normalized_coords)
#if defined(__APPLE__)
{
if (sampler->filter_mode != CL_FILTER_NEAREST)
{
// The maximum
if (gDeviceType == CL_DEVICE_TYPE_GPU)
// Some GPUs ain't so accurate
maxError += MAKE_HEX_FLOAT(0x1.0p-4f, 0x1L, -4);
else
// The standard method of 2d linear filtering delivers 4.0 ulps
// of error in round to nearest (8 in rtz).
maxError += 4.0f * FLT_EPSILON;
}
else
// normalized coordinates will introduce some error into the
// fractional part of the address, affecting results
maxError += 4.0f * FLT_EPSILON;
}
#else
{
#if !defined(_WIN32)
#warning Implementations will likely wish to pick a max allowable sampling error policy here that is better than the spec
#endif
// The spec allows linear filters to return any result most of the time.
// That's fine for implementations but a problem for testing. After all
// users aren't going to like garbage images. We have "picked a number"
// here that we are going to attempt to conform to. Implementations are
// free to pick another number, like infinity, if they like.
// We picked a number for you, to provide /some/ sanity
maxError = MAKE_HEX_FLOAT(0x1.0p-7f, 0x1L, -7);
// ...but this is what the spec allows:
// maxError = INFINITY;
// Please feel free to pick any positive number. (NaN wont work.)
}
#endif
// The error calculation itself can introduce error
maxError += FLT_EPSILON * 2;
return maxError;
}
size_t get_format_max_int(const cl_image_format *format)
{
switch (format->image_channel_data_type)