Camera and editor improvements

This commit is contained in:
Ilya Laktyushin
2023-05-29 16:27:26 +04:00
parent 69676bae7c
commit f11c45070e
6 changed files with 71 additions and 99 deletions

View File

@@ -210,11 +210,17 @@ final class VideoTextureSource: NSObject, TextureSource, AVPlayerItemOutputPullD
final class VideoInputPass: DefaultRenderPass {
private var cachedTexture: MTLTexture?
private let scalePass = VideoInputScalePass()
override var fragmentShaderFunctionName: String {
return "bt709ToRGBFragmentShader"
}
override func setup(device: MTLDevice, library: MTLLibrary) {
super.setup(device: device, library: library)
self.scalePass.setup(device: device, library: library)
}
func processPixelBuffer(_ pixelBuffer: CVPixelBuffer, rotation: TextureRotation, textureCache: CVMetalTextureCache, device: MTLDevice, commandBuffer: MTLCommandBuffer) -> MTLTexture? {
func textureFromPixelBuffer(_ pixelBuffer: CVPixelBuffer, pixelFormat: MTLPixelFormat, width: Int, height: Int, plane: Int) -> MTLTexture? {
var textureRef : CVMetalTexture?
@@ -247,9 +253,6 @@ final class VideoInputPass: DefaultRenderPass {
}
let (outputWidth, outputHeight) = textureDimensionsForRotation(width: width, height: height, rotation: rotation)
// let outputSize = CGSize(width: outputWidth, height: outputHeight).fitted(CGSize(width: 1920.0, height: 1920.0))
// outputWidth = Int(outputSize.width)
// outputHeight = Int(outputSize.height)
if self.cachedTexture == nil {
let textureDescriptor = MTLTextureDescriptor()
textureDescriptor.textureType = .type2D
@@ -285,6 +288,63 @@ final class VideoInputPass: DefaultRenderPass {
renderCommandEncoder.endEncoding()
return self.cachedTexture
var outputTexture = self.cachedTexture
if let texture = outputTexture {
outputTexture = self.scalePass.process(input: texture, device: device, commandBuffer: commandBuffer)
}
return outputTexture
}
}
final class VideoInputScalePass: DefaultRenderPass {
private var cachedTexture: MTLTexture?
override func process(input: MTLTexture, device: MTLDevice, commandBuffer: MTLCommandBuffer) -> MTLTexture? {
guard max(input.width, input.height) > 1920 else {
return input
}
self.setupVerticesBuffer(device: device)
let scaledSize = CGSize(width: input.width, height: input.height).fitted(CGSize(width: 1920.0, height: 1920.0))
let width = Int(scaledSize.width)
let height = Int(scaledSize.height)
if self.cachedTexture == nil || self.cachedTexture?.width != width || self.cachedTexture?.height != height {
let textureDescriptor = MTLTextureDescriptor()
textureDescriptor.textureType = .type2D
textureDescriptor.width = width
textureDescriptor.height = height
textureDescriptor.pixelFormat = input.pixelFormat
textureDescriptor.storageMode = .private
textureDescriptor.usage = [.shaderRead, .shaderWrite, .renderTarget]
guard let texture = device.makeTexture(descriptor: textureDescriptor) else {
return input
}
self.cachedTexture = texture
texture.label = "scaledVideoTexture"
}
let renderPassDescriptor = MTLRenderPassDescriptor()
renderPassDescriptor.colorAttachments[0].texture = self.cachedTexture!
renderPassDescriptor.colorAttachments[0].loadAction = .dontCare
renderPassDescriptor.colorAttachments[0].storeAction = .store
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
guard let renderCommandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) else {
return input
}
renderCommandEncoder.setViewport(MTLViewport(
originX: 0, originY: 0,
width: Double(width), height: Double(height),
znear: -1.0, zfar: 1.0)
)
renderCommandEncoder.setFragmentTexture(input, index: 0)
self.encodeDefaultCommands(using: renderCommandEncoder)
renderCommandEncoder.endEncoding()
return self.cachedTexture!
}
}