Index: trunk/src/VBox/Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp	(revision 26319)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp	(revision 26320)
@@ -169,4 +169,8 @@
 /* number of available video modes, set by VBoxBuildModesTable  */
 static uint32_t gNumVideoModes = 0;
+#ifdef VBOXWDDM
+/* preferred mode index */
+static uint32_t gPreferredVideoMode = 0;
+#endif
 
 static uint32_t g_xresNoVRAM = 0, g_yresNoVRAM = 0, g_bppNoVRAM = 0;
@@ -222,4 +226,7 @@
 
     gNumVideoModes = 0;
+#ifdef VBOXWDDM
+    gPreferredVideoMode = 0;
+#endif
 
     size_t numModesCurrentColorDepth;
@@ -302,4 +309,7 @@
                 VideoModes[gNumVideoModes].BlueMask                     = 0xFF;
                 VideoModes[gNumVideoModes].AttributeFlags               = VIDEO_MODE_GRAPHICS | VIDEO_MODE_COLOR | VIDEO_MODE_NO_OFF_SCREEN;
+#ifdef VBOXWDDM
+                gPreferredVideoMode = gNumVideoModes;
+#endif
             } break;
         }
@@ -564,4 +574,7 @@
      */
     int curKeyNo = 0;
+#ifdef VBOXWDDM
+    int fPreferredSet = 0;
+#endif
     do
     {
@@ -611,4 +624,12 @@
 
         dprintf(("VBoxVideo: adding mode from registry: xres = %d, yres = %d, bpp = %d\n", xres, yres, bpp));
+
+#ifdef VBOXWDDM
+        if (!fPreferredSet)
+        {
+            gPreferredVideoMode = gNumVideoModes;
+            fPreferredSet = 1;
+        }
+#endif
         /*
          * Build mode entry.
@@ -650,4 +671,12 @@
                 VideoModes[gNumVideoModes].GreenMask            = 0xFF00;
                 VideoModes[gNumVideoModes].BlueMask             = 0xFF;
+#ifdef VBOXWDDM
+                /* 32-bit mode is more preferable, select it if not yet */
+                if (fPreferredSet < 2)
+                {
+                    gPreferredVideoMode = gNumVideoModes;
+                    fPreferredSet = 2;
+                }
+#endif
                 break;
         }
@@ -740,4 +769,8 @@
 
                 dprintf(("VBoxVideo: setting special mode to xres = %d, yres = %d, bpp = %d\n", xres, yres, bpp));
+#ifdef VBOXWDDM
+                /* assign host-supplied as the most preferable */
+                gPreferredVideoMode = gNumVideoModes;
+#endif
                 /*
                  * Build mode entry.
@@ -870,5 +903,5 @@
  * or the available VRAM does not allow for additional modes.
  */
-VOID VBoxWddmGetModesTable(PDEVICE_EXTENSION DeviceExtension, bool bRebuildTable, VIDEO_MODE_INFORMATION ** ppModes, uint32_t * pcModes)
+VOID VBoxWddmGetModesTable(PDEVICE_EXTENSION DeviceExtension, bool bRebuildTable, VIDEO_MODE_INFORMATION ** ppModes, uint32_t * pcModes, uint32_t * pPreferrableMode)
 {
     static bool bTableInitialized = false;
@@ -881,4 +914,5 @@
     *ppModes = VideoModes;
     *pcModes = gNumVideoModes;
+    *pPreferrableMode = gPreferredVideoMode;
 }
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Miniport/VBoxVideo.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Miniport/VBoxVideo.h	(revision 26319)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Miniport/VBoxVideo.h	(revision 26320)
@@ -624,5 +624,7 @@
 RT_C_DECLS_END
 
-VOID VBoxWddmGetModesTable(PDEVICE_EXTENSION DeviceExtension, bool bRebuildTable, VIDEO_MODE_INFORMATION ** ppModes, uint32_t * pcModes);
+VOID VBoxWddmGetModesTable(PDEVICE_EXTENSION DeviceExtension, bool bRebuildTable, VIDEO_MODE_INFORMATION ** ppModes, uint32_t * pcModes, uint32_t * pPreferrableMode);
+D3DDDIFORMAT vboxWddmCalcPixelFormat(VIDEO_MODE_INFORMATION *pInfo);
+UINT vboxWddmCalcBitsPerPixel(D3DDDIFORMAT format);
 
 NTSTATUS vboxVidPnCheckTopology(const D3DKMDT_HVIDPN hDesiredVidPn,
@@ -646,25 +648,13 @@
         BOOLEAN *pbSupported);
 
