Index: trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp	(revision 30144)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp	(revision 30152)
@@ -2049,4 +2049,5 @@
             Assert(pD3DIfTex);
             RECT *pRect = NULL;
+            bool bNeedResynch = false;
             Assert(!pData->Flags.RangeValid);
             Assert(!pData->Flags.BoxValid);
@@ -2083,5 +2084,5 @@
                     }
 
-                    ++pLockAlloc->LockInfo.cLocks;
+                    bNeedResynch = !pData->Flags.Discard;
                 }
             }
@@ -2101,23 +2102,47 @@
                 }
                 Assert(pLockAlloc->LockInfo.LockedRect.pBits);
-            }
-
-
-            ++pLockAlloc->LockInfo.cLocks;
-
-            if (!pData->Flags.NotifyOnly)
-            {
-                pData->pSurfData = pLockAlloc->LockInfo.LockedRect.pBits;
-                pData->Pitch = pLockAlloc->LockInfo.LockedRect.Pitch;
-                pData->SlicePitch = 0;
-                Assert(pLockAlloc->SurfDesc.slicePitch == 0);
-                Assert(!pLockAlloc->pvMem);
-            }
-            else
-            {
-                Assert(pLockAlloc->pvMem);
-                Assert(pRc->RcDesc.enmPool == D3DDDIPOOL_SYSTEMMEM);
-                if (/* !pData->Flags.WriteOnly && */ !pData->Flags.Discard)
-                    vboxWddmLockUnlockMemSynch(pLockAlloc, &pLockAlloc->LockInfo.LockedRect, pRect, false /*bool bToLockInfo*/);
+
+                bNeedResynch = pLockAlloc->LockInfo.fFlags.Discard && !pData->Flags.Discard;
+
+                Assert(!bNeedResynch);
+
+                if (/*(pLockAlloc->LockInfo.fFlags.Discard && !pData->Flags.Discard)
+                        || */
+                        (pLockAlloc->LockInfo.fFlags.ReadOnly && !pData->Flags.ReadOnly))
+                {
+                    hr = pD3DIfTex->UnlockRect(pData->SubResourceIndex);
+                    Assert(hr == S_OK);
+                    if (hr == S_OK)
+                    {
+                        hr = pD3DIfTex->LockRect(pData->SubResourceIndex,
+                                &pLockAlloc->LockInfo.LockedRect,
+                                pRect,
+                                vboxDDI2D3DLockFlags(pData->Flags));
+                        Assert(hr == S_OK);
+                        pLockAlloc->LockInfo.fFlags.ReadOnly = 0;
+                    }
+                }
+            }
+
+            if (hr == S_OK)
+            {
+                ++pLockAlloc->LockInfo.cLocks;
+
+                if (!pData->Flags.NotifyOnly)
+                {
+                    pData->pSurfData = pLockAlloc->LockInfo.LockedRect.pBits;
+                    pData->Pitch = pLockAlloc->LockInfo.LockedRect.Pitch;
+                    pData->SlicePitch = 0;
+                    Assert(pLockAlloc->SurfDesc.slicePitch == 0);
+                    Assert(!pLockAlloc->pvMem);
+                }
+                else
+                {
+                    Assert(pLockAlloc->pvMem);
+                    Assert(pRc->RcDesc.enmPool == D3DDDIPOOL_SYSTEMMEM);
+
+                    if (bNeedResynch)
+                        vboxWddmLockUnlockMemSynch(pLockAlloc, &pLockAlloc->LockInfo.LockedRect, pRect, false /*bool bToLockInfo*/);
+                }
             }
         }
@@ -2136,7 +2161,7 @@
             }
 
-            /* else - we lock the entire texture, pRect == NULL */
-
-//            Assert(!pLockAlloc->LockInfo.cLocks);
+            /* else - we lock the entire vertex buffer, pRect == NULL */
+
+            Assert(!pAlloc->LockInfo.cLocks);
             if (!pAlloc->LockInfo.cLocks)
             {
@@ -2145,4 +2170,25 @@
                                       &pAlloc->LockInfo.LockedRect.pBits,
                                       vboxDDI2D3DLockFlags(pData->Flags));
