Index: trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-dx-dx11.cpp
===================================================================
--- trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-dx-dx11.cpp	(revision 102617)
+++ trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-dx-dx11.cpp	(revision 102622)
@@ -404,5 +404,4 @@
 /* Static function prototypes. */
 static int dxDeviceFlush(DXDEVICE *pDevice);
-static int dxDefineShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId, SVGACOTableDXSRViewEntry const *pEntry);
 static int dxDefineUnorderedAccessView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dUAViewId uaViewId, SVGACOTableDXUAViewEntry const *pEntry);
 static int dxDefineRenderTargetView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dRenderTargetViewId renderTargetViewId, SVGACOTableDXRTViewEntry const *pEntry);
@@ -5918,9 +5917,9 @@
 static int dxSetShaderResources(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderType type)
 {
-    DXDEVICE *pDevice = dxDeviceFromContext(pThisCC->svga.p3dState, pDXContext);
-    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
-
-//DEBUG_BREAKPOINT_TEST();
+    DXDEVICE *pDXDevice = dxDeviceFromContext(pThisCC->svga.p3dState, pDXContext);
+    AssertReturn(pDXDevice->pDevice, VERR_INVALID_STATE);
+
     AssertReturn(type >= SVGA3D_SHADERTYPE_MIN && type < SVGA3D_SHADERTYPE_MAX, VERR_INVALID_PARAMETER);
+
     uint32_t const idxShaderState = type - SVGA3D_SHADERTYPE_MIN;
     uint32_t const *pSRIds = &pDXContext->svgaDXContext.shaderState[idxShaderState].shaderResources[0];
@@ -5928,5 +5927,5 @@
     for (uint32_t i = 0; i < SVGA3D_DX_MAX_SRVIEWS; ++i)
     {
-        SVGA3dShaderResourceViewId shaderResourceViewId = pSRIds[i];
+        SVGA3dShaderResourceViewId const shaderResourceViewId = pSRIds[i];
         if (shaderResourceViewId != SVGA3D_INVALID_ID)
         {
@@ -5941,5 +5940,5 @@
     }
 
-    dxShaderResourceViewSet(pDevice, type, 0, SVGA3D_DX_MAX_SRVIEWS, papShaderResourceView);
+    dxShaderResourceViewSet(pDXDevice, type, 0, SVGA3D_DX_MAX_SRVIEWS, papShaderResourceView);
     return VINF_SUCCESS;
 }
@@ -5948,12 +5947,6 @@
 static DECLCALLBACK(int) vmsvga3dBackDXSetShaderResources(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, uint32_t startView, SVGA3dShaderType type, uint32_t cShaderResourceViewId, SVGA3dShaderResourceViewId const *paShaderResourceViewId)
 {
-    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
-    RT_NOREF(pBackend);
-
-    DXDEVICE *pDevice = dxDeviceFromContext(pThisCC->svga.p3dState, pDXContext);
-    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
-
-    RT_NOREF(startView, type, cShaderResourceViewId, paShaderResourceViewId);
-
+    /* Shader resources will be set in setupPipeline. */
+    RT_NOREF(pThisCC, pDXContext, startView, type, cShaderResourceViewId, paShaderResourceViewId);
     return VINF_SUCCESS;
 }
@@ -6416,4 +6409,19 @@
             break;
         }
+        case DXGI_FORMAT_R32_FLOAT:
+        {
+            float const *pValues = (float const *)pvElementData;
+            Log8(("{ " FLOAT_FMT_STR " },",
+                 FLOAT_FMT_ARGS(pValues[0])));
+            break;
+        }
+        case DXGI_FORMAT_R16G16B16A16_FLOAT:
+        {
+            uint16_t const *pValues = (uint16_t const *)pvElementData;
+            Log8(("{ /*f16*/ " FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR " },",
+                 FLOAT_FMT_ARGS(float16ToFloat(pValues[0])), FLOAT_FMT_ARGS(float16ToFloat(pValues[1])),
+                 FLOAT_FMT_ARGS(float16ToFloat(pValues[2])), FLOAT_FMT_ARGS(float16ToFloat(pValues[3]))));
+            break;
+        }
         case DXGI_FORMAT_R16G16_FLOAT:
         {
@@ -6449,4 +6457,11 @@
             Log8(("{ %u },",
                  pValues[0]));
+            break;
+        }
+        case DXGI_FORMAT_R16G16B16A16_SINT:
+        {
+            int16_t const *pValues = (int16_t const *)pvElementData;
+            Log8(("{ /*s16*/ %d, %d, %d, %d },",
+                 pValues[0], pValues[1], pValues[2], pValues[3]));
             break;
         }
