Drac32Drac's picture

CLGL Interop on platforms other than WGL?

First off, I want to that the creators of openTK and cloo for creating such wonderful products. I am having a blast creating a new game engine from scratch utilizing this feature and am amazed at the performance I am reaching with an opengl 3.2 context plus clgl interop. The project is just a hobbyist project right now though and more of a learning excercise than anything. The engine is only very lightly coupled to a GameWindow derived class that would be relatively simple to convert to glControl or other method if needed.

Now to my questions... does CLGL interop work at all for non windows platforms as of yet?
If it does, how would the CLContext be created for X11 or OSX? (either platform independant IntPtr from openTK or platform dependant p/invoke)


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Drac32Drac's picture

Ok, I've been doing a lot of digging around on this question and it looks like I may have figured out a working solution, but I have no means to test it right now on OSX or X11.

First off, we need access to different context commands for different platforms. So...

[DllImport("opengl32.dll")]
        extern static IntPtr wglGetCurrentDC();
 
        [DllImport("opengl32.dll")]
        extern static IntPtr glxGetCurrentDisplay();
 
        [DllImport("opengl32.dll")]
        extern static IntPtr CGLGetShareGroup(IntPtr context);

The ContextPropertyList should then be able to be populated by...

IGraphicsContextInternal ctx = (IGraphicsContextInternal)glCtx;
            IntPtr ctx_Ptr = ctx.Context.Handle;
 
            ComputeContextProperty p1, p2, p3;
            ComputeContextPropertyList Properties;
            ComputeContextProperty wglProperty, glxProperty, cglProperty;
            bool isWin = true, isMac = true, isX11 = true;
            try
            {
               wglProperty = new ComputeContextProperty(
                   ComputeContextPropertyName.CL_WGL_HDC_KHR, wglGetCurrentDC());
            }
            catch (EntryPointNotFoundException e) { isWin = false; }
 
            try
            {
               glxProperty = new ComputeContextProperty(
                   ComputeContextPropertyName.CL_GLX_DISPLAY_KHR, glxGetCurrentDisplay());
            }
            catch (EntryPointNotFoundException e) { isX11 = false; }
 
            try
            {
               cglProperty = new ComputeContextProperty(
                   ComputeContextPropertyName.CL_CGL_SHAREGROUP_KHR, CGLGetShareGroup(
                   ctx_Ptr));
            }
            catch (EntryPointNotFoundException e) { isMac = false; }
 
            if (isWin || isX11)
            {
                p1 = new ComputeContextProperty(ComputeContextPropertyName.CL_GL_CONTEXT_KHR,
                    ctx_Ptr);
                if (isWin) p2 = wglProperty;
                else p2 = glxProperty;
                p3 = new ComputeContextProperty(ComputeContextPropertyName.Platform,
                    ComputePlatform.Platforms[0].Handle.Value);
                List<ComputeContextProperty> props = new List<ComputeContextProperty>()
                    { p1, p2, p3 };
                Properties = new ComputeContextPropertyList(props);
            }
            else if (isMac)
            {
                List<ComputeContextProperty> props = new List<ComputeContextProperty>()
                    { cglProperty };
                Properties = new ComputeContextPropertyList(props);
            }
            else throw new NotSupportedException(
                "CLGL Context not supported on this operating system");

Once the property list is populated, it should be usable for clgl context creation on X11 or OSX the same way it would be for Windows.

I will admit a lack of experience on p/invoking in general, so I have no idea if the code listed is even sound on OSX or X11. If I am way off base with my solution, please let me know.

nythrix's picture

I haven't tried running a CL/GL interop outside windows. On the other hand, no one has submitted a bug or anything else regarding this area, so I assume Cloo can handle it.

Your code looks like it could work. I haven't tried it though.

Edit: Before accessing ComputePlatform.Platforms[0] check its existence. If no OpenCL is installed the list contains no items.

Drac32Drac's picture

Thanks for the reply, it will be a few days before I can test if it works though. I'll try to throw a test app together for it by then. I am very interested to know if it does indeed work as CLGL is such a powerful tool.

You are definately right about the Platforms[0] though, that one slipped my notice.