Skip to content

Commit

Permalink
Fix scripts for shader generation
Browse files Browse the repository at this point in the history
  • Loading branch information
W2Wizard committed Oct 8, 2024
1 parent 7a0d6f8 commit 6f9878f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 46 deletions.
41 changes: 22 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ set(BUILD_TESTS OFF CACHE BOOL "Build the tests to verify the integrity of the l
# Reduce the size of LodePNG, we don't need these things.
add_definitions(-D LODEPNG_NO_COMPILE_ENCODER)
add_definitions(-D LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS)
if(EMSCRIPTEN)
add_definitions(-D EMSCRIPTEN)
endif()

if(UNIX AND NOT EMSCRIPTEN)
set(CCSHADER ${PROJECT_SOURCE_DIR}/tools/compile_shader.sh)
add_compile_options(
-Wextra
-Wall
Expand All @@ -61,31 +57,38 @@ if(UNIX AND NOT EMSCRIPTEN)
endif(DEBUG)
else()
# TODO: Figure out what we need for windows.
set(CCSHADER ${PROJECT_SOURCE_DIR}/tools/compile_shader.bat)
endif()

# Build specific files
# @see https://cmake.org/cmake/help/latest/command/add_custom_command.html
# -----------------------------------------------------------------------------

if (UNIX)
set(CCSHADER ${TOOLS_DIR}/compile_shader.sh)
else()
set(CCSHADER ${TOOLS_DIR}/compile_shader.bat)
endif()

# Add custom command for fragment shader
add_custom_command(
COMMENT "Building fragment shader"
DEPENDS ${PROJECT_SOURCE_DIR}/shaders/default.frag
OUTPUT mlx_frag_shader.c
COMMAND ${CCSHADER} ${PROJECT_SOURCE_DIR}/shaders/default.frag > mlx_frag_shader.c
VERBATIM
PRE_BUILD
USES_TERMINAL
COMMENT "Building fragment shader"
DEPENDS ${PROJECT_SOURCE_DIR}/shaders/default.frag
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mlx_frag_shader.c
COMMAND ${CCSHADER} ${PROJECT_SOURCE_DIR}/shaders/default.frag ${EMSCRIPTEN} > ${CMAKE_CURRENT_BINARY_DIR}/mlx_frag_shader.c
VERBATIM
PRE_BUILD
USES_TERMINAL
)

# Add custom command for vertex shader
add_custom_command(
COMMENT "Building vertex shader"
DEPENDS ${PROJECT_SOURCE_DIR}/shaders/default.vert
OUTPUT mlx_vert_shader.c
COMMAND ${CCSHADER} ${PROJECT_SOURCE_DIR}/shaders/default.vert > mlx_vert_shader.c
VERBATIM
PRE_BUILD
USES_TERMINAL
COMMENT "Building vertex shader"
DEPENDS ${PROJECT_SOURCE_DIR}/shaders/default.vert
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mlx_vert_shader.c
COMMAND ${CCSHADER} ${PROJECT_SOURCE_DIR}/shaders/default.vert ${EMSCRIPTEN} > ${CMAKE_CURRENT_BINARY_DIR}/mlx_vert_shader.c
VERBATIM
PRE_BUILD
USES_TERMINAL
)

# Sources
Expand Down
10 changes: 8 additions & 2 deletions src/mlx_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,19 @@ bool mlx_loop_hook(mlx_t* mlx, void (*f)(void*), void* param)
}

// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
/**
* In Emscripten the lood is defined differently, there the this function
* is passed to the while loop instead
*/
void mlx_loop(mlx_t* mlx)
{
MLX_NONNULL(mlx);

#ifdef EMSCRIPTEN
static double start, oldstart = 0;
#else
double start, oldstart = 0;
#ifndef EMSCRIPTEN
while (!glfwWindowShouldClose(mlx->window))
while (!glfwWindowShouldClose(mlx->window))
{
#endif
start = glfwGetTime();
Expand Down
41 changes: 31 additions & 10 deletions tools/compile_shader.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
:: -----------------------------------------------------------------------------

@echo off
SETLOCAL DisableDelayedExpansion
SETLOCAL EnableDelayedExpansion

:: go to usage function if no arguments have been given to the script
:: Go to usage function if no arguments have been given to the script
IF [%1]==[] GOTO usage

:: check if input file exists before continuing
:: Check if input file exists before continuing
IF NOT EXIST %1 GOTO fnotfound

SET SHADERTYPE=%~x1
SET SHADERTYPE=%SHADERTYPE:~1%

echo // -----------------------------------------------------------------------------
echo // Codam Coding College, Amsterdam @ <2022-2023> by W2Wizard.
echo // Codam Coding College, Amsterdam @ 2022-2023 by W2Wizard.
echo // See README in the root project for more information.
echo // -----------------------------------------------------------------------------
echo.
Expand All @@ -25,18 +25,39 @@ echo.
echo #include "MLX42/MLX42_Int.h"
echo.

FOR /F "delims=" %%A IN (%1) DO IF NOT DEFINED VERSIONLINE set "VERSIONLINE=%%A"
echo const char* %SHADERTYPE%_shader = "%VERSIONLINE%\n"
FOR /F "skip=1 delims=" %%A IN (%1) DO (
IF "%%A" == "}" (echo "%%A";) ELSE (echo "%%A")
REM Read the shader version line
SET VERSIONLINE=
FOR /F "delims=" %%A IN (%1) DO (
IF NOT DEFINED VERSIONLINE (
SET VERSIONLINE=%%A
SET "FIRSTLINE=1"
) ELSE (
IF !FIRSTLINE! NEQ 1 (
IF "%%A" == "}" (
echo "%%A";
) ELSE (
echo "%%A"
)
)
)
)

REM Output the shader declaration
IF "%2"=="1" (
echo const char* %SHADERTYPE%_shader = "#version 300 es\n"
IF /I "%SHADERTYPE%"=="frag" (
echo "precision mediump float;\n"
)
) ELSE (
echo const char* %SHADERTYPE%_shader = "%VERSIONLINE%\n"
)

ENDLOCAL
EXIT /B 0

:: usage function exits the script with exit code 3 (path not found)
:: usage function exits the script with exit code 3 (missing arguments)
:usage
echo ERROR: missing arguments, use as follows: %0 ^<ShaderFile^> 1>&2
echo ERROR: missing arguments, use as follows: %0 ^<ShaderFile^> ^<Mode^> 1>&2
ENDLOCAL
EXIT /B 3

Expand Down
38 changes: 23 additions & 15 deletions tools/compile_shader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,46 @@
# See README in the root project for more information.
# -----------------------------------------------------------------------------

input_file=$1
output_file=$(basename "$input_file" .frag).c
echo "// Generated shader source from $input_file" > $output_file
echo "const char *shader_source = R\"GLSL(" >> $output_file
cat $input_file >> $output_file
echo ")GLSL\";" >> $output_file

# If no arguments have been given, exit with error code 1
if [ "$#" -ne 1 ]; then
echo "ERROR: missing arguments, use as follows: $0 <ShaderFile>" 1>&2
exit 1
if [ "$#" -ne 2 ]; then
echo "ERROR: missing arguments, use as follows: $0 <ShaderFile> <Mode>" 1>&2
exit 1
fi

# If file cannot be found, exit with error code 2
if [ ! -f "$1" ]; then
echo "ERROR: shader file not found: $1" 1>&2
exit 2
echo "ERROR: shader file not found: $1" 1>&2
exit 2
fi

SHADERTYPE="${1##*.}"

echo "// -----------------------------------------------------------------------------"
echo "// Codam Coding College, Amsterdam @ 2022-2023 by W2Wizard. "
echo "// Codam Coding College, Amsterdam @ 2022-2023 by W2Wizard. "
echo "// See README in the root project for more information. "
echo "// -----------------------------------------------------------------------------"
echo ""
echo "// If you wish to modify this file edit the .vert or .frag file!"
echo ""

# Include the MLX42 header
echo "#include \"MLX42/MLX42_Int.h\""
echo ""
echo "const char* ${SHADERTYPE}_shader = \"$(sed -n '1{p;q;}' "$1")\\n\""

{
# Skip over first line
# Start building the shader string
if [ "$2" -eq 1 ]; then
# Output WASM specific lines
echo "const char* ${SHADERTYPE}_shader = \"#version 300 es\\n\""
if [ "$SHADERTYPE" = "frag" ]; then
echo " \"precision mediump float;\""
fi
else
# Non-Wasm, output the original shader version
echo "const char* ${SHADERTYPE}_shader = \"$(sed -n '1{p;q;}' "$1")\\n\""
fi

# Read the rest of the shader file
read
while IFS= read -r LINE; do
if [ ! "${LINE}" = "" ]; then
Expand All @@ -48,4 +55,5 @@ echo "const char* ${SHADERTYPE}_shader = \"$(sed -n '1{p;q;}' "$1")\\n\""
fi
done
} < "$1"

exit 0

0 comments on commit 6f9878f

Please sign in to comment.