-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import Metal | ||
import simd | ||
|
||
final public class StdMeanNormalization { | ||
|
||
// MARK: - Propertires | ||
|
||
public let pipelineState: MTLComputePipelineState | ||
private let deviceSupportsNonuniformThreadgroups: Bool | ||
|
||
// MARK: - Life Cycle | ||
|
||
public convenience init(context: MTLContext) throws { | ||
try self.init(library: context.library(for: Self.self)) | ||
} | ||
|
||
public init(library: MTLLibrary) throws { | ||
self.deviceSupportsNonuniformThreadgroups = library.device | ||
.supports(feature: .nonUniformThreadgroups) | ||
let constantValues = MTLFunctionConstantValues() | ||
constantValues.set(self.deviceSupportsNonuniformThreadgroups, | ||
at: 0) | ||
self.pipelineState = try library.computePipelineState(function: Self.functionName, | ||
constants: constantValues) | ||
} | ||
|
||
// MARK: - Encode | ||
|
||
public func callAsFunction(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
mean: SIMD3<Float>, | ||
std: SIMD3<Float>, | ||
in commandBuffer: MTLCommandBuffer) { | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
mean: mean, | ||
std: std, | ||
in: commandBuffer) | ||
} | ||
|
||
public func callAsFunction(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
mean: SIMD3<Float>, | ||
std: SIMD3<Float>, | ||
using encoder: MTLComputeCommandEncoder) { | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
mean: mean, | ||
std: std, | ||
using: encoder) | ||
} | ||
|
||
public func encode(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
mean: SIMD3<Float>, | ||
std: SIMD3<Float>, | ||
in commandBuffer: MTLCommandBuffer) { | ||
commandBuffer.compute { encoder in | ||
encoder.label = "Normalize Kernel" | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
mean: mean, | ||
std: std, | ||
using: encoder) | ||
} | ||
} | ||
|
||
public func encode(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
mean: SIMD3<Float>, | ||
std: SIMD3<Float>, | ||
using encoder: MTLComputeCommandEncoder) { | ||
encoder.set(textures: [sourceTexture, | ||
destinationTexture]) | ||
encoder.set(mean, at: 0) | ||
encoder.set(std, at: 1) | ||
|
||
if self.deviceSupportsNonuniformThreadgroups { | ||
encoder.dispatch2d(state: self.pipelineState, | ||
exactly: destinationTexture.size) | ||
} else { | ||
encoder.dispatch2d(state: self.pipelineState, | ||
covering: destinationTexture.size) | ||
} | ||
} | ||
|
||
public static let functionName = "normalize" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Metal | ||
import simd | ||
|
||
final public class TextureAffineCrop { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import Metal | ||
|
||
final public class TextureDivideByConstant { | ||
|
||
// MARK: - Properties | ||
|
||
public let pipelineState: MTLComputePipelineState | ||
private let deviceSupportsNonuniformThreadgroups: Bool | ||
|
||
// MARK: - Init | ||
|
||
public convenience init(context: MTLContext, | ||
scalarType: MTLPixelFormat.ScalarType = .half) throws { | ||
try self.init(library: context.library(for: Self.self), | ||
scalarType: scalarType) | ||
} | ||
|
||
public init(library: MTLLibrary, | ||
scalarType: MTLPixelFormat.ScalarType = .half) throws { | ||
self.deviceSupportsNonuniformThreadgroups = library.device | ||
.supports(feature: .nonUniformThreadgroups) | ||
|
||
let constantValues = MTLFunctionConstantValues() | ||
constantValues.set(self.deviceSupportsNonuniformThreadgroups, | ||
at: 0) | ||
let functionName = Self.functionName + "_" + scalarType.rawValue | ||
self.pipelineState = try library.computePipelineState(function: functionName, | ||
constants: constantValues) | ||
} | ||
|
||
// MARK: - Encode | ||
|
||
public func callAsFunction(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
constant: SIMD4<Float>, | ||
in commandBuffer: MTLCommandBuffer) { | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
constant: constant, | ||
in: commandBuffer) | ||
} | ||
|
||
public func callAsFunction(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
constant: SIMD4<Float>, | ||
using encoder: MTLComputeCommandEncoder) { | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
constant: constant, | ||
using: encoder) | ||
} | ||
|
||
public func encode(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
constant: SIMD4<Float>, | ||
in commandBuffer: MTLCommandBuffer) { | ||
commandBuffer.compute { encoder in | ||
encoder.label = "Texture Divide by Constant" | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
constant: constant, | ||
using: encoder) | ||
} | ||
} | ||
|
||
public func encode(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
constant: SIMD4<Float>, | ||
using encoder: MTLComputeCommandEncoder) { | ||
encoder.set(textures: [sourceTexture, | ||
destinationTexture]) | ||
encoder.set(constant, at: 0) | ||
|
||
if self.deviceSupportsNonuniformThreadgroups { | ||
encoder.dispatch2d(state: self.pipelineState, | ||
exactly: sourceTexture.size) | ||
} else { | ||
encoder.dispatch2d(state: self.pipelineState, | ||
covering: sourceTexture.size) | ||
} | ||
} | ||
|
||
public func callAsFunction(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
constant: MTLBuffer, | ||
in commandBuffer: MTLCommandBuffer) { | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
constant: constant, | ||
in: commandBuffer) | ||
} | ||
|
||
public func callAsFunction(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
constant: MTLBuffer, | ||
using encoder: MTLComputeCommandEncoder) { | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
constant: constant, | ||
using: encoder) | ||
} | ||
|
||
public func encode(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
constant: MTLBuffer, | ||
in commandBuffer: MTLCommandBuffer) { | ||
commandBuffer.compute { encoder in | ||
encoder.label = "Texture Divide by Constant" | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
constant: constant, | ||
using: encoder) | ||
} | ||
} | ||
|
||
public func encode(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
constant: MTLBuffer, | ||
using encoder: MTLComputeCommandEncoder) { | ||
encoder.set(textures: [sourceTexture, | ||
destinationTexture]) | ||
encoder.setBuffer(constant, offset: 0, index: 0) | ||
|
||
if self.deviceSupportsNonuniformThreadgroups { | ||
encoder.dispatch2d(state: self.pipelineState, | ||
exactly: sourceTexture.size) | ||
} else { | ||
encoder.dispatch2d(state: self.pipelineState, | ||
covering: sourceTexture.size) | ||
} | ||
} | ||
|
||
public static let functionName = "divideByConstant" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Metal | ||
|
||
final public class TextureNormalization { | ||
|
||
// MARK: - Properties | ||
|
||
private let textureMax: TextureMax | ||
private let textureDivide: TextureDivideByConstant | ||
private let intermediateBuffer: MTLBuffer | ||
|
||
// MARK: - Life Cycle | ||
|
||
public convenience init(context: MTLContext) throws { | ||
try self.init(library: context.library(for: Self.self)) | ||
} | ||
|
||
public init(library: MTLLibrary) throws { | ||
self.textureDivide = try .init(library: library) | ||
self.textureMax = try .init(library: library) | ||
self.intermediateBuffer = try library.device.buffer(for: SIMD4<Float>.self, | ||
options: .storageModePrivate) | ||
} | ||
|
||
// MARK: - Encode | ||
|
||
public func callAsFunction(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
in commandBuffer: MTLCommandBuffer) { | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
in: commandBuffer) | ||
} | ||
|
||
public func callAsFunction(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
using encoder: MTLComputeCommandEncoder) { | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
using: encoder) | ||
} | ||
|
||
public func encode(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
in commandBuffer: MTLCommandBuffer) { | ||
commandBuffer.compute { encoder in | ||
encoder.label = "Texture Normalization" | ||
self.encode(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
using: encoder) | ||
} | ||
} | ||
|
||
public func encode(sourceTexture: MTLTexture, | ||
destinationTexture: MTLTexture, | ||
using encoder: MTLComputeCommandEncoder) { | ||
self.textureMax(sourceTexture: sourceTexture, | ||
resultBuffer: self.intermediateBuffer, | ||
using: encoder) | ||
self.textureDivide(sourceTexture: sourceTexture, | ||
destinationTexture: destinationTexture, | ||
constant: self.intermediateBuffer, | ||
using: encoder) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters