# Faster Blur? || Glassmorphism UI

**URL:** <https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086>\
**Category:** General JUCE discussion\
**Created:** [December 5, 2020, 8:17pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086 "2020-12-05T20:17:40Z")\
**Posts on this page:** 20\
**Page:** 5

<div class="post-metadata">

**Author:** ![LukeM1](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@LukeM1](https://forum.juce.com/u/LukeM1)\
**Post date:** [November 1, 2023, 10:33am UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/82 "2023-11-01T10:33:35Z")

</div>

OK, so here is the latest version of our stack shadows:

> **[GitHub - Luke-Sonnox/shadows](https://github.com/Luke-Sonnox/shadows)**
>
> Contribute to Luke-Sonnox/shadows development by creating an account on GitHub.

The main differences between my previous link:

- Improved efficiency by rendering the shadow into a temporary `juce::Image`
- Improved inner shadow compatibility with various design tools
- Fixed some issues with the `spread` parameter

I think this in combination with the vectorisation performance improvements from @sudara are getting us pretty close to something usable!

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [November 1, 2023, 1:32pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/83 "2023-11-01T13:32:32Z")

</div>

Great! Thanks for sharing!

I’m dying to get my module out, but Intel IPP is currently letting me down. I’m seeing up to a 2x speedup when using the equivalent of Apple’s SepConvolve (IPP’s [FilterSeperable](https://www.intel.com/content/www/us/en/docs/ipp/developer-reference/2021-9/filterseparable.html)) but unfortunately performance degrades to be worse than baseline once radii go above 10.

I have a few other implementations I made while learning how to implement StackBlur. On Windows, the most promising is a row by row / col by col vector implementation which provides a consistent 4-5x speedup (at least on my AMD Ryzen 9), however that relies on my IPP vector library wrapper and I’d prefer to not add that as a dependency for the module, so I guess I’d need to rewrite that to be standalone for windows only.

I’ll do a bit more spelunking on IPP as well. There might be a better option there…

I will reiterate that caching shadows (holding a copy of the shadow’s juce::Image for the next paint call) is probably the #1 most performance friendly approach when working with a lot of shadows — so even if I can’t get my high-bar mythical 10x speedup for blurs/shadow on Windows, it will still make my module worth publishing/using on windows.

---

<div class="post-metadata">

**Author:** ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)\
**Post date:** [November 2, 2023, 11:48am UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/84 "2023-11-02T11:48:14Z")

</div>

If only we could guarantee Metal for MacOS, we could bypass all the pain and utilise simple shaders to do all these effects.

---

<div class="post-metadata">

**Author:** ![PeterRoos](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@PeterRoos](https://forum.juce.com/u/PeterRoos)\
**Post date:** [November 2, 2023, 12:03pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/85 "2023-11-02T12:03:00Z")

</div>

I think there are also developers making plugins for Windows

---

<div class="post-metadata">

**Author:** ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)\
**Post date:** [November 2, 2023, 4:10pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/86 "2023-11-02T16:10:01Z")

</div>

I’m primarily a Windows dev. But it would be nice to have hardware rendering enabled for all formats because I don’t feel like supporting OpenGL any more, because of Apple’s hatred of all things third party. Which is a shame as I love shader coding! But that’s another story…🙂

---

<div class="post-metadata">

**Author:** ![PeterRoos](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@PeterRoos](https://forum.juce.com/u/PeterRoos)\
**Post date:** [November 2, 2023, 4:35pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/87 "2023-11-02T16:35:32Z")

</div>

Lol, I misinterpreted your comment, I read it as coming from a Mac centered world view 🤭

---

<div class="post-metadata">

**Author:** ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)\
**Post date:** [November 2, 2023, 4:40pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/88 "2023-11-02T16:40:04Z")

</div>

Haha. Yeah man, that’s easily done on the Juce forum.

---

<div class="post-metadata">

**Author:** ![Stevezee](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stevezee](https://forum.juce.com/u/Stevezee)\
**Post date:** [November 3, 2023, 5:23am UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/89 "2023-11-03T05:23:40Z")

</div>

I just gave this a shot, works well!

I’m using it on an animated stroke path on my analyzer… there’s a bit of a slowdown but not too bad at all.

![ScreenRecorderProject98_2](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/e/0/e0ca47573100f7f07bc0baf3f87773f88f099741.gif)

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [November 10, 2023, 12:10pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/90 "2023-11-10T12:10:07Z")

</div>

Finally managed to get the blur module done! (cross-posting here for future readers)

> [@\[New Module\] Melatonin Blur](https://forum.juce.com/t/new-module-melatonin-blur/58756):
>
> Happy Friday, here’s a new JUCE module! I worked hard on this the last few weeks. I wrote and benchmarked a couple dozen Stack Blur and convolution implementations. Need to give up for now slight_smile Hopefully this will encourage others to make modern vector UIs in JUCE. It should let you use as many drop shadows and inner shadows you like without compromising painting performance: Let me know if you give it a try. I have a few ideas for how the API could be nicer, but would love feedba…

I also wrote quite a bit about how Stack Blur works and thinking through how to make it faster:

> **[Implementing Mario's Stack Blur 15 times in C++ (with tests and benchmarks) ·...](https://melatonin.dev/blog/implementing-marios-stack-blur-15-times-in-cpp/)**
>
> I questionably traded 6 weeks of dev time for screaming fast CPU drop shadows

---

<div class="post-metadata">

**Author:** ![Toddler-Boy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/toddler-boy/32/2007_2.png) [@Toddler-Boy](https://forum.juce.com/u/Toddler-Boy)\
**Post date:** [November 10, 2023, 2:45pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/91 "2023-11-10T14:45:27Z")

</div>

So IPP is only used for a few vector operations here? Like adding two vectors, multiply them, etc.? If that is the case, I’m sure we can come up with a native solution for x64 and Neon without much trouble to reduce dependencies on these extremely heavy libraries.

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [November 10, 2023, 3:13pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/92 "2023-11-10T15:13:51Z")

</div>

Yes, the [current IPP implementation](https://github.com/sudara/melatonin_blur/blob/main/melatonin/implementations/ipp_vector.h) just uses zero, copy, convert uint8 to float, add, subtract, divide, and their equivalent to `addWithMultiply`.

IPP isn’t required. The single channel fallback is a [FloatVectorOperations](https://github.com/sudara/melatonin_blur/blob/main/melatonin/implementations/float_vector_stack_blur.h) implementation (literally just replacing the calls to IPP). On my PC it performs slightly better than Gin, but the caching of course means it just as fast as any other implementation on repaint.

I’d be interested in a SIMD implementation, it would also be useful for pre-macOS 11.0 / iOS 14.0.

---

<div class="post-metadata">

**Author:** ![Toddler-Boy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/toddler-boy/32/2007_2.png) [@Toddler-Boy](https://forum.juce.com/u/Toddler-Boy)\
**Post date:** [November 10, 2023, 3:41pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/93 "2023-11-10T15:41:38Z")

</div>

We’ll get on that soon. One of our team (@reFXmkamradt) is quite the optimizer. He’ll probably enjoy it 😃

---

<div class="post-metadata">

**Author:** ![reFXmkamradt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refxmkamradt/32/5173_2.png) [@reFXmkamradt](https://forum.juce.com/u/reFXmkamradt)\
**Post date:** [November 12, 2023, 1:14pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/94 "2023-11-12T13:14:57Z")

</div>

🙂 I have just glanced over it but I fear it is not going to be so easy. If the “ipp\_vector” version is really fast and the “float\_vector\_stack\_blur” version is a lot slower, then this makes me wonder. JUCE’s FloatVectorOperations do use SIMD instructions and for such trivial tasks as “add” or “divide” it should not make too much of a difference.

One thing comes to mind that IPP does and that is to dynamically branch into different implementations, depending on your actual CPU. Maybe on your machine it uses AVX512 for its operations which would reduce loads, stores and register pressure. If that is what makes it a lot faster, we would also need to offer multiple implementations that are dynamically selected on the end-users machine.

I can try to set up a test case and look into it a bit further.

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [November 12, 2023, 5:26pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/95 "2023-11-12T17:26:56Z")

</div>

Thanks for taking a look!

Yes, that’s pretty much the same reasoning why I didn’t jump into trying to make SIMD version of that particular algorithm. That “flavor” of the vector algorithm is a bit strange, both in that it converts to floats but also that it subjects a whole row (or column) to a series of vector operations via the vendor libs.

A better fit for SIMD is probably going back to the “naive” version, doing the calculation for 8/16 pixels at once (u16) and using something like libdivide for division. I think there’s a SIMD implementation like this floating around the ether too…

But! Thanks to the power of the internet, someone on twitter with an anime profile pic contacted me with _exactly_ what I was looking for this whole time — [an alternate take on the actual math behind the algorithm](https://x.com/sasuke___420/status/1723301134390132899?s=20). I had the intuition that the math could be simplified (but not the skills to do it).

I’ve spent some time with it and looks realllly promising: we’d be able to go row by row, col by col, generating 2 sets of “prefix sums”, and then just produce the final pixel component with 2 additions, 1 subtraction and 1 division. It reduces branching, eliminates having to manage the “queue” and everything stays in very nice tight loops. It should also have the same stable performance profile as the radii changes that stack blur has — but hopefully blow it out of the water?

I’m cooking up a naive version of that version to see if it’ll pass correctness. I wasn’t totally sure if the guy from twitter was going to work on a SIMD version, but he sent this link over, which talks about how to parallelize the “prefix sums” (accumulating sums) [Prefix Sum with SIMD - Algorithmica](https://en.algorithmica.org/hpc/algorithms/prefix/) . I’ll ping you when i get a naive version passing tests if you are interested!

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [November 12, 2023, 5:34pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/96 "2023-11-12T17:34:51Z")

</div>

The other thing re: SIMD that’s nagging at me is the ARGB version of the algorithm. With StackBlur + caching, everything is pretty much happy “enough” outside of big animating dropshadows. But all ARGB algorithms I’ve tried sort of end up performing worse than 4x a single channel — and blurring something like a big ole 1024x1024 ARGB picture is just never reasonable.

I keep wondering if a good approach would be to somehow handle the 4-byte ARGB chunk at once, or how to treat the ARGB version differently than the single channel. Been flipping through this to try to find some inspiration: [Software optimization resources. C++ and assembly. Windows, Linux, BSD, Mac OS X](https://www.agner.org/optimize/#manuals)

---

<div class="post-metadata">

**Author:** ![reFXmkamradt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refxmkamradt/32/5173_2.png) [@reFXmkamradt](https://forum.juce.com/u/reFXmkamradt)\
**Post date:** [November 12, 2023, 5:39pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/97 "2023-11-12T17:39:55Z")

</div>

OK, let me know as soon as you have tried the new approach. I set up a test case (single channel, 512x512) and checked “ipp” vs “juceFloatVector” vs “gin” and I really did not see such big improvements over gin’s implementation as mentioned in your benchmarks. Given “ipp” is the fastest, “gin” is about 1.5x slower and the “juce” version is about 1.7x slower and produced buggy results.

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [November 12, 2023, 5:56pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/98 "2023-11-12T17:56:31Z")

</div>

Interesting, was it similar for you across radii? Even though the FloatVector implementation probably won’t be in use long, I’d still be interested in how it broke (trying to make sure the tests catch all edge cases)

---

<div class="post-metadata">

**Author:** ![reFXmkamradt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refxmkamradt/32/5173_2.png) [@reFXmkamradt](https://forum.juce.com/u/reFXmkamradt)\
**Post date:** [November 13, 2023, 8:51am UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/99 "2023-11-13T08:51:13Z")

</div>

I tested for radius 5 and radius 25. The results were very similar.

I think the output is broken due to an edge case when the outermost pixels are set. This is what it looks like:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/3X/3/c/3c5d27a7ed664d5f4b03901094e63b66271a80c3.png)

This is the code to create the image:

```auto
	m_image = juce::Image ( juce::Image::PixelFormat::SingleChannel, 512, 512, true );
	juce::Graphics g ( m_image );
	g.setColour ( juce::Colours::black );
	g.drawRect ( 0, 0, 512, 512 );
 	melatonin::blur::juceFloatVectorSingleChannel ( m_image, 5 );

```

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [November 17, 2023, 9:25pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/100 "2023-11-17T21:25:36Z")

</div>

Whew, back from ADC!

@reFXmkamradt Thanks for the details on what turned out to be the most literal of edge cases, it should now be fixed with tests [here](https://github.com/sudara/melatonin_blur/pull/25)

Re: benchmarks, I opened an issue [here](https://github.com/sudara/melatonin_blur/issues/22) if you’d be willing to give some detail on your CPU as well as as how you benchmarked (what framework or method of timing, if you included setting up a source image and juce::graphics context like in the benchmark execution, etc.)

Re: new algo, [I’ve got a rough draft of the horizontal pass](https://github.com/sudara/melatonin_blur/pull/26/files#diff-f71d9742da2822e4e9f4dca748de6661b9943cb221e56e59b8392d774bd305f0R88-R157) passing tests with gin-similar performance. I think I’m going to try out a variable divisor (as a replacement for padding the edges with the radius), but after that I think it would be best to address SIMD needs vs. optimize it more linearly. I’ll DM you next week, the algo is very straightforward (create 2 accumulating sums, do two adds and a subtraction, 1 divide (or mul/shift)).

---

<div class="post-metadata">

**Author:** ![sudara](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/sudara/32/8071_2.png) [@sudara](https://forum.juce.com/u/sudara)\
**Post date:** [November 22, 2023, 1:10pm UTC](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086/101 "2023-11-22T13:10:27Z")

</div>

Spent another day or two and my conclusion is that the [prefix sum algorithm](https://github.com/sudara/melatonin_blur/pull/26)’s main advantage is simplicity — it’s easier to SIMD-ify than traditional stack blur as there’s just less logic. It would be a better “fallback” for Melatonin Blur for that reason. It still can’t outperform vImage on larger images (not sure about IPP yet), I have no idea what magic they are up to there!

Initial tests on NEON/SSE2 show the prefix sum horizontal pass roughly ~2x faster than stack blur. The bottleneck (especially on larger images) becomes reading/writing to pixels across image columns for the vertical pass. I’ve started working on a strategy of rotating the image before the vertical pass (so it can be another horizontal pass reading/writing from contiguous memory).

But for now I will take a break and get back to my actual UI. @reFXmkamradt, I DM’d details, feel free to dig in if you still want to. I’m a SSE noob, and an AVX implementation would be great (SSE is doing 4 uint8\_t prefix sums at once, it’s the main source of cpu time).

[Previous page](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086.md?page=4)

[Next page](https://forum.juce.com/t/faster-blur-glassmorphism-ui/43086.md?page=6)
