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