metal - Metal Compute Kernel 因两个纹理参数而失败
问题描述
我有金属计算内核,它需要两个纹理作为参数。但是,我遇到了内核无法运行的问题。我已将问题归结为这个简单的内核。
#include <metal_stdlib>
using namespace metal;
kernel void test_texture(texture2d<float, access::sample> tex1 [[texture(0)]],
texture2d<float, access::sample> tex2 [[texture(1)]],
device float *buf [[buffer(0)]],
uint idx [[thread_position_in_grid]])
{
buf[idx] = 100;
}
以及以下主机代码。
#import <Metal/Metal.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
const size_t max_buffer = 128000000;
const size_t max_texture = 16384;
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
id<MTLLibrary> library = [device newDefaultLibrary];
id<MTLCommandQueue> queue = [device newCommandQueue];
id<MTLBuffer> buffer = [device newBufferWithLength:sizeof(float)*max_buffer
options:MTLResourceCPUCacheModeDefaultCache |
MTLResourceStorageModeManaged];
MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init];
textureDescriptor.textureType = MTLTextureType2D;
textureDescriptor.pixelFormat = MTLPixelFormatR32Float;
textureDescriptor.width = max_texture;
textureDescriptor.height = max_texture;
textureDescriptor.depth = 1;
textureDescriptor.mipmapLevelCount = 1;
textureDescriptor.sampleCount = 1;
textureDescriptor.arrayLength = 1;
textureDescriptor.resourceOptions = MTLResourceStorageModePrivate | MTLResourceCPUCacheModeDefaultCache;
textureDescriptor.cpuCacheMode = MTLCPUCacheModeDefaultCache;
textureDescriptor.storageMode = MTLStorageModePrivate;
textureDescriptor.usage = MTLTextureUsageShaderRead;
id<MTLTexture> texture1 = [device newTextureWithDescriptor:textureDescriptor];
id<MTLTexture> texture2 = [device newTextureWithDescriptor:textureDescriptor];
MTLComputePipelineDescriptor *discriptor = [[MTLComputePipelineDescriptor alloc] init];
discriptor.computeFunction = [library newFunctionWithName:@"test_texture"];
discriptor.threadGroupSizeIsMultipleOfThreadExecutionWidth = YES;
id<MTLComputePipelineState> pipeline = [device newComputePipelineStateWithDescriptor:discriptor
options:MTLPipelineOptionNone
reflection:NULL
error:NULL];
id<MTLCommandBuffer> command_buffer = queue.commandBuffer;
id<MTLComputeCommandEncoder> compute_encoder = [command_buffer computeCommandEncoder];
[compute_encoder setComputePipelineState:pipeline];
[compute_encoder setTexture:texture1 atIndex:0];
[compute_encoder setTexture:texture2 atIndex:1];
[compute_encoder setBuffer:buffer offset:0 atIndex:0];
[compute_encoder dispatchThreads:MTLSizeMake(max_buffer, 1, 1) threadsPerThreadgroup:MTLSizeMake(1024, 1, 1)];
[compute_encoder endEncoding];
id<MTLBlitCommandEncoder> blit_encoder = [command_buffer blitCommandEncoder];
[blit_encoder synchronizeResource:buffer];
[blit_encoder endEncoding];
[command_buffer commit];
[command_buffer waitUntilCompleted];
float *result = (float *)buffer.contents;
NSLog(@"%f",result[0]);
}
return 0;
}
如果我注释掉第二个纹理参数,我会在读取结果缓冲区时得到预期值。但是,当我保持第二个纹理参数不变时,似乎内核没有运行并且结果中的值为零。在 MacOS 上的计算内核中可以采样的纹理数量是否有限制?或者是我在两个纹理中使用最大纹理尺寸引起的问题(我的纹理内存用完了)?强文本
解决方案
在您的情况下,错误很可能是由于纹理占用了您的整个视频内存预算而发生的。16384 x 16384 * sizeof(float) = 每个纹理 1024mb 的内存。因为您使用的是 MTLStorageModePrivate,所以资源仅存储在视频内存中。
推荐阅读
- java - Firestore 和 Java:删除 Array 的单个元素
- javascript - 如何使用键值从两个对象创建第三个对象
- ios - Flutter iOS 模拟器无法运行
- python - /profiles/user-profile/2/ 'int' 对象的 AttributeError 没有属性 '_meta'
- angular - 单击同一组件的不同按钮时的不同路线
- c - 如何释放这个strdup?
- python - 如何使用 selenium python 在日期选择器上单击活动日期?
- sql - 没有权限的用户可以更改 oracle 中的表
- javascript - 浏览器如何选择脚本的转译语言
- python - Tensorflow 初学者,关于线性模型的基本问题