Skip to content

Commit 32de628

Browse files
committed
SCons: Modernize shader builders
1 parent adc63c6 commit 32de628

File tree

8 files changed

+364
-482
lines changed

8 files changed

+364
-482
lines changed

gles3_builders.py

Lines changed: 292 additions & 359 deletions
Large diffs are not rendered by default.

glsl_builders.py

Lines changed: 42 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""Functions used to generate source files during build time"""
22

33
import os.path
4-
from typing import Optional
54

6-
from methods import print_error, to_raw_cstring
5+
from methods import generated_wrapper, print_error, to_raw_cstring
76

87

98
class RDHeaderStruct:
@@ -92,61 +91,49 @@ def include_file_in_rd_header(filename: str, header_data: RDHeaderStruct, depth:
9291
return header_data
9392

9493

95-
def build_rd_header(
96-
filename: str, optional_output_filename: Optional[str] = None, header_data: Optional[RDHeaderStruct] = None
97-
) -> None:
98-
header_data = header_data or RDHeaderStruct()
99-
include_file_in_rd_header(filename, header_data, 0)
100-
101-
if optional_output_filename is None:
102-
out_file = filename + ".gen.h"
103-
else:
104-
out_file = optional_output_filename
105-
106-
out_file_base = out_file
107-
out_file_base = out_file_base[out_file_base.rfind("/") + 1 :]
108-
out_file_base = out_file_base[out_file_base.rfind("\\") + 1 :]
109-
out_file_class = out_file_base.replace(".glsl.gen.h", "").title().replace("_", "").replace(".", "") + "ShaderRD"
110-
111-
if header_data.compute_lines:
112-
body_parts = [
113-
"static const char _compute_code[] = {\n%s\n\t\t};" % to_raw_cstring(header_data.compute_lines),
114-
f'setup(nullptr, nullptr, _compute_code, "{out_file_class}");',
115-
]
116-
else:
117-
body_parts = [
118-
"static const char _vertex_code[] = {\n%s\n\t\t};" % to_raw_cstring(header_data.vertex_lines),
119-
"static const char _fragment_code[] = {\n%s\n\t\t};" % to_raw_cstring(header_data.fragment_lines),
120-
f'setup(_vertex_code, _fragment_code, nullptr, "{out_file_class}");',
121-
]
122-
123-
body_content = "\n\t\t".join(body_parts)
124-
125-
# Intended curly brackets are doubled so f-string doesn't eat them up.
126-
shader_template = f"""/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
127-
#pragma once
94+
def build_rd_header(filename: str, shader: str) -> None:
95+
include_file_in_rd_header(shader, header_data := RDHeaderStruct(), 0)
96+
class_name = os.path.basename(shader).replace(".glsl", "").title().replace("_", "").replace(".", "") + "ShaderRD"
12897