-typedef struct VBOXVIDPNCMCONTEXT
+typedef struct VBOXVIDPNCOFUNCMODALITY
 {
     NTSTATUS Status;
     CONST DXGKARG_ENUMVIDPNCOFUNCMODALITY* pEnumCofuncModalityArg;
-}VBOXVIDPNCMCONTEXT, *PVBOXVIDPNCMCONTEXT;
-
-typedef struct VBOXVIDPN_NEW_SRCMODESET_CHECK
-{
-    VBOXVIDPNCMCONTEXT CommonInfo;
-    CONST D3DDDI_VIDEO_PRESENT_SOURCE_ID VidPnSourceId;
-    BOOLEAN bNeedAdjustment;
-}VBOXVIDPN_NEW_SRCMODESET_CHECK, *PVBOXVIDPN_NEW_SRCMODESET_CHECK;
-
-typedef struct VBOXVIDPN_NEW_SRCMODESET_POPULATION
-{
-    VBOXVIDPNCMCONTEXT CommonInfo;
-    CONST D3DDDI_VIDEO_PRESENT_SOURCE_ID VidPnSourceId;
-    /* new source mode (the one being populated) */
-    D3DKMDT_HVIDPNSOURCEMODESET hNewVidPnSourceModeSet;
-    CONST DXGK_VIDPNSOURCEMODESET_INTERFACE*  pNewVidPnSourceModeSetInterface;
-}VBOXVIDPN_NEW_SRCMODESET_POPULATION, *PVBOXVIDPN_NEW_SRCMODESET_POPULATION;
+    /* legacy mode information populated by the common XPDM-WDDM layer */
+    uint32_t cModes;
+    uint32_t iPreferredMode;
+    VIDEO_MODE_INFORMATION *pModes;
+}VBOXVIDPNCOFUNCMODALITY, *PVBOXVIDPNCOFUNCMODALITY;
 
 /* !!!NOTE: The callback is responsible for releasing the path */
Index: trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoVidPn.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoVidPn.cpp	(revision 26319)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoVidPn.cpp	(revision 26320)
@@ -358,4 +358,5 @@
 }
 
+#if 0
 DECLCALLBACK(BOOLEAN) vboxVidPnCofuncModalitySourceModeCheck(PDEVICE_EXTENSION pDevExt, const D3DKMDT_HVIDPN hDesiredVidPn, const DXGK_VIDPN_INTERFACE* pVidPnInterface,
         D3DKMDT_HVIDPNSOURCEMODESET hNewVidPnSourceModeSet, const DXGK_VIDPNSOURCEMODESET_INTERFACE *pVidPnSourceModeSetInterface,
@@ -370,5 +371,4 @@
     pCbContext->CommonInfo.Status = Status;
     return Status == STATUS_SUCCESS;
-
 }
 
@@ -385,5 +385,4 @@
     pCbContext->Status = Status;
     return Status == STATUS_SUCCESS;
-
 }
 
@@ -401,4 +400,196 @@
     return Status == STATUS_SUCCESS;
 }
