This project is read-only.

OUT_OF_RESOURCES error when using OpenGL context and rendering openCL to a texture

Aug 2, 2013 at 4:50 AM
Edited Aug 2, 2013 at 4:52 AM
I've got the "hard" bit out the way, of creating an OpenGL-shared context with OpenCLNet, using a new CreateGLContext function (Windows only for now), but the final act of releasing the texture is crashing with an OUT_OF_RESOURCES error.

OpenGL is initialised first, and then OpenCL is called.
        [DllImport("opengl32.dll")]
        extern static IntPtr wglGetCurrentDC();

        public void CreateGLContext(int platformNumber, DeviceType deviceType)
        {
            if (!OpenCLIsAvailable)
                throw new OpenCLNotAvailableException();

            IntPtr curDC = wglGetCurrentDC();
            OpenTK.Graphics.IGraphicsContextInternal ctx = (OpenTK.Graphics.IGraphicsContextInternal)OpenTK.Graphics.GraphicsContext.CurrentContext;
            IntPtr raw_context_handle = ctx.Context.Handle;

            Platform = OpenCL.GetPlatform(platformNumber);
            var devices = from d in Platform.QueryDevices(deviceType)
                          where ((RequireImageSupport && d.ImageSupport == true) || !RequireImageSupport) && d.HasExtensions(RequiredExtensions.ToArray<string>())
                          select d;
            IntPtr[] properties = new IntPtr[]
            {                
                (IntPtr)ContextProperties.GL_CONTEXT_KHR, 
                (IntPtr)raw_context_handle,
                //IntPtr.Zero,
                (IntPtr)ContextProperties.WGL_HDC_KHR,
                (IntPtr)curDC,
                //IntPtr.Zero,
                (IntPtr)ContextProperties.PLATFORM, Platform,
                IntPtr.Zero,
            };

            if (devices.Count() == 0)
                throw new OpenCLException("CreateDefaultContext: No OpenCL devices found that matched filter criteria.");

            CreateContext(Platform, properties, devices);
        }
Next I'm creating a Texture (OpenCL shared context must be activated prior to creating any texture, or the texture won't be shared):
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba32f, Width, Height, 0,
       (OpenTK.Graphics.OpenGL.PixelFormat)PixelInternalFormat.Rgba, PixelType.Float, IntPtr.Zero);
Then I'm enabling the texture for OpenCL use:
GL.glFinish();   // OpenGL must have finished processing before OpenCL can share the texture.
 Mem outputBuffer = OCLMan.Context.CreateFromGLTexture2D(MemFlags.WRITE_ONLY, (int)EnableCap.Texture2D, 0, output.ID);
            
int deviceIndex = 0;            // 

// QAcquire the texture before using.
OCLMan.CQ[deviceIndex].EnqueueAcquireGLObjects(1, new Mem[] { outputBuffer });
Then I set the parameters, and then execute:
                    RayTraceKernel.SetArg(0, Width); // int
                    RayTraceKernel.SetArg(1, Height); // int                   
                    RayTraceKernel.SetArg(2, Scene.cameraPosition); // float4
                    RayTraceKernel.SetArg(3, Scene.cameraForward); // float4
                    RayTraceKernel.SetArg(4, right);              // float4
                    RayTraceKernel.SetArg(5, up);                 // float4
                    RayTraceKernel.SetArg(6, _skyTexture);     // image2d_t
                    RayTraceKernel.SetArg(7, _woodTexture); // image2d_t
                    RayTraceKernel.SetArg(8, _sampler);       // sampler
                    RayTraceKernel.SetArg(9, outputBuffer); //  image2d_t

                    OCLMan.CQ[deviceIndex].EnqueueNDRangeKernel(RayTraceKernel, 2, rayTracingGlobalOffset, rayTracingGlobalWorkSize, null);


            OCLMan.CQ[deviceIndex].EnqueueReleaseGLObjects(1, new Mem[] { outputBuffer });            
            OCLMan.CQ[deviceIndex].Finish();
The error occurs with the EnqueueReleaseGLObjects, which simply says OUT_OF_RESOURCES.

Here's the main kernel function in the program:

kernel void RayTrace (int width, int height, float4 cameraPosition, float4 cameraForward, float4 cameraRight, float4 cameraUp, read_only image2d_t skyTexture, read_only image2d_t woodTexture, sampler_t sampler, global write_only image2d_t gloutput)
{
    size_t x = get_global_id(0); 
    size_t y = get_global_id(1);    
    
    float4 rayDirection = getRayDirection(width, height, x, y, cameraForward, cameraRight, cameraUp);
    float4 pixelColor = traceRay(cameraPosition, rayDirection, skyTexture, woodTexture, sampler);           

    write_imagef(gloutput, (int2)(x, y), pixelColor);
}
This all works fine if the memory object isn't a texture, but is a host BitmapData for example, so the raytracing bit is fine, plus it seems the Kernel program doesn't matter at all, I vaourpised it down to a blank function that does nothing at all, and I get the same error.

So it kinda feels like the texture isn't created in a way that OpenCL expects. The texture IS being changed post-OpenCL, but to some corrupted screen copy, rather than the result of the OpenCL call.

Any more experienced people got any insight into this?