Portability fixes

Makes the changes work on Windows and Android.
This commit is contained in:
Raph Levien 2022-07-13 17:20:58 -07:00
parent 61598d2da0
commit 1a20fe78f6
2 changed files with 27 additions and 40 deletions

View file

@ -20,7 +20,7 @@ use piet_gpu_hal::{
use piet::kurbo::Point; use piet::kurbo::Point;
use piet::{RenderContext, Text, TextAttribute, TextLayoutBuilder}; use piet::{RenderContext, Text, TextAttribute, TextLayoutBuilder};
use piet_gpu::{test_scenes, PietGpuRenderContext, Renderer}; use piet_gpu::{test_scenes, PietGpuRenderContext, RenderDriver, Renderer};
#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))] #[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
fn main() { fn main() {
@ -34,12 +34,9 @@ struct MyHandle {
// State required to render and present the contents // State required to render and present the contents
struct GfxState { struct GfxState {
session: Session, session: Session,
renderer: Renderer, render_driver: RenderDriver,
swapchain: Swapchain, swapchain: Swapchain,
current_frame: usize, current_frame: usize,
submitted: [Option<SubmittedCmdBuf>; NUM_FRAMES],
cmd_bufs: [Option<CmdBuf>; NUM_FRAMES],
query_pools: Vec<QueryPool>,
present_semaphores: Vec<Semaphore>, present_semaphores: Vec<Semaphore>,
} }
@ -110,22 +107,15 @@ impl GfxState {
let present_semaphores = (0..NUM_FRAMES) let present_semaphores = (0..NUM_FRAMES)
.map(|_| session.create_semaphore()) .map(|_| session.create_semaphore())
.collect::<Result<Vec<_>, Error>>()?; .collect::<Result<Vec<_>, Error>>()?;
let query_pools = (0..NUM_FRAMES)
.map(|_| session.create_query_pool(Renderer::QUERY_POOL_SIZE))
.collect::<Result<Vec<_>, Error>>()?;
let submitted = Default::default();
let cmd_bufs = Default::default();
let renderer = Renderer::new(&session, width, height, NUM_FRAMES)?; let renderer = Renderer::new(&session, width, height, NUM_FRAMES)?;
let render_driver = RenderDriver::new(&session, NUM_FRAMES, renderer);
Ok(GfxState { Ok(GfxState {
session, session,
renderer, render_driver,
swapchain, swapchain,
current_frame, current_frame,
submitted,
cmd_bufs,
query_pools,
present_semaphores, present_semaphores,
}) })
} }
@ -137,51 +127,47 @@ impl GfxState {
let frame_idx = self.current_frame % NUM_FRAMES; let frame_idx = self.current_frame % NUM_FRAMES;
let mut info_string = String::new(); let mut info_string = String::new();
if let Some(submitted) = self.submitted[frame_idx].take() { if self.current_frame >= NUM_FRAMES {
self.cmd_bufs[frame_idx] = submitted.wait().unwrap(); let stats = self
let ts = self .render_driver
.session .get_timing_stats(&self.session, frame_idx);
.fetch_query_pool(&self.query_pools[frame_idx]) info_string = stats.short_summary();
.unwrap(); println!("{}", info_string);
info_string = format!("{:.1}ms", ts.last().unwrap() * 1e3);
println!("render time: {:?}", ts);
} }
let mut ctx = PietGpuRenderContext::new(); let mut ctx = PietGpuRenderContext::new();
test_scenes::render_anim_frame(&mut ctx, self.current_frame); test_scenes::render_anim_frame(&mut ctx, self.current_frame);
//test_scenes::render_tiger(&mut ctx); //test_scenes::render_tiger(&mut ctx);
render_info_string(&mut ctx, &info_string); render_info_string(&mut ctx, &info_string);
if let Err(e) = self.renderer.upload_render_ctx(&mut ctx, frame_idx) { if let Err(e) = self
.render_driver
.upload_render_ctx(&self.session, &mut ctx)
{
println!("error in uploading: {}", e); println!("error in uploading: {}", e);
} }
let (image_idx, acquisition_semaphore) = self.swapchain.next().unwrap(); let (image_idx, acquisition_semaphore) = self.swapchain.next().unwrap();
let swap_image = self.swapchain.image(image_idx); let swap_image = self.swapchain.image(image_idx);
let query_pool = &self.query_pools[frame_idx]; self.render_driver.run_coarse(&self.session).unwrap();
let mut cmd_buf = self.cmd_bufs[frame_idx] let target = self.render_driver.record_fine(&self.session).unwrap();
.take() let cmd_buf = target.cmd_buf;
.unwrap_or_else(|| self.session.cmd_buf().unwrap());
cmd_buf.begin();
self.renderer.record(&mut cmd_buf, &query_pool, frame_idx);
// Image -> Swapchain // Image -> Swapchain
cmd_buf.image_barrier(&swap_image, ImageLayout::Undefined, ImageLayout::BlitDst); cmd_buf.image_barrier(&swap_image, ImageLayout::Undefined, ImageLayout::BlitDst);
cmd_buf.blit_image(&self.renderer.image_dev, &swap_image); cmd_buf.blit_image(target.image, &swap_image);
cmd_buf.image_barrier(&swap_image, ImageLayout::BlitDst, ImageLayout::Present); cmd_buf.image_barrier(&swap_image, ImageLayout::BlitDst, ImageLayout::Present);
cmd_buf.finish();
self.submitted[frame_idx] = Some( self.render_driver
self.session .submit(
.run_cmd_buf( &self.session,
cmd_buf,
&[&acquisition_semaphore], &[&acquisition_semaphore],
&[&self.present_semaphores[frame_idx]], &[&self.present_semaphores[frame_idx]],
) )
.unwrap(), .unwrap();
);
self.swapchain self.swapchain
.present(image_idx, &[&self.present_semaphores[frame_idx]]) .present(image_idx, &[&self.present_semaphores[frame_idx]])
.unwrap(); .unwrap();
self.render_driver.next_buffer();
self.current_frame += 1; self.current_frame += 1;
} }
} }

View file

@ -186,6 +186,7 @@ impl RenderDriver {
let frame = &mut self.frames[self.buf_ix]; let frame = &mut self.frames[self.buf_ix];
let cmd_buf = frame.cmd_buf.cmd_buf(session)?; let cmd_buf = frame.cmd_buf.cmd_buf(session)?;
unsafe { unsafe {
cmd_buf.begin();
self.renderer self.renderer
.record_fine(cmd_buf, &frame.fine_query_pool, 0); .record_fine(cmd_buf, &frame.fine_query_pool, 0);
} }