JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLImageTest.java
Go to the documentation of this file.
1/*
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opencl;
30
31
32import java.awt.image.BufferedImage;
33import java.io.IOException;
34import java.nio.IntBuffer;
35
36import javax.imageio.ImageIO;
37
38import org.junit.BeforeClass;
39import org.junit.FixMethodOrder;
40import org.junit.Test;
41import org.junit.runners.MethodSorters;
42
43import com.jogamp.opencl.test.util.MiscUtils;
44import com.jogamp.opencl.test.util.UITestCase;
45
46import static org.junit.Assert.*;
47import static java.lang.System.*;
48import static com.jogamp.common.nio.Buffers.*;
49import static com.jogamp.opencl.CLImageFormat.ChannelOrder.*;
50import static com.jogamp.opencl.CLImageFormat.ChannelType.*;
51
52/**
53 * Test testing CLImage API.
54 * @author Michael Bien, et.al
55 */
56@FixMethodOrder(MethodSorters.NAME_ASCENDING)
57public class CLImageTest extends UITestCase {
58
59 private static int[] pixels;
60
61 @BeforeClass
62 public static void init() throws IOException {
63 final BufferedImage bi = ImageIO.read(CLImageTest.class.getResourceAsStream("jogamp.png"));
64 pixels = new int[128*128*4];
65 bi.getData().getPixels(0, 0, 128, 128, pixels);
66 }
67
69
70 final CLPlatform[] platforms = CLPlatform.listCLPlatforms();
71 for (final CLPlatform platform : platforms) {
72 final CLDevice[] devices = platform.listCLDevices();
73
74 for (final CLDevice device : devices) {
75 if(device.isImageSupportAvailable()) {
76 return device;
77 }
78 }
79 }
80
81 return null;
82 }
83
84
85 @Test
87 final CLDevice device = getCompatibleDevice();
88 if(device == null) {
89 out.println("WARNING: can not test image api.");
90 return;
91 }
92 final CLContext context = CLContext.create(device);
93
94 try{
95 final CLImageFormat[] formats = context.getSupportedImage2dFormats();
96 assertTrue(formats.length > 0);
97 out.println("sample image format: "+formats[0]);
98// for (CLImageFormat format : formats) {
99// out.println(format);
100// }
101 }finally{
102 context.release();
103 }
104
105 }
106
107 @Test
108 public void image2dCopyTest() throws IOException {
109 final CLDevice device = getCompatibleDevice();
110 if(device == null) {
111 out.println("WARNING: can not test image api.");
112 return;
113 }
114 final CLContext context = CLContext.create(device);
115
116 final CLCommandQueue queue = device.createCommandQueue();
117
118 try{
119
120 final CLImageFormat format = new CLImageFormat(RGBA, UNSIGNED_INT32);
121
122 final CLImage2d<IntBuffer> imageA = context.createImage2d(newDirectIntBuffer(pixels), 128, 128, format);
123 final CLImage2d<IntBuffer> imageB = context.createImage2d(newDirectIntBuffer(pixels.length), 128, 128, format);
124
125 queue.putWriteImage(imageA, false)
126 .putCopyImage(imageA, imageB)
127 .putReadImage(imageB, true);
128
129 final IntBuffer bufferA = imageA.getBuffer();
130 final IntBuffer bufferB = imageB.getBuffer();
131
132 while(bufferA.hasRemaining()) {
133 assertEquals(bufferA.get(), bufferB.get());
134 }
135
136 }finally{
137 context.release();
138 }
139
140 }
141
142 @Test
143 public void image2dKernelCopyTest() throws IOException {
144 final CLDevice device = getCompatibleDevice();
145 if(device == null) {
146 out.println("WARNING: can not test image api.");
147 return;
148 }
149 final CLContext context = CLContext.create(device);
150
151 final String src =
152 "constant sampler_t imageSampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST; \n" +
153 "kernel void image2dCopy(read_only image2d_t input, write_only image2d_t output) { \n" +
154 " int2 coord = (int2)(get_global_id(0), get_global_id(1)); \n" +
155 " uint4 temp = read_imageui(input, imageSampler, coord); \n" +
156 " write_imageui(output, coord, temp); \n" +
157 "} \n";
158
159 final CLKernel kernel = context.createProgram(src).build().createCLKernel("image2dCopy");
160
161 final CLCommandQueue queue = device.createCommandQueue();
162
163 try{
164
165 final CLImageFormat format = new CLImageFormat(RGBA, UNSIGNED_INT32);
166
167 final CLImage2d<IntBuffer> imageA = context.createImage2d(newDirectIntBuffer(pixels), 128, 128, format);
168 final CLImage2d<IntBuffer> imageB = context.createImage2d(newDirectIntBuffer(pixels.length), 128, 128, format);
169
170 kernel.putArgs(imageA, imageB);
171 queue.putWriteImage(imageA, false)
172 .put2DRangeKernel(kernel, 0, 0, 128, 128, 0, 0)
173 .putReadImage(imageB, true);
174
175 final IntBuffer bufferA = imageA.getBuffer();
176 final IntBuffer bufferB = imageB.getBuffer();
177
178 while(bufferA.hasRemaining()) {
179 assertEquals(bufferA.get(), bufferB.get());
180 }
181
182 }finally{
183 context.release();
184 }
185
186 }
187 public static void main(final String[] args) throws IOException {
188 final String tstname = CLImageTest.class.getName();
189 org.junit.runner.JUnitCore.main(tstname);
190 }
191
192}
The command queue is used to queue a set of operations for a specific CLDevice.
CLCommandQueue putWriteImage(final CLImage2d<?> writeImage, final boolean blockingWrite)
Calls {@native clEnqueueWriteImage}.
CLCommandQueue putReadImage(final CLImage2d<?> readImage, final boolean blockingRead)
Calls {@native clEnqueueReadImage}.
CLCommandQueue put2DRangeKernel(final CLKernel kernel, final long globalWorkOffsetX, final long globalWorkOffsetY, final long globalWorkSizeX, final long globalWorkSizeY, final long localWorkSizeX, final long localWorkSizeY)
Calls {@native clEnqueueNDRangeKernel}.
CLCommandQueue putCopyImage(final CLImage2d<?> srcImage, final CLImage2d<?> dstImage)
Calls {@native clEnqueueCopyImage}.
CLContext is responsible for managing objects such as command-queues, memory, program and kernel obje...
Definition: CLContext.java:79
CLImageFormat[] getSupportedImage2dFormats(final Mem... flags)
Returns all supported 2d image formats with the (optional) memory allocation flags.
Definition: CLContext.java:554
CLProgram createProgram(final String src)
Creates a program from the given sources, the returned program is not build yet.
Definition: CLContext.java:243
static CLContext create()
Creates a context on all available devices (CL_DEVICE_TYPE_ALL).
Definition: CLContext.java:139
final CLImage2d<?> createImage2d(final int width, final int height, final CLImageFormat format, final Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.
Definition: CLContext.java:373
This object represents an OpenCL device.
Definition: CLDevice.java:53
CLCommandQueue createCommandQueue()
Definition: CLDevice.java:72
Represents the OpenCL image format with its channeltype and order.
Test testing CLImage API.
static void main(final String[] args)
High level abstraction for an OpenCL Kernel.
Definition: CLKernel.java:53
CLKernel putArgs(final CLMemory<?>... values)
Definition: CLKernel.java:149
CLPlatfrorm representing a OpenCL implementation (e.g.
Definition: CLPlatform.java:99
static CLPlatform[] listCLPlatforms()
Lists all available OpenCL implementations.
CLProgram build()
Builds this program for all devices associated with the context.
Definition: CLProgram.java:226
CLKernel createCLKernel(final String kernelName)
Creates a kernel with the specified kernel name.
Definition: CLProgram.java:410