-
Notifications
You must be signed in to change notification settings - Fork 8
/
r_method.cpp
556 lines (483 loc) · 17.1 KB
/
r_method.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
#include "engine.h"
#include "r_method.h"
#include "u_file.h"
#include "u_misc.h"
#include "u_hash.h"
#include "u_zip.h"
#include "u_set.h"
#include "u_log.h"
namespace r {
///! uniform
uniform::uniform() {
memset(this, 0, sizeof *this);
}
void uniform::set(int value) {
if (asInt == value) return;
U_ASSERT(m_type == kInt);
asInt = value;
post();
}
void uniform::set(int x, int y) {
if (asInt2[0] == x && asInt2[1] == y) return;
U_ASSERT(m_type == kInt2);
asInt2[0] = x;
asInt2[1] = y;
post();
}
void uniform::set(float value) {
if (asFloat == value) return;
U_ASSERT(m_type == kFloat);
asFloat = value;
post();
}
void uniform::set(const m::vec2 &value) {
if (asVec2.equals(value)) return;
U_ASSERT(m_type == kVec2);
asVec2 = value;
post();
}
void uniform::set(const m::vec3 &value) {
if (asVec3.equals(value)) return;
U_ASSERT(m_type == kVec3);
asVec3 = value;
post();
}
void uniform::set(const m::vec4 &value) {
if (asVec4.equals(value)) return;
U_ASSERT(m_type == kVec4);
asVec4 = value;
post();
}
void uniform::set(const m::mat4 &value) {
if (!memcmp(asMat4.ptr(), value.ptr(), sizeof value)) return;
U_ASSERT(m_type == kMat4);
asMat4 = value;
post();
}
void uniform::set(size_t count, const float *mats) {
U_ASSERT(m_type == kMat3x4Array);
U_ASSERT(count <= method::kMat3x4Space);
memcpy(asMat3x4Array.data, mats, sizeof(m::mat3x4) * count);
asMat3x4Array.count = count;
post();
}
void uniform::post() {
if (m_method != method::m_currentMethod)
return;
// Can't post change because the uniform handle is invalid. This is likely the
// result of using a uniform that is not found. This is allowed by the engine
// to make shader permutations less annoying to deal with. We waste some memory
// storing otherwise unpostable uniforms in some cases, but so be it.
if (m_handle == -1)
return;
switch (m_type) {
case kInt:
gl::Uniform1i(m_handle, asInt);
break;
case kInt2:
gl::Uniform2i(m_handle, asInt2[0], asInt2[1]);
break;
case kFloat:
gl::Uniform1f(m_handle, asFloat);
break;
case kVec2:
gl::Uniform2fv(m_handle, 1, &asVec2.x);
break;
case kVec3:
gl::Uniform3fv(m_handle, 1, &asVec3.x);
break;
case kVec4:
gl::Uniform4fv(m_handle, 1, &asVec4.x);
break;
case kMat3x4Array:
gl::UniformMatrix3x4fv(m_handle, asMat3x4Array.count, GL_FALSE, (const GLfloat *)asMat3x4Array.data);
break;
case kMat4:
gl::UniformMatrix4fv(m_handle, 1, GL_TRUE, asMat4.ptr());
break;
}
}
///! MethodCacheHeader
struct MethodCacheHeader {
static constexpr const char *kMagic = "SHADER";
static constexpr uint16_t kVersion = 1;
char magic[6];
uint16_t version;
uint32_t format;
void endianSwap();
};
inline void MethodCacheHeader::endianSwap() {
version = u::endianSwap(version);
format = u::endianSwap(format);
}
///! method
m::mat3x4 method::m_mat3x4Scratch[method::kMat3x4Space];
const method *method::m_currentMethod;
method::method()
: m_program(0)
, m_description(nullptr)
{
}
method::~method() {
destroy();
}
void method::destroy() {
for (auto &it : m_shaders)
if (it.second.object)
gl::DeleteShader(it.second.object);
if (m_program)
gl::DeleteProgram(m_program);
}
bool method::init(const char *description) {
m_program = gl::CreateProgram();
m_prelude = u::move(u::format("#version %d\n", gl::glslVersion()));
m_description = description;
return !!m_program;
}
bool method::reload() {
gl::UseProgram(0);
destroy();
if (!(m_program = gl::CreateProgram()))
return false;
for (const auto &it : m_shaders)
if (!addShader(it.first, it.second.shaderFile))
return false;
if (!finalize(m_attributes, m_fragData, false))
return false;
post();
enable();
for (auto &it : m_uniforms)
it.second.post();
return true;
}
void method::define(const char *macro) {
m_prelude += u::format("#define %s\n", macro);
// keep a list of "feature" macros
if (!strncmp(macro, "HAS_", 3) ||
!strncmp(macro, "USE_", 3))
m_defines.push_back(macro + 4); // skip "HAS_" or "USE_"
}
void method::define(const char *macro, size_t value) {
// keep a list of defines
m_prelude += u::format("#define %s %zu\n", macro, value);
}
void method::define(const char *macro, float value) {
// keep a list of defines
m_prelude += u::format("#define %s %f\n", macro, value);
}
u::optional<u::string> method::preprocess(const u::string &file, u::set<u::string> &uniforms, bool initial) {
auto fp = u::fopen(neoGamePath() + file, "r");
if (!fp)
return u::none;
u::string result;
if (initial)
result = m_prelude;
size_t lineno = 1;
for (u::string line; u::getline(fp, line); ) {
while (line[0] && strchr(" \t", line[0])) line.pop_front();
if (line[0] == '#') {
const auto split = u::split(&line[1]);
if (split.size() == 2 && split[0] == "include") {
auto thing = split[1];
const auto front = thing.pop_front(); // '"' or '<'
const auto back = thing.pop_back(); // '"' or '>'
if ((front == '<' && back != '>') && (front != back))
return u::format("#error invalid use of include directive on line %zu\n", lineno);
const u::optional<u::string> include = preprocess(thing, uniforms, false);
if (!include)
return u::format("#error failed to include %s\n", thing);
result += u::format("#line %zu\n%s\n", lineno++, *include);
continue;
}
}
if (!strncmp(&line[0], "uniform", 7)) {
// don't emit the uniform line if it's already declared
u::string name = &line[0] + 7;
while (u::isspace(name[0])) name.pop_front();
size_t semicolon = name.find(';');
if (semicolon != u::string::npos)
name.erase(semicolon, semicolon);
if (uniforms.find(name) == uniforms.end()) {
uniforms.insert(name);
result += line + "\n";
}
} else {
result += line + "\n";
}
lineno++;
}
return result;
}
bool method::addShader(GLenum type, const char *shaderFile) {
u::set<u::string> uniforms;
const auto pp = preprocess(shaderFile, uniforms);
if (!pp)
neoFatal("failed preprocessing `%s'", shaderFile);
auto &what = m_shaders[type];
what.shaderFile = shaderFile;
what.shaderText = u::move(*pp);
return true;
}
void method::enable() {
// avoid binding this multiple times
if (m_currentMethod == this)
return;
m_currentMethod = this;
gl::UseProgram(m_program);
}
void method::post() {
// Lookup all uniform locations and assign their owners
for (auto &it : m_uniforms) {
it.second.m_handle = gl::GetUniformLocation(m_program, it.first.c_str());
it.second.m_method = this;
}
}
uniform *method::getUniform(const u::string &name, uniform::type type) {
auto *const value = &m_uniforms[name];
value->m_type = type;
if (type == uniform::kMat3x4Array)
value->asMat3x4Array.data = m_mat3x4Scratch;
return value;
}
static u::zip gMethodCache;
static bool mountCache() {
if (gMethodCache.opened())
return true;
const auto cacheFile = neoUserPath() + "cache/methods.zip";
if (u::exists(cacheFile)) {
// Cache already exists: open
if (!gMethodCache.open(cacheFile))
return false;
u::Log::out("[cache] => mounted method cache `%s'\n", cacheFile);
return true;
}
// Cache does not exist: create the file
if (!gMethodCache.create(cacheFile)) {
u::Log::err("[cache] => failed to create cache file `%s'\n", cacheFile);
return false;
}
u::Log::out("[cache] => created method cache `%s'\n", cacheFile);
return true;
}
bool method::finalize(const u::initializer_list<const char *> &attributes,
const u::initializer_list<const char *> &fragData,
bool initial)
{
if (!mountCache())
return false;
// Check if the system supports any program binary format
GLint formats = 0;
if (gl::has(gl::ARB_get_program_binary))
gl::GetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &formats);
// Before compiling the shaders, concatenate their text and hash their contents.
// We'll then check if there exists a cached copy of this program and load
// that instead.
bool notUsingCache = true;
while (formats) {
u::string concatenate;
// Calculate how much memory we need to concatenate the shaders
size_t size = 0;
for (const auto &it : m_shaders)
size += it.second.shaderText.size();
concatenate.reserve(size);
// Concatenate the shaders
for (const auto &it : m_shaders)
concatenate += it.second.shaderText;
// Hash the result of the concatenation
auto hash = u::djbx33a((const unsigned char *)&concatenate[0], concatenate.size());
// Load the cached program in memory
auto load = gMethodCache.read(hash.hex());
if (!load)
break;
MethodCacheHeader *header = (MethodCacheHeader*)&(*load)[0];
header->endianSwap();
if (memcmp(header->magic, (const void *)MethodCacheHeader::kMagic, sizeof header->magic)
|| header->version != MethodCacheHeader::kVersion)
{
gMethodCache.remove(hash.hex());
break;
}
// Check if the format is supported
u::vector<GLint> supportedFormats(formats);
gl::GetIntegerv(GL_PROGRAM_BINARY_FORMATS, &supportedFormats[0]);
if (u::find(supportedFormats.begin(), supportedFormats.end(), GLint(header->format)) != supportedFormats.end()) {
GLint status = 0;
// Use the program Binary
gl::ProgramBinary(m_program, header->format, &(*load)[sizeof *header], (*load).size() - sizeof *header);
// Verify that this program is valid (linked)
gl::GetProgramiv(m_program, GL_LINK_STATUS, &status);
if (status) {
u::Log::out("[cache] => loaded (method) %s\n", hash.hex());
notUsingCache = false;
}
break;
}
// Not supported, remove it
u::Log::err("[cache] => failed loading `%s'\n", hash.hex());
gMethodCache.remove(hash.hex());
break;
}
if (notUsingCache) {
// compile the individual shaders
auto compile = [this](GLenum type, shader &shader_) {
GLint size = shader_.shaderText.size(),
status = 0,
length = 0;
if (!(shader_.object = gl::CreateShader(type)))
return false;
const GLchar *source = &shader_.shaderText[0];
gl::ShaderSource(shader_.object, 1, &source, &size);
gl::CompileShader(shader_.object);
gl::GetShaderiv(shader_.object, GL_COMPILE_STATUS, &status);
if (status) {
gl::AttachShader(m_program, shader_.object);
return true;
}
gl::GetShaderiv(shader_.object, GL_INFO_LOG_LENGTH, &length);
u::vector<char> log(length+1);
gl::GetShaderInfoLog(shader_.object, length, nullptr, &log[0]);
u::Log::err("[shader] => compilation error `%s':\n%s\n%s",
shader_.shaderFile,
&log[0],
source);
return false;
};
for (auto &it : m_shaders)
compile(it.first, it.second);
// Check if compilation succeeded
GLint success = 0;
GLint infoLogLength = 0;
u::string infoLog;
// Setup attributes
for (size_t i = 0; i < attributes.size(); i++)
gl::BindAttribLocation(m_program, i, attributes[i]);
for (size_t i = 0; i < fragData.size(); i++)
gl::BindFragDataLocation(m_program, i, fragData[i]);
if (gl::has(gl::ARB_get_program_binary)) {
// Need to hint to the compiler that in the future we'll be needing
// to retrieve the program binary
gl::ProgramParameteri(m_program, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
}
gl::LinkProgram(m_program);
gl::GetProgramiv(m_program, GL_LINK_STATUS, &success);
if (!success) {
gl::GetProgramiv(m_program, GL_INFO_LOG_LENGTH, &infoLogLength);
infoLog.resize(infoLogLength);
gl::GetProgramInfoLog(m_program, infoLogLength, nullptr, &infoLog[0]);
u::Log::err("[shader] => link error:\n%s\n", infoLog);
return false;
}
if (formats) {
// On some implementations, the binary is not actually available until the
// program has been used.
gl::UseProgram(m_program);
u::string concatenate;
// Calculate how much memory we need to concatenate the shaders
size_t size = 0;
for (auto &it : m_shaders)
size += it.second.shaderText.size();
concatenate.reserve(size);
// Concatenate the shaders
for (auto &it : m_shaders)
concatenate += it.second.shaderText;
// Hash the result of the concatenation
auto hash = u::djbx33a((const unsigned char *const)&concatenate[0], concatenate.size());
// Get the program binary from the driver
GLint length = 0;
GLenum binaryFormat = 0;
gl::GetProgramiv(m_program, GL_PROGRAM_BINARY_LENGTH, &length);
u::vector<unsigned char> programBinary(length);
gl::GetProgramBinary(m_program, length, nullptr, &binaryFormat, &programBinary[0]);
// Create the header
MethodCacheHeader header;
memcpy(header.magic, (const void *)MethodCacheHeader::kMagic, 6);
header.version = MethodCacheHeader::kVersion;
header.format = binaryFormat;
header.endianSwap();
// Serialize the data
u::vector<unsigned char> serialize(sizeof header + programBinary.size());
memcpy(&serialize[0], &header, sizeof header);
memcpy(&serialize[sizeof header], &programBinary[0], programBinary.size());
if (gMethodCache.write(hash.hex(), serialize)) {
u::Log::out("[cache] => wrote (method) %s\n", hash.hex());
} else {
u::Log::err("[cache] => failed writing (method) %s\n", hash.hex());
return false;
}
}
// Don't need these anymore
for (auto &it : m_shaders) {
if (it.second.object) {
gl::DeleteShader(it.second.object);
it.second.object = 0;
}
}
}
// Make a copy of these for reloads
m_attributes = attributes;
m_fragData = fragData;
// Make a pretty list of all the "USE_" and "HAS_" permutators for this
// shader.
u::string contents;
for (size_t i = 0; i < m_defines.size(); i++) {
contents += m_defines[i];
if (i != m_defines.size() - 1)
contents += ", ";
}
const char *const whence = initial ? "loaded" : "reloaded";
if (contents.empty())
u::Log::out("[method] => %s `%s' program\n", whence, m_description);
else
u::Log::out("[method] => %s `%s' program using [%s]\n", whence, m_description, contents);
return true;
}
///! defaultMethod
defaultMethod::defaultMethod()
: m_screenSize(nullptr)
, m_colorTextureUnit(nullptr)
{
}
bool defaultMethod::init() {
if (!method::init("default"))
return false;
if (gl::has(gl::ARB_texture_rectangle))
method::define("HAS_TEXTURE_RECTANGLE");
if (!addShader(GL_VERTEX_SHADER, "shaders/default.vs"))
return false;
if (!addShader(GL_FRAGMENT_SHADER, "shaders/default.fs"))
return false;
if (!finalize({ "position" }))
return false;
m_screenSize = getUniform("gScreenSize", uniform::kVec2);
m_colorTextureUnit = getUniform("gColorMap", uniform::kSampler);
post();
return true;
}
void defaultMethod::setColorTextureUnit(int unit) {
m_colorTextureUnit->set(unit);
}
void defaultMethod::setPerspective(const m::perspective &p) {
m_screenSize->set(m::vec2(p.width, p.height));
}
///! bboxMethod
bool bboxMethod::init() {
if (!method::init("bounding box"))
return false;
if (!addShader(GL_VERTEX_SHADER, "shaders/bbox.vs"))
return false;
if (!addShader(GL_FRAGMENT_SHADER, "shaders/bbox.fs"))
return false;
if (!finalize({ "position" }, { "diffuseOut" }))
return false;
m_WVP = getUniform("gWVP", uniform::kMat4);
m_color = getUniform("gColor", uniform::kVec3);
post();
return true;
}
void bboxMethod::setWVP(const m::mat4 &wvp) {
m_WVP->set(wvp);
}
void bboxMethod::setColor(const m::vec3 &color) {
m_color->set(color);
}
}