Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp	(revision 37869)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp	(revision 37870)
@@ -186,4 +186,8 @@
     Assert(pAlloc->hAllocation);
 
+    HANDLE hSharedHandle = pAlloc->hSharedHandle;
+
+    vboxVDbgPrint(("SharedHandle: (0x%p)\n", hSharedHandle));
+
     D3DDDICB_LOCK LockData;
     LockData.hAllocation = pAlloc->hAllocation;
@@ -203,5 +207,6 @@
     {
         UINT bpp = vboxWddmCalcBitsPerPixel(pAlloc->SurfDesc.format);
-        vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">surface info</exec>\n",
+        vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">surface info</exec>, ( !vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d )\n",
+                LockData.pData, pAlloc->D3DWidth, pAlloc->SurfDesc.height, bpp, pAlloc->SurfDesc.pitch,
                 LockData.pData, pAlloc->D3DWidth, pAlloc->SurfDesc.height, bpp, pAlloc->SurfDesc.pitch));
         if (pRect)
@@ -210,5 +215,7 @@
             Assert(pRect->bottom > pRect->top);
             vboxVDbgDoPrintRect("rect: ", pRect, "\n");
-            vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">rect info</exec>\n",
+            vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">rect info</exec>, ( !vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d )\n",
+                    ((uint8_t*)LockData.pData) + (pRect->top * pAlloc->SurfDesc.pitch) + ((pRect->left * bpp) >> 3),
+                    pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, pAlloc->SurfDesc.pitch,
                     ((uint8_t*)LockData.pData) + (pRect->top * pAlloc->SurfDesc.pitch) + ((pRect->left * bpp) >> 3),
                     pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, pAlloc->SurfDesc.pitch));
@@ -263,5 +270,6 @@
         {
             UINT bpp = vboxWddmCalcBitsPerPixel((D3DDDIFORMAT)Desc.Format);
-            vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">surface info</exec>\n",
+            vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">surface info</exec>, ( !vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d )\n",
+                    Lr.pBits, Desc.Width, Desc.Height, bpp, Lr.Pitch,
                     Lr.pBits, Desc.Width, Desc.Height, bpp, Lr.Pitch));
             if (pRect)
@@ -270,5 +278,7 @@
                 Assert(pRect->bottom > pRect->top);
                 vboxVDbgDoPrintRect("rect: ", pRect, "\n");
-                vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">rect info</exec>\n",
+                vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">rect info</exec>, ( !vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d )\n",
+                        ((uint8_t*)Lr.pBits) + (pRect->top * Lr.Pitch) + ((pRect->left * bpp) >> 3),
+                        pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, Lr.Pitch,
                         ((uint8_t*)Lr.pBits) + (pRect->top * Lr.Pitch) + ((pRect->left * bpp) >> 3),
                         pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, Lr.Pitch));
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h	(revision 37869)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h	(revision 37870)
@@ -168,5 +168,6 @@
                 ) \
         { \
-            vboxVDbgDoDumpRcRectByRc("== "__FUNCTION__": Texture Dump\n", _pRc, NULL, "\n"); \
+            vboxVDbgPrint(("== "__FUNCTION__": Texture Dump, SharedHandle(0x%p)\n", (_pRc)->aAllocations[0].hSharedHandle)); \
+            vboxVDbgDoDumpRcRectByRc("", _pRc, NULL, "\n"); \
         } \
     } while (0)
@@ -204,6 +205,8 @@
                 ) \
         { \
-            vboxVDbgDoDumpSurfRect("==>"__FUNCTION__" Src:\n", (_pSrcSurf), (_pSrcRect), "\n", true); \
-            vboxVDbgDoDumpSurfRect("==>"__FUNCTION__" Dst:\n", (_pDstSurf), (_pDstRect), "\n", true); \
+            vboxVDbgPrint(("==>"__FUNCTION__" Src: SharedHandle(0x%p)\n", (_pSrcRc)->aAllocations[0].hSharedHandle)); \
+            vboxVDbgDoDumpSurfRect("", (_pSrcSurf), (_pSrcRect), "\n", true); \
+            vboxVDbgPrint(("==>"__FUNCTION__" Dst: SharedHandle(0x%p)\n", (_pDstRc)->aAllocations[0].hSharedHandle)); \
+            vboxVDbgDoDumpSurfRect("", (_pDstSurf), (_pDstRect), "\n", true); \
         } \
     } while (0)
