Skip to content

Commit

Permalink
[shaders] Use constants in switch statements (#525)
Browse files Browse the repository at this point in the history
Naga didn't support constant evaluation until recently, which prevented
constant declarations from being referenced in switch case statements.
Now that naga supports consteval (starting in 0.19) and the feature is
available in vello, this PR updates the shaders to reference constants
directly instead of numerical literals.
  • Loading branch information
armansito authored Mar 18, 2024
1 parent b8b0e29 commit 74715ee
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 115 deletions.
27 changes: 9 additions & 18 deletions shader/coarse.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -357,40 +357,34 @@ fn main(
let tile_ix = sh_tile_base[el_ix] + sh_tile_stride[el_ix] * tile_y + tile_x;
let tile = tiles[tile_ix];
switch drawtag {
// DRAWTAG_FILL_COLOR
case 0x44u: {
case DRAWTAG_FILL_COLOR: {
write_path(tile, tile_ix, draw_flags);
let rgba_color = scene[dd];
write_color(CmdColor(rgba_color));
}
// DRAWTAG_FILL_LIN_GRADIENT
case 0x114u: {
case DRAWTAG_FILL_LIN_GRADIENT: {
write_path(tile, tile_ix, draw_flags);
let index = scene[dd];
let info_offset = di + 1u;
write_grad(CMD_LIN_GRAD, index, info_offset);
}
// DRAWTAG_FILL_RAD_GRADIENT
case 0x29cu: {
case DRAWTAG_FILL_RAD_GRADIENT: {
write_path(tile, tile_ix, draw_flags);
let index = scene[dd];
let info_offset = di + 1u;
write_grad(CMD_RAD_GRAD, index, info_offset);
}
// DRAWTAG_FILL_SWEEP_GRADIENT
case 0x254u: {
case DRAWTAG_FILL_SWEEP_GRADIENT: {
write_path(tile, tile_ix, draw_flags);
let index = scene[dd];
let info_offset = di + 1u;
write_grad(CMD_SWEEP_GRAD, index, info_offset);
}
// DRAWTAG_FILL_IMAGE
case 0x248u: {
case DRAWTAG_FILL_IMAGE: {
write_path(tile, tile_ix, draw_flags);
write_image(di + 1u);
}
// DRAWTAG_BEGIN_CLIP
case 0x9u: {
case DRAWTAG_BEGIN_CLIP: {
if tile.segment_count_or_ix == 0u && tile.backdrop == 0 {
clip_zero_depth = clip_depth + 1u;
} else {
Expand All @@ -400,8 +394,7 @@ fn main(
}
clip_depth += 1u;
}
// DRAWTAG_END_CLIP
case 0x21u: {
case DRAWTAG_END_CLIP: {
clip_depth -= 1u;
// A clip shape is always a non-zero fill (draw_flags=0).
write_path(tile, tile_ix, /*draw_flags=*/0u);
Expand All @@ -415,12 +408,10 @@ fn main(
} else {
// In "clip zero" state, suppress all drawing
switch drawtag {
// DRAWTAG_BEGIN_CLIP
case 0x9u: {
case DRAWTAG_BEGIN_CLIP: {
clip_depth += 1u;
}
// DRAWTAG_END_CLIP
case 0x21u: {
case DRAWTAG_END_CLIP: {
if clip_depth == clip_zero_depth {
clip_zero_depth = 0u;
}
Expand Down
15 changes: 5 additions & 10 deletions shader/draw_leaf.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,10 @@ fn main(
transform = read_transform(config.transform_base, bbox.trans_ix);
}
switch tag_word {
// DRAWTAG_FILL_COLOR
case 0x44u: {
case DRAWTAG_FILL_COLOR: {
info[di] = draw_flags;
}
// DRAWTAG_FILL_LIN_GRADIENT
case 0x114u: {
case DRAWTAG_FILL_LIN_GRADIENT: {
info[di] = draw_flags;
var p0 = bitcast<vec2<f32>>(vec2(scene[dd + 1u], scene[dd + 2u]));
var p1 = bitcast<vec2<f32>>(vec2(scene[dd + 3u], scene[dd + 4u]));
Expand All @@ -136,8 +134,7 @@ fn main(
info[di + 2u] = bitcast<u32>(line_xy.y);
info[di + 3u] = bitcast<u32>(line_c);
}
// DRAWTAG_FILL_RAD_GRADIENT
case 0x29cu: {
case DRAWTAG_FILL_RAD_GRADIENT: {
// Two-point conical gradient implementation based
// on the algorithm at <https://skia.org/docs/dev/design/conical/>
// This epsilon matches what Skia uses
Expand Down Expand Up @@ -220,8 +217,7 @@ fn main(
info[di + 8u] = bitcast<u32>(radius);
info[di + 9u] = bitcast<u32>((flags << 3u) | kind);
}
// DRAWTAG_FILL_SWEEP_GRADIENT
case 0x254u: {
case DRAWTAG_FILL_SWEEP_GRADIENT: {
info[di] = draw_flags;
let p0 = bitcast<vec2<f32>>(vec2(scene[dd + 1u], scene[dd + 2u]));
let xform = transform_mul(transform, Transform(vec4(1.0, 0.0, 0.0, 1.0), p0));
Expand All @@ -235,8 +231,7 @@ fn main(
info[di + 7u] = scene[dd + 3u];
info[di + 8u] = scene[dd + 4u];
}
// DRAWTAG_FILL_IMAGE
case 0x248u: {
case DRAWTAG_FILL_IMAGE: {
info[di] = draw_flags;
let inv = transform_inverse(transform);
info[di + 1u] = bitcast<u32>(inv.matrx.x);
Expand Down
49 changes: 18 additions & 31 deletions shader/fine.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -779,21 +779,18 @@ fn read_end_clip(cmd_ix: u32) -> CmdEndClip {
return CmdEndClip(blend, alpha);
}

const EXTEND_PAD: u32 = 0u;
const EXTEND_REPEAT: u32 = 1u;
const EXTEND_REFLECT: u32 = 2u;
fn extend_mode(t: f32, mode: u32) -> f32 {
let EXTEND_PAD = 0u;
let EXTEND_REPEAT = 1u;
let EXTEND_REFLECT = 2u;
switch mode {
// EXTEND_PAD
case 0u: {
case EXTEND_PAD: {
return clamp(t, 0.0, 1.0);
}
// EXTEND_REPEAT
case 1u: {
case EXTEND_REPEAT: {
return fract(t);
}
// EXTEND_REFLECT
default: {
case EXTEND_REFLECT, default: {
return abs(t - 2.0 * round(0.5 * t));
}
}
Expand Down Expand Up @@ -891,8 +888,7 @@ fn main(
break;
}
switch tag {
// CMD_FILL
case 1u: {
case CMD_FILL: {
let fill = read_fill(cmd_ix);
#ifdef msaa
fill_path_ms(fill, local_id.xy, &area);
Expand All @@ -901,25 +897,23 @@ fn main(
#endif
cmd_ix += 4u;
}
// CMD_STROKE
case 2u: {
case CMD_STROKE: {
// Stroking in fine rasterization is disabled, as strokes will be expanded
// to fills earlier in the pipeline. This implementation is a stub, just to
// keep the shader from crashing.
// TODO: Remove this case
for (var i = 0u; i < PIXELS_PER_THREAD; i++) {
area[i] = 0.0;
}
cmd_ix += 3u;
}
// CMD_SOLID
case 3u: {
case CMD_SOLID: {
for (var i = 0u; i < PIXELS_PER_THREAD; i += 1u) {
area[i] = 1.0;
}
cmd_ix += 1u;
}
// CMD_COLOR
case 5u: {
case CMD_COLOR: {
let color = read_color(cmd_ix);
let fg = unpack4x8unorm(color.rgba_color).wzyx;
for (var i = 0u; i < PIXELS_PER_THREAD; i += 1u) {
Expand All @@ -928,8 +922,7 @@ fn main(
}
cmd_ix += 2u;
}
// CMD_LIN_GRAD
case 6u: {
case CMD_LIN_GRAD: {
let lin = read_lin_grad(cmd_ix);
let d = lin.line_x * xy.x + lin.line_y * xy.y + lin.line_c;
for (var i = 0u; i < PIXELS_PER_THREAD; i += 1u) {
Expand All @@ -941,8 +934,7 @@ fn main(
}
cmd_ix += 3u;
}
// CMD_RAD_GRAD
case 7u: {
case CMD_RAD_GRAD: {
let rad = read_rad_grad(cmd_ix);
let focal_x = rad.focal_x;
let radius = rad.radius;
Expand Down Expand Up @@ -987,8 +979,7 @@ fn main(
}
cmd_ix += 3u;
}
// CMD_SWEEP_GRAD
case 8u: {
case CMD_SWEEP_GRAD: {
let sweep = read_sweep_grad(cmd_ix);
let scale = 1.0 / (sweep.t1 - sweep.t0);
for (var i = 0u; i < PIXELS_PER_THREAD; i += 1u) {
Expand Down Expand Up @@ -1021,8 +1012,7 @@ fn main(
}
cmd_ix += 3u;
}
// CMD_IMAGE
case 9u: {
case CMD_IMAGE: {
let image = read_image(cmd_ix);
let atlas_extents = image.atlas_offset + image.extents;
for (var i = 0u; i < PIXELS_PER_THREAD; i += 1u) {
Expand All @@ -1043,8 +1033,7 @@ fn main(
}
cmd_ix += 2u;
}
// CMD_BEGIN_CLIP
case 10u: {
case CMD_BEGIN_CLIP: {
if clip_depth < BLEND_STACK_SPLIT {
for (var i = 0u; i < PIXELS_PER_THREAD; i += 1u) {
blend_stack[clip_depth][i] = pack4x8unorm(rgba[i]);
Expand All @@ -1056,8 +1045,7 @@ fn main(
clip_depth += 1u;
cmd_ix += 1u;
}
// CMD_END_CLIP
case 11u: {
case CMD_END_CLIP: {
let end_clip = read_end_clip(cmd_ix);
clip_depth -= 1u;
for (var i = 0u; i < PIXELS_PER_THREAD; i += 1u) {
Expand All @@ -1073,8 +1061,7 @@ fn main(
}
cmd_ix += 3u;
}
// CMD_JUMP
case 12u: {
case CMD_JUMP: {
cmd_ix = ptcl[cmd_ix + 1u];
}
default: {}
Expand Down
Loading

0 comments on commit 74715ee

Please sign in to comment.