Login  Register

Re: CLImage2D example program - Exception while executing kernel

Posted by suleman on Apr 26, 2011; 3:57pm
URL: https://forum.jogamp.org/CLImage2D-example-program-Exception-while-executing-kernel-tp2866141p2866156.html

main code
=========
      ......
            //load image
            BufferedImage image = readImage("colors.png");
            assert image.getColorModel().getNumComponents() == 3;

            //Image dimensions
            int imageWidth = image.getWidth();
            int imageHeight = image.getHeight();

            //Output image
            BufferedImage outputImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
           
            //Create the memory object for the input- and output image

            float[] pixels = image.getRaster().getPixels(0, 0, image.getWidth(), image.getHeight(), (float[])null);


     CLImageFormat format = new CLImageFormat(RGB, FLOAT);

            CLImage2d<FloatBuffer> imageA = context.createImage2d(newDirectFloatBuffer(pixels), imageWidth, imageHeight, format);
            CLImage2d<FloatBuffer> imageB = context.createImage2d(newDirectFloatBuffer(pixels.length), imageWidth, imageHeight, format);
     
            //creade a command queue with benchmarking flag set
            CLCommandQueue queue = context.getDevices()[0].createCommandQueue(Mode.PROFILING_MODE);
           
            int localWorkSize = queue.getDevice().getMaxWorkGroupSize(); // Local work size dimensions
            int globalWorkSize = roundUp(localWorkSize, imageWidth*imageHeight);  // rounded up to the nearest multiple of the localWorkSize
             
            //create kernel and set function parameters
            CLKernel kernel = program.createCLKernel("gamma");
           
            //original lenna
            show(image, 0, 50, "reference");
           
            //a few gamma corrected versions
            float gamma = 0.5f;

            //gammaCorrection(gamma, queue, kernel, buffer, localWorkSize, globalWorkSize);
            gammaCorrection(imageWidth,imageHeight,gamma, queue, kernel, imageA,imageB,localWorkSize,globalWorkSize);
            FloatBuffer bufferA = imageA.getBuffer();
            FloatBuffer bufferB = imageB.getBuffer();
       
            // allocate a OpenCL buffer using the direct fb as working copy
            CLBuffer<FloatBuffer> buffer = context.createBuffer(bufferB, CLBuffer.Mem.READ_WRITE);
           
            show(createImage(imageWidth,imageHeight,buffer),image.getWidth()/2, 50, "gamma="+gamma);
           

------------------------------------------
//Kernel execution method
 private static void gammaCorrection(int width,int height,float gamma, CLCommandQueue queue, CLKernel kernel, CLImage2d imageA,CLImage2d imageB, int localWorkSize, int globalWorkSize) {
       
        float scaleFactor = (float) Math.pow(255, 1.0f-gamma);
       
        // setup kernel
        kernel.putArg(imageA).putArg(imageB).putArg(gamma).putArg(scaleFactor).putArg(width).putArg(height);  
        queue.putWriteImage(imageA, false);                                       // upload image
        queue.put2DRangeKernel(kernel, 0,0,width,height,0,0);   // execute program
        queue.putReadImage(imageB,true);
       
    }


KERNEL code
---------------------
 kernel void gamma(read_only image2d_t input, write_only image2d_t output,const float gamma, const float scale, const int width,const int height) {
       
        const sampler_t sampler=CLK_NORMALIZED_COORDS_FALSE|CLK_ADDRESS_CLAMP|CLK_FILTER_NEAREST;

        int x = get_global_id(0);
        int y = get_global_id(1);

        if((x>=width)||(y>=height))
                return;
 
        int2 coord = (int2)(x,y);
        float4 temp = read_imagef(input, sampler, coord);
  write_imagef(output, coord, pow(temp,(float4)gamma)*scale);
 
    }