Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 40388)
@@ -3130,7 +3130,7 @@
             if (RT_SUCCESS(rc))
             {
-                rc = VbglR3Init();
-                AssertRC(rc);
-                if (RT_SUCCESS(rc))
+//                rc = VbglR3Init();
+//                AssertRC(rc);
+//                if (RT_SUCCESS(rc))
                 {
                     HRESULT hr = vboxDispCmInit();
@@ -3146,5 +3146,5 @@
                         }
                     }
-                    VbglR3Term();
+//                    VbglR3Term();
                 }
             }
@@ -3169,5 +3169,5 @@
                 if (hr == S_OK)
                 {
-                    VbglR3Term();
+//                    VbglR3Term();
                     /// @todo RTR3Term();
                     return TRUE;
@@ -5672,13 +5672,23 @@
         for (UINT i = 0; i < pRc->cAllocations; ++i)
         {
-            BOOL fSetDontDelete = FALSE;
+            BOOL fSetDelete = FALSE;
             PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[i];
             if (pAlloc->hSharedHandle)
             {
+                /* using one and the same shared resource by different clients could lead to the situation where one client can still refer to the resource
+                 * while another one has deleted it
+                 * this could lead to gl state corruption on both host and guest side.
+                 * This is why we take extra care to avoid it.
+                 * Until we do a vboxWddmShRcRefAlloc call below the resource is guarantied to be present, however it can be removed any time after the call
+                 * if cShRcRefs is non-zero, i.e. the current cliet is not the one deleting it.
+                 * We first explicitely say to wine that resource must NOT be accessed any more and that all references to it should be cleaned */
+                pAdapter->D3D.pfnVBoxWineExD3DRc9SetShRcState((IDirect3DResource9*)pAlloc->pD3DIf, VBOXWINEEX_SHRC_STATE_GL_DISABLE);
+
                 DWORD cShRcRefs;
                 HRESULT tmpHr = vboxWddmShRcRefAlloc(pDevice, pAlloc, FALSE, &cShRcRefs);
-                if (cShRcRefs)
-                {
-                    fSetDontDelete = TRUE;
+                if (!cShRcRefs)
+                {
+                    /* the current client IS the one deleting this resource */
+                    fSetDelete = TRUE;
                 }
 #ifdef DEBUG_misha
@@ -5692,8 +5702,8 @@
             }
 
-            if (fSetDontDelete)
+            if (fSetDelete)
             {
                 Assert(pAlloc->pD3DIf);
-                pAdapter->D3D.pfnVBoxWineExD3DRc9SetDontDeleteGl((IDirect3DResource9*)pAlloc->pD3DIf);
+                pAdapter->D3D.pfnVBoxWineExD3DRc9SetShRcState((IDirect3DResource9*)pAlloc->pD3DIf, VBOXWINEEX_SHRC_STATE_GL_DELETE);
             }
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp	(revision 40388)
@@ -81,8 +81,8 @@
         }
 
-        pD3D->pfnVBoxWineExD3DRc9SetDontDeleteGl = (PFNVBOXWINEEXD3DRC9_SETDONTDELETEGL)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DRc9SetDontDeleteGl");
-        if (!pD3D->pfnVBoxWineExD3DRc9SetDontDeleteGl)
+        pD3D->pfnVBoxWineExD3DRc9SetShRcState = (PFNVBOXWINEEXD3DRC9_SETSHRCSTATE)GetProcAddress(pD3D->hD3DLib, "VBoxWineExD3DRc9SetShRcState");
+        if (!pD3D->pfnVBoxWineExD3DRc9SetShRcState)
         {
-            WARN(("no VBoxWineExD3DRc9SetDontDeleteGl"));
+            WARN(("no VBoxWineExD3DRc9SetShRcState"));
             break;
         }
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h	(revision 40388)
@@ -45,5 +45,5 @@
     PFNVBOXWINEEXD3DDEV9_UPDATE pfnVBoxWineExD3DDev9Update;
 
-    PFNVBOXWINEEXD3DRC9_SETDONTDELETEGL pfnVBoxWineExD3DRc9SetDontDeleteGl;
+    PFNVBOXWINEEXD3DRC9_SETSHRCSTATE pfnVBoxWineExD3DRc9SetShRcState;
 
     PFNVBOXWINEEXD3DSWAPCHAIN9_PRESENT pfnVBoxWineExD3DSwapchain9Present;
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/Makefile.kmk
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/Makefile.kmk	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/Makefile.kmk	(revision 40388)
@@ -189,5 +189,5 @@
 wined3dwddm_INCS           = vbox/libWineStub/include
 wined3dwddm_DEFS           = $(subst VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT,,$(wined3d_DEFS))
-wined3dwddm_DEFS          += VBOX_WITH_WDDM VBOX_WINE_WITH_SINGLE_CONTEXT
+wined3dwddm_DEFS          += VBOX_WITH_WDDM VBOX_WINE_WITH_SINGLE_CONTEXT IN_VBOXLIBWINE
 wined3dwddm_SOURCES        = $(subst wined3d.def,wined3dwddm.def,$(wined3d_SOURCES)) vbox/VBoxDbgGl.c
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/d3d9_private.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/d3d9_private.h	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/d3d9_private.h	(revision 40388)
@@ -48,7 +48,4 @@
 #include "wine/wined3d.h"
 
-#ifdef VBOX_WITH_WDDM
-#include "../vbox/VBoxWineEx.h"
-#endif
 
 /* ===========================================================================
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/d3d9wddm.def
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/d3d9wddm.def	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/d3d9wddm.def	(revision 40388)
@@ -17,3 +17,3 @@
   VBoxWineExD3DDev9Update
   VBoxWineExD3DSwapchain9Present
-  VBoxWineExD3DRc9SetDontDeleteGl
+  VBoxWineExD3DRc9SetShRcState
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c	(revision 40388)
@@ -3282,5 +3282,5 @@
 
 #ifdef VBOX_WITH_WDDM
-VBOXWINEEX_DECL(HRESULT) VBoxWineExD3DRc9SetDontDeleteGl(IDirect3DResource9 *iface)
+VBOXWINEEX_DECL(HRESULT) VBoxWineExD3DRc9SetShRcState(IDirect3DResource9 *iface, VBOXWINEEX_SHRC_STATE enmState)
 {
     D3DRESOURCETYPE enmType = IDirect3DResource9_GetType(iface);
@@ -3292,5 +3292,5 @@
             IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl*)iface ;
             wined3d_mutex_lock();
-            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DSurface);
+            hr = IWineD3DResource_SetShRcState((IWineD3DResource*)This->wineD3DSurface, enmState);
             wined3d_mutex_unlock();
             break;
@@ -3300,5 +3300,5 @@
             IDirect3DVolume9Impl *This = (IDirect3DVolume9Impl*)iface ;
             wined3d_mutex_lock();
-            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DVolume);
+            hr = IWineD3DResource_SetShRcState((IWineD3DResource*)This->wineD3DVolume, enmState);
             wined3d_mutex_unlock();
             break;
@@ -3308,5 +3308,5 @@
             IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl*)iface ;
             wined3d_mutex_lock();
-            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DTexture);
+            hr = IWineD3DResource_SetShRcState((IWineD3DResource*)This->wineD3DTexture, enmState);
             wined3d_mutex_unlock();
             break;
@@ -3316,5 +3316,5 @@
             IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl*)iface ;
             wined3d_mutex_lock();
-            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DVolumeTexture);
+            hr = IWineD3DResource_SetShRcState((IWineD3DResource*)This->wineD3DVolumeTexture, enmState);
             wined3d_mutex_unlock();
             break;
@@ -3324,5 +3324,5 @@
             IDirect3DCubeTexture9Impl *This = (IDirect3DCubeTexture9Impl*)iface ;
             wined3d_mutex_lock();
-            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DCubeTexture);
+            hr = IWineD3DResource_SetShRcState((IWineD3DResource*)This->wineD3DCubeTexture, enmState);
             wined3d_mutex_unlock();
             break;
@@ -3332,5 +3332,5 @@
             IDirect3DVertexBuffer9Impl *This = (IDirect3DVertexBuffer9Impl*)iface ;
             wined3d_mutex_lock();
-            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DVertexBuffer);
+            hr = IWineD3DResource_SetShRcState((IWineD3DResource*)This->wineD3DVertexBuffer, enmState);
             wined3d_mutex_unlock();
             break;
@@ -3340,5 +3340,5 @@
             IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl*)iface ;
             wined3d_mutex_lock();
-            hr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->wineD3DIndexBuffer);
+            hr = IWineD3DResource_SetShRcState((IWineD3DResource*)This->wineD3DIndexBuffer, enmState);
             wined3d_mutex_unlock();
             break;
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/wined3d.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/wined3d.h	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/wined3d.h	(revision 40388)
@@ -3434,6 +3434,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -3461,5 +3462,5 @@
 #define IWineD3DResource_GetType(This) (This)->lpVtbl->GetType(This)
 # ifdef VBOX_WITH_WDDM
-#  define IWineD3DResource_SetDontDeleteGl(This) (This)->lpVtbl->SetDontDeleteGl(This)
+#  define IWineD3DResource_SetShRcState(This, enmState) (This)->lpVtbl->SetShRcState(This, enmState)
 # endif
 #endif
@@ -4069,6 +4070,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -4689,6 +4691,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -4908,6 +4911,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -5159,6 +5163,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DTexture* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DTexture* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -5410,6 +5415,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DCubeTexture* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DCubeTexture* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -5665,6 +5671,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -6426,6 +6433,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/VBoxWineEx.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/VBoxWineEx.h	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/VBoxWineEx.h	(revision 40388)
@@ -15,4 +15,15 @@
 #ifndef ___VBoxWineEx_h__
 #define ___VBoxWineEx_h__
+
+typedef enum
+{
+    VBOXWINEEX_SHRC_STATE_UNDEFINED = 0,
+    /* the underlying GL resource can not be used because it can be removed concurrently by other SHRC client */
+    VBOXWINEEX_SHRC_STATE_GL_DISABLE,
+    /* the given client is requested to delete the underlying GL resource on SHRC termination */
+    VBOXWINEEX_SHRC_STATE_GL_DELETE
+} VBOXWINEEX_SHRC_STATE;
+
+#ifndef IN_VBOXLIBWINE
 
 #define VBOXWINEEX_VERSION 1
@@ -42,6 +53,6 @@
 typedef FNVBOXWINEEXD3DDEV9_UPDATE *PFNVBOXWINEEXD3DDEV9_UPDATE;
 
-typedef VBOXWINEEX_DECL(HRESULT) FNVBOXWINEEXD3DRC9_SETDONTDELETEGL(IDirect3DResource9 *iface);
-typedef FNVBOXWINEEXD3DRC9_SETDONTDELETEGL *PFNVBOXWINEEXD3DRC9_SETDONTDELETEGL;
+typedef VBOXWINEEX_DECL(HRESULT) FNVBOXWINEEXD3DRC9_SETSHRCSTATE(IDirect3DResource9 *iface, VBOXWINEEX_SHRC_STATE enmState);
+typedef FNVBOXWINEEXD3DRC9_SETSHRCSTATE *PFNVBOXWINEEXD3DRC9_SETSHRCSTATE;
 
 typedef VBOXWINEEX_DECL(HRESULT) FNVBOXWINEEXD3DSWAPCHAIN9_PRESENT(IDirect3DSwapChain9 *iface, IDirect3DSurface9 *surf);
@@ -69,5 +80,5 @@
                                                     IDirect3DDevice9Ex **outIface); /* update device parameters */
 
