Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 41636)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 41637)
@@ -35,81 +35,153 @@
 
 #ifdef VBOX_WDDMDISP_WITH_PROFILE
-#include "VBoxDispProfile.h"
+
+volatile uint32_t g_u32VBoxDispProfileFunctionLoggerIndex = 0;
+
+struct VBOXDISPPROFILE_GLOBAL {
+    VBoxDispProfileFpsCounter ProfileDdiFps;
+    VBoxDispProfileSet ProfileDdiFunc;
+} g_VBoxDispProfile;
 
 /* uncomment to enable particular logging */
-#define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_ENABLE
-//#define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_ENABLE
-
-#ifdef VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_ENABLE
-static VBoxDispProfileSet g_VBoxDispProfileDDI("D3D_DDI");
-#define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_PROLOGUE() VBOXDISPPROFILE_FUNCTION_LOGGER_DEFINE(g_VBoxDispProfileDDI)
-#define VBOXDDIROFILE_FUNCTION_LOGGER_DUMP() do {\
-        g_VBoxDispProfileDDI.dump(_pDev); \
+/* allows dumping fps + how much time is spent in ddi functions in comparison with the rest time */
+//# define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_ENABLE
+/* allows dumping time spent in each function and the number of calls made for any given function */
+# define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_ENABLE
+
+# ifdef VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_ENABLE
+
+extern volatile uint32_t g_u322VBoxDispProfileFunctionLoggerIndex = 0;
+
+//static VBoxDispProfileSet g_VBoxDispProfileDDI("D3D_DDI");
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_PROLOGUE(_pObj) VBOXDISPPROFILE_FUNCTION_LOGGER_DEFINE((_pObj)->ProfileDdiFunc)
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DUMP(_pObj) do {\
+        (_pObj)->ProfileDdiFunc.dump(_pObj); \
     } while (0)
-#define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_RESET() do {\
-        g_VBoxDispProfileDDI.resetEntries();\
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_RESET(_pObj) do {\
+        (_pObj)->ProfileDdiFunc.resetEntries();\
     } while (0)
-#define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DISABLE_CURRENT() do {\
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DISABLE_CURRENT() do {\
         VBOXDISPPROFILE_FUNCTION_LOGGER_DISABLE_CURRENT();\
     } while (0)
 
-
-#else
-#define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_PROLOGUE() do {} while(0)
-#define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DUMP() do {} while(0)
-#define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_RESET() do {} while(0)
-#define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DISABLE_CURRENT() do {} while (0)
-#endif
-
-#ifdef VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_ENABLE
-static VBoxDispProfileFpsCounter g_VBoxDispFpsDDI(64);
-#define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_PROLOGUE() VBOXDISPPROFILE_STATISTIC_LOGGER_DEFINE(&g_VBoxDispFpsDDI)
-#define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_DISABLE_CURRENT() do {\
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_LOG_AND_DISABLE_CURRENT() VBOXDISPPROFILE_FUNCTION_LOGGER_LOG_AND_DISABLE_CURRENT()
+
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_REPORT_FRAME(_pObj) do { \
+        if (!((_pObj)->ProfileDdiFunc.reportIteration() % 31) && !VBOXVDBG_IS_DWM()) {\
+            VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DUMP(_pObj); \
+            VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_RESET(_pObj); \
+        } \
+    } while (0)
+
+# else
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_PROLOGUE(_pObj) do {} while(0)
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DUMP(_pObj) do {} while(0)
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_RESET(_pObj) do {} while(0)
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DISABLE_CURRENT() do {} while (0)
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_LOG_AND_DISABLE_CURRENT() do {} while (0)
+#  define VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_REPORT_FRAME(_pDev) do {} while (0)
+# endif
+
+# ifdef VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_ENABLE
+//static VBoxDispProfileFpsCounter g_VBoxDispFpsDDI(64);
+#  define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_PROLOGUE(_pObj) VBOXDISPPROFILE_STATISTIC_LOGGER_DEFINE(&(_pObj)->ProfileDdiFps)
+#  define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_DISABLE_CURRENT() do {\
         VBOXDISPPROFILE_STATISTIC_LOGGER_DISABLE_CURRENT();\
     } while (0)
-#define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_REPORT_FRAME(_pDev) do { \
-        VBOXDISPPROFILE_STATISTIC_LOGGER_LOG_AND_DISABLE_CURRENT(); \
-        g_VBoxDispFpsDDI.ReportFrame(); \
-        if(!(g_VBoxDispFpsDDI.GetNumFrames() % 31)) \
+
+#  define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_DUMP(_pObj) do { \
+            double fps = (_pObj)->ProfileDdiFps.GetFps(); \
+            double cps = (_pObj)->ProfileDdiFps.GetCps(); \
+            double tup = (_pObj)->ProfileDdiFps.GetTimeProcPercent(); \
+            VBOXDISPPROFILE_DUMP(("[0x%p]: fps: %f, cps: %.1f, host %.1f%%", (_pObj), fps, cps, tup)); \
+    } while (0)
+
+#  define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_REPORT_FRAME(_pObj) do { \
+        (_pObj)->ProfileDdiFps.ReportFrame(); \
+        if(!((_pObj)->ProfileDdiFps.GetNumFrames() % 31)) \
         { \
-            double fps = g_VBoxDispFpsDDI.GetFps(); \
-            double cps = g_VBoxDispFpsDDI.GetCps(); \
-            double tup = g_VBoxDispFpsDDI.GetTimeProcPercent(); \
-            VBOXDISPPROFILE_DUMP(("fps: %f, cps: %.1f, host %.1f%%\n", fps, cps, tup)); \
+            VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_DUMP(_pObj); \
         } \
     } while (0)
