Skip to content

Use filtra for rendering filter layers - #1255

Merged
LaurenzV merged 5 commits into
mainfrom
filtra-filters
Nov 25, 2026
Merged

LaurenzV merged 5 commits into
mainfrom
filtra-filters

Conversation

@LaurenzV

@LaurenzV LaurenzV commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1185, fixes #1258, fixes #1153, fixes #1150, fixes #840, fixes #1152, fixes #1094.

Context

This PR proposes to move filter rendering in resvg to filtra, a crate I’ve been working on recently. The main motivation for this is that there is interest from other people to have direct SVG filter rendering capabilities, so moving that functionality to a separate crate allows us to reuse this logic. In particular, as can be seen in the numbers below, the crate is much much faster than resvg’s existing logic, and it also solves a number of currently open issues.

usvg now also stores filters using the types from filtra_types instead of its own, so the parsed filters can be passed to filtra directly. This is a breaking change to usvg's filter API: usvg::filter::Filter now dereferences to filtra_types::Filter<Group>, and usvg's own primitive types are gone (filtra_types is re-exported from usvg). filtra requires Rust 1.89, so the MSRV of usvg, resvg and the C API is raised from 1.85 to 1.89.

The only concern right now is binary size, which increases a bit, but honestly I don't think it's too bad given the speed boost we get.

Target Before After Change
macOS (aarch64) 1,394,608 B 1,477,264 B +81 KiB (+6%)
Windows (x86_64) 1,478,656 B 1,704,448 B +221 KiB (+15%)
wasm32 1,458,604 B 1,662,745 B +199 KiB (+14%)
wasm32 + simd128 1,355,321 B 1,469,901 B +112 KiB (+8%)

Benchmarks

As you can see, filter rendering is significantly faster now! Some images are still slow, but those are mostly due to tiny-skia being slow. The filter speedups themselves are huge.

macOS (Apple M1 Pro)

Filter Resolution Total, before Total, after (speedup) Filters only, before Filters only, after (speedup)
feGaussianBlur σ=10 512² 10.8 ms 3.01 ms (3.6×) 8.60 ms 0.81 ms (10.6×)
feGaussianBlur σ=10 2048² 195 ms 45.3 ms (4.3×) 162 ms 12.6 ms (12.9×)
feGaussianBlur σ=10 4096² 1175 ms 181 ms (6.5×) 1045 ms 51.4 ms (20.3×)
feDropShadow 4/4 σ=4 512² 16.5 ms 3.67 ms (4.5×) 14.3 ms 1.41 ms (10.1×)
feDropShadow 4/4 σ=4 2048² 287 ms 53.7 ms (5.3×) 254 ms 21.1 ms (12.1×)
feDropShadow 4/4 σ=4 4096² 1508 ms 215 ms (7.0×) 1379 ms 84.4 ms (16.3×)
feColorMatrix saturate 0.3 512² 7.18 ms 3.19 ms (2.3×) 4.92 ms 0.90 ms (5.5×)
feColorMatrix saturate 0.3 2048² 115 ms 47.5 ms (2.4×) 81.0 ms 14.5 ms (5.6×)
feColorMatrix saturate 0.3 4096² 439 ms 186 ms (2.4×) 310 ms 57.3 ms (5.4×)
feMorphology dilate r=3 512² 50.6 ms 3.07 ms (16.5×) 48.4 ms 0.88 ms (55.1×)
feMorphology dilate r=3 2048² 5775 ms 49.1 ms (117.7×) 5736 ms 16.0 ms (357.8×)
feMorphology dilate r=3 4096² 15463 ms 192 ms (80.3×) 15322 ms 62.4 ms (245.6×)
feTurbulence 4 oct. 512² 41.2 ms 7.64 ms (5.4×) 38.9 ms 5.42 ms (7.2×)
feTurbulence 4 oct. 2048² 652 ms 120 ms (5.5×) 619 ms 86.7 ms (7.1×)
feTurbulence 4 oct. 4096² 2602 ms 476 ms (5.5×) 2475 ms 346 ms (7.2×)
Bevel (blur+specular+composites) 512² 24.2 ms 5.29 ms (4.6×) 21.9 ms 3.03 ms (7.2×)
Bevel (blur+specular+composites) 2048² 404 ms 79.5 ms (5.1×) 372 ms 47.1 ms (7.9×)
Bevel (blur+specular+composites) 4096² 1996 ms 318 ms (6.3×) 1866 ms 189 ms (9.9×)

Windows (Intel Core i9-14900K)

