Skip to content
Open
Show file tree
Hide file tree
Changes from 128 commits
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
7ee37ec
Updating gitignore for builds
scotbrew Apr 4, 2025
2b06d58
Expand on Javascript wrapping of USD classes
scotbrew Apr 4, 2025
c96d5ea
Update minimum cmake version to 3.5
scotbrew Apr 4, 2025
21bf154
copy pxr/usdImaging/bin/usdviewweb/ to pxr/extras/usd/web
scotbrew Apr 4, 2025
6439a6f
Tailoring usdweb from usdviewweb source
scotbrew Apr 4, 2025
a6b02eb
Adding "web" dir to Cmake, tweaking usdweb.html to usdweb.js
scotbrew Apr 4, 2025
23d8327
Update .gitignore
scotbrew Apr 4, 2025
7812bb2
Expose USD viewport functions to javascript. Generalize code. Remov…
scotbrew Apr 4, 2025
93431c6
Remove unused usda file
scotbrew Apr 4, 2025
3987df1
Revise Usdweb api calls
scotbrew Apr 4, 2025
89882d5
uncommenting main() so will auto init on page load
scotbrew Apr 7, 2025
41a6b71
Add openStageFromUsdfile() function
scotbrew Apr 7, 2025
3ed41c1
Expose RenderParams.frame for SetTime()
scotbrew Apr 7, 2025
b452df5
Added sample actions to load models and hide prims.
scotbrew Apr 9, 2025
698daac
Add animation with arrow keys. Expose Time API needed.
scotbrew Apr 9, 2025
40732c7
Expose UsdUtilsExtractExternalReferences
scotbrew Apr 10, 2025
059f02f
Use cleaner Vector to JS Array as done in other areas of the code.
scotbrew Apr 10, 2025
231403e
WrapJs for VariantSet and VariantSets
scotbrew Apr 11, 2025
928e99a
Wrapping UsdObject, parent of UsdPrim
scotbrew Apr 13, 2025
6a84dfc
Exposing UsdwebFitCamera (for stage and specific prims)
scotbrew Apr 13, 2025
521ef09
Better file loading and new action buttons
scotbrew Apr 13, 2025
147fe3c
Switch from Camera to FreeCameraGL
scotbrew Apr 16, 2025
8edbcf3
Remove now unneeded function getStageBounds() in usdweb.cpp
scotbrew Apr 16, 2025
0bcd224
Remove camera.h/cpp, as replaced by freeCamera.h/cpp
scotbrew Apr 16, 2025
1a2bb85
wrap UsdGeomCamera
scotbrew Apr 16, 2025
cf343e5
Support Z-Up for camera stages
scotbrew Apr 16, 2025
b73ab35
tweak comments for Zoom()
scotbrew Apr 16, 2025
93b398d
Adjust FOV and Zoom factor
scotbrew Apr 16, 2025
fd61873
Removing unused code
scotbrew Apr 16, 2025
2af07bd
Tweak comments
scotbrew Apr 17, 2025
4b2f31f
Clean up default camera lighting.
scotbrew Apr 17, 2025
b581629
Add Standard ShaderBall model as option
scotbrew Apr 17, 2025
1b806a6
Add Shader Ball material variants
scotbrew Apr 17, 2025
66d6449
Remove unused #defines
scotbrew Apr 18, 2025
b4fa2ad
Add in basic_materialx.usdz and UVGrid buttons.
scotbrew Apr 18, 2025
f78f573
Add freeCamera look through stage camera prims
scotbrew Apr 27, 2025
2bd3747
Add download function.
scotbrew Apr 27, 2025
d642927
Enhance javascript wrapping for UsdGeomXformable
scotbrew Apr 27, 2025
5a2f878
For Javascript, add standard ::Get() for USD Ops and Attrs
scotbrew Apr 27, 2025
1f437e8
Expose to Javascript, UsdGeomXformOp::GetOpName()
scotbrew Apr 27, 2025
e5adc4d
Add UsdTimeCode, hook up Get/Set() to use it.
scotbrew Apr 28, 2025
fa440e4
Add UsdTimeCode and explicit coersion to double
scotbrew Apr 28, 2025
906fd68
Javascript update UsdShader, UsdShadeInput, UsdShadeOutput
scotbrew Apr 29, 2025
ad39573
Add Javascript UsdAttribute::Clear() and GetAttr() for UsdXformOp and…
scotbrew Apr 29, 2025
1cd974d
Added lighting, rendering, cameras. Expose to javascript.
scotbrew Apr 30, 2025
b5d43f0
prototype MaterialX integration (WIP)
scotbrew May 1, 2025
e20e4f0
Fix merge issue from previous checkin.
scotbrew May 1, 2025
d4c4a51
HACK: use string replace for shader code to fix MaterialX LightData.t…
scotbrew May 3, 2025
6af088b
Remove duplicate flag setting added in previous checkin
scotbrew May 3, 2025
e4d2839
Add test grid scenes
scotbrew May 3, 2025
2e2161f
Add additional test scenes
scotbrew May 3, 2025
0afa643
Make Standard ShaderBall material variants dynamic.
scotbrew May 3, 2025
d18f430
Checkboxes for lighting.
scotbrew May 3, 2025
fc7231e
Unrolling remaining functions that takes a sampler2D parameter.
scotbrew May 4, 2025
2d71ecf
Updating .gitignore
scotbrew May 7, 2025
28c3f64
Adding new test scene.
scotbrew May 7, 2025
8caac75
MaterialX: shaderCode string replace sampler2D -> texture2D, sampler
scotbrew May 10, 2025
cacae38
For debugging, writing out the resulting GLSL shader code.
scotbrew May 15, 2025
af762d9
Update build script for MaterialX.
scotbrew May 25, 2025
fe7d6c8
Update USD for MaterailX 1.39+
scotbrew May 25, 2025
09389c2
Refactoring HdStMaterialXShaderGen classes
scotbrew May 25, 2025
a4d4c4d
MaterialX WGSL running in USD
scotbrew May 27, 2025
45d254e
Code cleanup
scotbrew May 27, 2025
be48030
Build Code cleanup
scotbrew May 27, 2025
4106bbd
Code cleanup. Build success with standard build.
scotbrew May 27, 2025
33b6901
Code cleanup
scotbrew May 27, 2025
ff4cf1b
Tweak .gitignore
scotbrew May 27, 2025
85dc2f9
Build Cleanup
scotbrew May 27, 2025
4a23182
JavaScript Wrap: TfDebug
scotbrew May 27, 2025
273792f
Reorder env settings to make more readable
scotbrew May 28, 2025
eea60e7
Code cleanup
scotbrew May 28, 2025
03e8893
Code cleanup
scotbrew May 28, 2025
9056582
Tweaking debug output.
scotbrew May 28, 2025
7c1bbea
UsdWeb: Adding debugCodes. Cleaning up build.
scotbrew May 28, 2025
fd23469
Code cleanup.
scotbrew May 28, 2025
89f4c62
Use precise version for mx::TextureBakerGlslPtr change.
scotbrew May 28, 2025
ae34c23
Syntax tweak to match version convention
scotbrew May 28, 2025
aa34fee
Cleanup and enhance usdweb.html
scotbrew May 28, 2025
db2352d
Usdweb html: Reorganizing sample USD scenes
scotbrew May 28, 2025
eeac7fb
Usdweb html: FitCamera on rear wheel button
scotbrew May 28, 2025
4c1d77d
Fix MaterialX version check logic
scotbrew May 29, 2025
72b95fd
Add additional assets.
scotbrew May 29, 2025
0c3db2f
Modernize and fix MaterialX version #if checks.
scotbrew May 29, 2025
41f0c4a
Align MaterialX version check syntax with dev branch
scotbrew May 30, 2025
8195518
Update build_usd.py
scotbrew May 30, 2025
f945a2c
Add a note in the CMakeLists for review later
scotbrew May 30, 2025
f6f40f5
Move file ignore out of .gitignore
scotbrew May 30, 2025
4e170e4
Update js/.gitignore with comments to revisit and refactor
scotbrew May 30, 2025
4650821
Merge branch 'brews/feature/wasm/materialx' of https://github.com/sco…
scotbrew May 30, 2025
ccb8154
Revise CMake Options for Wasm
scotbrew May 30, 2025
2594fa0
Implement MaterialX version check in same place as dev branch
scotbrew May 30, 2025
c466916
materialXShaderGen: Integrate updates from dev branch
scotbrew May 30, 2025
08441d4
usdweb: Streamlining and cleanup.
scotbrew May 30, 2025
db33900
Updating usdweb assets and paths.
scotbrew May 31, 2025
12e4a87
Adding icon to usdweb.html
scotbrew May 31, 2025
5f53b23
Added docs describing html/emscripten/webgpu workings
scotbrew May 31, 2025
711cb22
tweak docs
scotbrew May 31, 2025
173b2c8
Add CMake docs
scotbrew May 31, 2025
ce4e4fc
Fixed monitorRunDependencies logic
scotbrew May 31, 2025
99a0f82
tweak output
scotbrew May 31, 2025
022ec9f
Add comments and refactor usdweb glfw
scotbrew Jun 1, 2025
4e52f15
clean up
scotbrew Jun 1, 2025
f0fbf5f
cleanup
scotbrew Jun 1, 2025
93b541a
Restored to start where it is working again.
scotbrew Jun 1, 2025
d97d336
Cleanup
scotbrew Jun 1, 2025
6e8b050
Add contrib.glfw3 CMake option
scotbrew Jun 2, 2025
a181204
Fix spacing.
scotbrew Jun 3, 2025
f8c02d6
Fix spacing
scotbrew Jun 3, 2025
ce0cf55
Add comments, WIP framework for EMSCRIPTEN_USE_CONTRIB_GLFW3
scotbrew Jun 3, 2025
25321b1
Updating README.md
scotbrew Jun 5, 2025
1f1c637
Update README.md
scotbrew Jun 5, 2025
3ad6035
Update README.md
scotbrew Jun 5, 2025
35c90ce
Update README.md
scotbrew Jun 5, 2025
d7833cf
Update README.md
scotbrew Jun 5, 2025
3fe7348
Update README.md
scotbrew Jun 5, 2025
7a00448
Update README.md
scotbrew Jun 5, 2025
3555e4e
Update README.md
scotbrew Jun 5, 2025
18a7d2e
Update README.md
scotbrew Jun 5, 2025
85b5c64
Update README.md
scotbrew Jun 5, 2025
7400347
Update README.md
scotbrew Jun 5, 2025
4fadb52
Update README.md
scotbrew Jun 5, 2025
723a81e
MaterialX shader limitation handled -- updating docs
scotbrew Jun 8, 2025
75ae865
Update build scripts for WGSL
scotbrew Jun 9, 2025
4638054
For WGSL, directly use sampler vars vs tokens
scotbrew Jul 5, 2025
fd4467c
Switch to prototype MaterialX branch for targetWasm
scotbrew Jul 5, 2025
3df1828
Merge branch 'adsk/feature/wasm' into brews/feature/wasm/materialx
scotbrew Jul 8, 2025
74ab04e
Syntax fix to previous merge
scotbrew Jul 8, 2025
c2748c6
Switch to Dawn webgpu_cpp header
scotbrew Jul 9, 2025
f58f556
Adding documentation to README.md
scotbrew Sep 4, 2025
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ if (PXR_BUILD_DOCUMENTATION)
endif()
endif()