@@ -6767,4 +6782,37 @@
 
 
+static int dxCreateShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId, SVGACOTableDXSRViewEntry const *pEntry)
+{
+    PVMSVGA3DSURFACE pSurface;
+    ID3D11Resource *pResource;
+    int rc = dxEnsureResource(pThisCC, pDXContext, pEntry->sid, &pSurface, &pResource);
+    AssertRCReturn(rc, rc);
+
+    DXVIEW *pView = &pDXContext->pBackendDXContext->paShaderResourceView[shaderResourceViewId];
+    Assert(pView->u.pView == NULL);
+
+    ID3D11ShaderResourceView *pShaderResourceView;
+    HRESULT hr = dxShaderResourceViewCreate(pThisCC, pDXContext, pEntry, pSurface, &pShaderResourceView);
+    AssertReturn(SUCCEEDED(hr), VERR_INVALID_STATE);
+
+    return dxViewInit(pView, pSurface, pDXContext, shaderResourceViewId, VMSVGA3D_VIEWTYPE_SHADERRESOURCE, pShaderResourceView);
+}
+
+
+static int dxEnsureShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId viewId, DXVIEW **ppResult)
+{
+    ASSERT_GUEST_RETURN(viewId < pDXContext->cot.cSRView, VERR_INVALID_PARAMETER);
+
+    DXVIEW *pDXView = &pDXContext->pBackendDXContext->paShaderResourceView[viewId];
+    if (!pDXView->u.pView)
+    {
+        SVGACOTableDXSRViewEntry const *pEntry = &pDXContext->cot.paSRView[viewId];
+        int rc = dxCreateShaderResourceView(pThisCC, pDXContext, viewId, pEntry);
+        AssertRCReturn(rc, rc);
+    }
+    *ppResult = pDXView;
+    return VINF_SUCCESS;
+}
+
 static void dxSetupPipeline(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
 {
@@ -6800,43 +6848,20 @@
             if (shaderResourceViewId != SVGA3D_INVALID_ID)
             {
-                ASSERT_GUEST_RETURN_VOID(shaderResourceViewId < pDXContext->pBackendDXContext->cShaderResourceView);
-
+                DXVIEW *pDXView;
+                rc = dxEnsureShaderResourceView(pThisCC, pDXContext, shaderResourceViewId, &pDXView);
+                AssertContinue(RT_SUCCESS(rc));
+
+#ifdef LOG_ENABLED
                 SVGACOTableDXSRViewEntry const *pSRViewEntry = dxGetShaderResourceViewEntry(pDXContext, shaderResourceViewId);
-                AssertContinue(pSRViewEntry != NULL);
-
-                uint32_t const sid = pSRViewEntry->sid;
-
-                PVMSVGA3DSURFACE pSurface;
-                rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, sid, &pSurface);
-                if (RT_FAILURE(rc))
-                {
-                    AssertMsgFailed(("sid = %u, rc = %Rrc\n", sid, rc));
-                    continue;
-                }
-
-                /* The guest might have invalidated the surface in which case pSurface->pBackendSurface is NULL. */
-                /** @todo This is not needed for "single DX device" mode. */
-                if (pSurface->pBackendSurface)
-                {
-                    /* Wait for the surface to finish drawing. */
-                    dxSurfaceWait(pThisCC->svga.p3dState, pSurface, pDXContext->cid);
-                }
-
-                /* If a view has not been created yet, do it now. */
-                if (!pDXContext->pBackendDXContext->paShaderResourceView[shaderResourceViewId].u.pView)
-                {
-//DEBUG_BREAKPOINT_TEST();
-                    LogFunc(("Re-creating SRV: sid=%u srvid = %u\n", sid, shaderResourceViewId));
-                    rc = dxDefineShaderResourceView(pThisCC, pDXContext, shaderResourceViewId, pSRViewEntry);
-                    AssertContinue(RT_SUCCESS(rc));
-                }
-
+                PVMSVGA3DSURFACE pSurface = NULL;
+                vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, pDXView->sid, &pSurface);
                 LogFunc(("srv[%d][%d] sid = %u, srvid = %u, format = %s(%d), %dx%d\n",
-                         idxShaderState, idxSR, sid, shaderResourceViewId, vmsvgaLookupEnum((int)pSRViewEntry->format, &g_SVGA3dSurfaceFormat2String), pSRViewEntry->format,
+                         idxShaderState, idxSR, pDXView->sid, shaderResourceViewId, vmsvgaLookupEnum((int)pSRViewEntry->format, &g_SVGA3dSurfaceFormat2String), pSRViewEntry->format,
                          pSurface->paMipmapLevels[0].cBlocksX * pSurface->cxBlock, pSurface->paMipmapLevels[0].cBlocksY * pSurface->cyBlock));
+#endif
 
 #ifdef DUMP_BITMAPS
                 SVGA3dSurfaceImageId image;
-                image.sid = sid;
+                image.sid = pDXView->sid;
                 image.face = 0;
                 image.mipmap = 0;
@@ -6849,12 +6874,10 @@
                 }
                 else
