v4k-git-backup/tools/glsl_join.py

62 lines
2.3 KiB
Python
Raw Normal View History

2023-08-10 21:53:51 +00:00
import argparse
import os
def glsl_to_c(output_filename: str):
glsl_directory = "engine/shaders/"
all_shaders = []
for filename in os.listdir(glsl_directory):
if filename.endswith(".glsl"):
with open(os.path.join(glsl_directory, filename), 'r') as f:
shader_code = f.read()
shader_code_escaped = ""
lines = shader_code.split('\n')
for idx, line in enumerate(lines):
if any(keyword in line for keyword in ["#if", "#ifdef", "#else", "#endif"]):
shader_code_escaped += line
else:
shader_code_escaped += f'"{line}\\n"'
# Append semicolon if it's the last line
if idx == len(lines) - 1:
shader_code_escaped += ";"
shader_code_escaped += "\n"
variable_name = os.path.splitext(filename)[0] # Remove the .glsl extension
2023-08-20 17:58:34 +00:00
c_shader_code = f'const char *const {variable_name} = \"//\" FILELINE \"\\n\"\n{shader_code_escaped}'
2023-08-10 21:53:51 +00:00
all_shaders.append(c_shader_code)
# Write all shaders to the C file
with open(output_filename, 'w') as f:
2023-08-20 17:58:34 +00:00
f.write('\n'.join(all_shaders)+'\n')
def glsl_to_h(output_filename: str):
glsl_directory = "engine/shaders/"
all_shaders = []
for filename in os.listdir(glsl_directory):
if filename.endswith(".glsl"):
with open(os.path.join(glsl_directory, filename), 'r') as f:
variable_name = os.path.splitext(filename)[0] # Remove the .glsl extension
h_shader_code = f'extern const char *const {variable_name};'
all_shaders.append(h_shader_code)
# Write all shaders to the C file
with open(output_filename, 'w') as f:
f.write('\n'.join(all_shaders)+'\n')
2023-08-10 21:53:51 +00:00
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--output", required=True, help="Output filename for the C file")
2023-08-20 17:58:34 +00:00
parser.add_argument("--header", required=False, help="Is header file output requested", action=argparse.BooleanOptionalAction)
2023-08-10 21:53:51 +00:00
args = parser.parse_args()
2023-08-20 17:58:34 +00:00
if args.header is True:
glsl_to_h(args.output)
else:
glsl_to_c(args.output)
2023-08-10 21:53:51 +00:00
if __name__ == "__main__":
main()