سؤال

I intend to make Graphics calculation with OpenCL such as ray casting, ray marching and others. And I want to use OpenGL to display result of this calculations (pixel images). I use texture buffer attached to frame buffer. OpenCL writes the result into the texture and then I use glBlitFrameBuffer function to copy texture data to application window framebuffer. I met a CL/GL inter problem during the implementation of it. I wrote a simple example to show it. This example shows framebuffer object and texture object initialization, their conjunction, OpenCL buffer creation from GL texture buffer. At the end the main render loop is shown. It consists of texture writing with new data in each frame, framebuffer attachment and copying of this framebuffer.

Texture Initialization:

for (int i = 0; i < data.Length; i +=4) {
    data [i] = 255;
}
GL.BindTexture (TextureTarget.Texture2D, tboID [0]);
GL.TexImage2D<byte>(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, w, h, 0,
                    PixelFormat.Rgba, PixelType.UnsignedByte, data);
GL.BindTexture (TextureTarget.Texture2D, 0)

TBO+FBO Initialization:

 GL.BindFramebuffer (FramebufferTarget.FramebufferExt, fboID [0]);
 GL.FramebufferTexture2D (FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0,
                          TextureTarget.Texture2D, tboID [0], 0);
 GL.BindFramebuffer (FramebufferTarget.FramebufferExt, 0);

CL/GL Initialization:

bufferID = CL.CreateFromGLTexture2D (context, memInfo, textureTarget, mipLevel, glBufferObject, out errorCode);

Render Loop:

 for (int i = 0; i < data.Length; i += 4) {
     data [i] = tt;
 }
 tt++;
 GL.BindTexture (TextureTarget.Texture2D, tboID [0]);
 GL.TexImage2D<byte> (TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, w, h, 0,
                      PixelFormat.Rgba, PixelType.UnsignedByte, data);

 GL.BindTexture (TextureTarget.Texture2D, 0);
 GL.BindFramebuffer (FramebufferTarget.FramebufferExt, fboID [0]);
 GL.FramebufferTexture2D (FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0,
                          TextureTarget.Texture2D, tboID [0], 0);
 GL.BindFramebuffer (FramebufferTarget.FramebufferExt, 0);GL.BindFramebuffer (FramebufferTarget.ReadFramebuffer, fboID [0]);

 GL.ReadBuffer (ReadBufferMode.ColorAttachment0);
 GL.DrawBuffer (DrawBufferMode.Back);

 GL.BlitFramebuffer (0, 0, w, h, 0, 0, w, h, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Nearest);

 GL.BindFramebuffer (FramebufferTarget.ReadFramebuffer, 0);

At the first glance this code looks weird, but it completely shows my problem. CL does not work at all here. In this application OpenCL context is created and OpenCL buffer initialization is occured. The work of this should be simple. The color of screen is being changed from black to red. And It does not work in this way. The color does not change from the initial red (texture initialization). But it works normal when I comment the CL/GL Initialization (creation of CL buffer from GL texture). Why is it so? Why the behavior of the GL buffer is changed depending on CL attachments? How to fix it and make it works?

هل كانت مفيدة؟

المحلول

Finally we were able to run Itun' code on my system (Windows 7 / AMD Radeon HD 5870). Recall that Itun' code gradually changes color in texture from black to red by means of GL after activating GL/CL Interop on this texture.

The results are at least strange. On my system it works as intended. However, on Itun' system (Windows 7 / NVidia GeForce) the same code does not work at all and does not provide any exceptions or error codes. In addition, I would like to mention that CL works with this texture properly on both systems. Therefore, something is wrong with GL in this case.

We have no idea on what's going on - it could be either Itun' outdated GPU hardware or buggy NVidia drivers.

نصائح أخرى

EDIT 2:

Then you need to check why you are getting an InvalidImageFormatDescriptor. Check if the parameter order is okay and whether your image descriptor in the tbo is misleading (internal image structure - see OpenCL specification). From the spec:

CL_INVALID_IMAGE_FORMAT_DESCRIPTOR if the OpenGL texture internal format does not map to a supported OpenCL image format.


EDIT:

So I understand OpenCL functionality in OpenTK is provided by a separate project named Cloo. For ComputeImage2D their documentation states:

CreateFromGLTexture2D (ComputeContext context, ComputeMemoryFlags flags, int textureTarget, int mipLevel, int textureId)

Compared to yours:

CreateFromGLTexture2D (context, MemFlags.MemReadWrite, TextureTarget.Texture2D, ((uint[])tboID.Clone()) [0], 0);

Looking at that you have the mip level and the tbo in the wrong order. A false initialization might lead to some unknown behaviour.


Hard to tell what might be the issue from the code you're providing. Am looking at the Interop as well right now, just getting into it. First thing I would try is put a try/catch block around it and try to get a clue about any possible error code.

Have you verified the obvious: Is the cl_khr_gl_sharing extension available on your device?

Another guess, since you only provided the Texture/Image Initialisation of the actual OpenCl/OpenGL Interop within your sample code: Did you acquire the memory object?

cl_int clEnqueueAcquireGLObjects (cl_command_queue command_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)

The OpenCL 1.1 specifiction states:

The function cl_int clEnqueueAcquireGLObjects is used to acquire OpenCL memory objects that have been created from OpenGL objects. These objects need to be acquired before they can be used by any OpenCL commands queued to a command-queue. The OpenGL objects are acquired by the OpenCL context associated with command_queue and can therefore be used by all command-queues associated with the OpenCL context.

So the issue might be that the memory object hasn't been bound to a specific command queue.

Also, one is responsible for issuing a glFinish() to ensure all the initialization on the OpenGL side is done before the memory object is acquired.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top