File: Open3DAddEncodedShader.cmake

package info (click to toggle)
open3d 0.16.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,688 kB
  • sloc: cpp: 193,088; python: 24,973; ansic: 8,356; javascript: 1,869; sh: 1,473; makefile: 236; xml: 69
file content (79 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# open3d_add_encoded_shader(<target>
#    OUTPUT_HEADER <header>
#    SOURCES <shader1> [<shader2>...]
# )
#
# Encodes the given set of shaders into a set of strings and stores them into <header>.
# The resulting header can be used in C++ code to pass the encoded shaders to the shader compiler.
#
# Use add_dependencies(<other_target> <target>) to build the header before <other_target> is built.
# Furthermore, make sure that <header> can be properly included in the C++ code.
function(open3d_add_encoded_shader target)
    cmake_parse_arguments(PARSE_ARGV 1 ARG "" "OUTPUT_HEADER" "SOURCES")

    # Check correct usage
    if (ARG_UNPARSED_ARGUMENTS)
        message(FATAL_ERROR "Unknown arguments: ${ARG_UNPARSED_ARGUMENTS}")
    endif()

    if (ARG_KEYWORDS_MISSING_VALUES)
        message(FATAL_ERROR "Missing values for arguments: ${ARG_KEYWORDS_MISSING_VALUES}")
    endif()

    if (NOT ARG_OUTPUT_HEADER)
        message(FATAL_ERROR "No output header file specified.")
    endif()

    if (NOT ARG_SOURCES)
        message(FATAL_ERROR "No shaders specified to generate the output header.")
    endif()

    # Build encoded shaders
    foreach (shader IN LISTS ARG_SOURCES)
        get_filename_component(SHADER_FULL_PATH "${shader}" ABSOLUTE)

        get_filename_component(SHADER_NAME "${shader}" NAME_WE)
        set(ENCODED_SHADER_FULL_PATH "${CMAKE_CURRENT_BINARY_DIR}/${SHADER_NAME}.h")

        file(RELATIVE_PATH ENCODED_SHADER_RELATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}" "${ENCODED_SHADER_FULL_PATH}")

        add_custom_command(
            OUTPUT ${ENCODED_SHADER_FULL_PATH}
            COMMAND ShaderEncoder ${ENCODED_SHADER_FULL_PATH} ${SHADER_FULL_PATH}
            COMMENT "Building Encoded Shader object ${ENCODED_SHADER_RELATIVE_PATH}"
            MAIN_DEPENDENCY ${shader} DEPENDS ShaderEncoder
            VERBATIM
        )

        list(APPEND ENCODED_SHADERS "${ENCODED_SHADER_FULL_PATH}")
    endforeach()

    # Link encoded shaders
    get_filename_component(OUTPUT_FULL_PATH "${ARG_OUTPUT_HEADER}" ABSOLUTE)
    file(RELATIVE_PATH OUTPUT_RELATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}" "${OUTPUT_FULL_PATH}")

    add_custom_command(
        OUTPUT ${OUTPUT_FULL_PATH}
        COMMAND ShaderLinker ${OUTPUT_FULL_PATH} ${ENCODED_SHADERS}
        COMMENT "Linking Encoded Shader header ${OUTPUT_RELATIVE_PATH}"
        DEPENDS ${ENCODED_SHADERS} ShaderLinker
        VERBATIM
    )

    add_custom_target(${target} ALL
        DEPENDS "${OUTPUT_FULL_PATH}"
    )
endfunction()

# Helper target for open3d_add_encoded_shader
if (NOT TARGET ShaderEncoder)
    add_executable(ShaderEncoder EXCLUDE_FROM_ALL)
    target_sources(ShaderEncoder PRIVATE ${CMAKE_CURRENT_LIST_DIR}/ShaderEncoder.cpp)
    target_compile_features(ShaderEncoder PRIVATE cxx_std_14)
endif()

if (NOT TARGET ShaderLinker)
    add_executable(ShaderLinker EXCLUDE_FROM_ALL)
    target_sources(ShaderLinker PRIVATE ${CMAKE_CURRENT_LIST_DIR}/ShaderLinker.cpp)
    target_compile_features(ShaderLinker PRIVATE cxx_std_14)
endif()