package com.jogamp.opencl.test; import com.jogamp.opencl.gl.CLGLImage2d; import java.nio.FloatBuffer; import com.jogamp.opencl.CLKernel; import com.jogamp.opencl.CLProgram; import com.jogamp.opencl.CLBuffer; import com.jogamp.opencl.CLCommandQueue; import com.jogamp.opencl.CLDevice; import com.jogamp.opencl.CLMemory.Mem; import com.jogamp.opencl.CLPlatform; import com.jogamp.opencl.gl.CLGLContext; import com.jogamp.opencl.gl.CLGLTexture2d; import com.jogamp.opencl.util.CLPlatformFilters; import java.awt.Dimension; import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.JFrame; import static javax.media.opengl.GL2.*; /** * * @author notzed */ public class Test2 implements GLEventListener { JFrame window; GLCanvas canvas; CLGLTexture2d fullTex; CLGLImage2d fullImg; CLBuffer fullBuffer; CLGLContext cl; CLCommandQueue queue; CLKernel kPlaneToImage; String src = "kernel void planeToImage(global float *greyf, write_only image2d_t img, float scalef) {" + " int lx = get_global_id(0);" + " int ly = get_global_id(1);" + " int2 pos = { lx, ly };" + " float v = greyf[lx + ly * 1024] * scalef;" + " write_imagef(img, pos, (float4)v);" + "}"; void run() { window = new JFrame("GL Surface"); canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL2))); canvas.setPreferredSize(new Dimension(1024, 768)); canvas.addGLEventListener(this); canvas.display(); canvas.setAutoSwapBufferMode(true); window.add(canvas); window.pack(); window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); window.setVisible(true); } CLGLTexture2d genTexture(CLGLContext cl, int w, int h, int fmt, int type) { int[] txt = new int[1]; GL2 gl = (GL2) cl.getGLContext().getGL(); gl.glGenTextures(1, txt, 0); gl.glBindTexture(GL_TEXTURE_2D, txt[0]); gl.glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gl.glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, type, null); gl.glBindTexture(GL_TEXTURE_2D, 0); gl.glFlush(); return cl.createFromGLTexture2d(GL_TEXTURE_2D, txt[0], 0, Mem.READ_WRITE); } CLGLImage2d genImage(CLGLContext cl, int w, int h, int fmt) { int[] buf = new int[1]; GL2 gl = (GL2) cl.getGLContext().getGL(); gl.glGenRenderbuffers(1, buf, 0); gl.glBindRenderbuffer(GL_RENDERBUFFER, buf[0]); gl.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, w, h); gl.glBindRenderbuffer(GL_RENDERBUFFER, 0); gl.glFlush(); return cl.createFromGLRenderbuffer(buf[0], Mem.READ_WRITE); } @Override public void init(GLAutoDrawable glad) { CLDevice device = CLPlatform.getDefault(CLPlatformFilters.type(CLDevice.Type.GPU)).getMaxFlopsDevice(); cl = CLGLContext.create(glad.getContext(), device); CLDevice devices[] = cl.getDevices(); queue = devices[0].createCommandQueue(); fullTex = genTexture(cl, 1024, 768, GL_RGBA, GL_UNSIGNED_BYTE); fullBuffer = cl.createFloatBuffer(1024 * 768, Mem.READ_WRITE); fullImg = genImage(cl, 1024, 768, GL_RGBA); CLProgram prog = cl.createProgram(src).build(); kPlaneToImage = prog.createCLKernel("planeToImage"); } @Override public void dispose(GLAutoDrawable glad) { System.out.println("dispose"); cl.release(); } @Override public void display(GLAutoDrawable glad) { GL2 gl = (GL2) glad.getGL(); gl.glFinish(); try { kPlaneToImage.setArg(0, fullBuffer); kPlaneToImage.setArg(1, fullTex); kPlaneToImage.setArg(2, 255f); queue.put2DRangeKernel(kPlaneToImage, 0, 0, 1024, 768, 64, 1); } catch (Exception x) { System.out.println("kplanetoimage with texture failed\n " + x.getMessage()); } // queue.finish(); try { kPlaneToImage.setArg(0, fullBuffer); kPlaneToImage.setArg(1, fullImg); kPlaneToImage.setArg(2, 255f); queue.put2DRangeKernel(kPlaneToImage, 0, 0, 1024, 768, 64, 1); } catch (Exception x) { System.out.println("kplanetoimage with gl render buffer failed\n " + x.getMessage()); } queue.finish(); gl.glClear(GL_COLOR_BUFFER_BIT); gl.glColor4f(1, 0, 1, 0.5f); // draw texture gl.glBindTexture(GL_TEXTURE_2D, fullTex.GLID); gl.glEnable(GL_TEXTURE_2D); gl.glBegin(GL_QUADS); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex2f(0, 0); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex2f(1024, 0); gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex2f(1024, 768); gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex2f(0, 768); gl.glEnd(); gl.glDisable(GL_TEXTURE_2D); gl.glBindTexture(GL_TEXTURE_2D, 0); // draw a line gl.glColor4f(1, 0, 0, 1.0f); gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gl.glEnable(GL_BLEND); gl.glEnable(GL_LINE_SMOOTH); gl.glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); gl.glBegin(GL2.GL_LINES); gl.glVertex2d(0, 0); gl.glVertex2d(100, 100); gl.glEnd(); gl.glDisable(GL_LINE_SMOOTH); gl.glDisable(GL_BLEND); gl.glFlush(); } @Override public void reshape(GLAutoDrawable glad, int x, int y, int width, int height) { GL2 gl = glad.getGL().getGL2(); gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL_MODELVIEW); gl.glLoadIdentity(); gl.glMatrixMode(GL_PROJECTION); gl.glLoadIdentity(); gl.glOrtho(0.0, width, 0.0, height, 0.0, 1.0); } /** * @param args the command line arguments */ public static void main(String[] args) { GLProfile.initSingleton(true); Test2 m = new Test2(); m.run(); } }