summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2018-07-24 15:40:33 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2018-07-25 11:39:05 -0700
commit888a516f1fec172081c06f4d2a98c08ae13a4d93 (patch)
tree9b25c54d9aa8866f500e8393a1a238f901a5835e /scripts
parent524a645f0b3b2c6e2891f3e07eff7305b575b2f9 (diff)
downloadqtlocation-mapboxgl-888a516f1fec172081c06f4d2a98c08ae13a4d93.tar.gz
[core] Compress shader source code
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/generate-shaders.js42
1 files changed, 30 insertions, 12 deletions
diff --git a/scripts/generate-shaders.js b/scripts/generate-shaders.js
index 6758793056..39307f358a 100755
--- a/scripts/generate-shaders.js
+++ b/scripts/generate-shaders.js
@@ -4,6 +4,7 @@ require('flow-remove-types/register');
const path = require('path');
const outputPath = 'src/mbgl/shaders';
+const zlib = require('zlib');
var shaders = require('../mapbox-gl-js/src/shaders');
@@ -11,6 +12,25 @@ 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();
+}
+
+function compressedConstant(data) {
+ return `[] () {
+ static const uint8_t compressed[] = {
+ ${compressedData(data)}
+ };
+ static std::string decompressed = util::decompress(std::string(reinterpret_cast<const char*>(compressed), sizeof(compressed)));
+ return decompressed.c_str();
+}()`
+}
+
writeIfModified(path.join(outputPath, 'preludes.hpp'), `// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
#pragma once
@@ -28,16 +48,15 @@ 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>
namespace mbgl {
namespace shaders {
-const char* vertexPrelude = R"MBGL_SHADER(
-${shaders.prelude.vertexSource}
-)MBGL_SHADER";
-const char* fragmentPrelude = R"MBGL_SHADER(
-${shaders.prelude.fragmentSource}
-)MBGL_SHADER";
+const char* vertexPrelude = ${compressedConstant(shaders.prelude.vertexSource)};
+const char* fragmentPrelude = ${compressedConstant(shaders.prelude.fragmentSource)};
} // namespace shaders
} // namespace mbgl
@@ -70,17 +89,16 @@ 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>
namespace mbgl {
namespace shaders {
const char* ${shaderName}::name = "${shaderName}";
-const char* ${shaderName}::vertexSource = R"MBGL_SHADER(
-${shaders[key].vertexSource}
-)MBGL_SHADER";
-const char* ${shaderName}::fragmentSource = R"MBGL_SHADER(
-${shaders[key].fragmentSource}
-)MBGL_SHADER";
+const char* ${shaderName}::vertexSource = ${compressedConstant(shaders[key].vertexSource)};
+const char* ${shaderName}::fragmentSource = ${compressedConstant(shaders[key].fragmentSource)};
} // namespace shaders
} // namespace mbgl