use crate::atlas::AtlasRect;
use crate::image::{ImageData, ImageFormat};
use crate::{Color, Corners, Edges, Point, Rect, Size};
use bytemuck::{Pod, Zeroable, cast_slice};
use slotmap::{SlotMap, new_key_type};
use std::ops::Range;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use wgpu::{VertexAttribute, vertex_attr_array};
const REMOVED_LOG: usize = 4096;
static NEXT_LIST: AtomicU64 = AtomicU64::new(1);
new_key_type! {
pub struct ListKey;
}
pub struct DrawLists {
id: u64,
lists: SlotMap<ListKey, DrawList>,
removed: Vec<u64>,
forgotten: u64,
}
impl Default for DrawLists {
fn default() -> Self {
DrawLists {
id: NEXT_LIST.fetch_add(1, Ordering::Relaxed),
lists: SlotMap::default(),
removed: Vec::new(),
forgotten: 0,
}
}
}
impl DrawLists {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, list: DrawList) -> ListKey {
self.lists.insert(list)
}
pub fn get(&self, key: ListKey) -> Option<&DrawList> {
self.lists.get(key)
}
pub fn get_mut(&mut self, key: ListKey) -> Option<&mut DrawList> {
self.lists.get_mut(key)
}
pub fn remove(&mut self, key: ListKey) -> Option<DrawList> {
let list = self.lists.remove(key)?;
if self.removed.len() == REMOVED_LOG {
self.forgotten += REMOVED_LOG as u64;
self.removed.clear();
}
self.removed.push(list.id);
Some(list)
}
pub(crate) fn id(&self) -> u64 {
self.id
}
pub(crate) fn removed_since(&self, seen: u64) -> (&[u64], u64) {
let known = seen.saturating_sub(self.forgotten) as usize;
let removed = self.removed.get(known..).unwrap_or_default();
(removed, self.forgotten + self.removed.len() as u64)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Quad {
pub bounds: Rect,
pub clip: Rect,
pub background: Color,
pub border_color: Color,
pub corner_radii: Corners,
pub border_widths: Edges,
pub backing: bool,
}
impl Quad {
pub const fn new(bounds: Rect, background: Color) -> Self {
Quad {
bounds,
clip: Rect::EVERYTHING,
background,
border_color: Color::TRANSPARENT,
corner_radii: Corners::ZERO,
border_widths: Edges::ZERO,
backing: false,
}
}
pub const fn backing(bounds: Rect) -> Self {
Quad {
backing: true,
..Quad::new(bounds, Color::TRANSPARENT)
}
}
pub fn line(bounds: Rect, color: Color) -> Self {
let Size { width, height } = bounds.size;
let border_widths = if height <= width {
Edges {
top: height,
..Edges::ZERO
}
} else {
Edges {
left: width,
..Edges::ZERO
}
};
Quad {
border_color: color,
border_widths,
..Quad::new(bounds, Color::TRANSPARENT)
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Shadow {
pub bounds: Rect,
pub clip: Rect,
pub corner_radii: Corners,
pub color: Color,
pub blur_radius: f32,
pub backing: bool,
}
impl Shadow {
pub const fn new(bounds: Rect, color: Color, blur_radius: f32) -> Self {
Shadow {
bounds,
clip: Rect::EVERYTHING,
corner_radii: Corners::ZERO,
color,
blur_radius,
backing: false,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Glyph {
pub origin: Point,
pub clip: Rect,
pub color: Color,
pub mask: AtlasRect,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ImageQuad {
pub bounds: Rect,
pub clip: Rect,
pub corner_radii: Corners,
pub source: Rect,
pub tint: Color,
pub image: ImageData,
}
impl ImageQuad {
pub const fn new(bounds: Rect, image: ImageData) -> Self {
ImageQuad {
bounds,
clip: Rect::EVERYTHING,
corner_radii: Corners::ZERO,
source: Rect::new(0.0, 0.0, 1.0, 1.0),
tint: Color::WHITE,
image,
}
}
}
#[derive(Clone, PartialEq)]
pub(crate) struct Batch {
pub image: Option<ImageData>,
pub instances: Range<u32>,
}
const SHADOW: u32 = 1;
const GLYPH: u32 = 2;
const IMAGE: u32 = 3;
const LAYER_FILL: u32 = 4;
const LAYER_RADII: u32 = 8;
const ALPHA_MASK: u32 = 16;
#[repr(C)]
#[derive(Clone, Copy, Default, Pod, Zeroable)]
pub(crate) struct Instance {
bounds: [f32; 4],
clip: [f32; 4],
radii: [f32; 4],
params: [f32; 4],
color: [u16; 4],
border_color: [u16; 4],
flags: u32,
}
impl Instance {
pub(crate) const ATTRIBUTES: [VertexAttribute; 7] = vertex_attr_array![
0 => Float32x4,
1 => Float32x4,
2 => Float32x4,
3 => Float32x4,
4 => Unorm16x4,
5 => Unorm16x4,
6 => Uint32,
];
pub(crate) const SIZE: u64 = size_of::<Instance>() as u64;
}
fn unorm(color: Color) -> [u16; 4] {
color
.to_array()
.map(|channel| (channel.clamp(0.0, 1.0) * f32::from(u16::MAX)).round() as u16)
}
#[derive(Clone, Default)]
struct Geometry {
instances: Vec<Instance>,
batches: Vec<Batch>,
}
pub struct DrawList {
id: u64,
geometry: Arc<Geometry>,
extent: Option<Rect>,
revision: u64,
}
impl Default for DrawList {
fn default() -> Self {
DrawList {
id: NEXT_LIST.fetch_add(1, Ordering::Relaxed),
geometry: Arc::default(),
extent: None,
revision: 0,
}
}
}
impl Clone for DrawList {
fn clone(&self) -> Self {
Self {
id: NEXT_LIST.fetch_add(1, Ordering::Relaxed),
geometry: self.geometry.clone(),
extent: self.extent,
revision: self.revision,
}
}
}
impl DrawList {
pub fn new() -> Self {
Self::default()
}
pub fn clear(&mut self) {
if let Some(geometry) = Arc::get_mut(&mut self.geometry) {
geometry.instances.clear();
geometry.batches.clear();
} else {
self.geometry = Arc::default();
}
self.extent = None;
self.revision += 1;
}
pub fn replace(&mut self, other: DrawList) {
if !Arc::ptr_eq(&self.geometry, &other.geometry) {
if self.extent != other.extent
|| self.batches() != other.batches()
|| cast_slice::<Instance, u8>(self.instances())
!= cast_slice::<Instance, u8>(other.instances())
{
self.revision += 1;
}
self.geometry = other.geometry;
self.extent = other.extent;
}
}
pub(crate) fn id(&self) -> u64 {
self.id
}
pub fn extent(&self) -> Option<Rect> {
self.extent
}
pub fn revision(&self) -> u64 {
self.revision
}
pub fn is_empty(&self) -> bool {
self.geometry.instances.is_empty()
}
pub fn len(&self) -> usize {
self.geometry.instances.len()
}
pub fn append(&mut self, other: &DrawList, offset: Point) {
let Some(extent) = other.extent else {
return;
};
let everything = Rect::EVERYTHING.to_array();
let geometry = Arc::make_mut(&mut self.geometry);
let base = geometry.instances.len() as u32;
if offset == Point::ZERO {
geometry.instances.extend_from_slice(other.instances());
} else {
let moved = other.instances().iter().map(|instance| {
let mut instance = *instance;
instance.bounds[0] += offset.x;
instance.bounds[1] += offset.y;
if instance.clip != everything {
instance.clip[0] += offset.x;
instance.clip[1] += offset.y;
}
instance
});
geometry.instances.extend(moved);
}
for batch in other.batches() {
let instances = base + batch.instances.start..base + batch.instances.end;
match geometry
.batches
.last_mut()
.filter(|last| last.image == batch.image)
{
Some(last) => last.instances.end = instances.end,
None => geometry.batches.push(Batch {
image: batch.image.clone(),
instances,
}),
}
}
let extent = extent.translate(offset);
self.extent = Some(self.extent.map_or(extent, |known| known.union(&extent)));
self.revision += 1;
}
pub fn push_quad(&mut self, quad: Quad) {
if quad.bounds.is_empty() || quad.clip.intersect(&quad.bounds).is_empty() {
return;
}
let backing = if quad.backing {
LAYER_FILL | LAYER_RADII
} else {
0
};
self.push(
None,
quad.bounds.inflate(1.0).intersect(&quad.clip),
Instance {
bounds: quad.bounds.to_array(),
clip: quad.clip.to_array(),
radii: quad.corner_radii.to_array(),
params: quad.border_widths.to_array(),
color: unorm(quad.background),
border_color: unorm(quad.border_color),
flags: backing,
},
);
}
pub fn push_shadow(&mut self, shadow: Shadow) {
if shadow.color.a <= 0.0 {
return;
}
let reach = shadow.blur_radius.max(0.0) * 1.5 + 2.0;
self.push(
None,
shadow.bounds.inflate(reach).intersect(&shadow.clip),
Instance {
bounds: shadow.bounds.to_array(),
clip: shadow.clip.to_array(),
radii: shadow.corner_radii.to_array(),
params: [shadow.blur_radius.max(0.0) * 0.5, 0.0, 0.0, 0.0],
color: unorm(shadow.color),
flags: SHADOW | if shadow.backing { LAYER_RADII } else { 0 },
..Default::default()
},
);
}
pub fn push_glyph(&mut self, glyph: Glyph) {
if glyph.color.a <= 0.0 {
return;
}
let mask = glyph.mask;
let reach = Rect {
origin: glyph.origin,
size: Size::new(mask.width as f32, mask.height as f32),
};
self.push(
None,
reach.inflate(1.0).intersect(&glyph.clip),
Instance {
bounds: [glyph.origin.x, glyph.origin.y, 0.0, 0.0],
clip: glyph.clip.to_array(),
params: [
mask.x as f32,
mask.y as f32,
mask.width as f32,
mask.height as f32,
],
color: unorm(glyph.color),
flags: GLYPH,
..Default::default()
},
);
}
pub fn push_image(&mut self, image: ImageQuad) {
let ImageQuad {
bounds,
clip,
corner_radii,
source,
tint,
image,
} = image;
if bounds.is_empty() || clip.intersect(&bounds).is_empty() || tint.a <= 0.0 {
return;
}
let mask = if image.format() == ImageFormat::Alpha8 {
ALPHA_MASK
} else {
0
};
self.push(
Some(image),
bounds.inflate(1.0).intersect(&clip),
Instance {
bounds: bounds.to_array(),
clip: clip.to_array(),
radii: corner_radii.to_array(),
params: source.to_array(),
color: unorm(tint),
flags: IMAGE | mask,
..Default::default()
},
);
}
fn push(&mut self, image: Option<ImageData>, reach: Rect, instance: Instance) {
let geometry = Arc::make_mut(&mut self.geometry);
let index = geometry.instances.len() as u32;
match geometry
.batches
.last_mut()
.filter(|batch| batch.image == image)
{
Some(batch) => batch.instances.end = index + 1,
None => geometry.batches.push(Batch {
image,
instances: index..index + 1,
}),
}
geometry.instances.push(instance);
self.extent = Some(self.extent.map_or(reach, |extent| extent.union(&reach)));
self.revision += 1;
}
pub(crate) fn instances(&self) -> &[Instance] {
&self.geometry.instances
}
pub(crate) fn batches(&self) -> &[Batch] {
&self.geometry.batches
}
}