ombre: Code improvements in `canvas` module
6a430a76a28a7220daecce72bc48105667e7d759
1 parent
7bf36659
ombre/src/paint/canvas.rs
+39 -34
| 17 | 17 | /// given that we are drawing triangles. |
|
| 18 | 18 | pub const DEFAULT_MAX_VERTICES: usize = 2usize.pow(18) - 1; |
|
| 19 | 19 | /// Default number of texture layers used by the rendering pipeline. |
|
| 20 | 20 | /// This is the maximum number of distinct texture objects available for rendering. |
|
| 21 | 21 | pub const DEFAULT_MAX_TEXTURE_LAYERS: usize = 16; |
|
| 22 | + | /// Size of the transform stack, in number of transform matrices. |
|
| 23 | + | pub const TRANSFORM_STACK_SIZE: usize = 24; |
|
| 22 | 24 | ||
| 23 | 25 | /// Errors that can occur during canvas operations. |
|
| 24 | 26 | pub enum Error<T: RenderBackend> { |
|
| 25 | 27 | /// Render backend error. |
|
| 26 | 28 | Render(T::Error), |
| 94 | 96 | /// |
|
| 95 | 97 | /// The system auto-allocates texture layers as needed when blitting images, up to the maximum |
|
| 96 | 98 | /// supported by the GPU (constant `N`). Each layer maintains its original size information, for |
|
| 97 | 99 | /// proper UV coordinate calculation in the shaders. |
|
| 98 | 100 | struct RenderState<const N: usize> { |
|
| 99 | - | /// Array of texture layers |
|
| 101 | + | /// Array of texture layers. |
|
| 100 | 102 | /// The first slot is reserved for the blank texture used in shape rendering. |
|
| 101 | - | textures: [Option<TextureLayer>; N], |
|
| 103 | + | textures: Array<TextureLayer, N>, |
|
| 102 | 104 | /// Parallel array to `textures` storing the dimensions of each image. |
|
| 103 | 105 | /// Used for UV coordinate calculations. |
|
| 104 | 106 | sizes: [Size<u32>; N], |
|
| 105 | 107 | /// Accumulates vertices for all paint operations in the current batch. |
|
| 106 | 108 | /// Used to minimize draw calls by batching compatible operations together. |
| 109 | 111 | queue: VecDeque<Op>, |
|
| 110 | 112 | } |
|
| 111 | 113 | ||
| 112 | 114 | impl<const N: usize> RenderState<N> { |
|
| 113 | 115 | fn new(texture: TextureInfo) -> Self { |
|
| 114 | - | let mut textures = [None; N]; |
|
| 116 | + | let mut textures = Array::new(); |
|
| 115 | 117 | let sizes = [texture.size; N]; |
|
| 116 | 118 | ||
| 117 | 119 | // The first layer contains a blank texture used for rendering shapes. |
|
| 118 | - | textures[0] = Some(TextureLayer { |
|
| 119 | - | image: None, |
|
| 120 | - | index: 0, |
|
| 121 | - | }); |
|
| 120 | + | textures |
|
| 121 | + | .push(TextureLayer { |
|
| 122 | + | image: None, |
|
| 123 | + | index: 0, |
|
| 124 | + | }) |
|
| 125 | + | .expect("RenderState::new: texture layer capacity must be greater than zero"); |
|
| 122 | 126 | ||
| 123 | 127 | Self { |
|
| 124 | 128 | textures, |
|
| 125 | 129 | sizes, |
|
| 126 | 130 | batch: VertexBatch::new(texture), |
|
| 127 | - | queue: VecDeque::new(), |
|
| 131 | + | queue: VecDeque::with_capacity(32), |
|
| 128 | 132 | } |
|
| 129 | 133 | } |
|
| 130 | 134 | ||
| 131 | 135 | // Helper function to find or create texture layer. |
|
| 132 | 136 | fn get_or_create_layer(&mut self, image: Image) -> Option<TextureLayer> { |
|
| 133 | 137 | // First try to find an existing layer with the given image. |
|
| 134 | - | if let Some(&layer) = self.textures.iter().flatten().find(|l| l.image == Some(image.id())) { |
|
| 138 | + | if let Some(&layer) = self.textures.iter().find(|l| l.image == Some(image.id())) { |
|
| 135 | 139 | return Some(layer); |
|
| 136 | 140 | } |
|
| 137 | 141 | // Then try to create new layer. |
|
| 138 | - | let index = self.textures.iter().position(Option::is_none)?; |
|
| 139 | 142 | let layer = TextureLayer { |
|
| 140 | 143 | image: Some(image.id()), |
|
| 141 | - | index: index as u8, |
|
| 144 | + | index: self.textures.len() as u8, |
|
| 142 | 145 | }; |
|
| 143 | - | self.textures[index] = Some(layer); |
|
| 144 | - | self.sizes[index] = image.size; |
|
| 146 | + | self.textures.push(layer).ok()?; |
|
| 147 | + | self.sizes[layer.index as usize] = image.size; |
|
| 145 | 148 | self.queue.push_back(Op::UploadImage { |
|
| 146 | 149 | image, |
|
| 147 | 150 | layer: layer.index, |
|
| 148 | 151 | }); |
|
| 149 | 152 | Some(layer) |
| 311 | 314 | ||
| 312 | 315 | Ok(Canvas { |
|
| 313 | 316 | offscreen, |
|
| 314 | 317 | debug: DebugState::default(), |
|
| 315 | 318 | action: Some(PassAction::default()), |
|
| 316 | - | stack: Vec::with_capacity(16), |
|
| 319 | + | stack: Array::new(), |
|
| 317 | 320 | shade, |
|
| 318 | 321 | present, |
|
| 319 | 322 | size, |
|
| 320 | 323 | size_scaled, |
|
| 321 | 324 | size_viewport: size_max, |
| 364 | 367 | /// Size of renderable viewport. This is the size available to render the canvas. |
|
| 365 | 368 | size_viewport: Size<f32>, |
|
| 366 | 369 | /// Size of the canvas, scaled (divided) by the `scale` factor. |
|
| 367 | 370 | size_scaled: Size<f32>, |
|
| 368 | 371 | /// Transform stack. Used by [`Canvas::save`] and [`Canvas::restore`]. |
|
| 369 | - | stack: Vec<Transform2D<Local, Screen>>, |
|
| 372 | + | stack: Array<Transform2D<Local, Screen>, TRANSFORM_STACK_SIZE>, |
|
| 370 | 373 | /// Current transform. |
|
| 371 | 374 | transform: Transform2D<Local, Screen>, |
|
| 372 | 375 | /// UI scale. |
|
| 373 | 376 | scale: f32, |
|
| 374 | 377 | /// Offscreen pass ID. |
| 459 | 462 | &mut self, |
|
| 460 | 463 | viewport: Size<f32>, |
|
| 461 | 464 | frame: &mut F, |
|
| 462 | 465 | ) -> Result<(), F::Error> { |
|
| 463 | 466 | let batch = self.render.batch.vertices(); |
|
| 464 | - | let length = batch.len(); |
|
| 465 | - | let chunk = self.config.max_vertices.min(length); |
|
| 466 | - | let bindings = &self.shade.bindings; |
|
| 467 | + | if batch.is_empty() { |
|
| 468 | + | return Ok(()); |
|
| 469 | + | } |
|
| 467 | 470 | ||
| 468 | 471 | frame.apply_pipeline(&self.shade.pipeline)?; |
|
| 469 | 472 | frame.apply_uniforms(&pipeline::Uniforms::new( |
|
| 470 | 473 | viewport, |
|
| 471 | 474 | Transform3D::identity(), |
|
| 472 | 475 | self.render.sizes, |
|
| 473 | 476 | ))?; |
|
| 474 | 477 | ||
| 475 | - | if !batch.is_empty() { |
|
| 476 | - | if length > self.config.max_vertices { |
|
| 477 | - | log::debug!( |
|
| 478 | - | target: "ui", |
|
| 479 | - | "vertex buffer capacity ({}) exceeded, \ |
|
| 480 | - | splitting batch render of {length} vertices into {} draw calls..", |
|
| 481 | - | self.config.max_vertices, |
|
| 482 | - | length.div_ceil(chunk) |
|
| 483 | - | ); |
|
| 484 | - | } |
|
| 485 | - | for chunk in batch.chunks(chunk) { |
|
| 486 | - | frame.buffer_update(bindings.vertex[0], BufferKind::Vertex, 0, chunk)?; |
|
| 487 | - | frame.apply_bindings(bindings)?; |
|
| 488 | - | frame.draw(0, chunk.len(), 1)?; |
|
| 489 | - | } |
|
| 478 | + | let bindings = &self.shade.bindings; |
|
| 479 | + | if batch.len() > self.config.max_vertices { |
|
| 480 | + | log::debug!( |
|
| 481 | + | target: "ui", |
|
| 482 | + | "vertex buffer capacity ({}) exceeded, \ |
|
| 483 | + | splitting batch render of {} vertices into {} draw calls..", |
|
| 484 | + | self.config.max_vertices, |
|
| 485 | + | batch.len(), |
|
| 486 | + | batch.len().div_ceil(self.config.max_vertices) |
|
| 487 | + | ); |
|
| 488 | + | } |
|
| 489 | + | for chunk in batch.chunks(self.config.max_vertices) { |
|
| 490 | + | frame.buffer_update(bindings.vertex[0], BufferKind::Vertex, 0, chunk)?; |
|
| 491 | + | frame.apply_bindings(bindings)?; |
|
| 492 | + | frame.draw(0, chunk.len(), 1)?; |
|
| 490 | 493 | } |
|
| 491 | 494 | Ok(()) |
|
| 492 | 495 | } |
|
| 493 | 496 | } |
|
| 494 | 497 |
| 515 | 518 | fn mask(&mut self, _shape: impl ToGeometry) { |
|
| 516 | 519 | todo!() |
|
| 517 | 520 | } |
|
| 518 | 521 | ||
| 519 | 522 | fn save(&mut self) { |
|
| 520 | - | self.stack.push(self.transform); |
|
| 523 | + | self.stack |
|
| 524 | + | .push(self.transform) |
|
| 525 | + | .expect("Canvas::save: transform stack capacity exceeded"); |
|
| 521 | 526 | } |
|
| 522 | 527 | ||
| 523 | 528 | fn restore(&mut self) { |
|
| 524 | 529 | self.transform = self.stack.pop().unwrap_or_default(); |
|
| 525 | 530 | } |