如何尽快交换缓冲区

编程入门 行业动态 更新时间:2024-10-23 15:17:56
本文介绍了如何尽快交换缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发OpenGL项目,正在使用VS15 C ++. 我的代码:

I'm working on OpenGL project and I'm using VS15 C++. My code:

timeBeginPeriod(1); //start timer to measure the gap GetSystemTime(&timeMiddle); timeMiddleLong = (timeMiddle.wMinute * 60 * 1000) + (timeMiddle.wSecond * 1000) + timeMiddle.wMilliseconds; myfile << "middle time = " << timeMiddleLong << endl; glClearColor(1.0f, 0, 0, 1.0f);//red screen //update screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SwapBuffers(hdc); glfwPollEvents(); Sleep(1); //sleep for ~2ms glClearColor(0, 0, 0, 0);//invisiable screen //update screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SwapBuffers(hdc); glfwPollEvents(); //stop timer and print to file the result GetSystemTime(&timeEnd); timeEndLong = (timeEnd.wMinute * 60 * 1000) + (timeEnd.wSecond * 1000) + timeEnd.wMilliseconds; myfile << "gap = " << timeEndLong - timeMiddleLong << endl; ReleaseDC(hWnd, hdc);

我想要什么? 尽可能快地在红色屏幕和不可见屏幕之间切换. 我知道什么? 我的显示器为60Hz,响应时间为〜7ms,这意味着如果显示器在红屏显示时完全刷新,它将停留13ms. 我能得到什么? 通过拍摄屏幕,我注意到红色屏幕停留了约32ms,这对我来说太长了.此外,在大多数情况下,间隙显示的值为〜2ms,有时为5-9ms 如何在最短的时间内显示红色屏幕?以及会持续多久?

What I want ? To switch between a red screen and invisible screen as fast as possible. What I know ? My monitor is 60Hz with respond time of ~7ms which means that if the monitor refreshes exactly when the red screen is up it will stay there for 13ms. What I get ? By filming the screen I noticed that the red screen stay for ~32ms which is too long for me. In addition, the gaps show values of ~2ms in most time and in some times 5-9ms How do I show the red screen for the shortest time? and how long it will be?

推荐答案

您的方法不起作用-所有的OpenGL函数(包括SwapBuffers)都可以在代码片段完成很长时间之后就在GPU上排队并执行.

Your approach won't work -- all the OpenGL functions, including the SwapBuffers, can be queued and executed on the GPU long after your code fragment had completed.

相反,请确保VSync处于打开状态(通常默认情况下为默认状态),并执行所有这些命令而中间不会出现任何睡眠:

Instead, make sure that VSync is on (it is by default, usually), and execute all those commands without any sleeps in the middle:

glClearColor(1.0f, 0, 0, 1.0f);//red screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SwapBuffers(hdc); glClearColor(0, 0, 0, 0); //invisiable screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SwapBuffers(hdc);

这将使红色屏幕闪烁一帧(对于60Hz显示器为16.7ms).

This will flash a red screen for exactly one frame (16.7ms for a 60Hz monitor).

由于OpenGL的异步特性,您的时间测量也不明智.您应该使用 OpenGL时间查询对象来测量GPU端的时间.

Due to the asynchronous nature of OpenGL your time measurements aren't sensible either. You should use OpenGL Time Query Objects to measure the times on the GPU side.

更多推荐

如何尽快交换缓冲区

本文发布于:2023-11-29 23:12:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓冲区

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!