Index: /trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win.cpp
===================================================================
--- /trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win.cpp	(revision 79680)
+++ /trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win.cpp	(revision 79681)
@@ -1242,12 +1242,29 @@
                                                       &pSurface->hSharedObject);
         else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE)
-            hr = pContext->pDevice->CreateTexture(cWidth,
-                                                  cHeight,
-                                                  numMipLevels,
-                                                  pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
-                                                  pSurface->formatD3D,
-                                                  D3DPOOL_DEFAULT,
-                                                  &pSharedSurface->u.pTexture,
-                                                  &pSurface->hSharedObject);
+        {
+            if (pSurface->fStencilAsTexture)
+            {
+                /* Use the INTZ format for a depth/stencil surface that will be used as a texture */
+                hr = pContext->pDevice->CreateTexture(cWidth,
+                                                      cHeight,
+                                                      1, /* mip levels */
+                                                      D3DUSAGE_DEPTHSTENCIL,
+                                                      FOURCC_INTZ,
+                                                      D3DPOOL_DEFAULT,
+                                                      &pSharedSurface->u.pTexture,
+                                                      &pSurface->hSharedObject);
+            }
+            else
+            {
+                hr = pContext->pDevice->CreateTexture(cWidth,
+                                                      cHeight,
+                                                      numMipLevels,
+                                                      pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
+                                                      pSurface->formatD3D,
+                                                      D3DPOOL_DEFAULT,
+                                                      &pSharedSurface->u.pTexture,
+                                                      &pSurface->hSharedObject);
+            }
+        }
         else
             hr = E_FAIL;
@@ -1891,4 +1908,21 @@
                                               &pSurface->u.pTexture,
                                               &pSurface->hSharedObject /* might result in poor performance */);
+        if (   hr == D3D_OK
+            && (   pSurface->formatD3D == D3DFMT_D24S8
+                || pSurface->formatD3D == D3DFMT_D24X8))
+        {
+            /* Create another texture object to serve as a bounce buffer as the
+             * D3DFMT_D24S8 and D3DFMT_D24X8 surface can't be locked apparently (from testing).
+             */
+            hr = pContext->pDevice->CreateTexture(cWidth,
+                                                  cHeight,
+                                                  1, /* mip levels */
+                                                  D3DUSAGE_DYNAMIC /* Lockable */,
+                                                  FOURCC_INTZ,
+                                                  D3DPOOL_SYSTEMMEM,
+                                                  &pSurface->bounce.pTexture,
+                                                  NULL);
+            AssertMsgReturn(hr == D3D_OK, ("CreateTexture (systemmem) failed with %x\n", hr), VERR_INTERNAL_ERROR);
+        }
         AssertMsgReturn(hr == D3D_OK, ("CreateTexture INTZ failed with %x\n", hr), VERR_INTERNAL_ERROR);
 
@@ -4048,5 +4082,4 @@
 #endif
         }
-        Assert(pRenderTarget->idAssociatedContext == cid);
 
         /** @todo Assert(!pRenderTarget->fDirty); */
@@ -4061,6 +4094,6 @@
             IDirect3DSurface9 *pStencilSurface;
 
-            hr = pRenderTarget->u.pTexture->GetSurfaceLevel(0, &pStencilSurface);
-            AssertMsgReturn(hr == D3D_OK, ("GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
+            rc = vmsvga3dGetD3DSurface(pState, pContext, pRenderTarget, target.face, target.mipmap, /*fLockable=*/ false, &pStencilSurface);
+            AssertRCReturn(rc, rc);
 
             hr = pContext->pDevice->SetDepthStencilSurface(pStencilSurface);
@@ -4070,4 +4103,5 @@
         else
         {
+            Assert(pRenderTarget->idAssociatedContext == cid);
             hr = pContext->pDevice->SetDepthStencilSurface(pRenderTarget->u.pSurface);
             AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
