我正在开发一个使用Metal渲染2D几何图形的应用程序。
目前,顶点的位置是在顶点函数内部解决的。我希望能够从同一顶点函数中将解决后的位置写回到缓冲区。
我认为这是可能的,尽管我第一次尝试做到这一点时:
vertex VertexOut basic_vertex(device VertexIn *vertices [[ buffer(0) ]],
device VertexOut *solvedVertices [[ buffer(1) ]],
vid [[ vertex_id ]])
{
VertexIn in vertices[vid];
VertexOut out;
out.position = ... // Solve the position of the vertex
solvedVertices[vid] = out // Write to the buffer later to be read by CPU
return out;
}
我遇到了这个编译时错误:
好的,我脑海中浮现出几个解决方案 - 我可以在第一次 - 非光栅化 - 通过一个顶点函数求解顶点位置,该函数声明如下:
vertex void solve_vertex(device VertexIn *unsolved [[ buffer(0) ]],
device VertexOut *solved [[ buffer(1) ]],
vid [[ vertex_id ]])
{
solved[vid] = ...
}
然后将这些已解决的顶点输入到一个现在简化了的 - 光栅化 - 顶点函数中。
另一种可行但不太吸引人的解决方案可能是在计算函数中解决它们。
那么,在这种情况下最好的方法是什么?从我的一点研究中,我发现Transform Feedback中做同样程序的过程,但我没有找到苹果文档/示例代码或其他网站上面对这种问题的最佳实践(除了问题开头的链接)。
constant
地址空间是只读的。请参见此处 - 函数、变量和限定符。 - jameslintaylor