shaders.hlsl

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