From c8ed14ae67b69fd30a3cf25b7ad0d39693d9d1f7 Mon Sep 17 00:00:00 2001 From: Chad Brokaw Date: Mon, 28 Nov 2022 18:20:29 -0500 Subject: [PATCH 1/2] read gradient data from info buf --- piet-wgsl/shader/coarse.wgsl | 55 ++++++++---------------------------- piet-wgsl/shader/fine.wgsl | 33 +++++++++++++--------- piet-wgsl/src/render.rs | 1 + piet-wgsl/src/shaders.rs | 1 + 4 files changed, 33 insertions(+), 57 deletions(-) diff --git a/piet-wgsl/shader/coarse.wgsl b/piet-wgsl/shader/coarse.wgsl index 7ef1f8a..b912ed2 100644 --- a/piet-wgsl/shader/coarse.wgsl +++ b/piet-wgsl/shader/coarse.wgsl @@ -117,31 +117,12 @@ fn write_color(color: CmdColor) { // could just write an info offset and have fine bind that buffer and read // from it. -fn write_lin_grad(lin: CmdLinGrad) { - alloc_cmd(5u); - ptcl[cmd_offset] = CMD_LIN_GRAD; - ptcl[cmd_offset + 1u] = lin.index; - ptcl[cmd_offset + 2u] = bitcast(lin.line_x); - ptcl[cmd_offset + 3u] = bitcast(lin.line_y); - ptcl[cmd_offset + 4u] = bitcast(lin.line_c); - cmd_offset += 5u; -} - -fn write_rad_grad(rad: CmdRadGrad) { - alloc_cmd(12u); - ptcl[cmd_offset] = CMD_RAD_GRAD; - ptcl[cmd_offset + 1u] = rad.index; - ptcl[cmd_offset + 2u] = bitcast(rad.matrx.x); - ptcl[cmd_offset + 3u] = bitcast(rad.matrx.y); - ptcl[cmd_offset + 4u] = bitcast(rad.matrx.z); - ptcl[cmd_offset + 5u] = bitcast(rad.matrx.w); - ptcl[cmd_offset + 6u] = bitcast(rad.xlat.x); - ptcl[cmd_offset + 7u] = bitcast(rad.xlat.y); - ptcl[cmd_offset + 8u] = bitcast(rad.c1.x); - ptcl[cmd_offset + 9u] = bitcast(rad.c1.y); - ptcl[cmd_offset + 10u] = bitcast(rad.ra); - ptcl[cmd_offset + 11u] = bitcast(rad.roff); - cmd_offset += 12u; +fn write_grad(ty: u32, index: u32, info_offset: u32) { + alloc_cmd(3u); + ptcl[cmd_offset] = ty; + ptcl[cmd_offset + 1u] = index; + ptcl[cmd_offset + 2u] = info_offset; + cmd_offset += 3u; } fn write_begin_clip() { @@ -358,29 +339,17 @@ fn main( case 0x114u: { let linewidth = bitcast(info[di]); write_path(tile, linewidth); - var lin: CmdLinGrad; - lin.index = scene[dd]; - lin.line_x = bitcast(info[di + 1u]); - lin.line_y = bitcast(info[di + 2u]); - lin.line_c = bitcast(info[di + 3u]); - write_lin_grad(lin); + let index = scene[dd]; + let info_offset = di + 1u; + write_grad(CMD_LIN_GRAD, index, info_offset); } // DRAWTAG_FILL_RAD_GRADIENT case 0x2dcu: { let linewidth = bitcast(info[di]); write_path(tile, linewidth); - var rad: CmdRadGrad; - rad.index = scene[dd]; - let m0 = bitcast(info[di + 1u]); - let m1 = bitcast(info[di + 2u]); - let m2 = bitcast(info[di + 3u]); - let m3 = bitcast(info[di + 4u]); - rad.matrx = vec4(m0, m1, m2, m3); - rad.xlat = vec2(bitcast(info[di + 5u]), bitcast(info[di + 6u])); - rad.c1 = vec2(bitcast(info[di + 7u]), bitcast(info[di + 8u])); - rad.ra = bitcast(info[di + 9u]); - rad.roff = bitcast(info[di + 10u]); - write_rad_grad(rad); + let index = scene[dd]; + let info_offset = di + 1u; + write_grad(CMD_RAD_GRAD, index, info_offset); } // DRAWTAG_BEGIN_CLIP case 0x05u: { diff --git a/piet-wgsl/shader/fine.wgsl b/piet-wgsl/shader/fine.wgsl index 74a783e..95d8d59 100644 --- a/piet-wgsl/shader/fine.wgsl +++ b/piet-wgsl/shader/fine.wgsl @@ -38,6 +38,9 @@ var ptcl: array; @group(0) @binding(5) var gradients: texture_2d; +@group(0) @binding(6) +var info: array; + fn read_fill(cmd_ix: u32) -> CmdFill { let tile = ptcl[cmd_ix + 1u]; let backdrop = i32(ptcl[cmd_ix + 2u]); @@ -57,23 +60,25 @@ fn read_color(cmd_ix: u32) -> CmdColor { fn read_lin_grad(cmd_ix: u32) -> CmdLinGrad { let index = ptcl[cmd_ix + 1u]; - let line_x = bitcast(ptcl[cmd_ix + 2u]); - let line_y = bitcast(ptcl[cmd_ix + 3u]); - let line_c = bitcast(ptcl[cmd_ix + 4u]); + let info_offset = ptcl[cmd_ix + 2u]; + let line_x = bitcast(info[info_offset]); + let line_y = bitcast(info[info_offset + 1u]); + let line_c = bitcast(info[info_offset + 2u]); return CmdLinGrad(index, line_x, line_y, line_c); } fn read_rad_grad(cmd_ix: u32) -> CmdRadGrad { let index = ptcl[cmd_ix + 1u]; - let m0 = bitcast(ptcl[cmd_ix + 2u]); - let m1 = bitcast(ptcl[cmd_ix + 3u]); - let m2 = bitcast(ptcl[cmd_ix + 4u]); - let m3 = bitcast(ptcl[cmd_ix + 5u]); + let info_offset = ptcl[cmd_ix + 2u]; + let m0 = bitcast(info[info_offset]); + let m1 = bitcast(info[info_offset + 1u]); + let m2 = bitcast(info[info_offset + 2u]); + let m3 = bitcast(info[info_offset + 3u]); let matrx = vec4(m0, m1, m2, m3); - let xlat = vec2(bitcast(ptcl[cmd_ix + 6u]), bitcast(ptcl[cmd_ix + 7u])); - let c1 = vec2(bitcast(ptcl[cmd_ix + 8u]), bitcast(ptcl[cmd_ix + 9u])); - let ra = bitcast(ptcl[cmd_ix + 10u]); - let roff = bitcast(ptcl[cmd_ix + 11u]); + let xlat = vec2(bitcast(info[info_offset + 4u]), bitcast(info[info_offset + 5u])); + let c1 = vec2(bitcast(info[info_offset + 6u]), bitcast(info[info_offset + 7u])); + let ra = bitcast(info[info_offset + 8u]); + let roff = bitcast(info[info_offset + 9u]); return CmdRadGrad(index, matrx, xlat, c1, ra, roff); } @@ -208,7 +213,7 @@ fn main( let fg_i = fg * area[i]; rgba[i] = rgba[i] * (1.0 - fg_i.a) + fg_i; } - cmd_ix += 2u; + cmd_ix += 3u; } // CMD_LIN_GRAD case 6u: { @@ -221,7 +226,7 @@ fn main( let fg_i = fg_rgba * area[i]; rgba[i] = rgba[i] * (1.0 - fg_i.a) + fg_i; } - cmd_ix += 5u; + cmd_ix += 3u; } // CMD_RAD_GRAD case 7u: { @@ -238,7 +243,7 @@ fn main( let fg_i = fg_rgba * area[i]; rgba[i] = rgba[i] * (1.0 - fg_i.a) + fg_i; } - cmd_ix += 12u; + cmd_ix += 3u; } // CMD_BEGIN_CLIP case 9u: { diff --git a/piet-wgsl/src/render.rs b/piet-wgsl/src/render.rs index 4ba0a9a..2d50881 100644 --- a/piet-wgsl/src/render.rs +++ b/piet-wgsl/src/render.rs @@ -414,6 +414,7 @@ pub fn render_full( ResourceProxy::Image(out_image), ptcl_buf, gradient_image, + info_buf, ], ); (recording, ResourceProxy::Image(out_image)) diff --git a/piet-wgsl/src/shaders.rs b/piet-wgsl/src/shaders.rs index ef35a8f..a8a4176 100644 --- a/piet-wgsl/src/shaders.rs +++ b/piet-wgsl/src/shaders.rs @@ -290,6 +290,7 @@ pub fn full_shaders(device: &Device, engine: &mut Engine) -> Result Date: Mon, 28 Nov 2022 18:29:57 -0500 Subject: [PATCH 2/2] fix and offset bug and remove comment --- piet-wgsl/shader/coarse.wgsl | 4 ---- piet-wgsl/shader/fine.wgsl | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/piet-wgsl/shader/coarse.wgsl b/piet-wgsl/shader/coarse.wgsl index b912ed2..2f78c5c 100644 --- a/piet-wgsl/shader/coarse.wgsl +++ b/piet-wgsl/shader/coarse.wgsl @@ -113,10 +113,6 @@ fn write_color(color: CmdColor) { cmd_offset += 2u; } -// Discussion point: these are basically copying from info to ptcl. We -// could just write an info offset and have fine bind that buffer and read -// from it. - fn write_grad(ty: u32, index: u32, info_offset: u32) { alloc_cmd(3u); ptcl[cmd_offset] = ty; diff --git a/piet-wgsl/shader/fine.wgsl b/piet-wgsl/shader/fine.wgsl index 95d8d59..85d9950 100644 --- a/piet-wgsl/shader/fine.wgsl +++ b/piet-wgsl/shader/fine.wgsl @@ -213,7 +213,7 @@ fn main( let fg_i = fg * area[i]; rgba[i] = rgba[i] * (1.0 - fg_i.a) + fg_i; } - cmd_ix += 3u; + cmd_ix += 2u; } // CMD_LIN_GRAD case 6u: {