【问题标题】:MPSCopyAllocator can't initializeMPSCopyAllocator 无法初始化
【发布时间】:2018-03-22 14:39:07
【问题描述】:

在 Objective-C 中 我尝试使用这种方法对命令缓冲区进行就地编码。

-(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 的正确方法是什么?

【问题讨论】:

    标签: metal metalkit


    【解决方案1】:

    很遗憾,在将NS_RETURNS_RETAINED 分配给变量时,您需要在块定义中包含它:

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

    为了简洁起见,我在这里省略了可空性注释,因为它们是可选的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 2020-01-22
      • 2017-03-26
      • 2019-01-05
      • 2015-06-30
      • 2012-12-21
      • 2014-06-24
      • 1970-01-01
      相关资源
      最近更新 更多