Updated compiler and processor to respect directories

This commit is contained in:
Digital Artifex
2025-08-11 00:04:56 -04:00
parent ebc5051588
commit 737dcdbab2
2 changed files with 40 additions and 25 deletions

View File

@@ -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 os
import subprocess import subprocess
@@ -20,12 +23,18 @@ for root, dirs, files in os.walk(source_directory):
if file.endswith('.frag'): if file.endswith('.frag'):
# Construct the full path to the source file # Construct the full path to the source file
source_file_path = os.path.join(root, 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_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 # Construct and execute the command
cmd = [ 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 '-o', output_file_path, source_file_path
] ]

View File

@@ -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 os
import re import re
import shutil import shutil
# Specify the directory where your .frag files are located # Specify the directory where your .frag files are located
directory_path = 'src' source_directory = 'src'
output_directory = 'processed' output_directory = 'processed'
# List of variables to update # List of variables to update
@@ -13,9 +16,8 @@ variables_to_update = [
r'iChannelTime', r'iChannelResolution' r'iChannelTime', r'iChannelResolution'
] ]
# Header and footer to append # Header to be prepended to the shader file
header = ''' header = '''#version 450
#version 450
layout(location = 0) in vec2 qt_TexCoord0; layout(location = 0) in vec2 qt_TexCoord0;
layout(location = 0) out vec4 fragColor; 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; vec2 fragCoord = vec2(qt_TexCoord0.x, 1.0 - qt_TexCoord0.y) * ubuf.iResolution.xy;
''' '''
# Footer to be appended, containing the main entry point
footer = ''' footer = '''
void main() { void main() {
vec4 color = vec4(0.0); vec4 color = vec4(0.0);
mainImage(color, fragCoord); 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: for file in files:
if file.endswith('.frag'): if file.endswith('.frag'):
file_path = os.path.join(root, file) 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: with open(file_path, 'r', encoding='utf-8') as f:
content = f.read() content = f.read()
# Replace variable names # 1. Remove any existing #version directive to avoid conflicts
content = re.sub(r'^\s*#version\s+.*?\n', '', content, flags=re.MULTILINE)
# 2. Remove any pre-existing main() function
content = re.sub(r'void\s+main\s*\([^)]*\)\s*\{[\s\S]*?\}', '', content)
# 3. Prepend 'ubuf.' to all shadertoy uniforms
for var in variables_to_update: for var in variables_to_update:
content = re.sub(r'\b' + re.escape(var) + r'\b', 'ubuf.' + var, content) pattern = r'(?<!\.)\b' + var + r'\b'
replacement = 'ubuf.' + var
content = re.sub(pattern, replacement, content)
# Insert header after first empty line # 4. Assemble the final, complete shader
insert_index = content.find('\n\n') + 1 final_content = header + '\n' + content.strip() + '\n' + footer
content = content[:insert_index] + header + content[insert_index:]
# Append footer
content += footer
# Construct new output path # Construct new output path
relative_path = os.path.relpath(root, directory_path) relative_path = os.path.relpath(root, source_directory)
new_root = os.path.join(output_directory, relative_path) new_root = os.path.join(output_directory, relative_path)
os.makedirs(new_root, exist_ok=True) os.makedirs(new_root, exist_ok=True)
new_file_path = os.path.join(new_root, file) new_file_path = os.path.join(new_root, file)
# Write to the new file # Write to the new file
with open(new_file_path, 'w', encoding='utf-8') as f: with open(new_file_path, 'w', encoding='utf-8') as f:
f.write(content) f.write(final_content)