Typed commands¶
What it does¶
The core RI:: commands that take raw data—cmdSetRootData
and cmdWriteSmallData—accept a byte span
(std::span<const std::byte>). In validation builds,
their size rules are checked at run time. <RI/Helpers/TypedCommands.hpp> adds typed overloads
that take a value directly and check those size and alignment rules at compile time, with
zero run-time cost:
| Helper | Accepts a type T that satisfies |
|---|---|
RI::Helpers::cmdSetRootData(cmd, value) |
RootDataValue: trivially copyable, not a pointer or std::span, and sizeof(T) is a multiple of 4 and at most kMaxRootDataSize (256) |
RI::Helpers::cmdWriteSmallData(cmd, buffer, offset, value) |
SmallWriteValue: a RootDataValue whose sizeof(T) is at most kMaxSmallWriteSize (64) bytes |
Pointers and std::span types are rejected by both concepts: even though they are trivially
copyable, their bytes represent an address (or a pointer and size), not the underlying data.
TypedCommands.hpp also brings the std::span<const std::byte> overloads into RI::Helpers
with using declarations, so code that writes using namespace RI::Helpers; or qualifies calls
with RI::Helpers:: can still pass a byte span.
Each helper is a one-line wrapper: it views the value's bytes with
std::as_bytes(std::span(&value, 1)) and forwards that span to the corresponding RI::
function. Using std::as_bytes is the standard C++ way to view an object's underlying bytes
without a reinterpret_cast.
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
}
RI::Helpers::cmdSetRootData(cmd, Root{.inputSlot = 0, .outputSlot = 1, .count = kCount});
RI::Helpers::cmdWriteSmallData(cmd, flags, 16, std::array<uint32, 4>{1, 0, 0, 1});
// RI::Helpers::cmdWriteSmallData(cmd, flags, 0, std::array<uint32, 17>{}); // does not compile: 68 bytes
What stays a run-time check¶
In validation builds, the following checks still happen at run time because they depend on run-time state:
- Matching the root data's size against the bound pipeline's
pipeline.rootDataSize, because the pipeline is only known at run time. - Checking that a small write's
offsetis a multiple of 4, that[offset, offset + sizeof(T))fits in the buffer, that the buffer hasBufferUsage::eCopyDst, and that the call is outside a rendering section.
How to roll your own¶
Pass std::as_bytes(std::span(&value, 1)) to RI::cmdSetRootData or RI::cmdWriteSmallData,
and add whatever compile-time constraints your project needs on the value type.