Filter Resolution Total, before Total, after (speedup) Filters only, before Filters only, after (speedup)
feGaussianBlur σ=10 512² 23.0 ms 3.98 ms (5.8×) 20.7 ms 1.55 ms (13.4×)
feGaussianBlur σ=10 2048² 477 ms 58.2 ms (8.2×) 443 ms 23.8 ms (18.6×)
feGaussianBlur σ=10 4096² 2390 ms 236 ms (10.1×) 2252 ms 96.8 ms (23.3×)
feDropShadow 4/4 σ=4 512² 30.5 ms 5.53 ms (5.5×) 28.1 ms 2.96 ms (9.5×)
feDropShadow 4/4 σ=4 2048² 600 ms 77.8 ms (7.7×) 567 ms 43.4 ms (13.1×)
feDropShadow 4/4 σ=4 4096² 2879 ms 316 ms (9.1×) 2741 ms 177 ms (15.5×)
feColorMatrix saturate 0.3 512² 11.4 ms 3.70 ms (3.1×) 9.08 ms 1.34 ms (6.8×)
feColorMatrix saturate 0.3 2048² 178 ms 54.5 ms (3.3×) 144 ms 20.2 ms (7.1×)
feColorMatrix saturate 0.3 4096² 718 ms 221 ms (3.2×) 581 ms 81.8 ms (7.1×)
feMorphology dilate r=3 512² 40.8 ms 4.04 ms (10.1×) 38.4 ms 1.67 ms (23.0×)
feMorphology dilate r=3 2048² 8648 ms 63.1 ms (137.2×) 8615 ms 28.7 ms (300.1×)
feMorphology dilate r=3 4096² > 30 s 257 ms (>117×) > 30 s 118 ms (>254×)
feTurbulence 4 oct. 512² 79.2 ms 11.8 ms (6.7×) 76.8 ms 9.47 ms (8.1×)
feTurbulence 4 oct. 2048² 1258 ms 184 ms (6.8×) 1224 ms 150 ms (8.2×)
feTurbulence 4 oct. 4096² 5039 ms 721 ms (7.0×) 4902 ms 583 ms (8.4×)
Bevel (blur+specular+composites) 512² 53.4 ms 6.55 ms (8.2×) 51.1 ms 4.13 ms (12.4×)
Bevel (blur+specular+composites) 2048² 964 ms 98.7 ms (9.8×) 930 ms 64.3 ms (14.5×)
Bevel (blur+specular+composites) 4096² 4349 ms 401 ms (10.8×) 4208 ms 261 ms (16.1×)

@LaurenzV

Copy link
Copy Markdown
Collaborator Author

Here a summary of the updated tests:

  • Lighting: Surface slopes are now measured per user unit instead of per pixel, so lighting no longer depends on the render scale. The shading has more contrast and matches Chrome and Firefox.
    • feDiffuseLighting/: complex-transform, lighting-color=currentColor, lighting-color=hsla, lighting-color=inherit, lighting-color=seagreen, linearRGB-color-interpolation, multiple-light-sources, single-light-source, single-light-source-with-comment, single-light-source-with-desc, single-light-source-with-invalid-child, single-light-source-with-title, single-light-source-with-title-and-desc, surfaceScale=-10, surfaceScale=5
    • feSpecularLighting/: lighting-color=hsla, with-feDistantLight, with-fePointLight, with-feSpotLight, with-feSpotLight-and-specular-and-exponent, with-feSpotLight-and-specularConstant=5
    • feSpotLight/: complex-transform, custom-attributes
    • fePointLight/: complex-transform
  • feTurbulence stitching: Stitching now uses the primitive subregion (FE1 §9.21). The old code passed the current pixel as the tile's position and mixed pixel and user units. Matches all browsers.
    • feTurbulence/stitchTiles=stitch
  • Filter lists: A blur no longer falls off too early at the edge of the previous filter's result. Matches Chrome and Firefox.
    • filter-functions/: two-urls, two-exact-urls
  • feConvolveMatrix defaults: divisor="0" uses the default divisor, and an out-of-range target is centred, instead of rendering nothing. Matches Chrome and Firefox.
    • feConvolveMatrix/: divisor=0, targetX=3, targetX=-1
  • feConvolveMatrix pass-through: A missing or wrongly sized kernelMatrix, or an invalid order, now passes the input through, as FE1 §9.9 says. Browsers still render nothing, following SVG 1.1's error handling; FE1 marks this as an open issue (Should newlines be remove or converted into spaces? #237).
    • feConvolveMatrix/: no-kernelMatrix, empty-kernelMatrix, kernelMatrix-with-not-enough-values, kernelMatrix-with-too-many-values, order=0, order-with-a-negative-value-1, order-with-a-negative-value-2
  • feMorphology: A missing, zero or negative radius leaves the input unchanged (FE1 §9.17). That includes radius="10 0", where browsers dilate along one axis only.
    • feMorphology/: no-radius, empty-radius, zero-radius, negative-radius, radius-with-too-many-values, radius=10-0
  • Negative numOctaves: Uses the initial value 1 (FE1 §9.21) instead of rendering nothing.
    • feTurbulence/numOctaves=-1
  • saturate outside 0–1: Over- or under-saturates as FE1 describes. Matches Chrome and WebKit.
    • feColorMatrix/: type=saturate-with-a-large-coefficient, type=saturate-with-negative-coefficient
  • feSpecularLighting exponent outside 1–128: Used as given, because FE1 dropped SVG 1.1's range. Matches Chrome.
    • feSpecularLighting/: specularExponent=0, specularExponent=256
  • Negative feSpotLight exponent: The formula is applied as written, since neither spec gives it a range. Browsers clamp it to 1–128, a range SVG 1.1 only defined for feSpecularLighting.
    • feSpotLight/specularExponent=-10

