OpenCL

Started by dronus, February 10, 2010, 14:46:50

Previous topic - Next topic

dronus

Hi,

a feature request.. maybe LWJGL could provide OpenCL wrappers too? It seems available on many platforms now..

Wolftein

it will be nice, i'm agree too.

Matzon

I am not sure what gain games will have for this? - but at any rate, I think that a patch would be more likely than someone from the existing contributors to work on this

spasi

LWJGL isn't used for games only and even games have a lot to gain out of OpenCL. Getting it in LWJGL is in my plans, but unfortunately I won't have time to work on it for the next 3 months (at least). So unless someone else decides to contribute, it's going to take a while.

Momoko_Fan

OpenCL is useful in games, two uses I can come up with is software skinning and particle updates. OpenCL is the only way to fully take advantage of SSE instructions in java.
Then again, you can already do that, with this project: http://www.jocl.org/. Surely a LWJGL integrated solution would be useful. Perhaps the two projects can work together or something.

VeAr

Another argument for having OpenCL integrated with OpenGL in LWJGL, is that OpenCL can share data with OpenGL. OpenCL can work with OpenGL textures and buffers directly. For example it would be possible to generate geometry (vertex data) using OpenCL into a buffer, then to use that buffer for rendering the geometry using OpenGL. Performance wise this is optimal, because no copying is occurring, and it all can happen on the GPU.

Matzon

From a brief glance at the site - it appears to not be tied to any API as such. You should be able to use it with LWJGL too?
I do not expect that the two API's can work together, without jocl doing a license change (currently L/GPL).

princec

I don't think mixed licenses will hurt anyone's ability to use JOCL with LWJGL.
But at any rate: JOCL's there, it does the job, doesn't need any competition from an alternative implementation for now (especially considering its extremely niche interest).
An SSE/assembler library might be nice though...

Cas :)

dronus

Quote from: VeAr on February 20, 2010, 22:05:15
Another argument for having OpenCL integrated with OpenGL in LWJGL, is that OpenCL can share data with OpenGL. OpenCL can work with OpenGL textures and buffers directly.
That is proposed but not implemented afaik. Neither Nvidia nor ATI drivers seems support that, pretty poor.

basil

afaik thats right. you cannot share a texture properly yet. works ok with buffertype stuff like PBO's and map unmap buffer but thats not realy helping yet.

anyway. you can create an opencl image from a gl texture but you cannot (probably) write or read it ... still, I managed to create such texture and at least read out the order, format and dimensions correctly.

one thing that would be realy helpful with lwjgl is a bit support with opencl context creation. the way I did it atm is something like this:

cl_context = clCreateContextFromType ( contextProperties, CL_DEVICE_TYPE_GPU, null, null, null );


where the properties are :

[... setup platforms ...]

cl_context_properties contextProperties = new cl_context_properties ();
contextProperties.addProperty ( CL_CONTEXT_PLATFORM, platforms [ 0 ] );


but then :

Class< org.lwjgl.opengl.Display > display = org.lwjgl.opengl.Display.class;

Field display_impl = display.getDeclaredField ( "display_impl" );
Field ctx          = display.getDeclaredField ( "context" );

display_impl.setAccessible ( true );
ctx.setAccessible ( true );

Method getHdc    = display_impl.get ( display ).getClass ().getDeclaredMethod ( "getHdc" );
Method getHandle = ctx.get ( display ).getClass ().getDeclaredMethod ( "getHandle" );

getHdc.setAccessible ( true );
getHandle.setAccessible ( true );

long       getHdcValue = ( Long ) getHdc.invoke ( display_impl.get ( display ));
ByteBuffer glHandle    = ( ByteBuffer ) getHandle.invoke ( ctx.get ( display ));

glHandle.rewind ();

contextProperties.addProperty ( CL_GL_CONTEXT_KHR, glHandle.getInt ( 0 ));
contextProperties.addProperty ( CL_WGL_HDC_KHR, getHdcValue );


works for windows and JOCL. clCreateFromGLTexture2D does not return with CL_INVALID_CONTEXT anymore.

very ugly, you may see my point :)

Kai

Quotevery ugly, you may see my point
Yeah, reflection is such a nice tool!

I currently want to integrate OpenCL into my OpenGL render framework, too, for some offscreen heightfield/normalmap generation computations for generated terrains. I currently do it via shaders but at some point this is becoming a real mess. Especially the fact that coordinates need to remap from pixel centroid positions to [0.0, 1.0] again for the whole viewport and things like that. OpenCL integration into LWJGL here would be REALLY nice.
And by "OpenCL integration" I merely mean the possibility to get low-level information, such as the context and drawable handles, out of LWJGL. JOGL does a fine job with that, according to the sample applications on the JOCL http://www.jocl.org/samples/samples.html site!

So for the meantime, doing it with reflection is the way I will go.

basil

Quote from: Kai on April 26, 2010, 10:47:18
[...] JOGL does a fine job with that, according to the sample applications on the JOCL http://www.jocl.org/samples/samples.html site!

So for the meantime, doing it with reflection is the way I will go.
aye! the new version shows us a realy nice way to handle that stuff.

look up the initContextProperties function in JOCLSimpleGL3.java. CL_WGL_HDC_KHR, CL_GLX_DISPLAY_KHR, CL_CGL_SHAREGROUP_KHR .. all that stuff.

basil

just to keep it complete .. with lwjgl v.2.5 we need to do something like that. notice how the way to access the context class changed :

Class< org.lwjgl.opengl.Display > display = org.lwjgl.opengl.Display.class;

Field display_impl = display.getDeclaredField ( "display_impl" );

display_impl.setAccessible ( true );

Drawable d          = org.lwjgl.opengl.Display.getDrawable ();
Method   getcontext = d.getClass ().getSuperclass ().getDeclaredMethod ( "getContext" );

getcontext.setAccessible ( true );

Object ctx       = getcontext.invoke ( d );

Method getHdc    = display_impl.get ( display ).getClass ().getDeclaredMethod ( "getHdc" );
Method getHandle = ctx.getClass ().getDeclaredMethod ( "getHandle" );

getHdc.setAccessible ( true );
getHandle.setAccessible ( true );

long       getHdcValue = ( Long ) getHdc.invoke ( display_impl.get ( display ));

ByteBuffer glHandle    = ( ByteBuffer ) getHandle.invoke ( ctx );

glHandle.rewind ();

contextProperties.addProperty ( CL_GL_CONTEXT_KHR, glHandle.getInt ( 0 ));
contextProperties.addProperty ( CL_WGL_HDC_KHR, getHdcValue );


... so we can use

Drawable d          = org.lwjgl.opengl.Display.getDrawable ();
Method   getcontext = d.getClass ().getSuperclass ().getDeclaredMethod ( "getContext" );


.. some good news about opencl v.1.1 :

http://gpgpu.org/2010/06/18/opencl-1-1-released

maybe now, if new drivers follow, we can do proper integration into opengl. :)

spasi

Hey, just letting you know that I've started working on OpenCL support. It's been only a couple of days, so I'm still in the process of reading the specification and figuring out the best way to integrate this in LWJGL. I haven't used OpenCL before and I've no clue how long this is going to take, I'll try to give you an estimate early next week.

basil

no hurry :) .. good news tho'!