-VBOXWINEEX_DECL(HRESULT) VBoxWineExD3DRc9SetDontDeleteGl(IDirect3DResource9 *iface);
+VBOXWINEEX_DECL(HRESULT) VBoxWineExD3DRc9SetShRcState(IDirect3DResource9 *iface, VBOXWINEEX_SHRC_STATE enmState);
 
 VBOXWINEEX_DECL(HRESULT) VBoxWineExD3DSwapchain9Present(IDirect3DSwapChain9 *iface,
@@ -77,3 +88,5 @@
 #endif
 
+#endif /* #ifndef IN_VBOXLIBWINE */
+
 #endif
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 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/libWineStub/include/wine/wined3d.h	(revision 40388)
@@ -6,4 +6,8 @@
 #ifndef __WIDL_WINED3D_H
 #define __WIDL_WINED3D_H
+
+#ifdef VBOX_WITH_WDDM
+#include "../../../vbox/VBoxWineEx.h"
+#endif
 
 # define DECLSPEC_HIDDEN
@@ -3432,6 +3436,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -3459,5 +3464,5 @@
 #define IWineD3DResource_GetType(This) (This)->lpVtbl->GetType(This)
 # ifdef VBOX_WITH_WDDM
-#  define IWineD3DResource_SetDontDeleteGl(This) (This)->lpVtbl->SetDontDeleteGl(This)
+#  define IWineD3DResource_SetShRcState(This, enmState) (This)->lpVtbl->SetShRcState(This, enmState)
 # endif
 #endif
@@ -4067,6 +4072,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -4687,6 +4693,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -4906,6 +4913,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -5157,6 +5165,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -5408,6 +5417,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -5663,6 +5673,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
@@ -6424,6 +6435,7 @@
 
 #ifdef VBOX_WITH_WDDM
-    HRESULT (STDMETHODCALLTYPE *SetDontDeleteGl)(
-        IWineD3DResource* This);
+    HRESULT (STDMETHODCALLTYPE *SetShRcState)(
+        IWineD3DResource* This,
+        VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/basetexture.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/basetexture.c	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/basetexture.c	(revision 40388)
@@ -296,4 +296,8 @@
     TRACE("(%p) : About to bind texture\n", This);
 
+#ifdef VBOX_WITH_WDDM
+    Assert(!VBOXSHRC_IS_DISABLED(This));
+#endif
+
     This->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
     if(srgb) {
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/buffer.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/buffer.c	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/buffer.c	(revision 40388)
@@ -1450,5 +1450,5 @@
     buffer_GetType,
 #ifdef VBOX_WITH_WDDM
-    IWineD3DResourceImpl_SetDontDeleteGl,
+    IWineD3DResourceImpl_SetShRcState,
 #endif
     /* IWineD3DBuffer methods */
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/cubetexture.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/cubetexture.c	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/cubetexture.c	(revision 40388)
@@ -246,12 +246,12 @@
 
 #ifdef VBOX_WITH_WDDM
-static HRESULT WINAPI IWineD3DCubeTextureImpl_SetDontDeleteGl(IWineD3DCubeTexture *iface) {
+static HRESULT WINAPI IWineD3DCubeTextureImpl_SetShRcState(IWineD3DCubeTexture *iface, VBOXWINEEX_SHRC_STATE enmState) {
     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl*)iface;
-    HRESULT hr = IWineD3DResourceImpl_SetDontDeleteGl((IWineD3DResource*)iface);
+    HRESULT hr = IWineD3DResourceImpl_SetShRcState((IWineD3DResource*)iface, enmState);
     unsigned int i, j;
 
     if (FAILED(hr))
     {
-        ERR("IWineD3DResource_SetDontDeleteGl failed");
+        ERR("IWineD3DResource_SetShRcState failed");
         return hr;
     }
@@ -260,9 +260,11 @@
         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]);
+                HRESULT tmpHr = IWineD3DResource_SetShRcState((IWineD3DResource*)This->surfaces[j][i], enmState);
                 Assert(tmpHr == S_OK);
             }
         }
     }
