add a shader-preprocessor (like cpp)
authorRudolf Polzer <divverent@xonotic.org>
Thu, 5 Apr 2012 14:27:14 +0000 (16:27 +0200)
committerRudolf Polzer <divverent@xonotic.org>
Thu, 5 Apr 2012 14:27:14 +0000 (16:27 +0200)
scripts/ex2x.shader
scripts/shader-audit.sh
scripts/shader-cpp.sh [new file with mode: 0644]
scripts/shader-parser.subr

index 0069b1a..ecd6b0d 100644 (file)
@@ -1,4 +1,4 @@
-textures/ex2x/base-yellowpaint
+textures/exx/base-yellowpaint
 {
        qer_editorimage textures/ex2x/base/base_yellowpaint.tga
        
 {
        qer_editorimage textures/ex2x/base/base_yellowpaint.tga
        
index 196d570..c583f0e 100755 (executable)
@@ -483,7 +483,7 @@ parse_shaderfile_pre()
        esac
 }
 
        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`
 
 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 (file)
index 0000000..202987a
--- /dev/null
@@ -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 "$@"
index ea96457..ef82832 100644 (file)
@@ -30,6 +30,8 @@ parse_shaderstage()
                        '}')
                                break
                                ;;
                        '}')
                                break
                                ;;
+                       '')
+                               ;;
                        *)
                                parse_shaderstage_line "$shaderparser_L" "$shaderparser_A1" "$shaderparser_Aother"
                                ;;
                        *)
                                parse_shaderstage_line "$shaderparser_L" "$shaderparser_A1" "$shaderparser_Aother"
                                ;;
@@ -61,6 +63,8 @@ parse_shader()
                        '}')
                                break
                                ;;
                        '}')
                                break
                                ;;
+                       '')
+                               ;;
                        *)
                                parse_shader_line "$shaderparser_L" "$shaderparser_A1" "$shaderparser_Aother"
                                ;;
                        *)
                                parse_shader_line "$shaderparser_L" "$shaderparser_A1" "$shaderparser_Aother"
                                ;;
@@ -109,7 +113,7 @@ shaderparser_strip_comments()
 parse_shaders()
 {
        shaderparser_t=`mktemp || echo ".temp"`
 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"
                shaderparser_strip_comments < "$shaderparser_X" > "$shaderparser_t"
                parsing_shaderfile="$shaderparser_X"
                parse_shaderfile < "$shaderparser_t"