SCons: Modernize shader builders

This commit is contained in:
Thaddeus Crews 2025-03-18 12:07:23 -05:00
parent adc63c6149
commit 32de6285a8
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC
8 changed files with 339 additions and 457 deletions

View file

@ -1,9 +1,8 @@
"""Functions used to generate source files during build time"""
import os.path
from typing import Optional
from methods import print_error, to_raw_cstring
from methods import generated_wrapper, print_error, to_raw_cstring
class RDHeaderStruct:
@ -92,61 +91,49 @@ def include_file_in_rd_header(filename: str, header_data: RDHeaderStruct, depth:
return header_data
def build_rd_header(
filename: str, optional_output_filename: Optional[str] = None, header_data: Optional[RDHeaderStruct] = None
) -> None:
header_data = header_data or RDHeaderStruct()
include_file_in_rd_header(filename, header_data, 0)
if optional_output_filename is None:
out_file = filename + ".gen.h"
else:
out_file = optional_output_filename
out_file_base = out_file
out_file_base = out_file_base[out_file_base.rfind("/") + 1 :]
out_file_base = out_file_base[out_file_base.rfind("\\") + 1 :]
out_file_class = out_file_base.replace(".glsl.gen.h", "").title().replace("_", "").replace(".", "") + "ShaderRD"
if header_data.compute_lines:
body_parts = [
"static const char _compute_code[] = {\n%s\n\t\t};" % to_raw_cstring(header_data.compute_lines),
f'setup(nullptr, nullptr, _compute_code, "{out_file_class}");',
]
else:
body_parts = [
"static const char _vertex_code[] = {\n%s\n\t\t};" % to_raw_cstring(header_data.vertex_lines),
"static const char _fragment_code[] = {\n%s\n\t\t};" % to_raw_cstring(header_data.fragment_lines),
f'setup(_vertex_code, _fragment_code, nullptr, "{out_file_class}");',
]
body_content = "\n\t\t".join(body_parts)
# Intended curly brackets are doubled so f-string doesn't eat them up.
shader_template = f"""/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
#pragma once
def build_rd_header(filename: str, shader: str) -> None:
include_file_in_rd_header(shader, header_data := RDHeaderStruct(), 0)
class_name = os.path.basename(shader).replace(".glsl", "").title().replace("_", "").replace(".", "") + "ShaderRD"
with generated_wrapper(filename) as file:
file.write(f"""\
#include "servers/rendering/renderer_rd/shader_rd.h"
class {out_file_class} : public ShaderRD {{
class {class_name} : public ShaderRD {{
public:
{class_name}() {{
""")
{out_file_class}() {{
if header_data.compute_lines:
file.write(f"""\
static const char *_vertex_code = nullptr;
static const char *_fragment_code = nullptr;
static const char _compute_code[] = {{
{to_raw_cstring(header_data.compute_lines)}
}};
""")
else:
file.write(f"""\
static const char _vertex_code[] = {{
{to_raw_cstring(header_data.vertex_lines)}
}};
static const char _fragment_code[] = {{
{to_raw_cstring(header_data.fragment_lines)}
}};
static const char *_compute_code = nullptr;
""")
{body_content}
file.write(f"""\
setup(_vertex_code, _fragment_code, _compute_code, "{class_name}");
}}
}};
"""
with open(out_file, "w", encoding="utf-8", newline="\n") as fd:
fd.write(shader_template)
""")
def build_rd_headers(target, source, env):
env.NoCache(target)
for x in source:
build_rd_header(filename=str(x))
for src in source:
build_rd_header(f"{src}.gen.h", str(src))
class RAWHeaderStruct:
@ -171,34 +158,18 @@ def include_file_in_raw_header(filename: str, header_data: RAWHeaderStruct, dept
line = fs.readline()
def build_raw_header(
filename: str, optional_output_filename: Optional[str] = None, header_data: Optional[RAWHeaderStruct] = None
):
header_data = header_data or RAWHeaderStruct()
include_file_in_raw_header(filename, header_data, 0)
def build_raw_header(filename: str, shader: str) -> None:
include_file_in_raw_header(shader, header_data := RAWHeaderStruct(), 0)
if optional_output_filename is None:
out_file = filename + ".gen.h"
else:
out_file = optional_output_filename
out_file_base = out_file.replace(".glsl.gen.h", "_shader_glsl")
out_file_base = out_file_base[out_file_base.rfind("/") + 1 :]
out_file_base = out_file_base[out_file_base.rfind("\\") + 1 :]
shader_template = f"""/* WARNING, THIS FILE WAS GENERATED, DO NOT EDIT */
#pragma once
static const char {out_file_base}[] = {{
with generated_wrapper(filename) as file:
file.write(f"""\
static const char {os.path.basename(shader).replace(".glsl", "_shader_glsl")}[] = {{
{to_raw_cstring(header_data.code)}
}};
"""
with open(out_file, "w", encoding="utf-8", newline="\n") as f:
f.write(shader_template)
""")
def build_raw_headers(target, source, env):
env.NoCache(target)
for x in source:
build_raw_header(filename=str(x))
for src in source:
build_raw_header(f"{src}.gen.h", str(src))