Headers containing shader program sources are generated using CMake.

This commit is contained in:
Maciej Sumiński 2013-07-17 23:55:59 +02:00
parent bbc0e8eb74
commit 92ccc2018e
3 changed files with 55 additions and 61 deletions

View File

@ -0,0 +1,44 @@
# CMake script file to process a GLSL source file, so it can be included
# in C array and compiled in to an application.
# number of input files
list( LENGTH inputFiles shadersNumber )
# write header
file( WRITE ${outputFile} "// Do not edit this file, it is autogenerated by CMake.
#ifndef SHADER_SRC_H
#define SHADER_SRC_H
const unsigned int shaders_number = ${shadersNumber};
const char *shaders_src[] = {\n" )
foreach( inputFile ${inputFiles} )
# put the input file name into the output file
file( APPEND ${outputFile} "\n// ${inputFile}" )
# process the input file
file( READ ${inputFile} contents )
# remove /* */ comments
string( REGEX REPLACE "/\\*.*\\*/" "" contents "${contents}" )
# remove // comments
string( REGEX REPLACE "//[^\n]*" "" contents "${contents}" )
# remove whitespaces at the beginning of each line
string( REGEX REPLACE "\n([\t ])*" "\n" contents "${contents}" )
# remove unnecessary spaces
string( REGEX REPLACE " *([\\*/+&\\|,=<>\(\)]) *" "\\1" contents "${contents}" )
# remove empty lines & wrap every line in "" and add '\n' at the end of each line
string( REGEX REPLACE "\n+" "\\\\n\"\n\"" contents "${contents}" )
# remove unnecessary " & \n from the beginning and the end of contents
string( REGEX REPLACE "^\\\\n\"" "" contents "${contents}" )
string( REGEX REPLACE "\"$" "," contents "${contents}" )
file( APPEND ${outputFile} "${contents}" )
endforeach( inputFile ${inputFiles} )
# write footer
file( APPEND ${outputFile} "};
#endif /* SHADER_SRC_H */" )

View File

@ -11,17 +11,20 @@ include_directories(
${INC_AFTER}
)
# Generate files containing shader programs
add_custom_command (
# Generate header files containing shader programs
# Order of input files is significant
add_custom_command(
OUTPUT gal/opengl/shader_src.h
DEPENDS gal/opengl/make_shader_src_h.sh
COMMAND ${CMAKE_COMMAND}
-DinputFiles="${PROJECT_SOURCE_DIR}/common/gal/opengl/shader.vert\\;${PROJECT_SOURCE_DIR}/common/gal/opengl/shader.frag"
-DoutputFile="shader_src.h"
-P ${CMAKE_MODULE_PATH}/Shaders.cmake
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/common/gal/opengl
COMMAND ${SHELL}
ARGS ${PROJECT_SOURCE_DIR}/common/gal/opengl/make_shader_src_h.sh
COMMENT "Generating headers containing GLSL source code"
)
add_custom_target (
ShaderHeader ALL
add_custom_target(
shader_headers ALL
DEPENDS gal/opengl/shader_src.h
)
@ -40,7 +43,7 @@ set(GAL_SRCS
)
add_library(gal STATIC ${GAL_SRCS})
add_dependencies(gal ShaderHeader)
add_dependencies(gal shader_headers)
if(WIN32)
add_definitions(-DGLEW_STATIC)

View File

@ -1,53 +0,0 @@
#!/bin/bash
# Make a header file containing GLSL source code
echo "Generating headers containing GLSL source code.."
# Source files to be included
SHADER_SRC=( "shader.vert" "shader.frag" )
# Number of shaders
SHADERS_NUMBER=${#SHADER_SRC[@]}
OUTPUT="shader_src.h"
UPDATE=false
# Check if it is necessary to regenerate headers
for filename in "${SHADER_SRC[@]}"
do
if [[ $filename -nt $OUTPUT ]]; then
UPDATE=true
fi
done
if [[ $UPDATE == false ]]; then
echo "Headers are up-to-date."
exit
fi
# Prepare GLSL source to be included in C array
function processSrc {
# 1st part: remove /* */ comments
# 2nd part: remove // comments
# 3rd part: remove blank lines (or containing only whitespaces)
# 4th & 5th part: wrap every line in quotation marks
sed '/\/\*/,/\*\//d; s/[ \t]*\/\/.*$//; /^[ \t]*$/d; s/^[ \t]*/"/; s/[ \t]*$/\\n"/' $1 >> $OUTPUT
echo "," >> $OUTPUT
}
# Header
echo "#ifndef SHADER_SRC_H
#define SHADER_SRC_H
const unsigned int shaders_number = $SHADERS_NUMBER;
const char *shaders_src[] = {" > $OUTPUT
# Main contents
for filename in "${SHADER_SRC[@]}"
do
processSrc $filename
done
# Footer
echo "};
#endif /* SHADER_SRC_H */" >> $OUTPUT
echo "Done."