X Tutup
Skip to content

Commit 9f402b3

Browse files
committed
JavaCL: added CLImage.fillImage (issue #232)
1 parent ca0229b commit 9f402b3

File tree

4 files changed

+96
-6
lines changed

4 files changed

+96
-6
lines changed

Core/src/main/velocity/com/nativelibs4java/opencl/CLImage.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package com.nativelibs4java.opencl;
33
import static com.nativelibs4java.opencl.CLException.error;
44
import static com.nativelibs4java.opencl.JavaCL.CL;
5+
import static com.nativelibs4java.opencl.JavaCL.check;
56
import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_FALSE;
67
import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_IMAGE_ELEMENT_SIZE;
78
import static com.nativelibs4java.opencl.library.IOpenCLLibrary.CL_IMAGE_FORMAT;
@@ -37,7 +38,7 @@ public abstract class CLImage extends CLMem {
3738
this.format = format;
3839
}
3940

40-
41+
protected abstract long[] getDimensions();
4142

4243
/**
4344
* Return image format descriptor specified when image is created with CLContext.create{Input|Output|InputOutput}{2D|3D}.
@@ -124,6 +125,65 @@ public void run() {
124125
return evt;
125126
}
126127

128+
/**
129+
#documentCallsFunction("clEnqueueFillImage")
130+
* @param queue
131+
* @param queue Queue on which to enqueue this fill buffer command.
132+
* @param color Color components to fill the buffer with.
133+
* @param origin Origin point.
134+
* @param region Size of the region to fill.
135+
#documentEventsToWaitForAndReturn()
136+
*/
137+
public CLEvent fillImage(CLQueue queue, Object color, CLEvent... eventsToWaitFor) {
138+
long[] region = getDimensions();
139+
long[] origin = new long[region.length];
140+
return fillImage(queue, color, origin, region, eventsToWaitFor);
141+
}
142+
143+
/**
144+
#documentCallsFunction("clEnqueueFillImage")
145+
* @param queue
146+
* @param queue Queue on which to enqueue this fill buffer command.
147+
* @param color Color components to fill the buffer with.
148+
* @param origin Origin point.
149+
* @param region Size of the region to fill.
150+
#documentEventsToWaitForAndReturn()
151+
*/
152+
public CLEvent fillImage(CLQueue queue, Object color, long[] origin, long[] region, CLEvent... eventsToWaitFor) {
153+
context.getPlatform().requireMinVersionValue("clEnqueueFillImage", 1.2);
154+
Pointer<?> pColor;
155+
if (color instanceof int[]) {
156+
pColor = pointerToInts((int[]) color);
157+
} else if (color instanceof float[]) {
158+
pColor = pointerToFloats((float[]) color);
159+
} else {
160+
throw new IllegalArgumentException("Color should be an int[] or a float[] with 4 elements.");
161+
}
162+
check(pColor.getValidElements() == 4, "Color should have 4 elements.");
163+
164+
#declareReusablePtrsAndEventsInOut()
165+
Pointer<SizeT> pOrigin = ptrs.sizeT3_1.pointerToSizeTs(origin);
166+
Pointer<SizeT> pRegion = ptrs.sizeT3_2.pointerToSizeTs(region);
167+
error(CL.clEnqueueFillImage(
168+
queue.getEntity(),
169+
getEntity(),
170+
getPeer(pColor),
171+
getPeer(pOrigin),
172+
getPeer(pRegion),
173+
#eventsInOutArgsRaw()
174+
));
175+
#returnEventOut("queue")
176+
}
177+
178+
// clEnqueueFillImage ( cl_command_queue command_queue,
179+
// cl_mem image,
180+
// const void *fill_color,
181+
// const size_t *origin,
182+
// const size_t *region,
183+
// cl_uint num_events_in_wait_list,
184+
// const cl_event *event_wait_list,
185+
// cl_event *event)
186+
127187
protected Pair<ByteBuffer, CLEvent> map(CLQueue queue, MapFlags flags,
128188
Pointer<SizeT> offset3, Pointer<SizeT> length3,
129189
Long imageRowPitch,

Core/src/main/velocity/com/nativelibs4java/opencl/CLImage2D.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public long getHeight() {
5656
return infos.getIntOrLong(getEntity(), CL_IMAGE_HEIGHT);
5757
}
5858

59+
@Override
60+
protected long[] getDimensions() {
61+
return new long[] { getWidth(), getHeight(), 1 };
62+
}
63+
5964
/**
6065
#documentEventsToWaitForAndReturn()
6166
*/

Core/src/main/velocity/com/nativelibs4java/opencl/CLImage3D.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public long getDepth() {
3939
return infos.getIntOrLong(getEntity(), CL_IMAGE_DEPTH);
4040
}
4141

42+
@Override
43+
protected long[] getDimensions() {
44+
return new long[] { getWidth(), getHeight(), getDepth() };
45+
}
46+
4247
/**
4348
#documentEventsToWaitForAndReturn()
4449
*/

Core/src/test/java/com/nativelibs4java/opencl/ImageTest.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void testRGBAImageSource() {
300300
public void testShortGrayImageSource() {
301301
if (!supportsImages())
302302
return;
303-
try {
303+
// try {
304304
CLContext context = JavaCL.createBestContext();
305305
CLQueue queue = context.createDefaultQueue();
306306
String src = "\n" +
@@ -391,8 +391,28 @@ public void testShortGrayImageSource() {
391391
}
392392
*
393393
*/
394-
} catch (Exception ex) {
395-
ex.printStackTrace();
396-
}
394+
// } catch (Exception ex) {
395+
// ex.printStackTrace();
396+
// }
397397
}
398-
}
398+
399+
@Test
400+
public void testFillImage() {
401+
if (!supportsImages())
402+
return;
403+
int width = 256, height = 256;
404+
CLImage2D clim = context.createImage2D(CLMem.Usage.InputOutput, CLImageFormat.INT_ARGB_FORMAT, width, height);
405+
406+
int red = 100, green = 110, blue = 120, alpha = 127;
407+
int argb = alpha << 24 | red << 16 | green << 8 | blue;
408+
CLEvent e = clim.fillImage(queue, new int[] { red, green, blue, alpha });
409+
BufferedImage im = clim.read(queue, e);
410+
//int[] rgb = im.getRGB(0, 0, width, height, null, 0, width);
411+
for (int x = 0; x < width; x++) {
412+
for (int y = 0; y < height; y++) {
413+
int pix = im.getRGB(x, y);
414+
assertEquals(argb, pix);
415+
}
416+
}
417+
}
418+
}

0 commit comments

Comments
 (0)
X Tutup