Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/common/wddm/VBoxMPIf.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/common/wddm/VBoxMPIf.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/common/wddm/VBoxMPIf.h	(revision 38982)
@@ -33,5 +33,5 @@
 
 /* One would increase this whenever definitions in this file are changed */
-#define VBOXVIDEOIF_VERSION 10
+#define VBOXVIDEOIF_VERSION 11
 
 #define VBOXWDDM_NODE_ID_SYSTEM           0
@@ -116,9 +116,4 @@
 } VBOXWDDM_ALLOCINFO, *PVBOXWDDM_ALLOCINFO;
 
-/* this resource is OpenResource'd rather than CreateResource'd */
-#define VBOXWDDM_RESOURCE_F_OPENNED      0x00000001
-/* identifies this is a resource created with CreateResource, the VBOXWDDMDISP_RESOURCE::fRcFlags is valid */
-#define VBOXWDDM_RESOURCE_F_TYPE_GENERIC 0x00000002
-
 typedef struct VBOXWDDM_RC_DESC
 {
@@ -135,7 +130,22 @@
 } VBOXWDDM_RC_DESC, *PVBOXWDDM_RC_DESC;
 
+typedef struct VBOXWDDMDISP_RESOURCE_FLAGS
+{
+    union
+    {
+        struct
+        {
+            UINT Opened     : 1; /* this resource is OpenResource'd rather than CreateResource'd */
+            UINT Generic    : 1; /* identifies this is a resource created with CreateResource, the VBOXWDDMDISP_RESOURCE::fRcFlags is valid */
+            UINT KmResource : 1; /* this resource has underlying km resource */
+            UINT Reserved   : 29; /* reserved */
+        };
+        UINT        Value;
+    };
+} VBOXWDDMDISP_RESOURCE_FLAGS, *PVBOXWDDMDISP_RESOURCE_FLAGS;
+
 typedef struct VBOXWDDM_RCINFO
 {
-    uint32_t fFlags;
+    VBOXWDDMDISP_RESOURCE_FLAGS fFlags;
     VBOXWDDM_RC_DESC RcDesc;
     uint32_t cAllocInfos;
@@ -426,4 +436,10 @@
     VBOXWDDM_UHGSMI_BUFFER_UI_INFO_ESCAPE aBuffers[1];
 } VBOXDISPIFESCAPE_UHGSMI_SUBMIT, *PVBOXDISPIFESCAPE_UHGSMI_SUBMIT;
+
+typedef struct VBOXDISPIFESCAPE_SHRC_REF
+{
+    VBOXDISPIFESCAPE EscapeHdr;
+    uint64_t hAlloc;
+} VBOXDISPIFESCAPE_SHRC_REF, *PVBOXDISPIFESCAPE_SHRC_REF;
 
 /* query info func */
@@ -504,4 +520,11 @@
         case D3DDDIFMT_DXT4:
         case D3DDDIFMT_DXT5:
+        case D3DDDIFMT_VERTEXDATA:
+        case D3DDDIFMT_INDEX16: /* <- yes, dx runtime treats it as such */
+            return 8;
+        case D3DDDIFMT_INDEX32:
+#ifdef DEBUG_misha
+            Assert(0); /* <- test correctness */
+#endif
             return 8;
         default:
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 38982)
@@ -1029,5 +1029,5 @@
 }
 
-#define VBOX_WDDM_SHRC_WO_NOTIFY
+//#define VBOX_WDDM_SHRC_WO_NOTIFY
 static BOOLEAN vboxWddmDalCheckAdd(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_ALLOCATION pAlloc, BOOLEAN fWrite)
 {
@@ -1512,28 +1512,259 @@
 }
 
-static HRESULT vboxWddmSurfSynchMem(PVBOXWDDMDISP_RESOURCE pRc, PVBOXWDDMDISP_ALLOCATION pAllocation)
+static HRESULT vboxWddmLockRect(PVBOXWDDMDISP_RESOURCE pRc, UINT iAlloc,
+        D3DLOCKED_RECT * pLockedRect,
+        CONST RECT *pRect,
+        DWORD fLockFlags)
+{
+    HRESULT hr = E_FAIL;
+    Assert(!pRc->aAllocations[iAlloc].LockInfo.cLocks);
+    Assert(pRc->cAllocations > iAlloc);
+    switch (pRc->aAllocations[0].enmD3DIfType)
+    {
+        case VBOXDISP_D3DIFTYPE_SURFACE:
+        {
+            IDirect3DSurface9 *pD3DIfSurf = (IDirect3DSurface9*)pRc->aAllocations[iAlloc].pD3DIf;
+            Assert(pD3DIfSurf);
+            hr = pD3DIfSurf->LockRect(pLockedRect, pRect, fLockFlags);
+            Assert(hr == S_OK);
+            break;
+        }
+        case VBOXDISP_D3DIFTYPE_TEXTURE:
+        {
+            IDirect3DTexture9 *pD3DIfTex = (IDirect3DTexture9*)pRc->aAllocations[0].pD3DIf;
+            Assert(pD3DIfTex);
+            hr = pD3DIfTex->LockRect(iAlloc, pLockedRect, pRect, fLockFlags);
+            Assert(hr == S_OK);
+            break;
+        }
+        case VBOXDISP_D3DIFTYPE_CUBE_TEXTURE:
+        {
+            IDirect3DCubeTexture9 *pD3DIfCubeTex = (IDirect3DCubeTexture9*)pRc->aAllocations[0].pD3DIf;
+            Assert(pD3DIfCubeTex);
+            hr = pD3DIfCubeTex->LockRect(VBOXDISP_CUBEMAP_INDEX_TO_FACE(pRc, iAlloc),
+                    VBOXDISP_CUBEMAP_INDEX_TO_LEVEL(pRc, iAlloc), pLockedRect, pRect, fLockFlags);
+            Assert(hr == S_OK);
+            break;
+        }
+        case VBOXDISP_D3DIFTYPE_VERTEXBUFFER:
+        {
+            IDirect3DVertexBuffer9 *pD3D9VBuf = (IDirect3DVertexBuffer9*)pRc->aAllocations[iAlloc].pD3DIf;
+            Assert(pD3D9VBuf);
+            hr = pD3D9VBuf->Lock(pRect ? pRect->left : 0/* offset */,
+                    pRect ? pRect->right : 0 /* size 2 lock - 0 means all */,
+                    &pLockedRect->pBits, fLockFlags);
+            Assert(hr == S_OK);
+            pLockedRect->Pitch = pRc->aAllocations[iAlloc].SurfDesc.pitch;
+            break;
+        }
+        case VBOXDISP_D3DIFTYPE_INDEXBUFFER:
+        {
+            IDirect3DIndexBuffer9 *pD3D9IBuf = (IDirect3DIndexBuffer9*)pRc->aAllocations[iAlloc].pD3DIf;
+            Assert(pD3D9IBuf);
+            hr = pD3D9IBuf->Lock(pRect ? pRect->left : 0/* offset */,
+                    pRect ? pRect->right : 0 /* size 2 lock - 0 means all */,
+                    &pLockedRect->pBits, fLockFlags);
+            Assert(hr == S_OK);
+            pLockedRect->Pitch = pRc->aAllocations[iAlloc].SurfDesc.pitch;
+            break;
+        }
+        default:
+            Assert(0);
+            break;
+    }
+    return hr;
+}
+
+static HRESULT vboxWddmUnlockRect(PVBOXWDDMDISP_RESOURCE pRc, UINT iAlloc)
 {
     HRESULT hr = S_OK;
-    Assert(pAllocation->enmD3DIfType == VBOXDISP_D3DIFTYPE_SURFACE);
-    if (pRc->RcDesc.enmPool == D3DDDIPOOL_SYSTEMMEM)
-    {
-        Assert(pAllocation->pvMem);
-        D3DLOCKED_RECT lockInfo;
-        IDirect3DSurface9 *pD3D9Surf = (IDirect3DSurface9*)pAllocation->pD3DIf;
-        hr = pD3D9Surf->LockRect(&lockInfo, NULL, D3DLOCK_DISCARD);
-        Assert(hr == S_OK);
-        if (hr == S_OK)
-        {
-            vboxWddmLockUnlockMemSynch(pAllocation, &lockInfo, NULL, true /*bool bToLockInfo*/);
-            HRESULT tmpHr = pD3D9Surf->UnlockRect();
-            Assert(tmpHr == S_OK);
-        }
+    Assert(pRc->cAllocations > iAlloc);
+    switch (pRc->aAllocations[0].enmD3DIfType)
+    {
+        case VBOXDISP_D3DIFTYPE_SURFACE:
+        {
+            IDirect3DSurface9 *pD3DIfSurf = (IDirect3DSurface9*)pRc->aAllocations[iAlloc].pD3DIf;
+            Assert(pD3DIfSurf);
+            hr = pD3DIfSurf->UnlockRect();
+            Assert(hr == S_OK);
+            break;
+        }
+        case VBOXDISP_D3DIFTYPE_TEXTURE:
+        {
+            IDirect3DTexture9 *pD3DIfTex = (IDirect3DTexture9*)pRc->aAllocations[0].pD3DIf;
+            Assert(pD3DIfTex);
+            hr = pD3DIfTex->UnlockRect(iAlloc);
+            Assert(hr == S_OK);
+            break;
+        }
+        case VBOXDISP_D3DIFTYPE_CUBE_TEXTURE:
+        {
+            IDirect3DCubeTexture9 *pD3DIfCubeTex = (IDirect3DCubeTexture9*)pRc->aAllocations[0].pD3DIf;
+            Assert(pD3DIfCubeTex);
+            hr = pD3DIfCubeTex->UnlockRect(VBOXDISP_CUBEMAP_INDEX_TO_FACE(pRc, iAlloc),
+                    VBOXDISP_CUBEMAP_INDEX_TO_LEVEL(pRc, iAlloc));
+            Assert(hr == S_OK);
+            break;
+        }
+        case VBOXDISP_D3DIFTYPE_VERTEXBUFFER:
+        {
+            IDirect3DVertexBuffer9 *pD3D9VBuf = (IDirect3DVertexBuffer9*)pRc->aAllocations[iAlloc].pD3DIf;
+            Assert(pD3D9VBuf);
+            hr = pD3D9VBuf->Unlock();
+            Assert(hr == S_OK);
+            break;
+        }
+        case VBOXDISP_D3DIFTYPE_INDEXBUFFER:
+        {
+            IDirect3DIndexBuffer9 *pD3D9IBuf = (IDirect3DIndexBuffer9*)pRc->aAllocations[iAlloc].pD3DIf;
+            Assert(pD3D9IBuf);
+            hr = pD3D9IBuf->Unlock();
+            Assert(hr == S_OK);
+            break;
+        }
+        default:
+            Assert(0);
+            hr = E_FAIL;
+            break;
+    }
+    return hr;
+}
+
+static HRESULT vboxWddmMemsetRc(PVBOXWDDMDISP_RESOURCE pRc, char c)
+{
+    for (UINT i = 0; i < pRc->cAllocations; ++i)
+    {
+        D3DLOCKED_RECT Rect;
+        HRESULT hr = vboxWddmLockRect(pRc, i, &Rect, NULL, D3DLOCK_DISCARD);
+        if (FAILED(hr))
+        {
+            WARN(("vboxWddmLockRect failed, hr(0x%x)", hr));
+            return hr;
+        }
+
+        PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[i];
+        UINT cbAllocPitch = pAlloc->SurfDesc.pitch;
+        if(Rect.Pitch == cbAllocPitch)
+        {
+            memset(Rect.pBits, 0, Rect.Pitch * pAlloc->SurfDesc.height);
+        }
+        else
+        {
+            Assert(0);
+            Assert(cbAllocPitch < (UINT)Rect.Pitch);
+            uint8_t *pData = (uint8_t*)Rect.pBits;
+            for (UINT j = 0; j < pAlloc->SurfDesc.height; ++j)
+            {
+                memset(Rect.pBits, c, cbAllocPitch);
+                pData += Rect.Pitch;
+            }
+        }
+
+        hr = vboxWddmUnlockRect(pRc, i);
+        Assert(SUCCEEDED(hr));
+    }
+    return S_OK;
+}
+
+static HRESULT vboxWddmSurfSynchMem(PVBOXWDDMDISP_RESOURCE pRc)
+{
+    if (pRc->RcDesc.enmPool != D3DDDIPOOL_SYSTEMMEM)
+    {
+        return S_OK;
+    }
+
+    for (UINT i = 0; i < pRc->cAllocations; ++i)
+    {
+        D3DLOCKED_RECT Rect;
+        HRESULT hr = vboxWddmLockRect(pRc, i, &Rect, NULL, D3DLOCK_DISCARD);
+        if (FAILED(hr))
+        {
+            WARN(("vboxWddmLockRect failed, hr(0x%x)", hr));
+            return hr;
+        }
+
+        PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[i];
+        Assert(pAlloc->pvMem);
+
+        vboxWddmLockUnlockMemSynch(pAlloc, &Rect, NULL, true /*bool bToLockInfo*/);
+
+        hr = vboxWddmUnlockRect(pRc, i);
+        Assert(SUCCEEDED(hr));
+    }
+    return S_OK;
+}
+
+#ifdef VBOXWDDMDISP_DEBUG
+static void vboxWddmDbgSynchMemCheck(PVBOXWDDMDISP_ALLOCATION pAlloc, D3DLOCKED_RECT *pLockInfo)
+{
+    Assert(pAlloc->SurfDesc.pitch);
+    Assert(pAlloc->pvMem);
+    int iRc = 0;
+
+    if (pAlloc->SurfDesc.pitch == pLockInfo->Pitch)
+    {
+        iRc = memcmp(pLockInfo->pBits, pAlloc->pvMem, pAlloc->SurfDesc.pitch * pAlloc->SurfDesc.height);
+        Assert(!iRc);
     }
     else
     {
-        Assert(!pAllocation->pvMem);
-    }
-    return hr;
-}
+        uint8_t *pvSrc, *pvDst;
+        uint32_t srcPitch, dstPitch;
+        if (1)
+        {
+            pvSrc = (uint8_t *)pAlloc->pvMem;
+            pvDst = (uint8_t *)pLockInfo->pBits;
+            srcPitch = pAlloc->SurfDesc.pitch;
+            dstPitch = pLockInfo->Pitch;
+        }
+        else
+        {
+            pvDst = (uint8_t *)pAlloc->pvMem;
+            pvSrc = (uint8_t *)pLockInfo->pBits;
+            dstPitch = pAlloc->SurfDesc.pitch;
+            srcPitch = (uint32_t)pLockInfo->Pitch;
+        }
+
+        Assert(pAlloc->SurfDesc.pitch <= (UINT)pLockInfo->Pitch);
+        uint32_t pitch = RT_MIN(srcPitch, dstPitch);
+        Assert(pitch);
+        for (UINT j = 0; j < pAlloc->SurfDesc.height; ++j)
+        {
+            iRc = memcmp(pvDst, pvSrc, pitch);
+            Assert(!iRc);
+            pvSrc += srcPitch;
+            pvDst += dstPitch;
+        }
+    }
+}
+
+static VOID vboxWddmDbgRcSynchMemCheck(PVBOXWDDMDISP_RESOURCE pRc)
+{
+    if (pRc->RcDesc.enmPool != D3DDDIPOOL_SYSTEMMEM)
+    {
+        return;
+    }
+
+    for (UINT i = 0; i < pRc->cAllocations; ++i)
+    {
+        D3DLOCKED_RECT Rect;
+        HRESULT hr = vboxWddmLockRect(pRc, i, &Rect, NULL, D3DLOCK_READONLY);
+        if (FAILED(hr))
+        {
+            WARN(("vboxWddmLockRect failed, hr(0x%x)", hr));
+            return;
+        }
+
+        PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[i];
+        Assert(pAlloc->pvMem);
+
+        vboxWddmDbgSynchMemCheck(pAlloc, &Rect);
+
+        hr = vboxWddmUnlockRect(pRc, i);
+        Assert(SUCCEEDED(hr));
+    }
+}
+#endif
+
 
 static D3DFORMAT vboxDDI2D3DFormat(D3DDDIFORMAT format)