@@ -215,6 +218,8 @@
                 ) \
         { \
-            vboxVDbgDoDumpSurfRect("<=="__FUNCTION__" Src:\n", (_pSrcSurf), (_pSrcRect), "\n", true); \
-            vboxVDbgDoDumpSurfRect("<=="__FUNCTION__" Dst:\n", (_pDstSurf), (_pDstRect), "\n", true); \
+            vboxVDbgPrint(("<=="__FUNCTION__" Src: SharedHandle(0x%p)\n", (_pSrcRc)->aAllocations[0].hSharedHandle)); \
+            vboxVDbgDoDumpSurfRect("", (_pSrcSurf), (_pSrcRect), "\n", true); \
+            vboxVDbgPrint(("<=="__FUNCTION__" Dst: SharedHandle(0x%p)\n", (_pDstRc)->aAllocations[0].hSharedHandle)); \
+            vboxVDbgDoDumpSurfRect("", (_pDstSurf), (_pDstRect), "\n", true); \
         } \
     } while (0)
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/cubetexture.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/cubetexture.c	(revision 37869)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/cubetexture.c	(revision 37870)
@@ -574,5 +574,4 @@
     {
         Assert(shared_handle);
-        VBOXSHRC_SET_INITIALIZED(texture);
         for (i = 0; i < texture->baseTexture.levels; ++i)
         {
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 37869)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 37870)
@@ -46,4 +46,36 @@
 #define GLINFO_LOCATION (*gl_info)
 
+#ifdef VBOX_WITH_WDDM
+void surface_shrc_lock_surf(IWineD3DSurfaceImpl *This)
+{
+    VBOXSHRC_LOCK(This);
+}
+
+void surface_shrc_unlock_surf(IWineD3DSurfaceImpl *This)
+{
+    VBOXSHRC_UNLOCK(This);
+    if (VBOXSHRC_IS_LOCKED(This))
+        return;
+
+    /* perform data->texture synchronization */
+    IWineD3DSurface_LoadLocation((IWineD3DSurface*)This, SFLAG_INTEXTURE, NULL);
+}
+
+void surface_shrc_lock(IWineD3DSurfaceImpl *This)
+{
+    if (!VBOXSHRC_IS_SHARED(This))
+        return;
+
+    surface_shrc_lock_surf(This);
+}
+
+void surface_shrc_unlock(IWineD3DSurfaceImpl *This)
+{
+    if (!VBOXSHRC_IS_SHARED(This))
+        return;
+    surface_shrc_unlock_surf(This);
+}
+#endif
+
 static void surface_cleanup(IWineD3DSurfaceImpl *This)
 {
@@ -410,4 +442,10 @@
     }
 
+#ifdef VBOX_WITH_WDDM
+    /* this will be a nop for the non-shared resource,
+     * for the shared resource this will ensure the surface is initialized properly */
+    surface_shrc_lock(surface);
+#endif
+
     /* "Standalone" surface. */
     IWineD3DSurface_SetContainer((IWineD3DSurface *)surface, NULL);
