1pub use wgpu;
20
21mod block;
22mod mesh;
23mod plan;
24mod target;
25
26pub use block::block_on;
27pub use mesh::{MeshRenderer, OrbitCamera, PreparedMeshes, mesh_budget};
28pub use plan::{PlanRenderer, PlanTarget};
29pub use target::{
30 AdapterSummary, OffscreenImage, RenderTargetCache, RenderTargetStats, RenderTextureRecycler,
31 RenderedTexture, render_offscreen, render_offscreen_with, render_texture,
32 render_texture_cached,
33};
34
35#[derive(Debug, Clone, PartialEq, thiserror::Error)]
37#[non_exhaustive]
38pub enum GpuError {
39 #[error("invalid camera: distance, angles and aspect ratio must describe a frustum")]
41 InvalidCamera,
42 #[error("camera outside the representable range")]
44 CameraRange,
45 #[error("the scene is incomplete")]
47 IncompleteScene,
48 #[error("the scene has no triangles")]
50 EmptyScene,
51 #[error("mesh {0:?} is not opaque")]
53 Translucent(cadkit_scene::ObjectId),
54 #[error("mesh {0:?} has a triangle index out of range")]
56 InvalidTriangle(cadkit_scene::ObjectId),
57 #[error("mesh {mesh:?}: {source}")]
59 Precision {
60 mesh: cadkit_scene::ObjectId,
62 source: cadkit_scene::SceneError,
64 },
65 #[error("the scene exceeds the device buffer limit")]
67 BufferLimit,
68 #[error("texture size {0}x{1} is zero or exceeds the device limit")]
70 TextureSize(u32, u32),
71 #[error("no GPU adapter: {0}")]
73 AdapterUnavailable(String),
74 #[error("no GPU device: {0}")]
76 DeviceUnavailable(String),
77 #[error("GPU validation error: {0}")]
79 Validation(String),
80 #[error("GPU out of memory: {0}")]
82 OutOfMemory(String),
83 #[error("GPU device error: {0}")]
85 Device(String),
86 #[error("GPU poll failed: {0}")]
88 Poll(String),
89 #[error("GPU readback failed: {0}")]
91 Readback(String),
92}
93
94async fn check(
96 validation: wgpu::ErrorScopeGuard,
97 allocation: wgpu::ErrorScopeGuard,
98) -> Result<(), GpuError> {
99 if let Some(error) = allocation.pop().await {
100 return Err(GpuError::OutOfMemory(error.to_string()));
101 }
102 if let Some(error) = validation.pop().await {
103 return Err(GpuError::Validation(error.to_string()));
104 }
105 Ok(())
106}
107
108fn scopes(device: &wgpu::Device) -> (wgpu::ErrorScopeGuard, wgpu::ErrorScopeGuard) {
110 let validation = device.push_error_scope(wgpu::ErrorFilter::Validation);
111 let allocation = device.push_error_scope(wgpu::ErrorFilter::OutOfMemory);
112 (validation, allocation)
113}
114
115fn linear(byte: u8) -> f64 {
117 let value = f64::from(byte) / 255.;
118 if value <= 0.04045 {
119 value / 12.92
120 } else {
121 ((value + 0.055) / 1.055).powf(2.4)
122 }
123}