https://github.com/schwa/compute
A high-level Swift framework that simplifies working with GPU Metal Compute.
https://github.com/schwa/compute
compute gpgpu metal shader swift
Last synced: 5 months ago
JSON representation
A high-level Swift framework that simplifies working with GPU Metal Compute.
- Host: GitHub
- URL: https://github.com/schwa/compute
- Owner: schwa
- License: mit
- Created: 2024-08-03T20:20:09.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-10-21T16:47:01.000Z (6 months ago)
- Last Synced: 2024-10-22T05:42:54.329Z (6 months ago)
- Topics: compute, gpgpu, metal, shader, swift
- Language: Swift
- Homepage:
- Size: 2.83 MB
- Stars: 40
- Watchers: 2
- Forks: 4
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Compute
This 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.
## Inspiration
This 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.
## Usage Example
Here's a quick example of how to use the Metal Compute Framework to perform a basic computation:
```swift
import Compute
import Metal// Example usage that adds two arrays of integers using Compute.
// Metal shader source code
let source = """
#include
using namespace metal;kernel void add(device int* inA [[buffer(0)]],
device int* inB [[buffer(1)]],
device int* result [[buffer(2)]],
uint id [[thread_position_in_grid]]) {
result[id] = inA[id] + inB[id];
}
"""// Set up the compute environment
let device = MTLCreateSystemDefaultDevice()!
let compute = try Compute(device: device)// Create input data
let count = 1000
let inA = [Int32](repeating: 1, count: count)
let inB = [Int32](repeating: 2, count: count)
var result = [Int32](repeating: 0, count: count)// Create Metal buffers
let bufferA = device.makeBuffer(bytes: inA, length: MemoryLayout.stride * count, options: [])!
let bufferB = device.makeBuffer(bytes: inB, length: MemoryLayout.stride * count, options: [])!
let bufferResult = device.makeBuffer(length: MemoryLayout.stride * count, options: [])!// Create a shader library and function
let library = ShaderLibrary.source(source)
let function = library.add// Create a compute pipeline and bind arguments.
var pipeline = try compute.makePipeline(function: function)
pipeline.arguments.inA = .buffer(bufferA)
pipeline.arguments.inB = .buffer(inB)
pipeline.arguments.result = .buffer(bufferResult)// Run the compute pipeline
try compute.run(pipeline: pipeline, width: count)// Read back the results
result = bufferResult.contents().withUnsafeBytes { $0.bindMemory(to: Int32.self) }// Verify the results
for i in 0..)## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## Contributing
Contributions are welcome to the Compute Framework.
Note: Some of our initial documentation and tests were AI-generated.
## Links
> [Metal Overview - Apple Developer](https://developer.apple.com/metal/)
Apple's main Metal documentation.
> - [developer.apple.com/metal/Metal-Shading-Language-Specification.pdf](https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf)
The Metal Shading Language Specification book. This is the definitive guide to writing shaders in Metal.
> - [developer.apple.com/metal/Metal-Feature-Set-Tables.pdf](https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf)
The Metal Feature Set Tables book. This is a reference for which features are available on which devices/Metal versions.
> - [Metal by Example – High-performance graphics and data-parallel programming for iOS and macOS](https://metalbyexample.com)
Warren Moore's blog is the single best resource for learning Metal programming.
> - [Introduction to Compute Programming in Metal – Metal by Example](https://metalbyexample.com/introduction-to-compute/)
Warren has some posts on Compute programming in Metal but they're showing their age a bit. Nevertheless, they're a good starting point.
> - [Shader | Apple Developer Documentation](https://developer.apple.com/documentation/swiftui/shader)
SwiftUI's Shader was the primary inspiration for this project.
> - [Calculating Threadgroup and Grid Sizes | Apple Developer Documentation](https://developer.apple.com/documentation/metal/compute_passes/calculating_threadgroup_and_grid_sizes)
> - [Creating Threads and Threadgroups | Apple Developer Documentation](https://developer.apple.com/documentation/metal/compute_passes/creating_threads_and_threadgroups)How to calculate threadgroup and grid sizes. This is a critical concept in Metal compute programming.