From 95a0a20a016897af335f1ccb3d2cd626ffd4687a Mon Sep 17 00:00:00 2001 From: Sandy Carter Date: Sat, 14 Jan 2023 18:56:27 -0500 Subject: [PATCH] bimg: Add texturec and cmake macro --- .cmake-format.py | 38 ++++++- CMakeLists.txt | 4 + README.md | 24 +++++ cmake/Config.cmake.in | 2 +- cmake/bgfxToolUtils.cmake | 147 +++++++++++++++++++++++++++ cmake/bimg/CMakeLists.txt | 4 + cmake/{tools => bimg}/texturec.cmake | 21 ++-- cmake/bx/CMakeLists.txt | 2 +- cmake/bx/bin2c.cmake | 10 ++ cmake/tools.cmake | 6 -- 10 files changed, 242 insertions(+), 16 deletions(-) rename cmake/{tools => bimg}/texturec.cmake (71%) diff --git a/.cmake-format.py b/.cmake-format.py index 48ea7ebe..3440b562 100644 --- a/.cmake-format.py +++ b/.cmake-format.py @@ -35,6 +35,24 @@ 'DISASM', 'WERROR'], 'nargs': '1+'}}, + '_bgfx_texturec_parse': { 'kwargs': { 'AS': 1, + 'FILE': 1, + 'FORMAT': 1, + 'MAX': 1, + 'MIPSKIP': 1, + 'OUTPUT': 1, + 'QUALITY': 1, + 'RADIANCE': 1, + 'REF': 1}, + 'pargs': { 'flags': [ 'MIPS', + 'NORMALMAP', + 'EQUIRECT', + 'STRIP', + 'SDF', + 'IQA', + 'PMA', + 'LINEAR'], + 'nargs': '*'}}, 'bgfx_compile_binary_to_header': { 'kwargs': { 'ARRAY_NAME': 1, 'INPUT_FILE': 1, 'OUTPUT_FILE': 1}, @@ -43,7 +61,25 @@ 'SHADERS': '+', 'TYPE': 1, 'VARYING_DEF': 1}, - 'pargs': {'flags': [], 'nargs': '*'}}} + 'pargs': {'flags': [], 'nargs': '*'}}, + 'bgfx_compile_texture': { 'kwargs': { 'AS': 1, + 'FILE': 1, + 'FORMAT': 1, + 'MAX': 1, + 'MIPSKIP': 1, + 'OUTPUT': 1, + 'QUALITY': 1, + 'RADIANCE': 1, + 'REF': 1}, + 'pargs': { 'flags': [ 'MIPS', + 'NORMALMAP', + 'EQUIRECT', + 'STRIP', + 'SDF', + 'IQA', + 'PMA', + 'LINEAR'], + 'nargs': '*'}}} # Override configurations per-command where available override_spec = {} diff --git a/CMakeLists.txt b/CMakeLists.txt index 21aaf69b..f7edc023 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,6 +94,10 @@ endif() # sets project version from api ver / git rev include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.cmake) +if(BGFX_BUILD_TOOLS AND BGFX_CUSTOM_TARGETS) + add_custom_target(tools) + set_target_properties(tools PROPERTIES FOLDER "bgfx/tools") +endif() include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/shared.cmake) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/cmake/bx) diff --git a/README.md b/README.md index c4af2665..5aeef91c 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,30 @@ target_include_directories(myLib ${CMAKE_BINARY_DIR}/include/generated/images) #include ``` +### `bgfx_compile_texture` +Add a build rule for a texture to the generated build system be compiled using texturec. +```cmake +bgfx_compile_texture( + FILE filename + OUTPUT filename + [FORMAT format] + [QUALITY default|fastest|highest] + [MIPS] + [MIPSKIP N] + [NORMALMAP] + [EQUIRECT] + [STRIP] + [SDF] + [REF alpha] + [IQA] + [PMA] + [LINEAR] + [MAX max size] + [RADIANCE model] + [AS extension] +) +``` + ### `bgfx_compile_shader_to_header` Add a build rule for a `*.sc` shader to the generated build system using shaderc. ```cmake diff --git a/cmake/Config.cmake.in b/cmake/Config.cmake.in index 45462bb8..3067dcd6 100644 --- a/cmake/Config.cmake.in +++ b/cmake/Config.cmake.in @@ -26,8 +26,8 @@ if(@CMAKE_CROSSCOMPILING@) endmacro() _bgfx_crosscompile_use_host_tool(bin2c) - _bgfx_crosscompile_use_host_tool(shaderc) _bgfx_crosscompile_use_host_tool(texturec) + _bgfx_crosscompile_use_host_tool(shaderc) _bgfx_crosscompile_use_host_tool(texturev) _bgfx_crosscompile_use_host_tool(geometryv) endif() diff --git a/cmake/bgfxToolUtils.cmake b/cmake/bgfxToolUtils.cmake index b676fe20..29a7e6b0 100644 --- a/cmake/bgfxToolUtils.cmake +++ b/cmake/bgfxToolUtils.cmake @@ -59,6 +59,153 @@ function(bgfx_compile_binary_to_header) ) endfunction() +# _bgfx_texturec_parse( +# FILE filename +# OUTPUT filename +# [FORMAT format] +# [QUALITY default|fastest|highest] +# [MIPS] +# [MIPSKIP N] +# [NORMALMAP] +# [EQUIRECT] +# [STRIP] +# [SDF] +# [REF alpha] +# [IQA] +# [PMA] +# [LINEAR] +# [MAX max size] +# [RADIANCE model] +# [AS extension] +# ) +function(_bgfx_texturec_parse ARG_OUT) + cmake_parse_arguments( + ARG "MIPS;NORMALMAP;EQUIRECT;STRIP;SDF;IQA;PMA;LINEAR" "FILE;OUTPUT;FORMAT;QUALITY;MIPSKIP;REF;MAX;RADIANCE;AS" + "" ${ARGN} + ) + set(CLI "") + + # -f + if(ARG_FILE) + list(APPEND CLI "-f" "${ARG_FILE}") + endif() + + # -o + if(ARG_OUTPUT) + list(APPEND CLI "-o" "${ARG_OUTPUT}") + endif() + + # -t + if(ARG_FORMAT) + list(APPEND CLI "-t" "${ARG_FORMAT}") + endif() + + # -q + if(ARG_QUALITY) + list(APPEND CLI "-q" "${ARG_QUALITY}") + endif() + + # --mips + if(ARG_MIPS) + list(APPEND CLI "--mips") + endif() + + # --mipskip + if(ARG_MIPSKIP) + list(APPEND CLI "--mipskip" "${ARG_MIPSKIP}") + endif() + + # --normalmap + if(ARG_NORMALMAP) + list(APPEND CLI "--normalmap") + endif() + + # --equirect + if(ARG_EQUIRECT) + list(APPEND CLI "--equirect") + endif() + + # --strip + if(ARG_STRIP) + list(APPEND CLI "--strip") + endif() + + # --sdf + if(ARG_SDF) + list(APPEND CLI "--sdf") + endif() + + # --ref + if(ARG_REF) + list(APPEND CLI "--ref" "${ARG_REF}") + endif() + + # --iqa + if(ARG_IQA) + list(APPEND CLI "--iqa") + endif() + + # --pma + if(ARG_PMA) + list(APPEND CLI "--pma") + endif() + + # --linear + if(ARG_LINEAR) + list(APPEND CLI "--linear") + endif() + + # --max + if(ARG_MAX) + list(APPEND CLI "--max" "${ARG_MAX}") + endif() + + # --radiance + if(ARG_RADIANCE) + list(APPEND CLI "--radiance" "${ARG_RADIANCE}") + endif() + + # --as + if(ARG_AS) + list(APPEND CLI "--as" "${ARG_AS}") + endif() + + set(${ARG_OUT} ${CLI} PARENT_SCOPE) +endfunction() + +# bgfx_compile_texture( +# FILE filename +# OUTPUT filename +# [FORMAT format] +# [QUALITY default|fastest|highest] +# [MIPS] +# [MIPSKIP N] +# [NORMALMAP] +# [EQUIRECT] +# [STRIP] +# [SDF] +# [REF alpha] +# [IQA] +# [PMA] +# [LINEAR] +# [MAX max size] +# [RADIANCE model] +# [AS extension] +# ) +# +function(bgfx_compile_texture) + cmake_parse_arguments( + ARG "MIPS;NORMALMAP;EQUIRECT;STRIP;SDF;IQA;PMA;LINEAR" "FILE;OUTPUT;FORMAT;QUALITY;MIPSKIP;REF;MAX;RADIANCE;AS" + "" ${ARGN} + ) + _bgfx_texturec_parse(CLI ${ARGV}) + add_custom_command( + OUTPUT ${ARG_OUTPUT} # + COMMAND bgfx::texturec ${CLI} # + MAIN_DEPENDENCY ${ARG_INPUT} # + ) +endfunction() + # _bgfx_shaderc_parse( # FILE filename # OUTPUT filename diff --git a/cmake/bimg/CMakeLists.txt b/cmake/bimg/CMakeLists.txt index 7e605b88..200b29bd 100644 --- a/cmake/bimg/CMakeLists.txt +++ b/cmake/bimg/CMakeLists.txt @@ -22,3 +22,7 @@ include(3rdparty/miniz.cmake) include(bimg.cmake) include(bimg_decode.cmake) include(bimg_encode.cmake) + +if(BGFX_BUILD_TOOLS_TEXTURE) + include(texturec.cmake) +endif() diff --git a/cmake/tools/texturec.cmake b/cmake/bimg/texturec.cmake similarity index 71% rename from cmake/tools/texturec.cmake rename to cmake/bimg/texturec.cmake index 64d65cc4..24380a57 100644 --- a/cmake/tools/texturec.cmake +++ b/cmake/bimg/texturec.cmake @@ -1,24 +1,31 @@ # bgfx.cmake - bgfx building in cmake # Written in 2017 by Joshua Brookover - +# # To the extent possible under law, the author(s) have dedicated all copyright # and related and neighboring rights to this software to the public domain # worldwide. This software is distributed without any warranty. - +# # You should have received a copy of the CC0 Public Domain Dedication along with # this software. If not, see . -include(CMakeParseArguments) +add_executable(texturec) -add_executable(texturec ${BIMG_DIR}/tools/texturec/texturec.cpp) -set_target_properties(texturec PROPERTIES FOLDER "bgfx/tools") +# Grab the texturec source files +file(GLOB_RECURSE TEXTUREC_SOURCES # + ${BIMG_DIR}/tools/texturec/*.cpp # + ${BIMG_DIR}/tools/texturec/*.h # +) + +target_sources(texturec PRIVATE ${TEXTUREC_SOURCES}) target_link_libraries(texturec PRIVATE bimg_decode bimg_encode bimg) -if(BGFX_CUSTOM_TARGETS) +set_target_properties(texturec PROPERTIES FOLDER "bgfx/tools") + +if(BGFX_BUILD_TOOLS AND BGFX_CUSTOM_TARGETS) add_dependencies(tools texturec) endif() if(ANDROID) - target_link_libraries(texturec log) + target_link_libraries(texturec PRIVATE log) elseif(IOS) set_target_properties(texturec PROPERTIES MACOSX_BUNDLE ON MACOSX_BUNDLE_GUI_IDENTIFIER texturec) endif() diff --git a/cmake/bx/CMakeLists.txt b/cmake/bx/CMakeLists.txt index fde277db..7fd53034 100644 --- a/cmake/bx/CMakeLists.txt +++ b/cmake/bx/CMakeLists.txt @@ -10,7 +10,7 @@ include(bx.cmake) -if(BGFX_BUILD_TOOLS) +if(BGFX_BUILD_TOOLS_BIN2C) include(bin2c.cmake) endif() diff --git a/cmake/bx/bin2c.cmake b/cmake/bx/bin2c.cmake index 12c4e124..5748d8f5 100644 --- a/cmake/bx/bin2c.cmake +++ b/cmake/bx/bin2c.cmake @@ -19,3 +19,13 @@ file(GLOB_RECURSE BIN2C_SOURCES # target_sources(bin2c PRIVATE ${BIN2C_SOURCES}) target_link_libraries(bin2c PRIVATE bx) set_target_properties(bin2c PROPERTIES FOLDER "bgfx/tools") + +if(BGFX_BUILD_TOOLS AND BGFX_CUSTOM_TARGETS) + add_dependencies(tools bin2c) +endif() + +if(ANDROID) + target_link_libraries(bin2c PRIVATE log) +elseif(IOS) + set_target_properties(bin2c PROPERTIES MACOSX_BUNDLE ON MACOSX_BUNDLE_GUI_IDENTIFIER bin2c) +endif() diff --git a/cmake/tools.cmake b/cmake/tools.cmake index b4ca8e9d..0e619a28 100644 --- a/cmake/tools.cmake +++ b/cmake/tools.cmake @@ -8,11 +8,6 @@ # You should have received a copy of the CC0 Public Domain Dedication along with # this software. If not, see . -if(BGFX_CUSTOM_TARGETS) - add_custom_target(tools) - set_target_properties(tools PROPERTIES FOLDER "bgfx/tools") -endif() - if(BGFX_BUILD_TOOLS_SHADER) include(${CMAKE_CURRENT_LIST_DIR}/tools/shaderc.cmake) endif() @@ -23,6 +18,5 @@ if(BGFX_BUILD_TOOLS_GEOMETRY) endif() if(BGFX_BUILD_TOOLS_TEXTURE) - include(${CMAKE_CURRENT_LIST_DIR}/tools/texturec.cmake) include(${CMAKE_CURRENT_LIST_DIR}/tools/texturev.cmake) endif()