Login  Register

Problem with memory

Posted by platinumthinker on Dec 07, 2012; 8:15pm
URL: https://forum.jogamp.org/Problem-with-memory-tp4027447.html

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();
        }
    }