29package com.jogamp.opencl;
31import com.jogamp.opencl.CLMemory.Mem;
32import com.jogamp.opencl.CLMemory.Map;
33import com.jogamp.opencl.test.util.UITestCase;
34import com.jogamp.common.nio.Buffers;
35import com.jogamp.common.util.Bitstream;
37import java.io.IOException;
38import java.nio.Buffer;
39import java.nio.ByteBuffer;
40import java.nio.ByteOrder;
41import java.nio.DoubleBuffer;
42import java.nio.FloatBuffer;
43import java.nio.IntBuffer;
44import java.nio.LongBuffer;
45import java.nio.ShortBuffer;
47import java.util.concurrent.CountDownLatch;
48import java.util.concurrent.TimeUnit;
50import org.junit.FixMethodOrder;
52import org.junit.runners.MethodSorters;
54import static org.junit.Assert.*;
55import static java.lang.System.*;
56import static com.jogamp.common.nio.Buffers.*;
57import static com.jogamp.opencl.test.util.MiscUtils.*;
58import static com.jogamp.opencl.util.CLPlatformFilters.*;
59import static com.jogamp.opencl.CLVersion.*;
64@FixMethodOrder(MethodSorters.NAME_ASCENDING)
69 final int elements = NUM_ELEMENTS;
70 final int padding = 19*SIZEOF_INT*2;
76 ByteBuffer byteBuffer = ByteBuffer.allocateDirect(elements*SIZEOF_INT + padding);
77 byteBuffer.position(padding / 2);
78 IntBuffer intBuffer = byteBuffer.slice().order(ByteOrder.nativeOrder()).asIntBuffer();
79 intBuffer.limit(elements);
83 assertEquals(elements * SIZEOF_INT, deviceBuffer.
getNIOSize());
89 final int elements = NUM_ELEMENTS;
90 final int padding = 312;
93 final IntBuffer hostBuffer = ByteBuffer.allocateDirect((elements + padding)*SIZEOF_INT).asIntBuffer();
94 hostBuffer.limit(elements);
98 assertEquals(elements*SIZEOF_INT, deviceBuffer.
getNIOSize());
106 final int size = 4200*SIZEOF_INT;
107 final int padding = 307;
113 ByteBuffer hostBuffer = ByteBuffer.allocateDirect(size + padding);
114 hostBuffer.position(padding/2);
115 hostBuffer = hostBuffer.slice();
116 hostBuffer.limit(size);
117 hostBuffer.order(ByteOrder.nativeOrder());
118 fillBuffer(hostBuffer, 12345);
128 bufferB.buffer.rewind();
129 checkIfEqual(hostBuffer, bufferB.buffer, size/SIZEOF_INT);
136 out.println(
" - - - highLevelTest; create buffer test - - - ");
149 final List<CLMemory<? extends Buffer>> buffers = context.
getMemoryObjects();
150 assertEquals(6, buffers.size());
159 final ByteBuffer anotherNIO = newDirectByteBuffer(2);
169 assertEquals(nio.capacity() * sizeOfBufferElem(nio), buffer.
getCLSize());
173 assertEquals(buffer.ID, clone.ID);
174 assertTrue(clone.
equals(buffer));
175 assertTrue(buffer.
equals(clone));
191 out.println(
" - - - highLevelTest; copy buffer test - - - ");
193 final int elements = NUM_ELEMENTS;
202 fillBuffer(clBufferA.buffer, 12345);
208 .
putCopyBuffer(clBufferA, clBufferB, clBufferA.buffer.capacity())
214 out.println(
"validating computed results...");
215 checkIfEqual(clBufferA.buffer, clBufferB.buffer, elements);
216 out.println(
"results are valid");
223 out.println(
" - - - highLevelTest; host pointer test - - - ");
225 final int elements = NUM_ELEMENTS;
229 final ByteBuffer buffer = Buffers.newDirectByteBuffer(elements*SIZEOF_INT);
231 fillBuffer(buffer, 12345);
237 for(
int i = 0; i < bufferConfig.length; i++) {
239 out.println(
"testing with "+bufferConfig[i] +
" config");
245 queue.
putCopyBuffer(clBufferA, clBufferB, clBufferA.buffer.capacity())
256 out.println(
"validating computed results...");
257 checkIfEqual(clBufferA.buffer, clBufferB.buffer, elements);
258 out.println(
"results are valid");
267 out.println(
" - - - highLevelTest; map buffer test - - - ");
269 final int elements = NUM_ELEMENTS;
270 final int sizeInBytes = elements*SIZEOF_INT;
296 assertEquals(sizeInBytes, mappedBufferA.capacity());
298 fillBuffer(mappedBufferA, 12345);
305 assertEquals(sizeInBytes, mappedBufferB.capacity());
307 out.println(
"validating computed results...");
308 checkIfEqual(mappedBufferA, mappedBufferB, elements);
309 out.println(
"results are valid");
320 out.println(
" - - - subBufferTest - - - ");
322 @SuppressWarnings(
"unchecked")
325 if(platform ==
null) {
326 out.println(
"aborting subBufferTest");
332 final int subelements = 5;
334 final int iMaxAlignment = Bitstream.uint32LongToInt(lMaxAlignment);
335 System.err.println(
"XXX: maxAlignment "+lMaxAlignment+
", 0x"+Long.toHexString(lMaxAlignment)+
", (int)"+iMaxAlignment+
", (int)0x"+Integer.toHexString(iMaxAlignment));
336 if( -1 == iMaxAlignment ) {
337 throw new RuntimeException(
"Cannot handle MaxMemBaseAddrAlign > MAX_INT, has 0x"+Long.toHexString(lMaxAlignment));
349 assertEquals(subelements, subBuffer.getCLSize());
350 assertEquals(iMaxAlignment, subBuffer.
getOffset());
351 assertEquals(iMaxAlignment, subBuffer.
getCLOffset());
352 assertEquals(buffer, subBuffer.
getParent());
366 out.println(
" - - - subBufferTest - - - ");
368 @SuppressWarnings(
"unchecked")
371 if(platform ==
null) {
372 out.println(
"aborting subBufferTest");
378 final int subelements = 5;
380 final int iMaxAlignment = Bitstream.uint32LongToInt(lMaxAlignment);
381 System.err.println(
"XXX: maxAlignment "+lMaxAlignment+
", 0x"+Long.toHexString(lMaxAlignment)+
", (int)"+iMaxAlignment+
", (int)0x"+Integer.toHexString(iMaxAlignment));
382 if( -1 == iMaxAlignment ) {
383 throw new RuntimeException(
"Cannot handle MaxMemBaseAddrAlign > MAX_INT, has 0x"+Long.toHexString(lMaxAlignment));
386 final int floatsPerAlignment = iMaxAlignment / Buffers.SIZEOF_FLOAT;
396 assertEquals(subelements, subBuffer.getBuffer().capacity());
397 assertEquals(floatsPerAlignment, subBuffer.
getOffset());
398 assertEquals(iMaxAlignment, subBuffer.
getCLOffset());
399 assertEquals(buffer, subBuffer.
getParent());
402 assertEquals(subBuffer.getCLCapacity(), subBuffer.getBuffer().capacity());
416 out.println(
" - - - destructorCallbackTest - - - ");
418 @SuppressWarnings(
"unchecked")
421 if(platform ==
null) {
422 out.println(
"aborting destructorCallbackTest");
431 final CountDownLatch countdown =
new CountDownLatch(1);
434 public void memoryDeallocated(
final CLMemory<?> mem) {
435 out.println(
"buffer released");
436 assertEquals(mem, buffer);
437 countdown.countDown();
442 countdown.await(2, TimeUnit.SECONDS);
443 assertEquals(countdown.getCount(), 0);
452 public static void main(
final String[] args)
throws IOException {
454 org.junit.runner.JUnitCore.
main(tstname);
void bufferWithHostPointerTest()
void createBufferFromLimitedBuffer()
void destructorCallbackTest()
void subBufferTest01ByteBuffer()
void writeCopyReadBufferTest()
static void main(final String[] args)
void subBufferTest02FloatBuffer()
void cloneWithLimitedBufferTest()
void copyLimitedSlicedBuffersTest()
OpenCL buffer object wrapping an optional NIO buffer.
CLSubBuffer< B > createSubBuffer(int offset, int size, final Mem... flags)
Creates a sub buffer with the specified region from this buffer.
List< CLSubBuffer< B > > getSubBuffers()
Returns the list of subbuffers.
boolean isSubBuffer()
Returns true if this is a sub buffer.
The command queue is used to queue a set of operations for a specific CLDevice.
CLCommandQueue putUnmapMemory(final CLMemory<?> memory, final Buffer mapped)
Calls {@native clEnqueueUnmapMemObject}.
CLCommandQueue finish()
Calls {@native clFinish}.
CLCommandQueue putCopyBuffer(final CLBuffer<?> src, final CLBuffer<?> dest)
Calls {@native clEnqueueCopyBuffer}.
CLCommandQueue putWriteBuffer(final CLBuffer<?> writeBuffer, final boolean blockingRead)
Calls {@native clEnqueueWriteBuffer}.
ByteBuffer putMapBuffer(final CLBuffer<?> buffer, final CLMemory.Map flag, final boolean blockingMap)
Calls {@native clEnqueueMapBuffer}.
CLCommandQueue putReadBuffer(final CLBuffer<?> readBuffer, final boolean blockingRead)
Calls {@native clEnqueueReadBuffer}.
CLContext is responsible for managing objects such as command-queues, memory, program and kernel obje...
List< CLMemory<? extends Buffer > > getMemoryObjects()
Returns a read only shapshot of all allocated memory objects associated with this context.
CLDevice[] getDevices()
Returns all devices associated with this CLContext.
final CLBuffer< ShortBuffer > createShortBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
static CLContext create()
Creates a context on all available devices (CL_DEVICE_TYPE_ALL).
final CLBuffer< DoubleBuffer > createDoubleBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
final CLBuffer< IntBuffer > createIntBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
final CLBuffer<?> createBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags.
final CLBuffer< ByteBuffer > createByteBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and buffer size in bytes.
final CLBuffer< LongBuffer > createLongBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
final CLBuffer< FloatBuffer > createFloatBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
long getMaxMemBaseAddrAlign()
Returns the maximum CLDevice#getMemBaseAddrAlign() of all devices.
This object represents an OpenCL device.
CLCommandQueue createCommandQueue()
Common superclass for all OpenCL memory types.
int getElementSize()
Returns the size in bytes of a single buffer element.
boolean equals(final Object obj)
Returns the OpenGL buffer type of this shared buffer.
int getCLCapacity()
Returns the size in buffer elements of this memory object.
B getBuffer()
Returns the optional NIO buffer for this memory object.
int getNIOCapacity()
Returns the capacity of the wrapped direct buffer or 0 if no buffer available.
void registerDestructorCallback(final CLMemObjectListener listener)
Registers a callback which will be called by the OpenCL implementation when the memory object is rele...
int getNIOSize()
Returns the size of the wrapped direct buffer in byte or 0 if no buffer available.
long getCLSize()
Returns the size of the allocated OpenCL memory in bytes.
A sub buffer of a CLBuffer.
int getOffset()
Returns the offset of this sub buffer to its parent in buffer elements.
CLBuffer< B > getParent()
Returns the parent buffer this buffer was created from.
boolean isSubBuffer()
Returns true.
int getCLOffset()
Returns the offset of this sub buffer to its parent in bytes.
Enumeration for the type of a device.
Configures the mapping process.
READ
Enum representing CL_MAP_READ.
WRITE
Enum representing CL_MAP_WRITE.
Memory settings for configuring CLMemory.
READ_WRITE
Enum representing CL_MEM_READ_WRITE.
COPY_BUFFER
Enum representing CL_MEM_COPY_HOST_PTR.
USE_BUFFER
Enum representing CL_MEM_USE_HOST_PTR.
READ_ONLY
Enum representing CL_MEM_READ_ONLY.
A callback which is invoked by the OpenCL implementation when the memory object is deleted and its re...