Skip to content

Commit

Permalink
documentation and command queue extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenebokhan committed Jul 1, 2024
1 parent 9e73061 commit a446cc4
Show file tree
Hide file tree
Showing 129 changed files with 4,155 additions and 424 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test Swift Package

on:
pull_request:
branches: [main]

jobs:
test:
runs-on: macos-latest

steps:
- uses: actions/checkout@v3
- uses: swift-actions/setup-swift@v2
with:
swift-version: '5.9'

- name: Build
run: swift build

- name: Run tests
run: swift test
7 changes: 7 additions & 0 deletions .spi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 1
builder:
configs:
- documentation_targets:
- MetalTools
- MetalComputeTools
- MetalRenderingTools
11 changes: 4 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.6
// swift-tools-version:5.9

import PackageDescription

Expand All @@ -25,8 +25,8 @@ let package = Package(
],
dependencies: [
.package(
url: "https://github.com/SwiftGFX/SwiftMath.git",
.upToNextMajor(from: "3.3.1")
url: "https://github.com/computer-graphics-tools/simd-tools.git",
.upToNextMinor(from: "0.0.1")
)
],
targets: [
Expand All @@ -40,10 +40,7 @@ let package = Package(
dependencies: [
.target(name: "MetalComputeToolsSharedTypes"),
.target(name: "MetalTools"),
.product(
name: "SwiftMath",
package: "SwiftMath"
)
.product(name: "SIMDTools", package: "simd-tools")
],
resources: [
.process("Kernels/BitonicSort/BitonicSort.metal"),
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# MetalTools

[![Platform Compatibility](https://img.shields.io/badge/Platforms-iOS%20|%20macOS-brightgreen)](https://swift.org/platforms/)
[![Swift Version](https://img.shields.io/badge/Swift-5.9-orange)](https://swift.org)

<p align="left">
<img src="Sources/MetalTools/MetalTools.docc/Resources/table-of-contents-art/[email protected]", width="120">
</p>

## Description

MetalTools provides a convenient, Swifty way of working with Metal. This library is heavily used in computer vision startups [ZERO10](https://zero10.ar) and [Prisma](https://prisma-ai.com).
Expand Down

This file was deleted.

42 changes: 38 additions & 4 deletions Sources/MetalComputeTools/Kernels/BitonicSort/BitonicSort.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import MetalTools

/// A class that implements the Bitonic Sort algorithm using Metal.
final public class BitonicSort {
// MARK: - Properties

private let firstPass: FirstPass
private let generalPass: GeneralPass
private let finalPass: FinalPass

// MARK: - Init
// MARK: - Initialization

/// Initializes a new BitonicSort instance using a Metal context.
///
/// - Parameters:
/// - context: The Metal context to use.
/// - scalarType: The scalar type of the data to be sorted.
/// - Throws: An error if initialization fails.
public convenience init(
context: MTLContext,
scalarType: MTLPixelFormat.ScalarType
Expand All @@ -19,6 +26,12 @@ final public class BitonicSort {
)
}

/// Initializes a new BitonicSort instance using a Metal library.
///
/// - Parameters:
/// - library: The Metal library containing the required kernel functions.
/// - scalarType: The scalar type of the data to be sorted.
/// - Throws: An error if initialization fails.
public init(
library: MTLLibrary,
scalarType: MTLPixelFormat.ScalarType
Expand All @@ -37,20 +50,32 @@ final public class BitonicSort {
)
}

// MARK: - Encode
// MARK: - Encoding

/// Encodes the sorting operation into a command buffer.
///
/// - Parameters:
/// - data: The buffer containing the data to be sorted.
/// - count: The number of elements to sort.
/// - commandBuffer: The command buffer to encode into.
public func callAsFunction(
data: MTLBuffer,
count: Int,
in commandeBuffer: MTLCommandBuffer
in commandBuffer: MTLCommandBuffer
) {
self.encode(
data: data,
count: count,
in: commandeBuffer
in: commandBuffer
)
}

/// Encodes the sorting operation into a command buffer.
///
/// - Parameters:
/// - data: The buffer containing the data to be sorted.
/// - count: The number of elements to sort.
/// - commandBuffer: The command buffer to encode into.
public func encode(
data: MTLBuffer,
count: Int,
Expand Down Expand Up @@ -117,6 +142,14 @@ final public class BitonicSort {
)
}

/// Creates a Metal buffer from an array of floating-point values, padding if necessary.
///
/// - Parameters:
/// - array: The array of floating-point values to create a buffer from.
/// - device: The Metal device to create the buffer on.
/// - options: Resource options for the buffer.
/// - Returns: A tuple containing the created buffer and the padded count.
/// - Throws: An error if buffer creation fails.
public static func buffer<T: FloatingPoint>(
from array: [T],
device: MTLDevice,
Expand All @@ -130,6 +163,7 @@ final public class BitonicSort {
)
}

// Private helper method for buffer creation
private static func buffer<T: Numeric>(
from array: [T],
paddingValue: T,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import MetalTools

extension BitonicSort {
/// Represents the final pass of the Bitonic Sort algorithm.
final class FinalPass {
// MARK: - Properties

/// The compute pipeline state for this pass.
let pipelineState: MTLComputePipelineState

/// Indicates whether the device supports non-uniform threadgroups.
private let deviceSupportsNonuniformThreadgroups: Bool

// MARK: - Init
// MARK: - Initialization

/// Initializes a new FinalPass instance using a Metal context.
///
/// - Parameters:
/// - context: The Metal context to use.
/// - scalarType: The scalar type of the data to be sorted.
/// - Throws: An error if initialization fails.
convenience init(
context: MTLContext,
scalarType: MTLPixelFormat.ScalarType
Expand All @@ -19,6 +29,12 @@ extension BitonicSort {
)
}

/// Initializes a new FinalPass instance using a Metal library.
///
/// - Parameters:
/// - library: The Metal library containing the required kernel functions.
/// - scalarType: The scalar type of the data to be sorted.
/// - Throws: An error if initialization fails.
init(
library: MTLLibrary,
scalarType: MTLPixelFormat.ScalarType
Expand All @@ -39,8 +55,17 @@ extension BitonicSort {
)
}

// MARK: - Encode
// MARK: - Encoding

/// Encodes the final pass of the sorting operation into a command buffer.
///
/// - Parameters:
/// - data: The buffer containing the data to be sorted.
/// - elementStride: The stride between elements in the buffer.
/// - params: Parameters for the sorting operation.
/// - gridSize: The size of the compute grid.
/// - unitSize: The size of each unit in the grid.
/// - commandBuffer: The command buffer to encode into.
func callAsFunction(
data: MTLBuffer,
elementStride: Int,
Expand All @@ -59,6 +84,15 @@ extension BitonicSort {
)
}

/// Encodes the final pass of the sorting operation using a compute command encoder.
///
/// - Parameters:
/// - data: The buffer containing the data to be sorted.
/// - elementStride: The stride between elements in the buffer.
/// - params: Parameters for the sorting operation.
/// - gridSize: The size of the compute grid.
/// - unitSize: The size of each unit in the grid.
/// - encoder: The compute command encoder to use.
func callAsFunction(
data: MTLBuffer,
elementStride: Int,
Expand All @@ -77,6 +111,15 @@ extension BitonicSort {
)
}

/// Encodes the final pass of the sorting operation into a command buffer.
///
/// - Parameters:
/// - data: The buffer containing the data to be sorted.
/// - elementStride: The stride between elements in the buffer.
/// - params: Parameters for the sorting operation.
/// - gridSize: The size of the compute grid.
/// - unitSize: The size of each unit in the grid.
/// - commandBuffer: The command buffer to encode into.
func encode(
data: MTLBuffer,
elementStride: Int,
Expand All @@ -98,6 +141,15 @@ extension BitonicSort {
}
}

/// Encodes the final pass of the sorting operation using a compute command encoder.
///
/// - Parameters:
/// - data: The buffer containing the data to be sorted.
/// - elementStride: The stride between elements in the buffer.
/// - params: Parameters for the sorting operation.
/// - gridSize: The size of the compute grid.
/// - unitSize: The size of each unit in the grid.
/// - encoder: The compute command encoder to use.
func encode(
data: MTLBuffer,
elementStride: Int,
Expand Down
Loading

0 comments on commit a446cc4

Please sign in to comment.