pub use wgpu;
mod block;
mod mesh;
mod plan;
mod target;
pub use block::block_on;
pub use mesh::{MeshRenderer, OrbitCamera, PreparedMeshes, mesh_budget};
pub use plan::{PlanRenderer, PlanTarget};
pub use target::{
AdapterSummary, OffscreenImage, RenderTargetCache, RenderTargetStats, RenderTextureRecycler,
RenderedTexture, render_offscreen, render_offscreen_with, render_texture,
render_texture_cached,
};
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[non_exhaustive]
pub enum GpuError {
#[error("invalid camera: distance, angles and aspect ratio must describe a frustum")]
InvalidCamera,
#[error("camera outside the representable range")]
CameraRange,
#[error("the scene is incomplete")]
IncompleteScene,
#[error("the scene has no triangles")]
EmptyScene,
#[error("mesh {0:?} is not opaque")]
Translucent(cadkit_scene::ObjectId),
#[error("mesh {0:?} has a triangle index out of range")]
InvalidTriangle(cadkit_scene::ObjectId),
#[error("mesh {mesh:?}: {source}")]
Precision {
mesh: cadkit_scene::ObjectId,
source: cadkit_scene::SceneError,
},
#[error("the scene exceeds the device buffer limit")]
BufferLimit,
#[error("texture size {0}x{1} is zero or exceeds the device limit")]
TextureSize(u32, u32),
#[error("no GPU adapter: {0}")]
AdapterUnavailable(String),
#[error("no GPU device: {0}")]
DeviceUnavailable(String),
#[error("GPU validation error: {0}")]
Validation(String),
#[error("GPU out of memory: {0}")]
OutOfMemory(String),
#[error("GPU device error: {0}")]
Device(String),
#[error("GPU poll failed: {0}")]
Poll(String),
#[error("GPU readback failed: {0}")]
Readback(String),
}
async fn check(
validation: wgpu::ErrorScopeGuard,
allocation: wgpu::ErrorScopeGuard,
) -> Result<(), GpuError> {
if let Some(error) = allocation.pop().await {
return Err(GpuError::OutOfMemory(error.to_string()));
}
if let Some(error) = validation.pop().await {
return Err(GpuError::Validation(error.to_string()));
}
Ok(())
}
fn scopes(device: &wgpu::Device) -> (wgpu::ErrorScopeGuard, wgpu::ErrorScopeGuard) {
let validation = device.push_error_scope(wgpu::ErrorFilter::Validation);
let allocation = device.push_error_scope(wgpu::ErrorFilter::OutOfMemory);
(validation, allocation)
}
fn linear(byte: u8) -> f64 {
let value = f64::from(byte) / 255.;
if value <= 0.04045 {
value / 12.92
} else {
((value + 0.055) / 1.055).powf(2.4)
}
}