-
Notifications
You must be signed in to change notification settings - Fork 14
[WIP] WebGPU Status
This document describes the state of the WebGPU implementation in Atom. The document refers only to rendering functionality, and it doesn't contain any information about compiling or running in a browser enviroment.
There's currently an incomplete implementation of a WebGPU RHI in Atom. Most of the main features are already implemented. The main limitations comes from the usage itself, since some features are either not supported by WebGPU and some major changes would be needed in order to accommodate a workaround.
All of o3de WebGPU changes are in the https://github.com/o3de/o3de/tree/webgpu-rhi branch (there's nothing in the development branch). The current changes in the branch have been "lightly" code reviewed, so a thorough review to all changes has to be done before integrating into the main branch.
AZSLc changes are also required. All of those changes are in the https://github.com/o3de/o3de-azslc/tree/webgpu branch (no changes for WebGPU are in development).
If o3de doesn't compile or run in a browser, how are you able to test the WebGPU RHI? You have to remember that WebGPU is just a wrapper around other rendering APIs (it's actually an RHI), and luckily there's open source libraries available that implement the WebGPU functionality on C++. In our case we decided to use Google's implementation, a library called Dawn (https://dawn.googlesource.com/dawn) in order to run in platforms other than the browser. Dawn is actually the same library that is used by Chromium when running WebGPU code.
The Dawn library has 2 main parts: the runtime library and an offline shader compiler called Tint. Both components are treated as separated 3rd party.
Disclaimer: All development was done on Windows. Other platforms have not been tested (but in theory they should work).
At this moment, Dawn and Tint have not been added as 3rd party libraries, so local building is needed in order to run on WebGPU. To achieve this you first need to download the source code of Dawn from https://dawn.googlesource.com/dawn and follow the instructions (https://dawn.googlesource.com/dawn/+/HEAD/docs/quickstart-cmake.md) for building and installing. After installing, go to the DawnTargets.cmake file and add the GLOBAL keyword when adding the library:
add_library(dawn::dawn_public_config INTERFACE IMPORTED GLOBAL)
add_library(dawn::webgpu_dawn SHARED IMPORTED GLOBAL)
Go to the DawnTargets-release.cmake and add the DEBUG and PROFILE configurations (copy paste from the release one)
Since Dawn is not a 3rd party lib yet, CMake is expecting a folder where the Dawn library is. You can do this by using CMAKE_PREFIX_PATH, something like this:
cmake -B build/windows -S . -G "Visual Studio 17" -DCMAKE_PREFIX_PATH=D:\Dawn\install\Release
WebGPU only accepts shaders written in WGSL. Atom shaders are written in HLSL, so we will need to translate the shaders into the proper format. In order to get to WGSL, we first need to transform from HLSL to SPIR-V. Luckily we already have DXC for doing this translation (used for Vulkan shaders). Then, we will need to use Tint (an offline compiler that is part of Dawn) in order to translate from SPIR-V to WGSL.
To build Tint, you will need to go back to the Dawn project, and compile the "tint_cmd_tint_cmd" project. A small modification is needed since Tint doesn't support allowing features when compiling a shader through command line (https://issues.chromium.org/issues/368289749). Go to the src/tint/lang/spirv/reader/ast_parser/parse.cc file and below "allowed_features.extensions.insert(wgsl::Extension::kChromiumDisableUniformityAnalysis);" enable all extensions:
allowed_features.features.insert(wgsl::LanguageFeature::kPacked4X8IntegerDotProduct);
allowed_features.features.insert(wgsl::LanguageFeature::kPointerCompositeAccess);
allowed_features.features.insert(wgsl::LanguageFeature::kReadonlyAndReadwriteStorageTextures);
allowed_features.features.insert(wgsl::LanguageFeature::kUnrestrictedPointerParameters);
After building the project, there should be a tint.exe along with a webgpu_dawn.dll file. Copy both files into a "path/to/AssetProcessor/Builders/Tint" folder (you will need to create the Tint folder). This is the expected location that AssetProcessor will look for the Tint compiler (this will be done automatically once Tint is a 3rd party).