Can OpenCL trigger renders in OpenGL?


I have a complex iterative optimisation problem involving several steps. One of those steps involves taking the current iteration, and using values from it to render dozens of versions of a simple scene, then taking those renderings and performing some calculations on the pixels to create values which are then fed back into the optimisation algorithm. These steps are iterated many times.

This pseudo code illustrates roughly what I have in mind

fill array_A from CPU
Loop 100 times
    perform step 1 on array_A
    use values in array_A as angles to render 100 images of a cube with different rotations
    sum pixels in each image, and write results to array_B
    add array_B to array_A
Transfer array_A back to CPU

One way to do this would be to use the CPU to take information from OpenCL, use it to render all of these scenes, then Enqueue the kernels to process the renderings. However, a faster way must surely be to keep everything on the GPU, without involving the CPU.

Is this possible?

You can use OpenGL/OpenCL interop API to convert CL buffer into GL buffer, use MultiDrawIndirect for rendering and copy the results into cl_image you previously converted into OpenGL texture the same way.

Although if you only ever need to render cubes it might be simpler to literally rasterize your cube inside an OpenCL kernel.

float x = float(get_global_id(0))/get_global_size(0);
float y = float(get_global_id(1))/get_global_size(1);
float4 color = (0,0,0,0);
float depth = infinity

foreach i in triangles:
  processed_triangle = vertex_shader(i) //Can be a kernel of it's own
  if ((x,y) is inside of processed_triangle) and depth > depth(processed_triangle, x,y):
    depth = depth(processed_triangle, x,y);
    color = pixel_shader(processed_triangle, x,y);
rendered_image[x,y] = color 

A proper tearless implementation of “(x,y) is inside of processed_triangle” might prove challenging though.