+
+    device_cleanup_durtify_texture_target(This->resource.device, ((IWineD3DSurfaceImpl*)This->surfaces[j][i])->texture_target);
 
     return WINED3D_OK;
@@ -444,5 +446,5 @@
     IWineD3DCubeTextureImpl_GetType,
 #ifdef VBOX_WITH_WDDM
-    IWineD3DCubeTextureImpl_SetDontDeleteGl,
+    IWineD3DCubeTextureImpl_SetShRcState,
 #endif
     /* IWineD3DBaseTexture */
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c	(revision 40388)
@@ -7010,4 +7010,47 @@
     }
     return WINED3D_OK;
+}
+
+/* context activation is done by the caller */
+void device_cleanup_durtify_texture_target(IWineD3DDeviceImpl *This, GLuint texture_target)
+{
+    const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
+    unsigned int i;
+    GLint active_texture=GL_TEXTURE0_ARB;
+    ENTER_GL();
+    glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
+
+    for (i = 0; i < gl_info->limits.textures; ++i)
+    {
+        /* Make appropriate texture active */
+        GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
+        checkGLcall("glActiveTextureARB");
+
+        /* don't do  glGet GL_TEXTURE_BINDING_xxx just ensure nothing is bound to IWineD3DSurfaceImpl::texture_target,
+         * and dirtify the state later */
+        if (texture_target == GL_TEXTURE_2D)
+            glBindTexture(texture_target, This->dummyTextureName[i]);
+        else
+            glBindTexture(texture_target, 0);
+    }
+
+    /* restore tha active texture unit */
+    GL_EXTCALL(glActiveTextureARB(active_texture));
+    checkGLcall("glActiveTextureARB");
+    LEAVE_GL();
+
+    /* dirtify */
+    for (i = 0; i < gl_info->limits.textures; ++i)
+    {
+        DWORD active_sampler = This->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB + i];
+
+        if (active_sampler != WINED3D_UNMAPPED_STAGE)
+        {
+            IWineD3DDeviceImpl_MarkStateDirty(This, STATE_SAMPLER(active_sampler));
+        }
+    }
+
+    /* do flush to ensure this all goes to host */
+    wglFlush();
 }
 #endif
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/resource.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/resource.c	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/resource.c	(revision 40388)
@@ -288,5 +288,5 @@
 
 #ifdef VBOX_WITH_WDDM
