Combining Metal rendering with iOS blur effect

Hello All


I'd like to get following effect in my application (iOS/macOS):

- First I'd like to have Metal-rendered background image

- Second, (preferably) standard UI blur effect on top of that

- And finally, at the very top, again something drawn with Metal.


I guess I could achieve that effect mimicking UI blur effect and just drawing everything in Metal. On iOS, I could use Metal Performance Shaders (Gaussian blur) to achieve effect similar to UI blur. I am hovewer concerned about future OS versions (what if blur style changes?), about performance (doubt that my implementation, especially on macOS without performance shaders, will be as fast as iOS's), and so on.


I can think of another idea: stacking NSViews/UIViews as follows:

- First one would be my standad View using Metal to display itself (View A)

- Second would be UIVisualEffectView or whatever one uses to achieve "system" blur effect in OS (View B)

- And third one (View C) would be yet another VIew using Metal for rendering, using apropriate alpha blending to make underlying View A blurred through View B visible. Rendering in this view will be pretty lightweight. A bit of overlapping symbols, mostly just "see through".


Will it work? If so, do I understand correctly that Views A and C should perform separate rendering, each one getting drawable for himself and presenting it? Will it hit my performance hard? How can I prevent it?


Thanks in advance

Michal

Answered by wcm in 177172022

Yes, if you stop drawing to an MTKView or CAMetalLayer, it will continue displaying the contents of the most recently-presented drawable.


You don't want to hold onto a drawable after the end of a frame for the sake of reusing its texture. Instead, you should render the scene into a separate offscreen texture (or blit from the drawable's texture to another texture) and use that texture for your accumulation buffer-style effects.

There's nothing fundamental preventing the second approach from working. Metal content is composited together just like any other content. Each view will render and present its own drawable. However, this will be quite demanding in terms of power and memory, and depending on how you do the blur, it may be difficult to maintain 60 FPS, especially on older iOS devices. Keeping all of the rendering in a single view gives you more control over how to apply the desired blur and/or vibrancy effect, though potentially at the expense of having to roll your own shaders. It seems like the surest approach would be to prototype the second solution and see how it performs, falling back onto a custom, single-view solution if it doesn't meet your performance requirements on your target systems.

Thanks for help, I'll give it a try. IPad pro is "base platform" for me, and I do not need 60FPS during what is off-screen prolonged Undo/Redo action anyway. I just want anmation on top to be smooth enough for peasant user experience. I can always get that by splitting work to be done ito smaller slices per-frame.


Thanks again

Michal

Forgive me double posting, but I have yet another question: what will happen if I stop rendering to bottom Metal view (view C)? Will last drawable content stay, and therefore, will overlying UIVisualEffectView retain last blurred "version" and stop from costly processing over and over again? Moreover, after awhile can I write upon last drawable's content by setting load action to "load", and some in-beetween alphas? Kinda like accumulation buffer in GL? I know, I can check if it works and I will, but I'd like to know if its good practive or not?


regards

michal

Accepted Answer

Yes, if you stop drawing to an MTKView or CAMetalLayer, it will continue displaying the contents of the most recently-presented drawable.


You don't want to hold onto a drawable after the end of a frame for the sake of reusing its texture. Instead, you should render the scene into a separate offscreen texture (or blit from the drawable's texture to another texture) and use that texture for your accumulation buffer-style effects.

Thanks. Yeah, holding to drawable idea was stupid of me :-)

Regards

Michal

Combining Metal rendering with iOS blur effect
 
 
Q