forked from mapsme/omim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader_preprocessor.py
231 lines (191 loc) · 8.86 KB
/
shader_preprocessor.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os
import sys
import hashlib
lowPDefine = "LOW_P"
lowPSearch = "lowp"
mediumPDefine = "MEDIUM_P"
mediumPSearch = "mediump"
highPDefine = "HIGH_P"
highPSearch = "highp"
maxPrecDefine = "MAXPREC_P"
maxPrecSearch = "MAXPREC"
def formatOutFilePath(baseDir, fileName):
return os.path.join(baseDir, "..", fileName)
def formatShaderSourceName(shaderFileName):
shaderSourceName = shaderFileName
return shaderSourceName.replace(".", "_").upper()
def formatShaderIndexName(shaderFileName):
return formatShaderSourceName(shaderFileName) + "_INDEX"
def formatShaderDocName(shaderName):
return shaderName.replace(".", "_") + ".txt"
def readIndexFile(filePath):
f = open(filePath)
gpuPrograms = dict()
index = 0
for line in f:
lineParts = line.strip().split()
if len(lineParts) != 3:
print("Incorrect GPU program definition : " + line)
exit(10)
vertexShader = next( f for f in lineParts if f.endswith(".vsh"))
fragmentShader = next( f for f in lineParts if f.endswith(".fsh"))
if not vertexShader:
print("Vertex shader not found in GPU program definition : " + line)
exit(11)
if not fragmentShader:
print("Fragment shader not found in GPU program definition : " + line)
exit(12)
if lineParts[0] in gpuPrograms.keys():
print("More than one difinition of %s gpu program" % lineParts[0])
exit(13)
gpuPrograms[index] = (vertexShader, fragmentShader, lineParts[0])
index += 1
return gpuPrograms
def generateShaderIndexes(shaders):
return dict((v, k) for k, v in enumerate(shaders))
def definitionChanged(newHeaderContent, defFilePath):
if not os.path.isfile(defFilePath):
return True
defContent = open(defFilePath, 'r').read()
oldMD5 = hashlib.md5()
oldMD5.update(defContent)
newMd5 = hashlib.md5()
newMd5.update(newHeaderContent)
return oldMD5.digest() != newMd5.digest()
def writeDefinitionFile(programIndex):
result = ""
result += "#pragma once\n\n"
result += "#include \"std/map.hpp\"\n"
result += "#include \"std/string.hpp\"\n"
result += "#include \"std/target_os.hpp\"\n"
result += "#include \"std/vector.hpp\"\n\n"
result += "namespace gpu\n"
result += "{\n"
result += "\n"
result += "#if defined(OMIM_OS_DESKTOP) && !defined(COMPILER_TESTS)\n"
result += " #define %s \"\" \n" % (lowPDefine)
result += " #define %s \"\" \n" % (mediumPDefine)
result += " #define %s \"\" \n" % (highPDefine)
result += " #define %s \"\" \n" % (maxPrecDefine)
result += "#else\n"
result += " #define %s \"%s\"\n" % (lowPDefine, lowPSearch)
result += " #define %s \"%s\"\n" % (mediumPDefine, mediumPSearch)
result += " #define %s \"%s\"\n" % (highPDefine, highPSearch)
result += " #define %s \"%s\"\n" % (maxPrecDefine, maxPrecSearch)
result += "#endif\n\n"
result += "struct ProgramInfo\n"
result += "{\n"
result += " ProgramInfo();\n"
result += " ProgramInfo(int vertexIndex, int fragmentIndex,\n"
result += " char const * vertexSource, char const * fragmentSource);\n"
result += " int m_vertexIndex;\n"
result += " int m_fragmentIndex;\n"
result += " char const * m_vertexSource;\n"
result += " char const * m_fragmentSource;\n"
result += "};\n\n"
for programName in programIndex.keys():
result += "extern int const %s;\n" % (programIndex[programName][2])
result += "\n"
result += "void InitGpuProgramsLib(map<int, ProgramInfo> & gpuIndex);\n\n"
result += "#if defined(COMPILER_TESTS)\n"
result += "extern vector<string> VertexEnum;\n"
result += "extern vector<string> FragmentEnum;\n\n"
result += "void InitEnumeration();\n\n"
result += "#endif\n"
result += "} // namespace gpu\n"
return result
def writeShader(outputFile, shaderFile, shaderDir):
outputFile.write(" static char const %s[] = \" \\\n" % (formatShaderSourceName(shaderFile)))
outputFile.write(" #ifdef GL_ES \\n\\\n")
outputFile.write(" #ifdef GL_FRAGMENT_PRECISION_HIGH \\n\\\n")
outputFile.write(" #define MAXPREC \" HIGH_P \" \\n\\\n")
outputFile.write(" #else \\n\\\n")
outputFile.write(" #define MAXPREC \" MEDIUM_P \" \\n\\\n")
outputFile.write(" #endif \\n\\\n")
outputFile.write(" precision MAXPREC float; \\n\\\n")
outputFile.write(" #endif \\n\\\n")
for line in open(os.path.join(shaderDir, shaderFile)):
if not line.lstrip().startswith("//"):
outputLine = line.rstrip().replace(lowPSearch, "\" " + lowPDefine + " \"")
outputLine = outputLine.replace(mediumPSearch, "\" " + mediumPDefine + " \"")
outputLine = outputLine.replace(highPSearch, "\" " + highPDefine+ " \"")
outputFile.write(" %s \\n\\\n" % (outputLine))
outputFile.write(" \";\n\n")
def writeShadersIndex(outputFile, shaderIndex):
for shader in shaderIndex:
outputFile.write("#define %s %s\n" % (formatShaderIndexName(shader), shaderIndex[shader]))
def writeImplementationFile(programsDef, shaderIndex, shaderDir, implFile, defFile, shaders):
vertexShaders = [s for s in shaders if s.endswith(".vsh")]
fragmentShaders = [s for s in shaders if s.endswith(".fsh")]
file = open(formatOutFilePath(shaderDir, implFile), 'w')
file.write("#include \"%s\"\n\n" % (defFile))
file.write("#include \"std/utility.hpp\"\n\n")
file.write("namespace gpu\n")
file.write("{\n\n")
for shader in shaderIndex.keys():
writeShader(file, shader, shaderDir)
file.write("//---------------------------------------------//\n")
writeShadersIndex(file, shaderIndex)
file.write("//---------------------------------------------//\n")
file.write("#if defined(COMPILER_TESTS)\n")
file.write("vector<string> VertexEnum;\n")
file.write("vector<string> FragmentEnum;\n\n")
file.write("#endif\n")
file.write("//---------------------------------------------//\n")
for program in programsDef:
file.write("const int %s = %s;\n" % (programsDef[program][2], program));
file.write("//---------------------------------------------//\n")
file.write("\n")
file.write("ProgramInfo::ProgramInfo()\n")
file.write(" : m_vertexIndex(-1)\n")
file.write(" , m_fragmentIndex(-1)\n")
file.write(" , m_vertexSource(NULL)\n")
file.write(" , m_fragmentSource(NULL) {}\n\n")
file.write("ProgramInfo::ProgramInfo(int vertexIndex, int fragmentIndex,\n")
file.write(" char const * vertexSource, char const * fragmentSource)\n")
file.write(" : m_vertexIndex(vertexIndex)\n")
file.write(" , m_fragmentIndex(fragmentIndex)\n")
file.write(" , m_vertexSource(vertexSource)\n")
file.write(" , m_fragmentSource(fragmentSource)\n")
file.write("{\n")
file.write("}\n")
file.write("\n")
file.write("void InitGpuProgramsLib(map<int, ProgramInfo> & gpuIndex)\n")
file.write("{\n")
for program in programsDef.keys():
vertexShader = programsDef[program][0]
vertexIndexName = formatShaderIndexName(vertexShader)
vertexSourceName = formatShaderSourceName(vertexShader)
fragmentShader = programsDef[program][1]
fragmentIndexName = formatShaderIndexName(fragmentShader)
fragmentSourceName = formatShaderSourceName(fragmentShader)
file.write(" gpuIndex.insert(make_pair(%s, ProgramInfo(%s, %s, %s, %s)));\n" % (program, vertexIndexName, fragmentIndexName, vertexSourceName, fragmentSourceName))
file.write("}\n\n")
file.write("#if defined(COMPILER_TESTS)\n")
file.write("void InitEnumeration()\n")
file.write("{\n")
for s in vertexShaders:
file.write(" VertexEnum.push_back(string(%s));\n" % formatShaderSourceName(s))
for s in fragmentShaders:
file.write(" FragmentEnum.push_back(string(%s));\n" % formatShaderSourceName(s))
file.write("}\n\n")
file.write("#endif\n")
file.write("} // namespace gpu\n")
file.close()
###############################
if len(sys.argv) < 4:
print("Usage : " + sys.argv[0] + " <shader_dir> <index_file_name> <generate_file_name>")
exit(1)
shaderDir = sys.argv[1]
indexFileName = sys.argv[2]
definesFile = sys.argv[3] + ".hpp"
implFile = sys.argv[3] + ".cpp"
shaders = [file for file in os.listdir(shaderDir) if os.path.isfile(os.path.join(shaderDir, file)) and (file.endswith(".vsh") or file.endswith(".fsh"))]
shaderIndex = generateShaderIndexes(shaders)
programDefinition = readIndexFile(os.path.join(shaderDir, indexFileName))
headerFile = writeDefinitionFile(programDefinition)
if definitionChanged(headerFile, formatOutFilePath(shaderDir, definesFile)):
f = open(formatOutFilePath(shaderDir, definesFile), 'w')
f.write(headerFile)
f.close()
writeImplementationFile(programDefinition, shaderIndex, shaderDir, implFile, definesFile, shaders)