2

I'm creating an OpenFX plugin to do some image processing for a VR system.

As both our existing processing code, and the host the plugin is intended for, are based on OpenCL, the host developers have given me an extension to OFX to share the OpenCL environment.

However - our code uses the C++ API (cl::Context), and the extension they've provided gives me a pointer to a C cl_context.

Is there any way for me to create a cl::Context from a cl_context, without taking ownership of the latter? (and the same for the cl::CommandQueue from cl_command_queue). The API docs online suggest that both the copy constructor and assignment operator take ownership.

Nick C
  • 319
  • 8
  • 23

1 Answers1

3

Fortunately you can inspect the OpenCL C++ header to see exactly what it does (at the bottom it just makes C API calls), and you can step into the code to verify it. When working with both C and C++ API, you must be careful about the OpenCL object retain/release counts, in particular if the C++ constructor doesn't retain but the destructor does release, then you have a problem (because you'd be releasing the host's context out from under them). However, it is easily fixed because you can just call retain yourself on the object after constructing to balance things out. I've mixed C API and C++ API in OpenCL and it was the retain/release philosophy of the C++ header that got me too, so I feel your pain. Curious, is the OpenFX host Resolve, Vegas, Catalyst, or something else? There's a unified OpenCL extension on the way for OpenFX.

Dithermaster
  • 6,223
  • 1
  • 12
  • 20
  • Hey you didn't answer the question. So how exactly do I do it, it cannot be done? For example Caffe uses the old API, but if we want to get a cl_context from caffe and use it in our API which uses cl::Context, its not possible? Retain is protected – raaj Jan 22 '18 at 18:26
  • You can create a Context object from your `cl_context` handle (there's a constructor for that, enabled by __CL_EXPLICIT_CONSTRUCTORS), or you can use the assignment operator to assign your `cl_context` handle to a Context object. The Context object now "owns" that context and will release it when it goes out of scope. If you still want to use your cl_context handle, then after creating or assigning the Context object, also call `clRetainContext` on the handle (which offsets the release done in the object's destructor). It's all the same `cl_content` regardless of C or C++ API. – Dithermaster Jan 22 '18 at 22:09