1// defines built-in functions supported by SkSL when running on a GPU 2 3$genType radians($genType degrees); 4$genType degrees($genType radians); 5$genType sin($genType angle); 6$genType cos($genType angle); 7$genType tan($genType angle); 8$genType asin($genType x); 9$genType acos($genType x); 10$genType atan($genType y, $genType x); 11$genType atan($genType y_over_x); 12$genType sinh($genType x); 13$genType cosh($genType x); 14$genType tanh($genType x); 15$genType asinh($genType x); 16$genType acosh($genType x); 17$genType atanh($genType x); 18$genType pow($genType x, $genType y); 19$genType exp($genType x); 20$genType log($genType x); 21$genType exp2($genType x); 22$genType log2($genType x); 23$genType sqrt($genType x); 24$genHType radians($genHType degrees); 25$genHType degrees($genHType radians); 26$genHType sin($genHType angle); 27$genHType cos($genHType angle); 28$genHType tan($genHType angle); 29$genHType asin($genHType x); 30$genHType acos($genHType x); 31$genHType atan($genHType y, $genHType x); 32$genHType atan($genHType y_over_x); 33$genHType sinh($genHType x); 34$genHType cosh($genHType x); 35$genHType tanh($genHType x); 36$genHType asinh($genHType x); 37$genHType acosh($genHType x); 38$genHType atanh($genHType x); 39$genHType pow($genHType x, $genHType y); 40$genHType exp($genHType x); 41$genHType log($genHType x); 42$genHType exp2($genHType x); 43$genHType log2($genHType x); 44$genHType sqrt($genHType x); 45$genType inversesqrt($genType x); 46$genHType inversesqrt($genHType x); 47$genType abs($genType x); 48$genHType abs($genHType x); 49$genIType abs($genIType x); 50$genType sign($genType x); 51$genHType sign($genHType x); 52$genIType sign($genIType x); 53$genType floor($genType x); 54$genHType floor($genHType x); 55$genType trunc($genType x); 56$genHType trunc($genHType x); 57$genType round($genType x); 58$genHType round($genHType x); 59$genType roundEven($genType x); 60$genHType roundEven($genHType x); 61$genType ceil($genType x); 62$genHType ceil($genHType x); 63$genType fract($genType x); 64$genHType fract($genHType x); 65$genType mod($genType x, float y); 66$genType mod($genType x, $genType y); 67$genHType mod($genHType x, half y); 68$genHType mod($genHType x, $genHType y); 69$genType modf($genType x, out $genType i); 70$genHType modf($genHType x, out $genHType i); 71$genType min($genType x, $genType y); 72$genType min($genType x, float y); 73$genHType min($genHType x, $genHType y); 74$genHType min($genHType x, half y); 75$genIType min($genIType x, $genIType y); 76$genIType min($genIType x, int y); 77$genType max($genType x, $genType y); 78$genType max($genType x, float y); 79$genHType max($genHType x, $genHType y); 80$genHType max($genHType x, half y); 81$genIType max($genIType x, $genIType y); 82$genIType max($genIType x, int y); 83$genType clamp($genType x, $genType minVal, $genType maxVal); 84$genType clamp($genType x, float minVal, float maxVal); 85$genHType clamp($genHType x, $genHType minVal, $genHType maxVal); 86$genHType clamp($genHType x, half minVal, half maxVal); 87$genIType clamp($genIType x, $genIType minVal, $genIType maxVal); 88$genIType clamp($genIType x, int minVal, int maxVal); 89$genUType clamp($genUType x, $genUType minVal, $genUType maxVal); 90$genUType clamp($genUType x, uint minVal, uint maxVal); 91$genType saturate($genType x); 92$genHType saturate($genHType x); 93$genType mix($genType x, $genType y, $genType a); 94$genType mix($genType x, $genType y, float a); 95$genHType mix($genHType x, $genHType y, $genHType a); 96$genHType mix($genHType x, $genHType y, half a); 97$genType mix($genType x, $genType y, $genBType a); 98$genHType mix($genHType x, $genHType y, $genBType a); 99$genIType mix($genIType x, $genIType y, $genBType a); 100$genBType mix($genBType x, $genBType y, $genBType a); 101$genType step($genType edge, $genType x); 102$genType step(float edge, $genType x); 103$genHType step($genHType edge, $genHType x); 104$genHType step(half edge, $genHType x); 105$genType smoothstep($genType edge0, $genType edge1, $genType x); 106$genType smoothstep(float edge0, float edge1, $genType x); 107$genHType smoothstep($genHType edge0, $genHType edge1, $genHType x); 108$genHType smoothstep(half edge0, half edge1, $genHType x); 109$genBType isnan($genType x); 110$genBType isinf($genType x); 111$genIType floatBitsToInt($genType value); 112$genUType floatBitsToUint($genType value); 113$genType intBitsToFloat($genIType value); 114$genType uintBitsToFloat($genUType value); 115$genType fma($genType a, $genType b, $genType c); 116$genHType fma($genHType a, $genHType b, $genHType c); 117sk_has_side_effects $genType frexp($genType x, out $genIType exp); 118sk_has_side_effects $genHType frexp($genHType x, out $genIType exp); 119$genType ldexp($genType x, in $genIType exp); 120$genHType ldexp($genHType x, in $genIType exp); 121uint packUnorm2x16(float2 v); 122uint packSnorm2x16(float2 v); 123uint packUnorm4x8(float4 v); 124uint packSnorm4x8(float4 v); 125float2 unpackUnorm2x16(uint p); 126float2 unpackSnorm2x16(uint p); 127float4 unpackUnorm4x8(uint p); 128float4 unpackSnorm4x8(uint p); 129uint packHalf2x16(float2 v); 130float2 unpackHalf2x16(uint v); 131float length($genType x); 132half length($genHType x); 133float distance($genType p0, $genType p1); 134half distance($genHType p0, $genHType p1); 135float dot($genType x, $genType y); 136half dot($genHType x, $genHType y); 137float3 cross(float3 x, float3 y); 138half3 cross(half3 x, half3 y); 139$genType normalize($genType x); 140$genHType normalize($genHType x); 141$genType faceforward($genType N, $genType I, $genType Nref); 142$genHType faceforward($genHType N, $genHType I, $genHType Nref); 143$genType reflect($genType I, $genType N); 144$genHType reflect($genHType I, $genHType N); 145$genType refract($genType I, $genType N, float eta); 146$genHType refract($genHType I, $genHType N, half eta); 147$mat matrixCompMult($mat x, $mat y); 148$hmat matrixCompMult($hmat x, $hmat y); 149$squareMat outerProduct($vec c, $vec r); 150float2x3 outerProduct(float3 c, float2 r); 151float3x2 outerProduct(float2 c, float3 r); 152float2x4 outerProduct(float4 c, float2 r); 153float4x2 outerProduct(float2 c, float4 r); 154float3x4 outerProduct(float4 c, float3 r); 155float4x3 outerProduct(float3 c, float4 r); 156$squareHMat outerProduct($hvec c, $hvec r); 157half2x3 outerProduct(half3 c, half2 r); 158half3x2 outerProduct(half2 c, half3 r); 159half2x4 outerProduct(half4 c, half2 r); 160half4x2 outerProduct(half2 c, half4 r); 161half3x4 outerProduct(half4 c, half3 r); 162half4x3 outerProduct(half3 c, half4 r); 163$squareMat transpose($squareMat m); 164float2x3 transpose(float3x2 m); 165float3x2 transpose(float2x3 m); 166float2x4 transpose(float4x2 m); 167float4x2 transpose(float2x4 m); 168float3x4 transpose(float4x3 m); 169float4x3 transpose(float3x4 m); 170$squareHMat transpose($squareHMat m); 171half2x3 transpose(half3x2 m); 172half3x2 transpose(half2x3 m); 173half2x4 transpose(half4x2 m); 174half4x2 transpose(half2x4 m); 175half3x4 transpose(half4x3 m); 176half4x3 transpose(half3x4 m); 177float determinant($squareMat m); 178half determinant($squareHMat m); 179$squareMat inverse($squareMat m); 180$squareHMat inverse($squareHMat m); 181$bvec lessThan($vec x, $vec y); 182$bvec lessThan($hvec x, $hvec y); 183$bvec lessThan($ivec x, $ivec y); 184$bvec lessThan($svec x, $svec y); 185$bvec lessThan($usvec x, $usvec y); 186$bvec lessThan($uvec x, $uvec y); 187$bvec lessThanEqual($vec x, $vec y); 188$bvec lessThanEqual($hvec x, $hvec y); 189$bvec lessThanEqual($ivec x, $ivec y); 190$bvec lessThanEqual($uvec x, $uvec y); 191$bvec lessThanEqual($svec x, $svec y); 192$bvec lessThanEqual($usvec x, $usvec y); 193$bvec greaterThan($vec x, $vec y); 194$bvec greaterThan($hvec x, $hvec y); 195$bvec greaterThan($ivec x, $ivec y); 196$bvec greaterThan($uvec x, $uvec y); 197$bvec greaterThan($svec x, $svec y); 198$bvec greaterThan($usvec x, $usvec y); 199$bvec greaterThanEqual($vec x, $vec y); 200$bvec greaterThanEqual($hvec x, $hvec y); 201$bvec greaterThanEqual($ivec x, $ivec y); 202$bvec greaterThanEqual($uvec x, $uvec y); 203$bvec greaterThanEqual($svec x, $svec y); 204$bvec greaterThanEqual($usvec x, $usvec y); 205$bvec equal($vec x, $vec y); 206$bvec equal($hvec x, $hvec y); 207$bvec equal($ivec x, $ivec y); 208$bvec equal($uvec x, $uvec y); 209$bvec equal($svec x, $svec y); 210$bvec equal($usvec x, $usvec y); 211$bvec equal($bvec x, $bvec y); 212$bvec notEqual($vec x, $vec y); 213$bvec notEqual($hvec x, $hvec y); 214$bvec notEqual($ivec x, $ivec y); 215$bvec notEqual($uvec x, $uvec y); 216$bvec notEqual($svec x, $svec y); 217$bvec notEqual($usvec x, $usvec y); 218$bvec notEqual($bvec x, $bvec y); 219bool any($bvec x); 220bool all($bvec x); 221$bvec not($bvec x); 222 223$genIType bitCount($genIType value); 224$genIType bitCount($genUType value); 225$genIType findLSB($genIType value); 226$genIType findLSB($genUType value); 227$genIType findMSB($genIType value); 228$genIType findMSB($genUType value); 229 230sampler2D makeSampler2D(texture2D texture, sampler s); 231int2 textureSize(sampler2DRect s); 232 233half4 sample(sampler1D s, float P); 234half4 sample(sampler1D s, float P, float bias); 235half4 sample(sampler2D s, float2 P); 236int4 sample(isampler2D s, float2 P); 237half4 sample(samplerExternalOES s, float2 P, float bias); 238half4 sample(samplerExternalOES s, float2 P); 239 240half4 sample(sampler2DRect s, float2 P); 241half4 sample(sampler2DRect s, float3 P); 242 243// Currently we do not support the generic types of loading subpassInput so we have some explicit 244// versions that we currently use 245half4 subpassLoad(subpassInput subpass); 246half4 subpassLoad(subpassInputMS subpass, int sample); 247 248half4 sample(sampler1D s, float2 P); 249half4 sample(sampler1D s, float2 P, float bias); 250half4 sample(sampler2D s, float3 P); 251half4 sample(sampler2D s, float3 P, float bias); 252 253$genType dFdx($genType p); 254$genType dFdy($genType p); 255$genHType dFdx($genHType p); 256$genHType dFdy($genHType p); 257$genType fwidth($genType p); 258$genHType fwidth($genHType p); 259float interpolateAtSample(float interpolant, int sample); 260float2 interpolateAtSample(float2 interpolant, int sample); 261float3 interpolateAtSample(float3 interpolant, int sample); 262float4 interpolateAtSample(float4 interpolant, int sample); 263float interpolateAtOffset(float interpolant, float2 offset); 264float2 interpolateAtOffset(float2 interpolant, float2 offset); 265float3 interpolateAtOffset(float3 interpolant, float2 offset); 266float4 interpolateAtOffset(float4 interpolant, float2 offset); 267 268// Definitions of functions implementing all of the SkBlendMode blends. 269 270half4 blend_clear(half4 src, half4 dst) { return half4(0); } 271 272half4 blend_src(half4 src, half4 dst) { return src; } 273 274half4 blend_dst(half4 src, half4 dst) { return dst; } 275 276half4 blend_src_over(half4 src, half4 dst) { return src + (1 - src.a)*dst; } 277 278half4 blend_dst_over(half4 src, half4 dst) { return (1 - dst.a)*src + dst; } 279 280half4 blend_src_in(half4 src, half4 dst) { return src*dst.a; } 281 282half4 blend_dst_in(half4 src, half4 dst) { return dst*src.a; } 283 284half4 blend_src_out(half4 src, half4 dst) { return (1 - dst.a)*src; } 285 286half4 blend_dst_out(half4 src, half4 dst) { return (1 - src.a)*dst; } 287 288half4 blend_src_atop(half4 src, half4 dst) { return dst.a*src + (1 - src.a)*dst; } 289 290half4 blend_dst_atop(half4 src, half4 dst) { return (1 - dst.a) * src + src.a*dst; } 291 292half4 blend_xor(half4 src, half4 dst) { return (1 - dst.a)*src + (1 - src.a)*dst; } 293 294half4 blend_plus(half4 src, half4 dst) { return min(src + dst, 1); } 295 296half4 blend_modulate(half4 src, half4 dst) { return src*dst; } 297 298half4 blend_screen(half4 src, half4 dst) { return src + (1 - src)*dst; } 299 300half _blend_overlay_component(half2 s, half2 d) { 301 return (2*d.x <= d.y) 302 ? 2*s.x*d.x 303 : s.y*d.y - 2*(d.y - d.x)*(s.y - s.x); 304} 305 306half4 blend_overlay(half4 src, half4 dst) { 307 half4 result = half4(_blend_overlay_component(src.ra, dst.ra), 308 _blend_overlay_component(src.ga, dst.ga), 309 _blend_overlay_component(src.ba, dst.ba), 310 src.a + (1 - src.a)*dst.a); 311 result.rgb += dst.rgb*(1 - src.a) + src.rgb*(1 - dst.a); 312 return result; 313} 314 315half4 blend_darken(half4 src, half4 dst) { 316 half4 result = blend_src_over(src, dst); 317 result.rgb = min(result.rgb, (1 - dst.a)*src.rgb + dst.rgb); 318 return result; 319} 320 321half4 blend_lighten(half4 src, half4 dst) { 322 half4 result = blend_src_over(src, dst); 323 result.rgb = max(result.rgb, (1 - dst.a)*src.rgb + dst.rgb); 324 return result; 325} 326 327half _guarded_divide(half n, half d) { 328 return sk_Caps.mustGuardDivisionEvenAfterExplicitZeroCheck 329 ? n/(d + 0.00000001) 330 : n/d; 331} 332 333half3 _guarded_divide(half3 n, half d) { 334 return sk_Caps.mustGuardDivisionEvenAfterExplicitZeroCheck 335 ? n/(d + 0.00000001) 336 : n/d; 337} 338 339half _color_dodge_component(half2 s, half2 d) { 340 if (d.x == 0) { 341 return s.x*(1 - d.y); 342 } else { 343 half delta = s.y - s.x; 344 if (delta == 0) { 345 return s.y*d.y + s.x*(1 - d.y) + d.x*(1 - s.y); 346 } else { 347 delta = min(d.y, _guarded_divide(d.x*s.y, delta)); 348 return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y); 349 } 350 } 351} 352 353half4 blend_color_dodge(half4 src, half4 dst) { 354 return half4(_color_dodge_component(src.ra, dst.ra), 355 _color_dodge_component(src.ga, dst.ga), 356 _color_dodge_component(src.ba, dst.ba), 357 src.a + (1 - src.a)*dst.a); 358} 359 360half _color_burn_component(half2 s, half2 d) { 361 if (d.y == d.x) { 362 return s.y*d.y + s.x*(1 - d.y) + d.x*(1 - s.y); 363 } else if (s.x == 0) { 364 return d.x*(1 - s.y); 365 } else { 366 half delta = max(0, d.y - _guarded_divide((d.y - d.x)*s.y, s.x)); 367 return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y); 368 } 369} 370 371half4 blend_color_burn(half4 src, half4 dst) { 372 return half4(_color_burn_component(src.ra, dst.ra), 373 _color_burn_component(src.ga, dst.ga), 374 _color_burn_component(src.ba, dst.ba), 375 src.a + (1 - src.a)*dst.a); 376} 377 378half4 blend_hard_light(half4 src, half4 dst) { return blend_overlay(dst, src); } 379 380half _soft_light_component(half2 s, half2 d) { 381 if (2*s.x <= s.y) { 382 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); 383 } else if (4.0 * d.x <= d.y) { 384 half DSqd = d.x*d.x; 385 half DCub = DSqd*d.x; 386 half DaSqd = d.y*d.y; 387 half DaCub = DaSqd*d.y; 388 return _guarded_divide(DaSqd*(s.x - d.x*(3*s.y - 6*s.x - 1)) + 12*d.y*DSqd*(s.y - 2*s.x) 389 - 16*DCub * (s.y - 2*s.x) - DaCub*s.x, DaSqd); 390 } else { 391 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; 392 } 393} 394 395half4 blend_soft_light(half4 src, half4 dst) { 396 return (dst.a == 0) ? src : half4(_soft_light_component(src.ra, dst.ra), 397 _soft_light_component(src.ga, dst.ga), 398 _soft_light_component(src.ba, dst.ba), 399 src.a + (1 - src.a)*dst.a); 400} 401 402half4 blend_difference(half4 src, half4 dst) { 403 return half4(src.rgb + dst.rgb - 2*min(src.rgb*dst.a, dst.rgb*src.a), 404 src.a + (1 - src.a)*dst.a); 405} 406 407half4 blend_exclusion(half4 src, half4 dst) { 408 return half4(dst.rgb + src.rgb - 2*dst.rgb*src.rgb, src.a + (1 - src.a)*dst.a); 409} 410 411half4 blend_multiply(half4 src, half4 dst) { 412 return half4((1 - src.a)*dst.rgb + (1 - dst.a)*src.rgb + src.rgb*dst.rgb, 413 src.a + (1 - src.a)*dst.a); 414} 415 416half _blend_color_luminance(half3 color) { return dot(half3(0.3, 0.59, 0.11), color); } 417 418half3 _blend_set_color_luminance(half3 hueSatColor, half alpha, half3 lumColor) { 419 half lum = _blend_color_luminance(lumColor); 420 half3 result = lum - _blend_color_luminance(hueSatColor) + hueSatColor; 421 half minComp = min(min(result.r, result.g), result.b); 422 half maxComp = max(max(result.r, result.g), result.b); 423 if (minComp < 0 && lum != minComp) { 424 result = lum + (result - lum) * _guarded_divide(lum, (lum - minComp)); 425 } 426 if (maxComp > alpha && maxComp != lum) { 427 return lum + _guarded_divide((result - lum) * (alpha - lum), (maxComp - lum)); 428 } else { 429 return result; 430 } 431} 432 433half _blend_color_saturation(half3 color) { 434 return max(max(color.r, color.g), color.b) - min(min(color.r, color.g), color.b); 435} 436 437half3 _blend_set_color_saturation_helper(half3 minMidMax, half sat) { 438 if (minMidMax.r < minMidMax.b) { 439 return half3(0, 440 _guarded_divide(sat*(minMidMax.g - minMidMax.r), (minMidMax.b - minMidMax.r)), 441 sat); 442 } else { 443 return half3(0); 444 } 445} 446 447half3 _blend_set_color_saturation(half3 hueLumColor, half3 satColor) { 448 half sat = _blend_color_saturation(satColor); 449 if (hueLumColor.r <= hueLumColor.g) { 450 if (hueLumColor.g <= hueLumColor.b) { 451 return _blend_set_color_saturation_helper(hueLumColor.rgb, sat); 452 } else if (hueLumColor.r <= hueLumColor.b) { 453 return _blend_set_color_saturation_helper(hueLumColor.rbg, sat).rbg; 454 } else { 455 return _blend_set_color_saturation_helper(hueLumColor.brg, sat).gbr; 456 } 457 } else if (hueLumColor.r <= hueLumColor.b) { 458 return _blend_set_color_saturation_helper(hueLumColor.grb, sat).grb; 459 } else if (hueLumColor.g <= hueLumColor.b) { 460 return _blend_set_color_saturation_helper(hueLumColor.gbr, sat).brg; 461 } else { 462 return _blend_set_color_saturation_helper(hueLumColor.bgr, sat).bgr; 463 } 464} 465 466half4 blend_hue(half4 src, half4 dst) { 467 half alpha = dst.a*src.a; 468 half3 sda = src.rgb*dst.a; 469 half3 dsa = dst.rgb*src.a; 470 return half4(_blend_set_color_luminance(_blend_set_color_saturation(sda, dsa), alpha, dsa) + 471 dst.rgb - dsa + src.rgb - sda, 472 src.a + dst.a - alpha); 473} 474 475half4 blend_saturation(half4 src, half4 dst) { 476 half alpha = dst.a*src.a; 477 half3 sda = src.rgb*dst.a; 478 half3 dsa = dst.rgb*src.a; 479 return half4(_blend_set_color_luminance(_blend_set_color_saturation(dsa, sda), alpha, dsa) + 480 dst.rgb - dsa + src.rgb - sda, 481 src.a + dst.a - alpha); 482} 483 484half4 blend_color(half4 src, half4 dst) { 485 half alpha = dst.a*src.a; 486 half3 sda = src.rgb*dst.a; 487 half3 dsa = dst.rgb*src.a; 488 return half4(_blend_set_color_luminance(sda, alpha, dsa) + dst.rgb - dsa + src.rgb - sda, 489 src.a + dst.a - alpha); 490} 491 492half4 blend_luminosity(half4 src, half4 dst) { 493 half alpha = dst.a*src.a; 494 half3 sda = src.rgb*dst.a; 495 half3 dsa = dst.rgb*src.a; 496 return half4(_blend_set_color_luminance(dsa, alpha, sda) + dst.rgb - dsa + src.rgb - sda, 497 src.a + dst.a - alpha); 498} 499 500// The max() guards against division by zero when the incoming color is transparent black 501half4 unpremul(half4 color) { return half4(color.rgb / max(color.a, 0.0001), color.a); } 502float4 unpremul(float4 color) { return float4(color.rgb / max(color.a, 0.0001), color.a); } 503 504float2 proj(float3 p) { return p.xy / p.z; } 505 506// Implement cross() as a determinant to communicate our intent more clearly to the compiler. 507// NOTE: Due to precision issues, it might be the case that cross(a, a) != 0. 508float cross(float2 a, float2 b) { 509 return sk_Caps.builtinDeterminantSupport ? determinant(float2x2(a, b)) 510 : a.x*b.y - a.y*b.x; 511} 512 513half cross(half2 a, half2 b) { 514 return sk_Caps.builtinDeterminantSupport ? determinant(half2x2(a, b)) 515 : a.x*b.y - a.y*b.x; 516} 517