0

我尝试裁剪MTLTexture苹果使用clipRectoffset属性提供的使用默认过滤器,它与实时视频过滤配合得很好。

但是对于自定义过滤器(使用custom shaders)的属性clipRectoffset对裁剪没有影响,MTLTexture我尝试使用MPSImageLanczosScale引用Crop 和缩放 MTLTexture进行裁剪。

func encodeWithTransform(to commandBuffer: MTLCommandBuffer, sourceTexture: MTLTexture, destinationTexture: MTLTexture, transform: MPSScaleTransform) {

   var newTransform = transform

   withUnsafePointer(to: &newTransform) { (transformPtr: UnsafePointer<MPSScaleTransform>) -> () in

       lanczosScaleFilter.scaleTransform = transformPtr
       lanczosScaleFilter.encode(commandBuffer: commandBuffer, sourceTexture: sourceTexture, destinationTexture: newCroppingTexture)
       self.imageFilter.encode(to: commandBuffer!, sourceTexture: newCroppingTexture, destinationTexture: destinationTexture, cropRect: MTLRegionMake2D(0, 0, Int(uiscreenWidth) / 4, sourceTexture.height), offset : CGPoint(x: 0 / scale, y:0))
    }
}

正如您在此处看到的,我们正在尝试裁剪纹理,然后对过滤器进行编码,这是一种双向操作,过程非常缓慢。

但是如果在不裁剪纹理的情况下应用过滤器,则性能非常平滑。编码方法看起来像这样。

    blur.clipRect = cropRect
    blur.offset = MPSOffset(x: Int(offset.x), y: Int(offset.y), z: 0)

    let threadsPerThreadgroup = MTLSizeMake(10, 10, 1)
    let threadgroupsPerGrid = MTLSizeMake(destinationTexture.width / threadsPerThreadgroup.width, sourceTexture.height / threadsPerThreadgroup.height, 1)

    let commandEncoder = commandBuffer.makeComputeCommandEncoder()
    commandEncoder.setComputePipelineState(pipelineState!)
    commandEncoder.setTexture(sourceTexture, at: 0)
    commandEncoder.setTexture(destinationTexture, at: 1)

    commandEncoder.dispatchThreadgroups(threadgroupsPerGrid, threadsPerThreadgroup: threadsPerThreadgroup)

    commandEncoder.endEncoding()

    autoreleasepool {
        var inplaceTexture = destinationTexture

        blur.encode(commandBuffer: commandBuffer, inPlaceTexture: &inplaceTexture, fallbackCopyAllocator: nil)
    }

无需使用模糊过滤器进行编码。但是我添加了这种跳跃,它会使用clipRectoffset属性进行裁剪,但没有运气。

因此,如果有人有更好的解决方案,可能会在自定义着色器方法中修改纹理矩形,这会很有帮助。另外,如果有人会建议我苹果如何使用它clipRect以及offset对我有帮助的属性。

4

0 回答 0