JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLGLTest.java
Go to the documentation of this file.
1/*
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29/*
30 * Created on Saturday, April 24 2010 02:58 AM
31 */
32
33package com.jogamp.opencl.gl;
34
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;
40
41import com.jogamp.opengl.GL2;
42import com.jogamp.opengl.GLException;
43
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;
55
56import java.io.IOException;
57import java.nio.ByteBuffer;
58import java.nio.IntBuffer;
59
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;
65
66import org.junit.FixMethodOrder;
67import org.junit.Test;
68import org.junit.runners.MethodSorters;
69
70import static com.jogamp.opencl.util.CLPlatformFilters.*;
71import static org.junit.Assert.*;
72import static java.lang.System.*;
73
74/**
75 * Test testing the JOGL - JOCL interoperability.
76 * @author Michael Bien, et.al
77 */
78@FixMethodOrder(MethodSorters.NAME_ASCENDING)
79public class CLGLTest extends UITestCase {
80
81 private static GLContext glcontext;
82 private static GLWindow glWindow;
83 private static Window window;
84
85 public static void initGL() {
86 window = NewtFactory.createWindow(new GLCapabilities(GLProfile.getDefault()));
87 assertNotNull(window);
88
89 window.setSize(640, 480);
90
91 glWindow = GLWindow.create(window);
92
93 assertNotNull(glWindow);
94 glWindow.setVisible(true);
95
96 glcontext = glWindow.getContext();
97// glcontext.makeCurrent();
98// out.println(" - - - - glcontext - - - - ");
99// out.println(glcontext);
100// out.println(" - - - - - - - - - - - - - ");
101 }
102
103 private void deinitGL() throws GLException {
104 glcontext.release();
105 glWindow.destroy();
106 window.destroy();
107
108 glcontext = null;
109 glWindow = null;
110 window = null;
111 }
112
113 @Test(timeout=15000)
114 public void createContextTest() {
115
116 out.println(" - - - glcl; createContextTest - - - ");
117
118 initGL();
119
120 @SuppressWarnings("unchecked")
121 final
123 @SuppressWarnings("unchecked")
124 final
126
127 if(device == null) {
128 out.println("Aborting test: no GLCL capable devices found.");
129 return;
130 }else{
131 out.println("isGLMemorySharingSupported==true on: \n "+device);
132 }
133
134 out.println(device.getPlatform());
135
136 assertNotNull(glcontext);
137 makeGLCurrent();
138 assertTrue(glcontext.isCurrent());
139
140 final CLContext context = CLGLContext.create(glcontext, device);
141 assertNotNull(context);
142
143 try{
144 out.println(context);
145 /*
146 CLDevice currentDevice = context.getCurrentGLCLDevice();
147 assertNotNull(currentDevice);
148 out.println(currentDevice);
149 */
150 }finally{
151 // destroy cl context, gl context still current
152 context.release();
153
154 deinitGL();
155 }
156
157 }
158
159 @Test(timeout=15000)
160 public void vboSharing() {
161
162 out.println(" - - - glcl; vboSharing - - - ");
163
164 initGL();
165 makeGLCurrent();
166 assertTrue(glcontext.isCurrent());
167
168 @SuppressWarnings("unchecked")
169 final
170 CLPlatform platform = CLPlatform.getDefault(glSharing(glcontext));
171 if(platform == null) {
172 out.println("test aborted");
173 return;
174 }
175
176 @SuppressWarnings("unchecked")
177 final
178 CLDevice theChosenOne = platform.getMaxFlopsDevice(CLDeviceFilters.glSharing());
179 out.println(theChosenOne);
180
181 final CLGLContext context = CLGLContext.create(glcontext, theChosenOne);
182
183 try{
184 out.println(context);
185
186 final GL2 gl = glcontext.getGL().getGL2();
187
188 final int[] id = new int[1];
189 gl.glGenBuffers(id.length, id, 0);
190
191 final IntBuffer glData = Buffers.newDirectIntBuffer(new int[] {0,1,2,3,4,5,6,7,8});
192 glData.rewind();
193
194 // create and write GL buffer
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);
200 gl.glFinish();
201
202
203 // create CLGL buffer
204 final IntBuffer clData = Buffers.newDirectIntBuffer(9);
205 final CLGLBuffer<IntBuffer> clBuffer = context.createFromGLBuffer(clData, id[0], glData.capacity()*4, Mem.READ_ONLY);
206
207 assertEquals(glData.capacity(), clBuffer.getCLCapacity());
208 assertEquals(glData.capacity()*4, clBuffer.getCLSize());
209
210
211 final CLCommandQueue queue = theChosenOne.createCommandQueue();
212
213 // read gl buffer into cl nio buffer
214 queue.putAcquireGLObject(clBuffer)
215 .putReadBuffer(clBuffer, true)
216 .putReleaseGLObject(clBuffer);
217
218 while(clData.hasRemaining()) {
219 assertEquals(glData.get(), clData.get());
220 }
221
222 out.println(clBuffer);
223
224 clBuffer.release();
225
226 gl.glDeleteBuffers(1, id, 0);
227
228 }finally{
229 context.release();
230 deinitGL();
231 }
232
233 }
234
235 @Test(timeout=15000)
236 public void textureSharing() {
237 out.println(" - - - glcl; textureSharing - - - ");
238
239 initGL();
240 makeGLCurrent();
241 assertTrue(glcontext.isCurrent());
242
243 @SuppressWarnings("unchecked")
244 final
245 CLPlatform [] clplatforms = CLPlatform.listCLPlatforms(glSharing(glcontext));
246 if(clplatforms.length == 0) {
247 out.println("no platform that supports OpenGL-OpenCL interoperability");
248 return;
249 }
250
251 for(final CLPlatform clplatform : clplatforms) {
252
253 @SuppressWarnings("unchecked")
254 final
255 CLDevice [] cldevices = clplatform.listCLDevices(CLDeviceFilters.glSharing());
256
257 for(final CLDevice cldevice : cldevices) {
258 out.println(cldevice);
259 textureSharingInner(cldevice);
260 }
261 }
262
263 deinitGL();
264 }
265
266 public void textureSharingInner(final CLDevice cldevice) {
267
268 final CLGLContext clglcontext = CLGLContext.create(glcontext, cldevice);
269
270 try {
271 out.println(clglcontext);
272
273 final GL2 gl = glcontext.getGL().getGL2();
274
275 // create and write GL texture
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);
284 gl.glFinish();
285
286 // create CLGL buffer
287 final ByteBuffer bufferCL = Buffers.newDirectByteBuffer(texWidth*texHeight*4);
288 final CLGLTexture2d<ByteBuffer> clTexture = clglcontext.createFromGLTexture2d(bufferCL, GL.GL_TEXTURE_2D, id[0], 0, CLBuffer.Mem.WRITE_ONLY);
289
290 // set texel values to a formula that can be read back and verified
291 final String sourceCL = "__kernel void writeTexture (__write_only image2d_t imageTex, unsigned w, unsigned h ) \n" +
292 "{ \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" +
296 " } \n" +
297 " } \n" +
298 "}";
299 final CLProgram program = clglcontext.createProgram(sourceCL);
300 program.build();
301 System.out.println(program.getBuildStatus());
302 System.out.println(program.getBuildLog());
303 assertTrue(program.isExecutable());
304
305 final CLKernel clkernel = program.createCLKernel("writeTexture")
306 .putArg(clTexture)
307 .putArg(texWidth)
308 .putArg(texHeight)
309 .rewind();
310
311 final CLCommandQueue queue = cldevice.createCommandQueue();
312
313 // write gl texture with cl kernel, then read it to host buffer
314 queue.putAcquireGLObject(clTexture)
315 .put1DRangeKernel(clkernel, 0, 1, 1)
316 .putReadImage(clTexture, true)
317 .putReleaseGLObject(clTexture)
318 .finish();
319
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);
332 }
333 }
334
335 out.println(clTexture);
336
337 clTexture.release();
338 gl.glDeleteBuffers(1, id, 0);
339 }
340 finally {
341 clglcontext.release();
342 }
343 }
344
345 private void makeGLCurrent() {
346 // we are patient...
347 while(true) {
348 try{
349 glcontext.makeCurrent();
350 break;
351 }catch(final RuntimeException ex) {
352 try {
353 Thread.sleep(200);
354 // I don't give up yet!
355 } catch (final InterruptedException ignore) { }
356 }
357 }
358 }
359
360 public static void main(final String[] args) throws IOException {
361 final String tstname = CLGLTest.class.getName();
362 org.junit.runner.JUnitCore.main(tstname);
363 }
364
365}
OpenCL buffer object wrapping an optional NIO buffer.
Definition: CLBuffer.java:46
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...
Definition: CLContext.java:79
CLProgram createProgram(final String src)
Creates a program from the given sources, the returned program is not build yet.
Definition: CLContext.java:243
synchronized void release()
Releases this context and all resources.
Definition: CLContext.java:493
This object represents an OpenCL device.
Definition: CLDevice.java:53
CLCommandQueue createCommandQueue()
Definition: CLDevice.java:72
CLPlatform getPlatform()
Returns the platform for this OpenCL object.
Definition: CLDevice.java:102
High level abstraction for an OpenCL Kernel.
Definition: CLKernel.java:53
CLKernel rewind()
Resets the argument index to 0.
Definition: CLKernel.java:158
CLKernel putArg(final CLMemory<?> value)
Definition: CLKernel.java:107
int getCLCapacity()
Returns the size in buffer elements of this memory object.
Definition: CLMemory.java:171
long getCLSize()
Returns the size of the allocated OpenCL memory in bytes.
Definition: CLMemory.java:164
CLPlatfrorm representing a OpenCL implementation (e.g.
Definition: CLPlatform.java:99
static CLPlatform[] listCLPlatforms()
Lists all available OpenCL implementations.
CLDevice getMaxFlopsDevice()
Returns the device with maximal FLOPS from this platform.
static CLPlatform getDefault()
Returns the default OpenCL platform or null when no platform found.
Represents a OpenCL program executed on one or more CLDevices.
Definition: CLProgram.java:64
String getBuildLog()
Returns the build log of this program on all devices.
Definition: CLProgram.java:538
boolean isExecutable()
Returns true if the build status 'BUILD_SUCCESS' for at least one device of this program exists.
Definition: CLProgram.java:570
CLProgram build()
Builds this program for all devices associated with the context.
Definition: CLProgram.java:226
Map< CLDevice, Status > getBuildStatus()
Returns the build status enum of this program for each device as Map.
Definition: CLProgram.java:558
CLKernel createCLKernel(final String kernelName)
Creates a kernel with the specified kernel name.
Definition: CLProgram.java:410
Shared buffer between OpenGL and OpenCL contexts.
Definition: CLGLBuffer.java:45
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.
Definition: CLGLTest.java:79
static void main(final String[] args)
Definition: CLGLTest.java:360
void textureSharingInner(final CLDevice cldevice)
Definition: CLGLTest.java:266
2D OpenCL image representing an 2D OpenGL texture.
static Filter< CLDevice > glSharing()
Accepts all devices which support OpenGL-OpenCL interoperability.
static Filter< CLPlatform > glSharing()
Accepts all platforms containing at least one devices of which supports OpenGL-OpenCL interoperabilit...
Memory settings for configuring CLMemory.
Definition: CLMemory.java:289
READ_ONLY
Enum representing CL_MEM_READ_ONLY.
Definition: CLMemory.java:313