use super::{
area::Area,
params::{self, Filter},
};
use crate::{renderer::viewport::scissor, DrawData, Rect, Vec2};
use std::ops::Range;
use winit::dpi::PhysicalSize;
pub const MAX_EFFECTS: usize = 256;
const DIRTY_LIMIT: usize = 32;
#[derive(Clone, Debug)]
pub struct Job {
pub command: usize,
pub sigma: f32,
pub bounds: Area,
pub filter: Filter,
pub pyramid: Vec<Area>,
pub scratch: Area,
pub result: Area,
}
#[derive(Debug, Default)]
pub struct Plan {
pub jobs: Vec<Job>,
pub batches: Vec<Range<usize>>,
}
impl Plan {
pub fn job_of(&self, command: usize) -> Option<usize> {
self.jobs
.binary_search_by_key(&command, |job| job.command)
.ok()
}
}
pub fn command_area(data: &DrawData, index: usize, size: [u32; 2]) -> Option<Area> {
let command = &data.commands[index];
if command.indices.is_empty() {
return None;
}
let physical = PhysicalSize::new(size[0], size[1]);
scissor(command.clip_rect, data.scale_factor, physical).map(Area::from_scissor)
}
fn mesh_area(data: &DrawData, index: usize, size: [u32; 2]) -> Option<Area> {
let command = &data.commands[index];
let (mut min, mut max) = (Vec2::splat(f32::INFINITY), Vec2::splat(f32::NEG_INFINITY));
for &i in &data.indices[command.indices.start as usize..command.indices.end as usize] {
let position = Vec2::from_array(data.vertices[i as usize].position);
min = min.min(position);
max = max.max(position);
}
if !(min.x <= max.x && min.y <= max.y) {
return None;
}
let bounds = Rect::from_min_max(min, max).intersect(command.clip_rect);
let physical = PhysicalSize::new(size[0], size[1]);
scissor(bounds, data.scale_factor, physical).map(Area::from_scissor)
}
pub fn regions(bounds: Area, filter: Filter, size: [u32; 2]) -> (Vec<Area>, Area, Area) {
let level = filter.level;
let last = params::level_size(size, level);
let result = if level == 0 {
bounds
} else {
bounds.to_level(level).grow(2, 2, last)
};
let r = filter.radius;
let scratch = result.grow(0, r, last);
let mut pyramid = vec![Area::default(); level as usize + 1];
pyramid[level as usize] = scratch.grow(r, 0, last);
for k in (1..=level).rev() {
pyramid[k as usize - 1] = pyramid[k as usize].finer(params::level_size(size, k - 1));
}
(pyramid, scratch, result)
}
pub fn plan(data: &DrawData, size: [u32; 2]) -> Plan {
let mut plan = Plan::default();
let mut dirty: Vec<Area> = Vec::new();
let mut start = 0;
for (index, command) in data.commands.iter().enumerate() {
let Some(blur) = command.blur else {
if !plan.jobs.is_empty() {
if let Some(area) = command_area(data, index, size) {
dirty.push(area);
}
}
continue;
};
let Some(bounds) = mesh_area(data, index, size) else {
continue;
};
let sigma = blur * data.scale_factor;
let filter = params::filter(sigma);
let (pyramid, scratch, result) = regions(bounds, filter, size);
let reads = pyramid[0];
if plan.jobs.is_empty() || dirty.iter().any(|area| area.intersects(reads)) {
if !plan.jobs.is_empty() {
plan.batches.push(start..plan.jobs.len());
}
start = plan.jobs.len();
dirty.clear();
}
dirty.push(bounds);
if dirty.len() > DIRTY_LIMIT {
let all = dirty.iter().copied().reduce(Area::union).unwrap_or(bounds);
dirty = vec![all];
}
plan.jobs.push(Job {
command: index,
sigma,
bounds,
filter,
pyramid,
scratch,
result,
});
}
if !plan.jobs.is_empty() {
plan.batches.push(start..plan.jobs.len());
}
plan
}