shaders.wgsl

  1struct GlobalParams {
  2    viewport_size: vec2<f32>,
  3    pad: vec2<u32>,
  4}
  5
  6var<uniform> globals: GlobalParams;
  7var t_sprite: texture_2d<f32>;
  8var s_sprite: sampler;
  9
 10const M_PI_F: f32 = 3.1415926;
 11const GRAYSCALE_FACTORS: vec3<f32> = vec3<f32>(0.2126, 0.7152, 0.0722);
 12
 13struct Bounds {
 14    origin: vec2<f32>,
 15    size: vec2<f32>,
 16}
 17struct Corners {
 18    top_left: f32,
 19    top_right: f32,
 20    bottom_right: f32,
 21    bottom_left: f32,
 22}
 23struct Edges {
 24    top: f32,
 25    right: f32,
 26    bottom: f32,
 27    left: f32,
 28}
 29struct Hsla {
 30    h: f32,
 31    s: f32,
 32    l: f32,
 33    a: f32,
 34}
 35
 36struct AtlasTextureId {
 37    index: u32,
 38    kind: u32,
 39}
 40
 41struct AtlasBounds {
 42    origin: vec2<i32>,
 43    size: vec2<i32>,
 44}
 45struct AtlasTile {
 46    texture_id: AtlasTextureId,
 47    tile_id: u32,
 48    padding: u32,
 49    bounds: AtlasBounds,
 50}
 51
 52struct TransformationMatrix {
 53    rotation_scale: mat2x2<f32>,
 54    translation: vec2<f32>,
 55}
 56
 57fn to_device_position_impl(position: vec2<f32>) -> vec4<f32> {
 58    let device_position = position / globals.viewport_size * vec2<f32>(2.0, -2.0) + vec2<f32>(-1.0, 1.0);
 59    return vec4<f32>(device_position, 0.0, 1.0);
 60}
 61
 62fn to_device_position(unit_vertex: vec2<f32>, bounds: Bounds) -> vec4<f32> {
 63    let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
 64    return to_device_position_impl(position);
 65}
 66
 67fn to_device_position_transformed(unit_vertex: vec2<f32>, bounds: Bounds, transform: TransformationMatrix) -> vec4<f32> {
 68    let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
 69    //Note: Rust side stores it as row-major, so transposing here
 70    let transformed = transpose(transform.rotation_scale) * position + transform.translation;
 71    return to_device_position_impl(transformed);
 72}
 73
 74fn to_tile_position(unit_vertex: vec2<f32>, tile: AtlasTile) -> vec2<f32> {
 75  let atlas_size = vec2<f32>(textureDimensions(t_sprite, 0));
 76  return (vec2<f32>(tile.bounds.origin) + unit_vertex * vec2<f32>(tile.bounds.size)) / atlas_size;
 77}
 78
 79fn distance_from_clip_rect_impl(position: vec2<f32>, clip_bounds: Bounds) -> vec4<f32> {
 80    let tl = position - clip_bounds.origin;
 81    let br = clip_bounds.origin + clip_bounds.size - position;
 82    return vec4<f32>(tl.x, br.x, tl.y, br.y);
 83}
 84
 85fn distance_from_clip_rect(unit_vertex: vec2<f32>, bounds: Bounds, clip_bounds: Bounds) -> vec4<f32> {
 86    let position = unit_vertex * vec2<f32>(bounds.size) + bounds.origin;
 87    return distance_from_clip_rect_impl(position, clip_bounds);
 88}
 89
 90fn hsla_to_rgba(hsla: Hsla) -> vec4<f32> {
 91    let h = hsla.h * 6.0; // Now, it's an angle but scaled in [0, 6) range
 92    let s = hsla.s;
 93    let l = hsla.l;
 94    let a = hsla.a;
 95
 96    let c = (1.0 - abs(2.0 * l - 1.0)) * s;
 97    let x = c * (1.0 - abs(h % 2.0 - 1.0));
 98    let m = l - c / 2.0;
 99
100    var color = vec4<f32>(m, m, m, a);
101
102    if (h >= 0.0 && h < 1.0) {
103        color.r += c;
104        color.g += x;
105    } else if (h >= 1.0 && h < 2.0) {
106        color.r += x;
107        color.g += c;
108    } else if (h >= 2.0 && h < 3.0) {
109        color.g += c;
110        color.b += x;
111    } else if (h >= 3.0 && h < 4.0) {
112        color.g += x;
113        color.b += c;
114    } else if (h >= 4.0 && h < 5.0) {
115        color.r += x;
116        color.b += c;
117    } else {
118        color.r += c;
119        color.b += x;
120    }
121
122    return color;
123}
124
125fn over(below: vec4<f32>, above: vec4<f32>) -> vec4<f32> {
126    let alpha = above.a + below.a * (1.0 - above.a);
127    let color = (above.rgb * above.a + below.rgb * below.a * (1.0 - above.a)) / alpha;
128    return vec4<f32>(color, alpha);
129}
130
131// A standard gaussian function, used for weighting samples
132fn gaussian(x: f32, sigma: f32) -> f32{
133    return exp(-(x * x) / (2.0 * sigma * sigma)) / (sqrt(2.0 * M_PI_F) * sigma);
134}
135
136// This approximates the error function, needed for the gaussian integral
137fn erf(v: vec2<f32>) -> vec2<f32> {
138    let s = sign(v);
139    let a = abs(v);
140    let r1 = 1.0 + (0.278393 + (0.230389 + 0.078108 * (a * a)) * a) * a;
141    let r2 = r1 * r1;
142    return s - s / (r2 * r2);
143}
144
145fn blur_along_x(x: f32, y: f32, sigma: f32, corner: f32, half_size: vec2<f32>) -> f32 {
146  let delta = min(half_size.y - corner - abs(y), 0.0);
147  let curved = half_size.x - corner + sqrt(max(0.0, corner * corner - delta * delta));
148  let integral = 0.5 + 0.5 * erf((x + vec2<f32>(-curved, curved)) * (sqrt(0.5) / sigma));
149  return integral.y - integral.x;
150}
151
152fn pick_corner_radius(point: vec2<f32>, radii: Corners) -> f32 {
153    if (point.x < 0.0) {
154        if (point.y < 0.0) {
155            return radii.top_left;
156        } else {
157            return radii.bottom_left;
158        }
159    } else {
160        if (point.y < 0.0) {
161            return radii.top_right;
162        } else {
163            return radii.bottom_right;
164        }
165    }
166}
167
168fn quad_sdf(point: vec2<f32>, bounds: Bounds, corner_radii: Corners) -> f32 {
169    let half_size = bounds.size / 2.0;
170    let center = bounds.origin + half_size;
171    let center_to_point = point - center;
172    let corner_radius = pick_corner_radius(center_to_point, corner_radii);
173    let rounded_edge_to_point = abs(center_to_point) - half_size + corner_radius;
174    return length(max(vec2<f32>(0.0), rounded_edge_to_point)) +
175        min(0.0, max(rounded_edge_to_point.x, rounded_edge_to_point.y)) -
176        corner_radius;
177}
178
179// --- quads --- //
180
181struct Quad {
182    order: u32,
183    pad: u32,
184    bounds: Bounds,
185    content_mask: Bounds,
186    background: Hsla,
187    border_color: Hsla,
188    corner_radii: Corners,
189    border_widths: Edges,
190}
191var<storage, read> b_quads: array<Quad>;
192
193struct QuadVarying {
194    @builtin(position) position: vec4<f32>,
195    @location(0) @interpolate(flat) background_color: vec4<f32>,
196    @location(1) @interpolate(flat) border_color: vec4<f32>,
197    @location(2) @interpolate(flat) quad_id: u32,
198    //TODO: use `clip_distance` once Naga supports it
199    @location(3) clip_distances: vec4<f32>,
200}
201
202@vertex
203fn vs_quad(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> QuadVarying {
204    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
205    let quad = b_quads[instance_id];
206
207    var out = QuadVarying();
208    out.position = to_device_position(unit_vertex, quad.bounds);
209    out.background_color = hsla_to_rgba(quad.background);
210    out.border_color = hsla_to_rgba(quad.border_color);
211    out.quad_id = instance_id;
212    out.clip_distances = distance_from_clip_rect(unit_vertex, quad.bounds, quad.content_mask);
213    return out;
214}
215
216@fragment
217fn fs_quad(input: QuadVarying) -> @location(0) vec4<f32> {
218    // Alpha clip first, since we don't have `clip_distance`.
219    if (any(input.clip_distances < vec4<f32>(0.0))) {
220        return vec4<f32>(0.0);
221    }
222
223    let quad = b_quads[input.quad_id];
224    // Fast path when the quad is not rounded and doesn't have any border.
225    if (quad.corner_radii.top_left == 0.0 && quad.corner_radii.bottom_left == 0.0 &&
226        quad.corner_radii.top_right == 0.0 &&
227        quad.corner_radii.bottom_right == 0.0 && quad.border_widths.top == 0.0 &&
228        quad.border_widths.left == 0.0 && quad.border_widths.right == 0.0 &&
229        quad.border_widths.bottom == 0.0) {
230        return input.background_color;
231    }
232
233    let half_size = quad.bounds.size / 2.0;
234    let center = quad.bounds.origin + half_size;
235    let center_to_point = input.position.xy - center;
236
237    let corner_radius = pick_corner_radius(center_to_point, quad.corner_radii);
238
239    let rounded_edge_to_point = abs(center_to_point) - half_size + corner_radius;
240    let distance =
241      length(max(vec2<f32>(0.0), rounded_edge_to_point)) +
242      min(0.0, max(rounded_edge_to_point.x, rounded_edge_to_point.y)) -
243      corner_radius;
244
245    let vertical_border = select(quad.border_widths.left, quad.border_widths.right, center_to_point.x > 0.0);
246    let horizontal_border = select(quad.border_widths.top, quad.border_widths.bottom, center_to_point.y > 0.0);
247    let inset_size = half_size - corner_radius - vec2<f32>(vertical_border, horizontal_border);
248    let point_to_inset_corner = abs(center_to_point) - inset_size;
249
250    var border_width = 0.0;
251    if (point_to_inset_corner.x < 0.0 && point_to_inset_corner.y < 0.0) {
252        border_width = 0.0;
253    } else if (point_to_inset_corner.y > point_to_inset_corner.x) {
254        border_width = horizontal_border;
255    } else {
256        border_width = vertical_border;
257    }
258
259    var color = input.background_color;
260    if (border_width > 0.0) {
261        let inset_distance = distance + border_width;
262        // Blend the border on top of the background and then linearly interpolate
263        // between the two as we slide inside the background.
264        let blended_border = over(input.background_color, input.border_color);
265        color = mix(blended_border, input.background_color,
266                    saturate(0.5 - inset_distance));
267    }
268
269    return color * vec4<f32>(1.0, 1.0, 1.0, saturate(0.5 - distance));
270}
271
272// --- shadows --- //
273
274struct Shadow {
275    order: u32,
276    blur_radius: f32,
277    bounds: Bounds,
278    corner_radii: Corners,
279    content_mask: Bounds,
280    color: Hsla,
281}
282var<storage, read> b_shadows: array<Shadow>;
283
284struct ShadowVarying {
285    @builtin(position) position: vec4<f32>,
286    @location(0) @interpolate(flat) color: vec4<f32>,
287    @location(1) @interpolate(flat) shadow_id: u32,
288    //TODO: use `clip_distance` once Naga supports it
289    @location(3) clip_distances: vec4<f32>,
290}
291
292@vertex
293fn vs_shadow(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> ShadowVarying {
294    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
295    var shadow = b_shadows[instance_id];
296
297    let margin = 3.0 * shadow.blur_radius;
298    // Set the bounds of the shadow and adjust its size based on the shadow's
299    // spread radius to achieve the spreading effect
300    shadow.bounds.origin -= vec2<f32>(margin);
301    shadow.bounds.size += 2.0 * vec2<f32>(margin);
302
303    var out = ShadowVarying();
304    out.position = to_device_position(unit_vertex, shadow.bounds);
305    out.color = hsla_to_rgba(shadow.color);
306    out.shadow_id = instance_id;
307    out.clip_distances = distance_from_clip_rect(unit_vertex, shadow.bounds, shadow.content_mask);
308    return out;
309}
310
311@fragment
312fn fs_shadow(input: ShadowVarying) -> @location(0) vec4<f32> {
313    // Alpha clip first, since we don't have `clip_distance`.
314    if (any(input.clip_distances < vec4<f32>(0.0))) {
315        return vec4<f32>(0.0);
316    }
317
318    let shadow = b_shadows[input.shadow_id];
319    let half_size = shadow.bounds.size / 2.0;
320    let center = shadow.bounds.origin + half_size;
321    let center_to_point = input.position.xy - center;
322
323    let corner_radius = pick_corner_radius(center_to_point, shadow.corner_radii);
324
325    // The signal is only non-zero in a limited range, so don't waste samples
326    let low = center_to_point.y - half_size.y;
327    let high = center_to_point.y + half_size.y;
328    let start = clamp(-3.0 * shadow.blur_radius, low, high);
329    let end = clamp(3.0 * shadow.blur_radius, low, high);
330
331    // Accumulate samples (we can get away with surprisingly few samples)
332    let step = (end - start) / 4.0;
333    var y = start + step * 0.5;
334    var alpha = 0.0;
335    for (var i = 0; i < 4; i += 1) {
336        let blur = blur_along_x(center_to_point.x, center_to_point.y - y,
337            shadow.blur_radius, corner_radius, half_size);
338        alpha +=  blur * gaussian(y, shadow.blur_radius) * step;
339        y += step;
340    }
341
342    return input.color * vec4<f32>(1.0, 1.0, 1.0, alpha);
343}
344
345// --- path rasterization --- //
346
347struct PathVertex {
348    xy_position: vec2<f32>,
349    st_position: vec2<f32>,
350    content_mask: Bounds,
351}
352var<storage, read> b_path_vertices: array<PathVertex>;
353
354struct PathRasterizationVarying {
355    @builtin(position) position: vec4<f32>,
356    @location(0) st_position: vec2<f32>,
357    //TODO: use `clip_distance` once Naga supports it
358    @location(3) clip_distances: vec4<f32>,
359}
360
361@vertex
362fn vs_path_rasterization(@builtin(vertex_index) vertex_id: u32) -> PathRasterizationVarying {
363    let v = b_path_vertices[vertex_id];
364
365    var out = PathRasterizationVarying();
366    out.position = to_device_position_impl(v.xy_position);
367    out.st_position = v.st_position;
368    out.clip_distances = distance_from_clip_rect_impl(v.xy_position, v.content_mask);
369    return out;
370}
371
372@fragment
373fn fs_path_rasterization(input: PathRasterizationVarying) -> @location(0) f32 {
374    let dx = dpdx(input.st_position);
375    let dy = dpdy(input.st_position);
376    if (any(input.clip_distances < vec4<f32>(0.0))) {
377        return 0.0;
378    }
379
380    let gradient = 2.0 * input.st_position.xx * vec2<f32>(dx.x, dy.x) - vec2<f32>(dx.y, dy.y);
381    let f = input.st_position.x * input.st_position.x - input.st_position.y;
382    let distance = f / length(gradient);
383    return saturate(0.5 - distance);
384}
385
386// --- paths --- //
387
388struct PathSprite {
389    bounds: Bounds,
390    color: Hsla,
391    tile: AtlasTile,
392}
393var<storage, read> b_path_sprites: array<PathSprite>;
394
395struct PathVarying {
396    @builtin(position) position: vec4<f32>,
397    @location(0) tile_position: vec2<f32>,
398    @location(1) color: vec4<f32>,
399}
400
401@vertex
402fn vs_path(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> PathVarying {
403    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
404    let sprite = b_path_sprites[instance_id];
405    // Don't apply content mask because it was already accounted for when rasterizing the path.
406
407    var out = PathVarying();
408    out.position = to_device_position(unit_vertex, sprite.bounds);
409    out.tile_position = to_tile_position(unit_vertex, sprite.tile);
410    out.color = hsla_to_rgba(sprite.color);
411    return out;
412}
413
414@fragment
415fn fs_path(input: PathVarying) -> @location(0) vec4<f32> {
416    let sample = textureSample(t_sprite, s_sprite, input.tile_position).r;
417    let mask = 1.0 - abs(1.0 - sample % 2.0);
418    return input.color * mask;
419}
420
421// --- underlines --- //
422
423struct Underline {
424    order: u32,
425    pad: u32,
426    bounds: Bounds,
427    content_mask: Bounds,
428    color: Hsla,
429    thickness: f32,
430    wavy: u32,
431}
432var<storage, read> b_underlines: array<Underline>;
433
434struct UnderlineVarying {
435    @builtin(position) position: vec4<f32>,
436    @location(0) @interpolate(flat) color: vec4<f32>,
437    @location(1) @interpolate(flat) underline_id: u32,
438    //TODO: use `clip_distance` once Naga supports it
439    @location(3) clip_distances: vec4<f32>,
440}
441
442@vertex
443fn vs_underline(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> UnderlineVarying {
444    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
445    let underline = b_underlines[instance_id];
446
447    var out = UnderlineVarying();
448    out.position = to_device_position(unit_vertex, underline.bounds);
449    out.color = hsla_to_rgba(underline.color);
450    out.underline_id = instance_id;
451    out.clip_distances = distance_from_clip_rect(unit_vertex, underline.bounds, underline.content_mask);
452    return out;
453}
454
455@fragment
456fn fs_underline(input: UnderlineVarying) -> @location(0) vec4<f32> {
457    // Alpha clip first, since we don't have `clip_distance`.
458    if (any(input.clip_distances < vec4<f32>(0.0))) {
459        return vec4<f32>(0.0);
460    }
461
462    let underline = b_underlines[input.underline_id];
463    if ((underline.wavy & 0xFFu) == 0u)
464    {
465        return vec4<f32>(0.0);
466    }
467
468    let half_thickness = underline.thickness * 0.5;
469    let st = (input.position.xy - underline.bounds.origin) / underline.bounds.size.y - vec2<f32>(0.0, 0.5);
470    let frequency = M_PI_F * 3.0 * underline.thickness / 8.0;
471    let amplitude = 1.0 / (2.0 * underline.thickness);
472    let sine = sin(st.x * frequency) * amplitude;
473    let dSine = cos(st.x * frequency) * amplitude * frequency;
474    let distance = (st.y - sine) / sqrt(1.0 + dSine * dSine);
475    let distance_in_pixels = distance * underline.bounds.size.y;
476    let distance_from_top_border = distance_in_pixels - half_thickness;
477    let distance_from_bottom_border = distance_in_pixels + half_thickness;
478    let alpha = saturate(0.5 - max(-distance_from_bottom_border, distance_from_top_border));
479    return input.color * vec4<f32>(1.0, 1.0, 1.0, alpha);
480}
481
482// --- monochrome sprites --- //
483
484struct MonochromeSprite {
485    order: u32,
486    pad: u32,
487    bounds: Bounds,
488    content_mask: Bounds,
489    color: Hsla,
490    tile: AtlasTile,
491    transformation: TransformationMatrix,
492}
493var<storage, read> b_mono_sprites: array<MonochromeSprite>;
494
495struct MonoSpriteVarying {
496    @builtin(position) position: vec4<f32>,
497    @location(0) tile_position: vec2<f32>,
498    @location(1) @interpolate(flat) color: vec4<f32>,
499    @location(3) clip_distances: vec4<f32>,
500}
501
502@vertex
503fn vs_mono_sprite(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> MonoSpriteVarying {
504    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
505    let sprite = b_mono_sprites[instance_id];
506
507    var out = MonoSpriteVarying();
508    out.position = to_device_position_transformed(unit_vertex, sprite.bounds, sprite.transformation);
509
510    out.tile_position = to_tile_position(unit_vertex, sprite.tile);
511    out.color = hsla_to_rgba(sprite.color);
512    out.clip_distances = distance_from_clip_rect(unit_vertex, sprite.bounds, sprite.content_mask);
513    return out;
514}
515
516@fragment
517fn fs_mono_sprite(input: MonoSpriteVarying) -> @location(0) vec4<f32> {
518    let sample = textureSample(t_sprite, s_sprite, input.tile_position).r;
519    // Alpha clip after using the derivatives.
520    if (any(input.clip_distances < vec4<f32>(0.0))) {
521        return vec4<f32>(0.0);
522    }
523    return input.color * vec4<f32>(1.0, 1.0, 1.0, sample);
524}
525
526// --- polychrome sprites --- //
527
528struct PolychromeSprite {
529    order: u32,
530    grayscale: u32,
531    bounds: Bounds,
532    content_mask: Bounds,
533    corner_radii: Corners,
534    tile: AtlasTile,
535}
536var<storage, read> b_poly_sprites: array<PolychromeSprite>;
537
538struct PolySpriteVarying {
539    @builtin(position) position: vec4<f32>,
540    @location(0) tile_position: vec2<f32>,
541    @location(1) @interpolate(flat) sprite_id: u32,
542    @location(3) clip_distances: vec4<f32>,
543}
544
545@vertex
546fn vs_poly_sprite(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index) instance_id: u32) -> PolySpriteVarying {
547    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
548    let sprite = b_poly_sprites[instance_id];
549
550    var out = PolySpriteVarying();
551    out.position = to_device_position(unit_vertex, sprite.bounds);
552    out.tile_position = to_tile_position(unit_vertex, sprite.tile);
553    out.sprite_id = instance_id;
554    out.clip_distances = distance_from_clip_rect(unit_vertex, sprite.bounds, sprite.content_mask);
555    return out;
556}
557
558@fragment
559fn fs_poly_sprite(input: PolySpriteVarying) -> @location(0) vec4<f32> {
560    let sample = textureSample(t_sprite, s_sprite, input.tile_position);
561    // Alpha clip after using the derivatives.
562    if (any(input.clip_distances < vec4<f32>(0.0))) {
563        return vec4<f32>(0.0);
564    }
565
566    let sprite = b_poly_sprites[input.sprite_id];
567    let distance = quad_sdf(input.position.xy, sprite.bounds, sprite.corner_radii);
568
569    var color = sample;
570    if ((sprite.grayscale & 0xFFu) != 0u) {
571        let grayscale = dot(color.rgb, GRAYSCALE_FACTORS);
572        color = vec4<f32>(vec3<f32>(grayscale), sample.a);
573    }
574    color.a *= saturate(0.5 - distance);
575    return color;
576}
577
578// --- surfaces --- //
579
580struct SurfaceParams {
581    bounds: Bounds,
582    content_mask: Bounds,
583}
584
585var<uniform> surface_locals: SurfaceParams;
586var t_y: texture_2d<f32>;
587var t_cb_cr: texture_2d<f32>;
588var s_surface: sampler;
589
590const ycbcr_to_RGB = mat4x4<f32>(
591    vec4<f32>( 1.0000f,  1.0000f,  1.0000f, 0.0),
592    vec4<f32>( 0.0000f, -0.3441f,  1.7720f, 0.0),
593    vec4<f32>( 1.4020f, -0.7141f,  0.0000f, 0.0),
594    vec4<f32>(-0.7010f,  0.5291f, -0.8860f, 1.0),
595);
596
597struct SurfaceVarying {
598    @builtin(position) position: vec4<f32>,
599    @location(0) texture_position: vec2<f32>,
600    @location(3) clip_distances: vec4<f32>,
601}
602
603@vertex
604fn vs_surface(@builtin(vertex_index) vertex_id: u32) -> SurfaceVarying {
605    let unit_vertex = vec2<f32>(f32(vertex_id & 1u), 0.5 * f32(vertex_id & 2u));
606
607    var out = SurfaceVarying();
608    out.position = to_device_position(unit_vertex, surface_locals.bounds);
609    out.texture_position = unit_vertex;
610    out.clip_distances = distance_from_clip_rect(unit_vertex, surface_locals.bounds, surface_locals.content_mask);
611    return out;
612}
613
614@fragment
615fn fs_surface(input: SurfaceVarying) -> @location(0) vec4<f32> {
616    // Alpha clip after using the derivatives.
617    if (any(input.clip_distances < vec4<f32>(0.0))) {
618        return vec4<f32>(0.0);
619    }
620
621    let y_cb_cr = vec4<f32>(
622        textureSampleLevel(t_y, s_surface, input.texture_position, 0.0).r,
623        textureSampleLevel(t_cb_cr, s_surface, input.texture_position, 0.0).rg,
624        1.0);
625
626    return ycbcr_to_RGB * y_cb_cr;
627}