Skip to main content

Module texture

Module texture 

Source
Expand description

One texture upload, for every format and every caller.

pixels ──pad rows to the device's copy alignment──▶ staging buffer
           │
           └──barrier ▸ copy_buffer_to_image ▸ barrier──▶ sampled image + view

§One layer or several, one level or a chain

upload_texture uploads a single-layer D2 image, which is what a sprite sheet and a glyph atlas are. upload_texture_layers uploads several layers of the same size into one D2Array image, which is topic 03 §3.2’s ArrayPages page: one image, one descriptor, and a layer index in the material row selecting between them. upload_texture_mip_layers is the same page with every layer’s mip chain behind it — topic 43’s filtering rung, whose chain crate::mip builds on the host.

All three go through the same body, and it records one copy per level of every layer rather than one covering all of them. That is not caution: a copy region’s extent is a 2D extent on every backend here — crcbl-dx12 refuses a depth_or_layers other than 1 by name, because CopyTextureRegion addresses one subresource — so the layer travels in ImageSubresourceLayers::base_layer, the level in ImageSubresourceLayers::mip, and the buffer offset says where that subresource’s rows start.

§A page that starts empty

upload_cleared_texture is the same copy for an image that is mostly zeroes — an atlas when it is created. The zeroes are written by a clear_buffer on the GPU and copied into every layer, and only the rectangles written over them are staged from the host. See that function for why the difference is the wasm heap’s.

§Several at once

Each function above is a batch of one: it records its copies, submits them and waits. A caller with many uploads to make before its first frame records them all into one UploadBatch instead, and pays for one submission and one wait. The batch also carries UploadBatch::fill, the same staging copy into a device-local buffer, so a renderer’s start-up state rides the same submission as its textures. A caller with work of its own to do before its first frame — crate::ui_pass’s renderer, and crcbl_render’s forward renderer — submits the batch with UploadBatch::submit_deferred instead, which does not wait: the copies run under that work, and the PendingUpload it returns frees the staging once the GPU is past it.

This is a startup path, not a frame path: it records its own barriers and blocks on Device::wait_idle, which is only legal because no graph is being recorded while it runs. See this crate’s docs on the one rule — the two staging uploads (crcbl_render::forward’s cube and this) are its named exceptions. Its barriers name only the image it creates, so a caller between two frames — ForwardRenderer::replace_page reloading a page — touches nothing a frame in flight reads, and the wait means no frame can bind the image before its copy has landed. A deferred submit has no wait, and leans on queue order instead: see PendingUpload for why that holds for a frame on the same queue.

§Why it is not in ui_pass

It was, and it was R8_UNORM only, with the glyph atlas’s labels baked in. The interesting part is that the old code was right by coincidence: it computed a row pitch from width, called it row_texels, and handed it to BufferImageCopy::buffer_row_length — which really is in texels, and really did equal the byte count, because R8Unorm is one byte per texel. For Rgba8Unorm the two differ by four, so the pitch here is computed in bytes and converted back to texels exactly once, at the copy.

Structs§

ClearedTextureDesc
The image upload_cleared_texture creates, and what goes on it.
PendingUpload
An UploadBatch submitted by submit_deferred and not yet known to have run: what it still owns, and the timeline value that says when it may go.
TexturePatch
Texels written over an upload_cleared_texture image as it is created: a width by height rectangle of layer layer, its top-left texel at x, y.
UploadBatch
Startup uploads recorded into one command buffer and submitted with one wait.
UploadedTexture
A texture that has been uploaded: the image, and the view callers bind.

Functions§

require_frame_queue
Refuses any queue but device’s QueueKind::Graphics queue for an upload UploadBatch::submit_deferred will not wait for: what names the caller, as “a UI pass” or “a forward renderer’s view”.
stage_region
A tightly packed region of format texels staged for an in-frame copy: a host-visible TRANSFER_SRC buffer holding pixels with its rows padded to the device’s copy alignment, and the row pitch in texels a BufferImageCopy reading it names.
upload_cleared_texture
Creates a sampled colour image whose every texel is zero, with patches written over it, and returns it with a full view.
upload_texture
Uploads a 2D colour texture via a staging-buffer copy, and returns the image and a view onto it.
upload_texture_layers
Uploads several equally-sized layers into one D2Array colour image, and returns it with an array view onto every layer.
upload_texture_mip_layers
upload_texture_layers with a mip chain behind every layer: one D2Array image whose level count is the chain’s length, and an array view onto every level of every layer.