+
+#  define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_LOG_AND_DISABLE_CURRENT() VBOXDISPPROFILE_STATISTIC_LOGGER_LOG_AND_DISABLE_CURRENT()
+# else
+#  define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_PROLOGUE(_pObj) do {} while(0)
+#  define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_DISABLE_CURRENT() do {} while (0)
+#  define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_LOG_AND_DISABLE_CURRENT() do {} while (0)
+#  define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_REPORT_FRAME(_pDev) do {} while (0)
+#  define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_DUMP(_pObj) do {} while (0)
+# endif
+
+# define VBOXDISPPROFILE_FUNCTION_DDI_PROLOGUE(_pObj) \
+        VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_PROLOGUE(_pObj); \
+        VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_PROLOGUE(_pObj);
+
+# define VBOXDISPPROFILE_DDI_LOG_AND_DISABLE_CURRENT() \
+        VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_LOG_AND_DISABLE_CURRENT(); \
+        VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_LOG_AND_DISABLE_CURRENT();
+
+# define VBOXDISPPROFILE_DDI_REPORT_FRAME(_pDev) do {\
+        VBOXDISPPROFILE_DDI_LOG_AND_DISABLE_CURRENT(); \
+        VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_REPORT_FRAME(_pDev); \
+        VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_REPORT_FRAME(_pDev); \
+    } while (0)
+
+# define VBOXDISPPROFILE_DDI_REPORT_FLUSH(_pDev) do {\
+        VBOXDISPPROFILE_DDI_LOG_AND_DISABLE_CURRENT(); \
+        VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_REPORT_FRAME(_pDev); \
+        VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_REPORT_FRAME(_pDev); \
+    } while (0)
+
+# define VBOXDISPPROFILE_DDI_INIT_CMN(_pObj, _name, _cEntries) do { \
+        (_pObj)->ProfileDdiFps = VBoxDispProfileFpsCounter(); \
+        (_pObj)->ProfileDdiFps.init(_cEntries); \
+        (_pObj)->ProfileDdiFunc = VBoxDispProfileSet(_name); \
+    } while (0)
+
+# define VBOXDISPPROFILE_DDI_TERM_CMN(_pObj) do { \
+        (_pObj)->ProfileDdiFps.term(); \
+    } while (0)
+
+# define VBOXDISPPROFILE_DDI_TERM(_pObj) do {\
+        VBOXDISPPROFILE_DDI_LOG_AND_DISABLE_CURRENT(); \
+        VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DUMP(_pObj); \
+        VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_RESET(_pObj); \
+        VBOXDISPPROFILE_DDI_TERM_CMN(_pObj); \
+    } while (0)
+
+# define VBOXDISPPROFILE_DDI_PRINT(_m) VBOXDISPPROFILE_DUMP(_m)
+
+# define VBOXDISPPROFILE_DDI_INIT_GLBL() VBOXDISPPROFILE_DDI_INIT_CMN(&g_VBoxDispProfile, "DDI_Adp", 64)
+# define VBOXDISPPROFILE_DDI_INIT_ADP(_pAdp) VBOXDISPPROFILE_DDI_INIT_CMN(_pAdp, "DDI_Adp", 64)
+# define VBOXDISPPROFILE_DDI_INIT_DEV(_pDev) VBOXDISPPROFILE_DDI_INIT_CMN(_pDev, "DDI_Dev", 64)
 #else
-#define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_PROLOGUE() do {} while(0)
-#define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_DISABLE_CURRENT() do {} while (0)
-#define VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_REPORT_FRAME(_pDev) do {} while (0)
-#endif
-
-#define VBOXDISPPROFILE_FUNCTION_DDI_PROLOGUE() \
-        VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_PROLOGUE(); \
-        VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_PROLOGUE();
-
-#define VBOXDISPPROFILE_DDI_DUMPRESET(_pDev) do {\
-        VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DUMP(); \
-        VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_RESET(); \
-        VBOXDISPPROFILE_DDI_FUNCTION_LOGGER_DISABLE_CURRENT();\
-    } while (0)
-
-#define VBOXDISPPROFILE_DDI_REPORT_FRAME(_pDev) do {\
-        VBOXDISPPROFILE_DDI_STATISTIC_LOGGER_REPORT_FRAME(_pDev); \
-    } while (0)
-
-
-#else
-#define VBOXDISPPROFILE_FUNCTION_DDI_PROLOGUE() do {} while (0)
-#define VBOXDISPPROFILE_DDI_DUMPRESET(_pDev) do {} while (0)
-#define VBOXDISPPROFILE_DDI_REPORT_FRAME(_pDev) do {} while (0)
+# define VBOXDISPPROFILE_FUNCTION_DDI_PROLOGUE(_pObj) do {} while (0)
+# define VBOXDISPPROFILE_DDI_REPORT_FRAME(_pDev) do {} while (0)
+# define VBOXDISPPROFILE_DDI_REPORT_FLUSH(_pDev) do {} while (0)
+# define VBOXDISPPROFILE_DDI_INIT_GLBL() do {} while (0)
+# define VBOXDISPPROFILE_DDI_INIT_ADP(_pAdp) do {} while (0)
+# define VBOXDISPPROFILE_DDI_INIT_DEV(_pDev) do {} while (0)
+# define VBOXDISPPROFILE_DDI_TERM(_pObj) do {} while (0)
+# define VBOXDISPPROFILE_DDI_PRINT(_m) do {} while (0)
 #endif
 
 /* debugging/profiling stuff could go here.
  * NOP in release */
-#define VBOXDISP_DDI_PROLOGUE() \
+#define VBOXDISP_DDI_PROLOGUE_CMN() \
     VBOXVDBG_BREAK_DDI(); \
-    VBOXDISPPROFILE_FUNCTION_DDI_PROLOGUE(); \
     VBOXVDBG_CREATE_CHECK_SWAPCHAIN();
+
+#define VBOXDISP_DDI_PROLOGUE_DEV(_hDevice) \
+    VBOXDISP_DDI_PROLOGUE_CMN(); \
+    VBOXDISPPROFILE_FUNCTION_DDI_PROLOGUE((PVBOXWDDMDISP_DEVICE)(_hDevice));
+
+#define VBOXDISP_DDI_PROLOGUE_ADP(_hAdapter) \
+    VBOXDISP_DDI_PROLOGUE_CMN(); \
+    VBOXDISPPROFILE_FUNCTION_DDI_PROLOGUE((PVBOXWDDMDISP_ADAPTER)(_hAdapter));
+
+#define VBOXDISP_DDI_PROLOGUE_GLBL() \
+    VBOXDISP_DDI_PROLOGUE_CMN(); \
+    VBOXDISPPROFILE_FUNCTION_DDI_PROLOGUE(&g_VBoxDispProfile);
 
 #ifdef VBOXDISPMP_TEST
