Skip to content

RI::cmdWriteSmallData

What it does

RI::cmdWriteSmallData(cmd, buffer, offset, data) writes a few values into buffer starting at byte offset offset, in order with the commands recorded around it. For example, you can use it to reset a counter at the start of a frame, set a flag, or write a handful of indices. The bytes are recorded into the command list itself, so no upload buffer or shader is involved.

Use this command for small data only: data.size() must be a non-zero multiple of 4 and at most kMaxSmallWriteSize (64) bytes per call. Larger data goes through an upload buffer and cmdCopyBuffer.

The core RI::cmdWriteSmallData function takes a byte span (std::span<const std::byte>). The typed helper, RI::Helpers::cmdWriteSmallData(cmd, buffer, offset, value), accepts any type satisfying SmallWriteValue (trivially copyable, not a pointer or std::span, and sizeof(T) a multiple of 4 up to 64 bytes) and checks those size and alignment rules at compile time.

Signature

// <RI/RI.hpp>, namespace RI
void cmdWriteSmallData(CommandList &cmd, const Buffer &buffer, uint64 offset, std::span<const std::byte> data);

From the header: A few values only (counters, flags, indices), recorded into the command list itself; larger data goes through an upload buffer and cmdCopyBuffer (P107). offset and data.size() are multiples of 4, data.size() at most kMaxSmallWriteSize. Outside rendering sections; counts as Stage::eCopy; the buffer needs eCopyDst. Typed values: RI::Helpers::cmdWriteSmallData.

Example

// Resets a 64-bit counter at the start of the frame: a few bytes, so they go
// into the command list itself; no upload buffer, no shader.
void resetCounter(RI::CommandList &cmd, const RI::Buffer &counters, uint64 start) {
    RI::Helpers::cmdWriteSmallData(cmd, counters, 0, start); // checked at compile time: 8 bytes
    // The same bytes through core, for data built at run time:
    // RI::cmdWriteSmallData(cmd, counters, 0, std::as_bytes(std::span(&start, 1)));
    RI::cmdBarrier(cmd, RI::Stage::eCopy, RI::Stage::eCompute); // a small write counts as a copy
}

The same in native code

// Resets a 64-bit counter at the start of the frame: a few bytes, so they go
// into the command list itself; no upload buffer, no shader.
void resetCounter(RI::CommandList &cmd, const RI::Buffer &counters, uint64 start) {
    RI::Helpers::cmdWriteSmallData(cmd, counters, 0, start); // checked at compile time: 8 bytes
    // The same bytes through core, for data built at run time:
    // RI::cmdWriteSmallData(cmd, counters, 0, std::as_bytes(std::span(&start, 1)));
    RI::cmdBarrier(cmd, RI::Stage::eCopy, RI::Stage::eCompute); // a small write counts as a copy
}
void resetCounter(VkCommandBuffer commands, VkBuffer counters, uint64_t start) {
    // A transfer command: the buffer needs TRANSFER_DST; at most 65536 bytes,
    // offset and size multiples of 4; not inside a render pass.
    vkCmdUpdateBuffer(commands, counters, 0, sizeof(start), &start);
    const VkMemoryBarrier2 barrier{.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2,
                                   .srcStageMask = VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT,
                                   .srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT,
                                   .dstStageMask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
                                   .dstAccessMask = VK_ACCESS_2_SHADER_READ_BIT | VK_ACCESS_2_SHADER_WRITE_BIT};
    const VkDependencyInfo dependency{.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
                                      .memoryBarrierCount = 1,
                                      .pMemoryBarriers = &barrier};
    vkCmdPipelineBarrier2(commands, &dependency);
}

What to watch for: vkCmdUpdateBuffer is a transfer command that must be called outside render passes, with an offset and size that are multiples of 4 and at most 65,536 bytes. Vulkan copies the data into the command buffer when the command is recorded.

void resetCounter(ID3D12GraphicsCommandList2 *list, ID3D12Resource *counters, UINT64 start) {
    // One parameter per 32-bit value, each with its own GPU address.
    const D3D12_GPU_VIRTUAL_ADDRESS address = counters->GetGPUVirtualAddress();
    const D3D12_WRITEBUFFERIMMEDIATE_PARAMETER words[2] = {
        {address, static_cast<UINT>(start)},
        {address + 4, static_cast<UINT>(start >> 32)},
    };
    list->WriteBufferImmediate(2, words, nullptr); // nullptr: the default mode
    // Then a barrier from the copy to the shaders that read the counter.
}

What to watch for: WriteBufferImmediate takes one parameter per 32-bit value, each with its own GPU address, so the command list grows with every value. D3D12 allows it inside a render pass, but RI forbids cmdWriteSmallData inside a rendering section so the rule is the same on both backends.

How RI does it

  • Vulkan: calls vkCmdUpdateBuffer once with the byte span.
  • D3D12: calls WriteBufferImmediate once, passing one parameter (destination GPU address and 32-bit value) per 4-byte word.

Rules and errors

In validation builds, each of these is a programmer error:

  • buffer must be created with BufferUsage::eCopyDst.
  • offset and data.size() must be multiples of 4, and data.size() must not be zero.
  • data.size() must be at most kMaxSmallWriteSize (64 bytes); exceeding it reports: cmdWriteSmallData: 68 bytes is more than kMaxSmallWriteSize (64); write larger data through an upload buffer and cmdCopyBuffer (P107).
  • The byte range [offset, offset + data.size()) must fit inside buffer.
  • cmdWriteSmallData must not be called inside a rendering section.

The typed helper turns the size and alignment rules on the value into compile-time constraints; BufferUsage::eCopyDst, offset, the buffer range, and the rendering-section rule are still checked at run time in validation builds.

For barriers, a small write counts as a copy (Stage::eCopy), like a buffer clear: insert a cmdBarrier from Stage::eCopy after the write before shaders read what it wrote (and into Stage::eCopy before the write if earlier GPU work wrote to the same range).