我可以从Metal着色器获取缓冲区的大小吗

我可以从Metal着色器获取缓冲区的大小吗

本文介绍了我可以从Metal着色器获取缓冲区的大小吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我用Swift编写的iOS应用中,我生成了一个带有以下内容的Metal缓冲区:

In my iOS app, written in Swift, I generate a Metal buffer with:

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

并使用以下命令将其绑定到我的着色器程序:

And bind it to my shader program with:

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

在用金属着色语言编写的着色器程序中,可以访问缓冲区的大小吗?我想访问缓冲区中的下一个顶点进行一些微分计算.像这样:

In my shader program, written in Metal shading language, can I access the size of the buffer? I would like to access the next vertex in my buffer to do some differential calculation. Something like:

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;
    // ...
}

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

Is my only option to pass the number of vertices as a uniform?

推荐答案

据我所知,不可以,因为顶点指向一个地址.就像C ++一样,必须有两件事才能知道数组的数量或大小:
1)知道数组的数据类型(浮点数或某些struct)
AND
2a)数据类型为OR的数组计数
2b)数组的总字节数.

As far as I know, no you can't because the vertices points to an address. Just like C++, must have two things to know the count or size of an array:
1) know what data type of the array (float or some struct)
AND
2a) the array count for the data type OR
2b) the total bytes of the array.

是的,您需要将数组计数统一传递.

So yes, you would need to pass the array count as a uniform.

这篇关于我可以从Metal着色器获取缓冲区的大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 02:09