正如Apple的文档中所提到的,着色语言的texture2d可以是int类型。我试图使用int类型的texture2d作为着色器语言的参数,但texture2d的write方法无法工作。
kernel void dummy(texture2d<int, access::write> outTexture [[ texture(0) ]],
uint2 gid [[ thread_position_in_grid ]])
{
outTexture.write( int4( 2, 4, 6, 8 ), gid );
}
但是,如果我用浮点数替换int,它就可以了。
kernel void dummy(texture2d<float, access::write> outTexture [[ texture(0) ]],
uint2 gid [[ thread_position_in_grid ]])
{
outTexture.write( float4( 1.0, 0, 0, 1.0 ), gid );
}
其他类型的texture2d,如int的texture2d,short的texture2d等,可以用作着色器函数参数,以及如何使用它们?感谢您查看我的问题。
相关的主机代码:
MTLTextureDescriptor *desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
desc.usage = MTLTextureUsageShaderWrite;
id<MTLTexture> texture = [device newTextureWithDescriptor:desc];
[commandEncoder setTexture:texture atIndex:0];
显示由GPU计算的输出的代码,w和h分别表示textrue的宽度和高度。
uint8_t* imageBytes = malloc(w*h*4);
memset( imageBytes, 0, w*h*4 );
MTLRegion region = MTLRegionMake2D(0, 0, [texture width], [texture height]);
[texture getBytes:imageBytes bytesPerRow:[texture width]*4 fromRegion:region mipmapLevel:0];
for( int j = 0; j < h; j++ )
{
printf("%3d: ", j);
for( int i = 0; i < w*pixel_size; i++ )
{
printf(" %3d",imageBytes[j*w*pixel_size+i] );
}
printf("\n")
}
答案 0 :(得分:0)
Metal Shading Language Guide表示:
注意:如果T为int或short,则与纹理关联的数据必须使用带符号的整数格式。如果T是uint或ushort,则与纹理关联的数据必须使用无符号整数格式。
您所要做的就是确保您在API(主机代码)中写入的纹理与您在内核函数中的纹理相匹配。或者,您也可以在写入int
之前将float
值转换为outTexture
。