Rework of the review on #11. Pages and standalone images were one `Textures` manager separated by a `TextureKind` tag, and images were a second instance list beside `Primitives::instances`. The tag forced `image_index()`/`layer()` to panic on the wrong kind of handle, and the second list forced an `is_image` branch through `free`, `region_mut`, `apply_free` and `PrimitiveChange`. Pages are now their own thing. `GlyphAtlas` owns its page images outright and hands the renderer dirty rectangles; `GpuPages` owns the array texture they upload to. `Textures` is standalone images only, so `TextureHandle` has one kind, `slot()` cannot be wrong, and nothing needs a free list that skips pages. `GlyphAtlas::insert` no longer takes a `Textures`, which drops that parameter from `TextData::render` and `SizeCtx` too. Images go back through the one instance list. A texture instance is an ordinary `PrimitiveInstance` whose `idx` names a texture rather than a group-1 entry, which `PrimitiveHandle::data_idx: Option` records. `RenderLayer::plan_draws` batches the layer's instances into runs sharing a bind group, so a ui with no images still plans a single draw, and an image draws in instance order rather than on top of its layer. Group 2 is now one `texture_2d_array` and a sampler, bound per run: the atlas for rects and glyphs, or one image viewed as an array of one. That removes the second texture binding and the 1x1 null view that had to fill it. Masks move to group 3, so resizing that buffer no longer stales every texture bind group, and `GpuTextures` no longer reports whether the caller must rebuild one. `GlyphPrimitive` drops its manual pad: the WGSL struct now declares the uvs as scalars, which matches the Rust layout exactly. `#[repr(C, align(8))]` would have left real padding bytes, which `bytemuck::Pod` forbids. Verified with a headless run of the `tabs` example and of a scratch example mixing images, rects, glyphs and a mask in one layer; 52 glyphs at size 300 grew the atlas array from 1 to 4 layers with every earlier page still sampling correctly.
90 lines
2.3 KiB
Rust
90 lines
2.3 KiB
Rust
use crate::{
|
|
Axis, AxisT, IdLike, Len, RenderedText, Size, TextAttrs, TextBuffer, TextData, UiVec2,
|
|
WidgetAxisFns, WidgetId, Widgets, XAxis, YAxis, ui::cache::Cache, util::Vec2,
|
|
};
|
|
|
|
pub struct SizeCtx<'a> {
|
|
pub text: &'a mut TextData,
|
|
pub(super) source: WidgetId,
|
|
pub(super) widgets: &'a Widgets,
|
|
pub(super) cache: &'a mut Cache,
|
|
/// TODO: should this be pub? rn used for sized
|
|
pub outer: UiVec2,
|
|
pub(super) output_size: Vec2,
|
|
pub(super) id: WidgetId,
|
|
}
|
|
|
|
impl SizeCtx<'_> {
|
|
pub fn id(&self) -> &WidgetId {
|
|
&self.id
|
|
}
|
|
|
|
pub fn source(&self) -> &WidgetId {
|
|
&self.source
|
|
}
|
|
|
|
pub(super) fn len_inner<A: const AxisT>(&mut self, id: WidgetId) -> Len {
|
|
if let Some((_, len)) = self.cache.size.axis::<A>().get(&id) {
|
|
return *len;
|
|
}
|
|
let len = self
|
|
.widgets
|
|
.get_dyn_dynamic(id)
|
|
.desired_len::<A>(&mut SizeCtx {
|
|
text: self.text,
|
|
source: self.source,
|
|
widgets: self.widgets,
|
|
cache: self.cache,
|
|
outer: self.outer,
|
|
output_size: self.output_size,
|
|
id,
|
|
});
|
|
self.cache.size.axis::<A>().insert(id, (self.outer, len));
|
|
len
|
|
}
|
|
|
|
pub fn width(&mut self, id: impl IdLike) -> Len {
|
|
self.len_inner::<XAxis>(id.id())
|
|
}
|
|
|
|
pub fn height(&mut self, id: impl IdLike) -> Len {
|
|
self.len_inner::<YAxis>(id.id())
|
|
}
|
|
|
|
pub fn len_axis(&mut self, id: impl IdLike, axis: Axis) -> Len {
|
|
match axis {
|
|
Axis::X => self.width(id),
|
|
Axis::Y => self.height(id),
|
|
}
|
|
}
|
|
|
|
pub fn size(&mut self, id: impl IdLike) -> Size {
|
|
let id = id.id();
|
|
Size {
|
|
x: self.width(id),
|
|
y: self.height(id),
|
|
}
|
|
}
|
|
|
|
pub fn px_size(&mut self) -> Vec2 {
|
|
self.outer.to_abs(self.output_size)
|
|
}
|
|
|
|
pub fn output_size(&mut self) -> Vec2 {
|
|
self.output_size
|
|
}
|
|
|
|
pub fn draw_text(
|
|
&mut self,
|
|
buffer: &mut TextBuffer,
|
|
attrs: &TextAttrs,
|
|
width: Option<f32>,
|
|
) -> RenderedText {
|
|
self.text.render(buffer, attrs, width)
|
|
}
|
|
|
|
pub fn label(&self, id: WidgetId) -> &String {
|
|
self.widgets.label(id)
|
|
}
|
|
}
|