Swift bindings for WebGPU; a new graphics and compute API.
Despite being designed for the web, WebGPU can also be used natively, enabling developers with a modern, cross-platform API, without some of the complexities of lower-level graphics libraries.
Efforts are being made to define a standard native version of the API via a shared header. Note, however, that the specification is still work-in-progress.
Currently, swift-webgpu is based on the Dawn implementation, and generated from dawn.json.
Requirements
Dawn
To use swift-webgpu, you’ll first need to build Dawn. See Dawn’s documentation to get started.
swift-webgpu depends on the libwebgpu_dawn and libdawn_native shared libraries, which can be built like so;
mkdir -p out/Release
cd out/Release
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=1 ../..
make # -j N for N-way parallel build
Running the Demos
First, clone this project;
git clone https://github.com/henrybetts/swift-webgpu.git
cd swift-webgpu
Remember to replace /path/to/dawn with the actual path to the dawn directory. These arguments tell the compiler where to find the dawn headers, and the linker where to find the shared libraries. The DAWN_JSON variable defines the location of dawn.json, which is needed for code generation.
Finally, run the demos;
cd .build/release
./DemoInfo
./DemoClearColor
./DemoTriangle
./DemoCube
./DemoBoids
Installation
To use swift-webgpu with Swift Package Manager, add it to your Package.swift file’s dependencies;
A typical application will start by creating an Instance, requesting an Adapter, and then requesting a Device. However, some of the specification is not yet implemented in Dawn, particularly regarding these initialization steps. Therefore, the DawnNative module exists for the time being, providing an entry point to the rest of the WebGPU API. For example;
import DawnNative
// create an instance
let instance = DawnNative.Instance()
// find an adapter
instance.discoverDefaultAdapters()
guard let adapter = instance.adapters.first else {
fatalError("No adapters found")
}
print("Using adapter: \(adapter.properties.name)")
// create a WebGPU.Device
guard let device = adapter.createDevice() else {
fatalError("Failed to create device")
}
swift-webgpu
Swift bindings for WebGPU; a new graphics and compute API.
Despite being designed for the web, WebGPU can also be used natively, enabling developers with a modern, cross-platform API, without some of the complexities of lower-level graphics libraries.
Efforts are being made to define a standard native version of the API via a shared header. Note, however, that the specification is still work-in-progress.
Currently, swift-webgpu is based on the Dawn implementation, and generated from dawn.json.
Requirements
Dawn
To use swift-webgpu, you’ll first need to build Dawn. See Dawn’s documentation to get started.
swift-webgpu depends on the
libwebgpu_dawn
andlibdawn_native
shared libraries, which can be built like so;Running the Demos
First, clone this project;
Build the package;
Remember to replace
/path/to/dawn
with the actual path to the dawn directory. These arguments tell the compiler where to find the dawn headers, and the linker where to find the shared libraries. TheDAWN_JSON
variable defines the location ofdawn.json
, which is needed for code generation.Finally, run the demos;
Installation
To use swift-webgpu with Swift Package Manager, add it to your
Package.swift
file’s dependencies;Then add
WebGPU
as a dependency of your target, and link thewebgpu_dawn
library;Basic Usage
Import the WebGPU module where needed;
A typical application will start by creating an
Instance
, requesting anAdapter
, and then requesting aDevice
. However, some of the specification is not yet implemented in Dawn, particularly regarding these initialization steps. Therefore, theDawnNative
module exists for the time being, providing an entry point to the rest of the WebGPU API. For example;See the demos for further usage examples.