简体   繁体   English

什么是Apple的Metal(金属着色器语言)纹理坐标?

[英]What are Apple's Metal (Metal Shader Language) texture coordinates?

In iOS or OS/X what texture coordinates are used in Metal Shader Language kernel function? 在iOS或OS / X中,Metal Shader Language内核函数使用了什么纹理坐标? For example, given an MTLTexture and uint2 gid[[thread_position_in_grid]] Is gid.x and gid.y between 0..1 (x and y are floats) or 0.. inTexture.get_width() (x and y are integers). 例如,给定MTLTextureuint2 gid[[thread_position_in_grid]] gid.xgid.y介于0..1(x和y是浮点数)或0 .. inTexture.get_width() (x和y是整数)之间。

Thanks in Advance 提前致谢

thread_position_in_grid is an index (an integer) in the grid that takes values in the ranges you specify in dispatchThreadgroups:threadsPerThreadgroup: . thread_position_in_grid是网格中的索引(整数),它采用您在dispatchThreadgroups:threadsPerThreadgroup:指定的范围中的值dispatchThreadgroups:threadsPerThreadgroup: . It's up to you to decide how many thread groups you want, and how many threads per group. 由您决定所需的线程组数以及每组的线程数。

In the following sample code you can see that threadsPerGroup.width * numThreadgroups.width == inputImage.width and threadsPerGroup.height * numThreadgroups.height == inputImage.height . 在下面的示例代码中,您可以看到threadsPerGroup.width * numThreadgroups.width == inputImage.widththreadsPerGroup.height * numThreadgroups.height == inputImage.height In this case, a position in the grid will thus be a non-normalized (integer) pixel coordinate. 在这种情况下,网格中的位置因此将是非标准化(整数)像素坐标。

Each launch of a compute shader in Metal is accompanied by a dense rectangular 3D grid of thread IDs. 每次在Metal中启动计算着色器都会伴随一个密集的矩形3D网格线程ID。 The dimensions of the grid is set when you call [MTLComputeCommandEncoder dispatchThreadGroups:threadsPerThreadgroup:] . 调用[MTLComputeCommandEncoder dispatchThreadGroups:threadsPerThreadgroup:]时,将设置网格的尺寸。 You can for example have a threadgroup size of {16,16,1} (256 threads in a threadgroup as a 16x16x1 square), and threadgroup count of {1,2,1} , which will cause two threadgroups to be launched with a total area of 512 threads in the shape {16,32,1} . 例如,您可以将线程组大小设置为{16,16,1} (线程组中的256个线程为16x16x1平方),线程组计数为{1,2,1} ,这将导致两个线程组以形状{16,32,1}的512个线程的总面积。 These are the integers that appear at the top of your kernel as [[thread_position_in_grid]] . 这些是作为[[thread_position_in_grid]]出现在内核顶部的整数。 The thread position is the way that you tell which thread you are, just like the threadID parameter passed to a block by dispatch_apply() . 线程位置是您告诉哪个线程的方式,就像dispatch_apply()传递给块的threadID参数一样。

Metal specifies no mapping from [[thread_position_in_grid]] to coordinates in a texture. Metal指定从[[thread_position_in_grid]]到纹理中的坐标的映射。 This is done by you in software in your compute shader. 这是由您在计算着色器中的软件中完成的。 If you want to read every other pixel in a region of a texture at some offset in the image, then you need to multiply the threadID by two and add an offset in your kernel before passing the new coordinate to texture2d.sample . 如果要在图像中的某个偏移处读取纹理区域中的每个其他像素,则需要将threadID乘以2并在将新坐标传递给texture2d.sample之前在内核中添加偏移量。 Since Metal can not launch partial threadgroups, it is up to you to make sure that unneeded threadgroups are not executed. 由于Metal无法启动部分线程组,因此您需要确保不执行不需要的线程组。 For example, when applied to a smaller texture, the full size of your 32x64 launch might cause you to write off the end of your texture. 例如,当应用于较小的纹理时,32x64启动的完整大小可能会导致您注销纹理的末尾。 In this case you must check the threadID to see if the thread will write off the end and then either return out of the shader or skip over the texture write call for that thread to avoid the problem. 在这种情况下,您必须检查threadID以查看线程是否将注销结束,然后返回着色器或跳过该线程的纹理写入调用以避免此问题。

thread_position_in_grid is always made of unsigned integers, and provides these options, but none of them are related to texture coordinates. thread_position_in_grid始终由无符号整数组成,并提供这些选项,但它们都不与纹理坐标相关。 It may be helpful to ask another, related question, because you seem to be conflating the idea of textures and kernel functions. 提出另一个相关问题可能会有所帮助,因为您似乎正在混淆纹理和内核函数的概念。

  • 16- or -32 bit 16位或-32位
  • 1D, 2D, or 3D 1D,2D或3D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM