49 "}", kMaxBlurSamples, kernelWidth).c_str(),
86 "}", kMaxBlurSamples, maxKernelSize).c_str(),
97 case StableKey::kInvalid:
101 case StableKey::k1DBlur4: {
103 return s1DBlurEffect;
105 case StableKey::k1DBlur8: {
107 return s1DBlurEffect;
109 case StableKey::k1DBlur12: {
111 return s1DBlurEffect;
113 case StableKey::k1DBlur16: {
115 return s1DBlurEffect;
117 case StableKey::k1DBlur20: {
119 return s1DBlurEffect;
121 case StableKey::k1DBlur28: {
123 return s1DBlurEffect;
125 case StableKey::k2DBlur4: {
127 return s2DBlurEffect;
129 case StableKey::k2DBlur8: {
131 return s2DBlurEffect;
133 case StableKey::k2DBlur12: {
135 return s2DBlurEffect;
137 case StableKey::k2DBlur16: {
139 return s2DBlurEffect;
141 case StableKey::k2DBlur20: {
143 return s2DBlurEffect;
145 case StableKey::k2DBlur28: {
147 return s2DBlurEffect;
149 case StableKey::kBlend: {
150 static constexpr char kBlendShaderCode[] =
151 "uniform shader s, d;"
153 "half4 main(float2 xy) {"
154 "return b.eval(s.eval(xy), d.eval(xy));"
163 case StableKey::kDecal: {
164 static constexpr char kDecalShaderCode[] =
165 "uniform shader image;"
166 "uniform float4 decalBounds;"
168 "half4 main(float2 coord) {"
169 "half4 d = half4(decalBounds - coord.xyxy) * half4(-1, -1, 1, 1);"
170 "d = saturate(d + 0.5);"
171 "return (d.x*d.y*d.z*d.w) * image.eval(coord);"
180 case StableKey::kDisplacement: {
184 static constexpr char kDisplacementShaderCode[] =
185 "uniform shader displMap;"
186 "uniform shader colorMap;"
187 "uniform half2 scale;"
188 "uniform half4 xSelect;"
189 "uniform half4 ySelect;"
191 "half4 main(float2 coord) {"
192 "half4 displColor = unpremul(displMap.eval(coord));"
193 "half2 displ = half2(dot(displColor, xSelect), dot(displColor, ySelect));"
194 "displ = scale * (displ - 0.5);"
195 "return colorMap.eval(coord + displ);"
200 kDisplacementShaderCode,
202 return sDisplacementEffect;
204 case StableKey::kLighting: {
205 static constexpr char kLightingShaderCode[] =
206 "const half kConeAAThreshold = 0.016;"
207 "const half kConeScale = 1.0 / kConeAAThreshold;"
209 "uniform shader normalMap;"
213 "uniform half4 materialAndLightType;"
215 "uniform half4 lightPosAndSpotFalloff;"
217 "uniform half4 lightDirAndSpotCutoff;"
219 "uniform half3 lightColor;"
221 "half3 surface_to_light(half3 coord) {"
222 "if (materialAndLightType.w < 0) {"
223 "return lightDirAndSpotCutoff.xyz;"
226 "return normalize(lightPosAndSpotFalloff.xyz - coord);"
230 "half spotlight_scale(half3 surfaceToLight) {"
231 "half cosCutoffAngle = lightDirAndSpotCutoff.w;"
232 "half cosAngle = -dot(surfaceToLight, lightDirAndSpotCutoff.xyz);"
233 "if (cosAngle < cosCutoffAngle) {"
236 "half scale = pow(cosAngle, lightPosAndSpotFalloff.w);"
237 "if (cosAngle < cosCutoffAngle + kConeAAThreshold) {"
238 "return scale * (cosAngle - cosCutoffAngle) * kConeScale;"
244 "half4 compute_lighting(half3 normal, half3 surfaceToLight) {"
246 "half3 color = lightColor;"
248 "if (materialAndLightType.w > 0) {"
249 "color *= spotlight_scale(surfaceToLight);"
253 "if (materialAndLightType.z == 0) {"
254 "half coeff = dot(normal, surfaceToLight);"
255 "color = saturate(coeff * color);"
256 "return half4(color, 1.0);"
258 "half3 halfDir = normalize(surfaceToLight + half3(0, 0, 1));"
259 "half shininess = materialAndLightType.y;"
260 "half coeff = pow(dot(normal, halfDir), shininess);"
261 "color = saturate(coeff * color);"
262 "return half4(color, max(max(color.r, color.g), color.b));"
266 "half4 main(float2 coord) {"
267 "half4 normalAndA = normalMap.eval(coord);"
268 "half depth = materialAndLightType.x;"
269 "half3 surfaceToLight = surface_to_light(half3(half2(coord),"
270 "depth*normalAndA.a));"
271 "return compute_lighting(normalAndA.xyz, surfaceToLight);"
278 return sLightingEffect;
280 case StableKey::kLinearMorphology: {
281 static constexpr char kLinearMorphologyShaderCode[] =
283 "const int kMaxLinearRadius = 14;"
285 "uniform shader child;"
286 "uniform half2 offset;"
288 "uniform int radius;"
290 "half4 main(float2 coord) {"
291 "half4 aggregate = flip*child.eval(coord);"
292 "for (int i = 1; i <= kMaxLinearRadius; ++i) {"
293 "if (i > radius) break;"
294 "half2 delta = half(i) * offset;"
295 "aggregate = max(aggregate, max(flip*child.eval(coord + delta),"
296 "flip*child.eval(coord - delta)));"
298 "return flip*aggregate;"
303 kLinearMorphologyShaderCode,
305 return sLinearMorphologyEffect;
308 case StableKey::kMagnifier: {
309 static constexpr char kMagnifierShaderCode[] =
310 "uniform shader src;"
311 "uniform float4 lensBounds;"
312 "uniform float4 zoomXform;"
313 "uniform float2 invInset;"
315 "half4 main(float2 coord) {"
316 "float2 zoomCoord = zoomXform.xy + zoomXform.zw*coord;"
319 "float2 edgeInset = min(coord - lensBounds.xy, lensBounds.zw - coord) *"
329 "float weight = (edgeInset.x < 2.0 && edgeInset.y < 2.0)"
331 "? (2.0 - length(2.0 - edgeInset))"
334 ": min(edgeInset.x, edgeInset.y);"
338 "weight = saturate(weight);"
339 "return src.eval(mix(coord, zoomCoord, weight*weight));"
344 kMagnifierShaderCode,
346 return sMagnifierEffect;
348 case StableKey::kNormal: {
349 static constexpr char kNormalShaderCode[] =
350 "uniform shader alphaMap;"
351 "uniform float4 edgeBounds;"
352 "uniform half negSurfaceDepth;"
354 "half3 normal(half3 alphaC0, half3 alphaC1, half3 alphaC2) {"
358 "const half3 kSobel = 0.25 * half3(1,2,1);"
359 "half3 alphaR0 = half3(alphaC0.x, alphaC1.x, alphaC2.x);"
360 "half3 alphaR2 = half3(alphaC0.z, alphaC1.z, alphaC2.z);"
361 "half nx = dot(kSobel, alphaC2) - dot(kSobel, alphaC0);"
362 "half ny = dot(kSobel, alphaR2) - dot(kSobel, alphaR0);"
363 "return normalize(half3(negSurfaceDepth * half2(nx, ny), 1));"
366 "half4 main(float2 coord) {"
367 "half3 alphaC0 = half3("
368 "alphaMap.eval(clamp(coord + float2(-1,-1), edgeBounds.LT, edgeBounds.RB)).a,"
369 "alphaMap.eval(clamp(coord + float2(-1, 0), edgeBounds.LT, edgeBounds.RB)).a,"
370 "alphaMap.eval(clamp(coord + float2(-1, 1), edgeBounds.LT, edgeBounds.RB)).a);"
371 "half3 alphaC1 = half3("
372 "alphaMap.eval(clamp(coord + float2( 0,-1), edgeBounds.LT, edgeBounds.RB)).a,"
373 "alphaMap.eval(clamp(coord + float2( 0, 0), edgeBounds.LT, edgeBounds.RB)).a,"
374 "alphaMap.eval(clamp(coord + float2( 0, 1), edgeBounds.LT, edgeBounds.RB)).a);"
375 "half3 alphaC2 = half3("
376 "alphaMap.eval(clamp(coord + float2( 1,-1), edgeBounds.LT, edgeBounds.RB)).a,"
377 "alphaMap.eval(clamp(coord + float2( 1, 0), edgeBounds.LT, edgeBounds.RB)).a,"
378 "alphaMap.eval(clamp(coord + float2( 1, 1), edgeBounds.LT, edgeBounds.RB)).a);"
380 "half mainAlpha = alphaC1.y;"
381 "return half4(normal(alphaC0, alphaC1, alphaC2), mainAlpha);"
388 return sNormalEffect;
390 case StableKey::kSparseMorphology: {
391 static constexpr char kSparseMorphologyShaderCode[] =
392 "uniform shader child;"
393 "uniform half2 offset;"
396 "half4 main(float2 coord) {"
397 "half4 aggregate = max(flip*child.eval(coord + offset),"
398 "flip*child.eval(coord - offset));"
399 "return flip*aggregate;"
404 kSparseMorphologyShaderCode,
406 return sSparseMorphologyEffect;
410 case StableKey::kArithmetic: {
411 static constexpr char kArithmeticBlenderCode[] =
413 "uniform half pmClamp;"
415 "half4 main(half4 src, half4 dst) {"
416 "half4 c = saturate(k.x * src * dst + k.y * src + k.z * dst + k.w);"
417 "c.rgb = min(c.rgb, max(c.a, pmClamp));"
423 kArithmeticBlenderCode,
425 return sArithmeticEffect;
429 case StableKey::kHighContrast: {
430 static constexpr char kHighContrastFilterCode[] =
431 "uniform half grayscale, invertStyle, contrast;"
433 "half3 rgb_to_hsl(half3 c) {"
434 "half mx = max(max(c.r,c.g),c.b),"
435 "mn = min(min(c.r,c.g),c.b),"
438 "g_lt_b = c.g < c.b ? 6.0 : 0.0;"
442 "half h = (1/6.0) * (mx == mn" "? 0.0 :"
443 "c.r >= c.g && c.r >= c.b ? invd * (c.g - c.b) + g_lt_b :"
444 "c.g >= c.b" "? invd * (c.b - c.r) + 2.0"
445 ": invd * (c.r - c.g) + 4.0);"
449 ": d / (l > 0.5 ? 2.0 - sum : sum);"
450 "return half3(h,s,l);"
452 "half4 main(half4 inColor) {"
453 "half3 c = inColor.rgb;"
454 "if (grayscale == 1) {"
455 "c = dot(half3(0.2126, 0.7152, 0.0722), c).rrr;"
457 "if (invertStyle == 1) {"
459 "} else if (invertStyle == 2) {"
462 "c = $hsl_to_rgb(c);"
464 "c = mix(half3(0.5), c, contrast);"
465 "return half4(saturate(c), inColor.a);"
470 kHighContrastFilterCode,
472 return sHighContrastEffect;
475 case StableKey::kLerp: {
476 static constexpr char kLerpFilterCode[] =
477 "uniform colorFilter cf0;"
478 "uniform colorFilter cf1;"
479 "uniform half weight;"
481 "half4 main(half4 color) {"
482 "return mix(cf0.eval(color), cf1.eval(color), weight);"
492 case StableKey::kLuma: {
493 static constexpr char kLumaFilterCode[] =
494 "half4 main(half4 inColor) {"
495 "return saturate(dot(half3(0.2126, 0.7152, 0.0722), inColor.rgb)).000r;"
505 case StableKey::kOverdraw: {
506 static constexpr char kOverdrawFilterCode[] =
507 "uniform half4 color0, color1, color2, color3, color4, color5;"
509 "half4 main(half4 color) {"
510 "half alpha = 255.0 * color.a;"
511 "return alpha < 0.5 ? color0"
512 ": alpha < 1.5 ? color1"
513 ": alpha < 2.5 ? color2"
514 ": alpha < 3.5 ? color3"
515 ": alpha < 4.5 ? color4 : color5;"
522 return sOverdrawEffect;