I see the documentation and examples, but have not been able to solve the problem. The application consists of a block of initialization openCL and infinitude cycle in which I form an image. I understand that the problem is that I do not clean the buffer, but I do not understand how to do it.
public class RenderGPU extends JFrame { int height, width; private BufferedImage bufferedImage; private boolean isRunning = true; CLCommandQueue queue; CLContext context; private CLProgram program; CLBuffer<FloatBuffer> cam_points; CLBuffer<IntBuffer> image; private int[] pixels; private int elementCount; private CLKernel kernel; public RenderGPU() { height = 500; width = 500; this.model = model; setSize(width, height); setVisible(true); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { isRunning = false; context.release(); System.exit(0); } }); initCL(); add(new JLabel(new ImageIcon(bufferedImage))); setSize(500, 500); pack(); new Runnable() { @Override public void run() { while (isRunning) { long time = System.currentTimeMillis(); renderFrame(); repaint(); setTitle(String.format("FPS %f", 1000./(System.currentTimeMillis() - time))); } } }.run(); } public void stop() { isRunning = false; } private void renderFrame() { image = context.createIntBuffer(elementCount * 3, CLMemory.Mem.WRITE_ONLY, CLMemory.Mem.USE_BUFFER); kernel = program.createCLKernel("color").putArg(image).rewind(); queue.put1DRangeKernel(kernel, 0, elementCount, 0) .putReadBuffer(image, true); image.getBuffer().get(pixels); queue.finish(); bufferedImage.getRaster().setPixels(0, 0, width, height, pixels); } public CLDevice getCompatibleDevice() { CLPlatform[] platforms = CLPlatform.listCLPlatforms(); for (CLPlatform platform : platforms) { CLDevice[] devices = platform.listCLDevices(); for (CLDevice device : devices) { if(device.isImageSupportAvailable()) { return device; } } } return null; } private void initCL() { elementCount = width * height; CLDevice device = getCompatibleDevice(); context = CLContext.create(device); try{ System.out.println("using " + device); queue = device.createCommandQueue(CLCommandQueue.Mode.PROFILING_MODE); int localWorkSize = min(device.getMaxWorkGroupSize(), 256); // Local work size dimensions int globalWorkSize = roundUp(localWorkSize, elementCount); // rounded up to the nearest multiple of the localWorkSize pixels = new int[elementCount * 3]; program = context.createProgram(ClassLoader.getSystemResourceAsStream("RayCasting/color.cl")).build(); System.out.println(program.getBuildStatus(device)); System.out.println(program.getBuildLog()); bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); } catch (IOException e) { e.printStackTrace(); } } |
I partially solved the problem with a memory leak. Now do not flow away gigabytes of memory =) But I still see that as time leak is. Using the latest version of the jocl.
|
In reply to this post by platinumthinker
import com.jogamp.opencl.*;
import javax.swing.*; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.io.IOException; import java.nio.FloatBuffer; import java.nio.IntBuffer; import static java.lang.Math.min; /** * Created by IntelliJ IDEA. * User: thinker * Date: 18.11.12 * Time: 3:42 */ public class RenderGPU extends JFrame { int height, width; private BufferedImage bufferedImage; private boolean isRunning = true; CLCommandQueue queue; CLContext context; CLBuffer<FloatBuffer> cam_points; CLBuffer<IntBuffer> image; private int[] pixels; private int elementCount; int x = -10, y = -10; private CLKernel kernel; public RenderGPU(l) { height = 500; width = 500; // color_buffer = new byte[width][height][4]; // depth_buffer = new float[width][height]; // normal_buffer = new float[width][height]; // light = new OmniLight(); // light.point = new Vector(0,1,1); // light.color = new Color(255, 253, 244); // buffer = new VoxelBuffer(model); // System.out.println("Finish buffering..."); setSize(width, height); setLocation(700, 0); setCursor(getToolkit().createCustomCursor(new BufferedImage( 1, 1, BufferedImage.TYPE_INT_ARGB ), new Point(), null)); setVisible(true); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { isRunning = false; context.release(); System.exit(0); } }); addMouseMotionListener(new MouseMotionListener() { @Override public void mouseDragged(MouseEvent e) { x = e.getX(); y = e.getY(); } @Override public void mouseMoved(MouseEvent e) { x = e.getX(); y = e.getY(); System.out.println(x+" "+y); } }); initCL(); add(new JLabel(new ImageIcon(bufferedImage))); setSize(500, 500); pack(); new Runnable() { @Override public void run() { while (isRunning) { long time = System.currentTimeMillis(); renderFrame(); repaint(); sleep(20 - (System.currentTimeMillis() - time)); setTitle(String.format("FPS %f", 1000./(System.currentTimeMillis() - time))); } } }.run(); } private void sleep(long time) { if(time < 1) return; try { Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } } public void stop() { isRunning = false; } private void renderFrame() { queue.put1DRangeKernel(kernel, 0, elementCount, 0) .putReadBuffer(image, true).finish(); image.getBuffer().get(pixels); image.getBuffer().rewind(); bufferedImage.getRaster().setPixels(0, 0, width, height, pixels); } public CLDevice getCompatibleDevice() { CLPlatform[] platforms = CLPlatform.listCLPlatforms(); for (CLPlatform platform : platforms) { CLDevice[] devices = platform.listCLDevices(); for (CLDevice device : devices) { if(device.isImageSupportAvailable()) { return device; } } } return null; } private void initCL() { elementCount = width * height; CLDevice device = getCompatibleDevice(); context = CLContext.create(device); try{ System.out.println("using " + device); queue = device.createCommandQueue(CLCommandQueue.Mode.PROFILING_MODE); pixels = new int[elementCount * 3]; CLProgram program = context.createProgram(ClassLoader.getSystemResourceAsStream("RayCasting/color.cl")).build(); image = context.createIntBuffer(elementCount * 3, CLMemory.Mem.WRITE_ONLY); kernel = program.createCLKernel("color").putArg(image).rewind(); System.out.println(program.getBuildStatus(device)); System.out.println(program.getBuildLog()); bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); } catch (IOException e) { e.printStackTrace(); } } } kernel void color(global int* image) { image[get_global_id(0) * 3] = 0; image[get_global_id(0) * 3 + 1] = 0; image[get_global_id(0) * 3 + 2] = 50; } |
Free forum by Nabble | Edit this page |