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
|
#!/usr/bin/env python
import sys, re, os, errno
output_dir = sys.argv[1]
file_names = 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
# 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()
def write_header():
header = """// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
#ifndef MBGL_SHADER_SHADERS
#define MBGL_SHADER_SHADERS
namespace mbgl {
struct shader_source {
const char *vertex;
const char *fragment;
};
enum {
%s
SHADER_COUNT
};
extern const shader_source shaders[SHADER_COUNT];
}
#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';
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}
""".format(
prefix = prefix,
suffix = suffix,
shaders = ''.join(shader_lines)
)
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')
|