mirror of
https://github.com/italicsjenga/vello.git
synced 2025-01-10 12:41:30 +11:00
Make canvas size dynamic
Instead of hard-coding the canvas size, pass it in on renderer creation. It's still fixed on desktop, but on Android it gets the size from the window.
This commit is contained in:
parent
698a1546d2
commit
660d7b8e91
|
@ -39,8 +39,6 @@ struct GfxState {
|
||||||
present_semaphores: Vec<Semaphore>,
|
present_semaphores: Vec<Semaphore>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const WIDTH: usize = 1080;
|
|
||||||
const HEIGHT: usize = 2280;
|
|
||||||
const NUM_FRAMES: usize = 2;
|
const NUM_FRAMES: usize = 2;
|
||||||
|
|
||||||
fn my_main() -> Result<(), Error> {
|
fn my_main() -> Result<(), Error> {
|
||||||
|
@ -52,9 +50,12 @@ fn my_main() -> Result<(), Error> {
|
||||||
Event::WindowCreated => {
|
Event::WindowCreated => {
|
||||||
let window = ndk_glue::native_window();
|
let window = ndk_glue::native_window();
|
||||||
if let Some(window) = &*window {
|
if let Some(window) = &*window {
|
||||||
|
let width = window.width() as usize;
|
||||||
|
let height = window.height() as usize;
|
||||||
let handle = get_handle(window);
|
let handle = get_handle(window);
|
||||||
let (instance, surface) = Instance::new(Some(&handle))?;
|
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 {
|
} else {
|
||||||
println!("native window is sadly none");
|
println!("native window is sadly none");
|
||||||
}
|
}
|
||||||
|
@ -91,11 +92,16 @@ unsafe impl HasRawWindowHandle for MyHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GfxState {
|
impl GfxState {
|
||||||
fn new(instance: &Instance, surface: Option<&Surface>) -> Result<GfxState, Error> {
|
fn new(
|
||||||
|
instance: &Instance,
|
||||||
|
surface: Option<&Surface>,
|
||||||
|
width: usize,
|
||||||
|
height: usize,
|
||||||
|
) -> Result<GfxState, Error> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let device = instance.device(surface)?;
|
let device = instance.device(surface)?;
|
||||||
let mut swapchain =
|
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 session = Session::new(device);
|
||||||
let mut current_frame = 0;
|
let mut current_frame = 0;
|
||||||
let present_semaphores = (0..NUM_FRAMES)
|
let present_semaphores = (0..NUM_FRAMES)
|
||||||
|
@ -108,7 +114,7 @@ impl GfxState {
|
||||||
let mut ctx = PietGpuRenderContext::new();
|
let mut ctx = PietGpuRenderContext::new();
|
||||||
render_scene(&mut ctx);
|
render_scene(&mut ctx);
|
||||||
|
|
||||||
let mut renderer = Renderer::new(&session)?;
|
let mut renderer = Renderer::new(&session, width, height)?;
|
||||||
renderer.upload_render_ctx(&mut ctx)?;
|
renderer.upload_render_ctx(&mut ctx)?;
|
||||||
|
|
||||||
let submitted: Option<SubmittedCmdBuf> = None;
|
let submitted: Option<SubmittedCmdBuf> = None;
|
||||||
|
|
|
@ -6,7 +6,10 @@ use clap::{App, Arg};
|
||||||
|
|
||||||
use piet_gpu_hal::{BufferUsage, Error, Instance, Session};
|
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)]
|
#[allow(unused)]
|
||||||
fn dump_scene(buf: &[u8]) {
|
fn dump_scene(buf: &[u8]) {
|
||||||
|
@ -245,7 +248,7 @@ fn main() -> Result<(), Error> {
|
||||||
render_scene(&mut ctx);
|
render_scene(&mut ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut renderer = Renderer::new(&session)?;
|
let mut renderer = Renderer::new(&session, WIDTH, HEIGHT)?;
|
||||||
renderer.upload_render_ctx(&mut ctx)?;
|
renderer.upload_render_ctx(&mut ctx)?;
|
||||||
let image_usage = BufferUsage::MAP_READ | BufferUsage::COPY_DST;
|
let image_usage = BufferUsage::MAP_READ | BufferUsage::COPY_DST;
|
||||||
let image_buf = session.create_buffer((WIDTH * HEIGHT * 4) as u64, image_usage)?;
|
let image_buf = session.create_buffer((WIDTH * HEIGHT * 4) as u64, image_usage)?;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use piet_gpu_hal::{Error, ImageLayout, Instance, Session, SubmittedCmdBuf};
|
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};
|
use clap::{App, Arg};
|
||||||
|
|
||||||
|
@ -12,6 +12,9 @@ use winit::{
|
||||||
|
|
||||||
const NUM_FRAMES: usize = 2;
|
const NUM_FRAMES: usize = 2;
|
||||||
|
|
||||||
|
const WIDTH: usize = 2048;
|
||||||
|
const HEIGHT: usize = 1536;
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
let matches = App::new("piet-gpu test")
|
let matches = App::new("piet-gpu test")
|
||||||
.arg(Arg::with_name("INPUT").index(1))
|
.arg(Arg::with_name("INPUT").index(1))
|
||||||
|
@ -62,7 +65,7 @@ fn main() -> Result<(), Error> {
|
||||||
render_scene(&mut ctx);
|
render_scene(&mut ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut renderer = Renderer::new(&session)?;
|
let mut renderer = Renderer::new(&session, WIDTH, HEIGHT)?;
|
||||||
renderer.upload_render_ctx(&mut ctx)?;
|
renderer.upload_render_ctx(&mut ctx)?;
|
||||||
|
|
||||||
let mut submitted: Option<SubmittedCmdBuf> = None;
|
let mut submitted: Option<SubmittedCmdBuf> = None;
|
||||||
|
|
|
@ -24,14 +24,9 @@ use piet_gpu_hal::{
|
||||||
|
|
||||||
use pico_svg::PicoSvg;
|
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_W: usize = 16;
|
||||||
const TILE_H: 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 PTCL_INITIAL_ALLOC: usize = 1024;
|
||||||
|
|
||||||
const MAX_BLEND_STACK: usize = 128;
|
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) {
|
pub fn render_scene(rc: &mut impl RenderContext) {
|
||||||
|
const WIDTH: usize = 2048;
|
||||||
|
const HEIGHT: usize = 1536;
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
for _ in 0..N_CIRCLES {
|
for _ in 0..N_CIRCLES {
|
||||||
let color = Color::from_rgba32_u32(rng.next_u32());
|
let color = Color::from_rgba32_u32(rng.next_u32());
|
||||||
|
@ -235,6 +232,11 @@ pub fn dump_k1_data(k1_buf: &[u32]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Renderer {
|
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
|
pub image_dev: Image, // resulting image
|
||||||
|
|
||||||
// The reference is held by the pipelines. We will be changing
|
// The reference is held by the pipelines. We will be changing
|
||||||
|
@ -284,7 +286,10 @@ pub struct Renderer {
|
||||||
|
|
||||||
impl Renderer {
|
impl Renderer {
|
||||||
/// Create a new renderer.
|
/// Create a new renderer.
|
||||||
pub unsafe fn new(session: &Session) -> Result<Self, Error> {
|
pub unsafe fn new(session: &Session, width: usize, height: usize) -> Result<Self, Error> {
|
||||||
|
// 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 dev = BufferUsage::STORAGE | BufferUsage::COPY_DST;
|
||||||
let host_upload = BufferUsage::MAP_WRITE | BufferUsage::COPY_SRC;
|
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 scene_buf = session.create_buffer(1 * 1024 * 1024, host_upload).unwrap();
|
||||||
|
|
||||||
let state_buf = session.create_buffer(1 * 1024 * 1024, dev)?;
|
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.
|
// Note: this must be updated when the config struct size changes.
|
||||||
const CONFIG_BUFFER_SIZE: u64 = 40;
|
const CONFIG_BUFFER_SIZE: u64 = 40;
|
||||||
|
@ -372,6 +377,8 @@ impl Renderer {
|
||||||
.build(&session, &k4_pipeline)?;
|
.build(&session, &k4_pipeline)?;
|
||||||
|
|
||||||
Ok(Renderer {
|
Ok(Renderer {
|
||||||
|
width,
|
||||||
|
height,
|
||||||
scene_buf,
|
scene_buf,
|
||||||
memory_buf_host,
|
memory_buf_host,
|
||||||
memory_buf_dev,
|
memory_buf_dev,
|
||||||
|
@ -423,13 +430,15 @@ impl Renderer {
|
||||||
const PATHSEG_SIZE: usize = 52;
|
const PATHSEG_SIZE: usize = 52;
|
||||||
const ANNO_SIZE: usize = 40;
|
const ANNO_SIZE: usize = 40;
|
||||||
const TRANS_SIZE: usize = 24;
|
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 mut alloc = 0;
|
||||||
let tile_base = alloc;
|
let tile_base = alloc;
|
||||||
alloc += ((n_paths + 3) & !3) * PATH_SIZE;
|
alloc += ((n_paths + 3) & !3) * PATH_SIZE;
|
||||||
let bin_base = alloc;
|
let bin_base = alloc;
|
||||||
alloc += ((n_paths + 255) & !255) * BIN_SIZE;
|
alloc += ((n_paths + 255) & !255) * BIN_SIZE;
|
||||||
let ptcl_base = alloc;
|
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;
|
let pathseg_base = alloc;
|
||||||
alloc += (n_pathseg * PATHSEG_SIZE + 3) & !3;
|
alloc += (n_pathseg * PATHSEG_SIZE + 3) & !3;
|
||||||
let anno_base = alloc;
|
let anno_base = alloc;
|
||||||
|
@ -439,8 +448,8 @@ impl Renderer {
|
||||||
let config = &[
|
let config = &[
|
||||||
n_paths as u32,
|
n_paths as u32,
|
||||||
n_pathseg as u32,
|
n_pathseg as u32,
|
||||||
WIDTH_IN_TILES as u32,
|
width_in_tiles as u32,
|
||||||
HEIGHT_IN_TILES as u32,
|
height_in_tiles as u32,
|
||||||
tile_base as u32,
|
tile_base as u32,
|
||||||
bin_base as u32,
|
bin_base as u32,
|
||||||
ptcl_base as u32,
|
ptcl_base as u32,
|
||||||
|
@ -533,7 +542,11 @@ impl Renderer {
|
||||||
cmd_buf.dispatch(
|
cmd_buf.dispatch(
|
||||||
&self.coarse_pipeline,
|
&self.coarse_pipeline,
|
||||||
&self.coarse_ds,
|
&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),
|
(256, 256, 1),
|
||||||
);
|
);
|
||||||
cmd_buf.write_timestamp(&query_pool, 6);
|
cmd_buf.write_timestamp(&query_pool, 6);
|
||||||
|
@ -541,7 +554,11 @@ impl Renderer {
|
||||||
cmd_buf.dispatch(
|
cmd_buf.dispatch(
|
||||||
&self.k4_pipeline,
|
&self.k4_pipeline,
|
||||||
&self.k4_ds,
|
&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),
|
(8, 4, 1),
|
||||||
);
|
);
|
||||||
cmd_buf.write_timestamp(&query_pool, 7);
|
cmd_buf.write_timestamp(&query_pool, 7);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::{borrow::Cow, ops::RangeBounds};
|
use std::{borrow::Cow, ops::RangeBounds};
|
||||||
|
|
||||||
|
use crate::MAX_BLEND_STACK;
|
||||||
use piet::{
|
use piet::{
|
||||||
kurbo::{Affine, Insets, PathEl, Point, Rect, Shape, Size},
|
kurbo::{Affine, Insets, PathEl, Point, Rect, Shape, Size},
|
||||||
HitTestPosition, TextAttribute, TextStorage,
|
HitTestPosition, TextAttribute, TextStorage,
|
||||||
|
@ -8,7 +9,6 @@ use piet::{
|
||||||
Color, Error, FixedGradient, FontFamily, HitTestPoint, ImageFormat, InterpolationMode,
|
Color, Error, FixedGradient, FontFamily, HitTestPoint, ImageFormat, InterpolationMode,
|
||||||
IntoBrush, LineMetric, RenderContext, StrokeStyle, Text, TextLayout, TextLayoutBuilder,
|
IntoBrush, LineMetric, RenderContext, StrokeStyle, Text, TextLayout, TextLayoutBuilder,
|
||||||
};
|
};
|
||||||
use crate::MAX_BLEND_STACK;
|
|
||||||
|
|
||||||
use piet_gpu_types::encoder::{Encode, Encoder};
|
use piet_gpu_types::encoder::{Encode, Encoder};
|
||||||
use piet_gpu_types::scene::{
|
use piet_gpu_types::scene::{
|
||||||
|
|
Loading…
Reference in a new issue