Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkKnownRuntimeEffects.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2024 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
13
15
16namespace {
17
18// This must be kept in sync w/ the version in BlurUtils.h
19static constexpr int kMaxBlurSamples = 28;
20
21SkRuntimeEffect* make_blur_1D_effect(int kernelWidth, const SkRuntimeEffect::Options& options) {
22 SkASSERT(kernelWidth <= kMaxBlurSamples);
23 // The SkSL structure performs two kernel taps; if the kernel has an odd width the last
24 // sample will be skipped with the current loop limit calculation.
25 SkASSERT(kernelWidth % 2 == 0);
28 // The coefficients are always stored for the max radius to keep the
29 // uniform block consistent across all effects.
30 "const int kMaxUniformKernelSize = %d / 2;"
31 // But we generate an exact loop over the kernel size. Note that this
32 // program can be used for kernels smaller than the constructed max as long
33 // as the kernel weights for excess entries are set to 0.
34 "const int kMaxLoopLimit = %d / 2;"
35
36 "uniform half4 offsetsAndKernel[kMaxUniformKernelSize];"
37 "uniform half2 dir;"
38
39 "uniform shader child;"
40
41 "half4 main(float2 coord) {"
42 "half4 sum = half4(0);"
43 "for (int i = 0; i < kMaxLoopLimit; ++i) {"
44 "half4 s = offsetsAndKernel[i];"
45 "sum += s.y * child.eval(coord + s.x*dir);"
46 "sum += s.w * child.eval(coord + s.z*dir);"
47 "}"
48 "return sum;"
49 "}", kMaxBlurSamples, kernelWidth).c_str(),
50 options);
51}
52
53SkRuntimeEffect* make_blur_2D_effect(int maxKernelSize, const SkRuntimeEffect::Options& options) {
54 SkASSERT(maxKernelSize % 4 == 0);
57 // The coefficients are always stored for the max radius to keep the
58 // uniform block consistent across all effects.
59 "const int kMaxUniformKernelSize = %d / 4;"
60 "const int kMaxUniformOffsetsSize = 2*kMaxUniformKernelSize;"
61 // But we generate an exact loop over the kernel size. Note that this
62 // program can be used for kernels smaller than the constructed max as long
63 // as the kernel weights for excess entries are set to 0.
64 "const int kMaxLoopLimit = %d / 4;"
65
66 // Pack scalar coefficients into half4 for better packing on std140, and
67 // upload offsets to avoid having to transform the 1D index into a 2D coord
68 "uniform half4 kernel[kMaxUniformKernelSize];"
69 "uniform half4 offsets[kMaxUniformOffsetsSize];"
70
71 "uniform shader child;"
72
73 "half4 main(float2 coord) {"
74 "half4 sum = half4(0);"
75
76 "for (int i = 0; i < kMaxLoopLimit; ++i) {"
77 "half4 k = kernel[i];"
78 "half4 o = offsets[2*i];"
79 "sum += k.x * child.eval(coord + o.xy);"
80 "sum += k.y * child.eval(coord + o.zw);"
81 "o = offsets[2*i + 1];"
82 "sum += k.z * child.eval(coord + o.xy);"
83 "sum += k.w * child.eval(coord + o.zw);"
84 "}"
85 "return sum;"
86 "}", kMaxBlurSamples, maxKernelSize).c_str(),
87 options);
88}
89
90} // anonymous namespace
91
94 SkRuntimeEffectPriv::SetStableKey(&options, static_cast<uint32_t>(stableKey));
95
96 switch (stableKey) {
97 case StableKey::kInvalid:
98 return nullptr;
99
100 // Shaders
101 case StableKey::k1DBlur4: {
102 static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_effect(4, options);
103 return s1DBlurEffect;
104 }
105 case StableKey::k1DBlur8: {
106 static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_effect(8, options);
107 return s1DBlurEffect;
108 }
109 case StableKey::k1DBlur12: {
110 static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_effect(12, options);
111 return s1DBlurEffect;
112 }
113 case StableKey::k1DBlur16: {
114 static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_effect(16, options);
115 return s1DBlurEffect;
116 }
117 case StableKey::k1DBlur20: {
118 static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_effect(20, options);
119 return s1DBlurEffect;
120 }
121 case StableKey::k1DBlur28: {
122 static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_effect(28, options);
123 return s1DBlurEffect;
124 }
125 case StableKey::k2DBlur4: {
126 static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_effect(4, options);
127 return s2DBlurEffect;
128 }
129 case StableKey::k2DBlur8: {
130 static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_effect(8, options);
131 return s2DBlurEffect;
132 }
133 case StableKey::k2DBlur12: {
134 static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_effect(12, options);
135 return s2DBlurEffect;
136 }
137 case StableKey::k2DBlur16: {
138 static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_effect(16, options);
139 return s2DBlurEffect;
140 }
141 case StableKey::k2DBlur20: {
142 static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_effect(20, options);
143 return s2DBlurEffect;
144 }
145 case StableKey::k2DBlur28: {
146 static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_effect(28, options);
147 return s2DBlurEffect;
148 }
149 case StableKey::kBlend: {
150 static constexpr char kBlendShaderCode[] =
151 "uniform shader s, d;"
152 "uniform blender b;"
153 "half4 main(float2 xy) {"
154 "return b.eval(s.eval(xy), d.eval(xy));"
155 "}";
156
157 static const SkRuntimeEffect* sBlendEffect =
159 kBlendShaderCode,
160 options);
161 return sBlendEffect;
162 }
163 case StableKey::kDecal: {
164 static constexpr char kDecalShaderCode[] =
165 "uniform shader image;"
166 "uniform float4 decalBounds;"
167
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);"
172 "}";
173
174 static const SkRuntimeEffect* sDecalEffect =
176 kDecalShaderCode,
177 options);
178 return sDecalEffect;
179 }
180 case StableKey::kDisplacement: {
181 // NOTE: This uses dot product selection to work on all GLES2 hardware (enforced by
182 // public runtime effect restrictions). Otherwise, this would use a "uniform ivec2"
183 // and component indexing to convert the displacement color into a vector.
184 static constexpr char kDisplacementShaderCode[] =
185 "uniform shader displMap;"
186 "uniform shader colorMap;"
187 "uniform half2 scale;"
188 "uniform half4 xSelect;" // Only one of RGBA will be 1, the rest are 0
189 "uniform half4 ySelect;"
190
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);"
196 "}";
197
198 static const SkRuntimeEffect* sDisplacementEffect =
200 kDisplacementShaderCode,
201 options);
202 return sDisplacementEffect;
203 }
204 case StableKey::kLighting: {
205 static constexpr char kLightingShaderCode[] =
206 "const half kConeAAThreshold = 0.016;"
207 "const half kConeScale = 1.0 / kConeAAThreshold;"
208
209 "uniform shader normalMap;"
210
211 // Packs surface depth, shininess, material type (0 == diffuse) and light type
212 // (< 0 = distant, 0 = point, > 0 = spot)
213 "uniform half4 materialAndLightType;"
214
215 "uniform half4 lightPosAndSpotFalloff;" // (x,y,z) are lightPos, w is spot falloff
216 // exponent
217 "uniform half4 lightDirAndSpotCutoff;" // (x,y,z) are lightDir,
218 // w is spot cos(cutoffAngle)
219 "uniform half3 lightColor;" // Material's k has already been multiplied in
220
221 "half3 surface_to_light(half3 coord) {"
222 "if (materialAndLightType.w < 0) {"
223 "return lightDirAndSpotCutoff.xyz;"
224 "} else {"
225 // Spot and point have the same equation
226 "return normalize(lightPosAndSpotFalloff.xyz - coord);"
227 "}"
228 "}"
229
230 "half spotlight_scale(half3 surfaceToLight) {"
231 "half cosCutoffAngle = lightDirAndSpotCutoff.w;"
232 "half cosAngle = -dot(surfaceToLight, lightDirAndSpotCutoff.xyz);"
233 "if (cosAngle < cosCutoffAngle) {"
234 "return 0.0;"
235 "}"
236 "half scale = pow(cosAngle, lightPosAndSpotFalloff.w);"
237 "if (cosAngle < cosCutoffAngle + kConeAAThreshold) {"
238 "return scale * (cosAngle - cosCutoffAngle) * kConeScale;"
239 "} else {"
240 "return scale;"
241 "}"
242 "}"
243
244 "half4 compute_lighting(half3 normal, half3 surfaceToLight) {"
245 // Point and distant light color contributions are constant
246 "half3 color = lightColor;"
247 // Spotlights fade based on the angle away from its direction
248 "if (materialAndLightType.w > 0) {"
249 "color *= spotlight_scale(surfaceToLight);"
250 "}"
251
252 // Diffuse and specular reflections scale the light's "color" differently
253 "if (materialAndLightType.z == 0) {"
254 "half coeff = dot(normal, surfaceToLight);"
255 "color = saturate(coeff * color);"
256 "return half4(color, 1.0);"
257 "} else {"
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));"
263 "}"
264 "}"
265
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);"
272 "}";
273
274 static const SkRuntimeEffect* sLightingEffect =
276 kLightingShaderCode,
277 options);
278 return sLightingEffect;
279 }
280 case StableKey::kLinearMorphology: {
281 static constexpr char kLinearMorphologyShaderCode[] =
282 // KEEP IN SYNC WITH SkMorphologyImageFilter.cpp DEFINITION
283 "const int kMaxLinearRadius = 14;"
284
285 "uniform shader child;"
286 "uniform half2 offset;"
287 "uniform half flip;" // -1 converts the max() calls to min()
288 "uniform int radius;"
289
290 "half4 main(float2 coord) {"
291 "half4 aggregate = flip*child.eval(coord);" // case 0 only samples once
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)));"
297 "}"
298 "return flip*aggregate;"
299 "}";
300
301 static const SkRuntimeEffect* sLinearMorphologyEffect =
303 kLinearMorphologyShaderCode,
304 options);
305 return sLinearMorphologyEffect;
306 }
307
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;"
314
315 "half4 main(float2 coord) {"
316 "float2 zoomCoord = zoomXform.xy + zoomXform.zw*coord;"
317 // edgeInset is the smallest distance to the lens bounds edges,
318 // in units of "insets".
319 "float2 edgeInset = min(coord - lensBounds.xy, lensBounds.zw - coord) *"
320 "invInset;"
321
322 // The equations for 'weight' ensure that it is 0 along the outside of
323 // lensBounds so it seams with any un-zoomed, un-filtered content. The zoomed
324 // content fills a rounded rectangle that is 1 "inset" in from lensBounds with
325 // circular corners with radii equal to the inset distance. Outside of this
326 // region, there is a non-linear weighting to compress the un-zoomed content
327 // to the zoomed content. The critical zone about each corner is limited
328 // to 2x"inset" square.
329 "float weight = (edgeInset.x < 2.0 && edgeInset.y < 2.0)"
330 // Circular distortion weighted by distance to inset corner
331 "? (2.0 - length(2.0 - edgeInset))"
332 // Linear zoom, or single-axis compression outside of the inset
333 // area (if delta < 1)
334 ": min(edgeInset.x, edgeInset.y);"
335
336 // Saturate before squaring so that negative weights are clamped to 0
337 // before squaring
338 "weight = saturate(weight);"
339 "return src.eval(mix(coord, zoomCoord, weight*weight));"
340 "}";
341
342 static const SkRuntimeEffect* sMagnifierEffect =
344 kMagnifierShaderCode,
345 options);
346 return sMagnifierEffect;
347 }
348 case StableKey::kNormal: {
349 static constexpr char kNormalShaderCode[] =
350 "uniform shader alphaMap;"
351 "uniform float4 edgeBounds;"
352 "uniform half negSurfaceDepth;"
353
354 "half3 normal(half3 alphaC0, half3 alphaC1, half3 alphaC2) {"
355 // The right column (or bottom row) terms of the Sobel filter. The left/top is
356 // just the negative, and the middle row/column is all 0s so those instructions
357 // are skipped.
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));"
364 "}"
365
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);"
379
380 "half mainAlpha = alphaC1.y;" // offset = (0,0)
381 "return half4(normal(alphaC0, alphaC1, alphaC2), mainAlpha);"
382 "}";
383
384 static const SkRuntimeEffect* sNormalEffect =
386 kNormalShaderCode,
387 options);
388 return sNormalEffect;
389 }
390 case StableKey::kSparseMorphology: {
391 static constexpr char kSparseMorphologyShaderCode[] =
392 "uniform shader child;"
393 "uniform half2 offset;"
394 "uniform half flip;"
395
396 "half4 main(float2 coord) {"
397 "half4 aggregate = max(flip*child.eval(coord + offset),"
398 "flip*child.eval(coord - offset));"
399 "return flip*aggregate;"
400 "}";
401
402 static const SkRuntimeEffect* sSparseMorphologyEffect =
404 kSparseMorphologyShaderCode,
405 options);
406 return sSparseMorphologyEffect;
407 }
408
409 // Blenders
410 case StableKey::kArithmetic: {
411 static constexpr char kArithmeticBlenderCode[] =
412 "uniform half4 k;"
413 "uniform half pmClamp;"
414
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));"
418 "return c;"
419 "}";
420
421 static const SkRuntimeEffect* sArithmeticEffect =
423 kArithmeticBlenderCode,
424 options);
425 return sArithmeticEffect;
426 }
427
428 // Color Filters
429 case StableKey::kHighContrast: {
430 static constexpr char kHighContrastFilterCode[] =
431 "uniform half grayscale, invertStyle, contrast;"
432
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),"
436 "d = mx-mn,"
437 "invd = 1.0 / d,"
438 "g_lt_b = c.g < c.b ? 6.0 : 0.0;"
439
440 // We'd prefer to write these tests like `mx == c.r`, but on some GPUs max(x,y) is
441 // not always equal to either x or y. So we use long form, c.r >= c.g && c.r >= c.b.
442 "half h = (1/6.0) * (mx == mn" "? 0.0 :"
443 /*mx==c.r*/ "c.r >= c.g && c.r >= c.b ? invd * (c.g - c.b) + g_lt_b :"
444 /*mx==c.g*/ "c.g >= c.b" "? invd * (c.b - c.r) + 2.0"
445 /*mx==c.b*/ ": invd * (c.r - c.g) + 4.0);"
446 "half sum = mx+mn,"
447 "l = sum * 0.5,"
448 "s = mx == mn ? 0.0"
449 ": d / (l > 0.5 ? 2.0 - sum : sum);"
450 "return half3(h,s,l);"
451 "}"
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;"
456 "}"
457 "if (invertStyle == 1) {" // brightness
458 "c = 1 - c;"
459 "} else if (invertStyle == 2) {" // lightness
460 "c = rgb_to_hsl(c);"
461 "c.b = 1 - c.b;"
462 "c = $hsl_to_rgb(c);"
463 "}"
464 "c = mix(half3(0.5), c, contrast);"
465 "return half4(saturate(c), inColor.a);"
466 "}";
467
468 static const SkRuntimeEffect* sHighContrastEffect =
470 kHighContrastFilterCode,
471 options);
472 return sHighContrastEffect;
473 }
474
475 case StableKey::kLerp: {
476 static constexpr char kLerpFilterCode[] =
477 "uniform colorFilter cf0;"
478 "uniform colorFilter cf1;"
479 "uniform half weight;"
480
481 "half4 main(half4 color) {"
482 "return mix(cf0.eval(color), cf1.eval(color), weight);"
483 "}";
484
485 static const SkRuntimeEffect* sLerpEffect =
487 kLerpFilterCode,
488 options);
489 return sLerpEffect;
490 }
491
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;"
496 "}";
497
498 static const SkRuntimeEffect* sLumaEffect =
500 kLumaFilterCode,
501 options);
502 return sLumaEffect;
503 }
504
505 case StableKey::kOverdraw: {
506 static constexpr char kOverdrawFilterCode[] =
507 "uniform half4 color0, color1, color2, color3, color4, color5;"
508
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;"
516 "}";
517
518 static const SkRuntimeEffect* sOverdrawEffect =
520 kOverdrawFilterCode,
521 options);
522 return sOverdrawEffect;
523 }
524 }
525
527}
528
529} // namespace SkKnownRuntimeEffects
const char * options
#define SkUNREACHABLE
Definition SkAssert.h:135
#define SkASSERT(cond)
Definition SkAssert.h:116
SkRuntimeEffect * SkMakeRuntimeEffect(SkRuntimeEffect::Result(*make)(SkString, const SkRuntimeEffect::Options &), const char *sksl, SkRuntimeEffect::Options options=SkRuntimeEffect::Options{})
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
static void SetStableKey(SkRuntimeEffect::Options *options, uint32_t stableKey)
static Result MakeForColorFilter(SkString sksl, const Options &)
static Result MakeForBlender(SkString sksl, const Options &)
static Result MakeForShader(SkString sksl, const Options &)
const SkRuntimeEffect * GetKnownRuntimeEffect(StableKey stableKey)