@@ -2077,4 +2308,5 @@
                             && pBbAlloc->SurfDesc.format == pRt->pAlloc->SurfDesc.format
                             && pBbAlloc->SurfDesc.VidPnSourceId == pRt->pAlloc->SurfDesc.VidPnSourceId
+#if 0
                             && (pBbRc == pRtRc
                                     || (pBbRc->fFlags == pRtRc->fFlags
@@ -2082,5 +2314,8 @@
 //                                            && pBbRc->RcDesc.fFlags.Value == pRtRc->RcDesc.fFlags.Value
                                         )
-                                ))
+
+                                )
+#endif
+                            )
                 {
                     vboxWddmSwapchainBbAddTail(pCur, pBbAlloc, TRUE);
@@ -2462,4 +2697,71 @@
     return hr;
 }
+
+static HRESULT vboxWddmShRcRefAlloc(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_ALLOCATION pAlloc, BOOL fAddRef, DWORD *pcRefs)
+{
+    D3DDDICB_ESCAPE DdiEscape = {0};
+    VBOXDISPIFESCAPE_SHRC_REF Data = {0};
+    DdiEscape.hContext = pDevice->DefaultContext.ContextInfo.hContext;
+    DdiEscape.hDevice = pDevice->hDevice;
+    DdiEscape.Flags.HardwareAccess = 1;
+    DdiEscape.pPrivateDriverData = &Data;
+    DdiEscape.PrivateDriverDataSize = sizeof (Data);
+    Data.EscapeHdr.escapeCode = fAddRef ? VBOXESC_SHRC_ADDREF : VBOXESC_SHRC_RELEASE;
+    Data.hAlloc = (uint64_t)pAlloc->hAllocation;
+    HRESULT hr = pDevice->RtCallbacks.pfnEscapeCb(pDevice->pAdapter->hAdapter, &DdiEscape);
+    if (FAILED(hr))
+    {
+        WARN(("pfnEscapeCb, hr (0x%x)", hr));
+        return TRUE;
+    }
+
+    LOG(("shrc(0x%p) refs(%d)", (void*)pAlloc->hSharedHandle, Data.EscapeHdr.u32CmdSpecific));
+    if (pcRefs)
+        *pcRefs = Data.EscapeHdr.u32CmdSpecific;
+
+    return hr;
+}
+
+static HRESULT vboxWddmShRcRefRc(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_RESOURCE pRc, BOOL fAddRef, DWORD *pcRefs)
+{
+    Assert(pRc->RcDesc.fFlags.SharedResource);
+    DWORD cTotalRefs = 0;
+    HRESULT hr = S_OK;
+    for (DWORD i = 0; i < pRc->cAllocations; ++i)
+    {
+        DWORD cRefs = 0;
+        PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[i];
+        if(!pAlloc->hSharedHandle)
+            continue;
+
+        hr = vboxWddmShRcRefAlloc(pDevice, pAlloc, fAddRef, &cRefs);
+
+        if (FAILED(hr))
+        {
+            WARN(("vboxWddmShRcRefAlloc failed, hr()0x%x", hr));
+            for (DWORD j = 0; j < i; ++j)
+            {
+                PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[i];
+                if(!pAlloc->hSharedHandle)
+                    continue;
+                HRESULT tmpHr = vboxWddmShRcRefAlloc(pDevice, pAlloc, !fAddRef, NULL);
+                Assert(SUCCEEDED(tmpHr));
+            }
+            return hr;
+        }
+
+        /* success! */
+        cTotalRefs += cRefs;
+    }
+
+    Assert(cTotalRefs || !fAddRef);
+
+    /* success! */
+    if (pcRefs)
+        *pcRefs = cTotalRefs;
+
+    return S_OK;
+}
+
 
 static HRESULT vboxWddmSwapchainChkCreateIf(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_SWAPCHAIN pSwapchain)
@@ -3494,4 +3796,5 @@
     if (pRc)
     {
+        VBOXVDBG_CHECK_SMSYNC(pRc);
         if (pRc->aAllocations[0].enmD3DIfType == VBOXDISP_D3DIFTYPE_TEXTURE)
         {
@@ -3591,4 +3894,5 @@
     if (pRc)
     {
+        VBOXVDBG_CHECK_SMSYNC(pRc);
         Assert(pRc->cAllocations == 1);
         pAlloc = &pRc->aAllocations[0];
@@ -3925,4 +4229,6 @@
     Assert(pDstRc->RcDesc.enmPool != D3DDDIPOOL_SYSTEMMEM);
     HRESULT hr = S_OK;
+    VBOXVDBG_CHECK_SMSYNC(pDstRc);
+    VBOXVDBG_CHECK_SMSYNC(pSrcRc);
 
     VBOXVDBG_DUMP_TEXBLT_ENTER(pSrcRc, &pData->SrcRect, pDstRc, &pData->DstPoint);
@@ -4269,4 +4575,5 @@
             if (!pLockAlloc->LockInfo.cLocks)
             {
+                VBOXVDBG_CHECK_SMSYNC(pRc);
                 switch (pTexAlloc->enmD3DIfType)
                 {
@@ -4417,4 +4724,5 @@
             if (!pAlloc->LockInfo.cLocks)
             {
+                VBOXVDBG_CHECK_SMSYNC(pRc);
                 if (!pData->Flags.MightDrawFromLocked || (!pData->Flags.Discard && !pData->Flags.NoOverwrite))
                 {
@@ -4509,4 +4817,5 @@
             if (!pAlloc->LockInfo.cLocks)
             {
+                VBOXVDBG_CHECK_SMSYNC(pRc);
                 if (!pData->Flags.MightDrawFromLocked || (!pData->Flags.Discard && !pData->Flags.NoOverwrite))
                 {
@@ -4589,4 +4898,7 @@
     else
     {
+#ifdef DEBUG_misha
+        Assert(0);
+#endif
         PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[pData->SubResourceIndex];
         D3DDDICB_LOCK LockData;
@@ -4715,4 +5027,5 @@
                 }
                 Assert(hr == S_OK);
+                VBOXVDBG_CHECK_SMSYNC(pRc);
             }
             else
@@ -4755,4 +5068,5 @@
                 hr = pD3D9VBuf->Unlock();
                 Assert(hr == S_OK);
+                VBOXVDBG_CHECK_SMSYNC(pRc);
             }
             else
@@ -4795,4 +5109,5 @@
                 hr = pD3D9IBuf->Unlock();
                 Assert(hr == S_OK);
+                VBOXVDBG_CHECK_SMSYNC(pRc);
             }
             else
@@ -4931,5 +5246,6 @@
         pRc->hKMResource = NULL;
         pRc->pDevice = pDevice;
-        pRc->fFlags = VBOXWDDM_RESOURCE_F_TYPE_GENERIC;
+        pRc->fFlags.Value = 0;
+        pRc->fFlags.Generic = 1;
         pRc->RcDesc.fFlags = pResource->Flags;
         pRc->RcDesc.enmFormat = pResource->Format;
@@ -4969,5 +5285,4 @@
             pAllocation->D3DWidth = pSurf->Width;
             pAllocation->pvMem = (void*)pSurf->pSysMem;
-            pAllocation->SurfDesc.pitch = pSurf->SysMemPitch;
             pAllocation->SurfDesc.slicePitch = pSurf->SysMemSlicePitch;
             pAllocation->SurfDesc.depth = pSurf->Depth;
@@ -4975,4 +5290,15 @@
             pAllocation->SurfDesc.height = pSurf->Height;
             pAllocation->SurfDesc.format = pResource->Format;
+            if (!vboxWddmFormatToFourcc(pResource->Format))
+                pAllocation->SurfDesc.bpp = vboxWddmCalcBitsPerPixel(pResource->Format);
+            else
+                pAllocation->SurfDesc.bpp = 0;
+
+            if (pSurf->SysMemPitch)
+            {
+                pAllocation->SurfDesc.pitch = pSurf->SysMemPitch;
+            }
+            else
+                pAllocation->SurfDesc.pitch = vboxWddmCalcPitch(pSurf->Width, pAllocation->SurfDesc.bpp);
         }
 
@@ -5006,6 +5332,4 @@
                         pAllocation->enmD3DIfType = VBOXDISP_D3DIFTYPE_SURFACE;
                         pAllocation->pD3DIf = pD3D9Surf;
-                        hr = vboxWddmSurfSynchMem(pRc, pAllocation);
-                        Assert(hr == S_OK);
                     }
                     else
@@ -5016,4 +5340,16 @@
                         }
                         break;
+                    }
+                }
+
+                if (SUCCEEDED(hr))
+                {
+                    if (pResource->Pool != D3DDDIPOOL_SYSTEMMEM)
+                    {
+                        vboxWddmMemsetRc(pRc, 0);
+                    }
+                    else
+                    {
+                        vboxWddmSurfSynchMem(pRc);
                     }
                 }
@@ -5039,22 +5375,4 @@
                         pAllocation->enmD3DIfType = VBOXDISP_D3DIFTYPE_VERTEXBUFFER;
                         pAllocation->pD3DIf = pD3D9VBuf;
-                        if (pResource->Pool == D3DDDIPOOL_SYSTEMMEM)
-                        {
-                            Assert(pAllocation->pvMem);
-                            D3DLOCKED_RECT lockInfo;
-                            hr = pD3D9VBuf->Lock(0, pAllocation->SurfDesc.width, &lockInfo.pBits, D3DLOCK_DISCARD);
-                            Assert(hr == S_OK);
-                            if (hr == S_OK)
-                            {
-                                lockInfo.Pitch = pAllocation->SurfDesc.pitch;
-                                vboxWddmLockUnlockMemSynch(pAllocation, &lockInfo, NULL, true /*bool bToLockInfo*/);
-                                HRESULT tmpHr = pD3D9VBuf->Unlock();
-                                Assert(tmpHr == S_OK);
-                            }
-                        }
-                        else
-                        {
-                            Assert(!pAllocation->pvMem);
-                        }
                     }
                     else
@@ -5065,4 +5383,16 @@
                         }
                         break;
+                    }
+                }
+
+                if (SUCCEEDED(hr))
+                {
+                    if (pResource->Pool != D3DDDIPOOL_SYSTEMMEM)
+                    {
+                        vboxWddmMemsetRc(pRc, 0);
+                    }
+                    else
+                    {
+                        vboxWddmSurfSynchMem(pRc);
                     }
                 }
@@ -5090,22 +5420,4 @@
                         pAllocation->enmD3DIfType = VBOXDISP_D3DIFTYPE_INDEXBUFFER;
                         pAllocation->pD3DIf = pD3D9IBuf;
-                        if (pResource->Pool == D3DDDIPOOL_SYSTEMMEM)
-                        {
-                            Assert(pAllocation->pvMem);
-                            D3DLOCKED_RECT lockInfo;
-                            hr = pD3D9IBuf->Lock(0, pAllocation->SurfDesc.width, &lockInfo.pBits, D3DLOCK_DISCARD);
-                            Assert(hr == S_OK);
-                            if (hr == S_OK)
-                            {
-                                lockInfo.Pitch = pAllocation->SurfDesc.pitch;
-                                vboxWddmLockUnlockMemSynch(pAllocation, &lockInfo, NULL, true /*bool bToLockInfo*/);
-                                HRESULT tmpHr = pD3D9IBuf->Unlock();
-                                Assert(tmpHr == S_OK);
-                            }
-                        }
-                        else
-                        {
-                            Assert(!pAllocation->pvMem);
-                        }
                     }
                     else
@@ -5116,4 +5428,16 @@
                         }
                         break;
+                    }
+                }
+
+                if (SUCCEEDED(hr))
+                {
+                    if (pResource->Pool != D3DDDIPOOL_SYSTEMMEM)
+                    {
+                        vboxWddmMemsetRc(pRc, 0);
+                    }
+                    else
+                    {
+                        vboxWddmSurfSynchMem(pRc);
                     }
                 }
@@ -5206,11 +5530,10 @@
 #endif
                             pAllocation->hSharedHandle = hSharedHandle;
-#ifdef DEBUG_misha
-                            if (pResource->Flags.SharedResource)
+
+                            if (!pavClientMem)
                             {
-                                vboxVDbgPrint(("\n\n********\nShared Texture pAlloc(0x%p) Handle(0x%x), (0n%d) CREATED, pid (0x%x), (0n%d)\n***********\n\n",
-                                        pAllocation, hSharedHandle, hSharedHandle, GetCurrentProcessId(), GetCurrentProcessId()));
+                                /* zero-init texture memory */
+
                             }
-#endif
                         }
 
@@ -5283,4 +5606,16 @@
                     }
                 }
+
+                if (SUCCEEDED(hr))
+                {
+                    if (pResource->Pool != D3DDDIPOOL_SYSTEMMEM)
+                    {
+                        vboxWddmMemsetRc(pRc, 0);
+                    }
+                    else
+                    {
+                        vboxWddmSurfSynchMem(pRc);
+                    }
+                }
             }
             else if (pResource->Flags.RenderTarget)
@@ -5330,10 +5665,5 @@
 #endif
                             pAllocation->hSharedHandle = hSharedHandle;
-                            hr = vboxWddmSurfSynchMem(pRc, pAllocation);
-                            Assert(hr == S_OK);
-                            if (hr == S_OK)
-                            {
-                                continue;
-                            }
+                            continue;
 
                             /* fail branch */
@@ -5347,4 +5677,17 @@
                         break;
                     }
+
+                    if (SUCCEEDED(hr))
+                    {
+                        if (pResource->Pool != D3DDDIPOOL_SYSTEMMEM)
+                        {
+                            vboxWddmMemsetRc(pRc, 0);
+                        }
+                        else
+                        {
+                            Assert(0);
+                            vboxWddmSurfSynchMem(pRc);
+                        }
+                    }
                 }
             }
@@ -5364,4 +5707,5 @@
         if (hr == S_OK && bIssueCreateResource)
         {
+            pRc->fFlags.KmResource = bCreateKMResource;
             D3DDDICB_ALLOCATE *pDdiAllocate = vboxWddmRequestAllocAlloc(pResource);
             Assert(pDdiAllocate);
@@ -5371,5 +5715,5 @@
                 Assert(pDdiAllocate->PrivateDriverDataSize == sizeof (VBOXWDDM_RCINFO));
                 PVBOXWDDM_RCINFO pRcInfo = (PVBOXWDDM_RCINFO)pDdiAllocate->pPrivateDriverData;
-                pRcInfo->fFlags = VBOXWDDM_RESOURCE_F_TYPE_GENERIC;
+                pRcInfo->fFlags = pRc->fFlags;
                 pRcInfo->RcDesc = pRc->RcDesc;
                 pRcInfo->cAllocInfos = pResource->SurfCount;
@@ -5423,12 +5767,23 @@
                 }
 
