Skip to main content

euv_core/render/software/
struct.rs

1use super::*;
2
3/// Single disjoint horizontal framebuffer strip dispatched to a persistent rasterization worker thread.
4#[cfg(not(target_arch = "wasm32"))]
5#[derive(Clone, Copy, Data, Debug, Default)]
6pub(crate) struct BandTaskSlice {
7    /// Raw mutable pixel buffer base address encoded as `usize` for `Send` transfer.
8    #[get(type(copy))]
9    pub(crate) pixels_ptr: usize,
10    /// Byte length of the disjoint horizontal pixel strip.
11    #[get(type(copy))]
12    pub(crate) pixels_len: usize,
13    /// Full framebuffer physical width in pixels.
14    #[get(type(copy))]
15    pub(crate) width: u32,
16    /// Inclusive top physical Y row of this strip.
17    #[get(type(copy))]
18    pub(crate) y_start: u32,
19    /// Number of physical rows inside this strip.
20    #[get(type(copy))]
21    pub(crate) height: u32,
22    /// Optional raw source pointer for parallel base-scene backdrop restore (`0` when unused).
23    #[get(type(copy))]
24    pub(crate) base_src_ptr: usize,
25    /// Horizontal byte offset within each row where backdrop restore starts.
26    #[get(type(copy))]
27    pub(crate) restore_x0_bytes: usize,
28    /// Horizontal byte length restored per row (`width * 4` for full-width contiguous restore).
29    #[get(type(copy))]
30    pub(crate) restore_row_bytes: usize,
31}
32
33/// Optional base-scene backdrop restore and fused full-viewport scrim LUT specification for band dispatch.
34#[derive(Clone, Copy, Debug, Default)]
35pub(crate) struct BandRestoreSpec<'a> {
36    /// Cached full-framebuffer RGBA8 base scene pixels, if restoring backdrop.
37    pub(crate) base_pixels: Option<&'a [u8]>,
38    /// Inclusive physical start X column to restore on each row.
39    pub(crate) x0: u32,
40    /// Exclusive physical end X column to restore on each row.
41    pub(crate) x1: u32,
42    /// Optional prebuilt 256-entry linear-light scrim LUT fused into backdrop restore.
43    pub(crate) scrim_luts: Option<&'a RgbaLutQuad>,
44}
45
46/// Precomputed 1D horizontal edge pixel sample reused across every vertical interior scanline of a rounded rectangle.
47#[derive(Clone, Copy, Debug, Default)]
48pub(crate) struct EdgeBlendSample {
49    /// Physical X pixel coordinate inside the target scanline.
50    pub(crate) x: u32,
51    /// Direct RGBA8 bytes written verbatim when `is_opaque` is true.
52    pub(crate) rgba: [u8; 4],
53    /// Pre-multiplied linear red channel `SRGB_TO_LIN_LUT[r] * alpha`.
54    pub(crate) sr_a: f32,
55    /// Pre-multiplied linear green channel `SRGB_TO_LIN_LUT[g] * alpha`.
56    pub(crate) sg_a: f32,
57    /// Pre-multiplied linear blue channel `SRGB_TO_LIN_LUT[b] * alpha`.
58    pub(crate) sb_a: f32,
59    /// Complement opacity `1.0 - alpha`.
60    pub(crate) inv_a: f32,
61    /// Quantized source alpha in `0..=255` for destination alpha update.
62    pub(crate) sa_u32: u32,
63    /// True when the sample overwrites destination pixels without blending.
64    pub(crate) is_opaque: bool,
65}
66
67/// Precomputed per-row exterior, anti-aliased, and dirty horizontal spans inside a cached rounded-clip corner mask.
68#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
69pub(crate) struct CornerMaskRowSpan {
70    /// Inclusive start X (in mask-local `0..dim`) of the contiguous exterior (`cov == 0`) run.
71    pub(crate) ext_start: u16,
72    /// Exclusive end X of the contiguous exterior (`cov == 0`) run.
73    pub(crate) ext_end: u16,
74    /// Inclusive start X of the anti-aliased (`1..=15`) transition run.
75    pub(crate) aa_start: u16,
76    /// Exclusive end X of the anti-aliased (`1..=15`) transition run.
77    pub(crate) aa_end: u16,
78    /// Inclusive start X of all non-interior (`cov < 16`) pixels requiring save/restore.
79    pub(crate) dirty_start: u16,
80    /// Exclusive end X of all non-interior (`cov < 16`) pixels requiring save/restore.
81    pub(crate) dirty_end: u16,
82}
83
84/// Cached exact 4x4 RGSS coverage grid (`0..=16` hit counts) for one rounded-clip quadrant `(radius, quadrant, aa)`.
85#[derive(Clone, Debug)]
86pub(crate) struct CachedCornerMask {
87    /// Bit representation of the clamped physical corner radius `r.to_bits()`.
88    pub(crate) radius_bits: u32,
89    /// Quadrant index (`0 = TL`, `1 = TR`, `2 = BR`, `3 = BL`).
90    pub(crate) quadrant: u8,
91    /// Whether 4x4 RGSS anti-aliasing is enabled (`false` for `AntialiasingMode::None`).
92    pub(crate) aa_enabled: bool,
93    /// Side length of the square coverage grid in physical pixels (`ceil(r + 1)`).
94    pub(crate) dim: u16,
95    /// Row-major exact coverage hit counts in `0..=16` (`0` = outside, `16` = fully inside).
96    pub(crate) cov: Vec<u8>,
97    /// Per-row horizontal exterior/AA/dirty span bounds (`len == dim`).
98    pub(crate) row_spans: Vec<CornerMaskRowSpan>,
99}
100
101/// Persistent cache of precomputed rounded-clip corner coverage masks reused across cards and frames.
102#[derive(Clone, Debug, Default)]
103pub(crate) struct CornerMaskCache {
104    /// Contiguous arena of cached quadrant coverage masks indexed by `u32` ID.
105    pub(crate) entries: Vec<CachedCornerMask>,
106}
107
108/// Single saved horizontal row segment belonging to one cached rounded-clip corner.
109#[derive(Clone, Copy, Debug, Default)]
110pub(crate) struct SavedCornerRow {
111    /// Global physical Y row coordinate.
112    pub(crate) y: u32,
113    /// Inclusive global physical start X of the saved segment.
114    pub(crate) sx0: u32,
115    /// Exclusive global physical end X of the saved segment.
116    pub(crate) sx1: u32,
117    /// Byte offset into `BandCornerScratch::saved_bytes` where this row's RGBA bytes start.
118    pub(crate) byte_off: usize,
119    /// Index into `CornerMaskCache::entries` for this quadrant.
120    pub(crate) mask_id: u32,
121    /// Mask-local X origin in global physical coordinates.
122    pub(crate) origin_x: i32,
123    /// Mask-local Y origin in global physical coordinates.
124    pub(crate) origin_y: i32,
125}
126
127/// Per-thread reusable scratch buffers for rounded-clip corner save and restore in `render_band`.
128#[derive(Debug, Default)]
129pub(crate) struct BandCornerScratch {
130    /// Bulk RGBA8 byte buffer storing pre-command corner pixels via `extend_from_slice`.
131    pub(crate) saved_bytes: Vec<u8>,
132    /// Saved row descriptors for the single-mask cached fast path.
133    pub(crate) saved_rows: Vec<SavedCornerRow>,
134    /// Clipped corner box bounds for the multi-mask / fallback path.
135    pub(crate) corner_boxes: Vec<(u32, u32, u32, u32)>,
136}
137
138/// Active rounded-rectangle clip mask in physical pixels applied during software band rasterization.
139#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
140pub(crate) struct RoundedClipMask {
141    /// Physical clip rectangle bounds.
142    #[get(type(copy))]
143    pub(crate) rect: Rect,
144    /// Physical corner radii.
145    #[get(type(copy))]
146    pub(crate) radius: CornerRadius,
147    /// Cached quadrant mask IDs `[tl, tr, br, bl]` into `CornerMaskCache::entries` (`u32::MAX` = uncached/inactive).
148    #[get(type(copy))]
149    pub(crate) corner_mask_ids: [u32; 4],
150}
151
152/// Pre-positioned glyph instance resolved once per frame on the main thread for zero-lookup band blitting.
153#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
154pub(crate) struct PositionedGlyph {
155    /// Direct arena slot index into `GlyphAtlas::entries`.
156    pub(crate) glyph_idx: u32,
157    /// Rounded physical X coordinate of the glyph's left edge.
158    pub(crate) x: i32,
159    /// Rounded physical Y coordinate of the glyph's top edge.
160    pub(crate) y: i32,
161    /// Cached glyph bitmap width in physical pixels for fast band culling.
162    pub(crate) width: u16,
163    /// Cached glyph bitmap height in physical pixels for fast band culling.
164    pub(crate) height: u16,
165}
166
167/// Shared read-only frame dispatch state consumed lock-free by persistent band workers during a single `render` call.
168#[cfg(not(target_arch = "wasm32"))]
169#[derive(Debug)]
170pub(crate) struct BandJob {
171    /// Base address of the caller-owned `BandTaskSlice` slice.
172    pub(crate) tasks_ptr: usize,
173    /// Number of horizontal strips in the frame job.
174    pub(crate) tasks_len: usize,
175    /// Base address of `DisplayList::commands`.
176    pub(crate) commands_ptr: usize,
177    /// Length of `DisplayList::commands`.
178    pub(crate) commands_len: usize,
179    /// Base address of the pre-processed `PreppedCommand` slice.
180    pub(crate) prepped_ptr: usize,
181    /// Length of the pre-processed `PreppedCommand` slice.
182    pub(crate) prepped_len: usize,
183    /// Base address of the per-frame pre-positioned `PositionedGlyph` slice.
184    pub(crate) glyphs_ptr: usize,
185    /// Length of the per-frame pre-positioned `PositionedGlyph` slice.
186    pub(crate) glyphs_len: usize,
187    /// Base address of the per-frame pre-computed `TextCaret` rectangle slice.
188    pub(crate) caret_rects_ptr: usize,
189    /// Length of the per-frame pre-computed `TextCaret` rectangle slice.
190    pub(crate) caret_rects_len: usize,
191    /// Base address of the per-frame active `RoundedClipMask` slice.
192    pub(crate) rounded_clips_ptr: usize,
193    /// Length of the per-frame active `RoundedClipMask` slice.
194    pub(crate) rounded_clips_len: usize,
195    /// Base address of the read-only cached quadrant corner masks slice.
196    pub(crate) corner_masks_ptr: usize,
197    /// Length of the read-only cached quadrant corner masks slice.
198    pub(crate) corner_masks_len: usize,
199    /// Base address of the shared read-only `GlyphAtlas`.
200    pub(crate) atlas_ptr: usize,
201    /// Optional raw pointer to a stack-pinned `RgbaLutQuad` fused into backdrop restore (`0` when unused).
202    pub(crate) scrim_luts_ptr: usize,
203    /// Active physical DPI scale factor.
204    pub(crate) scale: f32,
205    /// Active anti-aliasing policy.
206    pub(crate) aa: AntialiasingMode,
207    /// Atomic work-stealing strip cursor shared by the caller and all worker threads.
208    pub(crate) next_idx: AtomicUsize,
209    /// Atomic count of worker threads that finished observing this epoch.
210    pub(crate) workers_done: AtomicUsize,
211    /// Atomic accumulated shaded pixel count across all strips.
212    pub(crate) total_shaded: AtomicUsize,
213}
214
215/// Per-worker cache-line-isolated wake slot eliminating N-way mutex convoys and false sharing.
216#[cfg(not(target_arch = "wasm32"))]
217#[repr(align(128))]
218#[derive(Debug, Default)]
219pub(crate) struct BandWorkerSlot {
220    /// Per-worker monotonically increasing dispatch epoch counter.
221    pub(crate) epoch: AtomicUsize,
222    /// Non-zero while this worker thread is parked inside `wake.wait`.
223    pub(crate) parked: AtomicUsize,
224    /// Private mutex guarding this worker's condvar sleep transition.
225    pub(crate) lock: Mutex<()>,
226    /// Condition variable waking only this worker on a new epoch or shutdown.
227    pub(crate) wake: Condvar,
228}
229
230/// Lock-free atomic dispatch header and per-worker wake slots shared between the caller and persistent band workers.
231#[cfg(not(target_arch = "wasm32"))]
232#[derive(Debug, Default)]
233pub(crate) struct BandPoolShared {
234    /// Raw pointer address of the active `BandJob` (`0` while idle).
235    pub(crate) active_job: AtomicUsize,
236    /// Number of background workers dispatched for the current epoch.
237    pub(crate) active_workers: AtomicUsize,
238    /// Non-zero when the calling thread is parked waiting for workers to finish.
239    pub(crate) caller_parked: AtomicUsize,
240    /// Non-zero when the worker pool is shutting down during `Drop`.
241    pub(crate) shutdown: AtomicUsize,
242    /// Mutex guarding caller parking transitions (bypassed on the spin fast-path).
243    pub(crate) caller_lock: Mutex<()>,
244    /// Condition variable waking a parked caller once all dispatched workers finish an epoch.
245    pub(crate) wake_caller: Condvar,
246    /// Contention-free per-worker wake slots indexed by worker ID `0..worker_count`.
247    pub(crate) slots: Vec<BandWorkerSlot>,
248}
249
250/// Persistent native worker pool eliminating per-frame `pthread_create` overhead on high-refresh mobile and desktop targets.
251#[cfg(not(target_arch = "wasm32"))]
252#[derive(Debug)]
253pub(crate) struct BandWorkerPool {
254    /// Number of background worker threads (excluding the calling main thread).
255    pub(crate) worker_count: usize,
256    /// Non-blocking guard owning reusable horizontal band task slices across frames.
257    pub(crate) dispatch_tasks: Mutex<Vec<BandTaskSlice>>,
258    /// Shared atomic dispatch state and condition variables.
259    pub(crate) shared: Arc<BandPoolShared>,
260    /// Background worker join handles drained cleanly on `Drop`.
261    pub(crate) handles: Vec<JoinHandle<()>>,
262}
263
264/// Preprocessed draw-command metadata computed once on the main thread before parallel band rasterization.
265#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
266pub(crate) struct PreppedCommand {
267    /// Original index inside `DisplayList::commands`.
268    #[get(type(copy))]
269    pub(crate) cmd_index: usize,
270    /// Active intersected physical clip rectangle for this command.
271    #[get(type(copy))]
272    pub(crate) active_clip: Rect,
273    /// Inclusive top physical Y row affected by this command.
274    #[get(type(copy))]
275    pub(crate) y0: u32,
276    /// Exclusive bottom physical Y row affected by this command.
277    #[get(type(copy))]
278    pub(crate) y1: u32,
279    /// Optional opaque inner vertical-span cutout for a `BoxShadow` followed immediately by an opaque `FillRoundedRect`.
280    #[get(type(copy))]
281    pub(crate) opaque_cutout_h: Rect,
282    /// Optional opaque inner horizontal-span cutout for a `BoxShadow` followed immediately by an opaque `FillRoundedRect`.
283    #[get(type(copy))]
284    pub(crate) opaque_cutout_v: Rect,
285    /// Start index into the per-frame `PositionedGlyph` slice for `TextRun` commands (`0` when unused).
286    #[get(type(copy))]
287    pub(crate) glyph_run_start: u32,
288    /// Count of pre-positioned glyphs for `TextRun` commands (`0` when unused).
289    #[get(type(copy))]
290    pub(crate) glyph_run_len: u32,
291    /// Optional index into the per-frame pre-resolved `TextCaret` rectangle slice (`u32::MAX` when unused).
292    #[get(type(copy))]
293    pub(crate) caret_rect_idx: u32,
294    /// Start index into the per-frame `RoundedClipMask` slice (`0` when unused).
295    #[get(type(copy))]
296    pub(crate) rounded_clip_start: u32,
297    /// Count of active `RoundedClipMask` entries touching this command (`0` on fast path).
298    #[get(type(copy))]
299    pub(crate) rounded_clip_len: u16,
300    /// Relative index jump inside `prepped_scratch` when a `usize::MAX` clip-scope sentinel misses a band.
301    #[get(type(copy))]
302    pub(crate) skip_delta: u32,
303}
304
305/// Single open-addressed slot in `TextLayoutCache` mapping `(key_hash, width_key)` to an arena index.
306#[derive(Clone, Copy, Debug, Eq, PartialEq)]
307pub(crate) struct TextLayoutSlot {
308    /// Deterministic 64-bit FxHash of source text bytes and typography style.
309    pub(crate) key_hash: u64,
310    /// Quantized wrap width (`(max_w * 10.0).round() as u32`, or `u32::MAX` for unbounded lines).
311    pub(crate) width_key: u32,
312    /// Arena index into `TextLayoutCache::entries` (`u32::MAX` marks an empty slot).
313    pub(crate) entry_idx: u32,
314}
315
316/// Zero-clone, SipHash-free open-addressed cache of wrapped `TextLayoutResult` entries with bounded LRU eviction and telemetry.
317#[derive(Clone, Debug, Default)]
318pub(crate) struct TextLayoutCache {
319    /// Power-of-two linear-probing slot table (`entry_idx == u32::MAX` denotes an empty slot).
320    pub(crate) slots: Vec<TextLayoutSlot>,
321    /// Contiguous arena owning cached `TextLayoutResult` entries read lock-free across band workers.
322    pub(crate) entries: Vec<TextLayoutResult>,
323    /// Lookup keys `(key_hash, width_key)` aligned 1:1 with `entries` for slot table reconstruction after LRU eviction.
324    pub(crate) entry_keys: Vec<(u64, u32)>,
325    /// Monotonic access ticks aligned 1:1 with `entries` for bounded LRU eviction.
326    pub(crate) entry_last_used: Vec<u64>,
327    /// Tracked resident byte footprint per entry aligned 1:1 with `entries`.
328    pub(crate) entry_bytes: Vec<usize>,
329    /// Current total resident memory in bytes across all cached layouts.
330    pub(crate) resident_bytes: usize,
331    /// Optional custom memory budget in bytes (`0` selects `TEXT_LAYOUT_CACHE_BUDGET_BYTES`).
332    pub(crate) budget_bytes: usize,
333    /// Monotonic access clock incremented on every cache lookup.
334    pub(crate) lru_clock: u64,
335    /// Cumulative count of text layout cache hits.
336    pub(crate) cache_hits: u64,
337    /// Cumulative count of text layout cache misses.
338    pub(crate) cache_misses: u64,
339    /// Cumulative count of evicted cold text layouts.
340    pub(crate) evictions: u64,
341}
342
343/// High-performance CPU tile and analytical-SDF software rasterizer with zero GPU dependencies.
344#[derive(Clone, Data, Debug, Default)]
345pub struct SoftwareRenderer {
346    /// Persistent RGBA8 framebuffer reused across frames.
347    pub(crate) framebuffer: Framebuffer,
348    /// Shared glyph rasterization atlas.
349    pub(crate) glyph_atlas: GlyphAtlas,
350    /// Native or deterministic font rasterization provider.
351    pub(crate) font_provider: NativeSystemFont,
352    /// Deterministic text measurer matching `LayoutEngine` metrics.
353    #[get(type(copy))]
354    pub(crate) measurer: DeterministicTextMeasurer,
355    /// Zero-clone open-addressed cache of wrapped `TextLayoutResult` entries eliminating per-frame cloning and SipHash.
356    #[get(pub(crate))]
357    #[get_mut(pub(crate))]
358    #[set(pub(crate))]
359    pub(crate) text_layout_cache: TextLayoutCache,
360    /// Active physical clip-rectangle stack.
361    #[get(pub(crate))]
362    #[get_mut(pub(crate))]
363    #[set(pub(crate))]
364    pub(crate) clip_stack: Vec<Rect>,
365    /// Parallel stack of optional rounded-rectangle clip masks aligned 1:1 with `clip_stack`.
366    #[get(pub(crate))]
367    #[get_mut(pub(crate))]
368    #[set(pub(crate))]
369    pub(crate) rounded_clip_stack: Vec<Option<RoundedClipMask>>,
370    /// Active anti-aliasing policy.
371    #[get(type(copy))]
372    pub(crate) antialiasing: AntialiasingMode,
373    /// Tile edge length in physical pixels for damage tile accounting.
374    #[get(type(copy))]
375    pub(crate) tile_size: u32,
376    /// Cached physical width of the static base-scene snapshot behind active portals.
377    #[get(pub(crate), type(copy))]
378    #[set(pub(crate))]
379    pub(crate) base_cache_width: u32,
380    /// Cached physical height of the static base-scene snapshot behind active portals.
381    #[get(pub(crate), type(copy))]
382    #[set(pub(crate))]
383    pub(crate) base_cache_height: u32,
384    /// Full base-layer command hash for the cached static backdrop (`0` when invalid).
385    #[get(pub(crate), type(copy))]
386    #[set(pub(crate))]
387    pub(crate) base_cache_hash: u64,
388    /// Base-layer command hash observed on the immediately preceding frame to avoid copying active scroll frames.
389    #[get(pub(crate), type(copy))]
390    #[set(pub(crate))]
391    pub(crate) prev_base_hash: u64,
392    /// Saved RGBA8 pixels of the static base scene beneath active portals for fast overlay compositing.
393    #[get(pub(crate))]
394    #[get_mut(pub(crate))]
395    #[set(pub(crate))]
396    pub(crate) base_cache_pixels: Vec<u8>,
397    /// Reusable pre-processed draw command buffer cleared at frame start to avoid per-frame allocations.
398    #[get(pub(crate))]
399    #[get_mut(pub(crate))]
400    #[set(pub(crate))]
401    pub(crate) prepped_scratch: Vec<PreppedCommand>,
402    /// Reusable active rounded-clip mask buffer cleared at frame start to avoid per-frame allocations.
403    #[get(pub(crate))]
404    #[get_mut(pub(crate))]
405    #[set(pub(crate))]
406    pub(crate) rounded_clips_scratch: Vec<RoundedClipMask>,
407    /// Reusable flat buffer of pre-positioned glyphs emitted once per frame for zero-lookup band rasterization.
408    #[get(pub(crate))]
409    #[get_mut(pub(crate))]
410    #[set(pub(crate))]
411    pub(crate) positioned_glyphs_scratch: Vec<PositionedGlyph>,
412    /// Reusable line buffer for single-lookup `Center`/`Right` text alignment positioning.
413    #[get(pub(crate))]
414    #[get_mut(pub(crate))]
415    #[set(pub(crate))]
416    pub(crate) line_glyphs_scratch: Vec<(u32, f32, u16, u16)>,
417    /// Reusable buffer of pre-computed physical caret rectangles for `DrawCommand::TextCaret`.
418    #[get(pub(crate))]
419    #[get_mut(pub(crate))]
420    #[set(pub(crate))]
421    pub(crate) caret_rects_scratch: Vec<Rect>,
422    /// Persistent cache of precomputed rounded-clip corner coverage masks reused across cards and frames.
423    #[get(pub(crate))]
424    #[get_mut(pub(crate))]
425    #[set(pub(crate))]
426    pub(crate) corner_mask_cache: CornerMaskCache,
427    /// Physical union bounds of the foreground portal commands painted on the previous cached frame.
428    #[get(pub(crate), type(copy))]
429    #[set(pub(crate))]
430    pub(crate) prev_portal_bounds: Option<Rect>,
431    /// Full-viewport translucent scrim color active on the previous cached portal frame, if any.
432    #[get(pub(crate), type(copy))]
433    #[set(pub(crate))]
434    pub(crate) prev_portal_scrim: Option<Color>,
435    /// Physical union bounds of all prepped commands emitted during the latest `prepare_frame_range`.
436    #[get(pub(crate), type(copy))]
437    #[set(pub(crate))]
438    pub(crate) prepped_bounds: Option<Rect>,
439    /// Physical union bounds of `prepped_scratch[1..]` emitted during the latest `prepare_frame_range`.
440    #[get(pub(crate), type(copy))]
441    #[set(pub(crate))]
442    pub(crate) prepped_fg_bounds: Option<Rect>,
443    /// Reusable clip-scope stack `(clip_y0, clip_y1, optional_sentinel_idx)` for O(1) off-band subtree skipping.
444    #[get(pub(crate))]
445    #[get_mut(pub(crate))]
446    #[set(pub(crate))]
447    pub(crate) clip_scope_scratch: Vec<(u32, u32, Option<usize>)>,
448    /// Cached full-viewport translucent scrim color matching `cached_scrim_luts`.
449    #[get(skip)]
450    #[get_mut(skip)]
451    #[set(skip)]
452    pub(crate) cached_scrim_color: Option<Color>,
453    /// Cached 256-entry per-channel linear-light scrim LUT reused across frames with identical scrim color.
454    #[get(skip)]
455    #[get_mut(skip)]
456    #[set(skip)]
457    pub(crate) cached_scrim_luts: Option<Box<RgbaLutQuad>>,
458    /// Cumulative number of rasterization passes executed serially on the calling thread.
459    #[get(type(copy))]
460    #[get_mut(skip)]
461    #[set(skip)]
462    pub(crate) serial_dispatches: u64,
463    /// Cumulative number of rasterization passes dispatched across `BandWorkerPool`.
464    #[get(type(copy))]
465    #[get_mut(skip)]
466    #[set(skip)]
467    pub(crate) parallel_dispatches: u64,
468    /// Cumulative number of fully occluded draw commands culled without reordering translucent layers.
469    #[get(type(copy))]
470    #[get_mut(skip)]
471    #[set(skip)]
472    pub(crate) culled_occluded_commands: u64,
473    /// Current adaptive pixel-area threshold separating serial execution from multi-threaded band dispatch.
474    #[get(type(copy))]
475    #[get_mut(skip)]
476    #[set(skip)]
477    pub(crate) adaptive_parallel_threshold_pixels: usize,
478    /// Optional explicit pixel-area override for parallel band dispatch (`0` selects adaptive auto-tuning).
479    #[get(type(copy))]
480    #[get_mut(skip)]
481    #[set(skip)]
482    pub(crate) adaptive_threshold_override: usize,
483    /// Most recently observed serial band rasterization duration in microseconds.
484    #[get(type(copy))]
485    #[get_mut(skip)]
486    #[set(skip)]
487    pub(crate) last_serial_us: u64,
488    /// Most recently observed parallel band rasterization duration in microseconds.
489    #[get(type(copy))]
490    #[get_mut(skip)]
491    #[set(skip)]
492    pub(crate) last_parallel_us: u64,
493}
494
495/// Compact 16-byte hot per-column cache entry reused across every vertical shadow strip scanline.
496#[derive(Clone, Copy, Debug, Default)]
497pub(crate) struct ShadowStripHot {
498    /// Precomputed horizontal IGN phase term `(x as f32) * 0.067_110_56`.
499    pub(crate) ign_x: f32,
500    /// Last observed destination RGBA8 bytes packed as native-endian `u32`.
501    pub(crate) cached_dst: u32,
502    /// Cached quantized sRGB Q4 red output prior to IGN dither addition.
503    pub(crate) q4_r: i16,
504    /// Cached quantized sRGB Q4 green output prior to IGN dither addition.
505    pub(crate) q4_g: i16,
506    /// Cached quantized sRGB Q4 blue output prior to IGN dither addition.
507    pub(crate) q4_b: i16,
508    /// Cached composited output alpha byte.
509    pub(crate) out_a: u8,
510    /// Column blend state (`0` = inactive, `1` = uncached blend, `2` = cached blend, `3` = opaque).
511    pub(crate) state: u8,
512}
513
514/// Precomputed linear-light blend factors for a single shadow column or mirrored corner sample.
515#[derive(Clone, Copy, Debug, Default)]
516pub(crate) struct ShadowStripParams {
517    /// Premultiplied linear red channel `SRGB_TO_LIN_LUT[r] * alpha`.
518    pub(crate) sr_a: f32,
519    /// Premultiplied linear green channel `SRGB_TO_LIN_LUT[g] * alpha`.
520    pub(crate) sg_a: f32,
521    /// Premultiplied linear blue channel `SRGB_TO_LIN_LUT[b] * alpha`.
522    pub(crate) sb_a: f32,
523    /// Complement opacity `1.0 - alpha`.
524    pub(crate) inv_a: f32,
525    /// Quantized source alpha `(alpha * 255.0) as u32`.
526    pub(crate) sa_u32: u32,
527    /// Complement integer alpha `255 - sa_u32`.
528    pub(crate) inv_a_u32: u32,
529}
530
531/// Single-pixel destination cache shared across 2x/4x symmetrically mirrored shadow corner writes.
532#[derive(Clone, Copy, Debug, Default)]
533pub(crate) struct ShadowPixelCache {
534    /// Last observed destination RGBA8 bytes packed as native-endian `u32`.
535    pub(crate) dst_u32: u32,
536    /// Cached quantized sRGB Q4 red output prior to IGN dither addition.
537    pub(crate) q4_r: i32,
538    /// Cached quantized sRGB Q4 green output prior to IGN dither addition.
539    pub(crate) q4_g: i32,
540    /// Cached quantized sRGB Q4 blue output prior to IGN dither addition.
541    pub(crate) q4_b: i32,
542    /// Cached composited output alpha byte.
543    pub(crate) out_a: u8,
544    /// Whether `dst_u32` and `q4_*` have been populated for the current corner sample.
545    pub(crate) valid: bool,
546}
547
548/// Per-thread reusable 1D horizontal shadow falloff strip buffers eliminating per-shadow allocations.
549#[derive(Debug, Default)]
550pub(crate) struct BandShadowScratch {
551    /// Hot per-column cache entries for the left vertical shadow strip.
552    pub(crate) left_hot: Vec<ShadowStripHot>,
553    /// Cold linear blend parameters for the left vertical shadow strip.
554    pub(crate) left_params: Vec<ShadowStripParams>,
555    /// Hot per-column cache entries for the right vertical shadow strip.
556    pub(crate) right_hot: Vec<ShadowStripHot>,
557    /// Cold linear blend parameters for the right vertical shadow strip.
558    pub(crate) right_params: Vec<ShadowStripParams>,
559}
560
561/// Precomputed geometry and hoisted linear tint constants shared across rows of one `raster_shadow` invocation.
562#[derive(Clone, Copy, Debug)]
563pub(crate) struct ShadowRenderCtx {
564    /// Shadow tint RGBA color passed to `blend_shadow_span_const_alpha`.
565    pub(crate) color: Color,
566    /// Shadow tint sRGB RGB bytes `[r, g, b]`.
567    pub(crate) color_rgb: [u8; 3],
568    /// Hoisted linear-light shadow tint `[sr, sg, sb]`.
569    pub(crate) lin_rgb: [f32; 3],
570    /// Caster horizontal center X in physical pixels.
571    pub(crate) cx: f32,
572    /// Caster vertical center Y in physical pixels.
573    pub(crate) cy: f32,
574    /// Caster half-width in physical pixels.
575    pub(crate) half_w: f32,
576    /// Caster half-height in physical pixels.
577    pub(crate) half_h: f32,
578    /// Normalized shadow tint alpha in `[0.0, 1.0]`.
579    pub(crate) base_alpha: f32,
580    /// Inverse Gaussian standard deviation `1.0 / sigma.max(0.5)`.
581    pub(crate) inv_sigma: f32,
582    /// Gaussian cutoff radius `3.0 * sigma`.
583    pub(crate) cutoff: f32,
584    /// Clamped uniform corner radius in physical pixels.
585    pub(crate) uniform_r: f32,
586    /// Conservative squared distance threshold `(cutoff + uniform_r + 1e-3)^2` for sqrt-free corner early reject.
587    pub(crate) cutoff_r_safe_sq: f32,
588    /// Clamped inclusive visible left X bound.
589    pub(crate) x0: u32,
590    /// Clamped exclusive visible right X bound.
591    pub(crate) x1: u32,
592    /// Exclusive end X of the left corner region (`sx0` when `has_mid_x`, else `split_x`).
593    pub(crate) lx1: u32,
594    /// Inclusive start X of the right corner region (`sx1` when `has_mid_x`, else `split_x`).
595    pub(crate) rx0: u32,
596    /// Horizontal mirror sum `(2 * cx).round() - 1` when `cx` is half-integer aligned.
597    pub(crate) mirror_x_sum: i32,
598    /// True when the shadow has a middle constant-alpha horizontal span (`sx0 < sx1`).
599    pub(crate) has_mid_x: bool,
600}