Class GPUMemoryPool<T extends GPUMemoryPool.InternalMemory<D>,D>

java.lang.Object
org.boxutil.units.standard.GPUMemoryPool<T,D>

public class GPUMemoryPool<T extends GPUMemoryPool.InternalMemory<D>,D> extends Object
A memory pool for managing OpenGL Buffer Objects, supporting multi‑threaded calls via two different granularities of locks.

By default, to facilitate shader read/write access at given positions and to improve rendering performance, memory blocks are always allocated in a compact layout from free space, without padding for misaligned byte offsets, during defragmentation, larger blocks are moved to higher addresses.

REQUIRES:

Only OpenGL 1.5 or GLWrapper.Buffer.valid() as the minimum supported version.

For optimal performance, it is recommended that the implementation requires at least OpenGL 3.1 or GLWrapper.Buffer.valid_CopyBuffer()

CAUTION:

The thread to which this memory pool belongs MUST have an active OpenGL context, and that context MUST be shared with the main thread.

Any exceptions thrown by some methods of this pool MUST not be caught and ignored or left unhandled; otherwise, fatal error may occur, so such defects MUST be resolved during modding.

  • Field Details

    • immutableBuffer

      protected boolean immutableBuffer
    • persistentMapping

      protected boolean persistentMapping
    • init

      protected boolean init
    • gpuInit

      protected boolean gpuInit
    • invalid

      protected boolean invalid
    • requireCompact

      protected boolean requireCompact
    • glID

      protected int glID
    • memSpace

      protected long memSpace
    • memRightEdge

      protected long memRightEdge
    • memTotal

      protected long memTotal
    • lastCompactTime

      protected long lastCompactTime
    • behavior

      protected final GPUPoolBehavior<T extends GPUMemoryPool.InternalMemory<D>,D> behavior
    • mem

      protected List<T extends GPUMemoryPool.InternalMemory<D>> mem
    • memFree

      protected List<T extends GPUMemoryPool.InternalMemory<D>> memFree
    • memRef

      protected final AtomicInteger memRef
    • clientLock

      protected Lock clientLock
    • gpuLock

      protected ReadWriteLock gpuLock
    • clientMapping

      protected ByteBuffer clientMapping
  • Constructor Details

  • Method Details

    • init

      public void init()
      Initializes the environment; the actual vRAM for the buffer object is formally allocated only on the first malloc call.
    • destroy

      public void destroy()
    • isInvalid

      public boolean isInvalid()
    • isImmutableBuffer

      public boolean isImmutableBuffer()
    • isPersistentMapping

      public boolean isPersistentMapping()
    • getBufferID

      public int getBufferID()
      Returns:
      the buffer object name, 0 if never have call malloc with target, or pool was unsupported.

      Without mutex.

    • getPoolBehavior

      public GPUPoolBehavior<T,D> getPoolBehavior()
    • getBufferSpace

      public long getBufferSpace()
    • getBufferRightEdge

      public long getBufferRightEdge()
    • getBufferTotal

      public long getBufferTotal()
    • getBufferReference

      public int getBufferReference()
    • getClientLock

      public Lock getClientLock()
    • getGPULock

      public Lock getGPULock()
      For read or write on pool's OpenGL buffer object.
    • poolGPULock

      protected Lock poolGPULock()
    • isPoolRequireCompact

      public boolean isPoolRequireCompact()
    • getLastCompactTimestampNano

      public long getLastCompactTimestampNano()
    • getMappingBuffer

      @Nullable public ByteBuffer getMappingBuffer()
      Returns:
      only valid when isPersistentMapping() returns true, included reserved space if it has.
    • _runtimeBufferIDCheck

      protected void _runtimeBufferIDCheck()
    • _makeMemory

      protected T _makeMemory(GPUMemoryPool.MemoryBuilder<T,D> builder, D meta, long address, long size, int index)
    • _invalidateBuffer

      protected void _invalidateBuffer()
    • _eraseMemory

      protected void _eraseMemory(D meta)
    • _allocateBuffer

      protected void _allocateBuffer(long size)
    • _initBuffer

      protected void _initBuffer(D meta, long size)
    • _cleanupClientMem

      protected void _cleanupClientMem()
    • _copyRangeBufferOverlap

      protected void _copyRangeBufferOverlap(long srcAddress, long dstAddress, long size)
    • _expandBuffer

      protected void _expandBuffer(long reqSize)
    • _scanEndsFreeBlock

      protected T _scanEndsFreeBlock(long foundSize)
    • _mergeFreeBlock

      protected void _mergeFreeBlock(T memory)
    • _sizeOverRange

      protected boolean _sizeOverRange(long size)
    • malloc

      public T malloc(@Nullable D meta, long size)
      Returns:
      null when allocation failed.
    • realloc

      public boolean realloc(@NotNull T memory, long newSize)
      Bad for performance, so recommend to allocation the large enough memory block before all.
      Parameters:
      memory - different from C/C++, will not change the object pointer, only remapping and then copy data if needed.
      Returns:
      false when re-allocation failed.
    • split

      public T split(@NotNull T memory, long newSize, boolean fromStartOrEnd)
      Parameters:
      newSize - cannot be equal to the size of the old memory.
    • free

      public boolean free(@NotNull T memory)
      Even if memory was free, the operation is safe.
      Returns:
      false when still have reference after call.
    • share

      public T share(@NotNull T memory)
    • compact

      public long compact()
      Returns:
      times elapsed during defragmentation in nanosecond.