Skip to main content

UploadBatch

Struct UploadBatch 

Source
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>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<'a> Debug for UploadBatch<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Drop for UploadBatch<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for UploadBatch<'a>

§

impl<'a> !UnwindSafe for UploadBatch<'a>

§

impl<'a> Freeze for UploadBatch<'a>

§

impl<'a> Send for UploadBatch<'a>

§

impl<'a> Sync for UploadBatch<'a>

§

impl<'a> Unpin for UploadBatch<'a>

§

impl<'a> UnsafeUnpin for UploadBatch<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> HalThreadSafe for T
where T: Send + Sync,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.