问题
我第一次尝试性能着色器并遇到了运行时问题。返回的MTLTexture
似乎MTKTextureLoader
与 Metal Performance Shaders 的MPSImageFindKeypoints
编码器不兼容。
到目前为止,我发现的唯一提示来自 @warrenm 在 MPS 上的示例代码,它MTKTextureLoaderOptions
像我一样指定。我没有在文档中找到任何其他提及。
非常感谢任何帮助。
错误
/BuildRoot/Library/Caches/com.apple.xbs/Sources/MetalImage/MetalImage-121.0.2/MPSImage/Filters/MPSKeypoint.mm:166: failed assertion `Source 0x282ce8fc0 texture type (80) is unsupported
其中 0x282ce8fc0MTLTexture
来自纹理加载器。据我所见,没有 MTLTexture 类型 80,枚举范围高达 8 左右(不是十六进制)。
代码
CGFloat w = CGImageGetWidth(_image);
CGFloat h = CGImageGetHeight(_image);
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
id<MTLCommandQueue> commandQueue = [device newCommandQueue];
NSDictionary* textureOptions = @{ MTKTextureLoaderOptionSRGB: [[NSNumber alloc] initWithBool:NO] };
id<MTLTexture> texture = [[[MTKTextureLoader alloc] initWithDevice:device] newTextureWithCGImage:_image
options:textureOptions
error:nil];
id<MTLBuffer> keypointDataBuffer;
id<MTLBuffer> keypointCountBuffer;
MTLRegion region = MTLRegionMake2D(0, 0, w, h);
id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
MPSImageKeypointRangeInfo rangeInfo = {100,0.5};
MPSImageFindKeypoints* imageFindKeypoints = [[MPSImageFindKeypoints alloc] initWithDevice:device
info:&rangeInfo];
[imageFindKeypoints encodeToCommandBuffer:commandBuffer
sourceTexture:texture
regions:®ion
numberOfRegions:1
keypointCountBuffer:keypointCountBuffer
keypointCountBufferOffset:0
keypointDataBuffer:keypointDataBuffer
keypointDataBufferOffset:0];
[commandBuffer commit];
NSLog(keypointCountBuffer);
NSLog(keypointDataBuffer);
编辑
将图像转换为正确的像素格式后,我现在正在初始化缓冲区,如下所示:
id<MTLBuffer> keypointDataBuffer = [device newBufferWithLength:maxKeypoints*(sizeof(MPSImageKeypointData)) options:MTLResourceOptionCPUCacheModeDefault];
id<MTLBuffer> keypointCountBuffer = [device newBufferWithLength:sizeof(int) options:MTLResourceOptionCPUCacheModeDefault];
没有错误了。但是我现在如何阅读内容?
((MPSImageKeypointData*)[keypointDataBuffer contents])[0].keypointCoordinate
为所有索引返回 (0,0)。我也不知道怎么读keypointsCountBuffer
。转换为 int 值的缓冲区内容显示的值高于定义的 maxKeypoints。我看不到文档在哪里说计数缓冲区具有哪种格式。