在OpenGL中使用glut的vsync出现问题 [英] Trouble with vsync using glut in OpenGL

查看:158
本文介绍了在OpenGL中使用glut的vsync出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正竭尽全力地使Vsync在我的OpenGL应用程序中工作.这是重要的统计数据:

I'm struggling desperately to get Vsync to work in my OpenGL application. Here's the vital stats:

我正在使用Windows,使用C ++ OpenGL进行编码,并且正在将FreeGLUT用于我的OpenGL上下文(双缓冲).我知道要使交换缓冲区在Windows中等待垂直同步,您需要调用wglSwapIntervalEXT().

I'm using Windows, coding in C++ OpenGL and I'm using FreeGLUT for my OpenGL context (double buffering). I'm aware that for the swap buffer to wait for vertical sync in Windows you are required to call wglSwapIntervalEXT().

我的代码确实调用了此代码(如下所示),但是我仍然感到垂头丧气.我设法阻止它的唯一方法是调用glFinish(),这当然会给它带来很大的性能损失.

My code does call this (as you'll see below), yet I am still getting vertical tearing. The only way I've managed to stop it is by calling glFinish() which of course has a significant performance penalty associated with it.

main()函数的相关部分如下所示:

The relevant parts of my main() function look like this:

//Initiating glut window
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize (initial_window_width, initial_window_height);
glutInitWindowPosition (100, 100);
int glut_window_hWnd = glutCreateWindow(window_title.c_str()); 

//Setting up swap intervals
PFNWGLSWAPINTERVALEXTPROC       wglSwapIntervalEXT = NULL;
PFNWGLGETSWAPINTERVALEXTPROC    wglGetSwapIntervalEXT = NULL;

if (WGLExtensionSupported("WGL_EXT_swap_control"))
{
// Extension is supported, init pointers.
wglSwapIntervalEXT = PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");

// this is another function from WGL_EXT_swap_control extension
wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT");
}

wglSwapIntervalEXT (1)

init ();

glutMainLoop();    ///Starting the glut loop
return(0);

我应该指出wglInitSwapControlARB函数的返回值为true,因此支持该扩展.

I should point out that the return from the wglInitSwapControlARB function is true, so the extension is supported.

推荐答案

好吧-在我在这里获得的巨大帮助以及我自己的研究工作和处理工作之间,我发现了一些东西,包括适用于我的解决方案(以防其他人遇到此问题).

Ok - between the great help I've received here, and my own hours of research and messing around with it I've discovered a few things, including a solution that works for me (in case others come across this problem).

首先,我使用的是freeGLUT,我将代码转换为GLFW,结果是相同的,所以这不是API问题,请不要像我一样浪费时间!

Firstly, I was using freeGLUT, I converted my code to GLFW and the result was the same, so this was NOT an API issue, don't waste your time like I did!

至少在我的程序中,使用wglSwapIntervalEXT(1)不会停止垂直撕裂,这就是导致它难以解决的原因.

In my program at least, using wglSwapIntervalEXT(1) DOES NOT stop vertical tearing, and this was what led to it being such a headache to solve.

在我的NVIDIA驱动程序设置为VSYNC = ON的情况下,我仍在流泪(因为这等效于SwapInterval(1),但无济于事)-但设置正确,驱动程序正在执行应有的操作只是不知道,因为我还在流泪.

With my NVIDIA driver set to VSYNC = ON I was still getting tearing (because this is equivalent to SwapInterval(1) which doesn't help) - but it was set correctly, the driver was doing what it should have been I just didn't know it because I was still getting tearing.

因此我将NVIDIA驱动程序设置为VSYNC ='Application preference',并使用了wglSwapIntervalEXT(60)而不是我一直使用的1,并发现它实际上正在工作,因为它为我提供了大约1Hz的刷新率

So I set my NVIDIA driver to VSYNC = 'Application preference' and used wglSwapIntervalEXT(60) instead of 1 which I had always been using, and found that this was actually working because it was giving me a refresh rate of about 1Hz.

我不知道为什么wglSwapIntervalEXT(1)不能使我的屏幕垂直同步,但是wglSwapIntervalEXT(2)可以达到预期的效果,尽管显然我现在正在渲染效率低下的所有其他帧.

I don't know why wglSwapIntervalEXT(1) doesn't Vsync my screen, but wglSwapIntervalEXT(2) has the desired effect, though obviously I'm now rendering every other frame which is inefficient.

我发现禁用了VSYNC的glFinish不会撕裂,但是启用了它(如果有人可以解释为什么这样做会很棒).

I found that with VSYNC disabled glFinish DOES NOT help with tearing, but with it enabled it does (If anyone can explain why that would be great).

因此,总而言之,在设置了wglSwapIntervalEXT(1)并启用glFinish()的情况下,我再也不会撕裂了,但我仍然不明白为什么.

So in summary, with wglSwapIntervalEXT(1) set and glFinish() enabled I no longer have tearing, but I don't understand still why.

以下是我的应用中的一些性能统计信息(故意加载以使FPS低于60):

Here's some performance stats in my app (deliberately loaded to have FPS's below 60):

  • wglSwapIntervalEXT(0)=撕裂= 58 FPS
  • wglSwapIntervalEXT(1)=撕裂= 58 FPS
  • wglSwapIntervalEXT(2)=无撕裂= 30 FPS
  • wglSwapIntervalEXT(1)+ glFinish =不撕裂= 52 FPS

我希望这对以后的人有所帮助.感谢您的所有帮助.

I hope this helps someone in the future. Thanks for all your help.

这篇关于在OpenGL中使用glut的vsync出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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