pub struct UploadBatch<'a> { /* private fields */ }Expand description
Startup uploads recorded into one command buffer and submitted with one wait.
Each texture added is created at once, its staging written at once, and its
copies queued behind the ones before it; each buffer filled
is the caller’s already, and its copy joins them. Nothing reaches the device
until submit. One submission rather than one per upload
is the point: on Mesa’s ANV the first submit on a fresh device costs several
milliseconds and every later round trip through Device::wait_idle costs
more than the copies of an empty page, and a renderer’s start-up — this
crate’s crate::ui_pass, and crcbl_render’s forward renderer with its
views — makes many small uploads before its first frame.
The textures are the caller’s from the moment they are returned, and
neither they nor a filled buffer may be read until submit has returned
Ok — or, after submit_deferred, by anything
but later work on the batch’s queue until its PendingUpload retires.
Binding one into a bind group before then is fine: a descriptor names a
resource, it does not read it. The staging and zero buffers are the
batch’s, released when it drops: after the wait if it was submitted, and
before any submission if an add or the caller failed first. A deferred
submit hands them to its PendingUpload instead.
§No encoder until the submit
The barriers and copies are kept as data and recorded into an encoder that
submit creates, records, finishes and submits in one go —
never one held open while the caller does other work. A batch outlives
whole build steps that create, finish and submit encoders of their own, and
Device::create_command_encoder says at most one encoder per device may
be recording at a time: crcbl-webgpu streams every command into the one
encoder its browser side last created, so an encoder opened here at
new lost its copies to the first encoder a caller made after
it.
§A failed build abandons the batch
A caller building several objects into one batch destroys what it had
created when a later step fails, and the batch still holds copies into
those objects. Submitting them would be a copy into a destroyed image, so a
caller that rolls back something it added calls abandon,
and every later add and the submit itself refuse. Dropping the batch
unsubmitted is the other way out, and the ordinary one: a build that fails
returns its error past the batch, and nothing is submitted.
Implementations§
Source§impl<'a> UploadBatch<'a>
impl<'a> UploadBatch<'a>
Sourcepub fn new(
device: &'a dyn Device,
queue: Handle<Queue>,
label: &str,
) -> UploadBatch<'a>
pub fn new( device: &'a dyn Device, queue: Handle<Queue>, label: &str, ) -> UploadBatch<'a>
An empty batch; label is the stem of its encoder’s name.
Sourcepub fn queue(&self) -> Handle<Queue>
pub fn queue(&self) -> Handle<Queue>
The queue the batch submits to, for a builder that holds the batch and still submits some work of its own.
Sourcepub fn texture(
&mut self,
label: &str,
format: Format,
extent: (u32, u32),
layers: &[&[&[u8]]],
view_type: ImageViewType,
) -> Result<UploadedTexture, HalError>
pub fn texture( &mut self, label: &str, format: Format, extent: (u32, u32), layers: &[&[&[u8]]], view_type: ImageViewType, ) -> Result<UploadedTexture, HalError>
upload_texture_mip_layers, recorded into this batch:
layers[layer][level] at extent, viewed as view_type.
upload_texture’s one image is &[&[pixels]] and
ImageViewType::D2.
§Errors
As upload_texture_mip_layers, before anything is recorded, and
HalError::InvalidDescriptor once the batch is abandoned.
Sourcepub fn cleared_texture(
&mut self,
desc: &ClearedTextureDesc<'_>,
) -> Result<UploadedTexture, HalError>
pub fn cleared_texture( &mut self, desc: &ClearedTextureDesc<'_>, ) -> Result<UploadedTexture, HalError>
upload_cleared_texture, recorded into this batch.
§Errors
As upload_cleared_texture, before anything is recorded, and
HalError::InvalidDescriptor once the batch is abandoned.
Sourcepub fn fill(
&mut self,
label: &str,
buffer: Handle<Buffer>,
contents: &[u8],
final_state: ResourceState,
) -> Result<(), HalError>
pub fn fill( &mut self, label: &str, buffer: Handle<Buffer>, contents: &[u8], final_state: ResourceState, ) -> Result<(), HalError>
Writes contents over the start of buffer once, before any frame
exists, leaving it in final_state once submitted.
For a device-local buffer whose first frame reads something real —
a host write cannot reach one, and a clear cannot say anything but zero
and crcbl-dx12 refuses one outright. buffer must carry
BufferUsage::TRANSFER_DST and must never have held anything: its
contents are discarded rather than transitioned, so a buffer a frame has
already written is the wrong thing to hand this.
final_state is the state the buffer’s first importer declares as its
initial one, so the graph’s first barrier on it names a state the buffer
is really in. label names the staging buffer, "{label} staging".
§Errors
HalError from creating or writing the staging buffer, which is
destroyed again if the write fails, and HalError::InvalidDescriptor
once the batch is abandoned. Nothing is recorded on either.
Sourcepub fn abandon(&mut self)
pub fn abandon(&mut self)
Throws away everything recorded, because the caller is about to destroy
something it added — see the type’s docs. Every later add and
submit refuse; the staging buffers are still released
when the batch drops.
Sourcepub fn submit(self) -> Result<(), HalError>
pub fn submit(self) -> Result<(), HalError>
Records every queued copy into one encoder created here, submits it and
waits for the device, leaving every texture in
ResourceState::ShaderRead and every filled buffer in the state its
fill named.
§Errors
HalError from finishing, submitting or waiting, and
HalError::InvalidDescriptor for an abandoned batch, which submits
nothing. The textures and buffers stay the caller’s to destroy.
Sourcepub fn submit_deferred(self) -> Result<PendingUpload, HalError>
pub fn submit_deferred(self) -> Result<PendingUpload, HalError>
submit without the wait: the copies are submitted
and the staging and zero buffers, the command buffer and a timeline
semaphore the submission signals move into the returned
PendingUpload, which releases them once the GPU is past them.
For a start-up whose first submission is expensive — pikr measured the first one on a fresh device at 7–10 ms on an Intel iGPU under Mesa’s ANV, on 2026-10-29 — and which has other work to do before its first frame: the copies run under it instead of in front of it.
The textures and filled buffers may be read by any work submitted
to the same queue after this returns, with no wait in between; see
PendingUpload for why that holds on every backend. Work on another
queue, or a host read, must wait for PendingUpload::wait first.
§Without timeline semaphores it waits
A device without Features::TIMELINE_SEMAPHORE has nothing that
says when the copies are done short of Device::wait_idle, so this
is submit there, and the PendingUpload it returns
holds nothing. That is the WebGPU backend’s case, and it costs nothing
there: crcbl-webgpu’s wait_idle does not block, because the browser
keeps a destroyed buffer alive for the work already queued against it.
§Errors
As submit, and HalError from creating the
semaphore. On every error nothing is pending and the buffers have been
released: either nothing was submitted, or (on the waiting path) the
wait has already run or failed as submit’s does.