-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_cmake_includes.py
111 lines (83 loc) · 3.05 KB
/
generate_cmake_includes.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
#!/usr/bin/env python3
# Helper script to generate 'add_custom_command' includes for the glsl shader source includes
HEADER = """
###################################################################
# Automatically generated block for inclusion of the shader files #
# See "generate_cmake_includes.py" for more information #
###################################################################
# Set shaders directory and create it at build generation time
set(cmake_shaders_dir @{h_dir}@)
file(MAKE_DIRECTORY ${cmake_shaders_dir})
"""
BODY = """
add_custom_command(
OUTPUT ${cmake_shaders_dir}/@{program}@.h
COMMAND python3 @{embed_shader_py_dir}@ @{shader_sources}@ ${cmake_shaders_dir}/@{program}@.h
DEPENDS @{embed_shader_py_dir}@ @{shader_sources}@
)
add_custom_target( generate_@{program}@ DEPENDS ${cmake_shaders_dir}/@{program}@.h )
add_dependencies( @{main_target_name}@ generate_@{program}@ )
"""
FOOTER = """
target_include_directories( @{main_target_name}@ PUBLIC ${cmake_shaders_dir} )
###################################################################
# End of automatically generated block #
###################################################################
"""
main_target_name = "goo"
embed_shader_py_dir = "${CMAKE_CURRENT_SOURCE_DIR}/embed_shader.py"
shaders_dir = "./shaders"
cmake_shaders_dir = "${CMAKE_CURRENT_SOURCE_DIR}/shaders"
h_dir = "${CMAKE_CURRENT_BINARY_DIR}/shaders"
# https://stackoverflow.com/a/26531467/2531987
glsl_pipeline_order = {
".vert": 0, # Vartex shader
".tasc": 1, # Tasselation control shader
".tase": 2, # Tasselation evaluation shader
".geom": 3, # Geometry shader
".frag": 4, # Fragment shader
".comp": 5, # Compute shader
}
import os
from collections import defaultdict
programs = defaultdict(list)
# Find all shaders for each program
for file in os.listdir(shaders_dir):
name, ext = os.path.splitext(file)
if ext in glsl_pipeline_order:
programs[name].append(ext)
# Sort in pipeline order
for program in programs:
programs[program].sort(key=lambda val: glsl_pipeline_order[val])
# Create all sources
sources = []
for program, shaders in programs.items():
source = ""
for shader in shaders:
source += f"{cmake_shaders_dir}/{program}{shader} "
source = source[:-1]
sources.append((program, source))
# Create cmake shader includes
text = ""
header = HEADER
header = header.replace("@{h_dir}@", h_dir)
text += header
for program, source in sources:
body = BODY
body = body.replace("@{main_target_name}@", main_target_name)
body = body.replace("@{embed_shader_py_dir}@", embed_shader_py_dir)
body = body.replace("@{program}@", program)
body = body.replace("@{shader_sources}@", source)
text += body
footer = FOOTER
footer = footer.replace("@{main_target_name}@", main_target_name)
text += footer
assert not any(l == "@" for l in text)
import sys
argv = sys.argv
if len(argv) == 2:
output_filename = argv[1]
with open(output_filename, "w") as f:
f.write(text)
else:
print(text)