+#endif
+
+NTSTATUS vboxVidPnPopulateSourceModeInfoFromLegacy(PDEVICE_EXTENSION pDevExt,
+        D3DKMDT_VIDPN_SOURCE_MODE *pNewVidPnSourceModeInfo,
+        VIDEO_MODE_INFORMATION *pMode)
+{
+    NTSTATUS Status = STATUS_SUCCESS;
+    if (pMode->AttributeFlags & VIDEO_MODE_GRAPHICS)
+    {
+        /* this is a graphics mode */
+        pNewVidPnSourceModeInfo->Type = D3DKMDT_RMT_GRAPHICS;
+        pNewVidPnSourceModeInfo->Format.Graphics.PrimSurfSize.cx = pMode->VisScreenWidth;
+        pNewVidPnSourceModeInfo->Format.Graphics.PrimSurfSize.cy = pMode->VisScreenHeight;
+        pNewVidPnSourceModeInfo->Format.Graphics.VisibleRegionSize = pNewVidPnSourceModeInfo->Format.Graphics.PrimSurfSize;
+        pNewVidPnSourceModeInfo->Format.Graphics.Stride = pMode->ScreenStride;
+        pNewVidPnSourceModeInfo->Format.Graphics.PixelFormat = vboxWddmCalcPixelFormat(pMode);
+        Assert(pNewVidPnSourceModeInfo->Format.Graphics.PixelFormat != D3DDDIFMT_UNKNOWN);
+        if (pNewVidPnSourceModeInfo->Format.Graphics.PixelFormat != D3DDDIFMT_UNKNOWN)
+        {
+            pNewVidPnSourceModeInfo->Format.Graphics.ColorBasis = D3DKMDT_CB_SRGB;
+            if (pNewVidPnSourceModeInfo->Format.Graphics.PixelFormat == D3DDDIFMT_P8)
+                pNewVidPnSourceModeInfo->Format.Graphics.PixelValueAccessMode = D3DKMDT_PVAM_SETTABLEPALETTE;
+            else
+                pNewVidPnSourceModeInfo->Format.Graphics.PixelValueAccessMode = D3DKMDT_PVAM_DIRECT;
+        }
+        else
+        {
+            drprintf((__FUNCTION__": vboxWddmCalcPixelFormat failed\n"));
+            Status = STATUS_INVALID_PARAMETER;
+        }
+    }
+    else
+    {
+        /* @todo: XPDM driver does not seem to return text modes, should we? */
+        drprintf((__FUNCTION__": text mode not supported currently\n"));
+        AssertBreakpoint();
+        Status = STATUS_INVALID_PARAMETER;
+    }
+
+    return Status;
+}
+
+NTSTATUS vboxVidPnPopulateSourceModeSetFromLegacy(PDEVICE_EXTENSION pDevExt,
+        D3DKMDT_HVIDPNSOURCEMODESET hNewVidPnSourceModeSet,
+        const DXGK_VIDPNSOURCEMODESET_INTERFACE *pNewVidPnSourceModeSetInterface,
+        VIDEO_MODE_INFORMATION *pModes,
+        uint32_t cModes,
+        uint32_t iPreferredMomde)
+{
+    NTSTATUS Status = STATUS_SUCCESS;
+    for (uint32_t i = 0; i < cModes; ++i)
+    {
+        D3DKMDT_VIDPN_SOURCE_MODE *pNewVidPnSourceModeInfo;
+        Status = pNewVidPnSourceModeSetInterface->pfnCreateNewModeInfo(hNewVidPnSourceModeSet, &pNewVidPnSourceModeInfo);
+        Assert(Status == STATUS_SUCCESS);
+        if (Status == STATUS_SUCCESS)
+        {
+            Status = vboxVidPnPopulateSourceModeInfoFromLegacy(pDevExt, pNewVidPnSourceModeInfo, &pModes[i]);
+            Assert(Status == STATUS_SUCCESS);
+            if (Status == STATUS_SUCCESS)
+            {
+                D3DKMDT_VIDEO_PRESENT_SOURCE_MODE_ID modeId = pNewVidPnSourceModeInfo->Id;
+                Status = pNewVidPnSourceModeSetInterface->pfnAddMode(hNewVidPnSourceModeSet, pNewVidPnSourceModeInfo);
+                Assert(Status == STATUS_SUCCESS);
+                if (Status == STATUS_SUCCESS)
+                {
+                    if (iPreferredMomde == i)
+                    {
+                        Status = pNewVidPnSourceModeSetInterface->pfnPinMode(hNewVidPnSourceModeSet, modeId);
+                        Assert(Status == STATUS_SUCCESS);
+                        if (Status != STATUS_SUCCESS)
+                        {
+                            drprintf((__FUNCTION__": pfnPinMode failed, Status(0x%x)", Status));
+                            /* don't treat it as fatal */
+                            Status = STATUS_SUCCESS;
+                        }
+                    }
+                }
+                else
+                {
+                    drprintf((__FUNCTION__": pfnAddMode failed, Status(0x%x)", Status));
+                    pNewVidPnSourceModeSetInterface->pfnReleaseModeInfo(hNewVidPnSourceModeSet, pNewVidPnSourceModeInfo);
+                    break;
+                }
+            }
+            else
+            {
+                drprintf((__FUNCTION__": pfnCreateNewModeInfo failed, Status(0x%x)", Status));
+                pNewVidPnSourceModeSetInterface->pfnReleaseModeInfo(hNewVidPnSourceModeSet, pNewVidPnSourceModeInfo);
+                break;
+            }
+        }
+    }
+    return Status;
+}
+
+NTSTATUS vboxVidPnPopulateTargetModeInfoFromLegacy(PDEVICE_EXTENSION pDevExt,
+        D3DKMDT_VIDPN_TARGET_MODE *pNewVidPnTargetModeInfo,
+        VIDEO_MODE_INFORMATION *pMode)
+{
+    NTSTATUS Status = STATUS_SUCCESS;
+#if 0
+    todo();
+    if (pMode->AttributeFlags & VIDEO_MODE_GRAPHICS)
+    {
+        /* this is a graphics mode */
+        pNewVidPnSourceModeInfo->Type = D3DKMDT_RMT_GRAPHICS;
+        pNewVidPnSourceModeInfo->Format.Graphics.PrimSurfSize.cx = pMode->VisScreenWidth;
+        pNewVidPnSourceModeInfo->Format.Graphics.PrimSurfSize.cy = pMode->VisScreenHeight;
+        pNewVidPnSourceModeInfo->Format.Graphics.VisibleRegionSize = pNewVidPnSourceModeInfo->Format.Graphics.PrimSurfSize;
+        pNewVidPnSourceModeInfo->Format.Graphics.Stride = pMode->ScreenStride;
+        pNewVidPnSourceModeInfo->Format.Graphics.PixelFormat = vboxWddmCalcPixelFormat(pInfo);
+        Assert(pNewVidPnSourceModeInfo->Format.Graphics.PixelFormat != D3DDDIFMT_UNKNOWN);
+        if (pNewVidPnSourceModeInfo->Format.Graphics.PixelFormat != D3DDDIFMT_UNKNOWN)
+        {
+            pNewVidPnSourceModeInfo->Format.Graphics.ColorBasis = D3DKMDT_CB_SRGB;
+            if (pNewVidPnSourceModeInfo->Format.Graphics.PixelFormat == D3DDDIFMT_P8)
+                pNewVidPnSourceModeInfo->Format.Graphics.PixelValueAccessMode = D3DKMDT_PVAM_SETTABLEPALETTE;
+            else
+                pNewVidPnSourceModeInfo->Format.Graphics.PixelValueAccessMode = D3DKMDT_PVAM_DIRECT;
+        }
+        else
+        {
+            drprintf((__FUNCTION__": vboxWddmCalcPixelFormat failed\n"));
+            Status = STATUS_INVALID_PARAMETER;
+        }
+    }
+    else
+    {
+        /* @todo: XPDM driver does not seem to return text modes, should we? */
+        drprintf((__FUNCTION__": text mode not supported currently\n"));
+        AssertBreakPoint();
+        Status = STATUS_INVALID_PARAMETER;
+    }
+#endif
+    return Status;
+}
+
+NTSTATUS vboxVidPnPopulateTargetModeSetFromLegacy(PDEVICE_EXTENSION pDevExt,
+        D3DKMDT_HVIDPNTARGETMODESET hNewVidPnTargetModeSet,
+        const DXGK_VIDPNTARGETMODESET_INTERFACE *pNewVidPnTargetModeSetInterface,
+        VIDEO_MODE_INFORMATION *pModes,
+        uint32_t cModes,
+        uint32_t iPreferredMomde)
+{
+    NTSTATUS Status = STATUS_SUCCESS;
+    for (uint32_t i = 0; i < cModes; ++i)
+    {
+        D3DKMDT_VIDPN_TARGET_MODE *pNewVidPnTargetModeInfo;
+        Status = pNewVidPnTargetModeSetInterface->pfnCreateNewModeInfo(hNewVidPnTargetModeSet, &pNewVidPnTargetModeInfo);
+        Assert(Status == STATUS_SUCCESS);
+        if (Status == STATUS_SUCCESS)
+        {
+            Status = vboxVidPnPopulateTargetModeInfoFromLegacy(pDevExt, pNewVidPnTargetModeInfo, &pModes[i]);
+            Assert(Status == STATUS_SUCCESS);
+            if (Status == STATUS_SUCCESS)
+            {
+                D3DKMDT_VIDEO_PRESENT_SOURCE_MODE_ID modeId = pNewVidPnTargetModeInfo->Id;
+                Status = pNewVidPnTargetModeSetInterface->pfnAddMode(hNewVidPnTargetModeSet, pNewVidPnTargetModeInfo);
+                Assert(Status == STATUS_SUCCESS);
+                if (Status == STATUS_SUCCESS)
+                {
+                    if (iPreferredMomde == i)
+                    {
+                        Status = pNewVidPnTargetModeSetInterface->pfnPinMode(hNewVidPnTargetModeSet, modeId);
+                        Assert(Status == STATUS_SUCCESS);
+                        if (Status != STATUS_SUCCESS)
+                        {
+                            drprintf((__FUNCTION__": pfnPinMode failed, Status(0x%x)", Status));
+                            /* don't treat it as fatal */
+                            Status = STATUS_SUCCESS;
+                        }
+                    }
+                }
+                else
+                {
+                    drprintf((__FUNCTION__": pfnAddMode failed, Status(0x%x)", Status));
+                    pNewVidPnTargetModeSetInterface->pfnReleaseModeInfo(hNewVidPnTargetModeSet, pNewVidPnTargetModeInfo);
+                    break;
+                }
+            }
+            else
+            {
+                drprintf((__FUNCTION__": pfnCreateNewModeInfo failed, Status(0x%x)", Status));
+                pNewVidPnTargetModeSetInterface->pfnReleaseModeInfo(hNewVidPnTargetModeSet, pNewVidPnTargetModeInfo);
+                break;
+            }
+        }
+    }
+    return Status;
+}
+
 
 DECLCALLBACK(BOOLEAN) vboxVidPnCofuncModalityPathEnum(PDEVICE_EXTENSION pDevExt, const D3DKMDT_HVIDPN hDesiredVidPn, const DXGK_VIDPN_INTERFACE* pVidPnInterface,
@@ -406,9 +597,10 @@
         const D3DKMDT_VIDPN_PRESENT_PATH *pNewVidPnPresentPathInfo, PVOID pContext)
 {
-    D3DKMDT_HVIDPNSOURCEMODESET hNewVidPnSourceModeSet;
-    const DXGK_VIDPNSOURCEMODESET_INTERFACE *pVidPnSourceModeSetInterface;
-    PVBOXVIDPNCMCONTEXT pCbContext = (PVBOXVIDPNCMCONTEXT)pContext;
+    PVBOXVIDPNCOFUNCMODALITY pCbContext = (PVBOXVIDPNCOFUNCMODALITY)pContext;
     NTSTATUS Status = STATUS_SUCCESS;
     pCbContext->Status = STATUS_SUCCESS;
+    VIDEO_MODE_INFORMATION *pModes = pCbContext->pModes;
+    uint32_t cModes = pCbContext->cModes;
+    uint32_t iPreferredMode = pCbContext->iPreferredMode;
 
     /* adjust scaling */
@@ -438,14 +630,16 @@
             || pNewVidPnPresentPathInfo->VidPnSourceId != pCbContext->pEnumCofuncModalityArg->EnumPivot.VidPnSourceId)
     {
+        D3DKMDT_HVIDPNSOURCEMODESET hCurVidPnSourceModeSet;
+        const DXGK_VIDPNSOURCEMODESET_INTERFACE *pCurVidPnSourceModeSetInterface;
 
         Status = pVidPnInterface->pfnAcquireSourceModeSet(hDesiredVidPn,
                     pNewVidPnPresentPathInfo->VidPnSourceId,
-                    &hNewVidPnSourceModeSet,
-                    &pVidPnSourceModeSetInterface);
+                    &hCurVidPnSourceModeSet,
+                    &pCurVidPnSourceModeSetInterface);
         Assert(Status == STATUS_SUCCESS);
         if (Status == STATUS_SUCCESS)
         {
             CONST D3DKMDT_VIDPN_SOURCE_MODE* pPinnedVidPnSourceModeInfo;
-            Status = pVidPnSourceModeSetInterface->pfnAcquirePinnedModeInfo(hNewVidPnSourceModeSet, &pPinnedVidPnSourceModeInfo);
+            Status = pCurVidPnSourceModeSetInterface->pfnAcquirePinnedModeInfo(hCurVidPnSourceModeSet, &pPinnedVidPnSourceModeInfo);
             Assert(Status == STATUS_SUCCESS);
             if (Status == STATUS_SUCCESS)
@@ -453,72 +647,115 @@
                 if (!pPinnedVidPnSourceModeInfo)
                 {
-                    /* no pinned mode set, do adjustment */
-                    /* first iterate to see if there is anything that should be removed/added
-                     * if yes, create a new mode set and populate it with the necessary info */
-
-                    Status = vboxVidPnEnumSourceModes(pDevExt, hDesiredVidPn, pVidPnInterface,
-                        hNewVidPnSourceModeSet, pVidPnSourceModeSetInterface,
-                        vboxVidPnCofuncModalitySourceModeEnum, pCbContext);
+                    /* just create and populate the new source mode set for now */
+                    D3DKMDT_HVIDPNSOURCEMODESET hNewVidPnSourceModeSet;
+                    const DXGK_VIDPNSOURCEMODESET_INTERFACE *pNewVidPnSourceModeSetInterface;
+                    Status = pVidPnInterface->pfnCreateNewSourceModeSet(hDesiredVidPn,
+                                    pNewVidPnPresentPathInfo->VidPnSourceId, /*__in CONST D3DDDI_VIDEO_PRESENT_SOURCE_ID  VidPnSourceId */
+                                    &hNewVidPnSourceModeSet,
+                                    &pNewVidPnSourceModeSetInterface);
                     Assert(Status == STATUS_SUCCESS);
                     if (Status == STATUS_SUCCESS)
                     {
-                        Status = pCbContext->Status;
+                        Status = vboxVidPnPopulateSourceModeSetFromLegacy(pDevExt,
+                                hNewVidPnSourceModeSet, pNewVidPnSourceModeSetInterface,
+                                pModes, cModes, iPreferredMode);
                         Assert(Status == STATUS_SUCCESS);
-                        if (Status != STATUS_SUCCESS)
-                            drprintf((__FUNCTION__": vboxVidPnCofuncModalitySourceModeEnum failed Status(0x%x)\n", Status));
+                        if (Status == STATUS_SUCCESS)
+                        {
+                            Status = pVidPnInterface->pfnAssignSourceModeSet(hDesiredVidPn,
+                                    pNewVidPnPresentPathInfo->VidPnSourceId,
+                                    hNewVidPnSourceModeSet);
+                            Assert(Status == STATUS_SUCCESS);
+                            if(Status != STATUS_SUCCESS)
+                            {
+                                drprintf((__FUNCTION__": pfnAssignSourceModeSet failed Status(0x%x)\n", Status));
+                                pVidPnInterface->pfnReleaseSourceModeSet(hDesiredVidPn, hNewVidPnSourceModeSet);
+                            }
+                        }
+                        else
+                        {
+                            drprintf((__FUNCTION__": vboxVidPnPopulateSourceModeSetFromLegacy failed Status(0x%x)\n", Status));
+                            pVidPnInterface->pfnReleaseSourceModeSet(hDesiredVidPn, hNewVidPnSourceModeSet);
+                        }
                     }
                     else
-                        drprintf((__FUNCTION__": vboxVidPnEnumSourceModes failed Status(0x%x)\n", Status));
-                }
-
-                /* nothing to be done here, just release */
-                pVidPnSourceModeSetInterface->pfnReleaseModeInfo(hNewVidPnSourceModeSet, pPinnedVidPnSourceModeInfo);
+                        drprintf((__FUNCTION__": pfnCreateNewSourceModeSet failed Status(0x%x)\n", Status));
+                }
+                else
+                {
+                    /* release */
+                    pCurVidPnSourceModeSetInterface->pfnReleaseModeInfo(hCurVidPnSourceModeSet, pPinnedVidPnSourceModeInfo);
+                }
             }
             else
                 drprintf((__FUNCTION__": pfnAcquirePinnedModeInfo failed Status(0x%x)\n", Status));
 
