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 sampler2D makeSampler2D(texture2D texture, sampler s); 29 30$pure half4 sample(sampler2D s, float2 P); 31$pure half4 sample(sampler2D s, float3 P); 32$pure half4 sample(sampler2D s, float3 P, float bias); 33 34$pure half4 sample(samplerExternalOES s, float2 P); 35$pure half4 sample(samplerExternalOES s, float2 P, float bias); 36 37$pure half4 sample(sampler2DRect s, float2 P); 38$pure half4 sample(sampler2DRect s, float3 P); 39 40$pure half4 sampleLod(sampler2D s, float2 P, float lod); 41$pure half4 sampleLod(sampler2D s, float3 P, float lod); 42 43$pure half4 sampleGrad(sampler2D s, float2, float2 dPdx, float2 dPdy); 44 45// Currently we do not support the generic types of loading subpassInput so we have some explicit 46// versions that we currently use 47$pure half4 subpassLoad(subpassInput subpass); 48$pure half4 subpassLoad(subpassInputMS subpass, int sample); 49 50/** Atomically loads the value from `a` and returns it. */ 51$pure uint atomicLoad(atomicUint a); 52 53/** Atomically stores the value of `value` to `a` */ 54void atomicStore(atomicUint a, uint value); 55 56/** 57 * Performs an atomic addition of `value` to the contents of `a` and returns the original contents 58 * of `a` from before the addition occurred. 59 */ 60uint atomicAdd(atomicUint a, uint value); 61 62// Definitions of functions implementing all of the SkBlendMode blends. 63 64$pure half4 blend_clear(half4 src, half4 dst) { return half4(0); } 65 66$pure half4 blend_src(half4 src, half4 dst) { return src; } 67 68$pure half4 blend_dst(half4 src, half4 dst) { return dst; } 69 70$pure half4 blend_src_over(half4 src, half4 dst) { return src + (1 - src.a)*dst; } 71 72$pure half4 blend_dst_over(half4 src, half4 dst) { return (1 - dst.a)*src + dst; } 73 74$pure half4 blend_src_in(half4 src, half4 dst) { return src*dst.a; } 75 76$pure half4 blend_dst_in(half4 src, half4 dst) { return dst*src.a; } 77 78$pure half4 blend_src_out(half4 src, half4 dst) { return (1 - dst.a)*src; } 79 80$pure half4 blend_dst_out(half4 src, half4 dst) { return (1 - src.a)*dst; } 81 82$pure half4 blend_src_atop(half4 src, half4 dst) { return dst.a*src + (1 - src.a)*dst; } 83 84$pure half4 blend_dst_atop(half4 src, half4 dst) { return (1 - dst.a) * src + src.a*dst; } 85 86$pure half4 blend_xor(half4 src, half4 dst) { return (1 - dst.a)*src + (1 - src.a)*dst; } 87 88$pure half4 blend_plus(half4 src, half4 dst) { return min(src + dst, 1); } 89 90// This multi-purpose Porter-Duff blend function can perform any of the thirteen blends above, 91// when passed one of the following values for BlendOp: 92// - Clear: half4(0, 0, 0, 0) 93// - Src: half4(1, 0, 0, 0) 94// - Dst: half4(0, 1, 0, 0) 95// - SrcOver: half4(1, 0, 0, -1) 96// - DstOver: half4(0, 1, -1, 0) 97// - SrcIn: half4(0, 0, 1, 0) 98// - DstIn: half4(0, 0, 0, 1) 99// - SrcOut: half4(0, 0, -1, 0) 100// - DstOut: half4(0, 0, 0, -1) 101// - SrcATop: half4(0, 0, 1, -1) 102// - DstATop: half4(0, 0, -1, 1) 103// - Xor: half4(0, 0, -1, -1) 104// - Plus: half4(1, 1, 0, 0) 105$pure half4 blend_porter_duff(half4 blendOp, half4 src, half4 dst) { 106 half2 coeff = blendOp.xy + (blendOp.zw * (half2(dst.a, src.a) + min(blendOp.zw, 0))); 107 return min(half4(1), src * coeff.x + dst * coeff.y); 108} 109 110$pure half4 blend_modulate(half4 src, half4 dst) { return src*dst; } 111 112$pure half4 blend_screen(half4 src, half4 dst) { return src + (1 - src)*dst; } 113 114$pure half $blend_overlay_component(half2 s, half2 d) { 115 return (2*d.x <= d.y) ? 2*s.x*d.x 116 : s.y*d.y - 2*(d.y - d.x)*(s.y - s.x); 117} 118 119$pure half4 blend_overlay(half4 src, half4 dst) { 120 half4 result = half4($blend_overlay_component(src.ra, dst.ra), 121 $blend_overlay_component(src.ga, dst.ga), 122 $blend_overlay_component(src.ba, dst.ba), 123 src.a + (1 - src.a)*dst.a); 124 result.rgb += dst.rgb*(1 - src.a) + src.rgb*(1 - dst.a); 125 return result; 126} 127 128$pure half4 blend_overlay(half flip, half4 a, half4 b) { 129 return blend_overlay(bool(flip) ? b : a, bool(flip) ? a : b); 130} 131 132$pure half4 blend_lighten(half4 src, half4 dst) { 133 half4 result = blend_src_over(src, dst); 134 result.rgb = max(result.rgb, (1 - dst.a)*src.rgb + dst.rgb); 135 return result; 136} 137 138$pure half4 blend_darken(half mode /* darken: 1, lighten: -1 */, half4 src, half4 dst) { 139 half4 a = blend_src_over(src, dst); 140 half3 b = (1 - dst.a) * src.rgb + dst.rgb; // DstOver.rgb 141 a.rgb = mode * min(a.rgb * mode, b.rgb * mode); 142 return a; 143} 144 145$pure half4 blend_darken(half4 src, half4 dst) { 146 return blend_darken(1, src, dst); 147} 148 149const half $kGuardedDivideEpsilon = sk_Caps.mustGuardDivisionEvenAfterExplicitZeroCheck 150 ? 0.00000001 151 : 0.0; 152 153$pure inline half $guarded_divide(half n, half d) { 154 return n / (d + $kGuardedDivideEpsilon); 155} 156 157$pure inline half3 $guarded_divide(half3 n, half d) { 158 return n / (d + $kGuardedDivideEpsilon); 159} 160 161$pure half $color_dodge_component(half2 s, half2 d) { 162 if (d.x == 0) { 163 return s.x*(1 - d.y); 164 } else { 165 half delta = s.y - s.x; 166 if (delta == 0) { 167 return s.y*d.y + s.x*(1 - d.y) + d.x*(1 - s.y); 168 } else { 169 delta = min(d.y, $guarded_divide(d.x*s.y, delta)); 170 return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y); 171 } 172 } 173} 174 175$pure half4 blend_color_dodge(half4 src, half4 dst) { 176 return half4($color_dodge_component(src.ra, dst.ra), 177 $color_dodge_component(src.ga, dst.ga), 178 $color_dodge_component(src.ba, dst.ba), 179 src.a + (1 - src.a)*dst.a); 180} 181 182$pure half $color_burn_component(half2 s, half2 d) { 183 if (d.y == d.x) { 184 return s.y*d.y + s.x*(1 - d.y) + d.x*(1 - s.y); 185 } else if (s.x == 0) { 186 return d.x*(1 - s.y); 187 } else { 188 half delta = max(0, d.y - $guarded_divide((d.y - d.x)*s.y, s.x)); 189 return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y); 190 } 191} 192 193$pure half4 blend_color_burn(half4 src, half4 dst) { 194 return half4($color_burn_component(src.ra, dst.ra), 195 $color_burn_component(src.ga, dst.ga), 196 $color_burn_component(src.ba, dst.ba), 197 src.a + (1 - src.a)*dst.a); 198} 199 200$pure half4 blend_hard_light(half4 src, half4 dst) { 201 return blend_overlay(dst, src); 202} 203 204$pure half $soft_light_component(half2 s, half2 d) { 205 if (2*s.x <= s.y) { 206 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); 207 } else if (4.0 * d.x <= d.y) { 208 half DSqd = d.x*d.x; 209 half DCub = DSqd*d.x; 210 half DaSqd = d.y*d.y; 211 half DaCub = DaSqd*d.y; 212 return $guarded_divide(DaSqd*(s.x - d.x*(3*s.y - 6*s.x - 1)) + 12*d.y*DSqd*(s.y - 2*s.x) 213 - 16*DCub * (s.y - 2*s.x) - DaCub*s.x, DaSqd); 214 } else { 215 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; 216 } 217} 218 219$pure half4 blend_soft_light(half4 src, half4 dst) { 220 return (dst.a == 0) ? src : half4($soft_light_component(src.ra, dst.ra), 221 $soft_light_component(src.ga, dst.ga), 222 $soft_light_component(src.ba, dst.ba), 223 src.a + (1 - src.a)*dst.a); 224} 225 226$pure half4 blend_difference(half4 src, half4 dst) { 227 return half4(src.rgb + dst.rgb - 2*min(src.rgb*dst.a, dst.rgb*src.a), 228 src.a + (1 - src.a)*dst.a); 229} 230 231$pure half4 blend_exclusion(half4 src, half4 dst) { 232 return half4(dst.rgb + src.rgb - 2*dst.rgb*src.rgb, src.a + (1 - src.a)*dst.a); 233} 234 235$pure half4 blend_multiply(half4 src, half4 dst) { 236 return half4((1 - src.a)*dst.rgb + (1 - dst.a)*src.rgb + src.rgb*dst.rgb, 237 src.a + (1 - src.a)*dst.a); 238} 239 240$pure half $blend_color_luminance(half3 color) { return dot(half3(0.3, 0.59, 0.11), color); } 241 242$pure half3 $blend_set_color_luminance(half3 hueSatColor, half alpha, half3 lumColor) { 243 half lum = $blend_color_luminance(lumColor); 244 half3 result = lum - $blend_color_luminance(hueSatColor) + hueSatColor; 245 half minComp = min(min(result.r, result.g), result.b); 246 half maxComp = max(max(result.r, result.g), result.b); 247 if (minComp < 0 && lum != minComp) { 248 result = lum + (result - lum) * $guarded_divide(lum, (lum - minComp)); 249 } 250 if (maxComp > alpha && maxComp != lum) { 251 result = lum + $guarded_divide((result - lum) * (alpha - lum), (maxComp - lum)); 252 } 253 return result; 254} 255 256$pure half $blend_color_saturation(half3 color) { 257 return max(max(color.r, color.g), color.b) - min(min(color.r, color.g), color.b); 258} 259 260$pure half3 $blend_set_color_saturation(half3 color, half3 satColor) { 261 half mn = min(min(color.r, color.g), color.b); 262 half mx = max(max(color.r, color.g), color.b); 263 264 return (mx > mn) ? ((color - mn) * $blend_color_saturation(satColor)) / (mx - mn) 265 : half3(0); 266} 267 268$pure half4 blend_hslc(half2 flipSat, half4 src, half4 dst) { 269 half alpha = dst.a * src.a; 270 half3 sda = src.rgb * dst.a; 271 half3 dsa = dst.rgb * src.a; 272 half3 l = bool(flipSat.x) ? dsa : sda; 273 half3 r = bool(flipSat.x) ? sda : dsa; 274 if (bool(flipSat.y)) { 275 l = $blend_set_color_saturation(l, r); 276 r = dsa; 277 } 278 return half4($blend_set_color_luminance(l, alpha, r) + dst.rgb - dsa + src.rgb - sda, 279 src.a + dst.a - alpha); 280} 281 282$pure half4 blend_hue(half4 src, half4 dst) { 283 return blend_hslc(half2(0, 1), src, dst); 284} 285 286$pure half4 blend_saturation(half4 src, half4 dst) { 287 return blend_hslc(half2(1), src, dst); 288} 289 290$pure half4 blend_color(half4 src, half4 dst) { 291 return blend_hslc(half2(0), src, dst); 292} 293 294$pure half4 blend_luminosity(half4 src, half4 dst) { 295 return blend_hslc(half2(1, 0), src, dst); 296} 297 298$pure float2 proj(float3 p) { return p.xy / p.z; } 299 300// Implement cross() as a determinant to communicate our intent more clearly to the compiler. 301// NOTE: Due to precision issues, it might be the case that cross(a, a) != 0. 302$pure float cross_length_2d(float2 a, float2 b) { 303 return determinant(float2x2(a, b)); 304} 305 306$pure half cross_length_2d(half2 a, half2 b) { 307 return determinant(half2x2(a, b)); 308} 309 310$pure float2 perp(float2 v) { 311 return float2(-v.y, v.x); 312} 313 314$pure half2 perp(half2 v) { 315 return half2(-v.y, v.x); 316} 317