2017-04-18 91 views
2

我試圖將this shadertoy scene轉換爲Metal Kernel。在shadertoy代碼:金屬底紋語言 - FragCoord相當於?

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(); 
    .... 
} 

所以我可以讓我iResolutionwidthheight。但我不知道如何獲得gl_FragCoord

Metal_stdlib有什麼相當於gl_FragCoord?或者如果我必須計算,我怎樣才能獲得相同的價值?

+0

假設您正在調度與輸出紋理大小完全相同的單個網格,則與gl_FragCoord相當的一個是'float2(gid)/ float2(width,height)'。 – warrenm

+0

此外,這看起來可能是渲染過程中實際碎片函數的更好匹配,而不是計算函數。在這種情況下,片段座標將是一個輸入變量或'stage_in'結構域標記爲[[position]]'。 –

+0

@warrenm任何示例代碼如何做到這一點?假設我的屏幕分辨率是浮動的(800/600),我如何劃分'線程組'或'線程組數'? – sooon

回答

1

如果您正在窗口座標中尋找「片段」位置(如gl_FragCoord是),則可以使用float2(gid),範圍從(0,0)到(寬度,高度)。如果您的網格維度(線程組大小和線程組計數的乘積)完全匹配目標紋理的維度,則只有這種情況。