2 Replies Latest reply on Jan 12, 2012 9:08 AM by Itun

    Interoperability, block texture access

    Itun

       

      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:

       

          [code]for (int i = 0; i < data.Length; i +=4) {

                      data = 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)[/code]

       

       

      TBO+FBO Initialization:

           [code]

           GL.BindFramebuffer (FramebufferTarget.FramebufferExt, fboID [0]);

           GL.FramebufferTexture2D (FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0,

                                    TextureTarget.Texture2D, tboID [0], 0);

           GL.BindFramebuffer (FramebufferTarget.FramebufferExt, 0);

           [/code]

       

      CL/GL Initialization:

      [code]

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

      [/code]

      Render Loop: 

      [code]

          for (int i = 0; i < data.Length; i += 4) {

              data = 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);

      [/code]

      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?



        • Interoperability, block texture access
          nou

          do you create GL texture after OpenCL context creation?

            • Interoperability, block texture access
              Itun

              Yes.

              protected virtual void OnRenderThreadStarted () { graphicsContext = new GraphicsContext (graphicsMode, windowInfo); // GL context creation graphicsContext.MakeCurrent (windowInfo); hDC = ((WinWindowInfo)windowInfo).DeviceContext; if (GraphicsContext.ShareContexts) { Interlocked.Increment (ref graphicsContextCount); if (!sharedContextInitialized) { sharedContextInitialized = true; graphicsContext.LoadAll (); } } else { graphicsContext.LoadAll (); } Init (); if (RenderThreadStarted != null) RenderThreadStarted (this, EventArgs.Empty); } protected override void Init () { CLPlatform[] platforms = CLObject.Platforms; CLDevice device = null; CLPlatform platform = null; foreach (CLPlatform currentPlatform in platforms) { string[] strs = currentPlatform.GetPlatformInfoStrs (); foreach (string currentStr in strs) { Console.WriteLine (currentStr); } CLDevice[] devices = currentPlatform.Devices; foreach (CLDevice currentDevice in devices) { string[] strs2 = currentDevice.GetDeviceInfoStrs (); foreach (string currentStr in strs2) { Console.WriteLine (currentStr); } if (currentDevice.GetDeviceAvailable () && device == null) { device = currentDevice; platform = currentPlatform; } } } kernel = new SimpleKernel (device, PrePacked.getWinGLSharingContextProperties (platform.PlatformID)); // CL context creation kernel.Init ("B:\\C#\\Projects\\Graphics3(OpenCLTest)\\Graphics3(OpenCLTest)\\CLScripts\\pixelsFill.cl", "fill"); kernel.FillLocalGroupSizeByMaxValue (2); kernel.GlobalWorkGroupSize [0] = w; kernel.GlobalWorkGroupSize [1] = h; kernel.BuildGlobalWorkGroupSizeCompact (); InitFBOWithTBO (); // Texture buffer object and frame buffer object initialization GL.Finish (); textureBuffer = kernel.Context.CreateFromGLTexture2D ( MemFlags.MemReadWrite , TextureTarget.Texture2D, tboID [0], 0); }