98+
with generated_wrapper(filename) as file:
99+
file.write(f"""\
129100
#include "servers/rendering/renderer_rd/shader_rd.h"
130101
131-
class {out_file_class} : public ShaderRD {{
132-
102+
class {class_name} : public ShaderRD {{
133103
public:
134-
135-
{out_file_class}() {{
136-
137-
{body_content}
104+
{class_name}() {{
105+
""")
106+
107+
if header_data.compute_lines:
108+
file.write(f"""\
109+
static const char *_vertex_code = nullptr;
110+
static const char *_fragment_code = nullptr;
111+
static const char _compute_code[] = {{
112+
{to_raw_cstring(header_data.compute_lines)}
113+
}};
114+
""")
115+
else:
116+
file.write(f"""\
117+
static const char _vertex_code[] = {{
118+
{to_raw_cstring(header_data.vertex_lines)}
119+
}};
120+
static const char _fragment_code[] = {{
121+
{to_raw_cstring(header_data.fragment_lines)}
122+
}};
123+
static const char *_compute_code = nullptr;
124+
""")
125+
126+
file.write(f"""\
127+
setup(_vertex_code, _fragment_code, _compute_code, "{class_name}");
138128
}}
139129
}};
140-
"""
141-
142-
with open(out_file, "w", encoding="utf-8", newline="\n") as fd:
143-
fd.write(shader_template)
130+
""")
144131

145132

146133
def build_rd_headers(target, source, env):
147134
env.NoCache(target)
148-
for x in source:
149-
build_rd_header(filename=str(x))
135+
for src in source:
136+
build_rd_header(f"{src}.gen.h", str(src))
150137

151138

152139
class RAWHeaderStruct:
@@ -171,34 +158,18 @@ def include_file_in_raw_header(filename: str, header_data: RAWHeaderStruct, dept
171158
line = fs.readline()
172159

173160

174-
def build_raw_header(
175-
filename: str, optional_output_filename: Optional[str] = None, header_data: Optional[RAWHeaderStruct] = None
176-
):
177-
header_data = header_data or RAWHeaderStruct()
178-
include_file_in_raw_header(filename, header_data, 0)
161+
def build_raw_header(filename: str, shader: str) -> None:
162+
include_file_in_raw_header(shader, header_data := RAWHeaderStruct(), 0)
179163

180-
if optional_output_filename is None:
181-
out_file = filename + ".gen.h"
182-
else:
183-
out_file = optional_output_filename
184-
185-
out_file_base = out_file.replace(".glsl.gen.h", "_shader_glsl")
186-
out_file_base = out_file_base[out_file_base.rfind("/") + 1 :]
187-
out_file_base = out_file_base[out_file_base.rfind("\\") + 1 :]
188-
189-
shader_template = f"""/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
190-
#pragma once
191-
192-
static const char {out_file_base}[] = {{
164+
with generated_wrapper(filename) as file:
165+
file.write(f"""\
166+
static const char {os.path.basename(shader).replace(".glsl", "_shader_glsl")}[] = {{
193167
{to_raw_cstring(header_data.code)}
194168
}};
195-
"""
196-
197-
with open(out_file, "w", encoding="utf-8", newline="\n") as f:
198-
f.write(shader_template)
169+
""")
199170

200171

201172
def build_raw_headers(target, source, env):
202173
env.NoCache(target)
203-
for x in source:
204-
build_raw_header(filename=str(x))
174+
for src in source:
175+
build_raw_header(f"{src}.gen.h", str(src))

methods.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,15 +1503,15 @@ def generated_wrapper(
15031503
unassigned, the value is determined by file extension.
15041504
"""
15051505

1506-
if guard is None:
1507-
guard = path.endswith((".h", ".hh", ".hpp", ".hxx", ".inc"))
1508-
15091506
with open(path, "wt", encoding="utf-8", newline="\n") as file:
1510-
file.write(generate_copyright_header(path))
1511-
file.write("\n/* THIS FILE IS GENERATED. EDITS WILL BE LOST. */\n\n")
1512-
1513-
if guard:
1514-
file.write("#pragma once\n\n")
1507+
if not path.endswith(".out"): # For test output, we only care about the content.
1508+
file.write(generate_copyright_header(path))
1509+
file.write("\n/* THIS FILE IS GENERATED. EDITS WILL BE LOST. */\n\n")
1510+
1511+
if guard is None:
1512+
guard = path.endswith((".h", ".hh", ".hpp", ".hxx", ".inc"))
1513+
if guard:
1514+
file.write("#pragma once\n\n")
15151515

15161516
with StringIO(newline="\n") as str_io:
15171517
yield str_io
Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
1-
/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
2-
#pragma once
3-
4-
51
#include "drivers/gles3/shader_gles3.h"
62

7-
83
class VertexFragmentShaderGLES3 : public ShaderGLES3 {
9-
104
public:
11-
125
enum ShaderVariant {
136
MODE_NINEPATCH,
147
};
158

169
enum Specializations {
17-
DISABLE_LIGHTING=1,
10+
DISABLE_LIGHTING = 1,
1811
};
1912

20-
_FORCE_INLINE_ bool version_bind_shader(RID p_version,ShaderVariant p_variant,uint64_t p_specialization=0) { return _version_bind_shader(p_version,p_variant,p_specialization); }
13+
_FORCE_INLINE_ bool version_bind_shader(RID p_version, ShaderVariant p_variant, uint64_t p_specialization = 0) {
14+
return _version_bind_shader(p_version, p_variant, p_specialization);
15+
}
2116

2217
protected:
23-
2418
virtual void _init() override {
25-
26-
static const char **_uniform_strings=nullptr;
27-
static const char* _variant_defines[]={
19+
static const char **_uniform_strings = nullptr;
20+
static const char *_variant_defines[] = {
2821
"#define USE_NINEPATCH",
2922
};
30-
31-
static TexUnitPair *_texunit_pairs=nullptr;
32-
static UBOPair *_ubo_pairs=nullptr;
33-
static Specialization _spec_pairs[]={
34-
{"DISABLE_LIGHTING",false},
23+
static TexUnitPair *_texunit_pairs = nullptr;
24+
static UBOPair *_ubo_pairs = nullptr;
25+
static Specialization _spec_pairs[] = {
26+
{ "DISABLE_LIGHTING", false },
3527
};
36-
37-
static const Feedback* _feedbacks=nullptr;
38-
static const char _vertex_code[]={
28+
static const Feedback *_feedbacks = nullptr;
29+
static const char _vertex_code[] = {
3930
R"<!>(
4031
precision highp float;
4132
precision highp int;
@@ -51,7 +42,7 @@ void main() {
5142
)<!>"
5243
};
5344

54-
static const char _fragment_code[]={
45+
static const char _fragment_code[] = {
5546
R"<!>(
5647
precision highp float;
5748
precision highp int;
@@ -65,7 +56,9 @@ void main() {
6556
)<!>"
6657
};
6758

68-
_setup(_vertex_code,_fragment_code,"VertexFragmentShaderGLES3",0,_uniform_strings,0,_ubo_pairs,0,_feedbacks,0,_texunit_pairs,1,_spec_pairs,1,_variant_defines);
59+
_setup(_vertex_code, _fragment_code, "VertexFragmentShaderGLES3",
60+
0, _uniform_strings, 0, _ubo_pairs,
61+
0, _feedbacks, 0, _texunit_pairs,
62+
1, _spec_pairs, 1, _variant_defines);
6963
}
70-
7164
};

tests/python_build/fixtures/glsl/compute.out

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
2-
#pragma once
3-
41
static const char compute_shader_glsl[] = {
52
R"<!>(#[compute]
63

tests/python_build/fixtures/glsl/vertex_fragment.out

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
2-
#pragma once
3-
41
static const char vertex_fragment_shader_glsl[] = {
52
R"<!>(#[versions]
63

tests/python_build/fixtures/rd_glsl/compute.out

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
2-
#pragma once
3-
41
#include "servers/rendering/renderer_rd/shader_rd.h"
52

63
class ComputeShaderRD : public ShaderRD {
7-
84
public:
9-
105
ComputeShaderRD() {
11-
6+
static const char *_vertex_code = nullptr;
7+
static const char *_fragment_code = nullptr;
128
static const char _compute_code[] = {
139
R"<!>(
1410
#version 450
@@ -24,6 +20,6 @@ void main() {
2420
}
2521
)<!>"
2622
};
27-
setup(nullptr, nullptr, _compute_code, "ComputeShaderRD");
23+
setup(_vertex_code, _fragment_code, _compute_code, "ComputeShaderRD");
2824
}
2925
};

tests/python_build/fixtures/rd_glsl/vertex_fragment.out

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
2-
#pragma once
3-
41
#include "servers/rendering/renderer_rd/shader_rd.h"
52

63
class VertexFragmentShaderRD : public ShaderRD {
7-
84
public:
9-
105
VertexFragmentShaderRD() {
11-
126
static const char _vertex_code[] = {
137
R"<!>(
148
#version 450
@@ -38,6 +32,7 @@ void main() {
3832
}
3933
)<!>"
4034
};
41-
setup(_vertex_code, _fragment_code, nullptr, "VertexFragmentShaderRD");
35+
static const char *_compute_code = nullptr;
36+
setup(_vertex_code, _fragment_code, _compute_code, "VertexFragmentShaderRD");
4237
}
4338
};

0 commit comments

Comments
 (0)