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