RI::textureRequirements, RI::fits¶
What it does¶
Before you place a texture in a memory block, you need to know how many bytes it takes,
what alignment its offset requires, and which memory kinds can hold it. All three
depend on the GPU and on the whole TextureDesc (shape, dimensions, mip levels, samples,
format, usage, and view formats). RI::textureRequirements(device, desc) queries the driver
and returns these values without creating a texture.
The returned MemoryRequirements struct has three public fields:
sizeandalignment: place the texture at an offset that is a multiple ofalignment, with at leastsizebytes available from that offset.kinds: a bitmask with one bit per memory kind that can hold the texture. Test a kind withRI::fits(requirements, kind).
In practice, textures usually fit eGPU memory only (on D3D12, eGPU is the only kind
allowed). Allocate eGPU blocks for textures, and upload texels through a staging buffer in
eUpload memory (copies, or the transfer helpers).
Signature¶
// <RI/RI.hpp>, namespace RI
MemoryRequirements textureRequirements(Device &device, const TextureDesc &desc);
// <RI/RI.hpp>, namespace RI
constexpr bool fits(const MemoryRequirements &requirements, MemoryKind kind);
// <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¶
Three textures side by side in one block:
// Size, alignment and the memory kinds that can hold each texture.
const RI::MemoryRequirements colorReq = RI::textureRequirements(device, colorDesc);
const RI::MemoryRequirements depthReq = RI::textureRequirements(device, depthDesc);
const RI::MemoryRequirements storageReq = RI::textureRequirements(device, storageDesc);
if (!RI::fits(colorReq, RI::MemoryKind::eGPU)) {
return 1; // textures live in eGPU memory on every GPU RI supports
}
const uint64 depthOffset = alignUp(colorReq.size, depthReq.alignment);
const uint64 storageOffset = alignUp(depthOffset + depthReq.size, storageReq.alignment);
const uint64 storageEnd = storageOffset + storageReq.size;
The same in native code¶
// Size, alignment and the memory kinds that can hold each texture.
const RI::MemoryRequirements colorReq = RI::textureRequirements(device, colorDesc);
const RI::MemoryRequirements depthReq = RI::textureRequirements(device, depthDesc);
const RI::MemoryRequirements storageReq = RI::textureRequirements(device, storageDesc);
if (!RI::fits(colorReq, RI::MemoryKind::eGPU)) {
return 1; // textures live in eGPU memory on every GPU RI supports
}
const uint64 depthOffset = alignUp(colorReq.size, depthReq.alignment);
const uint64 storageOffset = alignUp(depthOffset + depthReq.size, storageReq.alignment);
const uint64 storageEnd = storageOffset + storageReq.size;
// Size, alignment and allowed memory types of an image, without creating it (Vulkan 1.3).
// (With a format list, chain it here too: requirements may depend on it.)
const VkDeviceImageMemoryRequirements query{.sType = VK_STRUCTURE_TYPE_DEVICE_IMAGE_MEMORY_REQUIREMENTS,
.pCreateInfo = &colorInfo};
VkMemoryRequirements2 colorReq{.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2};
vkGetDeviceImageMemoryRequirements(device, &query, &colorReq);
const VkDeviceImageMemoryRequirements depthQuery{.sType = VK_STRUCTURE_TYPE_DEVICE_IMAGE_MEMORY_REQUIREMENTS,
.pCreateInfo = &depthInfo};
VkMemoryRequirements2 depthReq{.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2};
vkGetDeviceImageMemoryRequirements(device, &depthQuery, &depthReq);
const VkDeviceSize depthOffset = alignUp(colorReq.memoryRequirements.size, depthReq.memoryRequirements.alignment);
// memoryTypeBits: the memory types that can hold the image (usually device-local only).
const uint32_t allowedTypes =
colorReq.memoryRequirements.memoryTypeBits & depthReq.memoryRequirements.memoryTypeBits;
What to watch for: memoryTypeBits lists the native memory types that accept the
image; RI maps them to memory kinds. In native Vulkan, when buffers and optimal-tiling
images share one memory block, Vulkan requires at least bufferImageGranularity bytes
between them (a device limit).
// A sampled depth texture is created TYPELESS: its depth view and its
// shader view use different formats of the same bits.
const D3D12_RESOURCE_DESC1 colorDesc =
textureDesc(kWidth, kHeight, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
const D3D12_RESOURCE_DESC1 depthDesc =
textureDesc(kWidth, kHeight, DXGI_FORMAT_R32_TYPELESS, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
D3D12_RESOURCE_ALLOCATION_INFO1 parts[2] = {};
const D3D12_RESOURCE_DESC1 descs[2] = {colorDesc, depthDesc};
// Both at once: the total, and each one's offset in a shared block.
const D3D12_RESOURCE_ALLOCATION_INFO total = device->GetResourceAllocationInfo2(0, 2, descs, parts);
What to watch for: placed textures are aligned to 64 KiB, and MSAA textures are aligned to
4 MiB. Passing several resource descriptions to GetResourceAllocationInfo2 at once returns
both the total size and each resource's offset in a shared block. Textures can live in
DEFAULT heaps only.
Rules and errors¶
- Calling
createTexturewith a memory block whose kind is not inkindsreports an unsupported error in all builds: for example,createTexture: this eRGBA8Unorm texture cannot be placed in this memory kind on '<GPU>' (check textureRequirements(...).kinds; allocateMemory(device, kind, size, &requirements) picks a type that fits)on Vulkan, orcreateTexture: textures need eGPU memory on D3D12on D3D12. - In validation builds, placing a texture at an offset that is not a multiple of
alignment, or in a block withoutsizebytes free fromoffset, reports a programmer error (for example,createTexture: placing the texture in memory: offset ... is not a multiple of the required alignment ...). - On Vulkan, RI raises
alignmentto at least the device'sbufferImageGranularityand roundssizeup to a multiple of it, so buffers and textures can share oneeGPUblock when placed usingMemoryRequirements::sizeandalignment. - MSAA textures require 4 MiB alignment on D3D12; RI allocates
eGPUmemory blocks on D3D12 with 4 MiB heap alignment so any texture fits.