@LaurenzV

Copy link
Copy Markdown
Collaborator Author

Here the comparison images for the ones that changed a lot:

resvg-filtra-reference-changes

@LaurenzV
LaurenzV marked this pull request as ready for review November 22, 2026 13:08
@LaurenzV
LaurenzV requested a review from nicoburns November 22, 2026 13:08
@nicoburns

nicoburns commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

@LaurenzV The increase in size for the non-WASM builds seems to come primarily from the inline(always) attributes. Both in this PR and fearless_simd's fallback.rs. Do you think we could disable those in both places for all(target_arch = "wasm32", not(target_feature = "simd128")) builds where there is no actually runtime SIMD detection anyway? It's also seems to be a slight speedup for wasm-without-simd filters (although on wasm-without-simd some of the existing filters (lighting, turbulence, color matrix) are actually faster).

(in general, perhaps you should do a pass on wasm-without-simd perf/size?)

I'm conflicted on how much emphasis to put on wasm-without-simd builds. On the one hand, there seems to be no little reason not to use simd on WASM in 2026. On the other hand, you have to opt-in with a cargo config / rustflag. So default builds won't have it enabled...

@LaurenzV

Copy link
Copy Markdown
Collaborator Author

Sure, I can take another closer look at WASM itself!

@nicoburns

Copy link
Copy Markdown
Collaborator

Timings I'm seeing. Run on wasm32-unknown-unknown in V8. I also did a run in wasmtime which makes the new no-simd filters looks much worse. But probably V8 is the more relevant target here.

Primitive main no-simd PR no-simd ratio main simd128 PR simd128 ratio
feTurbulence turbulence 4 oct 0.342 0.930 2.72× 0.312 0.160 0.51×
feTurbulence fractalNoise 4 oct 0.482 1.133 2.35× 0.447 0.299 0.67×
feColorMatrix saturate 0.121 0.159 1.32× 0.086 0.052 0.60×
feColorMatrix matrix 0.138 0.160 1.16× 0.099 0.052 0.53×
feColorMatrix hueRotate 0.124 0.158 1.28× 0.085 0.052 0.61×
feColorMatrix luminanceToAlpha 0.123 0.163 1.32× 0.083 0.053 0.64×
feComponentTransfer table 0.134 0.199 1.48× 0.099 0.052 0.52×
feComponentTransfer gamma 0.126 0.188 1.49× 0.096 0.054 0.56×
feGaussianBlur σ=6 0.156 0.084 0.54× 0.120 0.058 0.48×
feGaussianBlur σ=30 0.177 0.102 0.57× 0.150 0.083 0.56×
feDropShadow 0.231 0.171 0.74× 0.159 0.055 0.35×
feMorphology dilate r=3 0.203 0.086 0.42× 0.170 0.056 0.33×
feMorphology erode r=10 0.843 0.085 0.10× 0.789 0.056 0.07×
feConvolveMatrix 3×3 0.144 0.178 1.24× 0.110 0.057 0.52×
feConvolveMatrix 5×5 0.218 0.247 1.13× 0.151 0.067 0.45×
feDiffuseLighting distant 0.122 0.195 1.60× 0.077 0.048 0.62×
feDiffuseLighting point 0.144 0.245 1.69× 0.112 0.082 0.73×
feSpecularLighting distant 0.141 0.325 2.30× 0.104 0.055 0.53×
feSpecularLighting spot 0.171 0.493 2.88× 0.136 0.078 0.57×
feDisplacementMap 0.125 0.108 0.87× 0.091 0.052 0.58×
feComposite arithmetic 0.130 0.144 1.10× 0.090 0.053 0.60×
feComposite over 0.186 0.072 0.39× 0.109 0.050 0.46×
feComposite xor 0.190 0.074 0.39× 0.118 0.051 0.44×
feBlend multiply 0.181 0.074 0.41× 0.111 0.051 0.45×
feBlend color-dodge 0.188 0.180 0.96× 0.114 0.054 0.47×
feBlend hue 0.203 0.257 1.27× 0.119 0.055 0.46×
feFlood 0.099 0.069 0.70× 0.063 0.049 0.78×
feOffset 0.131 0.069 0.52× 0.083 0.050 0.60×
feMerge (3 nodes) 0.244 0.076 0.31× 0.144 0.049 0.34×
feTile 0.141 0.068 0.48× 0.083 0.048 0.58×
feImage (element ref) 0.118 0.081 0.69× 0.075 0.062 0.82×
empty filter (baseline) 0.016 0.016 0.99× 0.015 0.015 1.00×

@LaurenzV

Copy link
Copy Markdown
Collaborator Author

@nicoburns binary size should be back to normal now, and performance should also be better for that build mode now.

image

@nicoburns nicoburns left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better. The size increase is still a bit unfortunate, but it's a lot more reasonable now.

The fearless_simd changes also seems to have significantly sped up filter runtime of the WASM (no simd) builds (Oz). They're now consistently much faster than the old filters whereas previously some of them were regressions.

@LaurenzV
LaurenzV merged commit d950e16 into main Nov 25, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment