Jogamp
GLProfile.initSingleton(false) for webstart compatibility.
[jocl-demos.git] / src / com / jogamp / opencl / demos / joglinterop / GLCLInteroperabilityDemo.java
1 package com.jogamp.opencl.demos.joglinterop;
2
3 import com.jogamp.opencl.CLCommandQueue;
4 import com.jogamp.opencl.CLDevice;
5 import com.jogamp.opencl.gl.CLGLBuffer;
6 import com.jogamp.opencl.gl.CLGLContext;
7 import com.jogamp.opencl.CLKernel;
8 import com.jogamp.opencl.CLPlatform;
9 import com.jogamp.opencl.CLProgram;
10 import com.jogamp.opengl.util.Animator;
11 import java.io.IOException;
12 import javax.media.opengl.DebugGL2;
13 import javax.media.opengl.GL2;
14 import javax.media.opengl.GLAutoDrawable;
15 import javax.media.opengl.GLCapabilities;
16 import javax.media.opengl.GLEventListener;
17 import javax.media.opengl.GLProfile;
18 import javax.media.opengl.awt.GLCanvas;
19 import javax.media.opengl.glu.gl2.GLUgl2;
20 import javax.swing.JFrame;
21 import javax.swing.SwingUtilities;
22
23 import static com.jogamp.common.nio.Buffers.*;
24
25 /**
26  * JOCL - JOGL interoperability example.
27  * @author Michael Bien
28  */
29 public class GLCLInteroperabilityDemo implements GLEventListener {
30
31     private final GLUgl2 glu = new GLUgl2();
32
33     private final int MESH_SIZE = 256;
34
35     private int width;
36     private int height;
37
38 //    private final FloatBuffer vb;
39 //    private final IntBuffer ib;
40
41     private final int[] glObjects = new int[2];
42     private final int VERTICES = 0;
43 //    private final int INDICES  = 1;
44
45     private final UserSceneInteraction usi;
46
47     private CLGLContext clContext;
48     private CLKernel kernel;
49     private CLCommandQueue commandQueue;
50     private CLGLBuffer<?> clBuffer;
51
52     private float step = 0;
53
54     public GLCLInteroperabilityDemo() {
55
56         this.usi = new UserSceneInteraction();
57
58         // create direct memory buffers
59 //        vb = newFloatBuffer(MESH_SIZE * MESH_SIZE * 4);
60 //        ib = newIntBuffer((MESH_SIZE - 1) * (MESH_SIZE - 1) * 2 * 3);
61 //
62 //        // build indices
63 //        //    0---3
64 //        //    | \ |
65 //        //    1---2
66 //        for (int h = 0; h < MESH_SIZE - 1; h++) {
67 //            for (int w = 0; w < MESH_SIZE - 1; w++) {
68 //
69 //                // 0 - 3 - 2
70 //                ib.put(w * 6 + h * (MESH_SIZE - 1) * 6,      w + (h    ) * (MESH_SIZE)    );
71 //                ib.put(w * 6 + h * (MESH_SIZE - 1) * 6 + 1,  w + (h    ) * (MESH_SIZE) + 1);
72 //                ib.put(w * 6 + h * (MESH_SIZE - 1) * 6 + 2,  w + (h + 1) * (MESH_SIZE) + 1);
73 //
74 //                // 0 - 2 - 1
75 //                ib.put(w * 6 + h * (MESH_SIZE - 1) * 6 + 3,  w + (h    ) * (MESH_SIZE)    );
76 //                ib.put(w * 6 + h * (MESH_SIZE - 1) * 6 + 4,  w + (h + 1) * (MESH_SIZE) + 1);
77 //                ib.put(w * 6 + h * (MESH_SIZE - 1) * 6 + 5,  w + (h + 1) * (MESH_SIZE)    );
78 //
79 //            }
80 //        }
81 //        ib.rewind();
82
83         SwingUtilities.invokeLater(new Runnable() {
84             public void run() {
85                 initUI();
86             }
87         });
88
89     }
90
91     private void initUI() {
92
93         this.width  = 600;
94         this.height = 400;
95
96         GLCapabilities config = new GLCapabilities(GLProfile.get(GLProfile.GL2));
97         config.setSampleBuffers(true);
98         config.setNumSamples(4);
99
100         GLCanvas canvas = new GLCanvas(config);
101         canvas.addGLEventListener(this);
102         usi.init(canvas);
103
104         JFrame frame = new JFrame("JOGL-JOCL Interoperability Example");
105         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
106         frame.add(canvas);
107         frame.setSize(width, height);
108
109         frame.setVisible(true);
110
111     }
112
113
114     public void init(GLAutoDrawable drawable) {
115
116         if(clContext == null) {
117
118             // find gl compatible device
119             CLDevice[] devices = CLPlatform.getDefault().listCLDevices();
120             CLDevice device = null;
121             for (CLDevice d : devices) {
122                 if(d.isGLMemorySharingSupported()) {
123                     device = d;
124                     break;
125                 }
126             }
127             if(null==device) {
128                 throw new RuntimeException("couldn't find any CL/GL memory sharing devices ..");
129             }
130             // create OpenCL context before creating any OpenGL objects
131             // you want to share with OpenCL (AMD driver requirement)
132             clContext = CLGLContext.create(drawable.getContext(), device);
133
134             // enable GL error checking using the composable pipeline
135             drawable.setGL(new DebugGL2(drawable.getGL().getGL2()));
136
137             // OpenGL initialization
138             GL2 gl = drawable.getGL().getGL2();
139
140             gl.setSwapInterval(1);
141
142             gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
143
144             gl.glGenBuffers(glObjects.length, glObjects, 0);
145
146     //        gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, glObjects[INDICES]);
147     //        gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, ib.capacity() * SIZEOF_INT, ib, GL2.GL_STATIC_DRAW);
148     //        gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
149
150             gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
151                 gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, glObjects[VERTICES]);
152                 gl.glBufferData(GL2.GL_ARRAY_BUFFER, MESH_SIZE * MESH_SIZE * 4 * SIZEOF_FLOAT, null, GL2.GL_DYNAMIC_DRAW);
153                 gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
154             gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
155
156             pushPerspectiveView(gl);
157             gl.glFinish();
158
159             // init OpenCL
160             initCL();
161
162             // start rendering thread
163             Animator animator = new Animator(drawable);
164             animator.start();
165
166         }
167     }
168
169     private void initCL() {
170
171         CLProgram program;
172         try {
173             program = clContext.createProgram(getClass().getResourceAsStream("JoglInterop.cl"));
174             program.build();
175             System.out.println(program.getBuildStatus());
176             System.out.println(program.isExecutable());
177             System.out.println(program.getBuildLog());
178         } catch (IOException ex) {
179             throw new RuntimeException("can not handle exception", ex);
180         }
181
182         commandQueue = clContext.getMaxFlopsDevice().createCommandQueue();
183
184         clBuffer = clContext.createFromGLBuffer(glObjects[VERTICES], CLGLBuffer.Mem.WRITE_ONLY);
185
186         System.out.println("cl buffer type: " + clBuffer.getGLObjectType());
187         System.out.println("shared with gl buffer: " + clBuffer.getGLObjectID());
188
189         kernel = program.createCLKernel("sineWave")
190                         .putArg(clBuffer)
191                         .putArg(MESH_SIZE)
192                         .rewind();
193
194         System.out.println("cl initialised");
195     }
196
197
198     public void display(GLAutoDrawable drawable) {
199
200         GL2 gl = drawable.getGL().getGL2();
201
202         // ensure pipeline is clean before doing cl work
203         gl.glFinish();
204
205         computeHeightfield();
206
207         gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
208         gl.glLoadIdentity();
209
210         usi.interact(gl);
211
212         gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, glObjects[VERTICES]);
213         gl.glVertexPointer(4, GL2.GL_FLOAT, 0, 0);
214
215 //            gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, glObjects[INDICES]);
216
217         gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
218         gl.glDrawArrays(GL2.GL_POINTS, 0, MESH_SIZE * MESH_SIZE);
219 //            gl.glDrawElements(GL2.GL_TRIANGLES, ib.capacity(), GL2.GL_UNSIGNED_INT, 0);
220         gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
221
222 //            gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
223
224     }
225
226     /*
227      * Computes a heightfield using a OpenCL kernel.
228      */
229     private void computeHeightfield() {
230
231         kernel.setArg(2, step += 0.05f);
232
233         commandQueue.putAcquireGLObject(clBuffer.ID)
234                     .put2DRangeKernel(kernel, 0, 0, MESH_SIZE, MESH_SIZE, 0, 0)
235                     .putReleaseGLObject(clBuffer.ID)
236                     .finish();
237
238     }
239
240     private void pushPerspectiveView(GL2 gl) {
241
242         gl.glMatrixMode(GL2.GL_PROJECTION);
243         gl.glPushMatrix();
244
245             gl.glLoadIdentity();
246
247             glu.gluPerspective(60, width / (float)height, 1, 1000);
248             gl.glMatrixMode(GL2.GL_MODELVIEW);
249
250             gl.glPushMatrix();
251                 gl.glLoadIdentity();
252
253     }
254
255     private void popView(GL2 gl) {
256
257                 gl.glMatrixMode(GL2.GL_PROJECTION);
258             gl.glPopMatrix();
259
260             gl.glMatrixMode(GL2.GL_MODELVIEW);
261         gl.glPopMatrix();
262
263     }
264
265
266     public void reshape(GLAutoDrawable drawable, int arg1, int arg2, int width, int height) {
267         this.width = width;
268         this.height = height;
269         GL2 gl = drawable.getGL().getGL2();
270         popView(gl);
271         pushPerspectiveView(gl);
272     }
273
274     public void dispose(GLAutoDrawable drawable) {  }
275
276     public static void main(String[] args) {
277         //false for webstart compatibility
278         GLProfile.initSingleton(false);
279         
280         new GLCLInteroperabilityDemo();
281     }
282
283 }
http://JogAmp.org git info: FAQ, tutorial and man pages.