Skip to content

Commit

Permalink
Style Fixes (#98)
Browse files Browse the repository at this point in the history
* remove warnings

* style fixes

* update version

* introduce several bugfixes, new APIs (#103)

* introduce several bugfixes, new APIs

* fix alignment

* Squashed commit of the following:

    fix TextureWeightedMix (#104)

    Add Texture Mix (#100)

    * add texture mix

    * naming

    * update version

* update encoder

* replace mask reading with mask sampling

* fix shared memory size

* fix typo in float conversion

* Texture Mask Inversion (#106)

* add inversion to TextureMask

* remove redundant annotation

* RGBA to YCbCr (#105)

* rgba to ycbcr

* update rgba to ycbcr

* migrate to gather

* fixes

* use to matrix multiplication

* use matrix transpose

* simplify mean calculation

Co-authored-by: Andrey Volodin <siddok@gmail.com>
  • Loading branch information
eugenebokhan and s1ddok authored Aug 26, 2020
1 parent e78d167 commit 8f6c5eb
Show file tree
Hide file tree
Showing 48 changed files with 1,213 additions and 1,027 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.15.1'
s.version = '0.16.0'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.summary = 'Nano helpers for Metal framework'
s.homepage = 'https://github.com/s1ddok/Alloy'
Expand Down
7 changes: 3 additions & 4 deletions Alloy/Encoders/EuclideanDistance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,16 @@ final public class EuclideanDistance {
let blockSize = BlockSize(width: blockSizeWidth,
height: blockSizeHeight)

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

let threadgroupMemoryLength = threadgroupSize.width
* threadgroupSize.height
* 4
* MemoryLayout<Float16>.stride
* MemoryLayout<SIMD4<Float>>.stride

encoder.setThreadgroupMemoryLength(threadgroupMemoryLength,
index: 0)
Expand Down
38 changes: 18 additions & 20 deletions Alloy/Encoders/LookUpTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,61 +26,59 @@ final public class LookUpTable {

// MARK: - Encode

public func callAsFunction(sourceTexture: MTLTexture,
outputTexture: MTLTexture,
public func callAsFunction(source: MTLTexture,
destination: MTLTexture,
lut: MTLTexture,
intensity: Float,
in commandBuffer: MTLCommandBuffer) {
self.encode(sourceTexture: sourceTexture,
outputTexture: outputTexture,
self.encode(source: source,
destination: destination,
lut: lut,
intensity: intensity,
in: commandBuffer)
}

public func callAsFunction(sourceTexture: MTLTexture,
outputTexture: MTLTexture,
public func callAsFunction(source: MTLTexture,
destination: MTLTexture,
lut: MTLTexture,
intensity: Float,
using encoder: MTLComputeCommandEncoder) {
self.encode(sourceTexture: sourceTexture,
outputTexture: outputTexture,
self.encode(source: source,
destination: destination,
lut: lut,
intensity: intensity,
using: encoder)
}

public func encode(sourceTexture: MTLTexture,
outputTexture: MTLTexture,
public func encode(source: MTLTexture,
destination: MTLTexture,
lut: MTLTexture,
intensity: Float,
in commandBuffer: MTLCommandBuffer) {
commandBuffer.compute { encoder in
encoder.label = "Look Up Table"
self.encode(sourceTexture: sourceTexture,
outputTexture: outputTexture,
self.encode(source: source,
destination: destination,
lut: lut,
intensity: intensity,
using: encoder)
}
}

public func encode(sourceTexture: MTLTexture,
outputTexture: MTLTexture,
public func encode(source: MTLTexture,
destination: MTLTexture,
lut: MTLTexture,
intensity: Float,
using encoder: MTLComputeCommandEncoder) {
encoder.set(textures: [sourceTexture,
outputTexture,
lut])
encoder.set(intensity, at: 0)
encoder.setTextures(source, destination, lut)
encoder.setValue(intensity, at: 0)

if self.deviceSupportsNonuniformThreadgroups {
encoder.dispatch2d(state: pipelineState,
exactly: outputTexture.size)
exactly: destination.size)
} else {
encoder.dispatch2d(state: pipelineState,
covering: outputTexture.size)
covering: destination.size)
}
}

Expand Down
25 changes: 12 additions & 13 deletions Alloy/Encoders/MPSUnaryImageKernels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@ final public class MPSUnaryImageKernels {

// MARK: - Encode

public func callAsFunction(sourceTexture: MTLTexture,
destinationTexture: MTLTexture,
public func callAsFunction(source: MTLTexture,
destination: MTLTexture,
in commandBuffer: MTLCommandBuffer) {
self.encode(sourceTexture: sourceTexture,
destinationTexture: destinationTexture,
self.encode(source: source,
destination: destination,
in: commandBuffer)
}

public func encode(sourceTexture: MTLTexture,
destinationTexture: MTLTexture,
public func encode(source: MTLTexture,
destination: MTLTexture,
in commandBuffer: MTLCommandBuffer) {
guard self.kernelQueue
.count != 0
guard !self.kernelQueue.isEmpty
else { return }

let textureDescriptor = sourceTexture.descriptor
let textureDescriptor = source.descriptor
textureDescriptor.usage = [.shaderRead, .shaderWrite]
textureDescriptor.storageMode = .private
// We need only 2 temporary images in the worst case.
Expand All @@ -43,12 +42,12 @@ final public class MPSUnaryImageKernels {
if self.kernelQueue.count == 1 {
self.kernelQueue[0]
.encode(commandBuffer: commandBuffer,
sourceTexture: sourceTexture,
destinationTexture: destinationTexture)
sourceTexture: source,
destinationTexture: destination)
} else {
self.kernelQueue[0]
.encode(commandBuffer: commandBuffer,
sourceTexture: sourceTexture,
sourceTexture: source,
destinationTexture: temporaryImages[0].texture)

for i in 1 ..< self.kernelQueue.count - 1 {
Expand All @@ -63,7 +62,7 @@ final public class MPSUnaryImageKernels {
self.kernelQueue[self.kernelQueue.count - 1]
.encode(commandBuffer: commandBuffer,
sourceTexture: temporaryImages[0].texture,
destinationTexture: destinationTexture)
destinationTexture: destination)
}
}
}
40 changes: 18 additions & 22 deletions Alloy/Encoders/MaskGuidedBlur.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ final public class MaskGuidedBlur {

// MARK: - Encode

public func callAsFunction(sourceTexture: MTLTexture,
maskTexture: MTLTexture,
destinationTexture: MTLTexture,
public func callAsFunction(source: MTLTexture,
mask: MTLTexture,
destination: MTLTexture,
sigma: Float,
in commandBuffer: MTLCommandBuffer) {
self.encode(sourceTexture: sourceTexture,
maskTexture: maskTexture,
destinationTexture: destinationTexture,
self.encode(source: source,
mask: mask,
destination: destination,
sigma: sigma,
in: commandBuffer)
}

public func encode(sourceTexture: MTLTexture,
maskTexture: MTLTexture,
destinationTexture: MTLTexture,
public func encode(source: MTLTexture,
mask: MTLTexture,
destination: MTLTexture,
sigma: Float,
in commandBuffer: MTLCommandBuffer) {
let temporaryTextureDescriptor = sourceTexture.descriptor
let temporaryTextureDescriptor = source.descriptor
temporaryTextureDescriptor.usage = [.shaderRead, .shaderWrite]
temporaryTextureDescriptor.storageMode = .private
temporaryTextureDescriptor.pixelFormat = .rgba8Unorm
Expand All @@ -57,30 +57,26 @@ final public class MaskGuidedBlur {
textureDescriptor: temporaryTextureDescriptor)
defer { temporaryImage.readCount = 0 }

encoder.set(textures: [sourceTexture,
maskTexture,
temporaryImage.texture])
encoder.set(sigma, at: 0)
encoder.setTextures(source, mask, temporaryImage.texture)
encoder.setValue(sigma, at: 0)

if self.deviceSupportsNonuniformThreadgroups {
encoder.dispatch2d(state: self.blurRowPassState,
exactly: sourceTexture.size)
exactly: source.size)
} else {
encoder.dispatch2d(state: self.blurRowPassState,
covering: sourceTexture.size)
covering: source.size)
}

encoder.set(textures: [temporaryImage.texture,
maskTexture,
destinationTexture])
encoder.set(sigma, at: 0)
encoder.setTextures(temporaryImage.texture, mask, destination)
encoder.setValue(sigma, at: 0)

if self.deviceSupportsNonuniformThreadgroups {
encoder.dispatch2d(state: self.blurColumnPassState,
exactly: sourceTexture.size)
exactly: source.size)
} else {
encoder.dispatch2d(state: self.blurColumnPassState,
covering: sourceTexture.size)
covering: source.size)
}
}
}
Expand Down
39 changes: 19 additions & 20 deletions Alloy/Encoders/NormalizeKernel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,61 +26,60 @@ final public class NormalizeKernel {

// MARK: - Encode

public func callAsFunction(sourceTexture: MTLTexture,
destinationTexture: MTLTexture,
public func callAsFunction(source: MTLTexture,
destination: MTLTexture,
mean: SIMD3<Float>,
std: SIMD3<Float>,
in commandBuffer: MTLCommandBuffer) {
self.encode(sourceTexture: sourceTexture,
destinationTexture: destinationTexture,
self.encode(source: source,
destination: destination,
mean: mean,
std: std,
in: commandBuffer)
}

public func callAsFunction(sourceTexture: MTLTexture,
destinationTexture: MTLTexture,
public func callAsFunction(source: MTLTexture,
destination: MTLTexture,
mean: SIMD3<Float>,
std: SIMD3<Float>,
using encoder: MTLComputeCommandEncoder) {
self.encode(sourceTexture: sourceTexture,
destinationTexture: destinationTexture,
self.encode(source: source,
destination: destination,
mean: mean,
std: std,
using: encoder)
}

public func encode(sourceTexture: MTLTexture,
destinationTexture: MTLTexture,
public func encode(source: MTLTexture,
destination: MTLTexture,
mean: SIMD3<Float>,
std: SIMD3<Float>,
in commandBuffer: MTLCommandBuffer) {
commandBuffer.compute { encoder in
encoder.label = "Normalize Kernel"
self.encode(sourceTexture: sourceTexture,
destinationTexture: destinationTexture,
self.encode(source: source,
destination: destination,
mean: mean,
std: std,
using: encoder)
}
}

public func encode(sourceTexture: MTLTexture,
destinationTexture: MTLTexture,
public func encode(source: MTLTexture,
destination: 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)
encoder.setTextures(source, destination)
encoder.setValue(mean, at: 0)
encoder.setValue(std, at: 1)

if self.deviceSupportsNonuniformThreadgroups {
encoder.dispatch2d(state: self.pipelineState,
exactly: destinationTexture.size)
exactly: destination.size)
} else {
encoder.dispatch2d(state: self.pipelineState,
covering: destinationTexture.size)
covering: destination.size)
}
}

Expand Down
Loading

0 comments on commit 8f6c5eb

Please sign in to comment.