-
Notifications
You must be signed in to change notification settings - Fork 4
/
clcontextwrapper.h
171 lines (116 loc) · 3.67 KB
/
clcontextwrapper.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
#pragma once
#include <functional>
#include <string>
#include <vector>
struct CLContextWrapperPrivate;
enum class DeviceType
{
GPU_DEVICE,
CPU_DEVICE,
NONE
};
enum class BufferType
{
READ_ONLY,
WRITE_ONLY,
READ_AND_WRITE
};
struct NDRange
{
unsigned int workDim;
size_t globalSize[3];
size_t localSize[3];
size_t globalOffset[3];
NDRange()
{
workDim = 0;
globalSize[0] = globalSize[1] = globalSize[2] = 0;
localSize[0] = localSize[1] = localSize[2] = 0;
globalOffset[0] = globalOffset[1] = globalOffset[2] = 0;
}
};
enum class TextureWrapMode
{
CLAMP,
REPEAT
};
enum class TextureFilterMode
{
LINEAR,
NEAREST
};
struct TextureParams
{
TextureWrapMode wrapMode;
TextureFilterMode filterMode;
};
struct KernelArg
{
size_t byteSize;
void *data;
template <typename T>
KernelArg(T* aData) : byteSize(sizeof(T)), data(aData)
{
}
KernelArg(void* aData, size_t size) : byteSize(size), data(aData)
{
}
static KernelArg getShared(size_t size)
{
return KernelArg(0, size);
}
};
//typedef int BufferId; // TODO: Make an assert to ensure cl_mem = void *
typedef void * BufferId;
typedef void * KernelId; // TODO: Make an assert to ensure cl_kernel = void *
typedef unsigned int GLTextureId;
class CLContextWrapper
{
public:
CLContextWrapper();
~CLContextWrapper();
// Context Creation
bool createContext(DeviceType deviceType = DeviceType::CPU_DEVICE);
bool createContextWithOpengl();
// Context status
bool hasCreatedContext() const;
DeviceType getContextDeviceType() const;
size_t getMaxWorkGroupSize() const;
// Kernel
bool createProgramFromSource(const std::string & source);
KernelId prepareKernel(const std::string & kernelName);
KernelId getKernel(const std::string & kernelName);
size_t getWorkGroupSizeForKernel(const std::string & kernelName) const;
bool dispatchKernel(const std::string& kernelName, NDRange range);
bool dispatchKernel(const std::string& kernelName, NDRange range, const std::vector<KernelArg>& args);
bool setKernelArg(const std::string & kernelName, KernelArg args, int index);
void finish();
// OpenCL Buffers
template <typename T>
BufferId createBufferFromArray(size_t count, T * hostData = nullptr, BufferType type = BufferType::READ_AND_WRITE)
{
return createBuffer(sizeof(T)*count, hostData, type);
}
BufferId createBuffer(size_t bytesSize, void * hostData = nullptr, BufferType type = BufferType::READ_AND_WRITE);
template <typename T>
bool uploadArrayToBuffer(BufferId id, size_t count, T * data, size_t offset = 0, const bool blocking = true)
{
return uploadToBuffer(id, sizeof(T) * count, data, offset, blocking);
}
bool uploadToBuffer(BufferId id, size_t bytesSize, void * data, size_t offset = 0, const bool blocking = true);
template <typename T>
bool dowloadArrayFromBuffer(BufferId id, size_t count, T * data, size_t offset = 0, const bool blocking = true)
{
return dowloadFromBuffer(id, sizeof(T)* count, data, offset, blocking);
}
bool dowloadFromBuffer(BufferId id, size_t bytesSize, void * data, size_t offset = 0, const bool blocking = true);
// OpenGL
BufferId shareGLTexture(const GLTextureId id, BufferType type);
void executeSafeAndSyncronized(BufferId * textureToLock, unsigned int count, std::function<void()> exec);
// Static util
static std::vector<std::string> listAvailablePlatforms();
private:
CLContextWrapperPrivate * _this;
bool _hasCreatedContext;
DeviceType _deviceType;
};