Skip to content

Add BABYLON_NATIVE_PLUGIN_NATIVEENGINE_LOAD_IMAGES CMake option#1677

Merged
CedricGuillemet merged 9 commits into
BabylonJS:masterfrom
CedricGuillemet:NoImageLoading
Apr 28, 2026
Merged

Add BABYLON_NATIVE_PLUGIN_NATIVEENGINE_LOAD_IMAGES CMake option#1677
CedricGuillemet merged 9 commits into
BabylonJS:masterfrom
CedricGuillemet:NoImageLoading

Conversation

@CedricGuillemet
Copy link
Copy Markdown
Collaborator

@CedricGuillemet CedricGuillemet commented Apr 27, 2026

Summary

Adds a new CMake option BABYLON_NATIVE_PLUGIN_NATIVEENGINE_LOAD_IMAGES (default ON) that enables all bimg / bx / stb / WebP image-decoding code from the NativeEngine plugin. Useful for embedders that decode textures themselves and want to reduce binary size.

What it does when OFF

  • bimg, bimg_encode, bimg_decode are no longer linked into NativeEngine.
  • BABYLON_NATIVE_PLUGIN_NATIVEENGINE_WEBP is forced OFF (libwebp is not fetched/built).
  • All <bimg/...>, <bx/...>, <stb/...>, <webp/...> includes are guarded out.
  • All bimg-using helpers (Cast, TransformImage, GetPixelMapper, ReorientImage, ParseImage, PrepareImage, LoadTextureFromImage, LoadCubeTextureFromImages, related static_asserts) are excluded from compilation.
  • The following NativeEngine instance methods stay registered but throw a Napi::Error ("Image loading is disabled in this build (BABYLON_NATIVE_DISABLE_IMAGE_LOADING).") when invoked:
    • loadTexture
    • loadRawTexture
    • loadRawTexture2DArray
    • loadCubeTexture
    • loadCubeTextureWithMips
    • createImageBitmap
    • resizeImageBitmap
  • ReadTexture's bimg-based format-conversion path also throws; raw readback when source/target formats match continues to work.
  • FlipImage is kept available because it's also used by ReadTexture.

Binary size impact

Measured on Windows x64 Release Playground.exe (MSVC, default options):

Build Playground.exe
Image loading ON (default) 3,610,112 bytes
Image loading OFF 3,152,384 bytes
Saved 457,728 bytes (~447 KiB, ~12.7%)

How to use

cmake -B buildNoImage -A x64 -BABYLON_NATIVE_PLUGIN_NATIVEENGINE_LOAD_IMAGES=OFF
cmake --build buildNoImage --target Playground --config Release

When enabled, this option strips all bimg/bx/stb/webp image-decoding code
from the NativeEngine plugin:
- bimg, bimg_encode, bimg_decode are no longer linked
- libwebp build is forced off
- All bimg includes and helpers (ParseImage, PrepareImage,
  LoadTextureFromImage, LoadCubeTextureFromImages, etc.) are guarded out
- LoadTexture, LoadRawTexture, LoadRawTexture2DArray, LoadCubeTexture,
  LoadCubeTextureWithMips, CreateImageBitmap, ResizeImageBitmap, and the
  format-conversion path of ReadTexture throw a Napi::Error at runtime

Useful for embedders that supply their own decoded textures via
LoadRawTexture-equivalent native paths and want to reduce binary size.

Measured on Windows x64 Release Playground.exe:
  Image loading ON  : 3,610,112 bytes
  Image loading OFF : 3,330,048 bytes
  Saved             :   280,064 bytes (~7.8%)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 27, 2026 17:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in CMake build flag to compile the NativeEngine plugin without image decoding support (bimg/stb/WebP code paths), reducing binary size for embedders that handle texture decoding externally.

Changes:

  • Introduces BABYLON_NATIVE_DISABLE_IMAGE_LOADING top-level CMake option (default OFF) and forces BABYLON_NATIVE_PLUGIN_NATIVEENGINE_WEBP=OFF when enabled.
  • Guards bimg/stb/WebP includes and helper implementations in NativeEngine and makes image-loading-related NAPI entrypoints throw when invoked in a disabled build.
  • Stops explicitly linking bimg* into NativeEngine when the option is enabled.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
Plugins/NativeEngine/Source/NativeEngine.h Guards bimg/bx includes behind BABYLON_NATIVE_DISABLE_IMAGE_LOADING.
Plugins/NativeEngine/Source/NativeEngine.cpp Guards image-loading implementation; keeps APIs but throws when disabled; disables format conversion path when disabled.
Plugins/NativeEngine/CMakeLists.txt Conditionally links bimg* and adds the compile definition when image loading is disabled.
CMakeLists.txt Adds the new option and forces WebP support off when image loading is disabled.

Comment thread Plugins/NativeEngine/Source/NativeEngine.cpp Outdated
Comment thread Plugins/NativeEngine/Source/NativeEngine.cpp Outdated
Comment thread Plugins/NativeEngine/CMakeLists.txt Outdated
CedricGuillemet and others added 2 commits April 27, 2026 20:03
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Comment thread CMakeLists.txt Outdated
Comment thread Plugins/TestUtils/CMakeLists.txt Outdated
@CedricGuillemet CedricGuillemet merged commit 58ae8bf into BabylonJS:master Apr 28, 2026
28 checks passed
@CedricGuillemet CedricGuillemet changed the title Add BABYLON_NATIVE_DISABLE_IMAGE_LOADING CMake option Add BABYLON_NATIVE_PLUGIN_NATIVEENGINE_LOAD_IMAGES CMake option Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants