5
\$\begingroup\$

Just a quick question about fine-tuning/optimization. If I'm making a 2D platformer with a parallax-scrolling background (meaning no two frames are the same) do I really need to clear my renderer for every iteration? The entire scene will be drawn-over anyways, so if I can get rid of one extra function every iteration, it could help to speed things up a little bit, though I realize that render-clearing is not a very expensive function. By the way, I'm using SDL2.

\$\endgroup\$
  • \$\begingroup\$ No, you don't. As long as you draw to every pixel, every frame, there's really no need to clear the colour buffer as far as I'm aware. \$\endgroup\$ – Ben Apr 13 '14 at 18:39
  • 2
    \$\begingroup\$ Clearing the framebuffer is such an optimized task, skipping it is really unlikely to give you any kind of measurable gains. In fact, in some cases not clearing the framebuffer between frames can actually be slower. \$\endgroup\$ – bcrist Apr 13 '14 at 18:56
  • \$\begingroup\$ Check out Dirty Rectangles. Basically you re-render the space where the object was 1 frame ago and outside that space, becouse nothing produced a movement there. \$\endgroup\$ – dragons Apr 13 '14 at 19:38
  • \$\begingroup\$ As mentioned, hardware is extremely optimized to clear buffers and if you're using a depth buffer at all it's a step you already have to take to at least that extent. \$\endgroup\$ – Patrick Hughes Apr 13 '14 at 20:32
  • 1
    \$\begingroup\$ On tiled mobile GPUs (all iOS devices) you should clear even if the whole screen will be overdrawn. \$\endgroup\$ – SurvivalMachine Apr 14 '14 at 6:53
9
\$\begingroup\$

On tile based GPUs (commonly seen in mobile hardware) it is beneficial to clear the renderer, as it allows the GPU to dispose of the existing cached frame data.

Here's a quote from OpenGL Insights, dealing with the benefits of clearing the framebuffer on a tile based renderer:

"On a tile-based architecture, avoiding clears can be disasterous for performance: since the frame is built up in frame data, clearing all buffers will simply free up the existing frame data. In other words, not only is glClear very cheap, it actually improves performance by allowing unneeded frame data to be discarded."

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

No you don't need to.

As you said yourself, if you whole scene is drawn-over by the new rendered frame, then it makes no visual difference.

And as comments rightfully mentionned, it might have a positive or a negative impact on performance: profile on your target platform(s) to make your decision.

In the old days, it was common place to actually re-render only the parts of the frame buffer that required a change and keep the "old pixels" around to avoid costly clears. But today this is usually no longer a bottleneck.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.