This small module provides a convenience function for integrating shader compilation with glslc
into the CMake build process.
# Automatically download cmake-spirv with FetchContent
include(FetchContent)
FetchContent_Declare(cmake-spirv
GIT_REPOSITORY https://github.com/liliolett/cmake-spirv.git
GIT_TAG origin/v1
)
FetchContent_MakeAvailable(cmake-spirv)
# Add cmake-spirv to module path
list(APPEND CMAKE_MODULE_PATH ${cmake-spirv_SOURCE_DIR}/include)
# Include required modules
include(AddSpirvModules)
# Create shader target
add_spirv_modules(shaders
SOURCE_DIR res/shaders
BINARY_DIR res/shaders
SOURCES my_shader.vert my_shader.frag)
# Make sure shaders are compiled by specifying dependencies
add_dependencies(my_executable shaders)
cmake-spirv
uses a single version number that will be incremented with breaking API changes.
Versions are defined by git branches.
The latest version is v1.
add_spirv_modules(<target_name> [<option>...])
Adds custom commands to compile a number of shader source files to SPIR-V using glslc
and bundles them into a custom target called <target_name>
.
The commands are defined with the flags VERBATIM
and COMMAND_EXPAND_LISTS
enabled.
- Options
SOURCE_DIR <dir>
-
All source files will be looked up relative to this path. If the given value is not an absolute path it will be prefixed with
${CMAKE_CURRENT_SOURCE_DIR}
.
Default value:${CMAKE_CURRENT_SOURCE_DIR}
BINARY_DIR <dir>
-
All output files will be generated relative to this path. If the given value is not an absolute path it will be prefixed with
${CMAKE_CURRENT_BINARY_DIR}
.
Default value:${CMAKE_CURRENT_BINARY_DIR}
SOURCES <fname1> [<fname2>...]
-
The shader modules to be compiled, relative to
SOURCE_DIR
. For eachSOURCE
in this list, the resulting SPIR-V file will be generated at${BINARY_DIR}
/${SOURCE}.spv
OPTIONS <option1> [<option2>...]
-
Additional command line options for
glslc
, passed on verbatim. - See also