-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
730 lines (629 loc) · 25.9 KB
/
main.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <ctime>
#include <chrono>
#include "common.h"
#include "openglcontext.h"
#include "lodepng.h"
#include "json.hpp"
using json = nlohmann::json;
using namespace std::chrono;
// These codes mimic the ones used in 'get-image-glfw'
#define COMPILE_ERROR_EXIT_CODE (101)
#define LINK_ERROR_EXIT_CODE (102)
/*---------------------------------------------------------------------------*/
// Parameters, argument parsing
/*---------------------------------------------------------------------------*/
static void defaultParams(Params& params) {
params.width = 256;
params.height = 256;
params.shaderVersion = 0;
params.APIVersion = 0;
params.fragFilename = "";
params.vertFilename = "";
params.output = "output.png";
params.program = 0;
params.exitCompile = false;
params.exitLinking = false;
params.persist = false;
params.animate = false;
params.profile = false;
params.timeVarName = "time";
params.delay = 5;
params.binOut = "";
}
/*---------------------------------------------------------------------------*/
static void usage(char *name) {
std::cout << "Usage: " << name << " [options] <shader>.frag" << std::endl;
std::cout << std::endl;
const char *msg =
"The program will look for a JSON whose name is derived from the\n"
"shader as '<shader>.json'. This JSON file can contain uniforms\n"
"initialisations. If no JSON file is found, the program uses default\n"
"values for some uniforms.\n"
;
std::cout << msg;
std::cout << std::endl;
std::cout << "Options:" << std::endl;
const char *options[] = {
"--delay", "number of frames before PNG capture",
"--persist", "instruct the renderer to not quit after producing the image",
"--exit-compile", "exit after compilation",
"--exit-linking", "exit after linking",
"--output file.png", "set PNG output file name",
"--resolution <width> <height>", "set viewport resolution, in Pixels",
"--vertex shader.vert", "use a specific vertex shader",
"--dump-bin <file>", "dump binary output to given file (requires OpenGL >= 4.1, OpenGLES >= 3.0)",
"--profile", "report time needed to compile, link and render",
};
for (unsigned i = 0; i < (sizeof(options) / sizeof(*options)); i++) {
printf(" %-34.34s %s\n", options[i], options[i+1]);
i++;
}
std::cout << std::endl;
std::cout << "Return values:" << std::endl;
const char *errcode[] = {
"0", "Successful rendering",
"1", "Error",
"101", "Shader compilation error (either fragment or vertex)",
"102", "Shader linking error",
};
for (unsigned i = 0; i < (sizeof(errcode) / sizeof(*errcode)); i++) {
printf(" %-4.4s %s\n", errcode[i], errcode[i+1]);
i++;
}
std::cout << std::endl;
}
/*---------------------------------------------------------------------------*/
static void setParams(Params& params, int argc, char *argv[]) {
defaultParams(params);
for (int i = 1; i < argc; i++) {
std::string arg = std::string(argv[i]);
if (arg.compare(0, 2, "--") == 0) {
if (arg == "--exit-compile") {
params.exitCompile = true;
} else if (arg == "--exit-linking") {
params.exitLinking = true;
} else if (arg == "--persist") {
params.persist = true;
} else if (arg == "--animate") {
params.animate = true;
} else if (arg == "--profile") {
params.profile = true;
} else if (arg == "--delay") {
if ((i + 1) >= argc) { usage(argv[0]); crash("Missing value for option %s", "--delay"); }
params.delay = atoi(argv[++i]);
} else if (arg == "--output") {
if ((i + 1) >= argc) { usage(argv[0]); crash("Missing value for option %s", "--output"); }
params.output = argv[++i];
} else if (arg == "--resolution") {
if ((i + 2) >= argc) { usage(argv[0]); crash("Missing value for option %s", "--resolution"); }
params.width = atoi(argv[++i]);
params.height = atoi(argv[++i]);
} else if (arg == "--vertex") {
if ((i + 1) >= argc) { usage(argv[0]); crash("Missing value for option %s", "--vertex"); }
params.vertFilename = argv[++i];
} else if (arg == "--dump-bin") {
if ((i + 1) >= argc) { usage(argv[0]); crash("Missing value for option %s", "--dump-bin"); }
params.binOut = argv[++i];
} else if (arg == "--timevar-name") {
if ((i + 1) >= argc) { usage(argv[0]); crash("Missing value for option %s", "--timevar-name"); }
params.timeVarName = argv[++i];
} else {
usage(argv[0]);
crash("Invalid option: %s", argv[i]);
}
continue;
}
if (params.fragFilename == "") {
params.fragFilename = arg;
} else {
usage(argv[0]);
crash("Unexpected extra argument: %s", arg.c_str());
}
}
if (params.fragFilename == "") {
usage(argv[0]);
crash("Missing fragment shader argument");
}
}
/*---------------------------------------------------------------------------*/
void printAPI(const Params& params) {
switch (params.API) {
case API_OPENGL:
printf("OpenGL");
break;
case API_OPENGL_ES:
printf("OpenGLES");
break;
default:
crash("Invalid API value");
}
int major = params.APIVersion / 100;
int minor = (params.APIVersion % 100) / 10;
printf(" %d.%d", major, minor);
}
/*---------------------------------------------------------------------------*/
// Helpers
/*---------------------------------------------------------------------------*/
bool isFile(std::string filename) {
std::ifstream ifs(filename.c_str());
return ifs.good();
}
/*---------------------------------------------------------------------------*/
void readFile(std::string& contents, const std::string& filename) {
std::ifstream ifs(filename.c_str());
if(!ifs) {
crash("File not found: %s", filename.c_str());
}
std::stringstream ss;
ss << ifs.rdbuf();
contents = ss.str();
}
/*---------------------------------------------------------------------------*/
int getShaderVersion(const std::string& fragContents) {
size_t pos = fragContents.find('\n');
if (pos == std::string::npos) {
crash("cannot find end-of-line in fragment shader");
}
std::string sub = fragContents.substr(0, pos);
if (std::string::npos == sub.find("#version")) {
crash("cannot find ``#version'' in first line of fragment shader");
}
// TODO: use sscanf of c++ equivalent
if (std::string::npos != sub.find("110")) { return 110; }
if (std::string::npos != sub.find("120")) { return 120; }
if (std::string::npos != sub.find("130")) { return 130; }
if (std::string::npos != sub.find("140")) { return 140; }
if (std::string::npos != sub.find("150")) { return 150; }
if (std::string::npos != sub.find("330")) { return 330; }
if (std::string::npos != sub.find("400")) { return 400; }
if (std::string::npos != sub.find("410")) { return 410; }
if (std::string::npos != sub.find("420")) { return 420; }
if (std::string::npos != sub.find("430")) { return 430; }
if (std::string::npos != sub.find("440")) { return 440; }
if (std::string::npos != sub.find("450")) { return 450; }
// The following are OpenGL ES
if (std::string::npos != sub.find("100")) { return 100; }
if (std::string::npos != sub.find("300")) { return 300; }
crash("Cannot find a supported GLSL version in first line of fragment shader: ``%.80s''", sub.c_str());
}
/*---------------------------------------------------------------------------*/
void generateVertexShader(std::string& out, const Params& params) {
static const std::string vertGenericContents = std::string(
"vec2 _GLF_vertexPosition;\n"
"void main(void) {\n"
" gl_Position = vec4(_GLF_vertexPosition, 0.0, 1.0);\n"
"}\n"
);
if (params.vertFilename != "") {
readFile(out, params.vertFilename);
return;
}
std::stringstream ss;
ss << "#version " << params.shaderVersion;
if (params.shaderVersion == 300) {
// Version 300 must have the "es" suffix, and qualifies the
// _GLF_vertexPosition as "in" rather than "attribute"
ss << " es" << std::endl << "in ";
} else {
ss << std::endl << "attribute ";
}
ss << vertGenericContents;
out = ss.str();
//std::cerr << "Generated vertex shader:\n" << out << std::endl;
}
/*---------------------------------------------------------------------------*/
// JSON uniforms
/*---------------------------------------------------------------------------*/
static void setJSONDefaultEntries(json& j, const Params& params) {
json defaults = {
{"injectionSwitch", {
{"func", "glUniform2f"},
{"args", {0.0f, 1.0f}}
}},
{"time", {
{"func", "glUniform1f"},
{"args", {0.0f}}
}},
{"mouse", {
{"func", "glUniform2f"},
{"args", {0.0f, 0.0f}}
}},
{"resolution", {
{"func", "glUniform2f"},
{"args", {float(params.width), float(params.height)}}
}}
};
for (json::iterator it = defaults.begin(); it != defaults.end(); ++it) {
if (j.count(it.key()) == 0) {
j[it.key()] = it.value();
}
}
}
/*---------------------------------------------------------------------------*/
template<typename T>
T *getArray(const json& j) {
T *a = new T[j.size()];
for (unsigned i = 0; i < j.size(); i++) {
a[i] = j[i];
}
return a;
}
/*---------------------------------------------------------------------------*/
#define GLUNIFORM_ARRAYINIT(funcname, uniformloc, gltype, jsonarray) \
gltype *a = getArray<gltype>(jsonarray); \
funcname(uniformloc, jsonarray.size(), a); \
delete [] a
/*---------------------------------------------------------------------------*/
void setUniformsJSON(const GLuint& program, const Params& params) {
GLint nbUniforms;
GL_SAFECALL(glGetProgramiv, program, GL_ACTIVE_UNIFORMS, &nbUniforms);
if (nbUniforms == 0) {
// If there are no uniforms to set, return now
return;
}
// Read JSON file
std::string jsonFilename(params.fragFilename);
jsonFilename.replace(jsonFilename.end()-4, jsonFilename.end(), "json");
json j = json({});
if (isFile(jsonFilename)) {
std::string jsonContent;
readFile(jsonContent, jsonFilename);
j = json::parse(jsonContent);
} else {
// If and only if no JSON file, use the defaults
std::cerr << "Warning: file '" << jsonFilename << "' not found, will rely on default uniform values only" << std::endl;
setJSONDefaultEntries(j, params);
}
GLint uniformNameMaxLength = 0;
GL_SAFECALL(glGetProgramiv, program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformNameMaxLength);
GLchar *uniformName = new GLchar[uniformNameMaxLength];
GLint uniformSize;
GLenum uniformType;
for (int i = 0; i < nbUniforms; i++) {
GL_SAFECALL(glGetActiveUniform, program, i, uniformNameMaxLength, NULL, &uniformSize, &uniformType, uniformName);
// array name is '<name>[0]', sanitise it:
char *p = strchr(uniformName, '[');
if (p != NULL) {
*p = '\0';
}
if (j.count(uniformName) == 0) {
crash("missing JSON entry for uniform: %s", uniformName);
}
if (j.count(uniformName) > 1) {
crash("more than one JSON entry for uniform: %s", uniformName);
}
json uniformInfo = j[uniformName];
// Check presence of func and args entries
if (uniformInfo.find("func") == uniformInfo.end()) {
crash("malformed JSON: no 'func' entry for uniform: %s", uniformName);
}
if (uniformInfo.find("args") == uniformInfo.end()) {
crash("malformed JSON: no 'args' entry for uniform: %s", uniformName);
}
// Get uniform location
GLint uniformLocation = glGetUniformLocation(program, uniformName);
GL_CHECKERR("glGetUniformLocation");
if (uniformLocation == -1) {
crash("Cannot find uniform named: %s", uniformName);
}
// Dispatch to matching init function
std::string uniformFunc = uniformInfo["func"];
json args = uniformInfo["args"];
// TODO: check that args has the good number of fields and type
if (uniformFunc == "glUniform1f") {
glUniform1f(uniformLocation, args[0]);
} else if (uniformFunc == "glUniform2f") {
glUniform2f(uniformLocation, args[0], args[1]);
} else if (uniformFunc == "glUniform3f") {
glUniform3f(uniformLocation, args[0], args[1], args[2]);
} else if (uniformFunc == "glUniform4f") {
glUniform4f(uniformLocation, args[0], args[1], args[2], args[3]);
}
else if (uniformFunc == "glUniform1i") {
glUniform1i(uniformLocation, args[0]);
} else if (uniformFunc == "glUniform2i") {
glUniform2i(uniformLocation, args[0], args[1]);
} else if (uniformFunc == "glUniform3i") {
glUniform3i(uniformLocation, args[0], args[1], args[2]);
} else if (uniformFunc == "glUniform4i") {
glUniform4i(uniformLocation, args[0], args[1], args[2], args[3]);
}
// Note: GLES does not provide glUniformXui primitives
#ifndef GL_VERSION_ES_CM_1_0
else if (uniformFunc == "glUniform1ui") {
glUniform1ui(uniformLocation, args[0]);
} else if (uniformFunc == "glUniform2ui") {
glUniform2ui(uniformLocation, args[0], args[1]);
} else if (uniformFunc == "glUniform3ui") {
glUniform3ui(uniformLocation, args[0], args[1], args[2]);
} else if (uniformFunc == "glUniform4ui") {
glUniform4ui(uniformLocation, args[0], args[1], args[2], args[3]);
}
#endif // ifndef GL_VERSION_ES_CM_1_0
else if (uniformFunc == "glUniform1fv") {
GLUNIFORM_ARRAYINIT(glUniform1fv, uniformLocation, GLfloat, args);
} else if (uniformFunc == "glUniform2fv") {
GLUNIFORM_ARRAYINIT(glUniform2fv, uniformLocation, GLfloat, args);
} else if (uniformFunc == "glUniform3fv") {
GLUNIFORM_ARRAYINIT(glUniform3fv, uniformLocation, GLfloat, args);
} else if (uniformFunc == "glUniform4fv") {
GLUNIFORM_ARRAYINIT(glUniform4fv, uniformLocation, GLfloat, args);
}
else if (uniformFunc == "glUniform1iv") {
GLUNIFORM_ARRAYINIT(glUniform1iv, uniformLocation, GLint, args);
} else if (uniformFunc == "glUniform2iv") {
GLUNIFORM_ARRAYINIT(glUniform2iv, uniformLocation, GLint, args);
} else if (uniformFunc == "glUniform3iv") {
GLUNIFORM_ARRAYINIT(glUniform3iv, uniformLocation, GLint, args);
} else if (uniformFunc == "glUniform4iv") {
GLUNIFORM_ARRAYINIT(glUniform4iv, uniformLocation, GLint, args);
}
else {
crash("unknown/unsupported uniform init func: %s", uniformFunc.c_str());
}
GL_CHECKERR(uniformFunc.c_str());
}
delete [] uniformName;
}
/*---------------------------------------------------------------------------*/
void setUniformTime(const Params& params) {
GLint uniformLocation = glGetUniformLocation(params.program, params.timeVarName.c_str());
GL_CHECKERR("glGetUniformLocation");
if (uniformLocation == -1) {
crash("Cannot find uniform named: %s", params.timeVarName.c_str());
}
GLfloat timeVal = (GLfloat) (std::clock() / (float) CLOCKS_PER_SEC * 50.0);
GL_SAFECALL(glUniform1f, uniformLocation, timeVal);
}
/*---------------------------------------------------------------------------*/
// OpenGL
/*---------------------------------------------------------------------------*/
const char *openglErrorString(GLenum err) {
switch (err) {
case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
default: return "UNKNOW_ERROR";
}
}
/*---------------------------------------------------------------------------*/
void dumpBin(const Params& params, GLuint program) {
int supported = ((params.API == API_OPENGL && params.APIVersion >= 410) ||
(params.API == API_OPENGL_ES && params.APIVersion >= 300));
if (! supported) {
printf("Cannot dump binary:"
" requires OpenGL >= 4.1 or OpenGLES >= 3.0,"
" current version is: ");
printAPI(params);
printf("\n");
return;
}
GLint numFormats;
GL_SAFECALL(glGetIntegerv, GL_NUM_PROGRAM_BINARY_FORMATS, &numFormats);
if (numFormats <= 0) {
printf("Cannot dump binary: driver supports zero binary format\n");
return;
}
GLint length;
GL_SAFECALL(glGetProgramiv, program, GL_PROGRAM_BINARY_LENGTH, &length);
char *binary = (char *) malloc(length);
if (binary == NULL) {
crash("malloc failed");
}
GLenum format;
GL_SAFECALL(glGetProgramBinary, program, length, NULL, &format, (void *)binary);
std::ofstream binaryfile(params.binOut);
binaryfile.write(binary, length);
binaryfile.close();
}
/*---------------------------------------------------------------------------*/
void printShaderError(GLuint shader) {
GLint length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
// The length includes the NULL character
std::vector<GLchar> errorLog((size_t) length, 0);
glGetShaderInfoLog(shader, length, &length, &errorLog[0]);
if(length > 0) {
std::string s(&errorLog[0]);
std::cout << s << std::endl;
}
}
/*---------------------------------------------------------------------------*/
void printProgramError(GLuint program) {
GLint length = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
// The length includes the NULL character
std::vector<GLchar> errorLog((size_t) length, 0);
glGetProgramInfoLog(program, length, &length, &errorLog[0]);
if(length > 0) {
std::string s(&errorLog[0]);
std::cout << s << std::endl;
}
}
/*---------------------------------------------------------------------------*/
void openglInit(Params& params, const std::string& fragContents) {
const char *temp;
steady_clock::time_point timeStart;
GLint status = 0;
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GL_CHECKERR("glCreateShader");
std::string vertContents;
generateVertexShader(vertContents, params);
temp = vertContents.c_str();
GL_SAFECALL(glShaderSource, vertexShader, 1, &temp, NULL);
if (params.profile) {
GL_SAFECALL(glFinish);
timeStart = steady_clock::now();
}
GL_SAFECALL(glCompileShader, vertexShader);
if (params.profile) {
GL_SAFECALL(glFinish);
printf("vertex shader compile time (us): %ld\n", duration_cast<microseconds>(steady_clock::now() - timeStart).count());
}
GL_SAFECALL(glGetShaderiv, vertexShader, GL_COMPILE_STATUS, &status);
if (!status) {
printShaderError(vertexShader);
errcode_crash(COMPILE_ERROR_EXIT_CODE, "Vertex shader compilation failed (%s)", params.fragFilename.c_str());
}
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
GL_CHECKERR("glCreateShader");
temp = fragContents.c_str();
GL_SAFECALL(glShaderSource, fragmentShader, 1, &temp, NULL);
if (params.profile) {
GL_SAFECALL(glFinish);
timeStart = steady_clock::now();
}
GL_SAFECALL(glCompileShader, fragmentShader);
if (params.profile) {
GL_SAFECALL(glFinish);
printf("fragment shader compile time (us): %ld\n", duration_cast<microseconds>(steady_clock::now() - timeStart).count());
}
GL_SAFECALL(glGetShaderiv, fragmentShader, GL_COMPILE_STATUS, &status);
if (!status) {
printShaderError(fragmentShader);
errcode_crash(COMPILE_ERROR_EXIT_CODE, "Fragment shader compilation failed (%s)", params.fragFilename.c_str());
}
if (params.exitCompile) {
exit(EXIT_SUCCESS);
}
GLuint program = glCreateProgram();
int supported = ((params.API == API_OPENGL && params.APIVersion >= 410) ||
(params.API == API_OPENGL_ES && params.APIVersion >= 300));
if (supported) {
GL_SAFECALL(glProgramParameteri, program, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
}
GL_CHECKERR("glCreateProgram");
if (program == 0) {
crash("glCreateProgram()");
}
GL_SAFECALL(glAttachShader, program, vertexShader);
GL_SAFECALL(glAttachShader, program, fragmentShader);
if (params.profile) {
GL_SAFECALL(glFinish);
timeStart = steady_clock::now();
}
GL_SAFECALL(glLinkProgram, program);
if (params.profile) {
GL_SAFECALL(glFinish);
printf("link time (us): %ld\n", duration_cast<microseconds>(steady_clock::now() - timeStart).count());
}
GL_SAFECALL(glGetProgramiv, program, GL_LINK_STATUS, &status);
if (!status) {
printProgramError(program);
errcode_crash(LINK_ERROR_EXIT_CODE, "Program linking failed");
}
if(strcmp(params.binOut.c_str(), "")) {
dumpBin(params, program);
}
if (params.exitLinking) {
exit(EXIT_SUCCESS);
}
GLint vertPosLocInt = glGetAttribLocation(program, "_GLF_vertexPosition");
GL_CHECKERR("glGetAttribLocation");
if (vertPosLocInt == -1) {
crash("Cannot find position of _GLF_vertexPosition");
}
GLuint vertPosLoc = (GLuint) vertPosLocInt;
const float vertices[] = {
-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f
};
GLuint vertexBuffer;
if (params.API == API_OPENGL_ES || params.APIVersion >= 300) {
GLuint vertexArray;
GL_SAFECALL(glGenVertexArrays, 1, &vertexArray);
GL_SAFECALL(glBindVertexArray, vertexArray);
}
GL_SAFECALL(glGenBuffers, 1, &vertexBuffer);
GL_SAFECALL(glBindBuffer, GL_ARRAY_BUFFER, vertexBuffer);
GL_SAFECALL(glBufferData, GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GL_SAFECALL(glEnableVertexAttribArray, vertPosLoc);
GL_SAFECALL(glVertexAttribPointer, vertPosLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
GL_SAFECALL(glUseProgram, program);
setUniformsJSON(program, params);
GL_SAFECALL(glViewport, 0, 0, params.width, params.height);
params.program = program;
}
/*---------------------------------------------------------------------------*/
void openglRender(const Params& params) {
steady_clock::time_point timeStart;
if (params.animate) {
setUniformTime(params);
}
GL_SAFECALL(glClearColor, 0.0f, 0.0f, 0.0f, 1.0f);
GL_SAFECALL(glClear, GL_COLOR_BUFFER_BIT);
// GL_SAFECALL(glDrawElements, GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
if (params.profile) {
GL_SAFECALL(glFinish);
timeStart = steady_clock::now();
}
GL_SAFECALL(glDrawArrays, GL_TRIANGLES, 0, 3);
GL_SAFECALL(glDrawArrays, GL_TRIANGLES, 3, 3);
if (params.profile) {
GL_SAFECALL(glFinish);
printf("render time (us): %ld\n", duration_cast<microseconds>(steady_clock::now() - timeStart).count());
}
GL_SAFECALL(glFlush);
}
/*---------------------------------------------------------------------------*/
// PNG
/*---------------------------------------------------------------------------*/
// 4 channels: RGBA
#define CHANNELS (4)
/*---------------------------------------------------------------------------*/
void savePNG(Params& params) {
unsigned int uwidth = (unsigned int) params.width;
unsigned int uheight = (unsigned int) params.height;
std::vector<std::uint8_t> data(uwidth * uheight * CHANNELS);
GL_SAFECALL(glReadPixels, 0, 0, uwidth, uheight, GL_RGBA, GL_UNSIGNED_BYTE, &data[0]);
std::vector<std::uint8_t> flipped_data(uwidth * uheight * CHANNELS);
for (unsigned int h = 0; h < uheight ; h++)
for (unsigned int col = 0; col < uwidth * CHANNELS; col++)
flipped_data[h * uwidth * CHANNELS + col] =
data[(uheight - h - 1) * uwidth * CHANNELS + col];
unsigned png_error = lodepng::encode(params.output, flipped_data, uwidth, uheight);
if (png_error) {
crash("lodepng: %s", lodepng_error_text(png_error));
}
}
/*---------------------------------------------------------------------------*/
// Main
/*---------------------------------------------------------------------------*/
int main(int argc, char* argv[])
{
std::string fragContents;
Context context;
Params params;
setParams(params, argc, argv);
readFile(fragContents, params.fragFilename);
params.shaderVersion = getShaderVersion(fragContents);
contextInitAndGetAPI(params, context);
openglInit(params, fragContents);
int numFrames = 0;
bool saved = false;
while (contextKeepLooping(context)) {
openglRender(params);
contextSwap(context);
numFrames++;
if (numFrames == params.delay && !saved) {
savePNG(params);
saved = true;
if (params.persist) {
printf("Press any key to close the window...\n");
contextSetKeyCallback(context);
} else {
break;
}
}
}
contextTerminate(context);
exit(EXIT_SUCCESS);
}
/*---------------------------------------------------------------------------*/