+                Assert(!pRc->fFlags.Opened);
+//                Assert(!pRc->fFlags.KmResource);
+                Assert(pRc->fFlags.Generic);
+
                 if (bCreateKMResource)
                 {
+                    Assert(pRc->fFlags.KmResource);
+
                     hr = pDevice->RtCallbacks.pfnAllocateCb(pDevice->hDevice, pDdiAllocate);
                     Assert(hr == S_OK);
-                    Assert(pDdiAllocate->hKMResource);
+                    Assert(pDdiAllocate->hKMResource
+                            || pResource->Flags.SharedResource /* for some reason shared resources
+                                                                * are created with zero km resource handle on Win7+ */
+                            );
                 }
                 else
                 {
+                    Assert(!pRc->fFlags.KmResource);
+
                     pDdiAllocate->hResource = NULL;
                     pDdiAllocate->NumAllocations = 1;
@@ -5480,4 +5835,21 @@
                         pAllocation->pvMem = (void*)pSurf->pSysMem;
                         pAllocation->SurfDesc = pAllocInfo->SurfDesc;
+
+                        if (pResource->Flags.SharedResource)
+                        {
+                            if (pAllocation->hSharedHandle)
+                            {
+                                vboxWddmShRcRefAlloc(pDevice, pAllocation, TRUE, NULL);
+                            }
+#ifdef DEBUG_misha
+                            Assert(VBOXWDDMDISP_IS_TEXTURE(pResource->Flags));
+                            vboxVDbgPrint(("\n\n********\n(0x%x:0n%d)Shared CREATED pAlloc(0x%p), hRc(0x%p), hAl(0x%p), "
+                                            "Handle(0x%x), (0n%d) \n***********\n\n",
+                                        GetCurrentProcessId(), GetCurrentProcessId(),
+                                        pAllocation, pRc->hKMResource, pAllocation->hAllocation,
+                                        pAllocation->hSharedHandle, pAllocation->hSharedHandle
+                                        ));
+#endif
+                        }
                     }
 
