If you're using an NVIDIA GPU, install the CUDA Toolkit.
sudo apt install ocl-icd-opencl-dev opencl-headers sudo apt install opencl-c-headers opencl-clhpp-headers
OpenCL kernels are compiled at runtime. All you have to do is link OpenCL when compiling your program and include your kernels in your program. For
gcc just add flag
While you can use the C bindings in your C++ application, Khronos also provides a set of C++ bindings in
CL/cl2.hpp) which are much easier to use alongside std containers such as
When using C++ bindings, you also do not need to worry about releasing buffers since these are reference-counted.
OpenCL 1.2 Scalar Data Types
While all OpenCL devices support single-precision floats, not all support double-precision doubles.
float3 my_vec = (float3)(1.0);
where its elements are accessed using x,y,z as
To convert between vector types, use
- 3-component data types are aligned to 4 components. I.e. an array of
uchar3with 4 elements will be equivalent to an array of
uchar4with 4 elements.
Setting up OpenCL/OpenGL interop is fairly complicated and very hard to debug.
You will also need to manage synchronizing OpenGL/OpenCL so they do not access the same memory at the same time.
If you can, just use OpenGL compute shaders rather than OpenCL to simplify your life.
In C++, you can use
cl::ImageGL to access textures in OpenGL.
cl::Buffer are not the same thing. Interchanging them will result in
CL_INVALID_MEM_OBJECT errors or similar.
I recommend writing to a separate buffer and copying to images.
See clCreateFromGLTexture to get a list of compatible pixel formats.
If in doubt, use
GL_RGBA8 which is the most likely format to be supported.