Skip to main content

SSRenderer

Struct SSRenderer 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

pub fn texture_count(&self) -> usize

How many distinct texture hashes are registered.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,