157 lines
6.8 KiB
Rust
157 lines
6.8 KiB
Rust
//! Experimental host-side split planner for egui-wgpu 0.36.2.
|
|
//!
|
|
//! This file is not compiled by the client. It assumes `renderer-range.patch`
|
|
//! has been applied and intentionally performs no GPU work by itself.
|
|
use std::ops::Range;
|
|
|
|
use epaint::{ClippedPrimitive, Primitive};
|
|
|
|
use crate::BackdropMarker;
|
|
|
|
/// A range is always a slice of the complete list passed to `update_buffers`.
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
pub struct DrawRange {
|
|
pub jobs: Range<usize>,
|
|
/// Counts all preceding mesh jobs, including clipped and missing-texture meshes.
|
|
pub preceding_meshes: usize,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub struct BackdropRegion {
|
|
/// Unclipped panel rectangle in physical pixels (min x/y, max x/y).
|
|
pub panel: [f32; 4],
|
|
/// Intersection with egui clip and viewport, with exclusive upper edges.
|
|
pub scissor: [u32; 4],
|
|
pub corner_radius: f32,
|
|
pub sigma: f32,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum Step {
|
|
Draw(DrawRange),
|
|
Backdrop(BackdropRegion),
|
|
}
|
|
|
|
/// Construct an ordered plan without allocating GPU resources. A marker that
|
|
/// falls outside its clip or viewport is ignored. Invalid numeric parameters
|
|
/// reject the whole plan so the host can choose its opaque fallback *before*
|
|
/// encoding any pass.
|
|
pub fn plan(
|
|
jobs: &[ClippedPrimitive],
|
|
pixels_per_point: f32,
|
|
frame_size: [u32; 2],
|
|
) -> Result<Vec<Step>, &'static str> {
|
|
if !pixels_per_point.is_finite() || pixels_per_point <= 0.0 {
|
|
return Err("invalid pixel scale");
|
|
}
|
|
let mut steps = Vec::new();
|
|
let mut range_start = 0;
|
|
let mut range_meshes = 0;
|
|
let mut preceding_meshes = 0;
|
|
for (index, job) in jobs.iter().enumerate() {
|
|
match &job.primitive {
|
|
Primitive::Mesh(_) => range_meshes += 1,
|
|
Primitive::Callback(callback) => {
|
|
let Some(marker) = callback.callback.downcast_ref::<BackdropMarker>() else {
|
|
continue;
|
|
};
|
|
if !marker.blur_radius_points.is_finite()
|
|
|| marker.blur_radius_points <= 0.0
|
|
|| !marker.corner_radius_points.is_finite()
|
|
|| marker.corner_radius_points < 0.0
|
|
{
|
|
return Err("invalid backdrop parameters");
|
|
}
|
|
let panel = callback.rect;
|
|
let clip = job.clip_rect;
|
|
let panel_values = [panel.min.x, panel.min.y, panel.max.x, panel.max.y];
|
|
let clip_values = [clip.min.x, clip.min.y, clip.max.x, clip.max.y];
|
|
// An infinite clip is valid in egui; NaN is never meaningful.
|
|
if panel_values.iter().any(|value| !value.is_finite())
|
|
|| clip_values.iter().any(|value| value.is_nan())
|
|
{
|
|
return Err("non-finite backdrop rectangle");
|
|
}
|
|
let scale = pixels_per_point;
|
|
let logical = panel.intersect(clip);
|
|
// Match egui-wgpu's rounded clip scissor; the panel bounds are
|
|
// conservatively expanded for the shader's antialiased mask.
|
|
let left = ((panel.min.x * scale).floor() as u32)
|
|
.max((clip.min.x * scale).round() as u32).min(frame_size[0]);
|
|
let top = ((panel.min.y * scale).floor() as u32)
|
|
.max((clip.min.y * scale).round() as u32).min(frame_size[1]);
|
|
let right = ((panel.max.x * scale).ceil() as u32)
|
|
.min((clip.max.x * scale).round() as u32).min(frame_size[0]);
|
|
let bottom = ((panel.max.y * scale).ceil() as u32)
|
|
.min((clip.max.y * scale).round() as u32).min(frame_size[1]);
|
|
if range_start < index {
|
|
steps.push(Step::Draw(DrawRange { jobs: range_start..index, preceding_meshes }));
|
|
}
|
|
preceding_meshes += range_meshes;
|
|
range_meshes = 0;
|
|
range_start = index + 1;
|
|
if left < right && top < bottom && logical.min.x < logical.max.x && logical.min.y < logical.max.y {
|
|
let panel_pixels = [panel.min.x * scale, panel.min.y * scale,
|
|
panel.max.x * scale, panel.max.y * scale];
|
|
let corner_radius = marker.corner_radius_points * scale;
|
|
let sigma = marker.blur_radius_points * scale;
|
|
if panel_pixels.iter().any(|v| !v.is_finite()) || !corner_radius.is_finite() || !sigma.is_finite() {
|
|
return Err("scaled backdrop parameters overflowed");
|
|
}
|
|
steps.push(Step::Backdrop(BackdropRegion {
|
|
panel: panel_pixels, scissor: [left, top, right, bottom], corner_radius, sigma,
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if range_start < jobs.len() {
|
|
steps.push(Step::Draw(DrawRange { jobs: range_start..jobs.len(), preceding_meshes }));
|
|
}
|
|
Ok(steps)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use epaint::{emath::{pos2, Rect}, Mesh};
|
|
|
|
fn mesh(clip_rect: Rect) -> ClippedPrimitive {
|
|
ClippedPrimitive { clip_rect, primitive: Primitive::Mesh(Mesh::default()) }
|
|
}
|
|
|
|
fn marker(rect: Rect, clip_rect: Rect) -> ClippedPrimitive {
|
|
let marker = BackdropMarker { blur_radius_points: 4.0, corner_radius_points: 3.0 };
|
|
ClippedPrimitive { clip_rect, primitive: Primitive::Callback(marker.new_paint_callback(rect)) }
|
|
}
|
|
|
|
#[test]
|
|
fn tracks_even_clipped_meshes_and_matches_egui_clip_rounding() {
|
|
let rect = Rect::from_min_max(pos2(1.0, 2.0), pos2(10.0, 12.0));
|
|
let clip = Rect::from_min_max(pos2(2.1, 2.0), pos2(8.1, 12.0));
|
|
let jobs = [mesh(Rect::NOTHING), marker(rect, clip), mesh(Rect::EVERYTHING)];
|
|
let result = plan(&jobs, 1.5, [100, 100]).unwrap();
|
|
assert_eq!(result[0], Step::Draw(DrawRange { jobs: 0..1, preceding_meshes: 0 }));
|
|
assert_eq!(result[2], Step::Draw(DrawRange { jobs: 2..3, preceding_meshes: 1 }));
|
|
let Step::Backdrop(region) = &result[1] else { panic!("missing backdrop") };
|
|
assert_eq!(region.scissor, [3, 3, 12, 18]);
|
|
assert_eq!(region.corner_radius, 4.5);
|
|
}
|
|
|
|
#[test]
|
|
fn clipped_markers_do_not_shift_mesh_offsets() {
|
|
let rect = Rect::from_min_max(pos2(1.0, 1.0), pos2(4.0, 4.0));
|
|
let jobs = [marker(rect, Rect::NOTHING), mesh(Rect::EVERYTHING),
|
|
marker(rect, Rect::EVERYTHING), mesh(Rect::EVERYTHING)];
|
|
let result = plan(&jobs, 1.0, [10, 10]).unwrap();
|
|
assert_eq!(result[0], Step::Draw(DrawRange { jobs: 1..2, preceding_meshes: 0 }));
|
|
assert_eq!(result[2], Step::Draw(DrawRange { jobs: 3..4, preceding_meshes: 1 }));
|
|
}
|
|
|
|
#[test]
|
|
fn bad_scale_is_rejected_before_encoding() {
|
|
assert!(plan(&[], f32::NAN, [10, 10]).is_err());
|
|
assert!(plan(&[], 0.0, [10, 10]).is_err());
|
|
}
|
|
}
|