Skip to content

Commit

Permalink
stable alloy
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenebokhan committed Dec 27, 2019
1 parent 4fd57d9 commit 66ef7c8
Show file tree
Hide file tree
Showing 45 changed files with 1,082 additions and 768 deletions.
2 changes: 1 addition & 1 deletion Alloy.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Alloy'
s.version = '0.11.7'
s.version = '0.12.0'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.summary = 'Nano helpers for Metal framework'
s.homepage = 'https://github.com/s1ddok/Alloy'
Expand Down
49 changes: 34 additions & 15 deletions Alloy/CVPixelBuffer+MTLTexture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ import Metal
import CoreVideo.CVPixelBuffer

public extension CVPixelBuffer {
func metalTexture(using cache: CVMetalTextureCache, pixelFormat: MTLPixelFormat, planeIndex: Int = 0) -> MTLTexture? {

func metalTexture(using cache: CVMetalTextureCache,
pixelFormat: MTLPixelFormat,
planeIndex: Int = 0) -> MTLTexture? {
let width = CVPixelBufferGetWidthOfPlane(self, planeIndex)
let height = CVPixelBufferGetHeightOfPlane(self, planeIndex)

var texture: CVMetalTexture? = nil
let status = CVMetalTextureCacheCreateTextureFromImage(nil, cache, self, nil, pixelFormat, width, height, planeIndex, &texture)
let status = CVMetalTextureCacheCreateTextureFromImage(nil,
cache,
self,
nil,
pixelFormat,
width,
height,
planeIndex,
&texture)

var retVal: MTLTexture? = nil
if status == kCVReturnSuccess {
Expand All @@ -24,21 +35,29 @@ public extension CVPixelBuffer {

return retVal
}

}

public extension MTLContext {
func makeTextureCache(textureAge: Float = 1.0) -> CVMetalTextureCache? {
let options = [kCVMetalTextureCacheMaximumTextureAgeKey as NSString: NSNumber(value: textureAge)] as NSDictionary

func textureCache(textureAge: Float = 1.0) throws -> CVMetalTextureCache {
let textureAgeKey = kCVMetalTextureCacheMaximumTextureAgeKey as NSString
let textureAgeValue = NSNumber(value: textureAge)
let options = [textureAgeKey: textureAgeValue] as NSDictionary

var videoTextureCache: CVMetalTextureCache? = nil
let textureCacheError = CVMetalTextureCacheCreate(kCFAllocatorDefault, options, device, nil, &videoTextureCache);
if textureCacheError != kCVReturnSuccess {
print("ERROR: Wasn't able to create CVMetalTextureCache")
return nil
var videoTextureCache: CVMetalTextureCache! = nil
let status = CVMetalTextureCacheCreate(kCFAllocatorDefault,
options,
self.device,
nil,
&videoTextureCache)
if status != kCVReturnSuccess {
throw MetalError.MTLContextError.textureCacheCreationFailed
}

return videoTextureCache
}

}

public extension MTLTexture {
Expand All @@ -49,12 +68,12 @@ public extension MTLTexture {
else { return nil }

var pb: CVPixelBuffer? = nil
var status = try CVPixelBufferCreate(nil,
self.width,
self.height,
cvPixelFormat,
nil,
&pb)
var status = CVPixelBufferCreate(nil,
self.width,
self.height,
cvPixelFormat,
nil,
&pb)
guard status == kCVReturnSuccess,
let pixelBuffer = pb
else { return nil }
Expand Down
9 changes: 8 additions & 1 deletion Alloy/Encoders/BlockSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ public struct BlockSize {
public var width: UInt16
public var height: UInt16

public init(width: UInt16, height: UInt16) {
public init(width: UInt16,
height: UInt16) {
self.width = width
self.height = height
}

public init(width: Int,
height: Int) {
self.width = .init(width)
self.height = .init(height)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// EuclideanDistanceEncoder.swift
// EuclideanDistance.swift
// Alloy
//
// Created by Eugene Bokhan on 30/08/2019.
//

import Metal

final public class EuclideanDistanceEncoder {
final public class EuclideanDistance {

// MARK: - Properties

Expand All @@ -17,15 +17,15 @@ final public class EuclideanDistanceEncoder {

convenience public init(context: MTLContext,
scalarType: MTLPixelFormat.ScalarType = .half) throws {
guard let alloyLibrary = context.shaderLibrary(for: type(of: self))
guard let library = context.library(for: Self.self)
else { throw MetalError.MTLDeviceError.libraryCreationFailed }
try self.init(library: alloyLibrary,
try self.init(library: library,
scalarType: scalarType)
}

public init(library: MTLLibrary,
scalarType: MTLPixelFormat.ScalarType = .half) throws {
let functionName = type(of: self).functionName + "_" + scalarType.rawValue
let functionName = Self.functionName + "_" + scalarType.rawValue
self.pipelineState = try library.computePipelineState(function: functionName)
}

Expand All @@ -49,19 +49,29 @@ final public class EuclideanDistanceEncoder {
resultBuffer: MTLBuffer,
using encoder: MTLComputeCommandEncoder) {
let threadgroupSize = MTLSize(width: 8, height: 8, depth: 1).clamped(to: textureOne.size)
let blockSize = BlockSize(width: .init((textureOne.width + threadgroupSize.width - 1) / threadgroupSize.width),
height: .init((textureOne.height + threadgroupSize.height - 1) / threadgroupSize.height))
let blockSizeWidth = (textureOne.width + threadgroupSize.width - 1)
/ threadgroupSize.width
let blockSizeHeight = (textureOne.height + threadgroupSize.height - 1)
/ threadgroupSize.height
let blockSize = BlockSize(width: blockSizeWidth,
height: blockSizeHeight)

encoder.set(textures: [textureOne, textureTwo])
encoder.set(textures: [textureOne,
textureTwo])
encoder.set(blockSize, at: 0)
encoder.setBuffer(resultBuffer,
offset: 0,
index: 1)

encoder.setThreadgroupMemoryLength(threadgroupSize.width * threadgroupSize.height * 4 * MemoryLayout<Float16>.stride,
let threadgroupMemoryLength = threadgroupSize.width
* threadgroupSize.height
* 4
* MemoryLayout<Float16>.stride

encoder.setThreadgroupMemoryLength(threadgroupMemoryLength,
index: 0)
encoder.dispatch2d(state: self.pipelineState,
covering: .init(width: 1, height: 1, depth: 1),
covering: .one,
threadgroupSize: threadgroupSize)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// LookUpTableEncoder.swift
// LookUpTable.swift
// Alloy
//
// Created by Andrey Volodin on 29.10.2019.
//

import Metal

final public class LookUpTableEncoder {
final public class LookUpTable {

// MARK: - Properties

Expand All @@ -17,17 +17,18 @@ final public class LookUpTableEncoder {
// MARK: - Life Cycle

public convenience init(context: MTLContext) throws {
guard let library = context.shaderLibrary(for: type(of: self))
guard let library = context.library(for: Self.self)
else { throw MetalError.MTLDeviceError.libraryCreationFailed }
try self.init(library: library)
}

public init(library: MTLLibrary) throws {
self.deviceSupportsNonuniformThreadgroups = library.device.supports(feature: .nonUniformThreadgroups)
self.deviceSupportsNonuniformThreadgroups = library.device
.supports(feature: .nonUniformThreadgroups)
let constantValues = MTLFunctionConstantValues()
constantValues.set(self.deviceSupportsNonuniformThreadgroups,
at: 0)
let functionName = type(of: self).functionName
let functionName = Self.functionName
self.pipelineState = try library.computePipelineState(function: functionName,
constants: constantValues)
}
Expand All @@ -40,7 +41,7 @@ final public class LookUpTableEncoder {
intensity: Float,
in commandBuffer: MTLCommandBuffer) {
commandBuffer.compute { encoder in
encoder.label = "Look Up Table Encoder"
encoder.label = "Look Up Table"
self.encode(sourceTexture: sourceTexture,
outputTexture: outputTexture,
lut: lut,
Expand All @@ -54,7 +55,9 @@ final public class LookUpTableEncoder {
lut: MTLTexture,
intensity: Float,
using encoder: MTLComputeCommandEncoder) {
encoder.set(textures: [sourceTexture, outputTexture, lut])
encoder.set(textures: [sourceTexture,
outputTexture,
lut])
encoder.set(intensity, at: 0)

if self.deviceSupportsNonuniformThreadgroups {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// MPSUnaryImageKernelsEncoder.swift
// MPSUnaryImageKernels.swift
// Alloy
//
// Created by Eugene Bokhan on 27.09.2019.
//

import MetalPerformanceShaders

final public class MPSUnaryImageKernelsEncoder {
final public class MPSUnaryImageKernels {

// MARK: - Properties

Expand All @@ -23,7 +23,7 @@ final public class MPSUnaryImageKernelsEncoder {

public func encode(sourceTexture: MTLTexture,
destinationTexture: MTLTexture,
commandBuffer: MTLCommandBuffer) {
in commandBuffer: MTLCommandBuffer) {
guard self.kernelQueue
.count != 0
else { return }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MaskGuidedBlurEncoder.swift
// MaskGuidedBlur.swift
// Alloy
//
// Created by Eugene Bokhan on 29/08/2019.
Expand All @@ -8,7 +8,7 @@
import Metal
import MetalPerformanceShaders

final public class MaskGuidedBlurEncoder {
final public class MaskGuidedBlur {

// MARK: - Propertires

Expand All @@ -19,19 +19,20 @@ final public class MaskGuidedBlurEncoder {
// MARK: - Life Cycle

public convenience init(context: MTLContext) throws {
guard let library = context.shaderLibrary(for: type(of: self))
guard let library = context.library(for: Self.self)
else { throw MetalError.MTLDeviceError.libraryCreationFailed }
try self.init(library: library)
}

public init(library: MTLLibrary) throws {
self.deviceSupportsNonuniformThreadgroups = library.device.supports(feature: .nonUniformThreadgroups)
self.deviceSupportsNonuniformThreadgroups = library.device
.supports(feature: .nonUniformThreadgroups)
let constantValues = MTLFunctionConstantValues()
constantValues.set(self.deviceSupportsNonuniformThreadgroups,
at: 0)
self.blurRowPassState = try library.computePipelineState(function: type(of: self).blurRowPassFunctionName,
self.blurRowPassState = try library.computePipelineState(function: Self.blurRowPassFunctionName,
constants: constantValues)
self.blurColumnPassState = try library.computePipelineState(function: type(of: self).blurColumnPassFunctionName,
self.blurColumnPassState = try library.computePipelineState(function: Self.blurColumnPassFunctionName,
constants: constantValues)
}

Expand All @@ -48,6 +49,7 @@ final public class MaskGuidedBlurEncoder {
temporaryTextureDescriptor.pixelFormat = .rgba8Unorm

commandBuffer.compute { encoder in
encoder.label = "Mask Guided Blur"
let temporaryImage = MPSTemporaryImage(commandBuffer: commandBuffer,
textureDescriptor: temporaryTextureDescriptor)
defer { temporaryImage.readCount = 0 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// NormalizeKernelEncoder.swift
// Alloy-iOS
// NormalizeKernel.swift
// Alloy
//
// Created by Eugene Bokhan on 08/05/2019.
//

import Metal
import simd

final public class NormalizeKernelEncoder {
final public class NormalizeKernel {

// MARK: - Propertires

Expand All @@ -18,26 +18,27 @@ final public class NormalizeKernelEncoder {
// MARK: - Life Cycle

public convenience init(context: MTLContext) throws {
guard let library = context.shaderLibrary(for: type(of: self))
guard let library = context.library(for: Self.self)
else { throw MetalError.MTLDeviceError.libraryCreationFailed }
try self.init(library: library)
}

public init(library: MTLLibrary) throws {
self.deviceSupportsNonuniformThreadgroups = library.device.supports(feature: .nonUniformThreadgroups)
self.deviceSupportsNonuniformThreadgroups = library.device
.supports(feature: .nonUniformThreadgroups)
let constantValues = MTLFunctionConstantValues()
constantValues.set(self.deviceSupportsNonuniformThreadgroups,
at: 0)
self.pipelineState = try library.computePipelineState(function: type(of: self).functionName,
self.pipelineState = try library.computePipelineState(function: Self.functionName,
constants: constantValues)
}

// MARK: - Encode

public func encode(sourceTexture: MTLTexture,
destinationTexture: MTLTexture,
mean: vector_float3,
std: vector_float3,
mean: SIMD3<Float>,
std: SIMD3<Float>,
in commandBuffer: MTLCommandBuffer) {
commandBuffer.compute { encoder in
encoder.label = "Normalize Kernel"
Expand All @@ -51,10 +52,11 @@ final public class NormalizeKernelEncoder {

public func encode(sourceTexture: MTLTexture,
destinationTexture: MTLTexture,
mean: vector_float3,
std: vector_float3,
mean: SIMD3<Float>,
std: SIMD3<Float>,
using encoder: MTLComputeCommandEncoder) {
encoder.set(textures: [sourceTexture, destinationTexture])
encoder.set(textures: [sourceTexture,
destinationTexture])
encoder.set(mean, at: 0)
encoder.set(std, at: 1)

Expand Down
Loading

0 comments on commit 66ef7c8

Please sign in to comment.