1// Not exposed in shared module 2 3$pure $genIType mix($genIType x, $genIType y, $genBType a); 4$pure $genBType mix($genBType x, $genBType y, $genBType a); 5$pure $genType fma($genType a, $genType b, $genType c); 6$pure $genHType fma($genHType a, $genHType b, $genHType c); 7 $genType frexp($genType x, out $genIType exp); 8 $genHType frexp($genHType x, out $genIType exp); 9$pure $genType ldexp($genType x, in $genIType exp); 10$pure $genHType ldexp($genHType x, in $genIType exp); 11 12$pure uint packSnorm2x16(float2 v); 13$pure uint packUnorm4x8(float4 v); 14$pure uint packSnorm4x8(float4 v); 15$pure float2 unpackSnorm2x16(uint p); 16$pure float4 unpackUnorm4x8(uint p); 17$pure float4 unpackSnorm4x8(uint p); 18$pure uint packHalf2x16(float2 v); 19$pure float2 unpackHalf2x16(uint v); 20 21$pure $genIType bitCount($genIType value); 22$pure $genIType bitCount($genUType value); 23$pure $genIType findLSB($genIType value); 24$pure $genIType findLSB($genUType value); 25$pure $genIType findMSB($genIType value); 26$pure $genIType findMSB($genUType value); 27 28$pure half4 sample(sampler2D s, float2 P); 29$pure half4 sample(sampler2D s, float3 P); 30$pure half4 sample(sampler2D s, float3 P, float bias); 31 32$pure half4 sample(samplerExternalOES s, float2 P); 33$pure half4 sample(samplerExternalOES s, float2 P, float bias); 34 35$pure half4 sample(sampler2DRect s, float2 P); 36$pure half4 sample(sampler2DRect s, float3 P); 37 38$pure half4 sampleLod(sampler2D s, float2 P, float lod); 39$pure half4 sampleLod(sampler2D s, float3 P, float lod); 40 41$pure half4 sampleGrad(sampler2D s, float2, float2 dPdx, float2 dPdy); 42 43// Currently we do not support the generic types of loading subpassInput so we have some explicit 44// versions that we currently use 45$pure half4 subpassLoad(subpassInput subpass); 46$pure half4 subpassLoad(subpassInputMS subpass, int sample); 47 48/** Atomically loads the value from `a` and returns it. */ 49$pure uint atomicLoad(atomicUint a); 50 51/** Atomically stores the value of `value` to `a` */ 52void atomicStore(atomicUint a, uint value); 53 54/** 55 * Performs an atomic addition of `value` to the contents of `a` and returns the original contents 56 * of `a` from before the addition occurred. 57 */ 58uint atomicAdd(atomicUint a, uint value); 59 60// Definitions of functions implementing all of the SkBlendMode blends. 61 62$pure half4 blend_clear(half4 src, half4 dst) { return half4(0); } 63 64$pure half4 blend_src(half4 src, half4 dst) { return src; } 65 66$pure half4 blend_dst(half4 src, half4 dst) { return dst; } 67 68$pure half4 blend_src_over(half4 src, half4 dst) { return src + (1 - src.a)*dst; } 69 70$pure half4 blend_dst_over(half4 src, half4 dst) { return (1 - dst.a)*src + dst; } 71 72$pure half4 blend_src_in(half4 src, half4 dst) { return src*dst.a; } 73 74$pure half4 blend_dst_in(half4 src, half4 dst) { return dst*src.a; } 75 76$pure half4 blend_src_out(half4 src, half4 dst) { return (1 - dst.a)*src; } 77 78$pure half4 blend_dst_out(half4 src, half4 dst) { return (1 - src.a)*dst; } 79 80$pure half4 blend_src_atop(half4 src, half4 dst) { return dst.a*src + (1 - src.a)*dst; } 81 82$pure half4 blend_dst_atop(half4 src, half4 dst) { return (1 - dst.a) * src + src.a*dst; } 83 84$pure half4 blend_xor(half4 src, half4 dst) { return (1 - dst.a)*src + (1 - src.a)*dst; } 85 86// This multi-purpose Porter-Duff blend function can perform any of the twelve blends above, 87// when passed one of the following values for BlendOp: 88// - Clear: 0*src + 0*dst = (0 + 0*dstA)*src + (0 + 0*srcA)*dst = (0, 0, 0, 0) 89// - Src: 1*src + 0*dst = (1 + 0*dstA)*src + (0 + 0*srcA)*dst = (1, 0, 0, 0) 90// - Dst: 0*src + 1*dst = (0 + 0*dstA)*src + (1 + 0*srcA)*dst = (0, 1, 0, 0) 91// - SrcOver: 1*src + (1-srcA)*dst = (1 + 0*dstA)*src + (1 + -1*srcA)*dst = (1, 1, 0, -1) 92// - DstOver: (1-dstA)*src + 1*dst = (1 + -1*dstA)*src + (1 + 0*srcA)*dst = (1, 1, -1, 0) 93// - SrcIn: dstA*src + 0*dst = (0 + 1*dstA)*src + (0 + 0*srcA)*dst = (0, 0, 1, 0) 94// - DstIn: 0*src + srcA*dst = (0 + 0*dstA)*src + (0 + 1*srcA)*dst = (0, 0, 0, 1) 95// - SrcOut: (1-dstA)*src + 0*dst = (1 + -1*dstA)*src + (0 + 0*srcA)*dst = (1, 0, -1, 0) 96// - DstOut: 0*src + (1-srcA)*dst = (0 + 0*dstA)*src + (1 + -1*srcA)*dst = (0, 1, 0, -1) 97// - SrcATop: dstA*src + (1-srcA)*dst = (0 + 1*dstA)*src + (1 + -1*srcA)*dst = (0, 1, 1, -1) 98// - DstATop: (1-dstA)*src + srcA*dst = (1 + -1*dstA)*src + (0 + 1*srcA)*dst = (1, 0, -1, 1) 99// - Xor: (1-dstA)*src + (1-srcA)*dst = (1 + -1*dstA)*src + (1 + -1*srcA)*dst = (1, 1, -1, -1) 100$pure half4 blend_porter_duff(half4 blendOp, half4 src, half4 dst) { 101 // The supported blend modes all have coefficients that are of the form (C + S*alpha), where 102 // alpha is the other color's alpha channel. C can be 0 or 1, S can be -1, 0, or 1. 103 half2 coeff = blendOp.xy + blendOp.zw * half2(dst.a, src.a); 104 return src * coeff.x + dst * coeff.y; 105} 106 107$pure half4 blend_plus(half4 src, half4 dst) { return min(src + dst, 1); } 108 109$pure half4 blend_modulate(half4 src, half4 dst) { return src*dst; } 110 111$pure half4 blend_screen(half4 src, half4 dst) { return src + (1 - src)*dst; } 112 113$pure half $blend_overlay_component(half2 s, half2 d) { 114 return (2*d.x <= d.y) ? 2*s.x*d.x 115 : s.y*d.y - 2*(d.y - d.x)*(s.y - s.x); 116} 117 118$pure half4 blend_overlay(half4 src, half4 dst) { 119 half4 result = half4($blend_overlay_component(src.ra, dst.ra), 120 $blend_overlay_component(src.ga, dst.ga), 121 $blend_overlay_component(src.ba, dst.ba), 122 src.a + (1 - src.a)*dst.a); 123 result.rgb += dst.rgb*(1 - src.a) + src.rgb*(1 - dst.a); 124 return result; 125} 126 127$pure half4 blend_overlay(half flip, half4 a, half4 b) { 128 return blend_overlay(bool(flip) ? b : a, bool(flip) ? a : b); 129} 130 131$pure half4 blend_lighten(half4 src, half4 dst) { 132 half4 result = blend_src_over(src, dst); 133 result.rgb = max(result.rgb, (1 - dst.a)*src.rgb + dst.rgb); 134 return result; 135} 136 137$pure half4 blend_darken(half mode /* darken: 1, lighten: -1 */, half4 src, half4 dst) { 138 half4 a = blend_src_over(src, dst); 139 half3 b = (1 - dst.a) * src.rgb + dst.rgb; // DstOver.rgb 140 a.rgb = mode * min(a.rgb * mode, b.rgb * mode); 141 return a; 142} 143 144$pure half4 blend_darken(half4 src, half4 dst) { 145 return blend_darken(1, src, dst); 146} 147 148// A useful constant to check against when dividing a half-precision denominator. 149// Denormal half floats (values less than this) will compare not-equal to 0 but can easily cause the 150// division to overflow to infinity. Even regular values can overflow given the low maximum value. 151// For instance, any value x > ~3.998 will overflow when divided by $kMinNormalHalf. This is a 152// reasonable value even for wide gamut colors being input to these blend functions, but the 153// most correct denominator check is to treat anything with `denom < x/F16_MAX` as division by 0. 154const half $kMinNormalHalf = 1.0 / (1 << 14); 155 156const half $kGuardedDivideEpsilon = sk_Caps.mustGuardDivisionEvenAfterExplicitZeroCheck 157 ? 0.00000001 158 : 0.0; 159 160$pure inline half $guarded_divide(half n, half d) { 161 return n / (d + $kGuardedDivideEpsilon); 162} 163 164$pure inline half3 $guarded_divide(half3 n, half d) { 165 return n / (d + $kGuardedDivideEpsilon); 166} 167 168$pure half $color_dodge_component(half2 s, half2 d) { 169 // The following is a single flow of control implementation of: 170 // if (d.x == 0) { 171 // return s.x*(1 - d.y); 172 // } else { 173 // half delta = s.y - s.x; 174 // if (delta == 0) { 175 // return s.y*d.y + s.x*(1 - d.y) + d.x*(1 - s.y); 176 // } else { 177 // delta = min(d.y, $guarded_divide(d.x*s.y, delta)); 178 // return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y); 179 // } 180 // } 181 // 182 // When d.x == 0, then dxScale forces delta to 0 and simplifying the return value to s.x*(1-d.y) 183 // When s.y-s.x == 0, the mix selects d.y and min(d.y, d.y) leaves delta = d.y 184 // Otherwise the mix selects the delta expression in the final else branch. 185 half dxScale = d.x == 0 ? 0 : 1; 186 half delta = dxScale * min(d.y, abs(s.y-s.x) >= $kMinNormalHalf 187 ? $guarded_divide(d.x*s.y, s.y-s.x) 188 : d.y); 189 return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y); 190} 191 192$pure half4 blend_color_dodge(half4 src, half4 dst) { 193 return half4($color_dodge_component(src.ra, dst.ra), 194 $color_dodge_component(src.ga, dst.ga), 195 $color_dodge_component(src.ba, dst.ba), 196 src.a + (1 - src.a)*dst.a); 197} 198 199$pure half $color_burn_component(half2 s, half2 d) { 200 // The following is a single flow of control implementation of: 201 // if (d.y == d.x) { 202 // return s.y*d.y + s.x*(1 - d.y) + d.x*(1 - s.y); 203 // } else if (s.x == 0) { 204 // return d.x*(1 - s.y); 205 // } else { 206 // half delta = max(0, d.y - $guarded_divide((d.y - d.x)*s.y, s.x)); 207 // return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y); 208 // } 209 // 210 // When d.y == d.x, dyTerm is d.y. If s.x is also 0, the second ternary selects d.y, matching 211 // the first if condition. If s.x is not 0, then the $guarded_divide() evaluates to 0 and delta 212 // still evaluates to d.y. 213 // 214 // When d.y != d.x but s.x is 0, then dyTerm is 0 and the delta selects 0, matching the second 215 // if condition. 216 // 217 // Lastly, when d.y != d.x and s.x != 0, the delta evaluates to "d.y - min(d.y, 218 // $guarded_divide(...))", which is equivalent to max(0, d.y - $guarded_divide) except that it 219 // has the benefit of not wrapping the d.y evaluation in a max() to preserve the unclamped 220 // behavior when d.y == d.x. 221 half dyTerm = d.y == d.x ? d.y : 0; 222 half delta = abs(s.x) >= $kMinNormalHalf 223 ? d.y - min(d.y, $guarded_divide((d.y - d.x)*s.y, s.x)) 224 : dyTerm; 225 return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y); 226} 227 228$pure half4 blend_color_burn(half4 src, half4 dst) { 229 return half4($color_burn_component(src.ra, dst.ra), 230 $color_burn_component(src.ga, dst.ga), 231 $color_burn_component(src.ba, dst.ba), 232 src.a + (1 - src.a)*dst.a); 233} 234 235$pure half4 blend_hard_light(half4 src, half4 dst) { 236 return blend_overlay(dst, src); 237} 238 239$pure half $soft_light_component(half2 s, half2 d) { 240 if (2*s.x <= s.y) { 241 return $guarded_divide(d.x*d.x*(s.y - 2*s.x), d.y) + (1 - d.y)*s.x + d.x*(-s.y + 2*s.x + 1); 242 } else if (4.0 * d.x <= d.y) { 243 half DSqd = d.x*d.x; 244 half DCub = DSqd*d.x; 245 half DaSqd = d.y*d.y; 246 half DaCub = DaSqd*d.y; 247 return $guarded_divide(DaSqd*(s.x - d.x*(3*s.y - 6*s.x - 1)) + 12*d.y*DSqd*(s.y - 2*s.x) 248 - 16*DCub * (s.y - 2*s.x) - DaCub*s.x, DaSqd); 249 } else { 250 return d.x*(s.y - 2*s.x + 1) + s.x - sqrt(d.y*d.x)*(s.y - 2*s.x) - d.y*s.x; 251 } 252} 253 254$pure half4 blend_soft_light(half4 src, half4 dst) { 255 return (dst.a == 0) ? src : half4($soft_light_component(src.ra, dst.ra), 256 $soft_light_component(src.ga, dst.ga), 257 $soft_light_component(src.ba, dst.ba), 258 src.a + (1 - src.a)*dst.a); 259} 260 261$pure half4 blend_difference(half4 src, half4 dst) { 262 return half4(src.rgb + dst.rgb - 2*min(src.rgb*dst.a, dst.rgb*src.a), 263 src.a + (1 - src.a)*dst.a); 264} 265 266$pure half4 blend_exclusion(half4 src, half4 dst) { 267 return half4(dst.rgb + src.rgb - 2*dst.rgb*src.rgb, src.a + (1 - src.a)*dst.a); 268} 269 270$pure half4 blend_multiply(half4 src, half4 dst) { 271 return half4((1 - src.a)*dst.rgb + (1 - dst.a)*src.rgb + src.rgb*dst.rgb, 272 src.a + (1 - src.a)*dst.a); 273} 274 275$pure half $blend_color_luminance(half3 color) { return dot(half3(0.3, 0.59, 0.11), color); } 276 277$pure half3 $blend_set_color_luminance(half3 hueSatColor, half alpha, half3 lumColor) { 278 half lum = $blend_color_luminance(lumColor); 279 half3 result = lum - $blend_color_luminance(hueSatColor) + hueSatColor; 280 half minComp = min(min(result.r, result.g), result.b); 281 half maxComp = max(max(result.r, result.g), result.b); 282 if (minComp < 0 && lum != minComp) { 283 result = lum + (result - lum) * $guarded_divide(lum, (lum - minComp) + $kMinNormalHalf); 284 } 285 if (maxComp > alpha && maxComp != lum) { 286 result = lum + 287 $guarded_divide((result - lum) * (alpha - lum), (maxComp - lum) + $kMinNormalHalf); 288 } 289 return result; 290} 291 292$pure half $blend_color_saturation(half3 color) { 293 return max(max(color.r, color.g), color.b) - min(min(color.r, color.g), color.b); 294} 295 296$pure half3 $blend_set_color_saturation(half3 color, half3 satColor) { 297 half mn = min(min(color.r, color.g), color.b); 298 half mx = max(max(color.r, color.g), color.b); 299 300 return (mx > mn) ? ((color - mn) * $blend_color_saturation(satColor)) / (mx - mn) 301 : half3(0); 302} 303 304$pure half4 blend_hslc(half2 flipSat, half4 src, half4 dst) { 305 half alpha = dst.a * src.a; 306 half3 sda = src.rgb * dst.a; 307 half3 dsa = dst.rgb * src.a; 308 half3 l = bool(flipSat.x) ? dsa : sda; 309 half3 r = bool(flipSat.x) ? sda : dsa; 310 if (bool(flipSat.y)) { 311 l = $blend_set_color_saturation(l, r); 312 r = dsa; 313 } 314 return half4($blend_set_color_luminance(l, alpha, r) + dst.rgb - dsa + src.rgb - sda, 315 src.a + dst.a - alpha); 316} 317 318$pure half4 blend_hue(half4 src, half4 dst) { 319 return blend_hslc(half2(0, 1), src, dst); 320} 321 322$pure half4 blend_saturation(half4 src, half4 dst) { 323 return blend_hslc(half2(1), src, dst); 324} 325 326$pure half4 blend_color(half4 src, half4 dst) { 327 return blend_hslc(half2(0), src, dst); 328} 329 330$pure half4 blend_luminosity(half4 src, half4 dst) { 331 return blend_hslc(half2(1, 0), src, dst); 332} 333 334$pure float2 proj(float3 p) { return p.xy / p.z; } 335 336// Implement cross() as a determinant to communicate our intent more clearly to the compiler. 337// NOTE: Due to precision issues, it might be the case that cross(a, a) != 0. 338$pure float cross_length_2d(float2 a, float2 b) { 339 return determinant(float2x2(a, b)); 340} 341 342$pure half cross_length_2d(half2 a, half2 b) { 343 return determinant(half2x2(a, b)); 344} 345 346$pure float2 perp(float2 v) { 347 return float2(-v.y, v.x); 348} 349 350$pure half2 perp(half2 v) { 351 return half2(-v.y, v.x); 352} 353 354// Returns a bias given a scale factor, such that 'scale * (dist + bias)' converts the distance to 355// a per-pixel coverage value, automatically widening the visible coverage ramp for subpixel 356// dimensions. The 'scale' must already be equal to the narrowest dimension of the shape and clamped 357// to [0, 1.0]. 358$pure float coverage_bias(float scale) { 359 return 1.0 - 0.5 * scale; 360} 361