Expand description
Render Bundles
A render bundle is a prerecorded sequence of commands that can be replayed on a
command encoder with a single call. A single bundle can replayed any number of
times, on different encoders. Constructing a render bundle lets wgpu validate
and analyze its commands up front, so that replaying a bundle can be more
efficient than simply re-recording its commands each time.
Not all commands are available in bundles; for example, a render bundle may not
contain a [RenderCommand::SetViewport] command.
Most of wgpu’s backend graphics APIs have something like bundles. For example,
Vulkan calls them “secondary command buffers”, and Metal calls them “indirect
command buffers”. Although we plan to take advantage of these platform features
at some point in the future, for now wgpu’s implementation of render bundles
does not use them: at the hal level, wgpu render bundles just replay the
commands.
§Render Bundle Isolation
One important property of render bundles is that the draw calls in a render
bundle depend solely on the pipeline and state established within the render
bundle itself. A draw call in a bundle will never use a vertex buffer, say, that
was set in the RenderPass before executing the bundle. We call this property
‘isolation’, in that a render bundle is somewhat isolated from the passes that
use it.
Render passes are also isolated from the effects of bundles. After executing a render bundle, a render pass’s pipeline, bind groups, and vertex and index buffers are are unset, so the bundle cannot affect later draw calls in the pass.
A render pass is isolated from a bundle’s effects on immediate data
values. When encoding a render bundle, calls to set_immediates snapshot the immediate data
content at encoding time, and the immediate values cannot be changed after finish.
Before and after executing each individual bundle, all required immediate slots are cleared/reset,
therefore immediate data must be set again.
§Render Bundle Lifecycle
To create a render bundle:
-
Create a
RenderBundleEncoderby callingDevice::create_render_bundle_encoder. -
Record commands in the
RenderBundleEncoderusing methods onRenderBundleEncoder. -
Call
RenderBundleEncoder::finish, which analyzes and cleans up the command stream and returns aRenderBundle. -
Then, any number of times, call
RenderPass::execute_bundlesto execute the bundle as part of some render pass.
§Implementation
The most complex part of render bundles is the “finish” step, mostly implemented
in RenderBundleEncoder::finish. This consumes the commands stored in the
encoder’s [BasePass], while validating everything, tracking the state,
dropping redundant or unnecessary commands, and presenting the results as a new
RenderBundle. It doesn’t actually execute any commands.
This step also enforces the ‘isolation’ property mentioned above: every draw call is checked to ensure that the resources it uses on were established since the last time the pipeline was set. This means the bundle can be executed verbatim without any state tracking.
§Execution
When the bundle is used in an actual render pass, RenderBundle::execute is
called. It goes through the commands and issues them into the native command
buffer. Thanks to isolation, it doesn’t track any bind group invalidations or
index format changes.
!
Structs§
- Index
State 🔒 - A render bundle’s current index buffer state.
- Render
Bundle - cbindgen:ignore
- Render
Bundle Encoder - Render
Bundle Encoder Descriptor - Describes a
RenderBundleEncoder. - Render
Bundle Error - Error encountered when finishing recording a render bundle.
- Render
Bundle 🔒State - State 🔒
- The state of a single vertex buffer slot during render bundle encoding.
Enums§
- Create
Render Bundle Error - Error type returned from
RenderBundleEncoder::newif the sample count is invalid. - Execution
Error - Error type returned from
RenderBundleEncoder::newif the sample count is invalid. - Render
Bundle Error Inner - Error encountered when finishing recording a render bundle.
Functions§
- draw 🔒
- draw_
indexed 🔒 - draw_
mesh_ 🔒tasks - multi_
draw_ 🔒indirect - set_
bind_ 🔒group - set_
immediates 🔒 - set_
index_ 🔒buffer - set_
pipeline 🔒 - set_
vertex_ 🔒buffer - validate_
render_ 🔒bundle_ encoder_ descriptor - Validate a render bundle descriptor.