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$pure half4 blend_plus(half4 src, half4 dst) { return min(src + dst, 1); } 87 88// This multi-purpose Porter-Duff blend function can perform any of the thirteen blends above, 89// when passed one of the following values for BlendOp: 90// - Clear: half4(0, 0, 0, 0) 91// - Src: half4(1, 0, 0, 0) 92// - Dst: half4(0, 1, 0, 0) 93// - SrcOver: half4(1, 0, 0, -1) 94// - DstOver: half4(0, 1, -1, 0) 95// - SrcIn: half4(0, 0, 1, 0) 96// - DstIn: half4(0, 0, 0, 1) 97// - SrcOut: half4(0, 0, -1, 0) 98// - DstOut: half4(0, 0, 0, -1) 99// - SrcATop: half4(0, 0, 1, -1) 100// - DstATop: half4(0, 0, -1, 1) 101// - Xor: half4(0, 0, -1, -1) 102// - Plus: half4(1, 1, 0, 0) 103$pure half4 blend_porter_duff(half4 blendOp, half4 src, half4 dst) { 104 half2 coeff = blendOp.xy + (blendOp.zw * (half2(dst.a, src.a) + min(blendOp.zw, 0))); 105 return min(half4(1), src * coeff.x + dst * coeff.y); 106} 107 108$pure half4 blend_modulate(half4 src, half4 dst) { return src*dst; } 109 110$pure half4 blend_screen(half4 src, half4 dst) { return src + (1 - src)*dst; } 111 112$pure half $blend_overlay_component(half2 s, half2 d) { 113 return (2*d.x <= d.y) ? 2*s.x*d.x 114 : s.y*d.y - 2*(d.y - d.x)*(s.y - s.x); 115} 116 117$pure half4 blend_overlay(half4 src, half4 dst) { 118 half4 result = half4($blend_overlay_component(src.ra, dst.ra), 119 $blend_overlay_component(src.ga, dst.ga), 120 $blend_overlay_component(src.ba, dst.ba), 121 src.a + (1 - src.a)*dst.a); 122 result.rgb += dst.rgb*(1 - src.a) + src.rgb*(1 - dst.a); 123 return result; 124} 125 126$pure half4 blend_overlay(half flip, half4 a, half4 b) { 127 return blend_overlay(bool(flip) ? b : a, bool(flip) ? a : b); 128} 129 130$pure half4 blend_lighten(half4 src, half4 dst) { 131 half4 result = blend_src_over(src, dst); 132 result.rgb = max(result.rgb, (1 - dst.a)*src.rgb + dst.rgb); 133 return result; 134} 135 136$pure half4 blend_darken(half mode /* darken: 1, lighten: -1 */, half4 src, half4 dst) { 137 half4 a = blend_src_over(src, dst); 138 half3 b = (1 - dst.a) * src.rgb + dst.rgb; // DstOver.rgb 139 a.rgb = mode * min(a.rgb * mode, b.rgb * mode); 140 return a; 141} 142 143$pure half4 blend_darken(half4 src, half4 dst) { 144 return blend_darken(1, src, dst); 145} 146 147const half $kGuardedDivideEpsilon = sk_Caps.mustGuardDivisionEvenAfterExplicitZeroCheck 148 ? 0.00000001 149 : 0.0; 150 151$pure inline half $guarded_divide(half n, half d) { 152 return n / (d + $kGuardedDivideEpsilon); 153} 154 155$pure inline half3 $guarded_divide(half3 n, half d) { 156 return n / (d + $kGuardedDivideEpsilon); 157} 158 159$pure half $color_dodge_component(half2 s, half2 d) { 160 if (d.x == 0) { 161 return s.x*(1 - d.y); 162 } else { 163 half delta = s.y - s.x; 164 if (delta == 0) { 165 return s.y*d.y + s.x*(1 - d.y) + d.x*(1 - s.y); 166 } else { 167 delta = min(d.y, $guarded_divide(d.x*s.y, delta)); 168 return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y); 169 } 170 } 171} 172 173$pure half4 blend_color_dodge(half4 src, half4 dst) { 174 return half4($color_dodge_component(src.ra, dst.ra), 175 $color_dodge_component(src.ga, dst.ga), 176 $color_dodge_component(src.ba, dst.ba), 177 src.a + (1 - src.a)*dst.a); 178} 179 180$pure half $color_burn_component(half2 s, half2 d) { 181 if (d.y == d.x) { 182 return s.y*d.y + s.x*(1 - d.y) + d.x*(1 - s.y); 183 } else if (s.x == 0) { 184 return d.x*(1 - s.y); 185 } else { 186 half delta = max(0, d.y - $guarded_divide((d.y - d.x)*s.y, s.x)); 187 return delta*s.y + s.x*(1 - d.y) + d.x*(1 - s.y); 188 } 189} 190 191$pure half4 blend_color_burn(half4 src, half4 dst) { 192 return half4($color_burn_component(src.ra, dst.ra), 193 $color_burn_component(src.ga, dst.ga), 194 $color_burn_component(src.ba, dst.ba), 195 src.a + (1 - src.a)*dst.a); 196} 197 198$pure half4 blend_hard_light(half4 src, half4 dst) { 199 return blend_overlay(dst, src); 200} 201 202$pure half $soft_light_component(half2 s, half2 d) { 203 if (2*s.x <= s.y) { 204 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); 205 } else if (4.0 * d.x <= d.y) { 206 half DSqd = d.x*d.x; 207 half DCub = DSqd*d.x; 208 half DaSqd = d.y*d.y; 209 half DaCub = DaSqd*d.y; 210 return $guarded_divide(DaSqd*(s.x - d.x*(3*s.y - 6*s.x - 1)) + 12*d.y*DSqd*(s.y - 2*s.x) 211 - 16*DCub * (s.y - 2*s.x) - DaCub*s.x, DaSqd); 212 } else { 213 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; 214 } 215} 216 217$pure half4 blend_soft_light(half4 src, half4 dst) { 218 return (dst.a == 0) ? src : half4($soft_light_component(src.ra, dst.ra), 219 $soft_light_component(src.ga, dst.ga), 220 $soft_light_component(src.ba, dst.ba), 221 src.a + (1 - src.a)*dst.a); 222} 223 224$pure half4 blend_difference(half4 src, half4 dst) { 225 return half4(src.rgb + dst.rgb - 2*min(src.rgb*dst.a, dst.rgb*src.a), 226 src.a + (1 - src.a)*dst.a); 227} 228 229$pure half4 blend_exclusion(half4 src, half4 dst) { 230 return half4(dst.rgb + src.rgb - 2*dst.rgb*src.rgb, src.a + (1 - src.a)*dst.a); 231} 232 233$pure half4 blend_multiply(half4 src, half4 dst) { 234 return half4((1 - src.a)*dst.rgb + (1 - dst.a)*src.rgb + src.rgb*dst.rgb, 235 src.a + (1 - src.a)*dst.a); 236} 237 238$pure half $blend_color_luminance(half3 color) { return dot(half3(0.3, 0.59, 0.11), color); } 239 240$pure half3 $blend_set_color_luminance(half3 hueSatColor, half alpha, half3 lumColor) { 241 half lum = $blend_color_luminance(lumColor); 242 half3 result = lum - $blend_color_luminance(hueSatColor) + hueSatColor; 243 half minComp = min(min(result.r, result.g), result.b); 244 half maxComp = max(max(result.r, result.g), result.b); 245 if (minComp < 0 && lum != minComp) { 246 result = lum + (result - lum) * $guarded_divide(lum, (lum - minComp)); 247 } 248 if (maxComp > alpha && maxComp != lum) { 249 result = lum + $guarded_divide((result - lum) * (alpha - lum), (maxComp - lum)); 250 } 251 return result; 252} 253 254$pure half $blend_color_saturation(half3 color) { 255 return max(max(color.r, color.g), color.b) - min(min(color.r, color.g), color.b); 256} 257 258$pure half3 $blend_set_color_saturation(half3 color, half3 satColor) { 259 half mn = min(min(color.r, color.g), color.b); 260 half mx = max(max(color.r, color.g), color.b); 261 262 return (mx > mn) ? ((color - mn) * $blend_color_saturation(satColor)) / (mx - mn) 263 : half3(0); 264} 265 266$pure half4 blend_hslc(half2 flipSat, half4 src, half4 dst) { 267 half alpha = dst.a * src.a; 268 half3 sda = src.rgb * dst.a; 269 half3 dsa = dst.rgb * src.a; 270 half3 l = bool(flipSat.x) ? dsa : sda; 271 half3 r = bool(flipSat.x) ? sda : dsa; 272 if (bool(flipSat.y)) { 273 l = $blend_set_color_saturation(l, r); 274 r = dsa; 275 } 276 return half4($blend_set_color_luminance(l, alpha, r) + dst.rgb - dsa + src.rgb - sda, 277 src.a + dst.a - alpha); 278} 279 280$pure half4 blend_hue(half4 src, half4 dst) { 281 return blend_hslc(half2(0, 1), src, dst); 282} 283 284$pure half4 blend_saturation(half4 src, half4 dst) { 285 return blend_hslc(half2(1), src, dst); 286} 287 288$pure half4 blend_color(half4 src, half4 dst) { 289 return blend_hslc(half2(0), src, dst); 290} 291 292$pure half4 blend_luminosity(half4 src, half4 dst) { 293 return blend_hslc(half2(1, 0), src, dst); 294} 295 296$pure float2 proj(float3 p) { return p.xy / p.z; } 297 298// Implement cross() as a determinant to communicate our intent more clearly to the compiler. 299// NOTE: Due to precision issues, it might be the case that cross(a, a) != 0. 300$pure float cross_length_2d(float2 a, float2 b) { 301 return determinant(float2x2(a, b)); 302} 303 304$pure half cross_length_2d(half2 a, half2 b) { 305 return determinant(half2x2(a, b)); 306} 307 308$pure float2 perp(float2 v) { 309 return float2(-v.y, v.x); 310} 311 312$pure half2 perp(half2 v) { 313 return half2(-v.y, v.x); 314} 315 316// Returns a bias given a scale factor, such that 'scale * (dist + bias)' converts the distance to 317// a per-pixel coverage value, automatically widening the visible coverage ramp for subpixel 318// dimensions. The 'scale' must already be equal to the narrowest dimension of the shape and clamped 319// to [0, 1.0]. 320$pure float coverage_bias(float scale) { 321 return 1.0 - 0.5 * scale; 322} 323