VirtualBox

Ignore:
Timestamp:
Oct 20, 2017 7:13:09 AM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
118445
Message:

Devices/Graphics: VMSVGA support for cubemaps and compressed textures (incomplete, only D3D backend); vertex/index buffer fixes; saved state fixes; cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win.cpp

    r67972 r69136  
    55
    66/*
    7  * Copyright (C) 2013-2016 Oracle Corporation
     7 * Copyright (C) 2013-2017 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    161161static void vmsvgaDumpD3DCaps(D3DCAPS9 *pCaps);
    162162
     163
     164#define D3D_RELEASE(ptr) do { \
     165    if (ptr)                  \
     166    {                         \
     167        (ptr)->Release();     \
     168        (ptr) = 0;            \
     169    }                         \
     170} while (0)
    163171
    164172
     
    318326    RTSemEventDestroy(pState->WndRequestSem);
    319327
    320     if (pState->pD3D9)
    321         pState->pD3D9->Release();
     328    D3D_RELEASE(pState->pD3D9);
    322329
    323330    return VINF_SUCCESS;
     
    10941101{
    10951102    RT_NOREF(pState);
     1103
    10961104    RTAvlU32Destroy(&pSurface->pSharedObjectTree, vmsvga3dSharedSurfaceDestroyTree, pSurface);
    10971105    Assert(pSurface->pSharedObjectTree == NULL);
     
    11001108    {
    11011109    case SVGA3D_SURFACE_CUBEMAP:
    1102         AssertFailed(); /** @todo */
     1110    case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE:
     1111    case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
     1112        D3D_RELEASE(pSurface->u.pCubeTexture);
     1113        D3D_RELEASE(pSurface->bounce.pCubeTexture);
    11031114        break;
    11041115
     
    11071118    case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
    11081119        if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_VERTEXBUFFER)
    1109         {
    1110             if (pSurface->u.pVertexBuffer)
    1111                 pSurface->u.pVertexBuffer->Release();
    1112         }
     1120            D3D_RELEASE(pSurface->u.pVertexBuffer);
    11131121        else if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_INDEXBUFFER)
    1114         {
    1115             if (pSurface->u.pIndexBuffer)
    1116                 pSurface->u.pIndexBuffer->Release();
    1117         }
     1122            D3D_RELEASE(pSurface->u.pIndexBuffer);
    11181123        else
    11191124            AssertMsg(pSurface->u.pVertexBuffer == NULL, ("fu32ActualUsageFlags %x\n", pSurface->fu32ActualUsageFlags));
     
    11221127    case SVGA3D_SURFACE_HINT_TEXTURE:
    11231128    case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
    1124         if (pSurface->u.pTexture)
    1125             pSurface->u.pTexture->Release();
    1126         if (pSurface->bounce.pTexture)
    1127             pSurface->bounce.pTexture->Release();
     1129        D3D_RELEASE(pSurface->u.pTexture);
     1130        D3D_RELEASE(pSurface->bounce.pTexture);
    11281131        break;
    11291132
     
    11321135    case SVGA3D_SURFACE_HINT_DEPTHSTENCIL | SVGA3D_SURFACE_HINT_TEXTURE:    /** @todo actual texture surface not supported */
    11331136        if (pSurface->fStencilAsTexture)
    1134             pSurface->u.pTexture->Release();
     1137            D3D_RELEASE(pSurface->u.pTexture);
    11351138        else
    1136         if (pSurface->u.pSurface)
    1137             pSurface->u.pSurface->Release();
     1139            D3D_RELEASE(pSurface->u.pSurface);
    11381140        break;
    11391141
     
    11431145    }
    11441146
    1145     if (pSurface->pQuery)
    1146         pSurface->pQuery->Release();
     1147    D3D_RELEASE(pSurface->pQuery);
    11471148}
    11481149
     
    11561157    PVMSVGA3DSURFACE       pSurface = (PVMSVGA3DSURFACE)pvParam;
    11571158
    1158     switch (pSurface->flags & (SVGA3D_SURFACE_HINT_INDEXBUFFER | SVGA3D_SURFACE_HINT_VERTEXBUFFER | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET | SVGA3D_SURFACE_HINT_DEPTHSTENCIL | SVGA3D_SURFACE_CUBEMAP))
    1159     {
     1159    switch (pSurface->flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)
     1160    {
     1161    case SVGA3D_SURFACE_CUBEMAP:
     1162    case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE:
     1163    case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
     1164        LogFunc(("release shared cube texture object for context %d\n", pNode->Key));
     1165        Assert(pSharedSurface->u.pCubeTexture);
     1166        D3D_RELEASE(pSharedSurface->u.pCubeTexture);
     1167        break;
     1168
    11601169    case SVGA3D_SURFACE_HINT_TEXTURE:
    11611170    case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
    1162         Log(("vmsvga3dSharedSurfaceDestroyTree: release shared object for context %d\n", pNode->Key));
     1171        LogFunc(("release shared texture object for context %d\n", pNode->Key));
    11631172        Assert(pSharedSurface->u.pTexture);
    1164         if (pSharedSurface->u.pTexture)
    1165             pSharedSurface->u.pTexture->Release();
     1173        D3D_RELEASE(pSharedSurface->u.pTexture);
    11661174        break;
    11671175
     
    11751183
    11761184/* Get the shared surface copy or create a new one. */
    1177 static PVMSVGA3DSHAREDSURFACE vmsvga3dSurfaceGetSharedCopy(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface)
     1185static PVMSVGA3DSHAREDSURFACE vmsvga3dSurfaceGetSharedCopy(PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface)
    11781186{
    1179     RT_NOREF(pThis);
    11801187    Assert(pSurface->hSharedObject);
    11811188
     
    11831190    if (!pSharedSurface)
    11841191    {
    1185         HRESULT hr;
    1186         bool    ret;
    1187 
    1188         Log(("vmsvga3dSurfaceGetSharedCopy: Create shared texture copy d3d (%d,%d) cMip=%d usage %x format %x.\n",
    1189               pSurface->pMipmapLevels[0].size.width,
    1190               pSurface->pMipmapLevels[0].size.height,
    1191               pSurface->faces[0],
    1192               pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
    1193               pSurface->formatD3D));
     1192        const bool fCubeTexture = RT_BOOL(pSurface->flags & SVGA3D_SURFACE_CUBEMAP);
     1193        const uint32_t cWidth = pSurface->pMipmapLevels[0].mipmapSize.width;
     1194        const uint32_t cHeight = pSurface->pMipmapLevels[0].mipmapSize.height;
     1195        const uint32_t numMipLevels = pSurface->faces[0].numMipLevels;
     1196
     1197        LogFunc(("Create shared %stexture copy d3d (%d,%d) cMip=%d usage %x format %x.\n",
     1198                  fCubeTexture ? "cube " : "",
     1199                  cWidth,
     1200                  cHeight,
     1201                  numMipLevels,
     1202                  pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
     1203                  pSurface->formatD3D));
    11941204
    11951205        pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTMemAllocZ(sizeof(*pSharedSurface));
     
    11971207
    11981208        pSharedSurface->Core.Key = pContext->id;
    1199         ret = RTAvlU32Insert(&pSurface->pSharedObjectTree, &pSharedSurface->Core);
     1209        bool ret = RTAvlU32Insert(&pSurface->pSharedObjectTree, &pSharedSurface->Core);
    12001210        AssertReturn(ret, NULL);
    12011211
    12021212        /* Create shadow copy of the original shared texture. Shared d3d resources require Vista+ and have some restrictions. */
    1203         hr = pContext->pDevice->CreateTexture(pSurface->pMipmapLevels[0].size.width,
    1204                                               pSurface->pMipmapLevels[0].size.height,
    1205                                               pSurface->faces[0].numMipLevels,
    1206                                               pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET /* required for use as a StretchRect destination */,
    1207                                               pSurface->formatD3D,
    1208                                               D3DPOOL_DEFAULT,
    1209                                               &pSharedSurface->u.pTexture,
    1210                                               &pSurface->hSharedObject);
    1211         AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceGetSharedCopy: CreateTexture failed with %x\n", hr), NULL);
     1213        HRESULT hr;
     1214        if (fCubeTexture)
     1215            hr = pContext->pDevice->CreateCubeTexture(cWidth,
     1216                                                      numMipLevels,
     1217                                                      pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET /* required for use as a StretchRect destination */,
     1218                                                      pSurface->formatD3D,
     1219                                                      D3DPOOL_DEFAULT,
     1220                                                      &pSharedSurface->u.pCubeTexture,
     1221                                                      &pSurface->hSharedObject);
     1222        else
     1223            hr = pContext->pDevice->CreateTexture(cWidth,
     1224                                                  cHeight,
     1225                                                  numMipLevels,
     1226                                                  pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET /* required for use as a StretchRect destination */,
     1227                                                  pSurface->formatD3D,
     1228                                                  D3DPOOL_DEFAULT,
     1229                                                  &pSharedSurface->u.pTexture,
     1230                                                  &pSurface->hSharedObject);
     1231        AssertMsgReturn(hr == D3D_OK, ("CreateTexture failed with %x\n", hr), NULL);
    12121232    }
    12131233    return pSharedSurface;
     
    12271247        return VINF_SUCCESS;
    12281248
    1229     Log(("vmsvga3dSurfaceTrackUsage: track usage of sid=%x (cid=%d) for cid=%d\n", pSurface->id, pSurface->idAssociatedContext, pContext->id));
     1249    LogFunc(("track usage of sid=%x (cid=%d) for cid=%d, pQuery %p\n", pSurface->id, pSurface->idAssociatedContext, pContext->id, pSurface->pQuery));
    12301250
    12311251    /* Release the previous query object. */
    1232     if (pSurface->pQuery)
    1233     {
    1234         Log(("vmsvga3dSurfaceTrackUsage: release old query object\n"));
    1235         pSurface->pQuery->Release();
    1236         pSurface->pQuery = NULL;
    1237     }
     1252    D3D_RELEASE(pSurface->pQuery);
     1253
    12381254    HRESULT hr = pContext->pDevice->CreateQuery(D3DQUERYTYPE_EVENT, &pSurface->pQuery);
    12391255    AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceTrackUsage: CreateQuery failed with %x\n", hr), VERR_INTERNAL_ERROR);
     
    12901306    AssertMsgReturn(hr == S_OK, ("vmsvga3dSurfaceFinishDrawing: GetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
    12911307
    1292     pSurface->pQuery->Release();
    1293     pSurface->pQuery = NULL;
     1308    D3D_RELEASE(pSurface->pQuery);
    12941309#endif /* !VBOX_VMSVGA3D_WITH_WINE_OPENGL */
     1310
     1311    return VINF_SUCCESS;
     1312}
     1313
     1314DECLINLINE(D3DCUBEMAP_FACES) vmsvga3dCubemapFaceFromIndex(uint32_t iFace)
     1315{
     1316    D3DCUBEMAP_FACES Face;
     1317    switch (iFace)
     1318    {
     1319        case 0: Face = D3DCUBEMAP_FACE_POSITIVE_X; break;
     1320        case 1: Face = D3DCUBEMAP_FACE_NEGATIVE_X; break;
     1321        case 2: Face = D3DCUBEMAP_FACE_POSITIVE_Y; break;
     1322        case 3: Face = D3DCUBEMAP_FACE_NEGATIVE_Y; break;
     1323        case 4: Face = D3DCUBEMAP_FACE_POSITIVE_Z; break;
     1324        default:
     1325        case 5: Face = D3DCUBEMAP_FACE_NEGATIVE_Z; break;
     1326    }
     1327    return Face;
     1328}
     1329
     1330/** Get IDirect3DSurface9 for the given face and mipmap.
     1331 */
     1332int vmsvga3dGetD3DSurface(PVMSVGA3DCONTEXT pContext,
     1333                          PVMSVGA3DSURFACE pSurface,
     1334                          uint32_t face,
     1335                          uint32_t mipmap,
     1336                          bool fLockable,
     1337                          IDirect3DSurface9 **ppD3DSurf)
     1338{
     1339    AssertPtrReturn(pSurface->u.pSurface, VERR_INVALID_PARAMETER);
     1340
     1341    const bool fTexture     = RT_BOOL(pSurface->flags & SVGA3D_SURFACE_HINT_TEXTURE);
     1342    const bool fCubeTexture = RT_BOOL(pSurface->flags & SVGA3D_SURFACE_CUBEMAP);
     1343
     1344    IDirect3DBaseTexture9 *pTexture;
     1345    if (fLockable && pSurface->bounce.pTexture)
     1346       pTexture = pSurface->bounce.pTexture;
     1347    else
     1348       pTexture = pSurface->u.pTexture;
     1349
     1350#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
     1351    if (pSurface->idAssociatedContext != pContext->id)
     1352    {
     1353        AssertMsgReturn(!fLockable,
     1354                        ("Lockable surface must be from the same context (surface cid = %d, req cid = %d)",
     1355                         pSurface->idAssociatedContext, pContext->id),
     1356                        VERR_INVALID_PARAMETER);
     1357
     1358        if (fTexture)
     1359        {
     1360            LogFunc(("using texture sid=%x created for another context (%d vs %d)\n",
     1361                     pSurface->id, pSurface->idAssociatedContext, pContext->id));
     1362
     1363            PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pContext, pSurface);
     1364            AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
     1365
     1366            pTexture = pSharedSurface->u.pTexture;
     1367        }
     1368        else
     1369        {
     1370            AssertMsgFailed(("surface sid=%x created for another context (%d vs %d)\n",
     1371                            pSurface->id, pSurface->idAssociatedContext, pContext->id));
     1372        }
     1373    }
     1374#else
     1375    RT_NOREF(pContext);
     1376#endif
     1377
     1378    if (fCubeTexture)
     1379    {
     1380        Assert(pSurface->cFaces == 6);
     1381
     1382        IDirect3DCubeTexture9 *p = (IDirect3DCubeTexture9 *)pTexture;
     1383        D3DCUBEMAP_FACES FaceType = vmsvga3dCubemapFaceFromIndex(face);
     1384        HRESULT hr = p->GetCubeMapSurface(FaceType, mipmap, ppD3DSurf);
     1385        AssertMsgReturn(hr == D3D_OK, ("GetCubeMapSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
     1386    }
     1387    else if (fTexture)
     1388    {
     1389        Assert(pSurface->cFaces == 1);
     1390        Assert(face == 0);
     1391
     1392        IDirect3DTexture9 *p = (IDirect3DTexture9 *)pTexture;
     1393        HRESULT hr = p->GetSurfaceLevel(mipmap, ppD3DSurf);
     1394        AssertMsgReturn(hr == D3D_OK, ("GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
     1395    }
     1396    else
     1397    {
     1398        pSurface->u.pSurface->AddRef();
     1399        *ppD3DSurf = pSurface->u.pSurface;
     1400    }
    12951401
    12961402    return VINF_SUCCESS;
     
    13001406                        uint32_t cCopyBoxes, SVGA3dCopyBox *pBox)
    13011407{
    1302     PVMSVGA3DSTATE      pState = pThis->svga.p3dState;
    1303     PVMSVGA3DSURFACE    pSurfaceSrc;
    1304     uint32_t            sidSrc = src.sid;
    1305     PVMSVGA3DSURFACE    pSurfaceDest;
    1306     uint32_t            sidDest = dest.sid;
    1307     int                 rc = VINF_SUCCESS;
    1308 
     1408    PVMSVGA3DSTATE pState = pThis->svga.p3dState;
    13091409    AssertReturn(pState, VERR_NO_MEMORY);
    1310     AssertReturn(sidSrc < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
    1311     AssertReturn(sidSrc < pState->cSurfaces && pState->papSurfaces[sidSrc]->id == sidSrc, VERR_INVALID_PARAMETER);
    1312     AssertReturn(sidDest < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
    1313     AssertReturn(sidDest < pState->cSurfaces && pState->papSurfaces[sidDest]->id == sidDest, VERR_INVALID_PARAMETER);
    1314 
    1315     pSurfaceSrc  = pState->papSurfaces[sidSrc];
    1316     pSurfaceDest = pState->papSurfaces[sidDest];
    1317 
    1318     AssertReturn(pSurfaceSrc->faces[0].numMipLevels > src.mipmap, VERR_INVALID_PARAMETER);
    1319     AssertReturn(pSurfaceDest->faces[0].numMipLevels > dest.mipmap, VERR_INVALID_PARAMETER);
    1320 
    1321     const VMSVGA3DMIPMAPLEVEL *pMipmapLevelSrc = &pSurfaceSrc->pMipmapLevels[src.mipmap];
    1322     const VMSVGA3DMIPMAPLEVEL *pMipmapLevelDest = &pSurfaceDest->pMipmapLevels[dest.mipmap];
    1323 
    1324    // AssertMsgReturn(pSurfaceSrc->format == pSurfaceDest->format, ("Format mismatch (%d vs %d)!!\n", pSurfaceSrc->format, pSurfaceDest->format), VERR_INVALID_PARAMETER);
    1325 
    1326     /** @todo Support cubemaps. */
    1327     bool fSrcTexture  = !!(pSurfaceSrc->flags & SVGA3D_SURFACE_HINT_TEXTURE);
    1328     bool fDestTexture = !!(pSurfaceDest->flags & SVGA3D_SURFACE_HINT_TEXTURE);
     1410
     1411    const uint32_t sidSrc = src.sid;
     1412    const uint32_t sidDest = dest.sid;
     1413    int rc;
     1414
     1415    PVMSVGA3DSURFACE pSurfaceSrc;
     1416    rc = vmsvga3dSurfaceFromSid(pState, sidSrc, &pSurfaceSrc);
     1417    AssertRCReturn(rc, rc);
     1418
     1419    PVMSVGA3DSURFACE pSurfaceDest;
     1420    rc = vmsvga3dSurfaceFromSid(pState, sidDest, &pSurfaceDest);
     1421    AssertRCReturn(rc, rc);
     1422
     1423    PVMSVGA3DMIPMAPLEVEL pMipmapLevelSrc;
     1424    rc = vmsvga3dMipmapLevel(pSurfaceSrc, src.face, src.mipmap, &pMipmapLevelSrc);
     1425    AssertRCReturn(rc, rc);
     1426
     1427    PVMSVGA3DMIPMAPLEVEL pMipmapLevelDest;
     1428    rc = vmsvga3dMipmapLevel(pSurfaceDest, dest.face, dest.mipmap, &pMipmapLevelDest);
     1429    AssertRCReturn(rc, rc);
     1430
     1431    const bool fDestTexture = RT_BOOL(pSurfaceDest->flags & SVGA3D_SURFACE_HINT_TEXTURE);
    13291432
    13301433    if (    fDestTexture
     
    13321435        &&  pSurfaceSrc->u.pSurface)
    13331436    {
    1334         uint32_t            cid;
    1335         PVMSVGA3DCONTEXT    pContext;
    1336 
    1337         /** @todo stricter checks for associated context */
    1338         cid = pSurfaceSrc->idAssociatedContext;
    1339         if (    cid >= pState->cContexts
    1340             ||  pState->papContexts[cid]->id != cid)
    1341         {
    1342             Log(("vmsvga3dSurfaceCopy invalid context id!\n"));
    1343             return VERR_INVALID_PARAMETER;
    1344         }
    1345         pContext = pState->papContexts[cid];
    1346 
    1347         Log(("vmsvga3dSurfaceCopy: create texture sid=%x type=%d format=%d -> create texture\n", sidDest, pSurfaceDest->flags, pSurfaceDest->format));
     1437        const uint32_t cid = pSurfaceSrc->idAssociatedContext;
     1438
     1439        PVMSVGA3DCONTEXT pContext;
     1440        rc = vmsvga3dContextFromCid(pState, cid, &pContext);
     1441        AssertRCReturn(rc, rc);
     1442
     1443        LogFunc(("sid=%x type=%x format=%d -> create texture\n", sidDest, pSurfaceDest->flags, pSurfaceDest->format));
    13481444        rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurfaceDest);
    13491445        AssertRCReturn(rc, rc);
    13501446    }
    13511447
    1352     if (    pSurfaceSrc->u.pSurface
    1353         &&  pSurfaceDest->u.pSurface)
    1354     {
    1355         uint32_t                cid;
    1356         PVMSVGA3DCONTEXT        pContext;
    1357         IDirect3DTexture9      *pSrcTexture = pSurfaceSrc->u.pTexture;
    1358 
    1359         /** @todo stricter checks for associated context */
    1360         cid = pSurfaceDest->idAssociatedContext;
    1361         if (    cid >= pState->cContexts
    1362             ||  pState->papContexts[cid]->id != cid)
    1363         {
    1364             Log(("vmsvga3dSurfaceCopy invalid context id!\n"));
    1365             return VERR_INVALID_PARAMETER;
    1366         }
    1367         pContext = pState->papContexts[cid];
     1448    if (   pSurfaceSrc->u.pSurface
     1449        && pSurfaceDest->u.pSurface)
     1450    {
     1451        const uint32_t cidDst = pSurfaceDest->idAssociatedContext;
     1452
     1453        PVMSVGA3DCONTEXT pContextDst;
     1454        rc = vmsvga3dContextFromCid(pState, cidDst, &pContextDst);
     1455        AssertRCReturn(rc, rc);
    13681456
    13691457        /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
     
    13711459        vmsvga3dSurfaceFlush(pThis, pSurfaceDest);
    13721460
    1373 #ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
    1374         if (    fSrcTexture
    1375             &&  pSurfaceSrc->idAssociatedContext != cid)
    1376         {
    1377             Log(("vmsvga3dSurfaceCopy; using texture sid=%x created for another context (%d vs %d)\n", sidSrc, pSurfaceSrc->idAssociatedContext, cid));
    1378 
    1379             PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pThis, pContext, pSurfaceSrc);
    1380             AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
    1381 
    1382             pSrcTexture = pSharedSurface->u.pTexture;
    1383         }
    1384 #endif
    1385 
    1386         for (uint32_t i = 0; i < cCopyBoxes; i++)
    1387         {
    1388             HRESULT     hr;
    1389             RECT        RectSrc;
    1390             RECT        RectDest;
    1391             IDirect3DSurface9 *pSrc;
    1392             IDirect3DSurface9 *pDest;
     1461        IDirect3DSurface9 *pSrc;
     1462        rc = vmsvga3dGetD3DSurface(pContextDst, pSurfaceSrc, src.face, src.mipmap, false, &pSrc);
     1463        AssertRCReturn(rc, rc);
     1464
     1465        IDirect3DSurface9 *pDest;
     1466        rc = vmsvga3dGetD3DSurface(pContextDst, pSurfaceDest, dest.face, dest.mipmap, false, &pDest);
     1467        AssertRCReturnStmt(rc, D3D_RELEASE(pSrc), rc);
     1468
     1469        for (uint32_t i = 0; i < cCopyBoxes; ++i)
     1470        {
     1471            HRESULT hr;
    13931472
    13941473            SVGA3dCopyBox clipBox = pBox[i];
    1395             vmsvgaClipCopyBox(&pMipmapLevelSrc->size, &pMipmapLevelDest->size, &clipBox);
     1474            vmsvgaClipCopyBox(&pMipmapLevelSrc->mipmapSize, &pMipmapLevelDest->mipmapSize, &clipBox);
    13961475            if (   !clipBox.w
    13971476                || !clipBox.h
     
    14021481            }
    14031482
     1483            RECT RectSrc;
    14041484            RectSrc.left    = clipBox.srcx;
    14051485            RectSrc.top     = clipBox.srcy;
    14061486            RectSrc.right   = clipBox.srcx + clipBox.w;   /* exclusive */
    14071487            RectSrc.bottom  = clipBox.srcy + clipBox.h;   /* exclusive */
     1488
     1489            RECT RectDest;
    14081490            RectDest.left   = clipBox.x;
    14091491            RectDest.top    = clipBox.y;
     
    14111493            RectDest.bottom = clipBox.y + clipBox.h;   /* exclusive */
    14121494
    1413             Log(("vmsvga3dSurfaceCopy: (StretchRect) copy src sid=%x face=%d mipmap=%d (%d,%d)(%d,%d) to dest sid=%x face=%d mipmap=%d (%d,%d)\n", sidSrc, src.face, src.mipmap, RectSrc.left, RectSrc.top, RectSrc.right, RectSrc.bottom, sidDest, dest.face, dest.mipmap, pBox[i].x, pBox[i].y));
     1495            LogFunc(("StretchRect copy src sid=%x face=%d mipmap=%d (%d,%d)(%d,%d) to dest sid=%x face=%d mipmap=%d (%d,%d)\n", sidSrc, src.face, src.mipmap, RectSrc.left, RectSrc.top, RectSrc.right, RectSrc.bottom, sidDest, dest.face, dest.mipmap, pBox[i].x, pBox[i].y));
    14141496
    14151497            if (    sidSrc == sidDest
     
    14171499                &&  clipBox.srcy == clipBox.y)
    14181500            {
    1419                 Log(("vmsvga3dSurfaceCopy: redundant copy to the same surface at the same coordinates. Ignore. \n"));
     1501                LogFunc(("redundant copy to the same surface at the same coordinates. Ignore.\n"));
    14201502                continue;
    14211503            }
     
    14231505            Assert(!clipBox.srcz && !clipBox.z);
    14241506
    1425             if (fSrcTexture)
     1507            hr = pContextDst->pDevice->StretchRect(pSrc, &RectSrc, pDest, &RectDest, D3DTEXF_NONE);
     1508            AssertMsgReturnStmt(hr == D3D_OK,
     1509                                ("StretchRect failed with %x\n", hr),
     1510                                D3D_RELEASE(pDest); D3D_RELEASE(pSrc),
     1511                                VERR_INTERNAL_ERROR);
     1512        }
     1513
     1514        D3D_RELEASE(pDest);
     1515        D3D_RELEASE(pSrc);
     1516
     1517        /* Track the StretchRect operation. */
     1518        vmsvga3dSurfaceTrackUsage(pState, pContextDst, pSurfaceSrc);
     1519        vmsvga3dSurfaceTrackUsage(pState, pContextDst, pSurfaceDest);
     1520    }
     1521    else
     1522    {
     1523        /*
     1524         * Copy from/to memory to/from a surface. Or mem->mem.
     1525         * Use the context of existing HW surface, if any.
     1526         */
     1527        PVMSVGA3DCONTEXT pContext = NULL;
     1528        IDirect3DSurface9 *pD3DSurf = NULL;
     1529
     1530        if (pSurfaceSrc->u.pSurface)
     1531        {
     1532            AssertReturn(!pSurfaceDest->u.pSurface, VERR_INTERNAL_ERROR);
     1533
     1534            rc = vmsvga3dContextFromCid(pState, pSurfaceSrc->idAssociatedContext, &pContext);
     1535            AssertRCReturn(rc, rc);
     1536
     1537            rc = vmsvga3dGetD3DSurface(pContext, pSurfaceSrc, src.face, src.mipmap, true, &pD3DSurf);
     1538            AssertRCReturn(rc, rc);
     1539        }
     1540        else if (pSurfaceDest->u.pSurface)
     1541        {
     1542            AssertReturn(!pSurfaceSrc->u.pSurface, VERR_INTERNAL_ERROR);
     1543
     1544            rc = vmsvga3dContextFromCid(pState, pSurfaceDest->idAssociatedContext, &pContext);
     1545            AssertRCReturn(rc, rc);
     1546
     1547            rc = vmsvga3dGetD3DSurface(pContext, pSurfaceDest, dest.face, dest.mipmap, true, &pD3DSurf);
     1548            AssertRCReturn(rc, rc);
     1549        }
     1550
     1551        for (uint32_t i = 0; i < cCopyBoxes; ++i)
     1552        {
     1553            HRESULT        hr;
     1554
     1555            SVGA3dCopyBox clipBox = pBox[i];
     1556            vmsvgaClipCopyBox(&pMipmapLevelSrc->mipmapSize, &pMipmapLevelDest->mipmapSize, &clipBox);
     1557            if (   !clipBox.w
     1558                || !clipBox.h
     1559                || !clipBox.d)
    14261560            {
    1427                 hr = pSrcTexture->GetSurfaceLevel(src.mipmap /* Texture level */,
    1428                                                   &pSrc);
    1429                 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceCopy: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
     1561                LogFunc(("Skipped empty box.\n"));
     1562                continue;
     1563            }
     1564
     1565            RECT RectSrc;
     1566            RectSrc.left    = clipBox.srcx;
     1567            RectSrc.top     = clipBox.srcy;
     1568            RectSrc.right   = clipBox.srcx + clipBox.w;   /* exclusive */
     1569            RectSrc.bottom  = clipBox.srcy + clipBox.h;   /* exclusive */
     1570
     1571            RECT RectDest;
     1572            RectDest.left   = clipBox.x;
     1573            RectDest.top    = clipBox.y;
     1574            RectDest.right  = clipBox.x + clipBox.w;   /* exclusive */
     1575            RectDest.bottom = clipBox.y + clipBox.h;   /* exclusive */
     1576
     1577            LogFunc(("(manual) copy sid=%x face=%d mipmap=%d (%d,%d)(%d,%d) to sid=%x face=%d mipmap=%d (%d,%d)\n",
     1578                     sidSrc, src.face, src.mipmap, RectSrc.left, RectSrc.top, RectSrc.right, RectSrc.bottom,
     1579                     sidDest, dest.face, dest.mipmap, pBox[i].x, pBox[i].y));
     1580
     1581            Assert(!clipBox.srcz && !clipBox.z);
     1582            Assert(pSurfaceSrc->cbBlock == pSurfaceDest->cbBlock);
     1583            Assert(pSurfaceSrc->cxBlock == pSurfaceDest->cxBlock);
     1584            Assert(pSurfaceSrc->cyBlock == pSurfaceDest->cyBlock);
     1585
     1586            uint32_t cBlocksX = (clipBox.w + pSurfaceSrc->cxBlock - 1) / pSurfaceSrc->cxBlock;
     1587            uint32_t cBlocksY = (clipBox.h + pSurfaceSrc->cyBlock - 1) / pSurfaceSrc->cyBlock;
     1588
     1589            D3DLOCKED_RECT LockedSrcRect;
     1590            if (!pSurfaceSrc->u.pSurface)
     1591            {
     1592                uint32_t u32BlockX = clipBox.srcx / pSurfaceSrc->cxBlock;
     1593                uint32_t u32BlockY = clipBox.srcy / pSurfaceSrc->cyBlock;
     1594                Assert(u32BlockX * pSurfaceSrc->cxBlock == clipBox.srcx);
     1595                Assert(u32BlockY * pSurfaceSrc->cyBlock == clipBox.srcy);
     1596
     1597                LockedSrcRect.pBits = (uint8_t *)pMipmapLevelSrc->pSurfaceData +
     1598                                      pMipmapLevelSrc->cbSurfacePitch * u32BlockY + pSurfaceSrc->cbBlock * u32BlockX;
     1599                LockedSrcRect.Pitch = pMipmapLevelSrc->cbSurfacePitch;
    14301600            }
    14311601            else
    1432                 pSrc = pSurfaceSrc->u.pSurface;
    1433 
    1434             if (fDestTexture)
    14351602            {
    1436                 hr = pSurfaceDest->u.pTexture->GetSurfaceLevel(dest.mipmap /* Texture level */,
    1437                                                               &pDest);
    1438                 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceCopy: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
     1603                /* Must flush the context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
     1604                vmsvga3dSurfaceFlush(pThis, pSurfaceSrc);
     1605
     1606                hr = pD3DSurf->LockRect(&LockedSrcRect, &RectSrc, D3DLOCK_READONLY);
     1607                AssertMsgReturnStmt(hr == D3D_OK, ("LockRect failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
     1608            }
     1609
     1610            D3DLOCKED_RECT LockedDestRect;
     1611            if (!pSurfaceDest->u.pSurface)
     1612            {
     1613                uint32_t u32BlockX = clipBox.x / pSurfaceDest->cxBlock;
     1614                uint32_t u32BlockY = clipBox.y / pSurfaceDest->cyBlock;
     1615                Assert(u32BlockX * pSurfaceDest->cxBlock == clipBox.x);
     1616                Assert(u32BlockY * pSurfaceDest->cyBlock == clipBox.y);
     1617
     1618                LockedDestRect.pBits = (uint8_t *)pMipmapLevelDest->pSurfaceData +
     1619                                       pMipmapLevelDest->cbSurfacePitch * u32BlockY + pSurfaceDest->cbBlock * u32BlockX;
     1620                LockedDestRect.Pitch = pMipmapLevelDest->cbSurfacePitch;
     1621                pSurfaceDest->fDirty = true;
    14391622            }
    14401623            else
    1441                 pDest = pSurfaceDest->u.pSurface;
    1442 
    1443             /* UpdateSurface is too picky; use StretchRect instead */
    1444             hr = pContext->pDevice->StretchRect(pSrc, &RectSrc, pDest, &RectDest, D3DTEXF_NONE);
    1445 
    1446             /* GetSurfaceLevel increases the reference count; release the objects again. */
    1447             if (fDestTexture)
    1448                 pDest->Release();
    1449             if (fSrcTexture)
    1450                 pSrc->Release();
    1451 
    1452             /* Track the StretchRect operation. */
    1453             vmsvga3dSurfaceTrackUsage(pState, pContext, pSurfaceSrc);
    1454             vmsvga3dSurfaceTrackUsage(pState, pContext, pSurfaceDest);
    1455 
    1456             AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceCopy: UpdateSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1457         }
    1458     }
    1459     else
    1460     for (uint32_t i = 0; i < cCopyBoxes; i++)
    1461     {
    1462         HRESULT        hr;
    1463         D3DLOCKED_RECT LockedSrcRect;
    1464         D3DLOCKED_RECT LockedDestRect;
    1465         RECT           RectSrc;
    1466         RECT           RectDest;
    1467 
    1468         SVGA3dCopyBox clipBox = pBox[i];
    1469         vmsvgaClipCopyBox(&pMipmapLevelSrc->size, &pMipmapLevelDest->size, &clipBox);
    1470         if (   !clipBox.w
    1471             || !clipBox.h
    1472             || !clipBox.d)
    1473         {
    1474             LogFunc(("Skipped empty box.\n"));
    1475             continue;
    1476         }
    1477 
    1478         RectSrc.left    = clipBox.srcx;
    1479         RectSrc.top     = clipBox.srcy;
    1480         RectSrc.right   = clipBox.srcx + clipBox.w;   /* exclusive */
    1481         RectSrc.bottom  = clipBox.srcy + clipBox.h;   /* exclusive */
    1482         RectDest.left   = clipBox.x;
    1483         RectDest.top    = clipBox.y;
    1484         RectDest.right  = clipBox.x + clipBox.w;   /* exclusive */
    1485         RectDest.bottom = clipBox.y + clipBox.h;   /* exclusive */
    1486 
    1487         LogFunc(("(manual) copy sid=%x face=%d mipmap=%d (%d,%d)(%d,%d) to sid=%x face=%d mipmap=%d (%d,%d)\n",
    1488                  sidSrc, src.face, src.mipmap, RectSrc.left, RectSrc.top, RectSrc.right, RectSrc.bottom,
    1489                  sidDest, dest.face, dest.mipmap, pBox[i].x, pBox[i].y));
    1490 
    1491         Assert(!clipBox.srcz && !clipBox.z);
    1492         Assert(pSurfaceSrc->cbBlock == pSurfaceDest->cbBlock);
    1493 
    1494         if (!pSurfaceSrc->u.pSurface)
    1495         {
    1496             LockedSrcRect.pBits = (uint8_t *)pMipmapLevelSrc->pSurfaceData +
    1497                                   pMipmapLevelSrc->cbSurfacePitch * clipBox.srcy + pSurfaceSrc->cbBlock * clipBox.srcx;
    1498             LockedSrcRect.Pitch = pMipmapLevelSrc->cbSurfacePitch;
    1499         }
    1500         else
    1501         {
    1502             /** @todo This branch is dead code. Because if the source is a hardware surface, then
    1503              *  the code above creates hardware surface for the destination and does the copy in hardware.
    1504              *  I.e. we can not get here with pSurfaceSrc->u.pSurface != NULL.
    1505              */
    1506 
    1507             /* Must flush the context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
    1508             vmsvga3dSurfaceFlush(pThis, pSurfaceSrc);
    1509 
    1510             if (fSrcTexture)
    15111624            {
    1512                 Assert(!pSurfaceSrc->bounce.pTexture);
    1513                 hr = pSurfaceSrc->u.pTexture->LockRect(src.mipmap, /* Texture level */
    1514                                                        &LockedSrcRect,
    1515                                                        &RectSrc,
    1516                                                        D3DLOCK_READONLY);
     1625                /* Must flush the context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
     1626                vmsvga3dSurfaceFlush(pThis, pSurfaceDest);
     1627
     1628                hr = pD3DSurf->LockRect(&LockedDestRect, &RectDest, 0);
     1629                AssertMsgReturnStmt(hr == D3D_OK, ("LockRect failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
     1630            }
     1631
     1632            uint8_t *pDest = (uint8_t *)LockedDestRect.pBits;
     1633            const uint8_t *pSrc = (uint8_t *)LockedSrcRect.pBits;
     1634            for (uint32_t j = 0; j < cBlocksY; ++j)
     1635            {
     1636                memcpy(pDest, pSrc, cBlocksX * pSurfaceSrc->cbBlock);
     1637                pDest += LockedDestRect.Pitch;
     1638                pSrc  += LockedSrcRect.Pitch;
     1639            }
     1640
     1641            if (pD3DSurf)
     1642            {
     1643                hr = pD3DSurf->UnlockRect();
     1644                AssertMsgReturn(hr == D3D_OK, ("Unlock failed with %x\n", hr), VERR_INTERNAL_ERROR);
     1645            }
     1646        }
     1647
     1648        /* If the destination bounce texture has been used, then update the actual texture. */
     1649        if (   pSurfaceDest->u.pSurface
     1650            && fDestTexture
     1651            && pSurfaceDest->bounce.pTexture)
     1652        {
     1653            AssertMsgReturn(pContext, ("Context is NULL\n"), VERR_INTERNAL_ERROR);
     1654
     1655            /* Copy the new content to the actual texture object. */
     1656            IDirect3DBaseTexture9 *pSourceTexture;
     1657            IDirect3DBaseTexture9 *pDestinationTexture;
     1658            if (pSurfaceDest->flags & SVGA3D_SURFACE_CUBEMAP)
     1659            {
     1660                pSourceTexture = pSurfaceDest->bounce.pCubeTexture;
     1661                pDestinationTexture = pSurfaceDest->u.pCubeTexture;
    15171662            }
    15181663            else
    1519                 hr = pSurfaceSrc->u.pSurface->LockRect(&LockedSrcRect,
    1520                                                        &RectSrc,
    1521                                                        D3DLOCK_READONLY);
    1522             AssertMsgReturn(hr == D3D_OK, ("LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1523         }
    1524 
    1525         if (!pSurfaceDest->u.pSurface)
    1526         {
    1527             LockedDestRect.pBits = (uint8_t *)pMipmapLevelDest->pSurfaceData +
    1528                                    pMipmapLevelDest->cbSurfacePitch * clipBox.y + pSurfaceDest->cbBlock * clipBox.x;
    1529             LockedDestRect.Pitch = pMipmapLevelDest->cbSurfacePitch;
    1530         }
    1531         else
    1532         {
    1533             /* Must flush the context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
    1534             vmsvga3dSurfaceFlush(pThis, pSurfaceDest);
    1535 
    1536             if (fDestTexture)
    15371664            {
    1538                 if (pSurfaceDest->bounce.pTexture)
    1539                 {
    1540                     /* pSurfaceDest->u.pTexture can't be locked, see vmsvga3dBackCreateTexture */
    1541                     hr = pSurfaceDest->bounce.pTexture->LockRect(dest.mipmap, /* texture level */
    1542                                                                  &LockedDestRect,
    1543                                                                  &RectDest,
    1544                                                                  0);
    1545                 }
    1546                 else
    1547                 {
    1548                     hr = pSurfaceDest->u.pTexture->LockRect(dest.mipmap, /* texture level */
    1549                                                             &LockedDestRect,
    1550                                                             &RectDest,
    1551                                                             0);
    1552                 }
     1665                pSourceTexture = pSurfaceDest->bounce.pTexture;
     1666                pDestinationTexture = pSurfaceDest->u.pTexture;
    15531667            }
    1554             else
    1555                 hr = pSurfaceDest->u.pSurface->LockRect(&LockedDestRect,
    1556                                                         &RectDest,
    1557                                                         0);
    1558             AssertMsgReturn(hr == D3D_OK, ("LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1559         }
    1560 
    1561         uint8_t *pDest = (uint8_t *)LockedDestRect.pBits;
    1562         const uint8_t *pSrc = (uint8_t *)LockedSrcRect.pBits;
    1563 
    1564         for (uint32_t j = 0; j < clipBox.h; ++j)
    1565         {
    1566             memcpy(pDest, pSrc, clipBox.w * pSurfaceSrc->cbBlock);
    1567 
    1568             pDest += LockedDestRect.Pitch;
    1569             pSrc  += LockedSrcRect.Pitch;
    1570         }
    1571 
    1572         if (pSurfaceDest->u.pSurface)
    1573         {
    1574             if (fDestTexture)
    1575             {
    1576                 if (pSurfaceDest->bounce.pTexture)
    1577                 {
    1578                     hr = pSurfaceDest->bounce.pTexture->UnlockRect(dest.mipmap /* Texture level */);
    1579 
    1580                     /** @todo stricter checks for associated context */
    1581                     PVMSVGA3DCONTEXT pContext = NULL;
    1582                     const uint32_t cid = pSurfaceDest->idAssociatedContext;
    1583                     if (   cid < pState->cContexts
    1584                         && pState->papContexts[cid]->id == cid)
    1585                         pContext = pState->papContexts[cid];
    1586                     else
    1587                         AssertFailed();
    1588 
    1589                     if (pContext)
    1590                     {
    1591                         /* Copy the new content to the actual texture object. */
    1592                         HRESULT hr2 = pContext->pDevice->UpdateTexture(pSurfaceDest->bounce.pTexture, pSurfaceDest->u.pTexture);
    1593                         AssertMsg(hr2 == D3D_OK, ("UpdateTexture failed with %x\n", hr2)); RT_NOREF(hr2);
    1594                     }
    1595                 }
    1596                 else
    1597                     hr = pSurfaceDest->u.pTexture->UnlockRect(dest.mipmap /* Texture level */);
    1598             }
    1599             else
    1600                 hr = pSurfaceDest->u.pSurface->UnlockRect();
    1601             AssertMsgReturn(hr == D3D_OK, ("Unlock failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1602         }
    1603 
    1604         if (pSurfaceSrc->u.pSurface)
    1605         {
    1606             if (fSrcTexture)
    1607                 hr = pSurfaceSrc->u.pTexture->UnlockRect(src.mipmap /* Texture level */);
    1608             else
    1609                 hr = pSurfaceSrc->u.pSurface->UnlockRect();
    1610             AssertMsgReturn(hr == D3D_OK, ("Unlock failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1611         }
     1668            HRESULT hr2 = pContext->pDevice->UpdateTexture(pSourceTexture, pDestinationTexture);
     1669            AssertMsg(hr2 == D3D_OK, ("UpdateTexture failed with %x\n", hr2)); RT_NOREF(hr2);
     1670        }
     1671
     1672        D3D_RELEASE(pD3DSurf);
    16121673    }
    16131674
     
    16321693    RT_NOREF(pState);
    16331694    HRESULT hr;
    1634     IDirect3DTexture9 *pTexture;
    16351695
    16361696    Assert(pSurface->hSharedObject == NULL);
    1637 
    1638     if (    pSurface->formatD3D == D3DFMT_D24S8
    1639         ||  pSurface->formatD3D == D3DFMT_D24X8)
    1640     {
     1697    Assert(pSurface->u.pTexture == NULL);
     1698    Assert(pSurface->bounce.pTexture == NULL);
     1699
     1700    const uint32_t cWidth = pSurface->pMipmapLevels[0].mipmapSize.width;
     1701    const uint32_t cHeight = pSurface->pMipmapLevels[0].mipmapSize.height;
     1702    const uint32_t numMipLevels = pSurface->faces[0].numMipLevels;
     1703
     1704    /*
     1705     * Create D3D texture object.
     1706     */
     1707    if (pSurface->flags & SVGA3D_SURFACE_CUBEMAP)
     1708    {
     1709        Assert(pSurface->cFaces == 6);
     1710        Assert(cWidth == cHeight);
     1711
     1712        hr = pContext->pDevice->CreateCubeTexture(cWidth,
     1713                                                  numMipLevels,
     1714                                                  pSurface->fUsageD3D,
     1715                                                  pSurface->formatD3D,
     1716                                                  D3DPOOL_DEFAULT,
     1717                                                  &pSurface->u.pCubeTexture,
     1718                                                  &pSurface->hSharedObject);
     1719        if (hr == D3D_OK)
     1720        {
     1721            /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
     1722            hr = pContext->pDevice->CreateCubeTexture(cWidth,
     1723                                                      numMipLevels,
     1724                                                      (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
     1725                                                      pSurface->formatD3D,
     1726                                                      D3DPOOL_SYSTEMMEM,
     1727                                                      &pSurface->bounce.pCubeTexture,
     1728                                                      NULL);
     1729            AssertMsgReturnStmt(hr == D3D_OK,
     1730                                ("CreateCubeTexture (systemmem) failed with %x\n", hr),
     1731                                D3D_RELEASE(pSurface->u.pCubeTexture),
     1732                                VERR_INTERNAL_ERROR);
     1733        }
     1734        else
     1735        {
     1736            Log(("Format not accepted -> try old method\n"));
     1737            /* The format was probably not accepted; fall back to our old mode. */
     1738            hr = pContext->pDevice->CreateCubeTexture(cWidth,
     1739                                                      numMipLevels,
     1740                                                      (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) |  D3DUSAGE_DYNAMIC /* Lockable */,
     1741                                                      pSurface->formatD3D,
     1742                                                      D3DPOOL_DEFAULT,
     1743                                                      &pSurface->u.pCubeTexture,
     1744                                                      &pSurface->hSharedObject);
     1745            AssertMsgReturn(hr == D3D_OK, ("CreateCubeTexture (fallback) failed with %x\n", hr), VERR_INTERNAL_ERROR);
     1746        }
     1747    }
     1748    else if (   pSurface->formatD3D == D3DFMT_D24S8
     1749             || pSurface->formatD3D == D3DFMT_D24X8)
     1750    {
     1751        Assert(pSurface->cFaces == 1);
    16411752        Assert(pSurface->faces[0].numMipLevels == 1);
     1753
    16421754        /* Use the INTZ format for a depth/stencil surface that will be used as a texture */
    1643         hr = pContext->pDevice->CreateTexture(pSurface->pMipmapLevels[0].size.width,
    1644                                               pSurface->pMipmapLevels[0].size.height,
     1755        hr = pContext->pDevice->CreateTexture(cWidth,
     1756                                              cHeight,
    16451757                                              1,
    16461758                                              D3DUSAGE_DEPTHSTENCIL,
     
    16491761                                              &pSurface->u.pTexture,
    16501762                                              &pSurface->hSharedObject /* might result in poor performance */);
    1651         AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: CreateTexture INTZ failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1652         pTexture = pSurface->u.pTexture;
     1763        AssertMsgReturn(hr == D3D_OK, ("CreateTexture INTZ failed with %x\n", hr), VERR_INTERNAL_ERROR);
    16531764
    16541765        pSurface->fStencilAsTexture = true;
     
    16561767    else
    16571768    {
    1658         hr = pContext->pDevice->CreateTexture(pSurface->pMipmapLevels[0].size.width,
    1659                                               pSurface->pMipmapLevels[0].size.height,
    1660                                               pSurface->faces[0].numMipLevels,
     1769        /** @todo if (depth > 1) CreateVolumeTexture */
     1770        Assert(pSurface->cFaces == 1);
     1771
     1772        hr = pContext->pDevice->CreateTexture(cWidth,
     1773                                              cHeight,
     1774                                              numMipLevels,
    16611775                                              pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET /* required for use as a StretchRect destination */,
    16621776                                              pSurface->formatD3D,
     
    16671781        {
    16681782            /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
    1669             hr = pContext->pDevice->CreateTexture(pSurface->pMipmapLevels[0].size.width,
    1670                                                   pSurface->pMipmapLevels[0].size.height,
    1671                                                   pSurface->faces[0].numMipLevels,
     1783            hr = pContext->pDevice->CreateTexture(cWidth,
     1784                                                  cHeight,
     1785                                                  numMipLevels,
    16721786                                                  (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
    16731787                                                  pSurface->formatD3D,
     
    16751789                                                  &pSurface->bounce.pTexture,
    16761790                                                  NULL);
    1677             AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: CreateTexture (systemmem) failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1678             pTexture = pSurface->bounce.pTexture;
     1791            AssertMsgReturn(hr == D3D_OK, ("CreateTexture (systemmem) failed with %x\n", hr), VERR_INTERNAL_ERROR);
    16791792        }
    16801793        else
     
    16821795            Log(("Format not accepted -> try old method\n"));
    16831796            /* The format was probably not accepted; fall back to our old mode. */
    1684             hr = pContext->pDevice->CreateTexture(pSurface->pMipmapLevels[0].size.width,
    1685                                                   pSurface->pMipmapLevels[0].size.height,
    1686                                                   pSurface->faces[0].numMipLevels,
     1797            hr = pContext->pDevice->CreateTexture(cWidth,
     1798                                                  cHeight,
     1799                                                  numMipLevels,
    16871800                                                  (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) |  D3DUSAGE_DYNAMIC /* Lockable */,
    16881801                                                  pSurface->formatD3D,
     
    16901803                                                  &pSurface->u.pTexture,
    16911804                                                  &pSurface->hSharedObject /* might result in poor performance */);
    1692             AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: CreateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1693             pTexture = pSurface->u.pTexture;
     1805            AssertMsgReturn(hr == D3D_OK, ("CreateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
    16941806        }
    16951807    }
     
    16981810    {
    16991811        /* Set the mip map generation filter settings. */
    1700         hr = pSurface->u.pTexture->SetAutoGenFilterType((D3DTEXTUREFILTERTYPE)pSurface->autogenFilter);
     1812        if (pSurface->flags & SVGA3D_SURFACE_CUBEMAP)
     1813            hr = pSurface->u.pCubeTexture->SetAutoGenFilterType((D3DTEXTUREFILTERTYPE)pSurface->autogenFilter);
     1814        else
     1815            hr = pSurface->u.pTexture->SetAutoGenFilterType((D3DTEXTUREFILTERTYPE)pSurface->autogenFilter);
    17011816        AssertMsg(hr == D3D_OK, ("vmsvga3dBackCreateTexture: SetAutoGenFilterType failed with %x\n", hr));
    17021817    }
     
    17081823     */
    17091824    Log(("vmsvga3dBackCreateTexture: sync texture\n"));
    1710     for (uint32_t i = 0; i < pSurface->faces[0].numMipLevels; i++)
    1711     {
    1712         D3DLOCKED_RECT LockedRect;
    1713 
    1714         hr = pTexture->LockRect(i, /* texture level */
    1715                                 &LockedRect,
    1716                                 NULL,   /* entire texture */
    1717                                 0);
    1718 
    1719         AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1720 
    1721         Log(("vmsvga3dBackCreateTexture: sync texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pSurface->pMipmapLevels[i].cbSurfacePitch));
    1722 
    1723         uint8_t *pDest = (uint8_t *)LockedRect.pBits;
    1724         uint8_t *pSrc  = (uint8_t *)pSurface->pMipmapLevels[i].pSurfaceData;
    1725         for (uint32_t j = 0; j < pSurface->pMipmapLevels[i].size.height; j++)
    1726         {
    1727             memcpy(pDest, pSrc, pSurface->pMipmapLevels[i].cbSurfacePitch);
    1728 
    1729             pDest += LockedRect.Pitch;
    1730             pSrc  += pSurface->pMipmapLevels[i].cbSurfacePitch;
    1731         }
    1732 
    1733         hr = pTexture->UnlockRect(i /* texture level */);
    1734         AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1735 
    1736         pSurface->pMipmapLevels[i].fDirty = false;
     1825
     1826    if (pSurface->flags & SVGA3D_SURFACE_CUBEMAP)
     1827    {
     1828        IDirect3DCubeTexture9 *pCubeTexture = pSurface->bounce.pCubeTexture ?
     1829                                                  pSurface->bounce.pCubeTexture :
     1830                                                  pSurface->u.pCubeTexture;
     1831
     1832        for (uint32_t iFace = 0; iFace < 6; ++iFace)
     1833        {
     1834            const D3DCUBEMAP_FACES Face = vmsvga3dCubemapFaceFromIndex(iFace);
     1835
     1836            for (uint32_t i = 0; i < numMipLevels; ++i)
     1837            {
     1838                D3DLOCKED_RECT LockedRect;
     1839                hr = pCubeTexture->LockRect(Face,
     1840                                            i, /* texture level */
     1841                                            &LockedRect,
     1842                                            NULL,   /* entire texture */
     1843                                            0);
     1844                AssertMsgBreak(hr == D3D_OK, ("LockRect failed with %x\n", hr));
     1845
     1846                PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->pMipmapLevels[iFace * numMipLevels + i];
     1847
     1848                LogFunc(("sync texture face %d mipmap level %d (pitch %x vs %x)\n",
     1849                          iFace, i, LockedRect.Pitch, pMipLevel->cbSurfacePitch));
     1850
     1851                uint8_t *pDest = (uint8_t *)LockedRect.pBits;
     1852                const uint8_t *pSrc = (uint8_t *)pMipLevel->pSurfaceData;
     1853                for (uint32_t j = 0; j < pMipLevel->cBlocksY; ++j)
     1854                {
     1855                    memcpy(pDest, pSrc, pMipLevel->cbSurfacePitch);
     1856
     1857                    pDest += LockedRect.Pitch;
     1858                    pSrc  += pMipLevel->cbSurfacePitch;
     1859                }
     1860
     1861                hr = pCubeTexture->UnlockRect(Face, i /* texture level */);
     1862                AssertMsgBreak(hr == D3D_OK, ("UnlockRect failed with %x\n", hr));
     1863
     1864                pMipLevel->fDirty = false;
     1865            }
     1866
     1867            if (hr != D3D_OK)
     1868                break;
     1869        }
     1870
     1871        if (hr != D3D_OK)
     1872        {
     1873            D3D_RELEASE(pSurface->bounce.pCubeTexture);
     1874            D3D_RELEASE(pSurface->u.pCubeTexture);
     1875            return VERR_INTERNAL_ERROR;
     1876        }
     1877    }
     1878    else
     1879    {
     1880        IDirect3DTexture9 *pTexture = pSurface->bounce.pTexture ? pSurface->bounce.pTexture : pSurface->u.pTexture;
     1881
     1882        for (uint32_t i = 0; i < numMipLevels; ++i)
     1883        {
     1884            D3DLOCKED_RECT LockedRect;
     1885
     1886            hr = pTexture->LockRect(i, /* texture level */
     1887                                    &LockedRect,
     1888                                    NULL,   /* entire texture */
     1889                                    0);
     1890
     1891            AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
     1892
     1893            Log(("vmsvga3dBackCreateTexture: sync texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pSurface->pMipmapLevels[i].cbSurfacePitch));
     1894
     1895            uint8_t *pDest = (uint8_t *)LockedRect.pBits;
     1896            const uint8_t *pSrc = (uint8_t *)pSurface->pMipmapLevels[i].pSurfaceData;
     1897            for (uint32_t j = 0; j < pSurface->pMipmapLevels[i].cBlocksY; ++j)
     1898            {
     1899                memcpy(pDest, pSrc, pSurface->pMipmapLevels[i].cbSurfacePitch);
     1900
     1901                pDest += LockedRect.Pitch;
     1902                pSrc  += pSurface->pMipmapLevels[i].cbSurfacePitch;
     1903            }
     1904
     1905            hr = pTexture->UnlockRect(i /* texture level */);
     1906            AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
     1907
     1908            pSurface->pMipmapLevels[i].fDirty = false;
     1909        }
    17371910    }
    17381911
     
    17411914        Log(("vmsvga3dBackCreateTexture: sync dirty texture from bounce buffer\n"));
    17421915
    1743         hr = pContext->pDevice->UpdateTexture(pSurface->bounce.pTexture, pSurface->u.pTexture);
     1916        if (pSurface->flags & SVGA3D_SURFACE_CUBEMAP)
     1917            hr = pContext->pDevice->UpdateTexture(pSurface->bounce.pCubeTexture, pSurface->u.pCubeTexture);
     1918        else
     1919            hr = pContext->pDevice->UpdateTexture(pSurface->bounce.pTexture, pSurface->u.pTexture);
    17441920        AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: UpdateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
    17451921
     
    17661942 * @param   pState              The VMSVGA3d state.
    17671943 * @param   pDstSurface         The destination host surface.
     1944 * @param   uDstFace            The destination face (valid).
    17681945 * @param   uDstMipmap          The destination mipmap level (valid).
    17691946 * @param   pDstBox             The destination box.
    17701947 * @param   pSrcSurface         The source host surface.
     1948 * @param   uSrcFace            The destination face (valid).
    17711949 * @param   uSrcMipmap          The source mimap level (valid).
    17721950 * @param   pSrcBox             The source box.
     
    17751953 */
    17761954int vmsvga3dBackSurfaceStretchBlt(PVGASTATE pThis, PVMSVGA3DSTATE pState,
    1777                                   PVMSVGA3DSURFACE pDstSurface, uint32_t uDstMipmap, SVGA3dBox const *pDstBox,
    1778                                   PVMSVGA3DSURFACE pSrcSurface, uint32_t uSrcMipmap, SVGA3dBox const *pSrcBox,
     1955                                  PVMSVGA3DSURFACE pDstSurface, uint32_t uDstFace, uint32_t uDstMipmap, SVGA3dBox const *pDstBox,
     1956                                  PVMSVGA3DSURFACE pSrcSurface, uint32_t uSrcFace, uint32_t uSrcMipmap, SVGA3dBox const *pSrcBox,
    17791957                                  SVGA3dStretchBltMode enmMode, PVMSVGA3DCONTEXT pContext)
    17801958{
    17811959    HRESULT hr;
     1960    int rc;
    17821961
    17831962    /* Flush the drawing pipeline for this surface as it could be used in a shared context. */
     
    18001979
    18011980    IDirect3DSurface9 *pSrc;
    1802     bool const fSrcTexture  = !!(pSrcSurface->flags & SVGA3D_SURFACE_HINT_TEXTURE);
    1803     if (fSrcTexture)
    1804     {
    1805         hr = pSrcSurface->u.pTexture->GetSurfaceLevel(uSrcMipmap /* Texture level */, &pSrc);
    1806         AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceStretchBlt: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1807     }
    1808     else
    1809         pSrc = pSrcSurface->u.pSurface;
     1981    rc = vmsvga3dGetD3DSurface(pContext, pSrcSurface, uSrcFace, uSrcMipmap, false, &pSrc);
     1982    AssertRCReturn(rc, rc);
    18101983
    18111984    IDirect3DSurface9 *pDst;
    1812     bool const fDstTexture = !!(pDstSurface->flags & SVGA3D_SURFACE_HINT_TEXTURE);
    1813     if (fDstTexture)
    1814     {
    1815         hr = pDstSurface->u.pTexture->GetSurfaceLevel(uDstMipmap /* Texture level */, &pDst);
    1816         AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceStretchBlt: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1817     }
    1818     else
    1819         pDst = pDstSurface->u.pSurface;
     1985    rc = vmsvga3dGetD3DSurface(pContext, pDstSurface, uDstFace, uDstMipmap, false, &pDst);
     1986    AssertRCReturn(rc, rc);
    18201987
    18211988    D3DTEXTUREFILTERTYPE moded3d;
     
    18382005    hr = pContext->pDevice->StretchRect(pSrc, &RectSrc, pDst, &RectDst, moded3d);
    18392006
    1840     /* GetSurfaceLevel increases the reference count; release the objects again. */
    1841     if (fDstTexture)
    1842         pDst->Release();
    1843     if (fSrcTexture)
    1844         pSrc->Release();
    1845 
    1846     AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceStretchBlt: StretchRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
     2007    D3D_RELEASE(pDst);
     2008    D3D_RELEASE(pSrc);
     2009
     2010    AssertMsgReturn(hr == D3D_OK, ("StretchRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
    18472011
    18482012    /* Track the StretchRect operation. */
     
    18612025 * @param   pState              The VMSVGA3d state.
    18622026 * @param   pSurface            The host surface.
     2027 * @param   pMipLevel           Mipmap level. The caller knows it already.
     2028 * @param   uHostFace           The host face (valid).
    18632029 * @param   uHostMipmap         The host mipmap level (valid).
    18642030 * @param   GuestPtr            The guest pointer.
    1865  * @param   cbSrcPitch          The guest (?) pitch.
     2031 * @param   cbGuestPitch        The guest pitch.
    18662032 * @param   transfer            The transfer direction.
    1867  * @param   pBox                The box to copy.
     2033 * @param   pBox                The box to copy (clipped, valid).
    18682034 * @param   pContext            The context (for OpenGL).
    18692035 * @param   rc                  The current rc for all boxes.
    18702036 * @param   iBox                The current box number (for Direct 3D).
    18712037 */
    1872 int vmsvga3dBackSurfaceDMACopyBox(PVGASTATE pThis, PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface, uint32_t uHostMipmap,
    1873                                   SVGAGuestPtr GuestPtr, uint32_t cbSrcPitch, SVGA3dTransferType transfer,
     2038int vmsvga3dBackSurfaceDMACopyBox(PVGASTATE pThis, PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface,
     2039                                  PVMSVGA3DMIPMAPLEVEL pMipLevel, uint32_t uHostFace, uint32_t uHostMipmap,
     2040                                  SVGAGuestPtr GuestPtr, uint32_t cbGuestPitch, SVGA3dTransferType transfer,
    18742041                                  SVGA3dCopyBox const *pBox, PVMSVGA3DCONTEXT pContext, int rc, int iBox)
    18752042{
    18762043    HRESULT hr = D3D_OK;
    1877     DWORD dwFlags = transfer == SVGA3D_READ_HOST_VRAM ? D3DLOCK_READONLY : 0;
    1878     bool fTexture = false;
    1879     bool fVertex = false;
    1880     bool fRenderTargetTexture = false;
    1881     switch (pSurface->flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)
    1882     {
    1883     case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
    1884         fRenderTargetTexture = true;
    1885         /* no break */
    1886     case SVGA3D_SURFACE_HINT_TEXTURE:
    1887         fTexture = true;
    1888         /* no break */
    1889     case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
    1890         if (pSurface->fStencilAsTexture)
    1891             fTexture = true;
    1892         /* no break */
    1893     case SVGA3D_SURFACE_HINT_RENDERTARGET:
    1894     {
    1895         D3DLOCKED_RECT LockedRect;
     2044    const uint32_t u32SurfHints = pSurface->flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK;
     2045    const DWORD dwFlags = transfer == SVGA3D_READ_HOST_VRAM ? D3DLOCK_READONLY : 0;
     2046    // if (u32SurfHints != 0x18 && u32SurfHints != 0x60) ASMBreakpoint();
     2047
     2048    bool fTexture =    RT_BOOL(u32SurfHints & (SVGA3D_SURFACE_HINT_TEXTURE |
     2049                                               SVGA3D_SURFACE_CUBEMAP))
     2050                    || pSurface->fStencilAsTexture;
     2051
     2052    bool fRenderTarget = RT_BOOL(u32SurfHints & SVGA3D_SURFACE_HINT_RENDERTARGET);
     2053
     2054    if (fTexture || fRenderTarget)
     2055    {
     2056        rc = vmsvga3dContextFromCid(pState, pSurface->idAssociatedContext, &pContext);
     2057        AssertRCReturn(rc, rc);
     2058
     2059        /* Get the surface involved in the transfer. */
     2060        IDirect3DSurface9 *pSurf;
     2061        rc = vmsvga3dGetD3DSurface(pContext, pSurface, uHostFace, uHostMipmap, true, &pSurf);
     2062        AssertRCReturn(rc, rc);
     2063
     2064        /** @todo inefficient for VRAM buffers!! */
     2065        if (fTexture)
     2066        {
     2067            if (pSurface->bounce.pTexture)
     2068            {
     2069                if (   transfer == SVGA3D_READ_HOST_VRAM
     2070                    && fRenderTarget
     2071                    && iBox == 0 /* only the first time */)
     2072                {
     2073                    /* Copy the texture mipmap level to the bounce texture. */
     2074
     2075                    /* Source is the texture, destination is the bounce texture. */
     2076                    IDirect3DSurface9 *pSrc;
     2077                    rc = vmsvga3dGetD3DSurface(pContext, pSurface, uHostFace, uHostMipmap, false, &pSrc);
     2078                    AssertRCReturn(rc, rc);
     2079
     2080                    Assert(pSurf != pSrc);
     2081
     2082                    hr = pContext->pDevice->GetRenderTargetData(pSrc, pSurf);
     2083                    AssertMsgReturn(hr == D3D_OK, ("GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
     2084
     2085                    D3D_RELEASE(pSrc);
     2086                }
     2087            }
     2088        }
     2089
    18962090        RECT Rect;
    1897 
    18982091        Rect.left   = pBox->x;
    18992092        Rect.top    = pBox->y;
     
    19012094        Rect.bottom = pBox->y + pBox->h;   /* exclusive */
    19022095
    1903         /** @todo inefficient for VRAM buffers!! */
    1904         if (fTexture)
    1905         {
    1906             if (pSurface->bounce.pTexture)
    1907             {
    1908                 if (    transfer == SVGA3D_READ_HOST_VRAM
    1909                     &&  fRenderTargetTexture
    1910                     &&  iBox == 0 /* only the first time */)
    1911                 {
    1912                     IDirect3DSurface9 *pSrc, *pDest;
    1913 
    1914                     /** @todo stricter checks for associated context */
    1915                     uint32_t cid = pSurface->idAssociatedContext;
    1916                     if (    cid >= pState->cContexts
    1917                         ||  pState->papContexts[cid]->id != cid)
    1918                     {
    1919                         Log(("vmsvga3dSurfaceDMA invalid context id (%x - %x)!\n", cid, (cid >= pState->cContexts) ? -1 : pState->papContexts[cid]->id));
    1920                         AssertFailedReturn(VERR_INVALID_PARAMETER);
    1921                     }
    1922                     pContext = pState->papContexts[cid];
    1923 
    1924                     /** @todo only sync when something was actually rendered (since the last sync) */
    1925                     Log(("vmsvga3dSurfaceDMA: sync bounce buffer\n"));
    1926                     hr = pSurface->bounce.pTexture->GetSurfaceLevel(uHostMipmap, &pDest);
    1927                     AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceDMA: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1928 
    1929                     hr = pSurface->u.pTexture->GetSurfaceLevel(uHostMipmap, &pSrc);
    1930                     AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceDMA: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1931 
    1932                     hr = pContext->pDevice->GetRenderTargetData(pSrc, pDest);
    1933                     AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceDMA: GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1934 
    1935                     pSrc->Release();
    1936                     pDest->Release();
    1937                 }
    1938 
    1939                 hr = pSurface->bounce.pTexture->LockRect(uHostMipmap, /* texture level */
    1940                                                          &LockedRect,
    1941                                                          &Rect,
    1942                                                          dwFlags);
    1943             }
    1944             else
    1945                 hr = pSurface->u.pTexture->LockRect(uHostMipmap, /* texture level */
    1946                                                     &LockedRect,
    1947                                                     &Rect,
    1948                                                     dwFlags);
    1949         }
    1950         else
    1951             hr = pSurface->u.pSurface->LockRect(&LockedRect,
    1952                                                 &Rect,
    1953                                                 dwFlags);
    1954         AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceDMA: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1955 
    1956         if (fTexture)
    1957             Log(("Lock sid=%x TEXTURE (bounce=%d) memory for rectangle (%d,%d)(%d,%d)\n", pSurface->id, !!(pSurface->bounce.pTexture), Rect.left, Rect.top, Rect.right, Rect.bottom));
    1958         else
    1959             Log(("Lock sid=%x %s memory for rectangle (%d,%d)(%d,%d)\n", pSurface->id, (pSurface->flags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL) ? "DEPTH-STENCIL" : "RENDERTARGET", Rect.left, Rect.top, Rect.right, Rect.bottom));
     2096        D3DLOCKED_RECT LockedRect;
     2097        hr = pSurf->LockRect(&LockedRect, &Rect, dwFlags);
     2098        AssertMsgReturn(hr == D3D_OK, ("LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
     2099
     2100        LogFunc(("Lock sid=%x %s(bounce=%d) memory for rectangle (%d,%d)(%d,%d)\n",
     2101                 pSurface->id, fTexture ? "TEXTURE " : "", RT_BOOL(pSurface->bounce.pTexture),
     2102                 Rect.left, Rect.top, Rect.right, Rect.bottom));
     2103
     2104        uint32_t u32BlockX = pBox->srcx / pSurface->cxBlock;
     2105        uint32_t u32BlockY = pBox->srcy / pSurface->cyBlock;
     2106        Assert(u32BlockX * pSurface->cxBlock == pBox->srcx);
     2107        Assert(u32BlockY * pSurface->cyBlock == pBox->srcy);
     2108        uint32_t cBlocksX = (pBox->w + pSurface->cxBlock - 1) / pSurface->cxBlock;
     2109        uint32_t cBlocksY = (pBox->h + pSurface->cyBlock - 1) / pSurface->cyBlock;
    19602110
    19612111        rc = vmsvgaGMRTransfer(pThis,
     
    19642114                               LockedRect.Pitch,
    19652115                               GuestPtr,
    1966                                pBox->srcx * pSurface->cbBlock + pBox->srcy * cbSrcPitch,
    1967                                cbSrcPitch,
    1968                                pBox->w * pSurface->cbBlock,
    1969                                pBox->h);
     2116                               u32BlockX * pSurface->cbBlock + u32BlockY * cbGuestPitch,
     2117                               cbGuestPitch,
     2118                               cBlocksX * pSurface->cbBlock,
     2119                               cBlocksY);
    19702120        AssertRC(rc);
    19712121
    1972         Log4(("first line:\n%.*Rhxd\n", pBox->w * pSurface->cbBlock, LockedRect.pBits));
     2122        Log4(("first line:\n%.*Rhxd\n", cBlocksX * pSurface->cbBlock, LockedRect.pBits));
     2123
     2124        hr = pSurf->UnlockRect();
     2125
     2126        D3D_RELEASE(pSurf);
     2127
     2128        AssertMsgReturn(hr == D3D_OK, ("UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
    19732129
    19742130        if (fTexture)
     
    19762132            if (pSurface->bounce.pTexture)
    19772133            {
    1978                 hr = pSurface->bounce.pTexture->UnlockRect(uHostMipmap);
    1979                 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceDMA: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
    1980 
    19812134                if (transfer == SVGA3D_WRITE_HOST_VRAM)
    19822135                {
    1983                     /** @todo stricter checks for associated context */
    1984                     uint32_t cid = pSurface->idAssociatedContext;
    1985                     if (    cid >= pState->cContexts
    1986                         ||  pState->papContexts[cid]->id != cid)
    1987                     {
    1988                         Log(("vmsvga3dSurfaceDMA invalid context id!\n"));
    1989                         AssertFailedReturn(VERR_INVALID_PARAMETER);
    1990                     }
    1991                     pContext = pState->papContexts[cid];
    1992 
    1993                     Log(("vmsvga3dSurfaceDMA: sync texture from bounce buffer\n"));
     2136                    LogFunc(("Sync texture from bounce buffer\n"));
    19942137
    19952138                    /* Copy the new contents to the actual texture object. */
     
    20012144                }
    20022145            }
    2003             else
    2004                 hr = pSurface->u.pTexture->UnlockRect(uHostMipmap);
    2005         }
    2006         else
    2007             hr = pSurface->u.pSurface->UnlockRect();
    2008         AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceDMA: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
    2009         break;
    2010     }
    2011 
    2012     case SVGA3D_SURFACE_HINT_VERTEXBUFFER | SVGA3D_SURFACE_HINT_INDEXBUFFER:
    2013     case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
    2014         fVertex = RT_BOOL(pSurface->fu32ActualUsageFlags & SVGA3D_SURFACE_HINT_VERTEXBUFFER);
    2015         /* no break */
    2016 
    2017     case SVGA3D_SURFACE_HINT_INDEXBUFFER:
    2018     {
    2019         uint8_t *pData;
    2020         unsigned uDestOffset;
    2021 
    2022         uDestOffset = pBox->x * pSurface->cbBlock + pBox->y * pSurface->pMipmapLevels[uHostMipmap].cbSurfacePitch;
    2023         AssertReturn(uDestOffset + pBox->w * pSurface->cbBlock + (pBox->h - 1) * pSurface->pMipmapLevels[uHostMipmap].cbSurfacePitch <= pSurface->pMipmapLevels[uHostMipmap].cbSurface, VERR_INTERNAL_ERROR);
    2024 
     2146        }
     2147    }
     2148    else if (RT_BOOL(u32SurfHints & (SVGA3D_SURFACE_HINT_VERTEXBUFFER |
     2149                                     SVGA3D_SURFACE_HINT_INDEXBUFFER)))
     2150    {
     2151        /*
     2152         * Mesa SVGA driver can use the same buffer either for vertex or index data.
     2153         * But D3D distinguishes between index and vertex buffer objects.
     2154         * Therefore it should be possible to switch the buffer type on the fly.
     2155         *
     2156         * Always save the data to the memory buffer in pSurface->pMipmapLevels and,
     2157         * if necessary, recreate the corresponding D3D object with the data.
     2158         */
     2159
     2160        /* Buffers are uncompressed. */
     2161        AssertReturn(pSurface->cxBlock == 1 && pSurface->cyBlock == 1, VERR_INTERNAL_ERROR);
     2162
     2163        /* Current type of the buffer. */
     2164        const bool fVertex = RT_BOOL(pSurface->fu32ActualUsageFlags & SVGA3D_SURFACE_HINT_VERTEXBUFFER);
     2165
     2166        /* Caller already clipped pBox and buffers are 1-dimensional. */
     2167        Assert(pBox->y == 0 && pBox->h == 1 && pBox->z == 0 && pBox->d == 1);
     2168
     2169        const uint32_t uHostOffset = pBox->x * pSurface->cbBlock;
     2170        const uint32_t cbWidth = pBox->w * pSurface->cbBlock;
     2171
     2172        AssertReturn(uHostOffset < pMipLevel->cbSurface, VERR_INTERNAL_ERROR);
     2173        AssertReturn(cbWidth <= pMipLevel->cbSurface, VERR_INTERNAL_ERROR);
     2174        AssertReturn(uHostOffset <= pMipLevel->cbSurface - cbWidth, VERR_INTERNAL_ERROR);
     2175
     2176        uint8_t *pu8HostData = (uint8_t *)pMipLevel->pSurfaceData + uHostOffset;
     2177
     2178        const uint32_t uGuestOffset = pBox->srcx * pSurface->cbBlock;
     2179
     2180        /* Copy data between the guest and the host buffer. */
     2181        rc = vmsvgaGMRTransfer(pThis,
     2182                               transfer,
     2183                               pu8HostData,
     2184                               pMipLevel->cbSurfacePitch,
     2185                               GuestPtr,
     2186                               uGuestOffset,
     2187                               cbGuestPitch,
     2188                               cbWidth,
     2189                               1); /* Buffers are 1-dimensional */
     2190        AssertRC(rc);
     2191
     2192        Log4(("Buffer first line:\n%.*Rhxd\n", cbWidth, pu8HostData));
     2193
     2194        pMipLevel->fDirty = true;
     2195        pSurface->fDirty = true;
     2196
     2197        /* Also copy the data to the current D3D buffer object. */
     2198        uint8_t *pu8Buffer = NULL;
    20252199        /** @todo lock only as much as we really need */
    20262200        if (fVertex)
    2027             hr = pSurface->u.pVertexBuffer->Lock(0, 0, (void **)&pData, dwFlags);
     2201            hr = pSurface->u.pVertexBuffer->Lock(0, 0, (void **)&pu8Buffer, dwFlags);
    20282202        else
    2029             hr = pSurface->u.pIndexBuffer->Lock(0, 0, (void **)&pData, dwFlags);
    2030         AssertMsg(hr == D3D_OK, ("vmsvga3dSurfaceDMA: Lock %s failed with %x\n", (fVertex) ? "vertex" : "index", hr));
    2031 
    2032         Log(("Lock %s memory for rectangle (%d,%d)(%d,%d)\n", (fVertex) ? "vertex" : "index", pBox->x, pBox->y, pBox->x + pBox->w, pBox->y + pBox->h));
    2033 
    2034         rc = vmsvgaGMRTransfer(pThis,
    2035                                transfer,
    2036                                pData + uDestOffset,
    2037                                pSurface->pMipmapLevels[uHostMipmap].cbSurfacePitch,
    2038                                GuestPtr,
    2039                                pBox->srcx * pSurface->cbBlock + pBox->srcy * cbSrcPitch,
    2040                                cbSrcPitch,
    2041                                pBox->w * pSurface->cbBlock,
    2042                                pBox->h);
    2043         AssertRC(rc);
    2044 
    2045         Log4(("first line:\n%.*Rhxd\n", cbSrcPitch, pData));
     2203            hr = pSurface->u.pIndexBuffer->Lock(0, 0, (void **)&pu8Buffer, dwFlags);
     2204        AssertMsgReturn(hr == D3D_OK, ("Lock %s failed with %x\n", fVertex ? "vertex" : "index", hr), VERR_INTERNAL_ERROR);
     2205
     2206        LogFunc(("Lock %s memory for rectangle (%d,%d)(%d,%d)\n", fVertex ? "vertex" : "index", pBox->x, pBox->y, pBox->x + pBox->w, pBox->y + pBox->h));
     2207
     2208        const uint8_t *pu8Src = pu8HostData;
     2209        uint8_t *pu8Dst = pu8Buffer + uHostOffset;
     2210        memcpy(pu8Dst, pu8Src, cbWidth);
    20462211
    20472212        if (fVertex)
     
    20492214        else
    20502215            hr = pSurface->u.pIndexBuffer->Unlock();
    2051         AssertMsg(hr == D3D_OK, ("vmsvga3dSurfaceDMA: Unlock %s failed with %x\n", (fVertex) ? "vertex" : "index", hr));
    2052         break;
    2053     }
    2054 
    2055     default:
    2056         AssertMsgFailed(("Unsupported surface hint 0x%08X\n", pSurface->flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK));
    2057         break;
     2216        AssertMsg(hr == D3D_OK, ("Unlock %s failed with %x\n", fVertex ? "vertex" : "index", hr));
     2217    }
     2218    else
     2219    {
     2220        AssertMsgFailed(("Unsupported surface hint 0x%08X\n", u32SurfHints));
    20582221    }
    20592222
     
    21682331
    21692332                pContext->pDevice->ColorFill(pSrc, NULL, (D3DCOLOR)0x11122255);
    2170                 pSrc->Release();
     2333                D3D_RELEASE(pSrc);
    21712334            }
    21722335        }
     
    23022465     * Blit the surface rectangle(s) to the back buffer.
    23032466     */
    2304     uint32_t const cxSurface = pSurface->pMipmapLevels[0].size.width;
    2305     uint32_t const cySurface = pSurface->pMipmapLevels[0].size.height;
     2467    Assert(pSurface->cxBlock == 1 && pSurface->cyBlock == 1);
     2468    uint32_t const cxSurface = pSurface->pMipmapLevels[0].mipmapSize.width;
     2469    uint32_t const cySurface = pSurface->pMipmapLevels[0].mipmapSize.height;
    23062470    for (uint32_t i = 0; i < cRects; i++)
    23072471    {
     
    24492613
    24502614    if (pSurface->flags & SVGA3D_SURFACE_HINT_TEXTURE)
    2451         pSurfaceD3D->Release();
     2615        D3D_RELEASE(pSurfaceD3D);
    24522616
    24532617    AssertMsgReturn(hr == D3D_OK, ("vmsvga3dCommandPresent: StretchRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
     
    24562620    AssertMsgReturn(hr == D3D_OK, ("vmsvga3dCommandPresent: Present failed with %x\n", hr), VERR_INTERNAL_ERROR);
    24572621
    2458     pBackBuffer->Release();
     2622    D3D_RELEASE(pBackBuffer);
    24592623    return VINF_SUCCESS;
    24602624}
     
    24762640    PVMSVGA3DSTATE          pState = pThis->svga.p3dState;
    24772641
     2642    Log(("vmsvga3dContextDefine id %x\n", cid));
     2643
    24782644    AssertReturn(pState, VERR_NO_MEMORY);
    24792645    AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
    2480 
    2481     Log(("vmsvga3dContextDefine id %x\n", cid));
    24822646
    24832647    if (cid >= pState->cContexts)
     
    26302794                    {
    26312795                        uint32_t idx = i + face * pSurface->faces[0].numMipLevels;
    2632                         memcpy(&pMipLevelSize[idx], &pSurface->pMipmapLevels[idx].size, sizeof(SVGA3dSize));
     2796                        memcpy(&pMipLevelSize[idx], &pSurface->pMipmapLevels[idx].mipmapSize, sizeof(SVGA3dSize));
    26332797                    }
    26342798                }
     
    26552819                    Log(("vmsvga3dContextDestroy: remove shared dependency for surface sid=%x\n", sid));
    26562820
    2657                     switch (pSurface->flags & (SVGA3D_SURFACE_HINT_INDEXBUFFER | SVGA3D_SURFACE_HINT_VERTEXBUFFER | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET | SVGA3D_SURFACE_HINT_DEPTHSTENCIL | SVGA3D_SURFACE_CUBEMAP))
     2821                    switch (pSurface->flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)
    26582822                    {
    26592823                    case SVGA3D_SURFACE_HINT_TEXTURE:
    26602824                    case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
    26612825                        Assert(pSharedSurface->u.pTexture);
    2662                         if (pSharedSurface->u.pTexture)
    2663                             pSharedSurface->u.pTexture->Release();
     2826                        D3D_RELEASE(pSharedSurface->u.pTexture);
    26642827                        break;
    26652828
     
    26982861
    26992862        /* Release the D3D device object */
    2700         pContext->pDevice->Release();
     2863        D3D_RELEASE(pContext->pDevice);
    27012864
    27022865        /* Destroy the window we've created. */
     
    27882951                                AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
    27892952
    2790                                 pSrc->Release();
    2791                                 pDest->Release();
     2953                                D3D_RELEASE(pSrc);
     2954                                D3D_RELEASE(pDest);
    27922955
    27932956                                hr = pSurface->bounce.pTexture->LockRect(i,
     
    28182981
    28192982
    2820                     switch (pSurface->flags & (SVGA3D_SURFACE_HINT_INDEXBUFFER | SVGA3D_SURFACE_HINT_VERTEXBUFFER | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET | SVGA3D_SURFACE_HINT_DEPTHSTENCIL | SVGA3D_SURFACE_CUBEMAP))
     2983                    switch (pSurface->flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)
    28212984                    {
    28222985                    case SVGA3D_SURFACE_CUBEMAP:
    2823                         AssertFailed(); /** @todo */
     2986                    case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE:
     2987                    case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
     2988                        D3D_RELEASE(pSurface->u.pCubeTexture);
     2989                        D3D_RELEASE(pSurface->bounce.pCubeTexture);
    28242990                        break;
    28252991
     2992                    case SVGA3D_SURFACE_HINT_INDEXBUFFER | SVGA3D_SURFACE_HINT_VERTEXBUFFER:
    28262993                    case SVGA3D_SURFACE_HINT_INDEXBUFFER:
    2827                         pSurface->u.pIndexBuffer->Release();
    2828                         break;
    2829 
    28302994                    case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
    2831                         pSurface->u.pVertexBuffer->Release();
    2832                         pSurface->u.pVertexBuffer = NULL;
     2995                        if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_VERTEXBUFFER)
     2996                            D3D_RELEASE(pSurface->u.pVertexBuffer);
     2997                        else if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_INDEXBUFFER)
     2998                            D3D_RELEASE(pSurface->u.pIndexBuffer);
     2999                        else
     3000                            AssertMsg(pSurface->u.pVertexBuffer == NULL, ("fu32ActualUsageFlags %x\n", pSurface->fu32ActualUsageFlags));
    28333001                        break;
    28343002
    28353003                    case SVGA3D_SURFACE_HINT_TEXTURE:
    28363004                    case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
    2837                         pSurface->u.pTexture->Release();
    2838                         pSurface->u.pTexture = NULL;
    2839                         if (pSurface->bounce.pTexture)
    2840                         {
    2841                             pSurface->bounce.pTexture->Release();
    2842                             pSurface->bounce.pTexture = NULL;
    2843                         }
     3005                        D3D_RELEASE(pSurface->u.pTexture);
     3006                        D3D_RELEASE(pSurface->bounce.pTexture);
    28443007                        break;
    28453008
     
    28473010                    case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
    28483011                        if (pSurface->fStencilAsTexture)
    2849                             pSurface->u.pTexture->Release();
     3012                            D3D_RELEASE(pSurface->u.pTexture);
    28503013                        else
    2851                             pSurface->u.pSurface->Release();
    2852                         pSurface->u.pSurface = NULL;
     3014                            D3D_RELEASE(pSurface->u.pSurface);
    28533015                        break;
    28543016
    28553017                    default:
     3018                        AssertFailed();
    28563019                        break;
    28573020                    }
     
    38824045
    38834046                hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pDummyRenderTarget);
    3884                 pDummyRenderTarget->Release();
     4047                D3D_RELEASE(pDummyRenderTarget);
    38854048            }
    38864049            else
     
    39344097            {
    39354098                Log(("vmsvga3dSetRenderTarget DEPTH/STENCIL; cQualityLevels=%d\n", cQualityLevels));
    3936                 hr = pContext->pDevice->CreateDepthStencilSurface(pRenderTarget->pMipmapLevels[0].size.width,
    3937                                                                   pRenderTarget->pMipmapLevels[0].size.height,
     4099                hr = pContext->pDevice->CreateDepthStencilSurface(pRenderTarget->pMipmapLevels[0].mipmapSize.width,
     4100                                                                  pRenderTarget->pMipmapLevels[0].mipmapSize.height,
    39384101                                                                  pRenderTarget->formatD3D,
    39394102                                                                  pRenderTarget->multiSampleTypeD3D,
     
    40044167
    40054168            hr = pContext->pDevice->SetDepthStencilSurface(pStencilSurface);
    4006             pStencilSurface->Release();
     4169            D3D_RELEASE(pStencilSurface);
    40074170            AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
    40084171        }
     
    40474210                Log(("vmsvga3dSetRenderTarget; using texture sid=%x created for another context (%d vs %d)\n", target.sid, pRenderTarget->idAssociatedContext, cid));
    40484211
    4049                 PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pThis, pContext, pRenderTarget);
     4212                PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pContext, pRenderTarget);
    40504213                AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
    40514214
     
    40824245
    40834246                Log(("vmsvga3dSetRenderTarget COLOR; cQualityLevels=%d\n", cQualityLevels));
    4084                 Log(("Create rendertarget (%d,%d) format=%x multisample=%x\n", pRenderTarget->pMipmapLevels[0].size.width, pRenderTarget->pMipmapLevels[0].size.height, pRenderTarget->formatD3D, pRenderTarget->multiSampleTypeD3D));
    4085 
    4086                 hr = pContext->pDevice->CreateRenderTarget(pRenderTarget->pMipmapLevels[0].size.width,
    4087                                                            pRenderTarget->pMipmapLevels[0].size.height,
     4247                Log(("Create rendertarget (%d,%d) format=%x multisample=%x\n", pRenderTarget->pMipmapLevels[0].mipmapSize.width, pRenderTarget->pMipmapLevels[0].mipmapSize.height, pRenderTarget->formatD3D, pRenderTarget->multiSampleTypeD3D));
     4248
     4249                hr = pContext->pDevice->CreateRenderTarget(pRenderTarget->pMipmapLevels[0].mipmapSize.width,
     4250                                                           pRenderTarget->pMipmapLevels[0].mipmapSize.height,
    40884251                                                           pRenderTarget->formatD3D,
    40894252                                                           pRenderTarget->multiSampleTypeD3D,
     
    41114274        hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pSurface);
    41124275        if (fTexture)
    4113             pSurface->Release();    /* Release reference to texture level 0 */
     4276            D3D_RELEASE(pSurface);    /* Release reference to texture level 0 */
    41144277        AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
    41154278
     
    43904553                PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
    43914554
    4392                 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture sid=%x (%d,%d)\n", currentStage, pTextureState[i].value, pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height));
     4555                Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture sid=%x (%d,%d)\n", currentStage, pTextureState[i].value, pSurface->pMipmapLevels[0].mipmapSize.width, pSurface->pMipmapLevels[0].mipmapSize.height));
    43934556
    43944557                if (!pSurface->u.pTexture)
    43954558                {
    43964559                    Assert(pSurface->idAssociatedContext == SVGA3D_INVALID_ID);
    4397                     Log(("CreateTexture (%d,%d) level=%d fUsage=%x format=%x\n", pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height, pSurface->faces[0].numMipLevels, pSurface->fUsageD3D, pSurface->formatD3D));
     4560                    Log(("CreateTexture (%d,%d) level=%d fUsage=%x format=%x\n", pSurface->pMipmapLevels[0].mipmapSize.width, pSurface->pMipmapLevels[0].mipmapSize.height, pSurface->faces[0].numMipLevels, pSurface->fUsageD3D, pSurface->formatD3D));
    43984561                    int rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
    43994562                    AssertRCReturn(rc, rc);
     
    44104573                    Log(("vmsvga3dSetTextureState; using texture sid=%x created for another context (%d vs %d)\n", sid, pSurface->idAssociatedContext, cid));
    44114574
    4412                     PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pThis, pContext, pSurface);
     4575                    PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pContext, pSurface);
    44134576                    AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
    44144577
     
    48845047            uVertexMinOffset = pVertexDecl[iVertex].array.offset;
    48855048
     5049        /* pVertexSurface->u. is an union, so pVertexSurface->u.pIndexBuffer and pVertexSurface->u.pVertexBuffer are the same */
     5050        if (   pVertexSurface->u.pVertexBuffer
     5051            && !(pVertexSurface->fu32ActualUsageFlags & SVGA3D_SURFACE_HINT_VERTEXBUFFER))
     5052        {
     5053            /* The buffer object is not an vertex one. Switch type. */
     5054            Assert(pVertexSurface->fu32ActualUsageFlags & SVGA3D_SURFACE_HINT_INDEXBUFFER);
     5055            pVertexSurface->fu32ActualUsageFlags &= ~SVGA3D_SURFACE_HINT_INDEXBUFFER;
     5056
     5057            D3D_RELEASE(pVertexSurface->u.pIndexBuffer);
     5058            LogFunc(("index -> vertex buffer sid=%x\n", sidVertex));
     5059        }
     5060
    48865061        if (!pVertexSurface->u.pVertexBuffer)
    48875062        {
     
    49165091            pVertexSurface->fu32ActualUsageFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
    49175092        }
    4918         else
    4919             Assert(pVertexSurface->fDirty == false);
    49205093    }
    49215094
     
    50855258            Log(("vmsvga3dDrawPrimitives: index sid=%x\n", sidIndex));
    50865259
     5260            /* pIndexSurface->u. is an union, so pIndexSurface->u.pIndexBuffer and pIndexSurface->u.pVertexBuffer are the same */
     5261            if (   pIndexSurface->u.pIndexBuffer
     5262                && !(pIndexSurface->fu32ActualUsageFlags & SVGA3D_SURFACE_HINT_INDEXBUFFER))
     5263            {
     5264                /* The buffer object is not an index one. Switch type. */
     5265                Assert(pIndexSurface->fu32ActualUsageFlags & SVGA3D_SURFACE_HINT_VERTEXBUFFER);
     5266                pIndexSurface->fu32ActualUsageFlags &= ~SVGA3D_SURFACE_HINT_VERTEXBUFFER;
     5267
     5268                D3D_RELEASE(pIndexSurface->u.pVertexBuffer);
     5269                LogFunc(("vertex -> index buffer sid=%x\n", sidIndex));
     5270            }
     5271
    50875272            if (!pIndexSurface->u.pIndexBuffer)
    50885273            {
     
    51225307                pIndexSurface->fu32ActualUsageFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
    51235308            }
    5124             else
    5125                 Assert(pIndexSurface->fDirty == false);
    51265309
    51275310            hr = pContext->pDevice->SetIndices(pIndexSurface->u.pIndexBuffer);
     
    51835366        }
    51845367    }
    5185     pVertexDeclD3D->Release();
     5368    D3D_RELEASE(pVertexDeclD3D);
    51865369    RTMemFree(pVertexElement);
    51875370
     
    52175400        AssertMsgReturn(hr == S_OK, ("vmsvga3dSurfaceFinishDrawing: GetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
    52185401
    5219         pQuery->Release();
     5402        D3D_RELEASE(pQuery);
    52205403    }
    52215404#endif
     
    52395422
    52405423internal_error:
    5241     if (pVertexDeclD3D)
    5242         pVertexDeclD3D->Release();
     5424    D3D_RELEASE(pVertexDeclD3D);
    52435425    if (pVertexElement)
    52445426        RTMemFree(pVertexElement);
     
    52935475
    52945476    Log(("vmsvga3dShaderDefine %x shid=%x type=%s cbData=%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
    5295     Log3(("shader code:\n%.*Rhxd\n", cbData, pShaderData));
     5477#ifdef LOG_ENABLED
     5478    Log3(("Shader code:\n"));
     5479    const uint32_t cTokensPerLine = 8;
     5480    const uint32_t *paTokens = (uint32_t *)pShaderData;
     5481    const uint32_t cTokens = cbData / sizeof(uint32_t);
     5482    for (uint32_t iToken = 0; iToken < cTokens; ++iToken)
     5483    {
     5484        if ((iToken % cTokensPerLine) == 0)
     5485        {
     5486            if (iToken == 0)
     5487                Log3(("%08X", paTokens[iToken]));
     5488            else
     5489                Log3(("\n%08X", paTokens[iToken]));
     5490        }
     5491        else
     5492            Log3((" %08X", paTokens[iToken]));
     5493    }
     5494    Log3(("\n"));
     5495#endif
    52965496
    52975497    if (    cid >= pState->cContexts
     
    53575557    {
    53585558        Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
    5359         pDisassembly->Release();
     5559        D3D_RELEASE(pDisassembly);
    53605560    }
    53615561#endif
     
    54085608        {
    54095609            pShader = &pContext->paVertexShader[shid];
    5410             pShader->u.pVertexShader->Release();
     5610            D3D_RELEASE(pShader->u.pVertexShader);
    54115611        }
    54125612    }
     
    54185618        {
    54195619            pShader = &pContext->paPixelShader[shid];
    5420             pShader->u.pPixelShader->Release();
     5620            D3D_RELEASE(pShader->u.pPixelShader);
    54215621        }
    54225622    }
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette