在realize()前打开预编译选项指令:
osg::DisplaySettings::instance()->setCompileContextsHint(true);
mpr_osgviewer->realize();
显示如下信息:
此时虽然trait::shareContext变量有了值,但是实际上其共享渲染描述表句柄hglrc是没有创建成功的。
我试图自己创建opengl图形共享上下文hglrc以实现多线程预编译显示列表,避免帧冲击,可惜无法成功。
osg::GraphicsContext* gc = mpr_osgviewer->getCamera()->getGraphicsContext();
osgViewer::GraphicsWindowWin32* gw = dynamic_cast<osgViewer::GraphicsWindowWin32*>(gc);
if (gw)
{
HGLRC _sharehglrc = 0;
bool succ = gc->makeCurrent();
HGLRC hglrc = gw->getWGLContext();
bool succ2 = ::wglShareLists(hglrc,_sharehglrc);
succ2返回值是false,wglShareLists失败,_sharehglrc仍然为空。
查询了本机的GL_VERSION,发现是4.0,高于1.01版,按照函数说明不应该返回错误的。
从网上现有的信息看 好像现在window下osg使用多线程预编译显示列表确实是有问题的,由于没有能成功创建共享渲染描述表句柄,所以没有编译绘制线程存在。
http://bbs.osgchina.org/forum.php?mod=viewthread&tid=2836
【
没有CompileContext的话,DatabasePager本身就不会执行预编译。那么这个时候预编译工作实际上是由Renderer代为完成的:
如果没有定义CompileContext的话,那么此处将自动处理databasePager中等待编译的数据。
Windows下的CompileContext一直有问题不能正常使用,因此setDoPreCompile将直接使用Renderer进行预编译工作
】
实际是在渲染线程中做的预编译,占用了渲染时间,不是真正的多线程编译显示列表。
暂时是束手无策了。
查询osg论坛得知shareContext目前在Linux+nvidia显卡环境下ok,windows环境时是有问题的。
http://forum.openscenegraph.org/viewtopic.php?t=6638&highlight=
追加新的尝试及结果:
使用nehe教程中的lesson7例子中自己加入创建共享渲染描述表代码wglShareLists,是能返回成功值得。
然后将此段代码移植到osg的源代码中替换相关创建hglrc代码(修改osgViewer::Win32WindowingSystem::createGraphicsContext(traits)--->setWindow(hwnd))
代码执行测试发现使用wglShareLists创建共享渲染描述表 成功。。。。。。%>_<% %>_<%
static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
16, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
1, // Use Stencil Buffer ( * Important * )
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
int PixelFormat = ChoosePixelFormat (_hdc, &pfd); // Find A Compatible Pixel Format
if (PixelFormat == 0) // Did We Find A Compatible Format?
{
// Failed
ReleaseDC (_hwnd, _hdc); // Release Our Device Context
_hdc = 0; // Zero The Device Context
DestroyWindow (_hwnd); // Destroy The Window
_hwnd = 0; // Zero The Window Handle
return false; // Return False
}
if(!SetPixelFormat(_hdc,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
{
//KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return false; // Return FALSE
}
_hglrc = wglCreateContext(_hdc);
HGLRC hRCShareing=wglCreateContext(_hdc);// 用于分享hRC的资源2010.8.9
bool res = wglShareLists(_hglrc, hRCShareing);
可惜的是 在另一个线程编译显示列表和纹理对象时会和osg本身流程中的显示列表和纹理对象管理有资源冲突(线程资源访问冲突).