+
+static int
+NineSwapChain9_GetBackBufferCountForParams( struct NineSwapChain9 *This,
+ D3DPRESENT_PARAMETERS *pParams )
+{
+ int count = pParams->BackBufferCount;
+
+ /* When we have flip behaviour, d3d9 expects we get back the screen buffer when we flip.
+ * Here we don't get back the initial content of the screen. To emulate the behaviour
+ * we allocate an additional buffer */
+ if (pParams->SwapEffect != D3DSWAPEFFECT_COPY)
+ count++;
+ /* With DISCARD, as there is no guarantee about the buffer contents, we can use
+ * an arbitrary number of buffers */
+ if (pParams->SwapEffect == D3DSWAPEFFECT_DISCARD) {
+ /* thread_submit has a throttling equivalent to the throttling
+ * with throttling_value set to count-1. Most drivers use
+ * 2 for throttling_value. For performance use count of at least 3
+ * for thread_submit. */
+ if (This->actx->thread_submit && count < 3)
+ count = 3;
+ /* When we enable AllowDISCARDDelayedRelease, we must ensure
+ * to have at least 4 buffers to meet INTERVAL_IMMEDIATE,
+ * since the display server/compositor can hold 3 buffers
+ * without releasing them:
+ * . Buffer on screen.
+ * . Buffer scheduled kernel side to be next on screen.
+ * . Last buffer sent.
+ * For some reasons, 5 buffers are actually needed, because in
+ * case a pageflip is missed because rendering wasn't finished,
+ * the Xserver will hold 4 buffers. */
+ if (!This->actx->thread_submit &&
+ This->base.device->minor_version_num > 2 &&
+ pParams->PresentationInterval == D3DPRESENT_INTERVAL_IMMEDIATE &&
+ count < 5)
+ count = 5;
+ }
+
+ return count;
+}