pub struct SSRenderer { /* private fields */ }Expand description
Everything whose useful lifetime is the device, not one player and not one render target.
Pipelines, bind group layouts, the sampler, the white fallback and the atlas
textures are all valid for any target on the device that created them, so
exactly one of these serves every crate::SSScene and every
crate::SSPlayer drawing on that device. The host owns it and lends it,
the same way it lends Device and Queue.
Nothing here is format-specific: the pipeline key carries the target formats, so targets that disagree share one renderer.
Implementations§
Source§impl SSRenderer
impl SSRenderer
Sourcepub fn new(device: &Device) -> Self
pub fn new(device: &Device) -> Self
Build the device-scoped resources. Cheap: no pipeline is compiled until a frame asks for one, so this is the shader modules, the bind group layouts, a sampler and a 1x1 texture.
Takes no target formats — those ride in the pipeline key, which is what lets one renderer serve every target on the device.
Sourcepub fn pipeline_count(&self) -> usize
pub fn pipeline_count(&self) -> usize
How many pipelines have been compiled so far — that is, how many distinct combinations the animation has actually asked for.
Sourcepub fn set_texture(
&mut self,
device: &Device,
hash: u32,
view: &TextureView,
(width, height): (u32, u32),
alpha: AlphaMode,
)
pub fn set_texture( &mut self, device: &Device, hash: u32, view: &TextureView, (width, height): (u32, u32), alpha: AlphaMode, )
Register a texture the supplier owns: the renderer builds a
bind group around view and nothing else.
This is the channel an engine integration uses. Bevy hands over the
GpuImage’s view for its Handle<Image>, keeps the lifecycle it already
has (hot reload, unload, format and mip policy), and no atlas is uploaded
twice. alpha says which form the pixels are in — engine textures are
normally AlphaMode::Straight, ours are premultiplied — and costs no
pipeline, because it rides in the texture’s own bind group.
wgpu’s handles are refcounted internally, so the clone kept here cannot
dangle; it does hold the texture alive, so a supplier that has finished
with a hash calls SSRenderer::forget_texture.
Device loss is the supplier’s to repair on this path: the
renderer keeps no pixels it could rebuild from, so after
SSRenderer::restore_device the supplier re-creates its texture and
calls this again. Taking over a hash that was previously uploaded drops the
retained copy, so the two paths cannot both claim to own one entry.
Sourcepub fn forget_texture(&mut self, hash: u32)
pub fn forget_texture(&mut self, hash: u32)
Drop a texture registration (and any retained pixels for it).
The counterpart of SSRenderer::set_texture: until this is called the
renderer’s clone of the view keeps the supplier’s texture alive, which for
an engine means an unload that does not actually free anything. Draws that
still reference hash fall back to the 1x1 white texture.
Sourcepub fn texture_alpha_mode(&self, hash: u32) -> Option<AlphaMode>
pub fn texture_alpha_mode(&self, hash: u32) -> Option<AlphaMode>
Which form SSRenderer::set_texture / SSRenderer::upload_texture
registered hash in, or None if it is not registered.
Sourcepub fn upload_texture(
&mut self,
device: &Device,
queue: &Queue,
hash: u32,
rgba: &[u8],
(width, height): (u32, u32),
retention: Retention,
)
pub fn upload_texture( &mut self, device: &Device, queue: &Queue, hash: u32, rgba: &[u8], (width, height): (u32, u32), retention: Retention, )
Provide a texture the renderer creates. hash is the key the player
draws the image under: crate::SSPlayer::texture_key’s answer for a
pack’s atlas, or the key an override returned. This is the convenience path used by the
viewer, the web host and the C API; an engine integration wants
SSRenderer::set_texture instead.
rgba is straight-alpha (as decoded from PNG); it is premultiplied here so
the whole pipeline stays premultiplied (matching WebGL’s
UNPACK_PREMULTIPLY_ALPHA_WEBGL). The premul shader recovers straight RGB
before applying PartsColor.
This copies rgba so it can premultiply without touching the caller’s
buffer. A caller that can give the buffer up — anything that just decoded it
— wants SSRenderer::upload_texture_owned instead, which premultiplies in
place and copies nothing.
A texture the device cannot hold — empty, wider or taller than its
max_texture_dimension_2d, or with rgba not width * height * 4 bytes —
is not uploaded, and whatever hash held before stays: it is reported on
stderr instead of becoming a wgpu validation error, whose default handler
panics.
Sourcepub fn upload_texture_owned(
&mut self,
device: &Device,
queue: &Queue,
hash: u32,
rgba: Vec<u8>,
(width, height): (u32, u32),
retention: Retention,
)
pub fn upload_texture_owned( &mut self, device: &Device, queue: &Queue, hash: u32, rgba: Vec<u8>, (width, height): (u32, u32), retention: Retention, )
SSRenderer::upload_texture, taking the pixels rather than borrowing them.
The whole point is that no full-size copy happens: rgba is premultiplied in
place, written to the texture, and — for Retention::Retain — retained as
itself. A 4096² atlas is 64 MB, so the copy the borrowing form makes is
worth a second entry point.
Sourcepub fn texture_digest(&self, hash: u32) -> Option<u32>
pub fn texture_digest(&self, hash: u32) -> Option<u32>
What the bytes behind a player-loaded atlas hashed to, or None when this
key holds nothing or holds something a host registered.
The load walk asks so it can tell “somebody already uploaded exactly this”
from “this key holds a different picture now” — see texture_digests.
Sourcepub fn texture_size(&self, hash: u32) -> Option<(f32, f32)>
pub fn texture_size(&self, hash: u32) -> Option<(f32, f32)>
Pixel size of a registered atlas, for normalising the runtime’s UVs (which arrive in texture pixel space).
Sourcepub fn texture_count(&self) -> usize
pub fn texture_count(&self) -> usize
How many distinct texture hashes are registered.
Sourcepub fn texture_upload_count(&self) -> usize
pub fn texture_upload_count(&self) -> usize
How many textures SSRenderer::upload_texture has created since this
renderer was built (device restores included).
A diagnostic, and the only way to tell “N players of one pack uploaded the
atlas once between them” from “they each uploaded it over the last one” —
SSRenderer::texture_count reads the same either way.
Sourcepub fn forget_texture_sources(&mut self)
pub fn forget_texture_sources(&mut self)
Drop the retained copies of Retention::Retain uploads.
Saves the duplicated pixels when the host keeps its own and would rather
re-supply them itself; after this, SSRenderer::restore_device restores
nothing and every texture must be uploaded again before drawing.
Sourcepub fn restore_device(&mut self, device: &Device, queue: &Queue)
pub fn restore_device(&mut self, device: &Device, queue: &Queue)
Rebuild every GPU resource against a freshly created device after the
previous one was lost.
Because this is device-scoped, one call covers every scene and player on the device.
Textures uploaded with Retention::Retain come back from their retained
pixels. Retention::Reproducible ones do not: their supplier re-registers
them (for embedded packs that is SSPlayer::restore_textures, which
re-decodes from the .ssab it already owns). Anything holding a
target-scoped resource — the projection buffer — restores itself.