|
| 1 | +// |
| 2 | +// LookUpTableEncoder.swift |
| 3 | +// Alloy |
| 4 | +// |
| 5 | +// Created by Andrey Volodin on 29.10.2019. |
| 6 | +// |
| 7 | + |
| 8 | +import Metal |
| 9 | + |
| 10 | +final public class LookUpTableEncoder { |
| 11 | + |
| 12 | + // MARK: - Properties |
| 13 | + |
| 14 | + public let pipelineState: MTLComputePipelineState |
| 15 | + private let deviceSupportsNonuniformThreadgroups: Bool |
| 16 | + |
| 17 | + // MARK: - Life Cycle |
| 18 | + |
| 19 | + public convenience init(context: MTLContext) throws { |
| 20 | + guard let library = context.shaderLibrary(for: type(of: self)) |
| 21 | + else { throw CommonErrors.metalInitializationFailed } |
| 22 | + try self.init(library: library) |
| 23 | + } |
| 24 | + |
| 25 | + public init(library: MTLLibrary) throws { |
| 26 | + self.deviceSupportsNonuniformThreadgroups = library.device.supports(feature: .nonUniformThreadgroups) |
| 27 | + let constantValues = MTLFunctionConstantValues() |
| 28 | + constantValues.set(self.deviceSupportsNonuniformThreadgroups, |
| 29 | + at: 0) |
| 30 | + let functionName = type(of: self).functionName |
| 31 | + self.pipelineState = try library.computePipelineState(function: functionName, |
| 32 | + constants: constantValues) |
| 33 | + } |
| 34 | + |
| 35 | + // MARK: - Encode |
| 36 | + |
| 37 | + public func encode(sourceTexture: MTLTexture, |
| 38 | + outputTexture: MTLTexture, |
| 39 | + lut: MTLTexture, |
| 40 | + intensity: Float, |
| 41 | + in commandBuffer: MTLCommandBuffer) { |
| 42 | + commandBuffer.compute { encoder in |
| 43 | + encoder.label = "Look Up Table Encoder" |
| 44 | + self.encode(sourceTexture: sourceTexture, |
| 45 | + outputTexture: outputTexture, |
| 46 | + lut: lut, |
| 47 | + intensity: intensity, |
| 48 | + using: encoder) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public func encode(sourceTexture: MTLTexture, |
| 53 | + outputTexture: MTLTexture, |
| 54 | + lut: MTLTexture, |
| 55 | + intensity: Float, |
| 56 | + using encoder: MTLComputeCommandEncoder) { |
| 57 | + encoder.set(textures: [sourceTexture, outputTexture, lut]) |
| 58 | + encoder.set(intensity, at: 0) |
| 59 | + |
| 60 | + if self.deviceSupportsNonuniformThreadgroups { |
| 61 | + encoder.dispatch2d(state: pipelineState, |
| 62 | + exactly: outputTexture.size) |
| 63 | + } else { |
| 64 | + encoder.dispatch2d(state: pipelineState, |
| 65 | + covering: outputTexture.size) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public static let functionName = "lookUpTable" |
| 70 | +} |
0 commit comments