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  const float WAVE_FREQUENCY = 2.0;
 571  const float WAVE_HEIGHT_RATIO = 0.8;
 572
 573  Underline underline = underlines[input.underline_id];
 574  if (underline.wavy) {
 575    float half_thickness = underline.thickness * 0.5;
 576    float2 origin =
 577        float2(underline.bounds.origin.x, underline.bounds.origin.y);
 578
 579    float2 st = ((input.position.xy - origin) / underline.bounds.size.height) -
 580                float2(0., 0.5);
 581    float frequency = (M_PI_F * WAVE_FREQUENCY * underline.thickness) / underline.bounds.size.height;
 582    float amplitude = (underline.thickness * WAVE_HEIGHT_RATIO) / underline.bounds.size.height;
 583
 584    float sine = sin(st.x * frequency) * amplitude;
 585    float dSine = cos(st.x * frequency) * amplitude * frequency;
 586    float distance = (st.y - sine) / sqrt(1. + dSine * dSine);
 587    float distance_in_pixels = distance * underline.bounds.size.height;
 588    float distance_from_top_border = distance_in_pixels - half_thickness;
 589    float distance_from_bottom_border = distance_in_pixels + half_thickness;
 590    float alpha = saturate(
 591        0.5 - max(-distance_from_bottom_border, distance_from_top_border));
 592    return input.color * float4(1., 1., 1., alpha);
 593  } else {
 594    return input.color;
 595  }
 596}
 597
 598struct MonochromeSpriteVertexOutput {
 599  float4 position [[position]];
 600  float2 tile_position;
 601  float4 color [[flat]];
 602  float clip_distance [[clip_distance]][4];
 603};
 604
 605struct MonochromeSpriteFragmentInput {
 606  float4 position [[position]];
 607  float2 tile_position;
 608  float4 color [[flat]];
 609};
 610
 611vertex MonochromeSpriteVertexOutput monochrome_sprite_vertex(
 612    uint unit_vertex_id [[vertex_id]], uint sprite_id [[instance_id]],
 613    constant float2 *unit_vertices [[buffer(SpriteInputIndex_Vertices)]],
 614    constant MonochromeSprite *sprites [[buffer(SpriteInputIndex_Sprites)]],
 615    constant Size_DevicePixels *viewport_size
 616    [[buffer(SpriteInputIndex_ViewportSize)]],
 617    constant Size_DevicePixels *atlas_size
 618    [[buffer(SpriteInputIndex_AtlasTextureSize)]]) {
 619  float2 unit_vertex = unit_vertices[unit_vertex_id];
 620  MonochromeSprite sprite = sprites[sprite_id];
 621  float4 device_position =
 622      to_device_position_transformed(unit_vertex, sprite.bounds, sprite.transformation, viewport_size);
 623  float4 clip_distance = distance_from_clip_rect(unit_vertex, sprite.bounds,
 624                                                 sprite.content_mask.bounds);
 625  float2 tile_position = to_tile_position(unit_vertex, sprite.tile, atlas_size);
 626  float4 color = hsla_to_rgba(sprite.color);
 627  return MonochromeSpriteVertexOutput{
 628      device_position,
 629      tile_position,
 630      color,
 631      {clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
 632}
 633
 634fragment float4 monochrome_sprite_fragment(
 635    MonochromeSpriteFragmentInput input [[stage_in]],
 636    constant MonochromeSprite *sprites [[buffer(SpriteInputIndex_Sprites)]],
 637    texture2d<float> atlas_texture [[texture(SpriteInputIndex_AtlasTexture)]]) {
 638  constexpr sampler atlas_texture_sampler(mag_filter::linear,
 639                                          min_filter::linear);
 640  float4 sample =
 641      atlas_texture.sample(atlas_texture_sampler, input.tile_position);
 642  float4 color = input.color;
 643  color.a *= sample.a;
 644  return color;
 645}
 646
 647struct PolychromeSpriteVertexOutput {
 648  float4 position [[position]];
 649  float2 tile_position;
 650  uint sprite_id [[flat]];
 651  float clip_distance [[clip_distance]][4];
 652};
 653
 654struct PolychromeSpriteFragmentInput {
 655  float4 position [[position]];
 656  float2 tile_position;
 657  uint sprite_id [[flat]];
 658};
 659
 660vertex PolychromeSpriteVertexOutput polychrome_sprite_vertex(
 661    uint unit_vertex_id [[vertex_id]], uint sprite_id [[instance_id]],
 662    constant float2 *unit_vertices [[buffer(SpriteInputIndex_Vertices)]],
 663    constant PolychromeSprite *sprites [[buffer(SpriteInputIndex_Sprites)]],
 664    constant Size_DevicePixels *viewport_size
 665    [[buffer(SpriteInputIndex_ViewportSize)]],
 666    constant Size_DevicePixels *atlas_size
 667    [[buffer(SpriteInputIndex_AtlasTextureSize)]]) {
 668
 669  float2 unit_vertex = unit_vertices[unit_vertex_id];
 670  PolychromeSprite sprite = sprites[sprite_id];
 671  float4 device_position =
 672      to_device_position(unit_vertex, sprite.bounds, viewport_size);
 673  float4 clip_distance = distance_from_clip_rect(unit_vertex, sprite.bounds,
 674                                                 sprite.content_mask.bounds);
 675  float2 tile_position = to_tile_position(unit_vertex, sprite.tile, atlas_size);
 676  return PolychromeSpriteVertexOutput{
 677      device_position,
 678      tile_position,
 679      sprite_id,
 680      {clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
 681}
 682
 683fragment float4 polychrome_sprite_fragment(
 684    PolychromeSpriteFragmentInput input [[stage_in]],
 685    constant PolychromeSprite *sprites [[buffer(SpriteInputIndex_Sprites)]],
 686    texture2d<float> atlas_texture [[texture(SpriteInputIndex_AtlasTexture)]]) {
 687  PolychromeSprite sprite = sprites[input.sprite_id];
 688  constexpr sampler atlas_texture_sampler(mag_filter::linear,
 689                                          min_filter::linear);
 690  float4 sample =
 691      atlas_texture.sample(atlas_texture_sampler, input.tile_position);
 692  float distance =
 693      quad_sdf(input.position.xy, sprite.bounds, sprite.corner_radii);
 694
 695  float4 color = sample;
 696  if (sprite.grayscale) {
 697    float grayscale = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
 698    color.r = grayscale;
 699    color.g = grayscale;
 700    color.b = grayscale;
 701  }
 702  color.a *= sprite.opacity * saturate(0.5 - distance);
 703  return color;
 704}
 705
 706struct PathRasterizationVertexOutput {
 707  float4 position [[position]];
 708  float2 st_position;
 709  uint vertex_id [[flat]];
 710  float clip_rect_distance [[clip_distance]][4];
 711};
 712
 713struct PathRasterizationFragmentInput {
 714  float4 position [[position]];
 715  float2 st_position;
 716  uint vertex_id [[flat]];
 717};
 718
 719vertex PathRasterizationVertexOutput path_rasterization_vertex(
 720  uint vertex_id [[vertex_id]],
 721  constant PathRasterizationVertex *vertices [[buffer(PathRasterizationInputIndex_Vertices)]],
 722  constant Size_DevicePixels *atlas_size [[buffer(PathRasterizationInputIndex_ViewportSize)]]
 723) {
 724  PathRasterizationVertex v = vertices[vertex_id];
 725  float2 vertex_position = float2(v.xy_position.x, v.xy_position.y);
 726  float4 position = float4(
 727    vertex_position * float2(2. / atlas_size->width, -2. / atlas_size->height) + float2(-1., 1.),
 728    0.,
 729    1.
 730  );
 731  return PathRasterizationVertexOutput{
 732      position,
 733      float2(v.st_position.x, v.st_position.y),
 734      vertex_id,
 735      {
 736        v.xy_position.x - v.bounds.origin.x,
 737        v.bounds.origin.x + v.bounds.size.width - v.xy_position.x,
 738        v.xy_position.y - v.bounds.origin.y,
 739        v.bounds.origin.y + v.bounds.size.height - v.xy_position.y
 740      }
 741  };
 742}
 743
 744fragment float4 path_rasterization_fragment(
 745  PathRasterizationFragmentInput input [[stage_in]],
 746  constant PathRasterizationVertex *vertices [[buffer(PathRasterizationInputIndex_Vertices)]]
 747) {
 748  float2 dx = dfdx(input.st_position);
 749  float2 dy = dfdy(input.st_position);
 750
 751  PathRasterizationVertex v = vertices[input.vertex_id];
 752  Background background = v.color;
 753  Bounds_ScaledPixels path_bounds = v.bounds;
 754  float alpha;
 755  if (length(float2(dx.x, dy.x)) < 0.001) {
 756    alpha = 1.0;
 757  } else {
 758    float2 gradient = float2(
 759      (2. * input.st_position.x) * dx.x - dx.y,
 760      (2. * input.st_position.x) * dy.x - dy.y
 761    );
 762    float f = (input.st_position.x * input.st_position.x) - input.st_position.y;
 763    float distance = f / length(gradient);
 764    alpha = saturate(0.5 - distance);
 765  }
 766
 767  GradientColor gradient_color = prepare_fill_color(
 768    background.tag,
 769    background.color_space,
 770    background.solid,
 771    background.colors[0].color,
 772    background.colors[1].color
 773  );
 774
 775  float4 color = fill_color(
 776    background,
 777    input.position.xy,
 778    path_bounds,
 779    gradient_color.solid,
 780    gradient_color.color0,
 781    gradient_color.color1
 782  );
 783  return float4(color.rgb * color.a * alpha, alpha * color.a);
 784}
 785
 786struct PathSpriteVertexOutput {
 787  float4 position [[position]];
 788  float2 texture_coords;
 789};
 790
 791vertex PathSpriteVertexOutput path_sprite_vertex(
 792  uint unit_vertex_id [[vertex_id]],
 793  uint sprite_id [[instance_id]],
 794  constant float2 *unit_vertices [[buffer(SpriteInputIndex_Vertices)]],
 795  constant PathSprite *sprites [[buffer(SpriteInputIndex_Sprites)]],
 796  constant Size_DevicePixels *viewport_size [[buffer(SpriteInputIndex_ViewportSize)]]
 797) {
 798  float2 unit_vertex = unit_vertices[unit_vertex_id];
 799  PathSprite sprite = sprites[sprite_id];
 800  // Don't apply content mask because it was already accounted for when
 801  // rasterizing the path.
 802  float4 device_position =
 803      to_device_position(unit_vertex, sprite.bounds, viewport_size);
 804
 805  float2 screen_position = float2(sprite.bounds.origin.x, sprite.bounds.origin.y) + unit_vertex * float2(sprite.bounds.size.width, sprite.bounds.size.height);
 806  float2 texture_coords = screen_position / float2(viewport_size->width, viewport_size->height);
 807
 808  return PathSpriteVertexOutput{
 809    device_position,
 810    texture_coords
 811  };
 812}
 813
 814fragment float4 path_sprite_fragment(
 815  PathSpriteVertexOutput input [[stage_in]],
 816  texture2d<float> intermediate_texture [[texture(SpriteInputIndex_AtlasTexture)]]
 817) {
 818  constexpr sampler intermediate_texture_sampler(mag_filter::linear, min_filter::linear);
 819  return intermediate_texture.sample(intermediate_texture_sampler, input.texture_coords);
 820}
 821
 822struct SurfaceVertexOutput {
 823  float4 position [[position]];
 824  float2 texture_position;
 825  float clip_distance [[clip_distance]][4];
 826};
 827
 828struct SurfaceFragmentInput {
 829  float4 position [[position]];
 830  float2 texture_position;
 831};
 832
 833vertex SurfaceVertexOutput surface_vertex(
 834    uint unit_vertex_id [[vertex_id]], uint surface_id [[instance_id]],
 835    constant float2 *unit_vertices [[buffer(SurfaceInputIndex_Vertices)]],
 836    constant SurfaceBounds *surfaces [[buffer(SurfaceInputIndex_Surfaces)]],
 837    constant Size_DevicePixels *viewport_size
 838    [[buffer(SurfaceInputIndex_ViewportSize)]],
 839    constant Size_DevicePixels *texture_size
 840    [[buffer(SurfaceInputIndex_TextureSize)]]) {
 841  float2 unit_vertex = unit_vertices[unit_vertex_id];
 842  SurfaceBounds surface = surfaces[surface_id];
 843  float4 device_position =
 844      to_device_position(unit_vertex, surface.bounds, viewport_size);
 845  float4 clip_distance = distance_from_clip_rect(unit_vertex, surface.bounds,
 846                                                 surface.content_mask.bounds);
 847  // We are going to copy the whole texture, so the texture position corresponds
 848  // to the current vertex of the unit triangle.
 849  float2 texture_position = unit_vertex;
 850  return SurfaceVertexOutput{
 851      device_position,
 852      texture_position,
 853      {clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
 854}
 855
 856fragment float4 surface_fragment(SurfaceFragmentInput input [[stage_in]],
 857                                 texture2d<float> y_texture
 858                                 [[texture(SurfaceInputIndex_YTexture)]],
 859                                 texture2d<float> cb_cr_texture
 860                                 [[texture(SurfaceInputIndex_CbCrTexture)]]) {
 861  constexpr sampler texture_sampler(mag_filter::linear, min_filter::linear);
 862  const float4x4 ycbcrToRGBTransform =
 863      float4x4(float4(+1.0000f, +1.0000f, +1.0000f, +0.0000f),
 864               float4(+0.0000f, -0.3441f, +1.7720f, +0.0000f),
 865               float4(+1.4020f, -0.7141f, +0.0000f, +0.0000f),
 866               float4(-0.7010f, +0.5291f, -0.8860f, +1.0000f));
 867  float4 ycbcr = float4(
 868      y_texture.sample(texture_sampler, input.texture_position).r,
 869      cb_cr_texture.sample(texture_sampler, input.texture_position).rg, 1.0);
 870
 871  return ycbcrToRGBTransform * ycbcr;
 872}
 873
 874float4 hsla_to_rgba(Hsla hsla) {
 875  float h = hsla.h * 6.0; // Now, it's an angle but scaled in [0, 6) range
 876  float s = hsla.s;
 877  float l = hsla.l;
 878  float a = hsla.a;
 879
 880  float c = (1.0 - fabs(2.0 * l - 1.0)) * s;
 881  float x = c * (1.0 - fabs(fmod(h, 2.0) - 1.0));
 882  float m = l - c / 2.0;
 883
 884  float r = 0.0;
 885  float g = 0.0;
 886  float b = 0.0;
 887
 888  if (h >= 0.0 && h < 1.0) {
 889    r = c;
 890    g = x;
 891    b = 0.0;
 892  } else if (h >= 1.0 && h < 2.0) {
 893    r = x;
 894    g = c;
 895    b = 0.0;
 896  } else if (h >= 2.0 && h < 3.0) {
 897    r = 0.0;
 898    g = c;
 899    b = x;
 900  } else if (h >= 3.0 && h < 4.0) {
 901    r = 0.0;
 902    g = x;
 903    b = c;
 904  } else if (h >= 4.0 && h < 5.0) {
 905    r = x;
 906    g = 0.0;
 907    b = c;
 908  } else {
 909    r = c;
 910    g = 0.0;
 911    b = x;
 912  }
 913
 914  float4 rgba;
 915  rgba.x = (r + m);
 916  rgba.y = (g + m);
 917  rgba.z = (b + m);
 918  rgba.w = a;
 919  return rgba;
 920}
 921
 922float3 srgb_to_linear(float3 color) {
 923  return pow(color, float3(2.2));
 924}
 925
 926float3 linear_to_srgb(float3 color) {
 927  return pow(color, float3(1.0 / 2.2));
 928}
 929
 930// Converts a sRGB color to the Oklab color space.
 931// Reference: https://bottosson.github.io/posts/oklab/#converting-from-linear-srgb-to-oklab
 932float4 srgb_to_oklab(float4 color) {
 933  // Convert non-linear sRGB to linear sRGB
 934  color = float4(srgb_to_linear(color.rgb), color.a);
 935
 936  float l = 0.4122214708 * color.r + 0.5363325363 * color.g + 0.0514459929 * color.b;
 937  float m = 0.2119034982 * color.r + 0.6806995451 * color.g + 0.1073969566 * color.b;
 938  float s = 0.0883024619 * color.r + 0.2817188376 * color.g + 0.6299787005 * color.b;
 939
 940  float l_ = pow(l, 1.0/3.0);
 941  float m_ = pow(m, 1.0/3.0);
 942  float s_ = pow(s, 1.0/3.0);
 943
 944  return float4(
 945   	0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_,
 946   	1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_,
 947   	0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_,
 948   	color.a
 949  );
 950}
 951
 952// Converts an Oklab color to the sRGB color space.
 953float4 oklab_to_srgb(float4 color) {
 954  float l_ = color.r + 0.3963377774 * color.g + 0.2158037573 * color.b;
 955  float m_ = color.r - 0.1055613458 * color.g - 0.0638541728 * color.b;
 956  float s_ = color.r - 0.0894841775 * color.g - 1.2914855480 * color.b;
 957
 958  float l = l_ * l_ * l_;
 959  float m = m_ * m_ * m_;
 960  float s = s_ * s_ * s_;
 961
 962  float3 linear_rgb = float3(
 963   	4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
 964   	-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
 965   	-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s
 966  );
 967
 968  // Convert linear sRGB to non-linear sRGB
 969  return float4(linear_to_srgb(linear_rgb), color.a);
 970}
 971
 972float4 to_device_position(float2 unit_vertex, Bounds_ScaledPixels bounds,
 973                          constant Size_DevicePixels *input_viewport_size) {
 974  float2 position =
 975      unit_vertex * float2(bounds.size.width, bounds.size.height) +
 976      float2(bounds.origin.x, bounds.origin.y);
 977  float2 viewport_size = float2((float)input_viewport_size->width,
 978                                (float)input_viewport_size->height);
 979  float2 device_position =
 980      position / viewport_size * float2(2., -2.) + float2(-1., 1.);
 981  return float4(device_position, 0., 1.);
 982}
 983
 984float4 to_device_position_transformed(float2 unit_vertex, Bounds_ScaledPixels bounds,
 985                          TransformationMatrix transformation,
 986                          constant Size_DevicePixels *input_viewport_size) {
 987  float2 position =
 988      unit_vertex * float2(bounds.size.width, bounds.size.height) +
 989      float2(bounds.origin.x, bounds.origin.y);
 990
 991  // Apply the transformation matrix to the position via matrix multiplication.
 992  float2 transformed_position = float2(0, 0);
 993  transformed_position[0] = position[0] * transformation.rotation_scale[0][0] + position[1] * transformation.rotation_scale[0][1];
 994  transformed_position[1] = position[0] * transformation.rotation_scale[1][0] + position[1] * transformation.rotation_scale[1][1];
 995
 996  // Add in the translation component of the transformation matrix.
 997  transformed_position[0] += transformation.translation[0];
 998  transformed_position[1] += transformation.translation[1];
 999
1000  float2 viewport_size = float2((float)input_viewport_size->width,
1001                                (float)input_viewport_size->height);
1002  float2 device_position =
1003      transformed_position / viewport_size * float2(2., -2.) + float2(-1., 1.);
1004  return float4(device_position, 0., 1.);
1005}
1006
1007
1008float2 to_tile_position(float2 unit_vertex, AtlasTile tile,
1009                        constant Size_DevicePixels *atlas_size) {
1010  float2 tile_origin = float2(tile.bounds.origin.x, tile.bounds.origin.y);
1011  float2 tile_size = float2(tile.bounds.size.width, tile.bounds.size.height);
1012  return (tile_origin + unit_vertex * tile_size) /
1013         float2((float)atlas_size->width, (float)atlas_size->height);
1014}
1015
1016// Selects corner radius based on quadrant.
1017float pick_corner_radius(float2 center_to_point, Corners_ScaledPixels corner_radii) {
1018  if (center_to_point.x < 0.) {
1019    if (center_to_point.y < 0.) {
1020      return corner_radii.top_left;
1021    } else {
1022      return corner_radii.bottom_left;
1023    }
1024  } else {
1025    if (center_to_point.y < 0.) {
1026      return corner_radii.top_right;
1027    } else {
1028      return corner_radii.bottom_right;
1029    }
1030  }
1031}
1032
1033// Signed distance of the point to the quad's border - positive outside the
1034// border, and negative inside.
1035float quad_sdf(float2 point, Bounds_ScaledPixels bounds,
1036               Corners_ScaledPixels corner_radii) {
1037    float2 half_size = float2(bounds.size.width, bounds.size.height) / 2.0;
1038    float2 center = float2(bounds.origin.x, bounds.origin.y) + half_size;
1039    float2 center_to_point = point - center;
1040    float corner_radius = pick_corner_radius(center_to_point, corner_radii);
1041    float2 corner_to_point = fabs(center_to_point) - half_size;
1042    float2 corner_center_to_point = corner_to_point + corner_radius;
1043    return quad_sdf_impl(corner_center_to_point, corner_radius);
1044}
1045
1046// Implementation of quad signed distance field
1047float quad_sdf_impl(float2 corner_center_to_point, float corner_radius) {
1048    if (corner_radius == 0.0) {
1049        // Fast path for unrounded corners
1050        return max(corner_center_to_point.x, corner_center_to_point.y);
1051    } else {
1052        // Signed distance of the point from a quad that is inset by corner_radius
1053        // It is negative inside this quad, and positive outside
1054        float signed_distance_to_inset_quad =
1055            // 0 inside the inset quad, and positive outside
1056            length(max(float2(0.0), corner_center_to_point)) +
1057            // 0 outside the inset quad, and negative inside
1058            min(0.0, max(corner_center_to_point.x, corner_center_to_point.y));
1059
1060        return signed_distance_to_inset_quad - corner_radius;
1061    }
1062}
1063
1064// A standard gaussian function, used for weighting samples
1065float gaussian(float x, float sigma) {
1066  return exp(-(x * x) / (2. * sigma * sigma)) / (sqrt(2. * M_PI_F) * sigma);
1067}
1068
1069// This approximates the error function, needed for the gaussian integral
1070float2 erf(float2 x) {
1071  float2 s = sign(x);
1072  float2 a = abs(x);
1073  float2 r1 = 1. + (0.278393 + (0.230389 + (0.000972 + 0.078108 * a) * a) * a) * a;
1074  float2 r2 = r1 * r1;
1075  return s - s / (r2 * r2);
1076}
1077
1078float blur_along_x(float x, float y, float sigma, float corner,
1079                   float2 half_size) {
1080  float delta = min(half_size.y - corner - abs(y), 0.);
1081  float curved =
1082      half_size.x - corner + sqrt(max(0., corner * corner - delta * delta));
1083  float2 integral =
1084      0.5 + 0.5 * erf((x + float2(-curved, curved)) * (sqrt(0.5) / sigma));
1085  return integral.y - integral.x;
1086}
1087
1088float4 distance_from_clip_rect(float2 unit_vertex, Bounds_ScaledPixels bounds,
1089                               Bounds_ScaledPixels clip_bounds) {
1090  float2 position =
1091      unit_vertex * float2(bounds.size.width, bounds.size.height) +
1092      float2(bounds.origin.x, bounds.origin.y);
1093  return float4(position.x - clip_bounds.origin.x,
1094                clip_bounds.origin.x + clip_bounds.size.width - position.x,
1095                position.y - clip_bounds.origin.y,
1096                clip_bounds.origin.y + clip_bounds.size.height - position.y);
1097}
1098
1099float4 over(float4 below, float4 above) {
1100  float4 result;
1101  float alpha = above.a + below.a * (1.0 - above.a);
1102  result.rgb =
1103      (above.rgb * above.a + below.rgb * below.a * (1.0 - above.a)) / alpha;
1104  result.a = alpha;
1105  return result;
1106}
1107
1108GradientColor prepare_fill_color(uint tag, uint color_space, Hsla solid,
1109                                     Hsla color0, Hsla color1) {
1110  GradientColor out;
1111  if (tag == 0 || tag == 2) {
1112    out.solid = hsla_to_rgba(solid);
1113  } else if (tag == 1) {
1114    out.color0 = hsla_to_rgba(color0);
1115    out.color1 = hsla_to_rgba(color1);
1116
1117    // Prepare color space in vertex for avoid conversion
1118    // in fragment shader for performance reasons
1119    if (color_space == 1) {
1120      // Oklab
1121      out.color0 = srgb_to_oklab(out.color0);
1122      out.color1 = srgb_to_oklab(out.color1);
1123    }
1124  }
1125
1126  return out;
1127}
1128
1129float2x2 rotate2d(float angle) {
1130    float s = sin(angle);
1131    float c = cos(angle);
1132    return float2x2(c, -s, s, c);
1133}
1134
1135float4 fill_color(Background background,
1136                      float2 position,
1137                      Bounds_ScaledPixels bounds,
1138                      float4 solid_color, float4 color0, float4 color1) {
1139  float4 color;
1140
1141  switch (background.tag) {
1142    case 0:
1143      color = solid_color;
1144      break;
1145    case 1: {
1146      // -90 degrees to match the CSS gradient angle.
1147      float gradient_angle = background.gradient_angle_or_pattern_height;
1148      float radians = (fmod(gradient_angle, 360.0) - 90.0) * (M_PI_F / 180.0);
1149      float2 direction = float2(cos(radians), sin(radians));
1150
1151      // Expand the short side to be the same as the long side
1152      if (bounds.size.width > bounds.size.height) {
1153          direction.y *= bounds.size.height / bounds.size.width;
1154      } else {
1155          direction.x *=  bounds.size.width / bounds.size.height;
1156      }
1157
1158      // Get the t value for the linear gradient with the color stop percentages.
1159      float2 half_size = float2(bounds.size.width, bounds.size.height) / 2.;
1160      float2 center = float2(bounds.origin.x, bounds.origin.y) + half_size;
1161      float2 center_to_point = position - center;
1162      float t = dot(center_to_point, direction) / length(direction);
1163      // Check the direction to determine whether to use x or y
1164      if (abs(direction.x) > abs(direction.y)) {
1165          t = (t + half_size.x) / bounds.size.width;
1166      } else {
1167          t = (t + half_size.y) / bounds.size.height;
1168      }
1169
1170      // Adjust t based on the stop percentages
1171      t = (t - background.colors[0].percentage)
1172        / (background.colors[1].percentage
1173        - background.colors[0].percentage);
1174      t = clamp(t, 0.0, 1.0);
1175
1176      switch (background.color_space) {
1177        case 0:
1178          color = mix(color0, color1, t);
1179          break;
1180        case 1: {
1181          float4 oklab_color = mix(color0, color1, t);
1182          color = oklab_to_srgb(oklab_color);
1183          break;
1184        }
1185      }
1186      break;
1187    }
1188    case 2: {
1189        float gradient_angle_or_pattern_height = background.gradient_angle_or_pattern_height;
1190        float pattern_width = (gradient_angle_or_pattern_height / 65535.0f) / 255.0f;
1191        float pattern_interval = fmod(gradient_angle_or_pattern_height, 65535.0f) / 255.0f;
1192        float pattern_height = pattern_width + pattern_interval;
1193        float stripe_angle = M_PI_F / 4.0;
1194        float pattern_period = pattern_height * sin(stripe_angle);
1195        float2x2 rotation = rotate2d(stripe_angle);
1196        float2 relative_position = position - float2(bounds.origin.x, bounds.origin.y);
1197        float2 rotated_point = rotation * relative_position;
1198        float pattern = fmod(rotated_point.x, pattern_period);
1199        float distance = min(pattern, pattern_period - pattern) - pattern_period * (pattern_width / pattern_height) /  2.0f;
1200        color = solid_color;
1201        color.a *= saturate(0.5 - distance);
1202        break;
1203    }
1204  }
1205
1206  return color;
1207}