-                    Log(("Map failed %Rrc\n", rc));
+                    LogFunc(("Map failed %Rrc\n", rc));
 #endif
             }
         }
 
-        /* Set shader resources. */
-        rc = dxSetShaderResources(pThisCC, pDXContext, (SVGA3dShaderType)(idxShaderState + SVGA3D_SHADERTYPE_MIN));
-        AssertRC(rc);
+        dxSetShaderResources(pThisCC, pDXContext, (SVGA3dShaderType)(idxShaderState + SVGA3D_SHADERTYPE_MIN));
     }
 
@@ -8891,79 +8914,31 @@
 static DECLCALLBACK(int) vmsvga3dBackDXGenMips(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId)
 {
+    DXDEVICE *pDXDevice = dxDeviceFromContext(pThisCC->svga.p3dState, pDXContext);
+    AssertReturn(pDXDevice->pDevice, VERR_INVALID_STATE);
+
+    DXVIEW *pDXView;
+    int rc = dxEnsureShaderResourceView(pThisCC, pDXContext, shaderResourceViewId, &pDXView);
+    AssertRCReturn(rc, rc);
+
+    pDXDevice->pImmediateContext->GenerateMips(pDXView->u.pShaderResourceView);
+    return VINF_SUCCESS;
+}
+
+
+static DECLCALLBACK(int) vmsvga3dBackDXDefineShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId, SVGACOTableDXSRViewEntry const *pEntry)
+{
+    /* The view is created when it is used in setupPipeline. */
+    RT_NOREF(pThisCC, pDXContext, shaderResourceViewId, pEntry);
+    return VINF_SUCCESS;
+}
+
+
+static DECLCALLBACK(int) vmsvga3dBackDXDestroyShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId)
+{
     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     RT_NOREF(pBackend);
 
-    DXDEVICE *pDevice = dxDeviceFromContext(pThisCC->svga.p3dState, pDXContext);
-    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
-
-    ID3D11ShaderResourceView *pShaderResourceView = pDXContext->pBackendDXContext->paShaderResourceView[shaderResourceViewId].u.pShaderResourceView;
-    AssertReturn(pShaderResourceView, VERR_INVALID_STATE);
-
-    SVGACOTableDXSRViewEntry const *pSRViewEntry = dxGetShaderResourceViewEntry(pDXContext, shaderResourceViewId);
-    AssertReturn(pSRViewEntry, VERR_INVALID_STATE);
-
-    uint32_t const sid = pSRViewEntry->sid;
-
-    PVMSVGA3DSURFACE pSurface;
-    int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, sid, &pSurface);
-    AssertRCReturn(rc, rc);
-    AssertReturn(pSurface->pBackendSurface, VERR_INVALID_STATE);
-
-    pDevice->pImmediateContext->GenerateMips(pShaderResourceView);
-
-    pSurface->pBackendSurface->cidDrawing = pDXContext->cid;
-    return VINF_SUCCESS;
-}
-
-
-static int dxDefineShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId, SVGACOTableDXSRViewEntry const *pEntry)
-{
-    /* Get corresponding resource for pEntry->sid. Create the surface if does not yet exist. */
-    PVMSVGA3DSURFACE pSurface;
-    int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, pEntry->sid, &pSurface);
-    AssertRCReturn(rc, rc);
-
-    ID3D11ShaderResourceView *pShaderResourceView;
-    DXVIEW *pView = &pDXContext->pBackendDXContext->paShaderResourceView[shaderResourceViewId];
-    Assert(pView->u.pView == NULL);
-
-    if (pSurface->pBackendSurface == NULL)
-    {
-        /* Create the actual texture or buffer. */
-        /** @todo One function to create all resources from surfaces. */
-        if (pSurface->format != SVGA3D_BUFFER)
-            rc = vmsvga3dBackSurfaceCreateTexture(pThisCC, pDXContext, pSurface);
-        else
-            rc = vmsvga3dBackSurfaceCreateResource(pThisCC, pDXContext, pSurface);
-
-        AssertRCReturn(rc, rc);
-    }
-
-    HRESULT hr = dxShaderResourceViewCreate(pThisCC, pDXContext, pEntry, pSurface, &pShaderResourceView);
-    AssertReturn(SUCCEEDED(hr), VERR_INVALID_STATE);
-
-    return dxViewInit(pView, pSurface, pDXContext, shaderResourceViewId, VMSVGA3D_VIEWTYPE_SHADERRESOURCE, pShaderResourceView);
-}
-
-
-static DECLCALLBACK(int) vmsvga3dBackDXDefineShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId, SVGACOTableDXSRViewEntry const *pEntry)
-{
-    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
-    RT_NOREF(pBackend);
-
-    DXDEVICE *pDevice = dxDeviceFromContext(pThisCC->svga.p3dState, pDXContext);
-    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
-
-    /** @todo Probably not necessary because SRVs are defined in setupPipeline. */
-    return dxDefineShaderResourceView(pThisCC, pDXContext, shaderResourceViewId, pEntry);
-}
-
-
-static DECLCALLBACK(int) vmsvga3dBackDXDestroyShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId)
-{
-    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
-    RT_NOREF(pBackend);
-
-    return dxViewDestroy(&pDXContext->pBackendDXContext->paShaderResourceView[shaderResourceViewId]);
+    DXVIEW *pDXView = &pDXContext->pBackendDXContext->paShaderResourceView[shaderResourceViewId];
+    return dxViewDestroy(pDXView);
 }
 
@@ -9463,4 +9438,5 @@
                 for (uint32_t i = 0; i < pBackendDXContext->cShaderResourceView; ++i)
                 {
+                    /* Destroy the no longer used entries. */
                     DXVIEW *pDXView = &pBackendDXContext->paShaderResourceView[i];
                     if (i < cValidEntries)
@@ -9481,11 +9457,8 @@
                     continue; /* Skip uninitialized entry. */
 
-                /* Define views which were not defined yet in backend. */
                 DXVIEW *pDXView = &pBackendDXContext->paShaderResourceView[i];
                 /** @todo Verify that the pEntry content still corresponds to the view. */
                 if (pDXView->u.pView)
                     dxViewAddToList(pThisCC, pDXView);
-                else if (pDXView->enmViewType == VMSVGA3D_VIEWTYPE_NONE)
-                    dxDefineShaderResourceView(pThisCC, pDXContext, i, pEntry);
             }
             break;
