Files
filament/web
Mathias Agopian f4a079f663 backend: Propagate backend thread exceptions to the main thread. (#9903)
* Filament Error Handling Remediation and noexcept Cleanup

- Replaced assert_invariant with FILAMENT_CHECK_PRECONDITION in
  Renderer::beginFrame for caller bugs.
- Removed noexcept from Camera::setCustomProjection to allow precondition
  checks to throw.
- Removed noexcept from Texture and InstanceBuffer methods using precondition
  checks.
- Documented silent clamping in View::setBloomOptions.
- Clarified comment in FrameGraphResources regarding preconditions.
- Refined MaterialInstance::commit to split texture loop into check and update
  passes, preventing partial state changes on precondition failure.

* backend: Propagate backend thread exceptions to the main thread.

Introduce a mechanism to catch exceptions thrown on the backend thread and
rethrow them on the main thread. This prevents deadlocks and allows the
application to handle fatal backend failures gracefully.

- Consolidate synchronization primitives in DriverBase.
- Add mHasUnrecoverableError flag to interrupt blocked fence waits.
- Optimize waitForFence to avoid extra atomic reads in common paths.
- Propagate FenceStatus::ERROR across all backends.
- CommandBufferQueue now stores a std::exception_ptr when the backend fails.
- The backend enters a "zombie" state on failure, skipping further command
  execution but allowing clean shutdown.
- Public APIs in Renderer and Engine now check for stored exceptions and
  rethrow them, documented with @throws.
- Guarded by __EXCEPTIONS to ensure no overhead when exceptions are disabled.
- Add unit test for fence interruption and document new APIs.

BUGS=[407545700]

* feat: harden backend exceptions and add hasUnrecoverableFailure API

- Update Renderer::beginFrame() and Renderer::shouldRenderFrame() to
  return false early if an unrecoverable backend exception has been
  delivered to the main thread.
- Document the new return behavior for beginFrame() and
  shouldRenderFrame() in Renderer.h.
- Add Engine::hasUnrecoverableFailure() to the public API to allow
  apps to check for fatal errors without relying on exceptions.
- Implement hasUnrecoverableFailure() in FEngine by delegating to
  CommandBufferQueue.
- Expose Engine::hasUnrecoverableFailure() to Java bindings
  (Engine.java and JNI).
- Expose Engine::hasUnrecoverableFailure() to JavaScript bindings
  (jsbindings.cpp).

* java: propagate C++ Panics & exceptions to JAVA
2026-04-22 10:56:09 -07:00
..

Web Samples and Tutorials

This directory contains the web-based samples, tutorials, and the JavaScript wrapper for Filament.

Structure

  • examples/ Contains the source for the WebGL tutorials, HTML samples, materials, and assets. These examples use filament.js and filament.wasm to demonstrate various features of the engine.

  • filament-js/ Contains the JavaScript bindings for Filament, generated via Emscripten.

Building the Web Examples

To build the WebGL targets and compile all required materials and assets, you will need the Emscripten SDK installed and activated.

  1. Activate Emscripten SDK: Make sure you have EMSDK in your environment.

    cd path/to/emsdk
    ./emsdk activate latest
    source ./emsdk_env.sh
    
  2. Run the Build Script: From the root directory of the repository, execute the build script targeting WebGL:

    ./build.sh -p webgl release
    

    This will:

    • Compile the C++ engine to filament.wasm and filament.js.
    • Build all materials (.mat to .filamat) and process textures required by the examples.
    • Output everything into out/cmake-webgl-release/examples/.

Running the Examples

Because of CORS restrictions and the need to serve WebAssembly files with the correct MIME type, you must serve the files via a local web server.

  1. Install Python Requirements: The serve script dynamically renders Markdown tutorials on-the-fly using mistletoe. It is highly recommended to use a Python virtual environment to install dependencies.

    python3 -m venv .venv
    source .venv/bin/activate
    pip install -r web/examples/requirements.txt
    
  2. Start the Server: From the root of the repository, run the serve.py script:

    ./web/examples/serve.py
    
  3. View the Examples: Open your browser and navigate to http://localhost:8000. You will see an index page listing all the generated tutorials and samples.

Python Script: serve.py

The custom serve.py script in the web/examples/ directory performs the following:

  • Automatically detects the built files in the out/ directory.
  • Generates a main entry index.html listing all the available samples and tutorials.
  • Handles server-side rendering of .md tutorial files into HTML using an embedded template.

Porting a Web Sample to Official Docs (docs_src)

If you want your web sample or tutorial to appear on the official Filament documentation website via mdbook, follow these steps:

  1. Map the Built HTML: Add your generated .html or .md file to the mapping in docs_src/build/duplicates.json. This tells the documentation build script to copy your sample into the mdbook structure.

    "out/cmake-webgl-release/web/examples/examples/your_sample/your_sample.html": {
        "dest": "samples/web/your_sample.md"
    }
    
  2. Add to the Navigation Menu: Link your sample in the table of contents by adding it to docs_src/src_mdbook/src/SUMMARY.md under the "Web Tutorials" or "Web Samples" section.

  3. Generate a Thumbnail Image: Add your sample's name to the samples array in docs_src/build/snapshot_samples.py. Then, manually run this script (python3 snapshot_samples.py). It will launch a headless browser, wait for your scene to render, and snap a 100x100 preview image.

  4. Dynamic Asset Loading (Optional): When mdbook serves your sample, the assets (.filamat, textures) are segregated into a web/assets/ directory.

    • For <script src="..."> or <img src="..."> tags embedded in the HTML, the paths will be automatically rewritten by docs_src/build/copy_web_docs.py.
    • However, if you load files dynamically within your JavaScript code (e.g., using fetch()), you must prepend (window.FILAMENT_ASSET_DIR || '') to the file URL.
    • If you need window.FILAMENT_ASSET_DIR to be properly populated, make sure to add logic to docs_src/build/copy_web_docs.py to inject the proper path prefix for your sample.