Commit 92ccc201 authored by Maciej Sumiński's avatar Maciej Sumiński

Headers containing shader program sources are generated using CMake.

parent bbc0e8eb
# 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 */" )
...@@ -11,17 +11,20 @@ include_directories( ...@@ -11,17 +11,20 @@ include_directories(
${INC_AFTER} ${INC_AFTER}
) )
# Generate files containing shader programs # Generate header files containing shader programs
add_custom_command ( # Order of input files is significant
add_custom_command(
OUTPUT gal/opengl/shader_src.h 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 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/common/gal/opengl
COMMAND ${SHELL} COMMENT "Generating headers containing GLSL source code"
ARGS ${PROJECT_SOURCE_DIR}/common/gal/opengl/make_shader_src_h.sh
) )
add_custom_target ( add_custom_target(
ShaderHeader ALL shader_headers ALL
DEPENDS gal/opengl/shader_src.h DEPENDS gal/opengl/shader_src.h
) )
...@@ -40,7 +43,7 @@ set(GAL_SRCS ...@@ -40,7 +43,7 @@ set(GAL_SRCS
) )
add_library(gal STATIC ${GAL_SRCS}) add_library(gal STATIC ${GAL_SRCS})
add_dependencies(gal ShaderHeader) add_dependencies(gal shader_headers)
if(WIN32) if(WIN32)
add_definitions(-DGLEW_STATIC) add_definitions(-DGLEW_STATIC)
......
#!/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."
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment