Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPTypes.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPTypes.h	(revision 95158)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPTypes.h	(revision 95191)
@@ -202,4 +202,5 @@
 #endif
     VBOXUHGSMI_BUFFER_TYPE_FLAGS fUhgsmiType;
+    int                         CurVidPnSourceId;              /* Source index if used for a source, otherwise -1. */
 #ifdef VBOX_WITH_VMSVGA3D_DX
     /* Direct3D driver data for .enmType == VBOXWDDM_ALLOC_TYPE_D3D. */
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.cpp	(revision 95158)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.cpp	(revision 95191)
@@ -2140,4 +2140,5 @@
             KeInitializeSpinLock(&pAllocation->OpenLock);
             InitializeListHead(&pAllocation->OpenList);
+            pAllocation->CurVidPnSourceId = -1;
 
             switch (pAllocInfo->enmType)
@@ -2395,4 +2396,16 @@
     vboxVDbgBreakFv();
 
+    PVBOXMP_DEVEXT pDevExt = (PVBOXMP_DEVEXT)hAdapter;
+
+    for (UINT i = 0; i < pDestroyAllocation->NumAllocations; ++i)
+    {
+        PVBOXWDDM_ALLOCATION pAllocation = (PVBOXWDDM_ALLOCATION)pDestroyAllocation->pAllocationList[0];
+        if (pAllocation->CurVidPnSourceId != -1)
+        {
+            VBOXWDDM_SOURCE *pSource = &pDevExt->aSources[pAllocation->CurVidPnSourceId];
+            vboxWddmAssignPrimary(pSource, NULL, pAllocation->CurVidPnSourceId);
+        }
+    }
+
 #ifdef VBOX_WITH_VMSVGA3D_DX
     /* Check if this is a request from the D3D driver. */
@@ -2408,6 +2421,4 @@
 
     PVBOXWDDM_RESOURCE pRc = (PVBOXWDDM_RESOURCE)pDestroyAllocation->hResource;
-    PVBOXMP_DEVEXT pDevExt = (PVBOXMP_DEVEXT)hAdapter;
-
     if (pRc)
     {
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.h	(revision 95158)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPWddm.h	(revision 95191)
@@ -122,4 +122,5 @@
         pOldAlloc->bAssigned = FALSE;
         Assert(pOldAlloc->AllocData.SurfDesc.VidPnSourceId == srcId);
+        pOldAlloc->CurVidPnSourceId = -1;
     }
 
@@ -129,4 +130,5 @@
         pAllocation->bAssigned = TRUE;
         pAllocation->bVisible = pSource->bVisible;
+        pAllocation->CurVidPnSourceId = srcId;
 
         if (pSource->AllocData.hostID != pAllocation->AllocData.hostID)
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/gallium/VBoxMPDX.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/gallium/VBoxMPDX.cpp	(revision 95158)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/gallium/VBoxMPDX.cpp	(revision 95191)
@@ -215,4 +215,7 @@
         pAllocation->dx.SegmentId = 2;
     }
+    else
+        AssertFailedReturn(STATUS_INVALID_PARAMETER);
+
     pAllocationInfo->EvictionSegmentSet              = 0;
     pAllocationInfo->MaximumRenamingListLength       = 1;
@@ -343,7 +346,7 @@
     pAllocation->dx.pMDL = 0;
 
-    /* Legacy. Unused. */
     KeInitializeSpinLock(&pAllocation->OpenLock);
     InitializeListHead(&pAllocation->OpenList);
+    pAllocation->CurVidPnSourceId = -1;
 
     if (pAllocation->dx.desc.enmAllocationType == VBOXDXALLOCATIONTYPE_SURFACE)
@@ -372,4 +375,6 @@
     AssertReturn(pAllocation->enmType == VBOXWDDM_ALLOC_TYPE_D3D, STATUS_INVALID_PARAMETER);
 
+    Assert(pAllocation->cOpens == 0);
+
     if (pAllocation->dx.desc.enmAllocationType == VBOXDXALLOCATIONTYPE_SURFACE)
         Status = svgaDestroyAllocationSurface(pDevExt->pGa->hw.pSvga, pAllocation);
@@ -429,7 +434,23 @@
     for (unsigned i = 0; i < pRender->PatchLocationListInSize; ++i)
     {
+        if (cOut >= pRender->PatchLocationListOutSize)
+        {
+            /** @todo Merge generation of patches witht SvgaRenderCommandsD3D in order to correctly
+             * split a command buffer in case of STATUS_GRAPHICS_INSUFFICIENT_DMA_BUFFER?
+             */
+            DEBUG_BREAKPOINT_TEST();
+            Status = STATUS_GRAPHICS_INSUFFICIENT_DMA_BUFFER;
+            break;
+        }
+
         D3DDDI_PATCHLOCATIONLIST const *pIn = &pRender->pPatchLocationListIn[i];
         void * const pPatchAddress = (uint8_t *)pvDmaBuffer + pIn->PatchOffset;
         VBOXDXALLOCATIONTYPE const enmAllocationType = (VBOXDXALLOCATIONTYPE)pIn->DriverId;
+
+        /* "Even though the driver's DxgkDdiRender function pre-patches the DMA buffer, the driver
+         *  must still insert all of the references to allocations into the output patch-location list
+         *  that the pPatchLocationListOut member of DXGKARG_RENDER specifies."
+         */
+        pRender->pPatchLocationListOut[cOut] = *pIn;
 
         DXGK_ALLOCATIONLIST *pAllocationListEntry = &pRender->pAllocationList[pIn->AllocationIndex];
@@ -442,25 +463,7 @@
             if (enmAllocationType == VBOXDXALLOCATIONTYPE_SURFACE)
             {
-                /* Surfaces might also need a mobid. */
-                if (pAllocationListEntry->SegmentId == 3)
-                {
-                    /* DEFAULT resources only require the sid, because they exist outside the guest. */
-                    Assert(pAllocation->dx.sid != SVGA3D_INVALID_ID);
-                    Assert(pAllocation->dx.mobid != SVGA3D_INVALID_ID);
-                    Assert(pAllocation->dx.SegmentId == 3);
-
+                if (   pAllocation->dx.sid != SVGA3D_INVALID_ID
+                    && pAllocation->dx.mobid != SVGA3D_INVALID_ID)
                     *(uint32_t *)pPatchAddress = pAllocation->dx.sid;
-                    continue;
-                }
-                else
-                {
-                    /* For Aperture segment, the surface need a mob too, which must be created in BuildPagingBuffer. */
-                    if (   pAllocation->dx.sid != SVGA3D_INVALID_ID
-                        && pAllocation->dx.mobid != SVGA3D_INVALID_ID)
-                    {
-                        *(uint32_t *)pPatchAddress = pAllocation->dx.sid;
-                        continue;
-                    }
-                }
             }
             else if (   enmAllocationType == VBOXDXALLOCATIONTYPE_SHADERS
@@ -468,8 +471,5 @@
             {
                 if (pAllocation->dx.mobid != SVGA3D_INVALID_ID)
-                {
                     *(uint32_t *)pPatchAddress = pAllocation->dx.mobid;
-                    continue;
-                }
             }
         }
@@ -479,11 +479,8 @@
                 || enmAllocationType == VBOXDXALLOCATIONTYPE_SHADERS
                 || enmAllocationType == VBOXDXALLOCATIONTYPE_CO)
-            {
                 *(uint32_t *)pPatchAddress = SVGA3D_INVALID_ID;
-                continue;
-            }
-        }
-
-        pRender->pPatchLocationListOut[cOut++] = *pIn;
+        }
+
+        ++cOut;
     }
 
@@ -919,8 +916,4 @@
 NTSTATUS APIENTRY DxgkDdiDXPatch(PVBOXMP_DEVEXT pDevExt, const DXGKARG_PATCH *pPatch)
 {
-    GALOG(("pDmaBuffer = %p, cbDmaBuffer = %u, cPatches = %u\n",
-           pPatch->pDmaBuffer, pPatch->DmaBufferSubmissionEndOffset - pPatch->DmaBufferSubmissionStartOffset,
-           pPatch->PatchLocationListSubmissionLength));
-
     //DEBUG_BREAKPOINT_TEST();
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/gallium/VBoxMPGaWddm.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/gallium/VBoxMPGaWddm.cpp	(revision 95158)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/gallium/VBoxMPGaWddm.cpp	(revision 95191)
@@ -1276,10 +1276,11 @@
     SvgaFlush(pDevExt->pGa->hw.pSvga);
 
-    GALOG(("pDmaBuffer = %p, cbDmaBuffer = %u, cPatches = %u\n",
+    GALOG(("pDmaBuffer %p, cbDmaBuffer %u, PatchStart %u, cPatches %u, Flags 0xx, context type %d\n",
            pPatch->pDmaBuffer, pPatch->DmaBufferSubmissionEndOffset - pPatch->DmaBufferSubmissionStartOffset,
-           pPatch->PatchLocationListSubmissionLength));
-
-    /* The driver does not need to modify paging and present commands here. */
-    if (pPatch->Flags.Paging || pPatch->Flags.Present)
+           pPatch->PatchLocationListSubmissionStart, pPatch->PatchLocationListSubmissionLength, pPatch->Flags.Value,
+           ((PVBOXWDDM_CONTEXT)pPatch->hContext)->enmType));
+
+    /* The driver does not need to modify paging commands. */
+    if (pPatch->Flags.Paging)
         return STATUS_SUCCESS;
 