-HRESULT WINAPI IWineD3DResourceImpl_SetDontDeleteGl(IWineD3DResource *iface) {
+HRESULT WINAPI IWineD3DResourceImpl_SetShRcState(IWineD3DResource *iface, VBOXWINEEX_SHRC_STATE enmState) {
     IWineD3DResourceImpl *This = (IWineD3DResourceImpl*)iface;
     if (!VBOXSHRC_IS_SHARED(This))
@@ -296,6 +296,18 @@
     }
 
-    VBOXSHRC_SET_DONT_DELETE(This);
-    return WINED3D_OK;
-}
-#endif
+    switch (enmState)
+    {
+        case VBOXWINEEX_SHRC_STATE_GL_DISABLE:
+            VBOXSHRC_SET_DISABLE(This);
+            break;
+        case VBOXWINEEX_SHRC_STATE_GL_DELETE:
+            VBOXSHRC_SET_DELETE(This);
+            break;
+        default:
+            ERR("invalid arg");
+            return E_INVALIDARG;
+    }
+
+    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 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 40388)
@@ -672,4 +672,5 @@
     IWineD3DSurface_BindTexture((IWineD3DSurface *)This, srgb);
 }
+
 
 /* This function checks if the primary render target uses the 8bit paletted format. */
@@ -2868,4 +2869,8 @@
     IWineD3DBaseTexture *baseTexture = NULL;
 
