1 package com.mbien.opencl.demos.joglinterop;
3 import com.mbien.opencl.CLCommandQueue;
4 import com.mbien.opencl.CLContext;
5 import com.mbien.opencl.CLDevice;
6 import com.mbien.opencl.CLException;
7 import com.mbien.opencl.CLKernel;
8 import com.mbien.opencl.CLPlatform;
9 import com.mbien.opencl.CLProgram;
10 import com.sun.opengl.util.Animator;
11 import com.sun.opengl.util.BufferUtil;
12 import java.awt.event.WindowAdapter;
13 import java.awt.event.WindowEvent;
14 import java.io.IOException;
15 import java.nio.FloatBuffer;
16 import java.nio.IntBuffer;
17 import javax.media.opengl.DebugGL2;
18 import javax.media.opengl.GL2;
19 import javax.media.opengl.GLAutoDrawable;
20 import javax.media.opengl.GLCapabilities;
21 import javax.media.opengl.GLEventListener;
22 import javax.media.opengl.GLProfile;
23 import javax.media.opengl.awt.GLCanvas;
24 import javax.media.opengl.glu.gl2.GLUgl2;
25 import javax.swing.JFrame;
26 import javax.swing.SwingUtilities;
28 import static com.sun.opengl.util.BufferUtil.*;
31 * Sample for interoperability between JOCL and JOGL.
32 * @author Michael Bien
34 public class GLCLInteroperabilityDemo implements GLEventListener {
36 private final GLUgl2 glu = new GLUgl2();
38 private final int GRID_SIZE = 100;
43 private final FloatBuffer vb;
44 private final IntBuffer ib;
46 private final int[] buffer = new int[2];
47 private final int INDICES = 0;
48 private final int VERTICES = 1;
50 private final UserSceneInteraction usi;
52 private CLContext clContext;
53 private CLKernel kernel;
54 private CLCommandQueue commandQueue;
55 private final CLProgram program;
57 public GLCLInteroperabilityDemo() throws IOException {
59 this.usi = new UserSceneInteraction();
61 vb = newFloatBuffer(GRID_SIZE * GRID_SIZE * 4);
62 ib = newIntBuffer((GRID_SIZE - 1) * (GRID_SIZE - 1) * 2 * 3);
68 for (int h = 0; h < GRID_SIZE - 1; h++) {
69 for (int w = 0; w < GRID_SIZE - 1; w++) {
72 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6, w + (h) * (GRID_SIZE) );
73 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6 + 1, w + (h) * (GRID_SIZE) + 1 );
74 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6 + 2, w + (h + 1) * (GRID_SIZE) + 1);
77 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6 + 3, w + (h) * (GRID_SIZE) );
78 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6 + 4, w + (h + 1) * (GRID_SIZE) + 1);
79 ib.put(w * 6 + h * (GRID_SIZE - 1) * 6 + 5, w + (h + 1) * (GRID_SIZE) );
86 for (int w = 0; w < GRID_SIZE; w++) {
87 for (int h = GRID_SIZE; h > 0; h--) {
88 vb.put(w - GRID_SIZE / 2).put(h - GRID_SIZE / 2).put(0).put(1);
94 clContext = CLContext.create();
95 program = clContext.createProgram(getClass().getResourceAsStream("JoglInterop.cl"));
96 // System.out.println(program.getSource());
98 } catch (IOException ex) {
99 throw new CLException("can not handle exception", ex);
102 SwingUtilities.invokeLater(new Runnable() {
110 private void initUI() {
115 GLCapabilities config = new GLCapabilities(GLProfile.get(GLProfile.GL2));
116 config.setSampleBuffers(true);
117 config.setNumSamples(4);
119 GLCanvas canvas = new GLCanvas(config);
120 canvas.addGLEventListener(this);
123 JFrame frame = new JFrame("JOGL-JOCL Interoperability Example");
124 frame.addWindowListener(new WindowAdapter() {
126 public void windowClosed(WindowEvent e) {
132 frame.setSize(width, height);
134 frame.setVisible(true);
139 public void init(GLAutoDrawable drawable) {
141 // enable GL error checking using the composable pipeline
142 drawable.setGL(new DebugGL2(drawable.getGL().getGL2()));
144 GL2 gl = drawable.getGL().getGL2();
146 gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
148 gl.glGenBuffers(buffer.length, buffer, 0);
150 gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, buffer[INDICES]);
151 gl.glBufferData(GL2.GL_ARRAY_BUFFER, ib.capacity() * SIZEOF_FLOAT, ib, GL2.GL_STATIC_DRAW);
152 gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
154 gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, buffer[VERTICES]);
155 gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, vb.capacity() * SIZEOF_FLOAT, vb, GL2.GL_DYNAMIC_DRAW);
156 gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
159 // commandQueue = clContext.getMaxFlopsDevice().createCommandQueue();
161 // kernel = program.getCLKernel("sineWave");
162 // CLBuffer<FloatBuffer> clBuffer = clContext.createFromGLBuffer(vb, buffer[VERTICES], CLBuffer.Mem.WRITE_ONLY);
163 // kernel.setArg(0, clBuffer);
164 // kernel.setArg(1, GRID_SIZE);
165 // kernel.setArg(2, GRID_SIZE);
168 pushPerspectiveView(gl);
170 // start rendering thread
171 Animator animator = new Animator(drawable);
175 public void display(GLAutoDrawable drawable) {
177 GL2 gl = drawable.getGL().getGL2();
178 gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
183 gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
185 gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, buffer[VERTICES]);
186 gl.glVertexPointer(4, GL2.GL_FLOAT, 0, 0);
188 gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, buffer[INDICES]);
189 gl.glDrawElements(GL2.GL_TRIANGLES, ib.capacity(), GL2.GL_UNSIGNED_INT, 0);
191 gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
193 gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
197 private void pushPerspectiveView(GL2 gl) {
199 gl.glMatrixMode(GL2.GL_PROJECTION);
204 glu.gluPerspective(60, width / (float)height, 1, 1000);
205 gl.glMatrixMode(GL2.GL_MODELVIEW);
212 private void popView(GL2 gl) {
214 gl.glMatrixMode(GL2.GL_PROJECTION);
217 gl.glMatrixMode(GL2.GL_MODELVIEW);
223 public void reshape(GLAutoDrawable drawable, int arg1, int arg2, int width, int height) {
225 this.height = height;
226 GL2 gl = drawable.getGL().getGL2();
228 pushPerspectiveView(gl);
231 public void dispose(GLAutoDrawable drawable) { }
233 private void deinit() {
238 public static void main(String[] args) throws IOException {
239 new GLCLInteroperabilityDemo();