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