Source browser

cmake/shdr.cmake

Read the complete file below or download its original bytes. Line numbers provide stable links to a declaration or processing step.

File fingerprint
1# Compile native GLSL .cpp sources into C++11 adapters with embedded GPU source.
2include(CMakeParseArguments) # Load CMake's keyword-argument parser for the shader source list.
3find_package(Python3 3.8 COMPONENTS Interpreter REQUIRED) # Locate Python 3.8 or newer to run the native shader adapter generator.
4if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/../scripts/compile_shader.py") # Select the source-tree adapter generator when it is available.
5    set(_SHDR_COMPILE_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/../scripts/compile_shader.py") # Use the frontend script shipped in the source tree.
6else() # Select the frontend location used by installed packages.
7    set(_SHDR_COMPILE_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/compile_shader.py") # Find the installed frontend beside this CMake helper.
8endif() # Finish choosing the frontend location.
9
10# shdr_target_shaders(app PRIVATE SOURCES shader.cpp other.cpp)
11# Attach adapters directly to an executable or a loaded shared-library/plugin target.
12# Final targets retain the shader registration objects used by make_kernel.
13function(shdr_target_shaders target visibility) # Attach each native source and generated adapter to the requested final target.
14    if(NOT TARGET ${target}) # Validate that the destination target has already been declared.
15        message(FATAL_ERROR "shdr_target_shaders: target ${target} does not exist") # Explain that the application target must exist before attaching shaders.
16    endif() # Finish validating the destination target.
17    cmake_parse_arguments(SHDR "" "" "SOURCES" ${ARGN}) # Read the explicitly supplied native shader source list.
18    if(SHDR_UNPARSED_ARGUMENTS OR NOT SHDR_SOURCES) # Require a nonempty SOURCES list with recognized helper arguments.
19        message(FATAL_ERROR "usage: shdr_target_shaders(target PRIVATE SOURCES shader.cpp ...)") # Show the expected source-list syntax for an invalid call.
20    endif() # Finish validating the shader source arguments.
21    foreach(shader IN LISTS SHDR_SOURCES) # Attach one generated adapter for each supplied native shader.
22        get_filename_component(shader_path "${shader}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") # Resolve this shader path or derive its factory basename.
23        get_filename_component(shader_name "${shader}" NAME_WE) # Resolve this shader path or derive its factory basename.
24        get_target_property(registered_names ${target} SHDR_SHADER_NAMES) # Read the shader basenames already attached to this target.
25        if(shader_name IN_LIST registered_names) # Reject factory-name collisions from repeated shader basenames.
26            message(FATAL_ERROR "shdr_target_shaders: duplicate shader basename ${shader_name}") # Identify the repeated basename that would register the same factory twice.
27        endif() # Finish checking this target for a duplicate shader name.
28        set_property(TARGET ${target} APPEND PROPERTY SHDR_SHADER_NAMES "${shader_name}") # Remember this target metadata for duplicate detection.
29        set(generated "${CMAKE_CURRENT_BINARY_DIR}/shdr-generated/${target}/${shader_name}.cpp") # Choose a separate generated C++ file for this target and shader basename.
30        add_custom_command(OUTPUT "${generated}" # Generate a C++11 adapter whenever its shader or compiler changes.
31            COMMAND ${Python3_EXECUTABLE} "${_SHDR_COMPILE_SCRIPT}" # Run this tool with the arguments continued below.
32                --input "${shader_path}" --output "${generated}" --name "${shader_name}_kernel" # Supply this explicit tool option.
33            DEPENDS "${shader_path}" "${_SHDR_COMPILE_SCRIPT}" # Regenerate when one of these source dependencies changes.
34            COMMENT "Compiling and embedding shader ${shader_name}.cpp" # Describe the generated shader step in the build output.
35            VERBATIM) # Preserve each command argument exactly when invoking the tool.
36        # The native shader is shown in the IDE, GCC compiles its generated C++11 adapter.
37        set_source_files_properties("${shader_path}" PROPERTIES HEADER_FILE_ONLY TRUE) # Show the native shader in the IDE while compiling its generated adapter.
38        target_sources(${target} ${visibility} "${generated}" "${shader_path}") # Attach the generated adapter and its native shader to the application target.
39    endforeach() # Finish attaching every requested shader.
40endfunction() # Finish defining the native shader attachment helper.