config: Factor out crosscompile tool search

Add search for shaderc, texturec, texturev and geometryv
This commit is contained in:
Sandy Carter
2023-01-08 08:10:08 -05:00
committed by Sandy
parent 34d0f372b1
commit d50eec41c1
5 changed files with 142 additions and 76 deletions

View File

@@ -4,13 +4,8 @@
with section("parse"):
# Specify structure for custom cmake functions
additional_commands = { 'compile_shader_to_header': { 'kwargs': { 'OUTPUT_DIR': 1,
'SHADERS': '+',
'TYPE': 1,
'VARYING_DEF': 1},
'pargs': {'flags': [], 'nargs': '*'}},
'get_profile_ext': {'pargs': {'nargs': 2}},
'shaderc_parse': { 'kwargs': { 'BIN2C': 1,
additional_commands = { '_bgfx_get_profile_ext': {'pargs': {'nargs': 2}},
'_bgfx_shaderc_parse': { 'kwargs': { 'BIN2C': 1,
'DEFINES': '+',
'FILE': 1,
'INCLUDES': '+',
@@ -35,7 +30,12 @@ with section("parse"):
'DEBUG',
'DISASM',
'WERROR'],
'nargs': '1+'}}}
'nargs': '1+'}},
'bgfx_compile_shader_to_header': { 'kwargs': { 'OUTPUT_DIR': 1,
'SHADERS': '+',
'TYPE': 1,
'VARYING_DEF': 1},
'pargs': {'flags': [], 'nargs': '*'}}}
# Override configurations per-command where available
override_spec = {}

View File

