shaders.metal

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