Hi forum,
I have not got any response from the forum so far. Let me narrate what i have
been trying so far. I subclassed the osg::GraphicsOperation and inside the
Context::operator() (osg::GraphicsContext *gc)
Code:
class LIBRARY_EXPORT Context : public osg::GraphicsOperation
{
.....................
......................
}
i am trying to create the opencl context as follows:
Code:
void Context::operator() (osg::GraphicsContext *gc)
{
if(gc->getState() == NULL)
{
osg::notify(osg::FATAL) << __FUNCTION__ << ": "
<< "osg::GraphicsContext must have a valid
state. "
<< std::endl;
exit(EXIT_FAILURE);
}
//make this context as the currrent one
gc->makeCurrent();
if( NULL != osgCompute::GLMemory::getContext() &&
osgCompute::GLMemory::getContext()->getState()->getContextID() !=
gc->getState()->getContextID() )
{
osg::notify(osg::WARN)
<< __FUNCTION__ << ": osgOpenCL can handle only a single context."
<< " However multiple contexts are detected."
<< " Please make sure to share a GL context among all windows."
<< std::endl;
exit(EXIT_FAILURE);
}
// Bind context to osgCompute::GLMemory
if( osgCompute::GLMemory::getContext() == NULL )
osgCompute::GLMemory::bindToContext( *gc );
//declare some variables to create OpenCL
//specific
cl_uint numPlatforms;
cl_platform_id *platformIDs;
cl_int errNum;
cl_device_id deviceID;
//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;
exit(EXIT_FAILURE);
}
//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;
exit(EXIT_FAILURE);
}
//now choose the very first platform and store it
_m_clPlatform = platformIDs[0];
char cBuffer[1024];
char deviceName[48];
char deviceExtensions[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;
}
cl_uint clDevCount = 0;
//get the number of devices under the platform
errNum =
clGetDeviceIDs(_m_clPlatform,CL_DEVICE_TYPE_GPU,0,NULL,&clDevCount);
if(errNum != CL_SUCCESS)
{
osg::notify(osg::FATAL) << __FUNCTION__ << ": " << __LINE__ << ": "
<< " Error with clGetDeviceIDs(): "
<< getErrorString(errNum) << std::endl;
exit(EXIT_FAILURE);
}
//create a device list with the number of devices
cl_device_id* clDevices = new cl_device_id[clDevCount];
//populate the device list with another call to the clGetDeviceIDs()
errNum = clGetDeviceIDs(_m_clPlatform,
CL_DEVICE_TYPE_GPU,clDevCount,clDevices,NULL);
if(errNum != CL_SUCCESS)
{
osg::notify(osg::FATAL) << __FUNCTION__ << ": " << __LINE__ << ": "
<< " Error with clGetDeviceIDs(): "
<< getErrorString(errNum) << std::endl;
exit(EXIT_FAILURE);
}
unsigned int clDevUsed = 0;
unsigned int clEndDev = clDevCount - 1;
//browse through all the found devices in the platform for the CL-GL
interpolation support
for( unsigned int i = clDevUsed ; (!_m_glInterOpSupported && (i <=
clEndDev)); ++i)
{
size_t extensionSize;
//get the size of the supported device extensions to allocate memory
for each device in the loop
errNum =
clGetDeviceInfo(clDevices[i],CL_DEVICE_EXTENSIONS,0,NULL,&extensionSize);
if(errNum != CL_SUCCESS)
{
osg::notify(osg::FATAL) << __FUNCTION__ << ": " << __LINE__ << ": "
<< " Error with clGetDeviceInfo(): "
<< getErrorString(errNum) << std::endl;
exit(EXIT_FAILURE);
}
if(extensionSize > 0)
{
//we have some extensions to allocate memory with
char *extensions = (char*)malloc(extensionSize);
errNum =
clGetDeviceInfo(clDevices[i],CL_DEVICE_EXTENSIONS,extensionSize,extensions,&extensionSize);
if(errNum != CL_SUCCESS)
{
osg::notify(osg::FATAL) << __FUNCTION__ << ": " << __LINE__ <<
": "
<< " Error with clGetDeviceInfo(): "
<< getErrorString(errNum) << std::endl;
exit(EXIT_FAILURE);
}
std::string stdDevString(extensions);
free(extensions);
size_t szOldPos = 0;
// extensions string is space delimited
size_t szSpacePos = stdDevString.find(' ',szOldPos);
while(szSpacePos != stdDevString.npos)
{
if(strcmp(GL_SHARING_EXTENSION,stdDevString.substr(szOldPos,szSpacePos -
szOldPos).c_str()) == 0)
{
//store the device id index that supports the gl-cl
interoperability
clDevUsed = i;
//flag the gl-cl support to true
_m_glInterOpSupported = true;
break;
}
do
{
szOldPos = szSpacePos + 1;
szSpacePos = stdDevString.find(' ', szOldPos);
}while(szSpacePos == szOldPos);
} // end of while
} // end of extensionSize > 0
} // end of the for loop to find the appropriate device
if(_m_glInterOpSupported)
{
// GL - CL interpolation is supported
#if defined (__APPLE__) || defined(MACOSX)
osgViewer::GraphicsHandleCocoa *osxHandle =
dynamic_cast<osgViewer::GraphicsHandleCocoa*>(gc);
CGLContextObj glContext = CGLGetCurrentContext(); // get the
current GL context
CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext); // share
group
#elif defined(_WIN32)
osgViewer::GraphicsHandlewin32 *winHandle =
dynamic_cast<osgViewer::GraphicsHandlewin32*>(gc);
#else
osgViewer::GraphicsWindowX11 *unixWindow =
dynamic_cast<osgViewer::GraphicsWindowX11*>(gc);
if(unixWindow == NULL)
{
osg::notify(osg::FATAL) << __FUNCTION__ << ": " <<
__LINE__ << ": "
<< "Unix Graphics Handle
Casting is not successful. " << std::endl;
exit(EXIT_FAILURE);
}
#endif
cl_context_properties properties[] =
{
#if defined(_WIN32)
......
.......
#elif defined(__GNUC__)
CL_CONTEXT_PLATFORM, (cl_context_properties) _m_clPlatform,
CL_GL_CONTEXT_KHR, (cl_context_properties)
unixWindow->getContext(),//unixHandle->getContext(),
CL_GLX_DISPLAY_KHR, (cl_context_properties)
unixWindow->getDisplay(),//unixHandle->getDisplay(),
#elif defined(__APPLE__)
CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
(cl_context_properties) shareGroup,
#endif
0
};
//now create the opencl context from the valid opengl context
_m_clContext =
clCreateContext(properties,1,&clDevices[clDevUsed],NULL,NULL,&errNum);
if(errNum != CL_SUCCESS)
{
osg::notify(osg::FATAL) << __FUNCTION__ << ": " << __LINE__ <<
": "
<< " OpenCL Context Creation with GL-CL
interoperability was not successful :"
<< getErrorString(errNum) << std::endl;
exit(EXIT_FAILURE);
}
//STORE THE DEVICE ID FOR THE USED DEVICE
_m_clDevice = clDevices[clDevUsed];
//now create the command queue - with the profiling enabled
_m_clCommandQueue = clCreateCommandQueue(_m_clContext,
_m_clDevice,
CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE |
CL_QUEUE_PROFILING_ENABLE,&errNum);
if(errNum != CL_SUCCESS)
{
osg::notify(osg::FATAL) << "OpenCL Command Queue Creation was
not successful: " << errNum << std::endl;
//cleanup any context related stuff created so far
cleanup();
exit(EXIT_FAILURE);
}
}
else
{
osg::notify(osg::FATAL) << "GL - CL interoperability is not
supported by the system. " << std::endl;
exit(EXIT_FAILURE);
}
}
While compiling i get the following error:
Code:
../../../lib/libosgOpenCLd.so:-1: error: undefined reference to `typeinfo for
osgViewer::GraphicsWindowX11'
I have included the necessary files for it as follows:
Code:
#ifdef _WIN32
#include <osgViewer/api/Win32/GraphicsHandleWin32>
#elif defined(__GNUC__)
#include <osgViewer/api/X11/GraphicsHandleX11>
#include <osgViewer/api/X11/GraphicsWindowX11>
#elif defined( __APPLE__)
#include <osgViewer/api/Cocoa/GraphicsHandleCocoa>
#endif
I am developing on the Linux platform , so i omitted the windows related stuff
here.
I think the idea is the same.
Any idea what is missing ?
Thanks
Sajjadul
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=56700#56700
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org