29package com.jogamp.opencl;
31import com.jogamp.common.nio.Buffers;
32import com.jogamp.common.nio.PointerBuffer;
33import com.jogamp.opencl.llb.CL;
34import com.jogamp.opencl.llb.impl.CLMemObjectDestructorCallback;
35import java.nio.Buffer;
36import java.nio.IntBuffer;
37import java.util.ArrayList;
38import java.util.EnumSet;
41import static com.jogamp.opencl.CLException.*;
42import static com.jogamp.opencl.llb.CL.*;
49public abstract class CLMemory <B
extends Buffer> extends CLObjectResource {
59 private final CL binding;
67 this.buffer = directBuffer;
75 private void initElementSize() {
76 this.elementSize = (buffer==
null) ? 1 : Buffers.sizeOfBufferElem(buffer);
87 return (flags & CL_MEM_COPY_HOST_PTR) != 0
88 || (flags & CL_MEM_USE_HOST_PTR) != 0;
92 final PointerBuffer pb = PointerBuffer.allocateDirect(1);
94 final int ret = binding.
clGetMemObjectInfo(
id, CL_MEM_SIZE, pb.elementSize(), pb.getBuffer(),
null);
95 checkForError(ret,
"can not obtain buffer info");
110 public void memoryDeallocated(
final long memObjID) {
123 if(this.buffer !=
null && buffer !=
null && this.buffer.getClass() != buffer.getClass()) {
124 throw new IllegalArgumentException(
125 "expected a Buffer of class " + this.buffer.getClass()
126 +
" but got " + buffer.getClass());
128 this.buffer = buffer;
148 return buffer.limit();
195 final IntBuffer value = Buffers.newDirectIntBuffer(1);
197 checkForError(ret,
"can not obtain buffer map count.");
226 context.onMemoryReleased(
this);
227 if(ret != CL_SUCCESS) {
228 throw newException(ret,
"can not release "+
this);
256 public boolean equals(
final Object obj) {
260 if (getClass() != obj.getClass()) {
264 if (this.
ID != other.
ID) {
267 if (this.
context != other.
context && (
this.context ==
null || !
this.context.equals(other.
context))) {
276 hash = 83 * hash + (int) (this.
ID ^ (this.
ID >>> 32));
277 hash = 83 * hash + (this.context !=
null ? this.
context.
hashCode() : 0);
283 return getClass().getSimpleName()+
" [id: " +
ID+
" buffer: "+buffer+
"]";
347 private Mem(
final int config) {
348 this.CONFIG = config;
352 switch (bufferFlag) {
353 case CL_MEM_READ_WRITE:
355 case CL_MEM_READ_ONLY:
357 case CL_MEM_WRITE_ONLY:
359 case CL_MEM_USE_HOST_PTR:
361 case(CL_MEM_ALLOC_HOST_PTR):
362 return ALLOCATE_BUFFER;
363 case CL_MEM_COPY_HOST_PTR:
369 public static EnumSet<Mem>
valuesOf(
final int bitfield) {
370 final List<Mem> matching =
new ArrayList<Mem>();
371 final Mem[] values =
Mem.values();
372 for (
final Mem value : values) {
373 if((value.CONFIG & bitfield) != 0)
376 if(matching.isEmpty())
377 return EnumSet.noneOf(
Mem.class);
379 return EnumSet.copyOf(matching);
385 for (
int i = 0; i < flags.length; i++) {
386 clFlags |= flags[i].
CONFIG;
390 clFlags = CL_MEM_READ_WRITE;
427 private Map(
final int flags) {
432 if(flag == WRITE.FLAGS)
434 else if(flag == READ.FLAGS)
436 else if(flag == READ_WRITE.FLAGS)
457 if(type == CL_GL_OBJECT_BUFFER)
458 return GL_OBJECT_BUFFER;
459 else if(type == CL_GL_OBJECT_TEXTURE2D)
460 return GL_OBJECT_TEXTURE2D;
461 else if(type == CL_GL_OBJECT_TEXTURE3D)
462 return GL_OBJECT_TEXTURE3D;
463 else if(type == CL_GL_OBJECT_RENDERBUFFER)
464 return GL_OBJECT_RENDERBUFFER;
CLContext is responsible for managing objects such as command-queues, memory, program and kernel obje...
CL getCL()
Return the low level OpenCL interface.
CLPlatform getPlatform()
Returns the CLPlatform this context is running on.
Common superclass for all OpenCL memory types.
final void initCLCapacity()
int getElementSize()
Returns the size in bytes of a single buffer element.
int getMapCount()
Returns the number of buffer mappings.
boolean equals(final Object obj)
Returns the OpenGL buffer type of this shared buffer.
static CL getCL(final CLContext context)
int getCLCapacity()
Returns the size in buffer elements of this memory object.
boolean isReadWrite()
Returns true if this memory object was created with the Mem#READ_WRITE flag.
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.
static boolean isHostPointerFlag(final int flags)
Returns true if a host pointer must be specified on mem object creation.
boolean isReadOnly()
Returns true if this memory object was created with the Mem#READ_ONLY flag.
CLMemory(final CLContext context, final B directBuffer, final long size, final long id, final int flags)
CLMemory< B > use(final B buffer)
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.
static long getSizeImpl(final CLContext context, final long id)
EnumSet< Mem > getConfig()
Returns the configuration of this memory object.
long getCLSize()
Returns the size of the allocated OpenCL memory in bytes.
void release()
Releases the OpenCL resource.
boolean isWriteOnly()
Returns true if this memory object was created with the Mem#WRITE_ONLY flag.
abstract< T extends Buffer > CLMemory< T > cloneWith(T directBuffer)
Returns a new instance of CLMemory pointing to the same CLResource but using a different Buffer.
final long ID
The OpenCL object handle.
static GLObjectType valueOf(final int type)
Configures the mapping process.
READ
Enum representing CL_MAP_READ.
Map valueOf(final int flag)
final int FLAGS
Value of wrapped OpenCL flag.
WRITE
Enum representing CL_MAP_WRITE.
READ_WRITE
Enum representing CL_MAP_READ | CL_MAP_WRITE.
Memory settings for configuring CLMemory.
static int flagsToInt(final Mem[] flags)
WRITE_ONLY
Enum representing CL_MEM_WRITE_ONLY.
READ_WRITE
Enum representing CL_MEM_READ_WRITE.
ALLOCATE_BUFFER
Enum representing CL_MEM_ALLOC_HOST_PTR.
final int CONFIG
Value of wrapped OpenCL flag.
static EnumSet< Mem > valuesOf(final int bitfield)
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.
static Mem valueOf(final int bufferFlag)
A callback which is invoked by the OpenCL implementation when the memory object is deleted and its re...
void memoryDeallocated(CLMemory<?> mem)
Java bindings to OpenCL, the Open Computing Language.
int clReleaseMemObject(long memobj)
Interface to C language function: cl_int {@native clReleaseMemObject}(cl_mem memobj)
int clSetMemObjectDestructorCallback(long memObjID, CLMemObjectDestructorCallback cb)
Interface to C language function: int32_t {@native clSetMemObjectDestructorCallback}(cl_mem memobj...
int clGetMemObjectInfo(long memobj, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetMemObjectInfo}(cl_mem memobj,...
A callback which is invoked by the OpenCL implementation when the memory object is deleted and its re...