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