2022-08-12 05:29:15 +10:00
|
|
|
use piet_gpu::{samples, PicoSvg, RenderDriver, Renderer, SimpleText};
|
2022-07-15 04:46:46 +10:00
|
|
|
use piet_gpu_hal::{Error, ImageLayout, Instance, InstanceFlags, Session};
|
2022-08-12 05:29:15 +10:00
|
|
|
use piet_scene::{ResourceContext, Scene, SceneBuilder};
|
2021-06-26 12:42:33 +10:00
|
|
|
|
|
|
|
use clap::{App, Arg};
|
2020-05-05 01:05:54 +10:00
|
|
|
|
2022-10-18 12:12:41 +11:00
|
|
|
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
|
|
|
|
|
2020-05-05 01:05:54 +10:00
|
|
|
use winit::{
|
|
|
|
event::{Event, WindowEvent},
|
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
window::WindowBuilder,
|
|
|
|
};
|
|
|
|
|
|
|
|
const NUM_FRAMES: usize = 2;
|
|
|
|
|
2021-08-17 09:54:31 +10:00
|
|
|
const WIDTH: usize = 2048;
|
|
|
|
const HEIGHT: usize = 1536;
|
|
|
|
|
2020-05-05 01:05:54 +10:00
|
|
|
fn main() -> Result<(), Error> {
|
2021-06-26 12:42:33 +10:00
|
|
|
let matches = App::new("piet-gpu test")
|
|
|
|
.arg(Arg::with_name("INPUT").index(1))
|
2022-09-22 10:20:34 +10:00
|
|
|
.arg(Arg::with_name("flip").short('f').long("flip"))
|
2021-06-26 12:42:33 +10:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("scale")
|
2022-09-22 10:20:34 +10:00
|
|
|
.short('s')
|
2021-06-26 12:42:33 +10:00
|
|
|
.long("scale")
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
2022-04-05 06:09:24 +10:00
|
|
|
// Collect SVG if input
|
|
|
|
let svg = match matches.value_of("INPUT") {
|
|
|
|
Some(file) => {
|
|
|
|
let mut scale = matches
|
|
|
|
.value_of("scale")
|
|
|
|
.map(|scale| scale.parse().unwrap())
|
|
|
|
.unwrap_or(8.0);
|
|
|
|
if matches.is_present("flip") {
|
|
|
|
scale = -scale;
|
|
|
|
}
|
|
|
|
let xml_str = std::fs::read_to_string(file).unwrap();
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
let svg = PicoSvg::load(&xml_str, scale).unwrap();
|
|
|
|
println!("parsing time: {:?}", start.elapsed());
|
|
|
|
Some(svg)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2020-05-05 01:05:54 +10:00
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
let window = WindowBuilder::new()
|
|
|
|
.with_inner_size(winit::dpi::LogicalSize {
|
|
|
|
width: (WIDTH / 2) as f64,
|
|
|
|
height: (HEIGHT / 2) as f64,
|
|
|
|
})
|
|
|
|
.with_resizable(false) // currently not supported
|
|
|
|
.build(&event_loop)?;
|
|
|
|
|
2022-07-16 03:14:12 +10:00
|
|
|
let instance = Instance::new(InstanceFlags::default())?;
|
2021-10-19 09:52:57 +11:00
|
|
|
let mut info_string = "info".to_string();
|
2022-08-12 05:29:15 +10:00
|
|
|
let mut scene = Scene::default();
|
|
|
|
let mut rcx = ResourceContext::default();
|
|
|
|
let mut simple_text = piet_gpu::SimpleText::new();
|
2020-05-05 01:05:54 +10:00
|
|
|
unsafe {
|
2022-10-18 12:12:41 +11:00
|
|
|
let display_handle = window.raw_display_handle();
|
|
|
|
let window_handle = window.raw_window_handle();
|
|
|
|
let surface = instance.surface(display_handle, window_handle)?;
|
2022-07-15 04:46:46 +10:00
|
|
|
let device = instance.device()?;
|
|
|
|
let mut swapchain = instance.swapchain(WIDTH / 2, HEIGHT / 2, &device, &surface)?;
|
2021-05-30 09:33:52 +10:00
|
|
|
let session = Session::new(device);
|
2020-05-05 01:05:54 +10:00
|
|
|
|
|
|
|
let mut current_frame = 0;
|
|
|
|
let present_semaphores = (0..NUM_FRAMES)
|
2020-11-18 03:04:25 +11:00
|
|
|
.map(|_| session.create_semaphore())
|
2020-05-05 01:05:54 +10:00
|
|
|
.collect::<Result<Vec<_>, Error>>()?;
|
|
|
|
|
2022-07-14 07:40:41 +10:00
|
|
|
let renderer = Renderer::new(&session, WIDTH, HEIGHT, NUM_FRAMES)?;
|
|
|
|
let mut render_driver = RenderDriver::new(&session, NUM_FRAMES, renderer);
|
2022-08-12 05:29:15 +10:00
|
|
|
let mut sample_index = 0usize;
|
2020-05-05 01:05:54 +10:00
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
*control_flow = ControlFlow::Poll; // `ControlFlow::Wait` if only re-render on event
|
|
|
|
|
|
|
|
match event {
|
|
|
|
Event::WindowEvent { event, window_id } if window_id == window.id() => {
|
2022-03-01 04:38:14 +11:00
|
|
|
use winit::event::{ElementState, VirtualKeyCode};
|
2020-05-05 01:05:54 +10:00
|
|
|
match event {
|
|
|
|
WindowEvent::CloseRequested => {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
}
|
2022-03-01 04:38:14 +11:00
|
|
|
WindowEvent::KeyboardInput { input, .. } => {
|
|
|
|
if input.state == ElementState::Pressed {
|
|
|
|
match input.virtual_keycode {
|
2022-08-12 05:29:15 +10:00
|
|
|
Some(VirtualKeyCode::Left) => {
|
|
|
|
sample_index = sample_index.saturating_sub(1)
|
|
|
|
}
|
|
|
|
Some(VirtualKeyCode::Right) => {
|
|
|
|
sample_index = sample_index.saturating_add(1)
|
|
|
|
}
|
2022-03-01 04:38:14 +11:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 01:05:54 +10:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::MainEventsCleared => {
|
|
|
|
window.request_redraw();
|
|
|
|
}
|
|
|
|
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
|
|
|
let frame_idx = current_frame % NUM_FRAMES;
|
|
|
|
|
2022-07-14 07:40:41 +10:00
|
|
|
if current_frame >= NUM_FRAMES {
|
|
|
|
let stats = render_driver.get_timing_stats(&session, frame_idx);
|
|
|
|
info_string = stats.short_summary();
|
2021-10-19 09:52:57 +11:00
|
|
|
}
|
|
|
|
|
2022-04-05 06:09:24 +10:00
|
|
|
if let Some(svg) = &svg {
|
2022-08-12 05:29:15 +10:00
|
|
|
rcx.advance();
|
|
|
|
let mut builder = SceneBuilder::for_scene(&mut scene, &mut rcx);
|
|
|
|
samples::render_svg(&mut builder, svg, false);
|
|
|
|
render_info(&mut simple_text, &mut builder, &info_string);
|
|
|
|
builder.finish();
|
|
|
|
if let Err(e) = render_driver.upload_scene(&session, &scene, &rcx) {
|
|
|
|
println!("error in uploading: {}", e);
|
|
|
|
}
|
2022-04-21 18:20:54 +10:00
|
|
|
} else {
|
2022-08-12 05:29:15 +10:00
|
|
|
rcx.advance();
|
|
|
|
let mut builder = SceneBuilder::for_scene(&mut scene, &mut rcx);
|
|
|
|
|
|
|
|
const N_SAMPLES: usize = 4;
|
|
|
|
match sample_index % N_SAMPLES {
|
|
|
|
0 => samples::render_anim_frame(
|
|
|
|
&mut builder,
|
|
|
|
&mut simple_text,
|
|
|
|
current_frame,
|
|
|
|
),
|
|
|
|
1 => samples::render_blend_grid(&mut builder),
|
|
|
|
2 => samples::render_tiger(&mut builder, false),
|
|
|
|
_ => samples::render_scene(&mut builder),
|
|
|
|
}
|
|
|
|
render_info(&mut simple_text, &mut builder, &info_string);
|
|
|
|
builder.finish();
|
|
|
|
if let Err(e) = render_driver.upload_scene(&session, &scene, &rcx) {
|
|
|
|
println!("error in uploading: {}", e);
|
|
|
|
}
|
2020-05-05 01:05:54 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
let (image_idx, acquisition_semaphore) = swapchain.next().unwrap();
|
|
|
|
let swap_image = swapchain.image(image_idx);
|
2022-07-14 07:40:41 +10:00
|
|
|
render_driver.run_coarse(&session).unwrap();
|
|
|
|
let target = render_driver.record_fine(&session).unwrap();
|
|
|
|
let cmd_buf = target.cmd_buf;
|
2020-05-05 01:05:54 +10:00
|
|
|
|
|
|
|
// Image -> Swapchain
|
|
|
|
cmd_buf.image_barrier(
|
|
|
|
&swap_image,
|
|
|
|
ImageLayout::Undefined,
|
|
|
|
ImageLayout::BlitDst,
|
|
|
|
);
|
2022-07-14 07:40:41 +10:00
|
|
|
cmd_buf.blit_image(target.image, &swap_image);
|
2020-05-12 13:01:06 +10:00
|
|
|
cmd_buf.image_barrier(&swap_image, ImageLayout::BlitDst, ImageLayout::Present);
|
2022-07-14 07:40:41 +10:00
|
|
|
render_driver
|
|
|
|
.submit(
|
|
|
|
&session,
|
2021-05-26 01:25:24 +10:00
|
|
|
&[&acquisition_semaphore],
|
|
|
|
&[&present_semaphores[frame_idx]],
|
2020-05-05 01:05:54 +10:00
|
|
|
)
|
2022-07-14 07:40:41 +10:00
|
|
|
.unwrap();
|
2020-05-05 01:05:54 +10:00
|
|
|
|
|
|
|
swapchain
|
2021-05-26 11:06:51 +10:00
|
|
|
.present(image_idx, &[&present_semaphores[frame_idx]])
|
2020-05-05 01:05:54 +10:00
|
|
|
.unwrap();
|
|
|
|
|
2022-07-14 07:40:41 +10:00
|
|
|
render_driver.next_buffer();
|
2020-05-05 01:05:54 +10:00
|
|
|
current_frame += 1;
|
|
|
|
}
|
2021-10-20 11:25:08 +11:00
|
|
|
Event::LoopDestroyed => {
|
2022-07-14 07:40:41 +10:00
|
|
|
render_driver.wait_all(&session);
|
2021-10-20 11:25:08 +11:00
|
|
|
}
|
2020-05-05 01:05:54 +10:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-10-19 09:52:57 +11:00
|
|
|
|
2022-08-12 05:29:15 +10:00
|
|
|
fn render_info(simple_text: &mut SimpleText, sb: &mut SceneBuilder, info: &str) {
|
|
|
|
simple_text.add(
|
|
|
|
sb,
|
|
|
|
None,
|
|
|
|
40.0,
|
|
|
|
None,
|
|
|
|
piet_scene::Affine::translate(110.0, 50.0),
|
|
|
|
info,
|
|
|
|
);
|
2021-10-19 09:52:57 +11:00
|
|
|
}
|