pub struct GradientCache { /* private fields */ }Expand description
An LRU cache of baked gradient colour ramps packed into one upload buffer.
Implementations§
Source§impl GradientCache
impl GradientCache
Sourcepub fn new(capacity: u32, level: Level) -> Self
pub fn new(capacity: u32, level: Level) -> Self
Create a cache retaining at most capacity ramps.
Sourcepub fn for_texture(layout: GradientTextureLayout, level: Level) -> Self
pub fn for_texture(layout: GradientTextureLayout, level: Level) -> Self
Create a cache sized for the texture the ramps will be uploaded into.
Capacity is the worst-case entry count for that texture — every ramp baked
at MAX_GRADIENT_LUT_SIZE — so the packed buffer cannot outgrow the
texture regardless of how complex the cached gradients turn out to be.
Sourcepub fn capacity(&self) -> u32
pub fn capacity(&self) -> u32
The maximum number of entries retained across a GradientCache::maintain.
Sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Number of ramps currently resident.
Sourcepub fn has_changed(&self) -> bool
pub fn has_changed(&self) -> bool
Whether the packed bytes have changed since the last upload.
Sourcepub fn mark_synced(&mut self)
pub fn mark_synced(&mut self)
Record that the packed bytes have been uploaded.
Sourcepub fn lookup(&self, gradient: &EncodedGradient) -> Option<CachedRamp>
pub fn lookup(&self, gradient: &EncodedGradient) -> Option<CachedRamp>
Look a gradient up without baking it or disturbing LRU order.
Sourcepub fn get_or_create_ramp(&mut self, gradient: &EncodedGradient) -> CachedRamp
pub fn get_or_create_ramp(&mut self, gradient: &EncodedGradient) -> CachedRamp
Return the cached ramp for gradient, baking and packing it on a miss.
Offsets returned within one frame stay valid for that frame: baking only
appends, and the compaction that rewrites offsets happens in
GradientCache::maintain at the frame boundary.
Sourcepub fn maintain(&mut self)
pub fn maintain(&mut self)
Evict least-recently-used ramps down to GradientCache::capacity.
Call once per frame, after the frame’s paints have been encoded — never mid-frame, since compaction invalidates previously returned offsets.
Sourcepub fn take_luts(&mut self) -> Vec<u8> ⓘ
pub fn take_luts(&mut self) -> Vec<u8> ⓘ
Take the packed bytes, leaving the cache’s buffer empty.
Paired with GradientCache::restore_luts so an upload can pad the buffer
to the texture footprint and hand it back without copying the ramp bytes.
The restored buffer must hold the same logical content.
Sourcepub fn restore_luts(&mut self, luts: Vec<u8>)
pub fn restore_luts(&mut self, luts: Vec<u8>)
Give back a buffer taken by GradientCache::take_luts.
Sourcepub fn begin_upload(
&mut self,
layout: GradientTextureLayout,
) -> Option<LutUpload<'_>>
pub fn begin_upload( &mut self, layout: GradientTextureLayout, ) -> Option<LutUpload<'_>>
Borrow the packed bytes padded out to layout’s full byte footprint,
ready to hand to a texel copy.
Returns None when no ramps are packed, and when layout is too small
to hold them. The second case is a refusal, logged rather than
silently served: padding to a footprint below the packed length would
truncate the buffer instead of extending it, uploading a prefix of the
ramps under offsets computed for all of them — every gradient past the
cut would sample whatever the texture already held. Skipping the upload
leaves the previous frame’s texels in place, which is stale but
coherent, and leaves has_changed set so the next upload against a
large enough layout still happens.
The padding is applied to the cache’s own buffer and undone when the
returned LutUpload drops, so a served upload costs one resize rather
than a copy of every ramp.