Reuse wgpu device in all benchmarks

This commit is contained in:
Héctor Ramón Jiménez 2025-03-09 01:28:35 +01:00
parent f369bc86e2
commit 86e8494bfa
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -8,44 +8,15 @@ use iced::{
Color, Element, Font, Length, Pixels, Point, Rectangle, Size, Theme,
};
use iced_wgpu::Renderer;
use iced_wgpu::wgpu;
criterion_main!(benches);
criterion_group!(benches, wgpu_benchmark);
#[allow(unused_results)]
pub fn wgpu_benchmark(c: &mut Criterion) {
c.bench_function("wgpu — canvas (light)", |b| {
benchmark(b, |_| scene(10));
});
c.bench_function("wgpu — canvas (heavy)", |b| {
benchmark(b, |_| scene(1_000));
});
c.bench_function("wgpu - layered text (light)", |b| {
benchmark(b, |_| layered_text(10));
});
c.bench_function("wgpu - layered text (heavy)", |b| {
benchmark(b, |_| layered_text(1_000));
});
c.bench_function("wgpu - dynamic text (light)", |b| {
benchmark(b, |i| dynamic_text(1_000, i));
});
c.bench_function("wgpu - dynamic text (heavy)", |b| {
benchmark(b, |i| dynamic_text(100_000, i));
});
}
fn benchmark<'a>(
bencher: &mut Bencher<'_>,
view: impl Fn(usize) -> Element<'a, (), Theme, Renderer>,
) {
use iced_futures::futures::executor;
use iced_wgpu::graphics;
use iced_wgpu::graphics::Antialiasing;
use iced_wgpu::wgpu;
use iced_winit::core;
use iced_winit::runtime;
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::all(),
@ -72,18 +43,53 @@ fn benchmark<'a>(
))
.expect("request device");
c.bench_function("wgpu — canvas (light)", |b| {
benchmark(b, &adapter, &device, &queue, |_| scene(10));
});
c.bench_function("wgpu — canvas (heavy)", |b| {
benchmark(b, &adapter, &device, &queue, |_| scene(1_000));
});
c.bench_function("wgpu - layered text (light)", |b| {
benchmark(b, &adapter, &device, &queue, |_| layered_text(10));
});
c.bench_function("wgpu - layered text (heavy)", |b| {
benchmark(b, &adapter, &device, &queue, |_| layered_text(1_000));
});
c.bench_function("wgpu - dynamic text (light)", |b| {
benchmark(b, &adapter, &device, &queue, |i| dynamic_text(1_000, i));
});
c.bench_function("wgpu - dynamic text (heavy)", |b| {
benchmark(b, &adapter, &device, &queue, |i| dynamic_text(100_000, i));
});
}
fn benchmark<'a>(
bencher: &mut Bencher<'_>,
adapter: &wgpu::Adapter,
device: &wgpu::Device,
queue: &wgpu::Queue,
view: impl Fn(usize) -> Element<'a, (), Theme, Renderer>,
) {
use iced_wgpu::graphics;
use iced_wgpu::graphics::Antialiasing;
use iced_wgpu::wgpu;
use iced_winit::core;
use iced_winit::runtime;
let format = wgpu::TextureFormat::Bgra8UnormSrgb;
let mut engine = iced_wgpu::Engine::new(
&adapter,
&device,
&queue,
adapter,
device,
queue,
format,
Some(Antialiasing::MSAAx4),
);
let mut renderer =
Renderer::new(&device, &engine, Font::DEFAULT, Pixels::from(16));
Renderer::new(device, &engine, Font::DEFAULT, Pixels::from(16));
let viewport =
graphics::Viewport::with_physical_size(Size::new(3840, 2160), 2.0);
@ -135,8 +141,8 @@ fn benchmark<'a>(
renderer.present::<&str>(
&mut engine,
&device,
&queue,
device,
queue,
&mut encoder,
Some(Color::BLACK),
format,
@ -145,7 +151,7 @@ fn benchmark<'a>(
&[],
);
let submission = engine.submit(&queue, encoder);
let submission = engine.submit(queue, encoder);
let _ = device.poll(wgpu::Maintain::WaitForSubmissionIndex(submission));
i += 1;