| 1 |
// Graphite-specific fragment shader code |
| 2 |
|
| 3 |
const int $kTileModeClamp = 0; |
| 4 |
const int $kTileModeRepeat = 1; |
| 5 |
const int $kTileModeMirror = 2; |
| 6 |
const int $kTileModeDecal = 3; |
| 7 |
|
| 8 |
const int $kFilterModeNearest = 0; |
| 9 |
const int $kFilterModeLinear = 1; |
| 10 |
|
| 11 |
const int $kMaskFormatA8 = 0; |
| 12 |
const int $kMaskFormatA565 = 1; |
| 13 |
const int $kMaskFormatARGB = 2; |
| 14 |
|
| 15 |
const int $kShapeTypeRect = 0; |
| 16 |
const int $kShapeTypeRRect = 1; |
| 17 |
const int $kShapeTypeCircle = 2; |
| 18 |
|
| 19 |
// Matches GrTextureEffect::kLinearInset, to make sure we don't touch an outer |
| 20 |
// row or column with a weight of 0 when linear filtering. |
| 21 |
const float $kLinearInset = 0.5 + 0.00001; |
| 22 |
|
| 23 |
$pure half4 sk_error() { |
| 24 |
return half4(1.0, 0.0, 0.0, 1.0); |
| 25 |
} |
| 26 |
|
| 27 |
$pure half4 sk_passthrough(half4 color) { |
| 28 |
return color; |
| 29 |
} |
| 30 |
|
| 31 |
$pure half4 sk_rgb_opaque(float4 colorParam) { |
| 32 |
return half4(colorParam.rgb, 1.0); |
| 33 |
} |
| 34 |
|
| 35 |
$pure half4 sk_alpha_only(float4 colorParam) { |
| 36 |
return half4(0.0, 0.0, 0.0, colorParam.a); |
| 37 |
} |
| 38 |
|
| 39 |
$pure float3 $apply_srgb_xfer_fn(float3 x, float4 gabc, float3 def) { |
| 40 |
return mix(pow(gabc[1] * x + gabc[2], float3(gabc[0])) + def[1], |
| 41 |
(gabc[3] * x) + def[2], |
| 42 |
lessThan(x, float3(def[0]))); |
| 43 |
} |
| 44 |
|
| 45 |
$pure float3 $apply_pq_xfer_fn(float3 x, float3 abc, float3 def) { |
| 46 |
float3 x_C = pow(x, float3(abc[2])); |
| 47 |
return pow(max(abc[0] + abc[1] * x_C, 0) / (def[0] + def[1] * x_C), float3(def[2])); |
| 48 |
} |
| 49 |
|
| 50 |
$pure float3 $apply_hlg_xfer_fn(float3 x, float3 abc, float3 def) { |
| 51 |
return (def[2] + 1) * mix(exp((x - def[1]) * abc[2]) + def[0], |
| 52 |
pow(x * abc[0], float3(abc[1])), |
| 53 |
lessThanEqual(x * abc[0], float3(1))); |
| 54 |
} |
| 55 |
|
| 56 |
$pure float3 $apply_hlg_inv_xfer_fn(float3 x, float3 abc, float3 def) { |
| 57 |
x /= (def[2] + 1); |
| 58 |
return mix(abc[2] * log(x - def[0]) + def[1], |
| 59 |
abc[0] * pow(x, float3(abc[1])), |
| 60 |
lessThanEqual(x, float3(1))); |
| 61 |
} |
| 62 |
|
| 63 |
$pure half4 sk_color_space_transform(half4 color, |
| 64 |
half3x3 gamut, |
| 65 |
float4 srcGABC, |
| 66 |
float4 srcDEF_args, |
| 67 |
float4 dstGABC, |
| 68 |
float4 dstDEF_args, |
| 69 |
float4 srcOOTF_args, |
| 70 |
float4 dstOOTF_args) { |
| 71 |
// To encode whether to do premul/unpremul or make the output opaque, we use |
| 72 |
// srcDEF_args.w and dstDEF_args.w: |
| 73 |
// - identity: {0, 1} |
| 74 |
// - do unpremul: {-1, 1} |
| 75 |
// - do premul: {0, 0} |
| 76 |
// - do both: {-1, 0} |
| 77 |
// - alpha swizzle 1: {1, 1} |
| 78 |
// - alpha swizzle r: {1, 0} |
| 79 |
// |
| 80 |
// This depends on the assumption that if we're doing an alpha swizzle, we're not premultiplying |
| 81 |
// or unpremultiplying, so the alpha swizzle cases are mutually exclusive with the premul cases. |
| 82 |
|
| 83 |
if (srcDEF_args.w < 0) { |
| 84 |
color = unpremul(color); |
| 85 |
|
| 86 |
} else { |
| 87 |
// Alpha swizzle cases; we set alpha = {a, r, 1} . {1 - x, x - x * y, x * y}. |
| 88 |
// Given the encoded inputs described above, and ignoring the do unpremul case, we have: |
| 89 |
// 1. No-op ({x, y} = {0, 1} or {0, 0}) -> {a, r, 1} . {1, 0, 0} = a |
| 90 |
// 2. Use R ({x, y} = {1, 0}) -> {a, r, 1} . {0, 1, 0} = r |
| 91 |
// 3. Use 1 ({x, y} = {1, 1}) -> {a, r, 1} . {0, 0, 1} = 1 |
| 92 |
half alphaSwizzleA = 1.0 - half(srcDEF_args.w); |
| 93 |
half alphaSwizzle1 = half(srcDEF_args.w) * half(dstDEF_args.w); |
| 94 |
half alphaSwizzleR = half(srcDEF_args.w) - alphaSwizzle1; |
| 95 |
color.a = dot(color.ar1, half3(alphaSwizzleA, alphaSwizzleR, alphaSwizzle1)); |
| 96 |
} |
| 97 |
|
| 98 |
// To encode which transfer function to apply, we use the src and dst gamma values: |
| 99 |
// - identity: 0 |
| 100 |
// - sRGB: g > 0 |
| 101 |
// - PQ: -2 |
| 102 |
// - HLG: -1 |
| 103 |
float3 colorF = float3(color.rgb); |
| 104 |
if (srcGABC.x > 0) { |
| 105 |
colorF = sign(colorF) * $apply_srgb_xfer_fn(abs(colorF), srcGABC, srcDEF_args.xyz); |
| 106 |
} else if (srcGABC.x < -1 ) { |
| 107 |
colorF = sign(colorF) * $apply_pq_xfer_fn(abs(colorF), srcGABC.yzw, srcDEF_args.xyz); |
| 108 |
} else if (srcGABC.x < 0) { |
| 109 |
colorF = sign(colorF) * $apply_hlg_xfer_fn(abs(colorF), srcGABC.yzw, srcDEF_args.xyz); |
| 110 |
} |
| 111 |
|
| 112 |
if (srcOOTF_args.w != 0) { |
| 113 |
float Y = dot(srcOOTF_args.rgb, colorF); |
| 114 |
colorF *= sign(Y) * pow(abs(Y), srcOOTF_args.w); |
| 115 |
} |
| 116 |
|
| 117 |
colorF = gamut * colorF; |
| 118 |
|
| 119 |
if (dstOOTF_args.w != 0) { |
| 120 |
float Y = dot(dstOOTF_args.rgb, colorF); |
| 121 |
colorF *= sign(Y) * pow(abs(Y), dstOOTF_args.w); |
| 122 |
} |
| 123 |
|
| 124 |
if (dstGABC.x > 0) { |
| 125 |
colorF = sign(colorF) * $apply_srgb_xfer_fn(abs(colorF), dstGABC, dstDEF_args.xyz); |
| 126 |
} else if (dstGABC.x < -1 ) { |
| 127 |
colorF = sign(colorF) * $apply_pq_xfer_fn(abs(colorF), dstGABC.yzw, dstDEF_args.xyz); |
| 128 |
} else if (dstGABC.x < 0) { |
| 129 |
colorF = sign(colorF) * $apply_hlg_inv_xfer_fn(abs(colorF), dstGABC.yzw, dstDEF_args.xyz); |
| 130 |
} |
| 131 |
|
| 132 |
// Premul cases: |
| 133 |
// 1. No-op (noPremul = 1) -> max(...) = 1.0 -> identity function |
| 134 |
// 2. Premultiply (noPremul = 0) -> max(...) = color.a -> premul |
| 135 |
// |
| 136 |
// "noPremul" is 0 in the premul case, but also in the alpha-swizzle-r case, but the only time |
| 137 |
// we swizzle r to alpha is for a read swizzle of 000R, in which case the RGB channels are zero |
| 138 |
// and it doesn't matter what we multiply them by here. |
| 139 |
half noPremul = half(dstDEF_args.w); |
| 140 |
color.rgb = half3(colorF.rgb) * max(color.a, noPremul); |
| 141 |
|
| 142 |
return color; |
| 143 |
} |
| 144 |
|
| 145 |
$pure half4 sk_color_space_transform_premul(half4 color, half2 args) { |
| 146 |
// This shader can either do nothing, or perform one of three actions. These four possibilities |
| 147 |
// are encoded in a half2 argument as: |
| 148 |
// - identity: {0, 1} |
| 149 |
// - do unpremul: {-1, 1} |
| 150 |
// - do premul: {0, 0} |
| 151 |
// - make opaque: {1, 1} |
| 152 |
if (args.x < 0) { |
| 153 |
// Unpremul case (mutually exclusive with other cases, and costly). |
| 154 |
color = unpremul(color); |
| 155 |
|
| 156 |
} else { |
| 157 |
// The other cases (identity, premul, make opaque) are branchless. |
| 158 |
half opaque = args.x; |
| 159 |
half noPremul = args.y; |
| 160 |
|
| 161 |
// Opaque cases: |
| 162 |
// 1. No-op (opaque = 0) -> max(...) = color.a -> identity function |
| 163 |
// 2. Make opaque (opaque = 1) -> max(...) = 1.0 -> make opaque |
| 164 |
color.a = max(color.a, opaque); |
| 165 |
|
| 166 |
// Premul cases: |
| 167 |
// 1. No-op (noPremul = 1) -> max(...) = 1.0 -> identity function |
| 168 |
// 2. Premultiply (noPremul = 0) -> max(...) = color.a -> premul |
| 169 |
color.rgb = color.rgb * max(color.a, noPremul); |
| 170 |
} |
| 171 |
|
| 172 |
return color; |
| 173 |
} |
| 174 |
|
| 175 |
$pure half4 sk_color_space_transform_srgb(half4 color, |
| 176 |
half3x3 gamut, |
| 177 |
float4 srcGABC, |
| 178 |
float4 srcDEF_args, |
| 179 |
float4 dstGABC, |
| 180 |
float4 dstDEF_args) { |
| 181 |
// To encode whether to do premul/unpremul or make the output opaque, we use |
| 182 |
// srcDEF_args.w and dstDEF_args.w: |
| 183 |
// - identity: {0, 1} |
| 184 |
// - do unpremul: {-1, 1} |
| 185 |
// - do premul: {0, 0} |
| 186 |
// - do both: {-1, 0} |
| 187 |
// - alpha swizzle 1: {1, 1} |
| 188 |
// - alpha swizzle r: {1, 0} |
| 189 |
// |
| 190 |
// This depends on the assumption that if we're doing an alpha swizzle, we're not premultiplying |
| 191 |
// or unpremultiplying, so the alpha swizzle cases are mutually exclusive with the premul cases. |
| 192 |
|
| 193 |
if (srcDEF_args.w < 0) { |
| 194 |
color = unpremul(color); |
| 195 |
|
| 196 |
} else { |
| 197 |
// Alpha swizzle cases; we set alpha = {a, r, 1} . {1 - x, x - x * y, x * y}. |
| 198 |
// Given the encoded inputs described above, and ignoring the do unpremul cases, we have: |
| 199 |
// 1. No-op ({x, y} = {0, 1} or {0, 0}) -> {a, r, 1} . {1, 0, 0} = a |
| 200 |
// 2. Use R ({x, y} = {1, 0}) -> {a, r, 1} . {0, 1, 0} = r |
| 201 |
// 3. Use 1 ({x, y} = {1, 1}) -> {a, r, 1} . {0, 0, 1} = 1 |
| 202 |
half alphaSwizzleA = 1.0 - half(srcDEF_args.w); |
| 203 |
half alphaSwizzle1 = half(srcDEF_args.w) * half(dstDEF_args.w); |
| 204 |
half alphaSwizzleR = half(srcDEF_args.w) - alphaSwizzle1; |
| 205 |
color.a = dot(color.ar1, half3(alphaSwizzleA, alphaSwizzleR, alphaSwizzle1)); |
| 206 |
} |
| 207 |
|
| 208 |
float3 colorF = float3(color.rgb); |
| 209 |
colorF = sign(colorF) * $apply_srgb_xfer_fn(abs(colorF), srcGABC, srcDEF_args.xyz); |
| 210 |
|
| 211 |
colorF = gamut * colorF; |
| 212 |
|
| 213 |
colorF = sign(colorF) * $apply_srgb_xfer_fn(abs(colorF), dstGABC, dstDEF_args.xyz); |
| 214 |
|
| 215 |
// Premul cases: |
| 216 |
// 1. No-op (noPremul = 1) -> max(...) = 1.0 -> identity function |
| 217 |
// 2. Premultiply (noPremul = 0) -> max(...) = color.a -> premul |
| 218 |
// |
| 219 |
// "noPremul" is 0 in the premul case, but also in the alpha-swizzle-r case, but the only time |
| 220 |
// we swizzle r to alpha is for a read swizzle of 000R, in which case the RGB channels are zero |
| 221 |
// and it doesn't matter what we multiply them by here. |
| 222 |
half noPremul = half(dstDEF_args.w); |
| 223 |
color.rgb = half3(colorF.rgb) * max(color.a, noPremul); |
| 224 |
|
| 225 |
return color; |
| 226 |
} |
| 227 |
|
| 228 |
$pure half4 sk_analytic_clip(float2 coords, |
| 229 |
float4 rect, |
| 230 |
float2 radiusPlusHalf, |
| 231 |
half4 edgeSelect) { |
| 232 |
// Negative x indicates inverse fill |
| 233 |
float2 radius = float2(abs(radiusPlusHalf.x)); |
| 234 |
float2 dxy0 = edgeSelect.LT*((rect.LT + radius) - coords); |
| 235 |
float2 dxy1 = edgeSelect.RB*(coords - (rect.RB - radius)); |
| 236 |
float2 dxy = max(max(dxy0, dxy1), 0.0); |
| 237 |
// Use more complex length calculation to manage precision issues |
| 238 |
half circleCornerAlpha = half(saturate(radius.x*(1.0 - length(dxy*radiusPlusHalf.y)))); |
| 239 |
|
| 240 |
half4 rectEdgeAlphas = saturate(half4(coords - rect.LT, rect.RB - coords)); |
| 241 |
rectEdgeAlphas = mix(rectEdgeAlphas, half4(1), edgeSelect); |
| 242 |
|
| 243 |
half alpha = circleCornerAlpha * rectEdgeAlphas.x * rectEdgeAlphas.y * |
| 244 |
rectEdgeAlphas.z * rectEdgeAlphas.w; |
| 245 |
// Check for inverse fill |
| 246 |
alpha = radiusPlusHalf.x < 0 ? (1-alpha) : alpha; |
| 247 |
|
| 248 |
return half4(alpha); |
| 249 |
} |
| 250 |
|
| 251 |
$pure half4 sk_analytic_and_atlas_clip(float2 coords, |
| 252 |
float4 rect, |
| 253 |
float2 radiusPlusHalf, |
| 254 |
half4 edgeSelect, |
| 255 |
float2 texCoordOffset, |
| 256 |
float4 maskBounds, |
| 257 |
float2 invAtlasSize, |
| 258 |
sampler2D atlasSampler) { |
| 259 |
half4 analyticClip = sk_analytic_clip(coords, rect, radiusPlusHalf, edgeSelect); |
| 260 |
|
| 261 |
float2 texCoord = coords + texCoordOffset; |
| 262 |
float2 clampedTexCoord = clamp(texCoord, maskBounds.LT, maskBounds.RB); |
| 263 |
|
| 264 |
half atlasClip = sample(atlasSampler, clampedTexCoord * invAtlasSize).r; |
| 265 |
|
| 266 |
return analyticClip * atlasClip; |
| 267 |
} |
| 268 |
|
| 269 |
$pure float $tile(int tileMode, float f, float low, float high) { |
| 270 |
switch (tileMode) { |
| 271 |
case $kTileModeClamp: |
| 272 |
return clamp(f, low, high); |
| 273 |
|
| 274 |
case $kTileModeRepeat: { |
| 275 |
float length = high - low; |
| 276 |
return (mod(f - low, length) + low); |
| 277 |
} |
| 278 |
case $kTileModeMirror: { |
| 279 |
float length = high - low; |
| 280 |
float length2 = 2 * length; |
| 281 |
float tmp = mod(f - low, length2); |
| 282 |
return (mix(tmp, length2 - tmp, step(length, tmp)) + low); |
| 283 |
} |
| 284 |
default: // $kTileModeDecal |
| 285 |
// Decal is handled later. |
| 286 |
return f; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
$pure half4 $sample_image(float2 pos, float2 invImgSize, sampler2D s) { |
| 291 |
return sample(s, pos * invImgSize); |
| 292 |
} |
| 293 |
|
| 294 |
$pure half4 $sample_image_subset(float2 pos, |
| 295 |
float2 invImgSize, |
| 296 |
float4 subset, |
| 297 |
int tileModeX, |
| 298 |
int tileModeY, |
| 299 |
int filterMode, |
| 300 |
float2 linearFilterInset, |
| 301 |
sampler2D s) { |
| 302 |
// Do hard-edge shader transitions to the border color for nearest-neighbor decal tiling at the |
| 303 |
// subset boundaries. Snap the input coordinates to nearest neighbor before comparing to the |
| 304 |
// subset rect, to avoid GPU interpolation errors. See skbug.com/40041736. |
| 305 |
if (tileModeX == $kTileModeDecal && filterMode == $kFilterModeNearest) { |
| 306 |
float snappedX = floor(pos.x) + 0.5; |
| 307 |
if (snappedX < subset.x || snappedX > subset.z) { |
| 308 |
return half4(0); |
| 309 |
} |
| 310 |
} |
| 311 |
if (tileModeY == $kTileModeDecal && filterMode == $kFilterModeNearest) { |
| 312 |
float snappedY = floor(pos.y) + 0.5; |
| 313 |
if (snappedY < subset.y || snappedY > subset.w) { |
| 314 |
return half4(0); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
pos.x = $tile(tileModeX, pos.x, subset.x, subset.z); |
| 319 |
pos.y = $tile(tileModeY, pos.y, subset.y, subset.w); |
| 320 |
|
| 321 |
// Clamp to an inset subset to prevent sampling neighboring texels when coords fall exactly at |
| 322 |
// texel boundaries. |
| 323 |
float4 insetClamp; |
| 324 |
if (filterMode == $kFilterModeNearest) { |
| 325 |
insetClamp = float4(floor(subset.xy) + $kLinearInset, ceil(subset.zw) - $kLinearInset); |
| 326 |
} else { |
| 327 |
insetClamp = float4(subset.xy + linearFilterInset, subset.zw - linearFilterInset); |
| 328 |
} |
| 329 |
float2 clampedPos = clamp(pos, insetClamp.xy, insetClamp.zw); |
| 330 |
half4 color = $sample_image(clampedPos, invImgSize, s); |
| 331 |
|
| 332 |
if (filterMode == $kFilterModeLinear) { |
| 333 |
// Remember the amount the coord moved for clamping. This is used to implement shader-based |
| 334 |
// filtering for repeat and decal tiling. |
| 335 |
half2 error = half2(pos - clampedPos); |
| 336 |
half2 absError = abs(error); |
| 337 |
|
| 338 |
// Do 1 or 3 more texture reads depending on whether both x and y tiling modes are repeat |
| 339 |
// and whether we're near a single subset edge or a corner. Then blend the multiple reads |
| 340 |
// using the error values calculated above. |
| 341 |
bool sampleExtraX = tileModeX == $kTileModeRepeat; |
| 342 |
bool sampleExtraY = tileModeY == $kTileModeRepeat; |
| 343 |
if (sampleExtraX || sampleExtraY) { |
| 344 |
float extraCoordX; |
| 345 |
float extraCoordY; |
| 346 |
half4 extraColorX; |
| 347 |
half4 extraColorY; |
| 348 |
if (sampleExtraX) { |
| 349 |
extraCoordX = error.x > 0 ? insetClamp.x : insetClamp.z; |
| 350 |
extraColorX = $sample_image(float2(extraCoordX, clampedPos.y), |
| 351 |
invImgSize, s); |
| 352 |
} |
| 353 |
if (sampleExtraY) { |
| 354 |
extraCoordY = error.y > 0 ? insetClamp.y : insetClamp.w; |
| 355 |
extraColorY = $sample_image(float2(clampedPos.x, extraCoordY), |
| 356 |
invImgSize, s); |
| 357 |
} |
| 358 |
if (sampleExtraX && sampleExtraY) { |
| 359 |
half4 extraColorXY = $sample_image(float2(extraCoordX, extraCoordY), |
| 360 |
invImgSize, s); |
| 361 |
color = mix(mix(color, extraColorX, absError.x), |
| 362 |
mix(extraColorY, extraColorXY, absError.x), |
| 363 |
absError.y); |
| 364 |
} else if (sampleExtraX) { |
| 365 |
color = mix(color, extraColorX, absError.x); |
| 366 |
} else if (sampleExtraY) { |
| 367 |
color = mix(color, extraColorY, absError.y); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
// Do soft edge shader filtering for decal tiling and linear filtering using the error |
| 372 |
// values calculated above. |
| 373 |
if (tileModeX == $kTileModeDecal) { |
| 374 |
color *= max(1 - absError.x, 0); |
| 375 |
} |
| 376 |
if (tileModeY == $kTileModeDecal) { |
| 377 |
color *= max(1 - absError.y, 0); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
return color; |
| 382 |
} |
| 383 |
|
| 384 |
$pure half4 $cubic_filter_image(float2 pos, |
| 385 |
float2 invImgSize, |
| 386 |
float4 subset, |
| 387 |
int tileModeX, |
| 388 |
int tileModeY, |
| 389 |
half4x4 coeffs, |
| 390 |
sampler2D s) { |
| 391 |
// Determine pos's fractional offset f between texel centers. |
| 392 |
float2 f = fract(pos - 0.5); |
| 393 |
// Sample 16 points at 1-pixel intervals from [p - 1.5 ... p + 1.5]. |
| 394 |
pos -= 1.5; |
| 395 |
// Snap to texel centers to prevent sampling neighboring texels. |
| 396 |
pos = floor(pos) + 0.5; |
| 397 |
|
| 398 |
half4 wx = coeffs * half4(1.0, f.x, f.x * f.x, f.x * f.x * f.x); |
| 399 |
half4 wy = coeffs * half4(1.0, f.y, f.y * f.y, f.y * f.y * f.y); |
| 400 |
half4 color = half4(0); |
| 401 |
for (int y = 0; y < 4; ++y) { |
| 402 |
half4 rowColor = half4(0); |
| 403 |
for (int x = 0; x < 4; ++x) { |
| 404 |
rowColor += wx[x] * $sample_image_subset(pos + float2(x, y), invImgSize, subset, |
| 405 |
tileModeX, tileModeY, $kFilterModeNearest, |
| 406 |
float2($kLinearInset), s); |
| 407 |
} |
| 408 |
color += wy[y] * rowColor; |
| 409 |
} |
| 410 |
// Bicubic can send colors out of range, so clamp to get them back in gamut, assuming premul. |
| 411 |
color.a = saturate(color.a); |
| 412 |
color.rgb = clamp(color.rgb, half3(0.0), color.aaa); |
| 413 |
return color; |
| 414 |
} |
| 415 |
|
| 416 |
$pure half4 sk_image_shader(float2 coords, |
| 417 |
float2 invImgSize, |
| 418 |
float4 subset, |
| 419 |
int tileModeX, |
| 420 |
int tileModeY, |
| 421 |
int filterMode, |
| 422 |
sampler2D s) { |
| 423 |
return $sample_image_subset(coords, invImgSize, subset, tileModeX, tileModeY, |
| 424 |
filterMode, float2($kLinearInset), s); |
| 425 |
} |
| 426 |
|
| 427 |
$pure half4 sk_image_shader_clamp(float2 coords, |
| 428 |
float2 invImgSize, |
| 429 |
float4 subsetInsetClamp, |
| 430 |
sampler2D s) { |
| 431 |
return $sample_image(clamp(coords, subsetInsetClamp.xy, subsetInsetClamp.zw), invImgSize, s); |
| 432 |
} |
| 433 |
|
| 434 |
$pure half4 sk_cubic_image_shader(float2 coords, |
| 435 |
float2 invImgSize, |
| 436 |
float4 subset, |
| 437 |
int tileModeX, |
| 438 |
int tileModeY, |
| 439 |
half4x4 cubicCoeffs, |
| 440 |
sampler2D s) { |
| 441 |
return $cubic_filter_image(coords, invImgSize, subset, tileModeX, tileModeY, cubicCoeffs, s); |
| 442 |
} |
| 443 |
|
| 444 |
$pure half4 sk_hw_image_shader(float2 coords, sampler2D s) { |
| 445 |
// This assumes that `coords` have already been normalized. |
| 446 |
return sample(s, coords); |
| 447 |
} |
| 448 |
|
| 449 |
$pure half4 $yuv_to_rgb_no_swizzle(half Y, |
| 450 |
half U, |
| 451 |
half V, |
| 452 |
half alpha, |
| 453 |
half3x3 yuvToRGBMatrix, |
| 454 |
half3 yuvToRGBTranslate) { |
| 455 |
half3 preColor = half3(Y, U, V); |
| 456 |
half4 sampleColor; |
| 457 |
sampleColor.rgb = saturate(yuvToRGBMatrix * preColor.rgb + yuvToRGBTranslate); |
| 458 |
sampleColor.a = alpha; |
| 459 |
|
| 460 |
// We always return an unpremul color here to skip work when transforming colorspaces |
| 461 |
return sampleColor; |
| 462 |
} |
| 463 |
|
| 464 |
$pure half4 $yuv_to_rgb(half4 sampleColorY, |
| 465 |
half4 sampleColorU, |
| 466 |
half4 sampleColorV, |
| 467 |
half alpha, |
| 468 |
half4 channelSelectY, |
| 469 |
half4 channelSelectU, |
| 470 |
half4 channelSelectV, |
| 471 |
half3x3 yuvToRGBMatrix, |
| 472 |
half3 yuvToRGBTranslate) { |
| 473 |
half Y = dot(channelSelectY, sampleColorY); |
| 474 |
half U = dot(channelSelectU, sampleColorU); |
| 475 |
half V = dot(channelSelectV, sampleColorV); |
| 476 |
return $yuv_to_rgb_no_swizzle(Y, U, V, alpha, yuvToRGBMatrix, yuvToRGBTranslate); |
| 477 |
} |
| 478 |
|
| 479 |
$pure half4 sk_yuv_image_shader(float2 coords, |
| 480 |
float2 invImgSizeY, |
| 481 |
float2 invImgSizeUV, // Relative to Y's coordinate space |
| 482 |
float4 subset, |
| 483 |
float2 linearFilterUVInset, |
| 484 |
int tileModeX, |
| 485 |
int tileModeY, |
| 486 |
int filterModeY, |
| 487 |
int filterModeUV, |
| 488 |
half4 channelSelectY, |
| 489 |
half4 channelSelectU, |
| 490 |
half4 channelSelectV, |
| 491 |
half4 channelSelectA, |
| 492 |
half3x3 yuvToRGBMatrix, |
| 493 |
half3 yuvToRGBTranslate, |
| 494 |
sampler2D sY, |
| 495 |
sampler2D sU, |
| 496 |
sampler2D sV, |
| 497 |
sampler2D sA) { |
| 498 |
// If the filter modes are different between Y and UV, this means that |
| 499 |
// the base filtermode is nearest and we have to snap the coords to Y's |
| 500 |
// texel centers to get the correct positions for UV. |
| 501 |
if (filterModeY != filterModeUV) { |
| 502 |
coords = floor(coords) + 0.5; |
| 503 |
} |
| 504 |
|
| 505 |
int tileModeX_UV = tileModeX == $kTileModeDecal ? $kTileModeClamp : tileModeX; |
| 506 |
int tileModeY_UV = tileModeY == $kTileModeDecal ? $kTileModeClamp : tileModeY; |
| 507 |
|
| 508 |
half4 sampleColorY, sampleColorU, sampleColorV; |
| 509 |
sampleColorY = $sample_image_subset(coords, invImgSizeY, subset, tileModeX, tileModeY, |
| 510 |
filterModeY, float2($kLinearInset), sY); |
| 511 |
sampleColorU = $sample_image_subset(coords, invImgSizeUV, subset, tileModeX_UV, tileModeY_UV, |
| 512 |
filterModeUV, linearFilterUVInset, sU); |
| 513 |
sampleColorV = $sample_image_subset(coords, invImgSizeUV, subset, tileModeX_UV, tileModeY_UV, |
| 514 |
filterModeUV, linearFilterUVInset, sV); |
| 515 |
half alpha; |
| 516 |
if (channelSelectA == half4(1)) { |
| 517 |
alpha = 1; |
| 518 |
} else { |
| 519 |
half4 sampleColorA = $sample_image_subset(coords, invImgSizeY, subset, tileModeX, tileModeY, |
| 520 |
filterModeY, float2($kLinearInset), sA); |
| 521 |
alpha = dot(channelSelectA, sampleColorA); |
| 522 |
} |
| 523 |
|
| 524 |
return $yuv_to_rgb(sampleColorY, sampleColorU, sampleColorV, alpha, |
| 525 |
channelSelectY, channelSelectU, channelSelectV, |
| 526 |
yuvToRGBMatrix, yuvToRGBTranslate); |
| 527 |
} |
| 528 |
|
| 529 |
$pure half4 sk_cubic_yuv_image_shader(float2 coords, |
| 530 |
float2 invImgSizeY, |
| 531 |
float2 invImgSizeUV, // Relative to Y's coordinate space |
| 532 |
float4 subset, |
| 533 |
int tileModeX, |
| 534 |
int tileModeY, |
| 535 |
half4x4 cubicCoeffs, |
| 536 |
half4 channelSelectY, |
| 537 |
half4 channelSelectU, |
| 538 |
half4 channelSelectV, |
| 539 |
half4 channelSelectA, |
| 540 |
half3x3 yuvToRGBMatrix, |
| 541 |
half3 yuvToRGBTranslate, |
| 542 |
sampler2D sY, |
| 543 |
sampler2D sU, |
| 544 |
sampler2D sV, |
| 545 |
sampler2D sA) { |
| 546 |
int tileModeX_UV = tileModeX == $kTileModeDecal ? $kTileModeClamp : tileModeX; |
| 547 |
int tileModeY_UV = tileModeY == $kTileModeDecal ? $kTileModeClamp : tileModeY; |
| 548 |
|
| 549 |
half4 sampleColorY, sampleColorU, sampleColorV; |
| 550 |
sampleColorY = $cubic_filter_image(coords, invImgSizeY, subset, tileModeX, tileModeY, |
| 551 |
cubicCoeffs, sY); |
| 552 |
sampleColorU = $cubic_filter_image(coords, invImgSizeUV, subset, tileModeX_UV, tileModeY_UV, |
| 553 |
cubicCoeffs, sU); |
| 554 |
sampleColorV = $cubic_filter_image(coords, invImgSizeUV, subset, tileModeX_UV, tileModeY_UV, |
| 555 |
cubicCoeffs, sV); |
| 556 |
half alpha; |
| 557 |
if (channelSelectA == half4(1)) { |
| 558 |
alpha = 1; |
| 559 |
} else { |
| 560 |
half4 sampleColorA = $cubic_filter_image(coords, invImgSizeY, subset, tileModeX, tileModeY, |
| 561 |
cubicCoeffs, sA); |
| 562 |
alpha = dot(channelSelectA, sampleColorA); |
| 563 |
} |
| 564 |
|
| 565 |
return $yuv_to_rgb(sampleColorY, sampleColorU, sampleColorV, alpha, |
| 566 |
channelSelectY, channelSelectU, channelSelectV, |
| 567 |
yuvToRGBMatrix, yuvToRGBTranslate); |
| 568 |
} |
| 569 |
|
| 570 |
$pure half4 sk_hw_yuv_image_shader(float2 coords, |
| 571 |
float2 invImgSizeY, |
| 572 |
float2 invImgSizeUV, // Relative to Y's coordinate space |
| 573 |
float4 subset, |
| 574 |
float2 linearFilterUVInset, |
| 575 |
half4 channelSelectY, |
| 576 |
half4 channelSelectU, |
| 577 |
half4 channelSelectV, |
| 578 |
half4 channelSelectA, |
| 579 |
half3x3 yuvToRGBMatrix, |
| 580 |
half3 yuvToRGBTranslate, |
| 581 |
sampler2D sY, |
| 582 |
sampler2D sU, |
| 583 |
sampler2D sV, |
| 584 |
sampler2D sA) { |
| 585 |
// If the base filtermode is nearest and we have to snap the coords to Y's |
| 586 |
// texel centers to get the correct positions for UV, and then clamp so that |
| 587 |
// we don't sample any coordinates beyond the boundary. |
| 588 |
float4 subsetUV = subset; |
| 589 |
if (linearFilterUVInset.x < 0) { |
| 590 |
coords = floor(coords) + 0.5; |
| 591 |
// Snap Y subset to texel boundaries for nearest neighbor filter mode; |
| 592 |
// use the original subset for UV since it will have linear filtering. |
| 593 |
subset = float4(floor(subset.xy), ceil(subset.zw)); |
| 594 |
} |
| 595 |
|
| 596 |
float2 coordsUV = coords; |
| 597 |
if (linearFilterUVInset.y < 0) { |
| 598 |
linearFilterUVInset = abs(linearFilterUVInset); |
| 599 |
coordsUV = |
| 600 |
clamp(coords, subsetUV.xy + linearFilterUVInset, subsetUV.zw - linearFilterUVInset); |
| 601 |
coords = clamp(coords, subset.xy + $kLinearInset, subset.zw - $kLinearInset); |
| 602 |
} |
| 603 |
|
| 604 |
half4 sampleColorY, sampleColorU, sampleColorV, sampleColorA; |
| 605 |
sampleColorY = $sample_image(coords, invImgSizeY, sY); |
| 606 |
sampleColorU = $sample_image(coordsUV, invImgSizeUV, sU); |
| 607 |
sampleColorV = $sample_image(coordsUV, invImgSizeUV, sV); |
| 608 |
half alpha; |
| 609 |
if (channelSelectA == half4(1)) { |
| 610 |
alpha = 1; |
| 611 |
} else { |
| 612 |
half4 sampleColorA = $sample_image(coords, invImgSizeY, sA); |
| 613 |
alpha = dot(channelSelectA, sampleColorA); |
| 614 |
} |
| 615 |
|
| 616 |
return $yuv_to_rgb(sampleColorY, sampleColorU, sampleColorV, alpha, |
| 617 |
channelSelectY, channelSelectU, channelSelectV, |
| 618 |
yuvToRGBMatrix, yuvToRGBTranslate); |
| 619 |
} |
| 620 |
|
| 621 |
$pure half4 sk_hw_yuv_no_swizzle_image_shader(float2 coords, |
| 622 |
float2 invImgSizeY, |
| 623 |
float2 invImgSizeUV, // Relative to Y's coord space |
| 624 |
float4 subset, |
| 625 |
float2 linearFilterUVInset, |
| 626 |
half3x3 yuvToRGBMatrix, |
| 627 |
half4 yuvToRGBXlateAlphaParam, |
| 628 |
sampler2D sY, |
| 629 |
sampler2D sU, |
| 630 |
sampler2D sV, |
| 631 |
sampler2D sA) { |
| 632 |
// If the base filtermode is nearest and we have to snap the coords to Y's |
| 633 |
// texel centers to get the correct positions for UV, and then clamp so that |
| 634 |
// we don't sample any coordinates beyond the boundary. |
| 635 |
float4 subsetUV = subset; |
| 636 |
if (linearFilterUVInset.x < 0) { |
| 637 |
coords = floor(coords) + 0.5; |
| 638 |
// Snap Y subset to texel boundaries for nearest neighbor filter mode; |
| 639 |
// use the original subset for UV since it will have linear filtering. |
| 640 |
subset = float4(floor(subset.xy), ceil(subset.zw)); |
| 641 |
} |
| 642 |
|
| 643 |
float2 coordsUV = coords; |
| 644 |
if (linearFilterUVInset.y < 0) { |
| 645 |
linearFilterUVInset = abs(linearFilterUVInset); |
| 646 |
coordsUV = |
| 647 |
clamp(coords, subsetUV.xy + linearFilterUVInset, subsetUV.zw - linearFilterUVInset); |
| 648 |
coords = clamp(coords, subset.xy + $kLinearInset, subset.zw - $kLinearInset); |
| 649 |
} |
| 650 |
|
| 651 |
half4 sampleColorY, sampleColorU, sampleColorV, sampleColorA; |
| 652 |
half Y = $sample_image(coords, invImgSizeY, sY).r; |
| 653 |
half U = $sample_image(coordsUV, invImgSizeUV, sU).r; |
| 654 |
half V = $sample_image(coordsUV, invImgSizeUV, sV).r; |
| 655 |
// When it's a Y_U_V_A texture, yuvToRGBXlateAlphaParam.w is 0 and we have a real A sampler so |
| 656 |
// alpha is just that saturated value (should be a no-op). For Y_U_V, we set |
| 657 |
// yuvToRGBXlateAlphaParam.w to 1 and sample from Y, which after the saturate is always 1. |
| 658 |
half alpha = saturate($sample_image(coords, invImgSizeY, sA).r + yuvToRGBXlateAlphaParam.w); |
| 659 |
|
| 660 |
return $yuv_to_rgb_no_swizzle(Y, U, V, alpha, |
| 661 |
yuvToRGBMatrix, yuvToRGBXlateAlphaParam.xyz); |
| 662 |
} |
| 663 |
|
| 664 |
$pure half4 sk_dither(half4 colorIn, half range, sampler2D lut) { |
| 665 |
const float kImgSize = 8; |
| 666 |
|
| 667 |
half value = sample(lut, sk_FragCoord.xy / kImgSize).r - 0.5; // undo the bias in the table |
| 668 |
// For each color channel, add the random offset to the channel value and then clamp |
| 669 |
// between 0 and alpha to keep the color premultiplied. |
| 670 |
return half4(clamp(colorIn.rgb + value * range, 0.0, colorIn.a), colorIn.a); |
| 671 |
} |
| 672 |
|
| 673 |
$pure float2 $tile_grad(int tileMode, float2 t) { |
| 674 |
switch (tileMode) { |
| 675 |
case $kTileModeClamp: |
| 676 |
t.x = saturate(t.x); |
| 677 |
break; |
| 678 |
|
| 679 |
case $kTileModeRepeat: |
| 680 |
t.x = fract(t.x); |
| 681 |
break; |
| 682 |
|
| 683 |
case $kTileModeMirror: { |
| 684 |
float t_1 = t.x - 1; |
| 685 |
t.x = t_1 - 2 * floor(t_1 * 0.5) - 1; |
| 686 |
if (sk_Caps.mustDoOpBetweenFloorAndAbs) { |
| 687 |
// At this point the expected value of tiled_t should between -1 and 1, so this |
| 688 |
// clamp has no effect other than to break up the floor and abs calls and make sure |
| 689 |
// the compiler doesn't merge them back together. |
| 690 |
t.x = clamp(t.x, -1, 1); |
| 691 |
} |
| 692 |
t.x = abs(t.x); |
| 693 |
break; |
| 694 |
} |
| 695 |
|
| 696 |
case $kTileModeDecal: |
| 697 |
if (t.x < 0 || t.x > 1) { |
| 698 |
return float2(0, -1); |
| 699 |
} |
| 700 |
break; |
| 701 |
} |
| 702 |
|
| 703 |
return t; |
| 704 |
} |
| 705 |
|
| 706 |
$pure half4 $colorize_grad_4(float4 colorsParam[4], float4 offsetsParam, float2 t) { |
| 707 |
if (t.y < 0) { |
| 708 |
return half4(0); |
| 709 |
|
| 710 |
} else if (t.x <= offsetsParam[0]) { |
| 711 |
return half4(colorsParam[0]); |
| 712 |
} else if (t.x < offsetsParam[1]) { |
| 713 |
return half4(mix(colorsParam[0], colorsParam[1], (t.x - offsetsParam[0]) / |
| 714 |
(offsetsParam[1] - offsetsParam[0]))); |
| 715 |
} else if (t.x < offsetsParam[2]) { |
| 716 |
return half4(mix(colorsParam[1], colorsParam[2], (t.x - offsetsParam[1]) / |
| 717 |
(offsetsParam[2] - offsetsParam[1]))); |
| 718 |
} else if (t.x < offsetsParam[3]) { |
| 719 |
return half4(mix(colorsParam[2], colorsParam[3], (t.x - offsetsParam[2]) / |
| 720 |
(offsetsParam[3] - offsetsParam[2]))); |
| 721 |
} else { |
| 722 |
return half4(colorsParam[3]); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
$pure half4 $colorize_grad_8(float4 colorsParam[8], float4 offsetsParam[2], float2 t) { |
| 727 |
if (t.y < 0) { |
| 728 |
return half4(0); |
| 729 |
|
| 730 |
// Unrolled binary search through intervals |
| 731 |
// ( .. 0), (0 .. 1), (1 .. 2), (2 .. 3), (3 .. 4), (4 .. 5), (5 .. 6), (6 .. 7), (7 .. ). |
| 732 |
} else if (t.x < offsetsParam[1][0]) { |
| 733 |
if (t.x < offsetsParam[0][2]) { |
| 734 |
if (t.x <= offsetsParam[0][0]) { |
| 735 |
return half4(colorsParam[0]); |
| 736 |
} else if (t.x < offsetsParam[0][1]) { |
| 737 |
return half4(mix(colorsParam[0], colorsParam[1], |
| 738 |
(t.x - offsetsParam[0][0]) / |
| 739 |
(offsetsParam[0][1] - offsetsParam[0][0]))); |
| 740 |
} else { |
| 741 |
return half4(mix(colorsParam[1], colorsParam[2], |
| 742 |
(t.x - offsetsParam[0][1]) / |
| 743 |
(offsetsParam[0][2] - offsetsParam[0][1]))); |
| 744 |
} |
| 745 |
} else { |
| 746 |
if (t.x < offsetsParam[0][3]) { |
| 747 |
return half4(mix(colorsParam[2], colorsParam[3], |
| 748 |
(t.x - offsetsParam[0][2]) / |
| 749 |
(offsetsParam[0][3] - offsetsParam[0][2]))); |
| 750 |
} else { |
| 751 |
return half4(mix(colorsParam[3], colorsParam[4], |
| 752 |
(t.x - offsetsParam[0][3]) / |
| 753 |
(offsetsParam[1][0] - offsetsParam[0][3]))); |
| 754 |
} |
| 755 |
} |
| 756 |
} else { |
| 757 |
if (t.x < offsetsParam[1][2]) { |
| 758 |
if (t.x < offsetsParam[1][1]) { |
| 759 |
return half4(mix(colorsParam[4], colorsParam[5], |
| 760 |
(t.x - offsetsParam[1][0]) / |
| 761 |
(offsetsParam[1][1] - offsetsParam[1][0]))); |
| 762 |
} else { |
| 763 |
return half4(mix(colorsParam[5], colorsParam[6], |
| 764 |
(t.x - offsetsParam[1][1]) / |
| 765 |
(offsetsParam[1][2] - offsetsParam[1][1]))); |
| 766 |
} |
| 767 |
} else { |
| 768 |
if (t.x < offsetsParam[1][3]) { |
| 769 |
return half4(mix(colorsParam[6], colorsParam[7], |
| 770 |
(t.x - offsetsParam[1][2]) / |
| 771 |
(offsetsParam[1][3] - offsetsParam[1][2]))); |
| 772 |
} else { |
| 773 |
return half4(colorsParam[7]); |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
$pure half4 $colorize_grad_tex(sampler2D colorsAndOffsetsSampler, int numStops, float2 t) { |
| 780 |
const float kColorCoord = 0.25; |
| 781 |
const float kOffsetCoord = 0.75; |
| 782 |
|
| 783 |
if (t.y < 0) { |
| 784 |
return half4(0); |
| 785 |
} else if (t.x == 0) { |
| 786 |
return sampleLod(colorsAndOffsetsSampler, float2(0, kColorCoord), 0); |
| 787 |
} else if (t.x == 1) { |
| 788 |
return sampleLod(colorsAndOffsetsSampler, float2(1, kColorCoord), 0); |
| 789 |
} else { |
| 790 |
float low = 0; |
| 791 |
float high = float(numStops); |
| 792 |
float invNumStops = 1.0 / high; |
| 793 |
for (int loop = 1; loop < numStops; loop += loop) { |
| 794 |
float mid = floor((low + high) * 0.5); |
| 795 |
float samplePos = (mid + 0.5) * invNumStops; |
| 796 |
|
| 797 |
float2 tmp = sampleLod(colorsAndOffsetsSampler, float2(samplePos, kOffsetCoord), 0).xy; |
| 798 |
float offset = ldexp(tmp.x, int(tmp.y)); |
| 799 |
|
| 800 |
if (t.x < offset) { |
| 801 |
high = mid; |
| 802 |
} else { |
| 803 |
low = mid; |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
high = (low + 1.5) * invNumStops; |
| 808 |
low = (low + 0.5) * invNumStops; |
| 809 |
half4 color0 = sampleLod(colorsAndOffsetsSampler, float2(low, kColorCoord), 0); |
| 810 |
half4 color1 = sampleLod(colorsAndOffsetsSampler, float2(high, kColorCoord), 0); |
| 811 |
|
| 812 |
float2 tmp = sampleLod(colorsAndOffsetsSampler, float2(low, kOffsetCoord), 0).xy; |
| 813 |
float offset0 = ldexp(tmp.x, int(tmp.y)); |
| 814 |
|
| 815 |
tmp = sampleLod(colorsAndOffsetsSampler, float2(high, kOffsetCoord), 0).xy; |
| 816 |
float offset1 = ldexp(tmp.x, int(tmp.y)); |
| 817 |
|
| 818 |
return half4(mix(color0, color1, |
| 819 |
(t.x - offset0) / |
| 820 |
(offset1 - offset0))); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
$pure half4 $half4_from_array(float[] arr, int offset) { |
| 825 |
return half4(arr[offset + 0], |
| 826 |
arr[offset + 1], |
| 827 |
arr[offset + 2], |
| 828 |
arr[offset + 3]); |
| 829 |
} |
| 830 |
|
| 831 |
$pure half4 $colorize_grad_buf(float[] colorAndOffsetData, |
| 832 |
int offsetsBaseIndex, |
| 833 |
int numStops, |
| 834 |
float2 t) { |
| 835 |
int colorsBaseIndex = offsetsBaseIndex + numStops; |
| 836 |
if (t.y < 0) { |
| 837 |
return half4(0); |
| 838 |
} else if (t.x == 0) { |
| 839 |
return $half4_from_array(colorAndOffsetData, colorsBaseIndex); |
| 840 |
} else if (t.x == 1) { |
| 841 |
int lastColorIndex = colorsBaseIndex + (numStops - 1) * 4; |
| 842 |
return $half4_from_array(colorAndOffsetData, lastColorIndex); |
| 843 |
} else { |
| 844 |
// Binary search for the matching adjacent stop offsets running log2(numStops). |
| 845 |
int lowOffsetIndex = offsetsBaseIndex; |
| 846 |
int highOffsetIndex = lowOffsetIndex + numStops - 1; |
| 847 |
for (int i = 1; i < numStops; i += i) { |
| 848 |
int middleOffsetIndex = (lowOffsetIndex + highOffsetIndex) / 2; |
| 849 |
if (t.x < colorAndOffsetData[middleOffsetIndex]) { |
| 850 |
highOffsetIndex = middleOffsetIndex; |
| 851 |
} else { |
| 852 |
lowOffsetIndex = middleOffsetIndex; |
| 853 |
} |
| 854 |
} |
| 855 |
int lowColorIndex = colorsBaseIndex + (lowOffsetIndex - offsetsBaseIndex) * 4; |
| 856 |
float lowOffset = colorAndOffsetData[lowOffsetIndex]; |
| 857 |
half4 lowColor = $half4_from_array(colorAndOffsetData, lowColorIndex); |
| 858 |
|
| 859 |
int highColorIndex = colorsBaseIndex + (highOffsetIndex - offsetsBaseIndex) * 4; |
| 860 |
float highOffset = colorAndOffsetData[highOffsetIndex]; |
| 861 |
if (highOffset == lowOffset) { |
| 862 |
// If the t value falls exactly on a color stop, both lowOffset |
| 863 |
// and highOffset will be exactly the same so we avoid having |
| 864 |
// 0/0=NaN as our mix value. |
| 865 |
return lowColor; |
| 866 |
} else { |
| 867 |
half4 highColor = $half4_from_array(colorAndOffsetData, highColorIndex); |
| 868 |
|
| 869 |
return half4(mix(lowColor, highColor, (t.x - lowOffset) / (highOffset - lowOffset))); |
| 870 |
} |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
$pure float2 $linear_grad_layout(float2 pos) { |
| 875 |
// Add small epsilon since when the gradient is horizontally or vertically aligned, |
| 876 |
// pixels along the same column or row can have slightly different interpolated t values |
| 877 |
// causing pixels to choose the wrong offset when colorizing. This helps ensure pixels |
| 878 |
// along the same column or row choose the same gradient offsets. |
| 879 |
return float2(pos.x + 0.00001, 1); |
| 880 |
} |
| 881 |
|
| 882 |
$pure float2 $radial_grad_layout(float2 pos) { |
| 883 |
float t = length(pos); |
| 884 |
return float2(t, 1); |
| 885 |
} |
| 886 |
|
| 887 |
$pure float2 $sweep_grad_layout(float biasParam, float scaleParam, float2 pos) { |
| 888 |
// Some devices incorrectly implement atan2(y,x) as atan(y/x). In actuality it is |
| 889 |
// atan2(y,x) = 2 * atan(y / (sqrt(x^2 + y^2) + x)). To work around this we pass in |
| 890 |
// (sqrt(x^2 + y^2) + x) as the second parameter to atan2 in these cases. We let the device |
| 891 |
// handle the undefined behavior if the second parameter is 0, instead of doing the divide |
| 892 |
// ourselves and calling atan with the quotient. |
| 893 |
float angle; |
| 894 |
if (sk_Caps.atan2ImplementedAsAtanYOverX) { |
| 895 |
angle = 2 * atan(-pos.y, length(pos) - pos.x); |
| 896 |
} else { |
| 897 |
// Hardcode pi/2 for the angle when x == 0, to avoid undefined behavior in this |
| 898 |
// case. This hasn't proven to be necessary in the atan workaround case. |
| 899 |
angle = pos.x != 0.0 ? atan(-pos.y, -pos.x) : sign(pos.y) * -1.5707963267949; |
| 900 |
} |
| 901 |
|
| 902 |
// 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi] |
| 903 |
float t = (angle * 0.1591549430918 + 0.5 + biasParam) * scaleParam; |
| 904 |
return float2(t, 1); |
| 905 |
} |
| 906 |
|
| 907 |
$pure float2 $conical_grad_layout(float radius0, |
| 908 |
float dRadius, |
| 909 |
float a, |
| 910 |
float invA, |
| 911 |
float2 pos) { |
| 912 |
// When writing uniform values, if the gradient is radial, we encode a == 0 and since |
| 913 |
// the linear edge case is when a == 0, we differentiate the radial case with invA == 1. |
| 914 |
if (a == 0 && invA == 1) { |
| 915 |
// Radial case |
| 916 |
float t = length(pos) * dRadius - radius0; |
| 917 |
|
| 918 |
return float2(t, 1); |
| 919 |
} else { |
| 920 |
// Focal/strip case. |
| 921 |
float c = dot(pos, pos) - radius0 * radius0; |
| 922 |
float negB = 2 * (dRadius * radius0 + pos.x); |
| 923 |
|
| 924 |
float t; |
| 925 |
if (a == 0) { |
| 926 |
// Linear case, both circles intersect as exactly one point |
| 927 |
// with the focal point sitting on that point. |
| 928 |
|
| 929 |
// It is highly unlikely that b and c would be 0 resulting in NaN, if b == 0 due to |
| 930 |
// a specific translation, it would result is +/-Inf which propogates the sign to |
| 931 |
// isValid resulting in how the pixel is expected to look. |
| 932 |
t = c / negB; |
| 933 |
} else { |
| 934 |
// Quadratic case |
| 935 |
float d = negB*negB - 4*a*c; |
| 936 |
if (d < 0) { |
| 937 |
return float2(0, -1); |
| 938 |
} |
| 939 |
|
| 940 |
// T should be as large as possible, so when one circle fully encloses the other, |
| 941 |
// the sign will be positive or negative depending on the sign of dRadius. |
| 942 |
// When this isn't the case and they form a cone, the sign will always be positive. |
| 943 |
float quadSign = sign(1 - dRadius); |
| 944 |
t = invA * (negB + quadSign * sqrt(d)); |
| 945 |
} |
| 946 |
|
| 947 |
// Interpolated radius must be positive. |
| 948 |
float isValid = sign(t * dRadius + radius0); |
| 949 |
return float2(t, isValid); |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
$pure half4 sk_linear_grad_4_shader(float2 coords, |
| 954 |
float4 colorsParam[4], |
| 955 |
float4 offsetsParam, |
| 956 |
int tileMode, |
| 957 |
int colorSpace, |
| 958 |
int doUnpremul) { |
| 959 |
float2 t = $linear_grad_layout(coords); |
| 960 |
t = $tile_grad(tileMode, t); |
| 961 |
half4 color = $colorize_grad_4(colorsParam, offsetsParam, t); |
| 962 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 963 |
} |
| 964 |
|
| 965 |
$pure half4 sk_linear_grad_8_shader(float2 coords, |
| 966 |
float4 colorsParam[8], |
| 967 |
float4 offsetsParam[2], |
| 968 |
int tileMode, |
| 969 |
int colorSpace, |
| 970 |
int doUnpremul) { |
| 971 |
float2 t = $linear_grad_layout(coords); |
| 972 |
t = $tile_grad(tileMode, t); |
| 973 |
half4 color = $colorize_grad_8(colorsParam, offsetsParam, t); |
| 974 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 975 |
} |
| 976 |
|
| 977 |
$pure half4 sk_linear_grad_tex_shader(float2 coords, |
| 978 |
int numStops, |
| 979 |
int tileMode, |
| 980 |
int colorSpace, |
| 981 |
int doUnpremul, |
| 982 |
sampler2D colorAndOffsetSampler) { |
| 983 |
float2 t = $linear_grad_layout(coords); |
| 984 |
t = $tile_grad(tileMode, t); |
| 985 |
half4 color = $colorize_grad_tex(colorAndOffsetSampler, numStops, t); |
| 986 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 987 |
} |
| 988 |
|
| 989 |
$pure half4 sk_linear_grad_buf_shader(float2 coords, |
| 990 |
int numStops, |
| 991 |
int bufferOffset, |
| 992 |
int tileMode, |
| 993 |
int colorSpace, |
| 994 |
int doUnpremul, |
| 995 |
float[] colorAndOffsetData) { |
| 996 |
float2 t = $linear_grad_layout(coords); |
| 997 |
t = $tile_grad(tileMode, t); |
| 998 |
half4 color = $colorize_grad_buf(colorAndOffsetData, bufferOffset, numStops, t); |
| 999 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1000 |
} |
| 1001 |
|
| 1002 |
$pure half4 sk_radial_grad_4_shader(float2 coords, |
| 1003 |
float4 colorsParam[4], |
| 1004 |
float4 offsetsParam, |
| 1005 |
int tileMode, |
| 1006 |
int colorSpace, |
| 1007 |
int doUnpremul) { |
| 1008 |
float2 t = $radial_grad_layout(coords); |
| 1009 |
t = $tile_grad(tileMode, t); |
| 1010 |
half4 color = $colorize_grad_4(colorsParam, offsetsParam, t); |
| 1011 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1012 |
} |
| 1013 |
|
| 1014 |
$pure half4 sk_radial_grad_8_shader(float2 coords, |
| 1015 |
float4 colorsParam[8], |
| 1016 |
float4 offsetsParam[2], |
| 1017 |
int tileMode, |
| 1018 |
int colorSpace, |
| 1019 |
int doUnpremul) { |
| 1020 |
float2 t = $radial_grad_layout(coords); |
| 1021 |
t = $tile_grad(tileMode, t); |
| 1022 |
half4 color = $colorize_grad_8(colorsParam, offsetsParam, t); |
| 1023 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1024 |
} |
| 1025 |
|
| 1026 |
$pure half4 sk_radial_grad_tex_shader(float2 coords, |
| 1027 |
int numStops, |
| 1028 |
int tileMode, |
| 1029 |
int colorSpace, |
| 1030 |
int doUnpremul, |
| 1031 |
sampler2D colorAndOffsetSampler) { |
| 1032 |
float2 t = $radial_grad_layout(coords); |
| 1033 |
t = $tile_grad(tileMode, t); |
| 1034 |
half4 color = $colorize_grad_tex(colorAndOffsetSampler, numStops, t); |
| 1035 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1036 |
} |
| 1037 |
|
| 1038 |
$pure half4 sk_radial_grad_buf_shader(float2 coords, |
| 1039 |
int numStops, |
| 1040 |
int bufferOffset, |
| 1041 |
int tileMode, |
| 1042 |
int colorSpace, |
| 1043 |
int doUnpremul, |
| 1044 |
float[] colorAndOffsetData) { |
| 1045 |
float2 t = $radial_grad_layout(coords); |
| 1046 |
t = $tile_grad(tileMode, t); |
| 1047 |
half4 color = $colorize_grad_buf(colorAndOffsetData, bufferOffset, numStops, t); |
| 1048 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1049 |
} |
| 1050 |
|
| 1051 |
$pure half4 sk_sweep_grad_4_shader(float2 coords, |
| 1052 |
float4 colorsParam[4], |
| 1053 |
float4 offsetsParam, |
| 1054 |
float biasParam, |
| 1055 |
float scaleParam, |
| 1056 |
int tileMode, |
| 1057 |
int colorSpace, |
| 1058 |
int doUnpremul) { |
| 1059 |
float2 t = $sweep_grad_layout(biasParam, scaleParam, coords); |
| 1060 |
t = $tile_grad(tileMode, t); |
| 1061 |
half4 color = $colorize_grad_4(colorsParam, offsetsParam, t); |
| 1062 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1063 |
} |
| 1064 |
|
| 1065 |
$pure half4 sk_sweep_grad_8_shader(float2 coords, |
| 1066 |
float4 colorsParam[8], |
| 1067 |
float4 offsetsParam[2], |
| 1068 |
float biasParam, |
| 1069 |
float scaleParam, |
| 1070 |
int tileMode, |
| 1071 |
int colorSpace, |
| 1072 |
int doUnpremul) { |
| 1073 |
float2 t = $sweep_grad_layout(biasParam, scaleParam, coords); |
| 1074 |
t = $tile_grad(tileMode, t); |
| 1075 |
half4 color = $colorize_grad_8(colorsParam, offsetsParam, t); |
| 1076 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1077 |
} |
| 1078 |
|
| 1079 |
$pure half4 sk_sweep_grad_tex_shader(float2 coords, |
| 1080 |
float biasParam, |
| 1081 |
float scaleParam, |
| 1082 |
int numStops, |
| 1083 |
int tileMode, |
| 1084 |
int colorSpace, |
| 1085 |
int doUnpremul, |
| 1086 |
sampler2D colorAndOffsetSampler) { |
| 1087 |
float2 t = $sweep_grad_layout(biasParam, scaleParam, coords); |
| 1088 |
t = $tile_grad(tileMode, t); |
| 1089 |
half4 color = $colorize_grad_tex(colorAndOffsetSampler, numStops, t); |
| 1090 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1091 |
} |
| 1092 |
|
| 1093 |
$pure half4 sk_sweep_grad_buf_shader(float2 coords, |
| 1094 |
float biasParam, |
| 1095 |
float scaleParam, |
| 1096 |
int numStops, |
| 1097 |
int bufferOffset, |
| 1098 |
int tileMode, |
| 1099 |
int colorSpace, |
| 1100 |
int doUnpremul, |
| 1101 |
float[] colorAndOffsetData) { |
| 1102 |
float2 t = $sweep_grad_layout(biasParam, scaleParam, coords); |
| 1103 |
t = $tile_grad(tileMode, t); |
| 1104 |
half4 color = $colorize_grad_buf(colorAndOffsetData, bufferOffset, numStops, t); |
| 1105 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1106 |
} |
| 1107 |
|
| 1108 |
$pure half4 sk_conical_grad_4_shader(float2 coords, |
| 1109 |
float4 colorsParam[4], |
| 1110 |
float4 offsetsParam, |
| 1111 |
float radius0Param, |
| 1112 |
float dRadiusParam, |
| 1113 |
float aParam, |
| 1114 |
float invAParam, |
| 1115 |
int tileMode, |
| 1116 |
int colorSpace, |
| 1117 |
int doUnpremul) { |
| 1118 |
float2 t = $conical_grad_layout(radius0Param, dRadiusParam, aParam, invAParam, coords); |
| 1119 |
t = $tile_grad(tileMode, t); |
| 1120 |
half4 color = $colorize_grad_4(colorsParam, offsetsParam, t); |
| 1121 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1122 |
} |
| 1123 |
|
| 1124 |
$pure half4 sk_conical_grad_8_shader(float2 coords, |
| 1125 |
float4 colorsParam[8], |
| 1126 |
float4 offsetsParam[2], |
| 1127 |
float radius0Param, |
| 1128 |
float dRadiusParam, |
| 1129 |
float aParam, |
| 1130 |
float invAParam, |
| 1131 |
int tileMode, |
| 1132 |
int colorSpace, |
| 1133 |
int doUnpremul) { |
| 1134 |
float2 t = $conical_grad_layout(radius0Param, dRadiusParam, aParam, invAParam, coords); |
| 1135 |
t = $tile_grad(tileMode, t); |
| 1136 |
half4 color = $colorize_grad_8(colorsParam, offsetsParam, t); |
| 1137 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1138 |
} |
| 1139 |
|
| 1140 |
$pure half4 sk_conical_grad_tex_shader(float2 coords, |
| 1141 |
float radius0Param, |
| 1142 |
float dRadiusParam, |
| 1143 |
float aParam, |
| 1144 |
float invAParam, |
| 1145 |
int numStops, |
| 1146 |
int tileMode, |
| 1147 |
int colorSpace, |
| 1148 |
int doUnpremul, |
| 1149 |
sampler2D colorAndOffsetSampler) { |
| 1150 |
float2 t = $conical_grad_layout(radius0Param, dRadiusParam, aParam, invAParam, coords); |
| 1151 |
t = $tile_grad(tileMode, t); |
| 1152 |
half4 color = $colorize_grad_tex(colorAndOffsetSampler, numStops, t); |
| 1153 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1154 |
} |
| 1155 |
|
| 1156 |
$pure half4 sk_conical_grad_buf_shader(float2 coords, |
| 1157 |
float radius0Param, |
| 1158 |
float dRadiusParam, |
| 1159 |
float aParam, |
| 1160 |
float invAParam, |
| 1161 |
int numStops, |
| 1162 |
int bufferOffset, |
| 1163 |
int tileMode, |
| 1164 |
int colorSpace, |
| 1165 |
int doUnpremul, |
| 1166 |
float[] colorAndOffsetData) { |
| 1167 |
float2 t = $conical_grad_layout(radius0Param, dRadiusParam, aParam, invAParam, coords); |
| 1168 |
t = $tile_grad(tileMode, t); |
| 1169 |
half4 color = $colorize_grad_buf(colorAndOffsetData, bufferOffset, numStops, t); |
| 1170 |
return $interpolated_to_rgb_unpremul(color, colorSpace, doUnpremul); |
| 1171 |
} |
| 1172 |
|
| 1173 |
$pure half4 sk_hsl_matrix_colorfilter(half4 color, half4x4 m, half4 v) { |
| 1174 |
color = $rgb_to_hsl(color.rgb, color.a); // includes unpremul |
| 1175 |
color = (m * color) + v; |
| 1176 |
return $hsl_to_rgb(color.rgb, color.a); // includes clamp and premul |
| 1177 |
} |
| 1178 |
|
| 1179 |
$pure half4 sk_matrix_colorfilter(half4 color, half4x4 m, half4 v, half2 minMaxRGB) { |
| 1180 |
color = unpremul(color); |
| 1181 |
color = (m * color) + v; |
| 1182 |
|
| 1183 |
// minMaxRGB is a tuple that stores [minRGB, maxRGB]. minA and maxA are always 0 and 1. minRGB |
| 1184 |
// and maxRGB are either 0 and 1 for clamped color filters, or very large negative/positive |
| 1185 |
// values to be unclamped. |
| 1186 |
color = clamp(color, minMaxRGB.xxx0, minMaxRGB.yyy1); |
| 1187 |
color.rgb *= color.a; |
| 1188 |
return color; |
| 1189 |
} |
| 1190 |
|
| 1191 |
// This method computes the 4 x-coodinates ([0..1]) that should be used to look |
| 1192 |
// up in the Perlin noise shader's noise table. |
| 1193 |
$pure half4 $noise_helper(half2 noiseVec, |
| 1194 |
half2 stitchData, |
| 1195 |
int stitching, |
| 1196 |
sampler2D permutationSampler) { |
| 1197 |
const half kBlockSize = 256.0; |
| 1198 |
|
| 1199 |
half4 floorVal; |
| 1200 |
floorVal.xy = floor(noiseVec); |
| 1201 |
floorVal.zw = floorVal.xy + half2(1); |
| 1202 |
|
| 1203 |
// Adjust frequencies if we're stitching tiles |
| 1204 |
if (bool(stitching)) { |
| 1205 |
floorVal -= step(stitchData.xyxy, floorVal) * stitchData.xyxy; |
| 1206 |
} |
| 1207 |
|
| 1208 |
half sampleX = sample(permutationSampler, half2((floorVal.x + 0.5) / kBlockSize, 0.5)).r; |
| 1209 |
half sampleY = sample(permutationSampler, half2((floorVal.z + 0.5) / kBlockSize, 0.5)).r; |
| 1210 |
|
| 1211 |
half2 latticeIdx = half2(sampleX, sampleY); |
| 1212 |
|
| 1213 |
if (sk_Caps.PerlinNoiseRoundingFix) { |
| 1214 |
// Aggressively round to the nearest exact (N / 255) floating point values. |
| 1215 |
// This prevents rounding errors on some platforms (e.g., Tegras) |
| 1216 |
const half kInv255 = 1.0 / 255.0; |
| 1217 |
|
| 1218 |
latticeIdx = floor(latticeIdx * half2(255.0) + half2(0.5)) * half2(kInv255); |
| 1219 |
} |
| 1220 |
|
| 1221 |
// Get (x,y) coordinates with the permuted x |
| 1222 |
half4 noiseXCoords = kBlockSize*latticeIdx.xyxy + floorVal.yyww; |
| 1223 |
|
| 1224 |
noiseXCoords /= half4(kBlockSize); |
| 1225 |
return noiseXCoords; |
| 1226 |
} |
| 1227 |
|
| 1228 |
$pure half4 $noise_function(half2 noiseVec, |
| 1229 |
half4 noiseXCoords, |
| 1230 |
sampler2D noiseSampler) { |
| 1231 |
half2 fractVal = fract(noiseVec); |
| 1232 |
|
| 1233 |
// Hermite interpolation : t^2*(3 - 2*t) |
| 1234 |
half2 noiseSmooth = smoothstep(0, 1, fractVal); |
| 1235 |
|
| 1236 |
// This is used to convert the two 16bit integers packed into rgba 8 bit input into |
| 1237 |
// a [-1,1] vector |
| 1238 |
const half kInv256 = 0.00390625; // 1.0 / 256.0 |
| 1239 |
|
| 1240 |
half4 result; |
| 1241 |
|
| 1242 |
for (int channel = 0; channel < 4; channel++) { |
| 1243 |
|
| 1244 |
// There are 4 lines in the noise texture, put y coords at center of each. |
| 1245 |
half chanCoord = (half(channel) + 0.5) / 4.0; |
| 1246 |
|
| 1247 |
half4 sampleA = sample(noiseSampler, float2(noiseXCoords.x, chanCoord)); |
| 1248 |
half4 sampleB = sample(noiseSampler, float2(noiseXCoords.y, chanCoord)); |
| 1249 |
half4 sampleC = sample(noiseSampler, float2(noiseXCoords.w, chanCoord)); |
| 1250 |
half4 sampleD = sample(noiseSampler, float2(noiseXCoords.z, chanCoord)); |
| 1251 |
|
| 1252 |
half2 tmpFractVal = fractVal; |
| 1253 |
|
| 1254 |
// Compute u, at offset (0,0) |
| 1255 |
half u = dot((sampleA.ga + sampleA.rb*kInv256)*2 - 1, tmpFractVal); |
| 1256 |
|
| 1257 |
// Compute v, at offset (-1,0) |
| 1258 |
tmpFractVal.x -= 1.0; |
| 1259 |
half v = dot((sampleB.ga + sampleB.rb*kInv256)*2 - 1, tmpFractVal); |
| 1260 |
|
| 1261 |
// Compute 'a' as a linear interpolation of 'u' and 'v' |
| 1262 |
half a = mix(u, v, noiseSmooth.x); |
| 1263 |
|
| 1264 |
// Compute v, at offset (-1,-1) |
| 1265 |
tmpFractVal.y -= 1.0; |
| 1266 |
v = dot((sampleC.ga + sampleC.rb*kInv256)*2 - 1, tmpFractVal); |
| 1267 |
|
| 1268 |
// Compute u, at offset (0,-1) |
| 1269 |
tmpFractVal.x += 1.0; |
| 1270 |
u = dot((sampleD.ga + sampleD.rb*kInv256)*2 - 1, tmpFractVal); |
| 1271 |
|
| 1272 |
// Compute 'b' as a linear interpolation of 'u' and 'v' |
| 1273 |
half b = mix(u, v, noiseSmooth.x); |
| 1274 |
|
| 1275 |
// Compute the noise as a linear interpolation of 'a' and 'b' |
| 1276 |
result[channel] = mix(a, b, noiseSmooth.y); |
| 1277 |
} |
| 1278 |
|
| 1279 |
return result; |
| 1280 |
} |
| 1281 |
|
| 1282 |
// permutationSampler is [kBlockSize x 1] A8 |
| 1283 |
// noiseSampler is [kBlockSize x 4] RGBA8 premul |
| 1284 |
$pure half4 sk_perlin_noise_shader(float2 coords, |
| 1285 |
float2 baseFrequency, |
| 1286 |
float2 stitchDataIn, |
| 1287 |
int noiseType, |
| 1288 |
int numOctaves, |
| 1289 |
int stitching, |
| 1290 |
sampler2D permutationSampler, |
| 1291 |
sampler2D noiseSampler) { |
| 1292 |
const int kFractalNoise = 0; |
| 1293 |
const int kTurbulence = 1; |
| 1294 |
|
| 1295 |
// In the past, Perlin noise handled coordinates a bit differently than most shaders. |
| 1296 |
// It operated in device space, floored; it also had a one-pixel transform matrix applied to |
| 1297 |
// both the X and Y coordinates. This is roughly equivalent to adding 0.5 to the coordinates. |
| 1298 |
// This was originally done in order to better match preexisting golden images from WebKit. |
| 1299 |
// Perlin noise now operates in local space, which allows rotation to work correctly. To better |
| 1300 |
// approximate past behavior, we add 0.5 to the coordinates here. This is _not_ exactly the same |
| 1301 |
// because this adjustment is occurring in local space, not device space. |
| 1302 |
half2 noiseVec = half2((coords + 0.5) * baseFrequency); |
| 1303 |
|
| 1304 |
// Clear the color accumulator |
| 1305 |
half4 color = half4(0); |
| 1306 |
|
| 1307 |
half2 stitchData = half2(stitchDataIn); |
| 1308 |
|
| 1309 |
half ratio = 1.0; |
| 1310 |
|
| 1311 |
// Loop over all octaves |
| 1312 |
for (int octave = 0; octave < numOctaves; ++octave) { |
| 1313 |
half4 noiseXCoords = $noise_helper(noiseVec, stitchData, stitching, permutationSampler); |
| 1314 |
|
| 1315 |
half4 tmp = $noise_function(noiseVec, noiseXCoords, noiseSampler); |
| 1316 |
|
| 1317 |
if (noiseType != kFractalNoise) { |
| 1318 |
// For kTurbulence the result is: abs(noise[-1,1]) |
| 1319 |
tmp = abs(tmp); |
| 1320 |
} |
| 1321 |
|
| 1322 |
color += tmp * ratio; |
| 1323 |
|
| 1324 |
noiseVec *= half2(2.0); |
| 1325 |
ratio *= 0.5; |
| 1326 |
stitchData *= half2(2.0); |
| 1327 |
} |
| 1328 |
|
| 1329 |
if (noiseType == kFractalNoise) { |
| 1330 |
// For kFractalNoise the result is: noise[-1,1] * 0.5 + 0.5 |
| 1331 |
color = color * half4(0.5) + half4(0.5); |
| 1332 |
} |
| 1333 |
|
| 1334 |
// Clamp values |
| 1335 |
color = saturate(color); |
| 1336 |
|
| 1337 |
// Pre-multiply the result |
| 1338 |
return half4(color.rgb * color.a, color.a); |
| 1339 |
} |
| 1340 |
|
| 1341 |
$pure half4 sk_porter_duff_blend(half4 src, half4 dst, half4 coeffs) { |
| 1342 |
return blend_porter_duff(coeffs, src, dst); |
| 1343 |
} |
| 1344 |
|
| 1345 |
$pure half4 sk_hslc_blend(half4 src, half4 dst, half2 flipSat) { |
| 1346 |
return blend_hslc(flipSat, src, dst); |
| 1347 |
} |
| 1348 |
|
| 1349 |
$pure half4 sk_table_colorfilter(half4 inColor, sampler2D s) { |
| 1350 |
half4 coords = unpremul(inColor) * (255.0/256.0) + (0.5/256.0); |
| 1351 |
half4 color = half4(sample(s, half2(coords.r, 3.0/8.0)).r, |
| 1352 |
sample(s, half2(coords.g, 5.0/8.0)).r, |
| 1353 |
sample(s, half2(coords.b, 7.0/8.0)).r, |
| 1354 |
1); |
| 1355 |
return color * sample(s, half2(coords.a, 1.0/8.0)).r; |
| 1356 |
} |
| 1357 |
|
| 1358 |
$pure half4 sk_gaussian_colorfilter(half4 inColor) { |
| 1359 |
half factor = 1 - inColor.a; |
| 1360 |
factor = exp(-factor * factor * 4) - 0.018; |
| 1361 |
return half4(factor); |
| 1362 |
} |
| 1363 |
|
| 1364 |
$pure half4 sample_indexed_atlas(float2 textureCoords, |
| 1365 |
int atlasIndex, |
| 1366 |
sampler2D atlas0, |
| 1367 |
sampler2D atlas1, |
| 1368 |
sampler2D atlas2, |
| 1369 |
sampler2D atlas3) { |
| 1370 |
switch (atlasIndex) { |
| 1371 |
case 1: |
| 1372 |
return sample(atlas1, textureCoords); |
| 1373 |
case 2: |
| 1374 |
return sample(atlas2, textureCoords); |
| 1375 |
case 3: |
| 1376 |
return sample(atlas3, textureCoords); |
| 1377 |
default: |
| 1378 |
return sample(atlas0, textureCoords); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
$pure half3 $sample_indexed_atlas_lcd(float2 textureCoords, |
| 1383 |
int atlasIndex, |
| 1384 |
half2 offset, |
| 1385 |
sampler2D atlas0, |
| 1386 |
sampler2D atlas1, |
| 1387 |
sampler2D atlas2, |
| 1388 |
sampler2D atlas3) { |
| 1389 |
half3 distance = half3(1); |
| 1390 |
switch (atlasIndex) { |
| 1391 |
case 1: |
| 1392 |
distance.x = sample(atlas1, half2(textureCoords) - offset).r; |
| 1393 |
distance.y = sample(atlas1, textureCoords).r; |
| 1394 |
distance.z = sample(atlas1, half2(textureCoords) + offset).r; |
| 1395 |
break; |
| 1396 |
case 2: |
| 1397 |
distance.x = sample(atlas2, half2(textureCoords) - offset).r; |
| 1398 |
distance.y = sample(atlas2, textureCoords).r; |
| 1399 |
distance.z = sample(atlas2, half2(textureCoords) + offset).r; |
| 1400 |
break; |
| 1401 |
case 3: |
| 1402 |
distance.x = sample(atlas3, half2(textureCoords) - offset).r; |
| 1403 |
distance.y = sample(atlas3, textureCoords).r; |
| 1404 |
distance.z = sample(atlas3, half2(textureCoords) + offset).r; |
| 1405 |
break; |
| 1406 |
default: |
| 1407 |
distance.x = sample(atlas0, half2(textureCoords) - offset).r; |
| 1408 |
distance.y = sample(atlas0, textureCoords).r; |
| 1409 |
distance.z = sample(atlas0, half2(textureCoords) + offset).r; |
| 1410 |
break; |
| 1411 |
} |
| 1412 |
return distance; |
| 1413 |
} |
| 1414 |
|
| 1415 |
$pure half4 bitmap_text_coverage_fn(half4 texColor, int maskFormat) { |
| 1416 |
return (maskFormat == $kMaskFormatA8) ? texColor.rrrr |
| 1417 |
: texColor; |
| 1418 |
} |
| 1419 |
|
| 1420 |
$pure half4 sdf_text_coverage_fn(half texColor, |
| 1421 |
half2 gammaParams, |
| 1422 |
float2 unormTexCoords) { |
| 1423 |
// TODO: To minimize the number of shaders generated this is the full affine shader. |
| 1424 |
// For best performance it may be worth creating the uniform scale shader as well, |
| 1425 |
// as that's the most common case. |
| 1426 |
// TODO: Need to add 565 support. |
| 1427 |
|
| 1428 |
// The distance field is constructed as uchar8_t values, so that the zero value is at 128, |
| 1429 |
// and the supported range of distances is [-4 * 127/128, 4]. |
| 1430 |
// Hence to convert to floats our multiplier (width of the range) is 4 * 255/128 = 7.96875 |
| 1431 |
// and zero threshold is 128/255 = 0.50196078431. |
| 1432 |
half dist = 7.96875 * (texColor - 0.50196078431); |
| 1433 |
|
| 1434 |
// We may further adjust the distance for gamma correction. |
| 1435 |
dist -= gammaParams.x; |
| 1436 |
|
| 1437 |
// After the distance is unpacked, we need to correct it by a factor dependent on the |
| 1438 |
// current transformation. For general transforms, to determine the amount of correction |
| 1439 |
// we multiply a unit vector pointing along the SDF gradient direction by the Jacobian of |
| 1440 |
// unormTexCoords (which is the inverse transform for this fragment) and take the length of |
| 1441 |
// the result. |
| 1442 |
half2 dist_grad = half2(dFdx(dist), dFdy(dist)); |
| 1443 |
half dg_len2 = dot(dist_grad, dist_grad); |
| 1444 |
|
| 1445 |
// The length of the gradient may be near 0, so we need to check for that. This also |
| 1446 |
// compensates for the Adreno, which likes to drop tiles on division by 0. |
| 1447 |
dist_grad = (dg_len2 >= 0.0001) ? dist_grad * inversesqrt(dg_len2) |
| 1448 |
: half2(0.7071); |
| 1449 |
|
| 1450 |
// Computing the Jacobian and multiplying by the gradient. |
| 1451 |
float2x2 jacobian = float2x2(dFdx(unormTexCoords), dFdy(unormTexCoords)); |
| 1452 |
half2 grad = half2(jacobian * dist_grad); |
| 1453 |
|
| 1454 |
// This gives us a smooth step across approximately one fragment. |
| 1455 |
half approxFragWidth = 0.65 * length(grad); |
| 1456 |
|
| 1457 |
// TODO: handle aliased rendering |
| 1458 |
if (gammaParams.y > 0) { |
| 1459 |
// The smoothstep falloff compensates for the non-linear sRGB response curve. If we are |
| 1460 |
// doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want |
| 1461 |
// distance mapped linearly to coverage, so use a linear step: |
| 1462 |
return half4(saturate((dist + approxFragWidth) / (2.0 * approxFragWidth))); |
| 1463 |
} else { |
| 1464 |
return half4(smoothstep(-approxFragWidth, approxFragWidth, dist)); |
| 1465 |
} |
| 1466 |
} |
| 1467 |
|
| 1468 |
$pure half4 sdf_text_lcd_coverage_fn(float2 textureCoords, |
| 1469 |
half2 pixelGeometryDelta, |
| 1470 |
half4 gammaParams, |
| 1471 |
float2 unormTexCoords, |
| 1472 |
float texIndex, |
| 1473 |
sampler2D atlas0, |
| 1474 |
sampler2D atlas1, |
| 1475 |
sampler2D atlas2, |
| 1476 |
sampler2D atlas3) { |
| 1477 |
// TODO: To minimize the number of shaders generated this is the full affine shader. |
| 1478 |
// For best performance it may be worth creating the uniform scale shader as well, |
| 1479 |
// as that's the most common case. |
| 1480 |
|
| 1481 |
float2x2 jacobian = float2x2(dFdx(unormTexCoords), dFdy(unormTexCoords)); |
| 1482 |
half2 offset = half2(jacobian * pixelGeometryDelta); |
| 1483 |
|
| 1484 |
half3 distance = $sample_indexed_atlas_lcd(textureCoords, |
| 1485 |
int(texIndex), |
| 1486 |
offset, |
| 1487 |
atlas0, |
| 1488 |
atlas1, |
| 1489 |
atlas2, |
| 1490 |
atlas3); |
| 1491 |
// The distance field is constructed as uchar8_t values, so that the zero value is at 128, |
| 1492 |
// and the supported range of distances is [-4 * 127/128, 4]. |
| 1493 |
// Hence to convert to floats our multiplier (width of the range) is 4 * 255/128 = 7.96875 |
| 1494 |
// and zero threshold is 128/255 = 0.50196078431. |
| 1495 |
half3 dist = half3(7.96875) * (distance - half3(0.50196078431)); |
| 1496 |
|
| 1497 |
// We may further adjust the distance for gamma correction. |
| 1498 |
dist -= gammaParams.xyz; |
| 1499 |
|
| 1500 |
// After the distance is unpacked, we need to correct it by a factor dependent on the |
| 1501 |
// current transformation. For general transforms, to determine the amount of correction |
| 1502 |
// we multiply a unit vector pointing along the SDF gradient direction by the Jacobian of |
| 1503 |
// unormTexCoords (which is the inverse transform for this fragment) and take the length of |
| 1504 |
// the result. |
| 1505 |
half2 dist_grad = half2(dFdx(dist.g), dFdy(dist.g)); |
| 1506 |
half dg_len2 = dot(dist_grad, dist_grad); |
| 1507 |
|
| 1508 |
// The length of the gradient may be near 0, so we need to check for that. This also |
| 1509 |
// compensates for the Adreno, which likes to drop tiles on division by 0. |
| 1510 |
dist_grad = (dg_len2 >= 0.0001) ? dist_grad * inversesqrt(dg_len2) |
| 1511 |
: half2(0.7071); |
| 1512 |
|
| 1513 |
// Multiplying the Jacobian by the gradient. |
| 1514 |
half2 grad = half2(jacobian * dist_grad); |
| 1515 |
|
| 1516 |
// This gives us a smooth step across approximately one fragment. |
| 1517 |
half3 approxFragWidth = half3(0.65 * length(grad)); |
| 1518 |
|
| 1519 |
// TODO: handle aliased rendering |
| 1520 |
if (gammaParams.w > 0) { |
| 1521 |
// The smoothstep falloff compensates for the non-linear sRGB response curve. If we are |
| 1522 |
// doing gamma-correct rendering (to an sRGB or F16 buffer), then we actually want |
| 1523 |
// distance mapped linearly to coverage, so use a linear step: |
| 1524 |
return half4(saturate((dist + approxFragWidth / (2.0 * approxFragWidth))), 1); |
| 1525 |
} else { |
| 1526 |
return half4(smoothstep(half3(-approxFragWidth), half3(approxFragWidth), dist), 1); |
| 1527 |
} |
| 1528 |
} |
| 1529 |
|
| 1530 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1531 |
// Support functions for analytic round rectangles |
| 1532 |
|
| 1533 |
// Calculates 1/|∇| in device space by applying the chain rule to a local gradient vector and the |
| 1534 |
// 2x2 Jacobian describing the transform from local-to-device space. For non-perspective, this is |
| 1535 |
// equivalent to the "normal matrix", or the inverse transpose. For perspective, J should be |
| 1536 |
// W(u,v) [m00' - m20'u m01' - m21'u] derived from the first two columns of the 3x3 inverse. |
| 1537 |
// [m10' - m20'v m11' - m21'v] |
| 1538 |
$pure float $inverse_grad_len(float2 localGrad, float2x2 jacobian) { |
| 1539 |
// NOTE: By chain rule, the local gradient is on the left side of the Jacobian matrix |
| 1540 |
float2 devGrad = localGrad * jacobian; |
| 1541 |
// NOTE: This uses the L2 norm, which is more accurate than the L1 norm used by fwidth(). |
| 1542 |
// TODO: Switch to L1 since it is a 2x perf improvement according to Xcode with little visual |
| 1543 |
// impact, but start with L2 to measure the change separately from the algorithmic update. |
| 1544 |
// return 1.0 / (abs(devGrad.x) + abs(devGrad.y)); |
| 1545 |
return inversesqrt(dot(devGrad, devGrad)); |
| 1546 |
} |
| 1547 |
|
| 1548 |
// Returns distance from both sides of a stroked circle or ellipse. Elliptical coverage is |
| 1549 |
// only accurate if strokeRadius = 0. A positive value represents the interior of the stroke. |
| 1550 |
$pure float2 $elliptical_distance(float2 uv, float2 radii, float strokeRadius, float2x2 jacobian) { |
| 1551 |
// We do need to evaluate up to two circle equations: one with |
| 1552 |
// R = cornerRadius(r)+strokeRadius(s), and another with R = r-s. |
| 1553 |
// This can be consolidated into a common evaluation against a circle of radius sqrt(r^2+s^2): |
| 1554 |
// (x/(r+/-s))^2 + (y/(r+/-s))^2 = 1 |
| 1555 |
// x^2 + y^2 = (r+/-s)^2 |
| 1556 |
// x^2 + y^2 = r^2 + s^2 +/- 2rs |
| 1557 |
// (x/sqrt(r^2+s^2))^2 + (y/sqrt(r^2+s^2)) = 1 +/- 2rs/(r^2+s^2) |
| 1558 |
// The 2rs/(r^2+s^2) is the "width" that adjusts the implicit function to the outer or inner |
| 1559 |
// edge of the stroke. For fills and hairlines, s = 0, which means these operations remain valid |
| 1560 |
// for elliptical corners where radii holds the different X and Y corner radii. |
| 1561 |
float2 invR2 = 1.0 / (radii * radii + strokeRadius*strokeRadius); |
| 1562 |
float2 normUV = invR2 * uv; |
| 1563 |
float invGradLength = $inverse_grad_len(normUV, jacobian); |
| 1564 |
|
| 1565 |
// Since normUV already includes 1/r^2 in the denominator, dot with just 'uv' instead. |
| 1566 |
float f = 0.5 * invGradLength * (dot(uv, normUV) - 1.0); |
| 1567 |
|
| 1568 |
// This is 0 for fills/hairlines, which are the only types that allow |
| 1569 |
// elliptical corners (strokeRadius == 0). For regular strokes just use X. |
| 1570 |
float width = radii.x * strokeRadius * invR2.x * invGradLength; |
| 1571 |
return float2(width - f, width + f); |
| 1572 |
} |
| 1573 |
|
| 1574 |
// Accumulates the minimum (and negative maximum) of the outer and inner corner distances in 'dist' |
| 1575 |
// for a possibly elliptical corner with 'radii' and relative pixel location specified by |
| 1576 |
// 'cornerEdgeDist'. The corner's basis relative to the jacobian is defined in 'xyFlip'. |
| 1577 |
void $corner_distance(inout float2 dist, |
| 1578 |
float2x2 jacobian, |
| 1579 |
float2 strokeParams, |
| 1580 |
float2 cornerEdgeDist, |
| 1581 |
float2 xyFlip, |
| 1582 |
float2 radii) { |
| 1583 |
float2 uv = radii - cornerEdgeDist; |
| 1584 |
// NOTE: For mitered corners uv > 0 only if it's stroked, and in that case the |
| 1585 |
// subsequent conditions skip calculating anything. |
| 1586 |
if (all(greaterThan(uv, float2(0.0)))) { |
| 1587 |
if (all(greaterThan(radii, float2(0.0))) || |
| 1588 |
(strokeParams.x > 0.0 && strokeParams.y < 0.0 /* round-join */)) { |
| 1589 |
// A rounded corner so incorporate outer elliptical distance if we're within the |
| 1590 |
// quarter circle. |
| 1591 |
float2 d = $elliptical_distance(uv * xyFlip, radii, strokeParams.x, jacobian); |
| 1592 |
d.y = (radii.x - strokeParams.x <= 0.0) |
| 1593 |
? 1.0 // Disregard inner curve since it's collapsed into an inner miter. |
| 1594 |
: -d.y; // Negate so that "min" accumulates the maximum value instead. |
| 1595 |
dist = min(dist, d); |
| 1596 |
} else if (strokeParams.y == 0.0 /* bevel-join */) { |
| 1597 |
// Bevels are--by construction--interior mitered, so inner distance is based |
| 1598 |
// purely on the edge distance calculations, but the outer distance is to a 45-degree |
| 1599 |
// line and not the vertical/horizontal lines of the other edges. |
| 1600 |
float bevelDist = (strokeParams.x - uv.x - uv.y) * $inverse_grad_len(xyFlip, jacobian); |
| 1601 |
dist.x = min(dist.x, bevelDist); |
| 1602 |
} // Else it's a miter so both inner and outer distances are unmodified |
| 1603 |
} // Else we're not affected by the corner so leave distances unmodified |
| 1604 |
} |
| 1605 |
|
| 1606 |
// Accumulates the minimum (and negative maximum) of the outer and inner corner distances into 'd', |
| 1607 |
// for all four corners of a [round] rectangle. 'edgeDists' should be ordered LTRB with positive |
| 1608 |
// distance representing the interior of the edge. 'xRadii' and 'yRadii' should hold the per-corner |
| 1609 |
// elliptical radii, ordered TL, TR, BR, BL. |
| 1610 |
void $corner_distances(inout float2 d, |
| 1611 |
float2x2 J, |
| 1612 |
float2 stroke, // {radii, joinStyle}, see StrokeStyle struct definition |
| 1613 |
float4 edgeDists, |
| 1614 |
float4 xRadii, |
| 1615 |
float4 yRadii) { |
| 1616 |
$corner_distance(d, J, stroke, edgeDists.xy, float2(-1.0, -1.0), float2(xRadii[0], yRadii[0])); |
| 1617 |
$corner_distance(d, J, stroke, edgeDists.zy, float2( 1.0, -1.0), float2(xRadii[1], yRadii[1])); |
| 1618 |
$corner_distance(d, J, stroke, edgeDists.zw, float2( 1.0, 1.0), float2(xRadii[2], yRadii[2])); |
| 1619 |
$corner_distance(d, J, stroke, edgeDists.xw, float2(-1.0, 1.0), float2(xRadii[3], yRadii[3])); |
| 1620 |
} |
| 1621 |
|
| 1622 |
$pure half4 analytic_rrect_coverage_fn(float4 coords, |
| 1623 |
float4 jacobian, |
| 1624 |
float4 edgeDistances, |
| 1625 |
float4 xRadii, |
| 1626 |
float4 yRadii, |
| 1627 |
float2 strokeParams, |
| 1628 |
float2 perPixelControl) { |
| 1629 |
if (perPixelControl.x > 0.0) { |
| 1630 |
// A trivially solid interior pixel, either from a filled rect or round rect, or a |
| 1631 |
// stroke with sufficiently large width that the interior completely overlaps itself. |
| 1632 |
return half4(1.0); |
| 1633 |
} else if (perPixelControl.y > 1.0) { |
| 1634 |
// This represents a filled rectangle or quadrilateral, where the distances have already |
| 1635 |
// been converted to device space. Mitered strokes cannot use this optimization because |
| 1636 |
// their scale and bias is not uniform over the shape; Rounded shapes cannot use this |
| 1637 |
// because they rely on the edge distances being in local space to reconstruct the |
| 1638 |
// per-corner positions for the elliptical implicit functions. |
| 1639 |
float2 outerDist = min(edgeDistances.xy, edgeDistances.zw); |
| 1640 |
float c = min(outerDist.x, outerDist.y) * coords.w; |
| 1641 |
float scale = (perPixelControl.y - 1.0) * coords.w; |
| 1642 |
float bias = coverage_bias(scale); |
| 1643 |
return half4(saturate(scale * (c + bias))); |
| 1644 |
} else { |
| 1645 |
// Compute per-pixel coverage, mixing four outer edge distances, possibly four inner |
| 1646 |
// edge distances, and per-corner elliptical distances into a final coverage value. |
| 1647 |
// The Jacobian needs to be multiplied by W, but coords.w stores 1/w. |
| 1648 |
float2x2 J = float2x2(jacobian) / coords.w; |
| 1649 |
|
| 1650 |
float2 invGradLen = float2($inverse_grad_len(float2(1.0, 0.0), J), |
| 1651 |
$inverse_grad_len(float2(0.0, 1.0), J)); |
| 1652 |
float2 outerDist = invGradLen * (strokeParams.x + min(edgeDistances.xy, |
| 1653 |
edgeDistances.zw)); |
| 1654 |
|
| 1655 |
// d.x tracks minimum outer distance (pre scale-and-biasing to a coverage value). |
| 1656 |
// d.y tracks negative maximum inner distance (so min() over c accumulates min and outer |
| 1657 |
// and max inner simultaneously).) |
| 1658 |
float2 d = float2(min(outerDist.x, outerDist.y), -1.0); |
| 1659 |
float scale, bias; |
| 1660 |
|
| 1661 |
// Check for bidirectional coverage, which is is marked as a -1 from the vertex shader. |
| 1662 |
// We don't just check for < 0 since extrapolated fill triangle samples can have small |
| 1663 |
// negative values. |
| 1664 |
if (perPixelControl.x > -0.95) { |
| 1665 |
// A solid interior, so update scale and bias based on full width and height |
| 1666 |
float2 dim = invGradLen * (edgeDistances.xy + edgeDistances.zw + 2*strokeParams.xx); |
| 1667 |
scale = min(min(dim.x, dim.y), 1.0); |
| 1668 |
bias = coverage_bias(scale); |
| 1669 |
// Since we leave d.y = -1.0, no inner curve coverage will adjust it closer to 0, |
| 1670 |
// so 'finalCoverage' is based solely on outer edges and curves. |
| 1671 |
} else { |
| 1672 |
// Bidirectional coverage, so we modify c.y to hold the negative of the maximum |
| 1673 |
// interior coverage, and update scale and bias based on stroke width. |
| 1674 |
float2 strokeWidth = 2.0 * strokeParams.x * invGradLen; |
| 1675 |
float2 innerDist = strokeWidth - outerDist; |
| 1676 |
|
| 1677 |
d.y = -max(innerDist.x, innerDist.y); |
| 1678 |
if (strokeParams.x > 0.0) { |
| 1679 |
float narrowStroke = min(strokeWidth.x, strokeWidth.y); |
| 1680 |
// On an axis where innerDist >= -0.5, allow strokeWidth.x/y to be preserved as-is. |
| 1681 |
// On an axis where innerDist < -0.5, use the smaller of strokeWidth.x/y. |
| 1682 |
float2 strokeDim = mix(float2(narrowStroke), strokeWidth, |
| 1683 |
greaterThanEqual(innerDist, float2(-0.5))); |
| 1684 |
// Preserve the wider axis from the above calculation. |
| 1685 |
scale = saturate(max(strokeDim.x, strokeDim.y)); |
| 1686 |
bias = coverage_bias(scale); |
| 1687 |
} else { |
| 1688 |
// A hairline, so scale and bias should both be 1 |
| 1689 |
scale = bias = 1.0; |
| 1690 |
} |
| 1691 |
} |
| 1692 |
|
| 1693 |
// Check all corners, although most pixels should only be influenced by 1. |
| 1694 |
$corner_distances(d, J, strokeParams, edgeDistances, xRadii, yRadii); |
| 1695 |
|
| 1696 |
float outsetDist = min(perPixelControl.y, 0.0) * coords.w; |
| 1697 |
float finalCoverage = scale * (min(d.x + outsetDist, -d.y) + bias); |
| 1698 |
|
| 1699 |
return half4(saturate(finalCoverage)); |
| 1700 |
} |
| 1701 |
} |
| 1702 |
|
| 1703 |
$pure half4 per_edge_aa_quad_coverage_fn(float4 coords, |
| 1704 |
float4 edgeDistances) { |
| 1705 |
// This represents a filled rectangle or quadrilateral, where the distances have already |
| 1706 |
// been converted to device space. |
| 1707 |
float2 outerDist = min(edgeDistances.xy, edgeDistances.zw); |
| 1708 |
float c = min(outerDist.x, outerDist.y) * coords.w; |
| 1709 |
return half4(saturate(c)); |
| 1710 |
} |
| 1711 |
|
| 1712 |
$pure half4 circular_arc_coverage_fn(float4 circleEdge, |
| 1713 |
float3 clipPlane, |
| 1714 |
float3 isectPlane, |
| 1715 |
float3 unionPlane, |
| 1716 |
float roundCapRadius, |
| 1717 |
float4 roundCapPos) { |
| 1718 |
float d = length(circleEdge.xy); |
| 1719 |
half distanceToOuterEdge = half(circleEdge.z * (1.0 - d)); |
| 1720 |
half edgeAlpha = saturate(distanceToOuterEdge); |
| 1721 |
half distanceToInnerEdge = half(circleEdge.z * (d - circleEdge.w)); |
| 1722 |
half innerAlpha = saturate(distanceToInnerEdge); |
| 1723 |
edgeAlpha *= innerAlpha; |
| 1724 |
|
| 1725 |
half clip = half(saturate(circleEdge.z * dot(circleEdge.xy, clipPlane.xy) + clipPlane.z)); |
| 1726 |
clip *= half(saturate(circleEdge.z * dot(circleEdge.xy, isectPlane.xy) + isectPlane.z)); |
| 1727 |
clip = clip + half(saturate(circleEdge.z * dot(circleEdge.xy, unionPlane.xy) + unionPlane.z)); |
| 1728 |
|
| 1729 |
// We compute coverage of the round caps as circles at the butt caps produced |
| 1730 |
// by the clip planes, and union it with the current clip. |
| 1731 |
half dcap1 = half(circleEdge.z * (roundCapRadius - length(circleEdge.xy - roundCapPos.xy))); |
| 1732 |
half dcap2 = half(circleEdge.z * (roundCapRadius - length(circleEdge.xy - roundCapPos.zw))); |
| 1733 |
half capAlpha = max(dcap1, 0) + max(dcap2, 0); |
| 1734 |
clip = saturate(clip + capAlpha); |
| 1735 |
|
| 1736 |
return half4(clip*edgeAlpha); |
| 1737 |
} |
| 1738 |
|
| 1739 |
$pure half4 $rect_blur_coverage_fn(float2 coords, |
| 1740 |
float4 rect, |
| 1741 |
half isFast, |
| 1742 |
half invSixSigma, |
| 1743 |
sampler2D integral) { |
| 1744 |
half xCoverage; |
| 1745 |
half yCoverage; |
| 1746 |
|
| 1747 |
if (isFast != 0.0) { |
| 1748 |
// Get the smaller of the signed distance from the frag coord to the left and right |
| 1749 |
// edges and similar for y. |
| 1750 |
// The integral texture goes "backwards" (from 3*sigma to -3*sigma), So, the below |
| 1751 |
// computations align the left edge of the integral texture with the inset rect's |
| 1752 |
// edge extending outward 6 * sigma from the inset rect. |
| 1753 |
half2 pos = max(half2(rect.LT - coords), half2(coords - rect.RB)); |
| 1754 |
xCoverage = sample(integral, float2(invSixSigma * pos.x, 0.5)).r; |
| 1755 |
yCoverage = sample(integral, float2(invSixSigma * pos.y, 0.5)).r; |
| 1756 |
|
| 1757 |
} else { |
| 1758 |
// We just consider just the x direction here. In practice we compute x and y |
| 1759 |
// separately and multiply them together. |
| 1760 |
// We define our coord system so that the point at which we're evaluating a kernel |
| 1761 |
// defined by the normal distribution (K) at 0. In this coord system let L be left |
| 1762 |
// edge and R be the right edge of the rectangle. |
| 1763 |
// We can calculate C by integrating K with the half infinite ranges outside the |
| 1764 |
// L to R range and subtracting from 1: |
| 1765 |
// C = 1 - <integral of K from from -inf to L> - <integral of K from R to inf> |
| 1766 |
// K is symmetric about x=0 so: |
| 1767 |
// C = 1 - <integral of K from from -inf to L> - <integral of K from -inf to -R> |
| 1768 |
|
| 1769 |
// The integral texture goes "backwards" (from 3*sigma to -3*sigma) which is |
| 1770 |
// factored in to the below calculations. |
| 1771 |
// Also, our rect uniform was pre-inset by 3 sigma from the actual rect being |
| 1772 |
// blurred, also factored in. |
| 1773 |
half4 rect = half4(half2(rect.LT - coords), half2(coords - rect.RB)); |
| 1774 |
xCoverage = 1 - sample(integral, float2(invSixSigma * rect.L, 0.5)).r |
| 1775 |
- sample(integral, float2(invSixSigma * rect.R, 0.5)).r; |
| 1776 |
yCoverage = 1 - sample(integral, float2(invSixSigma * rect.T, 0.5)).r |
| 1777 |
- sample(integral, float2(invSixSigma * rect.B, 0.5)).r; |
| 1778 |
} |
| 1779 |
|
| 1780 |
return half4(xCoverage * yCoverage); |
| 1781 |
} |
| 1782 |
|
| 1783 |
$pure half4 $circle_blur_coverage_fn(float2 coords, float4 circle, sampler2D blurProfile) { |
| 1784 |
// We just want to compute "(length(vec) - solidRadius + 0.5) / textureRadius" but need to |
| 1785 |
// rearrange to avoid passing large values to length() that would overflow. We've precalculated |
| 1786 |
// "1 / textureRadius" and "(solidRadius - 0.5) / textureRadius" on the CPU as circle.z and |
| 1787 |
// circle.w, respectively. |
| 1788 |
float invTextureRadius = circle.z; |
| 1789 |
float normSolidRadius = circle.w; |
| 1790 |
|
| 1791 |
half2 vec = half2((coords - circle.xy) * invTextureRadius); |
| 1792 |
float dist = length(vec) - normSolidRadius; |
| 1793 |
return sample(blurProfile, float2(dist, 0.5)).rrrr; |
| 1794 |
} |
| 1795 |
|
| 1796 |
$pure half4 $rrect_blur_coverage_fn(float2 coords, |
| 1797 |
float4 proxyRect, |
| 1798 |
half edgeSize, |
| 1799 |
sampler2D ninePatch) { |
| 1800 |
// Warp the fragment position to the appropriate part of the 9-patch blur texture by |
| 1801 |
// snipping out the middle section of the proxy rect. |
| 1802 |
float2 translatedFragPosFloat = coords - proxyRect.LT; |
| 1803 |
float2 proxyCenter = (proxyRect.RB - proxyRect.LT) * 0.5; |
| 1804 |
|
| 1805 |
// Position the fragment so that (0, 0) marks the center of the proxy rectangle. |
| 1806 |
// Negative coordinates are on the left/top side and positive numbers are on the |
| 1807 |
// right/bottom. |
| 1808 |
translatedFragPosFloat -= proxyCenter; |
| 1809 |
|
| 1810 |
// Temporarily strip off the fragment's sign. x/y are now strictly increasing as we |
| 1811 |
// move away from the center. |
| 1812 |
half2 fragDirection = half2(sign(translatedFragPosFloat)); |
| 1813 |
translatedFragPosFloat = abs(translatedFragPosFloat); |
| 1814 |
|
| 1815 |
// Our goal is to snip out the "middle section" of the proxy rect (everything but the |
| 1816 |
// edge). We've repositioned our fragment position so that (0, 0) is the centerpoint |
| 1817 |
// and x/y are always positive, so we can subtract here and interpret negative results |
| 1818 |
// as being within the middle section. |
| 1819 |
half2 translatedFragPosHalf = half2(translatedFragPosFloat - (proxyCenter - edgeSize)); |
| 1820 |
|
| 1821 |
// Remove the middle section by clamping to zero. |
| 1822 |
translatedFragPosHalf = max(translatedFragPosHalf, 0); |
| 1823 |
|
| 1824 |
// Reapply the fragment's sign, so that negative coordinates once again mean left/top |
| 1825 |
// side and positive means bottom/right side. |
| 1826 |
translatedFragPosHalf *= fragDirection; |
| 1827 |
|
| 1828 |
// Offset the fragment so that (0, 0) marks the upper-left again, instead of the center |
| 1829 |
// point. |
| 1830 |
translatedFragPosHalf += half2(edgeSize); |
| 1831 |
|
| 1832 |
half2 proxyDims = half2(2.0 * edgeSize); |
| 1833 |
half2 texCoord = translatedFragPosHalf / proxyDims; |
| 1834 |
|
| 1835 |
return sample(ninePatch, texCoord).rrrr; |
| 1836 |
} |
| 1837 |
|
| 1838 |
$pure half4 blur_coverage_fn(float2 coords, |
| 1839 |
float4 shapeData, |
| 1840 |
half2 blurData, |
| 1841 |
int shapeType, |
| 1842 |
sampler2D s) { |
| 1843 |
switch (shapeType) { |
| 1844 |
case $kShapeTypeRect: { |
| 1845 |
return $rect_blur_coverage_fn(coords, shapeData, blurData.x, blurData.y, s); |
| 1846 |
} |
| 1847 |
case $kShapeTypeCircle: { |
| 1848 |
return $circle_blur_coverage_fn(coords, shapeData, s); |
| 1849 |
} |
| 1850 |
case $kShapeTypeRRect: { |
| 1851 |
return $rrect_blur_coverage_fn(coords, shapeData, blurData.x, s); |
| 1852 |
} |
| 1853 |
} |
| 1854 |
return half4(0); |
| 1855 |
} |