|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +// The scene segment table: one dispatch reads retained scenes in place. |
| 5 | +// |
| 6 | +// A retained scene drawn inside a layer does not copy its streams into the outer scene. The |
| 7 | +// scene buffer holds the outer scene data, then this table, then one resident block for each |
| 8 | +// retained scene. Each stream is a virtual stream: the outer elements before the first retained |
| 9 | +// scene, then the retained elements, then the next outer elements, and so on. The monoid scans |
| 10 | +// run over the virtual streams, so every monoid offset is a virtual offset. |
| 11 | +// |
| 12 | +// One table entry describes one segment, that is one contiguous piece of every stream. A shader |
| 13 | +// finds the segment of an element one time, from the virtual index of its path-tag word or of |
| 14 | +// its draw object. Then it reads each stream as scene[bias + virtual offset]. A bias is the |
| 15 | +// physical start minus the virtual start, in wrapping u32 arithmetic. The segments are in stream |
| 16 | +// order, and the first segment starts at virtual index zero in every stream. |
| 17 | +// |
| 18 | +// When config.segment_count is zero, the scene has no table and every helper returns the config |
| 19 | +// bases. Then the cost for each element is one compare of a uniform value. |
| 20 | +// |
| 21 | +// Like util.wgsl, this module references the `scene` and `config` bindings by name, so the root |
| 22 | +// shader must declare them. WGSL module-scope declarations do not depend on order, so the import |
| 23 | +// can be at the top of the root shader. The C# writer of the table is WebGPUSceneEncoder.cs. |
| 24 | + |
| 25 | +// Words in one table entry. |
| 26 | +const SCENE_SEGMENT_WORDS = 13u; |
| 27 | + |
| 28 | +// Virtual index of the first path-tag word of the segment. This is the key for path-tag elements. |
| 29 | +const SCENE_SEGMENT_PATHTAG_START = 0u; |
| 30 | +// Bias of the path-tag word stream. |
| 31 | +const SCENE_SEGMENT_PATHTAG_BIAS = 1u; |
| 32 | +// Bias of the path-data word stream. |
| 33 | +const SCENE_SEGMENT_PATHDATA_BIAS = 2u; |
| 34 | +// Bias of the style word stream. |
| 35 | +const SCENE_SEGMENT_STYLE_BIAS = 3u; |
| 36 | +// Bias of the transform stream: the physical word start minus 9 times the virtual transform index. |
| 37 | +const SCENE_SEGMENT_TRANSFORM_BIAS = 4u; |
| 38 | +// Virtual index of the first draw object of the segment. This is the key for draw objects. |
| 39 | +const SCENE_SEGMENT_DRAWTAG_START = 5u; |
| 40 | +// Bias of the draw-tag stream. |
| 41 | +const SCENE_SEGMENT_DRAWTAG_BIAS = 6u; |
| 42 | +// Bias of the draw-data word stream. |
| 43 | +const SCENE_SEGMENT_DRAWDATA_BIAS = 7u; |
| 44 | +// Word offset of the auxiliary brush data of the segment in the combined info buffer. |
| 45 | +const SCENE_SEGMENT_BRUSH_BASE = 8u; |
| 46 | +// First gradient-ramp row of the segment. |
| 47 | +const SCENE_SEGMENT_GRADIENT_ROW_BASE = 9u; |
| 48 | +// First image-atlas row of the segment. |
| 49 | +const SCENE_SEGMENT_ATLAS_Y_BASE = 10u; |
| 50 | +// Profile slot origin: the slot of a fill line is this value plus half the physical path-data |
| 51 | +// word of the line start. Zero when the segment has no profile table. |
| 52 | +const SCENE_SEGMENT_PROFILE_SLOTS = 11u; |
| 53 | +// Word offset of the profile record table of the segment, or zero when it has none. |
| 54 | +const SCENE_SEGMENT_PROFILE_RECORDS = 12u; |
| 55 | + |
| 56 | +// Returned in place of a table entry when the scene has no segment table. |
| 57 | +const SCENE_SEGMENT_NONE = 0xffffffffu; |
| 58 | + |
| 59 | +// Returns the word offset of the table entry for the last segment whose key at `key_field` is |
| 60 | +// less than or equal to `element_ix`. Empty segments share their key with the next segment, so |
| 61 | +// the search selects the segment that holds the element. Call this only when |
| 62 | +// config.segment_count is not zero. |
| 63 | +fn find_scene_segment(key_field: u32, element_ix: u32) -> u32 { |
| 64 | + var lo = 0u; |
| 65 | + var hi = config.segment_count; |
| 66 | + while lo + 1u < hi { |
| 67 | + let mid = (lo + hi) >> 1u; |
| 68 | + if scene[config.segment_table_base + mid * SCENE_SEGMENT_WORDS + key_field] <= element_ix { |
| 69 | + lo = mid; |
| 70 | + } else { |
| 71 | + hi = mid; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return config.segment_table_base + lo * SCENE_SEGMENT_WORDS; |
| 76 | +} |
| 77 | + |
| 78 | +// Returns the bias of the path-tag word stream for the path-tag word `word_ix`. |
| 79 | +fn resolve_path_tag_bias(word_ix: u32) -> u32 { |
| 80 | + if config.segment_count == 0u { |
| 81 | + return config.pathtag_base; |
| 82 | + } |
| 83 | + |
| 84 | + return scene[find_scene_segment(SCENE_SEGMENT_PATHTAG_START, word_ix) + SCENE_SEGMENT_PATHTAG_BIAS]; |
| 85 | +} |
| 86 | + |
| 87 | +// Returns the table entry of the segment that holds draw object `ix`, or SCENE_SEGMENT_NONE |
| 88 | +// when the scene has no segment table. |
| 89 | +fn resolve_draw_segment(ix: u32) -> u32 { |
| 90 | + if config.segment_count == 0u { |
| 91 | + return SCENE_SEGMENT_NONE; |
| 92 | + } |
| 93 | + |
| 94 | + return find_scene_segment(SCENE_SEGMENT_DRAWTAG_START, ix); |
| 95 | +} |
| 96 | + |
| 97 | +// Returns the bias of the draw-tag stream for a segment entry from resolve_draw_segment. |
| 98 | +fn draw_tag_bias(entry: u32) -> u32 { |
| 99 | + if entry == SCENE_SEGMENT_NONE { |
| 100 | + return config.drawtag_base; |
| 101 | + } |
| 102 | + |
| 103 | + return scene[entry + SCENE_SEGMENT_DRAWTAG_BIAS]; |
| 104 | +} |
| 105 | + |
| 106 | +// Returns the bias of the draw-data stream for a segment entry from resolve_draw_segment. |
| 107 | +fn draw_data_bias(entry: u32) -> u32 { |
| 108 | + if entry == SCENE_SEGMENT_NONE { |
| 109 | + return config.drawdata_base; |
| 110 | + } |
| 111 | + |
| 112 | + return scene[entry + SCENE_SEGMENT_DRAWDATA_BIAS]; |
| 113 | +} |
0 commit comments