# matdbg 1. [Capabilities](#capabilities) 1. [Setup for Desktop](#setup-for-desktop) 1. [Setup for Android](#setup-for-android) 1. [Debugger Usage](#debugger-usage) 1. [Architecture Overview](#architecture-overview) 1. [C++ Server](#c-server) 1. [JavaScript Client](#javascript-client) 1. [HTTP Requests](#http-requests) 1. [WebSocket Messages](#websocket-messages) 1. [Wish List](#wish-list) 1. [Screenshot](#screenshot) 1. [Material Chunks](#material-chunks) ## Capabilities matdbg is a library and web application that enables debugging and live-editing of Filament shaders. At the time of this writing, the following capabilities are supported. - OpenGL: Editing GLSL - Metal: Editing MSL - Vulkan: Editing transpiled GLSL, displaying disassembled SPIR-V Note that a given material can be built with multiple backends, even though only one backend is active in a particular session. For example, if the current app is using Vulkan, it is still possible to inspect the Metal shaders, as long as the material has been built with Metal support included. ## Setup for Desktop When using the easy build script, include the `-d` argument. For example: ./build.sh -fd debug gltf_viewer The `d` enables a CMake option called FILAMENT_ENABLE_MATDBG and the `f` ensures that CMake gets re-run so that the option is honored. Next, set an environment variable as follows. In Windows, use `set` instead of `export`. export FILAMENT_MATDBG_PORT=8080 Next, launch any app that links against a debug build of a Filament and point your web browser to http://localhost:8080. Skip ahead to **Debugger Usage**. ## Setup for Android Rebuild Filament for Android after enabling a CMake option called FILAMENT_ENABLE_MATDBG. Note that CMake is invoked from several places for Android (both gradle and our easy build script), so one pragmatic and reliable way of doing this is to simply hack `CMakeLists.txt` and `filament-android/CMakeLists.txt` by unconditionally setting FILAMENT_ENABLE_MATDBG to ON. After rebuilding Filament with the option enabled, ensure that internet permissions are enabled in your app by adding the following into your manifest as a child of the `` element. Now launch your app as usual. The Filament Engine sets up a server that is hardcoded to listen to port 8081. Next, you will need to forward your device's TCP port 8081 to your host port of choice. For example, to forward the matdbg server on your device to port 8081 on your host machine, do the following: adb forward tcp:8081 tcp:8081 This lets you go to http://localhost:8081 in Chrome on your host machine. Note that we generally use a release build of Filament when running on Android, so the shaders are optimized and very unreadable. This can be avoided by modifying the build such that `-g` is passed to matc even in release builds. ## Debugger Usage After opening the matdbg page in your browser, the usual first step is to select a material in the upper-left pane. Sometimes you might need force your app to redraw (e.g. by resizing the window) in order make the materials selectable. The next step is to select an active (boldface) shader variant in the lower-left pane. This allows you to view the GLSL, MSL, and SPIR-V code that was generated by `matc` or `filamat`. In the sidebar, inactive shader variants have a disabled appearance, but they can still be examined in the shader editor. The active status of each shader program is refreshed every second. You can also make modifications to GLSL or MSL, so long as the shader inputs and uniforms remain intact. After making an edit, click the `[rebuild]` button in the header. Note that your edits will be lost after closing the web page. ## Keyboard Shortcuts To save an edit, press **Cmd+S** (**Ctrl+S** on Linux/Windows) as an alternative to clicking `[rebuild]`. If the editor has focus, you can navigate between materials by holding **Shift+Ctrl** while pressing the up or down arrow. Navigation between variants is similar, just use left / right instead of up / down. ## Architecture Overview The matdbg library has two parts: a C++ server and a JavaScript client. The C++ server is responsible for instancing a [civetweb][1] context that handles HTTP and WebSocket requests. The JavaScript client is a small web app that contains a view into an in-browser database of materials. The WebSocket server receives push-style notifications from the client (such as edits) while the HTTP server responds to material queries using simple JSON messages. When a new WebSocket connection is established, the client asks the server for a list of materials in order to populate its in-browser database. If the connection is lost (e.g. if the app crashes), then the database stays intact and the web app is still functional. If a new Filament app is launched, the client inserts entries into its database rather than replacing the existing set. The material database is cleared only when the web page is manually refreshed by the user. ## C++ Server The civetweb server is wrapped by our `DebugServer` class, whose public interface is comprised of a couple methods that are called from the Filament engine: - **addMaterial** Notifies the debugger that the given material package is being loaded into the engine. - **setEditCallback** Sets up a callback that allows the Filament engine to listen for shader edits. - **setQueryCallback** Sets up a callback that allows the debugger to ask for current information. ## JavaScript Client The web app is written in simple, modern JavaScript and avoids frameworks like React or Angular. It uses the following third-party libraries which are fetched from a CDN using `