実際には、MPSKernal
という既定のフィルタ(appleとcustom compute Shaders
)を使用してライブカメラフィルタをメタルで適用しようとしています。メタルでカスタム計算シェーダを使用したときにメモリリークが発生する
私は、デフォルトのカーネル関数とカスタムカーネル関数の組み合わせでグリッドのコレクションビューにカスタムフィルタを適用しています。
アプリのクリップのようです。
しかし、私が観察はカスタムフィルタを使用するとmemory leaks
の多くは、アップルによって与えられたデフォルトのカーネル関数に比べて存在することです。
もし私が何か間違いがあったとしても、それは私には分かりません。
ここに私のカスタム計算シェーダがあります。
私は私のパイプラインを作成し、コードがカスタムシェーダとパイプラインの状態が次のように作成されここ
let blur = MPSImageGaussianBlur(device: device, sigma: 0)
let threadsPerThreadgroup = MTLSizeMake(4, 4, 1)
let threadgroupsPerGrid = MTLSizeMake(destinationTexture.width/threadsPerThreadgroup.width, destinationTexture.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 {
let inPlaceTexture = UnsafeMutablePointer<MTLTexture>.allocate(capacity: 1)
inPlaceTexture.initialize(to: destinationTexture)
blur.encode(commandBuffer: commandBuffer, inPlaceTexture: inPlaceTexture, fallbackCopyAllocator: nil)
}
を行くスレッドグループを通じて派遣にについて
kernel void customFunction1(
texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]]){
const float4 colorAtPixel = inTexture.read(gid);
const float4 outputColor = float4(colorAtPixel.r, colorAtPixel.g, colorAtPixel.b, 1);
outTexture.write(outputColor, gid);
}
。
let defaultLibrary = device.newDefaultLibrary()
let kernelFunction = defaultLibrary!.makeFunction(name: name)
do {
pipelineState = try device.makeComputePipelineState(function: kernelFunction!)
} catch {
fatalError("Unable to create pipeline state")
}
および器具には、いくつかのMalloc 16 bytes
および[Mtkview draw]
方法で漏れがあることを示します。
スクリーンショットは以下のとおりです。
私がどこでどのような問題が発生しているから見つけることに助けを求めています。
ありがとうございました。
うまくいきました。ありがとうございました。しかし、代替コピーアロケータの実装方法を教えてください。私はそれを試しましたが、それは@warrenmをクラッシュさせています。 –