-            pVidPnInterface->pfnReleaseSourceModeSet(hDesiredVidPn, hNewVidPnSourceModeSet);
+            pVidPnInterface->pfnReleaseSourceModeSet(hDesiredVidPn, hCurVidPnSourceModeSet);
         }
         else
+        {
             drprintf((__FUNCTION__": pfnAcquireSourceModeSet failed Status(0x%x)\n", Status));
-    }
-
+        }
+    }
+
+    /* ensure we didn't fail earlier */
     if (Status == STATUS_SUCCESS)
     {
-        D3DKMDT_HVIDPNTARGETMODESET hNewVidPnTargetModeSet;
-        const DXGK_VIDPNTARGETMODESET_INTERFACE *pVidPnTargetModeSetInterface;
-        Status = pVidPnInterface->pfnAcquireTargetModeSet(hDesiredVidPn,
-                            pNewVidPnPresentPathInfo->VidPnTargetId,
-                            &hNewVidPnTargetModeSet,
-                            &pVidPnTargetModeSetInterface);
+        if (pCbContext->pEnumCofuncModalityArg->EnumPivotType != D3DKMDT_EPT_VIDPNTARGET
+                || pNewVidPnPresentPathInfo->VidPnTargetId != pCbContext->pEnumCofuncModalityArg->EnumPivot.VidPnTargetId)
+        {
+            D3DKMDT_HVIDPNTARGETMODESET hCurVidPnTargetModeSet;
+            const DXGK_VIDPNTARGETMODESET_INTERFACE *pCurVidPnTargetModeSetInterface;
+            Status = pVidPnInterface->pfnAcquireTargetModeSet(hDesiredVidPn,
+                                pNewVidPnPresentPathInfo->VidPnTargetId,
+                                &hCurVidPnTargetModeSet,
+                                &pCurVidPnTargetModeSetInterface);
             Assert(Status == STATUS_SUCCESS);
             if (Status == STATUS_SUCCESS)
             {
                 CONST D3DKMDT_VIDPN_TARGET_MODE* pPinnedVidPnTargetModeInfo;
-                Status = pVidPnTargetModeSetInterface->pfnAcquirePinnedModeInfo(hNewVidPnTargetModeSet, &pPinnedVidPnTargetModeInfo);
+                Status = pCurVidPnTargetModeSetInterface->pfnAcquirePinnedModeInfo(hCurVidPnTargetModeSet, &pPinnedVidPnTargetModeInfo);
                 Assert(Status == STATUS_SUCCESS);
                 if (Status == STATUS_SUCCESS)
                 {
-                    if (pPinnedVidPnTargetModeInfo)
+                    if (!pPinnedVidPnTargetModeInfo)
                     {
-                        /* nothing to be done here, just release */
-                        pVidPnTargetModeSetInterface->pfnReleaseModeInfo(hNewVidPnTargetModeSet, pPinnedVidPnTargetModeInfo);
+                        /* just create and populate a new target mode info for now */
+                        D3DKMDT_HVIDPNTARGETMODESET hNewVidPnTargetModeSet;
+                        const DXGK_VIDPNTARGETMODESET_INTERFACE *pNewVidPnTargetModeSetInterface;
+                        Status = pVidPnInterface->pfnCreateNewTargetModeSet(hDesiredVidPn,
+                                        pNewVidPnPresentPathInfo->VidPnTargetId, /*__in CONST D3DDDI_VIDEO_PRESENT_TARGET_ID  VidPnTargetId */
+                                        &hNewVidPnTargetModeSet,
+                                        &pNewVidPnTargetModeSetInterface);
+                        Assert(Status == STATUS_SUCCESS);
+                        if (Status == STATUS_SUCCESS)
+                        {
+                            Status = vboxVidPnPopulateTargetModeSetFromLegacy(pDevExt,
+                                    hNewVidPnTargetModeSet, pNewVidPnTargetModeSetInterface,
+                                    pModes, cModes, iPreferredMode);
+                            Assert(Status == STATUS_SUCCESS);
+                            if (Status == STATUS_SUCCESS)
+                            {
+                                Status = pVidPnInterface->pfnAssignTargetModeSet(hDesiredVidPn,
+                                        pNewVidPnPresentPathInfo->VidPnTargetId,
+                                        hNewVidPnTargetModeSet);
+                                Assert(Status == STATUS_SUCCESS);
+                                if(Status != STATUS_SUCCESS)
+                                {
+                                    drprintf((__FUNCTION__": pfnAssignTargetModeSet failed Status(0x%x)\n", Status));
+                                    pVidPnInterface->pfnReleaseTargetModeSet(hDesiredVidPn, hNewVidPnTargetModeSet);
+                                }
+                            }
+                            else
+                            {
+                                drprintf((__FUNCTION__": vboxVidPnPopulateTargetModeSetFromLegacy failed Status(0x%x)\n", Status));
+                                pVidPnInterface->pfnReleaseTargetModeSet(hDesiredVidPn, hNewVidPnTargetModeSet);
+                            }
+                        }
                     }
                     else
                     {
-                        /* no pinned mode set, do adjustment */
-                        Status = vboxVidPnEnumTargetModes(pDevExt, hDesiredVidPn, pVidPnInterface,
-                                hNewVidPnTargetModeSet, pVidPnTargetModeSetInterface,
-                                vboxVidPnCofuncModalityTargetModeEnum, pCbContext);
-                        Assert(Status == STATUS_SUCCESS);
-                        if (Status == STATUS_SUCCESS)
-                        {
-                            Status = pCbContext->Status;
-                            Assert(Status == STATUS_SUCCESS);
-                            if (Status != STATUS_SUCCESS)
-                                drprintf((__FUNCTION__": vboxVidPnCofuncModalityTargetModeEnum failed Status(0x%x)\n", Status));
-                        }
-                        else
-                            drprintf((__FUNCTION__": vboxVidPnEnumTargetModes failed Status(0x%x)\n", Status));
+                        /* nothing to be done here, just release */
+                        pCurVidPnTargetModeSetInterface->pfnReleaseModeInfo(hCurVidPnTargetModeSet, pPinnedVidPnTargetModeInfo);
                     }
                 }
@@ -526,8 +763,9 @@
                     drprintf((__FUNCTION__": pfnAcquirePinnedModeInfo failed Status(0x%x)\n", Status));
 
-                pVidPnInterface->pfnReleaseTargetModeSet(hDesiredVidPn, hNewVidPnTargetModeSet);
+                pVidPnInterface->pfnReleaseTargetModeSet(hDesiredVidPn, hCurVidPnTargetModeSet);
             }
             else
                 drprintf((__FUNCTION__": pfnAcquireTargetModeSet failed Status(0x%x)\n", Status));
+        }
     }
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoWddm.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoWddm.cpp	(revision 26319)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoWddm.cpp	(revision 26320)
@@ -199,4 +199,70 @@
             return 0;
     }
