Metal如何将图像块分配到每个线程组? [英] How Metal distribute the image block to each thread group?

查看:204
本文介绍了Metal如何将图像块分配到每个线程组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果要进行灰度转换,则需要以以下方式设置我的threadsPerGroup和线程组.

For example, if I want to do a grayscale transformation, I need to set up my threadsPerGroup and thread group in the following way.

NSUInteger maxTotalThreadsPerThreadgroup = [self.computePipelineState maxTotalThreadsPerThreadgroup];
MTLSize threadgroupCounts = MTLSizeMake(threadExecutionWidth * 2, threadExecutionWidth * 2, 1);
MTLSize threadsPerThreadGroup = MTLSizeMake([self.texutre width] / threadgroupCounts.width + 1,
[self.texutre height] / threadgroupCounts.height + 1,
1);

我知道图像将被切成不同的块,每个块将由一个线程组处理.但是似乎在内核中,我们将只读取2d纹理,然后输出处理后的纹理.

I know the image will be chopped into different blocks and each one will be processed by one thread group. But it seems in the kernel, we will just read the 2d texture, and then output the processed texture.

但是问题是如何将图像切成不同的2d纹理?我们如何知道是否将每个图像块分配给一个要处理的线程?这是由Metal本身完成的吗?还是我们需要使用 gid 手动将每个块分配给每个线程组?

But the question is that how the image is chopped into different 2d texture? How do we know if each block of image get assigned to a thread to process? Is this done by Metal itself? Or we need to manually assign each block to each threadgroup by using the gid?

推荐答案

金属不知道或不在乎您的着色器是否在图像上运行.它不会砍"图像或类似的东西.

Metal doesn't know or care whether your shader is operating on an image. It doesn't "chop" the image or anything like that.

计算着色器通过网格"进行处理.网格是一个抽象.这是您组织工作的任意方式.金属不会给网格赋予任何意义,例如将网格中的位置与图像中的像素相关联.

A compute shader is processed over a "grid". The grid is an abstraction. It's an arbitrary way for you to organize the work. Metal doesn't assign any significance to the grid, such as associating a position in the grid with a pixel in an image.

这种关联(如果存在)暗含在您的着色器代码的行为方式中.是的,这很大程度上取决于着色器对thread_position_in_gridthread_position_in_threadgroupthread_index_in_threadgroup等的处理方式.

Such an association, if it exists, is implicit in how your shader code behaves. Yes, that is largely based on what the shader does with thread_position_in_grid, thread_position_in_threadgroup, thread_index_in_threadgroup, etc.

因此,如果您将gid变量与thread_position_in_grid属性一起使用,并且将其坐标用作图像坐标,则该用法决定了每个网格位置都对应于一个图像像素.一旦执行此操作,则每个线程组都对应于图像的一个块,因为一个线程组只是一个网格位置的块.同样,这不是Metal要做的,而是您的着色器正在做的.

So, if you're using a gid variable with the thread_position_in_grid attribute, and you use its coordinates as image coordinates, then that usage is what dictates that each grid position corresponds to an image pixel. Once you do that, then it follows that each thread group corresponds to a block of the image, since a thread group is just a block of grid positions. Again, though, this is not something that Metal is doing, it's something that your shader is doing.

您可以做一些完全不同的事情,而Metal不在乎.

You could do something entirely different and Metal wouldn't care.

这篇关于Metal如何将图像块分配到每个线程组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