+#ifdef VBOXWDDMDISP_WITH_TMPWORKAROUND
+                if (pData->Flags.MightDrawFromLocked)
+                {
+                        RECT r, *pr;
+                        if (pRange)
+                        {
+                            r.top = 0;
+                            r.left = pRange->Offset;
+                            r.bottom = 1;
+                            r.right = pRange->Offset + pRange->Size;
+                            pr = &r;
+                        }
+                        else
+                            pr = NULL;
+
+                        pAlloc->LockInfo.LockedRect.Pitch = pAlloc->SurfDesc.width;
+
+                        vboxWddmLockUnlockMemSynch(pAlloc, &pAlloc->LockInfo.LockedRect, pr, true /*bool bToLockInfo*/);
+                    pD3D9VBuf->Unlock();
+                }
+#endif
                 Assert(hr == S_OK);
                 if (hr == S_OK)
@@ -2162,6 +2208,4 @@
 //                        pAlloc->LockInfo.fFlags.RangeValid = 0;
                     }
-
-                    ++pAlloc->LockInfo.cLocks;
                 }
             }
@@ -2181,33 +2225,35 @@
             }
 
-
-            ++pAlloc->LockInfo.cLocks;
-
-            if (!pData->Flags.NotifyOnly)
-            {
-                pData->pSurfData = pAlloc->LockInfo.LockedRect.pBits;
-                pData->Pitch = pAlloc->LockInfo.LockedRect.Pitch;
-                pData->SlicePitch = 0;
-                Assert(pAlloc->SurfDesc.slicePitch == 0);
-                Assert(!pAlloc->pvMem);
-            }
-            else
-            {
-                Assert(pAlloc->pvMem);
-                Assert(pRc->RcDesc.enmPool == D3DDDIPOOL_SYSTEMMEM);
-                if (/* !pData->Flags.WriteOnly && */ !pData->Flags.Discard)
-                {
-                    RECT r, *pr;
-                    if (pRange)
+            if (hr == S_OK)
+            {
+                ++pAlloc->LockInfo.cLocks;
+
+                if (!pData->Flags.NotifyOnly)
+                {
+                    pData->pSurfData = pAlloc->LockInfo.LockedRect.pBits;
+                    pData->Pitch = pAlloc->LockInfo.LockedRect.Pitch;
+                    pData->SlicePitch = 0;
+                    Assert(pAlloc->SurfDesc.slicePitch == 0);
+                    Assert(!pAlloc->pvMem);
+                }
+                else
+                {
+                    Assert(pAlloc->pvMem);
+                    Assert(pRc->RcDesc.enmPool == D3DDDIPOOL_SYSTEMMEM);
+                    if (/* !pData->Flags.WriteOnly && */ !pData->Flags.Discard)
                     {
-                        r.top = 0;
-                        r.left = pRange->Offset;
-                        r.bottom = 1;
-                        r.right = pRange->Offset + pRange->Size;
-                        pr = &r;
+                        RECT r, *pr;
+                        if (pRange)
+                        {
+                            r.top = 0;
+                            r.left = pRange->Offset;
+                            r.bottom = 1;
+                            r.right = pRange->Offset + pRange->Size;
+                            pr = &r;
+                        }
+                        else
+                            pr = NULL;
+                        vboxWddmLockUnlockMemSynch(pAlloc, &pAlloc->LockInfo.LockedRect, pr, false /*bool bToLockInfo*/);
                     }
-                    else
-                        pr = NULL;
-                    vboxWddmLockUnlockMemSynch(pAlloc, &pAlloc->LockInfo.LockedRect, pr, false /*bool bToLockInfo*/);
                 }
             }
@@ -2322,5 +2368,9 @@
             Assert(pAlloc->LockInfo.cLocks < UINT32_MAX);
 //            pAlloc->LockInfo.cLocks = 0;
+#ifdef VBOXWDDMDISP_WITH_TMPWORKAROUND
+            if (!pAlloc->LockInfo.cLocks && !pAlloc->LockInfo.fFlags.MightDrawFromLocked)
+#else
             if (!pAlloc->LockInfo.cLocks)
+#endif
             {
 //                Assert(!pAlloc->LockInfo.cLocks);
