所以我在Win32应用程序中设置了我的OpenGL上下文。我正在建立一个多线程环境,在一个线程中处理窗口消息,在另一个线程中处理OpenGL渲染。到目前为止,我的工作流程如下:
主题A:
然后,在主题B中:
问题是,线程B中的wglMakeCurrent返回false,getLastError返回170(正在使用的资源)。我读过的所有内容都暗示这意味着渲染上下文已经在线程A中使用,但是在线程B被创建之前,我在线程A中明确地调用了“wglMakeCurrent(NULL,NULL)”。
可能出现什么问题?
PS。为了清楚起见,我不是试图从不同的并发线程运行OpenGL调用。每个OpenGL调用都将来自线程B,但在线程B存在之前创建渲染上下文除外。
编辑:这是一些源代码。这是在线程A中初始化窗口的函数:
Window* Window::init(void)
{
/* If the singleton exists already, just return a pointer to it */
if (singleton)
return singleton;
/* Allocate the singleton and check for errors */
singleton = new Window();
if (singleton == NULL)
{
fatalerror("In Window::init():\n");
fatalmore("Memory allocation failure.\n");
fatalmore("Could not allocate singleton.\n");
_getch();
exit(1);
}
/* Register window class */
WNDCLASSEX wc;
memset(&wc, 0, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = wndProc;
wc.hInstance = GetModuleHandle(NULL);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = APP_NAME;
if (!RegisterClassEx(&wc))
{
fatalerror("In Window::init():\n");
fatalmore("Failed to register window class.\n");
fatalmore("Error code: '%i'\n", GetLastError());
clean();
_getch();
exit(1);
}
/* Read settings */
int width = Settings::get()->screenWidth;
int height = Settings::get()->screenHeight;
DWORD exStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
DWORD style = WS_OVERLAPPEDWINDOW;
/* Are we using fullscreen mode? */
if (Settings::get()->fullscreen)
{
width = Settings::get()->fullscreenWidth;
height = Settings::get()->fullscreenHeight;
DEVMODE dm;
memset(&dm, 0, sizeof(DEVMODE));
dm.dmSize = sizeof(DEVMODE);
dm.dmPelsWidth = width;
dm.dmPelsHeight = height;
dm.dmBitsPerPel = 32;
dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
warning("in Window::init():\n");
warnmore("The requested fullscreen mode is not supported.\n");
warnmore("Will continue in windowed mode.\n");
width = Settings::get()->screenWidth;
height = Settings::get()->screenHeight;
Settings::get()->fullscreen = false;
}
else
{
exStyle = WS_EX_APPWINDOW;
style = WS_POPUP;
}
}
/* Create the window */
singleton->wnd = CreateWindowEx( exStyle,
APP_NAME, APP_NAME,
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | style,
CW_USEDEFAULT, CW_USEDEFAULT,
width, height,
NULL,
NULL,
GetModuleHandle(NULL),
NULL );
if (!singleton->wnd)
{
fatalerror("In Window::init():\n");
fatalmore("Could not create window.\n");
fatalmore("Error code: '%i'\n", GetLastError());
clean();
_getch();
exit(1);
}
/* Get the DC */
singleton->dc = GetDC(singleton->wnd);
if (!singleton->dc)
{
fatalerror("In Window::init():\n");
fatalmore("Failed to get display context.\n");
fatalmore("Error code: '%i'\n", GetLastError());
clean();
_getch();
exit(1);
}
/* Choose a pixel format */
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
pfd.iLayerType = PFD_MAIN_PLANE;
int pf = ChoosePixelFormat(singleton->dc, &pfd);
if (!pf)
{
fatalerror("In Window::init():\n");
fatalmore("Failed to choose a pixel format.\n");
fatalmore("Error code: '%i'\n", GetLastError());
clean();
_getch();
exit(1);
}
/* Set the pixel format */
if (!SetPixelFormat(singleton->dc, pf, &pfd))
{
fatalerror("In Window::init():\n");
fatalmore("Failed to set pixel format '%i'.\n", pf);
fatalmore("Error code: '%i'\n", GetLastError());
clean();
_getch();
exit(1);
}
/* Create our fake, temporary OpenGL context */
HGLRC temprc = wglCreateContext(singleton->dc);
if (!temprc)
{
fatalerror("In Window::init():\n");
fatalmore("Failed to create a temporary context.\n");
fatalmore("Error code: '%i'\n", GetLastError());
clean();
_getch();
exit(1);
}
wglMakeCurrent(singleton->dc, temprc);
/* Initialize the OpenGL extensions we need */
if (!initGLCreationExtensions())
{
fatalerror("In Window::init():\n");
fatalmore("Failed to initialize OpenGL context creation extensions.\n");
clean();
_getch();
exit(1);
}
/* Check for OpenGL version */
int majorver = 0, minorver = 0;
glGetIntegerv(GL_MAJOR_VERSION, &majorver);
glGetIntegerv(GL_MINOR_VERSION, &minorver);
if (majorver < 3 || (majorver == 3 && minorver < 1))
{
fatalerror("In Window::init():\n");
fatalmore("OpenGL version 3.1 or higher is required.\n");
clean();
_getch();
exit(1);
}
/* Define context attributes */
int contextAttribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, majorver,
WGL_CONTEXT_MINOR_VERSION_ARB, minorver,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
0
};
/* Create the real OpenGL context */
singleton->rc = wglCreateContextAttribsARB(singleton->dc, NULL, contextAttribs);
if (!singleton->rc)
{
fatalerror("In Window::init():\n");
fatalmore("Failed to create OpenGL context.\n");
fatalmore("Error code: '%i'\n", GetLastError());
clean();
_getch();
exit(1);
}
ShowWindow(singleton->wnd, SW_SHOW);
/* Delete the fake context */
wglMakeCurrent(NULL, NULL);
wglDeleteContext(temprc);
info("Successfully created window.\n");
printf("\n");
return singleton;
}
这是在线程B中运行的启动函数:
bool Window::renderThreadStartupFunc(void)
{
if (!singleton)
return false;
if (!singleton->dc)
return false;
if (!wglMakeCurrent(singleton->dc, singleton->rc))
{
error("In Window::renderThreadStartupFunc():\n");
errormore("Could not make context current.\n");
errormore("Error code: '%i'\n", GetLastError());
return false;
}
if (!initGLExtensions())
{
error("In Window::renderThreadStartupFunc():\n");
errormore("Failed to initialize OpenGL extensions.\n");
return false;
}
return true;
}
这是负责启动和关闭线程B的代码。它在一个单独的类中运行,并使用c ++ 11的std :: thread库。
Renderer* Renderer::init(void)
{
/* If the singleton exists already, just return a pointer to it */
if (singleton)
return singleton;
/* Allocate the singleton and check for errors */
singleton = new Renderer();
if (singleton == NULL)
{
fatalerror("In Renderer::init():\n");
fatalmore("Memory allocation failure.\n");
fatalmore("Could not allocate singleton.\n");
_getch();
exit(1);
}
Window::get();
singleton->running = true;
renderThread = new thread(main);
if (renderThread == NULL)
{
fatalerror("In Renderer::init():\n");
fatalmore("Memory allocation failure.\n");
fatalmore("Could not allocate rendering thread.\n");
_getch();
exit(1);
}
Message startup;
startup.type = MT_STARTUP;
singleton->queue.push(startup);
return singleton;
}
void Renderer::clean(void)
{
if (!singleton)
return;
Message shutdown;
shutdown.type = MT_SHUTDOWN;
singleton->queue.push(shutdown);
if (renderThread)
{
renderThread->join();
delete renderThread;
}
}
答案 0 :(得分:3)
一些建议:
使用临时助手上下文作为当前上下文(您的扩展函数指针与Windows中的活动上下文绑定)创建正确的上下文,即不要将助手上下文从当前上下文中释放并且不释放帮助器上下文,在创建适当的上下文之前。
在启动线程B之前,在正确的上下文和hdc上调用wglMakeCurrent(...),调用glFinish()然后再调用wglMakeCurrent(NULL,NULL)并删除临时上下文。对于一些有缺陷的司机来说,这是一个肮脏的解决方法。