Skip to content

Commit

Permalink
Replace preprocess_shaders.c with a shell script
Browse files Browse the repository at this point in the history
Turns out, if we use #version 300 es profile, we don't need to make so
many changes to the shaders at all. The ES is still the lowest common
denominator, so shaders need to take it into account, but the same
shaders will work just fine with core profile. The only change we need
to make is the version header and add float precision declaration for
the ES. This is easier done with sed from a shell script, which is also
easier to maintain. The downside is that whoever ports this to windows
will probably have to rewrite it in python.

Signed-off-by: Alexander Shishkin <[email protected]>
  • Loading branch information
virtuoso committed Oct 13, 2024
1 parent dc1e98e commit 6e983b2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 271 deletions.
5 changes: 0 additions & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ if [ -n "$1" ]; then
VERBOSE="--verbose"
fi

cd compile-time
cmake -B build/rel -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake --build build/rel
cd ..

cmake --build build/rel $VERBOSE $OPTS
cmake --build build/test $VERBOSE $OPTS
cmake --build build/debug $VERBOSE $OPTS
Expand Down
1 change: 0 additions & 1 deletion compile-time/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
project(CompileTimeTools)

add_executable (preprocess_shaders preprocess_shaders.c)
add_executable (ca3d ca3d.c)
263 changes: 0 additions & 263 deletions compile-time/preprocess_shaders.c

This file was deleted.

4 changes: 2 additions & 2 deletions demo/ldjam56/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif ()

set(SHADER_DIR "${ASSET_DIR}/${SHADER_TYPE}")
set(SHADER_SOURCE_DIR "${PARENT_DIR}/clap/shaders")
set(SHADER_PREPROCESSOR "${PARENT_DIR}/clap/compile-time/build/rel/preprocess_shaders")
set(SHADER_PREPROCESSOR "${PARENT_DIR}/clap/scripts/preprocess_shaders.sh")
set(SHADERS "ui"
"glyph"
"model"
Expand All @@ -40,7 +40,7 @@ FOREACH(s ${SHADERS})
LIST(APPEND SHADER_OUTS "${SHADER_DIR}/${s}.vert" "${SHADER_DIR}/${s}.frag")
add_custom_command(
OUTPUT "${SHADER_DIR}/${s}.vert" "${SHADER_DIR}/${s}.frag"
DEPENDS make-shader-output-dir "${SHADER_SOURCE_DIR}/${s}.vert" "${SHADER_SOURCE_DIR}/${s}.frag"
DEPENDS make-shader-output-dir "${SHADER_PREPROCESSOR}" "${SHADER_SOURCE_DIR}/${s}.vert" "${SHADER_SOURCE_DIR}/${s}.frag"
COMMAND "${SHADER_PREPROCESSOR}"
ARGS -t ${SHADER_TYPE} -o ${SHADER_DIR}/ ${SHADER_SOURCE_DIR}/${s}
)
Expand Down
62 changes: 62 additions & 0 deletions scripts/preprocess_shaders.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/sh -e

usage() {
echo "Need -t <type> -o <outdir> <src>"
}

mk_glsl() {
local src="$1"
local dest="$2"

cp "$src" "$dest"
}

mk_glsl_es() {
local src="$1"
local dest="$2"

sed -e 's,#version.*,#version 300 es\nprecision highp float;\n,' \
< "$src" > "$dest"
}

if test -z "$1"; then
usage()
exit 1
fi

while [ -n "$1" ]; do
case "$1" in
-t)
shift
type="$1"
;;
-o)
shift
outdir="$1"
;;
*)
src="$1"
;;
esac
shift
done

if test -z "$type" -o -z "$outdir" -o -z "$src"; then
usage()
exit 1
fi

shader=$(basename "$src")
case "$type" in
glsl)
mk_glsl "$src.vert" "$outdir/$shader.vert"
mk_glsl "$src.frag" "$outdir/$shader.frag"
;;
glsl-es)
mk_glsl_es "$src.vert" "$outdir/$shader.vert"
mk_glsl_es "$src.frag" "$outdir/$shader.frag"
;;
*)
exit 1
;;
esac

0 comments on commit 6e983b2

Please sign in to comment.