Thanks Robert,

I am providing you with more details on this.  I think i am realizing the 
graphics window before using the handle. Still to make sure i am providing with 
the code for your kind review.
Please Check the following code snippet:


Code:

int main(int argc, char *argv[])
{
   osg::setNotifyLevel( osg::FATAL );

   //load the model
   osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("cow.osg");
   
   //////////////////
   // SETUP VIEWER //
   //////////////////
   osgViewer::Viewer viewer;
   viewer.addEventHandler(new osgViewer::StatsHandler);
   viewer.addEventHandler(new osgOpenCL::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) );

   //create a singleton opencl context to make sure that
   //we are only working on one context
   osgOpenCL::Singleton<osgOpenCL::Context>::init();
   
   
osgOpenCL::Singleton<osgOpenCL::Context>::getPtr()->setupOsgOpenCLAndViewer(viewer);

   viewer.setSceneData(setupScene());

   return viewer.run();

}



 

"osgOpenCL::Singleton<osgOpenCL::Context>::getPtr()->setupOsgOpenCLAndViewer(viewer);
 " is doing the OpenCL context creation from the OpenGL context. As i mentioned 
before you do not need to worry about OpenCL related stuff. Now Lets get indie 
the function "setupOsgOpenCLAndViewer(viewer)"


Code:

   bool Context::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;
      //return a list of all active contexts
      viewer.getContexts( ctxs, true );

      //make sure that we are not getting any empty context
      if( ctxs.empty() )
      {
     osg::notify(osg::FATAL)<< __FUNCTION__ << ": no valid OpenGL context is 
found."<<std::endl;
     return false;
      }


      osg::GraphicsContext* ctx = NULL;


      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
      {
        //make the very first context in the list
        //the current context
        ctx = ctxs.front();
      }

      //make sure that the context is not NULL
      if( NULL == ctx )
      {
        osg::notify(osg::FATAL)<< __FUNCTION__ << ": cannot find valid OpenGL 
context."<<std::endl;
        return false;
      }

      if(!setupDeviceAndContext(*ctx))
      {
     osg::notify(osg::FATAL)<< __FUNCTION__ << ": cannot setup OpenGL with 
OpenCL."<<std::endl;
     return false;
      }
      return true;
   }




Now Inside the setupDeviceAndContext() i am doing the opencl initialization and 
only part of the snippet is related for your review. Here it goes:


Code:

   bool Context::setupDeviceAndContext(osg::GraphicsContext &ctx)
   {
      if( ctx.getState() == NULL )
      {
     osg::notify(osg::WARN)
        << __FUNCTION__ << ": osg::GraphicsContext must have a valid state."
        << std::endl;

     return false;
      }

      // Use first context to be found and make it current.
      ctx.makeCurrent();

      if( NULL != osgCompute::GLMemory::getContext() &&
      osgCompute::GLMemory::getContext()->getState()->getContextID() != 
ctx.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;

     return false;
      }

      // Bind context to osgCompute::GLMemory
      if( osgCompute::GLMemory::getContext() == NULL )
     osgCompute::GLMemory::bindToContext( ctx );
      else
     return false;


#ifdef defined(_WIN32)
      osgViewer::GraphicsHandleWin32 *windowsContext = NULL;
#elif defined( __GNUC__)
      osgViewer::GraphicsHandleX11 *linuxContext = NULL;
#elif defined( __APPLE__ )
      osgViewer::GraphicsHandleCarbon *osxContext = NULL;
#endif

      //platform dependent casting for the OpenCL context creation
#ifdef defined(_WIN32)
      windowsContext = dynamic_cast<osgViewer::GraphicsHandleWin32*>(&ctx);

      if(NULL == windowsContext)
      {
     osg::notify(osg::FATAL) << "Win32 Graphics Context Casting is 
unsuccessful" << std::endl;
     return false;
      }
#elif defined(__GNUC__)
      linuxContext = dynamic_cast<osgViewer::GraphicsHandleX11*>(&ctx);

      if(NULL == linuxContext)
      {
     osg::notify(osg::FATAL) << "X11 Graphics Context Casting is unsuccessful" 
<< std::endl;
     return false;
      }
#elif defined(__APPLE__)
      osxContext = dynamic_cast<osgViewer::GraphicsHandleCarbon*>(&ctx);

      if(NULL == osxContext)
      {
     osg::notify(osg::FATAL) << "MACOSX Graphics Context Casting is 
unsuccessful. " << std::endl;
     return false;
      }
#endif
............................................
............................................
     //by now the graphics context is found
     //and the platform id is also found
     cl_context_properties contextProperties[] =
        {
#ifdef defined(_WIN32)
           CL_CONTEXT_PLATFORM, (cl_context_properties) _m_clPlatform,
           CL_GL_CONTEXT_KHR, (cl_context_properties) 
windowsContext->getWGLContext(),
           CL_WGL_HDC_KHR, (cl_context_properties) windowsContext->getHDC(),
#elif defined(__GNUC__)
           CL_CONTEXT_PLATFORM, (cl_context_properties) _m_clPlatform,
           CL_GL_CONTEXT_KHR, (cl_context_properties) 
linuxContext->getContext(),
           CL_GLX_DISPLAY_KHR, (cl_context_properties) 
linuxContext->getDisplay(),
#elif defined(__APPLE__)
           CGLContextObj glContext = CGLGetCurrentContext(); // get the current 
GL context
           CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext); // share 
group

           CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
           (cl_context_properties) shareGroup,
#endif
           0
        };
}




I need those platform specific window handle to call those functions like 
getHDC() or getDisplay() .

Please let me know if you need any more info on this issue.

Thank you!

Regards
Sajjadul

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=56411#56411





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to