Skip to content

Commit

Permalink
Handle non-private storage renders in UpscalingFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
finnvoor committed Jun 5, 2024
1 parent 7f44147 commit b3a76b5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 14 additions & 2 deletions Sources/Upscaling/CoreImage/UpscalingFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,25 @@ public class UpscalingFilter: CIFilter {
spatialScalerDescriptor.outputTextureFormat = .bgra8Unorm
spatialScalerDescriptor.colorProcessingMode = .perceptual
spatialScaler = spatialScalerDescriptor.makeSpatialScaler(device: device)

let textureDescriptor = MTLTextureDescriptor()
textureDescriptor.width = Int(outputSize.width)
textureDescriptor.height = Int(outputSize.height)
textureDescriptor.pixelFormat = .bgra8Unorm
textureDescriptor.storageMode = .private
textureDescriptor.usage = [.renderTarget, .shaderRead]
intermediateOutputTexture = device.makeTexture(descriptor: textureDescriptor)
}

guard let spatialScaler else { return nil }
guard let spatialScaler, let intermediateOutputTexture else { return nil }

return try? UpscalingImageProcessorKernel.apply(
withExtent: CGRect(origin: .zero, size: spatialScaler.outputSize),
inputs: [inputImage],
arguments: ["spatialScaler": spatialScaler]
arguments: [
"spatialScaler": spatialScaler,
"intermediateOutputTexture": intermediateOutputTexture
]
)
#else
return inputImage
Expand All @@ -45,4 +56,5 @@ public class UpscalingFilter: CIFilter {
#if canImport(MetalFX)
private var spatialScaler: MTLFXSpatialScaler?
#endif
private var intermediateOutputTexture: MTLTexture?
}
15 changes: 13 additions & 2 deletions Sources/Upscaling/CoreImage/UpscalingImageProcessorKernel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,19 @@ public class UpscalingImageProcessorKernel: CIImageProcessorKernel {
return
}
spatialScaler.colorTexture = inputTexture
spatialScaler.outputTexture = outputTexture
spatialScaler.encode(commandBuffer: commandBuffer)
if outputTexture.storageMode == .private {
spatialScaler.outputTexture = outputTexture
spatialScaler.encode(commandBuffer: commandBuffer)
} else {
guard let intermediateOutputTexture = arguments?["intermediateOutputTexture"] as? MTLTexture else {
return
}
spatialScaler.outputTexture = intermediateOutputTexture
spatialScaler.encode(commandBuffer: commandBuffer)
let blitCommandEncoder = commandBuffer.makeBlitCommandEncoder()
blitCommandEncoder?.copy(from: intermediateOutputTexture, to: outputTexture)
blitCommandEncoder?.endEncoding()
}
#endif
}

Expand Down

0 comments on commit b3a76b5

Please sign in to comment.