@@ -487,6 +525,5 @@
     {
         Assert(shared_handle);
-        VBOXSHRC_SET_INITIALIZED(surface);
-        IWineD3DSurface_LoadLocation((IWineD3DSurface*)surface, SFLAG_INTEXTURE, NULL);
+        surface_shrc_unlock(surface);
         if (!VBOXSHRC_IS_SHARED_OPENED(surface))
         {
@@ -514,5 +551,5 @@
 
 #if defined(DEBUG_misha) && defined (VBOX_WITH_WDDM)
-    if (VBOXSHRC_IS_INITIALIZED(This))
+    if (VBOXSHRC_IS_SHARED_UNLOCKED(This))
     {
         Assert(0);
@@ -1803,4 +1840,5 @@
     IWineD3DDeviceImpl *myDevice = This->resource.device;
     const RECT *pass_rect = pRect;
+    HRESULT hr = S_OK;
 
     TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
@@ -1814,4 +1852,9 @@
         return WINED3DERR_INVALIDCALL;
     }
+
+#ifdef VBOX_WITH_WDDM
+    surface_shrc_lock(This);
+#endif
+
     This->Flags |= SFLAG_LOCKED;
 
@@ -1899,5 +1942,14 @@
     }
 
-    return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
+    hr = IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
+#ifdef VBOX_WITH_WDDM
+    if (FAILED(hr))
+    {
+        WARN("IWineD3DBaseSurfaceImpl_LockRect failed, hr (%d)\n", hr);
+        surface_shrc_unlock(This);
+    }
+    /* if lock succeeded, we keep the shrc locked until unlock */
+#endif
+    return hr;
 }
 
@@ -2108,4 +2160,9 @@
         IWineD3DSurface_DrawOverlay(iface);
     }
+
+#ifdef VBOX_WITH_WDDM
+        surface_shrc_unlock(This);
+#endif
+
     return WINED3D_OK;
 }
@@ -4146,4 +4203,5 @@
     IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
     IWineD3DDeviceImpl *myDevice = This->resource.device;
+    HRESULT hr = WINED3D_OK;
 
     TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
@@ -4155,4 +4213,9 @@
         return WINEDDERR_SURFACEBUSY;
     }
+
+#ifdef VBOX_WITH_WDDM
+    surface_shrc_lock(This);
+    surface_shrc_lock(Src);
+#endif
 
     /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
@@ -4162,8 +4225,10 @@
         if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
             TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
-            return WINED3DERR_INVALIDCALL;
+            hr = WINED3DERR_INVALIDCALL;
+            goto end;
         } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
             TRACE("Z Blit override handled the blit\n");
-            return WINED3D_OK;
+            hr = WINED3D_OK;
+            goto end;
         }
     }
@@ -4172,9 +4237,17 @@
     if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
         ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
-        if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
+        if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK)
+        {
+            hr = WINED3D_OK;
+            goto end;
+        }
     }
 
 #ifdef VBOX_WITH_WDDM
-    if (IWineD3DSurfaceImpl_BltSys2Vram(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
+    if (IWineD3DSurfaceImpl_BltSys2Vram(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK)
+    {
+        hr = WINED3D_OK;
+        goto end;
+    }
 #endif
 
@@ -4183,5 +4256,11 @@
      * other Blts are rather rare
      */
-    return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
+    hr = IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
+end:
+#ifdef VBOX_WITH_WDDM
+    surface_shrc_unlock(This);
+    surface_shrc_unlock(Src);
+#endif
+    return hr;
 }
 
@@ -4192,4 +4271,5 @@
     IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
     IWineD3DDeviceImpl *myDevice = This->resource.device;
+    HRESULT hr = WINED3D_OK;
 
     TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
@@ -4207,4 +4287,9 @@
         return WINED3DERR_INVALIDCALL;
     }
+
+#ifdef VBOX_WITH_WDDM
+    surface_shrc_lock(This);
+    surface_shrc_lock(srcImpl);
+#endif
 
     /* Special cases for RenderTargets */
@@ -4232,9 +4317,19 @@
             Flags |= WINEDDBLT_DONOTWAIT;
 
-        if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
-    }
-
-
-    return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
+        if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK)
+        {
+            hr = WINED3D_OK;
+            goto end;
+        }
+    }
+
+
+    hr = IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
+end:
+#ifdef VBOX_WITH_WDDM
+    surface_shrc_unlock(This);
+    surface_shrc_unlock(srcImpl);
+#endif
+    return hr;
 }
 
@@ -4614,5 +4709,5 @@
 
 #ifdef VBOX_WITH_WDDM
