forked from jbikker/tinybvh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny_ocl.h
1239 lines (1191 loc) · 50.6 KB
/
tiny_ocl.h
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
/*
The MIT License (MIT)
Copyright (c) 2024-2025, Jacco Bikker / Breda University of Applied Sciences.
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.
*/
// Mar 03, '25: version 0.2.0 : MacOS support, by wuyakuma
// Nov 18, '24: version 0.1.1 : Added custom alloc/free.
// Nov 15, '24: version 0.1.0 : Accidentally started another tiny lib.
//
// Use this in *one* .c or .cpp
// #define TINY_OCL_IMPLEMENTATION
// #include "tiny_ocl.h"
// To enable OpenGL interop, define TINY_OCL_GLINTEROP before the include:
// #define TINY_OCL_GLINTEROP
// #define TINY_OCL_IMPLEMENTATION
// #include "tiny_ocl.h"
//
#ifndef TINY_OCL_H_
#define TINY_OCL_H_
#define CL_TARGET_OPENCL_VERSION 300
#include <cl.h>
#include <vector>
// aligned memory allocation
// note: formally size needs to be a multiple of 'alignment'. See:
// https://en.cppreference.com/w/c/memory/aligned_alloc
// EMSCRIPTEN enforces this.
// Copy of the same construct in tinybvh, different namespace.
namespace tinyocl {
inline size_t make_multiple_64( size_t x ) { return (x + 63) & ~0x3f; }
}
#ifdef _MSC_VER // Visual Studio / C11
#define ALIGNED( x ) __declspec( align( x ) )
namespace tinyocl {
inline void* malloc64( size_t size, void* = nullptr )
{
return size == 0 ? 0 : _aligned_malloc( make_multiple_64( size ), 64 );
}
inline void free64( void* ptr, void* = nullptr ) { _aligned_free( ptr ); }
}
#else // EMSCRIPTEN / gcc / clang
#define ALIGNED( x ) __attribute__( ( aligned( x ) ) )
#if defined(__x86_64__) || defined(_M_X64) || defined(__wasm_simd128__) || defined(__wasm_relaxed_simd__)
#include <xmmintrin.h>
namespace tinyocl {
inline void* malloc64( size_t size, void* = nullptr )
{
return size == 0 ? 0 : _mm_malloc( make_multiple_64( size ), 64 );
}
inline void free64( void* ptr, void* = nullptr ) { _mm_free( ptr ); }
}
#else
namespace tinyocl {
inline void* malloc64( size_t size, void* = nullptr )
{
return size == 0 ? 0 : aligned_alloc( 64, make_multiple_64( size ) );
}
inline void free64( void* ptr, void* = nullptr ) { free( ptr ); }
}
#endif
#endif
namespace tinyocl {
// Math classes
struct oclint2
{
int x, y; oclint2() = default;
oclint2( const int a, const int b ) : x( a ), y( b ) {}
oclint2( const int a ) : x( a ), y( a ) {}
};
struct oclvec3 { float x, y, z; };
// ============================================================================
//
// T I N Y _ O C L I N T E R F A C E
//
// ============================================================================
// OpenCL context
// *Only!* in case you want to override the default memory allocator used by
// tinyocl::Buffer: call OpenCL::CreateInstance with OpenCLContext fields describing
// your allocator and deallocator, before using any tinyocl functionality.
// In all other cases, the first use of tinyocl (creating a buffer, loading a
// kernel) will take care of this for you transparently.
struct OpenCLContext
{
void* (*malloc)(size_t size, void* userdata) = malloc64;
void (*free)(void* ptr, void* userdata) = free64;
void* userdata = nullptr;
};
class OpenCL
{
public:
OpenCL( OpenCLContext ctx = {} ) : context( ctx ) { ocl = this; }
OpenCLContext context;
void* AlignedAlloc( size_t size );
void AlignedFree( void* ptr );
static void CreateInstance( OpenCLContext ctx ) { ocl = new OpenCL( ctx ); }
static OpenCL* GetInstance() { return ocl; }
inline static OpenCL* ocl = 0;
};
// OpenCL buffer
class Buffer
{
public:
enum { DEFAULT = 0, TEXTURE = 8, TARGET = 16, READONLY = 1, WRITEONLY = 2 };
// constructor / destructor
Buffer() : hostBuffer( 0 ) {}
Buffer( unsigned int N, void* ptr = 0, unsigned int t = DEFAULT );
~Buffer();
cl_mem* GetDevicePtr() { return &deviceBuffer; }
unsigned int* GetHostPtr();
void CopyToDevice( const bool blocking = true );
void CopyToDevice( const int offset, const int size, const bool blocking = true );
void CopyToDevice2( bool blocking, cl_event* e = 0, const size_t s = 0 );
void CopyFromDevice( const bool blocking = true );
void CopyFromDevice( const int offset, const int size, const bool blocking = true );
void CopyTo( Buffer* buffer );
void Clear();
private:
// data members
public:
unsigned int* hostBuffer;
cl_mem deviceBuffer = 0;
unsigned int type, size /* in bytes */, textureID;
bool ownData, aligned;
};
// OpenCL kernel
class Kernel
{
friend class Buffer;
public:
// constructor / destructor
Kernel( const char* file, const char* entryPoint );
Kernel( cl_program& existingProgram, char* entryPoint );
~Kernel();
// get / set
cl_kernel& GetKernel() { return kernel; }
cl_program& GetProgram() { return program; }
static cl_command_queue& GetQueue() { return queue; }
static cl_command_queue& GetQueue2() { return queue2; }
static cl_context& GetContext() { return context; }
static cl_device_id& GetDevice() { return device; }
static OpenCL ocl;
// run methods
#if 1
void Run( cl_event* eventToWaitFor = 0, cl_event* eventToSet = 0 );
void Run( cl_mem* buffers, const int count = 1, cl_event* eventToWaitFor = 0, cl_event* eventToSet = 0, cl_event* acq = 0, cl_event* rel = 0 );
void Run( Buffer* buffer, const oclint2 localSize = oclint2( 32, 2 ), cl_event* eventToWaitFor = 0, cl_event* eventToSet = 0, cl_event* acq = 0, cl_event* rel = 0 );
void Run( Buffer* buffer, const int count = 1, cl_event* eventToWaitFor = 0, cl_event* eventToSet = 0, cl_event* acq = 0, cl_event* rel = 0 );
#endif
void Run( const size_t count, const size_t localSize = 0, cl_event* eventToWaitFor = 0, cl_event* eventToSet = 0 );
void Run2D( const oclint2 count, const oclint2 lsize = 0, cl_event* eventToWaitFor = 0, cl_event* eventToSet = 0 );
// Argument passing with template trickery; up to 20 arguments in a single call;
// each argument may be of any of the supported types. Approach borrowed from NVIDIA/CUDA.
#define T_ typename
template<T_ A> void SetArguments( A a ) { InitArgs(); SetArgument( 0, a ); }
template<T_ A, T_ B> void SetArguments( A a, B b ) { InitArgs(); S( 0, a ); S( 1, b ); }
template<T_ A, T_ B, T_ C> void SetArguments( A a, B b, C c ) { InitArgs(); S( 0, a ); S( 1, b ); S( 2, c ); }
template<T_ A, T_ B, T_ C, T_ D> void SetArguments( A a, B b, C c, D d ) { InitArgs(); S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); }
template<T_ A, T_ B, T_ C, T_ D, T_ E> void SetArguments( A a, B b, C c, D d, E e ) { InitArgs(); S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); }
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F> void SetArguments( A a, B b, C c, D d, E e, F f )
{
InitArgs(); S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G> void SetArguments( A a, B b, C c, D d, E e, F f, G g )
{
InitArgs(); S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H> void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h )
{
InitArgs(); S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I> void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i )
{
InitArgs(); S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h ); S( 8, i );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e );
S( 5, f ); S( 6, g ); S( 7, h ); S( 8, i ); S( 9, j );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f );
S( 6, g ); S( 7, h ); S( 8, i ); S( 9, j ); S( 10, k );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f );
S( 6, g ); S( 7, h ); S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g );
S( 7, h ); S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g );
S( 7, h ); S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O, T_ P>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o ), S( 15, p );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O, T_ P, T_ Q>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p, Q q )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o ), S( 15, p );
S( 16, q );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O, T_ P, T_ Q, T_ R>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p, Q q, R r )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o ), S( 15, p );
S( 16, q ), S( 17, r );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O, T_ P, T_ Q, T_ R, T_ T>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p, Q q, R r, T t )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o ), S( 15, p );
S( 16, q ), S( 17, r ), S( 18, t );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O, T_ P, T_ Q, T_ R, T_ T, T_ U>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p, Q q, R r, T t, U u )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o ), S( 15, p );
S( 16, q ), S( 17, r ), S( 18, t ), S( 19, u );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O, T_ P, T_ Q, T_ R, T_ T, T_ U, T_ V>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p, Q q, R r, T t, U u, V v )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o ), S( 15, p );
S( 16, q ), S( 17, r ), S( 18, t ), S( 19, u ), S( 20, v );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O, T_ P, T_ Q, T_ R, T_ T, T_ U, T_ V, T_ W>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p, Q q, R r, T t, U u, V v, W w )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o ), S( 15, p );
S( 16, q ), S( 17, r ), S( 18, t ), S( 19, u ), S( 20, v ), S( 21, w );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O, T_ P, T_ Q, T_ R, T_ T, T_ U, T_ V, T_ W, T_ X>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p, Q q, R r, T t, U u, V v, W w, X x )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o ), S( 15, p );
S( 16, q ), S( 17, r ), S( 18, t ), S( 19, u ), S( 20, v ), S( 21, w ), S( 22, x );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O, T_ P, T_ Q, T_ R, T_ T, T_ U, T_ V, T_ W, T_ X, T_ Y>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p, Q q, R r, T t, U u, V v, W w, X x, Y y )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o ), S( 15, p );
S( 16, q ), S( 17, r ), S( 18, t ), S( 19, u ), S( 20, v ), S( 21, w ), S( 22, x ), S( 23, y );
}
template<T_ A, T_ B, T_ C, T_ D, T_ E, T_ F, T_ G, T_ H, T_ I, T_ J, T_ K, T_ L, T_ M, T_ N, T_ O, T_ P, T_ Q, T_ R, T_ T, T_ U, T_ V, T_ W, T_ X, T_ Y, T_ Z>
void SetArguments( A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p, Q q, R r, T t, U u, V v, W w, X x, Y y, Z z )
{
InitArgs();
S( 0, a ); S( 1, b ); S( 2, c ); S( 3, d ); S( 4, e ); S( 5, f ); S( 6, g ); S( 7, h );
S( 8, i ); S( 9, j ); S( 10, k ); S( 11, l ); S( 12, m ), S( 13, n ); S( 14, o ), S( 15, p );
S( 16, q ), S( 17, r ), S( 18, t ), S( 19, u ), S( 20, v ), S( 21, w ), S( 22, x ), S( 23, y ), S( 24, z );
}
template<T_ T> void S( unsigned i, T t ) { SetArgument( i, t ); }
void InitArgs() { acqBuffer = 0; /* nothing to acquire until told otherwise */ }
#undef T_
private:
void SetArgument( int idx, cl_mem* buffer );
void SetArgument( int idx, Buffer* buffer );
void SetArgument( int idx, oclvec3 value ); // special case: vec3 needs padding to 16 bytes.
template<class T> void SetArgument( int idx, T value )
{
CheckCLStarted();
if constexpr (sizeof( T ) == 12)
{
// probably int3 / float3; pad to 16 bytes
unsigned tmp[4] = {};
memcpy( tmp, &value, 12 );
clSetKernelArg( kernel, idx, 16, &value );
}
else
{
clSetKernelArg( kernel, idx, sizeof( T ), &value );
}
}
// other methods
public:
static bool InitCL();
static void CheckCLStarted();
static void KillCL();
static cl_device_id GetDeviceID() { return device; }
private:
// data members
char* sourceFile = 0;
Buffer* acqBuffer = 0;
cl_kernel kernel;
cl_mem vbo_cl;
cl_program program;
inline static cl_device_id device;
inline static cl_context context; // simplifies some things, but limits us to one device
inline static cl_command_queue queue, queue2;
inline static char* log = 0;
inline static bool isNVidia = false, isAMD = false, isIntel = false, isApple = false, isOther = false;
inline static bool isAmpere = false, isTuring = false, isPascal = false;
inline static bool isAda = false, isBlackwell = false, isRubin = false, isHopper = false;
inline static int vendorLines = 0;
inline static std::vector<Kernel*> loadedKernels;
public:
inline static bool candoInterop = false, clStarted = false;
};
} // namespace tinybvh
#endif // TINY_OCL_H_
// ============================================================================
//
// I M P L E M E N T A T I O N
//
// ============================================================================
#ifdef TINY_OCL_IMPLEMENTATION
#ifdef _MSC_VER
#pragma comment( lib, "../external/OpenCL/lib/OpenCL.lib" )
#endif
#ifdef TINY_OCL_GLINTEROP
#include "cl_gl.h"
#include "cl_ext.h"
GLFWwindow* GetGLFWWindow(); // we need access to the glfw window..
#endif
using namespace std;
using namespace tinyocl;
#include <stdarg.h>
#ifdef _MSC_VER
#include <direct.h>
#define getcwd _getcwd
#define chdir _chdir
#else
#include <unistd.h>
#endif
#include <fstream>
#define CHECKCL(r) CheckCL( r, __FILE__, __LINE__ )
void FatalError( const char* fmt, ... )
{
char t[65536];
va_list args;
va_start( args, fmt );
vsnprintf( t, sizeof( t ) - 2, fmt, args );
va_end( args );
#ifdef _WINDOWS_ // i.e., windows.h has been included.
MessageBox( NULL, t, "Fatal error", MB_OK );
#else
fprintf( stderr, t );
#endif
while (1) exit( 0 );
}
static string ReadTextFile( const char* _File )
{
ifstream s( _File );
string str( (istreambuf_iterator<char>( s )), istreambuf_iterator<char>() );
s.close();
return str;
}
int LineCount( const string s )
{
const char* p = s.c_str();
int lines = 0;
while (*p) if (*p++ == '\n') lines++;
return lines;
}
// CHECKCL method
// OpenCL error handling.
// ----------------------------------------------------------------------------
bool CheckCL( cl_int result, const char* file, int line )
{
if (result == CL_SUCCESS) return true;
if (result == CL_DEVICE_NOT_FOUND) FatalError( "Error: CL_DEVICE_NOT_FOUND\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_DEVICE_NOT_AVAILABLE) FatalError( "Error: CL_DEVICE_NOT_AVAILABLE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_COMPILER_NOT_AVAILABLE) FatalError( "Error: CL_COMPILER_NOT_AVAILABLE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_MEM_OBJECT_ALLOCATION_FAILURE) FatalError( "Error: CL_MEM_OBJECT_ALLOCATION_FAILURE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_OUT_OF_RESOURCES) FatalError( "Error: CL_OUT_OF_RESOURCES\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_OUT_OF_HOST_MEMORY) FatalError( "Error: CL_OUT_OF_HOST_MEMORY\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_PROFILING_INFO_NOT_AVAILABLE) FatalError( "Error: CL_PROFILING_INFO_NOT_AVAILABLE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_MEM_COPY_OVERLAP) FatalError( "Error: CL_MEM_COPY_OVERLAP\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_IMAGE_FORMAT_MISMATCH) FatalError( "Error: CL_IMAGE_FORMAT_MISMATCH\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_IMAGE_FORMAT_NOT_SUPPORTED) FatalError( "Error: CL_IMAGE_FORMAT_NOT_SUPPORTED\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_BUILD_PROGRAM_FAILURE) FatalError( "Error: CL_BUILD_PROGRAM_FAILURE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_MAP_FAILURE) FatalError( "Error: CL_MAP_FAILURE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_MISALIGNED_SUB_BUFFER_OFFSET) FatalError( "Error: CL_MISALIGNED_SUB_BUFFER_OFFSET\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST) FatalError( "Error: CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_VALUE) FatalError( "Error: CL_INVALID_VALUE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_DEVICE_TYPE) FatalError( "Error: CL_INVALID_DEVICE_TYPE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_PLATFORM) FatalError( "Error: CL_INVALID_PLATFORM\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_DEVICE) FatalError( "Error: CL_INVALID_DEVICE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_CONTEXT) FatalError( "Error: CL_INVALID_CONTEXT\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_QUEUE_PROPERTIES) FatalError( "Error: CL_INVALID_QUEUE_PROPERTIES\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_COMMAND_QUEUE) FatalError( "Error: CL_INVALID_COMMAND_QUEUE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_HOST_PTR) FatalError( "Error: CL_INVALID_HOST_PTR\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_MEM_OBJECT) FatalError( "Error: CL_INVALID_MEM_OBJECT\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_IMAGE_FORMAT_DESCRIPTOR) FatalError( "Error: CL_INVALID_IMAGE_FORMAT_DESCRIPTOR\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_IMAGE_SIZE) FatalError( "Error: CL_INVALID_IMAGE_SIZE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_SAMPLER) FatalError( "Error: CL_INVALID_SAMPLER\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_BINARY) FatalError( "Error: CL_INVALID_BINARY\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_BUILD_OPTIONS) FatalError( "Error: CL_INVALID_BUILD_OPTIONS\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_PROGRAM) FatalError( "Error: CL_INVALID_PROGRAM\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_PROGRAM_EXECUTABLE) FatalError( "Error: CL_INVALID_PROGRAM_EXECUTABLE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_KERNEL_NAME) FatalError( "Error: CL_INVALID_KERNEL_NAME\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_KERNEL_DEFINITION) FatalError( "Error: CL_INVALID_KERNEL_DEFINITION\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_KERNEL) FatalError( "Error: CL_INVALID_KERNEL\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_ARG_INDEX) FatalError( "Error: CL_INVALID_ARG_INDEX\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_ARG_VALUE) FatalError( "Error: CL_INVALID_ARG_VALUE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_ARG_SIZE) FatalError( "Error: CL_INVALID_ARG_SIZE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_KERNEL_ARGS) FatalError( "Error: CL_INVALID_KERNEL_ARGS\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_WORK_DIMENSION) FatalError( "Error: CL_INVALID_WORK_DIMENSION\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_WORK_GROUP_SIZE) FatalError( "Error: CL_INVALID_WORK_GROUP_SIZE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_WORK_ITEM_SIZE) FatalError( "Error: CL_INVALID_WORK_ITEM_SIZE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_GLOBAL_OFFSET) FatalError( "Error: CL_INVALID_GLOBAL_OFFSET\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_EVENT_WAIT_LIST) FatalError( "Error: CL_INVALID_EVENT_WAIT_LIST\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_EVENT) FatalError( "Error: CL_INVALID_EVENT\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_OPERATION) FatalError( "Error: CL_INVALID_OPERATION\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_GL_OBJECT) FatalError( "Error: CL_INVALID_GL_OBJECT\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_BUFFER_SIZE) FatalError( "Error: CL_INVALID_BUFFER_SIZE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_MIP_LEVEL) FatalError( "Error: CL_INVALID_MIP_LEVEL\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_GLOBAL_WORK_SIZE) FatalError( "Error: CL_INVALID_GLOBAL_WORK_SIZE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_PROPERTY) FatalError( "Error: CL_INVALID_PROPERTY\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_IMAGE_DESCRIPTOR) FatalError( "Error: CL_INVALID_IMAGE_DESCRIPTOR\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_COMPILER_OPTIONS) FatalError( "Error: CL_INVALID_COMPILER_OPTIONS\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_LINKER_OPTIONS) FatalError( "Error: CL_INVALID_LINKER_OPTIONS\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_DEVICE_PARTITION_COUNT) FatalError( "Error: CL_INVALID_DEVICE_PARTITION_COUNT\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_PIPE_SIZE) FatalError( "Error: CL_INVALID_PIPE_SIZE\n%s, line %i", file, line, "OpenCL error" );
if (result == CL_INVALID_DEVICE_QUEUE) FatalError( "Error: CL_INVALID_DEVICE_QUEUE\n%s, line %i", file, line, "OpenCL error" );
return false;
}
// getFirstDevice
// ----------------------------------------------------------------------------
static cl_device_id getFirstDevice( cl_context context )
{
size_t dataSize;
cl_device_id* devices;
clGetContextInfo( context, CL_CONTEXT_DEVICES, 0, NULL, &dataSize );
devices = (cl_device_id*)malloc( dataSize );
clGetContextInfo( context, CL_CONTEXT_DEVICES, dataSize, devices, NULL );
cl_device_id first = devices[0];
free( devices );
return first;
}
// getPlatformID
// ----------------------------------------------------------------------------
static cl_int getPlatformID( cl_platform_id* platform )
{
char chBuffer[1024];
cl_uint num_platforms, devCount;
cl_platform_id* clPlatformIDs;
cl_int error;
*platform = NULL;
CHECKCL( error = clGetPlatformIDs( 0, NULL, &num_platforms ) );
if (num_platforms == 0) CHECKCL( -1 );
clPlatformIDs = (cl_platform_id*)malloc( num_platforms * sizeof( cl_platform_id ) );
error = clGetPlatformIDs( num_platforms, clPlatformIDs, NULL );
cl_uint deviceType[2] = { CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU };
const char* deviceOrder[2][3] = { { "NVIDIA", "AMD", "" }, { "", "", "" } };
fprintf( stderr, "available OpenCL platforms:\n" );
for (cl_uint i = 0; i < num_platforms; ++i)
{
CHECKCL( error = clGetPlatformInfo( clPlatformIDs[i], CL_PLATFORM_NAME, 1024, &chBuffer, NULL ) );
printf( "#%i: %s\n", i, chBuffer );
}
for (cl_uint j = 0; j < 2; j++) for (int k = 0; k < 3; k++) for (cl_uint i = 0; i < num_platforms; ++i)
{
error = clGetDeviceIDs( clPlatformIDs[i], deviceType[j], 0, NULL, &devCount );
if ((error != CL_SUCCESS) || (devCount == 0)) continue;
CHECKCL( error = clGetPlatformInfo( clPlatformIDs[i], CL_PLATFORM_NAME, 1024, &chBuffer, NULL ) );
if (deviceOrder[j][k][0]) if (!strstr( chBuffer, deviceOrder[j][k] )) continue;
fprintf( stderr, "OpenCL device: %s\n", chBuffer );
*platform = clPlatformIDs[i], j = 2, k = 3;
break;
}
free( clPlatformIDs );
return CL_SUCCESS;
}
// memory management
// ----------------------------------------------------------------------------
void* OpenCL::AlignedAlloc( size_t size )
{
return OpenCL::context.malloc ? OpenCL::context.malloc( size, OpenCL::context.userdata ) : nullptr;
}
void OpenCL::AlignedFree( void* ptr )
{
if (OpenCL::context.free)
OpenCL::context.free( ptr, OpenCL::context.userdata );
}
// Buffer constructor
// ----------------------------------------------------------------------------
Buffer::Buffer( unsigned int N, void* ptr, unsigned int t )
{
if (!Kernel::clStarted) Kernel::InitCL();
type = t;
ownData = false;
int rwFlags = CL_MEM_READ_WRITE;
if (t & READONLY) rwFlags = CL_MEM_READ_ONLY;
if (t & WRITEONLY) rwFlags = CL_MEM_WRITE_ONLY;
aligned = false;
if ((t & (TEXTURE | TARGET)) == 0)
{
size = N;
textureID = 0; // not representing a texture
if (N > 0) deviceBuffer = clCreateBuffer( Kernel::GetContext(), rwFlags, size, 0, 0 );
hostBuffer = (unsigned*)ptr;
}
else
{
textureID = N; // representing texture N
if (!Kernel::candoInterop) FatalError( "didn't expect to get here." );
int error = 0;
#ifdef TINY_OCL_GLINTEROP
if (t == TARGET) deviceBuffer = clCreateFromGLTexture( Kernel::GetContext(), CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, N, &error );
else deviceBuffer = clCreateFromGLTexture( Kernel::GetContext(), CL_MEM_READ_ONLY, GL_TEXTURE_2D, 0, N, &error );
#endif
CHECKCL( error );
hostBuffer = 0;
}
}
// Buffer destructor
// ----------------------------------------------------------------------------
Buffer::~Buffer()
{
if (size > 0)
{
if (ownData)
{
OpenCL::GetInstance()->AlignedFree( hostBuffer );
hostBuffer = 0;
}
if ((type & (TEXTURE | TARGET)) == 0) clReleaseMemObject( deviceBuffer );
}
}
// GetHostPtr method
// ----------------------------------------------------------------------------
unsigned int* Buffer::GetHostPtr()
{
if (size == 0) return 0;
if (!hostBuffer)
{
hostBuffer = (unsigned*)OpenCL::GetInstance()->AlignedAlloc( size );
ownData = true;
aligned = true;
}
return hostBuffer;
}
// CopyToDevice methods
// ----------------------------------------------------------------------------
void Buffer::CopyToDevice( const bool blocking )
{
if (size == 0) return;
cl_int error;
if (!hostBuffer)
{
hostBuffer = (unsigned*)OpenCL::GetInstance()->AlignedAlloc( size );
ownData = true;
aligned = true;
}
CHECKCL( error = clEnqueueWriteBuffer( Kernel::GetQueue(), deviceBuffer, blocking, 0, size, hostBuffer, 0, 0, 0 ) );
}
void Buffer::CopyToDevice( const int offset, const int byteCount, const bool blocking )
{
if (size == 0) return;
cl_int error;
if (!hostBuffer)
{
hostBuffer = (unsigned*)OpenCL::GetInstance()->AlignedAlloc( size );
ownData = true;
aligned = true;
}
CHECKCL( error = clEnqueueWriteBuffer( Kernel::GetQueue(), deviceBuffer, blocking, offset, byteCount, hostBuffer, 0, 0, 0 ) );
}
// CopyToDevice2 method (uses 2nd queue)
// ----------------------------------------------------------------------------
void Buffer::CopyToDevice2( const bool blocking, cl_event* eventToSet, const size_t s )
{
if (size == 0) return;
cl_int error;
CHECKCL( error = clEnqueueWriteBuffer( Kernel::GetQueue2(), deviceBuffer, blocking ? CL_TRUE : CL_FALSE, 0, s == 0 ? size : s, hostBuffer, 0, 0, eventToSet ) );
}
// CopyFromDevice method
// ----------------------------------------------------------------------------
void Buffer::CopyFromDevice( const bool blocking )
{
if (size == 0) return;
cl_int error;
if (!hostBuffer)
{
hostBuffer = (unsigned*)OpenCL::GetInstance()->AlignedAlloc( size );
ownData = true;
aligned = true;
}
CHECKCL( error = clEnqueueReadBuffer( Kernel::GetQueue(), deviceBuffer, blocking, 0, size, hostBuffer, 0, 0, 0 ) );
}
void Buffer::CopyFromDevice( const int offset, const int byteCount, const bool blocking )
{
if (size == 0) return;
cl_int error;
if (!hostBuffer)
{
hostBuffer = (unsigned*)OpenCL::GetInstance()->AlignedAlloc( size );
ownData = true;
aligned = true;
}
CHECKCL( error = clEnqueueReadBuffer( Kernel::GetQueue(), deviceBuffer, blocking, offset, byteCount, hostBuffer, 0, 0, 0 ) );
}
// CopyTo
// ----------------------------------------------------------------------------
void Buffer::CopyTo( Buffer* buffer )
{
if (size > 0) clEnqueueCopyBuffer( Kernel::GetQueue(), deviceBuffer, buffer->deviceBuffer, 0, 0, size, 0, 0, 0 );
}
// Clear
// ----------------------------------------------------------------------------
void Buffer::Clear()
{
if (size == 0) return;
unsigned value = 0;
cl_int error;
CHECKCL( error = clEnqueueFillBuffer( Kernel::GetQueue(), deviceBuffer, &value, 4, 0, size, 0, 0, 0 ) );
}
// Kernel constructor
// ----------------------------------------------------------------------------
Kernel::Kernel( const char* file, const char* entryPoint )
{
if (!clStarted) InitCL();
// see if we have seen this source file before
for (int s = (int)loadedKernels.size(), i = 0; i < s; i++)
{
#ifdef _MSC_VER
if (!_stricmp( file, loadedKernels[i]->sourceFile ))
#else
if (!strcasecmp( file, loadedKernels[i]->sourceFile ))
#endif
{
cl_int error;
program = loadedKernels[i]->program;
kernel = clCreateKernel( program, entryPoint, &error );
CHECKCL( error );
return;
}
}
// backup working folder
char dirBackup[2048];
getcwd( dirBackup, 2047 );
// change directory
char* dir = new char[strlen( file ) + 1], * lastSlash, * fileName = dir;
strcpy( dir, file );
lastSlash = strstr( dir, "/" );
if (!lastSlash) lastSlash = strstr( dir, "\\" );
while (lastSlash)
{
char* nextSlash = strstr( lastSlash + 1, "/" );
if (!nextSlash) nextSlash = strstr( lastSlash + 1, "\\" );
if (!nextSlash) break;
lastSlash = nextSlash;
}
if (lastSlash)
{
*lastSlash = 0;
fileName = lastSlash + 1;
chdir( dir );
}
// load a cl file
sourceFile = new char[strlen( file ) + 1];
strcpy( sourceFile, file );
string csText = ReadTextFile( fileName );
if (csText.size() == 0) FatalError( "File %s not found", file );
// add vendor defines
vendorLines = 0;
if (isNVidia) csText = "#define ISNVIDIA\n" + csText, vendorLines++;
if (isAMD) csText = "#define ISAMD\n" + csText, vendorLines++;
if (isIntel) csText = "#define ISINTEL\n" + csText, vendorLines++;
if (isApple) csText = "#define ISAPPLE\n" + csText, vendorLines++;
if (isOther) csText = "#define ISOTHER\n" + csText, vendorLines++;
if (isAmpere) csText = "#define ISAMPERE\n" + csText, vendorLines++;
if (isTuring) csText = "#define ISTURING\n" + csText, vendorLines++;
if (isPascal) csText = "#define ISPASCAL\n" + csText, vendorLines++;
if (isAda) csText = "#define ISADA\n" + csText, vendorLines++;
// expand #include directives: cl compiler doesn't support these natively
// warning: this simple system does not handle nested includes.
struct Include { int start, end; string file; } includes[64];
int Ninc = 0;
#if 1 // should not be needed, but AMD seems to require it anyway...
if (isAMD) while (1)
{
// see if any #includes remain
size_t pos = csText.find( "#include" );
if (pos == string::npos) break;
// start of expanded source construction
string tmp;
if (pos > 0)
tmp = csText.substr( 0, pos - 1 ) + "\n",
includes[Ninc].start = LineCount( tmp ); // record first line of #include content
else
includes[Ninc].start = 0;
// parse filename of include file
pos = csText.find( "\"", pos + 1 );
if (pos == string::npos) FatalError( "Expected \" after #include in shader." );
size_t end = csText.find( "\"", pos + 1 );
if (end == string::npos) FatalError( "Expected second \" after #include in shader." );
string incFile = csText.substr( pos + 1, end - pos - 1 );
// load include file content
string incText = ReadTextFile( incFile.c_str() );
includes[Ninc].end = includes[Ninc].start + LineCount( incText );
includes[Ninc++].file = incFile;
if (incText.size() == 0) FatalError( "#include file not found:\n%s", incFile.c_str() );
// cleanup include file content: we get some crap first sometimes, but why?
int firstValidChar = 0;
while (incText[firstValidChar] < 0) firstValidChar++;
// add include file content and remainder of source to expanded source string
tmp += incText.substr( firstValidChar, string::npos );
tmp += csText.substr( end + 1, string::npos ) + "\n";
// repeat until no #includes left
csText = tmp;
}
#endif
// attempt to compile the loaded source text
const char* source = csText.c_str();
size_t size = strlen( source );
cl_int error;
program = clCreateProgramWithSource( context, 1, (const char**)&source, &size, &error );
CHECKCL( error );
// why does the nvidia compiler not support these:
// -cl-nv-maxrregcount=64 not faster than leaving it out (same for 128)
// -cl-no-subgroup-ifp ? fails on nvidia.
// AMD-compatible compilation, thanks Rosalie de Winther
char buildString[1024];
strcpy( buildString, "-cl-std=CL2.0 " );
strcat( buildString, "-cl-strict-aliasing " );
strcat( buildString, "-cl-fast-relaxed-math " );
strcat( buildString, "-cl-single-precision-constant " );
// strcat( buildString, "-cl-uniform-work-group-size " );
// strcat( buildString, "-cl-no-subgroup-ifp " );
if (isNVidia) strcat( buildString, "-cl-nv-opt-level=9 " );
// strcat( buildString, "-cl-nv-maxrregcount=32 " );
error = clBuildProgram( program, 0, NULL, buildString, NULL, NULL );
// handle errors
if (error == CL_SUCCESS)
{
// dump PTX via: https://forums.developer.nvidia.com/t/pre-compiling-opencl-kernels-tutorial/17089
// and: https://stackoverflow.com/questions/12868889/clgetprograminfo-cl-program-binary-sizes-incorrect-results
cl_uint devCount;
CHECKCL( clGetProgramInfo( program, CL_PROGRAM_NUM_DEVICES, sizeof( cl_uint ), &devCount, NULL ) );
size_t* sizes = new size_t[devCount];
sizes[0] = 0;
size_t received;
CHECKCL( clGetProgramInfo( program, CL_PROGRAM_BINARY_SIZES /* wrong data... */, devCount * sizeof( size_t ), sizes, &received ) );
char** binaries = new char* [devCount];
for (unsigned i = 0; i < devCount; i++)
binaries[i] = new char[sizes[i] + 1];
CHECKCL( clGetProgramInfo( program, CL_PROGRAM_BINARIES, devCount * sizeof( size_t ), binaries, NULL ) );
FILE* f = fopen( "buildlog.txt", "wb" );
for (unsigned i = 0; i < devCount; i++)
fwrite( binaries[i], 1, sizes[i] + 1, f );
fclose( f );
}
else
{
// obtain the error log from the cl compiler
if (!log) log = new char[256 * 1024]; // can be quite large
log[0] = 0;
clGetProgramBuildInfo( program, getFirstDevice( context ), CL_PROGRAM_BUILD_LOG, 256 * 1024, log, &size );
// save error log for closer inspection
FILE* f = fopen( "errorlog.txt", "wb" );
fwrite( log, 1, size, f );
fclose( f );
#if 0
// find and display the first errormat; just dump it to a window
log[2048] = 0; // truncate very long logs
FatalError( log, "Build error" );
#else
// find and display the first error. Note: platform specific sadly; code below is for NVIDIA
char* errorString = strstr( log, ": error:" );
if (errorString)
{
int errorPos = (int)(errorString - log);
while (errorPos > 0) if (log[errorPos - 1] == '\n') break; else errorPos--;
// translate file and line number of error and report
log[errorPos + 2048] = 0;
int lineNr = 0, linePos = 0;
char* lns = strstr( log + errorPos, ">:" ), * eol;
if (!lns) FatalError( log + errorPos ); else
{
lns += 2;
while (*lns >= '0' && *lns <= '9') lineNr = lineNr * 10 + (*lns++ - '0');
lns++; // proceed to line number
while (*lns >= '0' && *lns <= '9') linePos = linePos * 10 + (*lns++ - '0');
lns += 9; // proceed to error message
eol = lns;
while (*eol != '\n' && *eol > 0) eol++;
*eol = 0;
lineNr--; // we count from 0 instead of 1
// adjust file and linenr based on include file data
string errorFile = file;
bool errorInInclude = false;
for (int i = Ninc - 1; i >= 0; i--)
{
if (lineNr > includes[i].end)
{
for (int j = 0; j <= i; j++) lineNr -= includes[j].end - includes[j].start;
break;
}
else if (lineNr > includes[i].start)
{
errorFile = includes[i].file;
lineNr -= includes[i].start;
errorInInclude = true;
break;
}
}
if (!errorInInclude) lineNr -= vendorLines;
// present error message
char t[1024];
sprintf( t, "file %s, line %i, pos %i:\n%s", errorFile.c_str(), lineNr + 1, linePos, lns );
FatalError( t, "Build error" );
}
}
else
{
// error string has unknown format; just dump it to a window
log[2048] = 0; // truncate very long logs
FatalError( log, "Build error" );
}
#endif
}
kernel = clCreateKernel( program, entryPoint, &error );
if (kernel == 0) FatalError( "clCreateKernel failed: entry point not found." );
CHECKCL( error );
loadedKernels.push_back( this );
// restore working directory
chdir( dirBackup );
}
Kernel::Kernel( cl_program& existingProgram, char* entryPoint )
{
CheckCLStarted();
cl_int error;
program = existingProgram;
kernel = clCreateKernel( program, entryPoint, &error );
if (kernel == 0) FatalError( "clCreateKernel failed: entry point not found." );
CHECKCL( error );
}
// Kernel destructor
// ----------------------------------------------------------------------------
Kernel::~Kernel()
{
if (kernel) clReleaseKernel( kernel );
// if (program) clReleaseProgram( program ); // NOTE: may be shared with other kernels
kernel = 0;
// program = 0;
}
// InitCL method
// ----------------------------------------------------------------------------
bool Kernel::InitCL()
{
// prepare memory management
if (!OpenCL::ocl) OpenCL::ocl = new OpenCL(); // use the default memory allocation functions
// prepare OpenCL for first use
cl_platform_id platform;
cl_device_id* devices;
cl_uint devCount;
cl_int error;
if (!CHECKCL( error = getPlatformID( &platform ) )) return false;
if (!CHECKCL( error = clGetDeviceIDs( platform, CL_DEVICE_TYPE_ALL, 0, NULL, &devCount ) )) return false;
devices = new cl_device_id[devCount];
if (!CHECKCL( error = clGetDeviceIDs( platform, CL_DEVICE_TYPE_ALL, devCount, devices, NULL ) )) return false;
int deviceUsed = -1;
// search a capable OpenCL device
char device_string[1024], device_platform[1024];
for (unsigned i = 0; i < devCount; i++)
{
size_t extensionSize;
CHECKCL( error = clGetDeviceInfo( devices[i], CL_DEVICE_EXTENSIONS, 0, NULL, &extensionSize ) );
if (extensionSize > 0)
{
char* extensions = (char*)malloc( extensionSize );
CHECKCL( error = clGetDeviceInfo( devices[i], CL_DEVICE_EXTENSIONS, extensionSize, extensions, &extensionSize ) );
string deviceList( extensions );
free( extensions );
string mustHave[] = {
#if defined(__APPLE__) && defined(__MACH__)
"cl_APPLE_gl_sharing",
#else
"cl_khr_gl_sharing",
#endif
"cl_khr_global_int32_base_atomics"
};
bool hasAll = true;
for (int j = 0; j < 2; j++)
{
size_t o = 0, s = deviceList.find( ' ', o );
bool hasFeature = false;
while (s != deviceList.npos)
{
string subs = deviceList.substr( o, s - o );
if (strcmp( mustHave[j].c_str(), subs.c_str() ) == 0) hasFeature = true;
do { o = s + 1, s = deviceList.find( ' ', o ); } while (s == o);
}
if (!hasFeature) hasAll = false;
}
if (hasAll)
{
cl_context_properties props[] =
{
#ifdef TINY_OCL_GLINTEROP
CL_GL_CONTEXT_KHR, (cl_context_properties)glfwGetWGLContext( GetGLFWWindow() ),
CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
#endif
CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0
};
// attempt to create a context with the requested features
context = clCreateContext( props, 1, &devices[i], NULL, NULL, &error );
if (error == CL_SUCCESS)
{