XII Release 0.1.0
Loading...
Searching...
No Matches
xiiGPUResourcePool Class Reference

Manages a pool of reusable GPU resources such as buffers, textures, and samplers. More...

#include <GPUResourcePool.h>

Classes

struct  BufferHandleWithAge
 
struct  SamplerHandleWithAge
 
struct  TextureHandleWithAge
 

Public Member Functions

 xiiGPUResourcePool ()
 Constructs a GPU resource pool with default garbage collection thresholds.
 
 ~xiiGPUResourcePool ()
 Destroys the resource pool and releases all remaining resources.
 
xiiSharedPtr< xiiGALBufferGetBuffer (const xiiGALBufferCreationDescription &description)
 Retrieves a buffer from the pool or allocates a new one if no suitable match exists.
 
void ReturnBuffer (xiiSharedPtr< xiiGALBuffer > pBuffer)
 Returns a buffer to the pool for potential reuse.
 
xiiSharedPtr< xiiGALTextureGetRenderTarget (xiiUInt32 uiWidth, xiiUInt32 uiHeight, xiiEnum< xiiGALResourceFormat > format, xiiEnum< xiiGALMSAASampleCount > sampleCount, xiiUInt32 uiSliceColunt, bool bIsArray)
 Retrieves a render target texture with simplified input parameters.
 
xiiSharedPtr< xiiGALTextureGetTexture (const xiiGALTextureCreationDescription &description)
 Retrieves a texture from the pool or creates a new one with the provided description.
 
void ReturnTexture (xiiSharedPtr< xiiGALTexture > pTexture)
 Returns a texture to the pool for reuse.
 
xiiSharedPtr< xiiGALSamplerGetSampler (const xiiGALSamplerCreationDescription &description)
 Retrieves a GPU sampler from the pool or allocates one if needed.
 
void ReturnSampler (xiiSharedPtr< xiiGALSampler > pSampler)
 Returns a sampler to the pool for reuse.
 
void ReleaseStaleResources (xiiUInt32 uiMinimumAge)
 Releases resources that have not been used for at least uiMinimumAge frames.
 

Static Public Member Functions

static xiiGPUResourcePoolGetDefaultInstance ()
 Returns the global default GPU resource pool instance, if any.
 
static void SetDefaultInstance (xiiGPUResourcePool *pDefaultInstance)
 Sets the global default GPU resource pool instance.
 

Protected Member Functions

void CheckAndPotentiallyReleaseStaleResources ()
 Internal method that checks thresholds and releases stale resources if needed.
 
void UpdateMemoryStats () const
 Updates internal memory statistics for debugging or visualization.
 
void GALDeviceEventHandler (const xiiGALDeviceEvent &e)
 Handles events triggered by the underlying GAL device (e.g. EndFrame).
 

Protected Attributes

xiiEventSubscriptionID m_GALDeviceEventSubscriptionID
 
xiiUInt64 m_uiMemoryThresholdForGC = 256 * 1024 * 1024
 Memory limit (in bytes) that triggers resource garbage collection.
 
xiiUInt64 m_uiCurrentlyAllocatedMemory = 0
 Total memory currently allocated through this pool.
 
xiiUInt16 m_uiNumAllocationsThresholdForGC = 128
 Allocation count threshold that triggers garbage collection.
 
xiiUInt16 m_uiNumAllocationsSinceLastGC = 0
 Tracks how many allocations have occurred since the last GC.
 
xiiUInt16 m_uiFramesThresholdSinceLastGC = 60
 Number of frames to wait before rechecking unused resources for GC.
 
xiiUInt16 m_uiFramesSinceLastGC = 0
 Number of frames that have passed since the last GC.
 
xiiMap< xiiUInt32, xiiDynamicArray< TextureHandleWithAge > > m_AvailableTextures
 
xiiSet< xiiSharedPtr< xiiGALTexture > > m_TexturesInUse
 
xiiMap< xiiUInt32, xiiDynamicArray< BufferHandleWithAge > > m_AvailableBuffers
 
xiiSet< xiiSharedPtr< xiiGALBuffer > > m_BuffersInUse
 
xiiMap< xiiUInt32, xiiDynamicArray< SamplerHandleWithAge > > m_AvailableSamplers
 
xiiSet< xiiSharedPtr< xiiGALSampler > > m_SamplersInUse
 
xiiMutex m_Lock
 
xiiSharedPtr< xiiGALDevicem_pDevice
 

Detailed Description

Manages a pool of reusable GPU resources such as buffers, textures, and samplers.

The GPU resource pool helps reduce allocation overhead and memory fragmentation by reusing previously created GPU resources based on matching creation parameters. It is thread-safe and supports garbage collection of unused resources based on usage age and memory thresholds.

Resources can be retrieved using GetBuffer(), GetTexture(), GetSampler(), or GetRenderTarget(), and must be returned to the pool using the corresponding Return*() methods. The pool will automatically release stale resources that have not been used for a defined number of frames, or when memory or allocation count thresholds are exceeded.

A global default instance can be accessed and configured using GetDefaultInstance() and SetDefaultInstance().

See also
xiiGALBufferCreationDescription, xiiGALTextureCreationDescription, xiiGALSamplerCreationDescription

Member Function Documentation

◆ GetRenderTarget()

xiiSharedPtr< xiiGALTexture > xiiGPUResourcePool::GetRenderTarget ( xiiUInt32 uiWidth,
xiiUInt32 uiHeight,
xiiEnum< xiiGALResourceFormat > format,
xiiEnum< xiiGALMSAASampleCount > sampleCount,
xiiUInt32 uiSliceColunt,
bool bIsArray )

Retrieves a render target texture with simplified input parameters.

Internally constructs a full texture creation description and either reuses or allocates a new texture.

◆ ReleaseStaleResources()

void xiiGPUResourcePool::ReleaseStaleResources ( xiiUInt32 uiMinimumAge)

Releases resources that have not been used for at least uiMinimumAge frames.

This can be triggered automatically based on thresholds or manually, for example after editor window resizes or level transitions.

Parameters
uiMinimumAgeHow many frames at least the resource needs to have been unused before it will be stale.

The documentation for this class was generated from the following files: