Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 37733)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 37734)
@@ -1529,87 +1529,4 @@
     return hr;
 }
-
-#if 0
-static HRESULT vboxWddmRenderTargetUpdateSurface(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_ALLOCATION pAlloc, uint32_t iBBuf)
-{
-    if (pAlloc->SurfDesc.VidPnSourceId != pDevice->iPrimaryScreen)
-        return S_OK;
-
-    IDirect3DSurface9 *pD3D9Surf;
-    IDirect3DDevice9 *pDevice9If = pDevice->aScreens[pDevice->iPrimaryScreen].pDevice9If;
-    HRESULT hr = pDevice9If->GetBackBuffer(0 /*UINT iSwapChain*/,
-            iBBuf, D3DBACKBUFFER_TYPE_MONO, &pD3D9Surf);
-    Assert(hr == S_OK);
-    if (hr == S_OK)
-    {
-        Assert(pD3D9Surf);
-        Assert(pAlloc->enmD3DIfType == VBOXDISP_D3DIFTYPE_SURFACE);
-        if (pAlloc->pD3DIf)
-            pAlloc->pD3DIf->Release();
-        pAlloc->pD3DIf = pD3D9Surf;
-    }
-    return hr;
-}
-static HRESULT vboxWddmRenderTargetUpdate(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_RESOURCE pRc, uint32_t iNewRTFB)
-{
-    if (pRc->RcDesc.VidPnSourceId != pDevice->iPrimaryScreen)
-        return S_OK;
-
-    PVBOXWDDMDISP_ALLOCATION pAlloc;
-    UINT iBBuf = 0;
-    Assert(iNewRTFB < pRc->cAllocations);
-
-    for (UINT i = 1; i < pRc->cAllocations; ++i, ++iBBuf)
-    {
-        UINT iAlloc = (iNewRTFB + i) % pRc->cAllocations;
-        Assert(iAlloc != iNewRTFB);
-        pAlloc = &pRc->aAllocations[iAlloc];
-        HRESULT tmpHr = vboxWddmRenderTargetUpdateSurface(pDevice, pAlloc, iBBuf);
-        Assert(tmpHr == S_OK);
-    }
-
-    pAlloc = &pRc->aAllocations[iNewRTFB];
-    if (pRc->cAllocations > 1)
-    {
-#ifdef VBOXWDDM_WITH_VISIBLE_FB
-        HRESULT tmpHr = vboxWddmRenderTargetUpdateSurface(pDevice, pAlloc, ~0UL /* <- for the frontbuffer */);
-        Assert(tmpHr == S_OK);
-#else
-        if (pAlloc->pD3DIf)
-        {
-            pAlloc->pD3DIf->Release();
-            pAlloc->pD3DIf = NULL;
-        }
-#endif
-    }
-    else
-    {
-#ifndef VBOXDISP_WITH_WINE_BB_WORKAROUND
-# error "port me!"
-#endif
-        /* work-around wine backbuffer for devices w/o backbuffers */
-        HRESULT tmpHr = vboxWddmRenderTargetUpdateSurface(pDevice, pAlloc, 0);
-        Assert(tmpHr == S_OK);
-    }
-
-#ifdef DEBUG
-    for (UINT i = 0; i < pRc->cAllocations; ++i)
-    {
-        pAlloc = &pRc->aAllocations[i];
-        if (iNewRTFB == i)
-        {
-            Assert((!pAlloc->pD3DIf) == (pRc->cAllocations > 1));
-        }
-
-        for (UINT j = i+1; j < pRc->cAllocations; ++j)
-        {
-            PVBOXWDDMDISP_ALLOCATION pAllocJ = &pRc->aAllocations[j];
-            Assert(pAlloc->pD3DIf != pAllocJ->pD3DIf);
-        }
-    }
-#endif
-    return S_OK;
-}
-#endif
 
 static D3DFORMAT vboxDDI2D3DFormat(D3DDDIFORMAT format)
