金属将顶点着色器调用多少次? [英] How many times is the vertex shader called with metal?

查看:119
本文介绍了金属将顶点着色器调用多少次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习一些基本的金属渲染,并且坚持一些基本概念:

I've been learning some basic metal rendering, and I am stuck with some basic concepts:

我知道我们可以使用以下方式将顶点数据发送到着色器:

I know we can send vertex data to a shader using:

renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)

然后我们可以使用以下方法在着色器中检索它:

And then we can retrieve it in the shader with:

vertex float4 basic_vertex(const device VertexIn* vertexIn [[ buffer(0) ]], unsigned int vid [[ vertex_id ]])

据我了解,每个顶点将调用一次顶点函数,并且每次调用都会更新vertex_id以包含顶点索引.

As I understand it, the vertex function will be called once per each vertex, and vertex_id will update on each call to contain the vertex index.

问题是,那个vertex_id从哪里来?

The question is, from where comes that vertex_id?

我可以向着色器发送更多不同大小的数据:

I could send to the shader more data with different sizes:

renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
renderEncoder.setVertexBuffer(vertexBuffer2, offset: 0, index: 1)

如果vertexBuffer具有3个元素,而vertexBuffer2具有10个元素...顶点函数被调用多少次? 10个?

If vertexBuffer has 3 elements , and vertexBuffer2 has 10 elements ...how many times are the vertex function called? 10?

谢谢!

推荐答案

这取决于您在渲染命令编码器上进行的绘图调用.采用最简单的绘制方法:

That's determined by the draw call you make on the render command encoder. Take the simplest draw method:

drawPrimitives(type:vertexStart:vertexCount:)

vertexCount确定您的顶点函数被调用多少次.传递给顶点函数的顶点ID在vertexStartvertexStart + vertexCount - 1的范围内.

The vertexCount determines how many times your vertex function is called. The vertex IDs passed to the vertex function are those in the range from vertexStart to vertexStart + vertexCount - 1.

如果您考虑其他绘制方法:

If you consider another draw method:

drawPrimitives(type:vertexStart:vertexCount:instanceCount:)

那是在相同范围的顶点ID上.但是,它将调用您的顶点函数vertexCount * instanceCount次.将有instanceCount个调用,其顶点ID为vertexStart.这些调用的实例ID的范围为0到instanceCount - 1.同样,将有instanceCount个调用,其顶点ID为vertexStart + 1(假定为vertexCount >= 2),每个调用的[0..instanceCount-1]实例ID.等等.

That goes over the same range of vertex IDs. However, it calls your vertex function vertexCount * instanceCount times. There will be instanceCount calls with the vertex ID being vertexStart. The instance ID will range from 0 to instanceCount - 1 for those calls. Likewise, there will be instanceCount calls with the vertex ID being vertexStart + 1 (assuming vertexCount >= 2), one with each instance ID in [0..instanceCount-1]. Etc.

其他绘制方法还有其他多种选择,但是它们大多数不会影响调用顶点函数的次数.例如,baseInstance移动实例ID的范围,但不移动其大小.

The other draw methods have various other options, but they mostly don't affect how many times the vertex function is called. For example, baseInstance shifts the range of the instance IDs, but not its size.

各种drawIndexedPrimitives()方法从缓冲区获取特定的顶点ID,而不是枚举范围内的所有顶点ID.该缓冲区可以在多个位置包含给定的顶点ID.对于这种情况,我认为并没有定义是否可以为相同的顶点ID和实例ID多次调用顶点函数. Metal可能会尝试避免重复工作,但是,即使多个索引最终成为相同的顶点ID,实际上为索引缓冲区中的每个索引调用vertex函数实际上可能会更快.

The various drawIndexedPrimitives() methods get the specific vertex IDs from a buffer instead of enumerating all vertex IDs in a range. That buffer may contain a given vertex ID in multiple locations. For that case, I don't think it's defined whether the vertex function might be called multiple times for the same vertex ID and instance ID. Metal will presumably try to avoid duplicating effort, but it might end up actually being faster to just call the vertex function for every index in the index buffer even if multiple such indexes end up being the same vertex ID.

传递到顶点处理阶段的缓冲区中的顶点和数据之间的关系完全取决于您.您根本不需要传递任何缓冲区.例如,一个顶点函数可以仅根据顶点ID和实例ID完全通过计算生成顶点信息.

The relationship between vertexes and data in buffers you pass to the vertex processing stage is entirely up to you. You don't have to pass any buffers, at all. For example, a vertex function could generate vertex information completely computationally just from the vertex ID and instance ID.

当然,至少某些缓冲区包含每个顶点数据的数组是很普遍的,这些数组使用顶点ID进行索引.其他缓冲区可能是所有顶点都相同的统一数据(也就是说,您不使用顶点ID索引到该缓冲区中).金属本身不知道这一点.

It is pretty common, of course, for at least some of the buffers to contain arrays of per-vertex data that are indexed into using the vertex ID. Other buffers might be uniform data that's the same for all vertexes (that is, you don't index into that buffer using the vertex ID). Metal itself doesn't know this, though.

这篇关于金属将顶点着色器调用多少次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