与基本逻辑代码相比,OpenGL glDrawElements()调用如何计税? [英] How taxing are OpenGL glDrawElements() calls compared to basic logic code?

查看:166
本文介绍了与基本逻辑代码相比,OpenGL glDrawElements()调用如何计税?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算对我的OpenGL程序做一些优化(它不需要优化,但我是为了它的缘故)。出于好奇,OpenGL绘图函数与基本逻辑代码相比有多昂贵?目前,我正在开始一个游戏,屏幕上充满了广场,代表一个2D块状景观。这意味着对一个正方形(两个三角形)的绘制调用被调用多次。目前,我打算添加一些代码,看看当前框架中的块的位置,并将它们组合在一起。例如,如果有一个7块高的列,而不是7个单独的drawBlock()函数(其中包含glDrawElements()调用),我可以调用一个函数,绘制一个矩形是1 x 7,因此

I'm planning to do some optimization on my OpenGL program (it doesn't need optimizing, but I'm doing it for the sake of it). Out of curiosity, how expensive are OpenGL drawing functions compared to basic logic code? At the moment, I'm making the start of a game where the screen is filled with squares, to represent a 2D blocky landscape. This means that the draw call for a square(two triangles) is called many times. At the moment, I'm planning to add in some code that looks at the positioning of blocks in the current frame, and groups them together. For example, if there is a column that is 7 blocks high, instead of doing 7 separate drawBlock() functions (which contain the glDrawElements() calls) I could call one function, that draws a rectangle that is 1 x 7, and so on, throughout the screen.

如果计算要绘制的代码实际上占用了更多的CPU,而不是单独绘制块,我不会这样做

I won't bother doing this if the code that calculates what to draw, actually uses up more of the CPU than just drawing the blocks individually would.

推荐答案

glDrawElements (或任何其他 OpenGL渲染命令)无法真正估计。这是因为它的成本取决于在绘制调用之间改变的OpenGL状态。调用OpenGL状态改变函数(基本上,任何不是某种形式的glGet或某种形式的glDraw的任何OpenGL函数)的成本将相对较快。但它会使下一个绘图调用变慢。

The cost of glDrawElements (or any other OpenGL rendering command) cannot really be estimated. This is because its cost depends a great deal on what OpenGL state you changed between draw calls. The cost of calling an OpenGL state changing function (basically, any OpenGL function that isn't a glGet of some form or a glDraw of some form) will be relatively quick. But it will make the next draw call slower.

这个关于OpenGL性能的视频显示哪些状态更改在绘制时比其他状态更昂贵。非常好的部分开始大约31分钟。

This video on OpenGL performance shows which state changes are more costly at draw time than others. The really good part starts around 31 minutes in.

如果你没有在绘制调用之间改变任何OpenGL状态,绘制调用是相对较快的。不同的状态对绘制调用有不同的影响。从最快到最慢(根据NVIDIA上面的演示,所以带上一粒盐):

Draw calls are relatively fast if you haven't changed any OpenGL state between draw calls. Different pieces of state have different effects on draw calls. From fastest to slowest (according to NVIDIA's presentation above, so take it with a grain of salt):

  • Non-UBO uniform updates
  • Vertex buffer bindings (without changing formats)
  • UBO binding
  • Vertex format changes
  • Texture bindings
  • Fragment post-processing state changes
  • Shader program changes
  • Render target switches

现在,绘图调用比基本逻辑更昂贵。他们不便宜,即使他们之间没有状态的变化。如果效率对你的代码很重要,那么分组你的广场是一个好主意。

Now, a draw call will be more expensive than "basic logic". They're not cheap, even without state changes between them. If efficiency is important to your code, then grouping your squares is a good idea.

这篇关于与基本逻辑代码相比,OpenGL glDrawElements()调用如何计税?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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