@@ -1035,5 +1107,5 @@
 static BOOLEAN vboxWddmDalCheckAdd(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_ALLOCATION pAlloc, BOOLEAN fWrite)
 {
-    if (!pAlloc->hSharedHandle /* only shared resources matter */
+    if (!pAlloc->hAllocation /* only shared resources matter */
 #ifdef VBOX_WDDM_SHRC_WO_NOTIFY
             || !fWrite /* only write op matter */
@@ -1046,4 +1118,6 @@
         Assert(!pAlloc->DirtyAllocListEntry.pNext);
 #endif
+        Assert(!pAlloc->hSharedHandle);
+
         return FALSE;
     }
@@ -2887,6 +2961,14 @@
     {
         VBOXVDBG_ASSERT_IS_DWM(FALSE);
+
+        HRESULT hr = pDevice->pAdapter->D3D.pfnVBoxWineExD3DDev9Flush((IDirect3DDevice9Ex*)pDevice->pDevice9If);
+        Assert(hr == S_OK);
+
+        vboxWddmDalNotifyChange(pDevice);
+
         return S_OK;
     }
+
+    VBOXVDBG_ASSERT_IS_DWM(TRUE);
 
     BOOL bNeedPresent;
@@ -3149,5 +3231,5 @@
 static HRESULT APIENTRY vboxWddmDispGetCaps (HANDLE hAdapter, CONST D3DDDIARG_GETCAPS* pData)
 {
-	VBOXDISP_DDI_PROLOGUE();
+	VBOXDISP_DDI_PROLOGUE_ADP(hAdapter);
 
     vboxVDbgPrint(("==> "__FUNCTION__", hAdapter(0x%p), caps type(%d)\n", hAdapter, pData->Type));
@@ -3450,5 +3532,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetRenderState(HANDLE hDevice, CONST D3DDDIARG_RENDERSTATE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3464,5 +3546,5 @@
 static HRESULT APIENTRY vboxWddmDDevUpdateWInfo(HANDLE hDevice, CONST D3DDDIARG_WINFO* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
 //    PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
 //    VBOXDISPCRHGSMI_SCOPE_SET_DEV(pDevice);
@@ -3474,5 +3556,5 @@
 static HRESULT APIENTRY vboxWddmDDevValidateDevice(HANDLE hDevice, D3DDDIARG_VALIDATETEXTURESTAGESTATE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
 //    PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
 //    VBOXDISPCRHGSMI_SCOPE_SET_DEV(pDevice);
@@ -3489,5 +3571,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetTextureStageState(HANDLE hDevice, CONST D3DDDIARG_TEXTURESTAGESTATE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3515,5 +3597,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetTexture(HANDLE hDevice, UINT Stage, HANDLE hTexture)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3599,5 +3681,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetPixelShader(HANDLE hDevice, HANDLE hShaderHandle)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3614,5 +3696,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetPixelShaderConst(HANDLE hDevice, CONST D3DDDIARG_SETPIXELSHADERCONST* pData, CONST FLOAT* pRegisters)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3628,5 +3710,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetStreamSourceUm(HANDLE hDevice, CONST D3DDDIARG_SETSTREAMSOURCEUM* pData, CONST VOID* pUMBuffer )
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3654,5 +3736,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetIndices(HANDLE hDevice, CONST D3DDDIARG_SETINDICES* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3685,5 +3767,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetIndicesUm(HANDLE hDevice, UINT IndexSize, CONST VOID* pUMBuffer)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3700,5 +3782,5 @@
 static HRESULT APIENTRY vboxWddmDDevDrawPrimitive(HANDLE hDevice, CONST D3DDDIARG_DRAWPRIMITIVE* pData, CONST UINT* pFlagBuffer)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3778,5 +3860,5 @@
 static HRESULT APIENTRY vboxWddmDDevDrawIndexedPrimitive(HANDLE hDevice, CONST D3DDDIARG_DRAWINDEXEDPRIMITIVE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3832,5 +3914,5 @@
 static HRESULT APIENTRY vboxWddmDDevDrawRectPatch(HANDLE hDevice, CONST D3DDDIARG_DRAWRECTPATCH* pData, CONST D3DDDIRECTPATCH_INFO* pInfo, CONST FLOAT* pPatch)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3845,5 +3927,5 @@
 static HRESULT APIENTRY vboxWddmDDevDrawTriPatch(HANDLE hDevice, CONST D3DDDIARG_DRAWTRIPATCH* pData, CONST D3DDDITRIPATCH_INFO* pInfo, CONST FLOAT* pPatch)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3858,5 +3940,5 @@
 static HRESULT APIENTRY vboxWddmDDevDrawPrimitive2(HANDLE hDevice, CONST D3DDDIARG_DRAWPRIMITIVE2* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3955,5 +4037,5 @@
 static HRESULT APIENTRY vboxWddmDDevDrawIndexedPrimitive2(HANDLE hDevice, CONST D3DDDIARG_DRAWINDEXEDPRIMITIVE2* pData, UINT dwIndicesSize, CONST VOID* pIndexBuffer, CONST UINT* pFlagBuffer)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3968,5 +4050,5 @@
 static HRESULT APIENTRY vboxWddmDDevVolBlt(HANDLE hDevice, CONST D3DDDIARG_VOLUMEBLT* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3981,5 +4063,5 @@
 static HRESULT APIENTRY vboxWddmDDevBufBlt(HANDLE hDevice, CONST D3DDDIARG_BUFFERBLT* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -3994,5 +4076,5 @@
 static HRESULT APIENTRY vboxWddmDDevTexBlt(HANDLE hDevice, CONST D3DDDIARG_TEXBLT* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4073,5 +4155,5 @@
 static HRESULT APIENTRY vboxWddmDDevStateSet(HANDLE hDevice, D3DDDIARG_STATESET* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4084,5 +4166,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetPriority(HANDLE hDevice, CONST D3DDDIARG_SETPRIORITY* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
 //    PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
 //    Assert(pDevice);
@@ -4104,5 +4186,5 @@
 static HRESULT APIENTRY vboxWddmDDevClear(HANDLE hDevice, CONST D3DDDIARG_CLEAR* pData, UINT NumRect, CONST RECT* pRect)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4121,5 +4203,5 @@
 static HRESULT APIENTRY vboxWddmDDevUpdatePalette(HANDLE hDevice, CONST D3DDDIARG_UPDATEPALETTE* pData, CONST PALETTEENTRY* pPaletteData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4133,5 +4215,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetPalette(HANDLE hDevice, CONST D3DDDIARG_SETPALETTE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4145,5 +4227,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetVertexShaderConst(HANDLE hDevice, CONST D3DDDIARG_SETVERTEXSHADERCONST* pData , CONST VOID* pRegisters)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4161,5 +4243,5 @@
 static HRESULT APIENTRY vboxWddmDDevMultiplyTransform(HANDLE hDevice, CONST D3DDDIARG_MULTIPLYTRANSFORM* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4172,5 +4254,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetTransform(HANDLE hDevice, CONST D3DDDIARG_SETTRANSFORM* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4183,5 +4265,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetViewport(HANDLE hDevice, CONST D3DDDIARG_VIEWPORTINFO* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4201,5 +4283,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetZRange(HANDLE hDevice, CONST D3DDDIARG_ZRANGE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4217,5 +4299,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetMaterial(HANDLE hDevice, CONST D3DDDIARG_SETMATERIAL* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4228,5 +4310,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetLight(HANDLE hDevice, CONST D3DDDIARG_SETLIGHT* pData, CONST D3DDDI_LIGHT* pLightProperties)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4239,5 +4321,5 @@
 static HRESULT APIENTRY vboxWddmDDevCreateLight(HANDLE hDevice, CONST D3DDDIARG_CREATELIGHT* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4250,5 +4332,5 @@
 static HRESULT APIENTRY vboxWddmDDevDestroyLight(HANDLE hDevice, CONST D3DDDIARG_DESTROYLIGHT* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4261,5 +4343,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetClipPlane(HANDLE hDevice, CONST D3DDDIARG_SETCLIPPLANE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4276,5 +4358,5 @@
 static HRESULT APIENTRY vboxWddmDDevGetInfo(HANDLE hDevice, UINT DevInfoID, VOID* pDevInfoStruct, UINT DevInfoSize)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
 //    PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4309,5 +4391,5 @@
 static HRESULT APIENTRY vboxWddmDDevLock(HANDLE hDevice, D3DDDIARG_LOCK* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4759,5 +4841,5 @@
 static HRESULT APIENTRY vboxWddmDDevUnlock(HANDLE hDevice, CONST D3DDDIARG_UNLOCK* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4987,5 +5069,5 @@
 static HRESULT APIENTRY vboxWddmDDevLockAsync(HANDLE hDevice, D3DDDIARG_LOCKASYNC* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -4998,5 +5080,5 @@
 static HRESULT APIENTRY vboxWddmDDevUnlockAsync(HANDLE hDevice, CONST D3DDDIARG_UNLOCKASYNC* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5009,5 +5091,5 @@
 static HRESULT APIENTRY vboxWddmDDevRename(HANDLE hDevice, CONST D3DDDIARG_RENAME* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5063,5 +5145,5 @@
 static HRESULT APIENTRY vboxWddmDDevCreateResource(HANDLE hDevice, D3DDDIARG_CREATERESOURCE* pResource)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     HRESULT hr = S_OK;
@@ -5622,5 +5704,5 @@
                     if (pResource->Flags.SharedResource)
                     {
-#ifdef DEBUG_misha
+#ifdef VBOXWDDMDISP_DEBUG_PRINT_SHARED_CREATE
                         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), "
@@ -5667,5 +5749,5 @@
 static HRESULT APIENTRY vboxWddmDDevDestroyResource(HANDLE hDevice, HANDLE hResource)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5687,5 +5769,5 @@
             if (pAlloc->hSharedHandle)
             {
-#ifdef DEBUG_misha
+#ifdef VBOXWDDMDISP_DEBUG_PRINT_SHARED_CREATE
                 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",
@@ -5748,5 +5830,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetDisplayMode(HANDLE hDevice, CONST D3DDDIARG_SETDISPLAYMODE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     HRESULT hr = S_OK;
@@ -5780,5 +5862,5 @@
 static HRESULT APIENTRY vboxWddmDDevPresent(HANDLE hDevice, CONST D3DDDIARG_PRESENT* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5826,11 +5908,30 @@
         DdiPresent.hContext = pDevice->DefaultContext.ContextInfo.hContext;
 
+#if 0 //def VBOX_WDDMDISP_WITH_PROFILE
+        VBoxDispProfileScopeLogger<VBoxDispProfileEntry> profilePresentCbLogger(pDevice->ProfileDdiPresentCb.alloc("pfnPresentCb"));
+#endif
+#ifdef VBOXWDDMDISP_DEBUG_TIMER
+        HANDLE hTimer = NULL;
+        vboxVDbgTimerStart(pDevice->hTimerQueue, &hTimer, 1000);
+#endif
         hr = pDevice->RtCallbacks.pfnPresentCb(pDevice->hDevice, &DdiPresent);
+#ifdef VBOXWDDMDISP_DEBUG_TIMER
+        vboxVDbgTimerStop(pDevice->hTimerQueue, hTimer);
+#endif
+#if 0 //def VBOX_WDDMDISP_WITH_PROFILE
+        profilePresentCbLogger.logAndDisable();
+        if (pDevice->ProfileDdiPresentCb.getNumEntries() == 64)
+        {
+            pDevice->ProfileDdiPresentCb.dump(pDevice);
+            pDevice->ProfileDdiPresentCb.reset();
+        }
+#endif
         Assert(hr == S_OK);
     }
 
+    vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p), hr(0x%x)\n", hDevice, hr));
+
     VBOXDISPPROFILE_DDI_REPORT_FRAME(pDevice);
 
-    vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p), hr(0x%x)\n", hDevice, hr));
     return hr;
 }
@@ -5838,5 +5939,5 @@
 static HRESULT APIENTRY vboxWddmDDevFlush(HANDLE hDevice)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5855,4 +5956,7 @@
     }
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p), hr(0x%x)\n", hDevice, hr));
+
+    VBOXDISPPROFILE_DDI_REPORT_FLUSH(pDevice);
+
     return hr;
 }
@@ -5875,5 +5979,5 @@
 static HRESULT APIENTRY vboxWddmDDevCreateVertexShaderDecl(HANDLE hDevice, D3DDDIARG_CREATEVERTEXSHADERDECL* pData, CONST D3DDDIVERTEXELEMENT* pVertexElements)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5923,5 +6027,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetVertexShaderDecl(HANDLE hDevice, HANDLE hShaderHandle)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5938,5 +6042,5 @@
 static HRESULT APIENTRY vboxWddmDDevDeleteVertexShaderDecl(HANDLE hDevice, HANDLE hShaderHandle)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5951,5 +6055,5 @@
 static HRESULT APIENTRY vboxWddmDDevCreateVertexShaderFunc(HANDLE hDevice, D3DDDIARG_CREATEVERTEXSHADERFUNC* pData, CONST UINT* pCode)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5971,5 +6075,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetVertexShaderFunc(HANDLE hDevice, HANDLE hShaderHandle)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5985,5 +6089,5 @@
 static HRESULT APIENTRY vboxWddmDDevDeleteVertexShaderFunc(HANDLE hDevice, HANDLE hShaderHandle)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -5998,5 +6102,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetVertexShaderConstI(HANDLE hDevice, CONST D3DDDIARG_SETVERTEXSHADERCONSTI* pData, CONST INT* pRegisters)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6011,5 +6115,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetVertexShaderConstB(HANDLE hDevice, CONST D3DDDIARG_SETVERTEXSHADERCONSTB* pData, CONST BOOL* pRegisters)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6024,5 +6128,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetScissorRect(HANDLE hDevice, CONST RECT* pRect)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6037,5 +6141,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetStreamSource(HANDLE hDevice, CONST D3DDDIARG_SETSTREAMSOURCE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6082,5 +6186,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetStreamSourceFreq(HANDLE hDevice, CONST D3DDDIARG_SETSTREAMSOURCEFREQ* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6093,5 +6197,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetConvolutionKernelMono(HANDLE hDevice, CONST D3DDDIARG_SETCONVOLUTIONKERNELMONO* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6104,5 +6208,5 @@
 static HRESULT APIENTRY vboxWddmDDevComposeRects(HANDLE hDevice, CONST D3DDDIARG_COMPOSERECTS* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6116,5 +6220,5 @@
 static HRESULT APIENTRY vboxWddmDDevBlt(HANDLE hDevice, CONST D3DDDIARG_BLT* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6195,5 +6299,5 @@
 static HRESULT APIENTRY vboxWddmDDevColorFill(HANDLE hDevice, CONST D3DDDIARG_COLORFILL* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6225,5 +6329,5 @@
 static HRESULT APIENTRY vboxWddmDDevDepthFill(HANDLE hDevice, CONST D3DDDIARG_DEPTHFILL* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6237,5 +6341,5 @@
 static HRESULT APIENTRY vboxWddmDDevCreateQuery(HANDLE hDevice, D3DDDIARG_CREATEQUERY* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
 //    PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6267,5 +6371,5 @@
 static HRESULT APIENTRY vboxWddmDDevDestroyQuery(HANDLE hDevice, HANDLE hQuery)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     HRESULT hr = S_OK;
@@ -6282,5 +6386,5 @@
 static HRESULT APIENTRY vboxWddmDDevIssueQuery(HANDLE hDevice, CONST D3DDDIARG_ISSUEQUERY* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     HRESULT hr = S_OK;
@@ -6297,5 +6401,5 @@
 static HRESULT APIENTRY vboxWddmDDevGetQueryData(HANDLE hDevice, CONST D3DDDIARG_GETQUERYDATA* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     HRESULT hr = S_OK;
@@ -6323,5 +6427,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetRenderTarget(HANDLE hDevice, CONST D3DDDIARG_SETRENDERTARGET* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6342,5 +6446,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetDepthStencil(HANDLE hDevice, CONST D3DDDIARG_SETDEPTHSTENCIL* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6366,5 +6470,5 @@
 static HRESULT APIENTRY vboxWddmDDevGenerateMipSubLevels(HANDLE hDevice, CONST D3DDDIARG_GENERATEMIPSUBLEVELS* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6378,5 +6482,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetPixelShaderConstI(HANDLE hDevice, CONST D3DDDIARG_SETPIXELSHADERCONSTI* pData, CONST INT* pRegisters)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6392,5 +6496,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetPixelShaderConstB(HANDLE hDevice, CONST D3DDDIARG_SETPIXELSHADERCONSTB* pData, CONST BOOL* pRegisters)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6406,5 +6510,5 @@
 static HRESULT APIENTRY vboxWddmDDevCreatePixelShader(HANDLE hDevice, D3DDDIARG_CREATEPIXELSHADER* pData, CONST UINT* pCode)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6427,5 +6531,5 @@
 static HRESULT APIENTRY vboxWddmDDevDeletePixelShader(HANDLE hDevice, HANDLE hShaderHandle)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6441,5 +6545,5 @@
 static HRESULT APIENTRY vboxWddmDDevCreateDecodeDevice(HANDLE hDevice, D3DDDIARG_CREATEDECODEDEVICE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6453,5 +6557,5 @@
 static HRESULT APIENTRY vboxWddmDDevDestroyDecodeDevice(HANDLE hDevice, HANDLE hDecodeDevice)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6465,5 +6569,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetDecodeRenderTarget(HANDLE hDevice, CONST D3DDDIARG_SETDECODERENDERTARGET* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6477,5 +6581,5 @@
 static HRESULT APIENTRY vboxWddmDDevDecodeBeginFrame(HANDLE hDevice, D3DDDIARG_DECODEBEGINFRAME* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6489,5 +6593,5 @@
 static HRESULT APIENTRY vboxWddmDDevDecodeEndFrame(HANDLE hDevice, D3DDDIARG_DECODEENDFRAME* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6501,5 +6605,5 @@
 static HRESULT APIENTRY vboxWddmDDevDecodeExecute(HANDLE hDevice, CONST D3DDDIARG_DECODEEXECUTE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6513,5 +6617,5 @@
 static HRESULT APIENTRY vboxWddmDDevDecodeExtensionExecute(HANDLE hDevice, CONST D3DDDIARG_DECODEEXTENSIONEXECUTE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6525,5 +6629,5 @@
 static HRESULT APIENTRY vboxWddmDDevCreateVideoProcessDevice(HANDLE hDevice, D3DDDIARG_CREATEVIDEOPROCESSDEVICE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6537,5 +6641,5 @@
 static HRESULT APIENTRY vboxWddmDDevDestroyVideoProcessDevice(HANDLE hDevice, HANDLE hVideoProcessor)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6549,5 +6653,5 @@
 static HRESULT APIENTRY vboxWddmDDevVideoProcessBeginFrame(HANDLE hDevice, HANDLE hVideoProcess)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6561,5 +6665,5 @@
 static HRESULT APIENTRY vboxWddmDDevVideoProcessEndFrame(HANDLE hDevice, D3DDDIARG_VIDEOPROCESSENDFRAME* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6573,5 +6677,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetVideoProcessRenderTarget(HANDLE hDevice, CONST D3DDDIARG_SETVIDEOPROCESSRENDERTARGET* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6585,5 +6689,5 @@
 static HRESULT APIENTRY vboxWddmDDevVideoProcessBlt(HANDLE hDevice, CONST D3DDDIARG_VIDEOPROCESSBLT* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6597,5 +6701,5 @@
 static HRESULT APIENTRY vboxWddmDDevCreateExtensionDevice(HANDLE hDevice, D3DDDIARG_CREATEEXTENSIONDEVICE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6609,5 +6713,5 @@
 static HRESULT APIENTRY vboxWddmDDevDestroyExtensionDevice(HANDLE hDevice, HANDLE hExtension)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6621,5 +6725,5 @@
 static HRESULT APIENTRY vboxWddmDDevExtensionExecute(HANDLE hDevice, CONST D3DDDIARG_EXTENSIONEXECUTE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6633,12 +6737,19 @@
 static HRESULT APIENTRY vboxWddmDDevDestroyDevice(IN HANDLE hDevice)
 {
-    VBOXDISP_DDI_PROLOGUE();
-    vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
-
-    PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
-    Assert(pDevice);
-    VBOXDISPCRHGSMI_SCOPE_SET_DEV(pDevice);
-
-    VBOXDISPPROFILE_DDI_DUMPRESET(pDevice);
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
+    vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
+
+    PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
+    Assert(pDevice);
+    VBOXDISPCRHGSMI_SCOPE_SET_DEV(pDevice);
+
+    VBOXDISPPROFILE_DDI_PRINT(("Dumping on DestroyDevice: 0x%p", pDevice));
+    VBOXDISPPROFILE_DDI_TERM(pDevice);
+
+#ifdef VBOXWDDMDISP_DEBUG_TIMER
+        DeleteTimerQueueEx(pDevice->hTimerQueue, INVALID_HANDLE_VALUE /* see term */);
+        pDevice->hTimerQueue = NULL;
+#endif
+
     PVBOXWDDMDISP_ADAPTER pAdapter = pDevice->pAdapter;
     if (VBOXDISPMODE_IS_3D(pAdapter))
@@ -6680,5 +6791,5 @@
 static HRESULT APIENTRY vboxWddmDDevCreateOverlay(HANDLE hDevice, D3DDDIARG_CREATEOVERLAY* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6744,5 +6855,5 @@
 static HRESULT APIENTRY vboxWddmDDevUpdateOverlay(HANDLE hDevice, CONST D3DDDIARG_UPDATEOVERLAY* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6788,5 +6899,5 @@
 static HRESULT APIENTRY vboxWddmDDevFlipOverlay(HANDLE hDevice, CONST D3DDDIARG_FLIPOVERLAY* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6825,5 +6936,5 @@
 static HRESULT APIENTRY vboxWddmDDevGetOverlayColorControls(HANDLE hDevice, D3DDDIARG_GETOVERLAYCOLORCONTROLS* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     Assert(0);
@@ -6833,5 +6944,5 @@
 static HRESULT APIENTRY vboxWddmDDevSetOverlayColorControls(HANDLE hDevice, CONST D3DDDIARG_SETOVERLAYCOLORCONTROLS* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     Assert(0);
@@ -6841,5 +6952,5 @@
 static HRESULT APIENTRY vboxWddmDDevDestroyOverlay(HANDLE hDevice, CONST D3DDDIARG_DESTROYOVERLAY* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6863,5 +6974,5 @@
 static HRESULT APIENTRY vboxWddmDDevQueryResourceResidency(HANDLE hDevice, CONST D3DDDIARG_QUERYRESOURCERESIDENCY* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -6910,5 +7021,5 @@
 static HRESULT APIENTRY vboxWddmDDevOpenResource(HANDLE hDevice, D3DDDIARG_OPENRESOURCE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("==> "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -7005,4 +7116,5 @@
                     Assert(pAllocation->hSharedHandle);
 #endif
+#ifdef VBOXWDDMDISP_DEBUG_PRINT_SHARED_CREATE
                     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",
@@ -7011,4 +7123,5 @@
                                 pAllocation->hSharedHandle, pAllocation->hSharedHandle
                                 ));
+#endif
                 }
 
@@ -7126,5 +7239,5 @@
 static HRESULT APIENTRY vboxWddmDDevGetCaptureAllocationHandle(HANDLE hDevice, D3DDDIARG_GETCAPTUREALLOCATIONHANDLE* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -7139,5 +7252,5 @@
 static HRESULT APIENTRY vboxWddmDDevCaptureToSysMem(HANDLE hDevice, CONST D3DDDIARG_CAPTURETOSYSMEM* pData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_DEV(hDevice);
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
     PVBOXWDDMDISP_DEVICE pDevice = (PVBOXWDDMDISP_DEVICE)hDevice;
@@ -7152,5 +7265,5 @@
 static HRESULT APIENTRY vboxWddmDispCreateDevice (IN HANDLE hAdapter, IN D3DDDIARG_CREATEDEVICE* pCreateData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_ADP(hAdapter);
     HRESULT hr = S_OK;
     vboxVDbgPrint(("==> "__FUNCTION__", hAdapter(0x%p), Interface(%d), Version(%d)\n", hAdapter, pCreateData->Interface, pCreateData->Version));
@@ -7286,4 +7399,13 @@
         pCreateData->pDeviceFuncs->pfnRename = NULL; //vboxWddmDDevRename;
 
+        VBOXDISPPROFILE_DDI_INIT_DEV(pDevice);
+#ifdef VBOX_WDDMDISP_WITH_PROFILE
+        pDevice->ProfileDdiPresentCb = VBoxDispProfileSet("pfnPresentCb");
+#endif
+
+#ifdef VBOXWDDMDISP_DEBUG_TIMER
+        pDevice->hTimerQueue = CreateTimerQueue();
+        Assert(pDevice->hTimerQueue);
+#endif
 
         do
@@ -7380,5 +7502,5 @@
 static HRESULT APIENTRY vboxWddmDispCloseAdapter (IN HANDLE hAdapter)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_ADP(hAdapter);
     vboxVDbgPrint(("==> "__FUNCTION__", hAdapter(0x%p)\n", hAdapter));
 
@@ -7396,4 +7518,6 @@
 
     vboxCapsFree(pAdapter);
+
+    VBOXDISPPROFILE_DDI_TERM(pAdapter);
 
     RTMemFree(pAdapter);
@@ -7440,5 +7564,5 @@
 HRESULT APIENTRY OpenAdapter(__inout D3DDDIARG_OPENADAPTER*  pOpenData)
 {
-    VBOXDISP_DDI_PROLOGUE();
+    VBOXDISP_DDI_PROLOGUE_GLBL();
 
     vboxVDbgPrint(("==> "__FUNCTION__"\n"));
@@ -7525,6 +7649,10 @@
                     VBOXDISPCRHGSMI_SCOPE_SET_GLOBAL();
                     /* try enable the 3D */
+#if 1
                     hr = VBoxDispD3DOpen(&pAdapter->D3D);
                     Assert(hr == S_OK);
+#else
+                    hr = E_FAIL;
+#endif
                     if (hr == S_OK)
                     {
@@ -7573,4 +7701,6 @@
         hr = S_OK;
 //        RTMemFree(pAdapter);
+
+        VBOXDISPPROFILE_DDI_INIT_ADP(pAdapter);
     }
     else
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h	(revision 41636)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h	(revision 41637)
@@ -26,4 +26,11 @@
 #endif
 
+#ifdef VBOX_WDDMDISP_WITH_PROFILE
+#include <iprt/asm.h>
+extern volatile uint32_t g_u32VBoxDispProfileFunctionLoggerIndex;
+# define VBOXDISPPROFILE_FUNCTION_LOGGER_INDEX_GEN() ASMAtomicIncU32(&g_u32VBoxDispProfileFunctionLoggerIndex);
+# include "VBoxDispProfile.h"
+#endif
+
 #include <iprt/cdefs.h>
 #include <iprt/list.h>
@@ -77,4 +84,8 @@
     DDSURFACEDESC *paSurfDescs;
     UINT cMaxSimRTs;
+#ifdef VBOX_WDDMDISP_WITH_PROFILE
+    VBoxDispProfileFpsCounter ProfileDdiFps;
+    VBoxDispProfileSet ProfileDdiFunc;
+#endif
 #ifdef VBOX_WITH_VIDEOHWACCEL
     uint32_t cHeads;
@@ -205,4 +216,15 @@
     UINT cSamplerTextures;
     struct VBOXWDDMDISP_RESOURCE *aSamplerTextures[VBOXWDDMDISP_TOTAL_SAMPLERS];
+
+#ifdef VBOX_WDDMDISP_WITH_PROFILE
+    VBoxDispProfileFpsCounter ProfileDdiFps;
+    VBoxDispProfileSet ProfileDdiFunc;
+
+    VBoxDispProfileSet ProfileDdiPresentCb;
+#endif
+
+#ifdef VBOXWDDMDISP_DEBUG_TIMER
+    HANDLE hTimerQueue;
+#endif
 
     UINT cRTs;
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h	(revision 41636)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.h	(revision 41637)
@@ -24,5 +24,23 @@
 #include <iprt/semaphore.h>
 
+#       define _InterlockedExchange           _InterlockedExchange_StupidDDKVsCompilerCrap
+#       define _InterlockedExchangeAdd        _InterlockedExchangeAdd_StupidDDKVsCompilerCrap
+#       define _InterlockedCompareExchange    _InterlockedCompareExchange_StupidDDKVsCompilerCrap
+#       define _InterlockedAddLargeStatistic  _InterlockedAddLargeStatistic_StupidDDKVsCompilerCrap
+#       define _interlockedbittestandset      _interlockedbittestandset_StupidDDKVsCompilerCrap
+#       define _interlockedbittestandreset    _interlockedbittestandreset_StupidDDKVsCompilerCrap
+#       define _interlockedbittestandset64    _interlockedbittestandset64_StupidDDKVsCompilerCrap
+#       define _interlockedbittestandreset64  _interlockedbittestandreset64_StupidDDKVsCompilerCrap
+#       pragma warning(disable : 4163)
 #include <D3D9.h>
+#       pragma warning(default : 4163)
+#       undef  _InterlockedExchange
+#       undef  _InterlockedExchangeAdd
+#       undef  _InterlockedCompareExchange
+#       undef  _InterlockedAddLargeStatistic
+#       undef  _interlockedbittestandset
+#       undef  _interlockedbittestandreset
+#       undef  _interlockedbittestandset64
+#       undef  _interlockedbittestandreset64
 
 #include "../../../Wine/vbox/VBoxWineEx.h"
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp	(revision 41636)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp	(revision 41637)
@@ -50,7 +50,26 @@
 #include <iprt/assert.h>
 
+static DWORD g_VBoxVDbgFIsModuleNameInited = 0;
+static char g_VBoxVDbgModuleName[MAX_PATH];
+
+char *vboxVDbgDoGetModuleName()
+{
+    if (!g_VBoxVDbgFIsModuleNameInited)
+    {
+        DWORD cName = GetModuleFileNameA(NULL, g_VBoxVDbgModuleName, RT_ELEMENTS(g_VBoxVDbgModuleName));
+        if (!cName)
+        {
+            DWORD winEr = GetLastError();
+            WARN(("GetModuleFileNameA failed, winEr %d", winEr));
+            return NULL;
+        }
+        g_VBoxVDbgFIsModuleNameInited = TRUE;
+    }
+    return g_VBoxVDbgModuleName;
+}
+
 static void vboxDispLogDbgFormatStringV(char * szBuffer, uint32_t cbBuffer, const char * szString, va_list pArgList)
 {
-    uint32_t cbWritten = sprintf(szBuffer, "[0x%x.0x%x] Disp: ", GetCurrentProcessId(), GetCurrentThreadId());
+    uint32_t cbWritten = sprintf(szBuffer, "['%s' 0x%x.0x%x] Disp: ", vboxVDbgDoGetModuleName(), GetCurrentProcessId(), GetCurrentThreadId());
     if (cbWritten > cbBuffer)
     {
@@ -90,7 +109,4 @@
 DWORD g_VBoxVDbgFLog = 1;
 DWORD g_VBoxVDbgFLogFlow = 0;
-
-DWORD g_VBoxVDbgFIsModuleNameInited = 0;
-char g_VBoxVDbgModuleName[MAX_PATH];
 
 LONG g_VBoxVDbgFIsDwm = -1;
@@ -626,20 +642,4 @@
 }
 
-char *vboxVDbgDoGetModuleName()
-{
-    if (!g_VBoxVDbgFIsModuleNameInited)
-    {
-        DWORD cName = GetModuleFileNameA(NULL, g_VBoxVDbgModuleName, RT_ELEMENTS(g_VBoxVDbgModuleName));
-        if (!cName)
-        {
-            DWORD winEr = GetLastError();
-            WARN(("GetModuleFileNameA failed, winEr %d", winEr));
-            return NULL;
-        }
-        g_VBoxVDbgFIsModuleNameInited = TRUE;
-    }
-    return g_VBoxVDbgModuleName;
-}
-
 BOOL vboxVDbgDoCheckExe(const char * pszName)
 {
@@ -655,4 +655,35 @@
         return FALSE;
     return TRUE;
+}
+
+static VOID CALLBACK vboxVDbgTimerCb(__in PVOID lpParameter, __in BOOLEAN TimerOrWaitFired)
+{
+    Assert(0);
+}
+
+HRESULT vboxVDbgTimerStart(HANDLE hTimerQueue, HANDLE *phTimer, DWORD msTimeout)
+{
+    if (!CreateTimerQueueTimer(phTimer, hTimerQueue,
+                               vboxVDbgTimerCb,
+                               NULL,
+                               msTimeout, /* ms*/
+                               0,
+                               WT_EXECUTEONLYONCE))
+    {
+        DWORD winEr = GetLastError();
+        AssertMsgFailed(("CreateTimerQueueTimer failed, winEr (%d)\n", winEr));
+        return E_FAIL;
+    }
+    return S_OK;
+}
+
+HRESULT vboxVDbgTimerStop(HANDLE hTimerQueue, HANDLE hTimer)
+{
+    if (!DeleteTimerQueueTimer(hTimerQueue, hTimer, NULL))
+    {
+        DWORD winEr = GetLastError();
+        AssertMsg(winEr == ERROR_IO_PENDING, ("DeleteTimerQueueTimer failed, winEr (%d)\n", winEr));
+    }
+    return S_OK;
 }
 #endif
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h	(revision 41636)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h	(revision 41637)
@@ -44,4 +44,8 @@
 /* disable shared resource creation with wine */
 //#  define VBOXWDDMDISP_DEBUG_NOSHARED
+
+//#  define VBOXWDDMDISP_DEBUG_PRINT_SHARED_CREATE
+
+//#  define VBOXWDDMDISP_DEBUG_TIMER
 # endif
 
@@ -74,7 +78,4 @@
 extern DWORD g_VBoxVDbgFLog;
 extern DWORD g_VBoxVDbgFLogFlow;
-
-extern DWORD g_VBoxVDbgFIsModuleNameInited;
-extern char g_VBoxVDbgModuleName[];
 
 extern LONG g_VBoxVDbgFIsDwm;
@@ -261,4 +262,7 @@
 
 VOID vboxVDbgDoPrintLopLastCmd(const char* pszDesc);
+
+HRESULT vboxVDbgTimerStart(HANDLE hTimerQueue, HANDLE *phTimer, DWORD msTimeout);
+HRESULT vboxVDbgTimerStop(HANDLE hTimerQueue, HANDLE hTimer);
 
 extern DWORD g_VBoxVDbgPid;
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispProfile.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispProfile.h	(revision 41636)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispProfile.h	(revision 41637)
@@ -19,8 +19,8 @@
 #ifndef ___VBoxDispProfile_h__
 #define ___VBoxDispProfile_h__
+#include "VBoxDispD3DCmn.h"
+
 #include <iprt/ctype.h>
 #include <iprt/time.h>
-
-#include "VBoxDispD3DCmn.h"
 
 #define VBOXDISPPROFILE_MAX_SETSIZE 512
@@ -28,5 +28,5 @@
 #define VBOXDISPPROFILE_GET_TIME_MILLI() RTTimeMilliTS()
 #define VBOXDISPPROFILE_DUMP(_m) do {\
-        LogRel (_m); \
+        LOGREL_EXACT(_m); \
     } while (0)
 
@@ -58,12 +58,29 @@
     }
 
-    void dump(const PVBOXWDDMDISP_DEVICE pDevice) const
+    uint64_t getTime() const
+    {
+        return m_cTime;
+    }
+
+    uint32_t getNumCalls() const
+    {
+        return m_cCalls;
+    }
+
+    const char* getName() const
+    {
+        return m_pName;
+    }
+
+    void dump(void *pvObj, uint64_t cTotalEntriesTime, uint64_t cTotalTime) const
     {
 //        VBOXDISPPROFILE_DUMP((pDevice, "Entry '%s': calls(%d), time: nanos(%I64u), micros(%I64u), millis(%I64u)\n",
 //                m_pName, m_cCalls,
 //                m_cTime, m_cTime/1000, m_cTime/1000000));
-        VBOXDISPPROFILE_DUMP(("%s\t%d\t%I64u\t%I64u\t%I64u\n",
-                m_pName, m_cCalls,
-                m_cTime, m_cTime/1000, m_cTime/1000000));
+        VBOXDISPPROFILE_DUMP(("'%s' [0x%p]: \t%d\t%u\t%u\t%u\t%u\t%u", m_pName, pvObj,
+                m_cCalls,
+                (uint32_t)m_cTime, (uint32_t)(m_cTime/1000), (uint32_t)(m_cTime/1000000),
+                (uint32_t)(((double)m_cTime)/cTotalEntriesTime),
+                (uint32_t)(((double)m_cTime)/cTotalTime)));
     }
 private:
@@ -78,6 +95,17 @@
     VBoxDispProfileSet(const char *pName) :
         m_cEntries(0),
+        m_cIterations(0),
         m_pName(pName)
-    {}
+    {
+        m_cTime = VBOXDISPPROFILE_GET_TIME_NANO();
+    }
+
+    VBoxDispProfileSet() :
+        m_cEntries(0),
+        m_cIterations(0),
+        m_pName("global")
+    {
+        m_cTime = VBOXDISPPROFILE_GET_TIME_NANO();
+    }
 
     VBoxDispProfileEntry * alloc(const char *pName)
@@ -93,21 +121,67 @@
     }
 
+    VBoxDispProfileEntry * get(uint32_t u32Entry, const char *pName)
+    {
+        if (u32Entry < RT_ELEMENTS(m_Entries))
+        {
+            VBoxDispProfileEntry * entry = &m_Entries[u32Entry];
+            if (entry->getName())
+                return entry;
+            ++m_cEntries;
+            *entry = VBoxDispProfileEntry(pName);
+            return entry;
+        }
+        return NULL;
+    }
+
+    uint32_t reportIteration()
+    {
+        return ++m_cIterations;
+    }
+
+    uint32_t getNumIterations() const
+    {
+        return m_cIterations;
+    }
+
+    uint32_t getNumEntries() const
+    {
+        return m_cEntries;
+    }
+
+#define VBOXDISPPROFILESET_FOREACHENTRY(_op) \
+        for (uint32_t i = 0, e = 0; e < m_cEntries && i < RT_ELEMENTS(m_Entries); ++i) { \
+            if (m_Entries[i].getName()) { \
+                { \
+                _op  \
+                } \
+                ++e; \
+            } \
+        } \
+
     void resetEntries()
     {
-        for (uint32_t i = 0; i < m_cEntries; ++i)
-        {
-            m_Entries[i].reset();
-        }
-    }
-
-    void dump(const PVBOXWDDMDISP_DEVICE pDevice)
-    {
-        VBOXDISPPROFILE_DUMP((">>>> Start of VBox Disp Dump '%s': num entries(%d) >>>>>\n", m_pName, m_cEntries));
-        VBOXDISPPROFILE_DUMP(("Name\tCalls\tNanos\tMicros\tMillis\n"));
-        for (uint32_t i = 0; i < m_cEntries; ++i)
-        {
-            m_Entries[i].dump(pDevice);
-        }
-        VBOXDISPPROFILE_DUMP(("<<<< Endi of VBox Disp Dump '%s' <<<<<\n", m_pName));
+        VBOXDISPPROFILESET_FOREACHENTRY(  m_Entries[i].reset(); );
+        m_cTime = VBOXDISPPROFILE_GET_TIME_NANO();
+    }
+
+    void reset()
+    {
+        m_cEntries = 0;
+        m_cTime = VBOXDISPPROFILE_GET_TIME_NANO();
+    }
+
+    void dump(void *pvObj)
+    {
+        uint64_t cEntriesTime = 0;
+        VBOXDISPPROFILESET_FOREACHENTRY( cEntriesTime += m_Entries[i].getTime(); );
+
+        VBOXDISPPROFILE_DUMP((">>>> '%s' [0x%p]: Start of VBox Disp Dump: num entries(%d), et(%u), tt(%u) >>>>>", m_pName, pvObj, m_cEntries,
+                (uint32_t)(cEntriesTime / 1000000), (uint32_t)(m_cTime / 1000000)));
+        VBOXDISPPROFILE_DUMP(("Name\tCalls\tNanos\tMicros\tMillis\tentries_quota\ttotal_quota"));
+        VBOXDISPPROFILESET_FOREACHENTRY(
+                if (m_Entries[i].getNumCalls())
+                    m_Entries[i].dump(pvObj, cEntriesTime, m_cTime); );
+        VBOXDISPPROFILE_DUMP(("<<<< '%s' [0x%p]: End of VBox Disp Dump <<<<<", m_pName, pvObj));
     }
 
@@ -115,4 +189,6 @@
     VBoxDispProfileEntry m_Entries[VBOXDISPPROFILE_MAX_SETSIZE];
     uint32_t m_cEntries;
+    uint32_t m_cIterations;
+    uint64_t m_cTime;
     const char * m_pName;
 };
@@ -164,16 +240,48 @@
     VBoxDispProfileFpsCounter(uint32_t cPeriods)
     {
+        init(cPeriods);
+    }
+
+    VBoxDispProfileFpsCounter()
+    {
+        memset(&m_Data, 0, sizeof (m_Data));
+    }
+
+    ~VBoxDispProfileFpsCounter()
+    {
+        term();
+    }
+
+    void term()
+    {
+        if (m_Data.mpaPeriods)
+        {
+            RTMemFree(m_Data.mpaPeriods);
+            m_Data.mpaPeriods = NULL;
+        }
+        if (m_Data.mpaCalls)
+        {
+            RTMemFree(m_Data.mpaCalls);
+            m_Data.mpaCalls = NULL;
+        }
+        if (m_Data.mpaTimes)
+        {
+            RTMemFree(m_Data.mpaTimes);
+            m_Data.mpaTimes = NULL;
+        }
+        m_Data.mcPeriods = 0;
+    }
+
+    /* to be called in case fps counter was created with default constructor */
+    void init(uint32_t cPeriods)
+    {
         memset(&m_Data, 0, sizeof (m_Data));
         m_Data.mcPeriods = cPeriods;
-        m_Data.mpaPeriods = (uint64_t *)RTMemAllocZ(sizeof (m_Data.mpaPeriods[0]) * cPeriods);
-        m_Data.mpaCalls = (uint32_t *)RTMemAllocZ(sizeof (m_Data.mpaCalls[0]) * cPeriods);
-        m_Data.mpaTimes = (uint64_t *)RTMemAllocZ(sizeof (m_Data.mpaTimes[0]) * cPeriods);
-    }
-
-    ~VBoxDispProfileFpsCounter()
-    {
-        RTMemFree(m_Data.mpaPeriods);
-        RTMemFree(m_Data.mpaCalls);
-        RTMemFree(m_Data.mpaTimes);
+        if (cPeriods)
+        {
+            m_Data.mpaPeriods = (uint64_t *)RTMemAllocZ(sizeof (m_Data.mpaPeriods[0]) * cPeriods);
+            m_Data.mpaCalls = (uint32_t *)RTMemAllocZ(sizeof (m_Data.mpaCalls[0]) * cPeriods);
+            m_Data.mpaTimes = (uint64_t *)RTMemAllocZ(sizeof (m_Data.mpaTimes[0]) * cPeriods);
+        }
     }
 
@@ -264,8 +372,18 @@
     } while (0)
 
-#define VBOXDISPPROFILE_FUNCTION_LOGGER_DEFINE(_p)  \
+#ifdef VBOXDISPPROFILE_FUNCTION_LOGGER_GLOBAL_PROFILE
+# define VBOXDISPPROFILE_FUNCTION_LOGGER_DEFINE(_p)  \
         static VBoxDispProfileEntry * __pVBoxDispProfileEntry = NULL; \
         if (!__pVBoxDispProfileEntry) { __pVBoxDispProfileEntry = _p.alloc(__FUNCTION__); } \
         VBoxDispProfileScopeLogger<VBoxDispProfileEntry> __vboxDispProfileFunctionLogger(__pVBoxDispProfileEntry);
+#else
+# ifndef VBOXDISPPROFILE_FUNCTION_LOGGER_INDEX_GEN
+#  error "VBOXDISPPROFILE_FUNCTION_LOGGER_INDEX_GEN should be fedined!"
+# endif
+# define VBOXDISPPROFILE_FUNCTION_LOGGER_DEFINE(_p)  \
+        static uint32_t __u32VBoxDispProfileIndex = VBOXDISPPROFILE_FUNCTION_LOGGER_INDEX_GEN(); \
+        VBoxDispProfileEntry * __pVBoxDispProfileEntry = _p.get(__u32VBoxDispProfileIndex, __FUNCTION__); \
+        VBoxDispProfileScopeLogger<VBoxDispProfileEntry> __vboxDispProfileFunctionLogger(__pVBoxDispProfileEntry);
+#endif
 
 #define VBOXDISPPROFILE_STATISTIC_LOGGER_DISABLE_CURRENT() do { \
