Index: trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp	(revision 30317)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp	(revision 30346)
@@ -1078,18 +1078,4 @@
         fFlags |= D3DLOCK_DONOTWAIT;
     return fFlags;
-}
-
-static void vboxResourcePopulateRcDesc(VBOXWDDM_RC_DESC *pDesc, D3DDDIARG_CREATERESOURCE* pResource)
-{
-    pDesc->fFlags = pResource->Flags;
-    pDesc->enmFormat = pResource->Format;
-    pDesc->enmPool = pResource->Pool;
-    pDesc->enmMultisampleType = pResource->MultisampleType;
-    pDesc->MultisampleQuality = pResource->MultisampleQuality;
-    pDesc->MipLevels = pResource->MipLevels;
-    pDesc->Fvf = pResource->Fvf;
-    pDesc->VidPnSourceId = pResource->VidPnSourceId;
-    pDesc->RefreshRate = pResource->RefreshRate;
-    pDesc->enmRotation = pResource->Rotation;
 }
 
@@ -2565,4 +2551,9 @@
 }
 
+static void vboxWddmRequestAllocFree(D3DDDICB_ALLOCATE* pAlloc)
+{
+    RTMemFree(pAlloc);
+}
+
 static D3DDDICB_ALLOCATE* vboxWddmRequestAllocAlloc(D3DDDIARG_CREATERESOURCE* pResource)
 {
@@ -2610,30 +2601,42 @@
     PVBOXWDDMDISP_ADAPTER pAdapter = pDevice->pAdapter;
 
-    if (VBOXDISPMODE_IS_3D(pAdapter))
-    {
-        PVBOXWDDMDISP_RESOURCE pRc = vboxResourceAlloc(pResource->SurfCount);
-        Assert(pRc);
-        if (pRc)
-        {
-            pRc->hResource = pResource->hResource;
-            pRc->pDevice = pDevice;
-            pRc->fFlags = VBOXWDDM_RESOURCE_F_TYPE_GENERIC;
-            vboxResourcePopulateRcDesc(&pRc->RcDesc, pResource);
-            pRc->cAllocations = pResource->SurfCount;
-            for (UINT i = 0; i < pResource->SurfCount; ++i)
-            {
-                PVBOXWDDMDISP_ALLOCATION pAllocation = &pRc->aAllocations[i];
-                CONST D3DDDI_SURFACEINFO* pSurf = &pResource->pSurfList[i];
-                pAllocation->hAllocation = NULL;
-                pAllocation->enmType = VBOXWDDM_ALLOC_TYPE_UMD_RC_GENERIC;
-                pAllocation->pvMem = (void*)pSurf->pSysMem;
-                pAllocation->SurfDesc.pitch = pSurf->SysMemPitch;
-                pAllocation->SurfDesc.slicePitch = pSurf->SysMemSlicePitch;
-                pAllocation->SurfDesc.depth = pSurf->Depth;
-                pAllocation->SurfDesc.width = pSurf->Width;
-                pAllocation->SurfDesc.height = pSurf->Height;
-                pAllocation->SurfDesc.format = pResource->Format;
-            }
-
+    PVBOXWDDMDISP_RESOURCE pRc = vboxResourceAlloc(pResource->SurfCount);
+    Assert(pRc);
+    if (pRc)
+    {
+        bool bIssueCreateResource = false;
+
+        pRc->hResource = pResource->hResource;
+        pRc->hKMResource = NULL;
+        pRc->pDevice = pDevice;
+        pRc->fFlags = VBOXWDDM_RESOURCE_F_TYPE_GENERIC;
+        pRc->RcDesc.fFlags = pResource->Flags;
+        pRc->RcDesc.enmFormat = pResource->Format;
+        pRc->RcDesc.enmPool = pResource->Pool;
+        pRc->RcDesc.enmMultisampleType = pResource->MultisampleType;
+        pRc->RcDesc.MultisampleQuality = pResource->MultisampleQuality;
+        pRc->RcDesc.MipLevels = pResource->MipLevels;
+        pRc->RcDesc.Fvf = pResource->Fvf;
+        pRc->RcDesc.VidPnSourceId = pResource->VidPnSourceId;
+        pRc->RcDesc.RefreshRate = pResource->RefreshRate;
+        pRc->RcDesc.enmRotation = pResource->Rotation;
+        pRc->cAllocations = pResource->SurfCount;
+        for (UINT i = 0; i < pResource->SurfCount; ++i)
+        {
+            PVBOXWDDMDISP_ALLOCATION pAllocation = &pRc->aAllocations[i];
+            CONST D3DDDI_SURFACEINFO* pSurf = &pResource->pSurfList[i];
+            pAllocation->hAllocation = NULL;
+            pAllocation->enmType = VBOXWDDM_ALLOC_TYPE_UMD_RC_GENERIC;
+            pAllocation->pvMem = (void*)pSurf->pSysMem;
+            pAllocation->SurfDesc.pitch = pSurf->SysMemPitch;
+            pAllocation->SurfDesc.slicePitch = pSurf->SysMemSlicePitch;
+            pAllocation->SurfDesc.depth = pSurf->Depth;
+            pAllocation->SurfDesc.width = pSurf->Width;
+            pAllocation->SurfDesc.height = pSurf->Height;
+            pAllocation->SurfDesc.format = pResource->Format;
+        }
+
+        if (VBOXDISPMODE_IS_3D(pAdapter))
+        {
             if (pResource->Flags.RenderTarget)
             {
@@ -2642,4 +2645,5 @@
                 Assert(!pDevice->pDevice9If);
                 Assert(!pDevice->hWnd);
+                bIssueCreateResource = true;
                 hr = VBoxDispWndCreate(pAdapter, pResource->pSurfList[0].Width, pResource->pSurfList[0].Height, &hWnd);
                 Assert(hr == S_OK);
@@ -2948,38 +2952,11 @@
                 Assert(0);
             }
-
-            if (hr == S_OK)
-                pResource->hResource = pRc;
-            else
-                vboxResourceFree(pRc);
         }
         else
-        {
-            hr = E_OUTOFMEMORY;
-        }
-
-    }
-    else
-    {
-        PVBOXWDDMDISP_RESOURCE pRc = vboxResourceAlloc(pResource->SurfCount);
-        Assert(pRc);
-        if (pRc)
-        {
-            pRc->hResource = pResource->hResource;
-            pRc->hKMResource = NULL;
-            pRc->pDevice = pDevice;
-            pRc->fFlags = VBOXWDDM_RESOURCE_F_TYPE_GENERIC;
-            pRc->RcDesc.fFlags = pResource->Flags;
-            pRc->RcDesc.enmFormat = pResource->Format;
-            pRc->RcDesc.enmPool = pResource->Pool;
-            pRc->RcDesc.enmMultisampleType = pResource->MultisampleType;
-            pRc->RcDesc.MultisampleQuality = pResource->MultisampleQuality;
-            pRc->RcDesc.MipLevels = pResource->MipLevels;
-            pRc->RcDesc.Fvf = pResource->Fvf;
-            pRc->RcDesc.VidPnSourceId = pResource->VidPnSourceId;
-            pRc->RcDesc.RefreshRate = pResource->RefreshRate;
-            pRc->RcDesc.enmRotation = pResource->Rotation;
-            pRc->cAllocations = pResource->SurfCount;
-
+            bIssueCreateResource = true;
+
+
+        if (hr == S_OK && bIssueCreateResource)
+        {
             D3DDDICB_ALLOCATE *pDdiAllocate = vboxWddmRequestAllocAlloc(pResource);
             Assert(pDdiAllocate);
@@ -3005,5 +2982,5 @@
                     pDdiAllocI->VidPnSourceId = pResource->VidPnSourceId;
                     pDdiAllocI->Flags.Value = 0;
-                    if (pResource->Flags.Primary)
+                    if (pResource->Flags.Primary && pResource->Flags.RenderTarget)
                         pDdiAllocI->Flags.Primary = 1;
 
@@ -3041,4 +3018,6 @@
                 if (hr == S_OK)
                 {
+                    pRc->hKMResource = pDdiAllocate->hKMResource;
+
                     for (UINT i = 0; i < pResource->SurfCount; ++i)
                     {
@@ -3053,4 +3032,6 @@
                     }
                 }
+
+                vboxWddmRequestAllocFree(pDdiAllocate);
             }
             else
@@ -3064,9 +3045,15 @@
                 vboxResourceFree(pRc);
         }
+
+        if (hr == S_OK)
+            pResource->hResource = pRc;
         else
-        {
-            hr = E_OUTOFMEMORY;
-        }
-    }
+            vboxResourceFree(pRc);
+    }
+    else
+    {
+        hr = E_OUTOFMEMORY;
+    }
+
 
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p)\n", hDevice));
@@ -3104,5 +3091,8 @@
         }
     }
