Opencl and OpenGL work simultaneously

Source: Internet
Author: User

If you can use opencl, OpenGL, and OMAP for video processing, the computation speed will be greatly improved. This section briefly introduces how opencl and OpenGL work simultaneously.


Both opencl and OpenGL can be used for GPU operations, but the former is mainly used for general computing, while the latter is mainly used for image rendering. In some cases, we want to use opencl to calculate images and then display them on the display. If you directly call existing OpenGL APIs, you need to pass the computation results back to the host memory through the PCI-E bus, and then OpenGL returns the GPU-side memory through the PCI-E bus again. This results in additional copying and transmission costs. In fact, opencl's OpenGL extension function can be used to enable opencl and OpenGL to share the buffer, thus avoiding extra transmission. This kind of sharing can be understood as letting opencl and OpenGL share the same memory pointer.


Three steps are required to achieve this sharing:

1. opencl creates a context based on the OpenGL Context

// Create context attributes
Cl_context_properties properties [] = {
Cl_gl_context_khr, (cl_context_properties) wglgetcurrentcontext (), // obtain the OpenGL Context
Cl_wgl_hdc_khr, (cl_context_properties) wglgetcurrentdc (), // obtain OpenGL device information
Cl_context_platform, (cl_context_properties) platform, // obtain platform information
0 };
// Use the created property to create the context
CTX = clcreatecontext (properties, 1, & device, null, null, & ERR );

2. opencl creates memory Objects Based on OpenGL memory objects
// Taking buffer as an example, you need an OpenGL VBO to create a shared memory object.
Cl_mem clcreatefromglbuffer (cl_context context, cl_mem_flags flags,
Gluint vbo_desc, cl_int * ERR)

Note: OpenGL memory objects must be created first. To use images in opencl, texture in OpenGL is required.

3. Synchronization and access of shared memory objects
Obviously, opencl and OpenGL cannot access the same data object at the same time. They need to be locked and unlocked for synchronization.

// Lock
Int clenqueueacquireglobjects (cl_command_queue queue, cl_uint num_objects,
Const cl_mem * mem_objects, cl_uint num_events_in_wait_list,
Const cl_event * event_wait_list, cl_event * event)
// Run opencl code here after locking
// Unlock
Int clenqueuereleaseglobjects (cl_command_queue queue, cl_uint num_objects,
Const cl_mem * mem_objects, cl_uint num_events_in_wait_list,
Const cl_event * event_wait_list, cl_event * event)

After completing these three steps, OpenGL can directly use the shared memory object for rendering and other operations.

For more details, see Understanding opencl-OpenGL interoperability.

Opencl/OpenGL interoperation tutorial and example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.