@@ -5532,16 +5904,32 @@
         for (UINT i = 0; i < pRc->cAllocations; ++i)
         {
+            BOOL fSetDontDelete = FALSE;
             PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[i];
+            if (pAlloc->hSharedHandle)
+            {
+                DWORD cShRcRefs;
+                HRESULT tmpHr = vboxWddmShRcRefAlloc(pDevice, pAlloc, FALSE, &cShRcRefs);
+                if (cShRcRefs)
+                {
+                    fSetDontDelete = TRUE;
+                }
 #ifdef DEBUG_misha
-            if (pAlloc->hSharedHandle)
-            {
-                vboxVDbgPrint(("\n\n********\nShared Texture pAlloc(0x%p) Handle(0x%x), (0n%d) DESTROYED, pid (0x%x), (0n%d)\n***********\n\n",
-                        pAlloc, pAlloc->hSharedHandle, pAlloc->hSharedHandle, GetCurrentProcessId(), GetCurrentProcessId()));
-            }
-#endif
+                vboxVDbgPrint(("\n\n********\n(0x%x:0n%d)Shared DESTROYED pAlloc(0x%p), hRc(0x%p), hAl(0x%p), "
+                                "Handle(0x%x), (0n%d) \n***********\n\n",
+                            GetCurrentProcessId(), GetCurrentProcessId(),
+                            pAlloc, pRc->hKMResource, pAlloc->hAllocation,
+                            pAlloc->hSharedHandle, pAlloc->hSharedHandle
+                            ));
+#endif
+            }
+
+            if (fSetDontDelete)
+            {
+                Assert(pAlloc->pD3DIf);
+                pAdapter->D3D.pfnVBoxWineExD3DRc9SetDontDeleteGl((IDirect3DResource9*)pAlloc->pD3DIf);
+            }
+
             if (pAlloc->pD3DIf)
                 pAlloc->pD3DIf->Release();
-            if (pAlloc->pSecondaryOpenedD3DIf)
-                pAlloc->pSecondaryOpenedD3DIf->Release();
 
             PVBOXWDDMDISP_SWAPCHAIN pSwapchain = vboxWddmSwapchainForAlloc(pAlloc);
@@ -5560,23 +5948,18 @@
 
     Assert(pRc->hKMResource || VBOXDISPMODE_IS_3D(pAdapter));
-    if (pRc->hKMResource)
-    {
-        if (!(pRc->fFlags & VBOXWDDM_RESOURCE_F_OPENNED))
-        {
-            D3DDDICB_DEALLOCATE Dealloc;
-
-            Assert(pRc->hResource);
-
-            Dealloc.hResource = pRc->hResource;
-            /* according to the docs the below two are ignored in case we set the hResource */
-            Dealloc.NumAllocations = 0;
-            Dealloc.HandleList = NULL;
-            hr = pDevice->RtCallbacks.pfnDeallocateCb(pDevice->hDevice, &Dealloc);
-            Assert(hr == S_OK);
-        }
+    if (pRc->fFlags.KmResource)
+    {
+        D3DDDICB_DEALLOCATE Dealloc;
+        Assert(pRc->hResource);
+        Dealloc.hResource = pRc->hResource;
+        /* according to the docs the below two are ignored in case we set the hResource */
+        Dealloc.NumAllocations = 0;
+        Dealloc.HandleList = NULL;
+        hr = pDevice->RtCallbacks.pfnDeallocateCb(pDevice->hDevice, &Dealloc);
+        Assert(hr == S_OK);
     }
     else
     {
-        Assert(!(pRc->fFlags & VBOXWDDM_RESOURCE_F_OPENNED));
+        Assert(!(pRc->fFlags.Opened));
         for (UINT j = 0; j < pRc->cAllocations; ++j)
         {
@@ -5622,11 +6005,4 @@
 //    Assert(pScreen->iRenderTargetFrontBuf == pData->SubResourceIndex);
 
-#if 0
-    Assert(pAlloc->enmD3DIfType == VBOXDISP_D3DIFTYPE_SURFACE);
-    IDirect3DSurface9 *pSoD3DIfSurf = (IDirect3DSurface9*)pAlloc->pSecondaryOpenedD3DIf;
-    hr = pScreen->pDevice9If->SetRenderTarget(0, pSoD3DIfSurf);
-    Assert(hr == S_OK);
-    if (hr == S_OK)
-#endif
     {
         hr = pDevice->RtCallbacks.pfnSetDisplayModeCb(pDevice->hDevice, &DdiDm);
@@ -5946,4 +6322,5 @@
     if (pRc)
     {
+        VBOXVDBG_CHECK_SMSYNC(pRc);
         Assert(pRc->cAllocations == 1);
         pAlloc = &pRc->aAllocations[0];
@@ -6010,84 +6387,4 @@
     return E_FAIL;
 }
-static HRESULT vboxWddmLockRect(PVBOXWDDMDISP_RESOURCE pRc, UINT iAlloc,
-        D3DLOCKED_RECT * pLockedRect,
-        CONST RECT *pRect,
-        DWORD fLockFlags)
-{
-    HRESULT hr = E_FAIL;
-    Assert(!pRc->aAllocations[iAlloc].LockInfo.cLocks);
-    Assert(pRc->cAllocations > iAlloc);
-    switch (pRc->aAllocations[0].enmD3DIfType)
-    {
-        case VBOXDISP_D3DIFTYPE_SURFACE:
-        {
-            IDirect3DSurface9 *pD3DIfSurf = (IDirect3DSurface9*)pRc->aAllocations[iAlloc].pD3DIf;
-            Assert(pD3DIfSurf);
-            hr = pD3DIfSurf->LockRect(pLockedRect, pRect, fLockFlags);
-            Assert(hr == S_OK);
-            break;
-        }
-        case VBOXDISP_D3DIFTYPE_TEXTURE:
-        {
-            IDirect3DTexture9 *pD3DIfTex = (IDirect3DTexture9*)pRc->aAllocations[0].pD3DIf;
-            Assert(pD3DIfTex);
-            hr = pD3DIfTex->LockRect(iAlloc, pLockedRect, pRect, fLockFlags);
-            Assert(hr == S_OK);
-            break;
-        }
-        case VBOXDISP_D3DIFTYPE_CUBE_TEXTURE:
-        {
-            IDirect3DCubeTexture9 *pD3DIfCubeTex = (IDirect3DCubeTexture9*)pRc->aAllocations[0].pD3DIf;
-            Assert(pD3DIfCubeTex);
-            hr = pD3DIfCubeTex->LockRect(VBOXDISP_CUBEMAP_INDEX_TO_FACE(pRc, iAlloc),
-                    VBOXDISP_CUBEMAP_INDEX_TO_LEVEL(pRc, iAlloc), pLockedRect, pRect, fLockFlags);
-            Assert(hr == S_OK);
-            break;
-        }
-        default:
-            Assert(0);
-            break;
-    }
-    return hr;
-}
-
-static HRESULT vboxWddmUnlockRect(PVBOXWDDMDISP_RESOURCE pRc, UINT iAlloc)
-{
-    HRESULT hr = S_OK;
-    Assert(pRc->cAllocations > iAlloc);
-    switch (pRc->aAllocations[0].enmD3DIfType)
-    {
-        case VBOXDISP_D3DIFTYPE_SURFACE:
-        {
-            IDirect3DSurface9 *pD3DIfSurf = (IDirect3DSurface9*)pRc->aAllocations[iAlloc].pD3DIf;
-            Assert(pD3DIfSurf);
-            hr = pD3DIfSurf->UnlockRect();
-            Assert(hr == S_OK);
-            break;
-        }
-        case VBOXDISP_D3DIFTYPE_TEXTURE:
-        {
-            IDirect3DTexture9 *pD3DIfTex = (IDirect3DTexture9*)pRc->aAllocations[0].pD3DIf;
-            Assert(pD3DIfTex);
-            hr = pD3DIfTex->UnlockRect(iAlloc);
-            Assert(hr == S_OK);
-            break;
-        }
-        case VBOXDISP_D3DIFTYPE_CUBE_TEXTURE:
-        {
-            IDirect3DCubeTexture9 *pD3DIfCubeTex = (IDirect3DCubeTexture9*)pRc->aAllocations[0].pD3DIf;
-            Assert(pD3DIfCubeTex);
-            hr = pD3DIfCubeTex->UnlockRect(VBOXDISP_CUBEMAP_INDEX_TO_FACE(pRc, iAlloc),
-                    VBOXDISP_CUBEMAP_INDEX_TO_LEVEL(pRc, iAlloc));
-            Assert(hr == S_OK);
-            break;
-        }
-        default:
-            Assert(0);
-            hr = E_FAIL;
-            break;
-    }
-    return hr;
-}
 
 static HRESULT APIENTRY vboxWddmDDevBlt(HANDLE hDevice, CONST D3DDDIARG_BLT* pData)
@@ -6102,4 +6399,6 @@
     PVBOXWDDMDISP_RESOURCE pDstRc = (PVBOXWDDMDISP_RESOURCE)pData->hDstResource;
     PVBOXWDDMDISP_RESOURCE pSrcRc = (PVBOXWDDMDISP_RESOURCE)pData->hSrcResource;
+    VBOXVDBG_CHECK_SMSYNC(pDstRc);
+    VBOXVDBG_CHECK_SMSYNC(pSrcRc);
     Assert(pDstRc->cAllocations > pData->DstSubResourceIndex);
     Assert(pSrcRc->cAllocations > pData->SrcSubResourceIndex);
@@ -6186,4 +6485,5 @@
     if (hr == S_OK)
     {
+        VBOXVDBG_CHECK_SMSYNC(pRc);
         Assert(pSurfIf);
         hr = pDevice9If->ColorFill(pSurfIf, &pData->DstRect, pData->Color);
@@ -6308,4 +6608,5 @@
     IDirect3DDevice9 * pDevice9If = VBOXDISP_D3DEV(pDevice);
     PVBOXWDDMDISP_RESOURCE pRc = (PVBOXWDDMDISP_RESOURCE)pData->hRenderTarget;
+    VBOXVDBG_CHECK_SMSYNC(pRc);
     Assert(pRc);
     Assert(pData->SubResourceIndex < pRc->cAllocations);
@@ -6329,4 +6630,5 @@
     if (pRc)
     {
+        VBOXVDBG_CHECK_SMSYNC(pRc);
         Assert(pRc->cAllocations == 1);
         Assert(pRc->aAllocations[0].enmD3DIfType == VBOXDISP_D3DIFTYPE_SURFACE);
@@ -6893,4 +7195,6 @@
     HRESULT hr = S_OK;
 
+    Assert(pData->hKMResource);
+
     Assert(pData->NumAllocations);
     PVBOXWDDMDISP_RESOURCE pRc = vboxResourceAlloc(pData->NumAllocations);
@@ -6902,5 +7206,8 @@
         pRc->pDevice = pDevice;
         pRc->RcDesc.enmRotation = pData->Rotation;
-        pRc->fFlags = VBOXWDDM_RESOURCE_F_OPENNED;
+        pRc->fFlags.Value = 0;
+        pRc->fFlags.Generic = 1;
+        pRc->fFlags.Opened = 1;
+        pRc->fFlags.KmResource = 1;
         if (!pData->pPrivateDriverData || !pData->PrivateDriverDataSize)
         {
@@ -6948,7 +7255,9 @@
             {
                 VBOXWDDM_RCINFO *pRcInfo = (VBOXWDDM_RCINFO*)pData->pPrivateDriverData;
-                Assert(pRcInfo->fFlags == VBOXWDDM_RESOURCE_F_TYPE_GENERIC);
+                Assert(pRcInfo->fFlags.Generic);
+                Assert(!pRcInfo->fFlags.Opened);
                 Assert(pRcInfo->cAllocInfos == pData->NumAllocations);
-                pRc->fFlags = pRcInfo->fFlags | VBOXWDDM_RESOURCE_F_OPENNED;
+                pRc->fFlags = pRcInfo->fFlags;
+                pRc->fFlags.Opened = 1;
                 pRc->RcDesc = pRcInfo->RcDesc;
                 pRc->cAllocations = pData->NumAllocations;
@@ -6973,4 +7282,13 @@
                     Assert(pAllocation->hSharedHandle);
 #endif
+
+                    vboxWddmShRcRefAlloc(pDevice, pAllocation, TRUE, NULL);
+
+                    vboxVDbgPrint(("\n\n********\n(0x%x:0n%d)Shared OPENNED pAlloc(0x%p), hRc(0x%p), hAl(0x%p), "
+                                    "Handle(0x%x), (0n%d) \n***********\n\n",
+                                GetCurrentProcessId(), GetCurrentProcessId(),
+                                pAllocation, pRc->hKMResource, pAllocation->hAllocation,
+                                pAllocation->hSharedHandle, pAllocation->hSharedHandle
+                                ));
                 }
 
@@ -6983,10 +7301,4 @@
 #ifndef VBOXWDDMDISP_DEBUG_NOSHARED
                     Assert(pAllocation->hSharedHandle);
-#endif
-
-#ifdef DEBUG_misha
-                    vboxVDbgPrint(("\n\n********\nShared Texture pAlloc(0x%p) Handle(0x%x), (0n%d) OPENED, pid (0x%x), (0n%d)\n***********\n\n",
-                            pAllocation, hSharedHandle, hSharedHandle, GetCurrentProcessId(), GetCurrentProcessId()));
-
 #endif
 
@@ -7030,5 +7342,4 @@
                             hr = E_INVALIDARG;
                         }
-
                         hr = pDevice->pAdapter->D3D.pfnVBoxWineExD3DDev9CreateCubeTexture((IDirect3DDevice9Ex *)pDevice9If,
                                                     pAllocation->SurfDesc.width,
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h	(revision 38982)
@@ -33,5 +33,5 @@
 /* maximum number of direct render targets to be used before
  * switching to offscreen rendering */
-#define VBOXWDDMDISP_MAX_DIRECT_RTS      3
+#define VBOXWDDMDISP_MAX_DIRECT_RTS      0
 
 #define VBOXWDDMDISP_IS_TEXTURE(_f) ((_f).Texture || (_f).Value == 0)
@@ -240,5 +240,4 @@
     /* object type is defined by enmD3DIfType enum */
     IUnknown *pD3DIf;
-    IUnknown *pSecondaryOpenedD3DIf;
     VBOXDISP_D3DIFTYPE enmD3DIfType;
     /* list entry used to add allocation to the dirty alloc list */
@@ -257,5 +256,5 @@
     D3DKMT_HANDLE hKMResource;
     PVBOXWDDMDISP_DEVICE pDevice;
-    uint32_t fFlags;
+    VBOXWDDMDISP_RESOURCE_FLAGS fFlags;
     VBOXWDDM_RC_DESC RcDesc;
     UINT cAllocations;
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp	(revision 38982)
@@ -37,50 +37,67 @@
 #endif
     Assert(pD3D->hD3DLib);
-    if (pD3D->hD3DLib)
+    if (!pD3D->hD3DLib)
+    {
+        DWORD winErr = GetLastError();
+        WARN((__FUNCTION__": LoadLibraryW failed, winErr = (%d)", winErr));
+        return E_FAIL;
+    }
+
+    do
     {
         pD3D->pfnDirect3DCreate9Ex = (PFNVBOXDISPD3DCREATE9EX)GetProcAddress(pD3D->hD3DLib, "Direct3DCreate9Ex");
-        Assert(pD3D->pfnDirect3DCreate9Ex);
-        if (pD3D->pfnDirect3DCreate9Ex)
+        if (!pD3D->pfnDirect3DCreate9Ex)
         {
-            pD3D->pfnVBoxWineExD3DDev9CreateTexture = (PFNVBOXWINEEXD3DDEV9_CREATETEXTURE)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DDev9CreateTexture");
-            Assert(pD3D->pfnVBoxWineExD3DDev9CreateTexture);
-            if (pD3D->pfnVBoxWineExD3DDev9CreateTexture)
-            {
-                pD3D->pfnVBoxWineExD3DDev9CreateCubeTexture = (PFNVBOXWINEEXD3DDEV9_CREATECUBETEXTURE)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DDev9CreateCubeTexture");
-                Assert(pD3D->pfnVBoxWineExD3DDev9CreateCubeTexture);
-                if (pD3D->pfnVBoxWineExD3DDev9CreateCubeTexture)
-                {
-                    pD3D->pfnVBoxWineExD3DDev9Flush = (PFNVBOXWINEEXD3DDEV9_FLUSH)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DDev9Flush");
-                    Assert(pD3D->pfnVBoxWineExD3DDev9Flush);
-                    if (pD3D->pfnVBoxWineExD3DDev9Flush)
-                    {
-                        pD3D->pfnVBoxWineExD3DDev9Update = (PFNVBOXWINEEXD3DDEV9_UPDATE)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DDev9Update");
-                        Assert(pD3D->pfnVBoxWineExD3DDev9Update);
-                        if (pD3D->pfnVBoxWineExD3DDev9Update)
-                        {
-                            pD3D->pfnVBoxWineExD3DSwapchain9Present = (PFNVBOXWINEEXD3DSWAPCHAIN9_PRESENT)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DSwapchain9Present");
-                            Assert(pD3D->pfnVBoxWineExD3DSwapchain9Present);
-                            if (pD3D->pfnVBoxWineExD3DSwapchain9Present)
-                            {
-                                return S_OK;
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        else
-        {
-            DWORD winErr = GetLastError();
-            vboxVDbgPrintR((__FUNCTION__": GetProcAddressW (for Direct3DCreate9Ex) failed, winErr = (%d)", winErr));
+            WARN(("no Direct3DCreate9Ex"));
+            break;
         }
 
-        VBoxDispD3DClose(pD3D);
-    }
-    else
-    {
-        DWORD winErr = GetLastError();
-        vboxVDbgPrintR((__FUNCTION__": LoadLibraryW failed, winErr = (%d)", winErr));
-    }
+        pD3D->pfnVBoxWineExD3DDev9CreateTexture = (PFNVBOXWINEEXD3DDEV9_CREATETEXTURE)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DDev9CreateTexture");
+        if (!pD3D->pfnVBoxWineExD3DDev9CreateTexture)
+        {
+            WARN(("no VBoxWineExD3DDev9CreateTexture"));
+            break;
+        }
+
+        pD3D->pfnVBoxWineExD3DDev9CreateCubeTexture = (PFNVBOXWINEEXD3DDEV9_CREATECUBETEXTURE)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DDev9CreateCubeTexture");
+        if (!pD3D->pfnVBoxWineExD3DDev9CreateCubeTexture)
+        {
+            WARN(("no VBoxWineExD3DDev9CreateCubeTexture"));
+            break;
+        }
+
+        pD3D->pfnVBoxWineExD3DDev9Flush = (PFNVBOXWINEEXD3DDEV9_FLUSH)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DDev9Flush");
+        if (!pD3D->pfnVBoxWineExD3DDev9Flush)
+        {
+            WARN(("no VBoxWineExD3DDev9Flush"));
+            break;
+        }
+
+        pD3D->pfnVBoxWineExD3DDev9Update = (PFNVBOXWINEEXD3DDEV9_UPDATE)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DDev9Update");
+        if (!pD3D->pfnVBoxWineExD3DDev9Update)
+        {
+            WARN(("no VBoxWineExD3DDev9Update"));
+            break;
+        }
+
+        pD3D->pfnVBoxWineExD3DRc9SetDontDeleteGl = (PFNVBOXWINEEXD3DRC9_SETDONTDELETEGL)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DRc9SetDontDeleteGl");
+        if (!pD3D->pfnVBoxWineExD3DRc9SetDontDeleteGl)
+        {
+            WARN(("no VBoxWineExD3DRc9SetDontDeleteGl"));
+            break;
+        }
+
+        pD3D->pfnVBoxWineExD3DSwapchain9Present = (PFNVBOXWINEEXD3DSWAPCHAIN9_PRESENT)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DSwapchain9Present");
+        if (!pD3D->pfnVBoxWineExD3DSwapchain9Present)
+        {
+            WARN(("no VBoxWineExD3DSwapchain9Present"));
+            break;
+        }
+
+        return S_OK;
+
+    } while (0);
+
+    VBoxDispD3DClose(pD3D);
 
     return E_FAIL;
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h	(revision 38982)
@@ -45,4 +45,6 @@
     PFNVBOXWINEEXD3DDEV9_UPDATE pfnVBoxWineExD3DDev9Update;
 
+    PFNVBOXWINEEXD3DRC9_SETDONTDELETEGL pfnVBoxWineExD3DRc9SetDontDeleteGl;
+
     PFNVBOXWINEEXD3DSWAPCHAIN9_PRESENT pfnVBoxWineExD3DSwapchain9Present;
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp	(revision 38982)
@@ -64,6 +64,8 @@
 DWORD g_VBoxVDbgFBreakDdi = 0;
 
-DWORD g_VBoxVDbgFLogRel = 0;
-DWORD g_VBoxVDbgFLog = 0;
+DWORD g_VBoxVDbgFCheckSysMemSync = 0;
+
+DWORD g_VBoxVDbgFLogRel = 1;
+DWORD g_VBoxVDbgFLog = 1;
 DWORD g_VBoxVDbgFLogFlow = 0;
 
@@ -561,6 +563,12 @@
     switch (pExceptionRecord->ExceptionCode)
     {
-        case 0xc0000005: /* only access violation and debug exceptions actually matter */
-        case 0xc0000003:
+        case EXCEPTION_BREAKPOINT:
+        case EXCEPTION_ACCESS_VIOLATION:
+        case EXCEPTION_STACK_OVERFLOW:
+        case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
+        case EXCEPTION_FLT_DIVIDE_BY_ZERO:
+        case EXCEPTION_FLT_INVALID_OPERATION:
+        case EXCEPTION_INT_DIVIDE_BY_ZERO:
+        case EXCEPTION_ILLEGAL_INSTRUCTION:
             AssertRelease(0);
             break;
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h	(revision 38982)
@@ -52,4 +52,5 @@
 extern DWORD g_VBoxVDbgFBreakDdi;
 
+extern DWORD g_VBoxVDbgFCheckSysMemSync;
 
 /* log enable flags */
@@ -146,4 +147,8 @@
     } while (0)
 
+#define LOG vboxVDbgPrint
+#define LOGREL vboxVDbgPrintR
+#define LOGFLOW vboxVDbgPrintF
+
 #ifdef VBOXWDDMDISP_DEBUG
 
@@ -191,4 +196,11 @@
         )
 
+#define VBOXVDBG_IS_CHECK_ALLOWED(_type) ( \
+        g_VBoxVDbgFCheck##_type \
+        && (g_VBoxVDbgFCheck##_type == 1 \
+                || VBOXVDBG_IS_DUMP_ALLOWED_PID(g_VBoxVDbgFCheck##_type) \
+           ) \
+        )
+
 #define VBOXVDBG_IS_DUMP_SHARED_ALLOWED(_pRc) (\
         (_pRc)->RcDesc.fFlags.SharedResource \
@@ -214,6 +226,31 @@
     } while (0)
 
+#define VBOXVDBG_CHECK_SMSYNC(_pRc) do { \
+        if (VBOXVDBG_IS_CHECK_ALLOWED(SysMemSync)) { \
+            vboxWddmDbgRcSynchMemCheck((_pRc)); \
+        } \
+    } while (0)
+
+#define VBOXVDBG_DEV_CHECK_SHARED(_pDevice, _pIsShared) do { \
+        *(_pIsShared) = FALSE; \
+        for (UINT i = 0; i < (_pDevice)->cRTs; ++i) { \
+            PVBOXWDDMDISP_ALLOCATION pRtVar = (_pDevice)->apRTs[i]; \
+            if (pRtVar->pRc->RcDesc.fFlags.SharedResource) { *(_pIsShared) = TRUE; break; } \
+        } \
+    } while (0)
+
+#define VBOXVDBG_IS_DUMP_SHARED_ALLOWED_DEV(_pDevice, _pIsAllowed) do { \
+        VBOXVDBG_DEV_CHECK_SHARED(_pDevice, _pIsAllowed); \
+        if (*(_pIsAllowed)) \
+        { \
+            *(_pIsAllowed) = VBOXVDBG_IS_DUMP_ALLOWED(Shared); \
+        } \
+    } while (0)
+
 #define VBOXVDBG_DUMP_DRAWPRIM_ENTER(_pDevice) do { \
-        if (VBOXVDBG_IS_DUMP_ALLOWED(DrawPrim)) \
+        BOOL fDumpShaded = FALSE; \
+        VBOXVDBG_IS_DUMP_SHARED_ALLOWED_DEV(_pDevice, &fDumpShaded); \
+        if (fDumpShaded \
+                || VBOXVDBG_IS_DUMP_ALLOWED(DrawPrim)) \
         { \
             vboxVDbgDoDumpRt("==>"__FUNCTION__": RenderTarget Dump\n", (_pDevice), "\n"); \
@@ -222,5 +259,8 @@
 
 #define VBOXVDBG_DUMP_DRAWPRIM_LEAVE(_pDevice) do { \
-        if (VBOXVDBG_IS_DUMP_ALLOWED(DrawPrim)) \
+        BOOL fDumpShaded = FALSE; \
+        VBOXVDBG_IS_DUMP_SHARED_ALLOWED_DEV(_pDevice, &fDumpShaded); \
+        if (fDumpShaded \
+                || VBOXVDBG_IS_DUMP_ALLOWED(DrawPrim)) \
         { \
             vboxVDbgDoDumpRt("<=="__FUNCTION__": RenderTarget Dump\n", (_pDevice), "\n"); \
@@ -330,4 +370,5 @@
 #define VBOXVDBG_BREAK_SHARED(_pRc) do { } while (0)
 #define VBOXVDBG_BREAK_DDI() do { } while (0)
+#define VBOXVDBG_CHECK_SMSYNC(_pRc) do { } while (0)
 #endif
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPDevExt.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPDevExt.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPDevExt.h	(revision 38982)
@@ -106,4 +106,9 @@
    BOOL bNotifyDxDpc;
 
+#if 0
+   FAST_MUTEX ShRcTreeMutex;
+   AVLPVTREE ShRcTree;
+#endif
+
    VBOXWDDM_SOURCE aSources[VBOX_VIDEO_MAX_SCREENS];
    VBOXWDDM_TARGET aTargets[VBOX_VIDEO_MAX_SCREENS];
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.cpp	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.cpp	(revision 38982)
@@ -1473,2 +1473,62 @@
     }
 }
+
+#if 0
+VOID vboxShRcTreeInit(PVBOXMP_DEVEXT pDevExt)
+{
+    ExInitializeFastMutex(&pDevExt->ShRcTreeMutex);
+    pDevExt->ShRcTree = NULL;
+}
+
+VOID vboxShRcTreeTerm(PVBOXMP_DEVEXT pDevExt)
+{
+    Assert(!pDevExt->ShRcTree);
+    pDevExt->ShRcTree = NULL;
+}
+
+BOOLEAN vboxShRcTreePut(PVBOXMP_DEVEXT pDevExt, PVBOXWDDM_ALLOCATION pAlloc)
+{
+    HANDLE hSharedRc = pAlloc->hSharedHandle;
+    if (!hSharedRc)
+    {
+        WARN(("invalid call with zero shared handle!"));
+        return FALSE;
+    }
+    pAlloc->ShRcTreeEntry.Key = (AVLPVKEY)hSharedRc;
+    ExAcquireFastMutex(&pDevExt->ShRcTreeMutex);
+    bool bRc = RTAvlPVInsert(&pDevExt->ShRcTree, &pAlloc->ShRcTreeEntry);
+    ExReleaseFastMutex(&pDevExt->ShRcTreeMutex);
+    Assert(bRc);
+    return (BOOLEAN)bRc;
+}
+
+#define PVBOXWDDM_ALLOCATION_FROM_SHRCTREENODE(_p) ((PVBOXWDDM_ALLOCATION)(((uint8_t*)(_p)) - RT_OFFSETOF(VBOXWDDM_ALLOCATION, ShRcTreeEntry)))
+PVBOXWDDM_ALLOCATION vboxShRcTreeGet(PVBOXMP_DEVEXT pDevExt, HANDLE hSharedRc)
+{
+    ExAcquireFastMutex(&pDevExt->ShRcTreeMutex);
+    PAVLPVNODECORE pNode = RTAvlPVGet(&pDevExt->ShRcTree, (AVLPVKEY)hSharedRc);
+    ExReleaseFastMutex(&pDevExt->ShRcTreeMutex);
+    if (!pNode)
+        return NULL;
+    PVBOXWDDM_ALLOCATION pAlloc = PVBOXWDDM_ALLOCATION_FROM_SHRCTREENODE(pNode);
+    return pAlloc;
+}
+
+BOOLEAN vboxShRcTreeRemove(PVBOXMP_DEVEXT pDevExt, PVBOXWDDM_ALLOCATION pAlloc)
+{
+    HANDLE hSharedRc = pAlloc->hSharedHandle;
+    if (!hSharedRc)
+    {
+        WARN(("invalid call with zero shared handle!"));
+        return FALSE;
+    }
+    ExAcquireFastMutex(&pDevExt->ShRcTreeMutex);
+    PAVLPVNODECORE pNode = RTAvlPVRemove(&pDevExt->ShRcTree, (AVLPVKEY)hSharedRc);
+    ExReleaseFastMutex(&pDevExt->ShRcTreeMutex);
+    if (!pNode)
+        return NULL;
+    PVBOXWDDM_ALLOCATION pRetAlloc = PVBOXWDDM_ALLOCATION_FROM_SHRCTREENODE(pNode);
+    Assert(pRetAlloc == pAlloc);
+    return !!pRetAlloc;
+}
+#endif
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.h	(revision 38982)
@@ -151,3 +151,12 @@
 VOID vboxWddmCounterU32Wait(uint32_t volatile * pu32, uint32_t u32Val);
 
+#if 0
+/* wine shrc handle -> allocation map */
+VOID vboxShRcTreeInit(PVBOXMP_DEVEXT pDevExt);
+VOID vboxShRcTreeTerm(PVBOXMP_DEVEXT pDevExt);
+BOOLEAN vboxShRcTreePut(PVBOXMP_DEVEXT pDevExt, PVBOXWDDM_ALLOCATION pAlloc);
+PVBOXWDDM_ALLOCATION vboxShRcTreeGet(PVBOXMP_DEVEXT pDevExt, HANDLE hSharedRc);
+BOOLEAN vboxShRcTreeRemove(PVBOXMP_DEVEXT pDevExt, PVBOXWDDM_ALLOCATION pAlloc);
+#endif
+
 #endif /* #ifndef ___VBoxMPMisc_h__ */
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPTypes.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPTypes.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPTypes.h	(revision 38982)
@@ -32,4 +32,8 @@
 #include "VBoxMPVbva.h"
 
+#if 0
+#include <iprt/avl.h>
+#endif
+
 /* one page size */
 #define VBOXWDDM_C_DMA_BUFFER_SIZE         0x1000
@@ -118,5 +122,4 @@
     VBOXWDDM_ALLOC_TYPE enmType;
     volatile uint32_t cRefs;
-//    VBOXWDDM_ALLOCUSAGE_TYPE enmCurrentUsage;
     D3DDDI_RESOURCEFLAGS fRcFlags;
     UINT SegmentId;
@@ -128,4 +131,8 @@
     BOOLEAN bVisible;
     BOOLEAN bAssigned;
+#ifdef DEBUG
+    /* current for shared rc handling assumes that once resource has no opens, it can not be openned agaion */
+    BOOLEAN fAssumedDeletion;
+#endif
     VBOXWDDM_SURFACE_DESC SurfDesc;
     struct VBOXWDDM_RESOURCE *pResource;
@@ -133,4 +140,12 @@
     DXGK_ALLOCATIONUSAGEHINT UsageHint;
     uint32_t iIndex;
+    uint32_t cOpens;
+    LIST_ENTRY OpenList;
+    /* helps tracking when to release wine shared resource */
+    uint32_t cShRcRefs;
+    HANDLE hSharedHandle;
+#if 0
+    AVLPVNODECORE ShRcTreeEntry;
+#endif
     VBOXUHGSMI_SYNCHOBJECT_TYPE enmSynchType;
     union
@@ -143,5 +158,5 @@
 typedef struct VBOXWDDM_RESOURCE
 {
-    uint32_t fFlags;
+    VBOXWDDMDISP_RESOURCE_FLAGS fFlags;
     volatile uint32_t cRefs;
     VBOXWDDM_RC_DESC RcDesc;
@@ -251,7 +266,17 @@
 } VBOXWDDM_DMA_PRIVATEDATA_CHROMIUM_CMD, *PVBOXWDDM_DMA_PRIVATEDATA_CHROMIUM_CMD;
 
+typedef struct VBOXWDDM_DMA_PRIVATEDATA_ALLOCINFO_ON_SUBMIT
+{
+    VBOXWDDM_DMA_PRIVATEDATA_BASEHDR Base;
+    VBOXWDDM_DMA_ALLOCINFO aInfos[1];
+} VBOXWDDM_DMA_PRIVATEDATA_ALLOCINFO_ON_SUBMIT, *PVBOXWDDM_DMA_PRIVATEDATA_ALLOCINFO_ON_SUBMIT;
+
 typedef struct VBOXWDDM_OPENALLOCATION
 {
+    LIST_ENTRY ListEntry;
     D3DKMT_HANDLE  hAllocation;
+    PVBOXWDDM_ALLOCATION pAllocation;
+    PVBOXWDDM_DEVICE pDevice;
+    uint32_t cShRcRefs;
 } VBOXWDDM_OPENALLOCATION, *PVBOXWDDM_OPENALLOCATION;
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.h	(revision 38982)
@@ -178,5 +178,6 @@
     PVBOXWDDM_ALLOCATION pAlloc;
     VBOXVIDEOOFFSET offAlloc;
-    UINT segmentIdAlloc;
+    UINT segmentIdAlloc : 31;
+    UINT fWriteOp : 1;
     D3DDDI_VIDEO_PRESENT_SOURCE_ID srcId;
 } VBOXWDDM_DMA_ALLOCINFO, *PVBOXWDDM_DMA_ALLOCINFO;
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.cpp	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.cpp	(revision 38982)
@@ -51,8 +51,8 @@
 }
 
-DECLINLINE(PVBOXWDDM_ALLOCATION) vboxWddmGetAllocationFromOpenData(PVBOXMP_DEVEXT pDevExt, PVBOXWDDM_OPENALLOCATION pOa)
+DECLINLINE(PVBOXWDDM_ALLOCATION) vboxWddmGetAllocationFromHandle(PVBOXMP_DEVEXT pDevExt, D3DKMT_HANDLE hAllocation)
 {
     DXGKARGCB_GETHANDLEDATA GhData;
-    GhData.hObject = pOa->hAllocation;
+    GhData.hObject = hAllocation;
     GhData.Type = DXGK_HANDLE_ALLOCATION;
     GhData.Flags.Value = 0;
@@ -62,5 +62,6 @@
 DECLINLINE(PVBOXWDDM_ALLOCATION) vboxWddmGetAllocationFromAllocList(PVBOXMP_DEVEXT pDevExt, DXGK_ALLOCATIONLIST *pAllocList)
 {
-    return vboxWddmGetAllocationFromOpenData(pDevExt, (PVBOXWDDM_OPENALLOCATION)pAllocList->hDeviceSpecificAllocation);
+    PVBOXWDDM_OPENALLOCATION pOa = (PVBOXWDDM_OPENALLOCATION)pAllocList->hDeviceSpecificAllocation;
+    return pOa->pAllocation;
 }
 
@@ -955,4 +956,7 @@
                     VBoxMPCmnInitCustomVideoModes(pContext);
                     VBoxWddmInvalidateVideoModesInfo(pContext);
+#if 0
+                    vboxShRcTreeInit(pContext);
+#endif
 
 #ifdef VBOX_WITH_VIDEOHWACCEL
@@ -1010,4 +1014,7 @@
 #ifdef VBOX_WITH_VIDEOHWACCEL
     vboxVhwaFree(pDevExt);
+#endif
+#if 0
+    vboxShRcTreeTerm(pDevExt);
 #endif
 
@@ -1794,4 +1801,14 @@
                 vboxWddmAssignPrimary(pDevExt, &pDevExt->aSources[pAllocation->SurfDesc.VidPnSourceId], NULL, pAllocation->SurfDesc.VidPnSourceId);
             }
+
+#if 0
+            if (pAllocation->enmType == VBOXWDDM_ALLOC_TYPE_UMD_RC_GENERIC)
+            {
+                if (pAllocation->hSharedHandle)
+                {
+                    vboxShRcTreeRemove(pDevExt, pAllocation);
+                }
+            }
+#endif
             break;
         }
@@ -1903,4 +1920,5 @@
             pAllocation->bVisible = FALSE;
             pAllocation->bAssigned = FALSE;
+            InitializeListHead(&pAllocation->OpenList);
 
             switch (pAllocInfo->enmType)
@@ -1957,4 +1975,14 @@
                                 {
                                     pAllocationInfo->Flags.CpuVisible = 1;
+                                }
+                                else
+                                {
+                                    pAllocation->hSharedHandle = (HANDLE)pAllocInfo->hSharedHandle;
+#if 0
+                                    if (pAllocation->hSharedHandle)
+                                    {
+                                        vboxShRcTreePut(pDevExt, pAllocation);
+                                    }
+#endif
                                 }
                             }
@@ -3212,4 +3240,6 @@
         case DXGK_OPERATION_FILL:
         {
+            Assert(pBuildPagingBuffer->Fill.FillPattern == 0);
+            PVBOXWDDM_ALLOCATION pAlloc = (PVBOXWDDM_ALLOCATION)pBuildPagingBuffer->Fill.hAllocation;
 //            pBuildPagingBuffer->pDmaBuffer = (uint8_t*)pBuildPagingBuffer->pDmaBuffer + VBOXVDMACMD_SIZE(VBOXVDMACMD_DMA_BPB_FILL);
             break;
@@ -3556,4 +3586,5 @@
             case VBOXESC_UHGSMI_SUBMIT:
             {
+                /* submit UHGSMI command */
                 PVBOXWDDM_CONTEXT pContext = (PVBOXWDDM_CONTEXT)pEscape->hContext;
                 PVBOXDISPIFESCAPE_UHGSMI_SUBMIT pSubmit = (PVBOXDISPIFESCAPE_UHGSMI_SUBMIT)pEscapeHdr;
@@ -3574,4 +3605,5 @@
             case VBOXESC_UHGSMI_ALLOCATE:
             {
+                /* allocate UHGSMI buffer */
                 PVBOXWDDM_CONTEXT pContext = (PVBOXWDDM_CONTEXT)pEscape->hContext;
                 PVBOXDISPIFESCAPE_UHGSMI_ALLOCATE pAlocate = (PVBOXDISPIFESCAPE_UHGSMI_ALLOCATE)pEscapeHdr;
@@ -3589,4 +3621,5 @@
             case VBOXESC_UHGSMI_DEALLOCATE:
             {
+                /* deallocate UHGSMI buffer */
                 PVBOXWDDM_CONTEXT pContext = (PVBOXWDDM_CONTEXT)pEscape->hContext;
                 PVBOXDISPIFESCAPE_UHGSMI_DEALLOCATE pDealocate = (PVBOXDISPIFESCAPE_UHGSMI_DEALLOCATE)pEscapeHdr;
@@ -3604,4 +3637,5 @@
             case VBOXESC_GETVBOXVIDEOCMCMD:
             {
+                /* get the list of r0->r3 commands (d3d window visible regions reporting )*/
                 PVBOXWDDM_CONTEXT pContext = (PVBOXWDDM_CONTEXT)pEscape->hContext;
                 PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pRegions = (PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD)pEscapeHdr;
@@ -3619,4 +3653,5 @@
             case VBOXESC_SETVISIBLEREGION:
             {
+                /* visible regions for seamless */
                 LPRGNDATA lpRgnData = VBOXDISPIFESCAPE_DATA(pEscapeHdr, RGNDATA);
                 uint32_t cbData = VBOXDISPIFESCAPE_DATA_SIZE(pEscape->PrivateDriverDataSize);
@@ -3682,4 +3717,5 @@
             case VBOXESC_SCREENLAYOUT:
             {
+                /* set screen layout (unused currently) */
                 Assert(pEscape->PrivateDriverDataSize >= sizeof (VBOXDISPIFESCAPE_SCREENLAYOUT));
                 if (pEscape->PrivateDriverDataSize >= sizeof (VBOXDISPIFESCAPE_SCREENLAYOUT))
@@ -3711,4 +3747,5 @@
             case VBOXESC_SWAPCHAININFO:
             {
+                /* set swapchain information */
                 PVBOXWDDM_CONTEXT pContext = (PVBOXWDDM_CONTEXT)pEscape->hContext;
                 Status = vboxWddmSwapchainCtxEscape(pDevExt, pContext, (PVBOXDISPIFESCAPE_SWAPCHAININFO)pEscapeHdr, pEscape->PrivateDriverDataSize);
@@ -3717,7 +3754,79 @@
             }
             case VBOXESC_REINITVIDEOMODES:
+            {
+                /* clear driver's internal videomodes cache */
                 VBoxWddmInvalidateVideoModesInfo(pDevExt);
                 Status = STATUS_SUCCESS;
                 break;
+            }
+            case VBOXESC_SHRC_ADDREF:
+            case VBOXESC_SHRC_RELEASE:
+            {
+                PVBOXWDDM_DEVICE pDevice = (PVBOXWDDM_DEVICE)pEscape->hDevice;
+                /* query whether the allocation represanted by the given [wine-generated] shared resource handle still exists */
+                if (pEscape->PrivateDriverDataSize != sizeof (VBOXDISPIFESCAPE_SHRC_REF))
+                {
+                    WARN(("invalid buffer size for VBOXDISPIFESCAPE_SHRC_REF, was(%d), but expected (%d)",
+                            pEscape->PrivateDriverDataSize, sizeof (VBOXDISPIFESCAPE_SHRC_REF)));
+                    Status = STATUS_INVALID_PARAMETER;
+                    break;
+                }
+
+                PVBOXDISPIFESCAPE_SHRC_REF pShRcRef = (PVBOXDISPIFESCAPE_SHRC_REF)pEscapeHdr;
+                PVBOXWDDM_ALLOCATION pAlloc = vboxWddmGetAllocationFromHandle(pDevExt, (D3DKMT_HANDLE)pShRcRef->hAlloc);
+                if (!pAlloc)
+                {
+                    WARN(("failed to get allocation from handle"));
+                    Status = STATUS_INVALID_PARAMETER;
+                    break;
+                }
+
+                PVBOXWDDM_OPENALLOCATION pOa = NULL;
+                for (PLIST_ENTRY pCur = pAlloc->OpenList.Flink; pCur != &pAlloc->OpenList; pCur = pCur->Flink)
+                {
+                    PVBOXWDDM_OPENALLOCATION pCurOa = CONTAINING_RECORD(pCur, VBOXWDDM_OPENALLOCATION, ListEntry);
+                    if (pCurOa->pDevice == pDevice)
+                    {
+                        pOa = pCurOa;
+                        break;
+                    }
+                }
+
+                if (!pOa)
+                {
+                    WARN(("failed to get open allocation from alloc"));
+                    Status = STATUS_INVALID_PARAMETER;
+                    break;
+                }
+
+                Assert(pAlloc->cShRcRefs >= pOa->cShRcRefs);
+
+                if (pEscapeHdr->escapeCode == VBOXESC_SHRC_ADDREF)
+                {
+#ifdef DEBUG
+                    Assert(!pAlloc->fAssumedDeletion);
+#endif
+                    ++pAlloc->cShRcRefs;
+                    ++pOa->cShRcRefs;
+                }
+                else
+                {
+                    Assert(pAlloc->cShRcRefs);
+                    Assert(pOa->cShRcRefs);
+                    --pAlloc->cShRcRefs;
+                    --pOa->cShRcRefs;
+#ifdef DEBUG
+                    Assert(!pAlloc->fAssumedDeletion);
+                    if (!pAlloc->cShRcRefs)
+                    {
+                        pAlloc->fAssumedDeletion = TRUE;
+                    }
+#endif
+                }
+
+                pShRcRef->EscapeHdr.u32CmdSpecific = pAlloc->cShRcRefs;
+                Status = STATUS_SUCCESS;
+                break;
+            }
             case VBOXESC_DBGPRINT:
             {
@@ -4580,48 +4689,85 @@
     if (Status == STATUS_SUCCESS)
     {
-        for (UINT i = 0; i < pOpenAllocation->NumAllocations; ++i)
+        UINT i = 0;
+        for (; i < pOpenAllocation->NumAllocations; ++i)
         {
             DXGK_OPENALLOCATIONINFO* pInfo = &pOpenAllocation->pOpenAllocation[i];
             Assert(pInfo->PrivateDriverDataSize == sizeof (VBOXWDDM_ALLOCINFO));
             Assert(pInfo->pPrivateDriverData);
+            PVBOXWDDM_ALLOCATION pAllocation = vboxWddmGetAllocationFromHandle(pDevExt, pInfo->hAllocation);
+            if (!pAllocation)
+            {
+                WARN(("invalid handle"));
+                Status = STATUS_INVALID_PARAMETER;
+                break;
+            }
+
             PVBOXWDDM_OPENALLOCATION pOa = (PVBOXWDDM_OPENALLOCATION)vboxWddmMemAllocZero(sizeof (VBOXWDDM_OPENALLOCATION));
+            if (!pOa)
+            {
+                WARN(("failed to allocation alloc info"));
+                Status = STATUS_INSUFFICIENT_RESOURCES;
+                break;
+            }
+
+#ifdef DEBUG
+            for (PLIST_ENTRY pCur = pAllocation->OpenList.Flink; pCur != &pAllocation->OpenList; pCur = pCur->Flink)
+            {
+                PVBOXWDDM_OPENALLOCATION pCurOa = CONTAINING_RECORD(pCur, VBOXWDDM_OPENALLOCATION, ListEntry);
+                if (pCurOa->pDevice == pDevice)
+                {
+                    /* should not happen */
+                    Assert(0);
+                    break;
+                }
+            }
+            Assert(!pAllocation->fAssumedDeletion);
+#endif
+            InsertHeadList(&pAllocation->OpenList, &pOa->ListEntry);
             pOa->hAllocation = pInfo->hAllocation;
+            pOa->pAllocation = pAllocation;
+            pOa->pDevice = pDevice;
             pInfo->hDeviceSpecificAllocation = pOa;
 
+
             if (pRcInfo)
             {
+                Assert(pAllocation->enmType == VBOXWDDM_ALLOC_TYPE_UMD_RC_GENERIC);
+
+                if (pInfo->PrivateDriverDataSize < sizeof (VBOXWDDM_ALLOCINFO)
+                        || !pInfo->pPrivateDriverData)
+                {
+                    WARN(("invalid data size"));
+                    vboxWddmMemFree(pOa);
+                    Status = STATUS_INVALID_PARAMETER;
+                    break;
+                }
+                PVBOXWDDM_ALLOCINFO pAllocInfo = (PVBOXWDDM_ALLOCINFO)pInfo->pPrivateDriverData;
+
 #ifdef VBOX_WITH_VIDEOHWACCEL
                 if (pRcInfo->RcDesc.fFlags.Overlay)
                 {
-                    if (pInfo->PrivateDriverDataSize >= sizeof (VBOXWDDM_ALLOCINFO))
-                    {
-                        PVBOXWDDM_ALLOCINFO pAllocInfo = (PVBOXWDDM_ALLOCINFO)pInfo->pPrivateDriverData;
-                        PVBOXWDDM_ALLOCATION pAllocation = vboxWddmGetAllocationFromOpenData(pDevExt, pOa);
-                        Assert(pAllocation);
-                        if (pAllocation)
-                        {
-                            /* we have queried host for some surface info, like pitch & size,
-                             * need to return it back to the UMD (User Mode Drive) */
-                            pAllocInfo->SurfDesc = pAllocation->SurfDesc;
-                            /* success, just continue */
-                            continue;
-                        }
-                        else
-                            Status = STATUS_INVALID_PARAMETER;
-                    }
-                    else
-                        Status = STATUS_INVALID_PARAMETER;
-
-                    /* we are here in case of error */
-                    AssertBreakpoint();
-
-                    for (UINT j = 0; j < i; ++j)
-                    {
-                        DXGK_OPENALLOCATIONINFO* pInfo2Free = &pOpenAllocation->pOpenAllocation[j];
-                        PVBOXWDDM_OPENALLOCATION pOa2Free = (PVBOXWDDM_OPENALLOCATION)pInfo2Free->hDeviceSpecificAllocation;
-                        vboxWddmMemFree(pOa2Free);
-                    }
+                    /* we have queried host for some surface info, like pitch & size,
+                     * need to return it back to the UMD (User Mode Drive) */
+                    pAllocInfo->SurfDesc = pAllocation->SurfDesc;
+                    /* success, just continue */
                 }
 #endif
+            }
+
+            ++pAllocation->cOpens;
+        }
+
+        if (Status != STATUS_SUCCESS)
+        {
+            for (UINT j = 0; j < i; ++j)
+            {
+                DXGK_OPENALLOCATIONINFO* pInfo2Free = &pOpenAllocation->pOpenAllocation[j];
+                PVBOXWDDM_OPENALLOCATION pOa2Free = (PVBOXWDDM_OPENALLOCATION)pInfo2Free->hDeviceSpecificAllocation;
+                PVBOXWDDM_ALLOCATION pAllocation = pOa2Free->pAllocation;
+                RemoveEntryList(&pOa2Free->ListEntry);
+                Assert(pAllocation->cOpens);
+                --pAllocation->cOpens;
+                vboxWddmMemFree(pOa2Free);
             }
         }
@@ -4645,7 +4791,17 @@
     vboxVDbgBreakFv();
 
+    PVBOXWDDM_DEVICE pDevice = (PVBOXWDDM_DEVICE)hDevice;
+    PVBOXMP_DEVEXT pDevExt = pDevice->pAdapter;
+
     for (UINT i = 0; i < pCloseAllocation->NumAllocations; ++i)
     {
-        vboxWddmMemFree(pCloseAllocation->pOpenHandleList[i]);
+        PVBOXWDDM_OPENALLOCATION pOa2Free = (PVBOXWDDM_OPENALLOCATION)pCloseAllocation->pOpenHandleList[i];
+        PVBOXWDDM_ALLOCATION pAllocation = pOa2Free->pAllocation;
+        RemoveEntryList(&pOa2Free->ListEntry);
+        Assert(pAllocation->cShRcRefs >= pOa2Free->cShRcRefs);
+        pAllocation->cShRcRefs -= pOa2Free->cShRcRefs;
+        Assert(pAllocation->cOpens);
+        --pAllocation->cOpens;
+        vboxWddmMemFree(pOa2Free);
     }
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d8/device.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d8/device.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d8/device.c	(revision 38982)
@@ -30,4 +30,5 @@
 
 #include "config.h"
+#include "d3d8_private.h"
 
 #include <math.h>
@@ -41,6 +42,4 @@
 #include "wingdi.h"
 #include "wine/debug.h"
-
-#include "d3d8_private.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d8/directx.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d8/directx.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d8/directx.c	(revision 38982)
@@ -31,4 +31,5 @@
 
 #include "config.h"
+#include "d3d8_private.h"
 
 #include <stdarg.h>
@@ -42,6 +43,4 @@
 #include "wine/debug.h"
 #include "wine/unicode.h"
-
-#include "d3d8_private.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/d3d9wddm.def
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/d3d9wddm.def	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/d3d9wddm.def	(revision 38982)
@@ -17,2 +17,3 @@
   VBoxWineExD3DDev9Update
   VBoxWineExD3DSwapchain9Present
+  VBoxWineExD3DRc9SetDontDeleteGl
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c	(revision 38982)
@@ -506,5 +506,5 @@
     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
     IDirect3DSwapChain9Impl *newSwapchain;
-    HRESULT hr = IDirect3DDevice9Impl_DoCreateAdditionalSwapChain(iface, present_parameters, &newSwapchain);
+    HRESULT hr = IDirect3DDevice9Impl_DoCreateAdditionalSwapChain(iface, present_parameters, (IDirect3DSwapChain9**)&newSwapchain);
     if (FAILED(hr))
     {
@@ -520,5 +520,5 @@
     {
         ERR("Failed to add additional swapchain, hr %#x.\n", hr);
-        IUnknown_Release(newSwapchain);
+        IUnknown_Release((IDirect3DSwapChain9*)newSwapchain);
         return hr;
     }
@@ -3152,2 +3152,74 @@
     return D3D_OK;
 }
+
+#ifdef VBOX_WITH_WDDM
+VBOXWINEEX_DECL(HRESULT) VBoxWineExD3DRc9SetDontDeleteGl(IDirect3DResource9 *iface)
+{
+    D3DRESOURCETYPE enmType = IDirect3DResource9_GetType(iface);
+    HRESULT hr;
+    switch (enmType)
+    {
+        case D3DRTYPE_SURFACE:
+        {
+            IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl*)iface ;
+            wined3d_mutex_lock();
+            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DSurface);
+            wined3d_mutex_unlock();
+            break;
+        }
+        case D3DRTYPE_VOLUME:
+        {
+            IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl*)iface ;
+            wined3d_mutex_lock();
+            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DVolume);
+            wined3d_mutex_unlock();
+            break;
+        }
+        case D3DRTYPE_TEXTURE:
+        {
+            IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl*)iface ;
+            wined3d_mutex_lock();
+            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DTexture);
+            wined3d_mutex_unlock();
+            break;
+        }
+        case D3DRTYPE_VOLUMETEXTURE:
+        {
+            IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl*)iface ;
+            wined3d_mutex_lock();
+            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DVolumeTexture);
+            wined3d_mutex_unlock();
+            break;
+        }
+        case D3DRTYPE_CUBETEXTURE:
+        {
+            IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl*)iface ;
+            wined3d_mutex_lock();
+            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DCubeTexture);
+            wined3d_mutex_unlock();
+            break;
+        }
+        case D3DRTYPE_VERTEXBUFFER:
+        {
+            IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl*)iface ;
+            wined3d_mutex_lock();
+            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DVertexBuffer);
+            wined3d_mutex_unlock();
+            break;
+        }
+        case D3DRTYPE_INDEXBUFFER:
+        {
+            IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl*)iface ;
+            wined3d_mutex_lock();
+            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DIndexBuffer);
+            wined3d_mutex_unlock();
+            break;
+        }
+        default:
+            ERR("invalid arg");
+            hr = E_INVALIDARG;
+            break;
+    }
+    return hr;
+}
+#endif
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/debug.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/debug.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/debug.h	(revision 38982)
@@ -31,4 +31,8 @@
 #define __WINE_WINE_DEBUG_H
 
