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