From 1e3886b872c286e385013a8878e34c09f6da23fd Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Thu, 5 Apr 2012 16:27:14 +0200 Subject: [PATCH] add a shader-preprocessor (like cpp) --- scripts/ex2x.shader | 2 +- scripts/shader-audit.sh | 2 +- scripts/shader-cpp.sh | 164 +++++++++++++++++++++++++++++++++++++ scripts/shader-parser.subr | 6 +- 4 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 scripts/shader-cpp.sh diff --git a/scripts/ex2x.shader b/scripts/ex2x.shader index 0069b1a0..ecd6b0d8 100644 --- a/scripts/ex2x.shader +++ b/scripts/ex2x.shader @@ -1,4 +1,4 @@ -textures/ex2x/base-yellowpaint +textures/exx/base-yellowpaint { qer_editorimage textures/ex2x/base/base_yellowpaint.tga diff --git a/scripts/shader-audit.sh b/scripts/shader-audit.sh index 196d5703..c583f0ee 100755 --- a/scripts/shader-audit.sh +++ b/scripts/shader-audit.sh @@ -483,7 +483,7 @@ parse_shaderfile_pre() esac } -parse_shaders +parse_shaders *.shader textures_avail=`( cd ..; find textures/ -type f -not -name '*.sh' -not -name '*_norm.*' -not -name '*_glow.*' -not -name '*_gloss.*' -not -name '*_reflect.*' -not -name '*.xcf' -not -name '*.txt' ) | while IFS= read -r T; do shader_normalize "$T"; done | sort -u` textures_used=`echo "${textures_used#$LF}" | sort -u` diff --git a/scripts/shader-cpp.sh b/scripts/shader-cpp.sh new file mode 100644 index 00000000..202987ad --- /dev/null +++ b/scripts/shader-cpp.sh @@ -0,0 +1,164 @@ +#!/bin/sh + +case "$0" in + */*) + cd "${0%/*}" + ;; +esac + +. ./shader-parser.subr + +LF=" +" +TAB=" " + +parse_shader_pre() +{ + shadertext= + shaderconds= +} +parse_conditional() +{ + shaderconds="$shaderconds$LF$*" +} +parse_shader_line() +{ + case "$1" in + '#if') + parse_conditional $2 $3 + ;; + esac + shadertext="$shadertext$LF$TAB$*" +} +parse_shaderstage_pre() +{ + shadertext="$shadertext$LF$TAB{" +} +parse_shaderstage_line() +{ + case "$1" in + '#if') + parse_conditional $2 $3 + ;; + esac + shadertext="$shadertext$LF$TAB$TAB$*" +} +parse_shaderstage_post() +{ + shadertext="$shadertext$LF$TAB}" +} +# note: the conds are what is FALSE, not TRUE +shaderkill() +{ + parseALLtheconds=$conds + while :; do + case "$parseALLtheconds" in + '') + break + ;; + *$LF*) + thiscond=${parseALLtheconds%%$LF*} + parseALLtheconds=${parseALLtheconds#*$LF} + ;; + *) + thiscond=$parseALLtheconds + parseALLtheconds= + ;; + esac + [ -n "$thiscond" ] || continue + echo "$TAB""dpshaderkillifcvar $thiscond" + done +} +preprocess() +{ + condstack= + echo "$shadertext" | while IFS= read -r L; do + [ -n "$L" ] || continue + set -- $L + k=$1 + shift + case "$k" in + '#if') + case "$LF$conds$LF" in + *"$LF$*$LF"*) + condstack=0$condstack + ;; + *) + condstack=1$condstack + ;; + esac + ;; + '#else') + case "$condstack" in + 0) + condstack=1${condstack#0} + ;; + 1) + condstack=0${condstack#1} + ;; + esac + ;; + '#endif') + condstack=${condstack#?} + ;; + *) + case "$condstack" in + *0*) + ;; + *) + echo "$L" + ;; + esac + ;; + esac + done +} +conditionalize() +{ + case "$1" in + '') + # no conditions + echo "$parsing_shader" + echo "{" + shaderkill + preprocess + echo "}" + return + ;; + *$LF*) + # many conditions + firstcond="${1%%$LF*}" + restcond="${1#*$LF}" + ;; + *) + firstcond=$1 + restcond= + ;; + esac + ( + conds="$conds$LF$firstcond" + conditionalize "$restcond" + ) + ( + set -- $firstcond + case "$2" in + '==') op='!=' ;; + '!=') op='==' ;; + '>') op='<=' ;; + '<') op='>=' ;; + '>=') op='<' ;; + '<=') op='>' ;; + esac + set -- "$1" "$op" "$3" + conds="$conds$LF$*" + conditionalize "$restcond" + ) +} +parse_shader_post() +{ + allconds=`echo "$shaderconds" | sort -u | grep .` + conds= + conditionalize "$allconds" +} + +parse_shaders "$@" diff --git a/scripts/shader-parser.subr b/scripts/shader-parser.subr index ea964572..ef82832f 100644 --- a/scripts/shader-parser.subr +++ b/scripts/shader-parser.subr @@ -30,6 +30,8 @@ parse_shaderstage() '}') break ;; + '') + ;; *) parse_shaderstage_line "$shaderparser_L" "$shaderparser_A1" "$shaderparser_Aother" ;; @@ -61,6 +63,8 @@ parse_shader() '}') break ;; + '') + ;; *) parse_shader_line "$shaderparser_L" "$shaderparser_A1" "$shaderparser_Aother" ;; @@ -109,7 +113,7 @@ shaderparser_strip_comments() parse_shaders() { shaderparser_t=`mktemp || echo ".temp"` - for shaderparser_X in *.shader; do + for shaderparser_X in "$@"; do shaderparser_strip_comments < "$shaderparser_X" > "$shaderparser_t" parsing_shaderfile="$shaderparser_X" parse_shaderfile < "$shaderparser_t" -- 2.39.2