| 1 | @group(1) @binding(0) var t_instances: texture_2d<u32>; |
| 2 | |
| 3 | // Each texel of `t_instances` packs four 32-bit words of instance data. Most |
| 4 | // records are read strictly front to back through a cursor that keeps the |
| 5 | // most recently fetched texel and only touches the texture again when the next |
| 6 | // word crosses a texel boundary. This fetches each texel exactly once per |
| 7 | // record load instead of once per word. The `read_*` functions must therefore |
| 8 | // consume their words in struct declaration order; skipping or re-reading words |
| 9 | // would still return correct data (the cursor re-fetches whenever the texel |
| 10 | // index changes) but would forfeit the single-fetch-per-texel guarantee. |
| 11 | struct InstanceCursor { |
| 12 | word_index: u32, |
| 13 | texel_index: u32, |
| 14 | texel: vec4<u32>, |
| 15 | width: u32, |
| 16 | } |
| 17 | |
| 18 | fn fetch_instance_texel(texel_index: u32, width: u32) -> vec4<u32> { |
| 19 | let coordinate = vec2<i32>( |
| 20 | i32(texel_index % width), |
| 21 | i32(texel_index / width), |
| 22 | ); |
| 23 | return textureLoad(t_instances, coordinate, 0); |
| 24 | } |
| 25 | |
| 26 | fn instance_cursor(word_index: u32) -> InstanceCursor { |
| 27 | let width = textureDimensions(t_instances).x; |
| 28 | let texel_index = word_index / 4u; |
| 29 | return InstanceCursor( |
| 30 | word_index, |
| 31 | texel_index, |
| 32 | fetch_instance_texel(texel_index, width), |
| 33 | width, |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | fn read_word(cursor: ptr<function, InstanceCursor>) -> u32 { |
| 38 | let word_index = (*cursor).word_index; |
| 39 | let texel_index = word_index / 4u; |
| 40 | if texel_index != (*cursor).texel_index { |
| 41 | (*cursor).texel = fetch_instance_texel(texel_index, (*cursor).width); |
| 42 | (*cursor).texel_index = texel_index; |
| 43 | } |
| 44 | (*cursor).word_index = word_index + 1u; |
| 45 | return (*cursor).texel[word_index % 4u]; |
| 46 | } |
| 47 | |
| 48 | fn read_f32(cursor: ptr<function, InstanceCursor>) -> f32 { |
| 49 | return bitcast<f32>(read_word(cursor)); |
| 50 | } |
| 51 | |
| 52 | fn read_i32(cursor: ptr<function, InstanceCursor>) -> i32 { |
| 53 | return bitcast<i32>(read_word(cursor)); |
| 54 | } |
| 55 | |
| 56 | fn read_vec2_f32(cursor: ptr<function, InstanceCursor>) -> vec2<f32> { |
| 57 | return vec2<f32>(read_f32(cursor), read_f32(cursor)); |
| 58 | } |
| 59 | |
| 60 | fn read_vec2_i32(cursor: ptr<function, InstanceCursor>) -> vec2<i32> { |
| 61 | return vec2<i32>(read_i32(cursor), read_i32(cursor)); |
| 62 | } |
| 63 | |
| 64 | fn read_hsla(cursor: ptr<function, InstanceCursor>) -> Hsla { |
| 65 | return Hsla( |
| 66 | read_f32(cursor), |
| 67 | read_f32(cursor), |
| 68 | read_f32(cursor), |
| 69 | read_f32(cursor), |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | fn read_bounds(cursor: ptr<function, InstanceCursor>) -> Bounds { |
| 74 | return Bounds(read_vec2_f32(cursor), read_vec2_f32(cursor)); |
| 75 | } |
| 76 | |
| 77 | fn read_corners(cursor: ptr<function, InstanceCursor>) -> Corners { |
| 78 | return Corners( |
| 79 | read_f32(cursor), |
| 80 | read_f32(cursor), |
| 81 | read_f32(cursor), |
| 82 | read_f32(cursor), |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | fn read_edges(cursor: ptr<function, InstanceCursor>) -> Edges { |
| 87 | return Edges( |
| 88 | read_f32(cursor), |
| 89 | read_f32(cursor), |
| 90 | read_f32(cursor), |
| 91 | read_f32(cursor), |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | fn read_color_stop(cursor: ptr<function, InstanceCursor>) -> LinearColorStop { |
| 96 | return LinearColorStop(read_hsla(cursor), read_f32(cursor)); |
| 97 | } |
| 98 | |
| 99 | fn read_background(cursor: ptr<function, InstanceCursor>) -> Background { |
| 100 | return Background( |
| 101 | read_word(cursor), |
| 102 | read_word(cursor), |
| 103 | read_hsla(cursor), |
| 104 | read_f32(cursor), |
| 105 | array<LinearColorStop, 2>( |
| 106 | read_color_stop(cursor), |
| 107 | read_color_stop(cursor), |
| 108 | ), |
| 109 | read_word(cursor), |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | fn read_atlas_tile(cursor: ptr<function, InstanceCursor>) -> AtlasTile { |
| 114 | return AtlasTile( |
| 115 | AtlasTextureId( |
| 116 | read_word(cursor), |
| 117 | read_word(cursor), |
| 118 | ), |
| 119 | read_word(cursor), |
| 120 | read_word(cursor), |
| 121 | AtlasBounds( |
| 122 | read_vec2_i32(cursor), |
| 123 | read_vec2_i32(cursor), |
| 124 | ), |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | fn read_transformation(cursor: ptr<function, InstanceCursor>) -> TransformationMatrix { |
| 129 | return TransformationMatrix( |
| 130 | mat2x2<f32>( |
| 131 | read_vec2_f32(cursor), |
| 132 | read_vec2_f32(cursor), |
| 133 | ), |
| 134 | read_vec2_f32(cursor), |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | fn load_quad(instance_id: u32) -> Quad { |
| 139 | // Keep this fixed-layout decoder explicit. Some WebGL shader compilers fail |
| 140 | // to optimize the cursor's branches and dynamic vector indexing for quads. |
| 141 | let first_texel_index = instance_id * 10u; |
| 142 | let width = textureDimensions(t_instances).x; |
| 143 | let texel0 = fetch_instance_texel(first_texel_index, width); |
| 144 | let texel1 = fetch_instance_texel(first_texel_index + 1u, width); |
| 145 | let texel2 = fetch_instance_texel(first_texel_index + 2u, width); |
| 146 | let texel3 = fetch_instance_texel(first_texel_index + 3u, width); |
| 147 | let texel4 = fetch_instance_texel(first_texel_index + 4u, width); |
| 148 | let texel5 = fetch_instance_texel(first_texel_index + 5u, width); |
| 149 | let texel6 = fetch_instance_texel(first_texel_index + 6u, width); |
| 150 | let texel7 = fetch_instance_texel(first_texel_index + 7u, width); |
| 151 | let texel8 = fetch_instance_texel(first_texel_index + 8u, width); |
| 152 | let texel9 = fetch_instance_texel(first_texel_index + 9u, width); |
| 153 | |
| 154 | let values0 = bitcast<vec4<f32>>(texel0); |
| 155 | let values1 = bitcast<vec4<f32>>(texel1); |
| 156 | let values2 = bitcast<vec4<f32>>(texel2); |
| 157 | let values3 = bitcast<vec4<f32>>(texel3); |
| 158 | let values4 = bitcast<vec4<f32>>(texel4); |
| 159 | let values5 = bitcast<vec4<f32>>(texel5); |
| 160 | let values6 = bitcast<vec4<f32>>(texel6); |
| 161 | let values7 = bitcast<vec4<f32>>(texel7); |
| 162 | let values8 = bitcast<vec4<f32>>(texel8); |
| 163 | let values9 = bitcast<vec4<f32>>(texel9); |
| 164 | |
| 165 | return Quad( |
| 166 | texel0.x, |
| 167 | texel0.y, |
| 168 | Bounds(values0.zw, values1.xy), |
| 169 | Bounds(values1.zw, values2.xy), |
| 170 | Background( |
| 171 | texel2.z, |
| 172 | texel2.w, |
| 173 | Hsla(values3.x, values3.y, values3.z, values3.w), |
| 174 | values4.x, |
| 175 | array<LinearColorStop, 2>( |
| 176 | LinearColorStop( |
| 177 | Hsla(values4.y, values4.z, values4.w, values5.x), |
| 178 | values5.y, |
| 179 | ), |
| 180 | LinearColorStop( |
| 181 | Hsla(values5.z, values5.w, values6.x, values6.y), |
| 182 | values6.z, |
| 183 | ), |
| 184 | ), |
| 185 | texel6.w, |
| 186 | ), |
| 187 | Hsla(values7.x, values7.y, values7.z, values7.w), |
| 188 | Corners(values8.x, values8.y, values8.z, values8.w), |
| 189 | Edges(values9.x, values9.y, values9.z, values9.w), |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | fn load_shadow(instance_id: u32) -> Shadow { |
| 194 | var cursor = instance_cursor(instance_id * 28u); |
| 195 | return Shadow( |
| 196 | read_word(&cursor), |
| 197 | read_f32(&cursor), |
| 198 | read_bounds(&cursor), |
| 199 | read_corners(&cursor), |
| 200 | read_bounds(&cursor), |
| 201 | read_hsla(&cursor), |
| 202 | read_bounds(&cursor), |
| 203 | read_corners(&cursor), |
| 204 | read_word(&cursor), |
| 205 | read_word(&cursor), |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | fn load_path_vertex(vertex_id: u32) -> PathRasterizationVertex { |
| 210 | var cursor = instance_cursor(vertex_id * 26u); |
| 211 | return PathRasterizationVertex( |
| 212 | read_vec2_f32(&cursor), |
| 213 | read_vec2_f32(&cursor), |
| 214 | read_background(&cursor), |
| 215 | read_bounds(&cursor), |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | fn load_path_sprite(instance_id: u32) -> PathSprite { |
| 220 | var cursor = instance_cursor(instance_id * 4u); |
| 221 | return PathSprite(read_bounds(&cursor)); |
| 222 | } |
| 223 | |
| 224 | fn load_underline(instance_id: u32) -> Underline { |
| 225 | var cursor = instance_cursor(instance_id * 16u); |
| 226 | return Underline( |
| 227 | read_word(&cursor), |
| 228 | read_word(&cursor), |
| 229 | read_bounds(&cursor), |
| 230 | read_bounds(&cursor), |
| 231 | read_hsla(&cursor), |
| 232 | read_f32(&cursor), |
| 233 | read_word(&cursor), |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | fn load_mono_sprite(instance_id: u32) -> MonochromeSprite { |
| 238 | var cursor = instance_cursor(instance_id * 28u); |
| 239 | return MonochromeSprite( |
| 240 | read_word(&cursor), |
| 241 | read_word(&cursor), |
| 242 | read_bounds(&cursor), |
| 243 | read_bounds(&cursor), |
| 244 | read_hsla(&cursor), |
| 245 | read_atlas_tile(&cursor), |
| 246 | read_transformation(&cursor), |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | fn load_poly_sprite(instance_id: u32) -> PolychromeSprite { |
| 251 | var cursor = instance_cursor(instance_id * 24u); |
| 252 | return PolychromeSprite( |
| 253 | read_word(&cursor), |
| 254 | read_word(&cursor), |
| 255 | read_word(&cursor), |
| 256 | read_f32(&cursor), |
| 257 | read_bounds(&cursor), |
| 258 | read_bounds(&cursor), |
| 259 | read_corners(&cursor), |
| 260 | read_atlas_tile(&cursor), |
| 261 | ); |
| 262 | } |