+#if defined(VBOX_WITH_WDDM) || defined(VBOX_WINE_WITHOUT_LIBWINE)
+# error "unexpected include!!"
+#endif
+
 #include <stdarg.h>
 #include <windef.h>
@@ -317,3 +321,20 @@
 #endif
 
+
+#ifdef DEBUG_misha
+//# define VBOXWINEDBG_SHADERS
+#endif
+
+#ifdef VBOXWINEDBG_SHADERS
+
+void vboxWDbgPrintF(char * szString, ...);
+
+# define WDLOG(_m) do {\
+        vboxWDbgPrintF _m ; \
+    } while (0)
+#else
+
+# define WDLOG(_m) do { } while (0)
+#endif
+
 #endif  /* __WINE_WINE_DEBUG_H */
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/wined3d.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/wined3d.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/wined3d.h	(revision 38982)
@@ -6,4 +6,8 @@
 #ifndef __WIDL_WINED3D_H
 #define __WIDL_WINED3D_H
+
+#if defined(VBOX_WITH_WDDM) || defined(VBOX_WINE_WITHOUT_LIBWINE)
+# error "unexpected include!!"
+#endif
 
 #ifdef __cplusplus
@@ -3429,4 +3433,9 @@
         IWineD3DResource* This);
 
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
+
     END_INTERFACE
 } IWineD3DResourceVtbl;
