Index: /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp	(revision 32576)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp	(revision 32577)
@@ -1747,5 +1747,7 @@
     if (!pSwapchain)
     {
-//        Assert(0);
+#ifdef DEBUG_misha
+        Assert(0);
+#endif
         /* first search for the swapchain the alloc might be added to */
         PVBOXWDDMDISP_SWAPCHAIN pCur = RTListNodeGetFirst(&pDevice->SwapchainList, VBOXWDDMDISP_SWAPCHAIN, ListEntry);
@@ -1768,5 +1770,5 @@
                                     || (pBbRc->fFlags == pRtRc->fFlags
                                             && pBbRc->RcDesc.enmPool == pRtRc->RcDesc.enmPool
-                                            && pBbRc->RcDesc.fFlags.Value == pRtRc->RcDesc.fFlags.Value
+//                                            && pBbRc->RcDesc.fFlags.Value == pRtRc->RcDesc.fFlags.Value
                                         )
                                 ))
@@ -2349,6 +2351,6 @@
         {
             PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[i];
-            pAlloc->SurfDesc.width = 0x400;
-            pAlloc->SurfDesc.height = 0x300;
+            pAlloc->SurfDesc.width = 0x4;
+            pAlloc->SurfDesc.height = 0x4;
             pAlloc->SurfDesc.format = D3DDDIFMT_A8R8G8B8;
         }
@@ -3614,33 +3616,45 @@
     Assert(pSrcRc->RcDesc.enmPool == D3DDDIPOOL_SYSTEMMEM);
     Assert(pDstRc->RcDesc.enmPool != D3DDDIPOOL_SYSTEMMEM);
-    IDirect3DTexture9 *pD3DIfSrcTex = (IDirect3DTexture9*)pSrcRc->aAllocations[0].pD3DIf;
-    IDirect3DTexture9 *pD3DIfDstTex = (IDirect3DTexture9*)pDstRc->aAllocations[0].pD3DIf;
-    Assert(pD3DIfSrcTex);
-    Assert(pD3DIfDstTex);
     HRESULT hr = S_OK;
 
     if (pSrcRc->aAllocations[0].SurfDesc.width == pDstRc->aAllocations[0].SurfDesc.width
             && pSrcRc->aAllocations[0].SurfDesc.height == pDstRc->aAllocations[0].SurfDesc.height
-            && pSrcRc->RcDesc.enmFormat == pDstRc->RcDesc.enmFormat)
-    {
-        /* first check if we can do IDirect3DDevice9::UpdateTexture */
-        if (pData->DstPoint.x == 0 && pData->DstPoint.y == 0
+            && pSrcRc->RcDesc.enmFormat == pDstRc->RcDesc.enmFormat
+                &&pData->DstPoint.x == 0 && pData->DstPoint.y == 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)
-        {
-            hr = pDevice9If->UpdateTexture(pD3DIfSrcTex, pD3DIfDstTex);
+    {
+        IDirect3DTexture9 *pD3DIfSrcTex = (IDirect3DTexture9*)pSrcRc->aAllocations[0].pD3DIf;
+        IDirect3DTexture9 *pD3DIfDstTex = (IDirect3DTexture9*)pDstRc->aAllocations[0].pD3DIf;
+        Assert(pD3DIfSrcTex);
+        Assert(pD3DIfDstTex);
+        hr = pDevice9If->UpdateTexture(pD3DIfSrcTex, pD3DIfDstTex);
+        Assert(hr == S_OK);
+    }
+    else
+    {
+        IDirect3DSurface9 *pSrcSurfIf = NULL;
+        IDirect3DSurface9 *pDstSurfIf = NULL;
+        hr = vboxWddmSurfGet(pDstRc, 0, &pDstSurfIf);
+        Assert(hr == S_OK);
+        if (hr == S_OK)
+        {
+            hr = vboxWddmSurfGet(pSrcRc, 0, &pSrcSurfIf);
             Assert(hr == S_OK);
-        }
-        else
-        {
-            Assert(0);
-            /* @todo: impl */
-        }
-    }
-    else
-    {
-        Assert(0);
-        /* @todo: impl */
+            if (hr == S_OK)
+            {
+                RECT DstRect;
+                vboxWddmRectMoved(&DstRect, &pData->SrcRect, pData->DstPoint.x, pData->DstPoint.y);
+#ifdef DEBUG
+                RECT tstRect = {0,0, pDstRc->aAllocations[0].SurfDesc.width, pDstRc->aAllocations[0].SurfDesc.height};
+                Assert(vboxWddmRectIsCoveres(&tstRect, &DstRect));
+#endif
+                hr = pDevice9If->StretchRect(pSrcSurfIf, &pData->SrcRect, pDstSurfIf, &DstRect, D3DTEXF_NONE);
+                Assert(hr == S_OK);
+                pSrcSurfIf->Release();
+            }
+            pDstSurfIf->Release();
+        }
     }
 
@@ -4761,4 +4775,5 @@
                             if (hr == S_OK)
                             {
+#if 0
                                 if(pResource->Flags.Primary)
                                 {
@@ -4769,4 +4784,5 @@
                                     Assert(bIssueCreateResource);
                                 }
+#endif
                                 continue;
                             }
@@ -6752,5 +6768,5 @@
                     }
 #else
