RI::cmdClearTexture, RI::cmdClearBuffer¶
What it does¶
Record texture and buffer clears outside rendering sections:
RI::cmdClearTexture(cmd, view, value)sets every texel ofviewtovalue: it usesvalue.colorfor color textures (pass whole numbers for integer formats) andvalue.depthandvalue.stencilfor depth-stencil textures.RI::cmdClearBuffer(cmd, buffer, value, offset, size)sets every 32-bit word in a buffer range tovalue, interpreted as a raw bit pattern (for example,0x3f800000is1.0f). Omittingoffsetandsizeclears the whole buffer, andsize = kWholeSizeclears fromoffsetto 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¶
// 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
vkCmdClearColorImageorvkCmdClearDepthStencilImagein place for textures resting inGENERAL; otherwise it switches the texture toTRANSFER_DST_OPTIMALand back. For buffers, it callsvkCmdFillBufferwith 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_UINTviews, which may start at any multiple of 4 (whereas a raw view requires 16-byte alignment); a range larger than one view (512 MiB,kMaxBufferViewElementswords) is cleared in pieces.
Rules and errors¶
cmdClearTexturerequiresTextureUsage::eRenderTarget,eDepthStencil, oreStorage. On D3D12, clearing a texture without one of these usages is a programmer error; on Vulkan, the validation layer reports it.cmdClearBufferrequiresBufferUsage::eCopyDst(programmer error). On D3D12, the buffer must also be inMemoryKind::eGPUoreGPUUploadmemory becauseeUploadandeReadbackheaps forbid unordered access; violating this is a programmer error.cmdClearBufferrequiresoffsetandsizeto 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.