@@ -3451,4 +3460,7 @@
 #define IWineD3DResource_UnLoad(This) (This)->lpVtbl->UnLoad(This)
 #define IWineD3DResource_GetType(This) (This)->lpVtbl->GetType(This)
+# ifdef VBOX_WITH_WDDM
+#  define IWineD3DResource_SetDontDeleteGl(This) (This)->lpVtbl->SetDontDeleteGl(This)
+# endif
 #endif
 
@@ -4055,4 +4067,9 @@
     WINED3DRESOURCETYPE (STDMETHODCALLTYPE *GetType)(
         IWineD3DSurface* This);
+
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
 
     /*** IWineD3DSurface methods ***/
@@ -4671,4 +4688,9 @@
         IWineD3DVolume* This);
 
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
+
     /*** IWineD3DVolume methods ***/
     HRESULT (STDMETHODCALLTYPE *GetContainer)(
@@ -4884,4 +4906,9 @@
     WINED3DRESOURCETYPE (STDMETHODCALLTYPE *GetType)(
         IWineD3DBaseTexture* This);
+
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
 
     /*** IWineD3DBaseTexture methods ***/
@@ -5130,4 +5157,9 @@
     WINED3DRESOURCETYPE (STDMETHODCALLTYPE *GetType)(
         IWineD3DTexture* This);
+
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DTexture* This);
+#endif
 
     /*** IWineD3DBaseTexture methods ***/
