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