shaders.wgsl

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