33package com.jogamp.opencl.gl;
35import com.jogamp.common.nio.Buffers;
36import com.jogamp.opencl.CLBuffer;
37import com.jogamp.opencl.CLCommandQueue;
38import com.jogamp.opencl.CLKernel;
39import com.jogamp.opencl.CLProgram;
41import com.jogamp.opengl.GL2;
42import com.jogamp.opengl.GLException;
44import com.jogamp.opencl.CLDevice;
45import com.jogamp.newt.NewtFactory;
46import com.jogamp.newt.Window;
47import com.jogamp.newt.opengl.GLWindow;
48import com.jogamp.opencl.CLContext;
49import com.jogamp.opencl.CLMemory.Mem;
50import com.jogamp.opencl.CLPlatform;
51import com.jogamp.opencl.test.util.MiscUtils;
52import com.jogamp.opencl.test.util.UITestCase;
53import com.jogamp.opencl.util.CLDeviceFilters;
54import com.jogamp.opencl.util.CLPlatformFilters;
56import java.io.IOException;
57import java.nio.ByteBuffer;
58import java.nio.IntBuffer;
60import com.jogamp.opengl.GL;
61import com.jogamp.opengl.GLCapabilities;
62import com.jogamp.opengl.GLProfile;
63import com.jogamp.opengl.GLContext;
64import com.jogamp.opengl.fixedfunc.GLPointerFunc;
66import org.junit.FixMethodOrder;
68import org.junit.runners.MethodSorters;
70import static com.jogamp.opencl.util.CLPlatformFilters.*;
71import static org.junit.Assert.*;
72import static java.lang.System.*;
78@FixMethodOrder(MethodSorters.NAME_ASCENDING)
81 private static GLContext glcontext;
82 private static GLWindow glWindow;
83 private static Window window;
86 window = NewtFactory.createWindow(
new GLCapabilities(GLProfile.getDefault()));
87 assertNotNull(window);
89 window.setSize(640, 480);
91 glWindow = GLWindow.create(window);
93 assertNotNull(glWindow);
94 glWindow.setVisible(
true);
96 glcontext = glWindow.getContext();
103 private void deinitGL() throws GLException {
114 public
void createContextTest() {
116 out.println(
" - - - glcl; createContextTest - - - ");
120 @SuppressWarnings(
"unchecked")
123 @SuppressWarnings(
"unchecked")
128 out.println(
"Aborting test: no GLCL capable devices found.");
131 out.println(
"isGLMemorySharingSupported==true on: \n "+device);
136 assertNotNull(glcontext);
138 assertTrue(glcontext.isCurrent());
141 assertNotNull(context);
144 out.println(context);
160 public
void vboSharing() {
162 out.println(
" - - - glcl; vboSharing - - - ");
166 assertTrue(glcontext.isCurrent());
168 @SuppressWarnings(
"unchecked")
171 if(platform ==
null) {
172 out.println(
"test aborted");
176 @SuppressWarnings(
"unchecked")
179 out.println(theChosenOne);
184 out.println(context);
186 final GL2 gl = glcontext.getGL().getGL2();
188 final int[]
id =
new int[1];
189 gl.glGenBuffers(
id.length,
id, 0);
191 final IntBuffer glData = Buffers.newDirectIntBuffer(
new int[] {0,1,2,3,4,5,6,7,8});
195 gl.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
196 gl.glBindBuffer(GL.GL_ARRAY_BUFFER,
id[0]);
197 gl.glBufferData(GL.GL_ARRAY_BUFFER, glData.capacity()*4, glData, GL.GL_STATIC_DRAW);
198 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
199 gl.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
204 final IntBuffer clData = Buffers.newDirectIntBuffer(9);
208 assertEquals(glData.capacity()*4, clBuffer.
getCLSize());
218 while(clData.hasRemaining()) {
219 assertEquals(glData.get(), clData.get());
222 out.println(clBuffer);
226 gl.glDeleteBuffers(1,
id, 0);
236 public
void textureSharing() {
237 out.println(
" - - - glcl; textureSharing - - - ");
241 assertTrue(glcontext.isCurrent());
243 @SuppressWarnings(
"unchecked")
246 if(clplatforms.length == 0) {
247 out.println(
"no platform that supports OpenGL-OpenCL interoperability");
251 for(
final CLPlatform clplatform : clplatforms) {
253 @SuppressWarnings(
"unchecked")
257 for(
final CLDevice cldevice : cldevices) {
258 out.println(cldevice);
259 textureSharingInner(cldevice);
271 out.println(clglcontext);
273 final GL2 gl = glcontext.getGL().getGL2();
276 final int[]
id =
new int[1];
277 gl.glGenTextures(
id.length,
id, 0);
278 gl.glActiveTexture(GL.GL_TEXTURE0);
279 gl.glBindTexture (GL.GL_TEXTURE_2D,
id[0]);
280 final int texWidth = 2;
281 final int texHeight = 2;
282 gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, texWidth, texHeight, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
null );
283 gl.glBindTexture(GL.GL_TEXTURE_2D, 0);
287 final ByteBuffer bufferCL = Buffers.newDirectByteBuffer(texWidth*texHeight*4);
291 final String sourceCL =
"__kernel void writeTexture (__write_only image2d_t imageTex, unsigned w, unsigned h ) \n" +
293 " for(unsigned y=1; y<=h; ++y) { \n" +
294 " for(unsigned x=1; x<=w; ++x) { \n" +
295 " write_imagef(imageTex, (int2)(x-1,y-1), (float4)(((float)x)/((float)(4*w)), ((float)y)/((float)(4*h)), 0.0f, 1.0f)); \n" +
320 for(
int y = 1; y <= texHeight; y++) {
321 for(
int x = 1; x <= texWidth; x++) {
322 final byte bX = bufferCL.get();
323 final byte bY = bufferCL.get();
324 final byte bZero = bufferCL.get();
325 final byte bMinusOne = bufferCL.get();
326 final byte bXCheck = (byte)(((
float)x)/((float)(4*texWidth))*256);
327 final byte bYCheck = (byte)(((
float)y)/((
float)(4*texHeight))*256);
328 assertEquals(bXCheck, bX);
329 assertEquals(bYCheck, bY);
330 assertEquals(0, bZero);
331 assertEquals(-1, bMinusOne);
335 out.println(clTexture);
338 gl.glDeleteBuffers(1,
id, 0);
345 private void makeGLCurrent() {
349 glcontext.makeCurrent();
351 }
catch(
final RuntimeException ex) {
355 }
catch (
final InterruptedException ignore) { }
360 public static void main(
final String[] args)
throws IOException {
361 final String tstname =
CLGLTest.class.getName();
362 org.junit.runner.JUnitCore.
main(tstname);
OpenCL buffer object wrapping an optional NIO buffer.
The command queue is used to queue a set of operations for a specific CLDevice.
CLCommandQueue putAcquireGLObject(final CLGLObject glObject)
Calls {@native clEnqueueAcquireGLObjects}.
CLCommandQueue put1DRangeKernel(final CLKernel kernel, final long globalWorkOffset, final long globalWorkSize, final long localWorkSize)
Calls {@native clEnqueueNDRangeKernel}.
CLCommandQueue finish()
Calls {@native clFinish}.
CLCommandQueue putReleaseGLObject(final CLGLObject glObject)
Calls {@native clEnqueueReleaseGLObjects}.
CLCommandQueue putReadImage(final CLImage2d<?> readImage, final boolean blockingRead)
Calls {@native clEnqueueReadImage}.
CLCommandQueue putReadBuffer(final CLBuffer<?> readBuffer, final boolean blockingRead)
Calls {@native clEnqueueReadBuffer}.
CLContext is responsible for managing objects such as command-queues, memory, program and kernel obje...
CLProgram createProgram(final String src)
Creates a program from the given sources, the returned program is not build yet.
synchronized void release()
Releases this context and all resources.
This object represents an OpenCL device.
CLCommandQueue createCommandQueue()
CLPlatform getPlatform()
Returns the platform for this OpenCL object.
High level abstraction for an OpenCL Kernel.
CLKernel rewind()
Resets the argument index to 0.
CLKernel putArg(final CLMemory<?> value)
int getCLCapacity()
Returns the size in buffer elements of this memory object.
long getCLSize()
Returns the size of the allocated OpenCL memory in bytes.
Represents a OpenCL program executed on one or more CLDevices.
String getBuildLog()
Returns the build log of this program on all devices.
boolean isExecutable()
Returns true if the build status 'BUILD_SUCCESS' for at least one device of this program exists.
CLProgram build()
Builds this program for all devices associated with the context.
Map< CLDevice, Status > getBuildStatus()
Returns the build status enum of this program for each device as Map.
CLKernel createCLKernel(final String kernelName)
Creates a kernel with the specified kernel name.
Shared buffer between OpenGL and OpenCL contexts.
OpenCL Context supporting JOGL-JOCL interoperablity.
final CLGLBuffer<?> createFromGLBuffer(final int glBuffer, final long glBufferSize, final Mem... flags)
Creates a CLGLBuffer for memory sharing with the specified OpenGL buffer.
final CLGLTexture2d<?> createFromGLTexture2d(final int target, final int texture, final int mipmap, final Mem... flags)
static CLGLContext create(final GLContext glContext)
Creates a shared context on all available devices (CL_DEVICE_TYPE_ALL).
Test testing the JOGL - JOCL interoperability.
static void main(final String[] args)
void textureSharingInner(final CLDevice cldevice)
2D OpenCL image representing an 2D OpenGL texture.
static Filter< CLDevice > glSharing()
Accepts all devices which support OpenGL-OpenCL interoperability.
Memory settings for configuring CLMemory.
READ_ONLY
Enum representing CL_MEM_READ_ONLY.