-# define VBOXDISP_TEST_SWAPCHAIN
+//# define VBOXDISP_TEST_SWAPCHAIN
 # ifdef VBOXDISP_TEST_SWAPCHAIN
                     VBOXDISP_D3DEV(pDevice);
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoIf.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoIf.h	(revision 32576)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoIf.h	(revision 32577)
@@ -419,10 +419,10 @@
 }
 
-DECLINLINE(bool) vboxWddmRectIsEmpty(RECT * pRect)
+DECLINLINE(bool) vboxWddmRectIsEmpty(const RECT * pRect)
 {
     return pRect->left == pRect->right-1 && pRect->top == pRect->bottom-1;
 }
 
-DECLINLINE(bool) vboxWddmRectIsIntersect(RECT * pRect1, RECT * pRect2)
+DECLINLINE(bool) vboxWddmRectIsIntersect(const RECT * pRect1, const RECT * pRect2)
 {
     return !((pRect1->left < pRect2->left && pRect1->right < pRect2->left)
@@ -432,10 +432,4 @@
 }
 
-DECLINLINE(bool) vboxWddmRectIsInclude(RECT * pRect1, RECT * pRect2)
-{
-    return ((pRect1->left <= pRect2->left && pRect1->right >= pRect2->right)
-            && (pRect1->top <= pRect2->top && pRect1->bottom >= pRect2->bottom));
-}
-
 DECLINLINE(void) vboxWddmRectUnited(RECT * pDst, const RECT * pRect1, const RECT * pRect2)
 {
@@ -452,4 +446,26 @@
     pRect->right  += x;
     pRect->bottom += y;
+}
+
+DECLINLINE(void) vboxWddmRectMove(RECT * pRect, int x, int y)
+{
+    LONG w = pRect->right - pRect->left;
+    LONG h = pRect->bottom - pRect->top;
+    pRect->left   = x;
+    pRect->top    = y;
+    pRect->right  = w + x;
+    pRect->bottom = h + y;
+}
+
+DECLINLINE(void) vboxWddmRectTranslated(RECT *pDst, const RECT * pRect, int x, int y)
+{
+    *pDst = *pRect;
+    vboxWddmRectTranslate(pDst, x, y);
+}
+
+DECLINLINE(void) vboxWddmRectMoved(RECT *pDst, const RECT * pRect, int x, int y)
+{
+    *pDst = *pRect;
+    vboxWddmRectMove(pDst, x, y);
 }
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c	(revision 32576)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c	(revision 32577)
@@ -855,5 +855,5 @@
     context_validate(context
 #ifdef VBOXWDDM/* tmp work-around */
-            , context->device->swapchains[context->device->NumberOfSwapChains-1]
+            , (IWineD3DSwapChainImpl*)context->device->swapchains[context->device->NumberOfSwapChains-1]
 #endif
             );
@@ -1293,4 +1293,8 @@
     HDC hdc;
 
+#if defined(VBOXWDDM) && defined(DEBUG_misha)
+    Assert(0);
+#endif
+
     TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
 
@@ -1592,9 +1596,64 @@
 #ifdef VBOXWDDM
 static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target);
