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 Quad {
 457    uint order;
 458    uint border_style;
 459    Bounds bounds;
 460    Bounds content_mask;
 461    Background background;
 462    Hsla border_color;
 463    Corners corner_radii;
 464    Edges border_widths;
 465};
 466
 467struct QuadVertexOutput {
 468    nointerpolation uint quad_id: TEXCOORD0;
 469    float4 position: SV_Position;
 470    nointerpolation float4 border_color: COLOR0;
 471    nointerpolation float4 background_solid: COLOR1;
 472    nointerpolation float4 background_color0: COLOR2;
 473    nointerpolation float4 background_color1: COLOR3;
 474    float4 clip_distance: SV_ClipDistance;
 475};
 476
 477struct QuadFragmentInput {
 478    nointerpolation uint quad_id: TEXCOORD0;
 479    float4 position: SV_Position;
 480    nointerpolation float4 border_color: COLOR0;
 481    nointerpolation float4 background_solid: COLOR1;
 482    nointerpolation float4 background_color0: COLOR2;
 483    nointerpolation float4 background_color1: COLOR3;
 484};
 485
 486StructuredBuffer<Quad> quads: register(t1);
 487
 488QuadVertexOutput quad_vertex(uint vertex_id: SV_VertexID, uint quad_id: SV_InstanceID) {
 489    float2 unit_vertex = float2(float(vertex_id & 1u), 0.5 * float(vertex_id & 2u));
 490    Quad quad = quads[quad_id];
 491    float4 device_position = to_device_position(unit_vertex, quad.bounds);
 492
 493    GradientColor gradient = prepare_gradient_color(
 494        quad.background.tag,
 495        quad.background.color_space,
 496        quad.background.solid,
 497        quad.background.colors
 498    );
 499    float4 clip_distance = distance_from_clip_rect(unit_vertex, quad.bounds, quad.content_mask);
 500    float4 border_color = hsla_to_rgba(quad.border_color);
 501
 502    QuadVertexOutput output;
 503    output.position = device_position;
 504    output.border_color = border_color;
 505    output.quad_id = quad_id;
 506    output.background_solid = gradient.solid;
 507    output.background_color0 = gradient.color0;
 508    output.background_color1 = gradient.color1;
 509    output.clip_distance = clip_distance;
 510    return output;
 511}
 512
 513float4 quad_fragment(QuadFragmentInput input): SV_Target {
 514    Quad quad = quads[input.quad_id];
 515    float4 background_color = gradient_color(quad.background, input.position.xy, quad.bounds,
 516    input.background_solid, input.background_color0, input.background_color1);
 517
 518    bool unrounded = quad.corner_radii.top_left == 0.0 &&
 519        quad.corner_radii.top_right == 0.0 &&
 520        quad.corner_radii.bottom_left == 0.0 &&
 521        quad.corner_radii.bottom_right == 0.0;
 522
 523    // Fast path when the quad is not rounded and doesn't have any border
 524    if (quad.border_widths.top == 0.0 &&
 525        quad.border_widths.left == 0.0 &&
 526        quad.border_widths.right == 0.0 &&
 527        quad.border_widths.bottom == 0.0 &&
 528        unrounded) {
 529        return background_color;
 530    }
 531
 532    float2 size = quad.bounds.size;
 533    float2 half_size = size / 2.;
 534    float2 the_point = input.position.xy - quad.bounds.origin;
 535    float2 center_to_point = the_point - half_size;
 536
 537    // Signed distance field threshold for inclusion of pixels. 0.5 is the
 538    // minimum distance between the center of the pixel and the edge.
 539    const float antialias_threshold = 0.5;
 540
 541    // Radius of the nearest corner
 542    float corner_radius = pick_corner_radius(center_to_point, quad.corner_radii);
 543
 544    float2 border = float2(
 545        center_to_point.x < 0.0 ? quad.border_widths.left : quad.border_widths.right,
 546        center_to_point.y < 0.0 ? quad.border_widths.top : quad.border_widths.bottom
 547    );
 548
 549    // 0-width borders are reduced so that `inner_sdf >= antialias_threshold`.
 550    // The purpose of this is to not draw antialiasing pixels in this case.
 551    float2 reduced_border = float2(
 552        border.x == 0.0 ? -antialias_threshold : border.x,
 553        border.y == 0.0 ? -antialias_threshold : border.y
 554    );
 555
 556    // Vector from the corner of the quad bounds to the point, after mirroring
 557    // the point into the bottom right quadrant. Both components are <= 0.
 558    float2 corner_to_point = abs(center_to_point) - half_size;
 559
 560    // Vector from the point to the center of the rounded corner's circle, also
 561    // mirrored into bottom right quadrant.
 562    float2 corner_center_to_point = corner_to_point + corner_radius;
 563
 564    // Whether the nearest point on the border is rounded
 565    bool is_near_rounded_corner =
 566        corner_center_to_point.x >= 0.0 &&
 567        corner_center_to_point.y >= 0.0;
 568
 569    // Vector from straight border inner corner to point.
 570    //
 571    // 0-width borders are turned into width -1 so that inner_sdf is > 1.0 near
 572    // the border. Without this, antialiasing pixels would be drawn.
 573    float2 straight_border_inner_corner_to_point = corner_to_point + reduced_border;
 574
 575    // Whether the point is beyond the inner edge of the straight border
 576    bool is_beyond_inner_straight_border =
 577        straight_border_inner_corner_to_point.x > 0.0 ||
 578        straight_border_inner_corner_to_point.y > 0.0;
 579
 580    // Whether the point is far enough inside the quad, such that the pixels are
 581    // not affected by the straight border.
 582    bool is_within_inner_straight_border =
 583        straight_border_inner_corner_to_point.x < -antialias_threshold &&
 584        straight_border_inner_corner_to_point.y < -antialias_threshold;
 585
 586    // Fast path for points that must be part of the background
 587    if (is_within_inner_straight_border && !is_near_rounded_corner) {
 588        return background_color;
 589    }
 590
 591    // Signed distance of the point to the outside edge of the quad's border
 592    float outer_sdf = quad_sdf_impl(corner_center_to_point, corner_radius);
 593
 594    // Approximate signed distance of the point to the inside edge of the quad's
 595    // border. It is negative outside this edge (within the border), and
 596    // positive inside.
 597    //
 598    // This is not always an accurate signed distance:
 599    // * The rounded portions with varying border width use an approximation of
 600    //   nearest-point-on-ellipse.
 601    // * When it is quickly known to be outside the edge, -1.0 is used.
 602    float inner_sdf = 0.0;
 603    if (corner_center_to_point.x <= 0.0 || corner_center_to_point.y <= 0.0) {
 604        // Fast paths for straight borders
 605        inner_sdf = -max(straight_border_inner_corner_to_point.x,
 606                        straight_border_inner_corner_to_point.y);
 607    } else if (is_beyond_inner_straight_border) {
 608        // Fast path for points that must be outside the inner edge
 609        inner_sdf = -1.0;
 610    } else if (reduced_border.x == reduced_border.y) {
 611        // Fast path for circular inner edge.
 612        inner_sdf = -(outer_sdf + reduced_border.x);
 613    } else {
 614        float2 ellipse_radii = max(float2(0.0, 0.0), float2(corner_radius, corner_radius) - reduced_border);
 615        inner_sdf = quarter_ellipse_sdf(corner_center_to_point, ellipse_radii);
 616    }
 617
 618    // Negative when inside the border
 619    float border_sdf = max(inner_sdf, outer_sdf);
 620
 621    float4 color = background_color;
 622    if (border_sdf < antialias_threshold) {
 623        float4 border_color = input.border_color;
 624        // Dashed border logic when border_style == 1
 625        if (quad.border_style == 1) {
 626            // Position along the perimeter in "dash space", where each dash
 627            // period has length 1
 628            float t = 0.0;
 629
 630            // Total number of dash periods, so that the dash spacing can be
 631            // adjusted to evenly divide it
 632            float max_t = 0.0;
 633
 634            // Border width is proportional to dash size. This is the behavior
 635            // used by browsers, but also avoids dashes from different segments
 636            // overlapping when dash size is smaller than the border width.
 637            //
 638            // Dash pattern: (2 * border width) dash, (1 * border width) gap
 639            const float dash_length_per_width = 2.0;
 640            const float dash_gap_per_width = 1.0;
 641            const float dash_period_per_width = dash_length_per_width + dash_gap_per_width;
 642
 643            // Since the dash size is determined by border width, the density of
 644            // dashes varies. Multiplying a pixel distance by this returns a
 645            // position in dash space - it has units (dash period / pixels). So
 646            // a dash velocity of (1 / 10) is 1 dash every 10 pixels.
 647            float dash_velocity = 0.0;
 648
 649            // Dividing this by the border width gives the dash velocity
 650            const float dv_numerator = 1.0 / dash_period_per_width;
 651
 652            if (unrounded) {
 653                // When corners aren't rounded, the dashes are separately laid
 654                // out on each straight line, rather than around the whole
 655                // perimeter. This way each line starts and ends with a dash.
 656                bool is_horizontal = corner_center_to_point.x < corner_center_to_point.y;
 657                float border_width = is_horizontal ? border.x : border.y;
 658                dash_velocity = dv_numerator / border_width;
 659                t = is_horizontal ? the_point.x : the_point.y;
 660                t *= dash_velocity;
 661                max_t = is_horizontal ? size.x : size.y;
 662                max_t *= dash_velocity;
 663            } else {
 664                // When corners are rounded, the dashes are laid out clockwise
 665                // around the whole perimeter.
 666
 667                float r_tr = quad.corner_radii.top_right;
 668                float r_br = quad.corner_radii.bottom_right;
 669                float r_bl = quad.corner_radii.bottom_left;
 670                float r_tl = quad.corner_radii.top_left;
 671
 672                float w_t = quad.border_widths.top;
 673                float w_r = quad.border_widths.right;
 674                float w_b = quad.border_widths.bottom;
 675                float w_l = quad.border_widths.left;
 676
 677                // Straight side dash velocities
 678                float dv_t = w_t <= 0.0 ? 0.0 : dv_numerator / w_t;
 679                float dv_r = w_r <= 0.0 ? 0.0 : dv_numerator / w_r;
 680                float dv_b = w_b <= 0.0 ? 0.0 : dv_numerator / w_b;
 681                float dv_l = w_l <= 0.0 ? 0.0 : dv_numerator / w_l;
 682
 683                // Straight side lengths in dash space
 684                float s_t = (size.x - r_tl - r_tr) * dv_t;
 685                float s_r = (size.y - r_tr - r_br) * dv_r;
 686                float s_b = (size.x - r_br - r_bl) * dv_b;
 687                float s_l = (size.y - r_bl - r_tl) * dv_l;
 688
 689                float corner_dash_velocity_tr = corner_dash_velocity(dv_t, dv_r);
 690                float corner_dash_velocity_br = corner_dash_velocity(dv_b, dv_r);
 691                float corner_dash_velocity_bl = corner_dash_velocity(dv_b, dv_l);
 692                float corner_dash_velocity_tl = corner_dash_velocity(dv_t, dv_l);
 693
 694                // Corner lengths in dash space
 695                float c_tr = r_tr * (M_PI_F / 2.0) * corner_dash_velocity_tr;
 696                float c_br = r_br * (M_PI_F / 2.0) * corner_dash_velocity_br;
 697                float c_bl = r_bl * (M_PI_F / 2.0) * corner_dash_velocity_bl;
 698                float c_tl = r_tl * (M_PI_F / 2.0) * corner_dash_velocity_tl;
 699
 700                // Cumulative dash space upto each segment
 701                float upto_tr = s_t;
 702                float upto_r = upto_tr + c_tr;
 703                float upto_br = upto_r + s_r;
 704                float upto_b = upto_br + c_br;
 705                float upto_bl = upto_b + s_b;
 706                float upto_l = upto_bl + c_bl;
 707                float upto_tl = upto_l + s_l;
 708                max_t = upto_tl + c_tl;
 709
 710                if (is_near_rounded_corner) {
 711                    float radians = atan2(corner_center_to_point.y, corner_center_to_point.x);
 712                    float corner_t = radians * corner_radius;
 713
 714                    if (center_to_point.x >= 0.0) {
 715                        if (center_to_point.y < 0.0) {
 716                            dash_velocity = corner_dash_velocity_tr;
 717                            // Subtracted because radians is pi/2 to 0 when
 718                            // going clockwise around the top right corner,
 719                            // since the y axis has been flipped
 720                            t = upto_r - corner_t * dash_velocity;
 721                        } else {
 722                            dash_velocity = corner_dash_velocity_br;
 723                            // Added because radians is 0 to pi/2 when going
 724                            // clockwise around the bottom-right corner
 725                            t = upto_br + corner_t * dash_velocity;
 726                        }
 727                    } else {
 728                        if (center_to_point.y >= 0.0) {
 729                            dash_velocity = corner_dash_velocity_bl;
 730                            // Subtracted because radians is pi/1 to 0 when
 731                            // going clockwise around the bottom-left corner,
 732                            // since the x axis has been flipped
 733                            t = upto_l - corner_t * dash_velocity;
 734                        } else {
 735                            dash_velocity = corner_dash_velocity_tl;
 736                            // Added because radians is 0 to pi/2 when going
 737                            // clockwise around the top-left corner, since both
 738                            // axis were flipped
 739                            t = upto_tl + corner_t * dash_velocity;
 740                        }
 741                    }
 742                } else {
 743                    // Straight borders
 744                    bool is_horizontal = corner_center_to_point.x < corner_center_to_point.y;
 745                    if (is_horizontal) {
 746                        if (center_to_point.y < 0.0) {
 747                            dash_velocity = dv_t;
 748                            t = (the_point.x - r_tl) * dash_velocity;
 749                        } else {
 750                            dash_velocity = dv_b;
 751                            t = upto_bl - (the_point.x - r_bl) * dash_velocity;
 752                        }
 753                    } else {
 754                        if (center_to_point.x < 0.0) {
 755                            dash_velocity = dv_l;
 756                            t = upto_tl - (the_point.y - r_tl) * dash_velocity;
 757                        } else {
 758                            dash_velocity = dv_r;
 759                            t = upto_r + (the_point.y - r_tr) * dash_velocity;
 760                        }
 761                    }
 762                }
 763            }
 764            float dash_length = dash_length_per_width / dash_period_per_width;
 765            float desired_dash_gap = dash_gap_per_width / dash_period_per_width;
 766
 767            // Straight borders should start and end with a dash, so max_t is
 768            // reduced to cause this.
 769            max_t -= unrounded ? dash_length : 0.0;
 770            if (max_t >= 1.0) {
 771                // Adjust dash gap to evenly divide max_t
 772                float dash_count = floor(max_t);
 773                float dash_period = max_t / dash_count;
 774                border_color.a *= dash_alpha(t, dash_period, dash_length, dash_velocity, antialias_threshold);
 775            } else if (unrounded) {
 776                // When there isn't enough space for the full gap between the
 777                // two start / end dashes of a straight border, reduce gap to
 778                // make them fit.
 779                float dash_gap = max_t - dash_length;
 780                if (dash_gap > 0.0) {
 781                    float dash_period = dash_length + dash_gap;
 782                    border_color.a *= dash_alpha(t, dash_period, dash_length, dash_velocity, antialias_threshold);
 783                }
 784            }
 785        }
 786
 787        // Blend the border on top of the background and then linearly interpolate
 788        // between the two as we slide inside the background.
 789        float4 blended_border = over(background_color, border_color);
 790        color = lerp(background_color, blended_border,
 791                    saturate(antialias_threshold - inner_sdf));
 792    }
 793
 794    return color * float4(1.0, 1.0, 1.0, saturate(antialias_threshold - outer_sdf));
 795}
 796
 797/*
 798**
 799**              Shadows
 800**
 801*/
 802
 803struct Shadow {
 804    uint order;
 805    float blur_radius;
 806    Bounds bounds;
 807    Corners corner_radii;
 808    Bounds content_mask;
 809    Hsla color;
 810};
 811
 812struct ShadowVertexOutput {
 813    nointerpolation uint shadow_id: TEXCOORD0;
 814    float4 position: SV_Position;
 815    nointerpolation float4 color: COLOR;
 816    float4 clip_distance: SV_ClipDistance;
 817};
 818
 819struct ShadowFragmentInput {
 820  nointerpolation uint shadow_id: TEXCOORD0;
 821  float4 position: SV_Position;
 822  nointerpolation float4 color: COLOR;
 823};
 824
 825StructuredBuffer<Shadow> shadows: register(t1);
 826
 827ShadowVertexOutput shadow_vertex(uint vertex_id: SV_VertexID, uint shadow_id: SV_InstanceID) {
 828    float2 unit_vertex = float2(float(vertex_id & 1u), 0.5 * float(vertex_id & 2u));
 829    Shadow shadow = shadows[shadow_id];
 830
 831    float margin = 3.0 * shadow.blur_radius;
 832    Bounds bounds = shadow.bounds;
 833    bounds.origin -= margin;
 834    bounds.size += 2.0 * margin;
 835
 836    float4 device_position = to_device_position(unit_vertex, bounds);
 837    float4 clip_distance = distance_from_clip_rect(unit_vertex, bounds, shadow.content_mask);
 838    float4 color = hsla_to_rgba(shadow.color);
 839
 840    ShadowVertexOutput output;
 841    output.position = device_position;
 842    output.color = color;
 843    output.shadow_id = shadow_id;
 844    output.clip_distance = clip_distance;
 845
 846    return output;
 847}
 848
 849float4 shadow_fragment(ShadowFragmentInput input): SV_TARGET {
 850    Shadow shadow = shadows[input.shadow_id];
 851
 852    float2 half_size = shadow.bounds.size / 2.;
 853    float2 center = shadow.bounds.origin + half_size;
 854    float2 point0 = input.position.xy - center;
 855    float corner_radius = pick_corner_radius(point0, shadow.corner_radii);
 856
 857    // The signal is only non-zero in a limited range, so don't waste samples
 858    float low = point0.y - half_size.y;
 859    float high = point0.y + half_size.y;
 860    float start = clamp(-3. * shadow.blur_radius, low, high);
 861    float end = clamp(3. * shadow.blur_radius, low, high);
 862
 863    // Accumulate samples (we can get away with surprisingly few samples)
 864    float step = (end - start) / 4.;
 865    float y = start + step * 0.5;
 866    float alpha = 0.;
 867    for (int i = 0; i < 4; i++) {
 868        alpha += blur_along_x(point0.x, point0.y - y, shadow.blur_radius,
 869                            corner_radius, half_size) *
 870                gaussian(y, shadow.blur_radius) * step;
 871        y += step;
 872    }
 873
 874    return input.color * float4(1., 1., 1., alpha);
 875}
 876
 877/*
 878**
 879**              Path Rasterization
 880**
 881*/
 882
 883struct PathRasterizationSprite {
 884    float2 xy_position;
 885    float2 st_position;
 886    Background color;
 887    Bounds bounds;
 888};
 889
 890StructuredBuffer<PathRasterizationSprite> path_rasterization_sprites: register(t1);
 891
 892struct PathVertexOutput {
 893    float4 position: SV_Position;
 894    float2 st_position: TEXCOORD0;
 895    nointerpolation uint vertex_id: TEXCOORD1;
 896    float4 clip_distance: SV_ClipDistance;
 897};
 898
 899struct PathFragmentInput {
 900    float4 position: SV_Position;
 901    float2 st_position: TEXCOORD0;
 902    nointerpolation uint vertex_id: TEXCOORD1;
 903};
 904
 905PathVertexOutput path_rasterization_vertex(uint vertex_id: SV_VertexID) {
 906    PathRasterizationSprite sprite = path_rasterization_sprites[vertex_id];
 907
 908    PathVertexOutput output;
 909    output.position = to_device_position_impl(sprite.xy_position);
 910    output.st_position = sprite.st_position;
 911    output.vertex_id = vertex_id;
 912    output.clip_distance = distance_from_clip_rect_impl(sprite.xy_position, sprite.bounds);
 913
 914    return output;
 915}
 916
 917float4 path_rasterization_fragment(PathFragmentInput input): SV_Target {
 918    float2 dx = ddx(input.st_position);
 919    float2 dy = ddy(input.st_position);
 920    PathRasterizationSprite sprite = path_rasterization_sprites[input.vertex_id];
 921
 922    Background background = sprite.color;
 923    Bounds bounds = sprite.bounds;
 924
 925    float alpha;
 926    if (length(float2(dx.x, dy.x))) {
 927        alpha = 1.0;
 928    } else {
 929        float2 gradient = 2.0 * input.st_position.xx * float2(dx.x, dy.x) - float2(dx.y, dy.y);
 930        float f = input.st_position.x * input.st_position.x - input.st_position.y;
 931        float distance = f / length(gradient);
 932        alpha = saturate(0.5 - distance);
 933    }
 934
 935    GradientColor gradient = prepare_gradient_color(
 936        background.tag, background.color_space, background.solid, background.colors);
 937
 938    float4 color = gradient_color(background, input.position.xy, bounds,
 939        gradient.solid, gradient.color0, gradient.color1);
 940    return float4(color.rgb * color.a * alpha, alpha * color.a);
 941}
 942
 943/*
 944**
 945**              Path Sprites
 946**
 947*/
 948
 949struct PathSprite {
 950    Bounds bounds;
 951};
 952
 953struct PathSpriteVertexOutput {
 954    float4 position: SV_Position;
 955    float2 texture_coords: TEXCOORD0;
 956};
 957
 958StructuredBuffer<PathSprite> path_sprites: register(t1);
 959
 960PathSpriteVertexOutput path_sprite_vertex(uint vertex_id: SV_VertexID, uint sprite_id: SV_InstanceID) {
 961    float2 unit_vertex = float2(float(vertex_id & 1u), 0.5 * float(vertex_id & 2u));
 962    PathSprite sprite = path_sprites[sprite_id];
 963
 964    // Don't apply content mask because it was already accounted for when rasterizing the path
 965    float4 device_position = to_device_position(unit_vertex, sprite.bounds);
 966
 967    float2 screen_position = sprite.bounds.origin + unit_vertex * sprite.bounds.size;
 968    float2 texture_coords = screen_position / global_viewport_size;
 969
 970    PathSpriteVertexOutput output;
 971    output.position = device_position;
 972    output.texture_coords = texture_coords;
 973    return output;
 974}
 975
 976float4 path_sprite_fragment(PathSpriteVertexOutput input): SV_Target {
 977    return t_sprite.Sample(s_sprite, input.texture_coords);
 978}
 979
 980/*
 981**
 982**              Underlines
 983**
 984*/
 985
 986struct Underline {
 987    uint order;
 988    uint pad;
 989    Bounds bounds;
 990    Bounds content_mask;
 991    Hsla color;
 992    float thickness;
 993    uint wavy;
 994};
 995
 996struct UnderlineVertexOutput {
 997  nointerpolation uint underline_id: TEXCOORD0;
 998  float4 position: SV_Position;
 999  nointerpolation float4 color: COLOR;
1000  float4 clip_distance: SV_ClipDistance;
1001};
1002
1003struct UnderlineFragmentInput {
1004  nointerpolation uint underline_id: TEXCOORD0;
1005  float4 position: SV_Position;
1006  nointerpolation float4 color: COLOR;
1007};
1008
1009StructuredBuffer<Underline> underlines: register(t1);
1010
1011UnderlineVertexOutput underline_vertex(uint vertex_id: SV_VertexID, uint underline_id: SV_InstanceID) {
1012    float2 unit_vertex = float2(float(vertex_id & 1u), 0.5 * float(vertex_id & 2u));
1013    Underline underline = underlines[underline_id];
1014    float4 device_position = to_device_position(unit_vertex, underline.bounds);
1015    float4 clip_distance = distance_from_clip_rect(unit_vertex, underline.bounds,
1016                                                    underline.content_mask);
1017    float4 color = hsla_to_rgba(underline.color);
1018
1019    UnderlineVertexOutput output;
1020    output.position = device_position;
1021    output.color = color;
1022    output.underline_id = underline_id;
1023    output.clip_distance = clip_distance;
1024    return output;
1025}
1026
1027float4 underline_fragment(UnderlineFragmentInput input): SV_Target {
1028    const float WAVE_FREQUENCY = 2.0;
1029    const float WAVE_HEIGHT_RATIO = 0.8;
1030
1031    Underline underline = underlines[input.underline_id];
1032    if (underline.wavy) {
1033        float half_thickness = underline.thickness * 0.5;
1034        float2 origin = underline.bounds.origin;
1035
1036        float2 st = ((input.position.xy - origin) / underline.bounds.size.y) - float2(0., 0.5);
1037        float frequency = (M_PI_F * WAVE_FREQUENCY * underline.thickness) / underline.bounds.size.y;
1038        float amplitude = (underline.thickness * WAVE_HEIGHT_RATIO) / underline.bounds.size.y;
1039
1040        float sine = sin(st.x * frequency) * amplitude;
1041        float dSine = cos(st.x * frequency) * amplitude * frequency;
1042        float distance = (st.y - sine) / sqrt(1. + dSine * dSine);
1043        float distance_in_pixels = distance * underline.bounds.size.y;
1044        float distance_from_top_border = distance_in_pixels - half_thickness;
1045        float distance_from_bottom_border = distance_in_pixels + half_thickness;
1046        float alpha = saturate(
1047            0.5 - max(-distance_from_bottom_border, distance_from_top_border));
1048        return input.color * float4(1., 1., 1., alpha);
1049    } else {
1050        return input.color;
1051    }
1052}
1053
1054/*
1055**
1056**              Monochrome sprites
1057**
1058*/
1059
1060struct MonochromeSprite {
1061    uint order;
1062    uint pad;
1063    Bounds bounds;
1064    Bounds content_mask;
1065    Hsla color;
1066    AtlasTile tile;
1067    TransformationMatrix transformation;
1068};
1069
1070struct MonochromeSpriteVertexOutput {
1071    float4 position: SV_Position;
1072    float2 tile_position: POSITION;
1073    nointerpolation float4 color: COLOR;
1074    float4 clip_distance: SV_ClipDistance;
1075};
1076
1077struct MonochromeSpriteFragmentInput {
1078    float4 position: SV_Position;
1079    float2 tile_position: POSITION;
1080    nointerpolation float4 color: COLOR;
1081    float4 clip_distance: SV_ClipDistance;
1082};
1083
1084StructuredBuffer<MonochromeSprite> mono_sprites: register(t1);
1085
1086MonochromeSpriteVertexOutput monochrome_sprite_vertex(uint vertex_id: SV_VertexID, uint sprite_id: SV_InstanceID) {
1087    float2 unit_vertex = float2(float(vertex_id & 1u), 0.5 * float(vertex_id & 2u));
1088    MonochromeSprite sprite = mono_sprites[sprite_id];
1089    float4 device_position =
1090        to_device_position_transformed(unit_vertex, sprite.bounds, sprite.transformation);
1091    float4 clip_distance = distance_from_clip_rect(unit_vertex, sprite.bounds, sprite.content_mask);
1092    float2 tile_position = to_tile_position(unit_vertex, sprite.tile);
1093    float4 color = hsla_to_rgba(sprite.color);
1094
1095    MonochromeSpriteVertexOutput output;
1096    output.position = device_position;
1097    output.tile_position = tile_position;
1098    output.color = color;
1099    output.clip_distance = clip_distance;
1100    return output;
1101}
1102
1103float4 monochrome_sprite_fragment(MonochromeSpriteFragmentInput input): SV_Target {
1104    float sample = t_sprite.Sample(s_sprite, input.tile_position).r;
1105    float alpha_corrected = apply_contrast_and_gamma_correction(sample, input.color.rgb, grayscale_enhanced_contrast, gamma_ratios);
1106    return float4(input.color.rgb, input.color.a * alpha_corrected);
1107}
1108
1109/*
1110**
1111**              Polychrome sprites
1112**
1113*/
1114
1115struct PolychromeSprite {
1116    uint order;
1117    uint pad;
1118    uint grayscale;
1119    float opacity;
1120    Bounds bounds;
1121    Bounds content_mask;
1122    Corners corner_radii;
1123    AtlasTile tile;
1124};
1125
1126struct PolychromeSpriteVertexOutput {
1127    nointerpolation uint sprite_id: TEXCOORD0;
1128    float4 position: SV_Position;
1129    float2 tile_position: POSITION;
1130    float4 clip_distance: SV_ClipDistance;
1131};
1132
1133struct PolychromeSpriteFragmentInput {
1134    nointerpolation uint sprite_id: TEXCOORD0;
1135    float4 position: SV_Position;
1136    float2 tile_position: POSITION;
1137};
1138
1139StructuredBuffer<PolychromeSprite> poly_sprites: register(t1);
1140
1141PolychromeSpriteVertexOutput polychrome_sprite_vertex(uint vertex_id: SV_VertexID, uint sprite_id: SV_InstanceID) {
1142    float2 unit_vertex = float2(float(vertex_id & 1u), 0.5 * float(vertex_id & 2u));
1143    PolychromeSprite sprite = poly_sprites[sprite_id];
1144    float4 device_position = to_device_position(unit_vertex, sprite.bounds);
1145    float4 clip_distance = distance_from_clip_rect(unit_vertex, sprite.bounds,
1146                                                    sprite.content_mask);
1147    float2 tile_position = to_tile_position(unit_vertex, sprite.tile);
1148
1149    PolychromeSpriteVertexOutput output;
1150    output.position = device_position;
1151    output.tile_position = tile_position;
1152    output.sprite_id = sprite_id;
1153    output.clip_distance = clip_distance;
1154    return output;
1155}
1156
1157float4 polychrome_sprite_fragment(PolychromeSpriteFragmentInput input): SV_Target {
1158    PolychromeSprite sprite = poly_sprites[input.sprite_id];
1159    float4 sample = t_sprite.Sample(s_sprite, input.tile_position);
1160    float distance = quad_sdf(input.position.xy, sprite.bounds, sprite.corner_radii);
1161
1162    float4 color = sample;
1163    if ((sprite.grayscale & 0xFFu) != 0u) {
1164        float3 grayscale = dot(color.rgb, GRAYSCALE_FACTORS);
1165        color = float4(grayscale, sample.a);
1166    }
1167    color.a *= sprite.opacity * saturate(0.5 - distance);
1168    return color;
1169}