-    if(VBOXSHRC_IS_INITIALIZED(This)) {
+    if(VBOXSHRC_IS_SHARED_UNLOCKED(This)) {
         /* with the shared resource only texture can be considered valid
          * to make sure changes done to the resource in the other device context are visible
@@ -4731,5 +4826,9 @@
     if(This->Flags & flag) {
         TRACE("Location already up to date\n");
+#ifdef VBOX_WITH_WDDM
+        goto post_process;
+#else
         return WINED3D_OK;
+#endif
     }
 
@@ -4933,5 +5032,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    if (VBOXSHRC_IS_INITIALIZED(This))
+post_process:
+
+    if (VBOXSHRC_IS_SHARED_UNLOCKED(This))
     {
         /* with the shared resource only texture can be considered valid
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c	(revision 37869)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c	(revision 37870)
@@ -622,5 +622,4 @@
     {
         Assert(shared_handle);
-        VBOXSHRC_SET_INITIALIZED(texture);
         for (i = 0; i < texture->baseTexture.levels; ++i)
         {
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h	(revision 37869)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h	(revision 37870)
@@ -19,10 +19,8 @@
 #define VBOXSHRC_F_SHARED              0x00000001 /* shared rc */
 #define VBOXSHRC_F_SHARED_OPENED       0x00000002 /* if set shared rc is opened, otherwise it is created */
-#define VBOXSHRC_F_INITIALIZED         0x00000004 /* set once shared rc is initialized */
 
 #define VBOXSHRC_GET_SHAREFLAFS(_o) ((_o)->resource.sharerc_flags)
-#define VBOXSHRC_GET_SHAREHANDLE(_o) ((_o)->resource.sharerc_handle)
-#define VBOXSHRC_SET_SHAREHANDLE(_o, _h) ((_o)->resource.sharerc_handle = (_h))
-#define VBOXSHRC_SET_INITIALIZED(_o) (VBOXSHRC_GET_SHAREFLAFS(_o) |= VBOXSHRC_F_INITIALIZED)
+#define VBOXSHRC_GET_SHAREHANDLE(_o) ((HANDLE)(_o)->resource.sharerc_handle)
+#define VBOXSHRC_SET_SHAREHANDLE(_o, _h) ((_o)->resource.sharerc_handle = (DWORD)(_h))
 #define VBOXSHRC_COPY_SHAREDATA(_oDst, _oSrc) do { \
         VBOXSHRC_GET_SHAREFLAFS(_oDst) = VBOXSHRC_GET_SHAREFLAFS(_oSrc); \
@@ -33,6 +31,18 @@
 #define VBOXSHRC_IS_SHARED(_o) (!!(VBOXSHRC_GET_SHAREFLAFS(_o) & VBOXSHRC_F_SHARED))
 #define VBOXSHRC_IS_SHARED_OPENED(_o) (!!(VBOXSHRC_GET_SHAREFLAFS(_o) & VBOXSHRC_F_SHARED_OPENED))
-#define VBOXSHRC_IS_INITIALIZED(_o) (!!(VBOXSHRC_GET_SHAREFLAFS(_o) & VBOXSHRC_F_INITIALIZED))
+#define VBOXSHRC_IS_SHARED_UNLOCKED(_o) (VBOXSHRC_IS_SHARED(_o) && !VBOXSHRC_IS_LOCKED(_o))
 
+#define VBOXSHRC_LOCK(_o) do{ \
+        Assert(VBOXSHRC_IS_SHARED(_o)); \
+        ++(_o)->resource.sharerc_locks; \
+    } while (0)
+#define VBOXSHRC_UNLOCK(_o) do{ \
+        Assert(VBOXSHRC_IS_SHARED(_o)); \
+        --(_o)->resource.sharerc_locks; \
+        Assert((_o)->resource.sharerc_locks < UINT32_MAX/2); \
+    } while (0)
+#define VBOXSHRC_IS_LOCKED(_o) ( \
+        !!((_o)->resource.sharerc_locks) \
+        )
 #ifdef VBOX_WINE_WITH_IPRT
 # include <iprt/assert.h>
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 37869)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 37870)
@@ -1841,5 +1841,6 @@
 #ifdef VBOX_WITH_WDDM
     DWORD                   sharerc_flags; /* shared resource flags */
-    DWORD                   sharerc_handle;
+    DWORD                   sharerc_handle; /* shared resource handle */
+    DWORD                   sharerc_locks; /* lock count */
 #endif
     struct list             privateData;
