Skip to content

RI::cmdClearTexture, RI::cmdClearBuffer

What it does

Record texture and buffer clears outside rendering sections:

  • RI::cmdClearTexture(cmd, view, value) sets every texel of view to value: it uses value.color for color textures (pass whole numbers for integer formats) and value.depth and value.stencil for depth-stencil textures.
  • RI::cmdClearBuffer(cmd, buffer, value, offset, size) sets every 32-bit word in a buffer range to value, interpreted as a raw bit pattern (for example, 0x3f800000 is 1.0f). Omitting offset and size clears the whole buffer, and size = kWholeSize clears from offset to the end.

To clear an attachment at the start of a rendering section, use its load operation (LoadOp::eClear) instead: it is free or nearly so.

For barriers, clears count as copies: follow a clear with cmdBarrier(cmd, Stage::eCopy, ...) before reading what it wrote, and precede it with cmdBarrier(cmd, ..., Stage::eCopy) if earlier work used the resource.

Signature

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

From the header: Clears outside rendering sections (P65); they count as Stage::eCopy. Buffers need eCopyDst.

// <RI/RI.hpp>, namespace RI
void cmdClearBuffer(CommandList &cmd, const Buffer &buffer, uint32 value, uint64 offset = 0, uint64 size = kWholeSize);

From the header: offset and size are multiples of 4 (P107); kWholeSize clears to the end.

Example

// Clears outside rendering sections (P65). They count as copies for barriers.
RI::cmdClearTexture(cmd, storage.view, {.color = {0.0f, 0.0f, 0.0f, 0.0f}});
RI::cmdClearBuffer(cmd, counters, 0); // every 32-bit word set to 0
RI::cmdBarrier(cmd, RI::Stage::eCopy, RI::Stage::eCompute);

The same in native code

// Clears outside rendering sections (P65). They count as copies for barriers.
RI::cmdClearTexture(cmd, storage.view, {.color = {0.0f, 0.0f, 0.0f, 0.0f}});
RI::cmdClearBuffer(cmd, counters, 0); // every 32-bit word set to 0
RI::cmdBarrier(cmd, RI::Stage::eCopy, RI::Stage::eCompute);
// Outside a section, a color image is cleared as a transfer: TRANSFER_DST
// layout (or GENERAL), then back to where shaders read it.
transition(cmd, color, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
           VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_NONE,
           VK_PIPELINE_STAGE_2_CLEAR_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT);
const VkClearColorValue black{{0.0f, 0.0f, 0.0f, 1.0f}};
const VkImageSubresourceRange whole{.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
                                    .baseMipLevel = 0,
                                    .levelCount = VK_REMAINING_MIP_LEVELS,
                                    .baseArrayLayer = 0,
                                    .layerCount = VK_REMAINING_ARRAY_LAYERS};
vkCmdClearColorImage(cmd, color, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &black, 1, &whole);
transition(cmd, color, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
           VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_PIPELINE_STAGE_2_CLEAR_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT,
           VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
           VK_ACCESS_2_SHADER_SAMPLED_READ_BIT);
// Buffers: vkCmdFillBuffer(cmd, buffer, 0, VK_WHOLE_SIZE, value), a transfer write too.

What to watch for: vkCmdClearColorImage and vkCmdClearDepthStencilImage are transfer commands: the image needs TRANSFER_DST usage and the TRANSFER_DST_OPTIMAL or GENERAL layout. vkCmdFillBuffer needs TRANSFER_DST too, and an offset and size that are multiples of 4.

// A render target is cleared in the RENDER_TARGET layout, even outside a pass.
textureBarrier(cmd.Get(), color.Get(), D3D12_BARRIER_LAYOUT_SHADER_RESOURCE, D3D12_BARRIER_LAYOUT_RENDER_TARGET,
               D3D12_BARRIER_SYNC_ALL, D3D12_BARRIER_ACCESS_NO_ACCESS, D3D12_BARRIER_SYNC_RENDER_TARGET,
               D3D12_BARRIER_ACCESS_RENDER_TARGET);
const float black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
cmd->ClearRenderTargetView(rtv, black, 0, nullptr);
// Storage textures and buffers: ClearUnorderedAccessViewFloat/Uint, which
// need the view both in a shader-visible heap (GPU handle) and in a
// non-shader-visible one (CPU handle).

What to watch for: render targets and depth textures are cleared with ClearRenderTargetView or ClearDepthStencilView in their attachment layout. Other textures and buffers are cleared with ClearUnorderedAccessViewFloat or ClearUnorderedAccessViewUint, which require an unordered-access view in both a shader-visible heap and a CPU-only heap, and the resource must allow unordered access.

How RI does it

  • Vulkan: RI calls vkCmdClearColorImage or vkCmdClearDepthStencilImage in place for textures resting in GENERAL; otherwise it switches the texture to TRANSFER_DST_OPTIMAL and back. For buffers, it calls vkCmdFillBuffer with the requested range.
  • D3D12: render targets and depth textures are cleared inside a switch to their attachment layout. Storage textures and buffers use an unordered-access clear with descriptors in RI's own small heap: RI sets that heap for the clear, then sets your heaps (and root signatures) again, so your next commands see your heap and no slot of yours is used. Buffers are cleared through typed R32_UINT views, which may start at any multiple of 4 (whereas a raw view requires 16-byte alignment); a range larger than one view (512 MiB, kMaxBufferViewElements words) is cleared in pieces.

Rules and errors

  • cmdClearTexture requires TextureUsage::eRenderTarget, eDepthStencil, or eStorage. On D3D12, clearing a texture without one of these usages is a programmer error; on Vulkan, the validation layer reports it.
  • cmdClearBuffer requires BufferUsage::eCopyDst (programmer error). On D3D12, the buffer must also be in MemoryKind::eGPU or eGPUUpload memory because eUpload and eReadback heaps forbid unordered access; violating this is a programmer error.
  • cmdClearBuffer requires offset and size to be multiples of 4, with a non-empty range that fits in the buffer (checked in validation builds, programmer error).
  • Neither command may be called inside a rendering section (programmer error, checked in validation builds on Vulkan and in all builds on D3D12).
  • A texture placed over memory that another resource previously used must be discarded before it is cleared (reported as a programmer error in validation builds).
  • On D3D12, a storage-texture or buffer clear switches descriptor heaps twice, which may cost a short stall on some GPUs.