-
Notifications
You must be signed in to change notification settings - Fork 3
/
NanoCL.cpp
392 lines (324 loc) · 10.7 KB
/
NanoCL.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
// (c) 2014-2017, github.com/turbo
// MIT licensed
#include <windows.h>
#include <string>
#include <vector>
#include <GL/gl.h>
#ifndef __MINGW32__
#pragma comment(lib, "opengl32.lib") // MSVC, ICL
#pragma comment(lib, "gdi32.lib") // ICL
#pragma comment(lib, "user32.lib") // ICL
#endif
#define NanoCL_MAX_LOG_LENGTH 10000
#define NanoCL_V \
"varying vec2 pos;void " \
"main(void){pos=vec2(gl_MultiTexCoord0);gl_Position=gl_Vertex;}"
#define NanoCL_K \
"varying vec2 pos;vec4 read(sampler2D m){return texture2D(m,pos);}" \
"void commit(vec4 d){gl_FragColor=d;}"
#define defPROC(a, b, ...) \
typedef a(__stdcall *MCL##b)(__VA_ARGS__); \
MCL##b b = nullptr
#define loadPROC(a) \
a = MCL##a((wglGetProcAddress(#a))); \
if (a == nullptr) \
ExitProcess(printf("ERROR: Couldn't load GL(Ext) function %s.\n", #a));
#define kernel(k) #k
namespace NanoCL {
defPROC(const char *, glGetStringi, int, int);
defPROC(void, glActiveTexture, int);
defPROC(void, glAttachShader, unsigned, unsigned);
defPROC(void, glCompileShader, unsigned);
defPROC(void, glDeleteShader, unsigned);
defPROC(void, glGetInfoLogARB, unsigned, int, int *, char *);
defPROC(void, glGetObjectParameterivARB, unsigned, unsigned, int *);
defPROC(void, glLinkProgram, unsigned);
defPROC(void, glShaderSource, unsigned, int, const char **, const int *);
defPROC(void, glUniform1i, int, int);
defPROC(void, glUniform2fv, int, int, const float *);
defPROC(void, glUniform4fv, int, int, const float *);
defPROC(void, glUseProgram, unsigned);
defPROC(void, glBindFramebufferEXT, unsigned, unsigned);
defPROC(void, glDeleteFramebuffersEXT, int, const unsigned *);
defPROC(void, glFramebufferTexture2DEXT, unsigned, unsigned, unsigned, unsigned,
int);
defPROC(void, glGenFramebuffersEXT, int, unsigned *);
defPROC(void, glGenerateMipmapEXT, unsigned);
defPROC(int, glGetUniformLocation, unsigned, const char *);
defPROC(int, glCreateProgram, void);
defPROC(int, glCreateShader, unsigned);
typedef struct NCL_vec4f { float r, g, b, a; } NCL_vec4f;
inline void gpgpu_fillscreen(void) {
glBegin(6);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(+1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(+1.0f, +1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, +1.0f, 0.0f);
glEnd();
}
class gpgpu_texture2D {
public:
unsigned handle;
int width, height;
gpgpu_texture2D(int w, int h) : width(w), height(h) {
glGenTextures(1, &handle);
update_data(nullptr);
bind();
glTexParameteri(0x0DE1, 0x2802, 0x812F);
glTexParameteri(0x0DE1, 0x2803, 0x812F);
bind();
glTexParameteri(0x0DE1, 0x2801, 0x2600);
glTexParameteri(0x0DE1, 0x2800, 0x2600);
glGenerateMipmapEXT(0x0DE1);
}
~gpgpu_texture2D() {
glDeleteTextures(1, &handle);
handle = 0;
}
void draw(void) const {
bind();
glEnable(0x0DE1);
gpgpu_fillscreen();
}
void bind(int texture_unit = 0) const {
glActiveTexture(0x84C0 + texture_unit);
glBindTexture(0x0DE1, handle);
}
void update_data(const float *dat) {
bind();
glTexImage2D(0x0DE1, 0, 0x8814, width, height, 0, 0x1908, 0x1406, dat);
}
private:
gpgpu_texture2D(const gpgpu_texture2D &src);
void operator=(const gpgpu_texture2D &src);
};
inline void gpgpu_tex_scale(unsigned program, gpgpu_texture2D *tex,
const std::string &name) {
auto scale = glGetUniformLocation(program, (name + "Scale").c_str());
if (scale <= -1)
return;
const float argARB[] = {1.0f / tex->width, 1.0f / tex->height, 0.0f, 0.0f};
glUniform2fv(scale, 1, argARB);
}
inline void gpgpu_add(unsigned program, gpgpu_texture2D *tex,
const std::string &name, int texture_unit = 0) {
glUseProgram(program);
tex->bind(texture_unit);
glUniform1i(glGetUniformLocation(program, name.c_str()), texture_unit);
gpgpu_tex_scale(program, tex, name);
}
class gpgpu_framebuffer {
public:
unsigned handle;
gpgpu_texture2D *tex;
explicit gpgpu_framebuffer(gpgpu_texture2D *tex_) {
glGenFramebuffersEXT(1, &handle);
attach(tex_);
}
~gpgpu_framebuffer() {
glDeleteFramebuffersEXT(1, &handle);
handle = 0;
}
void run(unsigned prog) const {
bind();
glUseProgram(prog);
auto scale_index = glGetUniformLocation(prog, "locationScale");
if (scale_index > -1) {
const float argARB[] = {float(tex->width), float(tex->height), 0.0f,
0.0f};
glUniform4fv(scale_index, 1, argARB);
}
gpgpu_fillscreen();
}
void read(float *destination, int width, int height) const {
bind();
glReadPixels(0, 0, width, height, 0x1908, 0x1406, destination);
}
void bind(void) const {
glBindFramebufferEXT(0x8D40, handle);
if (tex)
glViewport(0, 0, tex->width, tex->height);
}
void attach(gpgpu_texture2D *tex_) {
tex = tex_;
if (!tex)
return;
glBindFramebufferEXT(0x8D40, handle);
glFramebufferTexture2DEXT(0x8D40, 0x8CE0, 0x0DE1, tex->handle, 0);
}
private:
gpgpu_framebuffer(const gpgpu_framebuffer &src);
void operator=(const gpgpu_framebuffer &src);
};
inline void gpgpu_init() {
static auto gpgpu_initted = false;
if (gpgpu_initted)
return;
gpgpu_initted = true;
const static PIXELFORMATDESCRIPTOR pfd = {
0, 0, PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0};
auto hDC = GetDC(CreateWindow(
#if (defined(_MSC_VER) && !defined(__INTEL_COMPILER))
LPCWSTR
#else
LPCSTR
#endif
("edit"),
nullptr, WS_POPUP | WS_MINIMIZE, 0, 0, 0, 0, nullptr, nullptr, nullptr,
nullptr));
SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd);
wglMakeCurrent(hDC, wglCreateContext(hDC));
int extListSize = 0;
glGetIntegerv(0x821D, &extListSize);
if (extListSize == 0)
ExitProcess(printf("ERROR: No GPU extensions detected (OpenGL context init "
"might have failed).\n"));
#pragma warning(disable : 4312)
loadPROC(glActiveTexture);
loadPROC(glGetUniformLocation);
loadPROC(glAttachShader);
loadPROC(glCompileShader);
loadPROC(glCreateProgram);
loadPROC(glCreateShader);
loadPROC(glDeleteShader);
loadPROC(glGetInfoLogARB);
loadPROC(glGetObjectParameterivARB);
loadPROC(glGetUniformLocation);
loadPROC(glLinkProgram);
loadPROC(glShaderSource);
loadPROC(glUniform1i);
loadPROC(glUniform2fv);
loadPROC(glUniform4fv);
loadPROC(glUseProgram);
loadPROC(glBindFramebufferEXT);
loadPROC(glDeleteFramebuffersEXT);
loadPROC(glFramebufferTexture2DEXT);
loadPROC(glGenFramebuffersEXT);
loadPROC(glGenerateMipmapEXT);
loadPROC(glGetStringi);
#pragma warning(default : 4312)
glDisable(0x0B71);
glDisable(0x0BC0);
glDisable(0x0BE2);
}
class gpgpu_array;
class context {
public:
explicit context() { gpgpu_init(); }
std::vector<gpgpu_array *> tex;
std::string utils;
void add_array(gpgpu_array *t) { tex.push_back(t); }
};
class gpgpu_array : public gpgpu_texture2D, public gpgpu_framebuffer {
public:
context &env;
std::string name;
gpgpu_array(context &env_, std::string name, int w, int h)
: gpgpu_texture2D(w, h), gpgpu_framebuffer(this), env(env_), name(name) {
env.add_array(this);
}
std::string get_tex_decls(void) const {
std::string s;
for (unsigned i = 0; i < env.tex.size(); i++)
s += "uniform sampler2D " + env.tex[i]->name + ";uniform vec2 " +
env.tex[i]->name + "Scale;";
return s + env.utils;
}
void swap(gpgpu_array &arr) {
std::swap(gpgpu_framebuffer::handle, arr.gpgpu_framebuffer::handle);
std::swap(gpgpu_texture2D::handle, arr.gpgpu_texture2D::handle);
}
};
inline unsigned gpgpu_runprep(gpgpu_array &dest, unsigned code) {
auto &env = dest.env;
unsigned texunit = 0;
for (unsigned i = 0; i < env.tex.size(); i++) {
if (env.tex[i] != &dest)
gpgpu_add(code, env.tex[i], env.tex[i]->name, texunit++);
else
gpgpu_tex_scale(code, env.tex[i], env.tex[i]->name);
}
return code;
}
// Debug GLSL
void checkShaderOp(unsigned obj, unsigned errType, const char *where) {
int compiled;
glGetObjectParameterivARB(obj, errType, &compiled);
if (compiled)
return;
char errorLog[NanoCL_MAX_LOG_LENGTH];
glGetInfoLogARB(obj, NanoCL_MAX_LOG_LENGTH, nullptr, errorLog);
printf("ERROR: Could not build GLSL shader (fatal).\n\n--- CODE DUMP "
"---\n%s\n\n--- ERROR LOG ---\n%s\n\n",
where, errorLog);
}
unsigned makeShaderObject(int target, const char *code) {
auto h = glCreateShader(target);
glShaderSource(h, 1, &code, nullptr);
glCompileShader(h);
checkShaderOp(h, 0x8B81, code);
return h;
}
unsigned makeProgramObject(const char *vertex, const char *fragment) {
if (glUseProgram == nullptr)
printf("ERROR: glUseProgram could not be loaded.\n");
auto p = glCreateProgram();
auto vo = makeShaderObject(0x8B31, vertex);
auto fo = makeShaderObject(0x8B30, fragment);
glAttachShader(p, vo);
glAttachShader(p, fo);
glLinkProgram(p);
checkShaderOp(p, 0x8B82, "link");
glDeleteShader(vo);
glDeleteShader(fo);
return p;
}
struct alloc {
NCL_vec4f *data;
unsigned dataWidth; // (CPU) [internal] texture width
unsigned dataHeight; // (CPU) [internal] texture height
gpgpu_array *gpuData; // (GPU) float-texture
alloc(context &gpuCtx, std::string UID, unsigned length) {
dataWidth = length;
int x, y;
for (x = 0; y = length / ++x | 0, x <= y;)
if (!(x * y - length))
dataHeight = y;
dataWidth = length / dataHeight;
data = new NCL_vec4f[length]();
gpuData = new gpgpu_array(gpuCtx, UID, dataWidth, dataHeight);
}
};
void push(alloc uID) { uID.gpuData->update_data((float *)(uID.data)); }
int make(alloc uID, const char *kernel) {
return makeProgramObject(
NanoCL_V,
(NanoCL_K + (*uID.gpuData).get_tex_decls() + std::string(kernel))
.c_str());
}
void run(alloc uID, const char *kernel) {
(*uID.gpuData).run(gpgpu_runprep(*uID.gpuData, make(uID, kernel)));
}
void run(alloc uID, int progID) {
(*uID.gpuData).run(gpgpu_runprep(*uID.gpuData, progID));
}
void pull(alloc uID) {
uID.gpuData->read(((float *)(uID.data)), uID.dataWidth, uID.dataHeight);
}
void swap(alloc A, alloc B) { A.gpuData->swap(*B.gpuData); }
}
#undef defPROC
#undef loadPROC
/* EOF */