• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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$genType saturate($genType x);
90$genHType saturate($genHType x);
91$genType mix($genType x, $genType y, $genType a);
92$genType mix($genType x, $genType y, float a);
93$genHType mix($genHType x, $genHType y, $genHType a);
94$genHType mix($genHType x, $genHType y, half a);
95$genType mix($genType x, $genType y, $genBType a);
96$genHType mix($genHType x, $genHType y, $genBType a);
97$genIType mix($genIType x, $genIType y, $genBType a);
98$genBType mix($genBType x, $genBType y, $genBType a);
99$genType step($genType edge, $genType x);
100$genType step(float edge, $genType x);
101$genHType step($genHType edge, $genHType x);
102$genHType step(half edge, $genHType x);
103$genType smoothstep($genType edge0, $genType edge1, $genType x);
104$genType smoothstep(float edge0, float edge1, $genType x);
105$genHType smoothstep($genHType edge0, $genHType edge1, $genHType x);
106$genHType smoothstep(half edge0, half edge1, $genHType x);
107$genBType isnan($genType x);
108$genBType isinf($genType x);
109$genIType floatBitsToInt($genType value);
110$genUType floatBitsToUint($genType value);
111$genType intBitsToFloat($genIType value);
112$genType uintBitsToFloat($genUType value);
113$genType fma($genType a, $genType b, $genType c);
114$genHType fma($genHType a, $genHType b, $genHType c);
115sk_has_side_effects $genType frexp($genType x, out $genIType exp);
116sk_has_side_effects $genHType frexp($genHType x, out $genIType exp);
117$genType ldexp($genType x, in $genIType exp);
118$genHType ldexp($genHType x, in $genIType exp);
119uint packUnorm2x16(float2 v);
120uint packSnorm2x16(float2 v);
121uint packUnorm4x8(float4 v);
122uint packSnorm4x8(float4 v);
123float2 unpackUnorm2x16(uint p);
124float2 unpackSnorm2x16(uint p);
125float4 unpackUnorm4x8(uint p);
126float4 unpackSnorm4x8(uint p);
127uint packHalf2x16(float2 v);
128float2 unpackHalf2x16(uint v);
129float length($genType x);
130half length($genHType x);
131float distance($genType p0, $genType p1);
132half distance($genHType p0, $genHType p1);
133float dot($genType x, $genType y);
134half dot($genHType x, $genHType y);
135float3 cross(float3 x, float3 y);
136half3 cross(half3 x, half3 y);
137$genType normalize($genType x);
138$genHType normalize($genHType x);
139$genType faceforward($genType N, $genType I, $genType Nref);
140$genHType faceforward($genHType N, $genHType I, $genHType Nref);
141$genType reflect($genType I, $genType N);
142$genHType reflect($genHType I, $genHType N);
143$genType refract($genType I, $genType N, float eta);
144$genHType refract($genHType I, $genHType N, float eta);
145$mat matrixCompMult($mat x, $mat y);
146$hmat matrixCompMult($hmat x, $hmat y);
147float2x2 outerProduct(float2 c, float2 r);
148float3x3 outerProduct(float3 c, float3 r);
149float4x4 outerProduct(float4 c, float4 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);
156half2x2 outerProduct(half2 c, half2 r);
157half3x3 outerProduct(half3 c, half3 r);
158half4x4 outerProduct(half4 c, half4 r);
159half2x3 outerProduct(half3 c, half2 r);
160half3x2 outerProduct(half2 c, half3 r);
161half2x4 outerProduct(half4 c, half2 r);
162half4x2 outerProduct(half2 c, half4 r);
163half3x4 outerProduct(half4 c, half3 r);
164half4x3 outerProduct(half3 c, half4 r);
165float2x2 transpose(float2x2 m);
166float3x3 transpose(float3x3 m);
167float4x4 transpose(float4x4 m);
168float2x3 transpose(float3x2 m);
169float3x2 transpose(float2x3 m);
170float2x4 transpose(float4x2 m);
171float4x2 transpose(float2x4 m);
172float3x4 transpose(float4x3 m);
173float4x3 transpose(float3x4 m);
174half2x2 transpose(half2x2 m);
175half3x3 transpose(half3x3 m);
176half4x4 transpose(half4x4 m);
177half2x3 transpose(half3x2 m);
178half3x2 transpose(half2x3 m);
179half2x4 transpose(half4x2 m);
180half4x2 transpose(half2x4 m);
181half3x4 transpose(half4x3 m);
182half4x3 transpose(half3x4 m);
183float determinant($squareMat m);
184half determinant($squareHMat m);
185$squareMat inverse($squareMat m);
186$squareHMat inverse($squareHMat m);
187$bvec lessThan($vec x, $vec y);
188$bvec lessThan($hvec x, $hvec y);
189$bvec lessThan($ivec x, $ivec y);
190$bvec lessThan($svec x, $svec y);
191$bvec lessThan($usvec x, $usvec y);
192$bvec lessThan($uvec x, $uvec y);
193$bvec lessThanEqual($vec x, $vec y);
194$bvec lessThanEqual($hvec x, $hvec y);
195$bvec lessThanEqual($ivec x, $ivec y);
196$bvec lessThanEqual($uvec x, $uvec y);
197$bvec lessThanEqual($svec x, $svec y);
198$bvec lessThanEqual($usvec x, $usvec y);
199$bvec greaterThan($vec x, $vec y);
200$bvec greaterThan($hvec x, $hvec y);
201$bvec greaterThan($ivec x, $ivec y);
202$bvec greaterThan($uvec x, $uvec y);
203$bvec greaterThan($svec x, $svec y);
204$bvec greaterThan($usvec x, $usvec y);
205$bvec greaterThanEqual($vec x, $vec y);
206$bvec greaterThanEqual($hvec x, $hvec y);
207$bvec greaterThanEqual($ivec x, $ivec y);
208$bvec greaterThanEqual($uvec x, $uvec y);
209$bvec greaterThanEqual($svec x, $svec y);
210$bvec greaterThanEqual($usvec x, $usvec y);
211$bvec equal($vec x, $vec y);
212$bvec equal($hvec x, $hvec y);
213$bvec equal($ivec x, $ivec y);
214$bvec equal($uvec x, $uvec y);
215$bvec equal($svec x, $svec y);
216$bvec equal($usvec x, $usvec y);
217$bvec equal($bvec x, $bvec y);
218$bvec notEqual($vec x, $vec y);
219$bvec notEqual($hvec x, $hvec y);
220$bvec notEqual($ivec x, $ivec y);
221$bvec notEqual($uvec x, $uvec y);
222$bvec notEqual($svec x, $svec y);
223$bvec notEqual($usvec x, $usvec y);
224$bvec notEqual($bvec x, $bvec y);
225bool any($bvec x);
226bool all($bvec x);
227$bvec not($bvec x);
228
229$genIType bitCount($genIType value);
230$genIType bitCount($genUType value);
231$genIType findLSB($genIType value);
232$genIType findLSB($genUType value);
233$genIType findMSB($genIType value);
234$genIType findMSB($genUType value);
235
236sampler2D makeSampler2D(texture2D texture, sampler s);
237int2 textureSize(sampler2DRect s);
238
239half4 sample(sampler1D s, float P);
240half4 sample(sampler1D s, float P, float bias);
241half4 sample(sampler2D s, float2 P);
242int4 sample(isampler2D s, float2 P);
243half4 sample(samplerExternalOES s, float2 P, float bias);
244half4 sample(samplerExternalOES s, float2 P);
245
246half4 sample(sampler2DRect s, float2 P);
247half4 sample(sampler2DRect s, float3 P);
248
249// Currently we do not support the generic types of loading subpassInput so we have some explicit
250// versions that we currently use
251half4 subpassLoad(subpassInput subpass);
252half4 subpassLoad(subpassInputMS subpass, int sample);
253
254half4 sample(sampler1D s, float2 P);
255half4 sample(sampler1D s, float2 P, float bias);
256half4 sample(sampler2D s, float3 P);
257half4 sample(sampler2D s, float3 P, float bias);
258
259$genType dFdx($genType p);
260$genType dFdy($genType p);
261$genHType dFdx($genHType p);
262$genHType dFdy($genHType p);
263$genType fwidth($genType p);
264$genHType fwidth($genHType p);
265float interpolateAtSample(float interpolant, int sample);
266float2 interpolateAtSample(float2 interpolant, int sample);
267float3 interpolateAtSample(float3 interpolant, int sample);
268float4 interpolateAtSample(float4 interpolant, int sample);
269float interpolateAtOffset(float interpolant, float2 offset);
270float2 interpolateAtOffset(float2 interpolant, float2 offset);
271float3 interpolateAtOffset(float3 interpolant, float2 offset);
272float4 interpolateAtOffset(float4 interpolant, float2 offset);
273
274// Definitions of functions implementing all of the SkBlendMode blends.
275
276half4 blend_clear(half4 src, half4 dst) { return half4(0); }
277
278half4 blend_src(half4 src, half4 dst) { return src; }
279
280half4 blend_dst(half4 src, half4 dst) { return dst; }
281
282half4 blend_src_over(half4 src, half4 dst) { return src + (1 - src.a)*dst; }
283
284half4 blend_dst_over(half4 src, half4 dst) { return (1 - dst.a)*src + dst; }
285
286half4 blend_src_in(half4 src, half4 dst) {
287    return sk_Caps.inBlendModesFailRandomlyForAllZeroVec
288            ? (src == half4(0) ? half4(0) : src*dst.a)
289            : src*dst.a;
290}
291
292half4 blend_dst_in(half4 src, half4 dst) { return blend_src_in(dst, src); }
293
294half4 blend_src_out(half4 src, half4 dst) { return (1 - dst.a)*src; }
295
296half4 blend_dst_out(half4 src, half4 dst) { return (1 - src.a)*dst; }
297
298half4 blend_src_atop(half4 src, half4 dst) { return dst.a*src + (1 - src.a)*dst; }
299
300half4 blend_dst_atop(half4 src, half4 dst)  { return  (1 - dst.a) * src + src.a*dst; }
301
302half4 blend_xor(half4 src, half4 dst) { return (1 - dst.a)*src + (1 - src.a)*dst; }
303
304half4 blend_plus(half4 src, half4 dst) { return min(src + dst, 1); }
305
306half4 blend_modulate(half4 src, half4 dst) { return src*dst; }
307
308half4 blend_screen(half4 src, half4 dst) { return src + (1 - src)*dst; }
309
310half _blend_overlay_component(half2 s, half2 d) {
311    return (2*d.x <= d.y)
312            ? 2*s.x*d.x
313            : s.y*d.y - 2*(d.y - d.x)*(s.y - s.x);
314}
315
316half4 blend_overlay(half4 src, half4 dst) {
317    half4 result = half4(_blend_overlay_component(src.ra, dst.ra),
318                         _blend_overlay_component(src.ga, dst.ga),
319                         _blend_overlay_component(src.ba, dst.ba),
320                         src.a + (1 - src.a)*dst.a);
321    result.rgb += dst.rgb*(1 - src.a) + src.rgb*(1 - dst.a);
322    return result;
323}
324
325half4 blend_darken(half4 src, half4 dst) {
326   half4 result = blend_src_over(src, dst);
327   result.rgb = min(result.rgb, (1 - dst.a)*src.rgb + dst.rgb);
328   return result;
329}
330
331half4 blend_lighten(half4 src, half4 dst) {
332    half4 result = blend_src_over(src, dst);
333    result.rgb = max(result.rgb, (1 - dst.a)*src.rgb + dst.rgb);
334    return result;
335}
336
337half _guarded_divide(half n, half d) {
338    return sk_Caps.mustGuardDivisionEvenAfterExplicitZeroCheck
339            ? n/(d + 0.00000001)
340            : n/d;
341}
342
343half3 _guarded_divide(half3 n, half d) {
344    return sk_Caps.mustGuardDivisionEvenAfterExplicitZeroCheck
345            ? n/(d + 0.00000001)
346            : n/d;
347}
348
349half _color_dodge_component(half2 s, half2 d) {
350    if (d.x == 0) {
351        return s.x*(1 - d.y);
352    } else {
353        half delta = s.y - s.x;
354        if (delta == 0) {
355             return s.y*d.y + s.x*(1 - d.y) + d.x*(1 - s.y);
356        } else {
357            delta = min(d.y, _guarded_divide(d.x*s.y, delta));
358            return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y);
359        }
360    }
361}
362
363half4 blend_color_dodge(half4 src, half4 dst) {
364    return half4(_color_dodge_component(src.ra, dst.ra),
365                 _color_dodge_component(src.ga, dst.ga),
366                 _color_dodge_component(src.ba, dst.ba),
367                 src.a + (1 - src.a)*dst.a);
368}
369
370half _color_burn_component(half2 s, half2 d) {
371    if (d.y == d.x) {
372        return s.y*d.y + s.x*(1 - d.y) + d.x*(1 - s.y);
373    } else if (s.x == 0) {
374        return d.x*(1 - s.y);
375    } else {
376        half delta = max(0, d.y - _guarded_divide((d.y - d.x)*s.y, s.x));
377        return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y);
378    }
379}
380
381half4 blend_color_burn(half4 src, half4 dst) {
382    return half4(_color_burn_component(src.ra, dst.ra),
383                 _color_burn_component(src.ga, dst.ga),
384                 _color_burn_component(src.ba, dst.ba),
385                 src.a + (1 - src.a)*dst.a);
386}
387
388half4 blend_hard_light(half4 src, half4 dst) { return blend_overlay(dst, src); }
389
390half _soft_light_component(half2 s, half2 d) {
391    if (2*s.x <= s.y) {
392        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);
393    } else if (4.0 * d.x <= d.y) {
394        half DSqd = d.x*d.x;
395        half DCub = DSqd*d.x;
396        half DaSqd = d.y*d.y;
397        half DaCub = DaSqd*d.y;
398        return _guarded_divide(DaSqd*(s.x - d.x*(3*s.y - 6*s.x - 1)) + 12*d.y*DSqd*(s.y - 2*s.x)
399                               - 16*DCub * (s.y - 2*s.x) - DaCub*s.x, DaSqd);
400    } else {
401        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;
402    }
403}
404
405half4 blend_soft_light(half4 src, half4 dst) {
406    return (dst.a == 0) ? src : half4(_soft_light_component(src.ra, dst.ra),
407                                      _soft_light_component(src.ga, dst.ga),
408                                      _soft_light_component(src.ba, dst.ba),
409                                      src.a + (1 - src.a)*dst.a);
410}
411
412half4 blend_difference(half4 src, half4 dst) {
413    return half4(src.rgb + dst.rgb - 2*min(src.rgb*dst.a, dst.rgb*src.a),
414                 src.a + (1 - src.a)*dst.a);
415}
416
417half4 blend_exclusion(half4 src, half4 dst) {
418    return half4(dst.rgb + src.rgb - 2*dst.rgb*src.rgb, src.a + (1 - src.a)*dst.a);
419}
420
421half4 blend_multiply(half4 src, half4 dst) {
422    return half4((1 - src.a)*dst.rgb + (1 - dst.a)*src.rgb + src.rgb*dst.rgb,
423                 src.a + (1 - src.a)*dst.a);
424}
425
426half _blend_color_luminance(half3 color) { return dot(half3(0.3, 0.59, 0.11), color); }
427
428half3 _blend_set_color_luminance(half3 hueSatColor, half alpha, half3 lumColor) {
429    half lum = _blend_color_luminance(lumColor);
430    half3 result = lum - _blend_color_luminance(hueSatColor) + hueSatColor;
431    half minComp = min(min(result.r, result.g), result.b);
432    half maxComp = max(max(result.r, result.g), result.b);
433    if (minComp < 0 && lum != minComp) {
434        result = lum + (result - lum) * _guarded_divide(lum, (lum - minComp));
435    }
436    if (maxComp > alpha && maxComp != lum) {
437        return lum + _guarded_divide((result - lum) * (alpha - lum), (maxComp - lum));
438    } else {
439        return result;
440    }
441}
442
443half _blend_color_saturation(half3 color) {
444    return max(max(color.r, color.g), color.b) - min(min(color.r, color.g), color.b);
445}
446
447half3 _blend_set_color_saturation_helper(half3 minMidMax, half sat) {
448    if (minMidMax.r < minMidMax.b) {
449        return half3(0,
450                    _guarded_divide(sat*(minMidMax.g - minMidMax.r), (minMidMax.b - minMidMax.r)),
451                    sat);
452    } else {
453        return half3(0);
454    }
455}
456
457half3 _blend_set_color_saturation(half3 hueLumColor, half3 satColor) {
458    half sat = _blend_color_saturation(satColor);
459    if (hueLumColor.r <= hueLumColor.g) {
460        if (hueLumColor.g <= hueLumColor.b) {
461            return _blend_set_color_saturation_helper(hueLumColor.rgb, sat);
462        } else if (hueLumColor.r <= hueLumColor.b) {
463            return _blend_set_color_saturation_helper(hueLumColor.rbg, sat).rbg;
464        } else {
465            return _blend_set_color_saturation_helper(hueLumColor.brg, sat).gbr;
466        }
467    } else if (hueLumColor.r <= hueLumColor.b) {
468       return _blend_set_color_saturation_helper(hueLumColor.grb, sat).grb;
469    } else if (hueLumColor.g <= hueLumColor.b) {
470       return _blend_set_color_saturation_helper(hueLumColor.gbr, sat).brg;
471    } else {
472       return _blend_set_color_saturation_helper(hueLumColor.bgr, sat).bgr;
473    }
474}
475
476half4 blend_hue(half4 src, half4 dst) {
477    half alpha = dst.a*src.a;
478    half3 sda = src.rgb*dst.a;
479    half3 dsa = dst.rgb*src.a;
480    return half4(_blend_set_color_luminance(_blend_set_color_saturation(sda, dsa), alpha, dsa) +
481                 dst.rgb - dsa + src.rgb - sda,
482                 src.a + dst.a - alpha);
483}
484
485half4 blend_saturation(half4 src, half4 dst) {
486    half alpha = dst.a*src.a;
487    half3 sda = src.rgb*dst.a;
488    half3 dsa = dst.rgb*src.a;
489    return half4(_blend_set_color_luminance(_blend_set_color_saturation(dsa, sda), alpha, dsa) +
490                 dst.rgb - dsa + src.rgb - sda,
491                 src.a + dst.a - alpha);
492}
493
494half4 blend_color(half4 src, half4 dst)  {
495    half alpha = dst.a*src.a;
496    half3 sda = src.rgb*dst.a;
497    half3 dsa = dst.rgb*src.a;
498    return half4(_blend_set_color_luminance(sda, alpha, dsa) + dst.rgb - dsa + src.rgb - sda,
499                 src.a + dst.a - alpha);
500}
501
502half4 blend_luminosity(half4 src, half4 dst) {
503    half alpha = dst.a*src.a;
504    half3 sda = src.rgb*dst.a;
505    half3 dsa = dst.rgb*src.a;
506    return half4(_blend_set_color_luminance(dsa, alpha, sda) + dst.rgb - dsa + src.rgb - sda,
507                 src.a + dst.a - alpha);
508}
509
510enum class SkBlendMode {
511    kClear      = 0,
512    kSrc        = 1,
513    kDst        = 2,
514    kSrcOver    = 3,
515    kDstOver    = 4,
516    kSrcIn      = 5,
517    kDstIn      = 6,
518    kSrcOut     = 7,
519    kDstOut     = 8,
520    kSrcATop    = 9,
521    kDstATop    = 10,
522    kXor        = 11,
523    kPlus       = 12,
524    kModulate   = 13,
525    kScreen     = 14,
526    kOverlay    = 15,
527    kDarken     = 16,
528    kLighten    = 17,
529    kColorDodge = 18,
530    kColorBurn  = 19,
531    kHardLight  = 20,
532    kSoftLight  = 21,
533    kDifference = 22,
534    kExclusion  = 23,
535    kMultiply   = 24,
536    kHue        = 25,
537    kSaturation = 26,
538    kColor      = 27,
539    kLuminosity = 28
540};
541
542half4 blend(SkBlendMode mode, half4 src, half4 dst) {
543    switch (mode) {
544        case SkBlendMode::kClear:      return blend_clear(src, dst);
545        case SkBlendMode::kSrc:        return blend_src(src, dst);
546        case SkBlendMode::kDst:        return blend_dst(src, dst);
547        case SkBlendMode::kSrcOver:    return blend_src_over(src, dst);
548        case SkBlendMode::kDstOver:    return blend_dst_over(src, dst);
549        case SkBlendMode::kSrcIn:      return blend_src_in(src, dst);
550        case SkBlendMode::kDstIn:      return blend_dst_in(src, dst);
551        case SkBlendMode::kSrcOut:     return blend_src_out(src, dst);
552        case SkBlendMode::kDstOut:     return blend_dst_out(src, dst);
553        case SkBlendMode::kSrcATop:    return blend_src_atop(src, dst);
554        case SkBlendMode::kDstATop:    return blend_dst_atop(src, dst);
555        case SkBlendMode::kXor:        return blend_xor(src, dst);
556        case SkBlendMode::kPlus:       return blend_plus(src, dst);
557        case SkBlendMode::kModulate:   return blend_modulate(src, dst);
558        case SkBlendMode::kScreen:     return blend_screen(src, dst);
559        case SkBlendMode::kOverlay:    return blend_overlay(src, dst);
560        case SkBlendMode::kDarken:     return blend_darken(src, dst);
561        case SkBlendMode::kLighten:    return blend_lighten(src, dst);
562        case SkBlendMode::kColorDodge: return blend_color_dodge(src, dst);
563        case SkBlendMode::kColorBurn:  return blend_color_burn(src, dst);
564        case SkBlendMode::kHardLight:  return blend_hard_light(src, dst);
565        case SkBlendMode::kSoftLight:  return blend_soft_light(src, dst);
566        case SkBlendMode::kDifference: return blend_difference(src, dst);
567        case SkBlendMode::kExclusion:  return blend_exclusion(src, dst);
568        case SkBlendMode::kMultiply:   return blend_multiply(src, dst);
569        case SkBlendMode::kHue:        return blend_hue(src, dst);
570        case SkBlendMode::kSaturation: return blend_saturation(src, dst);
571        case SkBlendMode::kColor:      return blend_color(src, dst);
572        case SkBlendMode::kLuminosity: return blend_luminosity(src, dst);
573        default: return half4(0);  // Avoids "'blend' can exit without returning a value."
574   }
575}
576
577// The max() guards against division by zero when the incoming color is transparent black
578half4  unpremul(half4 color)  { return half4(color.rgb / max(color.a, 0.0001), color.a); }
579float4 unpremul(float4 color) { return float4(color.rgb / max(color.a, 0.0001), color.a); }
580
581float2 proj(float3 p) { return p.xy / p.z; }
582
583// Implement cross() as a determinant to communicate our intent more clearly to the compiler.
584// NOTE: Due to precision issues, it might be the case that cross(a, a) != 0.
585float cross(float2 a, float2 b) {
586    return sk_Caps.builtinDeterminantSupport ? determinant(float2x2(a, b))
587                                             : a.x*b.y - a.y*b.x;
588}
589
590half cross(half2 a, half2 b) {
591    return sk_Caps.builtinDeterminantSupport ? determinant(half2x2(a, b))
592                                             : a.x*b.y - a.y*b.x;
593}
594