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