-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
342 lines (281 loc) · 15 KB
/
main.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from standard import *
from colored_print import *
import argparse
from enum import Enum
from typing import Tuple
import re
import os
def extract_variables_from_shader(shader_code: str):
"""
Extracts uniforms and attributes from the shader code.
Returns a dictionary with uniforms and vertex attributes along with their types.
"""
uniform_pattern = r"uniform\s+(\w+)\s+(\w+);"
attribute_pattern = r"in\s+(\w+)\s+(\w+);" # GLSL version 330 core uses 'in' for attributes
uniforms = re.findall(uniform_pattern, shader_code)
attributes = re.findall(attribute_pattern, shader_code)
return {
"uniforms": {name: v_type for v_type, name in uniforms},
"attributes": {name: v_type for v_type, name in attributes},
}
def validate_types(shader_variables, verbose):
"""
Validates the types of uniforms and attributes against expected types from dictionaries.
"""
valid_uniforms = []
valid_attributes = []
# Check uniforms
for name, v_type in shader_variables['uniforms'].items():
# Get the expected type based on the key from shader_uniform_variable_to_data
expected_type = None
for uniform_var in shader_uniform_variable_to_data.keys():
if uniform_var.name == name.upper(): # Compare enum names to shader variable names
expected_type = shader_uniform_variable_to_data[uniform_var]
break
if expected_type:
if expected_type.glsl_type != v_type:
colored_print(f" Error: Uniform '{name}' expected type '{expected_type.glsl_type}' but found '{v_type}', fix the type in the shader.", TextColor.RED)
else:
valid_uniforms.append(name.upper())
if verbose:
colored_print(f" Verified uniform '{name}' with type '{v_type}'.", TextColor.WHITE)
else:
colored_print(f" ERROR: Uniform '{name}' is not recognized, you either have a typo or you need to register the new attribute in the standard.", TextColor.RED)
# Check attributes
for name, v_type in shader_variables['attributes'].items():
expected_data = None
for attrib_var in shader_vertex_attribute_to_data.keys():
if attrib_var.name == name.upper(): # Compare enum names to shader variable names
expected_data = shader_vertex_attribute_to_data[attrib_var]
break
if expected_data:
if expected_data.glsl_type != v_type:
colored_print(f" Error: Attribute '{name}' expected type '{expected_data.glsl_type}' but found '{v_type}', fix the type in the shader.", TextColor.RED)
else:
valid_attributes.append(name.upper())
if verbose:
colored_print(f" Verified attribute '{name}' with type '{v_type}'", TextColor.GRAY)
else:
colored_print(f" ERROR: Attribute '{name}' is not recognized, you either have a typo or you need to register the new attribute in the standard.", TextColor.RED)
return valid_attributes, valid_uniforms
def validate_shader(shader_code: str, shader_program: ShaderProgram, verbose: bool) -> Tuple:
"""
Validates a shader file by logging issues if found.
"""
shader_variables = extract_variables_from_shader(shader_code)
# Validate types
valid_attrib_unifs = validate_types(shader_variables, verbose)
return shader_variables, valid_attrib_unifs # Return the variables for later use
def validate_all_shaders(shader_catalog, shader_directory, verbose: bool, output_info: bool):
"""
Iterates over all shaders in the shader catalog and validates them.
:param shader_catalog: Dictionary mapping ShaderType to ShaderProgram instances.
:param shader_directory: The directory where shader files are located.
:param output_info: Whether to output shader variable information.
"""
shader_info = {}
for shader_type, shader_program in shader_catalog.items():
# Construct the full file paths
vertex_shader_path = os.path.join(shader_directory, shader_program.vertex_shader_filename)
fragment_shader_path = os.path.join(shader_directory, shader_program.fragment_shader_filename)
# Load shader files from disk
try:
with open(vertex_shader_path, 'r') as vertex_shader_file:
vertex_shader_code = vertex_shader_file.read()
except FileNotFoundError:
colored_print(f"Error: Vertex shader file '{vertex_shader_path}' not found.", TextColor.RED)
continue
try:
with open(fragment_shader_path, 'r') as fragment_shader_file:
fragment_shader_code = fragment_shader_file.read()
except FileNotFoundError:
colored_print(f"Error: Fragment shader file '{fragment_shader_path}' not found.", TextColor.RED)
continue
colored_print(f"Validating shaders for {shader_type}:", TextColor.GREEN)
# Validate shaders
colored_print(f" Validating vertex shader: {shader_program.vertex_shader_filename}", TextColor.GREEN)
vertex_variables, valid_attrib_unifs = validate_shader(vertex_shader_code, shader_program, verbose)
colored_print(f" Validating fragment shader: {shader_program.fragment_shader_filename}", TextColor.GREEN)
fragment_variables, valid_frag_attrib_unifs = validate_shader(fragment_shader_code, shader_program, verbose)
all_valid_uniforms = valid_attrib_unifs[1] + valid_frag_attrib_unifs[1]
# Store shader info
shader_info[shader_type] = {
"attributes": vertex_variables['attributes'],
"uniforms": vertex_variables['uniforms'],
"valid_attributes": valid_attrib_unifs[0],
"valid_uniforms": all_valid_uniforms,
}
if output_info:
# Print shader information
colored_print("Shader Information:", TextColor.BRIGHT_BLUE)
for shader_type, info in shader_info.items():
colored_print(f"Shader Type: {shader_type.name}", TextColor.GREEN)
colored_print(" Vertex Attributes:", TextColor.MAGENTA)
for attr, attr_type in info["attributes"].items():
colored_print(f" {attr}: {attr_type}", TextColor.GRAY)
colored_print(" Uniforms:", TextColor.MAGENTA)
for uniform, uniform_type in info["uniforms"].items():
colored_print(f" {uniform}: {uniform_type}", TextColor.GRAY)
return shader_info
def generate_cpp(shader_info):
cpp_output = []
cpp_output.append("#ifndef SHADER_STANDARD_HPP")
cpp_output.append("#define SHADER_STANDARD_HPP")
cpp_output.append("#include <unordered_map>")
cpp_output.append("#include <string>")
cpp_output.append("#include <vector>")
cpp_output.append("#include <glad/glad.h>")
cpp_output.append("")
cpp_output.append("enum class ShaderType {")
for shader_type in ShaderType:
cpp_output.append(f" {shader_type.name},")
cpp_output.append("};")
cpp_output.append("")
# ShaderVertexAttributeVariable enum
cpp_output.append("enum class ShaderVertexAttributeVariable {")
for attribute in ShaderVertexAttributeVariable:
if attribute != ShaderVertexAttributeVariable.INDEX: # Exclude INDEX
cpp_output.append(f" {attribute.name},")
cpp_output.append("};")
cpp_output.append("")
# ShaderUniformVariable enum
cpp_output.append("enum class ShaderUniformVariable {")
for uniform in ShaderUniformVariable:
cpp_output.append(f" {uniform.name},")
cpp_output.append("};")
cpp_output.append("")
# ShaderCreationInfo struct
cpp_output.append("struct ShaderCreationInfo {")
cpp_output.append(" std::string vertex_path;")
cpp_output.append(" std::string fragment_path;")
cpp_output.append(" std::string geometry_path;")
cpp_output.append("};")
cpp_output.append("")
# ShaderProgramInfo struct
cpp_output.append("struct ShaderProgramInfo {")
cpp_output.append(" GLuint id;")
cpp_output.append("};")
cpp_output.append("")
# GLVertexAttributeConfiguration struct
cpp_output.append("struct GLVertexAttributeConfiguration {")
cpp_output.append(" GLint components_per_vertex;")
cpp_output.append(" GLenum data_type_of_component;")
cpp_output.append(" GLboolean normalize;")
cpp_output.append(" GLsizei stride;")
cpp_output.append(" GLvoid *pointer_to_start_of_data;")
cpp_output.append("};")
cpp_output.append("")
# Start class definition
cpp_output.append("class ShaderStandard {")
cpp_output.append("public:")
cpp_output.append(" std::unordered_map<ShaderVertexAttributeVariable, GLVertexAttributeConfiguration> shader_vertex_attribute_to_glva_configuration;")
cpp_output.append(" std::unordered_map<ShaderUniformVariable, std::string> shader_uniform_variable_to_name;")
cpp_output.append(" std::unordered_map<ShaderVertexAttributeVariable, std::string> shader_vertex_attribute_variable_to_name;")
cpp_output.append(" std::unordered_map<ShaderType, std::string> shader_type_to_name;")
cpp_output.append(" std::unordered_map<ShaderType, ShaderCreationInfo> shader_catalog;")
# New variables for used vertex attributes and uniforms
cpp_output.append(" std::unordered_map<ShaderType, std::vector<ShaderVertexAttributeVariable>> shader_to_used_vertex_attribute_variables;")
cpp_output.append(" std::unordered_map<ShaderType, std::vector<ShaderUniformVariable>> shader_to_used_uniform_variable;")
cpp_output.append("")
cpp_output.append(" ShaderStandard() {")
cpp_output.append(" shader_vertex_attribute_to_glva_configuration = {")
for attribute, config in vertex_attribute_to_configuration.items():
if attribute != ShaderVertexAttributeVariable.INDEX: # Exclude INDEX
cpp_output.append(f" {{ShaderVertexAttributeVariable::{attribute.name}, GLVertexAttributeConfiguration{{{config.components_per_vertex}, {config.data_type_of_component}, {config.normalize}, {config.stride}, {config.pointer_to_start_of_data}}}}},")
cpp_output.append(" };")
cpp_output.append(" shader_uniform_variable_to_name = {")
for uniform in ShaderUniformVariable:
cpp_output.append(f" {{ShaderUniformVariable::{uniform.name}, \"{uniform.name.lower()}\"}},")
cpp_output.append(" };")
cpp_output.append(" shader_vertex_attribute_variable_to_name = {")
for attribute in ShaderVertexAttributeVariable:
if attribute in vertex_attribute_to_configuration.keys():
cpp_output.append(f" {{ShaderVertexAttributeVariable::{attribute.name}, \"{attribute.name.lower()}\"}},")
cpp_output.append(" };")
cpp_output.append(" shader_type_to_name = {")
for shader_type in ShaderType:
cpp_output.append(f" {{ShaderType::{shader_type.name}, \"{shader_type.name.lower()}\"}},")
cpp_output.append(" };")
cpp_output.append(" shader_catalog = {")
for shader_type, prog in shader_catalog.items():
cpp_output.append(f" {{ShaderType::{shader_type.name}, {{\"assets/shaders/{prog.vertex_shader_filename}\", \"assets/shaders/{prog.fragment_shader_filename}\"}}}},")
cpp_output.append(" };")
# Generate shader_to_used_vertex_attribute_variables
cpp_output.append(" shader_to_used_vertex_attribute_variables = {")
for shader_type, variables in shader_info.items():
attributes = ', '.join(f"ShaderVertexAttributeVariable::{attr}" for attr in variables['valid_attributes'])
cpp_output.append(f" {{ShaderType::{shader_type.name}, {{{attributes}}}}},")
cpp_output.append(" };")
# Generate shader_to_used_uniform_variable
cpp_output.append(" shader_to_used_uniform_variable = {")
for shader_type, variables in shader_info.items():
uniforms = ', '.join(f"ShaderUniformVariable::{uniform}" for uniform in variables['valid_uniforms'])
cpp_output.append(f" {{ShaderType::{shader_type.name}, {{{uniforms}}}}},")
cpp_output.append(" };")
cpp_output.append(" }")
# End class definition
cpp_output.append("};")
cpp_output.append("#endif // SHADER_STANDARD_HPP")
# Define the output directory (script directory)
output_directory = os.path.dirname(os.path.abspath(__file__))
# Write to output header file
header_file_path = os.path.join(output_directory, "shader_standard.hpp")
with open(header_file_path, "w") as hpp_file:
hpp_file.write("\n".join(cpp_output))
def generate_py_shader_summary(shader_info):
py_output = [] # List to accumulate output lines
# Generate shader_to_used_vertex_attribute_variables
py_output.append("from standard import *\n")
py_output.append("shader_to_used_vertex_attribute_variables = {\n")
for shader_type, variables in shader_info.items():
attributes = ', '.join(f"ShaderVertexAttributeVariable.{attr}" for attr in variables['valid_attributes'])
py_output.append(f" ShaderType.{shader_type.name}: [{attributes}],\n")
py_output.append("}\n\n")
# # Generate shader_to_used_uniform_variable
# py_output.append("shader_to_used_uniform_variable = {\n")
# for shader_type, variables in shader_info.items():
# uniforms = ', '.join(f"ShaderUniformVariable.{uniform}" for uniform in variables['valid_uniforms'])
# py_output.append(f" ShaderType.{shader_type.name}: {{{uniforms}}},\n")
# py_output.append("}\n")
# Define the output directory (script directory)
output_directory = os.path.dirname(os.path.abspath(__file__))
# Write all accumulated lines to the file
summary_file_path = os.path.join(output_directory, "shader_summary.py")
with open(summary_file_path, 'w') as f:
f.writelines(py_output)
if __name__ == "__main__":
# Set up argument parsing
parser = argparse.ArgumentParser(description="Shader Standard Manager")
parser.add_argument(
"--verbose",
"-v",
action="store_true",
help="Enable verbose output for successful verifications"
)
parser.add_argument(
"--shader-directory",
"-sd",
type=str,
default="../../assets/shaders/",
help="Path to the directory containing shader files"
)
parser.add_argument(
"--summary",
"-s",
action="store_true",
help="Output shader variable information for each shader"
)
parser.add_argument(
"--gen-cpp",
"-gc",
action="store_true",
help="Generates the required cpp file to integrate with the shader cache"
)
parser.add_argument('--gen-py-shader-summary', '-gp', action="store_true", help="Generate Python shader summary file")
args = parser.parse_args()
# Validate all shaders
shader_info = validate_all_shaders(shader_catalog, args.shader_directory, args.verbose, args.summary)
if args.gen_cpp:
generate_cpp(shader_info)
if args.gen_py_shader_summary:
generate_py_shader_summary(shader_info)