summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2015-12-10 11:59:30 -0800
committerKonstantin Käfer <mail@kkaefer.com>2015-12-10 11:59:51 -0800
commit655b777635439d64f46930b20e6436c2f36364dc (patch)
treeafea63d31c098338d530ed7e8c079524b8c59913 /scripts
parentb7a238f425737a222304aff241b927b16c693fc7 (diff)
downloadqtlocation-mapboxgl-655b777635439d64f46930b20e6436c2f36364dc.tar.gz
[core] don't rebuild shaders unless they change
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/build-shaders.py106
1 files changed, 26 insertions, 80 deletions
diff --git a/scripts/build-shaders.py b/scripts/build-shaders.py
index 3f3b776710..226c1a2ecc 100755
--- a/scripts/build-shaders.py
+++ b/scripts/build-shaders.py
@@ -2,10 +2,8 @@
import sys, re, os, errno
-
-output_dir = sys.argv[1]
-file_names = sys.argv[2:]
-
+input_file = sys.argv[1]
+output_file = sys.argv[2]
def mkdir_p(path):
try:
@@ -15,92 +13,40 @@ def mkdir_p(path):
pass
else: raise
-# Load all shaders
-shaders = {}
-for file_name in file_names:
- parts = re.search('/(\w+)\.(vertex|fragment)\.glsl$', file_name)
- if parts:
- shader_name = parts.group(1)
- shader_type = parts.group(2)
- if not shader_name in shaders:
- shaders[shader_name] = {}
- with open(file_name, "r") as f:
- shaders[shader_name][shader_type] = f.read()
+shader_name, shader_type, extension = os.path.basename(input_file).split('.')
+with open(input_file, "r") as f:
+ data = f.read()
-def write_header():
- header = """// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
+content = """// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
-#ifndef MBGL_SHADER_SHADERS
-#define MBGL_SHADER_SHADERS
+#ifndef MBGL_SHADER_{NAME}_{TYPE}
+#define MBGL_SHADER_{NAME}_{TYPE}
#include <mbgl/platform/gl.hpp>
-namespace mbgl {
-
-struct shader_source {
- const GLchar *vertex;
- const GLchar *fragment;
-};
-
-enum {
-%s
- SHADER_COUNT
-};
-
-extern const shader_source shaders[SHADER_COUNT];
-
-}
+namespace mbgl {{
+namespace shaders {{
+namespace {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
-""" % '\n'.join([' %s_SHADER,' % name.upper() for name in shaders.keys()])
- header_path = os.path.join(output_dir, 'include/mbgl/shader/shaders.hpp')
- mkdir_p(os.path.dirname(header_path))
- with open(header_path, 'w') as f: f.write(header)
-
-def write_source(shader_platform, prefix, suffix):
- if shader_platform == 'gles2' or shader_platform == 'gles3':
- # OpenGL ES
- preamble = 'precision highp float;';
- else:
- # Desktop OpenGL
- preamble = '#version 120';
+}} // namespace {name}
+}} // namespace shaders
+}} // namespace mbgl
- shader_lines = [(""" {{
- R"{name}_vert({preamble}\n{vertex}){name}_vert",
- R"{name}_frag({preamble}\n{fragment}){name}_frag",
- }},
-""").format(
- name = shader,
- preamble = preamble,
- vertex = shaders[shader]['vertex'],
- fragment = shaders[shader]['fragment']
-) for shader in shaders]
-
- source = """// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
-#include <mbgl/platform/gl.hpp>
-{prefix}
-#include <mbgl/shader/shaders.hpp>
-
-namespace mbgl {{
-
-const shader_source shaders[SHADER_COUNT] = {{
-{shaders}
-}};
-
-}}
-{suffix}
+#endif
""".format(
- prefix = prefix,
- suffix = suffix,
- shaders = ''.join(shader_lines)
+ name = shader_name,
+ NAME = shader_name.upper(),
+ type = shader_type,
+ TYPE = shader_type.upper(),
+ data = data,
)
- source_path = os.path.join(output_dir, 'src/shader/shaders_' + shader_platform + '.cpp')
- mkdir_p(os.path.dirname(source_path))
- with open(source_path, 'w') as f: f.write(source)
-
-write_header()
-write_source('gl', '#ifndef GL_ES_VERSION_2_0', '#endif')
-write_source('gles2', '#ifdef GL_ES_VERSION_2_0', '#endif')
+mkdir_p(os.path.dirname(output_file))
+with open(output_file, 'w') as f: f.write(content)