10

In ping pong buffer we have two buffers, when one is being written by upstream component, the other is being read by the downstream component. When writing into one buffer is completed, the downstream component will switch to reading that as the other buffer is filled by the upstream component. This appears similar to how double buffering works.

Are ping-pong buffer and double buffer, two terms for the same thing?

quantum231
  • 11,218
  • 24
  • 99
  • 192

5 Answers5

14

No, they are not the same. Both are 'double buffering' in that they involve more than one buffer. However, ping-pong is a specific type, and the phrase 'double buffering' is usually (though not always) reserved for the not ping-pong type.

In ping-pong buffering, there are two buffers, either of which can be used for output. While one provides output, the other can be filled asynchronously. The buffers are then switched over when required. The essence of ping-pong buffering is that the output goes back and forth between the two buffers, just like a ping-pong ball goes back and forth, between the halves of the table.

Typically ping-pong buffering is used for video memory, especially when the memory is shared with the system. We have a large amount of information, and already have I/O addressing support for a simple and rapid switch of address spaces.

In double buffering, there is a first buffer that always receives the input, then a second buffer dedicated to driving the output, and a signal to transfer from one to the other.

Examples of double buffering are found in the HC595 shift register, and the MAX534 quad DAC - for the ability to receive and store the programmed word without changing the actual output until later. We have a small amount of information, and easy to connect memory, aka registers.

Neil_UK
  • 158,152
  • 3
  • 173
  • 387
  • 3
    Your definition of double-buffer sounds a lot like a shadow register. – DKNguyen Nov 17 '21 at 14:28
  • 1
    While a shadow register could be a double buffer, a double buffer is not necessarily a shadow register. Normally a shadow register is a local copy of a peripheral's register, useful if readback is not available – johnnymopo Nov 17 '21 at 15:37
  • I don't think the term double-buffering implies involves always writing to one buffer and then copying it to the other - eg in this [description of it on the Amiga](http://amigadev.elowar.com/read/ADCD_2.1/Libraries_Manual_guide/node00F7.html) it's clear that while at any one time, one buffer is used for updating and the other for hardware, both buffers perform both roles. – psmears Nov 18 '21 at 13:31
  • I would call that a ping-pong buffer. – SteveSh Nov 18 '21 at 20:54
  • For the double-buffer, I don't understand the purpose of copying the entire "write" buffer over to the "read" buffer. Are there any further examples or resources on this? Why not render the new buffer when it's ready? The extra step of copying over to a "read" buffer seems completely unnecessary. Still makes no sense to me. – mrbean Jan 20 '23 at 06:03
  • 1
    @mrbean They tend to be used for different things. Pingpong is often used for video buffers, when there's a large amount of information, and the addressing support. Double buffering tends to be used in multi channels DACs, where the DACs are programmed sequenentially, but all need to change their outputs at once, small amount of info and no need for read/write on the same memory port. – Neil_UK Jan 20 '23 at 06:29
9

This might just be opinion-based about the term, but in my opinion, a ping-pong buffer means a specific kind of double buffering.

So yes, they both mean double buffering, but they are different.

Typically double buffering means you have two buffers, but nothing is defined how they work, and they can be two completely separate buffers.

A ping-pong buffer usually means there is a single contiguous block of memory only divided logically in two halves.

This means that if you use DMA to receive or transmit data, without a ping-pong buffer, you may have two completely separate memory areas, and must stop at the end of one buffer and restart the transfer to use the second buffer. This also means that you are not limited to two buffers, and they don't have to be in fixed locations, you could always just allocate memory for a new buffer and tell your buffering system to transmit it and then deallocate the memory after it is done transmitting. This can be used to stream continuous data transfers but it might be cubmersome due to the pause which is why early PC sound cards had a slight gap between buffers so it can cause problems at high data rates. It also enables to transfer arbitrary buffer sizes when needed and transfers to be paused when there is nothing to transfer, like for example serial port data.

With a ping-pong buffer, you can set the DMA to work on the whole area of two contiguous buffers at once, and use the autorestart functionality of the DMA to wrap around automatically to start of first buffer, without reconfiguring anything. This is why some DMA controllers have half-buffer transfer complete interrupts to signal the user that half of buffer has been transmitted so that half can be overwritten with new data to send. This works best with continuous data streams of fixed block sizes, and is most often useful transferring audio. This mode was supported by later PC sound cards to have that gapless playback.