@@ -1841,7 +1758,10 @@
     {
 #ifndef VBOXWDDM_WITH_VISIBLE_FB
-        pSwapchain->pRenderTargetFbCopy->Release();
-        pSwapchain->pRenderTargetFbCopy = NULL;
-        pSwapchain->bRTFbCopyUpToDate = FALSE;
+        if (pSwapchain->pRenderTargetFbCopy)
+        {
+            pSwapchain->pRenderTargetFbCopy->Release();
+            pSwapchain->pRenderTargetFbCopy = NULL;
+            pSwapchain->bRTFbCopyUpToDate = FALSE;
+        }
 #endif
         pSwapchain->pSwapChainIf->Release();
@@ -2192,73 +2112,88 @@
     PVBOXWDDMDISP_RENDERTGT pRt = &pSwapchain->aRTs[iRt];
     HRESULT hr = pSwapchain->pSwapChainIf->GetBackBuffer(iBb, D3DBACKBUFFER_TYPE_MONO, &pD3D9Surf);
-    Assert(hr == S_OK);
-    if (hr == S_OK)
-    {
-        PVBOXWDDMDISP_ALLOCATION pAlloc = pRt->pAlloc;
-        Assert(pD3D9Surf);
-        Assert(pAlloc->enmD3DIfType == VBOXDISP_D3DIFTYPE_SURFACE);
-        if (pAlloc->pD3DIf)
-        {
-            if (pSwapchain->fFlags.bChanged)
-            {
-                IDirect3DSurface9 *pD3D9OldSurf = (IDirect3DSurface9*)pAlloc->pD3DIf;
-                if (pD3D9OldSurf && pD3D9OldSurf != pD3D9Surf)
-                {
-                    VOID *pvSwapchain = NULL;
-                    HRESULT tmpHr = pD3D9OldSurf->GetContainer(IID_IDirect3DSwapChain9, &pvSwapchain);
-                    if (tmpHr == S_OK)
-                    {
-                        Assert(pvSwapchain);
-                        ((IDirect3DSwapChain9 *)pvSwapchain)->Release();
-                    }
-                    else
-                    {
-                        Assert(!pvSwapchain);
-                    }
-                    if (pvSwapchain != pSwapchain->pSwapChainIf)
-                    {
+    if (FAILED(hr))
+    {
+        WARN(("GetBackBuffer failed, hr (0x%x)",hr));
+        return hr;
+    }
+
+    PVBOXWDDMDISP_ALLOCATION pAlloc = pRt->pAlloc;
+    Assert(pD3D9Surf);
+    Assert(pAlloc->enmD3DIfType == VBOXDISP_D3DIFTYPE_SURFACE);
+    if (pAlloc->pD3DIf)
+    {
+        if (pSwapchain->fFlags.bChanged)
+        {
+            IDirect3DSurface9 *pD3D9OldSurf = (IDirect3DSurface9*)pAlloc->pD3DIf;
+            if (pD3D9OldSurf && pD3D9OldSurf != pD3D9Surf)
+            {
+                VOID *pvSwapchain = NULL;
+                /* get the old surface's swapchain */
+                HRESULT tmpHr = pD3D9OldSurf->GetContainer(IID_IDirect3DSwapChain9, &pvSwapchain);
+                if (tmpHr == S_OK)
+                {
+                    Assert(pvSwapchain);
+                    ((IDirect3DSwapChain9 *)pvSwapchain)->Release();
+                }
+                else
+                {
+                    Assert(!pvSwapchain);
+                }
+
+                if (pvSwapchain != pSwapchain->pSwapChainIf)
+                {
+                    /* the swapchain has changed, copy data to the new surface */
 #ifdef DEBUG_misha
-                        /* @todo: we can not generally update the render target directly, implement */
-                        Assert(iBb != (~0));
+                    /* @todo: we can not generally update the render target directly, implement */
+                    Assert(iBb != (~0));
 #endif
-                        hr = pDevice->pDevice9If->StretchRect(pD3D9OldSurf, NULL, pD3D9Surf, NULL, D3DTEXF_NONE);
-                        Assert(hr == S_OK);
-                        if (pSwapchain->cRTs == 1)
-                        {
-                            /* synch bb and fb */
-                            hr = pSwapchain->pSwapChainIf->Present(NULL, NULL, NULL, NULL, 0);
-                            Assert(hr == S_OK);
-                            if (hr == S_OK)
-                            {
-                                pD3D9Surf->Release();
-                                hr = pSwapchain->pSwapChainIf->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pD3D9Surf);
-                                Assert(hr == S_OK);
-                                if (hr == S_OK)
-                                {
-                                    hr = pDevice->pDevice9If->StretchRect(pD3D9OldSurf, NULL, pD3D9Surf, NULL, D3DTEXF_NONE);
-                                    Assert(hr == S_OK);
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-            pAlloc->pD3DIf->Release();
-        }
-        pAlloc->pD3DIf = pD3D9Surf;
-        pRt->fFlags.Value = 0;
-
-        if (pSwapchain->fFlags.bChanged)
-        {
-            for (UINT i = 0; i < pDevice->cRTs; ++i)
-            {
-                if (pDevice->apRTs[i] == pAlloc)
-                {
-                    hr = vboxWddmRenderTargetSet(pDevice, i, pAlloc, TRUE);
+                    hr = pDevice->pDevice9If->StretchRect(pD3D9OldSurf, NULL, pD3D9Surf, NULL, D3DTEXF_NONE);
                     Assert(hr == S_OK);
                 }
             }
         }
-    }
+        pAlloc->pD3DIf->Release();
+    }
+
+    pAlloc->pD3DIf = pD3D9Surf;
+    pRt->fFlags.Value = 0;
+
+    if (pSwapchain->fFlags.bChanged)
+    {
+        for (UINT i = 0; i < pDevice->cRTs; ++i)
+        {
+            if (pDevice->apRTs[i] == pAlloc)
+            {
+                hr = vboxWddmRenderTargetSet(pDevice, i, pAlloc, TRUE);
+                Assert(hr == S_OK);
+            }
+        }
+    }
+
+#ifdef VBOXDISP_WITH_WINE_BB_WORKAROUND
+    if (pSwapchain->cRTs == 1)
+    {
+        /* here we sync the front-buffer with a backbuffer data*/
+        IDirect3DSurface9 *pD3D9Bb = (IDirect3DSurface9*)vboxWddmSwapchainGetBb(pSwapchain)->pAlloc->pD3DIf;
+        Assert(pD3D9Bb);
+        pD3D9Bb->AddRef();
+        /* we use backbuffer as a rt frontbuffer copy, so release the old one and assign the current bb */
+        if (pSwapchain->pRenderTargetFbCopy)
+        {
+            pSwapchain->pRenderTargetFbCopy->Release();
+        }
+        pSwapchain->pRenderTargetFbCopy = pD3D9Bb;
+        HRESULT tmpHr = pSwapchain->pSwapChainIf->GetFrontBufferData(pD3D9Bb);
+        if (SUCCEEDED(tmpHr))
+        {
+            VBOXVDBG_DUMP_SYNC_RT(pD3D9Bb);
+            pSwapchain->bRTFbCopyUpToDate = TRUE;
+        }
+        else
+        {
+            WARN(("GetFrontBufferData failed, hr (0x%x)", hr));
+        }
+    }
+#endif
     return hr;
 }
@@ -2299,5 +2234,5 @@
     pParams->MultiSampleType = vboxDDI2D3DMultiSampleType(pRc->RcDesc.enmMultisampleType);
     pParams->MultiSampleQuality = pRc->RcDesc.MultisampleQuality;
-#ifdef VBOXDISP_WITH_WINE_BB_WORKAROUND
+#if 0 //def VBOXDISP_WITH_WINE_BB_WORKAROUND /* this does not work so far any way :( */
     if (pSwapchain->cRTs == 1)
         pParams->SwapEffect = D3DSWAPEFFECT_COPY;
@@ -2478,4 +2413,16 @@
 #ifndef VBOXWDDM_WITH_VISIBLE_FB
         pSwapchain->bRTFbCopyUpToDate = FALSE;
+# ifdef VBOXDISP_WITH_WINE_BB_WORKAROUND
+        if (pSwapchain->cRTs == 1)
+        {
+            /* we will assign it to wine backbuffer on a swapchain synch */
+            if (pSwapchain->pRenderTargetFbCopy)
+            {
+                pSwapchain->pRenderTargetFbCopy->Release();
+                pSwapchain->pRenderTargetFbCopy = NULL;
+            }
+        }
+        else
+# endif
         if (!pSwapchain->pRenderTargetFbCopy)
         {
@@ -2611,4 +2558,89 @@
 }
 
+/* get the surface for the specified allocation in the swapchain */
+static HRESULT vboxWddmSwapchainSurfGet(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_SWAPCHAIN pSwapchain, PVBOXWDDMDISP_ALLOCATION pAlloc, IDirect3DSurface9 **ppSurf)
+{
+    Assert(pAlloc->pSwapchain == pSwapchain);
+
+#ifndef VBOXWDDM_WITH_VISIBLE_FB
+    if (vboxWddmSwapchainGetFb(pSwapchain)->pAlloc == pAlloc
+# ifdef VBOXDISP_WITH_WINE_BB_WORKAROUND
+
+            && vboxWddmSwapchainNumRTs(pSwapchain) != 1 /* for swapchains w/o a backbuffer the alloc will contain the back-buffer actually */
+            )
+    {
+        /* this is a front-buffer */
+        Assert(vboxWddmSwapchainNumRTs(pSwapchain) > 1);
+        IDirect3DSurface9 *pSurf = pSwapchain->pRenderTargetFbCopy;
+        Assert(pSurf);
+        pSurf->AddRef();
+        if (!pSwapchain->bRTFbCopyUpToDate)
+        {
+            HRESULT hr = pSwapchain->pSwapChainIf->GetFrontBufferData(pSurf);
+            if (FAILED(hr))
+            {
+                WARN(("GetFrontBufferData failed, hr (0x%x)", hr));
+                return hr;
+            }
+            pSwapchain->bRTFbCopyUpToDate = TRUE;
+        }
+
+        *ppSurf = pSurf;
+        return S_OK;
+    }
+# endif
+#endif
+
+    /* if this is not a front-buffer - just return the surface associated with the allocation */
+    return vboxWddmSurfGet(pAlloc->pRc, pAlloc->iAlloc, ppSurf);
+}
+
+static HRESULT vboxWddmSwapchainRtSurfGet(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_SWAPCHAIN pSwapchain, UINT iRt, PVBOXWDDMDISP_ALLOCATION pAlloc, BOOL bOnSwapchainSynch, IDirect3DSurface9 **ppSurf)
+{
+    Assert(pAlloc->pSwapchain == pSwapchain);
+    HRESULT hr = S_OK;
+
+    /* do the necessary swapchain synchronization first,
+     * not needed on swapchain synch since it is done already and we're called here just to set RTs */
+    if (!bOnSwapchainSynch)
+    {
+
+        /* iRt != 0 is untested here !! */
+        Assert(iRt == 0);
+        if (iRt == 0)
+        {
+            hr = vboxWddmSwapchainBbUpdate(pDevice, pSwapchain, pAlloc);
+            if (FAILED(hr))
+            {
+                WARN(("vboxWddmSwapchainBbUpdate failed, hr(0x%)",hr));
+                return hr;
+            }
+        }
+
+        Assert(!pSwapchain->fFlags.bChanged);
+        Assert(pSwapchain->pSwapChainIf);
+        hr = vboxWddmSwapchainChkCreateIf(pDevice, pSwapchain);
+        if (FAILED(hr))
+        {
+            WARN(("vboxWddmSwapchainChkCreateIf failed, hr(0x%)",hr));
+            return hr;
+        }
+    }
+
+
+    Assert(vboxWddmSwapchainGetBb(pSwapchain)->pAlloc == pAlloc || iRt != 0);
+    IDirect3DSurface9 *pSurf;
+    hr = vboxWddmSwapchainSurfGet(pDevice, pSwapchain, pAlloc, &pSurf);
+    if (FAILED(hr))
+    {
+        WARN(("vboxWddmSwapchainSurfGet failed, hr(0x%x)", hr));
+        return hr;
+    }
+
+    *ppSurf = pSurf;
+    return S_OK;
+
+}
+
 static HRESULT vboxWddmSwapchainPresent(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_ALLOCATION pBbAlloc)
 {
@@ -2689,173 +2721,4 @@
 #endif
 
-#if 0
-static HRESULT vboxWddmD3DDeviceCreate(PVBOXWDDMDISP_DEVICE pDevice, UINT iScreen, PVBOXWDDMDISP_RESOURCE pRc, D3DPRESENT_PARAMETERS * pParams, BOOL bLockable)
-{
-    UINT cSurfs = pParams->BackBufferCount + 1;
-    Assert(pRc->cAllocations = cSurfs);
-    IDirect3DDevice9 *pPrimaryDevice = pDevice->aScreens[pDevice->iPrimaryScreen].pDevice9If;
-    PVBOXWDDMDISP_SCREEN pScreen = &pDevice->aScreens[iScreen];
-    PVBOXWDDMDISP_ADAPTER pAdapter = pDevice->pAdapter;
-    HRESULT hr;
-    HWND hWnd = NULL;
-    Assert(!pScreen->pDevice9If);
-    Assert(!pScreen->hWnd);
-    hr = VBoxDispWndCreate(pAdapter, pParams->BackBufferWidth, pParams->BackBufferHeight, &hWnd);
-    Assert(hr == S_OK);
-    if (hr == S_OK)
-    {
-        pScreen->hWnd = hWnd;
-
-        DWORD fFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
-        if (pDevice->fFlags.AllowMultithreading)
-            fFlags |= D3DCREATE_MULTITHREADED;
-
-        IDirect3DDevice9 *pDevice9If = NULL;
-        pParams->hDeviceWindow = hWnd;
-                    /* @todo: it seems there should be a way to detect this correctly since
-                     * our vboxWddmDDevSetDisplayMode will be called in case we are using full-screen */
-        pParams->Windowed = TRUE;
-        //            params.EnableAutoDepthStencil = FALSE;
-        //            params.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
-        //            params.Flags;
-        //            params.FullScreen_RefreshRateInHz;
-        //            params.FullScreen_PresentationInterval;
-        hr = pAdapter->pD3D9If->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, fFlags, pParams, &pDevice9If);
-        Assert(hr == S_OK);
-        if (hr == S_OK)
-        {
-            pScreen->pDevice9If = pDevice9If;
-            pScreen->pRenderTargetRc = pRc;
-            ++pDevice->cScreens;
-
-            for (UINT i = 0; i < cSurfs; ++i)
-            {
-                PVBOXWDDMDISP_ALLOCATION pAllocation = &pRc->aAllocations[i];
-                pAllocation->enmD3DIfType = VBOXDISP_D3DIFTYPE_SURFACE;
-            }
-
-            if (pPrimaryDevice)
-            {
-                for (UINT i = 0; i < cSurfs; ++i)
-                {
-                    PVBOXWDDMDISP_ALLOCATION pAllocation = &pRc->aAllocations[i];
-                    IDirect3DSurface9 *pRt;
-                    IDirect3DSurface9 *pSecondaryOpenedRt;
-                    HANDLE hSharedHandle = NULL;
-                    hr = pPrimaryDevice->CreateRenderTarget(
-                            pParams->BackBufferWidth, pParams->BackBufferHeight,
-                            pParams->BackBufferFormat,
-                            pParams->MultiSampleType,
-                            pParams->MultiSampleQuality,
-                            TRUE, /*BOOL Lockable*/
-                            &pRt,
-                            &hSharedHandle);
-                    Assert(hr == S_OK);
-                    if (hr == S_OK)
-                    {
-                        Assert(hSharedHandle != NULL);
-                        /* open render target for primary device */
-                        hr = pDevice9If->CreateRenderTarget(
-                                    pParams->BackBufferWidth, pParams->BackBufferHeight,
-                                    pParams->BackBufferFormat,
-                                    pParams->MultiSampleType,
-                                    pParams->MultiSampleQuality,
-                                    TRUE, /*BOOL Lockable*/
-                                    &pSecondaryOpenedRt,
-                                    &hSharedHandle);
-                        Assert(hr == S_OK);
-                        if (hr == S_OK)
-                        {
-                            pAllocation->pD3DIf = pRt;
-                            pAllocation->pSecondaryOpenedD3DIf = pSecondaryOpenedRt;
-                            pAllocation->hSharedHandle = hSharedHandle;
-                            continue;
-                        }
-                        pRt->Release();
-                    }
-
-                    for (UINT j = 0; j < i; ++j)
-                    {
-                        PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[j];
-                        pAlloc->pD3DIf->Release();
-                        pAlloc->pSecondaryOpenedD3DIf->Release();
-                    }
-
-                    break;
-                }
-            }
-            else
-            {
-                pDevice->iPrimaryScreen = iScreen;
-                hr = vboxWddmRenderTargetUpdate(pDevice, pRc, 0);
-                Assert(hr == S_OK);
-            }
-
-            if (hr == S_OK)
-            {
-                for (UINT i = 0; i < cSurfs; ++i)
-                {
-                    PVBOXWDDMDISP_ALLOCATION pAllocation = &pRc->aAllocations[i];
-                    pAllocation->enmD3DIfType = VBOXDISP_D3DIFTYPE_SURFACE;
-                    hr = vboxWddmSurfSynchMem(pRc, pAllocation);
-                    Assert(hr == S_OK);
-                    if (hr != S_OK)
-                    {
-                        break;
-                    }
-                }
-
-#ifndef VBOXWDDM_WITH_VISIBLE_FB
-                if (!pPrimaryDevice)
-                {
-                    if (hr == S_OK)
-                    {
-                        IDirect3DSurface9* pD3D9Surf;
-                        hr = pDevice9If->CreateRenderTarget(
-                                pParams->BackBufferWidth, pParams->BackBufferHeight,
-                                pParams->BackBufferFormat,
-                                pParams->MultiSampleType,
-                                pParams->MultiSampleQuality,
-                                bLockable,
-                                &pD3D9Surf,
-                                NULL /* HANDLE* pSharedHandle */
-                                );
-                        Assert(hr == S_OK);
-                        if (hr == S_OK)
-                        {
-                            pDevice->pRenderTargetFbCopy = pD3D9Surf;
-                        }
-                    }
-                }
-#endif
-
-                if (hr != S_OK)
-                {
-                    for (UINT i = 0; i < cSurfs; ++i)
-                    {
-                        PVBOXWDDMDISP_ALLOCATION pAllocation = &pRc->aAllocations[i];
-                        pAllocation->pD3DIf->Release();
-                    }
-                }
-            }
-
-            if (hr != S_OK)
-            {
-                pDevice9If->Release();
-                --pDevice->cScreens;
-                Assert(pDevice->cScreens < UINT32_MAX/2);
-            }
-        }
-
-        if (hr != S_OK)
-        {
-            HRESULT tmpHr = VBoxDispWndDestroy(pAdapter, pScreen->hWnd);
-            Assert(tmpHr == S_OK);
-        }
-    }
-
-    return hr;
-}
-#endif
 static HRESULT vboxWddmD3DDeviceCreateDummy(PVBOXWDDMDISP_DEVICE pDevice)
 {
@@ -2900,81 +2763,4 @@
 }
 
-#if 0
-static HRESULT APIENTRY vboxWddmDDevDestroyResource(HANDLE hDevice, HANDLE hResource);
-
-static HRESULT vboxWddmD3DDeviceUpdate(PVBOXWDDMDISP_DEVICE pDevice, UINT iScreen, PVBOXWDDMDISP_RESOURCE pRc, D3DPRESENT_PARAMETERS * pParams, BOOL bLockable)
-{
-    UINT cSurfs = pParams->BackBufferCount + 1;
-    Assert(pRc->cAllocations = cSurfs);
-    Assert(iScreen == pDevice->iPrimaryScreen);
-    PVBOXWDDMDISP_SCREEN pScreen = &pDevice->aScreens[iScreen];
-    PVBOXWDDMDISP_RESOURCE pCurRc = pScreen->pRenderTargetRc;
-    IDirect3DDevice9Ex *pNewDevice;
-    HRESULT hr = pDevice->pAdapter->D3D.pfnVBoxWineExD3DDev9Update((IDirect3DDevice9Ex*)pScreen->pDevice9If, pParams, &pNewDevice);
-    Assert(hr == S_OK);
-    if (hr == S_OK)
-    {
-        pScreen->pDevice9If->Release();
-        pScreen->pDevice9If = pNewDevice;
-        pScreen->pRenderTargetRc = pRc;
-
-        for (UINT i = 0; i < cSurfs; ++i)
-        {
-            PVBOXWDDMDISP_ALLOCATION pAllocation = &pRc->aAllocations[i];
-            pAllocation->enmD3DIfType = VBOXDISP_D3DIFTYPE_SURFACE;
-        }
-
-#ifndef VBOXWDDM_WITH_VISIBLE_FB
-        if (pDevice->pRenderTargetFbCopy)
-        {
-            pDevice->pRenderTargetFbCopy->Release();
-        }
-        IDirect3DSurface9* pD3D9Surf;
-        hr = pNewDevice->CreateRenderTarget(
-                    pParams->BackBufferWidth, pParams->BackBufferHeight,
-                    pParams->BackBufferFormat,
-                    pParams->MultiSampleType,
-                    pParams->MultiSampleQuality,
-                    bLockable,
-                    &pD3D9Surf,
-                    NULL /* HANDLE* pSharedHandle */
-                    );
-        Assert(hr == S_OK);
-        if (hr == S_OK)
-        {
-            pDevice->pRenderTargetFbCopy = pD3D9Surf;
-        }
-#endif
-        if (hr == S_OK)
-        {
-            hr = vboxWddmRenderTargetUpdate(pDevice, pRc, 0);
-            Assert(hr == S_OK);
-            if (hr == S_OK)
-            {
-                for (UINT i = 0; i < cSurfs; ++i)
-                {
-                    PVBOXWDDMDISP_ALLOCATION pAllocation = &pRc->aAllocations[i];
-                    pAllocation->enmD3DIfType = VBOXDISP_D3DIFTYPE_SURFACE;
-                    hr = vboxWddmSurfSynchMem(pRc, pAllocation);
-                    Assert(hr == S_OK);
-                    if (hr != S_OK)
-                    {
-                        break;
-                    }
-                }
-            }
-        }
-    }
-
-
-    if (!pCurRc->hResource)
-    {
-        HRESULT tmpHr = vboxWddmDDevDestroyResource(pDevice, pCurRc);
-        Assert(tmpHr == S_OK);
-    }
-
-    return hr;
-}
-#endif
 /******/
 
@@ -2984,47 +2770,34 @@
     PVBOXWDDMDISP_SWAPCHAIN pSwapchain = vboxWddmSwapchainForAlloc(pAlloc);
     HRESULT hr = S_OK;
+    IDirect3DSurface9 *pD3D9Surf;
     if (pSwapchain)
     {
-        hr = vboxWddmSwapchainBbUpdate(pDevice, pSwapchain, pAlloc);
+        hr = vboxWddmSwapchainRtSurfGet(pDevice, pSwapchain, iRt, pAlloc, bOnSwapchainSynch, &pD3D9Surf);
         if (FAILED(hr))
+        {
+            WARN(("vboxWddmSwapchainRtSurfGet failed, hr(0x%)",hr));
             return hr;
-    }
-
-    IDirect3DSurface9 *pD3D9Surf;
-    if (!bOnSwapchainSynch && pSwapchain)
-    {
-        /* work-around wine double-buffering for the case we have no backbuffers */
-        Assert(!pSwapchain->fFlags.bChanged);
-        Assert(pSwapchain->pSwapChainIf);
-        vboxWddmSwapchainChkCreateIf(pDevice, pSwapchain);
-    }
-
-#ifdef VBOXDISP_WITH_WINE_BB_WORKAROUND
-    if (pSwapchain && vboxWddmSwapchainNumRTs(pSwapchain) == 1 && iRt == 0)
-    {
-        hr = pSwapchain->pSwapChainIf->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pD3D9Surf);
-        Assert(hr == S_OK);
-        Assert(pD3D9Surf);
+        }
     }
     else
-#endif
     {
         hr = vboxWddmSurfGet(pAlloc->pRc, pAlloc->iAlloc, &pD3D9Surf);
-        Assert(hr == S_OK);
-        Assert(pD3D9Surf);
-    }
-
+        if (FAILED(hr))
+        {
+            WARN(("vboxWddmSurfGet failed, hr(0x%)",hr));
+            return hr;
+        }
+    }
+
+    Assert(pD3D9Surf);
+
+    hr = pDevice9If->SetRenderTarget(iRt, pD3D9Surf);
+    Assert(hr == S_OK);
     if (hr == S_OK)
     {
-        Assert(pD3D9Surf);
-        hr = pDevice9If->SetRenderTarget(iRt, pD3D9Surf);
-        Assert(hr == S_OK);
-        if (hr == S_OK)
-        {
-            Assert(iRt < pDevice->cRTs);
-            pDevice->apRTs[iRt] = pAlloc;
-        }
-        pD3D9Surf->Release();
-    }
+        Assert(iRt < pDevice->cRTs);
+        pDevice->apRTs[iRt] = pAlloc;
+    }
+    pD3D9Surf->Release();
 
     return hr;
@@ -4973,5 +4746,4 @@
             if (pResource->Flags.SharedResource)
             {
-                Assert(0); /* <-- need to test that */
                 bIssueCreateResource = true;
                 bCreateKMResource = true;
@@ -6136,4 +5908,5 @@
     IDirect3DSurface9 *pSrcSurfIf = NULL;
     IDirect3DSurface9 *pDstSurfIf = NULL;
+    Assert(!pDstSwapchain || vboxWddmSwapchainGetFb(pDstSwapchain)->pAlloc != pDstAlloc || vboxWddmSwapchainNumRTs(pDstSwapchain) == 1);
     hr = vboxWddmSurfGet(pDstRc, pData->DstSubResourceIndex, &pDstSurfIf);
     Assert(hr == S_OK);
@@ -6143,52 +5916,10 @@
         do
         {
-#ifndef VBOXWDDM_WITH_VISIBLE_FB
-            if (pSrcSwapchain
-                    && pSrcSwapchain->pRenderTargetFbCopy
-                    && vboxWddmSwapchainGetFb(pSrcSwapchain)->pAlloc == pSrcAlloc
-# ifdef VBOXDISP_WITH_WINE_BB_WORKAROUND
-                    && vboxWddmSwapchainNumRTs(pSrcSwapchain) > 1 /* work-around wine backbuffer */
-# endif
-                    )
-            {
-# if 0
-                if (pData->DstRect.left == 0 && pData->DstRect.top == 0
-                        && pData->DstRect.right == pDstAlloc->SurfDesc.width
-                        && pData->DstRect.bottom == pDstAlloc->SurfDesc.height
-                        && pData->SrcRect.left == 0 && pData->SrcRect.top == 0
-                        && pData->SrcRect.right == pSrcAlloc->SurfDesc.width
-                        && pData->SrcRect.bottom == pSrcAlloc->SurfDesc.height
-                        && pSrcAlloc->SurfDesc.width == pDstAlloc->SurfDesc.width
-                        && pSrcAlloc->SurfDesc.height == pDstAlloc->SurfDesc.height
-                        && pSrcAlloc->SurfDesc.format == pDstAlloc->SurfDesc.format)
-                {
-                    hr = pDevice9If->GetFrontBufferData(0, pDstSurfIf);
-                    Assert(hr == S_OK);
-                    break;
-                }
-                else
-# endif
-                {
-                    pSrcSurfIf = pSrcSwapchain->pRenderTargetFbCopy;
-                    Assert(pSrcSurfIf);
-                    if (!pSrcSwapchain->bRTFbCopyUpToDate)
-                    {
-                        hr = pSrcSwapchain->pSwapChainIf->GetFrontBufferData(pSrcSurfIf);
-                        Assert(hr == S_OK);
-                        if (hr == S_OK)
-                        {
-                            /* do pSrcSurfIf->AddRef since we do a Release in the following if (hr == S_OK) branch */
-                            pSrcSwapchain->bRTFbCopyUpToDate = TRUE;
-                            pSrcSurfIf->AddRef();
-                        }
-                    }
-                    else
-                    {
-                        pSrcSurfIf->AddRef();
-                    }
-                }
+            if (pSrcSwapchain)
+            {
+                hr = vboxWddmSwapchainSurfGet(pDevice, pSrcSwapchain, pSrcAlloc, &pSrcSurfIf);
+                Assert(hr == S_OK);
             }
             else
-#endif
             {
                 hr = vboxWddmSurfGet(pSrcRc, pData->SrcSubResourceIndex, &pSrcSurfIf);
@@ -6225,109 +5956,4 @@
         Assert(0);
     }
-
-
-#if 0
-    if ((use pAlloc->enmD3DIfType instead!!! pDstRc->RcDesc.fFlags.Texture || pDstRc->RcDesc.fFlags.Value == 0)
-            && (pSrcRc->RcDesc.fFlags.Texture || pSrcRc->RcDesc.fFlags.Value == 0))
-    {
-        IDirect3DBaseTexture9 *pD3DIfSrcTex = (IDirect3DBaseTexture9*)pSrcAlloc->pD3DIf;
-        IDirect3DBaseTexture9 *pD3DIfDstTex = (IDirect3DBaseTexture9*)pDstAlloc->pD3DIf;
-        Assert(pD3DIfSrcTex);
-        Assert(pD3DIfDstTex);
-
-        if (pSrcRc->RcDesc.enmFormat == pDstRc->RcDesc.enmFormat)
-        {
-            if (pSrcRc->aAllocations[0].SurfDesc.width == pDstRc->aAllocations[0].SurfDesc.width
-                    && pSrcRc->aAllocations[0].SurfDesc.height == pDstRc->aAllocations[0].SurfDesc.height
-                    && pData->DstRect.left == 0 && pData->DstRect.top == 0
-                    && pData->SrcRect.left == 0 && pData->SrcRect.top == 0
-                    && pData->SrcRect.right - pData->SrcRect.left == pSrcRc->aAllocations[0].SurfDesc.width
-                    && pData->SrcRect.bottom - pData->SrcRect.top == pSrcRc->aAllocations[0].SurfDesc.height
-                    && pData->DstRect.right - pData->DstRect.left == pDstRc->aAllocations[0].SurfDesc.width
-                    && pData->DstRect.bottom - pData->DstRect.top == pDstRc->aAllocations[0].SurfDesc.height
-                    )
-            {
-                hr = pDevice9If->UpdateTexture(pD3DIfSrcTex, pD3DIfDstTex);
-                Assert(hr == S_OK);
-            }
-            else if (pData->SrcRect.right - pData->SrcRect.left == pData->DstRect.right - pData->DstRect.left
-                    && pData->SrcRect.bottom - pData->SrcRect.top == pData->DstRect.bottom - pData->DstRect.top)
-            {
-                Assert(pDstAlloc->SurfDesc.bpp);
-                Assert(pSrcAlloc->SurfDesc.bpp);
-                Assert(pSrcAlloc->SurfDesc.bpp == pDstAlloc->SurfDesc.bpp);
-                D3DLOCKED_RECT DstRect, SrcRect;
-                Assert(!pSrcAlloc->LockInfo.cLocks);
-                Assert(!pDstAlloc->LockInfo.cLocks);
-                hr = pD3DIfDstTex->LockRect(pData->DstSubResourceIndex, &DstRect, &pData->DstRect, D3DLOCK_DISCARD);
-                Assert(hr == S_OK);
-                if (hr == S_OK)
-                {
-                    hr = pD3DIfSrcTex->LockRect(pData->SrcSubResourceIndex, &SrcRect, &pData->SrcRect, D3DLOCK_READONLY);
-                    Assert(hr == S_OK);
-                    if (hr == S_OK)
-                    {
-                        hr = vboxWddmRectBltPerform((uint8_t *)DstRect.pBits, (uint8_t *)SrcRect.pBits,
-                                &pData->DstRect,  &pData->SrcRect,
-                                DstRect.Pitch, SrcRect.Pitch, pDstAlloc->SurfDesc.bpp);
-                        Assert(hr == S_OK);
-
-                        pD3DIfSrcTex->UnlockRect(pData->SrcSubResourceIndex);
-                    }
-                    pD3DIfDstTex->UnlockRect(pData->DstSubResourceIndex);
-                }
-            }
-            else
-            {
-
-                Assert(0);
-                /* @todo: impl */
-            }
-        }
-        else
-        {
-            Assert(0);
-            /* @todo: impl */
-        }
-    }
-    else
-    {
-        if (pData->SrcRect.right - pData->SrcRect.left == pData->DstRect.right - pData->DstRect.left
-                            && pData->SrcRect.bottom - pData->SrcRect.top == pData->DstRect.bottom - pData->DstRect.top)
-        {
-            Assert(pDstAlloc->SurfDesc.bpp);
-            Assert(pSrcAlloc->SurfDesc.bpp);
-            Assert(pSrcAlloc->SurfDesc.bpp == pDstAlloc->SurfDesc.bpp);
-
-            D3DLOCKED_RECT DstRect, SrcRect;
-            hr = vboxWddmLockRect(pDstAlloc, pData->DstSubResourceIndex, pDstRc->RcDesc.fFlags,
-                    &DstRect, &pData->DstRect, D3DLOCK_DISCARD);
-            Assert(hr == S_OK);
-            if (hr == S_OK)
-            {
-                hr = vboxWddmLockRect(pSrcAlloc, pData->SrcSubResourceIndex, pSrcRc->RcDesc.fFlags,
-                        &SrcRect, &pData->SrcRect, D3DLOCK_READONLY);
-                Assert(hr == S_OK);
-                if (hr == S_OK)
-                {
-                    hr = vboxWddmRectBltPerform((uint8_t *)DstRect.pBits, (uint8_t *)SrcRect.pBits,
-                            &pData->DstRect,  &pData->SrcRect,
-                            DstRect.Pitch, SrcRect.Pitch, pDstAlloc->SurfDesc.bpp);
-                    Assert(hr == S_OK);
-
-                    HRESULT tmpHr = vboxWddmUnlockRect(pSrcAlloc, pData->SrcSubResourceIndex, pSrcRc->RcDesc.fFlags);
-                    Assert(tmpHr == S_OK);
-                }
-                HRESULT tmpHr = vboxWddmUnlockRect(pDstAlloc, pData->DstSubResourceIndex, pDstRc->RcDesc.fFlags);
-                Assert(tmpHr == S_OK);
-            }
-        }
-        else
-        {
-            Assert(0);
-            /* @todo: impl */
-        }
-    }
-#endif
 
     PVBOXWDDMDISP_ALLOCATION pDAlloc = &pDstRc->aAllocations[pData->DstSubResourceIndex];
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp	(revision 37733)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp	(revision 37734)
@@ -50,8 +50,9 @@
 
 #ifdef VBOXWDDMDISP_DEBUG
-bool g_bVBoxVDbgFDumpSetTexture = false;
-bool g_bVBoxVDbgFDumpDrawPrim = false;
-bool g_bVBoxVDbgFDumpTexBlt = false;
-bool g_bVBoxVDbgFDumpBlt = false;
+DWORD g_VBoxVDbgFDumpSetTexture = 0;
+DWORD g_VBoxVDbgFDumpDrawPrim = 0;
+DWORD g_VBoxVDbgFDumpTexBlt = 0;
+DWORD g_VBoxVDbgFDumpBlt = 0;
+DWORD g_VBoxVDbgFDumpRtSynch = 0;
 
 typedef enum
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h	(revision 37733)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h	(revision 37734)
@@ -104,8 +104,9 @@
 
 #ifdef VBOXWDDMDISP_DEBUG
-extern bool g_bVBoxVDbgFDumpSetTexture;
-extern bool g_bVBoxVDbgFDumpDrawPrim;
-extern bool g_bVBoxVDbgFDumpTexBlt;
-extern bool g_bVBoxVDbgFDumpBlt;
+extern DWORD g_VBoxVDbgFDumpSetTexture;
+extern DWORD g_VBoxVDbgFDumpDrawPrim;
+extern DWORD g_VBoxVDbgFDumpTexBlt;
+extern DWORD g_VBoxVDbgFDumpBlt;
+extern DWORD g_VBoxVDbgFDumpRtSynch;
 
 void vboxDispLogDrvF(char * szString, ...);
@@ -131,5 +132,5 @@
 
 #define VBOXVDBG_DUMP_DRAWPRIM_ENTER(_pDevice) do { \
-        if (g_bVBoxVDbgFDumpDrawPrim) \
+        if (g_VBoxVDbgFDumpDrawPrim) \
         { \
             vboxVDbgDoDumpRt("==>"__FUNCTION__": RenderTarget Dump\n", (_pDevice), "\n"); \
@@ -138,5 +139,5 @@
 
 #define VBOXVDBG_DUMP_DRAWPRIM_LEAVE(_pDevice) do { \
-        if (g_bVBoxVDbgFDumpDrawPrim) \
+        if (g_VBoxVDbgFDumpDrawPrim) \
         { \
             vboxVDbgDoDumpRt("<=="__FUNCTION__": RenderTarget Dump\n", (_pDevice), "\n"); \
@@ -145,5 +146,5 @@
 
 #define VBOXVDBG_DUMP_SETTEXTURE(_pRc) do { \
-        if (g_bVBoxVDbgFDumpSetTexture) \
+        if (g_VBoxVDbgFDumpSetTexture) \
         { \
             vboxVDbgDoDumpRcRectByRc("== "__FUNCTION__": Texture Dump\n", _pRc, NULL, "\n"); \
@@ -152,5 +153,5 @@
 
 #define VBOXVDBG_DUMP_TEXBLT_ENTER(_pSrcRc, _pSrcRect, _pDstRc, _pDstPoint) do { \
-        if (g_bVBoxVDbgFDumpTexBlt) \
+        if (g_VBoxVDbgFDumpTexBlt) \
         { \
             RECT _DstRect; \
@@ -162,5 +163,5 @@
 
 #define VBOXVDBG_DUMP_TEXBLT_LEAVE(_pSrcRc, _pSrcRect, _pDstRc, _pDstPoint) do { \
-        if (g_bVBoxVDbgFDumpTexBlt) \
+        if (g_VBoxVDbgFDumpTexBlt) \
         { \
             RECT _DstRect; \
@@ -172,5 +173,5 @@
 
 #define VBOXVDBG_DUMP_BLT_ENTER(_pSrcSurf, _pSrcRect, _pDstSurf, _pDstRect) do { \
-        if (g_bVBoxVDbgFDumpBlt) \
+        if (g_VBoxVDbgFDumpBlt) \
         { \
             vboxVDbgDoDumpSurfRect("==>"__FUNCTION__" Src:\n", (_pSrcSurf), (_pSrcRect), "\n", true); \
@@ -180,8 +181,15 @@
 
 #define VBOXVDBG_DUMP_BLT_LEAVE(_pSrcSurf, _pSrcRect, _pDstSurf, _pDstRect) do { \
-        if (g_bVBoxVDbgFDumpBlt) \
+        if (g_VBoxVDbgFDumpBlt) \
         { \
             vboxVDbgDoDumpSurfRect("<=="__FUNCTION__" Src:\n", (_pSrcSurf), (_pSrcRect), "\n", true); \
             vboxVDbgDoDumpSurfRect("<=="__FUNCTION__" Dst:\n", (_pDstSurf), (_pDstRect), "\n", true); \
+        } \
+    } while (0)
+
+#define VBOXVDBG_DUMP_SYNC_RT(_pBbSurf) do { \
+        if (g_VBoxVDbgFDumpRtSynch) \
+        { \
+            vboxVDbgDoDumpSurfRect("== "__FUNCTION__" Bb:\n", (_pBbSurf), NULL, "\n", true); \
         } \
     } while (0)
@@ -194,4 +202,5 @@
 #define VBOXVDBG_DUMP_BLT_ENTER(_pSrcSurf, _pSrcRect, _pDstSurf, _pDstRect) do { } while (0)
 #define VBOXVDBG_DUMP_BLT_LEAVE(_pSrcSurf, _pSrcRect, _pDstSurf, _pDstRect) do { } while (0)
+#define VBOXVDBG_DUMP_SYNC_RT(_pBbSurf) do { } while (0)
 #endif
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.cpp	(revision 37733)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.cpp	(revision 37734)
@@ -223,5 +223,5 @@
 }
 
-static void vboxVideoCmSessionDestroy(PVBOXVIDEOCM_SESSION pSession)
+static void vboxVideoCmSessionDestroyLocked(PVBOXVIDEOCM_SESSION pSession)
 {
     /* signal event so that user-space client can figure out the context is destroyed
@@ -238,5 +238,5 @@
  * @return true iff the given session is destroyed
  */
-bool vboxVideoCmSessionCtxRemove(PVBOXVIDEOCM_SESSION pSession, PVBOXVIDEOCM_CTX pContext)
+bool vboxVideoCmSessionCtxRemoveLocked(PVBOXVIDEOCM_SESSION pSession, PVBOXVIDEOCM_CTX pContext)
 {
     bool bDestroy;
@@ -287,5 +287,5 @@
     if (bDestroy)
     {
-        vboxVideoCmSessionDestroy(pSession);
+        vboxVideoCmSessionDestroyLocked(pSession);
     }
 
@@ -294,5 +294,5 @@
 
 /* the session gets destroyed once the last context is removed from it */
-NTSTATUS vboxVideoCmSessionCreate(PVBOXVIDEOCM_MGR pMgr, PVBOXVIDEOCM_SESSION *ppSession, PKEVENT pUmEvent, PVBOXVIDEOCM_CTX pContext)
+NTSTATUS vboxVideoCmSessionCreateLocked(PVBOXVIDEOCM_MGR pMgr, PVBOXVIDEOCM_SESSION *ppSession, PKEVENT pUmEvent, PVBOXVIDEOCM_CTX pContext)
 {
     NTSTATUS Status = STATUS_UNSUCCESSFUL;
@@ -355,5 +355,5 @@
             if (!bFound)
             {
-                Status = vboxVideoCmSessionCreate(pMgr, &pSession, pUmEvent, pContext);
+                Status = vboxVideoCmSessionCreateLocked(pMgr, &pSession, pUmEvent, pContext);
                 Assert(Status == STATUS_SUCCESS);
             }
@@ -390,5 +390,5 @@
     if (Status == STATUS_SUCCESS)
     {
-        vboxVideoCmSessionCtxRemove(pSession, pContext);
+        vboxVideoCmSessionCtxRemoveLocked(pSession, pContext);
         LONG tstL = KeSetEvent(&pMgr->SynchEvent, 0, FALSE);
         Assert(!tstL);
@@ -474,5 +474,6 @@
             else
             {
-                pSession->bEventNeeded = false;
+                Assert(cbRemainingFirstCmd);
+                Assert(cbRemainingCmds);
                 break;
             }
@@ -510,2 +511,92 @@
 }
 
+static BOOLEAN vboxVideoCmHasUncompletedCmdsLocked(PVBOXVIDEOCM_MGR pMgr)
+{
+    PVBOXVIDEOCM_SESSION pSession = NULL;
+    for (PLIST_ENTRY pEntry = pMgr->SessionList.Flink; pEntry != &pMgr->SessionList; pEntry = pEntry->Flink)
+    {
+        pSession = VBOXCMENTRY_2_SESSION(pEntry);
+        ExAcquireFastMutex(&pSession->Mutex);
+        if (pSession->bEventNeeded)
+        {
+            /* commands still being processed */
+            ExReleaseFastMutex(&pSession->Mutex);
+            return TRUE;
+        }
+        ExReleaseFastMutex(&pSession->Mutex);
+    }
+    return FALSE;
+}
+
+/* waits for all outstanding commands to completed by client
+ * assumptions here are:
+ * 1. no new commands are submitted while we are waiting
+ * 2. it is assumed that a client completes all previously received commands
+ *    once it queries for the new set of commands */
+NTSTATUS vboxVideoCmWaitCompletedCmds(PVBOXVIDEOCM_MGR pMgr, uint32_t msTimeout)
+{
+    LARGE_INTEGER Timeout;
+    PLARGE_INTEGER pTimeout;
+    uint32_t cIters;
+
+    if (msTimeout != RT_INDEFINITE_WAIT)
+    {
+        uint32_t msIter = 2;
+        cIters = msTimeout/msIter;
+        if (!cIters)
+        {
+            msIter = msTimeout;
+            cIters = 1;
+        }
+        Timeout.QuadPart = -(int64_t) msIter /* ms */ * 10000;
+        pTimeout = &Timeout;
+    }
+    else
+    {
+        pTimeout = NULL;
+        cIters = 1;
+    }
+
+    Assert(cIters);
+    do
+    {
+        NTSTATUS Status = KeWaitForSingleObject(&pMgr->SynchEvent, Executive, KernelMode,
+                    FALSE, /* BOOLEAN Alertable */
+                    pTimeout /* PLARGE_INTEGER Timeout */
+        );
+        if (Status == STATUS_TIMEOUT)
+        {
+            --cIters;
+        }
+        else
+        {
+            if (!NT_SUCCESS(Status))
+            {
+                WARN(("KeWaitForSingleObject failed with Status (0x%x)", Status));
+                return Status;
+            }
+
+            /* succeeded */
+            if (!vboxVideoCmHasUncompletedCmdsLocked(pMgr))
+            {
+                LONG tstL = KeSetEvent(&pMgr->SynchEvent, 0, FALSE);
+                Assert(!tstL);
+                return STATUS_SUCCESS;
+            }
+
+            LONG tstL = KeSetEvent(&pMgr->SynchEvent, 0, FALSE);
+            Assert(!tstL);
+        }
+
+        if (!cIters)
+            break;
+
+        KeDelayExecutionThread(KernelMode, FALSE, pTimeout);
+        --cIters;
+        if (!cIters)
+            break;
+    } while (0);
+
+    return STATUS_TIMEOUT;
+}
+
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.h	(revision 37733)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.h	(revision 37734)
@@ -56,4 +56,7 @@
 
 NTSTATUS vboxVideoCmEscape(PVBOXVIDEOCM_CTX pContext, PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd);
+
+NTSTATUS vboxVideoCmWaitCompletedCmds(PVBOXVIDEOCM_MGR pMgr, uint32_t msTimeout);
+
 VOID vboxVideoCmLock(PVBOXVIDEOCM_CTX pContext);
 VOID vboxVideoCmUnlock(PVBOXVIDEOCM_CTX pContext);
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.cpp	(revision 37733)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.cpp	(revision 37734)
@@ -910,4 +910,5 @@
     };
     VBOXUHGSMI_SYNCHOBJECT_TYPE enmSynchType;
+    volatile uint32_t cRefs;
     MDL Mdl;
 } VBOXVIDEOCM_ALLOC_REF, *PVBOXVIDEOCM_ALLOC_REF;
@@ -1006,4 +1007,5 @@
                     NULL);
             Assert(Status == STATUS_SUCCESS);
+            Assert(pSynchEvent);
             break;
         case VBOXUHGSMI_SYNCHOBJECT_TYPE_SEMAPHORE:
@@ -1012,4 +1014,5 @@
                     NULL);
             Assert(Status == STATUS_SUCCESS);
+            Assert(pSynchSemaphore);
             break;
         case VBOXUHGSMI_SYNCHOBJECT_TYPE_NONE:
@@ -1032,4 +1035,5 @@
         if (pAllocRef)
         {
+            pAllocRef->cRefs = 1;
             MmInitializeMdl(&pAllocRef->Mdl, BaseVa, cbLength);
             __try
@@ -1105,4 +1109,7 @@
     if (pAllocRef)
     {
+        /* wait for the dereference, i.e. for all commands involving this allocation to complete */
+        vboxWddmCounterU32Wait(&pAllocRef->cRefs, 1);
+
         MmUnlockPages(&pAllocRef->Mdl);
         *ppAlloc = pAllocRef->pAlloc;
@@ -1126,11 +1133,14 @@
     ExAcquireFastMutex(&pContext->Mutex);
     PVBOXVIDEOCM_ALLOC_REF pAllocRef = (PVBOXVIDEOCM_ALLOC_REF)vboxWddmHTableGet(&pContext->AllocTable, hSesionHandle);
+    ASMAtomicIncU32(&pAllocRef->cRefs);
     ExReleaseFastMutex(&pContext->Mutex);
     return pAllocRef;
 }
 
-static VOID vboxVideoCmCtxAllocRefRelease(PVBOXVIDEOCM_ALLOC_REF pRef)
-{
-
+static VOID vboxVideoAMgrCtxAllocRefRelease(PVBOXVIDEOCM_ALLOC_REF pRef)
+{
+    uint32_t cRefs = ASMAtomicDecU32(&pRef->cRefs);
+    Assert(cRefs < UINT32_MAX/2);
+    Assert(cRefs >= 1); /* we do not do cleanup-on-zero here, instead we wait for the cRefs to reach 1 in vboxVideoAMgrCtxAllocUnmap before unmapping */
 }
 
@@ -1211,5 +1221,5 @@
         }
 
-        vboxVideoCmCtxAllocRefRelease(pRef);
+        vboxVideoAMgrCtxAllocRefRelease(pRef);
     }
 
@@ -1243,4 +1253,7 @@
             if (pRef)
             {
+#ifdef DEBUG_misha
+                Assert(pRef->cRefs == 2);
+#endif
                 pBufCmd->offBuffer = pRef->pAlloc->offData + pBufInfo->Info.offData;
                 pBufCmd->cbBuffer = pBufInfo->Info.cbData;
@@ -1250,5 +1263,12 @@
             else
             {
-                Assert(0);
+                WARN(("vboxVideoAMgrCtxAllocRefAcquire failed for hAlloc(0x%x)\n", pBufInfo->hAlloc));
+                /* release all previously acquired aloc references */
+                for (UINT j = 0; j < i; ++j)
+                {
+                    VBOXVDMACMD_CHROMIUM_BUFFER *pBufCmdJ = &pBody->aBuffers[j];
+                    PVBOXVIDEOCM_ALLOC_REF pRefJ = (PVBOXVIDEOCM_ALLOC_REF)pBufCmdJ;
+                    vboxVideoAMgrCtxAllocRefRelease(pRefJ);
+                }
                 Status = STATUS_INVALID_PARAMETER;
                 break;
@@ -1263,8 +1283,18 @@
             vboxVdmaDdiCmdSubmittedNotDx(pDdiCmd);
             int rc = vboxVdmaCBufDrSubmit(pDevExt, &pDevExt->u.primary.Vdma, pDr);
-            Assert(rc == VINF_SUCCESS);
             if (RT_SUCCESS(rc))
             {
                 return STATUS_SUCCESS;
+            }
+
+            WARN(("vboxVdmaCBufDrSubmit failed with rc (%d)\n", rc));
+
+            /* failure branch */
+            /* release all previously acquired aloc references */
+            for (UINT i = 0; i < cBuffers; ++i)
+            {
+                VBOXVDMACMD_CHROMIUM_BUFFER *pBufCmd = &pBody->aBuffers[i];
+                PVBOXVIDEOCM_ALLOC_REF pRef = (PVBOXVIDEOCM_ALLOC_REF)pBufCmd;
+                vboxVideoAMgrCtxAllocRefRelease(pRef);
             }
         }
@@ -1380,4 +1410,13 @@
 }
 
+
+VOID vboxWddmSleep(uint32_t u32Val)
+{
+    LARGE_INTEGER Interval;
+    Interval.QuadPart = -(int64_t) 2 /* ms */ * 10000;
+
+    KeDelayExecutionThread(KernelMode, FALSE, &Interval);
+}
+
 VOID vboxWddmCounterU32Wait(uint32_t volatile * pu32, uint32_t u32Val)
 {
@@ -1386,4 +1425,6 @@
     uint32_t u32CurVal;
 
+    Assert(KeGetCurrentIrql() < DISPATCH_LEVEL);
+
     while ((u32CurVal = ASMAtomicReadU32(pu32)) != u32Val)
     {
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.h	(revision 37733)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.h	(revision 37734)
@@ -148,5 +148,5 @@
 #endif
 
-
+VOID vboxWddmSleep(uint32_t u32Val);
 VOID vboxWddmCounterU32Wait(uint32_t volatile * pu32, uint32_t u32Val);
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.cpp	(revision 37733)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.cpp	(revision 37734)
@@ -1460,6 +1460,5 @@
     LOGF(("ENTER, context(0x%x)", MiniportDeviceContext));
 
-    /* @todo: */
-//    vboxVDbgBreakF();
+    vboxVDbgBreakFv();
 
     LOGF(("LEAVE, context(0x%x)", MiniportDeviceContext));
@@ -2673,4 +2672,5 @@
             PVBOXWDDM_ALLOCATION pDstAlloc = pBlt->Blt.DstAlloc.pAlloc;
             PVBOXWDDM_ALLOCATION pSrcAlloc = pBlt->Blt.SrcAlloc.pAlloc;
+
             uint32_t cContexts3D = ASMAtomicReadU32(&pDevExt->cContexts3D);
             BOOLEAN bComplete = TRUE;
@@ -3723,5 +3723,9 @@
                     /* ensure the last char is \0*/
                     *((uint8_t*)pDbgPrint + pEscape->PrivateDriverDataSize - 1) = '\0';
+#ifdef DEBUG_misha
+                    DbgPrint("%s", pDbgPrint->aStringBuf);
+#else
                     LOGREL(("%s", pDbgPrint->aStringBuf));
+#endif
                 }
                 Status = STATUS_SUCCESS;
