{"id":20810094,"url":"https://github.com/schwa/compute","last_synced_at":"2025-08-12T00:34:01.389Z","repository":{"id":251544167,"uuid":"837720135","full_name":"schwa/Compute","owner":"schwa","description":"A high-level Swift framework that simplifies working with GPU Metal Compute.","archived":false,"fork":false,"pushed_at":"2025-07-09T16:32:35.000Z","size":2976,"stargazers_count":63,"open_issues_count":5,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-11T07:09:47.736Z","etag":null,"topics":["compute","gpgpu","metal","shader","swift"],"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/schwa.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,"zenodo":null}},"created_at":"2024-08-03T20:20:09.000Z","updated_at":"2025-07-09T16:30:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"a2e6d6ca-93b2-496e-bfcf-798a4e0d11a8","html_url":"https://github.com/schwa/Compute","commit_stats":null,"previous_names":["schwa/compute"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/schwa/Compute","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schwa%2FCompute","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schwa%2FCompute/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schwa%2FCompute/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schwa%2FCompute/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/schwa","download_url":"https://codeload.github.com/schwa/Compute/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schwa%2FCompute/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269981005,"owners_count":24507274,"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","status":"online","status_checked_at":"2025-08-11T02:00:10.019Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["compute","gpgpu","metal","shader","swift"],"created_at":"2024-11-17T20:19:33.928Z","updated_at":"2025-08-12T00:34:01.380Z","avatar_url":"https://github.com/schwa.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"![alt text](Documentation/compute-logo.svg)\n\n# Compute\n\nThis project provides a high-level Swift framework for working with Metal compute shaders. It simplifies the process of setting up and executing compute tasks on GPU using Apple's Metal API.\n\n## Inspiration\n\nThis project draws inspiration from Apple's SwiftUI shaders introduced in iOS 17 and macOS Sonoma. SwiftUI shaders demonstrate how GPU operations can be made more accessible to developers, enabling complex visual effects with simple Swift code. Compute aims to provide a similar level of abstraction for Metal compute shaders, making it easier to perform data-parallel computations on the GPU.\n\n## Usage Example\n\nHere's a quick example of how to use the Metal Compute Framework to perform a basic computation:\n\n```swift\nimport Compute\nimport Metal\n\n// Example usage that adds two arrays of integers using Compute.\n\n// Metal shader source code\nlet source = \"\"\"\n#include \u003cmetal_stdlib\u003e\nusing namespace metal;\n\nkernel void add(device int* inA [[buffer(0)]],\n                device int* inB [[buffer(1)]],\n                device int* result [[buffer(2)]],\n                uint id [[thread_position_in_grid]]) {\n    result[id] = inA[id] + inB[id];\n}\n\"\"\"\n\n// Set up the compute environment\nlet device = MTLCreateSystemDefaultDevice()!\nlet compute = try Computer(device: device)\n\n// Create input data\nlet count = 1000\nlet inA = [Int32](repeating: 1, count: count)\nlet inB = [Int32](repeating: 2, count: count)\nvar result = [Int32](repeating: 0, count: count)\n\n// Create Metal buffers\nlet bufferA = device.makeBuffer(bytes: inA, length: MemoryLayout\u003cInt32\u003e.stride * count, options: [])!\nlet bufferB = device.makeBuffer(bytes: inB, length: MemoryLayout\u003cInt32\u003e.stride * count, options: [])!\nlet bufferResult = device.makeBuffer(length: MemoryLayout\u003cInt32\u003e.stride * count, options: [])!\n\n// Create a shader library and function\nlet library = ShaderLibrary.source(source)\nlet function = library.add\n\n// Create a compute pipeline and bind arguments.\nvar pipeline = try compute.makePipeline(function: function)\npipeline.arguments.inA = .buffer(bufferA)\npipeline.arguments.inB = .buffer(bufferB)\npipeline.arguments.result = .buffer(bufferResult)\n\n// Run the compute pipeline\ntry compute.run(pipeline: pipeline, width: count)\n\n// Read back the results\nvar bufferPointer = bufferResult.contents()\nlet bufferRawBuffer32 = bufferPointer.bindMemory(to: Int32.self, capacity: bufferResult.length)\nlet bufferBufferPointer = UnsafeBufferPointer(start: bufferRawBuffer32, count: count)\n    \n// Verify the results\nfor i in 0..\u003ccount {\n    assert(result[i] == inA[i] + inB[i], \"Computation error at index \\(i)\")\n}\n```\n\n## Requirements\n\nCompute current requires macOS 15/iOS 17 (which as of writing are in beta). This is because Compute uses the new metal logging facilities in Metal 3.2 It is possible to use Compute with earlier versions of macOS/iOS, but you will need to modify the code to use the older Metal API. Pull requests are welcome to make this work on older OSes.\n\n- iOS 17.0+ / macOS 15+\n- Swift 6.0+\n\n## Installation\n\n### Swift Package Manager\n\nTo integrate the Metal Compute Framework into your Xcode project using Swift Package Manager, add it to the dependencies value of your `Package.swift`:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/schwa/Compute.git\", .from(from: \"0.0.1\"))\n]\n```\n\nThen, specify it as a dependency of your target:\n\n```swift\ntargets: [\n    .target(name: \"YourTarget\", dependencies: [\"MetalComputeFramework\"]),\n]\n```\n\n## Features\n\nCompute is designed to reduce the amount of boilerplate setup code needed to use Metal compute shaders. It provides a high-level API for creating compute pipelines, binding arguments, and executing compute tasks on the GPU.\n\nCompute brings type safety and Swift-friendly syntax to Metal compute programming. It uses Swift enums and structs to represent Metal objects and types, making it easier to work with Metal buffers, textures, and other resources.\n\nCompute handles and throws errors upon resource creation, shader compilation, and compute pipeline execution. This makes it easier to catch and handle errors in your code.\n\nCompute also automatically adds labels to most Metal resources to aid in debugging.\n\nThis screenshot shows a comparison of code using Compute to code using \"bare\" Metal:\n\n![alt text](\u003cDocumentation/Screenshot 2024-08-04 at 09.57.19.png\u003e)\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome to the Compute Framework.\n\nNote: Some of our initial documentation and tests were AI-generated.\n\n## Links\n\n\u003e [Metal Overview - Apple Developer](https://developer.apple.com/metal/)\n\nApple's main Metal documentation.\n\n\u003e - [developer.apple.com/metal/Metal-Shading-Language-Specification.pdf](https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf)\n\nThe Metal Shading Language Specification book. This is the definitive guide to writing shaders in Metal.\n\n\u003e - [developer.apple.com/metal/Metal-Feature-Set-Tables.pdf](https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf)\n\nThe Metal Feature Set Tables book. This is a reference for which features are available on which devices/Metal versions.\n\n\u003e - [Metal by Example – High-performance graphics and data-parallel programming for iOS and macOS](https://metalbyexample.com)\n\nWarren Moore's blog is the single best resource for learning Metal programming.\n\n\u003e - [Introduction to Compute Programming in Metal – Metal by Example](https://metalbyexample.com/introduction-to-compute/)\n\nWarren has some posts on Compute programming in Metal but they're showing their age a bit. Nevertheless, they're a good starting point.\n\n\u003e - [Shader | Apple Developer Documentation](https://developer.apple.com/documentation/swiftui/shader)\n\nSwiftUI's Shader was the primary inspiration for this project.\n\n\u003e - [Calculating Threadgroup and Grid Sizes | Apple Developer Documentation](https://developer.apple.com/documentation/metal/compute_passes/calculating_threadgroup_and_grid_sizes)\n\u003e - [Creating Threads and Threadgroups | Apple Developer Documentation](https://developer.apple.com/documentation/metal/compute_passes/creating_threads_and_threadgroups)\n\nHow to calculate threadgroup and grid sizes. This is a critical concept in Metal compute programming.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschwa%2Fcompute","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fschwa%2Fcompute","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschwa%2Fcompute/lists"}