+static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage);
+
+BOOL context_acquire_context(struct wined3d_context * context, IWineD3DSurface *target, enum ContextUsage usage, BOOL bReValidate)
+{
+    IWineD3DDeviceImpl *device = context->device;
+    struct wined3d_context *current_context = context_get_current();
+    if (bReValidate)
+    {
+        IWineD3DSwapChain *swapchain = NULL;
+        if (target && SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
+            context_validate(current_context, (IWineD3DSwapChainImpl*)swapchain);
+            IWineD3DSwapChain_Release(swapchain);
+        }
+        else {
+                                                                                                    /* tmp work-around */
+            context_validate(current_context, (IWineD3DSwapChainImpl*)current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]);
+        }
+    }
+    context_setup_target(device, context, target);
+    context_enter(context);
+    Assert(context->valid);
+    if (!context->valid) return FALSE;
+
+    if (context != current_context)
+    {
+        if (!context_set_current(context)) ERR("Failed to activate the new context.\n");
+        else device->frag_pipe->enable_extension((IWineD3DDevice *)device, !context->last_was_blit);
+
+        if (context->vshader_const_dirty)
+        {
+            memset(context->vshader_const_dirty, 1,
+                    sizeof(*context->vshader_const_dirty) * device->d3d_vshader_constantF);
+            device->highest_dirty_vs_const = device->d3d_vshader_constantF;
+        }
+        if (context->pshader_const_dirty)
+        {
+            memset(context->pshader_const_dirty, 1,
+                   sizeof(*context->pshader_const_dirty) * device->d3d_pshader_constantF);
+            device->highest_dirty_ps_const = device->d3d_pshader_constantF;
+        }
+    }
+    else if (context->restore_ctx)
+    {
+        if (!pwglMakeCurrent(context->hdc, context->glCtx))
+        {
+            DWORD err = GetLastError();
+            ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
+                    context->hdc, context->glCtx, err);
+        }
+    }
+
+    context_apply_state(context, device, usage);
+
+    return TRUE;
+}
 
 struct wined3d_context *context_find_create(IWineD3DDeviceImpl *device, IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
         const struct wined3d_format_desc *ds_format_desc)
 {
-    int i;
+    UINT i;
     DWORD tid = GetCurrentThreadId();
     struct wined3d_context *context = NULL;
@@ -1619,8 +1678,15 @@
     else
     {
-        context_validate(context, swapchain);
-        context_setup_target(device, context, target);
-        context_enter(context);
-        Assert(context->valid);
+        if(!context_acquire_context(context, (IWineD3DSurface*)target, CTXUSAGE_RESOURCELOAD, TRUE))
+        {
+            ERR("Failed to acquire the context.\n");
+            Assert(0);
+            Assert(!context->valid);
+            context = NULL;
+        }
+        else
+        {
+            Assert(context->valid);
+        }
     }
 
@@ -1970,12 +2036,12 @@
     {
 #ifdef VBOXWDDM
-        IWineD3DSwapChainImpl *swapchain = NULL;
+        IWineD3DSwapChain *swapchain = NULL;
         if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
-            context_validate(current_context, swapchain);
+            context_validate(current_context, (IWineD3DSwapChainImpl*)swapchain);
             IWineD3DSwapChain_Release(swapchain);
         }
         else {
                                                                                                     /* tmp work-around */
-            context_validate(current_context, current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]);
+            context_validate(current_context, (IWineD3DSwapChainImpl*)current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]);
         }
 #else
@@ -1990,5 +2056,5 @@
         context = findThreadContextForSwapChain(swapchain, tid);
 #ifdef VBOXWDDM
-        context_validate(context, swapchain);
+        context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
 #endif
         IWineD3DSwapChain_Release(swapchain);
