`draw` had three branches, one per shader, which is the dynamic dispatch this was asking for. A primitive now brings a `PrimitiveRender`: it states the layout its shader reads, uploads whatever it owns, and records its own draws. `GlyphRender` owns the atlas and binds it once for a list; `ImageRender` owns the images and binds one per instance; the default owns nothing and draws them all in one call. The renderer sets the pipeline, the shared group, the list's data and the vertex buffer, and knows nothing else about what it is drawing. Measured before committing to it, since dispatch per list is the cost. Wall time on this machine swings 2x between runs of one binary, so the comparison is instructions retired, which is stable to 0.1%: at 256 layers drawing 8 rects, 8 glyphs and 2 images each, 7.4074e9 against 7.4145e9, and at 1024 layers 27.467e9 against 27.498e9. Both are 0.1%, which is 6 instructions per list drawn -- one indirect call. Recording a list into the pass costs wgpu about 5,400. `tests/draw_cost.rs` is that measurement, kept. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
225 lines
7.3 KiB
Rust
225 lines
7.3 KiB
Rust
use std::{cell::RefCell, rc::Rc};
|
|
use winit::event::WindowEvent;
|
|
|
|
use iris::prelude::*;
|
|
type ClientRsc = DefaultRsc<Client>;
|
|
|
|
fn main() {
|
|
DefaultApp::<Client>::run();
|
|
}
|
|
|
|
#[derive(DefaultUiState)]
|
|
pub struct Client {
|
|
ui_state: DefaultUiState,
|
|
info: WeakWidget<Text>,
|
|
}
|
|
|
|
impl DefaultAppState for Client {
|
|
fn new(
|
|
mut ui_state: DefaultUiState,
|
|
rsc: &mut DefaultRsc<Self>,
|
|
_: Proxy<Self::Event>,
|
|
) -> Self {
|
|
let rrect = rect(Color::WHITE).radius(20);
|
|
let pad_test = (
|
|
rrect.color(Color::BLUE),
|
|
(
|
|
rrect
|
|
.color(Color::RED)
|
|
.sized((100, 100))
|
|
.center()
|
|
.width(rest(2)),
|
|
(
|
|
rrect.color(Color::ORANGE),
|
|
rrect.color(Color::LIME).pad(10.0),
|
|
)
|
|
.span(Dir::RIGHT)
|
|
.width(rest(2)),
|
|
rrect.color(Color::YELLOW),
|
|
)
|
|
.span(Dir::RIGHT)
|
|
.pad(10)
|
|
.width(rest(3)),
|
|
)
|
|
.span(Dir::RIGHT)
|
|
.add(rsc);
|
|
|
|
let span_test = (
|
|
rrect.color(Color::GREEN).width(100),
|
|
rrect.color(Color::ORANGE),
|
|
rrect.color(Color::CYAN),
|
|
rrect.color(Color::BLUE).width(rel(0.5)),
|
|
rrect.color(Color::MAGENTA).width(100),
|
|
rrect.color(Color::RED).width(100),
|
|
)
|
|
.span(Dir::LEFT)
|
|
.add(rsc);
|
|
|
|
let span_add = Span::empty(Dir::RIGHT).add(rsc);
|
|
|
|
let add_button = rect(Color::LIME)
|
|
.radius(30)
|
|
.on(CursorSense::click(), move |_, rsc| {
|
|
let child = image(include_bytes!("assets/sungals.png"))
|
|
.center()
|
|
.add_strong(rsc);
|
|
span_add(rsc).push(child);
|
|
})
|
|
.sized((150, 150))
|
|
.align(Align::BOT_RIGHT);
|
|
|
|
let del_button = rect(Color::RED)
|
|
.radius(30)
|
|
.on(CursorSense::click(), move |_, rsc| {
|
|
span_add(rsc).pop();
|
|
})
|
|
.sized((150, 150))
|
|
.align(Align::BOT_LEFT);
|
|
|
|
let span_add_test = (span_add, add_button, del_button).stack().add(rsc);
|
|
|
|
let btext = |content| wtext(content).size(30);
|
|
|
|
let text_test = (
|
|
btext("this is a").align(Align::LEFT),
|
|
btext("teeeeeeeest").align(Align::RIGHT),
|
|
btext("okkk\nokkkkkk!").align(Align::LEFT),
|
|
btext("hmm"),
|
|
btext("a"),
|
|
(
|
|
btext("'").family(Family::Monospace).align(Align::TOP),
|
|
btext("'").family(Family::Monospace),
|
|
btext(":gamer mode").family(Family::Monospace),
|
|
rect(Color::CYAN).sized((10, 10)).center(),
|
|
rect(Color::RED).sized((100, 100)).center(),
|
|
rect(Color::PURPLE).sized((50, 50)).align(Align::TOP),
|
|
)
|
|
.span(Dir::RIGHT)
|
|
.center(),
|
|
wtext("pretty cool right?").size(50),
|
|
)
|
|
.span(Dir::DOWN)
|
|
.add(rsc);
|
|
|
|
let texts = Span::empty(Dir::DOWN).gap(10).add(rsc);
|
|
let msg_area = texts.scrollable().masked().background(rect(Color::SKY));
|
|
let add_text = wtext("add")
|
|
.editable(EditMode::MultiLine)
|
|
.text_align(Align::LEFT)
|
|
.size(30)
|
|
.attr::<Selectable>(())
|
|
.on(Submit, move |ctx, rsc| {
|
|
let w = ctx.widget;
|
|
let content = w.edit(rsc).take();
|
|
let text = wtext(content)
|
|
.editable(EditMode::MultiLine)
|
|
.size(30)
|
|
.text_align(Align::LEFT)
|
|
.wrap(true)
|
|
.attr::<Selectable>(());
|
|
let msg_box = text
|
|
.background(rect(Color::WHITE.darker(0.5)))
|
|
.add_strong(rsc);
|
|
texts(rsc).push(msg_box);
|
|
})
|
|
.add(rsc);
|
|
|
|
let text_edit_scroll = (
|
|
msg_area.height(rest(1)),
|
|
(
|
|
Rect::new(Color::WHITE.darker(0.9)),
|
|
(
|
|
add_text.width(rest(1)),
|
|
Rect::new(Color::GREEN)
|
|
.on(CursorSense::click(), move |ctx, rsc: &mut ClientRsc| {
|
|
rsc.run_event::<Submit>(add_text, (), ctx.state);
|
|
})
|
|
.sized((40, 40)),
|
|
)
|
|
.span(Dir::RIGHT)
|
|
.pad(10),
|
|
)
|
|
.stack()
|
|
.size(StackSize::Child(1))
|
|
.layer_offset(1)
|
|
.align(Align::BOT),
|
|
)
|
|
.span(Dir::DOWN)
|
|
.add(rsc);
|
|
|
|
let main = WidgetPtr::new().add(rsc);
|
|
|
|
let vals = Rc::new(RefCell::new((0, Vec::new())));
|
|
let mut switch_button = |color, to: WeakWidget, label| {
|
|
let to = to.upgrade(rsc);
|
|
let vec = &mut vals.borrow_mut().1;
|
|
let i = vec.len();
|
|
if vec.is_empty() {
|
|
vec.push(None);
|
|
main(rsc).set(to);
|
|
} else {
|
|
vec.push(Some(to));
|
|
}
|
|
let vals = vals.clone();
|
|
let rect = rect(color)
|
|
.on(CursorSense::click(), move |ctx, rsc| {
|
|
let (prev, vec) = &mut *vals.borrow_mut();
|
|
if let Some(h) = vec[i].take() {
|
|
vec[*prev] = main(rsc).replace(h);
|
|
*prev = i;
|
|
}
|
|
ctx.widget(rsc).color = color.darker(0.3);
|
|
})
|
|
.on(
|
|
CursorSense::HoverStart | CursorSense::unclick(),
|
|
move |ctx, rsc| {
|
|
ctx.widget(rsc).color = color.brighter(0.2);
|
|
},
|
|
)
|
|
.on(CursorSense::HoverEnd, move |ctx, rsc| {
|
|
ctx.widget(rsc).color = color;
|
|
});
|
|
(rect, wtext(label).size(30).text_align(Align::CENTER)).stack()
|
|
};
|
|
|
|
let tabs = (
|
|
switch_button(Color::RED, pad_test, "pad"),
|
|
switch_button(Color::GREEN, span_test, "span"),
|
|
switch_button(Color::BLUE, span_add_test, "image span"),
|
|
switch_button(Color::MAGENTA, text_test, "text layout"),
|
|
switch_button(
|
|
Color::YELLOW.mul_rgb(0.5),
|
|
text_edit_scroll,
|
|
"text edit scroll",
|
|
),
|
|
)
|
|
.span(Dir::RIGHT);
|
|
|
|
let info = wtext("").add(rsc);
|
|
let info_sect = info.pad(10).align(Align::RIGHT);
|
|
|
|
((tabs.height(40), main.pad(10)).span(Dir::DOWN), info_sect)
|
|
.stack()
|
|
.set_root(rsc, &mut ui_state);
|
|
|
|
Self { ui_state, info }
|
|
}
|
|
|
|
fn window_event(
|
|
&mut self,
|
|
_: WindowEvent,
|
|
rsc: &mut DefaultRsc<Self>,
|
|
render: &mut UiRenderState,
|
|
) {
|
|
let new = format!(
|
|
"widgets: {}\nactive: {}\ntextures: {}",
|
|
rsc.widgets().len(),
|
|
render.active_widgets(),
|
|
rsc.ui().textures.count(),
|
|
);
|
|
if new != *rsc.widgets()[self.info].content {
|
|
*rsc.widgets_mut()[self.info].content = new;
|
|
}
|
|
}
|
|
}
|