Ideas for rendering stacked UI elements

Started by
5 comments, last by Katie 7 years ago

Hello Everyone,

I've developed a basic theme able user interface system with opengl by drawing different shapes and text. I want the UI to be used on both mobile and desktop so I only want the UI to redraw when absolutely necessary in order to conserve battery life. So there is no rendering call in the main application loop. The time to paint can be at anytime a UI control believes it requires a redraw.

The issue that I'm having is that I can't figure out how to only draw the Control that needs redrawing. If the control that needs redrawing is semi transparent, the control behind it will need to be redrawn as well due to the fact that the redraw causes an opengl clear in the control's scissor rect, but if the control behind the control that needs redrawing is redrawn, all controls behind that and inside of that will need to be redrawn as well. To the point where I'm redrawing the entire screen every time something changes in one control. This has come to a head where I'm attempting to draw a textbox control and every time the caret blinks I'm redrawing all the controls on the screen, which doesn't seem very efficient.

Does anybody have any ideas or suggestions for ways in which I could draw semi transparent controls sitting on top of each other without redrawing the entire screen?

Thanks,

Advertisement

1. How sure are you that you're ACTUALLY being a power drain? Reason -- this is how all the rendering code on phones works and so the OS and GPU designers are really, really good at making them use very little power. Implement your basic version and use GPU debugging tools to see what workload you're actually generating..

2. Clip to the smallest changed area. Draw everything as you otherwise would. The clipping will cull out the areas that don't need rendering. Most mobile devices use tile-based renderings; the screen is rendered in squares, each of which accumulates its own processing list of primitive operations and they're rasterised/fragment processed independently. Draws which don't touch a lot of the tiles because they're clipped out incur no cost; the clipping ensures you're touching as few as possible. This is really all you should need to do. This only works for polygon clipping though, not stencil buffers. (You're after being able to discard geometry portions, not fragments).

3. There's a BUNCH of other "tricks" which *could* be used depending on exactly the drawing architecture you've got -- however, the last time I actually used any of them was on 8-bit micros where brute-force solutions like "redraw the whole screen" just weren't happening[1]. TBH, given that mid-range phones have **six to seven orders of magnitude** more processing power... you shouldn't need to be going there.

Seriously -- trust the machine developers, measure your impact and only optimise later.

Heck; if it comes to that --

4. Does your caret really need to blink? The cheapest geometry is geometry you don't draw at all...

[1] As an example, you grab a copy of the area under your caret. Now you can erase it to make it blink without needing a full redraw... Once you have a copy of the area without it and the screen buffer containing a version with the caret, you can just swap the memory areas over and save doing any drawing at all... See how strained that's all going to get?

No I'm not positive that I am actually being a power drain. what I'm doing is I'm checking the CPU usage through the task manager while I let the application run. In my mind, if all the app is doing is blinking a cursor, CPU usage should be 0%. (I checked visual studio, and with all its UI complexity, when it just sits and blinks a cursor, its CPU usage is 0%).

Granted, checking the task manager probably isn't the most scientific and rigorous way to test performance.

Your answer did get me thinking though, and I made the Text box request a repaint every time the cursor blinked. Even though the UI was being re rendered every time the cursor blinked, its was still only using 0% CPU as well. So yeah, maybe I am too concerned about performance and power usage.

The problem with clipping to the smallest change areas is if the controls are semi transparent. If a small textbox control sitting in the center of a Form is semi transparent, I need to render it and the background Form control as well in order to get the final blend colour between the 2 controls. If that Form is filling the entire screen, I have to re render the entire screen (along with all the other controls in the Form as well).

The only thing I can think of is to perhaps run though all the controls and their children, test if they intersect the rect being redrawn, and only redraw the controls (and their parent controls as well) if they intersect, but I wasn't sure if that was the most efficient way to do things or if I was actually wasting my time and should just rather do a brute force redraw every time instead.

Thanks


The problem with clipping to the smallest change areas is if the controls are semi transparent. If a small textbox control sitting in the center of a Form is semi transparent, I need to render it and the background Form control as well in order to get the final blend colour between the 2 controls. If that Form is filling the entire screen, I have to re render the entire screen (along with all the other controls in the Form as well).

Why do you think that? If you just draw that background inside the changed rectangle you do not overdraw anything outside it and therefore you do not need to redraw anything else outside it. (You either just change the draws to be only inside or use scissor.)

"only redraw the controls (and their parent controls as well) if they intersect"

The GL renderer is *already* doing that work (because it can't know you pre-clipped everything). Possibly it can even do it in hardware..

Just throw your geometry at it, let it sort it all out. Don't worry about it unless it's actually a problem.

Okay cool, so what I've begun to realize now is that I don't actually have to worry about sending opengl geometry outside the clip region, since it will deal with that itself (right?). What that has now done is shifted the problem from an being opengl one to something less specific and more general to graphics programming, something akin maybe to software culling perhaps?

The problem now is ensuring that controls that don't actually need to be rendered, don't send vertices through the pipeline in the first place, since opengl is going to discard them any way, and its costing CPU resources to build all those vertices. To that end, I'm no longer sure if this still the right place for the question.

But, given a control hierarchy, where each Paint() call runs through the hierarchy generating vertices for all controls and their children, is there a high performance solution that allows me to discard controls if they don't intersect the rect that needs redrawing? Right now, my only idea is to pass around the rect that will be redrawn and any control that doesn't intersect that rect doesn't get its vertices generated and sent to the GPU. My only concern is in situations where a tiny little square in the corner is being animated, but I now have to run through 300 other controls checking all of their rects every frame, seems a bit intensive, but at the same time, I'm guessing still cheaper than generating the vertices for those controls.

I hope I'm getting my concerns across properly?

Thanks,

The controls form a tree. A dialogue contains a couple of panes, the panes contain controls, the controls contain some static text and an entry box and so on on.

If the outer control completely contains the inner controls, it can check if the redraw is within its bounds and if not... doesn't propagate it to the contained controls.

This is how most real-world UI rendering systems operate -- boxes within boxes within boxes. It's common to Windows, MacOS, X11. It's happening inside web browsers and it's also the case for most mobile phone UIs. It's not the only solution, but it's a well worked one.

Most UIs with non-transparent elements can optimise further (because it's only the top-most opaque object which needs to paint). You'll have to exhaustively run through ALL the nodes and paint them in reverse stacking order.

The other solution is to use several drawing layers. You draw each UI elements to one as if opaque and composite the outputs together afterwards. This needs more memory and more GPU power but less CPU work.

This topic is closed to new replies.

Advertisement