Class GPUMemoryPool<T extends GPUMemoryPool.InternalMemory<D>,D>
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDo not forget override theGPUMemory.meta()if you need.static interfacestatic interface -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final GPUPoolBehavior<T,D> protected Lockprotected ByteBufferprotected intprotected booleanprotected ReadWriteLockprotected booleanprotected booleanprotected booleanprotected longprotected final AtomicIntegerprotected longprotected longprotected longprotected booleanprotected boolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void_allocateBuffer(long size) protected voidprotected void_copyRangeBufferOverlap(long srcAddress, long dstAddress, long size) protected void_eraseMemory(D meta) protected void_expandBuffer(long reqSize) protected void_initBuffer(D meta, long size) protected voidprotected T_makeMemory(GPUMemoryPool.MemoryBuilder<T, D> builder, D meta, long address, long size, int index) protected void_mergeFreeBlock(T memory) protected voidprotected T_scanEndsFreeBlock(long foundSize) protected boolean_sizeOverRange(long size) longcompact()voiddestroy()booleanEven if memory was free, the operation is safe.intintlonglonglongFor read or write on pool's OpenGL buffer object.longvoidinit()Initializes the environment; the actual vRAM for the buffer object is formally allocated only on the first malloc call.booleanbooleanbooleanbooleanprotected LockbooleanBad for performance, so recommend to allocation the large enough memory block before all.
-
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
-
mem
-
memFree
-
memRef
-
clientLock
-
gpuLock
-
clientMapping
-
-
Constructor Details
-
GPUMemoryPool
-
-
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,
0if never have call malloc with target, or pool was unsupported.Without mutex.
-
getPoolBehavior
-
getBufferSpace
public long getBufferSpace() -
getBufferRightEdge
public long getBufferRightEdge() -
getBufferTotal
public long getBufferTotal() -
getBufferReference
public int getBufferReference() -
getClientLock
-
getGPULock
For read or write on pool's OpenGL buffer object. -
poolGPULock
-
isPoolRequireCompact
public boolean isPoolRequireCompact() -
getLastCompactTimestampNano
public long getLastCompactTimestampNano() -
getMappingBuffer
- Returns:
- only valid when
isPersistentMapping()returnstrue, 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
-
_allocateBuffer
protected void _allocateBuffer(long size) -
_initBuffer
-
_cleanupClientMem
protected void _cleanupClientMem() -
_copyRangeBufferOverlap
protected void _copyRangeBufferOverlap(long srcAddress, long dstAddress, long size) -
_expandBuffer
protected void _expandBuffer(long reqSize) -
_scanEndsFreeBlock
-
_mergeFreeBlock
-
_sizeOverRange
protected boolean _sizeOverRange(long size) -
malloc
- Returns:
nullwhen allocation failed.
-
realloc
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:
falsewhen re-allocation failed.
-
split
- Parameters:
newSize- cannot be equal to the size of the old memory.
-
free
Even if memory was free, the operation is safe.- Returns:
falsewhen still have reference after call.
-
compact
public long compact()- Returns:
- times elapsed during defragmentation in nanosecond.
-