繁体   English   中英

我可以从Metal Shader获取缓冲区的大小吗?

[英]Can I get the size of a buffer from my Metal shader?

在我的iOS应用程序中,用Swift编写,我生成一个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;
    // ...
}

我唯一的选择是将顶点的数量作为统一传递?

据我所知,不,你不能,因为顶点指向一个地址。 就像C ++一样,必须有两件事要知道数组的数量或大小:
1)知道数组的数据类型(float或某些struct)

2a)数据类型OR的数组计数
2b)数组的总字节数。

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

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

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
} 

对于纹理缓冲区,您可以。

您可以从着色器代码中获取纹理缓冲区的大小。
纹理缓冲区有一个get_width()get_height()函数,它返回一个uint

uint get_width() const; uint get_height() const;

但这可能无法回答OP关于顶点缓冲区的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM