ombre: Recycle canvas texture layers
8ea131d93f61c20e8d0de9bb5e663e783281112f
Texture layers were allocated per image and never released, so once all layers were taken, newly blitted images silently failed to draw. Images are also identified by their pixel address, which can be reused by a new image once the old one is dropped, so a stale layer could be drawn in place of the new image. Layers now hold a weak reference to their image's pixels, which keeps the address from being reused while a layer refers to it. Layers of dropped images are released when the batch is cleared, and when all layers are taken, the least recently used one that isn't referenced by the current batch is recycled.
1 parent
dc1e5b1e
ombre/src/paint/canvas.rs
+76 -18
| 1 | 1 | //! Canvas rendering system for 2D graphics. |
|
| 2 | 2 | #![warn(missing_docs)] |
|
| 3 | 3 | ||
| 4 | 4 | use std::collections::{HashMap, VecDeque}; |
|
| 5 | 5 | use std::rc::Rc; |
|
| 6 | + | use std::sync::{Arc, Weak}; |
|
| 6 | 7 | ||
| 7 | 8 | use crate::math::ZDepth; |
|
| 8 | 9 | use crate::prelude::*; |
|
| 9 | 10 | use crate::text::Text; |
|
| 10 | 11 | use crate::ui::gfx::{font, present}; |
| 69 | 70 | /// Vertex and texture bindings for this pipeline. |
|
| 70 | 71 | bindings: Bindings<N, M>, |
|
| 71 | 72 | } |
|
| 72 | 73 | ||
| 73 | 74 | /// Represents a layer in a 2D texture array, tracking both the source image and its index. |
|
| 74 | - | #[derive(Copy, Clone, PartialEq, Debug)] |
|
| 75 | + | #[derive(Clone, Debug)] |
|
| 75 | 76 | struct TextureLayer { |
|
| 76 | - | /// The source image ID if this layer contains an image, or `None` if the texture was generated |
|
| 77 | - | /// programatically, eg. the blank texture used for shape rendering. |
|
| 78 | - | image: Option<ImageId>, |
|
| 77 | + | /// The source image pixels if this layer contains an image, or `None` if the layer is free or |
|
| 78 | + | /// the texture was generated programatically, eg. the blank texture used for shape rendering. |
|
| 79 | + | /// |
|
| 80 | + | /// Holding a weak reference keeps the pixel allocation from being reused by another image |
|
| 81 | + | /// while the layer refers to it, since images are identified by their pixel address. |
|
| 82 | + | image: Option<Weak<[Rgba8]>>, |
|
| 79 | 83 | /// The GPU texture layer index where this texture is stored. |
|
| 80 | 84 | index: u8, |
|
| 85 | + | /// The batch generation in which this layer was last used. |
|
| 86 | + | last_used: u64, |
|
| 87 | + | } |
|
| 88 | + | ||
| 89 | + | impl TextureLayer { |
|
| 90 | + | /// Check whether this layer contains the given image. |
|
| 91 | + | fn holds(&self, image: &Image) -> bool { |
|
| 92 | + | self.image |
|
| 93 | + | .as_ref() |
|
| 94 | + | .is_some_and(|pixels| std::ptr::addr_eq(pixels.as_ptr(), Arc::as_ptr(&image.pixels))) |
|
| 95 | + | } |
|
| 96 | + | ||
| 97 | + | /// Check whether this layer's image is still alive. |
|
| 98 | + | fn is_live(&self) -> bool { |
|
| 99 | + | self.image |
|
| 100 | + | .as_ref() |
|
| 101 | + | .is_some_and(|pixels| pixels.strong_count() > 0) |
|
| 102 | + | } |
|
| 81 | 103 | } |
|
| 82 | 104 | ||
| 83 | 105 | /// Internal rendering operations that need to be processed during the next frame. |
|
| 84 | 106 | enum Op { |
|
| 85 | 107 | /// Upload an image to a specific texture layer. |
| 109 | 131 | /// Accumulates vertices for all paint operations in the current batch. |
|
| 110 | 132 | /// Used to minimize draw calls by batching compatible operations together. |
|
| 111 | 133 | batch: VertexBatch, |
|
| 112 | 134 | /// Queue of pending render operations to execute in the next frame. |
|
| 113 | 135 | queue: VecDeque<Op>, |
|
| 136 | + | /// Incremented every time the batch is cleared. Layers used in the current generation are |
|
| 137 | + | /// referenced by the batch, and can't be recycled. |
|
| 138 | + | generation: u64, |
|
| 114 | 139 | } |
|
| 115 | 140 | ||
| 116 | 141 | impl<const N: usize> RenderState<N> { |
|
| 117 | 142 | fn new(texture: TextureInfo) -> Self { |
|
| 118 | 143 | let mut textures = Array::new(); |
| 121 | 146 | // The first layer contains a blank texture used for rendering shapes. |
|
| 122 | 147 | textures |
|
| 123 | 148 | .push(TextureLayer { |
|
| 124 | 149 | image: None, |
|
| 125 | 150 | index: 0, |
|
| 151 | + | last_used: 0, |
|
| 126 | 152 | }) |
|
| 127 | 153 | .expect("RenderState::new: texture layer capacity must be greater than zero"); |
|
| 128 | 154 | ||
| 129 | 155 | Self { |
|
| 130 | 156 | textures, |
|
| 131 | 157 | sizes, |
|
| 132 | 158 | batch: VertexBatch::new(texture), |
|
| 133 | 159 | queue: VecDeque::with_capacity(32), |
|
| 160 | + | generation: 0, |
|
| 134 | 161 | } |
|
| 135 | 162 | } |
|
| 136 | 163 | ||
| 137 | - | // Helper function to find or create texture layer. |
|
| 138 | - | fn get_or_create_layer(&mut self, image: Image) -> Option<TextureLayer> { |
|
| 164 | + | // Helper function to find or create texture layer, returning its index. |
|
| 165 | + | fn get_or_create_layer(&mut self, image: Image) -> Option<u8> { |
|
| 166 | + | let generation = self.generation; |
|
| 139 | 167 | // First try to find an existing layer with the given image. |
|
| 140 | - | if let Some(&layer) = self.textures.iter().find(|l| l.image == Some(image.id())) { |
|
| 141 | - | return Some(layer); |
|
| 168 | + | if let Some(layer) = self.textures.iter_mut().find(|l| l.holds(&image)) { |
|
| 169 | + | layer.last_used = generation; |
|
| 170 | + | return Some(layer.index); |
|
| 142 | 171 | } |
|
| 143 | 172 | // Then try to create new layer. |
|
| 144 | - | let layer = TextureLayer { |
|
| 145 | - | image: Some(image.id()), |
|
| 146 | - | index: self.textures.len() as u8, |
|
| 173 | + | let index = if self.textures.len() < self.textures.capacity() { |
|
| 174 | + | let index = self.textures.len() as u8; |
|
| 175 | + | self.textures |
|
| 176 | + | .push(TextureLayer { |
|
| 177 | + | image: None, |
|
| 178 | + | index, |
|
| 179 | + | last_used: generation, |
|
| 180 | + | }) |
|
| 181 | + | .ok()?; |
|
| 182 | + | index |
|
| 183 | + | } else { |
|
| 184 | + | // Otherwise, recycle the least recently used layer that isn't referenced by the |
|
| 185 | + | // current batch, preferring layers whose image was dropped. |
|
| 186 | + | self.textures |
|
| 187 | + | .iter() |
|
| 188 | + | .filter(|l| l.index != 0 && l.last_used != generation) |
|
| 189 | + | .min_by_key(|l| (l.is_live(), l.last_used))? |
|
| 190 | + | .index |
|
| 147 | 191 | }; |
|
| 148 | - | self.textures.push(layer).ok()?; |
|
| 149 | - | self.sizes[layer.index as usize] = image.size; |
|
| 192 | + | let layer = &mut self.textures.as_mut_slice()[index as usize]; |
|
| 193 | + | layer.image = Some(Arc::downgrade(&image.pixels)); |
|
| 194 | + | layer.last_used = generation; |
|
| 195 | + | self.sizes[index as usize] = image.size; |
|
| 150 | 196 | self.queue.push_back(Op::UploadImage { |
|
| 151 | 197 | image, |
|
| 152 | - | layer: layer.index, |
|
| 198 | + | layer: index, |
|
| 153 | 199 | }); |
|
| 154 | - | Some(layer) |
|
| 200 | + | Some(index) |
|
| 201 | + | } |
|
| 202 | + | ||
| 203 | + | /// Clear the batch, and release layers whose image was dropped. |
|
| 204 | + | fn clear(&mut self) { |
|
| 205 | + | self.batch.clear(); |
|
| 206 | + | self.generation += 1; |
|
| 207 | + | ||
| 208 | + | for layer in self.textures.iter_mut() { |
|
| 209 | + | if !layer.is_live() { |
|
| 210 | + | layer.image = None; |
|
| 211 | + | } |
|
| 212 | + | } |
|
| 155 | 213 | } |
|
| 156 | 214 | } |
|
| 157 | 215 | ||
| 158 | 216 | /// Configuration options for the canvas rendering system. |
|
| 159 | 217 | pub struct Config { |
| 507 | 565 | fn size(&self) -> Size<f32> { |
|
| 508 | 566 | self.size / self.scale |
|
| 509 | 567 | } |
|
| 510 | 568 | ||
| 511 | 569 | fn clear(&mut self) { |
|
| 512 | - | self.render.batch.clear(); |
|
| 570 | + | self.render.clear(); |
|
| 513 | 571 | self.action = Some(PassAction::Clear { |
|
| 514 | 572 | color: Some(Rgba::TRANSPARENT), |
|
| 515 | 573 | depth: Some(ZDepth::from(1.)), |
|
| 516 | 574 | stencil: None, |
|
| 517 | 575 | }); |
| 554 | 612 | opacity: f32, |
|
| 555 | 613 | ) { |
|
| 556 | 614 | let Some(layer) = self.render.get_or_create_layer(image) else { |
|
| 557 | 615 | log::error!( |
|
| 558 | 616 | target: "ui", |
|
| 559 | - | "texture limit exceeded: renderer only supports {N} concurrent texture(s)" |
|
| 617 | + | "texture limit exceeded: renderer only supports {N} texture(s) per batch" |
|
| 560 | 618 | ); |
|
| 561 | 619 | return; |
|
| 562 | 620 | }; |
|
| 563 | 621 | let src = src.into(); |
|
| 564 | 622 | let dst = dst.into(); |
|
| 565 | 623 | ||
| 566 | 624 | self.render.batch.blit( |
|
| 567 | 625 | Blit::new(src, dst.transform(self.transform)) |
|
| 568 | 626 | .tint(tint) |
|
| 569 | 627 | .opacity(opacity), |
|
| 570 | - | layer.index, |
|
| 628 | + | layer, |
|
| 571 | 629 | ); |
|
| 572 | 630 | } |
|
| 573 | 631 | ||
| 574 | 632 | fn text<'a>(&mut self, text: impl Into<Text<'a>>, dst: impl Into<Point2D<f32, Local>>) { |
|
| 575 | 633 | let offset = Vector2D::from(dst.into()); |