diff --git a/tools/ShaderToyProcessor.py b/tools/ShaderToyProcessor.py index 782b356..6a395b8 100644 --- a/tools/ShaderToyProcessor.py +++ b/tools/ShaderToyProcessor.py @@ -1,4 +1,48 @@ -# This file was originally part of the KDE Shader Wallpaper Project +# Komplex Wallpaper Engine +# Copyright (C) 2025 @DigitalArtifex | github.com/DigitalArtifex +# +# ShaderToyProcessor.py +# +# This file is used to convert the shaders in a shadertoy entry to a Komplex wallpaper +# package. It is designed to automate the process of preparing and compiling the shader +# files. +# +# The process is as follows: +# 1) Process the Common.frag file, if it exists +# 2) Read in the source file (.frag) +# 3) Append the Common.frag file, if it exists +# 4) Save file as `Name.tmp` into the temp directory +# 5) Process `Name.tmp` with `cpp -P`, outputting it as `Name.frag` +# 6) Delete the temp file +# 7) Prepare `Name.frag` by adding ubuf struct and version info +# 8) Replace known buffer calls to their ubuf equivalent +# 9) Compile `Name.frag` +# 10) Copy non-shader files +# +# This expanded process covers the following caveats of the original script: +# 1) when ubuf member names are used in Common file functions. +# ==== For instance, if the creator used iTime as a function variable +# this script renames the variable to _iTime +# 2) alters macro expansion to allow ill-formed macro use +# ==== Macros that take arguments, but don't have arguments in use would cause errors +# +# Usage: +# python ShaderToyProcessor.py [options] -i input_directory [-o output_dirctory] [-t temp_directory] +# +# This file uses code that was originally part of the KDE Shader Wallpaper Project. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see import os import re @@ -24,6 +68,7 @@ variables_to_update = [ ] # Header to be prepended to the shader file +# do not include the version declarative header = '''#version 450 layout(location = 0) in vec2 qt_TexCoord0; @@ -72,7 +117,8 @@ Examples: ) parser.add_argument('-i', '--input', - help='Input directory to process') + help='Input directory to process', + required=True) parser.add_argument('-v', '--verbose', action='store_true', @@ -88,108 +134,12 @@ Examples: parser.add_argument('-q', '--qsb', default='/usr/lib/qt6/bin/qsb', - help='QSB Compiler Location') + help='Path to QSB Compiler') return parser.parse_args() -def process(): - args = parse_arguments() - - if args.temp: - temp_directory = args.temp - - if args.input: - source_directory = args.input - else: - print(f"No input directory given") - sys.exit(1) - - if args.verbose: - print(f"Input directory: {source_directory}") - print(f"Output directory: {temp_directory}") - - try: - for root, dirs, files in os.walk(source_directory): - - # Grab the Common shader file, if it exists - common_file_path = os.path.join(root, 'Common.frag') - common_file_contents = "" - - if os.path.exists(common_file_path): - with open(common_file_path, 'r') as f: - common_file_contents = f.read() - - # 1. Remove any existing #version directive to avoid conflicts - common_file_contents = re.sub(r'^\s*#version\s+.*?\n', '', common_file_contents, flags=re.MULTILINE) - - # 2. Remove any pre-existing main() function - common_file_contents = re.sub(r'void\s+main\s*\([^)]*\)\s*\{[\s\S]*?\}', '', common_file_contents) - - # 3. Prepend 'ubuf.' to all shadertoy uniforms - for var in variables_to_update: - pattern = r'(?