-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Upscaling CIFilter + CIImageProcessorKernel
- Loading branch information
Showing
6 changed files
with
132 additions
and
1 deletion.
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
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,48 @@ | ||
import CoreImage | ||
import Foundation | ||
#if canImport(MetalFX) | ||
import MetalFX | ||
#endif | ||
|
||
// MARK: - UpscalingFilter | ||
|
||
public class UpscalingFilter: CIFilter { | ||
// MARK: Public | ||
|
||
public var inputImage: CIImage? | ||
public var outputSize: CGSize? | ||
|
||
override public var outputImage: CIImage? { | ||
#if canImport(MetalFX) | ||
guard let device, let inputImage, let outputSize else { return nil } | ||
|
||
if spatialScaler?.inputSize != inputImage.extent.size || spatialScaler?.outputSize != outputSize { | ||
let spatialScalerDescriptor = MTLFXSpatialScalerDescriptor() | ||
spatialScalerDescriptor.inputSize = inputImage.extent.size | ||
spatialScalerDescriptor.outputSize = outputSize | ||
spatialScalerDescriptor.colorTextureFormat = .bgra8Unorm | ||
spatialScalerDescriptor.outputTextureFormat = .bgra8Unorm | ||
spatialScalerDescriptor.colorProcessingMode = .perceptual | ||
spatialScaler = spatialScalerDescriptor.makeSpatialScaler(device: device) | ||
} | ||
|
||
guard let spatialScaler else { return nil } | ||
|
||
return try? UpscalingImageProcessorKernel.apply( | ||
withExtent: CGRect(origin: .zero, size: spatialScaler.outputSize), | ||
inputs: [inputImage], | ||
arguments: ["spatialScaler": spatialScaler] | ||
) | ||
#else | ||
return inputImage | ||
#endif | ||
} | ||
|
||
// MARK: Private | ||
|
||
private let device = MTLCreateSystemDefaultDevice() | ||
|
||
#if canImport(MetalFX) | ||
private var spatialScaler: MTLFXSpatialScaler? | ||
#endif | ||
} |
47 changes: 47 additions & 0 deletions
47
Sources/Upscaling/CoreImage/UpscalingImageProcessorKernel.swift
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,47 @@ | ||
import CoreImage | ||
import Foundation | ||
#if canImport(MetalFX) | ||
import MetalFX | ||
#endif | ||
|
||
// MARK: - UpscalingImageProcessorKernel | ||
|
||
public class UpscalingImageProcessorKernel: CIImageProcessorKernel { | ||
override public class var synchronizeInputs: Bool { false } | ||
override public class var outputFormat: CIFormat { .BGRA8 } | ||
|
||
override public class func formatForInput(at _: Int32) -> CIFormat { .BGRA8 } | ||
|
||
override public class func process( | ||
with inputs: [any CIImageProcessorInput]?, | ||
arguments: [String: Any]?, | ||
output: any CIImageProcessorOutput | ||
) throws { | ||
#if canImport(MetalFX) | ||
guard let spatialScaler = arguments?["spatialScaler"] as? MTLFXSpatialScaler, | ||
let inputTexture = inputs?.first?.metalTexture, | ||
let outputTexture = output.metalTexture, | ||
let commandBuffer = output.metalCommandBuffer else { | ||
return | ||
} | ||
spatialScaler.colorTexture = inputTexture | ||
spatialScaler.outputTexture = outputTexture | ||
spatialScaler.encode(commandBuffer: commandBuffer) | ||
#endif | ||
} | ||
|
||
override public class func roi( | ||
forInput _: Int32, | ||
arguments: [String: Any]?, | ||
outputRect: CGRect | ||
) -> CGRect { | ||
#if canImport(MetalFX) | ||
guard let spatialScaler = arguments?["spatialScaler"] as? MTLFXSpatialScaler else { | ||
return .null | ||
} | ||
return CGRect(origin: .zero, size: spatialScaler.inputSize) | ||
#else | ||
return outputRect | ||
#endif | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Sources/Upscaling/Extensions/MTLFXSpatialScaler+Extensions.swift
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,13 @@ | ||
#if canImport(MetalFX) | ||
import MetalFX | ||
|
||
extension MTLFXSpatialScaler { | ||
var inputSize: CGSize { | ||
CGSize(width: inputWidth, height: inputHeight) | ||
} | ||
|
||
var outputSize: CGSize { | ||
CGSize(width: outputWidth, height: outputHeight) | ||
} | ||
} | ||
#endif |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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