Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 50 additions & 20 deletions Dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,68 @@ set(BGFX_USE_DEBUG_SUFFIX OFF)

FetchContent_MakeAvailable_With_Message(bgfx.cmake)

# Babylon Native tunes a number of bgfx compile-time settings. Every value passed to
# babylon_native_bgfx_config below is a *default*: an embedder can override any of them
# by setting the matching BGFX_CONFIG_<name> variable before adding Babylon Native.
#
# The value is used as written, so an embedder can spell one the way bgfx spells its own
# defaults -- (4<<10) and the like. CMake booleans are the one thing that cannot be used,
# because the C preprocessor has nothing to do with them, and bgfx.cmake declares several
# of these names as options that can already hold ON or OFF. Those fall back to the
# default below.
#
# These are emitted here rather than left to bgfx.cmake because emitting the resolved
# value here is what makes overriding work at all: these definitions are added after
# bgfx.cmake's, so they are last on the compile line and win. Before this indirection they
# were hard-coded, so an embedder's value produced a conflicting duplicate definition and
# the hard-coded one took effect.
function(babylon_native_bgfx_config NAME DEFAULT)
set(value "${BGFX_CONFIG_${NAME}}")
string(TOUPPER "${value}" value_upper)
if(value STREQUAL "" OR value_upper MATCHES "^(ON|OFF|TRUE|FALSE|YES|NO|Y|N|IGNORE|NOTFOUND|.*-NOTFOUND)$")
Comment thread
bghgary marked this conversation as resolved.
Outdated
set(value "${DEFAULT}")
endif()
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_${NAME}=${value})
endfunction()

# Turn off debug annotations as it causes an access violation in D3D12.
# This flag is set using compile definitions because the bgfx.cmake option is ignored in debug configuration.
# See https://github.com/BabylonJS/bgfx.cmake/blob/0af3c9865a66aff1748a51bb466b24f05a123043/cmake/bgfx/bgfx.cmake#L126.
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_DEBUG_ANNOTATION=0)

target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_DEFAULT_MAX_ENCODERS=2)
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MAX_VERTEX_STREAMS=18)
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MIN_RESOURCE_COMMAND_BUFFER_SIZE=16)
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MIN_UNIFORM_BUFFER_SIZE=4096)
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_THRESHOLD_SIZE=256)
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_INCREMENT_SIZE=1024)
babylon_native_bgfx_config(DEBUG_ANNOTATION 0)

babylon_native_bgfx_config(DEFAULT_MAX_ENCODERS 2)
babylon_native_bgfx_config(MAX_VERTEX_STREAMS 18)
babylon_native_bgfx_config(MIN_RESOURCE_COMMAND_BUFFER_SIZE 16)
babylon_native_bgfx_config(MIN_UNIFORM_BUFFER_SIZE 4096)
babylon_native_bgfx_config(UNIFORM_BUFFER_RESIZE_THRESHOLD_SIZE 256)
babylon_native_bgfx_config(UNIFORM_BUFFER_RESIZE_INCREMENT_SIZE 1024)
target_compile_definitions(bgfx PUBLIC BGFX_PLATFORM_SUPPORTS_WGSL=0)

# Canvas plugin allocates one bgfx framebuffer per JS Canvas object and per
# text-rendering operation; combined with V8 GC pacing this can exceed the
# default 128 limit during long playground sweeps. We enforce a floor of 512:
# CI workflows pass -DBGFX_CONFIG_MAX_FRAME_BUFFERS, and any value below 512
# (including a smaller CI override) is clamped up so the pool never regresses
# below the size the Canvas sweeps need.
if(NOT BGFX_CONFIG_MAX_FRAME_BUFFERS OR BGFX_CONFIG_MAX_FRAME_BUFFERS LESS 512)
set(BGFX_CONFIG_MAX_FRAME_BUFFERS 512)
# The Canvas polyfill allocates one bgfx framebuffer per JS Canvas object and per
# text-rendering operation; combined with V8 GC pacing this can exceed the default
# 128 limit during long playground sweeps, so Canvas builds need a floor of 512.
# That floor is a Canvas requirement, not a universal one: an embedder that disables
# the polyfill draws into a handful of framebuffers and should be able to select a
# much smaller pool. Clamp only when Canvas is actually being built, and otherwise
# leave bgfx on its own default unless the embedder asked for something.
if(BABYLON_NATIVE_POLYFILL_CANVAS)
if(NOT BGFX_CONFIG_MAX_FRAME_BUFFERS OR BGFX_CONFIG_MAX_FRAME_BUFFERS LESS 512)
set(BGFX_CONFIG_MAX_FRAME_BUFFERS 512)
endif()
Comment thread
bghgary marked this conversation as resolved.
Outdated
endif()

if(BGFX_CONFIG_MAX_FRAME_BUFFERS)
babylon_native_bgfx_config(MAX_FRAME_BUFFERS ${BGFX_CONFIG_MAX_FRAME_BUFFERS})
endif()
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MAX_FRAME_BUFFERS=${BGFX_CONFIG_MAX_FRAME_BUFFERS})

# Temporary disable uniform debug.
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_DEBUG_UNIFORM=0)
babylon_native_bgfx_config(DEBUG_UNIFORM 0)

# Disable video decoding support (not used by Babylon Native).
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_VIDEO=0)
babylon_native_bgfx_config(VIDEO 0)

# Disable the C99 API (Babylon Native uses the C++ API only); saves binary size.
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_C99_API=0)
babylon_native_bgfx_config(C99_API 0)

if(GRAPHICS_API STREQUAL "D3D11")
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_RENDERER_DIRECT3D11=1)
Expand Down