Get the opengl context

Hello forum,

I need to create an opencl context from the existing opengl context.

How is it possible to get the reference of the single/current opengl context from OF ?

Any reference or example is needed.

Thanks
Sajjadul

The name depends on the platform

so for instance on the RPi I use

EGLContext context = (ofAppEGLWindow *) ofGetWindowPtr()->getEglContext();

Thanks for the hint!

I think it returns the current opengl context.

Do i have to think about the multiple opengl contexts in OF? How to extract information only of the current one in that case?

Thanks

Hi ,

@jvcleave Which class header do i have to include to get the opengl context ? I went to the source of your addons(ofxRPiCameraVideoGrabber) to get the hint and i am doing it in the following manner:

#pragma once

#include "ofMain.h"

#include <boost/compute/system.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/command_queue.hpp>

namespace compute = boost::compute;

class ofOpenCLSingleton
{
public:
   ofOpenCLSingleton();
   ~ofOpenCLSingleton();

   void initSharedContext();

   compute::device m_device;
   compute::context m_context;
   compute::command_queue m_command_queue;
};

I am getting the following error:

libs/openFrameworks/communication -I../../../libs/openFrameworks/3d -I../../../libs/openFrameworks/utils -I../../../libs/openFrameworks/video  -MMD -MP -MF obj/linux/Release/src/ofOpenCLSingleton.d -MT obj/linux/Release/src/ofOpenCLSingleton.o -o obj/linux/Release/src/ofOpenCLSingleton.o -c src/ofOpenCLSingleton.cpp
In file included from ../../../libs/openFrameworks/utils/ofConstants.h:344:0,
                 from ../../../libs/openFrameworks/ofMain.h:5,
                 from src/ofOpenCLSingleton.h:3,
                 from src/ofOpenCLSingleton.cpp:1:
/usr/include/c++/4.6/cmath:99:11: error: ‘::acos’ has not been declared
/usr/include/c++/4.6/cmath:115:11: error: ‘::asin’ has not been declared
/usr/include/c++/4.6/cmath:131:11: error: ‘::atan’ has not been declared
/usr/include/c++/4.6/cmath:147:11: error: ‘::atan2’ has not been declared
/usr/include/c++/4.6/cmath:166:11: error: ‘::ceil’ has not been declared
/usr/include/c++/4.6/cmath:182:11: error: ‘::cos’ has not been declared
/usr/include/c++/4.6/cmath:198:11: error: ‘::cosh’ has not been declared
/usr/include/c++/4.6/cmath:214:11: error: ‘::exp’ has not been declared
/usr/include/c++/4.6/cmath:230:11: error: ‘::fabs’ has not been declared
/usr/include/c++/4.6/cmath:246:11: error: ‘::floor’ has not been declared
/usr/include/c++/4.6/cmath:262:11: error: ‘::fmod’ has not been declared
/usr/include/c++/4.6/cmath:272:11: error: ‘::frexp’ has not been declared
/usr/include/c++/4.6/cmath:288:11: error: ‘::ldexp’ has not been declared
/usr/include/c++/4.6/cmath:304:11: error: ‘::log’ has not been declared
/usr/include/c++/4.6/cmath:320:11: error: ‘::log10’ has not been declared
/usr/include/c++/4.6/cmath:336:11: error: ‘::modf’ has not been declared
/usr/include/c++/4.6/cmath:346:11: error: ‘::pow’ has not been declared
/usr/include/c++/4.6/cmath:381:11: error: ‘::sin’ has not been declared
/usr/include/c++/4.6/cmath:397:11: error: ‘::sinh’ has not been declared
/usr/include/c++/4.6/cmath:413:11: error: ‘::sqrt’ has not been declared
/usr/include/c++/4.6/cmath:429:11: error: ‘::tan’ has not been declared
/usr/include/c++/4.6/cmath:445:11: error: ‘::tanh’ has not been declared
/usr/include/c++/4.6/cmath: In function ‘typename __gnu_cxx::__enable_if<std::__is_arithmetic<_Tp>::__value, int>::__type std::fpclassify(_Tp)’:
/usr/include/c++/4.6/cmath:737:35: error: ‘FP_NAN’ was not declared in this scope
/usr/include/c++/4.6/cmath:737:43: error: ‘FP_INFINITE’ was not declared in this scope
/usr/include/c++/4.6/cmath:737:56: error: ‘FP_NORMAL’ was not declared in this scope
/usr/include/c++/4.6/cmath:738:7: error: ‘FP_SUBNORMAL’ was not declared in this scope
/usr/include/c++/4.6/cmath:738:21: error: ‘FP_ZERO’ was not declared in this scope
In file included from ../../../libs/openFrameworks/types/ofPoint.h:3:0,
                 from ../../../libs/openFrameworks/math/ofMath.h:15,
                 from ../../../libs/openFrameworks/types/ofColor.h:5,
                 from ../../../libs/openFrameworks/types/ofTypes.h:4,
                 from ../../../libs/openFrameworks/utils/ofLog.h:5,
                 from ../../../libs/openFrameworks/ofMain.h:7,
                 from src/ofOpenCLSingleton.h:3,
                 from src/ofOpenCLSingleton.cpp:1:
