{"id":13871880,"url":"https://github.com/henrybetts/swift-webgpu","last_synced_at":"2025-10-21T10:24:32.236Z","repository":{"id":65536842,"uuid":"350712764","full_name":"henrybetts/swift-webgpu","owner":"henrybetts","description":"Swift bindings for WebGPU","archived":false,"fork":false,"pushed_at":"2024-10-28T00:56:48.000Z","size":326,"stargazers_count":86,"open_issues_count":2,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-10-28T04:51:03.452Z","etag":null,"topics":["dawn","gpu","graphics","swift","webgpu"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/henrybetts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-23T12:56:45.000Z","updated_at":"2024-10-28T00:56:53.000Z","dependencies_parsed_at":"2024-07-09T01:26:22.966Z","dependency_job_id":null,"html_url":"https://github.com/henrybetts/swift-webgpu","commit_stats":{"total_commits":137,"total_committers":1,"mean_commits":137.0,"dds":0.0,"last_synced_commit":"f160ab61d6201a5a597466d55807fbd5dba280c7"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrybetts%2Fswift-webgpu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrybetts%2Fswift-webgpu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrybetts%2Fswift-webgpu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrybetts%2Fswift-webgpu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/henrybetts","download_url":"https://codeload.github.com/henrybetts/swift-webgpu/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226089990,"owners_count":17572112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dawn","gpu","graphics","swift","webgpu"],"created_at":"2024-08-05T23:00:29.240Z","updated_at":"2025-10-21T10:24:27.193Z","avatar_url":"https://github.com/henrybetts.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"# swift-webgpu\n\nSwift bindings for [WebGPU](https://gpuweb.github.io/gpuweb/); a new graphics and compute API.\n\nDespite 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.\n\nEfforts are being made to define a standard native version of the API via a [shared header](https://github.com/webgpu-native/webgpu-headers). Note, however, that the specification is still work-in-progress.\n\nCurrently, swift-webgpu is based on the [Dawn](https://dawn.googlesource.com/dawn/) implementation, and generated from [dawn.json](https://dawn.googlesource.com/dawn/+/refs/heads/main/src/dawn/dawn.json).\n\n\n## Requirements\n\n### Dawn\n\nTo use swift-webgpu, you'll first need to build Dawn. Assuming you don't need to work on Dawn itself, the easiest way to build it is by following the [Quickstart with CMake guide](https://dawn.googlesource.com/dawn/+/HEAD/docs/quickstart-cmake.md).\n\nswift-webgpu depends on the bundled `libwebgpu_dawn` library, which can be built and installed like so;\n\n```sh\ngit clone https://dawn.googlesource.com/dawn\ncd dawn\ncmake -S . -B out/Release -DDAWN_FETCH_DEPENDENCIES=ON -DDAWN_ENABLE_INSTALL=ON -DDAWN_BUILD_SAMPLES=OFF -DCMAKE_BUILD_TYPE=Release\ncmake --build out/Release\n[sudo] cmake --install out/Release\n```\n\nThis should install the library and headers to `/usr/local/` - this is probably the simplest way to allow Swift to find the library currently.\n\nOn macOS, you may need to correct the install name of the library, like so:\n\n``` sh\nsudo install_name_tool -id /usr/local/lib/libwebgpu_dawn.dylib /usr/local/lib/libwebgpu_dawn.dylib\n```\n\nOtherwise you will likely need to place the library next to your executable, or configure rpaths appropriately for your executable.\n\n\n#### pkg-config\nYou may need to manually create a pkg-config file, depending on which tools you are using. For example, running `swift build` directly from the command line seems to search `/usr/local/` automatically, whereas building with Xcode does not. If you run into this issue, create a file at `/usr/local/lib/pkgconfig/webgpu.pc` with the following contents;\n```\nprefix=/usr/local\nincludedir=${prefix}/include\nlibdir=${prefix}/lib\n\nCflags: -I${includedir}\nLibs: -L${libdir} -lwebgpu_dawn\n```  \n\n#### dawn.json\nThis file contains a description of the WebGPU native API. By default, swift-webgpu will look for it in `/usr/local/share/dawn/`, so you will need to copy it there manually;\n```sh\nsudo install -d /usr/local/share/dawn\nsudo install src/dawn/dawn.json /usr/local/share/dawn/\n```\n\nAlternatively, you can specify a `DAWN_JSON` environment variable when building swift-webgpu.\n\n\n## Running the Demos\n\nFirst, clone this project;\n\n```sh\ngit clone https://github.com/henrybetts/swift-webgpu.git\ncd swift-webgpu\n```\n\nBuild the package (assuming that Dawn is installed and Swift can find it automatically);\n```sh\nswift build -c release\n```\n\nOtherwise, you may need to specify the search paths manually;\n```sh\nDAWN_JSON=/path/to/dawn/src/dawn/dawn.json \\\nswift build -c release \\\n-Xcc -I/path/to/dawn/include \\\n-Xcc -I/path/to/dawn/out/Release/gen/include \\\n-Xlinker -L/path/to/dawn/out/Release/src/dawn/native \\\n-Xlinker -rpath -Xlinker /path/to/dawn/out/Release/src/dawn/native\n```\n\nFinally, run the demos;\n\n```sh\ncd .build/release\n./DemoInfo\n./DemoClearColor\n./DemoTriangle\n./DemoCube\n./DemoBoids\n```\n\n\n## Installation\n\nTo use swift-webgpu with Swift Package Manager, add it to your `Package.swift` file's dependencies;\n\n```swift\n.package(url: \"https://github.com/henrybetts/swift-webgpu.git\", branch: \"main\")\n```\n\nThen add `WebGPU` as a dependency of your target;\n\n```swift\n.executableTarget(\n    name: \"MyApp\",\n    dependencies: [.product(name: \"WebGPU\", package: \"swift-webgpu\")],\n),\n```\n\n\n## Basic Usage\n\nImport the WebGPU module where needed;\n\n```swift\nimport WebGPU\n```\n\nA typical application will start by creating an `Instance`, requesting an `Adapter`, and then requesting a `Device`. For example;\n\n```swift\n// create an instance\nlet instance = createInstance()\n\n// find an adapter\nlet adapter = try await instance.requestAdapter()\nprint(\"Using adapter: \\(adapter.info.device)\")\n\n// create a device\nlet device = try await adapter.requestDevice()\n```\n\nYou'll usually want to set an error handler, to log any errors produced by the device;\n\n```swift\nlet uncapturedErrorCallback: UncapturedErrorCallback = { device, errorType, errorMessage in\n    print(\"Error (\\(errorType)): \\(errorMessage)\")\n}\n\nlet device = try await adapter.requestDevice(descriptor: .init(\n    uncapturedErrorCallback: uncapturedErrorCallback\n))\n```\n\nWith the device obtained, you can create most of the other types of WebGPU objects. A shader module, for example;\n\n```swift\nlet shaderModule = device.createShaderModule(\n  descriptor: .init(\n    nextInChain: ShaderSourceWgsl(\n      code: \"\"\"\n        @vertex\n        fn vertexMain() -\u003e @builtin(position) vec4f {\n          return vec4f(0, 0, 0, 1);\n        }\n         \n        @fragment\n        fn fragmentMain() -\u003e @location(0) vec4f {\n          return vec4f(1, 0, 0, 1);\n        }\n      \"\"\"\n    )\n  )\n)\n```\n\nOr a render pipeline;\n\n```swift\nlet renderPipeline = device.createRenderPipeline(\n  descriptor: .init(\n    vertex: VertexState(\n      module: shaderModule,\n      entryPoint: \"vertexMain\"\n    ),\n    fragment: FragmentState(\n      module: shaderModule,\n      entryPoint: \"fragmentMain\",\n      targets: [ColorTargetState(format: .bgra8Unorm)]\n    )\n  )\n)\n```\n\nMost objects are created with a descriptor. In some cases, WebGPU uses a chainable struct pattern to support future extensions or platform specific features. This is indicated by the `nextInChain` property. (There is scope to improve the ergonomics of this in Swift.)\n\nSee the demos for further usage examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenrybetts%2Fswift-webgpu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhenrybetts%2Fswift-webgpu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenrybetts%2Fswift-webgpu/lists"}