diff --git a/piet-gpu/bin/android.rs b/piet-gpu/bin/android.rs index 0f3abf0..3a697d1 100644 --- a/piet-gpu/bin/android.rs +++ b/piet-gpu/bin/android.rs @@ -39,8 +39,6 @@ struct GfxState { present_semaphores: Vec, } -const WIDTH: usize = 1080; -const HEIGHT: usize = 2280; const NUM_FRAMES: usize = 2; fn my_main() -> Result<(), Error> { @@ -52,9 +50,12 @@ fn my_main() -> Result<(), Error> { Event::WindowCreated => { let window = ndk_glue::native_window(); if let Some(window) = &*window { + let width = window.width() as usize; + let height = window.height() as usize; let handle = get_handle(window); let (instance, surface) = Instance::new(Some(&handle))?; - gfx_state = Some(GfxState::new(&instance, surface.as_ref())?); + gfx_state = + Some(GfxState::new(&instance, surface.as_ref(), width, height)?); } else { println!("native window is sadly none"); } @@ -91,11 +92,16 @@ unsafe impl HasRawWindowHandle for MyHandle { } impl GfxState { - fn new(instance: &Instance, surface: Option<&Surface>) -> Result { + fn new( + instance: &Instance, + surface: Option<&Surface>, + width: usize, + height: usize, + ) -> Result { unsafe { let device = instance.device(surface)?; let mut swapchain = - instance.swapchain(WIDTH / 2, HEIGHT / 2, &device, surface.unwrap())?; + instance.swapchain(width, height, &device, surface.unwrap())?; let session = Session::new(device); let mut current_frame = 0; let present_semaphores = (0..NUM_FRAMES) @@ -108,7 +114,7 @@ impl GfxState { let mut ctx = PietGpuRenderContext::new(); render_scene(&mut ctx); - let mut renderer = Renderer::new(&session)?; + let mut renderer = Renderer::new(&session, width, height)?; renderer.upload_render_ctx(&mut ctx)?; let submitted: Option = None; diff --git a/piet-gpu/bin/cli.rs b/piet-gpu/bin/cli.rs index 32c8c7d..3165fe0 100644 --- a/piet-gpu/bin/cli.rs +++ b/piet-gpu/bin/cli.rs @@ -6,7 +6,10 @@ use clap::{App, Arg}; use piet_gpu_hal::{BufferUsage, Error, Instance, Session}; -use piet_gpu::{render_scene, render_svg, PietGpuRenderContext, Renderer, HEIGHT, WIDTH}; +use piet_gpu::{render_scene, render_svg, PietGpuRenderContext, Renderer}; + +const WIDTH: usize = 2048; +const HEIGHT: usize = 1536; #[allow(unused)] fn dump_scene(buf: &[u8]) { @@ -245,7 +248,7 @@ fn main() -> Result<(), Error> { render_scene(&mut ctx); } - let mut renderer = Renderer::new(&session)?; + let mut renderer = Renderer::new(&session, WIDTH, HEIGHT)?; renderer.upload_render_ctx(&mut ctx)?; let image_usage = BufferUsage::MAP_READ | BufferUsage::COPY_DST; let image_buf = session.create_buffer((WIDTH * HEIGHT * 4) as u64, image_usage)?; diff --git a/piet-gpu/bin/winit.rs b/piet-gpu/bin/winit.rs index 5771082..2da6fe7 100644 --- a/piet-gpu/bin/winit.rs +++ b/piet-gpu/bin/winit.rs @@ -1,6 +1,6 @@ use piet_gpu_hal::{Error, ImageLayout, Instance, Session, SubmittedCmdBuf}; -use piet_gpu::{render_scene, PietGpuRenderContext, Renderer, HEIGHT, WIDTH, render_svg}; +use piet_gpu::{render_scene, render_svg, PietGpuRenderContext, Renderer}; use clap::{App, Arg}; @@ -12,6 +12,9 @@ use winit::{ const NUM_FRAMES: usize = 2; +const WIDTH: usize = 2048; +const HEIGHT: usize = 1536; + fn main() -> Result<(), Error> { let matches = App::new("piet-gpu test") .arg(Arg::with_name("INPUT").index(1)) @@ -62,7 +65,7 @@ fn main() -> Result<(), Error> { render_scene(&mut ctx); } - let mut renderer = Renderer::new(&session)?; + let mut renderer = Renderer::new(&session, WIDTH, HEIGHT)?; renderer.upload_render_ctx(&mut ctx)?; let mut submitted: Option = None; diff --git a/piet-gpu/src/lib.rs b/piet-gpu/src/lib.rs index a8ccf32..096f84e 100644 --- a/piet-gpu/src/lib.rs +++ b/piet-gpu/src/lib.rs @@ -24,14 +24,9 @@ use piet_gpu_hal::{ use pico_svg::PicoSvg; -pub const WIDTH: usize = TILE_W * WIDTH_IN_TILES; -pub const HEIGHT: usize = TILE_H * HEIGHT_IN_TILES; - const TILE_W: usize = 16; const TILE_H: usize = 16; -const WIDTH_IN_TILES: usize = 128; -const HEIGHT_IN_TILES: usize = 96; const PTCL_INITIAL_ALLOC: usize = 1024; const MAX_BLEND_STACK: usize = 128; @@ -50,6 +45,8 @@ pub fn render_svg(rc: &mut impl RenderContext, filename: &str, scale: f64) { } pub fn render_scene(rc: &mut impl RenderContext) { + const WIDTH: usize = 2048; + const HEIGHT: usize = 1536; let mut rng = rand::thread_rng(); for _ in 0..N_CIRCLES { let color = Color::from_rgba32_u32(rng.next_u32()); @@ -235,6 +232,11 @@ pub fn dump_k1_data(k1_buf: &[u32]) { } pub struct Renderer { + // These sizes are aligned to tile boundaries, though at some point + // we'll want to have a good strategy for dealing with odd sizes. + width: usize, + height: usize, + pub image_dev: Image, // resulting image // The reference is held by the pipelines. We will be changing @@ -284,7 +286,10 @@ pub struct Renderer { impl Renderer { /// Create a new renderer. - pub unsafe fn new(session: &Session) -> Result { + pub unsafe fn new(session: &Session, width: usize, height: usize) -> Result { + // For now, round up to tile alignment + let width = width + (width.wrapping_neg() & (TILE_W - 1)); + let height = height + (height.wrapping_neg() & (TILE_W - 1)); let dev = BufferUsage::STORAGE | BufferUsage::COPY_DST; let host_upload = BufferUsage::MAP_WRITE | BufferUsage::COPY_SRC; @@ -293,7 +298,7 @@ impl Renderer { let scene_buf = session.create_buffer(1 * 1024 * 1024, host_upload).unwrap(); let state_buf = session.create_buffer(1 * 1024 * 1024, dev)?; - let image_dev = session.create_image2d(WIDTH as u32, HEIGHT as u32)?; + let image_dev = session.create_image2d(width as u32, height as u32)?; // Note: this must be updated when the config struct size changes. const CONFIG_BUFFER_SIZE: u64 = 40; @@ -372,6 +377,8 @@ impl Renderer { .build(&session, &k4_pipeline)?; Ok(Renderer { + width, + height, scene_buf, memory_buf_host, memory_buf_dev, @@ -423,13 +430,15 @@ impl Renderer { const PATHSEG_SIZE: usize = 52; const ANNO_SIZE: usize = 40; const TRANS_SIZE: usize = 24; + let width_in_tiles = self.width / TILE_W; + let height_in_tiles = self.height / TILE_H; let mut alloc = 0; let tile_base = alloc; alloc += ((n_paths + 3) & !3) * PATH_SIZE; let bin_base = alloc; alloc += ((n_paths + 255) & !255) * BIN_SIZE; let ptcl_base = alloc; - alloc += WIDTH_IN_TILES * HEIGHT_IN_TILES * PTCL_INITIAL_ALLOC; + alloc += width_in_tiles * height_in_tiles * PTCL_INITIAL_ALLOC; let pathseg_base = alloc; alloc += (n_pathseg * PATHSEG_SIZE + 3) & !3; let anno_base = alloc; @@ -439,8 +448,8 @@ impl Renderer { let config = &[ n_paths as u32, n_pathseg as u32, - WIDTH_IN_TILES as u32, - HEIGHT_IN_TILES as u32, + width_in_tiles as u32, + height_in_tiles as u32, tile_base as u32, bin_base as u32, ptcl_base as u32, @@ -533,7 +542,11 @@ impl Renderer { cmd_buf.dispatch( &self.coarse_pipeline, &self.coarse_ds, - ((WIDTH as u32 + 255) / 256, (HEIGHT as u32 + 255) / 256, 1), + ( + (self.width as u32 + 255) / 256, + (self.height as u32 + 255) / 256, + 1, + ), (256, 256, 1), ); cmd_buf.write_timestamp(&query_pool, 6); @@ -541,7 +554,11 @@ impl Renderer { cmd_buf.dispatch( &self.k4_pipeline, &self.k4_ds, - ((WIDTH / TILE_W) as u32, (HEIGHT / TILE_H) as u32, 1), + ( + (self.width / TILE_W) as u32, + (self.height / TILE_H) as u32, + 1, + ), (8, 4, 1), ); cmd_buf.write_timestamp(&query_pool, 7); diff --git a/piet-gpu/src/render_ctx.rs b/piet-gpu/src/render_ctx.rs index 23fe3e3..43762fa 100644 --- a/piet-gpu/src/render_ctx.rs +++ b/piet-gpu/src/render_ctx.rs @@ -1,5 +1,6 @@ use std::{borrow::Cow, ops::RangeBounds}; +use crate::MAX_BLEND_STACK; use piet::{ kurbo::{Affine, Insets, PathEl, Point, Rect, Shape, Size}, HitTestPosition, TextAttribute, TextStorage, @@ -8,7 +9,6 @@ use piet::{ Color, Error, FixedGradient, FontFamily, HitTestPoint, ImageFormat, InterpolationMode, IntoBrush, LineMetric, RenderContext, StrokeStyle, Text, TextLayout, TextLayoutBuilder, }; -use crate::MAX_BLEND_STACK; use piet_gpu_types::encoder::{Encode, Encoder}; use piet_gpu_types::scene::{