OpenGL仅在Windows 8上闪烁

OpenGL仅在Windows 8上闪烁,opengl,windows-8,Opengl,Windows 8,我有一个Windows应用程序,它使用OpenGL以一种相当简单的方式将纹理图像(四边形)渲染到其客户端区域的多个窗口中 该应用程序在Windows 7上运行良好,但在Windows 8上,它有时会闪烁并显示背景以响应用户操作,如鼠标单击窗口。确切地说:有时当鼠标点击某个窗口时(不是每次点击鼠标),所有窗口都会短暂闪烁 我正在调查这种行为的可能原因。我试图捕捉WM_ERASEBKGND消息,但这似乎不是原因。我试过使用不同的GPU卡(和相应的驱动程序),这在所有的GPU卡(Nvidia Quad

我有一个Windows应用程序,它使用OpenGL以一种相当简单的方式将纹理图像(四边形)渲染到其客户端区域的多个窗口中

该应用程序在Windows 7上运行良好,但在Windows 8上,它有时会闪烁并显示背景以响应用户操作,如鼠标单击窗口。确切地说:有时当鼠标点击某个窗口时(不是每次点击鼠标),所有窗口都会短暂闪烁

我正在调查这种行为的可能原因。我试图捕捉WM_ERASEBKGND消息,但这似乎不是原因。我试过使用不同的GPU卡(和相应的驱动程序),这在所有的GPU卡(Nvidia Quadro和ATI Radeon HD)上都会发生。此外,我让一些图像窗口使用GDI进行绘图,事实上,当其他窗口闪烁时,这些图像不会(以及菜单、按钮区域和状态栏)

更新:由于窗口是C#用户控件,因此我已将AllPaintingWimPaint和UserPaintStyles设置为“true”。DoubleBuffer为“false”,因为将其设置为“true”会导致闪烁更加频繁

一句话:所有用OpenGL绘制的窗口都会偶尔闪烁一次,而且只有它们会闪烁,而且只在Win8上闪烁(而不是在Win7上)

我将非常感谢关于这一原因的提示。我的代码如下。我初始化上下文如下:

void InitWindow(HWND *hWnd, HDC *hDC, HGLRC *hRC)
{
    *hDC = GetDC(*hWnd);

    PIXELFORMATDESCRIPTOR pfd;
    ZeroMemory(&pfd, sizeof(pfd));
    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | 
            PFD_DOUBLEBUFFER;

    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    // pfd.cRedBits = 8;    
    // pfd.cGreenBits = 8;
    // pfd.cBlueBits = 8;
    pfd.cAlphaBits = 0;
    pfd.cDepthBits = 0;
    pfd.iLayerType = PFD_MAIN_PLANE;

    int format = ChoosePixelFormat(*hDC, &pfd);
    SetPixelFormat(*hDC, format, &pfd);

    *hRC = wglCreateContext(*hDC);

    wglMakeCurrent(*hDC, *hRC);
}
int Render(HWND hWnd, Raster* raster)
{
    Size RasterSize = raster->GetSize();

    HDC hDC = NULL;
    HGLRC hRC = NULL;

    InitWindow(&hWnd, &hDC, &hRC);

    GLenum raster_pixel_format = GL_BGRA_EXT;
    GLint internal_format = GL_RGBA;

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture (GL_TEXTURE_2D, texture);

    glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glTexImage2D (GL_TEXTURE_2D, 0, internal_format, 
            RasterSize.width, RasterSize.height,
            0, raster_pixel_format, GL_UNSIGNED_BYTE,
            raster->GetData());

    glBindTexture(GL_TEXTURE_2D, texture);

    RECT wndRect;
    ::GetClientRect(hWnd, &wndRect);
    GLsizei wndWidth = wndRect.right;
    GLsizei wndHeight = wndRect.bottom;

    glEnable(GL_TEXTURE_2D);

    // this is usually stated in window coordinates,
    // but since we know the raster gets its size from 
    // the window - we can use raster coordinates
    glViewport(0, 0, RasterSize.width, RasterSize.height);

    glBegin( GL_QUADS );    

    glTexCoord2d(0.0,0.0); glVertex2d(-1.0,+1.0);
    glTexCoord2d(1.0,0.0); glVertex2d(+1.0,+1.0);
    glTexCoord2d(1.0,1.0); glVertex2d(+1.0,-1.0);
    glTexCoord2d(0.0,1.0); glVertex2d(-1.0,-1.0);

    glEnd();

    glDisable(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, 0);
    glDeleteTextures(1, &texture);

    SwapBuffers(hDC);
    wglMakeCurrent(NULL, NULL);

    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);

    return SUCCESS;
}
然后像这样画到屏幕上:

