use std::{
borrow::Cow,
error::Error,
fmt,
ops::Range,
sync::Arc,
time::{Duration, Instant},
};
use crate::{
Camera2d, Circle, Color, ColorMap, DrawCommand, Fill, Line, LogicalScreenPosition,
LogicalViewport, ParticleInstance2d, PhysicalScreenPosition, Polyline, Rect, ScalarField,
Scene, ScreenClipRect, Shadow, ShapeStyle, Stroke, Vec2,
};
use config::select_surface_present_mode;
const INITIAL_VERTEX_CAPACITY: usize = 4096;
const CIRCLE_SEGMENTS: usize = 64;
const ROUND_CAP_SEGMENTS: usize = 16;
const CORNER_SEGMENTS: usize = 12;
const PREFERRED_SAMPLE_COUNT: u32 = 4;
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct Vertex {
world_position: [f32; 2],
depth: f32,
screen_offset: [f32; 2],
previous_direction: [f32; 2],
next_direction: [f32; 2],
normal_distance: f32,
color: [f32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct DynamicGpu {
world_position: [f32; 2],
depth: f32,
color: [f32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct CameraUniform {
camera_center: [f32; 4],
world_to_screen_x: [f32; 4],
world_to_screen_y: [f32; 4],
screen_to_clip: [f32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct HeatmapUniform {
value_range: [f32; 4],
dimensions: [u32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct CompositeUniform {
opacity: [f32; 4],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ScalarFieldSampling {
#[default]
Nearest,
Linear,
}
impl ScalarFieldSampling {
fn shader_value(self) -> u32 {
match self {
Self::Nearest => 0,
Self::Linear => 1,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BlendMode {
#[default]
Alpha,
Additive,
Replace,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RenderTargetLoad {
Load,
Clear(Color),
}
#[derive(Debug, Clone, Copy)]
struct GeometryExtents {
world_min: Vec2,
world_max: Vec2,
depth_min: f32,
depth_max: f32,
direction_min: Vec2,
direction_max: Vec2,
screen_offset_max_abs: Vec2,
normal_distance_max_abs: f32,
empty: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ScissorRect {
x: u32,
y: u32,
width: u32,
height: u32,
}
#[derive(Debug, Clone, Copy)]
struct ParticleDrawPreparation {
visible_count: usize,
upload: Duration,
camera_uniform_upload: Duration,
}
#[derive(Debug, Clone, PartialEq)]
struct PreparedDrawBatch {
vertex_range: Range<u32>,
screen_clip: Option<ScreenClipRect>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TessellationStats {
command_count: usize,
rendered_command_count: usize,
dropped_command_count: usize,
}
impl TessellationStats {
pub fn command_count(self) -> usize {
self.command_count
}
pub fn rendered_command_count(self) -> usize {
self.rendered_command_count
}
pub fn dropped_command_count(self) -> usize {
self.dropped_command_count
}
}
impl Vertex {
const ATTRIBUTES: [wgpu::VertexAttribute; 7] = wgpu::vertex_attr_array![
0 => Float32x2,
1 => Float32,
2 => Float32x2,
3 => Float32x2,
4 => Float32x2,
5 => Float32,
6 => Float32x4
];
const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &Self::ATTRIBUTES,
};
fn is_finite(self) -> bool {
self.world_position.iter().all(|value| value.is_finite())
&& self.depth.is_finite()
&& self.screen_offset.iter().all(|value| value.is_finite())
&& self
.previous_direction
.iter()
.all(|value| value.is_finite())
&& self.next_direction.iter().all(|value| value.is_finite())
&& self.normal_distance.is_finite()
&& self.color.iter().all(|value| value.is_finite())
}
}
impl DynamicGpu {
const ATTRIBUTES: [wgpu::VertexAttribute; 3] =
wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32, 2 => Float32x4];
const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &Self::ATTRIBUTES,
};
}
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct ParticleUnitVertex {
direction: [f32; 2],
}
impl ParticleUnitVertex {
const ATTRIBUTES: [wgpu::VertexAttribute; 1] = wgpu::vertex_attr_array![0 => Float32x2];
const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &Self::ATTRIBUTES,
};
}
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct ParticleGpu {
world_position: [f32; 2],
depth: f32,
radius: f32,
color: [f32; 4],
}
impl ParticleGpu {
const ATTRIBUTES: [wgpu::VertexAttribute; 4] = wgpu::vertex_attr_array![
1 => Float32x2,
2 => Float32,
3 => Float32,
4 => Float32x4
];
const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &Self::ATTRIBUTES,
};
fn screen_position(self, camera: CameraUniform) -> Vec2 {
let relative_x = self.world_position[0] - camera.camera_center[0];
let relative_y = self.world_position[1] - camera.camera_center[1];
Vec2::new(
camera.world_to_screen_x[0] * relative_x
+ camera.world_to_screen_x[1] * relative_y
+ camera.world_to_screen_x[2] * self.depth
+ camera.world_to_screen_x[3],
camera.world_to_screen_y[0] * relative_x
+ camera.world_to_screen_y[1] * relative_y
+ camera.world_to_screen_y[2] * self.depth
+ camera.world_to_screen_y[3],
)
}
fn is_safe_for(self, camera: CameraUniform) -> bool {
let relative_x = self.world_position[0] - camera.camera_center[0];
let relative_y = self.world_position[1] - camera.camera_center[1];
let screen = self.screen_position(camera);
let screen_x = screen.x;
let screen_y = screen.y;
let clip_x = screen_x * camera.screen_to_clip[0] + camera.screen_to_clip[2];
let clip_y = screen_y * camera.screen_to_clip[1] + camera.screen_to_clip[3];
relative_x.is_finite()
&& relative_y.is_finite()
&& screen_x.is_finite()
&& screen_y.is_finite()
&& (screen_x + self.radius).is_finite()
&& (screen_x - self.radius).is_finite()
&& (screen_y + self.radius).is_finite()
&& (screen_y - self.radius).is_finite()
&& clip_x.is_finite()
&& clip_y.is_finite()
}
fn intersects_viewport(self, camera: CameraUniform, viewport: LogicalViewport) -> bool {
let screen = self.screen_position(camera);
screen.x + self.radius >= 0.0
&& screen.x - self.radius <= viewport.width()
&& screen.y + self.radius >= 0.0
&& screen.y - self.radius <= viewport.height()
}
}
impl CameraUniform {
fn new(camera: Camera2d, viewport: LogicalViewport) -> Option<Self> {
let projection_cosine = camera.projection().tilt().cos();
let rotation_cosine = camera.rotation().cos();
let rotation_sine = camera.rotation().sin();
let zoom = camera.zoom();
let center = camera.center();
let projection_sine = camera.projection().tilt().sin();
let depth_scale = camera.projection().depth_scale();
let horizontal_x = zoom * rotation_cosine;
let horizontal_y = -zoom * rotation_sine * projection_cosine;
let vertical_x = -zoom * rotation_sine;
let vertical_y = -zoom * rotation_cosine * projection_cosine;
let horizontal_depth =
zoom * depth_scale * projection_sine * (rotation_cosine * 0.5 - rotation_sine);
let vertical_depth =
-zoom * depth_scale * projection_sine * (rotation_sine * 0.5 + rotation_cosine);
let uniform = Self {
camera_center: [center.x, center.y, 0.0, 0.0],
world_to_screen_x: [
horizontal_x,
horizontal_y,
horizontal_depth,
viewport.width() * 0.5,
],
world_to_screen_y: [
vertical_x,
vertical_y,
vertical_depth,
viewport.height() * 0.5,
],
screen_to_clip: [2.0 / viewport.width(), -2.0 / viewport.height(), -1.0, 1.0],
};
uniform.is_finite().then_some(uniform)
}
fn is_finite(self) -> bool {
self.camera_center
.iter()
.chain(self.world_to_screen_x.iter())
.chain(self.world_to_screen_y.iter())
.chain(self.screen_to_clip.iter())
.all(|value| value.is_finite())
}
#[cfg(test)]
fn world_to_screen(self, world: Vec2, depth: f32) -> Vec2 {
let relative = world - Vec2::new(self.camera_center[0], self.camera_center[1]);
Vec2::new(
self.world_to_screen_x[0] * relative.x
+ self.world_to_screen_x[1] * relative.y
+ self.world_to_screen_x[2] * depth
+ self.world_to_screen_x[3],
self.world_to_screen_y[0] * relative.x
+ self.world_to_screen_y[1] * relative.y
+ self.world_to_screen_y[2] * depth
+ self.world_to_screen_y[3],
)
}
#[cfg(test)]
fn direction_to_screen(self, direction: Vec2) -> Vec2 {
Vec2::new(
self.world_to_screen_x[0] * direction.x + self.world_to_screen_x[1] * direction.y,
self.world_to_screen_y[0] * direction.x + self.world_to_screen_y[1] * direction.y,
)
}
}
impl GeometryExtents {
fn empty(is_empty: bool) -> Self {
Self {
world_min: Vec2::splat(f32::INFINITY),
world_max: Vec2::splat(f32::NEG_INFINITY),
depth_min: f32::INFINITY,
depth_max: f32::NEG_INFINITY,
direction_min: Vec2::splat(f32::INFINITY),
direction_max: Vec2::splat(f32::NEG_INFINITY),
screen_offset_max_abs: Vec2::ZERO,
normal_distance_max_abs: 0.0,
empty: is_empty,
}
}
fn include(&mut self, vertex: Vertex) {
let world = Vec2::new(vertex.world_position[0], vertex.world_position[1]);
self.world_min.x = self.world_min.x.min(world.x);
self.world_min.y = self.world_min.y.min(world.y);
self.world_max.x = self.world_max.x.max(world.x);
self.world_max.y = self.world_max.y.max(world.y);
self.depth_min = self.depth_min.min(vertex.depth);
self.depth_max = self.depth_max.max(vertex.depth);
for direction in [vertex.previous_direction, vertex.next_direction] {
self.direction_min.x = self.direction_min.x.min(direction[0]);
self.direction_min.y = self.direction_min.y.min(direction[1]);
self.direction_max.x = self.direction_max.x.max(direction[0]);
self.direction_max.y = self.direction_max.y.max(direction[1]);
}
self.screen_offset_max_abs.x = self
.screen_offset_max_abs
.x
.max(vertex.screen_offset[0].abs());
self.screen_offset_max_abs.y = self
.screen_offset_max_abs
.y
.max(vertex.screen_offset[1].abs());
self.normal_distance_max_abs = self
.normal_distance_max_abs
.max(vertex.normal_distance.abs());
}
fn include_dynamic(&mut self, vertex: DynamicGpu) {
let world = Vec2::new(vertex.world_position[0], vertex.world_position[1]);
self.world_min.x = self.world_min.x.min(world.x);
self.world_min.y = self.world_min.y.min(world.y);
self.world_max.x = self.world_max.x.max(world.x);
self.world_max.y = self.world_max.y.max(world.y);
self.depth_min = self.depth_min.min(vertex.depth);
self.depth_max = self.depth_max.max(vertex.depth);
self.direction_min = Vec2::ZERO;
self.direction_max = Vec2::ZERO;
}
fn from_dynamic_vertices(vertices: &[DynamicGpu]) -> Self {
let mut extents = Self::empty(vertices.is_empty());
for vertex in vertices {
extents.include_dynamic(*vertex);
}
extents
}
fn from_vertices(vertices: &[Vertex]) -> Self {
let mut extents = Self::empty(vertices.is_empty());
for vertex in vertices {
extents.include(*vertex);
}
extents
}
fn is_safe_for(self, uniform: CameraUniform) -> bool {
if self.empty {
return true;
}
let center = Vec2::new(uniform.camera_center[0], uniform.camera_center[1]);
let world_horizontal = transformed_world_interval(
uniform.world_to_screen_x,
self.world_min,
self.world_max,
self.depth_min,
self.depth_max,
center,
);
let world_vertical = transformed_world_interval(
uniform.world_to_screen_y,
self.world_min,
self.world_max,
self.depth_min,
self.depth_max,
center,
);
let direction_horizontal = transformed_direction_interval(
uniform.world_to_screen_x,
self.direction_min,
self.direction_max,
);
let direction_vertical = transformed_direction_interval(
uniform.world_to_screen_y,
self.direction_min,
self.direction_max,
);
let maximum_miter = self.normal_distance_max_abs as f64 * 1_000.0;
let horizontal_limit = interval_max_abs(world_horizontal)
+ self.screen_offset_max_abs.x as f64
+ maximum_miter;
let vertical_limit =
interval_max_abs(world_vertical) + self.screen_offset_max_abs.y as f64 + maximum_miter;
[
world_horizontal.0,
world_horizontal.1,
world_vertical.0,
world_vertical.1,
direction_horizontal.0,
direction_horizontal.1,
direction_vertical.0,
direction_vertical.1,
horizontal_limit,
vertical_limit,
]
.into_iter()
.all(|value| value.is_finite() && value.abs() <= f32::MAX as f64)
}
}
fn transformed_world_interval(
row: [f32; 4],
minimum: Vec2,
maximum: Vec2,
depth_minimum: f32,
depth_maximum: f32,
center: Vec2,
) -> (f64, f64) {
let relative_minimum = minimum - center;
let relative_maximum = maximum - center;
let horizontal = interval_products(row[0], relative_minimum.x, relative_maximum.x);
let vertical = interval_products(row[1], relative_minimum.y, relative_maximum.y);
let depth = interval_products(row[2], depth_minimum, depth_maximum);
(
horizontal.0 + vertical.0 + depth.0 + row[3] as f64,
horizontal.1 + vertical.1 + depth.1 + row[3] as f64,
)
}
fn transformed_direction_interval(row: [f32; 4], minimum: Vec2, maximum: Vec2) -> (f64, f64) {
let horizontal = interval_products(row[0], minimum.x, maximum.x);
let vertical = interval_products(row[1], minimum.y, maximum.y);
(horizontal.0 + vertical.0, horizontal.1 + vertical.1)
}
fn interval_products(coefficient: f32, minimum: f32, maximum: f32) -> (f64, f64) {
let first = coefficient as f64 * minimum as f64;
let second = coefficient as f64 * maximum as f64;
(first.min(second), first.max(second))
}
fn interval_max_abs(interval: (f64, f64)) -> f64 {
interval.0.abs().max(interval.1.abs())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RenderStatus {
Drawn,
Skipped(RendererSurfaceStatus),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct RendererFrameMetrics {
tessellation: Duration,
upload: Duration,
camera_uniform_upload: Duration,
surface_acquire: Duration,
encode_submit_present: Duration,
total_cpu: Duration,
geometry_reused: bool,
geometry_streamed: bool,
tessellation_stats: TessellationStats,
}
impl RendererFrameMetrics {
pub fn tessellation(self) -> Duration {
self.tessellation
}
pub fn upload(self) -> Duration {
self.upload
}
pub fn camera_uniform_upload(self) -> Duration {
self.camera_uniform_upload
}
pub fn surface_acquire(self) -> Duration {
self.surface_acquire
}
pub fn encode_submit_present(self) -> Duration {
self.encode_submit_present
}
pub fn total_cpu(self) -> Duration {
self.total_cpu
}
pub fn geometry_reused(self) -> bool {
self.geometry_reused
}
pub fn geometry_streamed(self) -> bool {
self.geometry_streamed
}
pub fn tessellation_stats(self) -> TessellationStats {
self.tessellation_stats
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RenderReport {
status: RenderStatus,
metrics: RendererFrameMetrics,
}
impl RenderReport {
pub fn status(self) -> RenderStatus {
self.status
}
pub fn metrics(self) -> RendererFrameMetrics {
self.metrics
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RendererSurfaceStatus {
Timeout,
Occluded,
Outdated,
Lost,
Validation,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RendererFrameError {
Surface(RendererSurfaceStatus),
InvalidGeometryTransform,
GeometryCapacityTooLarge,
}
impl fmt::Display for RendererFrameError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Surface(status) => write!(formatter, "renderer surface failed: {status:?}"),
Self::InvalidGeometryTransform => {
write!(formatter, "camera and geometry overflow the GPU transform")
}
Self::GeometryCapacityTooLarge => {
write!(formatter, "geometry exceeds the GPU vertex-buffer limit")
}
}
}
}
impl Error for RendererFrameError {}
#[derive(Debug)]
pub enum RendererInitError {
CreateSurface(wgpu::CreateSurfaceError),
RequestAdapter(wgpu::RequestAdapterError),
RequestDevice(wgpu::RequestDeviceError),
NoSurfaceConfig,
RecoveryLimitReached {
limit: usize,
},
}
impl fmt::Display for RendererInitError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CreateSurface(error) => write!(formatter, "failed to create surface: {error}"),
Self::RequestAdapter(error) => write!(formatter, "failed to request adapter: {error}"),
Self::RequestDevice(error) => write!(formatter, "failed to request device: {error}"),
Self::NoSurfaceConfig => write!(formatter, "surface has no supported default config"),
Self::RecoveryLimitReached { limit } => write!(
formatter,
"device recovery limit reached with {limit} quarantined devices"
),
}
}
}
impl Error for RendererInitError {}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RendererConfigurationError {
InvalidScaleFactor {
scale_factor: f64,
},
InvalidRecoveryLimit {
limit: usize,
},
}
impl fmt::Display for RendererConfigurationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidScaleFactor { scale_factor } => write!(
formatter,
"renderer scale factor must be finite, positive, representable as f32, and keep a u32 surface finite in logical pixels, got {scale_factor}"
),
Self::InvalidRecoveryLimit { limit } => write!(
formatter,
"renderer recovery quarantine must retain between 1 and 8 devices, got {limit}"
),
}
}
}
impl Error for RendererConfigurationError {}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RendererCoordinateError {
NonFiniteConversion,
}
impl fmt::Display for RendererCoordinateError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"screen coordinate conversion produced a non-finite result"
)
}
}
impl Error for RendererCoordinateError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PreparedSceneError {
CapacityTooLarge,
}
impl fmt::Display for PreparedSceneError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::CapacityTooLarge => {
write!(
formatter,
"prepared scene exceeds the GPU vertex-buffer limit"
)
}
}
}
}
impl Error for PreparedSceneError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PreparedSceneRenderError {
RendererMismatch,
Frame(RendererFrameError),
}
impl fmt::Display for PreparedSceneRenderError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RendererMismatch => {
write!(formatter, "prepared scene belongs to a different renderer")
}
Self::Frame(error) => write!(formatter, "prepared frame failed: {error}"),
}
}
}
impl Error for PreparedSceneRenderError {}
pub struct PreparedScene {
renderer_identity: Arc<()>,
background: Color,
vertex_buffer: Arc<wgpu::Buffer>,
vertices: Arc<[Vertex]>,
command_count: usize,
vertex_count: usize,
geometry_extents: GeometryExtents,
draw_batches: Vec<PreparedDrawBatch>,
tessellation: TessellationStats,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DynamicVertex2d {
world_position: Vec2,
depth: f32,
color: Color,
}
impl DynamicVertex2d {
pub fn new(world_position: Vec2, depth: f32, color: Color) -> Result<Self, DynamicMeshError> {
if !world_position.is_finite() || !depth.is_finite() || !color.is_normalized() {
return Err(DynamicMeshError::InvalidVertex);
}
Ok(Self {
world_position,
depth,
color,
})
}
pub fn world_position(self) -> Vec2 {
self.world_position
}
pub fn depth(self) -> f32 {
self.depth
}
pub fn color(self) -> Color {
self.color
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DynamicMeshError {
InvalidVertexCount,
InvalidVertex,
UpdateRangeOutOfBounds,
RendererMismatch,
CapacityTooLarge,
}
impl fmt::Display for DynamicMeshError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidVertexCount => write!(
formatter,
"dynamic mesh vertex count must be divisible by three"
),
Self::InvalidVertex => write!(
formatter,
"dynamic mesh positions/depth must be finite and colors normalized"
),
Self::UpdateRangeOutOfBounds => write!(
formatter,
"dynamic mesh update range is outside the current mesh"
),
Self::RendererMismatch => {
write!(formatter, "dynamic mesh belongs to a different renderer")
}
Self::CapacityTooLarge => {
write!(
formatter,
"dynamic mesh exceeds the GPU vertex-buffer limit"
)
}
}
}
}
impl Error for DynamicMeshError {}
pub struct DynamicMesh2d {
renderer_identity: Arc<()>,
vertex_buffer: Arc<wgpu::Buffer>,
vertices: Vec<DynamicGpu>,
vertex_capacity: usize,
geometry_extents: GeometryExtents,
}
impl DynamicMesh2d {
pub fn vertex_count(&self) -> usize {
self.vertices.len()
}
pub fn vertex_capacity(&self) -> usize {
self.vertex_capacity
}
pub fn recovery_memory_bytes(&self) -> usize {
self.vertices.len() * std::mem::size_of::<DynamicGpu>()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DynamicMeshUpdateReport {
vertex_count: usize,
upload: Duration,
reallocated: bool,
}
impl DynamicMeshUpdateReport {
pub fn vertex_count(self) -> usize {
self.vertex_count
}
pub fn upload(self) -> Duration {
self.upload
}
pub fn reallocated(self) -> bool {
self.reallocated
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ParticleStatistics {
submitted: usize,
visibility_checked: usize,
visible: usize,
culled: usize,
budget_limited: usize,
dropped: usize,
rendered: usize,
}
impl ParticleStatistics {
pub fn submitted(self) -> usize {
self.submitted
}
pub fn visibility_checked(self) -> usize {
self.visibility_checked
}
pub fn visible(self) -> usize {
self.visible
}
pub fn culled(self) -> usize {
self.culled
}
pub fn budget_limited(self) -> usize {
self.budget_limited
}
pub fn dropped(self) -> usize {
self.dropped
}
pub fn rendered(self) -> usize {
self.rendered
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParticleRenderBudget {
max_visible_instances: usize,
max_gpu_bytes: usize,
max_upload_bytes_per_frame: usize,
max_visibility_checks_per_frame: usize,
}
impl ParticleRenderBudget {
pub const UNBOUNDED: Self = Self {
max_visible_instances: usize::MAX,
max_gpu_bytes: usize::MAX,
max_upload_bytes_per_frame: usize::MAX,
max_visibility_checks_per_frame: usize::MAX,
};
pub fn new(
max_visible_instances: usize,
max_gpu_bytes: usize,
max_upload_bytes_per_frame: usize,
) -> Result<Self, ParticleBudgetError> {
let minimum_bytes = std::mem::size_of::<ParticleGpu>();
if max_visible_instances == 0
|| max_gpu_bytes < minimum_bytes
|| max_upload_bytes_per_frame < minimum_bytes
{
return Err(ParticleBudgetError::InvalidLimit);
}
Ok(Self {
max_visible_instances,
max_gpu_bytes,
max_upload_bytes_per_frame,
max_visibility_checks_per_frame: usize::MAX,
})
}
pub fn with_max_visibility_checks(
mut self,
max_visibility_checks_per_frame: usize,
) -> Result<Self, ParticleBudgetError> {
if max_visibility_checks_per_frame < self.instance_limit() {
return Err(ParticleBudgetError::InvalidLimit);
}
self.max_visibility_checks_per_frame = max_visibility_checks_per_frame;
Ok(self)
}
pub const fn max_visible_instances(self) -> usize {
self.max_visible_instances
}
pub const fn max_gpu_bytes(self) -> usize {
self.max_gpu_bytes
}
pub const fn max_upload_bytes_per_frame(self) -> usize {
self.max_upload_bytes_per_frame
}
pub const fn max_visibility_checks_per_frame(self) -> usize {
self.max_visibility_checks_per_frame
}
fn instance_limit(self) -> usize {
self.max_visible_instances
.min(self.max_gpu_bytes / std::mem::size_of::<ParticleGpu>())
.min(self.max_upload_bytes_per_frame / std::mem::size_of::<ParticleGpu>())
}
}
impl Default for ParticleRenderBudget {
fn default() -> Self {
Self::UNBOUNDED
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParticleBudgetError {
InvalidLimit,
}
impl fmt::Display for ParticleBudgetError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"particle budget must fit at least one visible instance"
)
}
}
impl Error for ParticleBudgetError {}
pub struct ParticleField2d {
renderer_identity: Arc<()>,
instance_buffer: Arc<wgpu::Buffer>,
instances: Vec<ParticleGpu>,
visible_instances: Vec<ParticleGpu>,
instance_capacity: usize,
budget: ParticleRenderBudget,
statistics: ParticleStatistics,
}
impl ParticleField2d {
pub fn instance_count(&self) -> usize {
self.instances.len()
}
pub fn instance_capacity(&self) -> usize {
self.instance_capacity
}
pub fn statistics(&self) -> ParticleStatistics {
self.statistics
}
pub fn budget(&self) -> ParticleRenderBudget {
self.budget
}
pub fn recovery_memory_bytes(&self) -> usize {
self.instances.len() * std::mem::size_of::<ParticleGpu>()
}
pub fn cpu_allocation_bytes(&self) -> usize {
self.instances
.capacity()
.saturating_add(self.visible_instances.capacity())
.saturating_mul(std::mem::size_of::<ParticleGpu>())
}
pub fn gpu_allocation_bytes(&self) -> usize {
self.instance_capacity
.saturating_mul(std::mem::size_of::<ParticleGpu>())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParticleFieldError {
InvalidInstance,
UpdateRangeOutOfBounds,
RendererMismatch,
CapacityTooLarge,
}
impl fmt::Display for ParticleFieldError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidInstance => write!(formatter, "particle field instances must be valid"),
Self::UpdateRangeOutOfBounds => {
write!(
formatter,
"particle update range is outside the current field"
)
}
Self::RendererMismatch => {
write!(formatter, "particle field belongs to a different renderer")
}
Self::CapacityTooLarge => {
write!(
formatter,
"particle field exceeds the GPU instance-buffer limit"
)
}
}
}
}
impl Error for ParticleFieldError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParticleFieldUpdateReport {
statistics: ParticleStatistics,
preparation: Duration,
reallocated: bool,
}
impl ParticleFieldUpdateReport {
pub fn statistics(self) -> ParticleStatistics {
self.statistics
}
pub fn preparation(self) -> Duration {
self.preparation
}
#[deprecated(note = "particle GPU upload is deferred to render; use preparation()")]
pub fn upload(self) -> Duration {
self.preparation
}
pub fn reallocated(self) -> bool {
self.reallocated
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParticleFieldRenderError {
RendererMismatch,
InvalidBackground,
InvalidGeometryTransform,
Frame(RendererFrameError),
}
impl fmt::Display for ParticleFieldRenderError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RendererMismatch => {
write!(formatter, "particle field belongs to a different renderer")
}
Self::InvalidBackground => {
write!(formatter, "particle field background must be normalized")
}
Self::InvalidGeometryTransform => {
write!(formatter, "particle field geometry transform is invalid")
}
Self::Frame(error) => write!(formatter, "particle field frame failed: {error}"),
}
}
}
impl Error for ParticleFieldRenderError {}
pub struct ScalarFieldTexture {
renderer_identity: Arc<()>,
texture: wgpu::Texture,
field: ScalarField,
}
impl ScalarFieldTexture {
pub fn width(&self) -> usize {
self.field.width()
}
pub fn height(&self) -> usize {
self.field.height()
}
pub fn field(&self) -> &ScalarField {
&self.field
}
pub fn recovery_memory_bytes(&self) -> usize {
std::mem::size_of_val(self.field.values())
}
pub fn gpu_allocation_bytes(&self) -> usize {
self.recovery_memory_bytes()
}
}
pub struct RenderTarget2d {
renderer_identity: Arc<()>,
resource_identity: Arc<()>,
_texture: wgpu::Texture,
view: wgpu::TextureView,
width: u32,
height: u32,
allocation_bytes: usize,
}
pub struct TrailBuffer2d {
renderer_identity: Arc<()>,
front: RenderTarget2d,
back: RenderTarget2d,
}
impl TrailBuffer2d {
pub fn width(&self) -> u32 {
self.front.width()
}
pub fn height(&self) -> u32 {
self.front.height()
}
pub fn size(&self) -> (u32, u32) {
self.front.size()
}
pub fn allocation_bytes(&self) -> usize {
self.front
.allocation_bytes()
.saturating_add(self.back.allocation_bytes())
}
}
impl RenderTarget2d {
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
pub fn size(&self) -> (u32, u32) {
(self.width, self.height)
}
pub fn allocation_bytes(&self) -> usize {
self.allocation_bytes
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RenderTargetError {
ZeroDimension,
DimensionsTooLarge,
RendererMismatch,
InvalidOpacity,
SourceAliasesDestination,
InvalidBackground,
Frame(RendererFrameError),
}
impl fmt::Display for RenderTargetError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ZeroDimension => write!(formatter, "render target dimensions must be non-zero"),
Self::DimensionsTooLarge => {
write!(formatter, "render target dimensions exceed device limits")
}
Self::RendererMismatch => {
write!(formatter, "render target belongs to a different renderer")
}
Self::InvalidOpacity => write!(
formatter,
"render target opacity must be finite and in 0.0..=1.0"
),
Self::SourceAliasesDestination => {
write!(
formatter,
"trail source must be distinct from its destination targets"
)
}
Self::InvalidBackground => {
write!(formatter, "render target background must be normalized")
}
Self::Frame(error) => write!(formatter, "render target frame failed: {error}"),
}
}
}
impl Error for RenderTargetError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScalarFieldTextureError {
RendererMismatch,
DimensionsTooLarge,
UpdateRegionOutOfBounds,
InvalidUpdateValueCount,
NonFiniteUpdateValue,
}
impl fmt::Display for ScalarFieldTextureError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RendererMismatch => write!(
formatter,
"scalar field texture belongs to a different renderer"
),
Self::DimensionsTooLarge => write!(
formatter,
"scalar field dimensions exceed GPU texture limits"
),
Self::UpdateRegionOutOfBounds => {
write!(formatter, "scalar texture update region is out of bounds")
}
Self::InvalidUpdateValueCount => {
write!(
formatter,
"scalar texture update values do not match the region"
)
}
Self::NonFiniteUpdateValue => {
write!(formatter, "scalar texture update values must be finite")
}
}
}
}
impl Error for ScalarFieldTextureError {}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ScalarFieldRenderError {
RendererMismatch,
InvalidValueRange {
minimum: f32,
maximum: f32,
},
InvalidBackground,
Frame(RendererFrameError),
}
impl fmt::Display for ScalarFieldRenderError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RendererMismatch => write!(
formatter,
"scalar field texture belongs to a different renderer"
),
Self::InvalidValueRange { minimum, maximum } => {
write!(formatter, "invalid scalar value range {minimum}..{maximum}")
}
Self::InvalidBackground => {
write!(formatter, "scalar field background must be normalized")
}
Self::Frame(error) => write!(formatter, "scalar field frame failed: {error}"),
}
}
}
impl Error for ScalarFieldRenderError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScalarFieldUploadReport {
upload: Duration,
reallocated: bool,
}
impl ScalarFieldUploadReport {
pub fn upload(self) -> Duration {
self.upload
}
pub fn reallocated(self) -> bool {
self.reallocated
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DynamicMeshRenderError {
RendererMismatch,
InvalidBackground,
Frame(RendererFrameError),
}
impl fmt::Display for DynamicMeshRenderError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RendererMismatch => {
write!(formatter, "dynamic mesh belongs to a different renderer")
}
Self::InvalidBackground => {
write!(formatter, "dynamic mesh background must be normalized")
}
Self::Frame(error) => write!(formatter, "dynamic mesh frame failed: {error}"),
}
}
}
impl Error for DynamicMeshRenderError {}
impl PreparedScene {
pub fn command_count(&self) -> usize {
self.command_count
}
pub fn vertex_count(&self) -> usize {
self.vertex_count
}
pub fn draw_batch_count(&self) -> usize {
self.draw_batches.len()
}
pub fn recovery_memory_bytes(&self) -> usize {
self.vertices.len() * std::mem::size_of::<Vertex>()
}
pub fn tessellation_stats(&self) -> TessellationStats {
self.tessellation
}
}
pub struct WgpuRenderer {
renderer_identity: Arc<()>,
_instance: wgpu::Instance,
surface: wgpu::Surface<'static>,
_adapter: wgpu::Adapter,
device: wgpu::Device,
queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration,
requested_present_mode: RendererPresentMode,
surface_present_mode: RendererSurfacePresentMode,
scale_factor: f64,
pipeline: wgpu::RenderPipeline,
dynamic_pipeline: wgpu::RenderPipeline,
particle_pipeline: wgpu::RenderPipeline,
target_particle_pipeline: wgpu::RenderPipeline,
heatmap_pipeline: wgpu::RenderPipeline,
target_heatmap_pipeline: wgpu::RenderPipeline,
composition_pipelines: CompositionPipelines,
target_composition_pipelines: CompositionPipelines,
mesh3d_renderer: Mesh3dRenderer,
camera_uniform_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
heatmap_uniform_buffer: wgpu::Buffer,
heatmap_bind_group_layout: wgpu::BindGroupLayout,
color_map_cache: Option<CachedColorMap>,
vertex_buffer: Arc<wgpu::Buffer>,
particle_unit_buffer: wgpu::Buffer,
vertex_capacity: usize,
multisample_target: Option<MultisampleTarget>,
sample_count: u32,
vertices: Vec<Vertex>,
draw_batches: Vec<PreparedDrawBatch>,
retired_devices: Vec<RetiredDevice>,
max_quarantined_devices: usize,
}
struct RetiredDevice {
_adapter: wgpu::Adapter,
_device: wgpu::Device,
_queue: wgpu::Queue,
}
impl WgpuRenderer {
pub async fn new(
surface_target: impl Into<wgpu::SurfaceTarget<'static>>,
width: u32,
height: u32,
) -> Result<Self, RendererInitError> {
Self::new_with_options(
surface_target,
width,
height,
WgpuRendererOptions::default(),
)
.await
}
pub async fn new_with_options(
surface_target: impl Into<wgpu::SurfaceTarget<'static>>,
width: u32,
height: u32,
options: WgpuRendererOptions,
) -> Result<Self, RendererInitError> {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());
let surface = instance
.create_surface(surface_target)
.map_err(RendererInitError::CreateSurface)?;
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
force_fallback_adapter: false,
compatible_surface: Some(&surface),
apply_limit_buckets: false,
})
.await
.map_err(RendererInitError::RequestAdapter)?;
let (device, queue) = adapter
.request_device(&wgpu::DeviceDescriptor {
label: Some("sim-engine device"),
required_features: wgpu::Features::empty(),
required_limits: wgpu::Limits::default(),
experimental_features: wgpu::ExperimentalFeatures::disabled(),
memory_hints: wgpu::MemoryHints::Performance,
trace: wgpu::Trace::Off,
})
.await
.map_err(RendererInitError::RequestDevice)?;
let width = width.max(1);
let height = height.max(1);
let surface_capabilities = surface.get_capabilities(&adapter);
let mut config = surface
.get_default_config(&adapter, width, height)
.ok_or(RendererInitError::NoSurfaceConfig)?;
let surface_present_mode = select_surface_present_mode(
options.present_mode(),
&surface_capabilities.present_modes,
);
config.present_mode = surface_present_mode.to_wgpu();
surface.configure(&device, &config);
let sample_count = preferred_sample_count(&adapter, config.format);
let (
pipeline,
dynamic_pipeline,
particle_pipeline,
target_particle_pipeline,
heatmap_pipeline,
target_heatmap_pipeline,
composition_pipelines,
target_composition_pipelines,
camera_uniform_buffer,
camera_bind_group,
heatmap_uniform_buffer,
heatmap_bind_group_layout,
) = create_pipeline(&device, config.format, sample_count);
let vertex_buffer = Arc::new(create_vertex_buffer(&device, INITIAL_VERTEX_CAPACITY));
let particle_unit_buffer = create_particle_unit_buffer(&device, &queue);
let multisample_target = create_multisample_target(&device, &config, sample_count);
let mesh3d_renderer = Mesh3dRenderer::new(&device, config.format);
Ok(Self {
renderer_identity: Arc::new(()),
_instance: instance,
surface,
_adapter: adapter,
device,
queue,
config,
requested_present_mode: options.present_mode(),
surface_present_mode,
scale_factor: options.scale_factor(),
pipeline,
dynamic_pipeline,
particle_pipeline,
target_particle_pipeline,
heatmap_pipeline,
target_heatmap_pipeline,
composition_pipelines,
target_composition_pipelines,
mesh3d_renderer,
camera_uniform_buffer,
camera_bind_group,
heatmap_uniform_buffer,
heatmap_bind_group_layout,
color_map_cache: None,
vertex_buffer,
particle_unit_buffer,
vertex_capacity: INITIAL_VERTEX_CAPACITY,
multisample_target,
sample_count,
vertices: Vec::with_capacity(INITIAL_VERTEX_CAPACITY),
draw_batches: Vec::new(),
retired_devices: Vec::new(),
max_quarantined_devices: options.max_quarantined_devices(),
})
}
pub fn size(&self) -> (u32, u32) {
(self.config.width, self.config.height)
}
pub fn logical_size(&self) -> (f32, f32) {
let scale_factor = self.scale_factor as f32;
(
self.config.width as f32 / scale_factor,
self.config.height as f32 / scale_factor,
)
}
pub fn logical_viewport(&self) -> Result<LogicalViewport, crate::LogicalViewportError> {
let (width, height) = self.logical_size();
LogicalViewport::new(width, height)
}
pub fn physical_to_logical_screen(
&self,
position: PhysicalScreenPosition,
) -> Result<LogicalScreenPosition, RendererCoordinateError> {
physical_to_logical_screen(position, self.scale_factor as f32)
}
pub fn logical_to_physical_screen(
&self,
position: LogicalScreenPosition,
) -> Result<PhysicalScreenPosition, RendererCoordinateError> {
logical_to_physical_screen(position, self.scale_factor as f32)
}
pub fn scale_factor(&self) -> f64 {
self.scale_factor
}
pub fn surface_present_mode(&self) -> RendererSurfacePresentMode {
self.surface_present_mode
}
pub fn quarantined_device_count(&self) -> usize {
self.retired_devices.len()
}
pub fn max_quarantined_device_count(&self) -> usize {
self.max_quarantined_devices
}
pub fn remaining_device_recoveries(&self) -> usize {
self.max_quarantined_devices
.saturating_sub(self.retired_devices.len())
}
pub fn wait_for_gpu_idle(&self) -> Result<(), wgpu::PollError> {
self.device
.poll(wgpu::PollType::wait_indefinitely())
.map(|_| ())
}
pub fn set_scale_factor(
&mut self,
scale_factor: f64,
) -> Result<(), RendererConfigurationError> {
validate_scale_factor(scale_factor)?;
self.scale_factor = scale_factor;
Ok(())
}
pub fn resize(&mut self, width: u32, height: u32) {
if width == 0 || height == 0 {
return;
}
self.config.width = width;
self.config.height = height;
self.surface.configure(&self.device, &self.config);
self.multisample_target =
create_multisample_target(&self.device, &self.config, self.sample_count);
}
pub fn resize_with_scale_factor(
&mut self,
width: u32,
height: u32,
scale_factor: f64,
) -> Result<(), RendererConfigurationError> {
validate_scale_factor(scale_factor)?;
self.scale_factor = scale_factor;
self.resize(width, height);
Ok(())
}
pub fn render(
&mut self,
scene: &Scene,
camera: &Camera2d,
) -> Result<RenderStatus, RendererFrameError> {
self.render_with_metrics(scene, camera)
.map(RenderReport::status)
}
pub fn render_with_metrics(
&mut self,
scene: &Scene,
camera: &Camera2d,
) -> Result<RenderReport, RendererFrameError> {
let frame_started_at = Instant::now();
let tessellation_started_at = Instant::now();
self.vertices.clear();
self.draw_batches.clear();
let tessellation_stats =
tessellate_scene(scene, &mut self.vertices, &mut self.draw_batches);
self.ensure_vertex_capacity(self.vertices.len())?;
let tessellation = tessellation_started_at.elapsed();
let upload_started_at = Instant::now();
if !self.vertices.is_empty() {
self.queue
.write_buffer(&self.vertex_buffer, 0, bytemuck::cast_slice(&self.vertices));
}
let upload = upload_started_at.elapsed();
let vertex_buffer = Arc::clone(&self.vertex_buffer);
let draw_batches = std::mem::take(&mut self.draw_batches);
let geometry_extents = GeometryExtents::from_vertices(&self.vertices);
let result = self.draw_geometry(
scene.background(),
&vertex_buffer,
self.vertices.len(),
geometry_extents,
&draw_batches,
*camera,
tessellation,
upload,
false,
false,
tessellation_stats,
frame_started_at,
);
self.draw_batches = draw_batches;
result
}
pub fn prepare_scene(&self, scene: &Scene) -> Result<PreparedScene, PreparedSceneError> {
prepare_scene_resources(
&self.device,
&self.queue,
Arc::clone(&self.renderer_identity),
scene,
)
}
pub fn restore_prepared_scene(
&self,
source: &PreparedScene,
) -> Result<PreparedScene, PreparedSceneError> {
restore_prepared_scene_resources(
&self.device,
&self.queue,
Arc::clone(&self.renderer_identity),
source,
)
}
pub fn restore_dynamic_mesh(
&self,
source: &DynamicMesh2d,
) -> Result<DynamicMesh2d, DynamicMeshError> {
restore_dynamic_mesh_resources(
&self.device,
&self.queue,
Arc::clone(&self.renderer_identity),
source,
)
}
pub fn create_dynamic_mesh(
&self,
vertices: &[DynamicVertex2d],
) -> Result<DynamicMesh2d, DynamicMeshError> {
let vertices = dynamic_vertices_to_gpu(vertices)?;
let vertex_capacity = dynamic_vertex_capacity(vertices.len())
.filter(|capacity| buffer_capacity_fits::<DynamicGpu>(&self.device, *capacity))
.ok_or(DynamicMeshError::CapacityTooLarge)?;
let vertex_buffer = Arc::new(create_dynamic_vertex_buffer(&self.device, vertex_capacity));
if !vertices.is_empty() {
self.queue
.write_buffer(&vertex_buffer, 0, bytemuck::cast_slice(&vertices));
}
Ok(DynamicMesh2d {
renderer_identity: Arc::clone(&self.renderer_identity),
geometry_extents: GeometryExtents::from_dynamic_vertices(&vertices),
vertex_buffer,
vertices,
vertex_capacity,
})
}
pub fn update_dynamic_mesh(
&self,
mesh: &mut DynamicMesh2d,
vertices: &[DynamicVertex2d],
) -> Result<(), DynamicMeshError> {
self.update_dynamic_mesh_with_metrics(mesh, vertices)
.map(|_| ())
}
pub fn update_dynamic_mesh_with_metrics(
&self,
mesh: &mut DynamicMesh2d,
vertices: &[DynamicVertex2d],
) -> Result<DynamicMeshUpdateReport, DynamicMeshError> {
let update_started_at = Instant::now();
self.validate_dynamic_mesh(mesh)?;
validate_dynamic_vertices(vertices)?;
let reallocated = vertices.len() > mesh.vertex_capacity;
if reallocated {
mesh.vertex_capacity = dynamic_vertex_capacity(vertices.len())
.filter(|capacity| buffer_capacity_fits::<DynamicGpu>(&self.device, *capacity))
.ok_or(DynamicMeshError::CapacityTooLarge)?;
mesh.vertex_buffer = Arc::new(create_dynamic_vertex_buffer(
&self.device,
mesh.vertex_capacity,
));
}
mesh.vertices.clear();
mesh.vertices.reserve(vertices.len());
let mut geometry_extents = GeometryExtents::empty(vertices.is_empty());
for vertex in vertices.iter().copied() {
let vertex = dynamic_vertex_to_gpu(vertex);
geometry_extents.include_dynamic(vertex);
mesh.vertices.push(vertex);
}
if !mesh.vertices.is_empty() {
self.queue
.write_buffer(&mesh.vertex_buffer, 0, bytemuck::cast_slice(&mesh.vertices));
}
mesh.geometry_extents = geometry_extents;
Ok(DynamicMeshUpdateReport {
vertex_count: mesh.vertices.len(),
upload: update_started_at.elapsed(),
reallocated,
})
}
pub fn update_dynamic_mesh_range(
&self,
mesh: &mut DynamicMesh2d,
first_vertex: usize,
vertices: &[DynamicVertex2d],
) -> Result<(), DynamicMeshError> {
self.update_dynamic_mesh_range_with_metrics(mesh, first_vertex, vertices)
.map(|_| ())
}
pub fn update_dynamic_mesh_range_with_metrics(
&self,
mesh: &mut DynamicMesh2d,
first_vertex: usize,
vertices: &[DynamicVertex2d],
) -> Result<DynamicMeshUpdateReport, DynamicMeshError> {
let update_started_at = Instant::now();
self.validate_dynamic_mesh(mesh)?;
let end = first_vertex
.checked_add(vertices.len())
.ok_or(DynamicMeshError::UpdateRangeOutOfBounds)?;
if !first_vertex.is_multiple_of(3)
|| !vertices.len().is_multiple_of(3)
|| end > mesh.vertices.len()
{
return Err(DynamicMeshError::UpdateRangeOutOfBounds);
}
let vertices = dynamic_vertices_to_gpu(vertices)?;
if !vertices.is_empty() {
let offset = (first_vertex * std::mem::size_of::<DynamicGpu>()) as wgpu::BufferAddress;
self.queue
.write_buffer(&mesh.vertex_buffer, offset, bytemuck::cast_slice(&vertices));
mesh.vertices[first_vertex..end].copy_from_slice(&vertices);
mesh.geometry_extents = GeometryExtents::from_dynamic_vertices(&mesh.vertices);
}
Ok(DynamicMeshUpdateReport {
vertex_count: mesh.vertices.len(),
upload: update_started_at.elapsed(),
reallocated: false,
})
}
pub fn render_dynamic_mesh(
&mut self,
mesh: &DynamicMesh2d,
background: Color,
camera: &Camera2d,
) -> Result<RenderStatus, DynamicMeshRenderError> {
self.render_dynamic_mesh_with_metrics(mesh, background, camera)
.map(RenderReport::status)
}
pub fn render_dynamic_mesh_with_metrics(
&mut self,
mesh: &DynamicMesh2d,
background: Color,
camera: &Camera2d,
) -> Result<RenderReport, DynamicMeshRenderError> {
self.validate_dynamic_mesh(mesh)
.map_err(|_| DynamicMeshRenderError::RendererMismatch)?;
if !background.is_normalized() {
return Err(DynamicMeshRenderError::InvalidBackground);
}
let draw_batches = (!mesh.vertices.is_empty()).then_some(PreparedDrawBatch {
vertex_range: 0..mesh.vertices.len() as u32,
screen_clip: None,
});
self.draw_geometry(
background,
&mesh.vertex_buffer,
mesh.vertices.len(),
mesh.geometry_extents,
draw_batches.as_slice(),
*camera,
Duration::ZERO,
Duration::ZERO,
false,
true,
TessellationStats::default(),
Instant::now(),
)
.map_err(DynamicMeshRenderError::Frame)
}
pub fn create_particle_field(
&self,
instances: &[ParticleInstance2d],
) -> Result<ParticleField2d, ParticleFieldError> {
self.create_particle_field_with_budget(instances, ParticleRenderBudget::UNBOUNDED)
}
pub fn create_particle_field_with_budget(
&self,
instances: &[ParticleInstance2d],
budget: ParticleRenderBudget,
) -> Result<ParticleField2d, ParticleFieldError> {
let instances = particle_instances_to_gpu(instances)?;
let instance_capacity = particle_budgeted_capacity(instances.len(), budget)
.filter(|capacity| buffer_capacity_fits::<ParticleGpu>(&self.device, *capacity))
.ok_or(ParticleFieldError::CapacityTooLarge)?;
let instance_buffer = Arc::new(create_particle_instance_buffer(
&self.device,
instance_capacity,
));
Ok(ParticleField2d {
renderer_identity: Arc::clone(&self.renderer_identity),
instance_buffer,
statistics: particle_statistics(instances.len(), instances.len(), 0),
instances,
visible_instances: Vec::new(),
instance_capacity,
budget,
})
}
pub fn set_particle_field_budget(
&self,
field: &mut ParticleField2d,
budget: ParticleRenderBudget,
) -> Result<(), ParticleFieldError> {
self.validate_particle_field(field)?;
let desired_capacity = particle_budgeted_capacity(field.instances.len(), budget)
.filter(|capacity| buffer_capacity_fits::<ParticleGpu>(&self.device, *capacity))
.ok_or(ParticleFieldError::CapacityTooLarge)?;
if desired_capacity != field.instance_capacity {
field.instance_buffer = Arc::new(create_particle_instance_buffer(
&self.device,
desired_capacity,
));
field.instance_capacity = desired_capacity;
}
field.budget = budget;
field.visible_instances.clear();
field.statistics = particle_statistics(field.instances.len(), field.instances.len(), 0);
Ok(())
}
pub fn update_particle_field(
&self,
field: &mut ParticleField2d,
instances: &[ParticleInstance2d],
) -> Result<ParticleStatistics, ParticleFieldError> {
self.update_particle_field_with_metrics(field, instances)
.map(|report| report.statistics())
}
pub fn update_particle_field_with_metrics(
&self,
field: &mut ParticleField2d,
instances: &[ParticleInstance2d],
) -> Result<ParticleFieldUpdateReport, ParticleFieldError> {
let update_started_at = Instant::now();
self.validate_particle_field(field)?;
let instances = particle_instances_to_gpu(instances)?;
let desired_capacity = particle_budgeted_capacity(instances.len(), field.budget)
.filter(|capacity| buffer_capacity_fits::<ParticleGpu>(&self.device, *capacity))
.ok_or(ParticleFieldError::CapacityTooLarge)?;
let reallocated = desired_capacity > field.instance_capacity;
if reallocated {
field.instance_capacity = desired_capacity;
field.instance_buffer = Arc::new(create_particle_instance_buffer(
&self.device,
field.instance_capacity,
));
}
field.statistics = particle_statistics(instances.len(), instances.len(), 0);
field.instances = instances;
Ok(ParticleFieldUpdateReport {
statistics: field.statistics,
preparation: update_started_at.elapsed(),
reallocated,
})
}
pub fn update_particle_field_range(
&self,
field: &mut ParticleField2d,
first_instance: usize,
instances: &[ParticleInstance2d],
) -> Result<ParticleStatistics, ParticleFieldError> {
self.update_particle_field_range_with_metrics(field, first_instance, instances)
.map(|report| report.statistics())
}
pub fn update_particle_field_range_with_metrics(
&self,
field: &mut ParticleField2d,
first_instance: usize,
instances: &[ParticleInstance2d],
) -> Result<ParticleFieldUpdateReport, ParticleFieldError> {
let update_started_at = Instant::now();
self.validate_particle_field(field)?;
let range = particle_update_range(first_instance, instances.len(), field.instances.len())?;
let instances = particle_instances_to_gpu(instances)?;
if !instances.is_empty() {
field.instances[range].copy_from_slice(&instances);
}
field.statistics = particle_statistics(field.instances.len(), field.instances.len(), 0);
Ok(ParticleFieldUpdateReport {
statistics: field.statistics,
preparation: update_started_at.elapsed(),
reallocated: false,
})
}
pub fn restore_particle_field(
&self,
source: &ParticleField2d,
) -> Result<ParticleField2d, ParticleFieldError> {
restore_particle_field_resources(
&self.device,
&self.queue,
Arc::clone(&self.renderer_identity),
source,
)
}
pub fn create_scalar_field_texture(
&self,
field: ScalarField,
) -> Result<ScalarFieldTexture, ScalarFieldTextureError> {
create_scalar_field_texture_resources(
&self.device,
&self.queue,
Arc::clone(&self.renderer_identity),
field,
)
}
pub fn create_render_target(
&self,
width: u32,
height: u32,
) -> Result<RenderTarget2d, RenderTargetError> {
if width == 0 || height == 0 {
return Err(RenderTargetError::ZeroDimension);
}
if width > self.device.limits().max_texture_dimension_2d
|| height > self.device.limits().max_texture_dimension_2d
{
return Err(RenderTargetError::DimensionsTooLarge);
}
let allocation_bytes = render_target_allocation_bytes(self.config.format, width, height)
.ok_or(RenderTargetError::DimensionsTooLarge)?;
let texture = self.device.create_texture(&wgpu::TextureDescriptor {
label: Some("sim-engine render target"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: self.config.format,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
Ok(RenderTarget2d {
renderer_identity: Arc::clone(&self.renderer_identity),
resource_identity: Arc::new(()),
_texture: texture,
view,
width,
height,
allocation_bytes,
})
}
pub fn create_trail_buffer(
&mut self,
width: u32,
height: u32,
) -> Result<TrailBuffer2d, RenderTargetError> {
let front = self.create_render_target(width, height)?;
let back = self.create_render_target(width, height)?;
let trails = TrailBuffer2d {
renderer_identity: Arc::clone(&self.renderer_identity),
front,
back,
};
self.draw_clear_trail_buffer(&trails, Color::rgba(0.0, 0.0, 0.0, 0.0), Instant::now());
Ok(trails)
}
pub fn restore_render_target(
&self,
source: &RenderTarget2d,
) -> Result<RenderTarget2d, RenderTargetError> {
self.create_render_target(source.width(), source.height())
}
pub fn restore_trail_buffer(
&mut self,
source: &TrailBuffer2d,
) -> Result<TrailBuffer2d, RenderTargetError> {
self.create_trail_buffer(source.width(), source.height())
}
pub fn accumulate_trail_buffer(
&mut self,
trails: &mut TrailBuffer2d,
source: &RenderTarget2d,
retention: f32,
source_opacity: f32,
source_blend: BlendMode,
) -> Result<RenderReport, RenderTargetError> {
self.validate_trail_buffer(trails)?;
self.validate_render_target(source)?;
if Arc::ptr_eq(&source.resource_identity, &trails.front.resource_identity)
|| Arc::ptr_eq(&source.resource_identity, &trails.back.resource_identity)
{
return Err(RenderTargetError::SourceAliasesDestination);
}
if !retention.is_finite()
|| !(0.0..=1.0).contains(&retention)
|| !source_opacity.is_finite()
|| !(0.0..=1.0).contains(&source_opacity)
{
return Err(RenderTargetError::InvalidOpacity);
}
Ok(self.draw_trail_accumulation(
trails,
source,
retention,
source_opacity,
source_blend,
Instant::now(),
))
}
pub fn clear_trail_buffer(
&mut self,
trails: &mut TrailBuffer2d,
color: Color,
) -> Result<RenderReport, RenderTargetError> {
self.validate_trail_buffer(trails)?;
if !color.is_normalized() {
return Err(RenderTargetError::InvalidBackground);
}
Ok(self.draw_clear_trail_buffer(trails, color, Instant::now()))
}
pub fn compose_trail_buffer(
&mut self,
trails: &TrailBuffer2d,
blend_mode: BlendMode,
opacity: f32,
background: Color,
) -> Result<RenderReport, RenderTargetError> {
self.validate_trail_buffer(trails)?;
self.compose_render_target(&trails.front, blend_mode, opacity, background)
}
pub fn update_scalar_field_texture(
&self,
texture: &mut ScalarFieldTexture,
field: ScalarField,
) -> Result<ScalarFieldUploadReport, ScalarFieldTextureError> {
let upload_started_at = Instant::now();
self.validate_scalar_field_texture(texture)?;
let reallocated = texture.width() != field.width() || texture.height() != field.height();
if reallocated {
texture.texture = create_scalar_field_texture(&self.device, &field)?;
}
upload_scalar_field_texture(&self.queue, &texture.texture, &field)?;
texture.field = field;
Ok(ScalarFieldUploadReport {
upload: upload_started_at.elapsed(),
reallocated,
})
}
pub fn update_scalar_field_texture_region(
&self,
texture: &mut ScalarFieldTexture,
x: usize,
y: usize,
width: usize,
height: usize,
values: &[f32],
) -> Result<ScalarFieldUploadReport, ScalarFieldTextureError> {
let upload_started_at = Instant::now();
self.validate_scalar_field_texture(texture)?;
texture
.field
.replace_region(x, y, width, height, values)
.map_err(|error| match error {
crate::ScalarFieldError::InvalidValueCount { .. } => {
ScalarFieldTextureError::InvalidUpdateValueCount
}
crate::ScalarFieldError::NonFiniteValue => {
ScalarFieldTextureError::NonFiniteUpdateValue
}
_ => ScalarFieldTextureError::UpdateRegionOutOfBounds,
})?;
upload_scalar_field_texture_region(
&self.queue,
&texture.texture,
x,
y,
width,
height,
values,
)?;
Ok(ScalarFieldUploadReport {
upload: upload_started_at.elapsed(),
reallocated: false,
})
}
pub fn restore_scalar_field_texture(
&self,
source: &ScalarFieldTexture,
) -> Result<ScalarFieldTexture, ScalarFieldTextureError> {
self.create_scalar_field_texture(source.field.clone())
}
pub fn render_scalar_field_texture(
&mut self,
texture: &ScalarFieldTexture,
color_map: &ColorMap,
value_range: (f32, f32),
background: Color,
) -> Result<RenderStatus, ScalarFieldRenderError> {
self.render_scalar_field_texture_with_metrics(texture, color_map, value_range, background)
.map(RenderReport::status)
}
pub fn render_scalar_field_texture_with_metrics(
&mut self,
texture: &ScalarFieldTexture,
color_map: &ColorMap,
(minimum, maximum): (f32, f32),
background: Color,
) -> Result<RenderReport, ScalarFieldRenderError> {
self.validate_scalar_field_texture(texture)
.map_err(|_| ScalarFieldRenderError::RendererMismatch)?;
let value_extent = scalar_value_range_extent(minimum, maximum)
.ok_or(ScalarFieldRenderError::InvalidValueRange { minimum, maximum })?;
if !background.is_normalized() {
return Err(ScalarFieldRenderError::InvalidBackground);
}
self.draw_scalar_field_texture(
texture,
color_map,
minimum,
value_extent,
ScalarFieldSampling::Nearest,
background,
Instant::now(),
)
.map_err(ScalarFieldRenderError::Frame)
}
pub fn render_scalar_field_texture_with_sampling(
&mut self,
texture: &ScalarFieldTexture,
color_map: &ColorMap,
(minimum, maximum): (f32, f32),
sampling: ScalarFieldSampling,
background: Color,
) -> Result<RenderStatus, ScalarFieldRenderError> {
self.validate_scalar_field_texture(texture)
.map_err(|_| ScalarFieldRenderError::RendererMismatch)?;
let value_extent = scalar_value_range_extent(minimum, maximum)
.ok_or(ScalarFieldRenderError::InvalidValueRange { minimum, maximum })?;
if !background.is_normalized() {
return Err(ScalarFieldRenderError::InvalidBackground);
}
self.draw_scalar_field_texture(
texture,
color_map,
minimum,
value_extent,
sampling,
background,
Instant::now(),
)
.map(RenderReport::status)
.map_err(ScalarFieldRenderError::Frame)
}
pub fn render_scalar_field_texture_to_target(
&mut self,
target: &RenderTarget2d,
texture: &ScalarFieldTexture,
color_map: &ColorMap,
value_range: (f32, f32),
background: Color,
) -> Result<RenderReport, ScalarFieldRenderError> {
self.render_scalar_field_texture_to_target_with_sampling(
target,
texture,
color_map,
value_range,
ScalarFieldSampling::Nearest,
background,
)
}
pub fn render_scalar_field_texture_to_target_with_sampling(
&mut self,
target: &RenderTarget2d,
texture: &ScalarFieldTexture,
color_map: &ColorMap,
(minimum, maximum): (f32, f32),
sampling: ScalarFieldSampling,
background: Color,
) -> Result<RenderReport, ScalarFieldRenderError> {
self.validate_render_target(target)
.map_err(|_| ScalarFieldRenderError::RendererMismatch)?;
self.validate_scalar_field_texture(texture)
.map_err(|_| ScalarFieldRenderError::RendererMismatch)?;
let value_extent = scalar_value_range_extent(minimum, maximum)
.ok_or(ScalarFieldRenderError::InvalidValueRange { minimum, maximum })?;
if !background.is_normalized() {
return Err(ScalarFieldRenderError::InvalidBackground);
}
Ok(self.draw_scalar_field_texture_to_target(
target,
texture,
color_map,
minimum,
value_extent,
sampling,
background,
Instant::now(),
))
}
pub fn compose_render_target(
&mut self,
target: &RenderTarget2d,
blend_mode: BlendMode,
opacity: f32,
background: Color,
) -> Result<RenderReport, RenderTargetError> {
self.validate_render_target(target)?;
if !opacity.is_finite() || !(0.0..=1.0).contains(&opacity) {
return Err(RenderTargetError::InvalidOpacity);
}
if !background.is_normalized() {
return Err(RenderTargetError::InvalidBackground);
}
self.draw_composed_render_target(target, blend_mode, opacity, background, Instant::now())
.map_err(RenderTargetError::Frame)
}
pub fn render_particle_field(
&mut self,
field: &mut ParticleField2d,
background: Color,
camera: &Camera2d,
) -> Result<RenderStatus, ParticleFieldRenderError> {
self.render_particle_field_with_metrics(field, background, camera)
.map(RenderReport::status)
}
pub fn render_particle_field_with_metrics(
&mut self,
field: &mut ParticleField2d,
background: Color,
camera: &Camera2d,
) -> Result<RenderReport, ParticleFieldRenderError> {
self.validate_particle_field(field)
.map_err(|_| ParticleFieldRenderError::RendererMismatch)?;
if !background.is_normalized() {
return Err(ParticleFieldRenderError::InvalidBackground);
}
self.draw_particle_field(background, field, *camera, Instant::now())
.map_err(|error| match error {
RendererFrameError::InvalidGeometryTransform => {
ParticleFieldRenderError::InvalidGeometryTransform
}
other => ParticleFieldRenderError::Frame(other),
})
}
pub fn render_particle_field_to_target(
&mut self,
target: &RenderTarget2d,
field: &mut ParticleField2d,
camera: &Camera2d,
load: RenderTargetLoad,
) -> Result<RenderReport, ParticleFieldRenderError> {
self.validate_render_target(target)
.map_err(|_| ParticleFieldRenderError::RendererMismatch)?;
self.validate_particle_field(field)
.map_err(|_| ParticleFieldRenderError::RendererMismatch)?;
if matches!(load, RenderTargetLoad::Clear(color) if !color.is_normalized()) {
return Err(ParticleFieldRenderError::InvalidBackground);
}
self.draw_particle_field_to_target(target, field, *camera, load, Instant::now())
.map_err(|error| match error {
RendererFrameError::InvalidGeometryTransform => {
ParticleFieldRenderError::InvalidGeometryTransform
}
other => ParticleFieldRenderError::Frame(other),
})
}
pub fn render_prepared(
&mut self,
scene: &PreparedScene,
camera: &Camera2d,
) -> Result<RenderStatus, PreparedSceneRenderError> {
self.render_prepared_with_metrics(scene, camera)
.map(RenderReport::status)
}
pub fn render_prepared_with_metrics(
&mut self,
scene: &PreparedScene,
camera: &Camera2d,
) -> Result<RenderReport, PreparedSceneRenderError> {
if !prepared_scene_belongs_to(&self.renderer_identity, &scene.renderer_identity) {
return Err(PreparedSceneRenderError::RendererMismatch);
}
self.draw_geometry(
scene.background,
&scene.vertex_buffer,
scene.vertex_count,
scene.geometry_extents,
&scene.draw_batches,
*camera,
Duration::ZERO,
Duration::ZERO,
true,
false,
scene.tessellation,
Instant::now(),
)
.map_err(PreparedSceneRenderError::Frame)
}
#[allow(clippy::too_many_arguments)]
fn draw_geometry(
&mut self,
background: Color,
vertex_buffer: &wgpu::Buffer,
vertex_count: usize,
geometry_extents: GeometryExtents,
draw_batches: &[PreparedDrawBatch],
camera: Camera2d,
tessellation: Duration,
upload: Duration,
geometry_reused: bool,
geometry_streamed: bool,
tessellation_stats: TessellationStats,
frame_started_at: Instant,
) -> Result<RenderReport, RendererFrameError> {
let (logical_width, logical_height) = self.logical_size();
let viewport = LogicalViewport::new(logical_width, logical_height)
.map_err(|_| RendererFrameError::InvalidGeometryTransform)?;
let camera_uniform_upload_started_at = Instant::now();
let Some(camera_uniform) = CameraUniform::new(camera, viewport) else {
return Err(RendererFrameError::InvalidGeometryTransform);
};
if !geometry_extents.is_safe_for(camera_uniform) {
return Err(RendererFrameError::InvalidGeometryTransform);
}
self.queue.write_buffer(
&self.camera_uniform_buffer,
0,
bytemuck::bytes_of(&camera_uniform),
);
let camera_uniform_upload = camera_uniform_upload_started_at.elapsed();
let surface_acquire_started_at = Instant::now();
let surface_texture = match self.surface.get_current_texture() {
wgpu::CurrentSurfaceTexture::Success(texture)
| wgpu::CurrentSurfaceTexture::Suboptimal(texture) => texture,
wgpu::CurrentSurfaceTexture::Timeout => {
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Timeout),
tessellation,
upload,
camera_uniform_upload,
surface_acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
geometry_reused,
geometry_streamed,
tessellation_stats,
));
}
wgpu::CurrentSurfaceTexture::Occluded => {
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Occluded),
tessellation,
upload,
camera_uniform_upload,
surface_acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
geometry_reused,
geometry_streamed,
tessellation_stats,
));
}
wgpu::CurrentSurfaceTexture::Outdated => {
self.resize(self.config.width, self.config.height);
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Outdated),
tessellation,
upload,
camera_uniform_upload,
surface_acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
geometry_reused,
geometry_streamed,
tessellation_stats,
));
}
wgpu::CurrentSurfaceTexture::Lost => {
return Err(RendererFrameError::Surface(RendererSurfaceStatus::Lost));
}
wgpu::CurrentSurfaceTexture::Validation => {
return Err(RendererFrameError::Surface(
RendererSurfaceStatus::Validation,
));
}
};
let surface_acquire = surface_acquire_started_at.elapsed();
let encode_submit_present_started_at = Instant::now();
let surface_view = surface_texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let (view, resolve_target) = match &self.multisample_target {
Some(target) => (&target.view, Some(&surface_view)),
None => (&surface_view, None),
};
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("sim-engine render encoder"),
});
{
let color_attachment = wgpu::RenderPassColorAttachment {
view,
depth_slice: None,
resolve_target,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(background.to_wgpu()),
store: wgpu::StoreOp::Store,
},
};
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sim-engine render pass"),
color_attachments: &[Some(color_attachment)],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
if vertex_count > 0 {
let pipeline = if geometry_streamed {
&self.dynamic_pipeline
} else {
&self.pipeline
};
pass.set_pipeline(pipeline);
pass.set_bind_group(0, &self.camera_bind_group, &[]);
pass.set_vertex_buffer(0, vertex_buffer.slice(..));
for batch in draw_batches {
let scissor = match batch.screen_clip {
Some(screen_clip) => {
let Some(scissor) = screen_clip_to_scissor(
screen_clip,
viewport,
self.scale_factor as f32,
) else {
continue;
};
scissor
}
None => ScissorRect {
x: 0,
y: 0,
width: self.config.width,
height: self.config.height,
},
};
pass.set_scissor_rect(scissor.x, scissor.y, scissor.width, scissor.height);
pass.draw(batch.vertex_range.clone(), 0..1);
}
}
}
self.queue.submit([encoder.finish()]);
self.queue.present(surface_texture);
let encode_submit_present = encode_submit_present_started_at.elapsed();
Ok(render_report(
RenderStatus::Drawn,
tessellation,
upload,
camera_uniform_upload,
surface_acquire,
encode_submit_present,
frame_started_at.elapsed(),
geometry_reused,
geometry_streamed,
tessellation_stats,
))
}
#[allow(clippy::too_many_arguments)]
fn draw_scalar_field_texture(
&mut self,
texture: &ScalarFieldTexture,
color_map: &ColorMap,
minimum: f32,
value_extent: f32,
sampling: ScalarFieldSampling,
background: Color,
frame_started_at: Instant,
) -> Result<RenderReport, RendererFrameError> {
let upload_started_at = Instant::now();
let color_map_view = self.color_map_view(color_map);
let scalar_view = texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let uniform = HeatmapUniform {
value_range: [minimum, value_extent, 0.0, 0.0],
dimensions: [
texture.width() as u32,
texture.height() as u32,
sampling.shader_value(),
0,
],
};
self.queue.write_buffer(
&self.heatmap_uniform_buffer,
0,
bytemuck::bytes_of(&uniform),
);
let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("sim-engine heatmap bind group"),
layout: &self.heatmap_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&scalar_view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&color_map_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.heatmap_uniform_buffer.as_entire_binding(),
},
],
});
let upload = upload_started_at.elapsed();
let acquire_started_at = Instant::now();
let surface_texture = match self.surface.get_current_texture() {
wgpu::CurrentSurfaceTexture::Success(texture)
| wgpu::CurrentSurfaceTexture::Suboptimal(texture) => texture,
wgpu::CurrentSurfaceTexture::Timeout => {
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Timeout),
Duration::ZERO,
upload,
Duration::ZERO,
acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
));
}
wgpu::CurrentSurfaceTexture::Occluded => {
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Occluded),
Duration::ZERO,
upload,
Duration::ZERO,
acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
));
}
wgpu::CurrentSurfaceTexture::Outdated => {
self.resize(self.config.width, self.config.height);
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Outdated),
Duration::ZERO,
upload,
Duration::ZERO,
acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
));
}
wgpu::CurrentSurfaceTexture::Lost => {
return Err(RendererFrameError::Surface(RendererSurfaceStatus::Lost));
}
wgpu::CurrentSurfaceTexture::Validation => {
return Err(RendererFrameError::Surface(
RendererSurfaceStatus::Validation,
));
}
};
let surface_acquire = acquire_started_at.elapsed();
let encode_started_at = Instant::now();
let surface_view = surface_texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let (view, resolve_target) = match &self.multisample_target {
Some(target) => (&target.view, Some(&surface_view)),
None => (&surface_view, None),
};
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("sim-engine heatmap encoder"),
});
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sim-engine heatmap pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
depth_slice: None,
resolve_target,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(background.to_wgpu()),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(&self.heatmap_pipeline);
pass.set_bind_group(0, &bind_group, &[]);
pass.draw(0..6, 0..1);
}
self.queue.submit([encoder.finish()]);
self.queue.present(surface_texture);
Ok(render_report(
RenderStatus::Drawn,
Duration::ZERO,
upload,
Duration::ZERO,
surface_acquire,
encode_started_at.elapsed(),
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
))
}
#[allow(clippy::too_many_arguments)]
fn draw_scalar_field_texture_to_target(
&mut self,
target: &RenderTarget2d,
texture: &ScalarFieldTexture,
color_map: &ColorMap,
minimum: f32,
value_extent: f32,
sampling: ScalarFieldSampling,
background: Color,
frame_started_at: Instant,
) -> RenderReport {
let upload_started_at = Instant::now();
let color_map_view = self.color_map_view(color_map);
let scalar_view = texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
self.queue.write_buffer(
&self.heatmap_uniform_buffer,
0,
bytemuck::bytes_of(&HeatmapUniform {
value_range: [minimum, value_extent, 0.0, 0.0],
dimensions: [
texture.width() as u32,
texture.height() as u32,
sampling.shader_value(),
0,
],
}),
);
let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("sim-engine target heatmap bind group"),
layout: &self.heatmap_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&scalar_view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&color_map_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.heatmap_uniform_buffer.as_entire_binding(),
},
],
});
let upload = upload_started_at.elapsed();
let encode_started_at = Instant::now();
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("sim-engine render-target heatmap encoder"),
});
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sim-engine render-target heatmap pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &target.view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(premultiplied_wgpu_color(background)),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(&self.target_heatmap_pipeline);
pass.set_bind_group(0, &bind_group, &[]);
pass.draw(0..6, 0..1);
}
self.queue.submit([encoder.finish()]);
render_report(
RenderStatus::Drawn,
Duration::ZERO,
upload,
Duration::ZERO,
Duration::ZERO,
encode_started_at.elapsed(),
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
)
}
fn draw_composed_render_target(
&mut self,
target: &RenderTarget2d,
blend_mode: BlendMode,
opacity: f32,
background: Color,
frame_started_at: Instant,
) -> Result<RenderReport, RendererFrameError> {
let upload_started_at = Instant::now();
self.queue.write_buffer(
&self.composition_pipelines.uniform_buffer,
0,
bytemuck::bytes_of(&CompositeUniform {
opacity: [opacity, 0.0, 0.0, 0.0],
}),
);
let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("sim-engine composition bind group"),
layout: &self.composition_pipelines.bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&target.view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self
.composition_pipelines
.uniform_buffer
.as_entire_binding(),
},
],
});
let upload = upload_started_at.elapsed();
let acquire_started_at = Instant::now();
let surface_texture = match self.surface.get_current_texture() {
wgpu::CurrentSurfaceTexture::Success(texture)
| wgpu::CurrentSurfaceTexture::Suboptimal(texture) => texture,
wgpu::CurrentSurfaceTexture::Timeout => {
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Timeout),
Duration::ZERO,
upload,
Duration::ZERO,
acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
));
}
wgpu::CurrentSurfaceTexture::Occluded => {
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Occluded),
Duration::ZERO,
upload,
Duration::ZERO,
acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
));
}
wgpu::CurrentSurfaceTexture::Outdated => {
self.resize(self.config.width, self.config.height);
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Outdated),
Duration::ZERO,
upload,
Duration::ZERO,
acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
));
}
wgpu::CurrentSurfaceTexture::Lost => {
return Err(RendererFrameError::Surface(RendererSurfaceStatus::Lost));
}
wgpu::CurrentSurfaceTexture::Validation => {
return Err(RendererFrameError::Surface(
RendererSurfaceStatus::Validation,
));
}
};
let surface_acquire = acquire_started_at.elapsed();
let encode_started_at = Instant::now();
let surface_view = surface_texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let (view, resolve_target) = match &self.multisample_target {
Some(multisample_target) => (&multisample_target.view, Some(&surface_view)),
None => (&surface_view, None),
};
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("sim-engine composition encoder"),
});
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sim-engine composition pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
depth_slice: None,
resolve_target,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(background.to_wgpu()),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(self.composition_pipelines.pipeline(blend_mode));
pass.set_bind_group(0, &bind_group, &[]);
pass.draw(0..6, 0..1);
}
self.queue.submit([encoder.finish()]);
self.queue.present(surface_texture);
Ok(render_report(
RenderStatus::Drawn,
Duration::ZERO,
upload,
Duration::ZERO,
surface_acquire,
encode_started_at.elapsed(),
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
))
}
#[allow(clippy::too_many_arguments)]
fn draw_trail_accumulation(
&mut self,
trails: &mut TrailBuffer2d,
source: &RenderTarget2d,
retention: f32,
source_opacity: f32,
source_blend: BlendMode,
frame_started_at: Instant,
) -> RenderReport {
let upload_started_at = Instant::now();
self.queue.write_buffer(
&self.target_composition_pipelines.secondary_uniform_buffer,
0,
bytemuck::bytes_of(&CompositeUniform {
opacity: [retention, 0.0, 0.0, 0.0],
}),
);
self.queue.write_buffer(
&self.target_composition_pipelines.uniform_buffer,
0,
bytemuck::bytes_of(&CompositeUniform {
opacity: [source_opacity, 0.0, 0.0, 0.0],
}),
);
let history_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("sim-engine trail history bind group"),
layout: &self.target_composition_pipelines.bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&trails.front.view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self
.target_composition_pipelines
.secondary_uniform_buffer
.as_entire_binding(),
},
],
});
let source_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("sim-engine trail source bind group"),
layout: &self.target_composition_pipelines.bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&source.view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self
.target_composition_pipelines
.uniform_buffer
.as_entire_binding(),
},
],
});
let upload = upload_started_at.elapsed();
let encode_started_at = Instant::now();
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("sim-engine trail accumulation encoder"),
});
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sim-engine trail history pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &trails.back.view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(Color::rgba(0.0, 0.0, 0.0, 0.0).to_wgpu()),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(&self.target_composition_pipelines.alpha);
pass.set_bind_group(0, &history_bind_group, &[]);
pass.draw(0..6, 0..1);
}
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sim-engine trail source pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &trails.back.view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
pass.set_pipeline(self.target_composition_pipelines.pipeline(source_blend));
pass.set_bind_group(0, &source_bind_group, &[]);
pass.draw(0..6, 0..1);
}
self.queue.submit([encoder.finish()]);
std::mem::swap(&mut trails.front, &mut trails.back);
render_report(
RenderStatus::Drawn,
Duration::ZERO,
upload,
Duration::ZERO,
Duration::ZERO,
encode_started_at.elapsed(),
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
)
}
fn draw_clear_trail_buffer(
&self,
trails: &TrailBuffer2d,
color: Color,
frame_started_at: Instant,
) -> RenderReport {
let encode_started_at = Instant::now();
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("sim-engine trail clear encoder"),
});
for view in [&trails.front.view, &trails.back.view] {
let _pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sim-engine trail clear pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(premultiplied_wgpu_color(color)),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
}
self.queue.submit([encoder.finish()]);
render_report(
RenderStatus::Drawn,
Duration::ZERO,
Duration::ZERO,
Duration::ZERO,
Duration::ZERO,
encode_started_at.elapsed(),
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
)
}
fn prepare_particle_draw(
&self,
field: &mut ParticleField2d,
camera: Camera2d,
) -> Result<ParticleDrawPreparation, RendererFrameError> {
let (logical_width, logical_height) = self.logical_size();
let viewport = LogicalViewport::new(logical_width, logical_height)
.map_err(|_| RendererFrameError::InvalidGeometryTransform)?;
let Some(camera_uniform) = CameraUniform::new(camera, viewport) else {
return Err(RendererFrameError::InvalidGeometryTransform);
};
let instance_count = field.instances.len();
let visibility_checked = instance_count.min(field.budget.max_visibility_checks_per_frame);
if visibility_checked < instance_count {
field.visible_instances.clear();
for candidate_index in 0..visibility_checked {
let source_index =
uniformly_sampled_index(candidate_index, instance_count, visibility_checked);
let instance = field.instances[source_index];
if !instance.is_safe_for(camera_uniform) {
return Err(RendererFrameError::InvalidGeometryTransform);
}
if instance.intersects_viewport(camera_uniform, viewport) {
field.visible_instances.push(instance);
}
}
let visible_count = field.visible_instances.len();
let selected_count = visible_count.min(field.budget.instance_limit());
if selected_count < visible_count {
let mut visible_index = 0;
field.visible_instances.retain(|_| {
let selected = particle_visible_index_is_selected(
visible_index,
visible_count,
selected_count,
);
visible_index += 1;
selected
});
}
field.statistics = particle_statistics_with_budget(
instance_count,
visibility_checked,
visible_count,
selected_count,
0,
);
return self.upload_particle_draw(field, camera_uniform, selected_count);
}
let visible_count = visible_particle_count(&field.instances, camera_uniform, viewport)?;
let selected_count = visible_count.min(field.budget.instance_limit());
field.statistics = particle_statistics_with_budget(
instance_count,
instance_count,
visible_count,
selected_count,
0,
);
if selected_count == field.instances.len() {
field.visible_instances.clear();
} else {
field.visible_instances.clear();
let mut visible_index = 0;
for instance in field.instances.iter().copied() {
if !instance.intersects_viewport(camera_uniform, viewport) {
continue;
}
let selected_index = field.visible_instances.len();
if selected_index < selected_count
&& particle_visible_index_is_selected(
visible_index,
visible_count,
selected_count,
)
{
field.visible_instances.push(instance);
}
visible_index += 1;
}
}
self.upload_particle_draw(field, camera_uniform, selected_count)
}
fn upload_particle_draw(
&self,
field: &ParticleField2d,
camera_uniform: CameraUniform,
visible_count: usize,
) -> Result<ParticleDrawPreparation, RendererFrameError> {
let visible_instances =
if field.visible_instances.is_empty() && visible_count == field.instances.len() {
field.instances.as_slice()
} else {
field.visible_instances.as_slice()
};
let upload_started_at = Instant::now();
if !visible_instances.is_empty() {
self.queue.write_buffer(
&field.instance_buffer,
0,
bytemuck::cast_slice(visible_instances),
);
}
let upload = upload_started_at.elapsed();
let camera_uniform_upload_started_at = Instant::now();
self.queue.write_buffer(
&self.camera_uniform_buffer,
0,
bytemuck::bytes_of(&camera_uniform),
);
let camera_uniform_upload = camera_uniform_upload_started_at.elapsed();
Ok(ParticleDrawPreparation {
visible_count,
upload,
camera_uniform_upload,
})
}
fn draw_particle_field(
&mut self,
background: Color,
field: &mut ParticleField2d,
camera: Camera2d,
frame_started_at: Instant,
) -> Result<RenderReport, RendererFrameError> {
let preparation = self.prepare_particle_draw(field, camera)?;
let surface_acquire_started_at = Instant::now();
let surface_texture = match self.surface.get_current_texture() {
wgpu::CurrentSurfaceTexture::Success(texture)
| wgpu::CurrentSurfaceTexture::Suboptimal(texture) => texture,
wgpu::CurrentSurfaceTexture::Timeout => {
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Timeout),
Duration::ZERO,
preparation.upload,
preparation.camera_uniform_upload,
surface_acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
));
}
wgpu::CurrentSurfaceTexture::Occluded => {
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Occluded),
Duration::ZERO,
preparation.upload,
preparation.camera_uniform_upload,
surface_acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
));
}
wgpu::CurrentSurfaceTexture::Outdated => {
self.resize(self.config.width, self.config.height);
return Ok(render_report(
RenderStatus::Skipped(RendererSurfaceStatus::Outdated),
Duration::ZERO,
preparation.upload,
preparation.camera_uniform_upload,
surface_acquire_started_at.elapsed(),
Duration::ZERO,
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
));
}
wgpu::CurrentSurfaceTexture::Lost => {
return Err(RendererFrameError::Surface(RendererSurfaceStatus::Lost));
}
wgpu::CurrentSurfaceTexture::Validation => {
return Err(RendererFrameError::Surface(
RendererSurfaceStatus::Validation,
));
}
};
let surface_acquire = surface_acquire_started_at.elapsed();
let encode_submit_present_started_at = Instant::now();
let surface_view = surface_texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let (view, resolve_target) = match &self.multisample_target {
Some(target) => (&target.view, Some(&surface_view)),
None => (&surface_view, None),
};
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("sim-engine particle render encoder"),
});
{
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sim-engine particle render pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view,
depth_slice: None,
resolve_target,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(background.to_wgpu()),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
if preparation.visible_count > 0 {
pass.set_pipeline(&self.particle_pipeline);
pass.set_bind_group(0, &self.camera_bind_group, &[]);
pass.set_vertex_buffer(0, self.particle_unit_buffer.slice(..));
pass.set_vertex_buffer(1, field.instance_buffer.slice(..));
pass.draw(0..6, 0..preparation.visible_count as u32);
}
}
self.queue.submit([encoder.finish()]);
self.queue.present(surface_texture);
field.statistics.rendered = preparation.visible_count;
Ok(render_report(
RenderStatus::Drawn,
Duration::ZERO,
preparation.upload,
preparation.camera_uniform_upload,
surface_acquire,
encode_submit_present_started_at.elapsed(),
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
))
}
fn draw_particle_field_to_target(
&self,
target: &RenderTarget2d,
field: &mut ParticleField2d,
camera: Camera2d,
load: RenderTargetLoad,
frame_started_at: Instant,
) -> Result<RenderReport, RendererFrameError> {
let preparation = self.prepare_particle_draw(field, camera)?;
let encode_started_at = Instant::now();
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("sim-engine target particle encoder"),
});
{
let load = match load {
RenderTargetLoad::Load => wgpu::LoadOp::Load,
RenderTargetLoad::Clear(color) => {
wgpu::LoadOp::Clear(premultiplied_wgpu_color(color))
}
};
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("sim-engine target particle pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &target.view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
if preparation.visible_count > 0 {
pass.set_pipeline(&self.target_particle_pipeline);
pass.set_bind_group(0, &self.camera_bind_group, &[]);
pass.set_vertex_buffer(0, self.particle_unit_buffer.slice(..));
pass.set_vertex_buffer(1, field.instance_buffer.slice(..));
pass.draw(0..6, 0..preparation.visible_count as u32);
}
}
self.queue.submit([encoder.finish()]);
field.statistics.rendered = preparation.visible_count;
Ok(render_report(
RenderStatus::Drawn,
Duration::ZERO,
preparation.upload,
preparation.camera_uniform_upload,
Duration::ZERO,
encode_started_at.elapsed(),
frame_started_at.elapsed(),
false,
true,
TessellationStats::default(),
))
}
fn ensure_vertex_capacity(&mut self, vertex_count: usize) -> Result<(), RendererFrameError> {
if vertex_count <= self.vertex_capacity {
return Ok(());
}
self.vertex_capacity = dynamic_vertex_capacity(vertex_count)
.filter(|capacity| buffer_capacity_fits::<Vertex>(&self.device, *capacity))
.ok_or(RendererFrameError::GeometryCapacityTooLarge)?;
self.vertex_buffer = Arc::new(create_vertex_buffer(&self.device, self.vertex_capacity));
Ok(())
}
fn validate_dynamic_mesh(&self, mesh: &DynamicMesh2d) -> Result<(), DynamicMeshError> {
prepared_scene_belongs_to(&self.renderer_identity, &mesh.renderer_identity)
.then_some(())
.ok_or(DynamicMeshError::RendererMismatch)
}
fn validate_particle_field(&self, field: &ParticleField2d) -> Result<(), ParticleFieldError> {
prepared_scene_belongs_to(&self.renderer_identity, &field.renderer_identity)
.then_some(())
.ok_or(ParticleFieldError::RendererMismatch)
}
fn validate_scalar_field_texture(
&self,
texture: &ScalarFieldTexture,
) -> Result<(), ScalarFieldTextureError> {
prepared_scene_belongs_to(&self.renderer_identity, &texture.renderer_identity)
.then_some(())
.ok_or(ScalarFieldTextureError::RendererMismatch)
}
fn color_map_view(&mut self, color_map: &ColorMap) -> wgpu::TextureView {
if let Some(cached) = self
.color_map_cache
.as_ref()
.filter(|cached| cached.source == *color_map)
{
return cached.view.clone();
}
let cached = create_cached_color_map(&self.device, &self.queue, color_map);
let view = cached.view.clone();
self.color_map_cache = Some(cached);
view
}
fn validate_render_target(&self, target: &RenderTarget2d) -> Result<(), RenderTargetError> {
prepared_scene_belongs_to(&self.renderer_identity, &target.renderer_identity)
.then_some(())
.ok_or(RenderTargetError::RendererMismatch)
}
fn validate_trail_buffer(&self, trails: &TrailBuffer2d) -> Result<(), RenderTargetError> {
prepared_scene_belongs_to(&self.renderer_identity, &trails.renderer_identity)
.then_some(())
.ok_or(RenderTargetError::RendererMismatch)
}
}
fn prepared_scene_belongs_to(renderer_identity: &Arc<()>, scene_identity: &Arc<()>) -> bool {
Arc::ptr_eq(renderer_identity, scene_identity)
}
fn render_target_allocation_bytes(
format: wgpu::TextureFormat,
width: u32,
height: u32,
) -> Option<usize> {
let bytes_per_texel = u64::from(format.block_copy_size(None)?);
let byte_count = u64::from(width)
.checked_mul(u64::from(height))?
.checked_mul(bytes_per_texel)?;
usize::try_from(byte_count).ok()
}
fn dynamic_vertices_to_gpu(
vertices: &[DynamicVertex2d],
) -> Result<Vec<DynamicGpu>, DynamicMeshError> {
validate_dynamic_vertices(vertices)?;
Ok(vertices
.iter()
.copied()
.map(dynamic_vertex_to_gpu)
.collect())
}
fn validate_dynamic_vertices(vertices: &[DynamicVertex2d]) -> Result<(), DynamicMeshError> {
if !vertices.len().is_multiple_of(3) {
return Err(DynamicMeshError::InvalidVertexCount);
}
debug_assert!(vertices.iter().all(|vertex| {
vertex.world_position.is_finite()
&& vertex.depth.is_finite()
&& vertex.color.is_normalized()
}));
Ok(())
}
fn dynamic_vertex_to_gpu(vertex: DynamicVertex2d) -> DynamicGpu {
DynamicGpu {
world_position: [vertex.world_position.x, vertex.world_position.y],
depth: vertex.depth,
color: vertex.color.to_array(),
}
}
fn dynamic_vertex_capacity(vertex_count: usize) -> Option<usize> {
vertex_count.max(1).checked_next_power_of_two()
}
fn particle_instances_to_gpu(
instances: &[ParticleInstance2d],
) -> Result<Vec<ParticleGpu>, ParticleFieldError> {
instances
.iter()
.copied()
.map(|instance| {
let world_position = instance.world_position();
let radius = instance.radius();
let color = instance.color();
let depth = instance.depth();
if !world_position.is_finite()
|| !radius.is_finite()
|| radius <= 0.0
|| !color.is_normalized()
|| !depth.is_finite()
{
return Err(ParticleFieldError::InvalidInstance);
}
Ok(ParticleGpu {
world_position: [world_position.x, world_position.y],
depth,
radius,
color: color.to_array(),
})
})
.collect()
}
fn particle_instance_capacity(instance_count: usize) -> Option<usize> {
instance_count.max(1).checked_next_power_of_two()
}
fn particle_budgeted_capacity(
instance_count: usize,
budget: ParticleRenderBudget,
) -> Option<usize> {
let limit = budget.instance_limit();
let required = instance_count.min(limit).max(1);
particle_instance_capacity(required).map(|capacity| capacity.min(limit))
}
fn buffer_allocation_bytes<T>(capacity: usize) -> Option<u64> {
u64::try_from(capacity)
.ok()?
.checked_mul(std::mem::size_of::<T>() as u64)
}
fn buffer_capacity_fits<T>(device: &wgpu::Device, capacity: usize) -> bool {
buffer_allocation_bytes::<T>(capacity)
.is_some_and(|bytes| bytes <= device.limits().max_buffer_size)
}
fn particle_update_range(
first_instance: usize,
replacement_count: usize,
current_count: usize,
) -> Result<Range<usize>, ParticleFieldError> {
let end = first_instance
.checked_add(replacement_count)
.ok_or(ParticleFieldError::UpdateRangeOutOfBounds)?;
(end <= current_count)
.then_some(first_instance..end)
.ok_or(ParticleFieldError::UpdateRangeOutOfBounds)
}
#[cfg(test)]
fn visible_particle_instances(
instances: &[ParticleGpu],
camera: CameraUniform,
viewport: LogicalViewport,
) -> Result<Vec<ParticleGpu>, RendererFrameError> {
visible_particle_count(instances, camera, viewport)?;
Ok(instances
.iter()
.copied()
.filter(|instance| instance.intersects_viewport(camera, viewport))
.collect())
}
fn visible_particle_count(
instances: &[ParticleGpu],
camera: CameraUniform,
viewport: LogicalViewport,
) -> Result<usize, RendererFrameError> {
let mut visible = 0;
for instance in instances.iter().copied() {
if !instance.is_safe_for(camera) {
return Err(RendererFrameError::InvalidGeometryTransform);
}
visible += usize::from(instance.intersects_viewport(camera, viewport));
}
Ok(visible)
}
fn particle_visible_index_is_selected(
visible_index: usize,
visible_count: usize,
selected_count: usize,
) -> bool {
if visible_count == 0 || selected_count == 0 {
return false;
}
let before = visible_index as u128 * selected_count as u128 / visible_count as u128;
let after = (visible_index as u128 + 1) * selected_count as u128 / visible_count as u128;
before < after
}
fn uniformly_sampled_index(sample_index: usize, source_count: usize, sample_count: usize) -> usize {
debug_assert!(sample_index < sample_count);
debug_assert!(sample_count <= source_count);
(((sample_index as u128 + 1) * source_count as u128 - 1) / sample_count as u128) as usize
}
fn particle_statistics(
instance_count: usize,
visible: usize,
rendered: usize,
) -> ParticleStatistics {
particle_statistics_with_budget(instance_count, instance_count, visible, visible, rendered)
}
fn particle_statistics_with_budget(
instance_count: usize,
visibility_checked: usize,
visible: usize,
selected: usize,
rendered: usize,
) -> ParticleStatistics {
ParticleStatistics {
submitted: instance_count,
visibility_checked,
visible,
culled: visibility_checked.saturating_sub(visible),
budget_limited: instance_count
.saturating_sub(visibility_checked)
.saturating_add(visible.saturating_sub(selected)),
dropped: 0,
rendered,
}
}
fn prepare_scene_resources(
device: &wgpu::Device,
queue: &wgpu::Queue,
renderer_identity: Arc<()>,
scene: &Scene,
) -> Result<PreparedScene, PreparedSceneError> {
let mut vertices = Vec::new();
let mut draw_batches = Vec::new();
let tessellation = tessellate_scene(scene, &mut vertices, &mut draw_batches);
let geometry_extents = GeometryExtents::from_vertices(&vertices);
if !buffer_capacity_fits::<Vertex>(device, vertices.len().max(1)) {
return Err(PreparedSceneError::CapacityTooLarge);
}
let vertex_buffer = Arc::new(create_vertex_buffer(device, vertices.len()));
if !vertices.is_empty() {
queue.write_buffer(&vertex_buffer, 0, bytemuck::cast_slice(&vertices));
}
let vertex_count = vertices.len();
let vertices: Arc<[Vertex]> = vertices.into();
Ok(PreparedScene {
renderer_identity,
background: scene.background(),
vertex_buffer,
vertices,
command_count: scene.command_count(),
vertex_count,
geometry_extents,
draw_batches,
tessellation,
})
}
fn restore_prepared_scene_resources(
device: &wgpu::Device,
queue: &wgpu::Queue,
renderer_identity: Arc<()>,
source: &PreparedScene,
) -> Result<PreparedScene, PreparedSceneError> {
if !buffer_capacity_fits::<Vertex>(device, source.vertices.len().max(1)) {
return Err(PreparedSceneError::CapacityTooLarge);
}
let vertex_buffer = Arc::new(create_vertex_buffer(device, source.vertices.len()));
if !source.vertices.is_empty() {
queue.write_buffer(
&vertex_buffer,
0,
bytemuck::cast_slice(source.vertices.as_ref()),
);
}
Ok(PreparedScene {
renderer_identity,
background: source.background,
vertex_buffer,
vertices: Arc::clone(&source.vertices),
command_count: source.command_count,
vertex_count: source.vertex_count,
geometry_extents: source.geometry_extents,
draw_batches: source.draw_batches.clone(),
tessellation: source.tessellation,
})
}
fn restore_dynamic_mesh_resources(
device: &wgpu::Device,
queue: &wgpu::Queue,
renderer_identity: Arc<()>,
source: &DynamicMesh2d,
) -> Result<DynamicMesh2d, DynamicMeshError> {
if !buffer_capacity_fits::<DynamicGpu>(device, source.vertex_capacity) {
return Err(DynamicMeshError::CapacityTooLarge);
}
let vertex_buffer = Arc::new(create_dynamic_vertex_buffer(device, source.vertex_capacity));
if !source.vertices.is_empty() {
queue.write_buffer(
&vertex_buffer,
0,
bytemuck::cast_slice(source.vertices.as_slice()),
);
}
Ok(DynamicMesh2d {
renderer_identity,
vertex_buffer,
vertices: source.vertices.clone(),
vertex_capacity: source.vertex_capacity,
geometry_extents: source.geometry_extents,
})
}
fn restore_particle_field_resources(
device: &wgpu::Device,
_queue: &wgpu::Queue,
renderer_identity: Arc<()>,
source: &ParticleField2d,
) -> Result<ParticleField2d, ParticleFieldError> {
if !buffer_capacity_fits::<ParticleGpu>(device, source.instance_capacity) {
return Err(ParticleFieldError::CapacityTooLarge);
}
let instance_buffer = Arc::new(create_particle_instance_buffer(
device,
source.instance_capacity,
));
Ok(ParticleField2d {
renderer_identity,
instance_buffer,
instances: source.instances.clone(),
visible_instances: Vec::new(),
instance_capacity: source.instance_capacity,
budget: source.budget,
statistics: particle_statistics(source.instances.len(), source.instances.len(), 0),
})
}
#[allow(clippy::too_many_arguments)]
fn render_report(
status: RenderStatus,
tessellation: Duration,
upload: Duration,
camera_uniform_upload: Duration,
surface_acquire: Duration,
encode_submit_present: Duration,
total_cpu: Duration,
geometry_reused: bool,
geometry_streamed: bool,
tessellation_stats: TessellationStats,
) -> RenderReport {
RenderReport {
status,
metrics: RendererFrameMetrics {
tessellation,
upload,
camera_uniform_upload,
surface_acquire,
encode_submit_present,
total_cpu,
geometry_reused,
geometry_streamed,
tessellation_stats,
},
}
}
fn create_pipeline(
device: &wgpu::Device,
format: wgpu::TextureFormat,
sample_count: u32,
) -> (
wgpu::RenderPipeline,
wgpu::RenderPipeline,
wgpu::RenderPipeline,
wgpu::RenderPipeline,
wgpu::RenderPipeline,
wgpu::RenderPipeline,
CompositionPipelines,
CompositionPipelines,
wgpu::Buffer,
wgpu::BindGroup,
wgpu::Buffer,
wgpu::BindGroupLayout,
) {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("sim-engine flat color shader"),
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("primitive.wgsl"))),
});
let camera_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sim-engine camera uniform buffer"),
size: std::mem::size_of::<CameraUniform>() as wgpu::BufferAddress,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("sim-engine camera bind group layout"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
});
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("sim-engine camera bind group"),
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: camera_uniform_buffer.as_entire_binding(),
}],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("sim-engine shape pipeline layout"),
bind_group_layouts: &[Some(&camera_bind_group_layout)],
immediate_size: 0,
});
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("sim-engine shape pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[Some(Vertex::LAYOUT)],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: sample_count,
..Default::default()
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
});
let dynamic_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("sim-engine dynamic mesh pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("dynamic_vs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[Some(DynamicGpu::LAYOUT)],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: sample_count,
..Default::default()
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
});
let particle_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("sim-engine particle pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("particle_vs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[Some(ParticleUnitVertex::LAYOUT), Some(ParticleGpu::LAYOUT)],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: sample_count,
..Default::default()
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
});
let target_particle_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("sim-engine target particle pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("particle_vs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[Some(ParticleUnitVertex::LAYOUT), Some(ParticleGpu::LAYOUT)],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
..Default::default()
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
});
let heatmap_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sim-engine heatmap uniform buffer"),
size: std::mem::size_of::<HeatmapUniform>() as wgpu::BufferAddress,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let heatmap_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("sim-engine heatmap bind group layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2,
sample_type: wgpu::TextureSampleType::Float { filterable: false },
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2,
sample_type: wgpu::TextureSampleType::Float { filterable: false },
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});
let heatmap_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("sim-engine heatmap pipeline layout"),
bind_group_layouts: &[Some(&heatmap_bind_group_layout)],
immediate_size: 0,
});
let heatmap_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("sim-engine heatmap pipeline"),
layout: Some(&heatmap_pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("heatmap_vs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: sample_count,
..Default::default()
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("heatmap_fs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
});
let target_heatmap_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("sim-engine render-target heatmap pipeline"),
layout: Some(&heatmap_pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("heatmap_vs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("heatmap_fs_main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
});
let composition_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sim-engine composition uniform buffer"),
size: std::mem::size_of::<CompositeUniform>() as wgpu::BufferAddress,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let composition_secondary_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sim-engine composition secondary uniform buffer"),
size: std::mem::size_of::<CompositeUniform>() as wgpu::BufferAddress,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let composition_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("sim-engine composition bind group layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2,
sample_type: wgpu::TextureSampleType::Float { filterable: false },
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});
let composition_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("sim-engine composition pipeline layout"),
bind_group_layouts: &[Some(&composition_bind_group_layout)],
immediate_size: 0,
});
let composition_pipelines = CompositionPipelines {
alpha: create_composition_pipeline(
device,
&shader,
&composition_pipeline_layout,
format,
sample_count,
BlendMode::Alpha,
),
additive: create_composition_pipeline(
device,
&shader,
&composition_pipeline_layout,
format,
sample_count,
BlendMode::Additive,
),
replace: create_composition_pipeline(
device,
&shader,
&composition_pipeline_layout,
format,
sample_count,
BlendMode::Replace,
),
uniform_buffer: composition_uniform_buffer,
secondary_uniform_buffer: composition_secondary_uniform_buffer,
bind_group_layout: composition_bind_group_layout,
};
let target_composition_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sim-engine target composition uniform buffer"),
size: std::mem::size_of::<CompositeUniform>() as wgpu::BufferAddress,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let target_composition_secondary_uniform_buffer =
device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sim-engine target composition secondary uniform buffer"),
size: std::mem::size_of::<CompositeUniform>() as wgpu::BufferAddress,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let target_composition_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("sim-engine target composition bind group layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
view_dimension: wgpu::TextureViewDimension::D2,
sample_type: wgpu::TextureSampleType::Float { filterable: false },
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});
let target_composition_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("sim-engine target composition pipeline layout"),
bind_group_layouts: &[Some(&target_composition_bind_group_layout)],
immediate_size: 0,
});
let target_composition_pipelines = CompositionPipelines {
alpha: create_composition_pipeline(
device,
&shader,
&target_composition_pipeline_layout,
format,
1,
BlendMode::Alpha,
),
additive: create_composition_pipeline(
device,
&shader,
&target_composition_pipeline_layout,
format,
1,
BlendMode::Additive,
),
replace: create_composition_pipeline(
device,
&shader,
&target_composition_pipeline_layout,
format,
1,
BlendMode::Replace,
),
uniform_buffer: target_composition_uniform_buffer,
secondary_uniform_buffer: target_composition_secondary_uniform_buffer,
bind_group_layout: target_composition_bind_group_layout,
};
(
pipeline,
dynamic_pipeline,
particle_pipeline,
target_particle_pipeline,
heatmap_pipeline,
target_heatmap_pipeline,
composition_pipelines,
target_composition_pipelines,
camera_uniform_buffer,
camera_bind_group,
heatmap_uniform_buffer,
heatmap_bind_group_layout,
)
}
fn preferred_sample_count(adapter: &wgpu::Adapter, format: wgpu::TextureFormat) -> u32 {
let flags = adapter.get_texture_format_features(format).flags;
if flags.contains(
wgpu::TextureFormatFeatureFlags::MULTISAMPLE_X4
| wgpu::TextureFormatFeatureFlags::MULTISAMPLE_RESOLVE,
) {
PREFERRED_SAMPLE_COUNT
} else {
1
}
}
fn create_vertex_buffer(device: &wgpu::Device, capacity: usize) -> wgpu::Buffer {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sim-engine vertex buffer"),
size: (capacity.max(1) * std::mem::size_of::<Vertex>()) as wgpu::BufferAddress,
usage: wgpu::BufferUsages::VERTEX
| wgpu::BufferUsages::COPY_DST
| wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
})
}
fn create_dynamic_vertex_buffer(device: &wgpu::Device, capacity: usize) -> wgpu::Buffer {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sim-engine dynamic vertex buffer"),
size: (capacity.max(1) * std::mem::size_of::<DynamicGpu>()) as wgpu::BufferAddress,
usage: wgpu::BufferUsages::VERTEX
| wgpu::BufferUsages::COPY_DST
| wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
})
}
fn scalar_field_extent(field: &ScalarField) -> Result<wgpu::Extent3d, ScalarFieldTextureError> {
let width =
u32::try_from(field.width()).map_err(|_| ScalarFieldTextureError::DimensionsTooLarge)?;
let height =
u32::try_from(field.height()).map_err(|_| ScalarFieldTextureError::DimensionsTooLarge)?;
let _bytes_per_row = width
.checked_mul(std::mem::size_of::<f32>() as u32)
.ok_or(ScalarFieldTextureError::DimensionsTooLarge)?;
Ok(wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
})
}
fn scalar_value_range_extent(minimum: f32, maximum: f32) -> Option<f32> {
if !minimum.is_finite() || !maximum.is_finite() || maximum <= minimum {
return None;
}
let extent = maximum - minimum;
(extent.is_finite() && extent > 0.0).then_some(extent)
}
fn premultiplied_wgpu_color(color: Color) -> wgpu::Color {
let [red, green, blue, alpha] = color.to_array();
wgpu::Color {
r: f64::from(red * alpha),
g: f64::from(green * alpha),
b: f64::from(blue * alpha),
a: f64::from(alpha),
}
}
fn create_scalar_field_texture(
device: &wgpu::Device,
field: &ScalarField,
) -> Result<wgpu::Texture, ScalarFieldTextureError> {
let extent = scalar_field_extent(field)?;
let limits = device.limits();
if extent.width > limits.max_texture_dimension_2d
|| extent.height > limits.max_texture_dimension_2d
{
return Err(ScalarFieldTextureError::DimensionsTooLarge);
}
Ok(device.create_texture(&wgpu::TextureDescriptor {
label: Some("sim-engine scalar field texture"),
size: extent,
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R32Float,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
}))
}
fn upload_scalar_field_texture(
queue: &wgpu::Queue,
texture: &wgpu::Texture,
field: &ScalarField,
) -> Result<(), ScalarFieldTextureError> {
let extent = scalar_field_extent(field)?;
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
bytemuck::cast_slice(field.values()),
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(extent.width * std::mem::size_of::<f32>() as u32),
rows_per_image: Some(extent.height),
},
extent,
);
Ok(())
}
fn upload_scalar_field_texture_region(
queue: &wgpu::Queue,
texture: &wgpu::Texture,
x: usize,
y: usize,
width: usize,
height: usize,
values: &[f32],
) -> Result<(), ScalarFieldTextureError> {
let width = u32::try_from(width).map_err(|_| ScalarFieldTextureError::DimensionsTooLarge)?;
let height = u32::try_from(height).map_err(|_| ScalarFieldTextureError::DimensionsTooLarge)?;
let x = u32::try_from(x).map_err(|_| ScalarFieldTextureError::DimensionsTooLarge)?;
let y = u32::try_from(y).map_err(|_| ScalarFieldTextureError::DimensionsTooLarge)?;
let bytes_per_row = width
.checked_mul(std::mem::size_of::<f32>() as u32)
.ok_or(ScalarFieldTextureError::DimensionsTooLarge)?;
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture,
mip_level: 0,
origin: wgpu::Origin3d { x, y, z: 0 },
aspect: wgpu::TextureAspect::All,
},
bytemuck::cast_slice(values),
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(bytes_per_row),
rows_per_image: Some(height),
},
wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);
Ok(())
}
fn create_scalar_field_texture_resources(
device: &wgpu::Device,
queue: &wgpu::Queue,
renderer_identity: Arc<()>,
field: ScalarField,
) -> Result<ScalarFieldTexture, ScalarFieldTextureError> {
let texture = create_scalar_field_texture(device, &field)?;
upload_scalar_field_texture(queue, &texture, &field)?;
Ok(ScalarFieldTexture {
renderer_identity,
texture,
field,
})
}
fn create_cached_color_map(
device: &wgpu::Device,
queue: &wgpu::Queue,
color_map: &ColorMap,
) -> CachedColorMap {
const COLOR_MAP_LUT_SIZE: u32 = 256;
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("sim-engine color map texture"),
size: wgpu::Extent3d {
width: COLOR_MAP_LUT_SIZE,
height: 1,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8Unorm,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
let mut bytes = Vec::with_capacity(COLOR_MAP_LUT_SIZE as usize * 4);
for index in 0..COLOR_MAP_LUT_SIZE {
let color = color_map.sample_normalized(index as f32 / (COLOR_MAP_LUT_SIZE - 1) as f32);
bytes.extend(
color
.to_array()
.map(|channel| (channel * 255.0).round() as u8),
);
}
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
&bytes,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(1024),
rows_per_image: Some(1),
},
wgpu::Extent3d {
width: COLOR_MAP_LUT_SIZE,
height: 1,
depth_or_array_layers: 1,
},
);
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
CachedColorMap {
source: color_map.clone(),
_texture: texture,
view,
}
}
fn create_particle_unit_buffer(device: &wgpu::Device, queue: &wgpu::Queue) -> wgpu::Buffer {
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sim-engine particle unit quad buffer"),
size: (6 * std::mem::size_of::<ParticleUnitVertex>()) as wgpu::BufferAddress,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let unit_quad = [
ParticleUnitVertex {
direction: [-1.0, -1.0],
},
ParticleUnitVertex {
direction: [1.0, -1.0],
},
ParticleUnitVertex {
direction: [1.0, 1.0],
},
ParticleUnitVertex {
direction: [-1.0, -1.0],
},
ParticleUnitVertex {
direction: [1.0, 1.0],
},
ParticleUnitVertex {
direction: [-1.0, 1.0],
},
];
queue.write_buffer(&buffer, 0, bytemuck::cast_slice(&unit_quad));
buffer
}
fn create_particle_instance_buffer(device: &wgpu::Device, capacity: usize) -> wgpu::Buffer {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sim-engine particle instance buffer"),
size: (capacity.max(1) * std::mem::size_of::<ParticleGpu>()) as wgpu::BufferAddress,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
})
}
mod config;
mod mesh3d;
mod tessellation;
mod visualization;
use config::{
MultisampleTarget, create_multisample_target, logical_to_physical_screen,
physical_to_logical_screen, validate_scale_factor,
};
pub use config::{RendererPresentMode, RendererSurfacePresentMode, WgpuRendererOptions};
use mesh3d::Mesh3dRenderer;
pub use mesh3d::{
Mesh3dInstance, Mesh3dRenderError, Mesh3dRenderReport, Mesh3dResourceError, Object3dId,
RenderTarget3d, RetainedMesh3d, Scene3d, Scene3dError, Scene3dRestoreReport,
};
use tessellation::{screen_clip_to_scissor, tessellate_scene};
use visualization::{CachedColorMap, CompositionPipelines, create_composition_pipeline};
pub use visualization::{LayeredVisualizationError, LayeredVisualizationOptions};
#[cfg(test)]
use tessellation::world_vertex;
#[cfg(test)]
mod tests;