0
我想使用浮點變量作爲索引來訪問MTLBuffer的位置,但我必須將其轉換爲無符號整型。那麼,這是我的第一個想法。演員變量用作索引(金屬着色語言)
在實踐中,這似乎並沒有工作,但我不明白爲什麼。
我基本上有類似:
vertex VertexOut basic_vertex(const device float3 *vertex_array [[ buffer(0) ]],
const device float3 *color_array [[ buffer(1) ]],
(...))
{
// get the current vertex
float3 position = vertex_array[vid];
// get the color index
uint color_index = as_type<uint>(position.z);
// get the color
float3 color = color_array[color_index];
VertexOut vertexOut;
vertexOut.position = proj_Matrix * mv_Matrix * float4(position.x, position.y, 0, 1);
vertexOut.color = float4(color, 1);
return vertexOut;
}
我試圖減少的數據量,通過使用Z座標來索引顏色緩衝區,而不是重複相同的顏色被髮送到GPU對於大量的頂點。這樣,我不必傳遞6個浮點數(x,y,z,r,g,b),而只需要傳遞3加上顏色。
我得到的錯誤不是當我使用color_index變量來獲得顏色。問題是,當我嘗試訪問的顏色,例如:
vertexOut.color = float4(color, 1);
如果我這樣做,我會得到這個錯誤:
Execution of the command buffer was aborted due to an error during execution. Caused GPU Hang Error (IOAF code 3)
是否有變通方法是什麼我想完成? 我在這裏做錯了什麼?
預先感謝您
'as_type'在功能上等同於C++的'reinterpret_cast':它實際上將源值的位視爲提供的類型,在此上下文中不正確。你可能想要的是「floor」,或者根本不投射(也就是說,你可以用浮點數初始化索引,而(昂貴的)float-to-uint轉換將隱含地發生)。在不訪問顏色時不會出現此錯誤的事實可能表明,在未使用結果值時,數組訪問正在消除死代碼。 – warrenm
謝謝@warrenm,這解決了這個問題。我只需要刪除類型轉換並使用'floor'代替。我沒有想到'as_type'等於'reinterpret_cast'。 – mvcs
樂於助人;如果您想接受它,我已發佈我的評論作爲答案。 – warrenm