From cf5340c0bfc1914073ea1f4fc3ccad83f50dc57d Mon Sep 17 00:00:00 2001 From: Emily Leiviskä Date: Wed, 16 Nov 2016 16:55:30 +0100 Subject: Changes CLMemory's getNIOSize() and getNIOCapacity() to use buffer.limit() instead of capacity() in order to respect the user's desired buffer size in memory operations. --- test/com/jogamp/opencl/CLBufferTest.java | 48 +++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'test/com') diff --git a/test/com/jogamp/opencl/CLBufferTest.java b/test/com/jogamp/opencl/CLBufferTest.java index d5995903..de71dfe1 100644 --- a/test/com/jogamp/opencl/CLBufferTest.java +++ b/test/com/jogamp/opencl/CLBufferTest.java @@ -30,7 +30,6 @@ package com.jogamp.opencl; import com.jogamp.opencl.CLMemory.Mem; import com.jogamp.opencl.CLMemory.Map; -import com.jogamp.opencl.test.util.MiscUtils; import com.jogamp.opencl.test.util.UITestCase; import com.jogamp.common.nio.Buffers; import com.jogamp.common.util.Bitstream; @@ -38,6 +37,7 @@ import com.jogamp.common.util.Bitstream; import java.io.IOException; import java.nio.Buffer; import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.nio.DoubleBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; @@ -64,6 +64,52 @@ import static com.jogamp.opencl.CLVersion.*; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class CLBufferTest extends UITestCase { + @Test + public void cloneWithLimitedBufferTest() { + final int elements = NUM_ELEMENTS; + final int padding = 312; // Arbitrary number + final CLContext context = CLContext.create(); + + final IntBuffer hostBuffer = ByteBuffer.allocateDirect((elements + padding)*SIZEOF_INT).asIntBuffer(); + hostBuffer.limit(elements); + + final CLBuffer deviceBuffer = context.createBuffer(elements*SIZEOF_INT).cloneWith(hostBuffer); + assertEquals(elements, deviceBuffer.getCLCapacity()); + assertEquals(elements*SIZEOF_INT, deviceBuffer.getNIOSize()); + assertEquals(elements, deviceBuffer.getNIOCapacity()); + + context.release(); + } + + @Test + public void copyLimitedSlicedBuffersTest() { + final int size = 4200*SIZEOF_INT; // Arbitrary number that is a multiple of SIZEOF_INT; + final int padding = 307; // Totally arbitrary number > 0 + final CLContext context = CLContext.create(); + final CLCommandQueue queue = context.getDevices()[0].createCommandQueue(); + + // Make a buffer that is offset relative to the originally allocated position and has a limit that is + // not equal to the capacity to test whether all these attributes are correctly handled. + ByteBuffer hostBuffer = ByteBuffer.allocateDirect(size + padding); + hostBuffer.position(padding/2); // Offset the original buffer + hostBuffer = hostBuffer.slice(); // Slice it to have a new buffer that starts at the offset + hostBuffer.limit(size); + hostBuffer.order(ByteOrder.nativeOrder()); // Necessary for comparisons to work later on. + fillBuffer(hostBuffer, 12345); + + final CLBuffer bufferA = context.createBuffer(size).cloneWith(hostBuffer); + final CLBuffer bufferB = context.createByteBuffer(size); + + queue.putWriteBuffer(bufferA, false) + .putCopyBuffer(bufferA, bufferB, bufferA.getNIOSize()) + .putReadBuffer(bufferB, true).finish(); + + hostBuffer.rewind(); + bufferB.buffer.rewind(); + checkIfEqual(hostBuffer, bufferB.buffer, size/SIZEOF_INT); + context.release(); + } + @Test public void createBufferTest() { -- cgit v1.2.3 From 01f69625995299262c11ae6bcbf345119c7b892f Mon Sep 17 00:00:00 2001 From: Emily Leiviskä Date: Wed, 16 Nov 2016 17:10:00 +0100 Subject: Changing CLBuffer#create to respect the limit instead of capacity on the direct buffer that the CLBuffer is being created for as this more closely represents the users intention about the buffer size. --- src/com/jogamp/opencl/CLBuffer.java | 2 +- test/com/jogamp/opencl/CLBufferTest.java | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'test/com') diff --git a/src/com/jogamp/opencl/CLBuffer.java b/src/com/jogamp/opencl/CLBuffer.java index 065de079..81e036fc 100644 --- a/src/com/jogamp/opencl/CLBuffer.java +++ b/src/com/jogamp/opencl/CLBuffer.java @@ -82,7 +82,7 @@ public class CLBuffer extends CLMemory { final CL binding = context.getPlatform().getCLBinding(); final int[] result = new int[1]; - final int size = Buffers.sizeOfBufferElem(directBuffer) * directBuffer.capacity(); + final int size = Buffers.sizeOfBufferElem(directBuffer) * directBuffer.limit(); final long id = binding.clCreateBuffer(context.ID, flags, size, host_ptr, result, 0); CLException.checkForError(result[0], "can not create cl buffer"); diff --git a/test/com/jogamp/opencl/CLBufferTest.java b/test/com/jogamp/opencl/CLBufferTest.java index de71dfe1..0c6e1d11 100644 --- a/test/com/jogamp/opencl/CLBufferTest.java +++ b/test/com/jogamp/opencl/CLBufferTest.java @@ -64,6 +64,26 @@ import static com.jogamp.opencl.CLVersion.*; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class CLBufferTest extends UITestCase { + @Test + public void createBufferFromLimitedBuffer() { + final int elements = NUM_ELEMENTS; + final int padding = 19*SIZEOF_INT*2; // Totally arbitrary number > 0 divisible by 2*SIZEOF_INT + final CLContext context = CLContext.create(); + + // Make a buffer that is offset relative to the originally allocated position and has a + // limit that is + // not equal to the capacity to test whether all these attributes are correctly handled. + ByteBuffer byteBuffer = ByteBuffer.allocateDirect(elements*SIZEOF_INT + padding); + byteBuffer.position(padding / 2); // Offset the original buffer + IntBuffer intBuffer = byteBuffer.slice().order(ByteOrder.nativeOrder()).asIntBuffer(); // Slice it to have a new buffer that starts at the offset + intBuffer.limit(elements); + + final CLBuffer deviceBuffer = context.createBuffer(intBuffer); + assertEquals(elements, deviceBuffer.getCLCapacity()); + assertEquals(elements * SIZEOF_INT, deviceBuffer.getNIOSize()); + assertEquals(elements, deviceBuffer.getNIOCapacity()); + } + @Test public void cloneWithLimitedBufferTest() { final int elements = NUM_ELEMENTS; -- cgit v1.2.3