Hi forum,
I believe that there are similar threads almost about this issue, but they did
not answer the problem i am having now. I have been the going through the
source of osgCuda and took the hint from it about context creation. I created a
separate class that handles all the OpenCL context creation from osg as follows:
Code:
osgOpenCLContext::osgOpenCLContext(): m_clContext(NULL),
m_clPlatform(NULL),
m_clDevice(NULL),
m_clCommandQueue(NULL)
{
}
osgOpenCLContext::~osgOpenCLContext()
{
if(m_clCommandQueue) clReleaseCommandQueue(m_clCommandQueue);
if(m_clContext) clReleaseContext(m_clContext);
}
bool osgOpenCLContext::setupOsgOpenCLAndViewer(osgViewer::ViewerBase &viewer,
int ctxID /*= -1 */)
{
// You must use single threaded version since osgCompute currently
// does only support single threaded applications.
viewer.setThreadingModel( osgViewer::ViewerBase::SingleThreaded );
// Does create a single OpenGL context
// which is not released at the end of a frame to secure
// CUDA launches everywhere
viewer.setReleaseContextAtEndOfFrameHint(false);
// Create the current OpenGL context and make it current
if( !viewer.isRealized() )
viewer.realize();
osgViewer::ViewerBase::Contexts ctxs;
viewer.getContexts( ctxs, true );
if( ctxs.empty() )
{
osg::notify(osg::FATAL)<< __FUNCTION__ << ": no valid OpenGL context
is found."<<std::endl;
return false;
}
osg::GraphicsContext* ctx = NULL;
#ifdef WIN32
osgViewer::GraphicsHandleWin32 *windowsContext = NULL;
#else
osgViewer::GraphicsHandleX11 *linuxContext = NULL;
#endif
if( ctxID != -1 )
{ // Find context with ctxID and make it current.
for( unsigned int c=0; c<ctxs.size(); ++c )
{
if( ctxs[c]->getState()->getContextID() == ctxID )
{
ctx = ctxs[c];
}
}
}
else
{
ctx = ctxs.front();
}
if( NULL == ctx )
{
osg::notify(osg::FATAL)<< __FUNCTION__ << ": cannot find valid OpenGL
context."<<std::endl;
return false;
}
//platform dependent casting
#ifdef WIN32
windowsContext = dynamic_cast<osgViewer::GraphicsHandleWin32*>(ctx);
if(NULL == windowsContext)
{
osg::notify(osg::FATAL) << "Win32 Graphics Context Casting is
unsuccessful" << std::endl;
return false;
}
else
osg::notify(osg::INFO) << "Win32 Graphics Context Casting is
successful" << std::endl;
#else
linuxContext = dynamic_cast<osgViewer::GraphicsHandleX11*>(ctx);
if(NULL == linuxContext)
{
osg::notify(osg::FATAL) << "X11 Graphics Context Casting is
unsuccessful" << std::endl;
return false;
}
else
osg::notify(osg::INFO) << "X11 Graphics Context Casting is successful"
<< std::endl;
#endif
cl_uint numPlatforms;
cl_platform_id *platformIDs;
cl_int errNum;
//first query the total number of platforms
errNum = clGetPlatformIDs(0,NULL,&numPlatforms);
if(errNum != CL_SUCCESS || numPlatforms <= 0)
{
osg::notify(osg::FATAL) << "Failed to find any OpenCL platform" <<
std::endl;
return false;
}
//next, allocate memory for the installed platforms, and query
//to get the list
platformIDs = (cl_platform_id *)alloca(sizeof(cl_platform_id) *
numPlatforms);
errNum = clGetPlatformIDs(numPlatforms,platformIDs,NULL);
if(errNum != CL_SUCCESS)
{
osg::notify(osg::FATAL) << "Failed to find any OpenCL platform" <<
std::endl;
return false;
}
//now choose the very first platform
m_clPlatform = platformIDs[0];
char cBuffer[1024];
//get the platform information from the selected platform
errNum =
clGetPlatformInfo(m_clPlatform,CL_PLATFORM_NAME,sizeof(cBuffer),cBuffer,NULL);
if(errNum == CL_SUCCESS)
{
osg::notify(osg::INFO) << "OpenCL platform name: " << cBuffer <<
std::endl;
}
//by now the graphics context is found
//and the platform id is also found
cl_context_properties contextProperties[] =
{
#ifdef WIN32
CL_GL_CONTEXT_KHR, (cl_context_properties)
windowsContext->getWGLContext(),
CL_WGL_HDC_KHR, (cl_context_properties) windowsContext->getHDC(),
#else
CL_GL_CONTEXT_KHR, (cl_context_properties) linuxContext->getContext(),
CL_GLX_DISPLAY_KHR, (intptr_t) linuxContext->getDisplay(),
#endif
CL_CONTEXT_PLATFORM, (cl_context_properties) m_clPlatform,
0
};
//create context with the GPU device only
m_clContext =
clCreateContextFromType(contextProperties,CL_DEVICE_TYPE_GPU,NULL,NULL,&errNum);
if(errNum != CL_SUCCESS)
{
std::cout << "Could not create GPU context." << std::endl;
osg::notify(osg::FATAL) << "Could not create GPU context." <<
std::endl;
return false;
}
else
osg::notify(osg::INFO) << "Context Creation for the GPU is Successful"
<< std::endl;
I can access the platform information and the program crashes while OpenCL
context creation from the OpenGL context.
I have the output of the osg::notify() and it does neither mention about the
success nor failure of the OpenCL context creation from the OpenGL context that
i pulled from the osgViewer. That is puzzling !
I have the following statements in the main function:
Code:
int main(int argc, char *argv[])
{
osg::setNotifyLevel( osg::INFO );
//////////////////
// SETUP VIEWER //
//////////////////
osgViewer::Viewer viewer;
viewer.addEventHandler(new osgViewer::StatsHandler);
//viewer.addEventHandler(new osgCuda::StatsHandler);
viewer.addEventHandler(new osgViewer::HelpHandler);
viewer.setUpViewInWindow( 50, 50, 640, 480);
viewer.getCamera()->setClearColor( osg::Vec4(0.15, 0.15, 0.15, 1.0) );
//declare an opencl context
osgOpenCL::osgOpenCLContext clContext;
clContext.setupOsgOpenCLAndViewer(viewer);
return viewer.run();
}
And here goes the output of the output while running the OpenCL context
creation from osg:
Code:
CullSettings::readEnvironmentalVariables()
DatabasePager::addDatabaseThread() HANDLE_NON_HTTP
DatabasePager::addDatabaseThread() HANDLE_ONLY_HTTP
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
ShaderComposer::ShaderComposer() 0x8127da8
CullSettings::readEnvironmentalVariables()
ShaderComposer::ShaderComposer() 0x812b590
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
ShaderComposer::ShaderComposer() 0x812ee70
ShaderComposer::ShaderComposer() 0x812fff8
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
CullSettings::readEnvironmentalVariables()
ShaderComposer::ShaderComposer() 0x81330a8
ShaderComposer::ShaderComposer() 0x8134230
GraphicsContext::registerGraphicsContext 0x8135e48
GraphicsContext::getWindowingSystemInterface() 0x8127770 0xb7767d68
GraphicsContext::getWindowingSystemInterface() 0x8127770 0xb7767d68
ShaderComposer::ShaderComposer() 0x81dc448
GraphicsContext::createNewContextID() creating contextID=0
Updating the MaxNumberOfGraphicsContexts to 1
View::setUpViewOnSingleScreen - GraphicsWindow has been created successfully.
osg::State::_maxTexturePoolSize=0
osg::State::_maxBufferObjectPoolSize=0
X11 Graphics Context Casting is successful
OpenCL platform name: NVIDIA CUDA
Segmentation fault (core dumped)
As you can see that the X11 graphaics context casting is successful and i do
can access the platform information. And then the application crashes.
It will nice to hear views over this isssue to make it functional.
Thank you!
Regards
Sajjadul
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=53963#53963
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org