diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2016-08-15 14:36:16 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2016-08-16 15:29:34 +0200 |
commit | 51391740bf324fd7131f069d1d2d106113560df3 (patch) | |
tree | 93b93bd806525966b531ed8d7b1aeeee43705058 /scripts | |
parent | 4e211548d5029a353aaa8814c40599970a8098bd (diff) | |
download | qtlocation-mapboxgl-51391740bf324fd7131f069d1d2d106113560df3.tar.gz |
[build] convert build-shaders script from Python to Node.js
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/build-shaders.js | 71 | ||||
-rwxr-xr-x | scripts/build-shaders.py | 76 |
2 files changed, 71 insertions, 76 deletions
diff --git a/scripts/build-shaders.js b/scripts/build-shaders.js new file mode 100755 index 0000000000..9090f19e6d --- /dev/null +++ b/scripts/build-shaders.js @@ -0,0 +1,71 @@ +#!/usr/bin/env node + +var path = require('path'); +var fs = require('fs'); +var mkdirp = require('mkdirp'); + +var input_file = process.argv[2] +var output_file = process.argv[3]; + +if (!input_file || !output_file) { + console.warn('No input or output file given.'); + console.warn('Usage: %s [input.vertex.glsl] [output.vertex.hpp]', path.basename(process.argv[1])); + process.exit(1); +} + +var components = path.basename(input_file).split('.'); + +var shader_name = components[0]; +var shader_type = components[1]; +var extension = components[2]; + +var data = fs.readFileSync(input_file, 'utf8'); + +// Replace uniform pragmas + +var pragma_mapbox_regex = /(\s*)#pragma\s+mapbox\s*:\s+(define|initialize)\s+(low|medium|high)p\s+(float|vec(?:2|3|4))\s+(.*)/; + +var data = data.split('\n').map(function(line) { + var params = line.match(pragma_mapbox_regex); + if (params) { + if (params[2] === 'define') { + return params[1] + 'uniform ' + params[3] + 'p ' + params[4] + ' u_' + params[5] + ';'; + } else { + return params[1] + params[3] + 'p ' + params[4] + ' ' + params[5] + ' = u_' + params[5] + ';'; + } + } else { + return line; + } +}).join('\n'); + + +var content = + '#pragma once\n' + + '\n' + + '// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n' + + '\n' + + '#include <mbgl/gl/gl.hpp>\n' + + '\n' + + 'namespace mbgl {\n' + + 'namespace shaders {\n' + + 'namespace ' + shader_name + ' {\n' + + '\n' + + '#ifndef MBGL_SHADER_NAME_' + shader_name.toUpperCase() + '\n' + + '#define MBGL_SHADER_NAME_' + shader_name.toUpperCase() + '\n' + + 'constexpr const char* name = "' + shader_name + '";\n' + + '#endif // MBGL_SHADER_NAME_' + shader_name.toUpperCase() + '\n' + + '\n' + + 'constexpr const char* ' + shader_type + ' =\n' + + '#ifdef GL_ES_VERSION_2_0\n' + + ' "precision highp float;"\n' + + '#else\n' + + ' "#version 120"\n' + + '#endif\n' + + ' R"MBGL_SHADER(\n' + data + ')MBGL_SHADER";\n' + + '\n' + + '} // namespace ' + shader_name + '\n' + + '} // namespace shaders\n' + + '} // namespace mbgl\n'; + +mkdirp.sync(path.dirname(output_file)); +fs.writeFileSync(output_file, content); diff --git a/scripts/build-shaders.py b/scripts/build-shaders.py deleted file mode 100755 index d679172554..0000000000 --- a/scripts/build-shaders.py +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env python - -import sys, re, os, errno - -input_file = sys.argv[1] -output_file = sys.argv[2] - -def mkdir_p(path): - try: - os.makedirs(path) - except OSError as exc: # Python >2.5 - if exc.errno == errno.EEXIST and os.path.isdir(path): - pass - else: raise - -shader_name, shader_type, extension = os.path.basename(input_file).split('.') - -with open(input_file, "r") as f: - data = f.read() - -# Replace uniform pragmas - -pragma_mapbox_regex = re.compile("(\s*)\#pragma \mapbox\: (define|initialize) (low|medium|high)p (float|vec(2|3|4)) (.*)") - -def replace_uniform_pragmas(line): - if pragma_mapbox_regex.match(line): - params = line.split() - if params[2] == "define": - return """uniform {u_precision} {u_type} u_{u_name};""".format( - u_precision = params[3], - u_type = params[4], - u_name = params[5]) - else: - return """ {u_precision} {u_type} {u_name} = u_{u_name};""".format( - u_precision = params[3], - u_type = params[4], - u_name = params[5]) - else: - return line - -data = "\n".join([replace_uniform_pragmas(line) for line in data.split("\n")]) - -content = """#pragma once - -// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. - -#include <mbgl/gl/gl.hpp> - -namespace mbgl {{ -namespace shaders {{ -namespace {name} {{ - -#ifndef MBGL_SHADER_NAME_{NAME} -#define MBGL_SHADER_NAME_{NAME} -constexpr const char* name = "{name}"; -#endif // MBGL_SHADER_NAME_{NAME} - -#ifdef GL_ES_VERSION_2_0 -constexpr const char* {type} = R"MBGL_SHADER(precision highp float;\n{data})MBGL_SHADER"; -#else -constexpr const char* {type} = R"MBGL_SHADER(#version 120\n{data})MBGL_SHADER"; -#endif - -}} // namespace {name} -}} // namespace shaders -}} // namespace mbgl -""".format( - name = shader_name, - NAME = shader_name.upper(), - type = shader_type, - TYPE = shader_type.upper(), - data = data, -) - -mkdir_p(os.path.dirname(output_file)) -with open(output_file, 'w') as f: f.write(content) |