Justme
  • 127,425
  • 3
  • 97
  • 261
  • "A ping-pong buffer usually means there is a single contiguous block of memory only divided logically in two halves." I disagree. A ping pong buffer could be two separate memories, or just, in a pathological case, two registers. – SteveSh Nov 18 '21 at 21:38
  • @SteveSh I checked the manual of few MCUs I use and there the DMA controller is said to have hardware support for "circular mode", and in circular mode it can also support "double buffer mode", where there can be two separate buffers. It may be that the ping-pong does not mean a circular buffer after all so I may have used the term wrong. – Justme Nov 18 '21 at 21:53
  • But ping pong or double buffers are not unique to MCUs and DMA controllers. They have been part of a digital designer's toolbox ever since I can remember, and have been used way before MCU's came into existence. – SteveSh Nov 26 '21 at 11:50
5

A ping-pong buffer means switching to writing to the other buffer when the current first buffer is full. The filling process writes to whichever buffer is current. The emptying process reads from whichever buffer is not current. The switch is very fast: just a change to the write data pointer.

In video display systems, double buffering means only writing to a first buffer which, when complete, is copied to a second buffer then freed for reuse. The filling process only ever writes to the first buffer. The display is only updated from the second buffer: the frame store memory. The copy takes time and the larger the buffer, the longer it takes.

Video systems do this so that the filling process is never writing to the frame store memory, which can cause tearing, snow and other already well-documented problems.

TonyM
  • 21,742
  • 4
  • 39
  • 62
  • I think "double buffer" in the context of video display systems can also refer to ping-pong buffering. – user253751 Nov 17 '21 at 18:12
  • Graphics APIs have "swap buffers" command, which exchange the roles of two buffers (one for display, one for drawing) and call this a form of double buffering. This might be less relevant to hardware design but it is definitely standard terminology for GPU programming. – Kevin Reid Nov 18 '21 at 00:34
  • @user253751: In display contexts, double-buffering usually refers to situations where the display swap will be deferred until a non-displayed buffer is completely written; as I've seen the term used, ping-ping buffering implies that the buffer swap will occur unconditionally, which may result in less than ideal behavior in cases of buffer overrun/underrun. – supercat Nov 18 '21 at 19:54
2

As I interpret the term, ping-pong buffering refers to a form of double-buffering in which the process that fetches data from a transmit buffer or stores data into a receive buffer will unconditionally switch between the two buffers whenever it reaches the end of the current buffer, without regard for whether the process that's filling a transmit buffer or empty a recieve buffer is ready for it to do so. It will behave the same as other forms of double buffering in cases where the inactive buffer will always be ready for use by the time it is made active. The difference lies in what happens in case of buffer overrun/underrun.

In a ping-pong buffered video system, if a new frame doesn't get rendered in time, a partially-rendered frame may be output, and rendering falls behind by another frame, the system will start displaying the earlier frame again. By contrast, most double-buffered video systems are designed so that if there isn't a new frame ready by the time the display system finishes outputting the current one, the current display contents will be sent out again. Only when a new frame has been fully rendered will the display hardware show the contents of the buffer in which it was built.

Contrary to what some other answers claim, the term "double buffering" is often used for page-switching video systems, rather than merely to those where a fully rendered image is copied from one buffer to another. What makes "ping ping" buffering different from other page-flipping approaches is the way it behaves if the rendering process lags behind the display process.

supercat
  • 45,939
  • 2
  • 84
  • 143
  • I don't think there's anything about a ping pong buffer, in the general sense, that requires one buffer (memory) to be filled and then unconditionally switch to the other. Ping pong buffers can switch for other reasons, such as some external event like a switch strobe. It all depends on the particulars of the application. – SteveSh Nov 18 '21 at 21:35
1

Wikipedia, in my opinion, provides a good explanation of the differences.

https://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics

The article describes the following buffer techniques for computer graphics:

  • Software Double-Buffering
  • Page Flipping

Wikipedia

Software Double-Buffering uses system RAM as a frame buffer and copies the contents over to Video or Graphics RAM.

A software implementation of double buffering has all drawing operations store their results in some region of system RAM; any such region is often called a "back buffer". When all drawing operations are considered complete, the whole region (or only the changed portion) is copied into the video RAM (the "front buffer"); this copying is usually synchronized with the monitor's raster beam in order to avoid tearing. Software implementations of double buffering necessarily requires more memory and CPU time than single buffering because of the system memory allocated for the back buffer, the time for the copy operation, and the time waiting for synchronization.

