diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index f46acb8c7f..69ace65c72 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -254,24 +254,32 @@ impl Renderer { offset, content, } => { - let clip_layer = Layer::new( - Rectangle { - x: bounds.x as u32 - layer.offset.x, - y: bounds.y as u32 - layer.offset.y, - width: bounds.width as u32, - height: bounds.height as u32, - }, - layer.offset + *offset, - ); - - let new_layer = Layer::new(layer.bounds, layer.offset); - - layers.push(clip_layer); - - // TODO: Primitive culling - self.draw_primitive(content, layers); - - layers.push(new_layer); + let x = bounds.x as i32 - layer.offset.x as i32; + let y = bounds.y as i32 - layer.offset.y as i32; + let width = (bounds.width as i32 + x).min(bounds.width as i32); + let height = + (bounds.height as i32 + y).min(bounds.height as i32); + + // Only draw visible content on-screen + // TODO: Also, check for parent layer bounds to avoid further + // drawing in some circumstances. + if width > 0 && height > 0 { + let clip_layer = Layer::new( + Rectangle { + x: x.max(0) as u32, + y: y.max(0) as u32, + width: width as u32, + height: height as u32, + }, + layer.offset + *offset, + ); + + let new_layer = Layer::new(layer.bounds, layer.offset); + + layers.push(clip_layer); + self.draw_primitive(content, layers); + layers.push(new_layer); + } } } }