From 737dcdbab2946117b1446608087eb7ea4fa3d977 Mon Sep 17 00:00:00 2001 From: Digital Artifex <7929434+DigitalArtifex@users.noreply.github.com> Date: Mon, 11 Aug 2025 00:04:56 -0400 Subject: [PATCH] Updated compiler and processor to respect directories --- tools/ShaderCompiler.py | 15 +++++++++--- tools/ShaderProcessor.py | 50 ++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/tools/ShaderCompiler.py b/tools/ShaderCompiler.py index 941f8b3..c8eac00 100644 --- a/tools/ShaderCompiler.py +++ b/tools/ShaderCompiler.py @@ -1,3 +1,6 @@ +# This file was originally part of the KDE Shader Wallpaper Project +# it contains modifications from Neil Panda and myself + import os import subprocess @@ -20,12 +23,18 @@ for root, dirs, files in os.walk(source_directory): if file.endswith('.frag'): # Construct the full path to the source file source_file_path = os.path.join(root, file) - # Construct the output file path + + # Construct new output path + relative_path = os.path.relpath(root, source_directory) + new_root = os.path.join(output_directory, relative_path) + os.makedirs(new_root, exist_ok=True) + output_file_name = file.replace('.frag', '.frag.qsb') - output_file_path = os.path.join(output_directory, output_file_name) + output_file_path = os.path.join(new_root, output_file_name) + # Construct and execute the command cmd = [ - 'qsb', '--glsl', '100 es,120,150', '--hlsl', '50', '--msl', '12', + '/usr/lib/qt6/bin/qsb', '--glsl', '330', '--hlsl', '50', '--msl', '12', '-o', output_file_path, source_file_path ] diff --git a/tools/ShaderProcessor.py b/tools/ShaderProcessor.py index 2285fb5..b3b7149 100644 --- a/tools/ShaderProcessor.py +++ b/tools/ShaderProcessor.py @@ -1,9 +1,12 @@ +# This file was originally part of the KDE Shader Wallpaper Project +# it contains modifications from Neil Panda and myself + import os import re import shutil # Specify the directory where your .frag files are located -directory_path = 'src' +source_directory = 'src' output_directory = 'processed' # List of variables to update @@ -13,9 +16,8 @@ variables_to_update = [ r'iChannelTime', r'iChannelResolution' ] -# Header and footer to append -header = ''' -#version 450 +# Header to be prepended to the shader file +header = '''#version 450 layout(location = 0) in vec2 qt_TexCoord0; layout(location = 0) out vec4 fragColor; @@ -43,8 +45,8 @@ layout(binding = 4) uniform sampler2D iChannel3; vec2 fragCoord = vec2(qt_TexCoord0.x, 1.0 - qt_TexCoord0.y) * ubuf.iResolution.xy; ''' +# Footer to be appended, containing the main entry point footer = ''' - void main() { vec4 color = vec4(0.0); mainImage(color, fragCoord); @@ -52,7 +54,7 @@ void main() { } ''' -for root, dirs, files in os.walk(directory_path): +for root, dirs, files in os.walk(source_directory): for file in files: if file.endswith('.frag'): file_path = os.path.join(root, file) @@ -60,23 +62,27 @@ for root, dirs, files in os.walk(directory_path): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() - # Replace variable names - for var in variables_to_update: - content = re.sub(r'\b' + re.escape(var) + r'\b', 'ubuf.' + var, content) + # 1. Remove any existing #version directive to avoid conflicts + content = re.sub(r'^\s*#version\s+.*?\n', '', content, flags=re.MULTILINE) - # Insert header after first empty line - insert_index = content.find('\n\n') + 1 - content = content[:insert_index] + header + content[insert_index:] + # 2. Remove any pre-existing main() function + content = re.sub(r'void\s+main\s*\([^)]*\)\s*\{[\s\S]*?\}', '', content) - # Append footer - content += footer + # 3. Prepend 'ubuf.' to all shadertoy uniforms + for var in variables_to_update: + pattern = r'(?