+#ifdef VBOX_WITH_WDDM
+    Assert(!VBOXSHRC_IS_DISABLED(This));
+#endif
+
     TRACE("(%p)Checking to see if the container is a base texture\n", This);
     if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
@@ -5344,4 +5349,53 @@
 }
 
+#ifdef VBOX_WITH_WDDM
+static HRESULT WINAPI IWineD3DSurfaceImpl_SetShRcState(IWineD3DSurface *iface, VBOXWINEEX_SHRC_STATE enmState) {
+    IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl*)iface;
+    IWineD3DBaseTextureImpl *texture = NULL;
+    HRESULT hr;
+    unsigned int i;
+
+    if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture)))
+    {
+        /* this is a texture, check that the */
+        switch (enmState)
+        {
+            case VBOXWINEEX_SHRC_STATE_GL_DISABLE:
+                if (!VBOXSHRC_IS_DISABLED(texture))
+                {
+                    ERR("directly doing SetShRcState for texture surface not allowed!");
+                    return E_FAIL;
+                }
+                break;
+            case VBOXWINEEX_SHRC_STATE_GL_DELETE:
+                if (!VBOXSHRC_IS_DELETE(texture))
+                {
+                    ERR("directly doing SetShRcState for texture surface not allowed!");
+                    return E_FAIL;
+                }
+                break;
+            default:
+                ERR("invalid arg");
+                return E_INVALIDARG;
+        }
+
+        IWineD3DBaseTexture_Release(texture);
+    }
+
+
+    hr = IWineD3DResourceImpl_SetShRcState((IWineD3DResource*)iface, enmState);
+    if (FAILED(hr))
+    {
+        ERR("IWineD3DResource_SetShRcState failed");
+        return hr;
+    }
+
+    if (!texture)
+        device_cleanup_durtify_texture_target(This->resource.device, This->texture_target);
+
+    return WINED3D_OK;
+}
+#endif
+
 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
 {
@@ -5361,5 +5415,5 @@
     IWineD3DBaseSurfaceImpl_GetType,
 #ifdef VBOX_WITH_WDDM
-    IWineD3DResourceImpl_SetDontDeleteGl,
+    IWineD3DSurfaceImpl_SetShRcState,
 #endif
     /* IWineD3DSurface */
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface_gdi.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface_gdi.c	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface_gdi.c	(revision 40388)
@@ -667,5 +667,5 @@
     IWineD3DBaseSurfaceImpl_GetType,
 #ifdef VBOX_WITH_WDDM
-    IWineD3DResourceImpl_SetDontDeleteGl,
+    IWineD3DResourceImpl_SetShRcState,
 #endif
     /* IWineD3DSurface */
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c	(revision 40388)
@@ -232,12 +232,12 @@
 
 #ifdef VBOX_WITH_WDDM
-static HRESULT WINAPI IWineD3DTextureImpl_SetDontDeleteGl(IWineD3DTexture *iface) {
+static HRESULT WINAPI IWineD3DTextureImpl_SetShRcState(IWineD3DTexture *iface, VBOXWINEEX_SHRC_STATE enmState) {
     IWineD3DTextureImpl *This = (IWineD3DTextureImpl*)iface;
-    HRESULT hr = IWineD3DResourceImpl_SetDontDeleteGl((IWineD3DResource*)iface);
+    HRESULT hr = IWineD3DResourceImpl_SetShRcState((IWineD3DResource*)iface, enmState);
     unsigned int i;
 
     if (FAILED(hr))
     {
-        ERR("IWineD3DResource_SetDontDeleteGl failed");
+        ERR("IWineD3DResource_SetShRcState failed");
         return hr;
     }
@@ -247,8 +247,10 @@
         if (This->surfaces[i])
         {
-            HRESULT tmpHr = IWineD3DResource_SetDontDeleteGl((IWineD3DResource*)This->surfaces[i]);
+            HRESULT tmpHr = IWineD3DResource_SetShRcState((IWineD3DResource*)This->surfaces[i], enmState);
             Assert(tmpHr == S_OK);
         }
     }
+
+    device_cleanup_durtify_texture_target(This->resource.device, This->target);
 
     return WINED3D_OK;
@@ -299,4 +301,8 @@
 
     TRACE("(%p) : relay to BaseTexture\n", This);
+
+#ifdef VBOX_WITH_WDDM
+    Assert(!VBOXSHRC_IS_DISABLED(This));
+#endif
 
     hr = basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &set_gl_texture_desc);
@@ -451,5 +457,5 @@
     IWineD3DTextureImpl_GetType,
 #ifdef VBOX_WITH_WDDM
-    IWineD3DTextureImpl_SetDontDeleteGl,
+    IWineD3DTextureImpl_SetShRcState,
 #endif
     /* IWineD3DBaseTexture */
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h	(revision 40388)
@@ -19,5 +19,6 @@
 #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_F_GL_DISABLE          0x00000004 /* don't delete gl resources on d3d resource deletion */
+#define VBOXSHRC_F_GL_DELETE           0x00000008 /* don't delete gl resources on d3d resource deletion */
 
 #define VBOXSHRC_GET_SHAREFLAFS(_o) ((_o)->resource.sharerc_flags)
@@ -30,13 +31,20 @@
 #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_SET_DISABLE(_o) (VBOXSHRC_GET_SHAREFLAFS(_o) |= VBOXSHRC_F_GL_DISABLE)
+#define VBOXSHRC_SET_DELETE(_o) (VBOXSHRC_GET_SHAREFLAFS(_o) = (VBOXSHRC_GET_SHAREFLAFS(_o) | VBOXSHRC_F_GL_DELETE) & (~VBOXSHRC_F_GL_DISABLE))
 
 #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_IS_DISABLED(_o) ( \
+        VBOXSHRC_IS_SHARED(_o) \
+        && (VBOXSHRC_GET_SHAREFLAFS(_o) & VBOXSHRC_F_GL_DISABLE) \
     )
