Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 WebGPUDemo/.sourcekit-lsp/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"swiftPM": {
"swiftSDK": "swift-DEVELOPMENT-SNAPSHOT-2025-06-03-a_wasm"
"swiftSDK": "swift-DEVELOPMENT-SNAPSHOT-2025-08-14-a_wasm-embedded"
}
}
6 changes: 3 additions & 3 deletions WebGPUDemo/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions WebGPUDemo/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 6.1
// swift-tools-version: 6.2

import PackageDescription

Expand All @@ -11,7 +11,7 @@ let package = Package(
),
.package(
url: "https://github.com/swiftwasm/JavaScriptKit.git",
branch: "main",
from: "0.33.1",
),
],
targets: [
Expand Down
10 changes: 10 additions & 0 deletions WebGPUDemo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ the Swift SDK in the following command to the version that matches your installe
swift package --swift-sdk swift-6.2-DEVELOPMENT-SNAPSHOT-2025-06-17-a_wasm js --use-cdn
```

If you'd like to produce a smaller binary (under 400 kB), you'll have to use
`swift-DEVELOPMENT-SNAPSHOT-2025-08-11` or later development snapshot of the `main` Swift toolchain
branch. Earlier versions (including Swift 6.2) have no support for `async` functions in Embedded Swift,
which is required for WebGPU setup. Use the following command to build with Embedded Swift (update for
your installed toolchain version if needed):

```
swift package --swift-sdk swift-DEVELOPMENT-SNAPSHOT-2025-08-11-a_wasm-embedded js --use-cdn -c release
```

WebGPU is enabled by default in beta and technical preview versions of Safari. Safari 17 and 18 require enabling
WebGPU feature flag as shown on the screenshot:

Expand Down
48 changes: 25 additions & 23 deletions WebGPUDemo/Sources/Entrypoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,34 @@ func fetchImageBitmap(url: String) async throws(JSException) -> ImageBitmap {
)
}

typealias DefaultExecutorFactory = JavaScriptEventLoop

@main
struct Entrypoint {
static func main() {
JavaScriptEventLoop.installGlobalExecutor()
static func main() async {
let gpu = Window.global.navigator.gpu
Task {
do throws(JSException) {
let adapter = try await gpu.requestAdapter()!
let device = try await adapter.requestDevice()

let renderer = try await Renderer(
device: device,
gpu: gpu,
assets: .init(
shaders: fetchString(url: "Resources/shaders.wgsl"),
model: fetchString(url: "Resources/SwiftLogo/Swift3DLogo.obj"),
albedo: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_BaseColor.png"),
normal: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_Normal.png"),
metalRoughness: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_MetalRoughness.png"),
),
)

draw(renderer: renderer)
} catch {
console.error(data: error.thrownValue)
}

do throws(JSException) {
// Using promises instead of `async` functions due to https://github.com/swiftlang/swift/issues/83750
let adapterPromise: JSPromise = gpu.requestAdapter()
let devicePromise = JSPromise(from: try await adapterPromise.value().requestDevice())!
let device = try await GPUDevice(unsafelyWrapping: devicePromise.value().object!)

let renderer = try await Renderer(
device: device,
gpu: gpu,
assets: .init(
shaders: fetchString(url: "Resources/shaders.wgsl"),
model: fetchString(url: "Resources/SwiftLogo/Swift3DLogo.obj"),
albedo: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_BaseColor.png"),
normal: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_Normal.png"),
metalRoughness: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_MetalRoughness.png"),
),
)

draw(renderer: renderer)
} catch {
console.error(data: error.thrownValue)
}
}
}