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