forked from NewChromantics/SoyLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoyOpenCl.h
596 lines (478 loc) · 17.1 KB
/
SoyOpenCl.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
#pragma once
#include "SoyThread.h"
#include "SortArray.h"
#include "SoyString.h"
#include "SoyEnum.h"
#include "SoyPixels.h"
#include "SoyUniform.h"
namespace Opengl
{
class TTexture;
class TContext;
}
#if defined(TARGET_WINDOWS)
// gr: amd APP sdk, other includes/libs may be different?
#include <cl/Opencl.h>
#pragma comment( lib, "OpenCL.lib" )
#endif
#if defined(TARGET_OSX)
// add the OpenCL.framework
#include <opencl/opencl.h>
#endif
#define CL_DEVICE_TYPE_INVALID 0
#define CL_UNIFORM_INVALID_INDEX CL_UINT_MAX
class SoyPixelsImpl;
namespace OpenclDevice
{
enum Type
{
Invalid = CL_DEVICE_TYPE_INVALID,
CPU = CL_DEVICE_TYPE_CPU,
GPU = CL_DEVICE_TYPE_GPU,
ANY = CL_DEVICE_TYPE_CPU|CL_DEVICE_TYPE_GPU,
};
DECLARE_SOYENUM(OpenclDevice);
}
namespace OpenclBufferReadWrite
{
enum Type
{
ReadWrite = CL_MEM_READ_WRITE,
ReadOnly = CL_MEM_READ_ONLY,
WriteOnly = CL_MEM_WRITE_ONLY,
};
}
namespace Opencl
{
class TPlatform; // API, has multiple devices
class TDeviceMeta;
class TDevice; // context to interface with 1 or more devices (one cl context)
class TContext; // a command queue/thread on a device (cl queue)
class TContextThread; // context with a built in thread for processing jobs
class TProgram; // compilable shader with multiple kernels
class TKernel; // individual kernel from a program
class TUniform;
class TSync; // cl_wait_event
class TJob; // execute a kernel, on a context
class TBuffer;
class TBufferImage;
template<typename TYPE>
class TBufferArray;
class TKernelState; // current binding to a kernel
class TKernelIteration;
void GetDevices(ArrayBridge<TDeviceMeta>&& Metas,OpenclDevice::Type Filter);
void EnumDevices(std::function<void(const TDeviceMeta&)> EnumDevice);
std::string GetErrorString(cl_int Error);
cl_image_format GetImageFormat(SoyPixelsFormat::Type Format);
static const char* BuildOption_KernelInfo = "-cl-kernel-arg-info";
};
// type conversions
namespace Soy
{
cl_float2 VectorToCl(const vec2f& v);
cl_float3 VectorToCl(const vec3f& v);
cl_float4 VectorToCl(const vec4f& v);
cl_float8 VectorToCl8(const ArrayBridge<float>& v);
cl_float16 VectorToCl16(const ArrayBridge<float>& v);
cl_int4 VectorToCl(const vec4x<int>& v);
vec2f ClToVector(const cl_float2& v);
vec4f ClToVector(const cl_float4& v);
mathfu::Vector<float,8> ClToVector(const cl_float8& v);
}
std::ostream& operator<<(std::ostream &out,const cl_float2& in);
std::ostream& operator<<(std::ostream &out,const cl_float4& in);
class Opencl::TPlatform
{
public:
TPlatform(cl_platform_id Platform);
void GetDevices(ArrayBridge<TDeviceMeta>& Metas,OpenclDevice::Type Filter);
void EnumDevices(std::function<void(const TDeviceMeta&)> EnumDevice,OpenclDevice::Type Filter);
public:
std::string mName;
std::string mVersion;
std::string mVendor;
cl_platform_id mPlatform;
};
std::ostream& operator<<(std::ostream &out,const Opencl::TPlatform& in);
class Opencl::TDeviceMeta
{
public:
TDeviceMeta() :
mDevice ( nullptr )
{
}
TDeviceMeta(cl_device_id Device);
bool IsValid() const { return mDevice != nullptr; }
size_t GetMaxGlobalWorkGroupSize() const;
public:
Soy::TVersion mVersion;
cl_device_id mDevice;
std::string mVendor;
std::string mName;
std::string mDriverVersion;
std::string mDeviceVersion;
std::string mProfile;
Array<std::string> mExtensions;
OpenclDevice::Type mType;
bool mHasOpenglInteroperability;
bool mHasOpenglSyncSupport;
cl_uint maxComputeUnits;
cl_uint maxWorkItemDimensions;
size_t maxWorkItemSizes[32];
size_t maxWorkGroupSize;
cl_uint maxClockFrequency;
cl_ulong maxMemAllocSize;
cl_bool imageSupport;
cl_uint maxReadImageArgs;
cl_uint maxWriteImageArgs;
size_t image2dMaxWidth;
size_t image2dMaxHeight;
size_t image3dMaxWidth;
size_t image3dMaxHeight;
size_t image3dMaxDepth;
cl_uint maxSamplers;
size_t maxParameterSize;
cl_ulong globalMemCacheSize;
cl_ulong globalMemSize;
cl_ulong maxConstantBufferSize;
cl_uint maxConstantArgs;
cl_ulong localMemSize;
cl_bool errorCorrectionSupport;
size_t profilingTimerResolution;
cl_bool endianLittle;
cl_uint deviceAddressBits;
};
std::ostream& operator<<(std::ostream &out,const Opencl::TDeviceMeta& in);
class Opencl::TDevice
{
public:
TDevice(const ArrayBridge<cl_device_id>& Devices,Opengl::TContext* OpenglContext=nullptr);
TDevice(const ArrayBridge<TDeviceMeta>& Devices,Opengl::TContext* OpenglContext=nullptr);
~TDevice();
std::shared_ptr<TContext> CreateContext();
std::shared_ptr<TContextThread> CreateContextThread(const std::string& Name);
cl_context GetClContext() { return mContext; }
bool HasInteroperability(Opengl::TContext& Opengl);
private:
void CreateContext(const ArrayBridge<cl_device_id>& Devices,Opengl::TContext* OpenglContext);
protected:
Array<TDeviceMeta> mDevices; // devices attached to this context
cl_context mContext; // binding to a device
#if defined(TARGET_OSX)
CGLContextObj mSharedOpenglContext;
#endif
};
class Opencl::TContext : public PopWorker::TJobQueue, public PopWorker::TContext
{
public:
TContext(TDevice& Device,cl_device_id SubDevice);
~TContext();
std::shared_ptr<TBuffer> CreateBuffer();
std::shared_ptr<TBufferImage> CreateBufferImage();
virtual void Lock() override;
virtual void Unlock() override;
virtual bool IsLocked(std::thread::id Thread) override;
const TDeviceMeta& GetDevice() const { return mDeviceMeta; }
cl_context GetContext() { return mDevice.GetClContext(); } // get the opencl context
cl_command_queue GetQueue() const { return mQueue; }
bool HasInteroperability(Opengl::TContext& Opengl) { return mDevice.HasInteroperability(Opengl); }
bool operator==(const TContext& that) const
{
return mDevice.GetClContext() == that.mDevice.GetClContext();
}
bool operator!=(const TContext& that) const { return !(*this == that); }
protected:
TDevice& mDevice;
TDeviceMeta mDeviceMeta; // useful to cache to read vars
cl_command_queue mQueue;
private:
std::thread::id mLockedThread; // needed in the context as it gets locked in other places than the job queue
};
class Opencl::TContextThread : public SoyWorkerThread, public Opencl::TContext
{
public:
TContextThread(const std::string& Name,TDevice& Device,cl_device_id SubDevice) :
SoyWorkerThread ( Name, SoyWorkerWaitMode::Wake ),
Opencl::TContext ( Device, SubDevice )
{
WakeOnEvent( PopWorker::TJobQueue::mOnJobPushed );
Start();
}
// thread
virtual bool Iteration() override { PopWorker::TJobQueue::Flush(*this); return true; }
virtual bool CanSleep() override { return !PopWorker::TJobQueue::HasJobs(); } // break out of conditional with this
};
// threadsafe in opencl 1.1
// https://forums.khronos.org/showthread.php/7354-are-clCreateProgramWithSource-amp-clBuildProgram-thread-safe
class Opencl::TProgram
{
public:
TProgram(const std::string& Source,TContext& Context);
~TProgram();
public:
cl_program mProgram;
};
class Opencl::TBuffer
{
protected:
TBuffer(const std::string& DebugName);
TBuffer(size_t Size,TContext& Context,const std::string& DebugName);
public:
virtual ~TBuffer();
cl_mem GetMemBuffer() { return mMem; }
template<typename TYPE>
void Read(ArrayBridge<TYPE>& Array,TContext& Context,TSync* Sync)
{
// construct a new array
auto* ArrayPtr = reinterpret_cast<uint8*>( Array.GetArray() );
size_t ByteCount = Array.GetDataSize();
auto Array8 = GetRemoteArray( ArrayPtr, ByteCount );
auto Array8Bridge = GetArrayBridge( Array8 );
ReadImpl( Array8Bridge, Context, Sync );
}
template<typename TYPE>
void Read(ArrayBridge<TYPE>&& Array,TContext& Context,TSync* Sync)
{
Read( Array, Context, Sync );
}
template<typename TYPE>
void Read(TYPE& Item,TContext& Context,TSync* Sync)
{
auto ItemArray = GetRemoteArray( &Item, 1 );
Read( GetArrayBridge( ItemArray ), Context, Sync );
}
size_t GetMaxSize() const { return mBufferSize; }
virtual size_t GetArraySize() const { throw Soy::AssertException("Buffer is not an array"); }
virtual size_t GetArrayElementSize() const { throw Soy::AssertException("Buffer is not an array"); }
protected:
void ReadImpl(ArrayInterface<uint8>& Array,TContext& Context,TSync* Sync);
void Write(const uint8* Array,size_t Size,TContext& Context,TSync* Sync);
protected:
size_t mBufferSize;
cl_mem mMem;
std::string mDebugName;
};
template<typename TYPE>
class Opencl::TBufferArray : public TBuffer
{
public:
TBufferArray(ArrayBridge<TYPE>&& Array,TContext& Context,const std::string& DebugName,TSync* Sync=nullptr) :
TBuffer ( Array.GetDataSize(), Context, DebugName ),
mElementSize ( 0 )
{
Write( reinterpret_cast<uint8*>( Array.GetArray() ), Array.GetDataSize(), Context, Sync );
mElementSize = Array.GetElementSize();
}
TBufferArray(ArrayBridge<TYPE>& Array,TContext& Context,const std::string& DebugName,TSync* Sync=nullptr) :
TBuffer ( Array.GetDataSize(), Context, DebugName ),
mElementSize ( 0 )
{
Write( reinterpret_cast<uint8*>( Array.GetArray() ), Array.GetDataSize(), Context, Sync );
mElementSize = Array.GetElementSize();
}
virtual size_t GetArraySize() const override { return mBufferSize / mElementSize; }
virtual size_t GetArrayElementSize() const override { return mElementSize; }
static std::shared_ptr<TBufferArray<TYPE>> Alloc(ArrayBridge<TYPE>&& Array,TContext& Context,const std::string& DebugName,TSync* Sync=nullptr)
{
std::shared_ptr<TBufferArray<TYPE>> BufferPtr( new TBufferArray( Array, Context, DebugName, Sync ) );
return BufferPtr;
}
private:
size_t mElementSize;
};
class Opencl::TBufferImage : public TBuffer
{
public:
TBufferImage(const SoyPixelsMeta& Meta,TContext& Context,const SoyPixelsImpl* ClientStorage,OpenclBufferReadWrite::Type ReadWrite,const std::string& DebugName,Opencl::TSync* Semaphore=nullptr);
TBufferImage(const SoyPixelsImpl& Image,TContext& Context,bool ClientStorage,OpenclBufferReadWrite::Type ReadWrite,const std::string& DebugName,Opencl::TSync* Semaphore=nullptr);
TBufferImage(const Opengl::TTexture& Image,Opengl::TContext& OpenglContext,TContext& Context,OpenclBufferReadWrite::Type ReadWrite,const std::string& DebugName,Opencl::TSync* Semaphore=nullptr);
~TBufferImage();
TBufferImage& operator=(TBufferImage&& Move);
void Write(const Opengl::TTexture& Image,Opencl::TSync* Semaphore);
void Write(const SoyPixelsImpl& Image,Opencl::TSync* Semaphore);
void Write(TBufferImage& Image,Opencl::TSync* Semaphore);
void Read(SoyPixelsImpl& Image,Opencl::TSync* Semaphore);
void Read(Opengl::TTexture& Image,Opencl::TSync* Semaphore);
void Read(Opengl::TTextureAndContext& TextureAndContext,Opencl::TSync* Semaphore);
SoyPixelsMeta GetMeta() const { return mMeta; }
private:
TContext& mContext;
const Opengl::TTexture* mOpenglObject; // pointer to reduce dependancy. If texture is deleted in the meantime results are undefined anyway. maybe make this safer sometime
SoyPixelsMeta mMeta;
};
class Opencl::TKernelIteration
{
const static size_t DIMENSIONS=3;
public:
TKernelIteration() :
mFirst ( 0 ),
mCount ( 0 ),
mBlocking ( true )
{
mFirst.SetAll(0);
mCount.SetAll(0);
}
explicit TKernelIteration(size_t Exec1,bool Blocking) :
mFirst ( 1 ),
mCount ( 1 ),
mBlocking ( Blocking )
{
mFirst.SetAll(0);
assert( DIMENSIONS == 1 );
mCount[0] = Exec1;
}
explicit TKernelIteration(size_t Exec1,size_t Exec2,bool Blocking) :
mFirst ( 2 ),
mCount ( 2 ),
mBlocking ( Blocking )
{
mFirst.SetAll(0);
mCount[0] = Exec1;
mCount[1] = Exec2;
}
explicit TKernelIteration(size_t Exec1,size_t Exec2,size_t Exec3,bool Blocking) :
mFirst ( 3 ),
mCount ( 3 ),
mBlocking ( Blocking )
{
mFirst.SetAll(0);
mCount[0] = Exec1;
mCount[1] = Exec2;
mCount[2] = Exec3;
}
public:
BufferArray<size_t,DIMENSIONS> mFirst;
BufferArray<size_t,DIMENSIONS> mCount;
bool mBlocking;
};
class Opencl::TKernelState : public Soy::TUniformContainer
{
friend class TKernel;
protected:
TKernelState(TKernel& Kernel);
public:
virtual ~TKernelState();
TUniform GetUniform(const std::string& Name) const;
// gr: not uniforms, but matching name of opengl
// gr: like opengl, these now throw on error, silent(return) if uniform doesn't exist
virtual bool SetUniform(const char* Name,const int& v) override;
virtual bool SetUniform(const char* Name,const float& v) override;
virtual bool SetUniform(const char* Name,const vec2f& v) override;
virtual bool SetUniform(const char* Name,const vec3f& v) override;
virtual bool SetUniform(const char* Name,const vec4f& v) override;
virtual bool SetUniform(const char* Name,const ArrayBridge<float>&& v);
virtual bool SetUniform(const char* Name,const Opengl::TTextureAndContext& v) override
{
return SetUniform( Name, v, OpenclBufferReadWrite::ReadWrite );
}
bool SetUniform(const char* Name,const Opengl::TTextureAndContext& v,OpenclBufferReadWrite::Type ReadWriteMode);
bool SetUniform(const char* Name,const SoyPixelsImpl& Pixels) override { return SetUniform( Name, Pixels, OpenclBufferReadWrite::ReadWrite ); }
bool SetUniform(const char* Name,const SoyPixelsImpl& Pixels,OpenclBufferReadWrite::Type ReadWriteMode);
bool SetUniform(const char* Name,TBuffer& Buffer);
void SetUniform(const std::string& Name,std::shared_ptr<TBuffer>& Buffer);
void SetUniform(const std::string& Name,const SoyPixelsImpl& Pixels,OpenclBufferReadWrite::Type ReadWriteMode,bool Blocking);
// throw on error, assuming wrong uniform is fatal
// read back data from a buffer that was used as a uniform
void ReadUniform(const char* Name,SoyPixelsImpl& Pixels);
void ReadUniform(const char* Name,Opengl::TTextureAndContext& Texture);
template<typename TYPE>
void ReadUniform(const char* Name,ArrayBridge<TYPE>&& Data,size_t ElementsToRead=0);
void ReadUniform(const char* Name,TBuffer& Buffer);
void GetIterations(ArrayBridge<TKernelIteration>&& IterationSplits,const ArrayBridge<vec2x<size_t>>&& Iterations);
void QueueIteration(const TKernelIteration& Iteration);
void QueueIteration(const TKernelIteration& Iteration,TSync& Semaphore);
const TDeviceMeta& GetDevice();
TContext& GetContext();
private:
void QueueIterationImpl(const TKernelIteration& Iteration,TSync* Semaphore);
// get buffer for a uniform - only applies to temporary ones we created
TBuffer& GetUniformBuffer(const char* Name); // throw if non-existant. assuming fatal if we're trying to read data from a uniform
void OnAssignedUniform(const std::string& Name,bool Success)
{
if ( Success )
mAssignedArguments.PushBack( Name );
}
public:
TKernel& mKernel;
private:
// gr: changed to array because of map crashes... hopefully this will find out why
Array<std::pair<std::string,std::shared_ptr<TBuffer>>> mBuffers;
Array<std::string> mAssignedArguments; // for detecting when an argument hasn't been set
//std::map<std::string,std::shared_ptr<TBuffer>> mBuffers; // temporarily allocated buffers for uniforms
};
class Opencl::TUniform : public Soy::TUniform
{
public:
TUniform() :
mIndex ( CL_UNIFORM_INVALID_INDEX )
{
}
TUniform(const std::string& Name,const std::string& Type,cl_uint Index) :
Soy::TUniform ( Name, Type ),
mIndex ( Index )
{
}
bool IsValid() const { return mIndex != CL_UNIFORM_INVALID_INDEX; }
bool operator==(const std::string& Name) const { return mName == Name; }
public:
cl_uint mIndex;
};
std::ostream& operator<<(std::ostream &out,const Opencl::TUniform& in);
class Opencl::TKernel
{
public:
TKernel(const std::string& Kernel,TProgram& Program);
~TKernel();
// cl_kernel's are the only things that aren't thread safe(re-entrant safe)
// when setting arguments
TKernelState Lock(TContext& Context);
void Unlock();
TContext& GetContext();
TUniform GetUniform(const char* Name) const;
bool HasUniform(const char* Name) const { return GetUniform(Name).IsValid(); }
public:
std::string mKernelName; // only for debug
cl_kernel mKernel;
Array<TUniform> mUniforms;
protected:
std::mutex mLock;
TContext* mLockedContext; // kernels need to be locked to a queue
};
class Opencl::TJob : public PopWorker::TJob
{
public:
TJob(TKernel& Kernel,TContext& Context); // should be able to get context from run()
protected:
TKernel& mKernel;
TContext& mContext;
};
class Opencl::TSync
{
public:
TSync();
~TSync() { Release(); }
void Release(); // occasionally we need to invalidate a sync
void Wait();
public:
cl_event mEvent;
};
template<typename TYPE>
inline void Opencl::TKernelState::ReadUniform(const char* Name,ArrayBridge<TYPE>&& Data,size_t ElementsToRead)
{
auto& Buffer = GetUniformBuffer(Name);
if ( ElementsToRead == 0 )
ElementsToRead = Buffer.GetArraySize();
if ( Buffer.GetArrayElementSize() != Data.GetElementSize() )
{
std::stringstream Error;
Error << "ReadUniform() to array (element size=" << Data.GetElementSize() << ") from buffer array(element size=" << Buffer.GetArrayElementSize() << ") mismatch";
throw Soy::AssertException(Error.str());
}
// auto alloc array
if ( Data.GetSize() < ElementsToRead )
Data.SetSize(ElementsToRead);
auto ArrayWrapper = GetRemoteArray( Data.GetArray(), ElementsToRead );
Opencl::TSync Semaphore;
Buffer.Read( GetArrayBridge(ArrayWrapper), GetContext(), &Semaphore );
Semaphore.Wait();
}