From 876a3ad58179a137c15d25c66bca2fee305e158b Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Tue, 29 Nov 2022 17:35:19 -0800 Subject: [PATCH] Combine info and bin_data buffers We need to reduce the number of buffer bindings to 8 so it can run on all WebGPU devices. The best candidate was to combine info and bin_data, which are written by two different stages (draw_leaf and binning, respectively), both read by coarse, and are unstructured, so the only real shader change needed is to add the offset for the binning data. I thought I was going to have to do a blit to make it fit, but fortunately that wasn't needed. Progress to #202 --- piet-wgsl/shader/binning.wgsl | 2 +- piet-wgsl/shader/coarse.wgsl | 17 +++++++---------- piet-wgsl/shader/shared/config.wgsl | 4 ++++ piet-wgsl/src/engine.rs | 2 +- piet-wgsl/src/render.rs | 15 ++++++++------- piet-wgsl/src/shaders.rs | 1 - piet-wgsl/src/util.rs | 1 - 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/piet-wgsl/shader/binning.wgsl b/piet-wgsl/shader/binning.wgsl index 69aa821..0aab723 100644 --- a/piet-wgsl/shader/binning.wgsl +++ b/piet-wgsl/shader/binning.wgsl @@ -146,7 +146,7 @@ fn main( let count_packed = sh_count[count_ix / 2u][bin_ix]; idx += (count_packed >> (16u * (count_ix & 1u))) & 0xffffu; } - let offset = sh_chunk_offset[bin_ix]; + let offset = config.bin_data_start + sh_chunk_offset[bin_ix]; bin_data[offset + idx] = element_ix; } x += 1; diff --git a/piet-wgsl/shader/coarse.wgsl b/piet-wgsl/shader/coarse.wgsl index f139175..f60753f 100644 --- a/piet-wgsl/shader/coarse.wgsl +++ b/piet-wgsl/shader/coarse.wgsl @@ -27,7 +27,7 @@ struct BinHeader { var bin_headers: array; @group(0) @binding(4) -var bin_data: array; +var info_bin_data: array; @group(0) @binding(5) var paths: array; @@ -36,12 +36,9 @@ var paths: array; var tiles: array; @group(0) @binding(7) -var info: array; - -@group(0) @binding(8) var bump: BumpAllocators; -@group(0) @binding(9) +@group(0) @binding(8) var ptcl: array; @@ -208,8 +205,8 @@ fn main( } } ix -= select(part_start_ix, sh_part_count[part_ix - 1u], part_ix > 0u); - let offset = sh_part_offsets[part_ix]; - sh_drawobj_ix[local_id.x] = bin_data[offset + ix]; + let offset = config.bin_data_start + sh_part_offsets[part_ix]; + sh_drawobj_ix[local_id.x] = info_bin_data[offset + ix]; } wr_ix = min(rd_ix + N_TILE, ready_ix); if wr_ix - rd_ix >= N_TILE || (wr_ix >= ready_ix && partition_ix >= n_partitions) { @@ -326,14 +323,14 @@ fn main( switch drawtag { // DRAWTAG_FILL_COLOR case 0x44u: { - let linewidth = bitcast(info[di]); + let linewidth = bitcast(info_bin_data[di]); write_path(tile, linewidth); let rgba_color = scene[dd]; write_color(CmdColor(rgba_color)); } // DRAWTAG_FILL_LIN_GRADIENT case 0x114u: { - let linewidth = bitcast(info[di]); + let linewidth = bitcast(info_bin_data[di]); write_path(tile, linewidth); let index = scene[dd]; let info_offset = di + 1u; @@ -341,7 +338,7 @@ fn main( } // DRAWTAG_FILL_RAD_GRADIENT case 0x2dcu: { - let linewidth = bitcast(info[di]); + let linewidth = bitcast(info_bin_data[di]); write_path(tile, linewidth); let index = scene[dd]; let info_offset = di + 1u; diff --git a/piet-wgsl/shader/shared/config.wgsl b/piet-wgsl/shader/shared/config.wgsl index 21d9b0e..54f94f6 100644 --- a/piet-wgsl/shader/shared/config.wgsl +++ b/piet-wgsl/shader/shared/config.wgsl @@ -11,6 +11,10 @@ struct Config { n_path: u32, n_clip: u32, + // To reduce the number of bindings, info and bin data are combined + // into one buffer. + bin_data_start: u32, + // offsets within scene buffer (in u32 units) // Note: this is a difference from piet-gpu, which is in bytes pathtag_base: u32, diff --git a/piet-wgsl/src/engine.rs b/piet-wgsl/src/engine.rs index 5c66303..b9d16b1 100644 --- a/piet-wgsl/src/engine.rs +++ b/piet-wgsl/src/engine.rs @@ -329,7 +329,7 @@ impl Engine { } Command::Clear(proxy, offset, size) => { let buffer = bind_map.get_or_create(*proxy, device)?; - encoder.clear_buffer(buffer, *offset, *size) + encoder.clear_buffer(buffer, *offset, *size); } } } diff --git a/piet-wgsl/src/render.rs b/piet-wgsl/src/render.rs index b7ef6aa..54a0bb6 100644 --- a/piet-wgsl/src/render.rs +++ b/piet-wgsl/src/render.rs @@ -33,6 +33,7 @@ struct Config { n_drawobj: u32, n_path: u32, n_clip: u32, + bin_data_start: u32, pathtag_base: u32, pathdata_base: u32, drawtag_base: u32, @@ -209,6 +210,7 @@ pub fn render_full( // TODO: calculate for real when we do rectangles let n_drawobj = n_path; let n_clip = data.n_clip; + let bin_data_start = n_drawobj * MAX_DRAWINFO_SIZE as u32; let new_width = next_multiple_of(width, 16); let new_height = next_multiple_of(height, 16); @@ -222,6 +224,7 @@ pub fn render_full( n_drawobj, n_path, n_clip, + bin_data_start, pathtag_base, pathdata_base, drawtag_base, @@ -278,7 +281,7 @@ pub fn render_full( [config_buf, scene_buf, draw_reduced_buf], ); let draw_monoid_buf = ResourceProxy::new_buf(n_drawobj as u64 * DRAWMONOID_SIZE); - let info_buf = ResourceProxy::new_buf(n_drawobj as u64 * MAX_DRAWINFO_SIZE); + let info_bin_data_buf = ResourceProxy::new_buf(1 << 20); let clip_inp_buf = ResourceProxy::new_buf(data.n_clip as u64 * CLIP_INP_SIZE); recording.dispatch( shaders.draw_leaf, @@ -289,7 +292,7 @@ pub fn render_full( draw_reduced_buf, path_bbox_buf, draw_monoid_buf, - info_buf, + info_bin_data_buf, clip_inp_buf, ], ); @@ -329,7 +332,6 @@ pub fn render_full( } let draw_bbox_buf = ResourceProxy::new_buf(n_path as u64 * DRAW_BBOX_SIZE); let bump_buf = BufProxy::new(BUMP_SIZE); - let bin_data_buf = ResourceProxy::new_buf(1 << 20); let width_in_bins = (config.width_in_tiles + 15) / 16; let height_in_bins = (config.height_in_tiles + 15) / 16; let n_bins = width_in_bins * height_in_bins; @@ -346,7 +348,7 @@ pub fn render_full( clip_bbox_buf, draw_bbox_buf, bump_buf, - bin_data_buf, + info_bin_data_buf, bin_header_buf, ], ); @@ -395,10 +397,9 @@ pub fn render_full( scene_buf, draw_monoid_buf, bin_header_buf, - bin_data_buf, + info_bin_data_buf, path_buf, tile_buf, - info_buf, bump_buf, ptcl_buf, ], @@ -414,7 +415,7 @@ pub fn render_full( ResourceProxy::Image(out_image), ptcl_buf, gradient_image, - info_buf, + info_bin_data_buf, ], ); (recording, ResourceProxy::Image(out_image)) diff --git a/piet-wgsl/src/shaders.rs b/piet-wgsl/src/shaders.rs index b893ab5..1df6b14 100644 --- a/piet-wgsl/src/shaders.rs +++ b/piet-wgsl/src/shaders.rs @@ -259,7 +259,6 @@ pub fn full_shaders(device: &Device, engine: &mut Engine) -> Result