将数据加载到定义为MTLTextureType1DArray的纹理中

时间:2018-09-12 18:00:53

标签: objective-c metal

我正在尝试建立一些计算内核,这些内核需要对傅立叶系数的某些径向轮廓进行插值。本质上,我需要在一个索引中保持谨慎,而在另一个索引中进行内插。我认为实现这些一维纹理数组将使我能够使用GPU的内置插值函数。浏览Metal文档,看来MTLTextureType1DArrayMTLPixelFormatR32Float就是正确的设置。

我正在将纹理描述设置为

MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init];
textureDescriptor.pixelFormat = MTLPixelFormatR32Float;
textureDescriptor.textureType = MTLTextureType1DArray;

textureDescriptor.width = numRadialPoints;
textureDescriptor.height = 1;
textureDescriptor.depth = 1;

textureDescriptor.arrayLength = numArrays;

textureDescriptor.mipmapLevelCount = 1;
textureDescriptor.sampleCount = 1;

textureDescriptor.resourceOptions = MTLResourceCPUCacheModeWriteCombined | MTLResourceStorageModeManaged;
textureDescriptor.cpuCacheMode = MTLCPUCacheModeWriteCombined;
textureDescriptor.storageMode = MTLStorageModeManaged;
textureDescriptor.usage = MTLTextureUsageShaderRead;

我不知道现在如何加载纹理数据。我的第一次尝试是

_texture = [device newTextureWithDescriptor:textureDescriptor];
[_texture replaceRegion:MTLRegionMake2D(0, 0, numRadialPoints, numArrays) mipmapLevel:0 withBytes:floatbuffer bytesPerRow:4*numRadialPoints];

但这会导致错误,因为高度不匹配。

_validateReplaceRegion:144: failed assertion `(origin.y + size.height)(163) must be <= height(1).'

如何将数据加载到MTLTextureType1DArray中?

1 个答案:

答案 0 :(得分:2)

您需要多次调用-replaceRegion:mipmapLevel:slice:withBytes:bytesPerRow:bytesPerImage:,一次调用数组的每个元素。您可以使用slice参数指定数组索引。

region参数必须为一维。