我尝试翻译 this shadertoy scene进入 Metal Kernel
。在 shadertoy 代码中:
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 dir = rayDirection(45.0, iResolution.xy, fragCoord);
...
}
在OpenGL
情况下,我们需要从glfw
窗口发送iResolution
。 fragCoord
将是来自 Fragment Shader
的 gl_FragCoord
。
我在 metal
文件中有这个:
kernel void compute(texture2d<float, access::write> output [[texture(0)]],
uint2 gid [[thread_position_in_grid]]) {
int width = output.get_width();
int height = output.get_height();
....
}
所以我可以从 width
和 height
得到我的 iResolution
。但我不确定如何获取 gl_FragCoord
。
Metal_stdlib
是否有等同于 gl_FragCoord
的东西?或者如果我必须计算,我怎样才能获得相同的值?
最佳答案
如果您正在寻找窗口坐标中的“片段”位置(如 gl_FragCoord
),您可以使用 float2(gid)
,范围从 (0 , 0) 到 (宽度, 高度)。仅当您的网格尺寸(线程组大小和线程组计数的乘积)与目标纹理的尺寸完全匹配时才会出现这种情况。
关于glsl - Metal 着色语言 - FragCoord 等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43471463/