../../../libs/openFrameworks/math/ofVec3f.h: In member function ‘ofVec3f ofVec3f::getRotated(float, float, float) const’:
../../../libs/openFrameworks/math/ofVec3f.h:566:38: error: call of overloaded ‘cos(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:566:38: note: candidates are:
/usr/include/c++/4.6/cmath:185:3: note: float std::cos(float)
/usr/include/c++/4.6/cmath:189:3: note: long double std::cos(long double)
../../../libs/openFrameworks/math/ofVec3f.h:567:38: error: call of overloaded ‘sin(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:567:38: note: candidates are:
/usr/include/c++/4.6/cmath:384:3: note: float std::sin(float)
/usr/include/c++/4.6/cmath:388:3: note: long double std::sin(long double)
../../../libs/openFrameworks/math/ofVec3f.h:568:38: error: call of overloaded ‘cos(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:568:38: note: candidates are:
/usr/include/c++/4.6/cmath:185:3: note: float std::cos(float)
/usr/include/c++/4.6/cmath:189:3: note: long double std::cos(long double)
../../../libs/openFrameworks/math/ofVec3f.h:569:38: error: call of overloaded ‘sin(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:569:38: note: candidates are:
/usr/include/c++/4.6/cmath:384:3: note: float std::sin(float)
/usr/include/c++/4.6/cmath:388:3: note: long double std::sin(long double)
../../../libs/openFrameworks/math/ofVec3f.h:570:38: error: call of overloaded ‘cos(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:570:38: note: candidates are:
/usr/include/c++/4.6/cmath:185:3: note: float std::cos(float)
/usr/include/c++/4.6/cmath:189:3: note: long double std::cos(long double)
../../../libs/openFrameworks/math/ofVec3f.h:571:38: error: call of overloaded ‘sin(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:571:38: note: candidates are:
/usr/include/c++/4.6/cmath:384:3: note: float std::sin(float)
/usr/include/c++/4.6/cmath:388:3: note: long double std::sin(long double)
../../../libs/openFrameworks/math/ofVec3f.h: In member function ‘ofVec3f& ofVec3f::rotate(float, float, float)’:
../../../libs/openFrameworks/math/ofVec3f.h:597:38: error: call of overloaded ‘cos(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:597:38: note: candidates are:
/usr/include/c++/4.6/cmath:185:3: note: float std::cos(float)
/usr/include/c++/4.6/cmath:189:3: note: long double std::cos(long double)
../../../libs/openFrameworks/math/ofVec3f.h:598:38: error: call of overloaded ‘sin(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:598:38: note: candidates are:
/usr/include/c++/4.6/cmath:384:3: note: float std::sin(float)
/usr/include/c++/4.6/cmath:388:3: note: long double std::sin(long double)
../../../libs/openFrameworks/math/ofVec3f.h:599:38: error: call of overloaded ‘cos(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:599:38: note: candidates are:
/usr/include/c++/4.6/cmath:185:3: note: float std::cos(float)
/usr/include/c++/4.6/cmath:189:3: note: long double std::cos(long double)
../../../libs/openFrameworks/math/ofVec3f.h:600:38: error: call of overloaded ‘sin(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:600:38: note: candidates are:
/usr/include/c++/4.6/cmath:384:3: note: float std::sin(float)
/usr/include/c++/4.6/cmath:388:3: note: long double std::sin(long double)
../../../libs/openFrameworks/math/ofVec3f.h:601:38: error: call of overloaded ‘cos(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:601:38: note: candidates are:
/usr/include/c++/4.6/cmath:185:3: note: float std::cos(float)
/usr/include/c++/4.6/cmath:189:3: note: long double std::cos(long double)
../../../libs/openFrameworks/math/ofVec3f.h:602:38: error: call of overloaded ‘sin(double)’ is ambiguous
../../../libs/openFrameworks/math/ofVec3f.h:602:38: note: candidates are:
/usr/include/c++/4.6/cmath:384:3: note: float std::sin(float)
/usr/include/c++/4.6/cmath:388:3: note: long double std::sin(long double)
In file included from ../../../libs/openFrameworks/types/ofColor.h:5:0,
                 from ../../../libs/openFrameworks/types/ofTypes.h:4,
                 from ../../../libs/openFrameworks/utils/ofLog.h:5,
                 from ../../../libs/openFrameworks/ofMain.h:7,
                 from src/ofOpenCLSingleton.h:3,
                 from src/ofOpenCLSingleton.cpp:1:
../../../libs/openFrameworks/math/ofMath.h: In function ‘Type ofInterpolateCosine(Type, Type, float)’:
../../../libs/openFrameworks/math/ofMath.h:91:22: error: call of overloaded ‘cos(double&)’ is ambiguous
../../../libs/openFrameworks/math/ofMath.h:91:22: note: candidates are:
/usr/include/c++/4.6/cmath:185:3: note: float std::cos(float)
/usr/include/c++/4.6/cmath:189:3: note: long double std::cos(long double)
In file included from ../../../libs/poco/include/Poco/Mutex.h:57:0,
                 from ../../../libs/openFrameworks/types/ofTypes.h:69,
                 from ../../../libs/openFrameworks/utils/ofLog.h:5,
                 from ../../../libs/openFrameworks/ofMain.h:7,
                 from src/ofOpenCLSingleton.h:3,
                 from src/ofOpenCLSingleton.cpp:1:
../../../libs/poco/include/Poco/Mutex_POSIX.h: In member function ‘bool Poco::MutexImpl::tryLockImpl()’:
../../../libs/poco/include/Poco/Mutex_POSIX.h:91:17: error: ‘EBUSY’ was not declared in this scope
make[1]: *** [obj/linux/Release/src/ofOpenCLSingleton.o] Error 1
make[1]: Leaving directory `/home/sajjad/Downloads/openFrameworks/release/of_v0.8.1_linux_release/apps/myApps/mySmokeParticles'
make: *** [Release] Error 2

In the source i have not done anything yet though.

#include "ofOpenCLSingleton.h"

ofOpenCLSingleton::ofOpenCLSingleton()
{

}

ofOpenCLSingleton::~ofOpenCLSingleton()
{

}

void ofOpenCLSingleton::initSharedContext()
{

}

Any hint to get rid of it ?

Thanks

Looks like you have a boost linking problem

Hi

I edited the config.make file as follows:

PROJECT_EXTERNAL_SOURCE_PATHS += /usr/local/cuda-5.5/include
PROJECT_EXTERNAL_SOURCE_PATHS += /home/sajjad/Downloads/OpenCL/library/BoostCompute/compute-develop/include

I checked that do not get that error if comment the first line. But i need it so that boost.compute can access the OpenCL related header file.

Any example / reference where OpenCL is included in the OF project ?

I personally haven’t used OpenCL/Cuda with OF. Typically the way I try and approach these issues is to find a non-OF app that uses the libraries and then incorporate their Makefile into the OF app’s project Makefile

If you are using compiled libraries for cuda/boost you will probably need some stuff for the LDFLAGS

Hi

I am already used OpenCL with non-OF application and it is working fine.

Here goes the snapshot of the Makefile:

CC = g++
CFLAGS = -O2 -g
INCLUDE = -I/usr/local/cuda-5.5/include
INCLUDE += -I/home/sajjad/Downloads/OpenCL/library/BoostCompute/compute-develop/include
LDFLAGS =  -lglut -lGL -lGLU -lGLEW -lOpenCL
COBJS=$(patsubst %.cpp,%.o,$(wildcard *.cpp))

EXE= smokeparticles

all: $(COBJS) 
	$(CC) $(CFLAGS) -o $(EXE) $(COBJS) $(LDFLAGS)

%.o : %.cpp 
	$(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE) 


clean:
	rm -f $(EXE) *.o *~

Now how to incorporate its makefile into the OF app’s prioject Makefile ?

The project LDFLAGS is also mentioned as follows:

PROJECT_LDFLAGS += -lOpenCL

The error prevails.

Any other hint?

maybe try using

PROJECT_CFLAGS = -I/usr/local/cuda-5.5/include -I/home/sajjad/Downloads/OpenCL/library/BoostCompute/compute-develop/include

instead of PROJECT_EXTERNAL_SOURCE_PATHS

Thanks !

The last hint seems to be compiling fine. I got something new . I am trying to create a singleton instance of the OpenCL context so that several classes within the project can access the single opencl context. To make sure that i am having the opencl device with the opengl interoperability i am trying to print the device name to the console. But is not printing anything . Here goes the snippet:

#include "ofMain.h"
#include "ofApp.h"
#include "singleton.h"
#include "OpenCLSingleton.h"
//========================================================================
int main( )
{
   
   ofSetupOpenGL(1024,768,OF_WINDOW);			// <-------- setup the GL context
   
   // this kicks off the running of my app
   // can be OF_WINDOW or OF_FULLSCREEN
   // pass in width and height too:
   ofRunApp(new ofApp());

   Singleton<OpenCLSingleton>::init();

   OpenCLSingleton *sharedContext = Singleton<OpenCLSingleton>::getPtr();

   //make sure the shared context is valid
   if(!sharedContext)
   {
      std::cerr << "No shared context could be initialized." << std::endl;
      exit(EXIT_FAILURE);
   }
   sharedContext->initSharedContext();

   std::cout << sharedContext->m_device.name() << std::endl;
   
}

I just to make sure that i got the opencl device to work with .

I think the opengl context is not created until the call

 ofRunApp(new ofApp());

Am i right?

What function do i have to call to print to the console ?

Thanks

you hardly ever put anything in main.cpp - you want to put all that in your ofApp

Thanks! @jvcleave

Eventually i managed to access the opencl device with the opengl interoperability.

Hi @jvcleave

One more thing…

As i have mentioned i have initialized a singleton instance of the class that contains the one and only opencl context that will be shared among several class files.

I want to de-initialize the singleton context when the program exits. I have moved the context initialization as you suggested in the last post as follows:

void ofApp::setup()
{
   Singleton<OpenCLSingleton>::init();
   
   OpenCLSingleton *sharedContext = Singleton<OpenCLSingleton>::getPtr();
   
   //make sure the shared context is valid
   if(!sharedContext)
   {
      std::cerr << "No shared context could be initialized." << std::endl;
      exit();
   }

   std::cout << "\nDevice Name: " << sharedContext->m_device.name() << std::endl;
} 

Now i want to remove the singleton instance when the application is closed with the following call:

Singleton<OpenCLSingleton>::deinit();

But i do not understand where(inside which member functions) to put it inside the ofApp.cpp class definitions.

ofApp has an exit() method available

add to ofApp.h

void exit();

and to ofApp.cpp

void ofApp::exit()
{
  Singleton<OpenCLSingleton>::deinit();
}
1 Like