OpenGL,在GPU上测量渲染时间 [英] OpenGL, measuring rendering time on gpu

查看:633
本文介绍了OpenGL,在GPU上测量渲染时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处

所以我想在GPU方面进行一些测量.

So I would like to take some measurements on the gpu side.

通过阅读线程我在我的绘图函数周围编写了这段代码,包括gl错误检查和swapBuffers()(实际上已禁用自动交换)

By reading this thread I wrote this code around my draw functions, including the gl error check and the swapBuffers() (auto swapping is indeed disabled)

        gl4.glBeginQuery(GL4.GL_TIME_ELAPSED, queryId[0]);
        {    
            draw(gl4);

            checkGlError(gl4);

            glad.swapBuffers();
        }
        gl4.glEndQuery(GL4.GL_TIME_ELAPSED);
        gl4.glGetQueryObjectiv(queryId[0], GL4.GL_QUERY_RESULT, frameGpuTime, 0);

并且由于OpenGL渲染命令应该是异步(驱动程序可以缓冲多达X命令一起批量发送之前),我的问题本质上是关于以下情况:

And since OpenGL rendering commands are supposed to be asynchronous ( the driver can buffer up to X commands before sending them all together in one batch), my question regards essentially if:

  • 上面的代码是正确的

  • the code above is correct

我是对的,假设在一个新帧的开始,所有先前的GL命令(来自前一帧)已在gpu上发送,执行和终止

I am right assuming that at the begin of a new frame all the previous GL commands (from the previous frame) have been sent, executed and terminated on the gpu

我是对的,假设当我用glGetQueryObjectivGL_QUERY_RESULT获得查询结果时,到目前为止所有GL命令都已终止?那是OpenGL将等待直到结果变得可用(从线程开始)?

I am right assuming that when I get query result with glGetQueryObjectiv and GL_QUERY_RESULT all the GL commands so far have been terminated? That is OpenGL will wait until the result become available (from the thread)?

推荐答案

是的,当您查询计时器时,它将一直阻塞,直到数据可用为止,即,直到GPU完成了从开始到结束查询之间发生的所有事情为止.为了避免与GPU同步,您可以使用GL_QUERY_RESULT_AVAILABLE来检查结果是否已经可用,然后再读取它们.这可能需要不太直接的代码来保持对打开的查询的标签并定期检查它们,但这对性能的影响最小.每次等待值都是确保性能的可靠方法.

Yes, when you query the timer it will block until the data is available, ie until the GPU is finished with everything that happened between beginning and ending the query. To avoid synchronising with the GPU, you can use GL_QUERY_RESULT_AVAILABLE to check if the results are already available and only then read them then. That might require less straightforward code to keep tabs on open queries and periodically checking them, but it will have the least performance impact. Waiting for the value every time is a sure way to kill your performance.

要解决您的第二个问题,交换缓冲区并不一定意味着它将阻塞,直到操作成功.您可能会看到这种行为,但是很有可能它只是一个隐式glFlush并且命令缓冲区还不为空.这也是更想要的行为,因为理想情况下,您希望立即开始下一帧并保持CPU命令缓冲区已满.不过,请查看实现文档以获取更多信息,因为这是实现的定义.

To address your second question, swapping the buffer doesn't necessarily mean it will block until the operation succeeds. You may see that behaviour, but it's just as likely that it is just an implicit glFlush and the command buffer is not empty yet. Which is also the more wanted behaviour because ideally you want to start with your next frame right away and keep the CPUs command buffer filled. Check the implementations documentation for more info though, as that is implementation defined.

顺便说一句,检查错误最终可能是隐式同步,因此当您在命令流中等待错误检查时,您可能会看到命令缓冲区为空.

Edit 2: Checking for errors might end up being an implicit synchronization by the way, so you will probably see the command buffer emptying when you wait for error checking in the command stream.

这篇关于OpenGL,在GPU上测量渲染时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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