xref: /aosp_15_r20/external/skia/src/sksl/sksl_rt_shader.sksl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1layout(builtin=15) float4 sk_FragCoord;
2
3//--- Luma ------------------------------------------------------------------------
4
5half4 sk_luma(half3 color) {
6    return saturate(dot(half3(0.2126, 0.7152, 0.0722), color)).000r;
7}
8
9//--- Decal ------------------------------------------------------------------------
10
11half4 sk_decal(shader image, float2 coord, float4 decalBounds) {
12    half4 d = half4(decalBounds - coord.xyxy) * half4(-1, -1, 1, 1);
13    d = saturate(d + 0.5);
14    return (d.x * d.y * d.z * d.w) * image.eval(coord);
15}
16
17//--- Displacement -----------------------------------------------------------------
18
19half4 sk_displacement(shader displMap,
20                      shader colorMap,
21                      float2 coord,
22                      half2 scale,
23                      half4 xSelect,  // Only one of RGBA will be 1, the rest are 0
24                      half4 ySelect) {
25    half4 displColor = unpremul(displMap.eval(coord));
26    half2 displ = half2(dot(displColor, xSelect), dot(displColor, ySelect));
27    displ = scale * (displ - 0.5);
28    return colorMap.eval(coord + displ);
29}
30
31//--- Magnifier --------------------------------------------------------------------
32
33half4 sk_magnifier(shader src, float2 coord, float4 lensBounds, float4 zoomXform,
34                   float2 invInset) {
35    float2 zoomCoord = zoomXform.xy + zoomXform.zw*coord;
36    // edgeInset is the smallest distance to the lens bounds edges, in units of "insets".
37    float2 edgeInset = min(coord - lensBounds.xy, lensBounds.zw - coord) * invInset;
38
39    // The equations for 'weight' ensure that it is 0 along the outside of
40    // lensBounds so it seams with any un-zoomed, un-filtered content. The zoomed
41    // content fills a rounded rectangle that is 1 "inset" in from lensBounds with
42    // circular corners with radii equal to the inset distance. Outside of this
43    // region, there is a non-linear weighting to compress the un-zoomed content
44    // to the zoomed content. The critical zone about each corner is limited
45    // to 2x"inset" square.
46    float weight = all(lessThan(edgeInset, float2(2.0)))
47        // Circular distortion weighted by distance to inset corner
48        ? (2.0 - length(2.0 - edgeInset))
49        // Linear zoom, or single-axis compression outside of the inset
50        // area (if delta < 1)
51        : min(edgeInset.x, edgeInset.y);
52
53    // Saturate before squaring so that negative weights are clamped to 0
54    // before squaring
55    weight = saturate(weight);
56    return src.eval(mix(coord, zoomCoord, weight*weight));
57}
58
59//--- High Contrast ----------------------------------------------------------------
60
61$pure half3 $high_contrast_rgb_to_hsl(half3 c) {
62    half mx = max(max(c.r,c.g),c.b),
63         mn = min(min(c.r,c.g),c.b),
64          d = mx-mn,
65       invd = 1.0 / d,
66     g_lt_b = c.g < c.b ? 6.0 : 0.0;
67
68    // We'd prefer to write these tests like `mx == c.r`, but on some GPUs, max(x,y) is
69    // not always equal to either x or y. So we use long form, c.r >= c.g && c.r >= c.b.
70    half h = (1/6.0) * (mx == mn                 ? 0.0 :
71        /*mx==c.r*/     c.r >= c.g && c.r >= c.b ? invd * (c.g - c.b) + g_lt_b :
72        /*mx==c.g*/     c.g >= c.b               ? invd * (c.b - c.r) + 2.0
73        /*mx==c.b*/                              : invd * (c.r - c.g) + 4.0);
74    half sum = mx+mn,
75           l = sum * 0.5,
76           s = mx == mn ? 0.0
77                        : d / (l > 0.5 ? 2.0 - sum : sum);
78    return half3(h,s,l);
79}
80
81half3 sk_high_contrast(half3 color, half grayscale, half invertStyle, half contrast) {
82    if (grayscale == 1) {
83        color = dot(half3(0.2126, 0.7152, 0.0722), color).rrr;
84    }
85    if (invertStyle == 1) {  // brightness
86        color = 1.0 - color;
87    } else if (invertStyle == 2) {  // lightness
88        color = $high_contrast_rgb_to_hsl(color);
89        color.b = 1 - color.b;
90        color = $hsl_to_rgb(color);
91    }
92    return saturate(mix(half3(0.5), color, contrast));
93}
94
95//--- Normal -----------------------------------------------------------------------
96
97$pure half3 $normal_filter(half3 alphaC0, half3 alphaC1, half3 alphaC2, half negSurfaceDepth) {
98    // The right column (or bottom row) terms of the Sobel filter. The left/top is just the
99    // negative, and the middle row/column is all 0s so those instructions are skipped.
100    const half3 kSobel = 0.25 * half3(1,2,1);
101    half3 alphaR0 = half3(alphaC0.x, alphaC1.x, alphaC2.x);
102    half3 alphaR2 = half3(alphaC0.z, alphaC1.z, alphaC2.z);
103    half nx = dot(kSobel, alphaC2) - dot(kSobel, alphaC0);
104    half ny = dot(kSobel, alphaR2) - dot(kSobel, alphaR0);
105    return normalize(half3(negSurfaceDepth * half2(nx, ny), 1));
106}
107
108half4 sk_normal(shader alphaMap, float2 coord, float4 edgeBounds, half negSurfaceDepth) {
109   half3 alphaC0 = half3(
110        alphaMap.eval(clamp(coord + float2(-1,-1), edgeBounds.LT, edgeBounds.RB)).a,
111        alphaMap.eval(clamp(coord + float2(-1, 0), edgeBounds.LT, edgeBounds.RB)).a,
112        alphaMap.eval(clamp(coord + float2(-1, 1), edgeBounds.LT, edgeBounds.RB)).a);
113   half3 alphaC1 = half3(
114        alphaMap.eval(clamp(coord + float2( 0,-1), edgeBounds.LT, edgeBounds.RB)).a,
115        alphaMap.eval(clamp(coord + float2( 0, 0), edgeBounds.LT, edgeBounds.RB)).a,
116        alphaMap.eval(clamp(coord + float2( 0, 1), edgeBounds.LT, edgeBounds.RB)).a);
117   half3 alphaC2 = half3(
118        alphaMap.eval(clamp(coord + float2( 1,-1), edgeBounds.LT, edgeBounds.RB)).a,
119        alphaMap.eval(clamp(coord + float2( 1, 0), edgeBounds.LT, edgeBounds.RB)).a,
120        alphaMap.eval(clamp(coord + float2( 1, 1), edgeBounds.LT, edgeBounds.RB)).a);
121
122   half mainAlpha = alphaC1.y; // offset = (0,0)
123   return half4($normal_filter(alphaC0, alphaC1, alphaC2, negSurfaceDepth), mainAlpha);
124}
125
126//--- Lighting ---------------------------------------------------------------------
127
128$pure half3 $surface_to_light(half lightType, half3 lightPos, half3 lightDir, half3 coord) {
129    // Spot (> 0) and point (== 0) have the same equation
130    return lightType >= 0 ? normalize(lightPos - coord)
131                          : lightDir;
132}
133
134$pure half $spotlight_scale(half3 lightDir, half3 surfaceToLight, half cosCutoffAngle,
135                            half spotFalloff) {
136    const half kConeAAThreshold = 0.016;
137    const half kConeScale = 1.0 / kConeAAThreshold;
138
139    half cosAngle = -dot(surfaceToLight, lightDir);
140    if (cosAngle < cosCutoffAngle) {
141        return 0.0;
142    } else {
143        half scale = pow(cosAngle, spotFalloff);
144        return (cosAngle < cosCutoffAngle + kConeAAThreshold)
145                    ? scale * (cosAngle - cosCutoffAngle) * kConeScale
146                    : scale;
147    }
148}
149
150$pure half4 $compute_lighting(half3 color, half shininess, half materialType, half lightType,
151                              half3 normal, half3 lightDir, half3 surfaceToLight,
152                              half cosCutoffAngle, half spotFalloff) {
153    // Point and distant light color contributions are constant, but
154    // spotlights fade based on the angle away from its direction.
155    if (lightType > 0) {
156        color *= $spotlight_scale(lightDir, surfaceToLight, cosCutoffAngle, spotFalloff);
157    }
158
159    // Diffuse and specular reflections scale the light's color differently
160    if (materialType == 0) {
161        half coeff = dot(normal, surfaceToLight);
162        color = saturate(coeff * color);
163        return half4(color, 1.0);
164    } else {
165        half3 halfDir = normalize(surfaceToLight + half3(0, 0, 1));
166        half coeff = pow(dot(normal, halfDir), shininess);
167        color = saturate(coeff * color);
168        return half4(color, max(max(color.r, color.g), color.b));
169    }
170}
171
172half4 sk_lighting(shader normalMap, float2 coord,
173                  half depth, half shininess, half materialType, half lightType,
174                  half3 lightPos, half spotFalloff,
175                  half3 lightDir, half cosCutoffAngle,
176                  half3 lightColor) {
177    half4 normalAndA = normalMap.eval(coord);
178    half3 surfaceToLight = $surface_to_light(lightType, lightPos, lightDir,
179                                             half3(coord, depth * normalAndA.a));
180    return $compute_lighting(lightColor, shininess, materialType, lightType, normalAndA.xyz,
181                             lightDir, surfaceToLight, cosCutoffAngle, spotFalloff);
182}
183
184//--- Arithmetic Blend -------------------------------------------------------------
185
186half4 sk_arithmetic_blend(half4 src, half4 dst, half4 k, half pmClamp) {
187    half4 color = saturate(k.x * src * dst + k.y * src + k.z * dst + k.w);
188    color.rgb = min(color.rgb, max(color.a, pmClamp));
189    return color;
190}
191
192//--- Sparse Morphology ------------------------------------------------------------
193
194half4 sk_sparse_morphology(shader child, float2 coord, half2 offset, half flip) {
195    half4 aggregate = max(flip * child.eval(coord + offset),
196                          flip * child.eval(coord - offset));
197    return flip * aggregate;
198}
199
200//--- Linear Morphology ------------------------------------------------------------
201
202half4 sk_linear_morphology(shader child, float2 coord, half2 offset, half flip, int radius) {
203
204    // KEEP IN SYNC WITH CONSTANT IN `SkMorphologyImageFilter.cpp`
205    const int kMaxLinearRadius = 14;
206
207    half4 aggregate = flip * child.eval(coord); // case 0 only needs a single sample
208    half2 delta = offset;
209    for (int i = 1; i <= kMaxLinearRadius; ++i) {
210        if (i > radius) break;
211        aggregate = max(aggregate, max(flip * child.eval(coord + delta),
212                                       flip * child.eval(coord - delta)));
213        delta += offset;
214    }
215    return flip * aggregate;
216}
217
218//--- Overdraw ---------------------------------------------------------------------
219
220half4 sk_overdraw(half  alpha,  half4 color0, half4 color1, half4 color2,
221                  half4 color3, half4 color4, half4 color5) {
222   return alpha < (0.5 / 255.) ? color0
223        : alpha < (1.5 / 255.) ? color1
224        : alpha < (2.5 / 255.) ? color2
225        : alpha < (3.5 / 255.) ? color3
226        : alpha < (4.5 / 255.) ? color4
227                               : color5;
228}
229