2017-07-19 8 views
0

実際には、MPSKernalという既定のフィルタ(appleとcustom compute Shaders)を使用してライブカメラフィルタをメタルで適用しようとしています。メタルでカスタム計算シェーダを使用したときにメモリリークが発生する

私は、デフォルトのカーネル関数とカスタムカーネル関数の組み合わせでグリッドのコレクションビューにカスタムフィルタを適用しています。

アプリのクリップのようです。

enter image description here

しかし、私が観察はカスタムフィルタを使用すると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]方法で漏れがあることを示します。

スクリーンショットは以下のとおりです。

enter image description here

私がどこでどのような問題が発生しているから見つけることに助けを求めています。

ありがとうございました。

答えて

1

埋め込みテクスチャパラメータを格納するためにUnsafeMutablePointerを明示的に割り当てる理由はありません。ちなみに、それはあなたのリークの原因です:あなたは、ポインタを割り当てて、決してそれを割り当てを解除します。

代わりにテクスチャを渡すためにローカル変数を使用します。

var inPlaceTexture = destinationTexture 
blur.encode(commandBuffer: commandBuffer, inPlaceTexture: &inPlaceTexture, fallbackCopyAllocator: nil) 

ところで、あなたは(最終的に)あなたが、フォールバックを設けることなく、インプレースencodeメソッドを呼び出す場合、悪い時間を持つようになるだろうアロケータまたは戻り値のチェックインプレースエンコーディングは特定の状況では失敗するため、失敗した場合に適切なテクスチャを割り当てるクロージャを用意する必要があります。

+0

うまくいきました。ありがとうございました。しかし、代替コピーアロケータの実装方法を教えてください。私はそれを試しましたが、それは@warrenmをクラッシュさせています。 –

関連する問題