X Tutup
Skip to content

Commit 66d95ea

Browse files
committed
JavaCL: added CLBuffer.copyTo, .copyBytesTo, .copyElementsTo, and added CLImage.copyTo missed in last commit (issue #508)
1 parent 3f7d385 commit 66d95ea

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,5 +452,45 @@ public <T> CLBuffer<T> as(Class<T> newTargetType) {
452452
PointerIO<T> newIO = PointerIO.getInstance(newTargetType);
453453
return copyGLMark(new CLBuffer<T>(context, getByteCount(), mem, owner, newIO));
454454
}
455+
456+
/**
457+
#documentCallsFunction("clEnqueueCopyBuffer")
458+
* @param queue
459+
#documentEventsToWaitForAndReturn()
460+
*/
461+
public CLEvent copyTo(CLQueue queue, CLBuffer destination, CLEvent... eventsToWaitFor) {
462+
return copyBytesTo(queue, destination, 0, 0, getByteCount(), eventsToWaitFor);
463+
}
464+
465+
/**
466+
#documentCallsFunction("clEnqueueCopyBuffer")
467+
* @param queue
468+
#documentEventsToWaitForAndReturn()
469+
*/
470+
public CLEvent copyBytesTo(CLQueue queue, CLBuffer destination, long sourceByteOffset, long destinationByteOffset, long byteCount, CLEvent... eventsToWaitFor) {
471+
#declareReusablePtrsAndEventsInOut()
472+
error(CL.clEnqueueCopyBuffer(
473+
queue.getEntity(),
474+
getEntity(),
475+
destination.getEntity(),
476+
sourceByteOffset,
477+
destinationByteOffset,
478+
byteCount,
479+
#eventsInOutArgsRaw()));
480+
#returnEventOut("queue")
481+
}
482+
483+
/**
484+
#documentCallsFunction("clEnqueueCopyBuffer")
485+
* @param queue
486+
#documentEventsToWaitForAndReturn()
487+
*/
488+
public CLEvent copyElementsTo(CLQueue queue, CLBuffer destination, long sourceElementOffset, long destinationElementOffset, long elementCount, CLEvent... eventsToWaitFor) {
489+
return copyBytesTo(queue, destination,
490+
sourceElementOffset * getElementSize(),
491+
destinationElementOffset * getElementSize(),
492+
elementCount * getElementSize(),
493+
eventsToWaitFor);
494+
}
455495

456496
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,6 @@ public CLEvent unmap(CLQueue queue, ByteBuffer buffer, CLEvent... eventsToWaitFo
232232

233233
/**
234234
#documentCallsFunction("clEnqueueCopyImage")
235-
* see {@link CLImage2D#map(com.nativelibs4java.opencl.CLQueue, com.nativelibs4java.opencl.CLMem.MapFlags, com.nativelibs4java.opencl.CLEvent[]) }
236-
* see {@link CLImage3D#map(com.nativelibs4java.opencl.CLQueue, com.nativelibs4java.opencl.CLMem.MapFlags, com.nativelibs4java.opencl.CLEvent[]) }
237235
* @param queue
238236
#documentEventsToWaitForAndReturn()
239237
*/
@@ -245,8 +243,6 @@ public CLEvent copyTo(CLQueue queue, CLImage destination, CLEvent... eventsToWai
245243

246244
/**
247245
#documentCallsFunction("clEnqueueCopyImage")
248-
* see {@link CLImage2D#map(com.nativelibs4java.opencl.CLQueue, com.nativelibs4java.opencl.CLMem.MapFlags, com.nativelibs4java.opencl.CLEvent[]) }
249-
* see {@link CLImage3D#map(com.nativelibs4java.opencl.CLQueue, com.nativelibs4java.opencl.CLMem.MapFlags, com.nativelibs4java.opencl.CLEvent[]) }
250246
* @param queue
251247
#documentEventsToWaitForAndReturn()
252248
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,15 @@ public void testFill() {
166166
fail("Expected pattern length failure");
167167
} catch (Throwable th) {}
168168
}
169+
@Test
170+
public void testCopyTo() {
171+
CLBuffer<Integer> b = context.createIntBuffer(CLMem.Usage.InputOutput, pointerToInts(1, 2, 3, 4));
172+
CLBuffer<Integer> out = context.createIntBuffer(CLMem.Usage.InputOutput, 4);
173+
CLEvent e = b.copyElementsTo(queue, out, 2, 1, 2);
174+
assertArrayEquals(new int[] { 0, 3, 4, 0 }, out.read(queue, e).getInts());
175+
176+
e = b.copyTo(queue, out);
177+
assertArrayEquals(new int[] { 1, 2, 3, 4 }, out.read(queue, e).getInts());
178+
}
169179

170180
}

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
* To change this template, choose Tools | Templates
3-
* and open the template in the editor.
4-
*/
5-
61
package com.nativelibs4java.opencl;
72

83
import org.bridj.Pointer;
@@ -11,16 +6,7 @@
116
import static org.junit.Assert.*;
127

138
import java.awt.image.BufferedImage;
14-
import java.nio.IntBuffer;
15-
16-
import org.junit.Assert;
17-
import org.junit.BeforeClass;
189
import org.junit.Test;
19-
20-
import com.nativelibs4java.test.MiscTestUtils;
21-
import com.nativelibs4java.util.ImageUtils;
22-
import java.nio.ByteOrder;
23-
import java.nio.FloatBuffer;
2410
import java.util.Arrays;
2511
import java.util.List;
2612
import org.junit.runners.Parameterized;
@@ -117,6 +103,23 @@ public void testARGBShortGrayReadWrite() {
117103
assertSameImage(im, clim.read(queue));
118104
}
119105

106+
@Test
107+
public void testCopyTo() {
108+
int width = 2, height = 2;
109+
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
110+
int value = 0xaabbccdd;
111+
image.setRGB(0, 0, value);
112+
image.setRGB(1, 1, value);
113+
CLImage2D src = context.createImage2D(CLMem.Usage.InputOutput, image, true);
114+
CLImage2D dest = context.createImage2D(CLMem.Usage.InputOutput, src.getFormat(), width, height);
115+
CLEvent e = src.copyTo(queue, dest);
116+
BufferedImage res = dest.read(queue, e);
117+
assertEquals(value, res.getRGB(0, 0));
118+
assertEquals(0, res.getRGB(1, 0));
119+
assertEquals(value, res.getRGB(1, 1));
120+
assertEquals(0, res.getRGB(0, 1));
121+
}
122+
120123

121124

122125
@Test
@@ -411,7 +414,7 @@ public void testFillImage() {
411414
for (int x = 0; x < width; x++) {
412415
for (int y = 0; y < height; y++) {
413416
int pix = im.getRGB(x, y);
414-
assertEquals(argb, pix);
417+
assertEquals("x = " + x + ", y = " + y, argb, pix);
415418
}
416419
}
417420
}

0 commit comments

Comments
 (0)
X Tutup