Skip to content

Commit 581de14

Browse files
authored
Add WebP support (#1512)
- Added a new dependency on libwebp, updated NOTICES, etc. - Added new CMake option to remove webp support as this dependency costs ~200KB to the final disk footprint. - Updated some CMake variables in dependencies to use local variables instead of cached variables. - Added a test for a glTF with WebP textures coming from the Babylon.js side.
1 parent 720b77a commit 581de14

7 files changed

Lines changed: 133 additions & 38 deletions

File tree

940 KB
Loading

Apps/Playground/Scripts/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
"playgroundId": "#2FDQT5#1508",
5353
"referenceImage": "iridescence_gltf.png"
5454
},
55+
{
56+
"title": "EXT_texture_webp",
57+
"playgroundId": "#LSAUH2#6",
58+
"referenceImage": "webp.png"
59+
},
5560
{
5661
"title": "GLTF Animation Skin Type",
5762
"playgroundId": "#DS8AA7#27",

CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,27 @@ FetchContent_Declare(OpenXR-SDK
4747
FetchContent_Declare(SPIRV-Cross
4848
GIT_REPOSITORY https://github.com/BabylonJS/SPIRV-Cross.git
4949
GIT_TAG 578a291759db6fe7c3f4735d3512c0526ad18efc)
50+
FetchContent_Declare(libwebp
51+
GIT_REPOSITORY https://github.com/webmproject/libwebp.git
52+
GIT_TAG 57e324e2eb99be46df46d77b65705e34a7ae616c)
5053
# --------------------------------------------------
5154

5255
FetchContent_MakeAvailable(CMakeExtensions)
5356

54-
5557
if(VISIONOS)
5658
FetchContent_MakeAvailable_With_Message(ios-cmake)
5759
set(CMAKE_TOOLCHAIN_FILE "${ios-cmake_SOURCE_DIR}/ios.toolchain.cmake" CACHE PATH "")
5860
set(PLATFORM "VISIONOSCOMBINED" CACHE STRING "")
5961
set(DEPLOYMENT_TARGET "1.0" CACHE STRING "")
6062
set(ENABLE_ARC OFF CACHE STRING "Enables or disables ARC support.")
61-
endif()
62-
63-
if(IOS)
63+
elseif(IOS)
6464
FetchContent_MakeAvailable_With_Message(ios-cmake)
6565
set(CMAKE_TOOLCHAIN_FILE "${ios-cmake_SOURCE_DIR}/ios.toolchain.cmake" CACHE PATH "")
6666
set(PLATFORM "OS64COMBINED" CACHE STRING "")
6767
set(DEPLOYMENT_TARGET "13" CACHE STRING "")
6868
set(ENABLE_ARC OFF CACHE STRING "Enables or disables ARC support.")
6969
endif()
7070

71-
7271
project(BabylonNative)
7372

7473
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
@@ -93,6 +92,7 @@ option(BABYLON_NATIVE_PLUGIN_EXTERNALTEXTURE "Include Babylon Native Plugin Exte
9392
option(BABYLON_NATIVE_PLUGIN_NATIVECAMERA "Include Babylon Native Plugin NativeCamera." ON)
9493
option(BABYLON_NATIVE_PLUGIN_NATIVECAPTURE "Include Babylon Native Plugin NativeCapture." ON)
9594
option(BABYLON_NATIVE_PLUGIN_NATIVEENGINE "Include Babylon Native Plugin NativeEngine." ON)
95+
option(BABYLON_NATIVE_PLUGIN_NATIVEENGINE_WEBP "Include Babylon Native Plugin NativeEngine - WebP." ON)
9696
option(BABYLON_NATIVE_PLUGIN_NATIVEINPUT "Include Babylon Native Plugin NativeInput." ON)
9797
option(BABYLON_NATIVE_PLUGIN_NATIVEOPTIMIZATIONS "Include Babylon Native Plugin NativeOptimizations." ON)
9898
option(BABYLON_NATIVE_PLUGIN_NATIVETRACING "Include Babylon Native Plugin NativeTracing." ON)

Dependencies/CMakeLists.txt

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
2+
13
# --------------------------------------------------
24
# AndroidExtensions
35
# --------------------------------------------------
@@ -28,12 +30,12 @@ target_include_directories(base-n INTERFACE "${base-n_SOURCE_DIR}/include")
2830
# --------------------------------------------------
2931
# bgfx.cmake
3032
# --------------------------------------------------
31-
set(BGFX_BUILD_TOOLS OFF CACHE BOOL "")
32-
set(BGFX_BUILD_EXAMPLES OFF CACHE BOOL "")
33-
set(BGFX_INSTALL OFF CACHE BOOL "")
34-
set(BGFX_CUSTOM_TARGETS OFF CACHE BOOL "")
35-
set(BGFX_USE_DEBUG_SUFFIX OFF CACHE BOOL "")
36-
set(BGFX_OPENGL_USE_EGL ON CACHE BOOL "")
33+
set(BGFX_BUILD_TOOLS OFF)
34+
set(BGFX_BUILD_EXAMPLES OFF)
35+
set(BGFX_INSTALL OFF)
36+
set(BGFX_CUSTOM_TARGETS OFF)
37+
set(BGFX_USE_DEBUG_SUFFIX OFF)
38+
set(BGFX_OPENGL_USE_EGL ON)
3739
FetchContent_MakeAvailable_With_Message(bgfx.cmake)
3840

3941
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MULTITHREADED=1)
@@ -78,15 +80,15 @@ endif()
7880
# --------------------------------------------------
7981
# glslang
8082
# --------------------------------------------------
81-
set(BUILD_EXTERNAL OFF CACHE BOOL "")
82-
set(ENABLE_SPVREMAPPER OFF CACHE BOOL "")
83-
set(ENABLE_GLSLANG_BINARIES OFF CACHE BOOL "")
84-
set(ENABLE_HLSL OFF CACHE BOOL "")
85-
set(ENABLE_OPT OFF CACHE BOOL "")
86-
set(ENABLE_CTEST OFF CACHE BOOL "")
87-
set(SKIP_GLSLANG_INSTALL ON CACHE BOOL "")
88-
set(ENABLE_GLSLANG_WEBMIN ON CACHE BOOL "")
89-
set(ENABLE_GLSLANG_WEBMIN_DEVEL OFF CACHE BOOL "")
83+
set(BUILD_EXTERNAL OFF)
84+
set(ENABLE_SPVREMAPPER OFF)
85+
set(ENABLE_GLSLANG_BINARIES OFF)
86+
set(ENABLE_HLSL OFF)
87+
set(ENABLE_OPT OFF)
88+
set(ENABLE_CTEST OFF)
89+
set(SKIP_GLSLANG_INSTALL ON)
90+
set(ENABLE_GLSLANG_WEBMIN ON)
91+
set(ENABLE_GLSLANG_WEBMIN_DEVEL OFF)
9092

9193
if(NOT TARGET glslang)
9294
FetchContent_MakeAvailable_With_Message(glslang)
@@ -109,7 +111,7 @@ if(BABYLON_NATIVE_BUILD_APPS AND (WIN32 OR (APPLE AND NOT IOS AND NOT VISIONOS)
109111
# Default build type for my test projects are /MDd (MultiThreaded DLL) but GTests default to /MTd (MultiTreaded)
110112
# see https://github.com/google/googletest/blob/main/googletest/README.md
111113
# "Enabling this option will make gtest link the runtimes dynamically too, and match the project in which it is included."
112-
set(gtest_force_shared_crt OFF CACHE BOOL "" FORCE)
114+
set(gtest_force_shared_crt OFF)
113115
endif()
114116

115117
FetchContent_MakeAvailable_With_Message(googletest)
@@ -138,22 +140,22 @@ endif()
138140
# --------------------------------------------------
139141
# SPIRV-Cross
140142
# --------------------------------------------------
141-
set(SPIRV_CROSS_CLI OFF CACHE BOOL "")
142-
set(SPIRV_CROSS_ENABLE_TESTS OFF CACHE BOOL "")
143-
set(SPIRV_CROSS_ENABLE_CPP OFF CACHE BOOL "")
144-
set(SPIRV_CROSS_ENABLE_REFLECT OFF CACHE BOOL "")
145-
set(SPIRV_CROSS_ENABLE_C_API OFF CACHE BOOL "")
146-
set(SPIRV_CROSS_ENABLE_UTIL OFF CACHE BOOL "")
147-
set(SPIRV_CROSS_SKIP_INSTALL ON CACHE BOOL "")
148-
set(SPIRV_CROSS_ENABLE_WEBMIN ON CACHE BOOL "")
143+
set(SPIRV_CROSS_CLI OFF)
144+
set(SPIRV_CROSS_ENABLE_TESTS OFF)
145+
set(SPIRV_CROSS_ENABLE_CPP OFF)
146+
set(SPIRV_CROSS_ENABLE_REFLECT OFF)
147+
set(SPIRV_CROSS_ENABLE_C_API OFF)
148+
set(SPIRV_CROSS_ENABLE_UTIL OFF)
149+
set(SPIRV_CROSS_SKIP_INSTALL ON)
150+
set(SPIRV_CROSS_ENABLE_WEBMIN ON)
149151
if(NOT GRAPHICS_API STREQUAL "OpenGL")
150-
set(SPIRV_CROSS_ENABLE_GLSL OFF CACHE BOOL "")
152+
set(SPIRV_CROSS_ENABLE_GLSL OFF)
151153
endif()
152154
if(NOT GRAPHICS_API STREQUAL "Metal")
153-
set(SPIRV_CROSS_ENABLE_MSL OFF CACHE BOOL "")
155+
set(SPIRV_CROSS_ENABLE_MSL OFF)
154156
endif()
155157
if(NOT GRAPHICS_API STREQUAL "D3D11" AND NOT GRAPHICS_API STREQUAL "D3D12")
156-
set(SPIRV_CROSS_ENABLE_HLSL OFF CACHE BOOL "")
158+
set(SPIRV_CROSS_ENABLE_HLSL OFF)
157159
endif()
158160
FetchContent_MakeAvailable_With_Message(SPIRV-Cross)
159161
set_property(TARGET spirv-cross-core PROPERTY FOLDER Dependencies/SPIRV-Cross)
@@ -176,7 +178,7 @@ foreach(target spirv-cross-core spirv-cross-glsl spirv-cross-msl spirv-cross-hls
176178
endif()
177179
endforeach()
178180

179-
# seen with Sergio, disable warnings until unused parameter [-Wunused-parameter] get fixed.
181+
# Disable warnings until unused parameter [-Wunused-parameter] get fixed.
180182
if(TARGET SPIRV)
181183
disable_warnings(SPIRV)
182184
endif()
@@ -194,6 +196,35 @@ if(BABYLON_NATIVE_PLUGIN_NATIVEXR AND (WIN32 OR ANDROID OR IOS))
194196
warnings_as_errors(xr)
195197
endif()
196198

199+
# --------------------------------------------------
200+
# WebP
201+
# --------------------------------------------------
202+
if(BABYLON_NATIVE_PLUGIN_NATIVEENGINE_WEBP)
203+
set(WEBP_BUILD_ANIM_UTILS OFF)
204+
set(WEBP_BUILD_CWEBP OFF)
205+
set(WEBP_BUILD_DWEBP OFF)
206+
set(WEBP_BUILD_GIF2WEBP OFF)
207+
set(WEBP_BUILD_IMG2WEBP OFF)
208+
set(WEBP_BUILD_VWEBP OFF)
209+
set(WEBP_BUILD_WEBPINFO OFF)
210+
set(WEBP_BUILD_LIBWEBPMUX OFF)
211+
set(WEBP_BUILD_WEBPMUX OFF)
212+
set(WEBP_BUILD_EXTRAS OFF)
213+
FetchContent_MakeAvailable_With_Message(libwebp)
214+
215+
set_property(TARGET sharpyuv PROPERTY UNITY_BUILD false)
216+
set_property(TARGET sharpyuv PROPERTY FOLDER Dependencies/libwebp)
217+
set_property(TARGET webp PROPERTY FOLDER Dependencies/libwebp)
218+
set_property(TARGET webpdecode PROPERTY FOLDER Dependencies/libwebp)
219+
set_property(TARGET webpdecoder PROPERTY FOLDER Dependencies/libwebp)
220+
set_property(TARGET webpdemux PROPERTY FOLDER Dependencies/libwebp)
221+
set_property(TARGET webpdsp PROPERTY FOLDER Dependencies/libwebp)
222+
set_property(TARGET webpdspdecode PROPERTY FOLDER Dependencies/libwebp)
223+
set_property(TARGET webpencode PROPERTY FOLDER Dependencies/libwebp)
224+
set_property(TARGET webputils PROPERTY FOLDER Dependencies/libwebp)
225+
set_property(TARGET webputilsdecode PROPERTY FOLDER Dependencies/libwebp)
226+
endif()
227+
197228
# --------------------------------------------------
198229
# WindowsAppSDK
199230
# --------------------------------------------------

NOTICE.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,40 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
416416
\\****************************************************************************/
417417
```
418418

419+
# libwebp
420+
421+
```
422+
Copyright (c) 2010, Google Inc. All rights reserved.
423+
424+
Redistribution and use in source and binary forms, with or without
425+
modification, are permitted provided that the following conditions are
426+
met:
427+
428+
* Redistributions of source code must retain the above copyright
429+
notice, this list of conditions and the following disclaimer.
430+
431+
* Redistributions in binary form must reproduce the above copyright
432+
notice, this list of conditions and the following disclaimer in
433+
the documentation and/or other materials provided with the
434+
distribution.
435+
436+
* Neither the name of Google nor the names of its contributors may
437+
be used to endorse or promote products derived from this software
438+
without specific prior written permission.
439+
440+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
441+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
442+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
443+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
444+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
445+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
446+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
447+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
448+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
449+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
450+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
451+
```
452+
419453
# Node.js
420454

421455
```

Plugins/NativeEngine/CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ endif()
2929

3030
add_library(NativeEngine ${SOURCES})
3131

32+
warnings_as_errors(NativeEngine)
33+
3234
target_include_directories(NativeEngine
3335
PUBLIC "Include"
3436
PRIVATE "${BIMG_DIR}/3rdparty")
3537

3638
target_link_libraries(NativeEngine
3739
PUBLIC napi
38-
PRIVATE JsRuntime
39-
PRIVATE GraphicsDevice
4040
PRIVATE arcana
4141
PRIVATE bgfx
4242
PRIVATE bimg
@@ -45,9 +45,17 @@ target_link_libraries(NativeEngine
4545
PRIVATE bx
4646
PRIVATE glslang
4747
PRIVATE glslang-default-resource-limits
48-
PRIVATE SPIRV
49-
PRIVATE GraphicsDeviceContext)
50-
warnings_as_errors(NativeEngine)
48+
PRIVATE GraphicsDevice
49+
PRIVATE GraphicsDeviceContext
50+
PRIVATE JsRuntime
51+
PRIVATE SPIRV)
52+
53+
if(BABYLON_NATIVE_PLUGIN_NATIVEENGINE_WEBP)
54+
target_compile_definitions(NativeEngine
55+
PRIVATE WEBP)
56+
target_link_libraries(NativeEngine
57+
PRIVATE webp)
58+
endif()
5159

5260
if(TARGET spirv-cross-hlsl)
5361
target_link_libraries(NativeEngine

Plugins/NativeEngine/Source/NativeEngine.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <Babylon/ShaderCache.h>
2626
#include "ShaderCache.h"
2727

28+
#ifdef WEBP
29+
#include <webp/decode.h>
30+
#endif
31+
2832
namespace Babylon
2933
{
3034
namespace
@@ -186,6 +190,19 @@ namespace Babylon
186190
bimg::ImageContainer* image{bimg::imageParse(&allocator, data.data(), static_cast<uint32_t>(data.size()))};
187191
if (image == nullptr)
188192
{
193+
#ifdef WEBP
194+
int width;
195+
int height;
196+
if (WebPGetInfo(data.data(), data.size(), &width, &height))
197+
{
198+
image = bimg::imageAlloc(&allocator, bimg::TextureFormat::RGBA8, static_cast<uint16_t>(width), static_cast<uint16_t>(height), 1, 1, false, false);
199+
if (WebPDecodeRGBAInto(data.data(), data.size(), static_cast<uint8_t*>(image->m_data), static_cast<size_t>(image->m_size), width * 4))
200+
{
201+
return image;
202+
}
203+
}
204+
#endif
205+
189206
throw std::runtime_error{"Failed to parse image."};
190207
}
191208

0 commit comments

Comments
 (0)