void InitWindow(HWND *hWnd, HDC *hDC, HGLRC *hRC)
{
    *hDC = GetDC(*hWnd);

    PIXELFORMATDESCRIPTOR pfd;
    ZeroMemory(&pfd, sizeof(pfd));
    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | 
            PFD_DOUBLEBUFFER;

    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    // pfd.cRedBits = 8;    
    // pfd.cGreenBits = 8;
    // pfd.cBlueBits = 8;
    pfd.cAlphaBits = 0;
    pfd.cDepthBits = 0;
    pfd.iLayerType = PFD_MAIN_PLANE;

    int format = ChoosePixelFormat(*hDC, &pfd);
    SetPixelFormat(*hDC, format, &pfd);

    *hRC = wglCreateContext(*hDC);

    wglMakeCurrent(*hDC, *hRC);
}
int Render(HWND hWnd, Raster* raster)
{
    Size RasterSize = raster->GetSize();

    HDC hDC = NULL;
    HGLRC hRC = NULL;

    InitWindow(&hWnd, &hDC, &hRC);

    GLenum raster_pixel_format = GL_BGRA_EXT;
    GLint internal_format = GL_RGBA;

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture (GL_TEXTURE_2D, texture);

    glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glTexImage2D (GL_TEXTURE_2D, 0, internal_format, 
            RasterSize.width, RasterSize.height,
            0, raster_pixel_format, GL_UNSIGNED_BYTE,
            raster->GetData());

    glBindTexture(GL_TEXTURE_2D, texture);

    RECT wndRect;
    ::GetClientRect(hWnd, &wndRect);
    GLsizei wndWidth = wndRect.right;
    GLsizei wndHeight = wndRect.bottom;

    glEnable(GL_TEXTURE_2D);

    // this is usually stated in window coordinates,
    // but since we know the raster gets its size from 
    // the window - we can use raster coordinates
    glViewport(0, 0, RasterSize.width, RasterSize.height);

    glBegin( GL_QUADS );    

    glTexCoord2d(0.0,0.0); glVertex2d(-1.0,+1.0);
    glTexCoord2d(1.0,0.0); glVertex2d(+1.0,+1.0);
    glTexCoord2d(1.0,1.0); glVertex2d(+1.0,-1.0);
    glTexCoord2d(0.0,1.0); glVertex2d(-1.0,-1.0);

    glEnd();

    glDisable(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, 0);
    glDeleteTextures(1, &texture);

    SwapBuffers(hDC);
    wglMakeCurrent(NULL, NULL);

    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);

    return SUCCESS;
}

问题是由于使用了带有多个视频卡的机器。
禁用任何一张卡都可以解决此问题。

首先,为什么每次绘制场景时都要创建和销毁渲染上下文?WGL禁止您多次为特定HWND的设备上下文设置像素格式,因此从技术上讲,这是无效的行为。您可能会侥幸逃脱,因为您每次都选择相同的像素格式,但它仍然无效。我坚信,如果您不在每一帧创建和销毁渲染上下文,您的问题就会消失。实际上,这是一个相当简化的代码。有一个开关可以打开/关闭上下文缓存(以HWND作为键),这使得InitWindow()只能被调用一次。我通常不缓存这些句柄,因为我仍然存在缓存失效问题(窗口在层次结构中被破坏得太远),但即使打开它(没有窗口被破坏)也没有帮助。同样,在Win7中,应用程序在没有缓存的情况下平稳运行。