@@ -5377,4 +5409,9 @@
         IWineD3DCubeTexture* This);
 
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DCubeTexture* This);
+#endif
+
     /*** IWineD3DBaseTexture methods ***/
     DWORD (STDMETHODCALLTYPE *SetLOD)(
@@ -5627,4 +5664,9 @@
         IWineD3DVolumeTexture* This);
 
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
+
     /*** IWineD3DBaseTexture methods ***/
     DWORD (STDMETHODCALLTYPE *SetLOD)(
@@ -6382,4 +6424,9 @@
     WINED3DRESOURCETYPE (STDMETHODCALLTYPE *GetType)(
         IWineD3DBuffer* This);
+
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
 
     /*** IWineD3DBuffer methods ***/
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/VBoxWineEx.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/VBoxWineEx.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/VBoxWineEx.h	(revision 38982)
@@ -16,8 +16,10 @@
 #define ___VBoxWineEx_h__
 
+#define VBOXWINEEX_VERSION 1
+
 #ifndef IN_VBOXWINEEX
-#  define VBOXWINEEX_DECL(_type) __declspec(dllimport) _type WINAPI
+# define VBOXWINEEX_DECL(_type)   __declspec(dllimport) _type WINAPI
 # else
-#  define VBOXWINEEX_DECL(_type) __declspec(dllexport) _type WINAPI
+# define VBOXWINEEX_DECL(_type)  __declspec(dllexport) _type WINAPI
 #endif
 
@@ -39,4 +41,7 @@
 typedef VBOXWINEEX_DECL(HRESULT) FNVBOXWINEEXD3DDEV9_UPDATE(IDirect3DDevice9Ex *iface, D3DPRESENT_PARAMETERS * pParams, IDirect3DDevice9Ex **outIface);
 typedef FNVBOXWINEEXD3DDEV9_UPDATE *PFNVBOXWINEEXD3DDEV9_UPDATE;
+
+typedef VBOXWINEEX_DECL(HRESULT) FNVBOXWINEEXD3DRC9_SETDONTDELETEGL(IDirect3DResource9 *iface);
+typedef FNVBOXWINEEXD3DRC9_SETDONTDELETEGL *PFNVBOXWINEEXD3DRC9_SETDONTDELETEGL;
 
 typedef VBOXWINEEX_DECL(HRESULT) FNVBOXWINEEXD3DSWAPCHAIN9_PRESENT(IDirect3DSwapChain9 *iface, IDirect3DSurface9 *surf);
@@ -64,4 +69,6 @@
                                                     IDirect3DDevice9Ex **outIface); /* update device parameters */
 
+VBOXWINEEX_DECL(HRESULT) VBoxWineExD3DRc9SetDontDeleteGl(IDirect3DResource9 *iface);
+
 VBOXWINEEX_DECL(HRESULT) VBoxWineExD3DSwapchain9Present(IDirect3DSwapChain9 *iface,
                                 IDirect3DSurface9 *surf); /* use the given surface as a frontbuffer content source */
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/libWineStub/include/wine/debug.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/libWineStub/include/wine/debug.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/libWineStub/include/wine/debug.h	(revision 38982)
@@ -31,6 +31,5 @@
 #define __WINE_WINE_DEBUG_H
 
-#include <stdarg.h>
-#include <windef.h>
+#include <windows.h>
 #ifndef GUID_DEFINED
 #include <guiddef.h>
@@ -322,3 +321,22 @@
 #endif
 
+
+#ifdef DEBUG_misha
+# define VBOXWINEDBG_SHADERS
+#endif
+
+#ifdef VBOXWINEDBG_SHADERS
+#include <stdio.h>
+#include <stdarg.h>
+
+void vboxWDbgPrintF(char * szString, ...);
+
+# define WDLOG(_m) do {\
+        vboxWDbgPrintF _m ; \
+    } while (0)
+#else
+
+# define WDLOG(_m) do { } while (0)
+#endif
+
 #endif  /* __WINE_WINE_DEBUG_H */
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/libWineStub/include/wine/wined3d.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/libWineStub/include/wine/wined3d.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/libWineStub/include/wine/wined3d.h	(revision 38982)
@@ -3431,4 +3431,9 @@
         IWineD3DResource* This);
 
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
+
     END_INTERFACE
 } IWineD3DResourceVtbl;
@@ -3453,4 +3458,7 @@
 #define IWineD3DResource_UnLoad(This) (This)->lpVtbl->UnLoad(This)
 #define IWineD3DResource_GetType(This) (This)->lpVtbl->GetType(This)
+# ifdef VBOX_WITH_WDDM
+#  define IWineD3DResource_SetDontDeleteGl(This) (This)->lpVtbl->SetDontDeleteGl(This)
+# endif
 #endif
 
@@ -4057,4 +4065,9 @@
     WINED3DRESOURCETYPE (STDMETHODCALLTYPE *GetType)(
         IWineD3DSurface* This);
+
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
 
     /*** IWineD3DSurface methods ***/
@@ -4673,4 +4686,9 @@
         IWineD3DVolume* This);
 
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
+
     /*** IWineD3DVolume methods ***/
     HRESULT (STDMETHODCALLTYPE *GetContainer)(
@@ -4886,4 +4904,9 @@
     WINED3DRESOURCETYPE (STDMETHODCALLTYPE *GetType)(
         IWineD3DBaseTexture* This);
+
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
 
     /*** IWineD3DBaseTexture methods ***/
@@ -5132,4 +5155,9 @@
     WINED3DRESOURCETYPE (STDMETHODCALLTYPE *GetType)(
         IWineD3DTexture* This);
+
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
 
     /*** IWineD3DBaseTexture methods ***/
@@ -5379,4 +5407,9 @@
         IWineD3DCubeTexture* This);
 
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
+
     /*** IWineD3DBaseTexture methods ***/
     DWORD (STDMETHODCALLTYPE *SetLOD)(
@@ -5629,4 +5662,9 @@
         IWineD3DVolumeTexture* This);
 
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
+
     /*** IWineD3DBaseTexture methods ***/
     DWORD (STDMETHODCALLTYPE *SetLOD)(
@@ -6384,4 +6422,9 @@
     WINED3DRESOURCETYPE (STDMETHODCALLTYPE *GetType)(
         IWineD3DBuffer* This);
+
+#ifdef VBOX_WITH_WDDM
+    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
+        IWineD3DResource* This);
+#endif
 
     /*** IWineD3DBuffer methods ***/
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/basetexture.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/basetexture.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/basetexture.c	(revision 38982)
@@ -114,5 +114,5 @@
 
 #ifdef VBOX_WITH_WDDM
-    if (VBOXSHRC_IS_SHARED_OPENED(This))
+    if (!VBOXSHRC_CAN_DELETE(device, This))
     {
         This->baseTexture.texture_rgb.name = 0;
@@ -291,9 +291,10 @@
         if (VBOXSHRC_IS_SHARED_OPENED(This))
         {
-            gl_tex->name = VBOXSHRC_GET_SHAREHANDLE(This);
+            gl_tex->name = (GLuint)VBOXSHRC_GET_SHAREHANDLE(This);
         }
         else
 #endif
         {
+            isNewTexture = TRUE;
             glGenTextures(1, &gl_tex->name);
 #ifdef VBOX_WITH_WDDM
@@ -306,4 +307,5 @@
         checkGLcall("glGenTextures");
         TRACE("Generated texture %d\n", gl_tex->name);
+#ifndef VBOX_WITH_WDDM
         if (This->resource.pool == WINED3DPOOL_DEFAULT) {
             /* Tell opengl to try and keep this texture in video ram (well mostly) */
@@ -313,4 +315,8 @@
 
         }
+#else
+        /* chromium code on host fails to resolve texture name to texture obj for some reason
+         * @todo: investigate */
+#endif
         /* Initialise the state of the texture object
         to the openGL defaults, not the directx defaults */
@@ -329,7 +335,7 @@
         gl_tex->states[WINED3DTEXSTA_TSSADDRESSW]   = WINED3DTADDRESS_WRAP;
         IWineD3DBaseTexture_SetDirty(iface, TRUE);
-        isNewTexture = TRUE;
-
-        if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
+
+        if(isNewTexture
+                && This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
             /* This means double binding the texture at creation, but keeps the code simpler all
              * in all, and the run-time path free from additional checks
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/buffer.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/buffer.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/buffer.c	(revision 38982)
@@ -1449,4 +1449,7 @@
     buffer_UnLoad,
     buffer_GetType,
+#ifdef VBOX_WITH_WDDM
+    IWineD3DResourceImpl_SetDontDeleteGl,
+#endif
     /* IWineD3DBuffer methods */
     buffer_Map,
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/cubetexture.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/cubetexture.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/cubetexture.c	(revision 38982)
@@ -245,4 +245,29 @@
 }
 
+#ifdef VBOX_WITH_WDDM
+static HRESULT WINAPI IWineD3DCubeTextureImpl_SetDontDeleteGl(IWineD3DCubeTexture *iface) {
+    IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl*)iface;
+    HRESULT hr = IWineD3DResourceImpl_SetDontDeleteGl((IWineD3DResource*)iface);
+    unsigned int i, j;
+
+    if (FAILED(hr))
+    {
+        ERR("IWineD3DResource_SetDontDeleteGl failed");
+        return hr;
+    }
+
+    for (i = 0; i < This->baseTexture.levels; ++i) {
+        for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z; ++j) {
+            if (This->surfaces[j][i]) {
+                HRESULT tmpHr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->surfaces[j][i]);
+                Assert(tmpHr == S_OK);
+            }
+        }
+    }
+
+    return WINED3D_OK;
+}
+#endif
+
 /* ******************************************************
    IWineD3DCubeTexture IWineD3DBaseTexture parts follow
@@ -418,4 +443,7 @@
     IWineD3DCubeTextureImpl_UnLoad,
     IWineD3DCubeTextureImpl_GetType,
+#ifdef VBOX_WITH_WDDM
+    IWineD3DCubeTextureImpl_SetDontDeleteGl,
+#endif
     /* IWineD3DBaseTexture */
     IWineD3DCubeTextureImpl_SetLOD,
@@ -606,5 +634,5 @@
             for (j = 0; j < 6; ++j)
             {
-                Assert((*shared_handle) == ((IWineD3DSurfaceImpl*)texture->surfaces[j][i])->texture_name);
+                Assert((*shared_handle) == (HANDLE)((IWineD3DSurfaceImpl*)texture->surfaces[j][i])->texture_name);
             }
         }
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c	(revision 38982)
@@ -4080,5 +4080,9 @@
     if (stage >= sizeof(This->stateBlock->textures) / sizeof(*This->stateBlock->textures))
     {
+#ifdef DEBUG_misha
+        ERR("Ignoring invalid stage %u.\n", stage);
+#else
         WARN("Ignoring invalid stage %u.\n", stage);
+#endif
         return WINED3D_OK;
     }
@@ -6280,5 +6284,5 @@
         Assert(!VBOXSHRC_IS_SHARED(surface));
 # endif
-        if (!VBOXSHRC_IS_SHARED_OPENED(surface))
+        if (VBOXSHRC_CAN_DELETE(device, surface))
 #endif
         {
@@ -6996,5 +7000,5 @@
         }
 
-        This->swapchains = (IWineD3DSwapChain *)pvNewBuf;
+        This->swapchains = pvNewBuf;
     }
     else
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/glsl_shader.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/glsl_shader.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/glsl_shader.c	(revision 38982)
@@ -241,5 +241,7 @@
     };
 
+#ifndef VBOXWINEDBG_SHADERS
     if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
