Skip to content

Memory and slot allocators

What it does

RI places every buffer and texture at an offset you choose in a Memory you allocated, and you pick every descriptor slot. Two helpers do the bookkeeping for you:

  • Memory allocator (<RI/Helpers/MemoryAllocator.hpp>): manages Memory blocks for each MemoryKind (64 MiB per block by default, or a dedicated block sized for any request whose aligned size is larger) and places resources into them using first-fit allocation. allocate takes a MemoryKind and MemoryRequirements and returns an Allocation; createBuffer and createTexture (which always uses MemoryKind::eGPU) query the requirements, allocate a range, and create the resource in one call.
  • Slot allocator (<RI/Helpers/SlotAllocator.hpp>): hands out descriptor heap slots in the range first through first + count - 1. Any slots outside that range remain yours to manage directly.

Both allocators defer freeing until the GPU is finished: release and releaseSlot take the TimelinePoint of the last submission that may use the range or slot, and neither allocator reuses it until that point is reached on the queue. In release, the lastUse parameter defaults to {} (timeline point 0, which is always reached), freeing the range immediately.

MemoryAllocator creates blocks without passing MemoryRequirements, so each block uses the default native memory type for its MemoryKind. If a resource's MemoryRequirements exclude that default type (for example, on Vulkan, drivers such as RADV may allow resources like indirect scratch buffers only in specific native types), allocate a dedicated block for it with RI::allocateMemory(device, kind, size, &requirements).

When an allocation reuses a range that previously held another resource, Allocation::needsDiscard is set to true. If you place a texture in that allocation, its contents are undefined, so call RI::cmdDiscard on it before its first use.

The memory allocator stores a pointer to the Device it was created with, so keep that Device alive at the same address until you call destroyMemoryAllocator. Similarly, the slot allocator stores a pointer to the Queue passed to createSlotAllocator.

Example

RI::Helpers::MemoryAllocator allocator = RI::Helpers::createMemoryAllocator(device);
RI::Helpers::Allocation bufferAllocation;
RI::Buffer buffer = RI::Helpers::createBuffer(
    allocator, RI::MemoryKind::eGPU,
    {.size = 1 << 20, .usage = RI::BufferUsage::eReadWrite | RI::BufferUsage::eCopyDst}, bufferAllocation);
RI::Helpers::Allocation textureAllocation;
RI::Texture texture = RI::Helpers::createTexture(allocator,
                                                 {.width = 256,
                                                  .height = 256,
                                                  .format = RI::Format::eRGBA8Unorm,
                                                  .usage = RI::TextureUsage::eSampled | RI::TextureUsage::eCopySrc |
                                                           RI::TextureUsage::eCopyDst},
                                                 textureAllocation);
// When textureAllocation.needsDiscard is set, the range held another
// resource before: cmdDiscard the texture before its first use (P69).
// Slots 16 .. 1023 of the resource heap; 0 .. 15 stay yours.
RI::Helpers::SlotAllocator slots = RI::Helpers::createSlotAllocator(device.graphicsQueue, 16, 1008);
const uint32 slot = RI::Helpers::allocateSlot(slots);
// ... write a descriptor to `slot`, record and submit work that reads it ...
// Free the slot once the last frame that may read it is done.
RI::Helpers::releaseSlot(slots, slot, lastFrame);

How to roll your own

  • Memory: Call RI::allocateMemory once per block, query each resource's size and alignment with RI::bufferRequirements or RI::textureRequirements, and place each resource with RI::createBuffer or RI::createTexture at a free, aligned offset. Keep a free list per block, and hold released ranges aside until RI::isReached(queue, point) returns true.
  • Slots: Maintain a free list of integer slot indices, and hold released slots aside until RI::isReached(queue, point) returns true before handing them out again.