@@ -2025,5 +2091,5 @@
 #ifdef VBOXWDDM
         context_validate(context,
-                This->swapchains[This->NumberOfSwapChains-1] /* tmp work-around */
+                (IWineD3DSwapChainImpl*)This->swapchains[This->NumberOfSwapChains-1] /* tmp work-around */
                                  );
 #endif
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c	(revision 32576)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c	(revision 32577)
@@ -6863,18 +6863,21 @@
 static HRESULT WINAPI IWineD3DDeviceImpl_Flush(IWineD3DDevice *iface)
 {
-    IWineD3DSwapChain *swapChain = NULL;
+    IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
+    struct wined3d_context *context;
     int i;
-    int swapchains = IWineD3DDeviceImpl_GetNumberOfSwapChains(iface);
-
-    TRACE("iface %p.\n", iface);
-
-    for(i = 0 ; i < swapchains ; i ++) {
-
-        IWineD3DDeviceImpl_GetSwapChain(iface, i, &swapChain);
-        TRACE("presentinng chain %d, %p\n", i, swapChain);
-        IWineD3DSwapChain_Flush(swapChain);
-        IWineD3DSwapChain_Release(swapChain);
-    }
-
+    for (i = 0; i < This->numContexts; ++i)
+    {
+        context = This->contexts[i];
+        if (context_acquire_context(context, NULL, CTXUSAGE_RESOURCELOAD, TRUE))
+        {
+            Assert(context->valid);
+            wglFlush();
+            context_release(context);
+        }
+        else
+        {
+            WARN("Invalid context, skipping flush.\n");
+        }
+    }
     return WINED3D_OK;
 }
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c	(revision 32576)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c	(revision 32577)
@@ -555,25 +555,4 @@
 }
 
-#ifdef VBOXWDDM
-static HRESULT WINAPI IWineD3DSwapChainImpl_Flush(IWineD3DSwapChain *iface)
-{
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    struct wined3d_context *context;
-    RECT src_rect, dst_rect;
-    BOOL render_to_fbo;
-    unsigned int sync;
-    int retval;
-
-    context = context_acquire(This->device, This->backBuffer[0], CTXUSAGE_RESOURCELOAD);
-
-    if (context->valid) wglFlush();
-    else  WARN("Invalid context, skipping flush.\n");
-
-    context_release(context);
-
-    return WINED3D_OK;
-}
-#endif
-
 static const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
 {
@@ -595,7 +574,4 @@
     IWineD3DBaseSwapChainImpl_SetGammaRamp,
     IWineD3DBaseSwapChainImpl_GetGammaRamp,
-#ifdef VBOXWDDM
-    IWineD3DSwapChainImpl_Flush,
-#endif
 };
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain_gdi.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain_gdi.c	(revision 32576)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain_gdi.c	(revision 32577)
@@ -254,13 +254,4 @@
     return WINED3D_OK;
 }
-
-#ifdef VBOXWDDM
-static HRESULT WINAPI IWineGDISwapChainImpl_Flush(IWineD3DSwapChain *iface) {
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
-
-    WARN("GDI Swapchain doesn't implement flush, returning WINED3DERR_INVALIDCALL\n");
-    return WINED3DERR_INVALIDCALL;
-}
-#endif
 
 const IWineD3DSwapChainVtbl IWineGDISwapChain_Vtbl =
@@ -283,6 +274,3 @@
     IWineD3DBaseSwapChainImpl_SetGammaRamp,
     IWineD3DBaseSwapChainImpl_GetGammaRamp,
-#ifdef VBOXWDM
-    IWineGDISwapChainImpl_Flush,
-#endif
 };
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 32576)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 32577)
@@ -1249,4 +1249,5 @@
 struct wined3d_context *context_find_create(IWineD3DDeviceImpl *device, IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
         const struct wined3d_format_desc *ds_format_desc) DECLSPEC_HIDDEN;
+BOOL context_acquire_context(struct wined3d_context * context, IWineD3DSurface *target, enum ContextUsage usage, BOOL bReValidate) DECLSPEC_HIDDEN;
 #endif
 void context_destroy(IWineD3DDeviceImpl *This, struct wined3d_context *context) DECLSPEC_HIDDEN;
