Skip to content

RI::cmdDiscard

What it does

RI::cmdDiscard(cmd, view) declares that the texture contents in view are no longer needed and resets that range: existing texels become undefined, and any hidden compression metadata is reinitialized.

It is required before the first use of a texture whose memory previously held another resource (aliasing: see Textures and views). Stale compression metadata can corrupt rendering, and a load operation (eClear, eDontCare) does not reset it or count as a discard. You may also call cmdDiscard on a texture whose contents you no longer need before rendering to it with eDontCare.

A discard is a full barrier for the view: it waits for all earlier work and blocks all later work. Record cmdDiscard once, right before the texture's first use, outside any rendering section.

Signature

// <RI/RI.hpp>, namespace RI
void cmdDiscard(CommandList &cmd, const TextureView &view);

From the header: Required before first use of a texture whose memory held another resource (P69). A full-scope barrier.

Example

// Two textures over the same bytes: use one, then the other, never both at once.
RI::Texture blurH = RI::createTexture(device, memory, scratchOffset, scratchDesc);
RI::Texture blurV = RI::createTexture(device, memory, scratchOffset, scratchDesc);
// 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 same in native code

// 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);
// An image that takes over memory another resource used: a transition from
// UNDEFINED drops the old contents and resets compression metadata. Required
// before its first use, even if the next pass clears it.
transition(cmd, color, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
           VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
           VK_ACCESS_2_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT,
           VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT);

What to watch for: transitioning from VK_IMAGE_LAYOUT_UNDEFINED (UNDEFINED) acts as the discard, allowing the driver to drop old contents and reinitialize compression metadata. Transitioning from the image's previous layout would preserve stale metadata.

// A texture that takes over memory another resource used: a barrier from
// UNDEFINED with the DISCARD flag initializes its compression metadata.
// Required before its first use, even if the next pass clears it.
textureBarrier(cmd.Get(), color.Get(), D3D12_BARRIER_LAYOUT_UNDEFINED, D3D12_BARRIER_LAYOUT_RENDER_TARGET,
               D3D12_BARRIER_SYNC_ALL, D3D12_BARRIER_ACCESS_NO_ACCESS, D3D12_BARRIER_SYNC_RENDER_TARGET,
               D3D12_BARRIER_ACCESS_RENDER_TARGET, D3D12_TEXTURE_BARRIER_FLAG_DISCARD);

What to watch for: with D3D12 enhanced barriers, a discard is a texture barrier from D3D12_BARRIER_LAYOUT_UNDEFINED with D3D12_TEXTURE_BARRIER_FLAG_DISCARD. Aliased render targets and depth-stencil textures must be initialized this way (or by a clear or copy) before use, or rendering may be corrupt on some GPUs.

Rules and errors

  • In validation builds, RI tracks resource placements in each memory block. If a texture is placed over bytes that another resource (alive or destroyed) previously occupied, and you use it as a rendering attachment, clear it, or write to it as a copy or resolve destination without calling cmdDiscard first, RI reports a programmer error: <call>: the texture was placed over memory that held another resource; record cmdDiscard before its first use (P69; load operations do not count) (where <call> is the command name, such as cmdBeginRendering). Shader accesses through bindless descriptors cannot be tracked and are not checked; release builds perform no placement checks.
  • Do not call cmdDiscard inside a rendering section; doing so is a programmer error.
  • Buffers need no discard.
  • A new texture placed in fresh memory does not need cmdDiscard, because createTexture resets it once at creation.
  • cmdDiscard discards only the subresource range covered by view, so pass the whole-texture view (texture.view) when a texture takes over aliased memory.