RI::bufferRequirements¶
What it does¶
Before you place a buffer in a memory block, you need to know how many bytes it takes and
at what alignment its offset must be. Both depend on the GPU and on the buffer's usage, so
RI asks the driver: RI::bufferRequirements(device, desc) returns them for a BufferDesc
without creating a buffer.
Use the returned MemoryRequirements to size memory blocks and to choose offsets: each buffer's
offset must be a multiple of alignment, and size bytes starting at that offset must fit in
the block.
The returned struct also includes kinds, a bitmask with one bit per
memory kind that can hold the resource, which you can test with
RI::fits. Every buffer fits every memory kind the device supports;
kinds matters mainly for textures (textureRequirements), which
usually fit eGPU only.
Signature¶
// <RI/RI.hpp>, namespace RI
MemoryRequirements bufferRequirements(Device &device, const BufferDesc &desc);
// <RI/RI.hpp>, namespace RI
struct MemoryRequirements {
uint64 size = 0;
uint64 alignment = 0;
uint32 kinds = 0; // bit i set: MemoryKind i can hold the resource
// internal: backend-private state (not part of the API)
};
From the header: Size, alignment and compatible memory kinds of a resource placed in memory (P31, P58 (1)). Buffers fit every available kind; textures usually only eGPU.
| Field | Type | Default | Description |
|---|---|---|---|
size |
uint64 |
0 |
|
alignment |
uint64 |
0 |
|
kinds |
uint32 |
0 |
bit i set: MemoryKind i can hold the resource |
Example¶
// How much memory, at what alignment, a buffer with this description needs.
const RI::BufferDesc dataDesc{
.size = kBytes,
.usage = RI::BufferUsage::eRead | RI::BufferUsage::eReadWrite | RI::BufferUsage::eCopySrc |
RI::BufferUsage::eCopyDst,
};
const RI::MemoryRequirements dataReq = RI::bufferRequirements(device, dataDesc);
// Two such buffers side by side: the second starts at an aligned offset.
const uint64 outputOffset = alignUp(dataReq.size, dataReq.alignment);
with a small helper:
uint64 alignUp(uint64 value, uint64 alignment) { return (value + alignment - 1) / alignment * alignment; }
The same in native code¶
// How much memory, at what alignment, a buffer with this description needs.
const RI::BufferDesc dataDesc{
.size = kBytes,
.usage = RI::BufferUsage::eRead | RI::BufferUsage::eReadWrite | RI::BufferUsage::eCopySrc |
RI::BufferUsage::eCopyDst,
};
const RI::MemoryRequirements dataReq = RI::bufferRequirements(device, dataDesc);
// Two such buffers side by side: the second starts at an aligned offset.
const uint64 outputOffset = alignUp(dataReq.size, dataReq.alignment);
// Size, alignment and allowed memory types of a buffer, without creating it (Vulkan 1.3).
const VkBufferCreateInfo dataInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = kBytes,
.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
const VkDeviceBufferMemoryRequirements query{.sType = VK_STRUCTURE_TYPE_DEVICE_BUFFER_MEMORY_REQUIREMENTS,
.pCreateInfo = &dataInfo};
VkMemoryRequirements2 dataReq{.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2};
vkGetDeviceBufferMemoryRequirements(device, &query, &dataReq);
const VkMemoryRequirements &req = dataReq.memoryRequirements; // size, alignment, memoryTypeBits
const VkDeviceSize outputOffset = alignUp(req.size, req.alignment);
What to watch for: before Vulkan 1.3, you had to create the buffer first and call
vkGetBufferMemoryRequirements instead of vkGetDeviceBufferMemoryRequirements. The
returned memoryTypeBits field lists the native memory types the buffer accepts.
// Size and alignment of a placed buffer (64 KiB alignment for buffers).
const D3D12_RESOURCE_DESC1 dataDesc = bufferDesc(kBytes, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
const D3D12_RESOURCE_ALLOCATION_INFO info = device->GetResourceAllocationInfo2(0, 1, &dataDesc, nullptr);
const UINT64 outputOffset = (info.SizeInBytes + info.Alignment - 1) / info.Alignment * info.Alignment;
What to watch for: placed buffers are aligned to 64 KiB
(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT), and their size is rounded up to a multiple
of 64 KiB.
Rules and errors¶
- Lay out a memory block using the returned
MemoryRequirements::sizeandalignment, neverdesc.size: the next buffer's offset is the previous offset plus the previous buffer's returnedsize, rounded up to the next buffer'salignment.createBufferchecks placement againstMemoryRequirements::size, notdesc.size. - Typical values: on D3D12,
sizeandalignmentare multiples of 64 KiB for every buffer. On Vulkan, RI raisesalignmentto at least the device'sbufferImageGranularityand roundssizeup to a multiple of it so that buffers and textures can share a block without further care (for example, 1 byte on AMD or 1 KiB on NVIDIA, where a 72-byte buffer takes 1024 bytes). - In validation builds,
createBufferchecks placement: an offset that is not a multiple ofalignment, or a buffer whoseMemoryRequirements::sizedoes not fit in the block, is a programmer error (for example,createBuffer: placing the buffer in memory: offset 100 is not a multiple of the required alignment 256). - On Vulkan, passing
BufferUsage::eAccelerationStructureInputwithoutDeviceLimits::rayTracingorBufferUsage::eIndirectScratchwithoutDeviceLimits::indirectRootDatareports an unsupported error.