Page flipping utilizes two buffers directly in Video or Graphics RAM.

At any one time, one buffer is actively being displayed by the monitor, while the other, background buffer is being drawn. When the background buffer is complete, the roles of the two are switched. The page-flip is typically accomplished by modifying a hardware register in the video display controller—the value of a pointer to the beginning of the display data in the video memory.

Intel GPU's

Another thing to note is that not all GPU's have dedicated Video RAM or VRAM. For instance, Intel Graphics controllers do not have dedicated Video RAM.

https://www.intel.com/content/www/us/en/support/articles/000041253/graphics.html

The Graphics Processing Unit (GPU) does not have a dedicated memory; it uses shared memory that will be allocated automatically depending on various factors.

More resources from Intel for FPGA's

For double-buffering, the Frame Buffer II IP core uses two frame buffers in external RAM.

The writer uses one buffer to store input pixels. The reader locks the second buffer that reads the output pixels from the memory. When both writer and reader complete processing a frame, the buffers are exchanged. The input frame can then be read back from the memory and sent to the output, while the buffer that has just been used to create the output can be overwritten with fresh input.

For triple-buffering, the IP core uses three frame buffers in external RAM.

The writer uses one buffer to store input pixels. The reader locks the second buffer that reads the output pixels from the memory. The third buffer is a spare buffer that allows the input and the output sides to swap buffers asynchronously. The spare buffer can be clean or dirty. Considered clean if it contains a fresh frame that has not been sent. Considered dirty if it contains an old frame that has already been sent by the reader component. When the writer completes storing a frame in memory, it swaps its buffer with the spare buffer if the spare buffer is dirty.

The buffer locked by the writer becomes the new spare buffer and is clean because it contains a fresh frame. If the spare buffer is already clean when the writer completes writing the current input frame: If dropping frames is allowed—the writer drops the newly received frame and overwrites its buffer with the next incoming frame. If dropping frames is not allowed—the writer stalls until the reader completes its frame and replaces the spare buffer with a dirty buffer. When the reader completes reading and produces a frame from memory, it swaps its buffer with the spare buffer if the spare buffer is clean.

The buffer locked by the reader becomes the new spare buffer; and is dirty because it contains an old frame that has been sent previously. If the spare buffer is already dirty when the reader completes the current output frame: If repeating frames is allowed—the reader immediately repeats the frame that has just been sent. If repeating frames is not allowed—the reader stalls until the writer completes its frame and replaces the spare buffer with a clean buffer.

Another resource from Cornell

Double buffering, aka ping-pong buffering, is a technique to simplify programming when multiple operations are applied to the same image in succession. We have buffers. At any time, one buffer is the write buffer and the other is the read buffer. The operation you can perform on the buffer is as indicated by the name. You can only read from the read buffer, and you can only write from the write buffer. We can swap the buffer to exchange their roles.

mrbean
  • 683
  • 5
  • 12
  • 1
    Since I am hardware engineer aka chip designer, the question was from that perspective :) but the information you have provided is still useful to me – quantum231 Jan 20 '23 at 14:27
  • That is also my background. In that case, you may want to check out these resources :) https://www.intel.com/content/www/us/en/docs/programmable/683416/22-1/double-buffering-75744.html https://www.intel.com/content/www/us/en/docs/programmable/683416/22-1/triple-buffering.html https://www.amazon.com/Design-Embedded-Image-Processing-FPGAs-ebook/dp/B005FXVEDY/ref=sr_1_1?crid=77RSSCL9R0WX&keywords=Image+Processing+FPGA&qid=1674233932&s=books&sprefix=image+processing+fpga%2Cstripbooks%2C72&sr=1-1 https://www.cs.cornell.edu/courses/cs4620/2017sp/cs4621/lecture08/exhibit01.html – mrbean Jan 20 '23 at 17:08
  • 1
    https://www.amazon.com/Video-Demystified-Handbook-Digital-Engineer-ebook/dp/B0089NVYIM/ref=sr_1_1?keywords=video+demystified&qid=1674234456&s=books&sprefix=Video+Demys%2Cstripbooks%2C77&sr=1-1 – mrbean Jan 20 '23 at 17:08