shaders.metal

   1#include <metal_stdlib>
   2#include <simd/simd.h>
   3
   4using namespace metal;
   5
   6float4 hsla_to_rgba(Hsla hsla);
   7float3 srgb_to_linear(float3 color);
   8float3 linear_to_srgb(float3 color);
   9float4 srgb_to_oklab(float4 color);
  10float4 oklab_to_srgb(float4 color);
  11float4 to_device_position(float2 unit_vertex, Bounds_ScaledPixels bounds,
  12                          constant Size_DevicePixels *viewport_size);
  13float4 to_device_position_transformed(float2 unit_vertex, Bounds_ScaledPixels bounds,
  14                          TransformationMatrix transformation,
  15                          constant Size_DevicePixels *input_viewport_size);
  16
  17float2 to_tile_position(float2 unit_vertex, AtlasTile tile,
  18                        constant Size_DevicePixels *atlas_size);
  19float4 distance_from_clip_rect(float2 unit_vertex, Bounds_ScaledPixels bounds,
  20                               Bounds_ScaledPixels clip_bounds);
  21float corner_dash_velocity(float dv1, float dv2);
  22float dash_alpha(float t, float period, float length, float dash_velocity,
  23                 float antialias_threshold);
  24float quarter_ellipse_sdf(float2 point, float2 radii);
  25float pick_corner_radius(float2 center_to_point, Corners_ScaledPixels corner_radii);
  26float quad_sdf(float2 point, Bounds_ScaledPixels bounds,
  27               Corners_ScaledPixels corner_radii);
  28float quad_sdf_impl(float2 center_to_point, float corner_radius);
  29float gaussian(float x, float sigma);
  30float2 erf(float2 x);
  31float blur_along_x(float x, float y, float sigma, float corner,
  32                   float2 half_size);
  33float4 over(float4 below, float4 above);
  34float radians(float degrees);
  35float4 fill_color(Background background, float2 position, Bounds_ScaledPixels bounds,
  36  float4 solid_color, float4 color0, float4 color1);
  37
  38struct GradientColor {
  39  float4 solid;
  40  float4 color0;
  41  float4 color1;
  42};
  43GradientColor prepare_fill_color(uint tag, uint color_space, Hsla solid, Hsla color0, Hsla color1);
  44
  45struct QuadVertexOutput {
  46  uint quad_id [[flat]];
  47  float4 position [[position]];
  48  float4 border_color [[flat]];
  49  float4 background_solid [[flat]];
  50  float4 background_color0 [[flat]];
  51  float4 background_color1 [[flat]];
  52  float clip_distance [[clip_distance]][4];
  53};
  54
  55struct QuadFragmentInput {
  56  uint quad_id [[flat]];
  57  float4 position [[position]];
  58  float4 border_color [[flat]];
  59  float4 background_solid [[flat]];
  60  float4 background_color0 [[flat]];
  61  float4 background_color1 [[flat]];
  62};
  63
  64vertex QuadVertexOutput quad_vertex(uint unit_vertex_id [[vertex_id]],
  65                                    uint quad_id [[instance_id]],
  66                                    constant float2 *unit_vertices
  67                                    [[buffer(QuadInputIndex_Vertices)]],
  68                                    constant Quad *quads
  69                                    [[buffer(QuadInputIndex_Quads)]],
  70                                    constant Size_DevicePixels *viewport_size
  71                                    [[buffer(QuadInputIndex_ViewportSize)]]) {
  72  float2 unit_vertex = unit_vertices[unit_vertex_id];
  73  Quad quad = quads[quad_id];
  74  float4 device_position =
  75      to_device_position(unit_vertex, quad.bounds, viewport_size);
  76  float4 clip_distance = distance_from_clip_rect(unit_vertex, quad.bounds,
  77                                                 quad.content_mask.bounds);
  78  float4 border_color = hsla_to_rgba(quad.border_color);
  79
  80  GradientColor gradient = prepare_fill_color(
  81    quad.background.tag,
  82    quad.background.color_space,
  83    quad.background.solid,
  84    quad.background.colors[0].color,
  85    quad.background.colors[1].color
  86  );
  87
  88  return QuadVertexOutput{
  89      quad_id,
  90      device_position,
  91      border_color,
  92      gradient.solid,
  93      gradient.color0,
  94      gradient.color1,
  95      {clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
  96}
  97
  98fragment float4 quad_fragment(QuadFragmentInput input [[stage_in]],
  99                              constant Quad *quads
 100                              [[buffer(QuadInputIndex_Quads)]]) {
 101  Quad quad = quads[input.quad_id];
 102
 103  // Signed distance field threshold for inclusion of pixels. 0.5 is the
 104  // minimum distance between the center of the pixel and the edge.
 105  const float antialias_threshold = 0.5;
 106
 107  float4 background_color = fill_color(quad.background, input.position.xy, quad.bounds,
 108    input.background_solid, input.background_color0, input.background_color1);
 109  float4 border_color = input.border_color;
 110
 111  // Apply content_mask corner radii clipping
 112  float clip_sdf = quad_sdf(input.position.xy, quad.content_mask.bounds,
 113    quad.content_mask.corner_radii);
 114  float clip_alpha = saturate(antialias_threshold - clip_sdf);
 115  background_color.a *= clip_alpha;
 116  border_color *= clip_alpha;
 117
 118  bool unrounded = quad.corner_radii.top_left == 0.0 &&
 119    quad.corner_radii.bottom_left == 0.0 &&
 120    quad.corner_radii.top_right == 0.0 &&
 121    quad.corner_radii.bottom_right == 0.0;
 122
 123  // Fast path when the quad is not rounded and doesn't have any border
 124  if (quad.border_widths.top == 0.0 &&
 125      quad.border_widths.left == 0.0 &&
 126      quad.border_widths.right == 0.0 &&
 127      quad.border_widths.bottom == 0.0 &&
 128      unrounded) {
 129    return background_color;
 130  }
 131
 132  float2 size = float2(quad.bounds.size.width, quad.bounds.size.height);
 133  float2 half_size = size / 2.0;
 134  float2 point = input.position.xy - float2(quad.bounds.origin.x, quad.bounds.origin.y);
 135  float2 center_to_point = point - half_size;
 136
 137  // Radius of the nearest corner
 138  float corner_radius = pick_corner_radius(center_to_point, quad.corner_radii);
 139
 140  // Width of the nearest borders
 141  float2 border = float2(
 142    center_to_point.x < 0.0 ? quad.border_widths.left : quad.border_widths.right,
 143    center_to_point.y < 0.0 ? quad.border_widths.top : quad.border_widths.bottom
 144  );
 145
 146  // 0-width borders are reduced so that `inner_sdf >= antialias_threshold`.
 147  // The purpose of this is to not draw antialiasing pixels in this case.
 148  float2 reduced_border = float2(
 149    border.x == 0.0 ? -antialias_threshold : border.x,
 150    border.y == 0.0 ? -antialias_threshold : border.y);
 151
 152  // Vector from the corner of the quad bounds to the point, after mirroring
 153  // the point into the bottom right quadrant. Both components are <= 0.
 154  float2 corner_to_point = fabs(center_to_point) - half_size;
 155
 156  // Vector from the point to the center of the rounded corner's circle, also
 157  // mirrored into bottom right quadrant.
 158  float2 corner_center_to_point = corner_to_point + corner_radius;
 159
 160  // Whether the nearest point on the border is rounded
 161  bool is_near_rounded_corner =
 162    corner_center_to_point.x >= 0.0 &&
 163    corner_center_to_point.y >= 0.0;
 164
 165  // Vector from straight border inner corner to point.
 166  //
 167  // 0-width borders are turned into width -1 so that inner_sdf is > 1.0 near
 168  // the border. Without this, antialiasing pixels would be drawn.
 169  float2 straight_border_inner_corner_to_point = corner_to_point + reduced_border;
 170
 171  // Whether the point is beyond the inner edge of the straight border
 172  bool is_beyond_inner_straight_border =
 173    straight_border_inner_corner_to_point.x > 0.0 ||
 174    straight_border_inner_corner_to_point.y > 0.0;
 175
 176  // Whether the point is far enough inside the quad, such that the pixels are
 177  // not affected by the straight border.
 178  bool is_within_inner_straight_border =
 179    straight_border_inner_corner_to_point.x < -antialias_threshold &&
 180    straight_border_inner_corner_to_point.y < -antialias_threshold;
 181
 182  // Fast path for points that must be part of the background
 183  if (is_within_inner_straight_border && !is_near_rounded_corner) {
 184    return background_color;
 185  }
 186
 187  // Signed distance of the point to the outside edge of the quad's border
 188  float outer_sdf = quad_sdf_impl(corner_center_to_point, corner_radius);
 189
 190  // Approximate signed distance of the point to the inside edge of the quad's
 191  // border. It is negative outside this edge (within the border), and
 192  // positive inside.
 193  //
 194  // This is not always an accurate signed distance:
 195  // * The rounded portions with varying border width use an approximation of
 196  //   nearest-point-on-ellipse.
 197  // * When it is quickly known to be outside the edge, -1.0 is used.
 198  float inner_sdf = 0.0;
 199  if (corner_center_to_point.x <= 0.0 || corner_center_to_point.y <= 0.0) {
 200    // Fast paths for straight borders
 201    inner_sdf = -max(straight_border_inner_corner_to_point.x,
 202                     straight_border_inner_corner_to_point.y);
 203  } else if (is_beyond_inner_straight_border) {
 204    // Fast path for points that must be outside the inner edge
 205    inner_sdf = -1.0;
 206  } else if (reduced_border.x == reduced_border.y) {
 207    // Fast path for circular inner edge.
 208    inner_sdf = -(outer_sdf + reduced_border.x);
 209  } else {
 210    float2 ellipse_radii = max(float2(0.0), float2(corner_radius) - reduced_border);
 211    inner_sdf = quarter_ellipse_sdf(corner_center_to_point, ellipse_radii);
 212  }
 213
 214  // Negative when inside the border
 215  float border_sdf = max(inner_sdf, outer_sdf);
 216
 217  float4 color = background_color;
 218  if (border_sdf < antialias_threshold) {
 219    // Dashed border logic when border_style == 1
 220    if (quad.border_style == 1) {
 221      // Position along the perimeter in "dash space", where each dash
 222      // period has length 1
 223      float t = 0.0;
 224
 225      // Total number of dash periods, so that the dash spacing can be
 226      // adjusted to evenly divide it
 227      float max_t = 0.0;
 228
 229      // Border width is proportional to dash size. This is the behavior
 230      // used by browsers, but also avoids dashes from different segments
 231      // overlapping when dash size is smaller than the border width.
 232      //
 233      // Dash pattern: (2 * border width) dash, (1 * border width) gap
 234      const float dash_length_per_width = 2.0;
 235      const float dash_gap_per_width = 1.0;
 236      const float dash_period_per_width = dash_length_per_width + dash_gap_per_width;
 237
 238      // Since the dash size is determined by border width, the density of
 239      // dashes varies. Multiplying a pixel distance by this returns a
 240      // position in dash space - it has units (dash period / pixels). So
 241      // a dash velocity of (1 / 10) is 1 dash every 10 pixels.
 242      float dash_velocity = 0.0;
 243
 244      // Dividing this by the border width gives the dash velocity
 245      const float dv_numerator = 1.0 / dash_period_per_width;
 246
 247      if (unrounded) {
 248        // When corners aren't rounded, the dashes are separately laid
 249        // out on each straight line, rather than around the whole
 250        // perimeter. This way each line starts and ends with a dash.
 251        bool is_horizontal = corner_center_to_point.x < corner_center_to_point.y;
 252        float border_width = is_horizontal ? border.x : border.y;
 253        // When border width of some side is 0, we need to use the other side width for dash velocity.
 254        if (border_width == 0.0) {
 255            border_width = is_horizontal ? border.y : border.x;
 256        }
 257        dash_velocity = dv_numerator / border_width;
 258        t = is_horizontal ? point.x : point.y;
 259        t *= dash_velocity;
 260        max_t = is_horizontal ? size.x : size.y;
 261        max_t *= dash_velocity;
 262      } else {
 263        // When corners are rounded, the dashes are laid out clockwise
 264        // around the whole perimeter.
 265
 266        float r_tr = quad.corner_radii.top_right;
 267        float r_br = quad.corner_radii.bottom_right;
 268        float r_bl = quad.corner_radii.bottom_left;
 269        float r_tl = quad.corner_radii.top_left;
 270
 271        float w_t = quad.border_widths.top;
 272        float w_r = quad.border_widths.right;
 273        float w_b = quad.border_widths.bottom;
 274        float w_l = quad.border_widths.left;
 275
 276        // Straight side dash velocities
 277        float dv_t = w_t <= 0.0 ? 0.0 : dv_numerator / w_t;
 278        float dv_r = w_r <= 0.0 ? 0.0 : dv_numerator / w_r;
 279        float dv_b = w_b <= 0.0 ? 0.0 : dv_numerator / w_b;
 280        float dv_l = w_l <= 0.0 ? 0.0 : dv_numerator / w_l;
 281
 282        // Straight side lengths in dash space
 283        float s_t = (size.x - r_tl - r_tr) * dv_t;
 284        float s_r = (size.y - r_tr - r_br) * dv_r;
 285        float s_b = (size.x - r_br - r_bl) * dv_b;
 286        float s_l = (size.y - r_bl - r_tl) * dv_l;
 287
 288        float corner_dash_velocity_tr = corner_dash_velocity(dv_t, dv_r);
 289        float corner_dash_velocity_br = corner_dash_velocity(dv_b, dv_r);
 290        float corner_dash_velocity_bl = corner_dash_velocity(dv_b, dv_l);
 291        float corner_dash_velocity_tl = corner_dash_velocity(dv_t, dv_l);
 292
 293        // Corner lengths in dash space
 294        float c_tr = r_tr * (M_PI_F / 2.0) * corner_dash_velocity_tr;
 295        float c_br = r_br * (M_PI_F / 2.0) * corner_dash_velocity_br;
 296        float c_bl = r_bl * (M_PI_F / 2.0) * corner_dash_velocity_bl;
 297        float c_tl = r_tl * (M_PI_F / 2.0) * corner_dash_velocity_tl;
 298
 299        // Cumulative dash space upto each segment
 300        float upto_tr = s_t;
 301        float upto_r = upto_tr + c_tr;
 302        float upto_br = upto_r + s_r;
 303        float upto_b = upto_br + c_br;
 304        float upto_bl = upto_b + s_b;
 305        float upto_l = upto_bl + c_bl;
 306        float upto_tl = upto_l + s_l;
 307        max_t = upto_tl + c_tl;
 308
 309        if (is_near_rounded_corner) {
 310          float radians = atan2(corner_center_to_point.y, corner_center_to_point.x);
 311          float corner_t = radians * corner_radius;
 312
 313          if (center_to_point.x >= 0.0) {
 314            if (center_to_point.y < 0.0) {
 315              dash_velocity = corner_dash_velocity_tr;
 316              // Subtracted because radians is pi/2 to 0 when
 317              // going clockwise around the top right corner,
 318              // since the y axis has been flipped
 319              t = upto_r - corner_t * dash_velocity;
 320            } else {
 321              dash_velocity = corner_dash_velocity_br;
 322              // Added because radians is 0 to pi/2 when going
 323              // clockwise around the bottom-right corner
 324              t = upto_br + corner_t * dash_velocity;
 325            }
 326          } else {
 327            if (center_to_point.y >= 0.0) {
 328              dash_velocity = corner_dash_velocity_bl;
 329              // Subtracted because radians is pi/1 to 0 when
 330              // going clockwise around the bottom-left corner,
 331              // since the x axis has been flipped
 332              t = upto_l - corner_t * dash_velocity;
 333            } else {
 334              dash_velocity = corner_dash_velocity_tl;
 335              // Added because radians is 0 to pi/2 when going
 336              // clockwise around the top-left corner, since both
 337              // axis were flipped
 338              t = upto_tl + corner_t * dash_velocity;
 339            }
 340          }
 341        } else {
 342          // Straight borders
 343          bool is_horizontal = corner_center_to_point.x < corner_center_to_point.y;
 344          if (is_horizontal) {
 345            if (center_to_point.y < 0.0) {
 346              dash_velocity = dv_t;
 347              t = (point.x - r_tl) * dash_velocity;
 348            } else {
 349              dash_velocity = dv_b;
 350              t = upto_bl - (point.x - r_bl) * dash_velocity;
 351            }
 352          } else {
 353            if (center_to_point.x < 0.0) {
 354              dash_velocity = dv_l;
 355              t = upto_tl - (point.y - r_tl) * dash_velocity;
 356            } else {
 357              dash_velocity = dv_r;
 358              t = upto_r + (point.y - r_tr) * dash_velocity;
 359            }
 360          }
 361        }
 362      }
 363
 364      float dash_length = dash_length_per_width / dash_period_per_width;
 365      float desired_dash_gap = dash_gap_per_width / dash_period_per_width;
 366
 367      // Straight borders should start and end with a dash, so max_t is
 368      // reduced to cause this.
 369      max_t -= unrounded ? dash_length : 0.0;
 370      if (max_t >= 1.0) {
 371        // Adjust dash gap to evenly divide max_t
 372        float dash_count = floor(max_t);
 373        float dash_period = max_t / dash_count;
 374        border_color.a *= dash_alpha(t, dash_period, dash_length, dash_velocity,
 375                                     antialias_threshold);
 376      } else if (unrounded) {
 377        // When there isn't enough space for the full gap between the
 378        // two start / end dashes of a straight border, reduce gap to
 379        // make them fit.
 380        float dash_gap = max_t - dash_length;
 381        if (dash_gap > 0.0) {
 382          float dash_period = dash_length + dash_gap;
 383          border_color.a *= dash_alpha(t, dash_period, dash_length, dash_velocity,
 384                                       antialias_threshold);
 385        }
 386      }
 387    }
 388
 389    // Blend the border on top of the background and then linearly interpolate
 390    // between the two as we slide inside the background.
 391    float4 blended_border = over(background_color, border_color);
 392    color = mix(background_color, blended_border,
 393                saturate(antialias_threshold - inner_sdf));
 394  }
 395
 396  return color * float4(1.0, 1.0, 1.0, saturate(antialias_threshold - outer_sdf));
 397}
 398
 399// Returns the dash velocity of a corner given the dash velocity of the two
 400// sides, by returning the slower velocity (larger dashes).
 401//
 402// Since 0 is used for dash velocity when the border width is 0 (instead of
 403// +inf), this returns the other dash velocity in that case.
 404//
 405// An alternative to this might be to appropriately interpolate the dash
 406// velocity around the corner, but that seems overcomplicated.
 407float corner_dash_velocity(float dv1, float dv2) {
 408  if (dv1 == 0.0) {
 409    return dv2;
 410  } else if (dv2 == 0.0) {
 411    return dv1;
 412  } else {
 413    return min(dv1, dv2);
 414  }
 415}
 416
 417// Returns alpha used to render antialiased dashes.
 418// `t` is within the dash when `fmod(t, period) < length`.
 419float dash_alpha(
 420    float t, float period, float length, float dash_velocity,
 421    float antialias_threshold) {
 422  float half_period = period / 2.0;
 423  float half_length = length / 2.0;
 424  // Value in [-half_period, half_period]
 425  // The dash is in [-half_length, half_length]
 426  float centered = fmod(t + half_period - half_length, period) - half_period;
 427  // Signed distance for the dash, negative values are inside the dash
 428  float signed_distance = abs(centered) - half_length;
 429  // Antialiased alpha based on the signed distance
 430  return saturate(antialias_threshold - signed_distance / dash_velocity);
 431}
 432
 433// This approximates distance to the nearest point to a quarter ellipse in a way
 434// that is sufficient for anti-aliasing when the ellipse is not very eccentric.
 435// The components of `point` are expected to be positive.
 436//
 437// Negative on the outside and positive on the inside.
 438float quarter_ellipse_sdf(float2 point, float2 radii) {
 439  // Scale the space to treat the ellipse like a unit circle
 440  float2 circle_vec = point / radii;
 441  float unit_circle_sdf = length(circle_vec) - 1.0;
 442  // Approximate up-scaling of the length by using the average of the radii.
 443  //
 444  // TODO: A better solution would be to use the gradient of the implicit
 445  // function for an ellipse to approximate a scaling factor.
 446  return unit_circle_sdf * (radii.x + radii.y) * -0.5;
 447}
 448
 449struct ShadowVertexOutput {
 450  float4 position [[position]];
 451  float4 color [[flat]];
 452  uint shadow_id [[flat]];
 453  float clip_distance [[clip_distance]][4];
 454};
 455
 456struct ShadowFragmentInput {
 457  float4 position [[position]];
 458  float4 color [[flat]];
 459  uint shadow_id [[flat]];
 460};
 461
 462vertex ShadowVertexOutput shadow_vertex(
 463    uint unit_vertex_id [[vertex_id]], uint shadow_id [[instance_id]],
 464    constant float2 *unit_vertices [[buffer(ShadowInputIndex_Vertices)]],
 465    constant Shadow *shadows [[buffer(ShadowInputIndex_Shadows)]],
 466    constant Size_DevicePixels *viewport_size
 467    [[buffer(ShadowInputIndex_ViewportSize)]]) {
 468  float2 unit_vertex = unit_vertices[unit_vertex_id];
 469  Shadow shadow = shadows[shadow_id];
 470
 471  float margin = 3. * shadow.blur_radius;
 472  // Set the bounds of the shadow and adjust its size based on the shadow's
 473  // spread radius to achieve the spreading effect
 474  Bounds_ScaledPixels bounds = shadow.bounds;
 475  bounds.origin.x -= margin;
 476  bounds.origin.y -= margin;
 477  bounds.size.width += 2. * margin;
 478  bounds.size.height += 2. * margin;
 479
 480  float4 device_position =
 481      to_device_position(unit_vertex, bounds, viewport_size);
 482  float4 clip_distance =
 483      distance_from_clip_rect(unit_vertex, bounds, shadow.content_mask.bounds);
 484  float4 color = hsla_to_rgba(shadow.color);
 485
 486  return ShadowVertexOutput{
 487      device_position,
 488      color,
 489      shadow_id,
 490      {clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
 491}
 492
 493fragment float4 shadow_fragment(ShadowFragmentInput input [[stage_in]],
 494                                constant Shadow *shadows
 495                                [[buffer(ShadowInputIndex_Shadows)]]) {
 496  Shadow shadow = shadows[input.shadow_id];
 497
 498  float2 origin = float2(shadow.bounds.origin.x, shadow.bounds.origin.y);
 499  float2 size = float2(shadow.bounds.size.width, shadow.bounds.size.height);
 500  float2 half_size = size / 2.;
 501  float2 center = origin + half_size;
 502  float2 point = input.position.xy - center;
 503  float corner_radius;
 504  if (point.x < 0.) {
 505    if (point.y < 0.) {
 506      corner_radius = shadow.corner_radii.top_left;
 507    } else {
 508      corner_radius = shadow.corner_radii.bottom_left;
 509    }
 510  } else {
 511    if (point.y < 0.) {
 512      corner_radius = shadow.corner_radii.top_right;
 513    } else {
 514      corner_radius = shadow.corner_radii.bottom_right;
 515    }
 516  }
 517
 518  float alpha;
 519  if (shadow.blur_radius == 0.) {
 520    float distance = quad_sdf(input.position.xy, shadow.bounds, shadow.corner_radii);
 521    alpha = saturate(0.5 - distance);
 522  } else {
 523    // The signal is only non-zero in a limited range, so don't waste samples
 524    float low = point.y - half_size.y;
 525    float high = point.y + half_size.y;
 526    float start = clamp(-3. * shadow.blur_radius, low, high);
 527    float end = clamp(3. * shadow.blur_radius, low, high);
 528
 529    // Accumulate samples (we can get away with surprisingly few samples)
 530    float step = (end - start) / 4.;
 531    float y = start + step * 0.5;
 532    alpha = 0.;
 533    for (int i = 0; i < 4; i++) {
 534      alpha += blur_along_x(point.x, point.y - y, shadow.blur_radius,
 535                            corner_radius, half_size) *
 536               gaussian(y, shadow.blur_radius) * step;
 537      y += step;
 538    }
 539  }
 540
 541  return input.color * float4(1., 1., 1., alpha);
 542}
 543
 544struct UnderlineVertexOutput {
 545  float4 position [[position]];
 546  float4 color [[flat]];
 547  uint underline_id [[flat]];
 548  float clip_distance [[clip_distance]][4];
 549};
 550
 551struct UnderlineFragmentInput {
 552  float4 position [[position]];
 553  float4 color [[flat]];
 554  uint underline_id [[flat]];
 555};
 556
 557vertex UnderlineVertexOutput underline_vertex(
 558    uint unit_vertex_id [[vertex_id]], uint underline_id [[instance_id]],
 559    constant float2 *unit_vertices [[buffer(UnderlineInputIndex_Vertices)]],
 560    constant Underline *underlines [[buffer(UnderlineInputIndex_Underlines)]],
 561    constant Size_DevicePixels *viewport_size
 562    [[buffer(ShadowInputIndex_ViewportSize)]]) {
 563  float2 unit_vertex = unit_vertices[unit_vertex_id];
 564  Underline underline = underlines[underline_id];
 565  float4 device_position =
 566      to_device_position(unit_vertex, underline.bounds, viewport_size);
 567  float4 clip_distance = distance_from_clip_rect(unit_vertex, underline.bounds,
 568                                                 underline.content_mask.bounds);
 569  float4 color = hsla_to_rgba(underline.color);
 570  return UnderlineVertexOutput{
 571      device_position,
 572      color,
 573      underline_id,
 574      {clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
 575}
 576
 577fragment float4 underline_fragment(UnderlineFragmentInput input [[stage_in]],
 578                                   constant Underline *underlines
 579                                   [[buffer(UnderlineInputIndex_Underlines)]]) {
 580  const float WAVE_FREQUENCY = 2.0;
 581  const float WAVE_HEIGHT_RATIO = 0.8;
 582
 583  Underline underline = underlines[input.underline_id];
 584  if (underline.wavy) {
 585    float half_thickness = underline.thickness * 0.5;
 586    float2 origin =
 587        float2(underline.bounds.origin.x, underline.bounds.origin.y);
 588
 589    float2 st = ((input.position.xy - origin) / underline.bounds.size.height) -
 590                float2(0., 0.5);
 591    float frequency = (M_PI_F * WAVE_FREQUENCY * underline.thickness) / underline.bounds.size.height;
 592    float amplitude = (underline.thickness * WAVE_HEIGHT_RATIO) / underline.bounds.size.height;
 593
 594    float sine = sin(st.x * frequency) * amplitude;
 595    float dSine = cos(st.x * frequency) * amplitude * frequency;
 596    float distance = (st.y - sine) / sqrt(1. + dSine * dSine);
 597    float distance_in_pixels = distance * underline.bounds.size.height;
 598    float distance_from_top_border = distance_in_pixels - half_thickness;
 599    float distance_from_bottom_border = distance_in_pixels + half_thickness;
 600    float alpha = saturate(
 601        0.5 - max(-distance_from_bottom_border, distance_from_top_border));
 602    return input.color * float4(1., 1., 1., alpha);
 603  } else {
 604    return input.color;
 605  }
 606}
 607
 608struct MonochromeSpriteVertexOutput {
 609  float4 position [[position]];
 610  float2 tile_position;
 611  float4 color [[flat]];
 612  float clip_distance [[clip_distance]][4];
 613};
 614
 615struct MonochromeSpriteFragmentInput {
 616  float4 position [[position]];
 617  float2 tile_position;
 618  float4 color [[flat]];
 619};
 620
 621vertex MonochromeSpriteVertexOutput monochrome_sprite_vertex(
 622    uint unit_vertex_id [[vertex_id]], uint sprite_id [[instance_id]],
 623    constant float2 *unit_vertices [[buffer(SpriteInputIndex_Vertices)]],
 624    constant MonochromeSprite *sprites [[buffer(SpriteInputIndex_Sprites)]],
 625    constant Size_DevicePixels *viewport_size
 626    [[buffer(SpriteInputIndex_ViewportSize)]],
 627    constant Size_DevicePixels *atlas_size
 628    [[buffer(SpriteInputIndex_AtlasTextureSize)]]) {
 629  float2 unit_vertex = unit_vertices[unit_vertex_id];
 630  MonochromeSprite sprite = sprites[sprite_id];
 631  float4 device_position =
 632      to_device_position_transformed(unit_vertex, sprite.bounds, sprite.transformation, viewport_size);
 633  float4 clip_distance = distance_from_clip_rect(unit_vertex, sprite.bounds,
 634                                                 sprite.content_mask.bounds);
 635  float2 tile_position = to_tile_position(unit_vertex, sprite.tile, atlas_size);
 636  float4 color = hsla_to_rgba(sprite.color);
 637  return MonochromeSpriteVertexOutput{
 638      device_position,
 639      tile_position,
 640      color,
 641      {clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
 642}
 643
 644fragment float4 monochrome_sprite_fragment(
 645    MonochromeSpriteFragmentInput input [[stage_in]],
 646    constant MonochromeSprite *sprites [[buffer(SpriteInputIndex_Sprites)]],
 647    texture2d<float> atlas_texture [[texture(SpriteInputIndex_AtlasTexture)]]) {
 648  constexpr sampler atlas_texture_sampler(mag_filter::linear,
 649                                          min_filter::linear);
 650  float4 sample =
 651      atlas_texture.sample(atlas_texture_sampler, input.tile_position);
 652  float4 color = input.color;
 653  color.a *= sample.a;
 654  return color;
 655}
 656
 657struct PolychromeSpriteVertexOutput {
 658  float4 position [[position]];
 659  float2 tile_position;
 660  uint sprite_id [[flat]];
 661  float clip_distance [[clip_distance]][4];
 662};
 663
 664struct PolychromeSpriteFragmentInput {
 665  float4 position [[position]];
 666  float2 tile_position;
 667  uint sprite_id [[flat]];
 668};
 669
 670vertex PolychromeSpriteVertexOutput polychrome_sprite_vertex(
 671    uint unit_vertex_id [[vertex_id]], uint sprite_id [[instance_id]],
 672    constant float2 *unit_vertices [[buffer(SpriteInputIndex_Vertices)]],
 673    constant PolychromeSprite *sprites [[buffer(SpriteInputIndex_Sprites)]],
 674    constant Size_DevicePixels *viewport_size
 675    [[buffer(SpriteInputIndex_ViewportSize)]],
 676    constant Size_DevicePixels *atlas_size
 677    [[buffer(SpriteInputIndex_AtlasTextureSize)]]) {
 678
 679  float2 unit_vertex = unit_vertices[unit_vertex_id];
 680  PolychromeSprite sprite = sprites[sprite_id];
 681  float4 device_position =
 682      to_device_position(unit_vertex, sprite.bounds, viewport_size);
 683  float4 clip_distance = distance_from_clip_rect(unit_vertex, sprite.bounds,
 684                                                 sprite.content_mask.bounds);
 685  float2 tile_position = to_tile_position(unit_vertex, sprite.tile, atlas_size);
 686  return PolychromeSpriteVertexOutput{
 687      device_position,
 688      tile_position,
 689      sprite_id,
 690      {clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
 691}
 692
 693fragment float4 polychrome_sprite_fragment(
 694    PolychromeSpriteFragmentInput input [[stage_in]],
 695    constant PolychromeSprite *sprites [[buffer(SpriteInputIndex_Sprites)]],
 696    texture2d<float> atlas_texture [[texture(SpriteInputIndex_AtlasTexture)]]) {
 697  PolychromeSprite sprite = sprites[input.sprite_id];
 698  constexpr sampler atlas_texture_sampler(mag_filter::linear,
 699                                          min_filter::linear);
 700  float4 sample =
 701      atlas_texture.sample(atlas_texture_sampler, input.tile_position);
 702  float distance =
 703      quad_sdf(input.position.xy, sprite.bounds, sprite.corner_radii);
 704
 705  float4 color = sample;
 706  if (sprite.grayscale) {
 707    float grayscale = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
 708    color.r = grayscale;
 709    color.g = grayscale;
 710    color.b = grayscale;
 711  }
 712  color.a *= sprite.opacity * saturate(0.5 - distance);
 713  return color;
 714}
 715
 716struct PathRasterizationVertexOutput {
 717  float4 position [[position]];
 718  float2 st_position;
 719  uint vertex_id [[flat]];
 720  float clip_rect_distance [[clip_distance]][4];
 721};
 722
 723struct PathRasterizationFragmentInput {
 724  float4 position [[position]];
 725  float2 st_position;
 726  uint vertex_id [[flat]];
 727};
 728
 729vertex PathRasterizationVertexOutput path_rasterization_vertex(
 730  uint vertex_id [[vertex_id]],
 731  constant PathRasterizationVertex *vertices [[buffer(PathRasterizationInputIndex_Vertices)]],
 732  constant Size_DevicePixels *atlas_size [[buffer(PathRasterizationInputIndex_ViewportSize)]]
 733) {
 734  PathRasterizationVertex v = vertices[vertex_id];
 735  float2 vertex_position = float2(v.xy_position.x, v.xy_position.y);
 736  float4 position = float4(
 737    vertex_position * float2(2. / atlas_size->width, -2. / atlas_size->height) + float2(-1., 1.),
 738    0.,
 739    1.
 740  );
 741  return PathRasterizationVertexOutput{
 742      position,
 743      float2(v.st_position.x, v.st_position.y),
 744      vertex_id,
 745      {
 746        v.xy_position.x - v.bounds.origin.x,
 747        v.bounds.origin.x + v.bounds.size.width - v.xy_position.x,
 748        v.xy_position.y - v.bounds.origin.y,
 749        v.bounds.origin.y + v.bounds.size.height - v.xy_position.y
 750      }
 751  };
 752}
 753
 754fragment float4 path_rasterization_fragment(
 755  PathRasterizationFragmentInput input [[stage_in]],
 756  constant PathRasterizationVertex *vertices [[buffer(PathRasterizationInputIndex_Vertices)]]
 757) {
 758  float2 dx = dfdx(input.st_position);
 759  float2 dy = dfdy(input.st_position);
 760
 761  PathRasterizationVertex v = vertices[input.vertex_id];
 762  Background background = v.color;
 763  Bounds_ScaledPixels path_bounds = v.bounds;
 764  float alpha;
 765  if (length(float2(dx.x, dy.x)) < 0.001) {
 766    alpha = 1.0;
 767  } else {
 768    float2 gradient = float2(
 769      (2. * input.st_position.x) * dx.x - dx.y,
 770      (2. * input.st_position.x) * dy.x - dy.y
 771    );
 772    float f = (input.st_position.x * input.st_position.x) - input.st_position.y;
 773    float distance = f / length(gradient);
 774    alpha = saturate(0.5 - distance);
 775  }
 776
 777  GradientColor gradient_color = prepare_fill_color(
 778    background.tag,
 779    background.color_space,
 780    background.solid,
 781    background.colors[0].color,
 782    background.colors[1].color
 783  );
 784
 785  float4 color = fill_color(
 786    background,
 787    input.position.xy,
 788    path_bounds,
 789    gradient_color.solid,
 790    gradient_color.color0,
 791    gradient_color.color1
 792  );
 793  return float4(color.rgb * color.a * alpha, alpha * color.a);
 794}
 795
 796struct PathSpriteVertexOutput {
 797  float4 position [[position]];
 798  float2 texture_coords;
 799};
 800
 801vertex PathSpriteVertexOutput path_sprite_vertex(
 802  uint unit_vertex_id [[vertex_id]],
 803  uint sprite_id [[instance_id]],
 804  constant float2 *unit_vertices [[buffer(SpriteInputIndex_Vertices)]],
 805  constant PathSprite *sprites [[buffer(SpriteInputIndex_Sprites)]],
 806  constant Size_DevicePixels *viewport_size [[buffer(SpriteInputIndex_ViewportSize)]]
 807) {
 808  float2 unit_vertex = unit_vertices[unit_vertex_id];
 809  PathSprite sprite = sprites[sprite_id];
 810  // Don't apply content mask because it was already accounted for when
 811  // rasterizing the path.
 812  float4 device_position =
 813      to_device_position(unit_vertex, sprite.bounds, viewport_size);
 814
 815  float2 screen_position = float2(sprite.bounds.origin.x, sprite.bounds.origin.y) + unit_vertex * float2(sprite.bounds.size.width, sprite.bounds.size.height);
 816  float2 texture_coords = screen_position / float2(viewport_size->width, viewport_size->height);
 817
 818  return PathSpriteVertexOutput{
 819    device_position,
 820    texture_coords
 821  };
 822}
 823
 824fragment float4 path_sprite_fragment(
 825  PathSpriteVertexOutput input [[stage_in]],
 826  texture2d<float> intermediate_texture [[texture(SpriteInputIndex_AtlasTexture)]]
 827) {
 828  constexpr sampler intermediate_texture_sampler(mag_filter::linear, min_filter::linear);
 829  return intermediate_texture.sample(intermediate_texture_sampler, input.texture_coords);
 830}
 831
 832struct SurfaceVertexOutput {
 833  float4 position [[position]];
 834  float2 texture_position;
 835  float clip_distance [[clip_distance]][4];
 836};
 837
 838struct SurfaceFragmentInput {
 839  float4 position [[position]];
 840  float2 texture_position;
 841};
 842
 843vertex SurfaceVertexOutput surface_vertex(
 844    uint unit_vertex_id [[vertex_id]], uint surface_id [[instance_id]],
 845    constant float2 *unit_vertices [[buffer(SurfaceInputIndex_Vertices)]],
 846    constant SurfaceBounds *surfaces [[buffer(SurfaceInputIndex_Surfaces)]],
 847    constant Size_DevicePixels *viewport_size
 848    [[buffer(SurfaceInputIndex_ViewportSize)]],
 849    constant Size_DevicePixels *texture_size
 850    [[buffer(SurfaceInputIndex_TextureSize)]]) {
 851  float2 unit_vertex = unit_vertices[unit_vertex_id];
 852  SurfaceBounds surface = surfaces[surface_id];
 853  float4 device_position =
 854      to_device_position(unit_vertex, surface.bounds, viewport_size);
 855  float4 clip_distance = distance_from_clip_rect(unit_vertex, surface.bounds,
 856                                                 surface.content_mask.bounds);
 857  // We are going to copy the whole texture, so the texture position corresponds
 858  // to the current vertex of the unit triangle.
 859  float2 texture_position = unit_vertex;
 860  return SurfaceVertexOutput{
 861      device_position,
 862      texture_position,
 863      {clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
 864}
 865
 866fragment float4 surface_fragment(SurfaceFragmentInput input [[stage_in]],
 867                                 texture2d<float> y_texture
 868                                 [[texture(SurfaceInputIndex_YTexture)]],
 869                                 texture2d<float> cb_cr_texture
 870                                 [[texture(SurfaceInputIndex_CbCrTexture)]]) {
 871  constexpr sampler texture_sampler(mag_filter::linear, min_filter::linear);
 872  const float4x4 ycbcrToRGBTransform =
 873      float4x4(float4(+1.0000f, +1.0000f, +1.0000f, +0.0000f),
 874               float4(+0.0000f, -0.3441f, +1.7720f, +0.0000f),
 875               float4(+1.4020f, -0.7141f, +0.0000f, +0.0000f),
 876               float4(-0.7010f, +0.5291f, -0.8860f, +1.0000f));
 877  float4 ycbcr = float4(
 878      y_texture.sample(texture_sampler, input.texture_position).r,
 879      cb_cr_texture.sample(texture_sampler, input.texture_position).rg, 1.0);
 880
 881  return ycbcrToRGBTransform * ycbcr;
 882}
 883
 884float4 hsla_to_rgba(Hsla hsla) {
 885  float h = hsla.h * 6.0; // Now, it's an angle but scaled in [0, 6) range
 886  float s = hsla.s;
 887  float l = hsla.l;
 888  float a = hsla.a;
 889
 890  float c = (1.0 - fabs(2.0 * l - 1.0)) * s;
 891  float x = c * (1.0 - fabs(fmod(h, 2.0) - 1.0));
 892  float m = l - c / 2.0;
 893
 894  float r = 0.0;
 895  float g = 0.0;
 896  float b = 0.0;
 897
 898  if (h >= 0.0 && h < 1.0) {
 899    r = c;
 900    g = x;
 901    b = 0.0;
 902  } else if (h >= 1.0 && h < 2.0) {
 903    r = x;
 904    g = c;
 905    b = 0.0;
 906  } else if (h >= 2.0 && h < 3.0) {
 907    r = 0.0;
 908    g = c;
 909    b = x;
 910  } else if (h >= 3.0 && h < 4.0) {
 911    r = 0.0;
 912    g = x;
 913    b = c;
 914  } else if (h >= 4.0 && h < 5.0) {
 915    r = x;
 916    g = 0.0;
 917    b = c;
 918  } else {
 919    r = c;
 920    g = 0.0;
 921    b = x;
 922  }
 923
 924  float4 rgba;
 925  rgba.x = (r + m);
 926  rgba.y = (g + m);
 927  rgba.z = (b + m);
 928  rgba.w = a;
 929  return rgba;
 930}
 931
 932float3 srgb_to_linear(float3 color) {
 933  return pow(color, float3(2.2));
 934}
 935
 936float3 linear_to_srgb(float3 color) {
 937  return pow(color, float3(1.0 / 2.2));
 938}
 939
 940// Converts a sRGB color to the Oklab color space.
 941// Reference: https://bottosson.github.io/posts/oklab/#converting-from-linear-srgb-to-oklab
 942float4 srgb_to_oklab(float4 color) {
 943  // Convert non-linear sRGB to linear sRGB
 944  color = float4(srgb_to_linear(color.rgb), color.a);
 945
 946  float l = 0.4122214708 * color.r + 0.5363325363 * color.g + 0.0514459929 * color.b;
 947  float m = 0.2119034982 * color.r + 0.6806995451 * color.g + 0.1073969566 * color.b;
 948  float s = 0.0883024619 * color.r + 0.2817188376 * color.g + 0.6299787005 * color.b;
 949
 950  float l_ = pow(l, 1.0/3.0);
 951  float m_ = pow(m, 1.0/3.0);
 952  float s_ = pow(s, 1.0/3.0);
 953
 954  return float4(
 955   	0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_,
 956   	1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_,
 957   	0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_,
 958   	color.a
 959  );
 960}
 961
 962// Converts an Oklab color to the sRGB color space.
 963float4 oklab_to_srgb(float4 color) {
 964  float l_ = color.r + 0.3963377774 * color.g + 0.2158037573 * color.b;
 965  float m_ = color.r - 0.1055613458 * color.g - 0.0638541728 * color.b;
 966  float s_ = color.r - 0.0894841775 * color.g - 1.2914855480 * color.b;
 967
 968  float l = l_ * l_ * l_;
 969  float m = m_ * m_ * m_;
 970  float s = s_ * s_ * s_;
 971
 972  float3 linear_rgb = float3(
 973   	4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
 974   	-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
 975   	-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s
 976  );
 977
 978  // Convert linear sRGB to non-linear sRGB
 979  return float4(linear_to_srgb(linear_rgb), color.a);
 980}
 981
 982float4 to_device_position(float2 unit_vertex, Bounds_ScaledPixels bounds,
 983                          constant Size_DevicePixels *input_viewport_size) {
 984  float2 position =
 985      unit_vertex * float2(bounds.size.width, bounds.size.height) +
 986      float2(bounds.origin.x, bounds.origin.y);
 987  float2 viewport_size = float2((float)input_viewport_size->width,
 988                                (float)input_viewport_size->height);
 989  float2 device_position =
 990      position / viewport_size * float2(2., -2.) + float2(-1., 1.);
 991  return float4(device_position, 0., 1.);
 992}
 993
 994float4 to_device_position_transformed(float2 unit_vertex, Bounds_ScaledPixels bounds,
 995                          TransformationMatrix transformation,
 996                          constant Size_DevicePixels *input_viewport_size) {
 997  float2 position =
 998      unit_vertex * float2(bounds.size.width, bounds.size.height) +
 999      float2(bounds.origin.x, bounds.origin.y);
1000
1001  // Apply the transformation matrix to the position via matrix multiplication.
1002  float2 transformed_position = float2(0, 0);
1003  transformed_position[0] = position[0] * transformation.rotation_scale[0][0] + position[1] * transformation.rotation_scale[0][1];
1004  transformed_position[1] = position[0] * transformation.rotation_scale[1][0] + position[1] * transformation.rotation_scale[1][1];
1005
1006  // Add in the translation component of the transformation matrix.
1007  transformed_position[0] += transformation.translation[0];
1008  transformed_position[1] += transformation.translation[1];
1009
1010  float2 viewport_size = float2((float)input_viewport_size->width,
1011                                (float)input_viewport_size->height);
1012  float2 device_position =
1013      transformed_position / viewport_size * float2(2., -2.) + float2(-1., 1.);
1014  return float4(device_position, 0., 1.);
1015}
1016
1017
1018float2 to_tile_position(float2 unit_vertex, AtlasTile tile,
1019                        constant Size_DevicePixels *atlas_size) {
1020  float2 tile_origin = float2(tile.bounds.origin.x, tile.bounds.origin.y);
1021  float2 tile_size = float2(tile.bounds.size.width, tile.bounds.size.height);
1022  return (tile_origin + unit_vertex * tile_size) /
1023         float2((float)atlas_size->width, (float)atlas_size->height);
1024}
1025
1026// Selects corner radius based on quadrant.
1027float pick_corner_radius(float2 center_to_point, Corners_ScaledPixels corner_radii) {
1028  if (center_to_point.x < 0.) {
1029    if (center_to_point.y < 0.) {
1030      return corner_radii.top_left;
1031    } else {
1032      return corner_radii.bottom_left;
1033    }
1034  } else {
1035    if (center_to_point.y < 0.) {
1036      return corner_radii.top_right;
1037    } else {
1038      return corner_radii.bottom_right;
1039    }
1040  }
1041}
1042
1043// Signed distance of the point to the quad's border - positive outside the
1044// border, and negative inside.
1045float quad_sdf(float2 point, Bounds_ScaledPixels bounds,
1046               Corners_ScaledPixels corner_radii) {
1047    float2 half_size = float2(bounds.size.width, bounds.size.height) / 2.0;
1048    float2 center = float2(bounds.origin.x, bounds.origin.y) + half_size;
1049    float2 center_to_point = point - center;
1050    float corner_radius = pick_corner_radius(center_to_point, corner_radii);
1051    float2 corner_to_point = fabs(center_to_point) - half_size;
1052    float2 corner_center_to_point = corner_to_point + corner_radius;
1053    return quad_sdf_impl(corner_center_to_point, corner_radius);
1054}
1055
1056// Implementation of quad signed distance field
1057float quad_sdf_impl(float2 corner_center_to_point, float corner_radius) {
1058    if (corner_radius == 0.0) {
1059        // Fast path for unrounded corners
1060        return max(corner_center_to_point.x, corner_center_to_point.y);
1061    } else {
1062        // Signed distance of the point from a quad that is inset by corner_radius
1063        // It is negative inside this quad, and positive outside
1064        float signed_distance_to_inset_quad =
1065            // 0 inside the inset quad, and positive outside
1066            length(max(float2(0.0), corner_center_to_point)) +
1067            // 0 outside the inset quad, and negative inside
1068            min(0.0, max(corner_center_to_point.x, corner_center_to_point.y));
1069
1070        return signed_distance_to_inset_quad - corner_radius;
1071    }
1072}
1073
1074// A standard gaussian function, used for weighting samples
1075float gaussian(float x, float sigma) {
1076  return exp(-(x * x) / (2. * sigma * sigma)) / (sqrt(2. * M_PI_F) * sigma);
1077}
1078
1079// This approximates the error function, needed for the gaussian integral
1080float2 erf(float2 x) {
1081  float2 s = sign(x);
1082  float2 a = abs(x);
1083  float2 r1 = 1. + (0.278393 + (0.230389 + (0.000972 + 0.078108 * a) * a) * a) * a;
1084  float2 r2 = r1 * r1;
1085  return s - s / (r2 * r2);
1086}
1087
1088float blur_along_x(float x, float y, float sigma, float corner,
1089                   float2 half_size) {
1090  float delta = min(half_size.y - corner - abs(y), 0.);
1091  float curved =
1092      half_size.x - corner + sqrt(max(0., corner * corner - delta * delta));
1093  float2 integral =
1094      0.5 + 0.5 * erf((x + float2(-curved, curved)) * (sqrt(0.5) / sigma));
1095  return integral.y - integral.x;
1096}
1097
1098float4 distance_from_clip_rect(float2 unit_vertex, Bounds_ScaledPixels bounds,
1099                               Bounds_ScaledPixels clip_bounds) {
1100  float2 position =
1101      unit_vertex * float2(bounds.size.width, bounds.size.height) +
1102      float2(bounds.origin.x, bounds.origin.y);
1103  return float4(position.x - clip_bounds.origin.x,
1104                clip_bounds.origin.x + clip_bounds.size.width - position.x,
1105                position.y - clip_bounds.origin.y,
1106                clip_bounds.origin.y + clip_bounds.size.height - position.y);
1107}
1108
1109float4 over(float4 below, float4 above) {
1110  float4 result;
1111  float alpha = above.a + below.a * (1.0 - above.a);
1112  result.rgb =
1113      (above.rgb * above.a + below.rgb * below.a * (1.0 - above.a)) / alpha;
1114  result.a = alpha;
1115  return result;
1116}
1117
1118GradientColor prepare_fill_color(uint tag, uint color_space, Hsla solid,
1119                                     Hsla color0, Hsla color1) {
1120  GradientColor out;
1121  if (tag == 0 || tag == 2) {
1122    out.solid = hsla_to_rgba(solid);
1123  } else if (tag == 1) {
1124    out.color0 = hsla_to_rgba(color0);
1125    out.color1 = hsla_to_rgba(color1);
1126
1127    // Prepare color space in vertex for avoid conversion
1128    // in fragment shader for performance reasons
1129    if (color_space == 1) {
1130      // Oklab
1131      out.color0 = srgb_to_oklab(out.color0);
1132      out.color1 = srgb_to_oklab(out.color1);
1133    }
1134  }
1135
1136  return out;
1137}
1138
1139float2x2 rotate2d(float angle) {
1140    float s = sin(angle);
1141    float c = cos(angle);
1142    return float2x2(c, -s, s, c);
1143}
1144
1145float4 fill_color(Background background,
1146                      float2 position,
1147                      Bounds_ScaledPixels bounds,
1148                      float4 solid_color, float4 color0, float4 color1) {
1149  float4 color;
1150
1151  switch (background.tag) {
1152    case 0:
1153      color = solid_color;
1154      break;
1155    case 1: {
1156      // -90 degrees to match the CSS gradient angle.
1157      float gradient_angle = background.gradient_angle_or_pattern_height;
1158      float radians = (fmod(gradient_angle, 360.0) - 90.0) * (M_PI_F / 180.0);
1159      float2 direction = float2(cos(radians), sin(radians));
1160
1161      // Expand the short side to be the same as the long side
1162      if (bounds.size.width > bounds.size.height) {
1163          direction.y *= bounds.size.height / bounds.size.width;
1164      } else {
1165          direction.x *=  bounds.size.width / bounds.size.height;
1166      }
1167
1168      // Get the t value for the linear gradient with the color stop percentages.
1169      float2 half_size = float2(bounds.size.width, bounds.size.height) / 2.;
1170      float2 center = float2(bounds.origin.x, bounds.origin.y) + half_size;
1171      float2 center_to_point = position - center;
1172      float t = dot(center_to_point, direction) / length(direction);
1173      // Check the direction to determine whether to use x or y
1174      if (abs(direction.x) > abs(direction.y)) {
1175          t = (t + half_size.x) / bounds.size.width;
1176      } else {
1177          t = (t + half_size.y) / bounds.size.height;
1178      }
1179
1180      // Adjust t based on the stop percentages
1181      t = (t - background.colors[0].percentage)
1182        / (background.colors[1].percentage
1183        - background.colors[0].percentage);
1184      t = clamp(t, 0.0, 1.0);
1185
1186      switch (background.color_space) {
1187        case 0:
1188          color = mix(color0, color1, t);
1189          break;
1190        case 1: {
1191          float4 oklab_color = mix(color0, color1, t);
1192          color = oklab_to_srgb(oklab_color);
1193          break;
1194        }
1195      }
1196      break;
1197    }
1198    case 2: {
1199        float gradient_angle_or_pattern_height = background.gradient_angle_or_pattern_height;
1200        float pattern_width = (gradient_angle_or_pattern_height / 65535.0f) / 255.0f;
1201        float pattern_interval = fmod(gradient_angle_or_pattern_height, 65535.0f) / 255.0f;
1202        float pattern_height = pattern_width + pattern_interval;
1203        float stripe_angle = M_PI_F / 4.0;
1204        float pattern_period = pattern_height * sin(stripe_angle);
1205        float2x2 rotation = rotate2d(stripe_angle);
1206        float2 relative_position = position - float2(bounds.origin.x, bounds.origin.y);
1207        float2 rotated_point = rotation * relative_position;
1208        float pattern = fmod(rotated_point.x, pattern_period);
1209        float distance = min(pattern, pattern_period - pattern) - pattern_period * (pattern_width / pattern_height) /  2.0f;
1210        color = solid_color;
1211        color.a *= saturate(0.5 - distance);
1212        break;
1213    }
1214  }
1215
1216  return color;
1217}