+}
+
+D3DDDIFORMAT vboxWddmCalcPixelFormat(VIDEO_MODE_INFORMATION *pInfo)
+{
+    switch (pInfo->BitsPerPlane)
+    {
+        case 32:
+            if(!(pInfo->AttributeFlags & VIDEO_MODE_PALETTE_DRIVEN) && !(pInfo->AttributeFlags & VIDEO_MODE_MANAGED_PALETTE))
+            {
+                if (pInfo->RedMask == 0xFF0000 && pInfo->GreenMask == 0xFF00 && pInfo->BlueMask == 0xFF)
+                    return D3DDDIFMT_X8R8G8B8;
+                drprintf((__FUNCTION__": unsupported format: bpp(%d), rmask(%d), gmask(%d), bmask(%d)\n", pInfo->BitsPerPlane, pInfo->RedMask, pInfo->GreenMask, pInfo->BlueMask));
+                AssertBreakpoint();
+            }
+            else
+            {
+                drprintf((__FUNCTION__": unsupported AttributeFlags(0x%x)\n", pInfo->AttributeFlags));
+                AssertBreakpoint();
+            }
+            break;
+        case 24:
+            if(!(pInfo->AttributeFlags & VIDEO_MODE_PALETTE_DRIVEN) && !(pInfo->AttributeFlags & VIDEO_MODE_MANAGED_PALETTE))
+            {
+                if (pInfo->RedMask == 0xFF0000 && pInfo->GreenMask == 0xFF00 && pInfo->BlueMask == 0xFF)
+                    return D3DDDIFMT_R8G8B8;
+                drprintf((__FUNCTION__": unsupported format: bpp(%d), rmask(%d), gmask(%d), bmask(%d)\n", pInfo->BitsPerPlane, pInfo->RedMask, pInfo->GreenMask, pInfo->BlueMask));
+                AssertBreakpoint();
+            }
+            else
+            {
+                drprintf((__FUNCTION__": unsupported AttributeFlags(0x%x)\n", pInfo->AttributeFlags));
+                AssertBreakpoint();
+            }
+            break;
+        case 16:
+            if(!(pInfo->AttributeFlags & VIDEO_MODE_PALETTE_DRIVEN) && !(pInfo->AttributeFlags & VIDEO_MODE_MANAGED_PALETTE))
+            {
+                if (pInfo->RedMask == 0xF800 && pInfo->GreenMask == 0x7E0 && pInfo->BlueMask == 0x1F)
+                    return D3DDDIFMT_R5G6B5;
+                drprintf((__FUNCTION__": unsupported format: bpp(%d), rmask(%d), gmask(%d), bmask(%d)\n", pInfo->BitsPerPlane, pInfo->RedMask, pInfo->GreenMask, pInfo->BlueMask));
+                AssertBreakpoint();
+            }
+            else
+            {
+                drprintf((__FUNCTION__": unsupported AttributeFlags(0x%x)\n", pInfo->AttributeFlags));
+                AssertBreakpoint();
+            }
+            break;
+        case 8:
+            if((pInfo->AttributeFlags & VIDEO_MODE_PALETTE_DRIVEN) && (pInfo->AttributeFlags & VIDEO_MODE_MANAGED_PALETTE))
+            {
+                return D3DDDIFMT_P8;
+            }
+            else
+            {
+                drprintf((__FUNCTION__": unsupported AttributeFlags(0x%x)\n", pInfo->AttributeFlags));
+                AssertBreakpoint();
+            }
+            break;
+        default:
+            drprintf((__FUNCTION__": unsupported bpp(%d)\n", pInfo->BitsPerPlane));
+            AssertBreakpoint();
+            break;
+    }
+
+    return D3DDDIFMT_UNKNOWN;
 }
 
@@ -1439,6 +1505,9 @@
         if (Status == STATUS_SUCCESS)
         {
-            VBOXVIDPNCMCONTEXT CbContext = {0};
+            VBOXVIDPNCOFUNCMODALITY CbContext = {0};
             CbContext.pEnumCofuncModalityArg = pEnumCofuncModalityArg;
+            VBoxWddmGetModesTable(pContext, true, &CbContext.pModes, &CbContext.cModes, &CbContext.iPreferredMode);
+            Assert(CbContext.cModes);
+            Assert(CbContext.cModes > CbContext.iPreferredMode);
             Status = vboxVidPnEnumPaths(pContext, pEnumCofuncModalityArg->hConstrainingVidPn, pVidPnInterface,
                     hVidPnTopology, pVidPnTopologyInterface,
