2017-10-11 84 views
1

在Objective-C 我嘗試使用此方法在原地對命令緩衝區進行編碼。MPSCopyAllocator無法初始化

-(BOOL) encodeToCommandBuffer: (nonnull id <MTLCommandBuffer>)commandBuffer 
        inPlaceTexture: (__nonnull id <MTLTexture> __strong * __nonnull) texture 
      fallbackCopyAllocator: (nullable MPSCopyAllocator) copyAllocator 
       MPS_SWIFT_NAME(encode(commandBuffer:inPlaceTexture:fallbackCopyAllocator:)); 

我想創建一個新的MPSCopyAllocator。

我從文檔中使用了下面的代碼。

MPSCopyAllocator myAllocator = ^id <MTLTexture> _Nonnull (MPSKernel * __nonnull filter, __nonnull id <MTLCommandBuffer> cmdBuf, __nonnull id <MTLTexture> sourceTexture) 
{ 
    MTLPixelFormat format = sourceTexture.pixelFormat; 
    MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat: format width: sourceTexture.width height: sourceTexture.height mipmapped: NO]; 

    id <MTLTexture> result = [cmdBuf.device newTextureWithDescriptor: d]; 

    return result; 
    / 
}; 

,但我得到的問題,導航語義問題

Incompatible block pointer types initializing '__strong MPSCopyAllocator' (aka 'id<MTLTexture> _Nonnull (^__strong)(MPSKernel * _Nonnull __strong, id<MTLCommandBuffer> _Nonnull __strong, id<MTLTexture> _Nonnull __strong)') with an expression of type 'id<MTLTexture> _Nonnull (^)(MPSKernel * _Nonnull __strong, id<MTLCommandBuffer> _Nonnull __strong, id<MTLTexture> _Nonnull __strong)' 

的MPSCopyAllocator定義

typedef id <MTLTexture> __nonnull NS_RETURNS_RETAINED (^MPSCopyAllocator)(MPSKernel * __nonnull filter, 
                      id <MTLCommandBuffer> __nonnull commandBuffer, 
                      id <MTLTexture> __nonnull sourceTexture); 

什麼是創建一個MPSCopyAllocator的正確方法?

回答

2

遺憾的是,你需要包括NS_RETURNS_RETAINED在塊定義將其分配給一個變量時:

MPSCopyAllocator allocator = ^id <MTLTexture> NS_RETURNS_RETAINED(MPSKernel *filter, 
                    id <MTLCommandBuffer> commandBuffer, 
                    id <MTLTexture> sourceTexture) 
{ 
    // ... 
}; 

爲了簡潔,我在這裏省略了空性的註釋,因爲他們是可選的。

+0

非常感謝,它的工作原理! –