oopsie (orderlerss -> ordered rendering)

This commit is contained in:
2025-11-22 20:22:26 -05:00
parent d757e805e8
commit 90c579d734
10 changed files with 105 additions and 75 deletions

View File

@@ -2,11 +2,12 @@ use std::ops::{Index, IndexMut};
use crate::render::{MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst, Primitives};
struct LayerNode {
struct LayerNode<T> {
next: Ptr,
prev: Ptr,
child: Option<Child>,
data: Layer,
depth: usize,
data: T,
}
#[derive(Clone, Copy, Debug)]
@@ -21,26 +22,22 @@ enum Ptr {
/// TODO: currently this does not ever free layers
/// is that realistically desired?
pub struct Layers {
vec: Vec<LayerNode>,
pub struct Layers<T> {
vec: Vec<LayerNode<T>>,
/// index of last layer at top level (start at first = 0)
last: usize,
}
/// TODO: this can be replaced with Primitives itself atm
#[derive(Default)]
pub struct Layer {
pub primitives: Primitives,
}
#[derive(Clone, Copy)]
struct Child {
head: usize,
tail: usize,
}
impl Layers {
pub fn new() -> Layers {
pub type PrimitiveLayers = Layers<Primitives>;
impl<T: Default> Layers<T> {
pub fn new() -> Layers<T> {
Self {
vec: vec![LayerNode::head()],
last: 0,
@@ -52,7 +49,7 @@ impl Layers {
self.vec.push(LayerNode::head());
}
fn push(&mut self, node: LayerNode) -> usize {
fn push(&mut self, node: LayerNode<T>) -> usize {
let i = self.vec.len();
self.vec.push(node);
i
@@ -63,9 +60,10 @@ impl Layers {
return i;
}
let i_new = self.push(LayerNode::new(
Layer::default(),
T::default(),
self.vec[i].next,
Ptr::Next(i),
self.vec[i].depth,
));
self.vec[i].next = Ptr::Next(i_new);
self.vec[i_new].prev = Ptr::Next(i);
@@ -82,9 +80,10 @@ impl Layers {
return c.head;
}
let i_child = self.push(LayerNode::new(
Layer::default(),
T::default(),
Ptr::Parent(i),
Ptr::Parent(i),
self.vec[i].depth + 1,
));
self.vec[i].child = Some(Child {
head: i_child,
@@ -93,107 +92,115 @@ impl Layers {
i_child
}
pub fn iter_mut(&mut self) -> LayerIteratorMut<'_> {
pub fn iter_mut(&mut self) -> LayerIteratorMut<'_, T> {
LayerIteratorMut::new(&mut self.vec, self.last)
}
pub fn iter_orderless_mut(&mut self) -> impl Iterator<Item = (usize, &mut Layer)> {
pub fn iter_orderless_mut(&mut self) -> impl Iterator<Item = (usize, &mut T)> {
self.vec.iter_mut().map(|n| &mut n.data).enumerate()
}
pub fn iter(&self) -> impl Iterator<Item = (usize, &Layer)> {
pub fn iter(&self) -> impl Iterator<Item = (usize, &T)> {
self.indices().map(|i| (i, &self.vec[i].data))
}
pub fn indices(&self) -> LayerIndexIterator<'_> {
pub fn iter_depth(&self) -> impl Iterator<Item = ((usize, usize), &T)> {
self.indices()
.map(|i| ((i, self.vec[i].depth), &self.vec[i].data))
}
pub fn indices(&self) -> LayerIndexIterator<'_, T> {
LayerIndexIterator::new(&self.vec, self.last)
}
pub fn write<P: Primitive>(&mut self, layer: usize, info: PrimitiveInst<P>) -> PrimitiveHandle {
self[layer].primitives.write(layer, info)
}
pub fn free(&mut self, h: &PrimitiveHandle) -> MaskIdx {
self[h.layer].primitives.free(h)
}
}
impl Default for Layers {
impl PrimitiveLayers {
pub fn write<P: Primitive>(&mut self, layer: usize, info: PrimitiveInst<P>) -> PrimitiveHandle {
self[layer].write(layer, info)
}
pub fn free(&mut self, h: &PrimitiveHandle) -> MaskIdx {
self[h.layer].free(h)
}
}
impl<T: Default> Default for Layers<T> {
fn default() -> Self {
Self::new()
}
}
impl Index<usize> for Layers {
type Output = Layer;
impl<T> Index<usize> for Layers<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
&self.vec[index].data
}
}
impl IndexMut<usize> for Layers {
impl<T> IndexMut<usize> for Layers<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.vec[index].data
}
}
impl LayerNode {
pub fn new(data: Layer, next: Ptr, prev: Ptr) -> Self {
impl<T: Default> LayerNode<T> {
pub fn new(data: T, next: Ptr, prev: Ptr, depth: usize) -> Self {
Self {
next,
prev,
child: None,
data,
depth,
}
}
pub fn head() -> Self {
Self::new(Layer::default(), Ptr::None, Ptr::None)
Self::new(T::default(), Ptr::None, Ptr::None, 0)
}
}
pub struct LayerIteratorMut<'a> {
inner: LayerIndexIterator<'a>,
pub struct LayerIteratorMut<'a, T> {
inner: LayerIndexIterator<'a, T>,
}
impl<'a> Iterator for LayerIteratorMut<'a> {
type Item = (usize, &'a mut Layer);
impl<'a, T> Iterator for LayerIteratorMut<'a, T> {
type Item = (usize, &'a mut T);
fn next(&mut self) -> Option<Self::Item> {
let i = self.inner.next()?;
// SAFETY: requires index iterator to work properly
#[allow(mutable_transmutes)]
let layer = unsafe { std::mem::transmute::<&Layer, &mut Layer>(&self.inner.vec[i].data) };
let layer = unsafe { std::mem::transmute::<&T, &mut T>(&self.inner.vec[i].data) };
Some((i, layer))
}
}
impl<'a> DoubleEndedIterator for LayerIteratorMut<'a> {
impl<'a, T> DoubleEndedIterator for LayerIteratorMut<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
let i = self.inner.next_back()?;
// SAFETY: requires index iterator to work properly
#[allow(mutable_transmutes)]
let layer = unsafe { std::mem::transmute::<&Layer, &mut Layer>(&self.inner.vec[i].data) };
let layer = unsafe { std::mem::transmute::<&T, &mut T>(&self.inner.vec[i].data) };
Some((i, layer))
}
}
impl<'a> LayerIteratorMut<'a> {
fn new(vec: &'a mut Vec<LayerNode>, last: usize) -> Self {
impl<'a, T> LayerIteratorMut<'a, T> {
fn new(vec: &'a mut Vec<LayerNode<T>>, last: usize) -> Self {
Self {
inner: LayerIndexIterator::new(vec, last),
}
}
}
pub struct LayerIndexIterator<'a> {
pub struct LayerIndexIterator<'a, T> {
next: Option<usize>,
next_back: Option<usize>,
vec: &'a Vec<LayerNode>,
vec: &'a Vec<LayerNode<T>>,
}
impl<'a> Iterator for LayerIndexIterator<'a> {
impl<'a, T> Iterator for LayerIndexIterator<'a, T> {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
@@ -224,7 +231,7 @@ impl<'a> Iterator for LayerIndexIterator<'a> {
}
}
impl<'a> DoubleEndedIterator for LayerIndexIterator<'a> {
impl<'a, T> DoubleEndedIterator for LayerIndexIterator<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
let ret_i = self.next_back?;
let node = &self.vec[ret_i];
@@ -246,8 +253,8 @@ impl<'a> DoubleEndedIterator for LayerIndexIterator<'a> {
}
}
impl<'a> LayerIndexIterator<'a> {
fn new(vec: &'a Vec<LayerNode>, last: usize) -> Self {
impl<'a, T> LayerIndexIterator<'a, T> {
fn new(vec: &'a Vec<LayerNode<T>>, last: usize) -> Self {
let mut last = last;
while let Some(c) = vec[last].child {
last = c.tail;

View File

@@ -1,6 +1,6 @@
use crate::{
layout::{
Axis, Layers, Len, Modules, Size, TextAttrs, TextBuffer, TextData, RenderedText,
Axis, PrimitiveLayers, Len, Modules, Size, TextAttrs, TextBuffer, TextData, RenderedText,
TextureHandle, Textures, UiRegion, UiVec2, Vec2, WidgetId, Widgets,
},
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
@@ -25,7 +25,7 @@ pub struct Painter<'a, 'c> {
struct PainterCtx<'a> {
pub widgets: &'a Widgets,
pub active: &'a mut HashMap<Id, WidgetInstance>,
pub layers: &'a mut Layers,
pub layers: &'a mut PrimitiveLayers,
pub textures: &'a mut Textures,
pub masks: &'a mut TrackedArena<Mask, u32>,
pub text: &'a mut TextData,
@@ -64,7 +64,7 @@ pub struct WidgetInstance {
pub struct PainterData {
pub widgets: Widgets,
pub active: HashMap<Id, WidgetInstance>,
pub layers: Layers,
pub layers: PrimitiveLayers,
pub textures: Textures,
pub text: TextData,
pub output_size: Vec2,
@@ -277,7 +277,7 @@ impl<'a> PainterCtx<'a> {
fn mov(&mut self, id: Id, from: UiRegion, to: UiRegion) {
let active = self.active.get_mut(&id).unwrap();
for h in &active.primitives {
let region = self.layers[h.layer].primitives.region_mut(h);
let region = self.layers[h.layer].region_mut(h);
*region = region.outside(&from).within(&to);
}
active.region = active.region.outside(&from).within(&to);

View File

@@ -184,6 +184,18 @@ impl Ui {
}
}
pub fn debug_layers(&self) {
for ((idx, depth), primitives) in self.data.layers.iter_depth() {
let indent = " ".repeat(depth * 2);
let len = primitives.instances().len();
print!("{indent}{idx}: {len} primitives");
if len >= 1 {
print!(" ({})", primitives.instances()[0].binding);
}
println!();
}
}
pub fn window_region<W>(&self, id: &impl IdLike<W>) -> Option<PixelRegion> {
let region = self.data.active.get(&id.id())?.region;
Some(region.to_px(self.data.output_size))