我可以从我的金属着色器中获得缓冲区的大小吗?

在我用Swift编写的iOS应用程序中,我生成一个Metal缓冲区:

vertexBuffer = device.newBufferWithBytes(vertices, length: vertices.count * sizeofValue(vertices[0]), options: nil) 

并将其绑定到我的着色器程序:

 renderCommandEncoder.setVertexBuffer(vertexBuffer, offset: 0, atIndex: 1) 

在我使用Metal着色语言编写的着色器程序中,我可以访问缓冲区的大小吗? 我想访问我的缓冲区中的下一个顶点做一些差分计算。 就像是:

 vertex float4 my_vertex(const device packed_float3* vertices [[buffer(1)]], unsigned int vid[[vertex_id]]) { float4 vertex = vertices[vid]; // Need to clamp this to not go beyond buffer, // but how do I know the max value of vid? float4 nextVertex = vertices[vid + 1]; float4 tangent = nextVertex - vertex; // ... } 

我唯一的select是将顶点的数量作为一个统一体来传递吗?

据我所知,不,你不能因为顶点指向一个地址。 就像C ++一样,必须有两件事情来知道数组的数量或大小:
1)知道数组的什么数据types(float或者一些结构体)

2a)数据typesOR的数组数量
2b)数组的总字节数。

所以是的,你需要传递数组作为统一。

其实你可以。 您可以将结果值用于循环或条件。 你不能用它来初始化对象。 (所以dynamic数组失败)

 uint tempUint = 0; // some random type uint uintSize = sizeof(tempUint); // get the size for the type uint aVectorSize = sizeof(aVector) / uintSize; // divide the buffer by the type. float dynamicArray[aVectorSize]; // this fails for (uint counter = 0; counter < aVectorSize; ++ counter) { // do stuff }; if (aVectorSize > 10) { // do more stuff }