交错顶点提交对性能有何帮助? [英] How does interleaved vertex submission help performance?

查看:77
本文介绍了交错顶点提交对性能有何帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读并看到了其他问题,这些问题通常都指向将顶点位置和颜色等交织到一个数组中的建议,因为这可以最大限度地减少从 cpu 发送到 gpu 的数据.

I have read and seen other questions that all generally point to the suggestion to interleav vertex positions and colors, etc into one array, as this minimizes the data that gets sent from cpu to gpu.

我不清楚的是 OpenGL 如何做到这一点,即使使用交错数组,您仍然必须对位置和颜色指针进行单独的 GL 调用.如果两个指针都使用同一个数组,只是设置为从该数组中的不同点开始,绘制调用是否不会复制数组两次,因为它是两个不同指针的对象?

What I'm not clear on is how OpenGL does this when, even with an interleaved array, you must still make separate GL calls for position and color pointers. If both pointers use the same array, just set to start at different points in that array, does the draw call not copy the array twice since it was the object of two different pointers?

推荐答案

这主要是关于缓存.例如,假设我们有 4 个顶点和 4 种颜色.您可以通过这种方式提供信息(对不起,我不记得确切的函数名称)

This is mostly about cache. For example, imagine we have 4 vertex and 4 colors. You can provide the information this way (excuse me but I don't remember the exact function names)

glVertexPointer(..., vertex);
glColorPointer(..., colors);

它在内部所做的是读取顶点[0],然后应用颜色[0],然后再将顶点[1] 和颜色[1].如您所见,例如,如果顶点的长度为 20 兆字节,那么顶点[0] 和颜色[0] 至少可以相距 20 兆字节.

What it internally does, is read vertex[0], then apply colors[0], then again vertex[1] with colors[1]. As you can see, if vertex is, for example, 20 megs long, vertex[0] and colors[0] will be, to say the least, 20 megabytes apart from each other.

另一方面,如果你提供一个像 { vertex0, color0, vertex1, color1, etc.} 这样的结构,将会有很多缓存命中,因为 vertex0 和 color0 在一起,vertex1 也是如此和颜色1.

Now, on the other hand, if you provide a structure like { vertex0, color0, vertex1, color1, etc.} there will be a lot of cache hits because, well, vertex0 and color0 are together, and so are vertex1 and color1.

希望这有助于回答问题

第二次阅读时,我可能没有回答这个问题.您可能想知道 OpenGL 如何知道从该结构中读取哪些值?就像我之前说过的结构,例如 { vertex, color, vertex, color } 你告诉 OpenGL 顶点在位置 0,偏移量为 2(所以下一个将在位置 2,然后是 4 等)和颜色从位置 1 开始,偏移量也为 2(所以位置 1,然后是 3,等等).

edit: on second read, I may not have answered the question. You might probably be wondering how does OpenGL know which values to read from that structure, maybe? Like I said before with a structure such as { vertex, color, vertex, color } you tell OpenGL that vertex is at position 0, with an offset of 2 (so next one will be at position 2, then 4, etc) and color starts at position 1, with an offset of 2 also (so position 1, then 3, etc).

补充:如果你想要一个更实际的例子,看看这个链接 http://www.lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_(VBO).您可以在那里看到它如何只提供一次缓冲区,然后使用偏移量来高效渲染.

addition: In case you want a more practical example, look at this link http://www.lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_(VBO). You can see there how it only provides the buffer once and then uses offsets to render efficiently.

这篇关于交错顶点提交对性能有何帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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