-    else
+
+    Assert(pRc->hResource);
+    Assert(pRc->hKMResource || VBOXDISPMODE_IS_3D(pAdapter));
+    if (pRc->hKMResource)
     {
         if (!(pRc->fFlags & VBOXWDDM_RESOURCE_F_OPENNED))
@@ -3110,5 +3100,4 @@
             D3DDDICB_DEALLOCATE Dealloc;
             Dealloc.hResource = pRc->hResource;
-            Assert(pRc->hResource);
             /* according to the docs the below two are ignored in case we set the hResource */
             Dealloc.NumAllocations = 0;
@@ -3157,5 +3146,7 @@
 #endif
     }
+#if 1
     else
+#endif
     {
         D3DDDICB_PRESENT DdiPresent = {0};
@@ -3163,4 +3154,5 @@
         {
             PVBOXWDDMDISP_RESOURCE pRc = (PVBOXWDDMDISP_RESOURCE)pData->hSrcResource;
+            Assert(pRc->hKMResource);
             Assert(pRc->cAllocations > pData->SrcSubResourceIndex);
             PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[pData->SrcSubResourceIndex];
@@ -3171,4 +3163,5 @@
         {
             PVBOXWDDMDISP_RESOURCE pRc = (PVBOXWDDMDISP_RESOURCE)pData->hDstResource;
+            Assert(pRc->hKMResource);
             Assert(pRc->cAllocations > pData->DstSubResourceIndex);
             PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[pData->DstSubResourceIndex];
Index: trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoWddm.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoWddm.cpp	(revision 30317)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoWddm.cpp	(revision 30346)
@@ -1542,5 +1542,13 @@
                                     Status = STATUS_UNSUCCESSFUL;
                             }
+                            else
 #endif
+                            if (pAllocInfo->fFlags.RenderTarget)
+                            {
+                                pAllocationInfo->Flags.CpuVisible = 1;
+                                Assert(pAllocation->SurfDesc.bpp);
+                                Assert(pAllocation->SurfDesc.pitch);
+                                Assert(pAllocation->SurfDesc.cbSize);
+                            }
                         }
 //                        else