@@ -1,13 +1,11 @@
bgfx.cmake
==========
# bgfx.cmake
[![Build Status](https://github.com/bkaradzic/bgfx.cmake/workflows/Release/badge.svg)](https://github.com/bkaradzic/bgfx.cmake/workflows/Release/badge.svg)
**NOTE: This port only made to be used as C++ library, some features (such as bindings) might not work! For those features, please use original repo with GENie instead.**
This repo contains cmake configuration files that can be used to build bgfx with CMake.
Building
---
## Building
```bash
git clone https://github.com/bkaradzic/bgfx.cmake.git
@@ -20,12 +18,10 @@ cmake --build cmake-build
If downloading via zip (instead of using git submodules) manually download bx, bimg and bgfx and copy them into the root directory, or locate them via `BX_DIR`, `BIMG_DIR` and `BGFX_DIR` CMake variables.
How To Use
----------
## How To Use
This project is setup to be included a few different ways. To include bgfx source code in your project simply use add_subdirectory to include this project. To build bgfx binaries build the `INSTALL` target (or `make install`). The installed files will be in the directory specified by `CMAKE_INSTALL_PREFIX` which we recommend you set to `./install` so it will export to your build directory. Note you may want to build install on both `Release` and `Debug` configurations.
Features
--------
## Features
* No outside dependencies besides bx, bimg, bgfx, and CMake.
* Tested on
* Windows, OSX, Linux, Android, UWP, Emscripten (experimental)
@@ -33,6 +29,66 @@ Features
* Compiles bgfx, tools & examples.
* Detects shader modifications and automatically rebuilds them for all examples.
Does this work with latest bx/bgfx/bimg?
----------------------------------------
## Added cmake commands
bgfx.cmake will install `bgfxToolUtils.cmake` which has useful cmake functions for using bgfx's tools:
### `bgfx_compile_shader_to_header`
Add a build rule for a `*.sc` shader to the generated build system using shaderc.
```cmake
bgfx_compile_shader_to_header(
TYPE VERTEX|FRAGMENT|COMPUTE
SHADERS filenames
VARYING_DEF filename
OUTPUT_DIR directory
)
```
This defines a shaderc command to generate headers for a number of `TYPE` shaders with `SHADERS` files and `VARYING_DEF` file in the `OUTPUT_DIR` directory. There will be one generated shader for each supported rendering API on this current platform according to the `BGFX_EMBEDDED_SHADER` macro in `bgfx/embedded_shader.h`.
The generated headers will have names in the format of `${SHADERS}.${RENDERING_API}.bin.h` where `RENDERING_API` can be `glsl`, `essl`, `spv`, `dx9`, `dx11` and `mtl` depending on the availability of the platform.
Adding these `SHADERS` as source files to a target will run `shaderc` at build time and they will rebuild if either the contents of the `SHADERS` or the `VARYING_DEF` change.
#### Examples: Generating shaders as headers
```cmake
bgfx_compile_shader_to_header(
TYPE VERTEX
SHADERS vs.sc
VARYING_DEF varying.def.sc
OUTPUT_DIR ${CMAKE_BINARY_DIR}/include/generated/shaders
)
bgfx_compile_shader_to_header(
TYPE FRAGMENT
SHADERS fs.sc
VARYING_DEF ${CMAKE_SOURCE_DIR}/varying.def.sc
OUTPUT_DIR ${CMAKE_BINARY_DIR}/include/generated/shaders
)
add_library(myLib main.cpp vs.sc fs.sc)
target_include_directories(myLib ${CMAKE_BINARY_DIR}/include/generated/shaders)
```
```cpp
// main.cpp
#include <vs.sc.glsl.bin.h>
#include <vs.sc.essl.bin.h>
#include <vs.sc.spv.bin.h>
#include <fs.sc.glsl.bin.h>
#include <fs.sc.essl.bin.h>
#include <fs.sc.spv.bin.h>
#if defined(_WIN32)
#include <vs.sc.dx9.bin.h>
#include <vs.sc.dx11.bin.h>
#include <fs.sc.dx9.bin.h>
#include <fs.sc.dx11.bin.h>
#endif // defined(_WIN32)
#if __APPLE__
#include <vs.sc.mtl.bin.h>
#include <fs.sc.mtl.bin.h>
#endif // __APPLE__
const bgfx::EmbeddedShader k_vs = BGFX_EMBEDDED_SHADER(vs);
const bgfx::EmbeddedShader k_fs = BGFX_EMBEDDED_SHADER(fs);
```
## Does this work with latest bx/bgfx/bimg?
Probably! This project needs to be updated if a dependency is added or the bgfx build system changes. The bgfx repository is very active but these types of changes are rare. New examples have to be added manually as well, but not doing so will merely result in that example not showing up and won't break anything else. If pulling latest causes issues, be sure to manually reconfigure CMake as the glob patterns may need to be refreshed (the use of glob patterns in CMake is generally discouraged but in this project it helps to ensure upwards compatibilty with newer bgfx updates).

View File

@@ -6,16 +6,26 @@ list(GET BGFX_INCLUDE_PATH 0 BGFX_INCLUDE_PATH_1) # bgfx::bgfx exports include d
set(BGFX_SHADER_INCLUDE_PATH ${BGFX_INCLUDE_PATH_1}/bgfx)
# If cross compiling, we need a host-compatible version of shaderc to compile shaders
if(@CMAKE_CROSSCOMPILING@ AND NOT TARGET bgfx::shaderc)
if(@CMAKE_CROSSCOMPILING@)
macro(_bgfx_crosscompile_use_host_tool TOOL_NAME)
if(NOT TARGET bgfx::${TOOL_NAME})
find_program(
shaderc_EXECUTABLE REQUIRED
NAMES bgfx-shaderc shaderc
PATHS /usr/bin ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/packages/bgfx_x64-linux/tools/bgfx
${TOOL_NAME}_EXECUTABLE REQUIRED
NAMES bgfx-${TOOL_NAME} ${TOOL_NAME}
PATHS /usr/bin #
${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/packages/bgfx_x64-linux/tools/bgfx
${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/packages/bgfx_x64-windows/tools/bgfx
${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/packages/bgfx_x64-osx/tools/bgfx
)
add_executable(bgfx::shaderc IMPORTED)
set_target_properties(bgfx::shaderc PROPERTIES IMPORTED_LOCATION "${shaderc_EXECUTABLE}")
add_executable(bgfx::${TOOL_NAME} IMPORTED)
set_target_properties(bgfx::${TOOL_NAME} PROPERTIES IMPORTED_LOCATION "${${TOOL_NAME}_EXECUTABLE}")
endif()
endmacro()
_bgfx_crosscompile_use_host_tool(shaderc)
_bgfx_crosscompile_use_host_tool(texturec)
_bgfx_crosscompile_use_host_tool(texturev)
_bgfx_crosscompile_use_host_tool(geometryv)
endif()
include("${CMAKE_CURRENT_LIST_DIR}/bgfxToolUtils.cmake")

View File

@@ -1,4 +1,4 @@
# shaderc_parse(
# _bgfx_shaderc_parse(
# FILE filename
# OUTPUT filename
# FRAGMENT|VERTEX|COMPUTE
@@ -17,7 +17,7 @@
# [DISASM]
# [WERROR]
# )
function(shaderc_parse ARG_OUT)
function(_bgfx_shaderc_parse ARG_OUT)
cmake_parse_arguments(
ARG
"DEPENDS;ANDROID;ASM_JS;IOS;LINUX;NACL;OSX;WINDOWS;PREPROCESS;RAW;FRAGMENT;VERTEX;COMPUTE;VERBOSE;DEBUG;DISASM;WERROR"
@@ -61,14 +61,14 @@ function(shaderc_parse ARG_OUT)
foreach(P ${PLATFORMS})
if(ARG_${P})
if(PLATFORM)
message(SEND_ERROR "Call to shaderc_parse() cannot have both flags ${PLATFORM} and ${P}.")
message(SEND_ERROR "Call to _bgfx_shaderc_parse() cannot have both flags ${PLATFORM} and ${P}.")
return()
endif()
set(PLATFORM "${P}")
endif()
endforeach()
if(PLATFORM STREQUAL "")
message(SEND_ERROR "Call to shaderc_parse() must have a platform flag: ${PLATFORMS}")
message(SEND_ERROR "Call to _bgfx_shaderc_parse() must have a platform flag: ${PLATFORMS}")
return()
elseif(PLATFORM STREQUAL ANDROID)
list(APPEND CLI "--platform" "android")
@@ -116,14 +116,14 @@ function(shaderc_parse ARG_OUT)
foreach(T ${TYPES})
if(ARG_${T})
if(TYPE)
message(SEND_ERROR "Call to shaderc_parse() cannot have both flags ${TYPE} and ${T}.")
message(SEND_ERROR "Call to _bgfx_shaderc_parse() cannot have both flags ${TYPE} and ${T}.")
return()
endif()
set(TYPE "${T}")
endif()
endforeach()
if("${TYPE}" STREQUAL "")
message(SEND_ERROR "Call to shaderc_parse() must have a type flag: ${TYPES}")
message(SEND_ERROR "Call to _bgfx_shaderc_parse() must have a type flag: ${TYPES}")
return()
elseif("${TYPE}" STREQUAL "FRAGMENT")
list(APPEND CLI "--type" "fragment")
@@ -171,7 +171,7 @@ function(shaderc_parse ARG_OUT)
set(${ARG_OUT} ${CLI} PARENT_SCOPE)
endfunction()
function(_get_profile_ext PROFILE PROFILE_EXT)
function(_bgfx_get_profile_ext PROFILE PROFILE_EXT)
string(REPLACE 300_es essl PROFILE ${PROFILE})
string(REPLACE 120 glsl PROFILE ${PROFILE})
string(REPLACE spirv spv PROFILE ${PROFILE})
@@ -183,14 +183,14 @@ function(_get_profile_ext PROFILE PROFILE_EXT)
set(${PROFILE_EXT} ${PROFILE} PARENT_SCOPE)
endfunction()
# compile_shader_to_header(
# bgfx_compile_shader_to_header(
# TYPE VERTEX|FRAGMENT|COMPUTE
# SHADERS filenames
# VARYING_DEF filename
# OUTPUT_DIR directory
#)
# )
#
function(compile_shader_to_header)
function(bgfx_compile_shader_to_header)
set(options "")
set(oneValueArgs TYPE VARYING_DEF OUTPUT_DIR)
set(multiValueArgs SHADERS)
@@ -235,13 +235,13 @@ function(compile_shader_to_header)
set(OUTPUTS "")
set(COMMANDS "")
foreach(PROFILE ${PROFILES})
_get_profile_ext(${PROFILE} PROFILE_EXT)
_bgfx_get_profile_ext(${PROFILE} PROFILE_EXT)
set(OUTPUT ${ARGS_OUTPUT_DIR}/${SHADER_FILE_BASENAME}.${PROFILE_EXT}.bin.h)
set(PLATFORM_I ${PLATFORM})
if(PROFILE STREQUAL "spirv")
set(PLATFORM_I LINUX)
endif()
shaderc_parse(
_bgfx_shaderc_parse(
CLI #
${ARGS_TYPE} ${PLATFORM_I} WERROR "$<$<CONFIG:debug>:DEBUG>$<$<CONFIG:relwithdebinfo>:DEBUG>"
FILE ${SHADER_FILE_ABSOLUTE}

View File

@@ -41,7 +41,7 @@ function(add_bgfx_shader FILE FOLDER)
# dx9
if(NOT "${TYPE}" STREQUAL "COMPUTE")
set(DX9_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/dx9/${FILENAME}.bin)
shaderc_parse(
_bgfx_shaderc_parse(
DX9 ${COMMON} WINDOWS
PROFILE ${D3D_PREFIX}_3_0
O 3
@@ -54,14 +54,14 @@ function(add_bgfx_shader FILE FOLDER)
# dx11
set(DX11_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/dx11/${FILENAME}.bin)
if(NOT "${TYPE}" STREQUAL "COMPUTE")
shaderc_parse(
_bgfx_shaderc_parse(
DX11 ${COMMON} WINDOWS
PROFILE ${D3D_PREFIX}_5_0
O 3
OUTPUT ${DX11_OUTPUT}
)
else()
shaderc_parse(
_bgfx_shaderc_parse(
DX11 ${COMMON} WINDOWS
PROFILE ${D3D_PREFIX}_5_0
O 1
@@ -75,7 +75,7 @@ function(add_bgfx_shader FILE FOLDER)
if(APPLE)
# metal
set(METAL_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/metal/${FILENAME}.bin)
shaderc_parse(METAL ${COMMON} OSX PROFILE metal OUTPUT ${METAL_OUTPUT})
_bgfx_shaderc_parse(METAL ${COMMON} OSX PROFILE metal OUTPUT ${METAL_OUTPUT})
list(APPEND OUTPUTS "METAL")
set(OUTPUTS_PRETTY "${OUTPUTS_PRETTY}Metal, ")
endif()
@@ -83,7 +83,7 @@ function(add_bgfx_shader FILE FOLDER)
# essl
if(NOT "${TYPE}" STREQUAL "COMPUTE")
set(ESSL_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/essl/${FILENAME}.bin)
shaderc_parse(ESSL ${COMMON} ANDROID OUTPUT ${ESSL_OUTPUT})
_bgfx_shaderc_parse(ESSL ${COMMON} ANDROID OUTPUT ${ESSL_OUTPUT})
list(APPEND OUTPUTS "ESSL")
set(OUTPUTS_PRETTY "${OUTPUTS_PRETTY}ESSL, ")
endif()
@@ -91,9 +91,9 @@ function(add_bgfx_shader FILE FOLDER)
# glsl
set(GLSL_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/glsl/${FILENAME}.bin)
if(NOT "${TYPE}" STREQUAL "COMPUTE")
shaderc_parse(GLSL ${COMMON} LINUX PROFILE 120 OUTPUT ${GLSL_OUTPUT})
_bgfx_shaderc_parse(GLSL ${COMMON} LINUX PROFILE 120 OUTPUT ${GLSL_OUTPUT})
else()
shaderc_parse(GLSL ${COMMON} LINUX PROFILE 430 OUTPUT ${GLSL_OUTPUT})
_bgfx_shaderc_parse(GLSL ${COMMON} LINUX PROFILE 430 OUTPUT ${GLSL_OUTPUT})
endif()
list(APPEND OUTPUTS "GLSL")
set(OUTPUTS_PRETTY "${OUTPUTS_PRETTY}GLSL, ")
@@ -101,7 +101,7 @@ function(add_bgfx_shader FILE FOLDER)
# spirv
if(NOT "${TYPE}" STREQUAL "COMPUTE")
set(SPIRV_OUTPUT ${BGFX_DIR}/examples/runtime/shaders/spirv/${FILENAME}.bin)
shaderc_parse(SPIRV ${COMMON} LINUX PROFILE spirv OUTPUT ${SPIRV_OUTPUT})
_bgfx_shaderc_parse(SPIRV ${COMMON} LINUX PROFILE spirv OUTPUT ${SPIRV_OUTPUT})
list(APPEND OUTPUTS "SPIRV")
set(OUTPUTS_PRETTY "${OUTPUTS_PRETTY}SPIRV")
set(OUTPUT_FILES "")