2023-07-06 16:09:54 +08:00
|
|
|
cmake_policy(PUSH)
|
|
|
|
cmake_policy(SET CMP0057 NEW)
|
|
|
|
|
|
|
|
##############################################################
|
|
|
|
## enable shader lang by shader compiler: glslcc
|
2023-08-25 16:49:08 +08:00
|
|
|
find_program(GLSLCC_EXE NAMES glslcc
|
2023-08-25 16:57:38 +08:00
|
|
|
PATHS ${_AX_ROOT}/tools/external/glslcc
|
2023-08-25 16:49:08 +08:00
|
|
|
)
|
2023-08-25 16:46:32 +08:00
|
|
|
|
2023-08-25 16:49:08 +08:00
|
|
|
if (NOT GLSLCC_EXE)
|
2023-08-25 16:57:38 +08:00
|
|
|
message("glslcc not found.")
|
2023-08-25 16:49:08 +08:00
|
|
|
message(FATAL_ERROR "Please run setup.ps1 again to download glslcc, and run CMake again.")
|
|
|
|
endif()
|
|
|
|
|
2023-08-25 16:57:38 +08:00
|
|
|
macro(glslcc_option variable value)
|
|
|
|
if(NOT DEFINED ${variable})
|
|
|
|
set(${variable} ${value})
|
|
|
|
endif()
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
glslcc_option(GLSLCC_FRAG_SOURCE_FILE_EXTENSIONS .frag;.fsh)
|
|
|
|
glslcc_option(GLSLCC_VERT_SOURCE_FILE_EXTENSIONS .vert;.vsh)
|
|
|
|
glslcc_option(GLSLCC_OUT_DIR ${CMAKE_BINARY_DIR}/runtime/axslc)
|
|
|
|
glslcc_option(GLSLCC_OUT_SUFFIX "")
|
|
|
|
glslcc_option(GLSLCC_FLAT_UBOS TRUE)
|
|
|
|
|
2023-07-08 17:17:10 +08:00
|
|
|
message(STATUS "GLSLCC_FRAG_SOURCE_FILE_EXTENSIONS=${GLSLCC_FRAG_SOURCE_FILE_EXTENSIONS}")
|
|
|
|
message(STATUS "GLSLCC_VERT_SOURCE_FILE_EXTENSIONS=${GLSLCC_VERT_SOURCE_FILE_EXTENSIONS}")
|
2023-08-25 16:57:38 +08:00
|
|
|
message(STATUS "GLSLCC_OUT_SUFFIX=${GLSLCC_OUT_SUFFIX}")
|
|
|
|
message(STATUS "GLSLCC_FLAT_UBOS=${GLSLCC_FLAT_UBOS}")
|
2023-07-06 16:09:54 +08:00
|
|
|
|
|
|
|
# PROPERTY: include direcotries (optional)
|
2023-08-25 16:57:38 +08:00
|
|
|
define_property(SOURCE PROPERTY GLSLCC_INCLUDE_DIRS
|
2023-07-06 16:09:54 +08:00
|
|
|
BRIEF_DOCS "Compiled shader include directories"
|
|
|
|
FULL_DOCS "Compiled shader include directories, seperated with comma")
|
2023-07-06 21:15:28 +08:00
|
|
|
|
|
|
|
# Find shader sources in specified directory
|
|
|
|
# syntax: ax_find_shaders(dir shader_sources [RECURSE])
|
|
|
|
# examples:
|
|
|
|
# - ax_find_shaders("${CMAKE_CURRENT_LIST_DIR}/core/renderer/shaders" runtime_shader_sources)
|
|
|
|
# - ax_find_shaders("${CMAKE_CURRENT_LIST_DIR}/Source" custom_shader_sources RECURSE)
|
|
|
|
function (ax_find_shaders dir varName)
|
|
|
|
set(options RECURSE)
|
|
|
|
cmake_parse_arguments(opt "${options}" "" "" ${ARGN})
|
|
|
|
|
|
|
|
set(SC_FILTERS "")
|
|
|
|
foreach(fileext ${GLSLCC_FRAG_SOURCE_FILE_EXTENSIONS})
|
|
|
|
list(APPEND SC_FILTERS "${dir}/*${fileext}")
|
|
|
|
endforeach()
|
|
|
|
foreach(fileext ${GLSLCC_VERT_SOURCE_FILE_EXTENSIONS})
|
|
|
|
list(APPEND SC_FILTERS "${dir}/*${fileext}")
|
|
|
|
endforeach()
|
|
|
|
if (opt_RECURSE)
|
|
|
|
file(GLOB_RECURSE out_files ${SC_FILTERS})
|
|
|
|
else()
|
|
|
|
file(GLOB out_files ${SC_FILTERS})
|
|
|
|
endif()
|
|
|
|
set(${varName} ${out_files} PARENT_SCOPE)
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
# This function allow make shader files (.frag, .vert) compiled with glslcc
|
|
|
|
# usage:
|
2023-08-25 16:57:38 +08:00
|
|
|
# - ax_target_compile_shaders(axmol FILES source_files): output compiled shader to ${CMAKE_BINARY_DIR}/runtime/shaders/${SC_LANG}/xxx_fs
|
|
|
|
# - ax_target_compile_shaders(axmol FILES source_files CUSTOM): output compiled shader to ${CMAKE_BINARY_DIR}/runtime/shaders/${SC_LANG}/xxx_fs
|
2023-07-06 21:15:28 +08:00
|
|
|
# - ax_target_compile_shaders(axmol FILES source_files CVAR): the shader will compiled to c hex header for embed include by C/C++ use
|
|
|
|
# Use global variable to control shader file extension:
|
|
|
|
# - GLSLCC_FRAG_SOURCE_FILE_EXTENSIONS: default is .frag;.fsh
|
|
|
|
# - GLSLCC_VERT_SOURCE_FILE_EXTENSIONS: default is .vert;.vsh
|
|
|
|
#
|
2023-07-06 16:09:54 +08:00
|
|
|
function (ax_target_compile_shaders target_name)
|
2023-08-25 16:57:38 +08:00
|
|
|
set(options RUNTIME CVAR)
|
2023-07-06 16:09:54 +08:00
|
|
|
set(multiValueArgs FILES)
|
|
|
|
cmake_parse_arguments(opt "${options}" "" "${multiValueArgs}" ${ARGN})
|
|
|
|
|
|
|
|
foreach(SC_FILE ${opt_FILES})
|
|
|
|
get_filename_component(FILE_EXT ${SC_FILE} LAST_EXT)
|
|
|
|
get_filename_component(FILE_NAME ${SC_FILE} NAME_WE)
|
|
|
|
string(TOLOWER "${FILE_EXT}" FILE_EXT)
|
|
|
|
|
|
|
|
# silent when compile shader success
|
|
|
|
set(SC_FLAGS "--silent" "--err-format=msvc")
|
|
|
|
|
|
|
|
# shader lang
|
|
|
|
set(SC_PROFILE "")
|
2023-08-25 16:57:38 +08:00
|
|
|
if(ANDROID OR WINRT OR AX_USE_ANGLE)
|
2023-07-06 16:09:54 +08:00
|
|
|
# version 300 es
|
2023-08-25 16:57:38 +08:00
|
|
|
set(OUT_LANG "ESSL")
|
|
|
|
set(SC_PROFILE "300")
|
2023-07-06 16:09:54 +08:00
|
|
|
list(APPEND SC_FLAGS "--lang=gles" "--profile=${SC_PROFILE}")
|
2023-08-25 16:57:38 +08:00
|
|
|
elseif (WIN32 OR LINUX)
|
2023-07-06 16:09:54 +08:00
|
|
|
# version 330
|
|
|
|
set(OUT_LANG "GLSL")
|
|
|
|
set(SC_PROFILE "330")
|
|
|
|
list(APPEND SC_FLAGS "--lang=glsl" "--profile=${SC_PROFILE}")
|
2023-08-25 16:57:38 +08:00
|
|
|
elseif (APPLE)
|
2023-07-06 16:09:54 +08:00
|
|
|
set(OUT_LANG "MSL")
|
2023-08-25 16:57:38 +08:00
|
|
|
list(APPEND SC_FLAGS "--lang=msl" "--defines=METAL")
|
2023-07-06 16:09:54 +08:00
|
|
|
endif()
|
|
|
|
|
|
|
|
# includes
|
|
|
|
get_source_file_property(INC_DIRS ${SC_FILE} GLSLCC_INCLUDE_DIRS)
|
|
|
|
if (INC_DIRS STREQUAL "NOTFOUND")
|
|
|
|
set(INC_DIRS "")
|
|
|
|
endif()
|
|
|
|
list(APPEND INC_DIRS "${_AX_ROOT}/core/renderer/shaders")
|
|
|
|
list(APPEND SC_FLAGS "--include-dirs=${INC_DIRS}")
|
|
|
|
|
2023-08-25 16:57:38 +08:00
|
|
|
# flat-ubs
|
|
|
|
if(${GLSLCC_FLAT_UBOS})
|
|
|
|
list(APPEND SC_FLAGS "--flatten-ubos")
|
2023-07-08 17:17:10 +08:00
|
|
|
endif()
|
|
|
|
|
2023-07-06 16:09:54 +08:00
|
|
|
# input
|
|
|
|
if (${FILE_EXT} IN_LIST GLSLCC_FRAG_SOURCE_FILE_EXTENSIONS)
|
|
|
|
list(APPEND SC_FLAGS "--frag=${SC_FILE}")
|
2023-08-25 16:57:38 +08:00
|
|
|
set(TYPE "fs")
|
2023-07-06 16:09:54 +08:00
|
|
|
elseif(${FILE_EXT} IN_LIST GLSLCC_VERT_SOURCE_FILE_EXTENSIONS)
|
2023-08-25 16:57:38 +08:00
|
|
|
set(TYPE "vs")
|
2023-07-06 16:09:54 +08:00
|
|
|
list(APPEND SC_FLAGS "--vert=${SC_FILE}")
|
|
|
|
else()
|
|
|
|
message(FATAL_ERROR "Invalid shader source, the file extesion must be one of .frag;.vert")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# output
|
2023-07-08 17:17:10 +08:00
|
|
|
set(OUT_DIR ${GLSLCC_OUT_DIR})
|
|
|
|
if (NOT (IS_DIRECTORY ${OUT_DIR}))
|
|
|
|
file(MAKE_DIRECTORY ${OUT_DIR})
|
2023-07-06 16:09:54 +08:00
|
|
|
endif()
|
2023-08-25 16:57:38 +08:00
|
|
|
if (NOT opt_CVAR)
|
|
|
|
list(APPEND SC_FLAGS "--output=${OUT_DIR}/${FILE_NAME}${GLSLCC_OUT_SUFFIX}" )
|
2023-07-19 23:41:16 +08:00
|
|
|
|
2023-08-25 16:57:38 +08:00
|
|
|
# glscc will auto insert ${FILE_NAME}_vs.bin or ${FILE_NAME}_fs.bin
|
|
|
|
# so we set OUTPUT to match with it, otherwise will cause cause incremental build to work incorrectly.
|
|
|
|
set(SC_OUTPUT "${OUT_DIR}/${FILE_NAME}_${TYPE}${GLSLCC_OUT_SUFFIX}")
|
|
|
|
else()
|
|
|
|
set(SC_OUTPUT "${OUT_DIR}/${FILE_NAME}_${TYPE}${GLSLCC_OUT_SUFFIX}.h")
|
|
|
|
list(APPEND SC_FLAGS "${OUT_FILE}" "--cvar=shader_rt_${FILE_NAME}" "--output=${SC_OUTPUT}")
|
2023-08-25 16:49:08 +08:00
|
|
|
endif()
|
|
|
|
|
2023-08-25 16:57:38 +08:00
|
|
|
set(SC_COMMENT "Compiling shader ${SC_FILE} for ${OUT_LANG}${SC_PROFILE} to ${SC_OUTPUT} ...")
|
|
|
|
string(REPLACE ";" " " FULL_COMMAND_LINE "${GLSLCC_EXE};${SC_FLAGS} ...")
|
|
|
|
add_custom_command(
|
|
|
|
MAIN_DEPENDENCY ${SC_FILE} OUTPUT ${SC_OUTPUT} COMMAND ${GLSLCC_EXE} ${SC_FLAGS}
|
|
|
|
COMMENT "${FULL_COMMAND_LINE}"
|
|
|
|
)
|
2023-07-06 16:09:54 +08:00
|
|
|
endforeach()
|
|
|
|
target_sources(${target_name} PRIVATE ${opt_FILES})
|
|
|
|
endfunction()
|
|
|
|
cmake_policy(POP)
|