summaryrefslogtreecommitdiff
path: root/scripts/generate-shaders.js
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2018-07-24 16:30:25 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2018-07-25 11:39:05 -0700
commit10e4ea0f28803dbea4b74319f7bb917d1acf560c (patch)
treee92b5746b1d2f064262e8e9b3a27720b3cba0b3a /scripts/generate-shaders.js
parent888a516f1fec172081c06f4d2a98c08ae13a4d93 (diff)
downloadqtlocation-mapboxgl-10e4ea0f28803dbea4b74319f7bb917d1acf560c.tar.gz
[core] Compress all shader source as a single corpus
Diffstat (limited to 'scripts/generate-shaders.js')
-rwxr-xr-xscripts/generate-shaders.js82
1 files changed, 60 insertions, 22 deletions
diff --git a/scripts/generate-shaders.js b/scripts/generate-shaders.js
index 39307f358a..5532bd33cd 100755
--- a/scripts/generate-shaders.js
+++ b/scripts/generate-shaders.js
@@ -12,24 +12,66 @@ delete shaders.lineGradient;
require('./style-code');
-function compressedData(src) {
- return zlib.deflateSync(src, {level: zlib.Z_BEST_COMPRESSION})
- .toString('hex')
- .match(/.{1,16}/g)
- .map(line => line.match(/.{1,2}/g).map(n => `0x${n}`).join(', '))
- .join(',\n ')
- .trim();
+let concatenated = '';
+let offsets = {};
+
+for (const key in shaders) {
+ const vertex = concatenated.length;
+ concatenated += shaders[key].vertexSource;
+ concatenated += '\0';
+
+ const fragment = concatenated.length;
+ concatenated += shaders[key].fragmentSource;
+ concatenated += '\0';
+
+ offsets[key] = {vertex, fragment};
+}
+
+const compressed = zlib.deflateSync(concatenated, {level: zlib.Z_BEST_COMPRESSION})
+ .toString('hex')
+ .match(/.{1,16}/g)
+ .map(line => line.match(/.{1,2}/g).map(n => `0x${n}`).join(', '))
+ .join(',\n ')
+ .trim();
+
+function sourceOffset(key, type) {
+ return `source() + ${offsets[key][type]}`
}
-function compressedConstant(data) {
- return `[] () {
+writeIfModified(path.join(outputPath, 'source.hpp'), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
+
+#pragma once
+
+namespace mbgl {
+namespace shaders {
+
+const char* source();
+
+} // namespace shaders
+} // namespace mbgl
+`);
+
+writeIfModified(path.join(outputPath, 'source.cpp'), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
+
+#include <mbgl/shaders/source.hpp>
+#include <mbgl/util/compression.hpp>
+
+#include <cstdint>
+
+namespace mbgl {
+namespace shaders {
+
+const char* source() {
static const uint8_t compressed[] = {
- ${compressedData(data)}
+ ${compressed}
};
static std::string decompressed = util::decompress(std::string(reinterpret_cast<const char*>(compressed), sizeof(compressed)));
return decompressed.c_str();
-}()`
-}
+};
+
+} // namespace shaders
+} // namespace mbgl
+`);
writeIfModified(path.join(outputPath, 'preludes.hpp'), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
@@ -48,15 +90,13 @@ extern const char* fragmentPrelude;
writeIfModified(path.join(outputPath, 'preludes.cpp'), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
#include <mbgl/shaders/preludes.hpp>
-#include <mbgl/util/compression.hpp>
-
-#include <cstdint>
+#include <mbgl/shaders/source.hpp>
namespace mbgl {
namespace shaders {
-const char* vertexPrelude = ${compressedConstant(shaders.prelude.vertexSource)};
-const char* fragmentPrelude = ${compressedConstant(shaders.prelude.fragmentSource)};
+const char* vertexPrelude = ${sourceOffset('prelude', 'vertex')};
+const char* fragmentPrelude = ${sourceOffset('prelude', 'fragment')};
} // namespace shaders
} // namespace mbgl
@@ -89,16 +129,14 @@ public:
writeIfModified(path.join(outputPath, `${shaderName}.cpp`), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
#include <mbgl/shaders/${shaderName}.hpp>
-#include <mbgl/util/compression.hpp>
-
-#include <cstdint>
+#include <mbgl/shaders/source.hpp>
namespace mbgl {
namespace shaders {
const char* ${shaderName}::name = "${shaderName}";
-const char* ${shaderName}::vertexSource = ${compressedConstant(shaders[key].vertexSource)};
-const char* ${shaderName}::fragmentSource = ${compressedConstant(shaders[key].fragmentSource)};
+const char* ${shaderName}::vertexSource = ${sourceOffset(key, 'vertex')};
+const char* ${shaderName}::fragmentSource = ${sourceOffset(key, 'fragment')};
} // namespace shaders
} // namespace mbgl