+#define VBOXSHRC_IS_DELETE(_o) ( \
+        VBOXSHRC_IS_SHARED(_o) \
+        && (VBOXSHRC_GET_SHAREFLAFS(_o) & VBOXSHRC_F_GL_DELETE) \
+    )
+
+#define VBOXSHRC_CAN_DELETE(_d, _o) (!VBOXSHRC_IS_DISABLED(_o))
 
 #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 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/volume.c	(revision 40388)
@@ -382,5 +382,5 @@
     IWineD3DVolumeImpl_GetType,
 #ifdef VBOX_WITH_WDDM
-    IWineD3DResourceImpl_SetDontDeleteGl,
+    IWineD3DResourceImpl_SetShRcState,
 #endif
     /* IWineD3DVolume */
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/volumetexture.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/volumetexture.c	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/volumetexture.c	(revision 40388)
@@ -340,5 +340,5 @@
     IWineD3DVolumeTextureImpl_GetType,
 #ifdef VBOX_WITH_WDDM
-    IWineD3DResourceImpl_SetDontDeleteGl,
+    IWineD3DResourceImpl_SetShRcState,
 #endif
     /* BaseTexture */
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 40387)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 40388)
@@ -1811,4 +1811,6 @@
 LRESULT device_process_message(IWineD3DDeviceImpl *device, HWND window,
         UINT message, WPARAM wparam, LPARAM lparam, WNDPROC proc) DECLSPEC_HIDDEN;
+#else
+void device_cleanup_durtify_texture_target(IWineD3DDeviceImpl *This, GLuint texture_target);
 #endif
 void device_resource_add(IWineD3DDeviceImpl *This, IWineD3DResource *resource) DECLSPEC_HIDDEN;
@@ -1902,5 +1904,5 @@
 
 #ifdef VBOX_WITH_WDDM
-HRESULT WINAPI IWineD3DResourceImpl_SetDontDeleteGl(IWineD3DResource *iface) ;
+HRESULT WINAPI IWineD3DResourceImpl_SetShRcState(IWineD3DResource *iface, VBOXWINEEX_SHRC_STATE enmState);
 #endif
 
Index: /trunk/src/VBox/GuestHost/OpenGL/util/error.c
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/util/error.c	(revision 40387)
+++ /trunk/src/VBox/GuestHost/OpenGL/util/error.c	(revision 40388)
@@ -251,5 +251,5 @@
         va_end( args );
 
-#if defined(WINDOWS) && defined(DEBUG)/* && !defined(IN_GUEST) */
+#if defined(WINDOWS) && defined(DEBUG) && !defined(IN_GUEST)
         DebugBreak();
 #endif
