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