+#endif
 
     GL_EXTCALL(glGetObjectParameterivARB(obj,
@@ -270,11 +272,11 @@
         if (is_spam)
         {
-            TRACE("Spam received from GLSL shader #%u:\n", obj);
-            while ((line = get_info_log_line(&ptr))) TRACE("    %s\n", line);
+            WDLOG(("Spam received from GLSL shader #%u:\n", obj));
+            while ((line = get_info_log_line(&ptr))) WDLOG(("    %s\n", line));
         }
         else
         {
-            FIXME("Error received from GLSL shader #%u:\n", obj);
-            while ((line = get_info_log_line(&ptr))) FIXME("    %s\n", line);
+            WDLOG(("Error received from GLSL shader #%u:\n", obj));
+            while ((line = get_info_log_line(&ptr))) WDLOG(("    %s\n", line));
         }
         HeapFree(GetProcessHeap(), 0, infoLog);
@@ -319,15 +321,15 @@
         }
 
-        FIXME("Object %u:\n", objects[i]);
+        WDLOG(("Object %u:\n", objects[i]));
         GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
-        FIXME("    GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
+        WDLOG(("    GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp)));
         GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
-        FIXME("    GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
-        FIXME("\n");
+        WDLOG(("    GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp));
+        WDLOG(("\n"));
 
         ptr = source;
         GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
-        while ((line = get_info_log_line(&ptr))) FIXME("    %s\n", line);
-        FIXME("\n");
+        while ((line = get_info_log_line(&ptr))) WDLOG(("    %s\n", line));
+        WDLOG(("\n"));
     }
 
@@ -337,19 +339,43 @@
 
 /* GL locking is done by the caller. */
-static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
-{
-    GLint tmp;
-
+static void shader_glsl_validate_compile_link(const struct wined3d_gl_info *gl_info, GLhandleARB program, GLboolean fIsProgram)
+{
+    GLint tmp = -1;
+
+#ifndef VBOXWINEDBG_SHADERS
     if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
+#endif
 
     GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
     if (tmp == GL_PROGRAM_OBJECT_ARB)
     {
+        if (!fIsProgram)
+        {
+            ERR("this is a program, but shader expected");
+        }
         GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
         if (!tmp)
         {
-            FIXME("Program %u link status invalid.\n", program);
+            ERR("Program %u link status invalid.\n", program);
             shader_glsl_dump_program_source(gl_info, program);
         }
+    }
+    else if (tmp == GL_SHADER_OBJECT_ARB)
+    {
+        if (fIsProgram)
+        {
+            ERR("this is a shader, but program expected");
+        }
+
+        GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
+        if (!tmp)
+        {
+            ERR("Shader %u compile status invalid.\n", program);
+            shader_glsl_dump_program_source(gl_info, program);
+        }
+    }
+    else
+    {
+        ERR("unexpected oject type(%d)!", tmp);
     }
 
@@ -1003,5 +1029,10 @@
                  * Writing gl_ClipVertex requires one uniform for each clipplane as well.
                  */
+#ifdef DEBUG_misha
+                /* tmp work-around to make Internet Explorer in win8 work with GPU supporting only with 256 shader uniform vars */
+                max_constantsF = gl_info->limits.glsl_vs_float_constants - 1;
+#else
                 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
+#endif
                 if(ctx_priv->cur_vs_args->clip_enabled)
                 {
@@ -4031,5 +4062,5 @@
     GL_EXTCALL(glCompileShaderARB(ret));
     checkGLcall("glCompileShaderARB(ret)");
-
+    shader_glsl_validate_compile_link(gl_info, ret, FALSE);
     return ret;
 }
@@ -4153,5 +4184,5 @@
     GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
     GL_EXTCALL(glCompileShaderARB(shader_obj));
-    print_glsl_info_log(gl_info, shader_obj);
+    shader_glsl_validate_compile_link(gl_info, shader_obj, FALSE);
 
     /* Store the shader object */
@@ -4229,5 +4260,5 @@
     GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
     GL_EXTCALL(glCompileShaderARB(shader_obj));
-    print_glsl_info_log(gl_info, shader_obj);
+    shader_glsl_validate_compile_link(gl_info, shader_obj, FALSE);
 
     return shader_obj;
@@ -4509,5 +4540,5 @@
     TRACE("Linking GLSL shader program %u\n", programId);
     GL_EXTCALL(glLinkProgramARB(programId));
-    shader_glsl_validate_link(gl_info, programId);
+    shader_glsl_validate_compile_link(gl_info, programId, TRUE);
 
     entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0,
@@ -4655,8 +4686,11 @@
     GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
     GL_EXTCALL(glCompileShaderARB(vshader_id));
+    shader_glsl_validate_compile_link(gl_info, vshader_id, FALSE);
 
     pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
     GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
     GL_EXTCALL(glCompileShaderARB(pshader_id));
+
+    shader_glsl_validate_compile_link(gl_info, vshader_id, FALSE);
 
     program_id = GL_EXTCALL(glCreateProgramObjectARB());
@@ -4664,6 +4698,5 @@
     GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
     GL_EXTCALL(glLinkProgramARB(program_id));
-
-    shader_glsl_validate_link(gl_info, program_id);
+    shader_glsl_validate_compile_link(gl_info, program_id, TRUE);
 
     /* Once linked we can mark the shaders for deletion. They will be deleted once the program
@@ -5209,2 +5242,15 @@
     shader_glsl_color_fixup_supported,
 };
+
+#ifdef VBOXWINEDBG_SHADERS
+void vboxWDbgPrintF(char * szString, ...)
+{
+    char szBuffer[4096*2] = {0};
+    va_list pArgList;
+    va_start(pArgList, szString);
+    _vsnprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), szString, pArgList);
+    va_end(pArgList);
+
+    OutputDebugStringA(szBuffer);
+}
+#endif
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/palette.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/palette.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/palette.c	(revision 38982)
@@ -30,10 +30,10 @@
 
 #include "config.h"
+#include "wined3d_private.h"
+
 #include "winerror.h"
 #include "wine/debug.h"
-
 #include <string.h>
 
-#include "wined3d_private.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/resource.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/resource.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/resource.c	(revision 38982)
@@ -61,4 +61,7 @@
 
 #ifdef VBOX_WITH_WDDM
+    resource->sharerc_handle = 0;
+    resource->sharerc_flags = 0;
+    resource->sharerc_locks = 0;
     if (pool == WINED3DPOOL_SYSTEMMEM && pvClientMem)
     {
@@ -72,5 +75,5 @@
         if (pool == WINED3DPOOL_DEFAULT && shared_handle)
         {
-            resource->sharerc_handle = *shared_handle;
+            resource->sharerc_handle = (DWORD)*shared_handle;
             resource->sharerc_flags = VBOXSHRC_F_SHARED;
             if (*shared_handle)
@@ -283,2 +286,16 @@
     return WINED3D_OK;
 }
+
+#ifdef VBOX_WITH_WDDM
+HRESULT WINAPI IWineD3DResourceImpl_SetDontDeleteGl(IWineD3DResource *iface) {
+    IWineD3DResourceImpl *This = (IWineD3DResourceImpl*)iface;
+    if (!VBOXSHRC_IS_SHARED(This))
+    {
+        ERR("invalid arg");
+        return E_INVALIDARG;
+    }
+
+    VBOXSHRC_SET_DONT_DELETE(This);
+    return WINED3D_OK;
+}
+#endif
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 38982)
@@ -100,5 +100,5 @@
     if (This->texture_name
 #ifdef VBOX_WITH_WDDM
-            && !VBOXSHRC_IS_SHARED_OPENED(This)
+            && VBOXSHRC_CAN_DELETE(device, This)
 #endif
             )
@@ -580,9 +580,16 @@
     if (!*name && new_name)
     {
+        BOOL fPersistent = FALSE;
         /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
          * surface has no texture name yet. See if we can get rid of this. */
         if (This->Flags & flag)
             ERR("Surface has SFLAG_INTEXTURE set, but no texture name\n");
-        IWineD3DSurface_ModifyLocation(iface, flag, FALSE);
+#ifdef VBOX_WITH_WDDM
+        if (VBOXSHRC_IS_SHARED_OPENED(This))
+        {
+            fPersistent = TRUE;
+        }
+#endif
+        IWineD3DSurface_ModifyLocation(iface, flag, fPersistent);
     }
 
@@ -590,4 +597,7 @@
     if (VBOXSHRC_IS_SHARED(This))
     {
+        Assert(VBOXSHRC_GET_SHAREHANDLE(This) == NULL
+                || (GLuint)VBOXSHRC_GET_SHAREHANDLE(This) == new_name
+                || new_name == 0 /* on cleanup */);
         VBOXSHRC_SET_SHAREHANDLE(This, new_name);
     }
@@ -1291,7 +1301,12 @@
             GLclampf tmp;
             tmp = 0.9f;
+#ifndef VBOX_WITH_WDDM
             ENTER_GL();
             glPrioritizeTextures(1, &This->texture_name, &tmp);
             LEAVE_GL();
+#else
+            /* chromium code on host fails to resolve texture name to texture obj for some reason
+             * @todo: investigate */
+#endif
         }
 
@@ -1407,12 +1422,11 @@
         ENTER_GL();
 #ifdef VBOX_WITH_WDDM
-        if (!VBOXSHRC_IS_SHARED_OPENED(This))
+        if (VBOXSHRC_CAN_DELETE(device, This))
 #endif
+        {
             glDeleteTextures(1, &This->texture_name);
+            glDeleteTextures(1, &This->texture_name_srgb);
+        }
         This->texture_name = 0;
-#ifdef VBOX_WITH_WDDM
-        if (!VBOXSHRC_IS_SHARED_OPENED(This))
-#endif
-            glDeleteTextures(1, &This->texture_name_srgb);
         This->texture_name_srgb = 0;
         LEAVE_GL();
@@ -1767,4 +1781,21 @@
 }
 
+#ifdef VBOX_WITH_WDDM
+void surface_setup_location_onopen(IWineD3DSurfaceImpl *This)
+{
+    IWineD3DDeviceImpl *device = This->resource.device;
+    const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
+    struct wined3d_context * context = NULL;
+    if (!device->isInDraw) context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
+
+    surface_prepare_texture(This, gl_info, FALSE);
+    surface_bind_and_dirtify(This, FALSE);
+
+    if (context) context_release(context);
+
+    IWineD3DSurface_ModifyLocation((IWineD3DSurface*)This, SFLAG_INTEXTURE, TRUE);
+}
+#endif
+
 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This)
 {
@@ -2807,5 +2838,5 @@
                 if (VBOXSHRC_IS_SHARED_OPENED(This))
                 {
-                    *name = VBOXSHRC_GET_SHAREHANDLE(This);
+                    *name = (GLuint)VBOXSHRC_GET_SHAREHANDLE(This);
                 }
                 else
@@ -4791,4 +4822,14 @@
         }
     }
+    else if (This->Flags & SFLAG_CLIENTMEM)
+    {
+        if(!(This->Flags & SFLAG_INSYSMEM)) {
+            IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
+        } else {
+            This->Flags &= ~SFLAG_LOCATIONS;
+            This->Flags |= SFLAG_INSYSMEM;
+        }
+
+    }
 #endif
 
@@ -5120,4 +5161,10 @@
 //        }
     }
+    else if (This->Flags & SFLAG_CLIENTMEM)
+    {
+        Assert(!!(This->Flags & SFLAG_INSYSMEM));
+        This->Flags &= ~SFLAG_LOCATIONS;
+        This->Flags |= SFLAG_INSYSMEM;
+    }
     else
 #endif
@@ -5216,4 +5263,7 @@
     IWineD3DSurfaceImpl_UnLoad,
     IWineD3DBaseSurfaceImpl_GetType,
+#ifdef VBOX_WITH_WDDM
+    IWineD3DResourceImpl_SetDontDeleteGl,
+#endif
     /* IWineD3DSurface */
     IWineD3DBaseSurfaceImpl_GetContainer,
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface_gdi.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface_gdi.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface_gdi.c	(revision 38982)
@@ -666,4 +666,7 @@
     IWineGDISurfaceImpl_UnLoad,
     IWineD3DBaseSurfaceImpl_GetType,
+#ifdef VBOX_WITH_WDDM
+    IWineD3DResourceImpl_SetDontDeleteGl,
+#endif
     /* IWineD3DSurface */
     IWineD3DBaseSurfaceImpl_GetContainer,
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c	(revision 38982)
@@ -231,4 +231,29 @@
 }
 
+#ifdef VBOX_WITH_WDDM
+static HRESULT WINAPI IWineD3DTextureImpl_SetDontDeleteGl(IWineD3DTexture *iface) {
+    IWineD3DTextureImpl *This = (IWineD3DTextureImpl*)iface;
+    HRESULT hr = IWineD3DResourceImpl_SetDontDeleteGl((IWineD3DResource*)iface);
+    unsigned int i;
+
+    if (FAILED(hr))
+    {
+        ERR("IWineD3DResource_SetDontDeleteGl failed");
+        return hr;
+    }
+
+    for (i = 0; i < This->baseTexture.levels; ++i)
+    {
+        if (This->surfaces[i])
+        {
+            HRESULT tmpHr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->surfaces[i]);
+            Assert(tmpHr == S_OK);
+        }
+    }
+
+    return WINED3D_OK;
+}
+#endif
+
 /* ******************************************************
    IWineD3DTexture IWineD3DBaseTexture parts follow
@@ -289,4 +314,5 @@
             surface_set_texture_name(This->surfaces[i], gl_tex->name, This->baseTexture.is_srgb);
         }
+
         /* Conditinal non power of two textures use a different clamping default. If we're using the GL_WINE_normalized_texrect
          * partial driver emulation, we're dealing with a GL_TEXTURE_2D texture which has the address mode set to repeat - something
@@ -295,14 +321,19 @@
          */
         if(IWineD3DBaseTexture_IsCondNP2(iface)) {
-            ENTER_GL();
-            glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-            checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
-            glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-            checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
-            glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-            checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
-            glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-            checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
-            LEAVE_GL();
+#ifdef VBOX_WITH_WDDM
+            if (!VBOXSHRC_IS_SHARED_OPENED(This))
+#endif
+            {
+                ENTER_GL();
+                glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+                checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
+                glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+                checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
+                glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+                checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
+                glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+                checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
+                LEAVE_GL();
+            }
             gl_tex->states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_CLAMP;
             gl_tex->states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_CLAMP;
@@ -419,4 +450,7 @@
     IWineD3DTextureImpl_UnLoad,
     IWineD3DTextureImpl_GetType,
+#ifdef VBOX_WITH_WDDM
+    IWineD3DTextureImpl_SetDontDeleteGl,
+#endif
     /* IWineD3DBaseTexture */
     IWineD3DTextureImpl_SetLOD,
@@ -632,19 +666,23 @@
         }
 #endif
-        IWineD3DSurface_LoadLocation(texture->surfaces[0], SFLAG_INTEXTURE, NULL);
-        if (!VBOXSHRC_IS_SHARED_OPENED(texture))
-        {
-            Assert(!(*shared_handle));
-            *shared_handle = VBOXSHRC_GET_SHAREHANDLE(texture);
-        }
-        else
-        {
-            Assert(*shared_handle);
-            Assert(*shared_handle == VBOXSHRC_GET_SHAREHANDLE(texture));
+        for (i = 0; i < texture->baseTexture.levels; ++i)
+        {
+            if (!VBOXSHRC_IS_SHARED_OPENED(texture))
+            {
+                IWineD3DSurface_LoadLocation(texture->surfaces[i], SFLAG_INTEXTURE, NULL);
+                Assert(!(*shared_handle));
+                *shared_handle = VBOXSHRC_GET_SHAREHANDLE(texture);
+            }
+            else
+            {
+                surface_setup_location_onopen((IWineD3DSurfaceImpl*)texture->surfaces[i]);
+                Assert(*shared_handle);
+                Assert(*shared_handle == VBOXSHRC_GET_SHAREHANDLE(texture));
+            }
         }
 #ifdef DEBUG
         for (i = 0; i < texture->baseTexture.levels; ++i)
         {
-            Assert((*shared_handle) == ((IWineD3DSurfaceImpl*)texture->surfaces[i])->texture_name);
+            Assert((GLuint)(*shared_handle) == ((IWineD3DSurfaceImpl*)texture->surfaces[i])->texture_name);
         }
 #endif
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h	(revision 38982)
@@ -19,4 +19,5 @@
 #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_DONT_DELETE         0x00000004 /* don't delete gl resources on d3d resource deletion */
 
 #define VBOXSHRC_GET_SHAREFLAFS(_o) ((_o)->resource.sharerc_flags)
@@ -24,12 +25,18 @@
 #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); \
-        VBOXSHRC_SET_SHAREHANDLE(_oDst, VBOXSHRC_GET_SHAREFLAFS(_oSrc)); \
+        VBOXSHRC_GET_SHAREFLAFS(_oDst) = VBOXSHRC_GET_SHAREFLAFS(_oSrc);   \
+        VBOXSHRC_SET_SHAREHANDLE(_oDst, VBOXSHRC_GET_SHAREHANDLE(_oSrc)); \
     } while (0)
 #define VBOXSHRC_SET_SHARED(_o) (VBOXSHRC_GET_SHAREFLAFS(_o) |= VBOXSHRC_F_SHARED)
 #define VBOXSHRC_SET_SHARED_OPENED(_o) (VBOXSHRC_GET_SHAREFLAFS(_o) |= VBOXSHRC_F_SHARED_OPENED)
+#define VBOXSHRC_SET_DONT_DELETE(_o) (VBOXSHRC_GET_SHAREFLAFS(_o) |= VBOXSHRC_F_DONT_DELETE)
+
 #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_SHARED_UNLOCKED(_o) (VBOXSHRC_IS_SHARED(_o) && !VBOXSHRC_IS_LOCKED(_o))
+#define VBOXSHRC_CAN_DELETE(_d, _o) ( \
+        !VBOXSHRC_IS_SHARED(_o) \
+        || !(VBOXSHRC_GET_SHAREFLAFS(_o) & VBOXSHRC_F_DONT_DELETE) \
+    )
 
 #define VBOXSHRC_LOCK(_o) do{ \
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/volume.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/volume.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/volume.c	(revision 38982)
@@ -381,4 +381,7 @@
     IWineD3DVolumeImpl_UnLoad,
     IWineD3DVolumeImpl_GetType,
+#ifdef VBOX_WITH_WDDM
+    IWineD3DResourceImpl_SetDontDeleteGl,
+#endif
     /* IWineD3DVolume */
     IWineD3DVolumeImpl_GetContainer,
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/volumetexture.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/volumetexture.c	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/volumetexture.c	(revision 38982)
@@ -339,4 +339,7 @@
     IWineD3DVolumeTextureImpl_UnLoad,
     IWineD3DVolumeTextureImpl_GetType,
+#ifdef VBOX_WITH_WDDM
+    IWineD3DResourceImpl_SetDontDeleteGl,
+#endif
     /* BaseTexture */
     IWineD3DVolumeTextureImpl_SetLOD,
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 38930)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 38982)
@@ -1887,4 +1887,8 @@
         const void *data, DWORD data_size, DWORD flags) DECLSPEC_HIDDEN;
 
+#ifdef VBOX_WITH_WDDM
+HRESULT WINAPI IWineD3DResourceImpl_SetDontDeleteGl(IWineD3DResource *iface) ;
+#endif
+
 /* Tests show that the start address of resources is 32 byte aligned */
 #define RESOURCE_ALIGNMENT 16
@@ -1951,4 +1955,7 @@
 void surface_prepare_texture(IWineD3DSurfaceImpl *surface,
         const struct wined3d_gl_info *gl_info, BOOL srgb) DECLSPEC_HIDDEN;
+#ifdef VBOX_WITH_WDDM
+void surface_setup_location_onopen(IWineD3DSurfaceImpl *This) DECLSPEC_HIDDEN;
+#endif
 
 typedef struct IWineD3DBaseTextureImpl
