diff options
-rw-r--r-- | src/com/jogamp/opencl/CLImage2d.java | 9 | ||||
-rw-r--r-- | src/com/jogamp/opencl/CLImage3d.java | 9 | ||||
-rw-r--r-- | test/com/jogamp/opencl/CLImageTest.java | 153 | ||||
-rw-r--r-- | test/com/jogamp/opencl/HighLevelBindingTest.java | 30 | ||||
-rw-r--r-- | test/com/jogamp/opencl/jogamp.png | bin | 0 -> 7882 bytes |
5 files changed, 165 insertions, 36 deletions
diff --git a/src/com/jogamp/opencl/CLImage2d.java b/src/com/jogamp/opencl/CLImage2d.java index 7cf04cf1..c6d4c96a 100644 --- a/src/com/jogamp/opencl/CLImage2d.java +++ b/src/com/jogamp/opencl/CLImage2d.java @@ -24,9 +24,12 @@ public class CLImage2d<B extends Buffer> extends CLImage<B> { int width, int height, int rowPitch, CLImageFormat format, int flags) { CL cl = context.cl; - IntBuffer err = Buffers.newDirectByteBuffer(4).asIntBuffer(); - - long id = cl.clCreateImage2D(context.ID, flags, format.getFormatImpl(), width, height, rowPitch, directBuffer, err); + IntBuffer err = Buffers.newDirectIntBuffer(1); + B host_ptr = null; + if(isHostPointerFlag(flags)) { + host_ptr = directBuffer; + } + long id = cl.clCreateImage2D(context.ID, flags, format.getFormatImpl(), width, height, rowPitch, host_ptr, err); checkForError(err.get(), "can not create 2d image"); return new CLImage2d<B>(context, directBuffer, format, width, height, id, flags); diff --git a/src/com/jogamp/opencl/CLImage3d.java b/src/com/jogamp/opencl/CLImage3d.java index fd127864..efa97fd3 100644 --- a/src/com/jogamp/opencl/CLImage3d.java +++ b/src/com/jogamp/opencl/CLImage3d.java @@ -30,9 +30,12 @@ public class CLImage3d<B extends Buffer> extends CLImage<B> { int width, int height, int depth, int rowPitch, int slicePitch, CLImageFormat format, int flags) { CL cl = context.cl; - IntBuffer err = Buffers.newDirectByteBuffer(4).asIntBuffer(); - - long id = cl.clCreateImage3D(context.ID, flags, format.getFormatImpl(), width, height, depth, rowPitch, slicePitch, directBuffer, err); + IntBuffer err = Buffers.newDirectIntBuffer(1); + B host_ptr = null; + if(isHostPointerFlag(flags)) { + host_ptr = directBuffer; + } + long id = cl.clCreateImage3D(context.ID, flags, format.getFormatImpl(), width, height, depth, rowPitch, slicePitch, host_ptr, err); checkForError(err.get(), "can not create 2d image"); return new CLImage3d<B>(context, directBuffer, format, width, height, depth, id, flags); diff --git a/test/com/jogamp/opencl/CLImageTest.java b/test/com/jogamp/opencl/CLImageTest.java new file mode 100644 index 00000000..dc80b9e1 --- /dev/null +++ b/test/com/jogamp/opencl/CLImageTest.java @@ -0,0 +1,153 @@ +package com.jogamp.opencl; + + +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.nio.IntBuffer; +import javax.imageio.ImageIO; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; +import static java.lang.System.*; +import static com.jogamp.common.nio.Buffers.*; +import static com.jogamp.opencl.CLImageFormat.ChannelOrder.*; +import static com.jogamp.opencl.CLImageFormat.ChannelType.*; + +/** + * Test testing CLImage API. + * @author Michael Bien + */ +public class CLImageTest { + + private static int[] pixels; + + @BeforeClass + public static void init() throws IOException { + BufferedImage bi = ImageIO.read(CLImageTest.class.getResourceAsStream("jogamp.png")); + pixels = new int[128*128*4]; + bi.getData().getPixels(0, 0, 128, 128, 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; + } + + + @Test + public void supportedImageFormatsTest() { + CLDevice device = getCompatibleDevice(); + if(device == null) { + out.println("WARNING: can not test image api."); + return; + } + CLContext context = CLContext.create(device); + + try{ + CLImageFormat[] formats = context.getSupportedImage2dFormats(); + assertTrue(formats.length > 0); + out.println("sample image format: "+formats[0]); +// for (CLImageFormat format : formats) { +// out.println(format); +// } + }finally{ + context.release(); + } + + } + + @Test + public void image2dCopyTest() throws IOException { + + CLDevice device = getCompatibleDevice(); + if(device == null) { + out.println("WARNING: can not test image api."); + return; + } + CLContext context = CLContext.create(device); + + CLCommandQueue queue = device.createCommandQueue(); + + try{ + + CLImageFormat format = new CLImageFormat(RGBA, UNSIGNED_INT32); + + CLImage2d<IntBuffer> imageA = context.createImage2d(newDirectIntBuffer(pixels), 128, 128, format); + CLImage2d<IntBuffer> imageB = context.createImage2d(newDirectIntBuffer(pixels.length), 128, 128, format); + + queue.putWriteImage(imageA, false) + .putCopyImage(imageA, imageB) + .putReadImage(imageB, true); + + IntBuffer bufferA = imageA.getBuffer(); + IntBuffer bufferB = imageB.getBuffer(); + + while(bufferA.hasRemaining()) { + assertEquals(bufferA.get(), bufferB.get()); + } + + }finally{ + context.release(); + } + + } + + @Test + public void image2dKernelCopyTest() throws IOException { + + CLDevice device = getCompatibleDevice(); + if(device == null) { + out.println("WARNING: can not test image api."); + return; + } + CLContext context = CLContext.create(device); + + String src = + "constant sampler_t imageSampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST; \n" + + "kernel void image2dCopy(read_only image2d_t input, write_only image2d_t output) { \n" + + " int2 coord = (int2)(get_global_id(0), get_global_id(1)); \n" + + " uint4 temp = read_imageui(input, imageSampler, coord); \n" + + " write_imageui(output, coord, temp); \n" + + "} \n"; + + CLKernel kernel = context.createProgram(src).build().createCLKernel("image2dCopy"); + + CLCommandQueue queue = device.createCommandQueue(); + + try{ + + CLImageFormat format = new CLImageFormat(RGBA, UNSIGNED_INT32); + + CLImage2d<IntBuffer> imageA = context.createImage2d(newDirectIntBuffer(pixels), 128, 128, format); + CLImage2d<IntBuffer> imageB = context.createImage2d(newDirectIntBuffer(pixels.length), 128, 128, format); + + kernel.putArgs(imageA, imageB); + queue.putWriteImage(imageA, false) + .put2DRangeKernel(kernel, 0, 0, 128, 128, 0, 0) + .putReadImage(imageB, true); + + IntBuffer bufferA = imageA.getBuffer(); + IntBuffer bufferB = imageB.getBuffer(); + + while(bufferA.hasRemaining()) { + assertEquals(bufferA.get(), bufferB.get()); + } + + }finally{ + context.release(); + } + + } + +} diff --git a/test/com/jogamp/opencl/HighLevelBindingTest.java b/test/com/jogamp/opencl/HighLevelBindingTest.java index 884e22ee..e927f1fa 100644 --- a/test/com/jogamp/opencl/HighLevelBindingTest.java +++ b/test/com/jogamp/opencl/HighLevelBindingTest.java @@ -321,35 +321,5 @@ public class HighLevelBindingTest { context.release(); } - - @Test - public void supportedImageFormatsTest() { - - CLDevice[] devices = CLPlatform.getDefault().listCLDevices(); - - CLDevice theChosenOne = null; - for (CLDevice device : devices) { - if(device.isImageSupportAvailable()) { - theChosenOne = device; - break; - } - } - - if(theChosenOne == null) { - out.println("can not test image api."); - return; - } - - CLContext context = CLContext.create(theChosenOne); - - try{ - CLImageFormat[] formats = context.getSupportedImage2dFormats(); - assertTrue(formats.length > 0); - out.println("sample image format: "+formats[0]); - }finally{ - context.release(); - } - - } } diff --git a/test/com/jogamp/opencl/jogamp.png b/test/com/jogamp/opencl/jogamp.png Binary files differnew file mode 100644 index 00000000..0a1d4901 --- /dev/null +++ b/test/com/jogamp/opencl/jogamp.png |