javabufferstrategybufferedstrategy

When using createBufferStrategy() in java, does it help to have more than 2 buffers? Is there a downside?


It seems like most people recommend just using 2 or 3. Is that just because more than 3 takes up too much processing power or something (forgive me I'm kind of new to this)? In what kind of programs would you use more than 3 buffers?

2 or 3 works fine for my program, I'm just curious.


Solution

  • Actually its pretty easy to understand once you know the benefits of buffer strategies. Let's take a brief look at what happens in all three cases.


    In single buffering you have only one display to write image data to. In contrast double buffering has two displays, the front and back buffer.

    Commonly the rendering and logical process of an application are split and run in parallel (for example the monitor and the graphic card). Lets say the rendering process has a poll rate that displays an image every 15ms on the monitor. Imagine the logical process is currently performing some image manipulations (drawing a circle) but is not finished at the moment (the circle is only drawn half). In single buffering you will now see a half circle on the screen as the rendering process displays an unfinished image.

    In double buffering the logical process will only write to the back buffer and only if it has ended the drawing process it will mark the back buffer as finished. Then you swap the contents of the back buffer with the front buffer, the rendering process will now display a finished image, you will not see any artifacts.

    Illustration of double buffering

    So the advantage of double buffering is that the user will not see any artefacts nor experience stuff like flickering or so. However this comes at a cost of increased running time (swapping operation) and especially an increased cost of space (2x the whole image).


    Now while tripple buffering comes at even more cost (3x image space) it will speed up the process. Here you have two back buffer and one front buffer.

    Imagine you use double buffering and currently you are swapping the back buffer to the front buffer as you have just finished a drawing operation. This can take some time and meanwhile your graphic card (which is very fast - faster than your software code that exchanges the buffers) could begin with the next drawing operation but it can't as the buffer is blocked.

    With triple buffering the graphic card could now just start drawing to the other buffer as one back buffer is always free and not involved in any swapping mechanism.


    Now that we know how things work it is also clear why you do not see buffer solutions with more than 3 buffers (at least not in common applications) - it simply has no benefit from the view of the general concept.

    Increased amounts of buffers can be seen when dealing with 3D virtual reality stuff (stereoscopic images) for example, you could use double buffering for the left and for the right channel respectively ending with quad buffering in total.

    As a last side note vsync means synchronizing the swapping of back and front buffer with the poll rate of your monitor to minimize tearing effects.