Skip to content

Commit 5b8137e

Browse files
committed
Fix compatibility with Embedded Swift
1 parent ffcb2db commit 5b8137e

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

WebGPUDemo/Sources/Entrypoint.swift

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import _Concurrency
1314
import DOM
1415
import JavaScriptEventLoop
1516
import JavaScriptKit
17+
import WebAPIBase
1618
import WebGPU
1719

18-
func fetchString(url: String) async throws -> String {
20+
func fetchString(url: String) async throws(JSException) -> String {
1921
let result = try await Window.global.fetch(input: .init(url))
2022
return try await result.text()
2123
}
2224

23-
func fetchImageBitmap(url: String) async throws -> ImageBitmap {
25+
func fetchImageBitmap(url: String) async throws(JSException) -> ImageBitmap {
2426
let blob = try await Window.global.fetch(input: .init(url)).blob()
2527
return try await Window.global.createImageBitmap(
2628
image: .blob(blob),
@@ -34,22 +36,26 @@ struct Entrypoint {
3436
JavaScriptEventLoop.installGlobalExecutor()
3537
let gpu = Window.global.navigator.gpu
3638
Task {
37-
let adapter = try await gpu.requestAdapter()!
38-
let device = try await adapter.requestDevice()
39+
do throws(JSException) {
40+
let adapter = try await gpu.requestAdapter()!
41+
let device = try await adapter.requestDevice()
3942

40-
let renderer = try await Renderer(
41-
device: device,
42-
gpu: gpu,
43-
assets: .init(
44-
shaders: fetchString(url: "Resources/shaders.wgsl"),
45-
model: fetchString(url: "Resources/SwiftLogo/Swift3DLogo.obj"),
46-
albedo: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_BaseColor.png"),
47-
normal: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_Normal.png"),
48-
metalRoughness: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_MetalRoughness.png"),
49-
),
50-
)
43+
let renderer = try await Renderer(
44+
device: device,
45+
gpu: gpu,
46+
assets: .init(
47+
shaders: fetchString(url: "Resources/shaders.wgsl"),
48+
model: fetchString(url: "Resources/SwiftLogo/Swift3DLogo.obj"),
49+
albedo: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_BaseColor.png"),
50+
normal: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_Normal.png"),
51+
metalRoughness: fetchImageBitmap(url: "Resources/SwiftLogo/T_M_swiftLogo_MetalRoughness.png"),
52+
),
53+
)
5154

52-
draw(renderer: renderer)
55+
draw(renderer: renderer)
56+
} catch {
57+
console.error(data: error.thrownValue)
58+
}
5359
}
5460
}
5561
}

0 commit comments

Comments
 (0)