diff --git a/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs b/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs index 155bb21c91d..e21efd41cac 100644 --- a/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs @@ -44,6 +44,24 @@ pub(super) fn tile_uniform([x, y, w, h]: [u32; 4], core: Option<[i32; 4]>) -> [f [x as f32, y as f32, w as f32, h as f32, cx0, cy0, cx1, cy1] } +/// Run `f` with its validation and out-of-memory errors caught, rather than reaching the +/// device's uncaptured-error handler (which takes the GPU canvas down for the session): what `f` +/// returned, and the first error. Native wgpu reports these synchronously; where they arrive +/// later (WebGPU) none is seen. +pub(super) fn catch_gpu_errors(device: &wgpu::Device, f: impl FnOnce() -> T) -> (T, Option) { + let validation = device.push_error_scope(wgpu::ErrorFilter::Validation); + let memory = device.push_error_scope(wgpu::ErrorFilter::OutOfMemory); + let out = f(); + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + let mut error = None; + for pop in [memory.pop(), validation.pop()] { + if let std::task::Poll::Ready(Some(e)) = std::pin::pin!(pop).poll(&mut cx) { + error.get_or_insert(e); + } + } + (out, error) +} + impl DocTextures { /// The texture set for a `size` canvas, or `None` (nothing created) when it doesn't fit /// the canvas budget or the GPU's memory. A set the GPU can't hold (a 3 GP canvas is 16 GB @@ -54,17 +72,7 @@ impl DocTextures { if bytes > res.canvas_budget.load(Ordering::Relaxed) { return None; } - let validation = device.push_error_scope(wgpu::ErrorFilter::Validation); - let memory = device.push_error_scope(wgpu::ErrorFilter::OutOfMemory); - let set = Self::create(device, res, size, tile, format); - // Native wgpu reports these synchronously; where they arrive later (WebGPU) none is seen. - let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); - let mut error = None; - for pop in [memory.pop(), validation.pop()] { - if let std::task::Poll::Ready(Some(e)) = std::pin::pin!(pop).poll(&mut cx) { - error.get_or_insert(e); - } - } + let (set, error) = catch_gpu_errors(device, || Self::create(device, res, size, tile, format)); if let Some(e) = error { log::warn!("{} MB of canvas textures failed ({e}); larger canvases are drawn on the CPU", bytes >> 20); res.canvas_budget.fetch_min(bytes.saturating_sub(1), Ordering::Relaxed); diff --git a/crates/ui-egui/src/canvas/gpu_canvas/mip_tests.rs b/crates/ui-egui/src/canvas/gpu_canvas/mip_tests.rs index feb0df375e8..8de45748b6f 100644 --- a/crates/ui-egui/src/canvas/gpu_canvas/mip_tests.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/mip_tests.rs @@ -66,6 +66,37 @@ fn canvas_mips_preserve_opaque_pixels_and_partial_updates() { } // On the same device (see the top of the file). canvas_textures_the_gpu_refuses_leave_it_working(&gpu); + refused_viewport_page_work_is_caught(&gpu); +} + +/// #5332 review: viewport page uploads run their texture, buffer and bind group creation and +/// their write inside the same error scopes as a texture set, so an error the GPU raises there +/// (out of memory, in use) is returned instead of disabling the GPU for the session. A texture +/// wider than the device allows is refused the same way, synchronously, on any adapter. +fn refused_viewport_page_work_is_caught(gpu: &GpuCanvas) { + let device = &gpu.rs.device; + let max = device.limits().max_texture_dimension_2d; + let (texture, error) = super::doc_textures::catch_gpu_errors(device, || { + device.create_texture(&wgpu::TextureDescriptor { + label: Some("too_wide_page"), + size: wgpu::Extent3d { width: max + 1, height: 1, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: FORMAT, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }) + }); + drop(texture); + assert!(error.is_some(), "the refusal is caught"); + assert!(gpu.health.is_ok(), "{:?}", gpu.fault()); + // A page the GPU accepts is stored as before. + let buf = Buffer::filled(Rect::from_xywh(0, 0, 64, 64), [0.5, 0.5, 0.5, 1.0]); + let core = Rect::new(1, 1, 63, 63); + assert!(gpu.upload_page(PageUpload { key: 9, page: 1, size: [64, 64], depth: SampleType::U8, factor: 1, core, buf: &buf })); + assert_eq!(gpu.pages(9).0, vec![1]); + assert!(gpu.health.is_ok(), "{:?}", gpu.fault()); } /// #4017: a 3 GP canvas's 16 GB of textures failed with Out of Memory, and the uncaptured error diff --git a/crates/ui-egui/src/canvas/gpu_canvas/pages.rs b/crates/ui-egui/src/canvas/gpu_canvas/pages.rs index b5f9fe7e7d9..05ff59ee407 100644 --- a/crates/ui-egui/src/canvas/gpu_canvas/pages.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/pages.rs @@ -2,7 +2,7 @@ //! `canvas::viewport_pages`): independent single-level textures at their own reduction, drawn //! by the same tile pipeline. They live under their own key, never next to a texture set. -use super::doc_textures::{init_buffer, tile_uniform}; +use super::doc_textures::{catch_gpu_errors, init_buffer, tile_uniform}; use super::resources::{DocTextures, Resources, Tile, f32_bytes}; use super::texels::{texel_bytes, texels}; use super::{GpuCanvas, Ordering, wgpu}; @@ -108,34 +108,47 @@ impl GpuCanvas { if held.saturating_add(bytes) > budget { return false; } - let texture = device.create_texture(&wgpu::TextureDescriptor { - label: Some("pc_viewport_page"), - size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format, - usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, - view_formats: &[], - }); - let view = texture.create_view(&Default::default()); let rect = [x, y, dw, dh]; - let uniform = - init_buffer(device, "pc_viewport_page", &f32_bytes(&tile_uniform(rect, Some([core.x0, core.y0, core.x1, core.y1]))), wgpu::BufferUsages::UNIFORM); - let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("pc_viewport_page"), - layout: &res.tile_bgl, - entries: &[ - wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&view) }, - wgpu::BindGroupEntry { binding: 1, resource: uniform.as_entire_binding() }, - ], + // A page the GPU refuses (out of memory) fails alone: it falls back to the CPU canvas + // instead of the uncaptured error disabling the GPU canvas for the session. + let ((texture, view, uniform, bind_group), error) = catch_gpu_errors(device, || { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("pc_viewport_page"), + size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + let view = texture.create_view(&Default::default()); + let uniform = init_buffer( + device, + "pc_viewport_page", + &f32_bytes(&tile_uniform(rect, Some([core.x0, core.y0, core.x1, core.y1]))), + wgpu::BufferUsages::UNIFORM, + ); + let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("pc_viewport_page"), + layout: &res.tile_bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&view) }, + wgpu::BindGroupEntry { binding: 1, resource: uniform.as_entire_binding() }, + ], + }); + queue.write_texture( + texture.as_image_copy(), + &texels(format, &buf.px), + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(w * texel_bytes(format) as u32), rows_per_image: Some(h) }, + wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + ); + (texture, view, uniform, bind_group) }); - queue.write_texture( - texture.as_image_copy(), - &texels(format, &buf.px), - wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(w * texel_bytes(format) as u32), rows_per_image: Some(h) }, - wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, - ); + if let Some(e) = error { + log::warn!("a {w}×{h} viewport page couldn't be stored on the GPU: {e}"); + return false; + } let Some(d) = res.docs.get_mut(&key) else { return false }; if let Some(i) = replaced { d.tiles.remove(i);