2017-04-22 49 views
1

如何使用struct來獲取緩衝區值?例如:金屬着色語言 - 使用Struct來獲取緩衝區值

struct mouseInput 
{ 
float x; 
float y; 
}; 

kernel void compute(texture2d<float, access::write> output [[texture(0)]], 
        constant float &time [[buffer(0)]], 
        constant mouseInput.x &mouseX [[buffer(1)]],///<--mouseX from swift 
        constant mouseInput.y &mouseY [[buffer(2)]],///<--mouseY from swift 
        uint2 gid [[thread_position_in_grid]]) { 
... 
} 

然後我可以訪問mouseInput.xMetal等等任何地方。最接近的是this thread但是我不確定如何將其轉化爲我的用途。

回答

2

爲鼠標位置的兩個組件使用單獨的緩衝區似乎對我來說很愚蠢和浪費。

創建一個包含兩個緩衝區的單個緩衝區。

struct mouseInput 
{ 
float x; 
float y; 
}; 

kernel void compute(texture2d<float, access::write> output [[texture(0)]], 
        constant float &time [[buffer(0)]], 
        constant mouseInput &mouse [[buffer(1)]], 
        uint2 gid [[thread_position_in_grid]]) { 
... 
} 

事實上,這取決於你的應用程序的其餘部分,它可能是有道理的時間用鼠標位置相結合:

struct params 
{ 
    float time; 
    float2 mouse; 
}; 

kernel void compute(texture2d<float, access::write> output [[texture(0)]], 
        constant params &params [[buffer(0)]], 
        uint2 gid [[thread_position_in_grid]]) { 
... 
// use params.time to get the time value. 
// Use params.mouse.x and params.mouse.y to get the mouse position. 
} 
然後用類似的簽名寫你的計算功能
相關問題