且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Metal如何将图像块分配到每个线程组?

更新时间:2022-03-19 23:25:56

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

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.