Textures and views¶
What a texture is¶
A texture is GPU memory arranged as a 2D or 3D grid of texels (and optionally multiple mip levels, array layers, or cube faces), in a format such as eRGBA8Unorm or eD32Float. Unlike a buffer, its bytes are stored in a driver-chosen layout and are often compressed in ways shaders do not see. Depending on its usage flags, shaders can sample it with filtering or read and write individual texels, and the GPU can render into it.
In RI, a texture is created like a buffer: you describe it, ask how much memory it needs, and place it in a memory block you allocated:
// A color target that shaders also sample, with an sRGB view format (P74).
const RI::TextureDesc colorDesc{
.width = kWidth,
.height = kHeight,
.format = RI::Format::eRGBA8Unorm,
.usage = RI::TextureUsage::eRenderTarget | RI::TextureUsage::eSampled,
.viewFormats = {RI::Format::eRGBA8Srgb},
};
// A depth-stencil target that shaders also read (soft particles, SSAO, ...).
const RI::TextureDesc depthDesc{.width = kWidth, .height = kHeight, .format = depthFormat, .usage = depthUsage};
// A texture compute shaders write, with a full mip chain.
const RI::TextureDesc storageDesc{
.width = 512,
.height = 512,
.mips = 10,
.format = RI::Format::eRGBA16Float,
.usage = RI::TextureUsage::eStorage | RI::TextureUsage::eSampled,
};
// 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;
RI::Memory memory = RI::allocateMemory(device, RI::MemoryKind::eGPU, scratchOffset + scratchReq.size);
RI::Texture color = RI::createTexture(device, memory, 0, colorDesc);
RI::Texture depth = RI::createTexture(device, memory, depthOffset, depthDesc);
RI::Texture storage = RI::createTexture(device, memory, storageOffset, storageDesc);
// Each texture comes with a view of the whole texture in its own format: color.view.
Call textureRequirements to get the size, alignment, and compatible memory kinds for a texture; in practice, textures usually fit only eGPU memory (on D3D12, eGPU is the only kind allowed).
Usage flags¶
The usage flags declare everything the texture will be used for:
TextureUsage |
Lets you |
|---|---|
eSampled |
sample or read it in shaders through a ReadView (Texture2D<T>) |
eStorage |
read and write it in shaders through a ReadWriteView (RWTexture2D<T>) |
eRenderTarget |
render to it as a color attachment |
eDepthStencil |
use it as a depth (and stencil) attachment |
eCopySrc / eCopyDst |
copy from / to it (copies) |
eReadOnlyDepthSampling |
sample a depth texture during a read-only depth section (requires a depth format with eSampled and eDepthStencil) |
Set every usage flag you need, and no more. Extra flags can disable driver compression, increase the memory size returned by textureRequirements, and change the internal layout the texture rests in between operations (see Image layouts are RI's business). Before creating a texture, you can call formatSupported to check whether the GPU supports a format for your chosen usage flags and sample count.
View formats and compression¶
A texture's texels can be reinterpreted in another format with the same texel size (bytes per texel)—for example, eRGBA8Unorm as eRGBA8Srgb (so shaders decode sRGB on read), or eR32Uint as eR32Float. List those extra formats in viewFormats when you create the texture.
Leave viewFormats empty when you do not need format reinterpretation, which is the default. Drivers can compress textures whose format never changes, and giving an exact viewFormats list lets them keep compression when possible. Creating a view in a format not listed in viewFormats (other than the texture's own format) is a programmer error in every build.
Views¶
A view names part of a texture in one format: a range of mips, a range of layers (or depth slices for 3D textures), and an aspect. Every operation that uses a texture takes a TextureView: rendering attachments, descriptors, copies, clears, and discards.
Every texture comes with a ready-made view: texture.view, which covers the whole texture in its own format. Create additional views once when you need them with createView, and destroy them with destroyView before destroying the texture:
// The same texels read as sRGB (listed in viewFormats).
RI::TextureView colorSrgb = RI::createView(device, color, {.format = RI::Format::eRGBA8Srgb});
// Shaders read depth or stencil, never both: a depth-only view.
RI::TextureView depthOnly = RI::createView(device, depth, {.aspect = RI::Aspect::eDepth});
// One mip, e.g. to write mip 1 while sampling mip 0.
RI::TextureView storageMip1 = RI::createView(device, storage, {.baseMip = 1, .mipCount = 1});
// storageMip1.width == 256: a view reports the size of its first mip.
A TextureView reports its format, width and height (of its first mip), and samples.
Aspects: color, depth, stencil¶
A depth-stencil texture holds two aspects: depth and stencil. By default (Aspect::eDefault), a view covers both, which is what a depth-stencil attachment needs. Shaders can read only one aspect at a time, so a shader-readable view of a depth-stencil texture must specify Aspect::eDepth or Aspect::eStencil. A depth-only format (eD16Unorm, eD32Float) has only a depth aspect, so its default view (texture.view) can be sampled directly.
In shaders¶
Texture views go into the same descriptor heap as buffer views, through .texture (writeDescriptors):
// Texture views go in heap slots like buffer views, through `.texture`.
RI::DescriptorHeap heap = RI::createDescriptorHeap(device, {.resourceCount = 16});
RI::writeDescriptors(device, heap,
{{.slot = 0, .descriptor = RI::ReadView{.texture = colorSrgb}}, // Texture2D<float4>
{.slot = 1, .descriptor = RI::ReadView{.texture = depthOnly}}, // Texture2D<float>
{.slot = 2, .descriptor = RI::ReadWriteView{.texture = storageMip1}}}); // RWTexture2D<float4>
The shader indexes the descriptor heap with the slot, just like for buffers:
Texture2D<float4> albedo = ResourceDescriptorHeap[root.albedoSlot];
RWTexture2D<float4> target = ResourceDescriptorHeap[root.targetSlot];
Shaders read and write textures without layout transitions or declared resource states (you still use cmdBarrier to synchronize writes before reads).
Discard and aliasing¶
Two textures (or a texture and a buffer) can be placed over the same bytes of memory if they are never used at the same time—for example, two scratch textures for a blur, or the G-buffer of one pass and the bloom chain of the next. This is aliasing, and it saves memory.
A texture that takes over memory previously used by another resource starts with garbage in both its texels and its hidden compression metadata, which the GPU trusts. A load operation (eClear, eDontCare) does not reset that metadata. Before the first use of such a texture, record cmdDiscard:
// blurH and blurV share memory. Before each one's first use in a pass,
// discard it: its contents (and hidden compression data) are reset (P69).
RI::cmdDiscard(cmd, blurH.view);
RI::cmdBeginRendering(cmd, {.colors = {{.view = blurH.view, .load = RI::LoadOp::eDontCare}}});
// ... draws ...
RI::cmdEndRendering(cmd);
// blurH's contents are no longer needed: blurV takes over the memory.
RI::cmdDiscard(cmd, blurV.view);
RI::cmdBeginRendering(cmd, {.colors = {{.view = blurV.view, .load = RI::LoadOp::eDontCare}}});
// ... draws ...
RI::cmdEndRendering(cmd);
The rule is simple: if any other resource was ever placed over any byte of the texture's memory, discard the texture before using it, in the command list where it takes over the memory. Because cmdDiscard drops the texture's contents, never discard a texture whose texels you still need. Buffers do not need to be discarded.
A texture placed in fresh memory does not need cmdDiscard, because createTexture resets its metadata once when creating it.
Validation check. In validation builds, RI tracks every placement in each memory block. When a texture placed over bytes that another resource occupied (alive or destroyed) is used as an attachment, cleared, or written as a copy destination before being discarded, RI reports a programmer error:
cmdBeginRendering: the texture was placed over memory that held another resource; record cmdDiscard before its first use (P69; load operations do not count). Shader uses are invisible under bindless descriptors and are not checked. Release builds track nothing.
Formats¶
| Kind | Formats |
|---|---|
| 8-bit color | eR8Unorm, eRG8Unorm, eRGBA8Unorm, eRGBA8Srgb, eBGRA8Unorm, eBGRA8Srgb |
| packed color | eRGB10A2Unorm, eR11G11B10Float |
| float color | eR16Float, eRG16Float, eRGBA16Float, eR32Float, eRG32Float, eRGBA32Float |
| integer color | eR32Uint, eRG32Uint, eRGBA32Uint |
| depth | eD16Unorm, eD32Float |
| depth and stencil | eD24UnormS8Uint (not on AMD Vulkan), eD32FloatS8Uint |
RI::isDepthFormat, RI::hasStencil, and RI::bytesPerTexel (which returns 0 for depth and depth-stencil formats; use RI::copyBytesPerTexel for copy layouts) let you query a format's properties.