pxr_toplevel_epilogue()
pxr_toplevel_epilogue()
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ It reuses the existing shaders that are converted to SPIR-V by [Shaderc](https:/
and then WGSL by [Tint](https://dawn.googlesource.com/tint), finally consumed by a browser with WebGPU feature
enabled.

Features:
- **MaterialX** supported with the WebGPU HdStorm renderer as of 1.39.4 (?).

#### Getting Started with HgiWebGPU

As we worked on a WebGPU based backend, we developed 2 solutions for different scenarios according
Expand All @@ -31,10 +34,15 @@ export HDX_ENABLE_OIT=0
export HGI_ENABLE_WEBGPU=1
```

- **Wasm-based**: It is used for rendering and viewing on a web browser.
**WebAssembly (Wasm) Examples:**
- **usdviewweb** : Used for rendering and viewing on a web browser.
The WebAssembly content can be tested using [usdviewweb](./pxr/usdImaging/bin/usdviewweb/README.md). For running an example on the browser, please follow the instructions [under usdviewweb folder](./pxr/usdImaging/bin/usdviewweb/README.md).
Please build USD with `--build-target wasm` option.

- **usdweb** : Example using HdStorm rendering controlled by JavaScript USD commands
in a web browser. See [extras/usd/web/README.md](./extras/usd/web/README.md) for more information.


**Limitations**

- OIT is disabled due to missing support of GLSL `atomicAdd` and binding of a
Expand Down
61 changes: 45 additions & 16 deletions build_scripts/build_usd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,22 +1638,45 @@ def InstallDraco(context, force, buildArgs):
MATERIALX_URL = "https://github.com/AcademySoftwareFoundation/MaterialX/archive/v1.39.3.zip"

def InstallMaterialX(context, force, buildArgs):
if context.targetWasm:
# For Wasm WebGPU/WGSL shader generation for MaterialX,
# MaterialX as of 6/9/2025 needs to be used with the
# "WGSL/WebGPU GLSL ShaderGenerator" pull request integrated.
# The full MaterialX release with this enhancement should be
# the pending version 1.39.4.
global MATERIALX_URL
#MATERIALX_URL = 'https://github.com/AcademySoftwareFoundation/MaterialX/archive/8eaf80cf7b6a27c1a7b3eeb8c9bc8872fddb4d60.zip'
MATERIALX_URL = 'https://github.com/scotbrew/MaterialX/archive/refs/heads/brews/feature/wgsl_texsampler_code_replace.zip'
with CurrentWorkingDirectory(DownloadURL(MATERIALX_URL, context, force)):
cmakeOptions = ['-DMATERIALX_BUILD_SHARED_LIBS=ON',
'-DMATERIALX_BUILD_TESTS=OFF'
'-DMATERIALX_BUILD_TESTS=OFF',
]

if context.targetWasm:
cmakeOptions.extend([
'-DCMAKE_CXX_FLAGS="' + EMSCRIPTEN_CMAKE_CXX_FLAGS + '"',
'-DCMAKE_C_FLAGS="'+ EMSCRIPTEN_CMAKE_CXX_FLAGS + ' "',
'-DCMAKE_EXE_LINKER_FLAGS="' + EMSCRIPTEN_CMAKE_EXE_LINKER_FLAGS + '"',
# For Wasm
'-DCMAKE_CXX_FLAGS="' + EMSCRIPTEN_CMAKE_CXX_FLAGS + '"', # removed: + ' -s SIDE_MODULE=1"',
'-DCMAKE_C_FLAGS="' + EMSCRIPTEN_CMAKE_CXX_FLAGS + '"', # removed: + ' -s SIDE_MODULE=1"',
'-DCMAKE_EXE_LINKER_FLAGS="' + EMSCRIPTEN_CMAKE_EXE_LINKER_FLAGS + ' -s MAIN_MODULE=1"',
'-DBUILD_SHARED_LIBS=OFF',
# MaterialX config
'-DMATERIALX_BUILD_JS=OFF', # We don't need the js bindings
'-DMATERIALX_BUILD_TESTS=OFF',
'-DMATERIALX_BUILD_RENDER=ON',
'-DMATERIALX_BUILD_RENDER_PLATFORMS=OFF',
'-DMATERIALX_BUILD_GEN_OSL=OFF',
'-DMATERIALX_BUILD_GEN_MDL=OFF'])
'-DMATERIALX_BUILD_GEN_MDL=OFF',
'-DMATERIALX_BUILD_GEN_GLSL=ON',
'-DMATERIALX_BUILD_GEN_MSL=OFF',
])
# MaterialX/source/MaterialXRenderGlsl/CMakeLists.txt will error if compiling
# on MacOS and APPLE=ON is not set.
# Consider change to MaterialX to move find_package(OpenGL REQUIRED) outside of the platform
# if(APPLE/UNIX) block
# Note: USD requires -DMATERIALX_BUILD_RENDER=ON tools for materialXFilter.cpp
# so we cannot just disable that compiler flag.
if MacOS():
cmakeOptions.append('-DAPPLE=ON')


elif MacOSTargetEmbedded(context):
Expand Down Expand Up @@ -2164,6 +2187,9 @@ def InstallUSD(context, force, buildArgs):

if context.buildMaterialX:
extraArgs.append('-DPXR_ENABLE_MATERIALX_SUPPORT=ON')
if context.targetWasm:
# build is failing without this being set
extraArgs.append(f'-DMaterialX_DIR="{context.instDir}/lib/cmake/MaterialX"')
else:
extraArgs.append('-DPXR_ENABLE_MATERIALX_SUPPORT=OFF')

Expand All @@ -2190,6 +2216,7 @@ def InstallUSD(context, force, buildArgs):
extraArgs += buildArgs

if context.targetWasm:
extraArgs.append('-DPXR_ENABLE_METAL_SUPPORT=OFF')

if context.buildJsBindings:
extraArgs.append('-DPXR_ENABLE_JS_BINDINGS_SUPPORT=ON')
Expand Down Expand Up @@ -2394,15 +2421,6 @@ def InstallUSD(context, force, buildArgs):
help=("Enable code signing for macOS builds "
"(defaults to enabled on Apple Silicon)"))

group.add_argument("--webgpu", dest="buildWebGPU", action="store_true",
help="Build WebGPU Hgi")

subgroup = group.add_mutually_exclusive_group()
subgroup.add_argument("--js-bindings", dest="buildJsBindings", action="store_true",
default=True, help="Build with JavaScript bindings")
subgroup.add_argument("--no-js-bindings", dest="buildJsBindings", action="store_false",
help="Do not build with JavaScript bindings")

if Linux():
group.add_argument("--use-cxx11-abi", type=int, choices=[0, 1],
help=("Use C++11 ABI for libstdc++. (see docs above)"))
Expand Down Expand Up @@ -2632,6 +2650,15 @@ def InstallUSD(context, force, buildArgs):
dest="build_animx_tests", action="store_false",
help="Do not build AnimX spline tests (default)")

group = parser.add_argument_group(title="Web")
group.add_argument("--webgpu", dest="buildWebGPU", action="store_true",
help="Build WebGPU Hgi")
subgroup = group.add_mutually_exclusive_group()
subgroup.add_argument("--js-bindings", dest="buildJsBindings", action="store_true",
default=True, help="Build with JavaScript bindings")
subgroup.add_argument("--no-js-bindings", dest="buildJsBindings", action="store_false",
help="Do not build with JavaScript bindings")

args = parser.parse_args()

class InstallContext:
Expand Down Expand Up @@ -2712,7 +2739,7 @@ def __init__(self, args):

self.ignorePaths = args.ignore_paths or []
# Build target and code signing
self.targetWasm = (args.build_target == TARGET_WASM or args.build_target == TARGET_WASM_NODE or args.build_target == TARGET_WASM64)
self.targetWasm = ( args.build_target in (TARGET_WASM, TARGET_WASM_NODE, TARGET_WASM64) ) # bool
self.targetWasm64 = (args.build_target == TARGET_WASM64)

self.targetWasmNode = (args.build_target == TARGET_WASM_NODE)
Expand Down Expand Up @@ -3163,11 +3190,13 @@ def _JoinVersion(v):
AnimX Tests: {buildAnimXTests}
Examples {buildExamples}
Tutorials {buildTutorials}
WebGPU {buildWebGPU}
Tools {buildTools}
Alembic Plugin {buildAlembic}
HDF5 support: {enableHDF5}
Draco Plugin {buildDraco}
Web
Javascript Bindings {buildJsBindings}
WebGPU {buildWebGPU}

Dependencies {dependencies}"""

Expand Down
28 changes: 28 additions & 0 deletions cmake/defaults/Options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ option(PXR_BUILD_PYTHON_DOCUMENTATION "Generate Python documentation" OFF)
option(PXR_BUILD_HTML_DOCUMENTATION "Generate HTML documentation if PXR_BUILD_DOCUMENTATION is ON" ON)
option(PXR_ENABLE_PYTHON_SUPPORT "Enable Python based components for USD" ON)
option(PXR_ENABLE_JS_BINDINGS_SUPPORT "Enable Javascript bindings for USD" ON)
option(PXR_BUILD_JS_USDWEB "[Experimental] Build extras/usd/web " ON)
option(PXR_BUILD_JS_STANDALONE "[Experimental] Build extras/usd/js_bindings" ON)
option(PXR_BUILD_JS_USDVIEWWEB "[Experimental] Build pxr/usdImaging/bin/usdviewweb" ON)
if (NOT EMSCRIPTEN)
set(PXR_ENABLE_JS_BINDINGS_SUPPORT OFF)
endif()
Expand Down Expand Up @@ -187,6 +190,31 @@ message(STATUS
set(PXR_ENABLE_WEBGPU_SUPPORT "ON" CACHE BOOL "" FORCE)
endif()

if (${PXR_BUILD_JS_STANDALONE})
if (NOT ${PXR_ENABLE_JS_BINDINGS_SUPPORT})
message(STATUS
"Setting PXR_BUILD_JS_STANDALONE=OFF because "
"PXR_ENABLE_JS_BINDINGS_SUPPORT=OFF")
set(PXR_BUILD_JS_STANDALONE "OFF" CACHE BOOL "" FORCE)
endif()
endif()
if (${PXR_BUILD_JS_USDWEB})
if (NOT ${PXR_ENABLE_WEBGPU_SUPPORT})
message(STATUS
"Setting PXR_BUILD_JS_USDWEB=OFF because "
"PXR_ENABLE_WEBGPU_SUPPORT=OFF")
set(PXR_BUILD_JS_USDWEB "OFF" CACHE BOOL "" FORCE)
endif()
endif()
if (${PXR_BUILD_JS_USDVIEWWEB})
if (NOT ${PXR_ENABLE_WEBGPU_SUPPORT})
message(STATUS
"Setting PXR_BUILD_JS_USDVIEWWEB=OFF because "
"PXR_ENABLE_WEBGPU_SUPPORT=OFF")
set(PXR_BUILD_JS_USDVIEWWEB "OFF" CACHE BOOL "" FORCE)
endif()
endif()

if (${PXR_ENABLE_GL_SUPPORT} OR ${PXR_ENABLE_METAL_SUPPORT} OR ${PXR_ENABLE_VULKAN_SUPPORT} OR ${PXR_ENABLE_WEBGPU_SUPPORT})
set(PXR_BUILD_GPU_SUPPORT "ON")
else()
Expand Down
5 changes: 4 additions & 1 deletion extras/usd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ endif()
if (PXR_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
if (PXR_ENABLE_JS_BINDINGS_SUPPORT)
if (PXR_BUILD_JS_STANDALONE)
add_subdirectory(js_bindings)
endif()
if (PXR_BUILD_JS_USDWEB)
add_subdirectory(web)
endif()
5 changes: 5 additions & 0 deletions extras/usd/js_bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ install(
)
# Install files for npm package

# ================
# REVISIT: This install DESTINATION appears to be cluttering the source folders
# Resulting and intermediate files should go into the target build folders
# under ${CMAKE_INSTALL_PREFIX}
# ================
install(
FILES
${BUILD_FILES}
Expand Down
161 changes: 161 additions & 0 deletions extras/usd/web/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
set(PXR_PREFIX pxr/usdweb)
set(PXR_PACKAGE usdweb)

pxr_cpp_bin(${PXR_PACKAGE}
LIBRARIES
usdImagingGL
hdStorm
usdShaders
usdUtils
hdSt
usdMtlx
sdrGlslfx

INCLUDE_DIRS
${TBB_INCLUDE_DIRS}

PRIVATE_CLASSES
debugCodes
)

set(APP_CPPFILES
usdweb.cpp
freeCameraGL.cpp
)

set(ICON_FILES
USDLogoUnsized.svg
)

file(GLOB ASSET_FILES "assets/*.usd*" "assets/*.png" "assets/*.exr")

# ==============================
# EMSCRIPTEN_USE_CONTRIB_GLFW
# This feature is a WIP (work in progress)
# It can be enabled by adding -DEMSCRIPTEN_USE_CONTRIB_GLFW3=ON
#
# Sample build setup:
# build_scripts/build_usd.py --cmake-build-args "-DEMSCRIPTEN_USE_CONTRIB_GLFW3=ON"
#
# ==============================

if (EMSCRIPTEN)

# Determine which GLFW3 to use through Emscripten
if (EMSCRIPTEN_USE_CONTRIB_GLFW3)
set(EMSCRIPTEN_GLFW_BUILD_FLAGS "--use-port=contrib.glfw3 -DEMSCRIPTEN_USE_CONTRIB_GLFW3")
else()
set(EMSCRIPTEN_GLFW_BUILD_FLAGS "-s USE_GLFW=3")
endif()

list(APPEND RESOURCE_PARAMETERS "--preload-file ${PROJECT_BINARY_DIR}/plugins_plugInfo.json@/usd/plugInfo.json")
list(APPEND RESOURCE_PARAMETERS "--preload-file ${PROJECT_BINARY_DIR}/plugins_plugInfo.json@/plugin/usd/plugInfo.json")

# TODO: Add in MaterialX to the RESOURCE_PARAMETERS for target_link_options or target_link_libraries

# XXX: We manually set the libraries instead of using the LIBRARIES argument pxr_cpp_bin due to
# problems with the tbb library.
target_link_libraries(${PXR_PACKAGE}
${RESOURCE_PARAMETERS}
${TBB_tbb_LIBRARY}
)
set(BUILD_FILES
${CMAKE_CURRENT_BINARY_DIR}/${PXR_PACKAGE}.js
${CMAKE_CURRENT_BINARY_DIR}/${PXR_PACKAGE}.wasm
${CMAKE_CURRENT_BINARY_DIR}/${PXR_PACKAGE}.data
${CMAKE_CURRENT_SOURCE_DIR}/scripts/wasm-server.py
${CMAKE_CURRENT_SOURCE_DIR}/${PXR_PACKAGE}.html
)

set(CMAKE_EXE_LINKER_FLAGS_USDVIEWWEB " --tracing ${DEBUG_OPTIONS} \
${EMSCRIPTEN_GLFW_BUILD_FLAGS} -pthread \
-s ENVIRONMENT=web,worker \
-s MAXIMUM_MEMORY=4GB \
-s PTHREAD_POOL_SIZE=8 \
-s WASM_BIGINT \
-s NO_EXIT_RUNTIME=1 \
-s SHARED_MEMORY=1 \
")
if(PXR_WASM_NODE)
target_link_options(${PXR_PACKAGE} PRIVATE "SHELL:-s NODERAWFS=0")
endif ()
if (PXR_ENABLE_JS_BINDINGS_SUPPORT)
set(CMAKE_EXE_LINKER_FLAGS_USDVIEWWEB "${CMAKE_EXE_LINKER_FLAGS_USDVIEWWEB} --bind")
endif()

# GLFW OPTIONS
# -s USE_GLFW=3 This is the older Javascript version of GLFW, hardcoded to Module["canvas"]
# --use-port=contrib.glfw3 This is more modern, supports multiple windows, and is faster
# It is an Emscripten port of GLFW written in C++ for the web/wasm
# platform. It currently supports GLFW API 3.4.
# See https://github.com/pongasoft/emscripten-glfw
# It is referred to in https://emscripten.org/docs/compiling/Contrib-Ports.html
# If using --use-port=contrib.glfw3, consider:
# disableWebGL2
#
# Taking out: -s FULL_ES3 \
# -s USE_WEBGPU=1 \ (since now using Dawn)
target_link_options(${PXR_PACKAGE} PRIVATE "SHELL:${CMAKE_EXE_LINKER_FLAGS_USDVIEWWEB} \
${EMSCRIPTEN_GLFW_BUILD_FLAGS} -pthread \
-s MAX_WEBGL_VERSION=2 \
-O0 \
-s ALLOW_MEMORY_GROWTH \
")

# Add the following to the local web filesystem
# * /webgpu-env-settings
# * /libraries/ Required for MaterialX
#
# Note that -pthread automatically sets -mbulk-memory -matomics and SHARED_ARRAY_BUFFER
#
target_compile_options(${PXR_PACKAGE} PRIVATE "SHELL:${CMAKE_EXE_LINKER_FLAGS_USDVIEWWEB} ${EMSCRIPTEN_GLFW_BUILD_FLAGS} -pthread -sMODULARIZE=0 -O0")
target_link_options(${PXR_PACKAGE} PRIVATE "SHELL: \
${EMSCRIPTEN_GLFW_BUILD_FLAGS} -pthread \
--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/resources/webgpu-env-settings@/webgpu-env-settings \
--embed-file ${CMAKE_INSTALL_PREFIX}/libraries@/libraries/ \
--pre-js ${CMAKE_CURRENT_SOURCE_DIR}/resources/preload.js \
--profiling-funcs \
")

if(CMAKE_BUILD_TYPE MATCHES "(Debug|DEBUG|debug|RelWithDebInfo|RELWITHDEBINFO|relwithdebinfo)")
message("Building with debug information")
# See https://developer.chrome.com/blog/faster-wasm-debugging#debug_fission
# Note: -g is set automatically so we're just adding some additional flags
target_link_options(${PXR_PACKAGE} PRIVATE "SHELL: -gseparate-dwarf -s ERROR_ON_WASM_CHANGES_AFTER_LINK")
target_compile_options(${PXR_PACKAGE} PRIVATE "SHELL: -gdwarf-5 -gsplit-dwarf -gpubnames")
add_custom_command(
TARGET ${PXR_PACKAGE}
POST_BUILD
COMMAND emstrip --no-strip-all --remove-section=name ${CMAKE_CURRENT_BINARY_DIR}/${PXR_PACKAGE}.wasm && emdwp -e ${CMAKE_CURRENT_BINARY_DIR}/${PXR_PACKAGE}.wasm.debug.wasm -o ${CMAKE_CURRENT_BINARY_DIR}/${PXR_PACKAGE}.wasm.debug.wasm.dwp
COMMENT "Currently reduces the debug build from 332M to 85M while keeping debug information in separate files"
)
list(APPEND
BUILD_FILES
${CMAKE_CURRENT_BINARY_DIR}/${PXR_PACKAGE}.wasm.debug.wasm
)
endif()

install(
FILES
${BUILD_FILES}
DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
)
endif()

# Install icon files to the bin directory
install(
FILES
${ICON_FILES}
DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
)

# Install asset files to the bin/assets directory
install(
FILES
${ASSET_FILES}
DESTINATION ${CMAKE_INSTALL_PREFIX}/bin/assets
)

target_sources(${PXR_PACKAGE} PRIVATE
${APP_CPPFILES}
)
Loading