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