VirtualBox

Changeset 91361 in vbox for trunk/src/VBox/Devices


Ignore:
Timestamp:
Sep 24, 2021 12:48:04 PM (3 years ago)
Author:
vboxsync
Message:

Devices/Graphics: VMSVGA new commands

Location:
trunk/src/VBox/Devices
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA-cmd.cpp

    r89356 r91361  
    942942
    943943
    944 int vmsvgaR3UpdateGBSurface(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dSurfaceImageId const *pImageId, SVGA3dBox const *pBox)
     944int vmsvgaR3UpdateGBSurface(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImageId, SVGA3dBox const *pBox)
    945945{
    946946    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     
    955955        {
    956956            VMSVGA3D_MAPPED_SURFACE map;
    957             rc = pSvgaR3State->pFuncsMap->pfnSurfaceMap(pThisCC, idDXContext, pImageId, pBox, VMSVGA3D_SURFACE_MAP_WRITE_DISCARD, &map);
     957            rc = vmsvga3dSurfaceMap(pThisCC, pImageId, pBox, VMSVGA3D_SURFACE_MAP_WRITE_DISCARD, &map);
    958958            if (RT_SUCCESS(rc))
    959959            {
     
    979979                }
    980980
    981                 pSvgaR3State->pFuncsMap->pfnSurfaceUnmap(pThisCC, pImageId, &map, /* fWritten= */ true);
     981                vmsvga3dSurfaceUnmap(pThisCC, pImageId, &map, /* fWritten= */ true);
    982982            }
    983983        }
     
    990990
    991991
    992 int vmsvgaR3UpdateGBSurfaceEx(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dSurfaceImageId const *pImageId, SVGA3dBox const *pBoxDst, SVGA3dPoint const *pPtSrc)
     992int vmsvgaR3UpdateGBSurfaceEx(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImageId, SVGA3dBox const *pBoxDst, SVGA3dPoint const *pPtSrc)
    993993{
    994994    /* pPtSrc must be verified by the caller. */
     
    10041004        {
    10051005            VMSVGA3D_MAPPED_SURFACE map;
    1006             rc = pSvgaR3State->pFuncsMap->pfnSurfaceMap(pThisCC, idDXContext, pImageId, pBoxDst, VMSVGA3D_SURFACE_MAP_WRITE_DISCARD, &map);
     1006            rc = vmsvga3dSurfaceMap(pThisCC, pImageId, pBoxDst, VMSVGA3D_SURFACE_MAP_WRITE_DISCARD, &map);
    10071007            if (RT_SUCCESS(rc))
    10081008            {
     
    10281028                }
    10291029
    1030                 pSvgaR3State->pFuncsMap->pfnSurfaceUnmap(pThisCC, pImageId, &map, /* fWritten= */ true);
     1030                vmsvga3dSurfaceUnmap(pThisCC, pImageId, &map, /* fWritten= */ true);
    10311031            }
    10321032        }
     
    12811281    vmsvga3dSurfaceDefine(pThisCC, pCmd->sid, pCmd->surfaceFlags, pCmd->format,
    12821282                          pCmd->multisampleCount, pCmd->autogenFilter,
    1283                           pCmd->face[0].numMipLevels, &paMipLevelSizes[0]);
     1283                          pCmd->face[0].numMipLevels, &paMipLevelSizes[0], /* fAllocMipLevels = */ true);
    12841284}
    12851285
     
    13511351    {
    13521352        /* Create the host surface. */
    1353         /** @todo fGBO = true flag. */
    13541353        vmsvga3dSurfaceDefine(pThisCC, pCmd->sid, pCmd->surfaceFlags, pCmd->format,
    13551354                              pCmd->multisampleCount, pCmd->autogenFilter,
    1356                               pCmd->numMipLevels, &pCmd->size);
     1355                              pCmd->numMipLevels, &pCmd->size, /* fAllocMipLevels = */ false);
    13571356    }
    13581357}
     
    14691468
    14701469
     1470static int vmsvgaR3TransferSurfaceLevel(PVGASTATECC pThisCC,
     1471                                        PVMSVGAMOB pMob,
     1472                                        SVGA3dSurfaceImageId const *pImage,
     1473                                        SVGA3dBox const *pBox,
     1474                                        SVGA3dTransferType enmTransfer)
     1475{
     1476    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     1477
     1478    VMSVGA3D_SURFACE_MAP enmMapType;
     1479    if (enmTransfer == SVGA3D_WRITE_HOST_VRAM)
     1480        enmMapType = VMSVGA3D_SURFACE_MAP_WRITE_DISCARD;
     1481    else if (enmTransfer == SVGA3D_READ_HOST_VRAM)
     1482        enmMapType = VMSVGA3D_SURFACE_MAP_READ;
     1483    else
     1484        AssertFailedReturn(VERR_INVALID_PARAMETER);
     1485
     1486    VMSGA3D_BOX_DIMENSIONS dims;
     1487    int rc = vmsvga3dGetBoxDimensions(pThisCC, pImage, pBox, &dims);
     1488    AssertRCReturn(rc, rc);
     1489
     1490    VMSVGA3D_MAPPED_SURFACE map;
     1491    rc = vmsvga3dSurfaceMap(pThisCC, pImage, pBox, enmMapType, &map);
     1492    if (RT_SUCCESS(rc))
     1493    {
     1494        /* Copy mapped surface <-> MOB. */
     1495        uint8_t *pu8Map = (uint8_t *)map.pvData;
     1496        uint32_t offMob = dims.offSubresource + dims.offBox;
     1497        for (uint32_t z = 0; z < dims.cDepth; ++z)
     1498        {
     1499            for (uint32_t y = 0; y < dims.cyBlocks; ++y)
     1500            {
     1501                if (enmTransfer == SVGA3D_READ_HOST_VRAM)
     1502                    rc = vmsvgaR3GboWrite(pSvgaR3State, &pMob->Gbo, offMob, pu8Map, dims.cbRow);
     1503                else
     1504                    rc = vmsvgaR3GboRead(pSvgaR3State, &pMob->Gbo, offMob, pu8Map, dims.cbRow);
     1505                if (RT_FAILURE(rc))
     1506                    break;
     1507
     1508                pu8Map += map.cbRowPitch;
     1509                offMob += dims.cbPitch;
     1510            }
     1511        }
     1512
     1513        // vmsvga3dMapWriteBmpFile(&map, "Dynamic");
     1514
     1515        bool const fWritten = (enmTransfer == SVGA3D_WRITE_HOST_VRAM);
     1516        vmsvga3dSurfaceUnmap(pThisCC, pImage, &map, fWritten);
     1517    }
     1518
     1519    return rc;
     1520}
     1521
     1522
    14711523/* SVGA_3D_CMD_UPDATE_GB_IMAGE 1101 */
    1472 static void vmsvga3dCmdUpdateGBImage(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdUpdateGBImage const *pCmd)
    1473 {
    1474 //     ASMBreakpoint();
     1524static void vmsvga3dCmdUpdateGBImage(PVGASTATECC pThisCC, SVGA3dCmdUpdateGBImage const *pCmd)
     1525{
     1526//ASMBreakpoint();
    14751527    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    14761528
    14771529    LogFlowFunc(("sid=%u @%u,%u,%u %ux%ux%u\n",
    14781530                 pCmd->image.sid, pCmd->box.x, pCmd->box.y, pCmd->box.z, pCmd->box.w, pCmd->box.h, pCmd->box.d));
     1531
     1532/*
     1533   SVGA3dSurfaceFormat format;
     1534   SVGA3dSurface1Flags surface1Flags;
     1535   uint32 numMipLevels;
     1536   uint32 multisampleCount;
     1537   SVGA3dTextureFilter autogenFilter;
     1538   SVGA3dSize size;
     1539   SVGAMobId mobid;
     1540   uint32 arraySize;
     1541   uint32 mobPitch;
     1542   SVGA3dSurface2Flags surface2Flags;
     1543   uint8 multisamplePattern;
     1544   uint8 qualityLevel;
     1545   uint16 bufferByteStride;
     1546   float minLOD;
     1547*/
    14791548
    14801549    /* "update a surface from its backing MOB." */
     
    14871556        if (pMob)
    14881557        {
    1489             VMSVGA3D_MAPPED_SURFACE map;
    1490             /** @todo vmsvga3dSurfaceMap */
    1491             rc = pSvgaR3State->pFuncsMap->pfnSurfaceMap(pThisCC, idDXContext, &pCmd->image, &pCmd->box, VMSVGA3D_SURFACE_MAP_WRITE_DISCARD, &map);
    1492             if (RT_SUCCESS(rc))
     1558            rc = vmsvgaR3TransferSurfaceLevel(pThisCC, pMob, &pCmd->image, &pCmd->box, SVGA3D_WRITE_HOST_VRAM);
     1559            AssertRC(rc);
     1560        }
     1561    }
     1562}
     1563
     1564
     1565/* SVGA_3D_CMD_UPDATE_GB_SURFACE 1102 */
     1566static void vmsvga3dCmdUpdateGBSurface(PVGASTATECC pThisCC, SVGA3dCmdUpdateGBSurface const *pCmd)
     1567{
     1568//ASMBreakpoint();
     1569    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     1570
     1571    LogFlowFunc(("sid=%u\n",
     1572                 pCmd->sid));
     1573
     1574    /* "update a surface from its backing MOB." */
     1575    SVGAOTableSurfaceEntry entrySurface;
     1576    int rc = vmsvgaR3OTableRead(pSvgaR3State, &pSvgaR3State->aGboOTables[SVGA_OTABLE_SURFACE],
     1577                            pCmd->sid, SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, &entrySurface, sizeof(entrySurface));
     1578    if (RT_SUCCESS(rc))
     1579    {
     1580        PVMSVGAMOB pMob = vmsvgaR3MobGet(pSvgaR3State, entrySurface.mobid);
     1581        if (pMob)
     1582        {
     1583            uint32 const arraySize = (entrySurface.surface1Flags & SVGA3D_SURFACE_CUBEMAP)
     1584                                   ? SVGA3D_MAX_SURFACE_FACES
     1585                                   : RT_MAX(entrySurface.arraySize, 1);
     1586            for (uint32_t iArray = 0; iArray < arraySize; ++iArray)
    14931587            {
    1494                 /* Copy MOB -> mapped surface. */
    1495                 uint32_t offSrc = pCmd->box.x * map.cbPixel
    1496                                 + pCmd->box.y * entrySurface.size.width * map.cbPixel
    1497                                 + pCmd->box.z * entrySurface.size.height * entrySurface.size.width * map.cbPixel;
    1498                 uint8_t *pu8Dst = (uint8_t *)map.pvData;
    1499                 for (uint32_t z = 0; z < pCmd->box.d; ++z)
     1588                for (uint32_t iMipmap = 0; iMipmap < entrySurface.numMipLevels; ++iMipmap)
    15001589                {
    1501                     for (uint32_t y = 0; y < pCmd->box.h; ++y)
    1502                     {
    1503                         rc = vmsvgaR3GboRead(pSvgaR3State, &pMob->Gbo, offSrc, pu8Dst, pCmd->box.w * map.cbPixel);
    1504                         if (RT_FAILURE(rc))
    1505                             break;
    1506 
    1507                         pu8Dst += map.cbRowPitch;
    1508                         offSrc += entrySurface.size.width * map.cbPixel;
    1509                     }
    1510 
    1511                     pu8Dst += map.cbDepthPitch;
    1512                     offSrc += entrySurface.size.height * entrySurface.size.width * map.cbPixel;
     1590                    SVGA3dSurfaceImageId image;
     1591                    image.sid = pCmd->sid;
     1592                    image.face = iArray;
     1593                    image.mipmap = iMipmap;
     1594
     1595                    rc = vmsvgaR3TransferSurfaceLevel(pThisCC, pMob, &image, /* all pBox = */ NULL, SVGA3D_WRITE_HOST_VRAM);
     1596                    AssertRCBreak(rc);
    15131597                }
    1514 
    1515                 // vmsvga3dMapWriteBmpFile(&map, "Dynamic");
    1516 
    1517                 pSvgaR3State->pFuncsMap->pfnSurfaceUnmap(pThisCC, &pCmd->image, &map, /* fWritten =  */true);
     1598            }
     1599        }
     1600    }
     1601}
     1602
     1603
     1604/* SVGA_3D_CMD_READBACK_GB_IMAGE 1103 */
     1605static void vmsvga3dCmdReadbackGBImage(PVGASTATECC pThisCC, SVGA3dCmdReadbackGBImage const *pCmd)
     1606{
     1607//ASMBreakpoint();
     1608    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     1609
     1610    LogFlowFunc(("sid=%u, face=%u, mipmap=%u\n",
     1611                 pCmd->image.sid, pCmd->image.face, pCmd->image.mipmap));
     1612
     1613    /* Read a surface to its backing MOB. */
     1614    SVGAOTableSurfaceEntry entrySurface;
     1615    int rc = vmsvgaR3OTableRead(pSvgaR3State, &pSvgaR3State->aGboOTables[SVGA_OTABLE_SURFACE],
     1616                            pCmd->image.sid, SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, &entrySurface, sizeof(entrySurface));
     1617    if (RT_SUCCESS(rc))
     1618    {
     1619        PVMSVGAMOB pMob = vmsvgaR3MobGet(pSvgaR3State, entrySurface.mobid);
     1620        if (pMob)
     1621        {
     1622            rc = vmsvgaR3TransferSurfaceLevel(pThisCC, pMob, &pCmd->image, /* all pBox = */ NULL, SVGA3D_READ_HOST_VRAM);
     1623            AssertRC(rc);
     1624        }
     1625    }
     1626}
     1627
     1628
     1629/* SVGA_3D_CMD_READBACK_GB_SURFACE 1104 */
     1630static void vmsvga3dCmdReadbackGBSurface(PVGASTATECC pThisCC, SVGA3dCmdReadbackGBSurface const *pCmd)
     1631{
     1632//ASMBreakpoint();
     1633    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     1634
     1635    LogFlowFunc(("sid=%u\n",
     1636                 pCmd->sid));
     1637
     1638    /* Read a surface to its backing MOB. */
     1639    SVGAOTableSurfaceEntry entrySurface;
     1640    int rc = vmsvgaR3OTableRead(pSvgaR3State, &pSvgaR3State->aGboOTables[SVGA_OTABLE_SURFACE],
     1641                            pCmd->sid, SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, &entrySurface, sizeof(entrySurface));
     1642    if (RT_SUCCESS(rc))
     1643    {
     1644        PVMSVGAMOB pMob = vmsvgaR3MobGet(pSvgaR3State, entrySurface.mobid);
     1645        if (pMob)
     1646        {
     1647            uint32 const arraySize = (entrySurface.surface1Flags & SVGA3D_SURFACE_CUBEMAP)
     1648                                   ? SVGA3D_MAX_SURFACE_FACES
     1649                                   : RT_MAX(entrySurface.arraySize, 1);
     1650            for (uint32_t iArray = 0; iArray < arraySize; ++iArray)
     1651            {
     1652                for (uint32_t iMipmap = 0; iMipmap < entrySurface.numMipLevels; ++iMipmap)
     1653                {
     1654                    SVGA3dSurfaceImageId image;
     1655                    image.sid = pCmd->sid;
     1656                    image.face = iArray;
     1657                    image.mipmap = iMipmap;
     1658
     1659                    rc = vmsvgaR3TransferSurfaceLevel(pThisCC, pMob, &image, /* all pBox = */ NULL, SVGA3D_READ_HOST_VRAM);
     1660                    AssertRCBreak(rc);
     1661                }
    15181662            }
    15191663        }
     
    17421886                        /* Copy the screen target surface to the memory buffer. */
    17431887                        VMSVGA3D_MAPPED_SURFACE map;
    1744                         rc = pSvgaR3State->pFuncsMap->pfnSurfaceMap(pThisCC, SVGA_ID_INVALID, &entryScreenTarget.image, NULL, VMSVGA3D_SURFACE_MAP_READ, &map);
     1888                        rc = vmsvga3dSurfaceMap(pThisCC, &entryScreenTarget.image, NULL, VMSVGA3D_SURFACE_MAP_READ, &map);
    17451889                        if (RT_SUCCESS(rc))
    17461890                        {
     
    17591903                            }
    17601904
    1761                             pSvgaR3State->pFuncsMap->pfnSurfaceUnmap(pThisCC, &entryScreenTarget.image, &map, /* fWritten =  */ false);
     1905                            vmsvga3dSurfaceUnmap(pThisCC, &entryScreenTarget.image, &map, /* fWritten =  */ false);
    17621906
    17631907                            vmsvgaR3UpdateScreen(pThisCC, pScreen, pCmd->rect.x, pCmd->rect.y, pCmd->rect.w, pCmd->rect.h);
     
    17911935    entry.arraySize = pCmd->arraySize;
    17921936    // entry.mobPitch = 0;
     1937    // ...
    17931938    int rc = vmsvgaR3OTableWrite(pSvgaR3State, &pSvgaR3State->aGboOTables[SVGA_OTABLE_SURFACE],
    17941939                                 pCmd->sid, SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, &entry, sizeof(entry));
     
    17961941    {
    17971942        /* Create the host surface. */
    1798         /** @todo fGBO = true flag. */
     1943        /** @todo SVGAOTableSurfaceEntry as input parameter? */
    17991944        vmsvga3dSurfaceDefine(pThisCC, pCmd->sid, pCmd->surfaceFlags, pCmd->format,
    18001945                              pCmd->multisampleCount, pCmd->autogenFilter,
    1801                               pCmd->numMipLevels, &pCmd->size);
     1946                              pCmd->numMipLevels, &pCmd->size, /* fAllocMipLevels = */ false);
    18021947    }
    18031948}
     
    18071952static void vmsvga3dCmdDefineGBMob64(PVGASTATECC pThisCC, SVGA3dCmdDefineGBMob64 const *pCmd)
    18081953{
    1809 //     ASMBreakpoint();
     1954//ASMBreakpoint();
    18101955    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    18111956
     
    19652110
    19662111/* SVGA_3D_CMD_DX_READBACK_CONTEXT 1146 */
    1967 static int vmsvga3dCmdDXReadbackContext(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXReadbackContext const *pCmd, uint32_t cbCmd)
    1968 {
    1969 #ifdef VMSVGA3D_DX
    1970 ASMBreakpoint();
    1971     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    1972     RT_NOREF(pSvgaR3State, pCmd, cbCmd);
    1973     return vmsvga3dDXReadbackContext(pThisCC, idDXContext);
    1974 #else
    1975     RT_NOREF(pThisCC, idDXContext, pCmd, cbCmd);
     2112static int vmsvga3dCmdDXReadbackContext(PVGASTATECC pThisCC, SVGA3dCmdDXReadbackContext const *pCmd, uint32_t cbCmd)
     2113{
     2114#ifdef VMSVGA3D_DX
     2115//ASMBreakpoint();
     2116    RT_NOREF(cbCmd);
     2117
     2118    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     2119
     2120    /* "Request that the device flush the contents back into guest memory." */
     2121    SVGAOTableDXContextEntry entry;
     2122    int rc = vmsvgaR3OTableRead(pSvgaR3State, &pSvgaR3State->aGboOTables[SVGA_OTABLE_DXCONTEXT],
     2123                                pCmd->cid, sizeof(SVGAOTableDXContextEntry), &entry, sizeof(entry));
     2124    if (RT_SUCCESS(rc))
     2125    {
     2126        if (entry.mobid != SVGA_ID_INVALID)
     2127        {
     2128            PVMSVGAMOB pMob = vmsvgaR3MobGet(pSvgaR3State, entry.mobid);
     2129            if (pMob)
     2130            {
     2131                /* Get the content. */
     2132                SVGADXContextMobFormat *pSvgaDXContext = (SVGADXContextMobFormat *)RTMemAlloc(sizeof(SVGADXContextMobFormat));
     2133                if (pSvgaDXContext)
     2134                {
     2135                    rc = vmsvga3dDXReadbackContext(pThisCC, pCmd->cid, pSvgaDXContext);
     2136                    if (RT_SUCCESS(rc))
     2137                        rc = vmsvgaR3GboWrite(pSvgaR3State, &pMob->Gbo, 0, pSvgaDXContext, sizeof(SVGADXContextMobFormat));
     2138
     2139                    RTMemFree(pSvgaDXContext);
     2140                }
     2141                else
     2142                    rc = VERR_NO_MEMORY;
     2143            }
     2144        }
     2145    }
     2146
     2147    return rc;
     2148#else
     2149    RT_NOREF(pThisCC, pCmd, cbCmd);
    19762150    return VERR_NOT_SUPPORTED;
    19772151#endif
     
    20842258{
    20852259#ifdef VMSVGA3D_DX
    2086 ASMBreakpoint();
    2087     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    2088     RT_NOREF(pSvgaR3State, pCmd, cbCmd);
    2089     return vmsvga3dDXDrawInstanced(pThisCC, idDXContext);
     2260//ASMBreakpoint();
     2261    RT_NOREF(cbCmd);
     2262    return vmsvga3dDXDrawInstanced(pThisCC, idDXContext, pCmd);
    20902263#else
    20912264    RT_NOREF(pThisCC, idDXContext, pCmd, cbCmd);
     
    20992272{
    21002273#ifdef VMSVGA3D_DX
    2101 ASMBreakpoint();
    2102     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    2103     RT_NOREF(pSvgaR3State, pCmd, cbCmd);
    2104     return vmsvga3dDXDrawIndexedInstanced(pThisCC, idDXContext);
     2274//ASMBreakpoint();
     2275    RT_NOREF(cbCmd);
     2276    return vmsvga3dDXDrawIndexedInstanced(pThisCC, idDXContext, pCmd);
    21052277#else
    21062278    RT_NOREF(pThisCC, idDXContext, pCmd, cbCmd);
     
    24912663
    24922664/* SVGA_3D_CMD_DX_UPDATE_SUBRESOURCE 1182 */
    2493 static int vmsvga3dCmdDXUpdateSubResource(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXUpdateSubResource const *pCmd, uint32_t cbCmd)
    2494 {
    2495 #ifdef VMSVGA3D_DX
    2496 ASMBreakpoint();
    2497     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    2498     RT_NOREF(pSvgaR3State, pCmd, cbCmd);
    2499     return vmsvga3dDXUpdateSubResource(pThisCC, idDXContext);
    2500 #else
    2501     RT_NOREF(pThisCC, idDXContext, pCmd, cbCmd);
     2665static int vmsvga3dCmdDXUpdateSubResource(PVGASTATECC pThisCC, SVGA3dCmdDXUpdateSubResource const *pCmd, uint32_t cbCmd)
     2666{
     2667#ifdef VMSVGA3D_DX
     2668//ASMBreakpoint();
     2669    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     2670    RT_NOREF(cbCmd);
     2671
     2672    LogFlowFunc(("sid=%u, subResource=%u, box=%d,%d,%d %ux%ux%u\n",
     2673                 pCmd->sid, pCmd->subResource, pCmd->box.x, pCmd->box.y, pCmd->box.z, pCmd->box.w, pCmd->box.h, pCmd->box.z));
     2674
     2675    /* "Inform the device that the guest-contents have been updated." */
     2676    SVGAOTableSurfaceEntry entrySurface;
     2677    int rc = vmsvgaR3OTableRead(pSvgaR3State, &pSvgaR3State->aGboOTables[SVGA_OTABLE_SURFACE],
     2678                                pCmd->sid, SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, &entrySurface, sizeof(entrySurface));
     2679    if (RT_SUCCESS(rc))
     2680    {
     2681        PVMSVGAMOB pMob = vmsvgaR3MobGet(pSvgaR3State, entrySurface.mobid);
     2682        if (pMob)
     2683        {
     2684            uint32 const arraySize = (entrySurface.surface1Flags & SVGA3D_SURFACE_CUBEMAP)
     2685                                   ? SVGA3D_MAX_SURFACE_FACES
     2686                                   : RT_MAX(entrySurface.arraySize, 1);
     2687            ASSERT_GUEST_RETURN(pCmd->subResource < arraySize * entrySurface.numMipLevels, VERR_INVALID_PARAMETER);
     2688            /* pCmd->box will be verified by the mapping function. */
     2689            RT_UNTRUSTED_VALIDATED_FENCE();
     2690
     2691            /** @todo Mapping functions should use subresource index rather than SVGA3dSurfaceImageId? */
     2692            SVGA3dSurfaceImageId image;
     2693            image.sid = pCmd->sid;
     2694            vmsvga3dCalcMipmapAndFace(entrySurface.numMipLevels, pCmd->subResource, &image.mipmap, &image.face);
     2695
     2696            rc = vmsvgaR3TransferSurfaceLevel(pThisCC, pMob, &image, &pCmd->box, SVGA3D_WRITE_HOST_VRAM);
     2697            AssertRC(rc);
     2698        }
     2699    }
     2700
     2701    return rc;
     2702#else
     2703    RT_NOREF(pThisCC, pCmd, cbCmd);
    25022704    return VERR_NOT_SUPPORTED;
    25032705#endif
     
    25062708
    25072709/* SVGA_3D_CMD_DX_READBACK_SUBRESOURCE 1183 */
    2508 static int vmsvga3dCmdDXReadbackSubResource(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXReadbackSubResource const *pCmd, uint32_t cbCmd)
    2509 {
    2510 #ifdef VMSVGA3D_DX
    2511 ASMBreakpoint();
    2512     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    2513     RT_NOREF(pSvgaR3State, pCmd, cbCmd);
    2514     return vmsvga3dDXReadbackSubResource(pThisCC, idDXContext);
    2515 #else
    2516     RT_NOREF(pThisCC, idDXContext, pCmd, cbCmd);
     2710static int vmsvga3dCmdDXReadbackSubResource(PVGASTATECC pThisCC, SVGA3dCmdDXReadbackSubResource const *pCmd, uint32_t cbCmd)
     2711{
     2712#ifdef VMSVGA3D_DX
     2713//ASMBreakpoint();
     2714    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     2715    RT_NOREF(cbCmd);
     2716
     2717    LogFlowFunc(("sid=%u, subResource=%u\n",
     2718                 pCmd->sid, pCmd->subResource));
     2719
     2720    /* "Request the device to flush the dirty contents into the guest." */
     2721    SVGAOTableSurfaceEntry entrySurface;
     2722    int rc = vmsvgaR3OTableRead(pSvgaR3State, &pSvgaR3State->aGboOTables[SVGA_OTABLE_SURFACE],
     2723                                pCmd->sid, SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, &entrySurface, sizeof(entrySurface));
     2724    if (RT_SUCCESS(rc))
     2725    {
     2726        PVMSVGAMOB pMob = vmsvgaR3MobGet(pSvgaR3State, entrySurface.mobid);
     2727        if (pMob)
     2728        {
     2729            uint32 const arraySize = (entrySurface.surface1Flags & SVGA3D_SURFACE_CUBEMAP)
     2730                                   ? SVGA3D_MAX_SURFACE_FACES
     2731                                   : RT_MAX(entrySurface.arraySize, 1);
     2732            ASSERT_GUEST_RETURN(pCmd->subResource < arraySize * entrySurface.numMipLevels, VERR_INVALID_PARAMETER);
     2733            RT_UNTRUSTED_VALIDATED_FENCE();
     2734
     2735            /** @todo Mapping functions should use subresource index rather than SVGA3dSurfaceImageId? */
     2736            SVGA3dSurfaceImageId image;
     2737            image.sid = pCmd->sid;
     2738            vmsvga3dCalcMipmapAndFace(entrySurface.numMipLevels, pCmd->subResource, &image.mipmap, &image.face);
     2739
     2740            rc = vmsvgaR3TransferSurfaceLevel(pThisCC, pMob, &image, /* all pBox = */ NULL, SVGA3D_READ_HOST_VRAM);
     2741            AssertRC(rc);
     2742        }
     2743    }
     2744
     2745    return rc;
     2746#else
     2747    RT_NOREF(pThisCC, pCmd, cbCmd);
    25172748    return VERR_NOT_SUPPORTED;
    25182749#endif
     
    25212752
    25222753/* SVGA_3D_CMD_DX_INVALIDATE_SUBRESOURCE 1184 */
    2523 static int vmsvga3dCmdDXInvalidateSubResource(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXInvalidateSubResource const *pCmd, uint32_t cbCmd)
    2524 {
    2525 #ifdef VMSVGA3D_DX
    2526 ASMBreakpoint();
    2527     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    2528     RT_NOREF(pSvgaR3State, pCmd, cbCmd);
    2529     return vmsvga3dDXInvalidateSubResource(pThisCC, idDXContext);
    2530 #else
    2531     RT_NOREF(pThisCC, idDXContext, pCmd, cbCmd);
     2754static int vmsvga3dCmdDXInvalidateSubResource(PVGASTATECC pThisCC, SVGA3dCmdDXInvalidateSubResource const *pCmd, uint32_t cbCmd)
     2755{
     2756#ifdef VMSVGA3D_DX
     2757ASMBreakpoint();
     2758    PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     2759    RT_NOREF(cbCmd);
     2760
     2761    LogFlowFunc(("sid=%u, subResource=%u\n",
     2762                 pCmd->sid, pCmd->subResource));
     2763
     2764    /* "Notify the device that the contents can be lost." */
     2765    SVGAOTableSurfaceEntry entrySurface;
     2766    int rc = vmsvgaR3OTableRead(pSvgaR3State, &pSvgaR3State->aGboOTables[SVGA_OTABLE_SURFACE],
     2767                                pCmd->sid, SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, &entrySurface, sizeof(entrySurface));
     2768    if (RT_SUCCESS(rc))
     2769    {
     2770        uint32_t iFace;
     2771        uint32_t iMipmap;
     2772        vmsvga3dCalcMipmapAndFace(entrySurface.numMipLevels, pCmd->subResource, &iMipmap, &iFace);
     2773        vmsvga3dSurfaceInvalidate(pThisCC, pCmd->sid, iFace, iMipmap);
     2774    }
     2775
     2776    return rc;
     2777#else
     2778    RT_NOREF(pThisCC, pCmd, cbCmd);
    25322779    return VERR_NOT_SUPPORTED;
    25332780#endif
     
    29123159
    29133160/* SVGA_3D_CMD_DX_TRANSFER_FROM_BUFFER 1210 */
    2914 static int vmsvga3dCmdDXTransferFromBuffer(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXTransferFromBuffer const *pCmd, uint32_t cbCmd)
    2915 {
    2916 #ifdef VMSVGA3D_DX
    2917 ASMBreakpoint();
    2918     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    2919     RT_NOREF(pSvgaR3State, pCmd, cbCmd);
    2920     return vmsvga3dDXTransferFromBuffer(pThisCC, idDXContext);
    2921 #else
    2922     RT_NOREF(pThisCC, idDXContext, pCmd, cbCmd);
     3161static int vmsvga3dCmdDXTransferFromBuffer(PVGASTATECC pThisCC, SVGA3dCmdDXTransferFromBuffer const *pCmd, uint32_t cbCmd)
     3162{
     3163#ifdef VMSVGA3D_DX
     3164//ASMBreakpoint();
     3165    RT_NOREF(cbCmd);
     3166
     3167    /* Plan:
     3168     * - map the buffer;
     3169     * - map the surface;
     3170     * - copy from buffer map to the surface map.
     3171     */
     3172
     3173    int rc;
     3174
     3175    SVGA3dSurfaceImageId imageBuffer;
     3176    imageBuffer.sid = pCmd->srcSid;
     3177    imageBuffer.face = 0;
     3178    imageBuffer.mipmap = 0;
     3179
     3180    SVGA3dSurfaceImageId imageSurface;
     3181    imageSurface.sid = pCmd->destSid;
     3182    rc = vmsvga3dCalcSurfaceMipmapAndFace(pThisCC, pCmd->destSid, pCmd->destSubResource, &imageSurface.mipmap, &imageSurface.face);
     3183    AssertRCReturn(rc, rc);
     3184
     3185    /*
     3186     * Map the buffer.
     3187     */
     3188    VMSVGA3D_MAPPED_SURFACE mapBuffer;
     3189    rc = vmsvga3dSurfaceMap(pThisCC, &imageBuffer, NULL, VMSVGA3D_SURFACE_MAP_READ, &mapBuffer);
     3190    if (RT_SUCCESS(rc))
     3191    {
     3192        /*
     3193         * Map the surface.
     3194         */
     3195        VMSVGA3D_MAPPED_SURFACE mapSurface;
     3196        rc = vmsvga3dSurfaceMap(pThisCC, &imageSurface, &pCmd->destBox, VMSVGA3D_SURFACE_MAP_WRITE_DISCARD, &mapSurface);
     3197        if (RT_SUCCESS(rc))
     3198        {
     3199            /*
     3200             * Copy the mapped buffer to the surface.
     3201             */
     3202            uint8_t const *pu8Buffer = (uint8_t *)mapBuffer.pvData;
     3203            uint32_t const cbBuffer = mapBuffer.box.w * mapBuffer.cbPixel;
     3204
     3205            if (pCmd->srcOffset <= cbBuffer)
     3206            {
     3207                RT_UNTRUSTED_VALIDATED_FENCE();
     3208                uint8_t const *pu8BufferBegin = pu8Buffer;
     3209                uint8_t const *pu8BufferEnd = pu8Buffer + (cbBuffer - pCmd->srcOffset);
     3210
     3211                pu8Buffer += pCmd->srcOffset;
     3212
     3213                uint8_t *pu8Surface = (uint8_t *)mapSurface.pvData;
     3214
     3215                uint32_t const cbWidth = mapSurface.box.w * mapSurface.cbPixel;
     3216                for (uint32_t z = 0; z < mapSurface.box.d && RT_SUCCESS(rc); ++z)
     3217                {
     3218                    uint8_t const *pu8BufferRow = pu8Buffer;
     3219                    uint8_t *pu8SurfaceRow = pu8Surface;
     3220                    for (uint32_t y = 0; y < mapSurface.box.h; ++y)
     3221                    {
     3222                        ASSERT_GUEST_STMT_BREAK(   (uintptr_t)pu8BufferRow >= (uintptr_t)pu8BufferBegin
     3223                                                && (uintptr_t)pu8BufferRow < (uintptr_t)pu8BufferEnd
     3224                                                && (uintptr_t)(pu8BufferRow + cbWidth) > (uintptr_t)pu8BufferBegin
     3225                                                && (uintptr_t)(pu8BufferRow + cbWidth) <= (uintptr_t)pu8BufferEnd,
     3226                                                rc = VERR_INVALID_PARAMETER);
     3227
     3228                        memcpy(pu8SurfaceRow, pu8BufferRow, cbWidth);
     3229
     3230                        pu8SurfaceRow += mapSurface.cbRowPitch;
     3231                        pu8BufferRow += pCmd->srcPitch;
     3232                    }
     3233
     3234                    pu8Buffer += pCmd->srcSlicePitch;
     3235                    pu8Surface += mapSurface.cbDepthPitch;
     3236                }
     3237            }
     3238            else
     3239                ASSERT_GUEST_FAILED_STMT(rc = VERR_INVALID_PARAMETER);
     3240
     3241            vmsvga3dSurfaceUnmap(pThisCC, &imageSurface, &mapSurface, true);
     3242        }
     3243
     3244        vmsvga3dSurfaceUnmap(pThisCC, &imageBuffer, &mapBuffer, false);
     3245    }
     3246
     3247    return rc;
     3248#else
     3249    RT_NOREF(pThisCC, pCmd, cbCmd);
    29233250    return VERR_NOT_SUPPORTED;
    29243251#endif
     
    29903317{
    29913318#ifdef VMSVGA3D_DX
    2992 ASMBreakpoint();
    2993     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    2994     RT_NOREF(pSvgaR3State, pCmd, cbCmd);
    2995     return vmsvga3dDXPredTransferFromBuffer(pThisCC, idDXContext);
     3319//ASMBreakpoint();
     3320    RT_NOREF(idDXContext, cbCmd);
     3321
     3322    /* This command is executed in a context: "The context is implied from the command buffer header."
     3323     * However the device design allows to do the transfer without a context, so re-use context-less command handler.
     3324     */
     3325    SVGA3dCmdDXTransferFromBuffer cmd;
     3326    cmd.srcSid          = pCmd->srcSid;
     3327    cmd.srcOffset       = pCmd->srcOffset;
     3328    cmd.srcPitch        = pCmd->srcPitch;
     3329    cmd.srcSlicePitch   = pCmd->srcSlicePitch;
     3330    cmd.destSid         = pCmd->destSid;
     3331    cmd.destSubResource = pCmd->destSubResource;
     3332    cmd.destBox         = pCmd->destBox;
     3333    return vmsvga3dCmdDXTransferFromBuffer(pThisCC, &cmd, sizeof(cmd));
    29963334#else
    29973335    RT_NOREF(pThisCC, idDXContext, pCmd, cbCmd);
     
    43134651        SVGA3dCmdUpdateGBImage *pCmd = (SVGA3dCmdUpdateGBImage *)pvCmd;
    43144652        VMSVGAFIFO_CHECK_3D_CMD_MIN_SIZE_BREAK(sizeof(*pCmd));
    4315         vmsvga3dCmdUpdateGBImage(pThisCC, idDXContext, pCmd);
     4653        vmsvga3dCmdUpdateGBImage(pThisCC, pCmd);
    43164654        break;
    43174655    }
     
    43214659        SVGA3dCmdUpdateGBSurface *pCmd = (SVGA3dCmdUpdateGBSurface *)pvCmd;
    43224660        VMSVGAFIFO_CHECK_3D_CMD_MIN_SIZE_BREAK(sizeof(*pCmd));
    4323         VMSVGA_3D_CMD_NOTIMPL(); RT_NOREF(pCmd);
     4661        vmsvga3dCmdUpdateGBSurface(pThisCC, pCmd);
    43244662        break;
    43254663    }
     
    43294667        SVGA3dCmdReadbackGBImage *pCmd = (SVGA3dCmdReadbackGBImage *)pvCmd;
    43304668        VMSVGAFIFO_CHECK_3D_CMD_MIN_SIZE_BREAK(sizeof(*pCmd));
    4331         VMSVGA_3D_CMD_NOTIMPL(); RT_NOREF(pCmd);
     4669        vmsvga3dCmdReadbackGBImage(pThisCC, pCmd);
    43324670        break;
    43334671    }
     
    43374675        SVGA3dCmdReadbackGBSurface *pCmd = (SVGA3dCmdReadbackGBSurface *)pvCmd;
    43384676        VMSVGAFIFO_CHECK_3D_CMD_MIN_SIZE_BREAK(sizeof(*pCmd));
    4339         VMSVGA_3D_CMD_NOTIMPL(); RT_NOREF(pCmd);
     4677        vmsvga3dCmdReadbackGBSurface(pThisCC, pCmd);
    43404678        break;
    43414679    }
     
    46665004        SVGA3dCmdDXReadbackContext *pCmd = (SVGA3dCmdDXReadbackContext *)pvCmd;
    46675005        VMSVGAFIFO_CHECK_3D_CMD_MIN_SIZE_BREAK(sizeof(*pCmd));
    4668         rcParse = vmsvga3dCmdDXReadbackContext(pThisCC, idDXContext, pCmd, cbCmd);
     5006        rcParse = vmsvga3dCmdDXReadbackContext(pThisCC, pCmd, cbCmd);
    46695007        break;
    46705008    }
     
    49545292        SVGA3dCmdDXUpdateSubResource *pCmd = (SVGA3dCmdDXUpdateSubResource *)pvCmd;
    49555293        VMSVGAFIFO_CHECK_3D_CMD_MIN_SIZE_BREAK(sizeof(*pCmd));
    4956         rcParse = vmsvga3dCmdDXUpdateSubResource(pThisCC, idDXContext, pCmd, cbCmd);
     5294        rcParse = vmsvga3dCmdDXUpdateSubResource(pThisCC, pCmd, cbCmd);
    49575295        break;
    49585296    }
     
    49625300        SVGA3dCmdDXReadbackSubResource *pCmd = (SVGA3dCmdDXReadbackSubResource *)pvCmd;
    49635301        VMSVGAFIFO_CHECK_3D_CMD_MIN_SIZE_BREAK(sizeof(*pCmd));
    4964         rcParse = vmsvga3dCmdDXReadbackSubResource(pThisCC, idDXContext, pCmd, cbCmd);
     5302        rcParse = vmsvga3dCmdDXReadbackSubResource(pThisCC, pCmd, cbCmd);
    49655303        break;
    49665304    }
     
    49705308        SVGA3dCmdDXInvalidateSubResource *pCmd = (SVGA3dCmdDXInvalidateSubResource *)pvCmd;
    49715309        VMSVGAFIFO_CHECK_3D_CMD_MIN_SIZE_BREAK(sizeof(*pCmd));
    4972         rcParse = vmsvga3dCmdDXInvalidateSubResource(pThisCC, idDXContext, pCmd, cbCmd);
     5310        rcParse = vmsvga3dCmdDXInvalidateSubResource(pThisCC, pCmd, cbCmd);
    49735311        break;
    49745312    }
     
    51785516        SVGA3dCmdDXTransferFromBuffer *pCmd = (SVGA3dCmdDXTransferFromBuffer *)pvCmd;
    51795517        VMSVGAFIFO_CHECK_3D_CMD_MIN_SIZE_BREAK(sizeof(*pCmd));
    5180         rcParse = vmsvga3dCmdDXTransferFromBuffer(pThisCC, idDXContext, pCmd, cbCmd);
     5518        rcParse = vmsvga3dCmdDXTransferFromBuffer(pThisCC, pCmd, cbCmd);
    51815519        break;
    51825520    }
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA.cpp

    r90852 r91361  
    33243324        if (RT_SUCCESS(rc))
    33253325        {
     3326            LogFunc(("status %RX32 errorOffset %RX32 id %RX64 flags %RX32 length %RX32 ptr %RX64 offset %RX32 dxContext %RX32 (%RX32 %RX32 %RX32 %RX32 %RX32 %RX32)\n",
     3327                     pCmdBuf->hdr.status,
     3328                     pCmdBuf->hdr.errorOffset,
     3329                     pCmdBuf->hdr.id,
     3330                     pCmdBuf->hdr.flags,
     3331                     pCmdBuf->hdr.length,
     3332                     pCmdBuf->hdr.ptr.pa,
     3333                     pCmdBuf->hdr.offset,
     3334                     pCmdBuf->hdr.dxContext,
     3335                     pCmdBuf->hdr.mustBeZero[0],
     3336                     pCmdBuf->hdr.mustBeZero[1],
     3337                     pCmdBuf->hdr.mustBeZero[2],
     3338                     pCmdBuf->hdr.mustBeZero[3],
     3339                     pCmdBuf->hdr.mustBeZero[4],
     3340                     pCmdBuf->hdr.mustBeZero[5]));
     3341
    33263342            /* Verify the command buffer header. */
    33273343            if (RT_LIKELY(   pCmdBuf->hdr.status == SVGA_CB_STATUS_NONE
     
    58275843
    58285844extern VMSVGA3DBACKENDDESC const g_BackendLegacy;
    5829 #ifdef VMSVGA3D_DX
     5845#if defined(VMSVGA3D_DX_BACKEND)
    58305846extern VMSVGA3DBACKENDDESC const g_BackendDX;
    58315847#endif
     
    58575873
    58585874    VMSVGA3DBACKENDDESC const *pBackend = NULL;
    5859 #ifdef VMSVGA3D_DX
     5875#if defined(VMSVGA3D_DX_BACKEND)
    58605876    if (pThis->fVMSVGA10)
    58615877        pBackend = &g_BackendDX;
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA.h

    r88904 r91361  
    648648
    649649#ifdef VBOX_WITH_VMSVGA3D
    650 int vmsvgaR3UpdateGBSurface(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dSurfaceImageId const *pImageId, SVGA3dBox const *pBox);
    651 int vmsvgaR3UpdateGBSurfaceEx(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dSurfaceImageId const *pImageId, SVGA3dBox const *pBoxDst, SVGA3dPoint const *pPtSrc);
     650int vmsvgaR3UpdateGBSurface(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImageId, SVGA3dBox const *pBox);
     651int vmsvgaR3UpdateGBSurfaceEx(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImageId, SVGA3dBox const *pBoxDst, SVGA3dPoint const *pPtSrc);
    652652#endif
    653653
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-dx.cpp

    r88904 r91361  
    153153
    154154
    155 int vmsvga3dDXReadbackContext(PVGASTATECC pThisCC, uint32_t idDXContext)
     155int vmsvga3dDXReadbackContext(PVGASTATECC pThisCC, uint32_t idDXContext, SVGADXContextMobFormat *pSvgaDXContext)
    156156{
    157157    int rc;
     
    166166
    167167    rc = pSvgaR3State->pFuncsDX->pfnDXReadbackContext(pThisCC, pDXContext);
     168    if (RT_SUCCESS(rc))
     169        memcpy(pSvgaDXContext, &pDXContext->svgaDXContext, sizeof(*pSvgaDXContext));
    168170    return rc;
    169171}
     
    292294    ASSERT_GUEST_RETURN(pDXContext->cot.paSampler, VERR_INVALID_STATE);
    293295    for (uint32_t i = 0; i < cSamplerId; ++i)
    294         ASSERT_GUEST_RETURN(paSamplerId[i] < pDXContext->cot.cSampler, VERR_INVALID_PARAMETER);
     296    {
     297        SVGA3dSamplerId const samplerId = paSamplerId[i];
     298        ASSERT_GUEST_RETURN(   samplerId < pDXContext->cot.cSampler
     299                            || samplerId == SVGA_ID_INVALID, VERR_INVALID_PARAMETER);
     300    }
    295301    RT_UNTRUSTED_VALIDATED_FENCE();
    296302
     
    320326    image.mipmap = 0;
    321327    VMSVGA3D_MAPPED_SURFACE map;
    322     int rc2 = pSvgaR3State->pFuncsMap->pfnSurfaceMap(pThisCC, idDXContext, &image, NULL, VMSVGA3D_SURFACE_MAP_READ, &map);
     328    int rc2 = vmsvga3dSurfaceMap(pThisCC, &image, NULL, VMSVGA3D_SURFACE_MAP_READ, &map);
    323329    if (RT_SUCCESS(rc2))
    324330    {
    325331        vmsvga3dMapWriteBmpFile(&map, "rt-");
    326         pSvgaR3State->pFuncsMap->pfnSurfaceUnmap(pThisCC, &image, &map, /* fWritten =  */ false);
     332        vmsvga3dSurfaceUnmap(pThisCC, &image, &map, /* fWritten =  */ false);
    327333    }
    328334#endif
     
    348354
    349355
    350 int vmsvga3dDXDrawInstanced(PVGASTATECC pThisCC, uint32_t idDXContext)
     356int vmsvga3dDXDrawInstanced(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXDrawInstanced const *pCmd)
    351357{
    352358    int rc;
     
    360366    AssertRCReturn(rc, rc);
    361367
    362     rc = pSvgaR3State->pFuncsDX->pfnDXDrawInstanced(pThisCC, pDXContext);
    363     return rc;
    364 }
    365 
    366 
    367 int vmsvga3dDXDrawIndexedInstanced(PVGASTATECC pThisCC, uint32_t idDXContext)
     368    rc = pSvgaR3State->pFuncsDX->pfnDXDrawInstanced(pThisCC, pDXContext,
     369             pCmd->vertexCountPerInstance, pCmd->instanceCount, pCmd->startVertexLocation, pCmd->startInstanceLocation);
     370    return rc;
     371}
     372
     373
     374int vmsvga3dDXDrawIndexedInstanced(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXDrawIndexedInstanced const *pCmd)
    368375{
    369376    int rc;
     
    377384    AssertRCReturn(rc, rc);
    378385
    379     rc = pSvgaR3State->pFuncsDX->pfnDXDrawIndexedInstanced(pThisCC, pDXContext);
     386    rc = pSvgaR3State->pFuncsDX->pfnDXDrawIndexedInstanced(pThisCC, pDXContext,
     387             pCmd->indexCountPerInstance, pCmd->instanceCount, pCmd->startIndexLocation, pCmd->baseVertexLocation, pCmd->startInstanceLocation);
    380388    return rc;
    381389}
     
    827835    AssertRCReturn(rc, rc);
    828836
     837    /** @todo Memcpy if both resources do not have the hardware resource. */
     838
    829839    rc = pSvgaR3State->pFuncsDX->pfnDXPredCopyRegion(pThisCC, pDXContext, pCmd->dstSid, pCmd->dstSubResource, pCmd->srcSid, pCmd->srcSubResource, &pCmd->box);
    830840    return rc;
     
    885895
    886896    rc = pSvgaR3State->pFuncsDX->pfnDXGenMips(pThisCC, pDXContext, shaderResourceViewId);
    887     return rc;
    888 }
    889 
    890 
    891 int vmsvga3dDXUpdateSubResource(PVGASTATECC pThisCC, uint32_t idDXContext)
    892 {
    893     int rc;
    894     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    895     AssertReturn(pSvgaR3State->pFuncsDX && pSvgaR3State->pFuncsDX->pfnDXUpdateSubResource, VERR_INVALID_STATE);
    896     PVMSVGA3DSTATE p3dState = pThisCC->svga.p3dState;
    897     AssertReturn(p3dState, VERR_INVALID_STATE);
    898 
    899     PVMSVGA3DDXCONTEXT pDXContext;
    900     rc = vmsvga3dDXContextFromCid(p3dState, idDXContext, &pDXContext);
    901     AssertRCReturn(rc, rc);
    902 
    903     rc = pSvgaR3State->pFuncsDX->pfnDXUpdateSubResource(pThisCC, pDXContext);
    904     return rc;
    905 }
    906 
    907 
    908 int vmsvga3dDXReadbackSubResource(PVGASTATECC pThisCC, uint32_t idDXContext)
    909 {
    910     int rc;
    911     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    912     AssertReturn(pSvgaR3State->pFuncsDX && pSvgaR3State->pFuncsDX->pfnDXReadbackSubResource, VERR_INVALID_STATE);
    913     PVMSVGA3DSTATE p3dState = pThisCC->svga.p3dState;
    914     AssertReturn(p3dState, VERR_INVALID_STATE);
    915 
    916     PVMSVGA3DDXCONTEXT pDXContext;
    917     rc = vmsvga3dDXContextFromCid(p3dState, idDXContext, &pDXContext);
    918     AssertRCReturn(rc, rc);
    919 
    920     rc = pSvgaR3State->pFuncsDX->pfnDXReadbackSubResource(pThisCC, pDXContext);
    921     return rc;
    922 }
    923 
    924 
    925 int vmsvga3dDXInvalidateSubResource(PVGASTATECC pThisCC, uint32_t idDXContext)
    926 {
    927     int rc;
    928     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    929     AssertReturn(pSvgaR3State->pFuncsDX && pSvgaR3State->pFuncsDX->pfnDXInvalidateSubResource, VERR_INVALID_STATE);
    930     PVMSVGA3DSTATE p3dState = pThisCC->svga.p3dState;
    931     AssertReturn(p3dState, VERR_INVALID_STATE);
    932 
    933     PVMSVGA3DDXCONTEXT pDXContext;
    934     rc = vmsvga3dDXContextFromCid(p3dState, idDXContext, &pDXContext);
    935     AssertRCReturn(rc, rc);
    936 
    937     rc = pSvgaR3State->pFuncsDX->pfnDXInvalidateSubResource(pThisCC, pDXContext);
    938897    return rc;
    939898}
     
    14001359    AssertRCReturn(rc, rc);
    14011360
     1361    AssertReturn(pDXContext->paShader, VERR_INVALID_STATE);
     1362
    14021363    SVGA3dShaderId const shaderId = pCmd->shaderId;
    14031364
     
    14141375    pEntry->mobid         = SVGA_ID_INVALID;
    14151376
    1416     if (!pDXContext->paShader)
    1417     {
    1418         /* Create host array for information abput shaders. */
    1419         pDXContext->paShader = (PVMSVGA3DSHADER)RTMemAllocZ(pDXContext->cot.cShader * sizeof(VMSVGA3DSHADER));
    1420         AssertReturn(pDXContext->paShader, VERR_NO_MEMORY);
    1421         for (uint32_t i = 0; i < pDXContext->cot.cShader; ++i)
    1422             pDXContext->paShader[i].id = SVGA_ID_INVALID;
    1423     }
     1377    /** @todo Remove from here. */
     1378//    if (!pDXContext->paShader)
     1379//    {
     1380//        /* Create host array for information about shaders. */
     1381//        pDXContext->paShader = (PVMSVGA3DSHADER)RTMemAllocZ(pDXContext->cot.cShader * sizeof(VMSVGA3DSHADER));
     1382//        AssertReturn(pDXContext->paShader, VERR_NO_MEMORY);
     1383//        for (uint32_t i = 0; i < pDXContext->cot.cShader; ++i)
     1384//            pDXContext->paShader[i].id = SVGA_ID_INVALID;
     1385//    }
    14241386
    14251387    PVMSVGA3DSHADER pShader = &pDXContext->paShader[shaderId];
     
    14381400    pShader->u.pvBackendShader = NULL;
    14391401
    1440     rc = pSvgaR3State->pFuncsDX->pfnDXDefineShader(pThisCC, pDXContext, pShader);
     1402    rc = pSvgaR3State->pFuncsDX->pfnDXDefineShader(pThisCC, pDXContext, shaderId, pEntry);
    14411403    return rc;
    14421404}
     
    16681630
    16691631    uint32_t cEntries = 0;
     1632    uint32_t cValidEntries = 0;
    16701633    if (RT_SUCCESS(rc))
    16711634    {
     
    16871650
    16881651        cEntries = cbCOT / s_acbEntry[pCmd->type];
    1689         uint32_t const cValidEntries = validSizeInBytes / s_acbEntry[pCmd->type];
    1690 
    1691         rc = pSvgaR3State->pFuncsDX->pfnDXSetCOTable(pThisCC, pDXContext, pCmd->type, cEntries, cValidEntries);
     1652        cValidEntries = validSizeInBytes / s_acbEntry[pCmd->type];
    16921653    }
    16931654
     
    17421703                pDXContext->cot.paShader          = (SVGACOTableDXShaderEntry *)pvCOT;
    17431704                pDXContext->cot.cShader           = cEntries;
     1705
     1706                /* Create host array for information about shaders. */
     1707                RTMemFree(pDXContext->paShader);
     1708                pDXContext->paShader = NULL;
     1709
     1710                if (pDXContext->cot.cShader)
     1711                {
     1712                    pDXContext->paShader = (PVMSVGA3DSHADER)RTMemAllocZ(pDXContext->cot.cShader * sizeof(VMSVGA3DSHADER));
     1713                    AssertReturn(pDXContext->paShader, VERR_NO_MEMORY);
     1714                    for (uint32_t i = 0; i < pDXContext->cot.cShader; ++i)
     1715                        pDXContext->paShader[i].id = SVGA_ID_INVALID;
     1716                }
    17441717                break;
    17451718            case SVGA_COTABLE_UAVIEW:
     
    17531726        vmsvgaR3MobBackingStoreDelete(pSvgaR3State, pMob);
    17541727
     1728    /* Notify the backend. */
     1729    if (RT_SUCCESS(rc))
     1730        rc = pSvgaR3State->pFuncsDX->pfnDXSetCOTable(pThisCC, pDXContext, pCmd->type, cValidEntries);
     1731
    17551732    return rc;
    17561733}
     
    17961773
    17971774
    1798 int vmsvga3dDXTransferFromBuffer(PVGASTATECC pThisCC, uint32_t idDXContext)
    1799 {
    1800     int rc;
    1801     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    1802     AssertReturn(pSvgaR3State->pFuncsDX && pSvgaR3State->pFuncsDX->pfnDXTransferFromBuffer, VERR_INVALID_STATE);
    1803     PVMSVGA3DSTATE p3dState = pThisCC->svga.p3dState;
    1804     AssertReturn(p3dState, VERR_INVALID_STATE);
    1805 
    1806     PVMSVGA3DDXCONTEXT pDXContext;
    1807     rc = vmsvga3dDXContextFromCid(p3dState, idDXContext, &pDXContext);
    1808     AssertRCReturn(rc, rc);
    1809 
    1810     rc = pSvgaR3State->pFuncsDX->pfnDXTransferFromBuffer(pThisCC, pDXContext);
    1811     return rc;
    1812 }
    1813 
    1814 
    18151775int vmsvga3dDXSurfaceCopyAndReadback(PVGASTATECC pThisCC, uint32_t idDXContext)
    18161776{
     
    18811841
    18821842
    1883 int vmsvga3dDXPredTransferFromBuffer(PVGASTATECC pThisCC, uint32_t idDXContext)
    1884 {
    1885     int rc;
    1886     PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
    1887     AssertReturn(pSvgaR3State->pFuncsDX && pSvgaR3State->pFuncsDX->pfnDXPredTransferFromBuffer, VERR_INVALID_STATE);
    1888     PVMSVGA3DSTATE p3dState = pThisCC->svga.p3dState;
    1889     AssertReturn(p3dState, VERR_INVALID_STATE);
    1890 
    1891     PVMSVGA3DDXCONTEXT pDXContext;
    1892     rc = vmsvga3dDXContextFromCid(p3dState, idDXContext, &pDXContext);
    1893     AssertRCReturn(rc, rc);
    1894 
    1895     rc = pSvgaR3State->pFuncsDX->pfnDXPredTransferFromBuffer(pThisCC, pDXContext);
    1896     return rc;
    1897 }
    1898 
    1899 
    19001843int vmsvga3dDXMobFence64(PVGASTATECC pThisCC, uint32_t idDXContext)
    19011844{
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-internal.h

    r91011 r91361  
    547547    uint32_t                id; /** @todo sid */
    548548    /* Which context created the corresponding resource.
    549      * SVGA_ID_INVALID means that resource has not been created yet, or has been created by the shared context.
     549     * SVGA_ID_INVALID means that resource has not been created yet.
    550550     * A resource has been created if VMSVGA3DSURFACE_HAS_HW_SURFACE is true.
    551551     *
     
    13151315{
    13161316    *ppDXContext = NULL;
    1317     if (cid == SVGA_ID_INVALID)
    1318         return VERR_INVALID_STATE;
    13191317    AssertReturn(cid < pState->cDXContexts, VERR_INVALID_PARAMETER);
    13201318    PVMSVGA3DDXCONTEXT const pDXContext = pState->papDXContexts[cid];
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-savedstate.cpp

    r89163 r91361  
    497497
    498498                rc = vmsvga3dSurfaceDefine(pThisCC, sid, surface.surfaceFlags, surface.format, surface.multiSampleCount,
    499                                            surface.autogenFilter, surface.cLevels, &pMipmapLevelSize[0]);
     499                                           surface.autogenFilter, surface.cLevels, &pMipmapLevelSize[0], /* fAllocMipLevels = */ true);
    500500                AssertRCReturn(rc, rc);
    501501
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win-dx.cpp

    r89163 r91361  
    4444#include <d3d11.h>
    4545
     46
     47/** Fake ID for the backend DX context. The context creates all shared textures. */
     48#define DX_CID_BACKEND UINT32_C(0xfffffffe)
     49
    4650#define DX_RELEASE_ARRAY(a_Count, a_papArray) do { \
    4751    for (uint32_t i = 0; i < (a_Count); ++i) \
     
    7377    VMSVGA3DBACKRESTYPE enmResType;
    7478    DXGI_FORMAT enmDxgiFormat;
     79    union
     80    {
     81        ID3D11Resource     *pResource;
     82        ID3D11Texture2D    *pTexture2D;
     83        ID3D11Texture3D    *pTexture3D;
     84        ID3D11Buffer       *pBuffer;
     85    } u;
     86
     87    ID3D11Texture2D    *pDynamicTexture;  /* For screen updates from memory. */ /** @todo One for all screens. */
     88    ID3D11Texture2D    *pStagingTexture;  /* For Reading the screen content. */ /** @todo One for all screens. */
     89    ID3D11Texture3D    *pDynamicTexture3D;  /* For screen updates from memory. */ /** @todo One for all screens. */
     90    ID3D11Texture3D    *pStagingTexture3D;  /* For Reading the screen content. */ /** @todo One for all screens. */
     91
     92    /* Screen targets are created as shared surfaces. */
     93    HANDLE              SharedHandle;     /* The shared handle of this structure. */
     94
     95    /* DX context which last rendered to the texture.
     96     * This is only for render targets and screen targets, which can be shared between contexts.
     97     * The backend context (cid == DX_CID_BACKEND) can also be a drawing context.
     98     */
     99    uint32_t cidDrawing;
     100
    75101    /** AVL tree containing DXSHAREDTEXTURE structures. */
    76102    AVLU32TREE SharedTextureTree;
    77     union
    78     {
    79         struct
    80         {
    81             ID3D11Resource     *pResource;
    82         } Resource;
    83         struct
    84         {
    85             ID3D11Texture2D    *pTexture;         /* The texture for the screen content. */
    86             ID3D11Texture2D    *pDynamicTexture;  /* For screen updates from memory. */ /** @todo One for all screens. */
    87             ID3D11Texture2D    *pStagingTexture;  /* For Reading the screen content. */ /** @todo One for all screens. */
    88             /* Screen targets are created as shared surfaces. */
    89             HANDLE              SharedHandle;     /* The shared handle of this structure. */
    90         } ScreenTarget;
    91         struct
    92         {
    93             ID3D11Texture2D    *pTexture;         /* The texture for the screen content. */
    94             ID3D11Texture2D    *pDynamicTexture;  /* For screen updates from memory. */ /** @todo One for every format. */
    95             ID3D11Texture2D    *pStagingTexture;  /* For Reading the screen content. */ /** @todo One for every format. */
    96         } Texture;
    97         struct
    98         {
    99              ID3D11Buffer      *pBuffer;
    100         } Buffer;
    101     } u;
     103
    102104} VMSVGA3DBACKENDSURFACE;
    103105
     
    147149typedef struct VMSVGA3DBACKENDDXCONTEXT
    148150{
    149     DXDEVICE                   device;                 /* Device for the this context operation. */
     151    DXDEVICE                   device;                 /* DX device interfaces for this context operations. */
    150152
    151153    /* Arrays for Context-Object Tables. Number of entries depends on COTable size. */
     
    206208}
    207209
    208 
     210#define DX_REPLACE_X8_WITH_A8
    209211static DXGI_FORMAT vmsvgaDXSurfaceFormat2Dxgi(SVGA3dSurfaceFormat format)
    210212{
     
    218220    switch (format)
    219221    {
     222#ifdef DX_REPLACE_X8_WITH_A8
     223        case SVGA3D_X8R8G8B8:                   return DXGI_FORMAT_B8G8R8A8_UNORM;
     224#else
    220225        case SVGA3D_X8R8G8B8:                   return DXGI_FORMAT_B8G8R8X8_UNORM;
     226#endif
    221227        case SVGA3D_A8R8G8B8:                   return DXGI_FORMAT_B8G8R8A8_UNORM;
    222228        case SVGA3D_R5G6B5:                     return DXGI_FORMAT_B5G6R5_UNORM;
     
    333339        case SVGA3D_B8G8R8A8_TYPELESS:          return DXGI_FORMAT_B8G8R8A8_TYPELESS;
    334340        case SVGA3D_B8G8R8A8_UNORM_SRGB:        return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
     341#ifdef DX_REPLACE_X8_WITH_A8
     342        case SVGA3D_B8G8R8X8_TYPELESS:          return DXGI_FORMAT_B8G8R8A8_TYPELESS;
     343        case SVGA3D_B8G8R8X8_UNORM_SRGB:        return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
     344#else
    335345        case SVGA3D_B8G8R8X8_TYPELESS:          return DXGI_FORMAT_B8G8R8X8_TYPELESS;
    336346        case SVGA3D_B8G8R8X8_UNORM_SRGB:        return DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
     347#endif
    337348        case SVGA3D_Z_DF16:                     break;
    338349        case SVGA3D_Z_DF24:                     break;
     
    359370        case SVGA3D_B5G5R5A1_UNORM:             return DXGI_FORMAT_B5G5R5A1_UNORM;
    360371        case SVGA3D_B8G8R8A8_UNORM:             return DXGI_FORMAT_B8G8R8A8_UNORM;
     372#ifdef DX_REPLACE_X8_WITH_A8
     373        case SVGA3D_B8G8R8X8_UNORM:             return DXGI_FORMAT_B8G8R8A8_UNORM;
     374#else
    361375        case SVGA3D_B8G8R8X8_UNORM:             return DXGI_FORMAT_B8G8R8X8_UNORM;
     376#endif
    362377        case SVGA3D_BC4_UNORM:                  return DXGI_FORMAT_BC4_UNORM;
    363378        case SVGA3D_BC5_UNORM:                  return DXGI_FORMAT_BC5_UNORM;
     
    698713        LogRel(("VMSVGA: Feature level %#x\n", pDevice->FeatureLevel));
    699714
     715#ifdef DEBUG
     716        /* Break into debugger when DX runtime detects anything unusual. */
     717        HRESULT hr2;
     718        ID3D11Debug *pDebug = 0;
     719        hr2 = pDevice->pDevice->QueryInterface(__uuidof(ID3D11Debug), (void**)&pDebug);
     720        if (SUCCEEDED(hr2))
     721        {
     722            ID3D11InfoQueue *pInfoQueue = 0;
     723            hr2 = pDebug->QueryInterface(__uuidof(ID3D11InfoQueue), (void**)&pInfoQueue);
     724            if (SUCCEEDED(hr2))
     725            {
     726                pInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
     727                pInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
     728                pInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_WARNING, true);
     729
     730                D3D11_MESSAGE_ID saIgnoredMessageIds[] =
     731                {
     732                    /* Message ID:                                    Caused by: */
     733                    D3D11_MESSAGE_ID_CREATEINPUTLAYOUT_TYPE_MISMATCH, /* Autogenerated input signatures. */
     734                };
     735
     736                D3D11_INFO_QUEUE_FILTER filter;
     737                RT_ZERO(filter);
     738                filter.DenyList.NumIDs = RT_ELEMENTS(saIgnoredMessageIds);
     739                filter.DenyList.pIDList = saIgnoredMessageIds;
     740                pInfoQueue->AddStorageFilterEntries(&filter);
     741
     742                D3D_RELEASE(pInfoQueue);
     743            }
     744            D3D_RELEASE(pDebug);
     745        }
     746#endif
     747
    700748        IDXGIDevice *pDxgiDevice = 0;
    701749        hr = pDevice->pDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&pDxgiDevice);
     
    731779
    732780
    733 static ID3D11Resource *dxResource(PVMSVGA3DSURFACE pSurface, VMSVGA3DDXCONTEXT *pDXContext)
     781DECLINLINE(bool) dxIsSurfaceShareable(PVMSVGA3DSURFACE pSurface)
     782{
     783    /* It is not expected that volume textures will be shared between contexts. */
     784    if (pSurface->surfaceFlags & SVGA3D_SURFACE_VOLUME)
     785        return false;
     786
     787    return pSurface->surfaceFlags & SVGA3D_SURFACE_SCREENTARGET
     788        || pSurface->surfaceFlags & SVGA3D_SURFACE_BIND_RENDER_TARGET;
     789}
     790
     791
     792DXDEVICE *dxDeviceFromCid(uint32_t cid, PVMSVGA3DSTATE pState)
     793{
     794    if (cid != DX_CID_BACKEND)
     795    {
     796        VMSVGA3DDXCONTEXT *pDXContext;
     797        int rc = vmsvga3dDXContextFromCid(pState, cid, &pDXContext);
     798        if (RT_SUCCESS(rc))
     799            return &pDXContext->pBackendDXContext->device;
     800    }
     801    else
     802        return &pState->pBackend->device;
     803
     804    AssertFailed();
     805    return NULL;
     806}
     807
     808
     809static int dxDeviceFlush(DXDEVICE *pDevice)
     810{
     811    /** @todo Should the flush follow the query submission? */
     812    pDevice->pImmediateContext->Flush();
     813
     814    ID3D11Query *pQuery = 0;
     815    D3D11_QUERY_DESC qd;
     816    RT_ZERO(qd);
     817    qd.Query = D3D11_QUERY_EVENT;
     818
     819    HRESULT hr = pDevice->pDevice->CreateQuery(&qd, &pQuery);
     820    Assert(hr == S_OK); RT_NOREF(hr);
     821    pDevice->pImmediateContext->End(pQuery);
     822
     823    BOOL queryData;
     824    while (pDevice->pImmediateContext->GetData(pQuery, &queryData, sizeof(queryData), 0) != S_OK)
     825        RTThreadYield();
     826
     827    D3D_RELEASE(pQuery);
     828
     829    return VINF_SUCCESS;
     830}
     831
     832
     833static int dxContextWait(uint32_t cidDrawing, PVMSVGA3DSTATE pState)
     834{
     835    /* Flush cidDrawing context and issue a query. */
     836    DXDEVICE *pDXDevice = dxDeviceFromCid(cidDrawing, pState);
     837    return dxDeviceFlush(pDXDevice);
     838}
     839
     840
     841static ID3D11Resource *dxResource(PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface, VMSVGA3DDXCONTEXT *pDXContext)
    734842{
    735843    VMSVGA3DBACKENDSURFACE *pBackendSurface = pSurface->pBackendSurface;
     
    737845        AssertFailedReturn(NULL);
    738846
    739     if (pDXContext->cid == pSurface->idAssociatedContext)
    740         return pBackendSurface->u.Resource.pResource;
     847    uint32_t const cidRequesting = pDXContext ? pDXContext->cid : DX_CID_BACKEND;
     848    if (cidRequesting == pSurface->idAssociatedContext)
     849        return pBackendSurface->u.pResource;
     850
     851    AssertReturn(pDXContext, NULL);
    741852
    742853    /*
    743854     * Another context is requesting.
    744855     */
    745     /* Expecting this for screen targets only. */
    746     Assert(pSurface->surfaceFlags & SVGA3D_SURFACE_SCREENTARGET);
    747     Assert(pSurface->idAssociatedContext == SVGA_ID_INVALID);
     856    Assert(dxIsSurfaceShareable(pSurface));
     857    Assert(pSurface->idAssociatedContext == DX_CID_BACKEND);
    748858
    749859    DXSHAREDTEXTURE *pSharedTexture = (DXSHAREDTEXTURE *)RTAvlU32Get(&pBackendSurface->SharedTextureTree, pDXContext->cid);
     
    753863        AssertReturn(pDevice->pDevice, NULL);
    754864
    755         AssertReturn(pBackendSurface->u.ScreenTarget.SharedHandle, NULL);
     865        AssertReturn(pBackendSurface->SharedHandle, NULL);
    756866
    757867        /* This context has not yet opened the texture. */
     
    763873        AssertReturn(fSuccess, NULL);
    764874
    765         HRESULT hr = pDevice->pDevice->OpenSharedResource(pBackendSurface->u.ScreenTarget.SharedHandle, __uuidof(ID3D11Texture2D), (void**)&pSharedTexture->pTexture);
     875        HRESULT hr = pDevice->pDevice->OpenSharedResource(pBackendSurface->SharedHandle, __uuidof(ID3D11Texture2D), (void**)&pSharedTexture->pTexture);
    766876        Assert(SUCCEEDED(hr));
    767877        if (SUCCEEDED(hr))
     
    775885    }
    776886
     887    /* Wait for drawing to finish. */
     888    if (pBackendSurface->cidDrawing != SVGA_ID_INVALID)
     889    {
     890        if (pBackendSurface->cidDrawing != pDXContext->cid)
     891        {
     892            dxContextWait(pBackendSurface->cidDrawing, pState);
     893            pBackendSurface->cidDrawing = SVGA_ID_INVALID;
     894        }
     895    }
     896
    777897    return pSharedTexture->pTexture;
    778898}
    779899
     900
     901static int dxTrackRenderTargets(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
     902{
     903    PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
     904    AssertReturn(pState, VERR_INVALID_STATE);
     905
     906//ASMBreakpoint();
     907    for (int i = 0; i < RT_ELEMENTS(pDXContext->svgaDXContext.renderState.renderTargetViewIds); ++i)
     908    {
     909        uint32_t const renderTargetViewId = pDXContext->svgaDXContext.renderState.renderTargetViewIds[i];
     910        if (renderTargetViewId == SVGA_ID_INVALID)
     911            continue;
     912
     913        AssertContinue(renderTargetViewId < pDXContext->cot.cRTView);
     914
     915        SVGACOTableDXRTViewEntry const *pRTViewEntry = &pDXContext->cot.paRTView[renderTargetViewId];
     916
     917        PVMSVGA3DSURFACE pSurface;
     918        int rc = vmsvga3dSurfaceFromSid(pState, pRTViewEntry->sid, &pSurface);
     919        if (   RT_SUCCESS(rc)
     920            && pSurface->pBackendSurface)
     921        {
     922            pSurface->pBackendSurface->cidDrawing = pDXContext->cid;
     923        }
     924    }
     925    return VINF_SUCCESS;
     926}
     927
     928static D3D11_BLEND dxBlendFactorAlpha(uint8_t svgaBlend)
     929{
     930    /* "Blend options that end in _COLOR are not allowed." but the guest sometimes sends them. */
     931    switch (svgaBlend)
     932    {
     933        case SVGA3D_BLENDOP_SRCCOLOR:     return D3D11_BLEND_SRC_ALPHA;
     934        case SVGA3D_BLENDOP_INVSRCCOLOR:  return D3D11_BLEND_INV_SRC_ALPHA;
     935        case SVGA3D_BLENDOP_DESTCOLOR:    return D3D11_BLEND_DEST_ALPHA;
     936        case SVGA3D_BLENDOP_INVDESTCOLOR: return D3D11_BLEND_INV_DEST_ALPHA;
     937        case SVGA3D_BLENDOP_SRC1COLOR:    return D3D11_BLEND_SRC1_ALPHA;
     938        case SVGA3D_BLENDOP_INVSRC1COLOR: return D3D11_BLEND_INV_SRC1_ALPHA;
     939        default:
     940            break;
     941    }
     942    return (D3D11_BLEND)svgaBlend;
     943}
     944
     945static D3D11_BLEND dxBlendFactorColor(uint8_t svgaBlend)
     946{
     947    return (D3D11_BLEND)svgaBlend;
     948}
     949
     950static D3D11_BLEND_OP dxBlendOp(uint8_t svgaBlendEq)
     951{
     952    return (D3D11_BLEND_OP)svgaBlendEq;
     953}
    780954
    781955/** @todo AssertCompile for types like D3D11_COMPARISON_FUNC and SVGA3dComparisonFunc */
     
    788962    {
    789963        BlendDesc.RenderTarget[i].BlendEnable           = RT_BOOL(pEntry->perRT[i].blendEnable);
    790         BlendDesc.RenderTarget[i].SrcBlend              = (D3D11_BLEND)pEntry->perRT[i].srcBlend;
    791         BlendDesc.RenderTarget[i].DestBlend             = (D3D11_BLEND)pEntry->perRT[i].destBlend;
    792         BlendDesc.RenderTarget[i].BlendOp               = (D3D11_BLEND_OP)pEntry->perRT[i].blendOp;
    793         BlendDesc.RenderTarget[i].SrcBlendAlpha         = (D3D11_BLEND)pEntry->perRT[i].srcBlendAlpha;
    794         BlendDesc.RenderTarget[i].DestBlendAlpha        = (D3D11_BLEND)pEntry->perRT[i].destBlendAlpha;
    795         BlendDesc.RenderTarget[i].BlendOpAlpha          = (D3D11_BLEND_OP)pEntry->perRT[i].blendOpAlpha;
     964        BlendDesc.RenderTarget[i].SrcBlend              = dxBlendFactorColor(pEntry->perRT[i].srcBlend);
     965        BlendDesc.RenderTarget[i].DestBlend             = dxBlendFactorColor(pEntry->perRT[i].destBlend);
     966        BlendDesc.RenderTarget[i].BlendOp               = dxBlendOp         (pEntry->perRT[i].blendOp);
     967        BlendDesc.RenderTarget[i].SrcBlendAlpha         = dxBlendFactorAlpha(pEntry->perRT[i].srcBlendAlpha);
     968        BlendDesc.RenderTarget[i].DestBlendAlpha        = dxBlendFactorAlpha(pEntry->perRT[i].destBlendAlpha);
     969        BlendDesc.RenderTarget[i].BlendOpAlpha          = dxBlendOp         (pEntry->perRT[i].blendOpAlpha);
    796970        BlendDesc.RenderTarget[i].RenderTargetWriteMask = pEntry->perRT[i].renderTargetWriteMask;
    797971        /** @todo logicOpEnable and logicOp */
     
    8321006{
    8331007    D3D11_SAMPLER_DESC desc;
    834     desc.Filter         = (D3D11_FILTER)pEntry->filter;
     1008    /* Guest sometimes sends inconsistent (from D3D11 point of view) set of filter flags. */
     1009    if (pEntry->filter & SVGA3D_FILTER_ANISOTROPIC)
     1010        desc.Filter     = (pEntry->filter & SVGA3D_FILTER_COMPARE)
     1011                        ? D3D11_FILTER_COMPARISON_ANISOTROPIC
     1012                        : D3D11_FILTER_ANISOTROPIC;
     1013    else
     1014        desc.Filter     = (D3D11_FILTER)pEntry->filter;
    8351015    desc.AddressU       = (D3D11_TEXTURE_ADDRESS_MODE)pEntry->addressU;
    8361016    desc.AddressV       = (D3D11_TEXTURE_ADDRESS_MODE)pEntry->addressV;
     
    8741054
    8751055
    876 static HRESULT dxRenderTargetViewCreate(PVMSVGA3DDXCONTEXT pDXContext, SVGACOTableDXRTViewEntry const *pEntry, VMSVGA3DSURFACE *pSurface, ID3D11RenderTargetView **pp)
     1056static HRESULT dxRenderTargetViewCreate(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGACOTableDXRTViewEntry const *pEntry, VMSVGA3DSURFACE *pSurface, ID3D11RenderTargetView **pp)
    8771057{
    8781058    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    8791059
    880     ID3D11Resource *pResource = dxResource(pSurface, pDXContext);
    881     //pBackendSurface->u.Texture.pTexture;
     1060    ID3D11Resource *pResource = dxResource(pThisCC->svga.p3dState, pSurface, pDXContext);
    8821061
    8831062    D3D11_RENDER_TARGET_VIEW_DESC desc;
     
    9491128
    9501129
    951 static HRESULT dxShaderResourceViewCreate(PVMSVGA3DDXCONTEXT pDXContext, SVGACOTableDXSRViewEntry const *pEntry, VMSVGA3DSURFACE *pSurface, ID3D11ShaderResourceView **pp)
     1130static HRESULT dxShaderResourceViewCreate(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGACOTableDXSRViewEntry const *pEntry, VMSVGA3DSURFACE *pSurface, ID3D11ShaderResourceView **pp)
    9521131{
    9531132    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    9541133
    955     ID3D11Resource *pResource = dxResource(pSurface, pDXContext);
    956 //    ID3D11Resource *pResource = pBackendSurface->u.Texture.pTexture;
     1134    ID3D11Resource *pResource = dxResource(pThisCC->svga.p3dState, pSurface, pDXContext);
    9571135
    9581136    D3D11_SHADER_RESOURCE_VIEW_DESC desc;
     
    10061184            break;
    10071185        case SVGA3D_RESOURCE_TEXTURECUBE:
    1008             AssertFailed(); /** @todo test. */
    10091186            desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
    10101187            desc.TextureCube.MostDetailedMip = pEntry->desc.tex.mostDetailedMip;
     
    10281205
    10291206
    1030 static HRESULT dxDepthStencilViewCreate(DXDEVICE *pDevice, SVGACOTableDXDSViewEntry const *pEntry, VMSVGA3DBACKENDSURFACE *pBackendSurface, ID3D11DepthStencilView **pp)
    1031 {
    1032     ID3D11Resource *pResource = pBackendSurface->u.Texture.pTexture;
     1207static HRESULT dxDepthStencilViewCreate(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGACOTableDXDSViewEntry const *pEntry, VMSVGA3DSURFACE *pSurface, ID3D11DepthStencilView **pp)
     1208{
     1209    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
     1210
     1211    ID3D11Resource *pResource = dxResource(pThisCC->svga.p3dState, pSurface, pDXContext);
    10331212
    10341213    D3D11_DEPTH_STENCIL_VIEW_DESC desc;
     
    11831362
    11841363
     1364static int dxBackendSurfaceAlloc(PVMSVGA3DBACKENDSURFACE *ppBackendSurface)
     1365{
     1366    PVMSVGA3DBACKENDSURFACE pBackendSurface = (PVMSVGA3DBACKENDSURFACE)RTMemAllocZ(sizeof(VMSVGA3DBACKENDSURFACE));
     1367    AssertPtrReturn(pBackendSurface, VERR_NO_MEMORY);
     1368    pBackendSurface->cidDrawing = SVGA_ID_INVALID;
     1369    *ppBackendSurface = pBackendSurface;
     1370    return VINF_SUCCESS;
     1371}
     1372
     1373
    11851374static int vmsvga3dBackSurfaceCreateScreenTarget(PVGASTATECC pThisCC, PVMSVGA3DSURFACE pSurface)
    11861375{
     
    12031392    }
    12041393
    1205     PVMSVGA3DBACKENDSURFACE pBackendSurface = (PVMSVGA3DBACKENDSURFACE)RTMemAllocZ(sizeof(VMSVGA3DBACKENDSURFACE));
    1206     AssertPtrReturn(pBackendSurface, VERR_NO_MEMORY);
     1394    PVMSVGA3DBACKENDSURFACE pBackendSurface;
     1395    int rc = dxBackendSurfaceAlloc(&pBackendSurface);
     1396    AssertRCReturn(rc, rc);
    12071397
    12081398    D3D11_TEXTURE2D_DESC td;
     
    12211411    td.MiscFlags          = D3D11_RESOURCE_MISC_SHARED;
    12221412
    1223     HRESULT hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->u.ScreenTarget.pTexture);
     1413    HRESULT hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->u.pTexture2D);
    12241414    Assert(SUCCEEDED(hr));
    12251415    if (SUCCEEDED(hr))
     
    12301420        td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    12311421        td.MiscFlags      = 0;
    1232         hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->u.ScreenTarget.pDynamicTexture);
     1422        hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->pDynamicTexture);
    12331423        Assert(SUCCEEDED(hr));
    12341424    }
     
    12401430        td.BindFlags      = 0; /* No flags allowed. */
    12411431        td.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
    1242         hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->u.ScreenTarget.pStagingTexture);
     1432        hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->pStagingTexture);
    12431433        Assert(SUCCEEDED(hr));
    12441434    }
     
    12481438        /* Get the shared handle. */
    12491439        IDXGIResource *pDxgiResource = NULL;
    1250         hr = pBackendSurface->u.ScreenTarget.pTexture->QueryInterface(__uuidof(IDXGIResource), (void**)&pDxgiResource);
     1440        hr = pBackendSurface->u.pTexture2D->QueryInterface(__uuidof(IDXGIResource), (void**)&pDxgiResource);
    12511441        Assert(SUCCEEDED(hr));
    12521442        if (SUCCEEDED(hr))
    12531443        {
    1254             hr = pDxgiResource->GetSharedHandle(&pBackendSurface->u.ScreenTarget.SharedHandle);
     1444            hr = pDxgiResource->GetSharedHandle(&pBackendSurface->SharedHandle);
    12551445            Assert(SUCCEEDED(hr));
    12561446            D3D_RELEASE(pDxgiResource);
     
    12661456        pBackendSurface->enmDxgiFormat = td.Format;
    12671457        pSurface->pBackendSurface = pBackendSurface;
    1268         pSurface->idAssociatedContext = SVGA_ID_INVALID;
     1458        pSurface->idAssociatedContext = DX_CID_BACKEND;
    12691459        pSurface->fDirty = true;
    12701460        return VINF_SUCCESS;
     
    12721462
    12731463    /* Failure. */
    1274     D3D_RELEASE(pBackendSurface->u.ScreenTarget.pStagingTexture);
    1275     D3D_RELEASE(pBackendSurface->u.ScreenTarget.pDynamicTexture);
    1276     D3D_RELEASE(pBackendSurface->u.ScreenTarget.pTexture);
     1464    D3D_RELEASE(pBackendSurface->pStagingTexture);
     1465    D3D_RELEASE(pBackendSurface->pDynamicTexture);
     1466    D3D_RELEASE(pBackendSurface->u.pTexture2D);
    12771467    RTMemFree(pBackendSurface);
    12781468    return VERR_NO_MEMORY;
     
    13051495    AssertReturn(p3dState, VERR_INVALID_STATE);
    13061496
    1307     DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    1308     AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
     1497    PVMSVGA3DBACKEND pBackend = p3dState->pBackend;
     1498    AssertReturn(pBackend, VERR_INVALID_STATE);
     1499
     1500    UINT MiscFlags;
     1501    DXDEVICE *pDXDevice;
     1502    if (dxIsSurfaceShareable(pSurface))
     1503    {
     1504        pDXDevice = &pBackend->device;
     1505        MiscFlags = D3D11_RESOURCE_MISC_SHARED;
     1506    }
     1507    else
     1508    {
     1509        pDXDevice = &pDXContext->pBackendDXContext->device;
     1510        MiscFlags = 0;
     1511    }
     1512    AssertReturn(pDXDevice->pDevice, VERR_INVALID_STATE);
    13091513
    13101514    if (pSurface->pBackendSurface != NULL)
     
    13141518    }
    13151519
    1316     PVMSVGA3DBACKENDSURFACE pBackendSurface = (PVMSVGA3DBACKENDSURFACE)RTMemAllocZ(sizeof(VMSVGA3DBACKENDSURFACE));
    1317     AssertPtrReturn(pBackendSurface, VERR_NO_MEMORY);
     1520    PVMSVGA3DBACKENDSURFACE pBackendSurface;
     1521    int rc = dxBackendSurfaceAlloc(&pBackendSurface);
     1522    AssertRCReturn(rc, rc);
    13181523
    13191524    uint32_t const cWidth = pSurface->paMipmapLevels[0].mipmapSize.width;
     
    13341539         * Create the texture in backend device and open for the specified context.
    13351540         */
    1336         PVMSVGA3DBACKEND pBackend = p3dState->pBackend;
    1337         AssertReturn(pBackend, VERR_INVALID_STATE);
    1338 
    1339         DXDEVICE *pDXDevice = &pBackend->device;
    1340         AssertReturn(pDXDevice->pDevice, VERR_INVALID_STATE);
    1341 
    13421541        D3D11_TEXTURE2D_DESC td;
    13431542        RT_ZERO(td);
     
    13531552        td.BindFlags          = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
    13541553        td.CPUAccessFlags     = 0;
    1355         td.MiscFlags          = D3D11_RESOURCE_MISC_SHARED;
    1356 
    1357         hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->u.ScreenTarget.pTexture);
     1554        td.MiscFlags          = MiscFlags;
     1555
     1556        hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->u.pTexture2D);
    13581557        Assert(SUCCEEDED(hr));
    13591558        if (SUCCEEDED(hr))
     
    13641563            td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    13651564            td.MiscFlags      = 0;
    1366             hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->u.ScreenTarget.pDynamicTexture);
     1565            hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->pDynamicTexture);
    13671566            Assert(SUCCEEDED(hr));
    13681567        }
     
    13741573            td.BindFlags      = 0; /* No flags allowed. */
    13751574            td.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
    1376             hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->u.ScreenTarget.pStagingTexture);
     1575            hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->pStagingTexture);
    13771576            Assert(SUCCEEDED(hr));
    13781577        }
     
    13821581            /* Get the shared handle. */
    13831582            IDXGIResource *pDxgiResource = NULL;
    1384             hr = pBackendSurface->u.ScreenTarget.pTexture->QueryInterface(__uuidof(IDXGIResource), (void**)&pDxgiResource);
     1583            hr = pBackendSurface->u.pTexture2D->QueryInterface(__uuidof(IDXGIResource), (void**)&pDxgiResource);
    13851584            Assert(SUCCEEDED(hr));
    13861585            if (SUCCEEDED(hr))
    13871586            {
    1388                 hr = pDxgiResource->GetSharedHandle(&pBackendSurface->u.ScreenTarget.SharedHandle);
     1587                hr = pDxgiResource->GetSharedHandle(&pBackendSurface->SharedHandle);
    13891588                Assert(SUCCEEDED(hr));
    13901589                D3D_RELEASE(pDxgiResource);
     
    14021601        Assert(cWidth == cHeight);
    14031602        Assert(cDepth == 1);
    1404 
    1405         pBackendSurface->enmResType = VMSVGA3D_RESTYPE_CUBE_TEXTURE;
    1406         AssertFailed(); /** @todo implement */
    1407         hr = E_FAIL;
     1603//ASMBreakpoint();
     1604        D3D11_SUBRESOURCE_DATA *paInitialData = NULL;
     1605        D3D11_SUBRESOURCE_DATA aInitialData[6 * SVGA3D_MAX_MIP_LEVELS];
     1606        if (pSurface->paMipmapLevels[0].pSurfaceData)
     1607        {
     1608            /** @todo Can happen for a non GBO surface or if GBO texture was updated prior to creation if the hardware resource. Test this. */
     1609            /** @todo for (i = 0; i < pSurface->cFaces * numMipLevels; ++i) */
     1610            for (uint32_t iFace = 0; iFace < 6; ++iFace)
     1611            {
     1612                for (uint32_t i = 0; i < numMipLevels; ++i)
     1613                {
     1614                    uint32_t const iSubresource = vmsvga3dCalcSubresource(i, iFace, numMipLevels);
     1615
     1616                    PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[iSubresource];
     1617                    D3D11_SUBRESOURCE_DATA *p = &aInitialData[iSubresource];
     1618                    p->pSysMem          = pMipmapLevel->pSurfaceData;
     1619                    p->SysMemPitch      = pMipmapLevel->cbSurfacePitch;
     1620                    p->SysMemSlicePitch = pMipmapLevel->cbSurfacePlane;
     1621                }
     1622            }
     1623            paInitialData = &aInitialData[0];
     1624        }
     1625
     1626        D3D11_TEXTURE2D_DESC td;
     1627        RT_ZERO(td);
     1628        td.Width              = cWidth;
     1629        td.Height             = cHeight;
     1630        td.MipLevels          = numMipLevels;
     1631        td.ArraySize          = 6;
     1632        td.Format             = dxgiFormat;
     1633        td.SampleDesc.Count   = 1;
     1634        td.SampleDesc.Quality = 0;
     1635        td.Usage              = D3D11_USAGE_DEFAULT;
     1636        td.BindFlags          = dxBindFlags(pSurface->surfaceFlags);
     1637        td.CPUAccessFlags     = 0; /** @todo */
     1638        td.MiscFlags          = MiscFlags | D3D11_RESOURCE_MISC_TEXTURECUBE; /** @todo */
     1639        if (   numMipLevels > 1
     1640            && (td.BindFlags & (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET)) == (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET))
     1641            td.MiscFlags     |= D3D11_RESOURCE_MISC_GENERATE_MIPS; /* Required for GenMips. */
     1642
     1643        hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->u.pTexture2D);
     1644        Assert(SUCCEEDED(hr));
     1645        if (SUCCEEDED(hr))
     1646        {
     1647            /* Map-able texture. */
     1648            td.MipLevels      = 1; /* Must be for D3D11_USAGE_DYNAMIC. */
     1649            td.ArraySize      = 1; /* Must be for D3D11_USAGE_DYNAMIC. */
     1650            td.Usage          = D3D11_USAGE_DYNAMIC;
     1651            td.BindFlags      = D3D11_BIND_SHADER_RESOURCE; /* Have to specify a supported flag, otherwise E_INVALIDARG will be returned. */
     1652            td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
     1653            td.MiscFlags      = 0;
     1654            hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->pDynamicTexture);
     1655            Assert(SUCCEEDED(hr));
     1656        }
     1657
     1658        if (SUCCEEDED(hr))
     1659        {
     1660            /* Staging texture. */
     1661            td.Usage          = D3D11_USAGE_STAGING;
     1662            td.BindFlags      = 0; /* No flags allowed. */
     1663            td.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
     1664            td.MiscFlags      = 0;
     1665            hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->pStagingTexture);
     1666            Assert(SUCCEEDED(hr));
     1667        }
     1668
     1669        if (   SUCCEEDED(hr)
     1670            && MiscFlags == D3D11_RESOURCE_MISC_SHARED)
     1671        {
     1672            /* Get the shared handle. */
     1673            IDXGIResource *pDxgiResource = NULL;
     1674            hr = pBackendSurface->u.pTexture2D->QueryInterface(__uuidof(IDXGIResource), (void**)&pDxgiResource);
     1675            Assert(SUCCEEDED(hr));
     1676            if (SUCCEEDED(hr))
     1677            {
     1678                hr = pDxgiResource->GetSharedHandle(&pBackendSurface->SharedHandle);
     1679                Assert(SUCCEEDED(hr));
     1680                D3D_RELEASE(pDxgiResource);
     1681            }
     1682        }
     1683
     1684        if (SUCCEEDED(hr))
     1685        {
     1686            pBackendSurface->enmResType = VMSVGA3D_RESTYPE_CUBE_TEXTURE;
     1687        }
    14081688    }
    14091689    else if (pSurface->surfaceFlags & SVGA3D_SURFACE_1D)
     
    14191699             * Volume texture.
    14201700             */
    1421             pBackendSurface->enmResType = VMSVGA3D_RESTYPE_TEXTURE_3D;
    1422             AssertFailed(); /** @todo implement */
    1423             hr = E_FAIL;
     1701            Assert(pSurface->cFaces == 1);
     1702
     1703            D3D11_SUBRESOURCE_DATA *paInitialData = NULL;
     1704            D3D11_SUBRESOURCE_DATA aInitialData[SVGA3D_MAX_MIP_LEVELS];
     1705            if (pSurface->paMipmapLevels[0].pSurfaceData)
     1706            {
     1707                /** @todo Can happen for a non GBO surface or if GBO texture was updated prior to creation if the hardware resource. Test this. */
     1708                for (uint32_t i = 0; i < numMipLevels; ++i)
     1709                {
     1710                    PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
     1711                    D3D11_SUBRESOURCE_DATA *p = &aInitialData[i];
     1712                    p->pSysMem          = pMipmapLevel->pSurfaceData;
     1713                    p->SysMemPitch      = pMipmapLevel->cbSurfacePitch;
     1714                    p->SysMemSlicePitch = pMipmapLevel->cbSurfacePlane;
     1715                }
     1716                paInitialData = &aInitialData[0];
     1717            }
     1718
     1719            D3D11_TEXTURE3D_DESC td;
     1720            RT_ZERO(td);
     1721            td.Width              = cWidth;
     1722            td.Height             = cHeight;
     1723            td.Depth              = cDepth;
     1724            td.MipLevels          = numMipLevels;
     1725            td.Format             = dxgiFormat;
     1726            td.Usage              = D3D11_USAGE_DEFAULT;
     1727            td.BindFlags          = dxBindFlags(pSurface->surfaceFlags);
     1728            td.CPUAccessFlags     = 0; /** @todo */
     1729            td.MiscFlags          = MiscFlags; /** @todo */
     1730            if (   numMipLevels > 1
     1731                && (td.BindFlags & (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET)) == (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET))
     1732                td.MiscFlags     |= D3D11_RESOURCE_MISC_GENERATE_MIPS; /* Required for GenMips. */
     1733
     1734            hr = pDXDevice->pDevice->CreateTexture3D(&td, paInitialData, &pBackendSurface->u.pTexture3D);
     1735            Assert(SUCCEEDED(hr));
     1736            if (SUCCEEDED(hr))
     1737            {
     1738                /* Map-able texture. */
     1739                td.MipLevels      = 1; /* Must be for D3D11_USAGE_DYNAMIC. */
     1740                td.Usage          = D3D11_USAGE_DYNAMIC;
     1741                td.BindFlags      = D3D11_BIND_SHADER_RESOURCE; /* Have to specify a supported flag, otherwise E_INVALIDARG will be returned. */
     1742                td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
     1743                td.MiscFlags      = 0;
     1744                hr = pDXDevice->pDevice->CreateTexture3D(&td, paInitialData, &pBackendSurface->pDynamicTexture3D);
     1745                Assert(SUCCEEDED(hr));
     1746            }
     1747
     1748            if (SUCCEEDED(hr))
     1749            {
     1750                /* Staging texture. */
     1751                td.Usage          = D3D11_USAGE_STAGING;
     1752                td.BindFlags      = 0; /* No flags allowed. */
     1753                td.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
     1754                td.MiscFlags      = 0;
     1755                hr = pDXDevice->pDevice->CreateTexture3D(&td, paInitialData, &pBackendSurface->pStagingTexture3D);
     1756                Assert(SUCCEEDED(hr));
     1757            }
     1758
     1759            if (   SUCCEEDED(hr)
     1760                && MiscFlags == D3D11_RESOURCE_MISC_SHARED)
     1761            {
     1762                /* Get the shared handle. */
     1763                IDXGIResource *pDxgiResource = NULL;
     1764                hr = pBackendSurface->u.pTexture3D->QueryInterface(__uuidof(IDXGIResource), (void**)&pDxgiResource);
     1765                Assert(SUCCEEDED(hr));
     1766                if (SUCCEEDED(hr))
     1767                {
     1768                    hr = pDxgiResource->GetSharedHandle(&pBackendSurface->SharedHandle);
     1769                    Assert(SUCCEEDED(hr));
     1770                    D3D_RELEASE(pDxgiResource);
     1771                }
     1772            }
     1773
     1774            if (SUCCEEDED(hr))
     1775            {
     1776                pBackendSurface->enmResType = VMSVGA3D_RESTYPE_TEXTURE_3D;
     1777            }
    14241778        }
    14251779        else
     
    14341788            if (pSurface->paMipmapLevels[0].pSurfaceData)
    14351789            {
    1436                 /** @todo Can happen for a non GBO surface. Test this. */
     1790                /** @todo Can happen for a non GBO surface or if GBO texture was updated prior to creation if the hardware resource. Test this. */
    14371791                for (uint32_t i = 0; i < numMipLevels; ++i)
    14381792                {
     
    14581812            td.BindFlags          = dxBindFlags(pSurface->surfaceFlags);
    14591813            td.CPUAccessFlags     = 0; /** @todo */
    1460             td.MiscFlags          = 0; /** @todo */
     1814            td.MiscFlags          = MiscFlags; /** @todo */
    14611815            if (   numMipLevels > 1
    14621816                && (td.BindFlags & (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET)) == (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET))
    14631817                td.MiscFlags     |= D3D11_RESOURCE_MISC_GENERATE_MIPS; /* Required for GenMips. */
    14641818
    1465             hr = pDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->u.Texture.pTexture);
     1819            hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->u.pTexture2D);
    14661820            Assert(SUCCEEDED(hr));
    14671821            if (SUCCEEDED(hr))
     
    14731827                td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    14741828                td.MiscFlags      = 0;
    1475                 hr = pDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->u.Texture.pDynamicTexture);
     1829                hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->pDynamicTexture);
    14761830                Assert(SUCCEEDED(hr));
    14771831            }
     
    14841838                td.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
    14851839                td.MiscFlags      = 0;
    1486                 hr = pDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->u.Texture.pStagingTexture);
     1840                hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->pStagingTexture);
    14871841                Assert(SUCCEEDED(hr));
     1842            }
     1843
     1844            if (   SUCCEEDED(hr)
     1845                && MiscFlags == D3D11_RESOURCE_MISC_SHARED)
     1846            {
     1847                /* Get the shared handle. */
     1848                IDXGIResource *pDxgiResource = NULL;
     1849                hr = pBackendSurface->u.pTexture2D->QueryInterface(__uuidof(IDXGIResource), (void**)&pDxgiResource);
     1850                Assert(SUCCEEDED(hr));
     1851                if (SUCCEEDED(hr))
     1852                {
     1853                    hr = pDxgiResource->GetSharedHandle(&pBackendSurface->SharedHandle);
     1854                    Assert(SUCCEEDED(hr));
     1855                    D3D_RELEASE(pDxgiResource);
     1856                }
    14881857            }
    14891858
     
    15081877        pBackendSurface->enmDxgiFormat = dxgiFormat;
    15091878        pSurface->pBackendSurface = pBackendSurface;
    1510         if (pBackendSurface->enmResType == VMSVGA3D_RESTYPE_SCREEN_TARGET)
    1511             pSurface->idAssociatedContext = SVGA_ID_INVALID;
     1879        if (RT_BOOL(MiscFlags & D3D11_RESOURCE_MISC_SHARED))
     1880            pSurface->idAssociatedContext = DX_CID_BACKEND;
    15121881        else
    15131882            pSurface->idAssociatedContext = pDXContext->cid;
     
    15161885
    15171886    /* Failure. */
    1518     D3D_RELEASE(pBackendSurface->u.Texture.pStagingTexture);
    1519     D3D_RELEASE(pBackendSurface->u.Texture.pDynamicTexture);
    1520     D3D_RELEASE(pBackendSurface->u.Texture.pTexture);
     1887    D3D_RELEASE(pBackendSurface->pStagingTexture);
     1888    D3D_RELEASE(pBackendSurface->pDynamicTexture);
     1889    D3D_RELEASE(pBackendSurface->u.pTexture2D);
    15211890    RTMemFree(pBackendSurface);
    15221891    return VERR_NO_MEMORY;
     
    15261895static int vmsvga3dBackSurfaceCreateDepthStencilTexture(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, PVMSVGA3DSURFACE pSurface)
    15271896{
    1528     DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    1529     AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
     1897    DXDEVICE *pDXDevice = &pDXContext->pBackendDXContext->device;
     1898    AssertReturn(pDXDevice->pDevice, VERR_INVALID_STATE);
    15301899
    15311900    if (pSurface->pBackendSurface != NULL)
     
    15351904    }
    15361905
    1537     PVMSVGA3DBACKENDSURFACE pBackendSurface = (PVMSVGA3DBACKENDSURFACE)RTMemAllocZ(sizeof(VMSVGA3DBACKENDSURFACE));
    1538     AssertPtrReturn(pBackendSurface, VERR_NO_MEMORY);
     1906    PVMSVGA3DBACKENDSURFACE pBackendSurface;
     1907    int rc = dxBackendSurfaceAlloc(&pBackendSurface);
     1908    AssertRCReturn(rc, rc);
     1909
     1910    uint32_t const cWidth = pSurface->paMipmapLevels[0].mipmapSize.width;
     1911    uint32_t const cHeight = pSurface->paMipmapLevels[0].mipmapSize.height;
     1912    uint32_t const numMipLevels = pSurface->cLevels;
     1913
     1914    D3D11_SUBRESOURCE_DATA *paInitialData = NULL;
     1915    D3D11_SUBRESOURCE_DATA aInitialData[SVGA3D_MAX_MIP_LEVELS];
     1916    if (pSurface->paMipmapLevels[0].pSurfaceData)
     1917    {
     1918        /** @todo Can happen for a non GBO surface or if GBO texture was updated prior to creation if the hardware resource. Test this. */
     1919        for (uint32_t i = 0; i < numMipLevels; ++i)
     1920        {
     1921            PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
     1922            D3D11_SUBRESOURCE_DATA *p = &aInitialData[i];
     1923            p->pSysMem          = pMipmapLevel->pSurfaceData;
     1924            p->SysMemPitch      = pMipmapLevel->cbSurfacePitch;
     1925            p->SysMemSlicePitch = pMipmapLevel->cbSurfacePlane;
     1926        }
     1927        paInitialData = &aInitialData[0];
     1928    }
    15391929
    15401930    D3D11_TEXTURE2D_DESC td;
    15411931    RT_ZERO(td);
    1542     td.Width              = pSurface->paMipmapLevels[0].mipmapSize.width;
    1543     td.Height             = pSurface->paMipmapLevels[0].mipmapSize.height;
     1932    td.Width              = cWidth;
     1933    td.Height             = cHeight;
    15441934    Assert(pSurface->cLevels == 1);
    15451935    td.MipLevels          = 1;
     
    15501940    td.SampleDesc.Quality = 0;
    15511941    td.Usage              = D3D11_USAGE_DEFAULT;
    1552     td.BindFlags          = D3D11_BIND_DEPTH_STENCIL;
     1942    td.BindFlags          = dxBindFlags(pSurface->surfaceFlags);
    15531943    td.CPUAccessFlags     = 0;
    15541944    td.MiscFlags          = 0;
    15551945
    1556     HRESULT hr = pDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->u.Texture.pTexture);
     1946    HRESULT hr = pDXDevice->pDevice->CreateTexture2D(&td, 0, &pBackendSurface->u.pTexture2D);
    15571947    Assert(SUCCEEDED(hr));
     1948    if (SUCCEEDED(hr))
     1949    {
     1950        /* Map-able texture. */
     1951        td.MipLevels      = 1; /* Must be for D3D11_USAGE_DYNAMIC. */
     1952        td.Usage          = D3D11_USAGE_DYNAMIC;
     1953        td.BindFlags      = D3D11_BIND_SHADER_RESOURCE; /* Have to specify a supported flag, otherwise E_INVALIDARG will be returned. */
     1954        td.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
     1955        td.MiscFlags      = 0;
     1956        hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->pDynamicTexture);
     1957        Assert(SUCCEEDED(hr));
     1958    }
     1959
     1960    if (SUCCEEDED(hr))
     1961    {
     1962        /* Staging texture. */
     1963        td.Usage          = D3D11_USAGE_STAGING;
     1964        td.BindFlags      = 0; /* No flags allowed. */
     1965        td.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
     1966        td.MiscFlags      = 0;
     1967        hr = pDXDevice->pDevice->CreateTexture2D(&td, paInitialData, &pBackendSurface->pStagingTexture);
     1968        Assert(SUCCEEDED(hr));
     1969    }
     1970
    15581971    if (SUCCEEDED(hr))
    15591972    {
     
    15651978        pSurface->pBackendSurface = pBackendSurface;
    15661979        pSurface->idAssociatedContext = pDXContext->cid;
    1567         pSurface->fDirty = true;
     1980        //pSurface->fDirty = true;
    15681981        return VINF_SUCCESS;
    15691982    }
    15701983
    15711984    /* Failure. */
    1572     D3D_RELEASE(pBackendSurface->u.Texture.pStagingTexture);
    1573     D3D_RELEASE(pBackendSurface->u.Texture.pDynamicTexture);
    1574     D3D_RELEASE(pBackendSurface->u.Texture.pTexture);
     1985    D3D_RELEASE(pBackendSurface->pStagingTexture);
     1986    D3D_RELEASE(pBackendSurface->pDynamicTexture);
     1987    D3D_RELEASE(pBackendSurface->u.pTexture2D);
    15751988    RTMemFree(pBackendSurface);
    15761989    return VERR_NO_MEMORY;
     
    15982011    }
    15992012
    1600     PVMSVGA3DBACKENDSURFACE pBackendSurface = (PVMSVGA3DBACKENDSURFACE)RTMemAllocZ(sizeof(VMSVGA3DBACKENDSURFACE));
    1601     AssertPtrReturn(pBackendSurface, VERR_NO_MEMORY);
     2013    PVMSVGA3DBACKENDSURFACE pBackendSurface;
     2014    int rc = dxBackendSurfaceAlloc(&pBackendSurface);
     2015    AssertRCReturn(rc, rc);
    16022016
    16032017    D3D11_BUFFER_DESC bd;
     
    16142028    bd.StructureByteStride = 0;
    16152029
    1616     HRESULT hr = pDevice->pDevice->CreateBuffer(&bd, 0, &pBackendSurface->u.Buffer.pBuffer);
     2030    HRESULT hr = pDevice->pDevice->CreateBuffer(&bd, 0, &pBackendSurface->u.pBuffer);
    16172031    if (SUCCEEDED(hr))
    16182032    {
     
    16292043
    16302044    /* Failure. */
    1631     D3D_RELEASE(pBackendSurface->u.Buffer.pBuffer);
     2045    D3D_RELEASE(pBackendSurface->u.pBuffer);
    16322046    RTMemFree(pBackendSurface);
    16332047    return VERR_NO_MEMORY;
     
    16492063    }
    16502064
    1651     PVMSVGA3DBACKENDSURFACE pBackendSurface = (PVMSVGA3DBACKENDSURFACE)RTMemAllocZ(sizeof(VMSVGA3DBACKENDSURFACE));
    1652     AssertPtrReturn(pBackendSurface, VERR_NO_MEMORY);
     2065    PVMSVGA3DBACKENDSURFACE pBackendSurface;
     2066    int rc = dxBackendSurfaceAlloc(&pBackendSurface);
     2067    AssertRCReturn(rc, rc);
    16532068
    16542069    D3D11_BUFFER_DESC bd;
     
    16612076    bd.StructureByteStride = 0;
    16622077
    1663     HRESULT hr = pDevice->pDevice->CreateBuffer(&bd, 0, &pBackendSurface->u.Buffer.pBuffer);
     2078    HRESULT hr = pDevice->pDevice->CreateBuffer(&bd, 0, &pBackendSurface->u.pBuffer);
    16642079    if (SUCCEEDED(hr))
    16652080    {
     
    16762091
    16772092    /* Failure. */
    1678     D3D_RELEASE(pBackendSurface->u.Buffer.pBuffer);
     2093    D3D_RELEASE(pBackendSurface->u.pBuffer);
    16792094    RTMemFree(pBackendSurface);
    16802095    return VERR_NO_MEMORY;
     
    16942109    bd.StructureByteStride = 0;
    16952110
    1696     return pDevice->pDevice->CreateBuffer(&bd, pInitialData, &pBackendSurface->u.Buffer.pBuffer);
     2111    return pDevice->pDevice->CreateBuffer(&bd, pInitialData, &pBackendSurface->u.pBuffer);
    16972112}
    16982113
     
    17112126    bd.StructureByteStride = 0;
    17122127
    1713     return pDevice->pDevice->CreateBuffer(&bd, pInitialData, &pBackendSurface->u.Buffer.pBuffer);
     2128    return pDevice->pDevice->CreateBuffer(&bd, pInitialData, &pBackendSurface->u.pBuffer);
    17142129}
    17152130
     
    17262141    }
    17272142
    1728     PVMSVGA3DBACKENDSURFACE pBackendSurface = (PVMSVGA3DBACKENDSURFACE)RTMemAllocZ(sizeof(VMSVGA3DBACKENDSURFACE));
    1729     AssertPtrReturn(pBackendSurface, VERR_NO_MEMORY);
     2143    PVMSVGA3DBACKENDSURFACE pBackendSurface;
     2144    int rc = dxBackendSurfaceAlloc(&pBackendSurface);
     2145    AssertRCReturn(rc, rc);
    17302146
    17312147    HRESULT hr;
     
    17432159        }
    17442160        else
    1745             D3D_RELEASE(pBackendSurface->u.Buffer.pBuffer);
     2161            D3D_RELEASE(pBackendSurface->u.pBuffer);
    17462162    }
    17472163    else if (pSurface->surfaceFlags & (  SVGA3D_SURFACE_BIND_VERTEX_BUFFER
     
    17572173        }
    17582174        else
    1759             D3D_RELEASE(pBackendSurface->u.Buffer.pBuffer);
     2175            D3D_RELEASE(pBackendSurface->u.pBuffer);
    17602176    }
    17612177    else
     
    18012217    }
    18022218
    1803     rc = RTLdrLoadSystem("D3DCompiler_47", /* fNoUnload = */ true, &pBackend->hD3DCompiler);
    1804     AssertRC(rc);
    18052219    if (RT_SUCCESS(rc))
    18062220    {
    1807         rc = RTLdrGetSymbol(pBackend->hD3DCompiler, "D3DDisassemble", (void **)&pBackend->pfnD3DDisassemble);
    1808         AssertRC(rc);
    1809     }
    1810 
     2221        /* Failure to load the shader disassembler is ignored. */
     2222        int rc2 = RTLdrLoadSystem("D3DCompiler_47", /* fNoUnload = */ true, &pBackend->hD3DCompiler);
     2223        AssertRC(rc2);
     2224        if (RT_SUCCESS(rc2))
     2225        {
     2226            rc2 = RTLdrGetSymbol(pBackend->hD3DCompiler, "D3DDisassemble", (void **)&pBackend->pfnD3DDisassemble);
     2227            AssertRC(rc2);
     2228        }
     2229        Log6Func(("Load D3DDisassemble: %Rrc\n", rc2));
     2230    }
     2231//ASMBreakpoint();
    18112232    return rc;
    18122233}
     
    18352256    if (pState->pBackend)
    18362257    {
     2258        /** @todo Clean up backends. */
     2259
    18372260        dxDeviceDestroy(pState->pBackend, &pState->pBackend->device);
    18382261
     
    20792502
    20802503
    2081 static DECLCALLBACK(int) dxContextFlushShared(PAVLU32NODECORE pCore, void *pvUser)
    2082 {
    2083     PVMSVGA3DSTATE pState = (PVMSVGA3DSTATE)pvUser;
    2084 
    2085     // Flush pCore->Key context and issue a query.
    2086     PVMSVGA3DDXCONTEXT pDXContextDrawing;
    2087     int rc = vmsvga3dDXContextFromCid(pState, pCore->Key, &pDXContextDrawing);
    2088     AssertRCReturn(rc, rc);
    2089 
    2090     DXDEVICE *pDeviceDrawing = &pDXContextDrawing->pBackendDXContext->device;
    2091     pDeviceDrawing->pImmediateContext->Flush();
    2092 
    2093     ID3D11Query *pQuery = 0;
    2094     D3D11_QUERY_DESC qd;
    2095     RT_ZERO(qd);
    2096     qd.Query = D3D11_QUERY_EVENT;
    2097     HRESULT hr2 = pDeviceDrawing->pDevice->CreateQuery(&qd, &pQuery);
    2098     Assert(hr2 == S_OK); RT_NOREF(hr2);
    2099 
    2100     pDeviceDrawing->pImmediateContext->End(pQuery);
    2101 
    2102     BOOL queryData;
    2103     while (pDeviceDrawing->pImmediateContext->GetData(pQuery, &queryData, sizeof(queryData), 0) != S_OK)
    2104     {
    2105         RTThreadYield();
    2106     }
    2107     D3D_RELEASE(pQuery);
    2108 
    2109     return VINF_SUCCESS;
    2110 }
    2111 
    2112 
    2113 
    2114 static DECLCALLBACK(int) vmsvga3dSurfaceMap(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dSurfaceImageId const *pImage, SVGA3dBox const *pBox,
    2115                                             VMSVGA3D_SURFACE_MAP enmMapType, VMSVGA3D_MAPPED_SURFACE *pMap)
     2504static DECLCALLBACK(int) vmsvga3dBackSurfaceMap(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, SVGA3dBox const *pBox,
     2505                                                VMSVGA3D_SURFACE_MAP enmMapType, VMSVGA3D_MAPPED_SURFACE *pMap)
    21162506{
    21172507    PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
     
    21252515    AssertRCReturn(rc, rc);
    21262516
    2127     Assert(pImage->face == 0 && pImage->mipmap == 0); /** @todo implement. */
    2128 
    2129     PVMSVGA3DDXCONTEXT pDXContext;
    2130 
    21312517    PVMSVGA3DBACKENDSURFACE pBackendSurface = pSurface->pBackendSurface;
    2132     if (!pBackendSurface)
    2133     {
    2134         /* This means that the guest uploads new data to the surface.
    2135          * So the data should be copied either to a host memory buffer,
    2136          * or to the actual surface.
    2137          * In the latter case the BackendSurface must be created here.
    2138          */
    2139         rc = vmsvga3dDXContextFromCid(pState, idDXContext, &pDXContext);
    2140         AssertRCReturn(rc, rc);
    2141 
    2142         rc = vmsvga3dBackSurfaceCreate(pThisCC, pDXContext, pSurface);
    2143         AssertRCReturn(rc, rc);
    2144 
    2145         pBackendSurface = pSurface->pBackendSurface;
    2146     }
    2147 
    2148     /* Figure out whether the surface belongs to a DXContext or to the backend. */
    2149     if (pSurface->idAssociatedContext != SVGA_ID_INVALID)
    2150         vmsvga3dDXContextFromCid(pState, pSurface->idAssociatedContext, &pDXContext);
    2151     else
    2152         pDXContext = NULL;
    2153 
    2154     DXDEVICE *pDevice;
    2155     if (pDXContext)
    2156         pDevice = &pDXContext->pBackendDXContext->device;
    2157     else
    2158         pDevice = &pBackend->device;
    2159     AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
     2518    AssertPtrReturn(pBackendSurface, VERR_INVALID_STATE);
     2519
     2520    PVMSVGA3DMIPMAPLEVEL pMipLevel;
     2521    rc = vmsvga3dMipmapLevel(pSurface, pImage->face, pImage->mipmap, &pMipLevel);
     2522    ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
     2523
     2524    /* A surface is always mapped by the DX context which has created the surface. */
     2525    DXDEVICE *pDevice = dxDeviceFromCid(pSurface->idAssociatedContext, pState);
     2526    AssertReturn(pDevice && pDevice->pDevice, VERR_INVALID_STATE);
    21602527
    21612528    SVGA3dBox clipBox;
     
    21632530    {
    21642531        clipBox = *pBox;
    2165         vmsvgaR3ClipBox(&pSurface->paMipmapLevels[0].mipmapSize, &clipBox);
     2532        vmsvgaR3ClipBox(&pMipLevel->mipmapSize, &clipBox);
    21662533        ASSERT_GUEST_RETURN(clipBox.w && clipBox.h && clipBox.d, VERR_INVALID_PARAMETER);
    21672534    }
     
    21712538        clipBox.y = 0;
    21722539        clipBox.z = 0;
    2173         clipBox.w = pSurface->paMipmapLevels[0].mipmapSize.width;
    2174         clipBox.h = pSurface->paMipmapLevels[0].mipmapSize.height;
    2175         clipBox.d = pSurface->paMipmapLevels[0].mipmapSize.depth;
     2540        clipBox.w = pMipLevel->mipmapSize.width;
     2541        clipBox.h = pMipLevel->mipmapSize.height;
     2542        clipBox.d = pMipLevel->mipmapSize.depth;
    21762543    }
    21772544
     
    21942561    if (pBackendSurface->enmResType == VMSVGA3D_RESTYPE_SCREEN_TARGET)
    21952562    {
     2563        Assert(pImage->face == 0 && pImage->mipmap == 0);
     2564
    21962565        ID3D11Texture2D *pMappedTexture;
    21972566        if (enmMapType == VMSVGA3D_SURFACE_MAP_READ)
    21982567        {
    2199             pMappedTexture = pBackendSurface->u.ScreenTarget.pStagingTexture;
    2200 
    2201             /** @todo Wait for the surface to finish drawing. */
    2202             if (pBackendSurface->SharedTextureTree)
     2568            pMappedTexture = pBackendSurface->pStagingTexture;
     2569
     2570            /* Wait for the surface to finish drawing. */
     2571            if (pBackendSurface->cidDrawing != SVGA_ID_INVALID)
    22032572            {
    2204                  int rc2 = RTAvlU32DoWithAll(&pBackendSurface->SharedTextureTree, 0, dxContextFlushShared, pState);
    2205                  AssertRC(rc2);
     2573                dxContextWait(pBackendSurface->cidDrawing, pState);
     2574                pBackendSurface->cidDrawing = SVGA_ID_INVALID;
    22062575            }
    22072576
    22082577            /* Copy the texture content to the staging texture. */
    2209             pDevice->pImmediateContext->CopyResource(pBackendSurface->u.ScreenTarget.pStagingTexture, pBackendSurface->u.ScreenTarget.pTexture);
     2578            pDevice->pImmediateContext->CopyResource(pBackendSurface->pStagingTexture, pBackendSurface->u.pTexture2D);
    22102579        }
    22112580        else
    2212             pMappedTexture = pBackendSurface->u.ScreenTarget.pDynamicTexture;
     2581            pMappedTexture = pBackendSurface->pDynamicTexture;
    22132582
    22142583        UINT const Subresource = 0; /* Screen target surfaces have only one subresource. */
     
    22302599            rc = VERR_NOT_SUPPORTED;
    22312600    }
    2232     else if (pBackendSurface->enmResType == VMSVGA3D_RESTYPE_TEXTURE)
    2233     {
     2601    else if (   pBackendSurface->enmResType == VMSVGA3D_RESTYPE_TEXTURE
     2602             || pBackendSurface->enmResType == VMSVGA3D_RESTYPE_CUBE_TEXTURE
     2603             || pBackendSurface->enmResType == VMSVGA3D_RESTYPE_TEXTURE_3D)
     2604    {
     2605//Assert(pImage->face == 0 && pImage->mipmap == 0);
     2606if (!pBackendSurface->pStagingTexture || !pBackendSurface->pDynamicTexture) return VERR_NOT_IMPLEMENTED;
    22342607        ID3D11Texture2D *pMappedTexture;
    22352608        if (enmMapType == VMSVGA3D_SURFACE_MAP_READ)
    22362609        {
    2237             pMappedTexture = pBackendSurface->u.ScreenTarget.pStagingTexture;
    2238 
    2239             /* Copy the texture content to the staging texture. */
    2240             pDevice->pImmediateContext->CopyResource(pBackendSurface->u.ScreenTarget.pStagingTexture, pBackendSurface->u.ScreenTarget.pTexture);
     2610            pMappedTexture = pBackendSurface->pStagingTexture;
     2611
     2612            /* Wait for the surface to finish drawing. */
     2613            if (pBackendSurface->cidDrawing != SVGA_ID_INVALID)
     2614            {
     2615                dxContextWait(pBackendSurface->cidDrawing, pState);
     2616                pBackendSurface->cidDrawing = SVGA_ID_INVALID;
     2617            }
     2618
     2619            /* Copy the texture content to the staging texture.
     2620             * The requested miplevel of the texture is copied to the miplevel 0 of the staging texture,
     2621             * because the staging (and dynamic) structures do not have miplevels.
     2622             * Always copy entire miplevel so all Dst are zero and pSrcBox is NULL, as D3D11 requires.
     2623             */
     2624            ID3D11Resource *pDstResource = pBackendSurface->pStagingTexture;
     2625            UINT DstSubresource = 0;
     2626            UINT DstX = 0;
     2627            UINT DstY = 0;
     2628            UINT DstZ = 0;
     2629            ID3D11Resource *pSrcResource = pBackendSurface->u.pTexture2D;
     2630            UINT SrcSubresource = D3D11CalcSubresource(pImage->mipmap, pImage->face, pSurface->cLevels);
     2631            D3D11_BOX *pSrcBox = NULL;
     2632            //D3D11_BOX SrcBox;
     2633            //SrcBox.left   = 0;
     2634            //SrcBox.top    = 0;
     2635            //SrcBox.front  = 0;
     2636            //SrcBox.right  = pMipLevel->mipmapSize.width;
     2637            //SrcBox.bottom = pMipLevel->mipmapSize.height;
     2638            //SrcBox.back   = pMipLevel->mipmapSize.depth;
     2639            pDevice->pImmediateContext->CopySubresourceRegion(pDstResource, DstSubresource, DstX, DstY, DstZ,
     2640                                                              pSrcResource, SrcSubresource, pSrcBox);
    22412641        }
    22422642        else
    2243             pMappedTexture = pBackendSurface->u.ScreenTarget.pDynamicTexture;
    2244 
    2245         UINT const Subresource = 0; /* Screen target surfaces have only one subresource. */
     2643            pMappedTexture = pBackendSurface->pDynamicTexture;
     2644
     2645        UINT const Subresource = 0;
    22462646        HRESULT hr = pDevice->pImmediateContext->Map(pMappedTexture, Subresource,
    22472647                                                     d3d11MapType, /* MapFlags =  */ 0, &mappedResource);
     
    22642664    {
    22652665        UINT const Subresource = 0; /* Buffers have only one subresource. */
    2266         HRESULT hr = pDevice->pImmediateContext->Map(pSurface->pBackendSurface->u.Buffer.pBuffer, Subresource,
     2666        HRESULT hr = pDevice->pImmediateContext->Map(pSurface->pBackendSurface->u.pBuffer, Subresource,
    22672667                                                     d3d11MapType, /* MapFlags =  */ 0, &mappedResource);
    22682668        if (SUCCEEDED(hr))
     
    22932693
    22942694
    2295 static DECLCALLBACK(int) vmsvga3dSurfaceUnmap(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, VMSVGA3D_MAPPED_SURFACE *pMap, bool fWritten)
     2695static DECLCALLBACK(int) vmsvga3dBackSurfaceUnmap(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, VMSVGA3D_MAPPED_SURFACE *pMap, bool fWritten)
    22962696{
    22972697    PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
     
    23092709    AssertReturn(pBackendSurface, VERR_INVALID_PARAMETER);
    23102710
    2311     PVMSVGA3DDXCONTEXT pDXContext;
    2312     /* Figure out whether the surface belongs to a DXContext or to the backend. */
    2313     if (pSurface->idAssociatedContext != SVGA_ID_INVALID)
    2314         vmsvga3dDXContextFromCid(pState, pSurface->idAssociatedContext, &pDXContext);
    2315     else
    2316         pDXContext = NULL;
    2317 
    2318     DXDEVICE *pDevice;
    2319     if (pDXContext)
    2320         pDevice = &pDXContext->pBackendDXContext->device;
    2321     else
    2322         pDevice = &pBackend->device;
    2323     AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
     2711    PVMSVGA3DMIPMAPLEVEL pMipLevel;
     2712    rc = vmsvga3dMipmapLevel(pSurface, pImage->face, pImage->mipmap, &pMipLevel);
     2713    ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
     2714
     2715    /* A surface is always mapped by the DX context which has created the surface. */
     2716    DXDEVICE *pDevice = dxDeviceFromCid(pSurface->idAssociatedContext, pState);
     2717    AssertReturn(pDevice && pDevice->pDevice, VERR_INVALID_STATE);
    23242718
    23252719    if (pBackendSurface->enmResType == VMSVGA3D_RESTYPE_SCREEN_TARGET)
     
    23272721        ID3D11Texture2D *pMappedTexture;
    23282722        if (pMap->enmMapType == VMSVGA3D_SURFACE_MAP_READ)
    2329             pMappedTexture = pBackendSurface->u.ScreenTarget.pStagingTexture;
     2723            pMappedTexture = pBackendSurface->pStagingTexture;
    23302724        else
    2331             pMappedTexture = pBackendSurface->u.ScreenTarget.pDynamicTexture;
     2725            pMappedTexture = pBackendSurface->pDynamicTexture;
    23322726
    23332727        UINT const Subresource = 0; /* Screen target surfaces have only one subresource. */
     
    23392733                || pMap->enmMapType == VMSVGA3D_SURFACE_MAP_WRITE_DISCARD))
    23402734        {
    2341             ID3D11Resource *pDstResource = pBackendSurface->u.ScreenTarget.pTexture;
     2735            ID3D11Resource *pDstResource = pBackendSurface->u.pTexture2D;
    23422736            UINT DstSubresource = Subresource;
    23432737            UINT DstX = pMap->box.x;
    23442738            UINT DstY = pMap->box.y;
    23452739            UINT DstZ = pMap->box.z;
    2346             ID3D11Resource *pSrcResource = pBackendSurface->u.ScreenTarget.pDynamicTexture;
     2740            ID3D11Resource *pSrcResource = pBackendSurface->pDynamicTexture;
    23472741            UINT SrcSubresource = Subresource;
    23482742            D3D11_BOX SrcBox;
     
    23532747            SrcBox.bottom = pMap->box.y + pMap->box.h;
    23542748            SrcBox.back   = pMap->box.z + pMap->box.d;
     2749
    23552750            pDevice->pImmediateContext->CopySubresourceRegion(pDstResource, DstSubresource, DstX, DstY, DstZ,
    23562751                                                              pSrcResource, SrcSubresource, &SrcBox);
     2752
     2753            pBackendSurface->cidDrawing = pSurface->idAssociatedContext;
    23572754        }
    23582755    }
    2359     else if (pBackendSurface->enmResType == VMSVGA3D_RESTYPE_TEXTURE)
     2756    else if (   pBackendSurface->enmResType == VMSVGA3D_RESTYPE_TEXTURE
     2757             || pBackendSurface->enmResType == VMSVGA3D_RESTYPE_CUBE_TEXTURE
     2758             || pBackendSurface->enmResType == VMSVGA3D_RESTYPE_TEXTURE_3D)
    23602759    {
    23612760        ID3D11Texture2D *pMappedTexture;
    23622761        if (pMap->enmMapType == VMSVGA3D_SURFACE_MAP_READ)
    2363             pMappedTexture = pBackendSurface->u.ScreenTarget.pStagingTexture;
     2762            pMappedTexture = pBackendSurface->pStagingTexture;
    23642763        else
    2365             pMappedTexture = pBackendSurface->u.ScreenTarget.pDynamicTexture;
    2366 
    2367         UINT const Subresource = 0; /* Screen target surfaces have only one subresource. */
     2764            pMappedTexture = pBackendSurface->pDynamicTexture;
     2765
     2766        UINT const Subresource = 0; /* Staging or dynamic textures have one subresource. */
    23682767        pDevice->pImmediateContext->Unmap(pMappedTexture, Subresource);
    23692768
     
    23732772                || pMap->enmMapType == VMSVGA3D_SURFACE_MAP_WRITE_DISCARD))
    23742773        {
    2375             ID3D11Resource *pDstResource = pBackendSurface->u.ScreenTarget.pTexture;
    2376             UINT DstSubresource = Subresource;
     2774            ID3D11Resource *pDstResource = pBackendSurface->u.pTexture2D;
     2775            UINT DstSubresource = D3D11CalcSubresource(pImage->mipmap, pImage->face, pSurface->cLevels);
    23772776            UINT DstX = pMap->box.x;
    23782777            UINT DstY = pMap->box.y;
    23792778            UINT DstZ = pMap->box.z;
    2380             ID3D11Resource *pSrcResource = pBackendSurface->u.ScreenTarget.pDynamicTexture;
     2779            ID3D11Resource *pSrcResource = pBackendSurface->pDynamicTexture;
    23812780            UINT SrcSubresource = Subresource;
    23822781            D3D11_BOX SrcBox;
     
    23872786            SrcBox.bottom = pMap->box.y + pMap->box.h;
    23882787            SrcBox.back   = pMap->box.z + pMap->box.d;
     2788
    23892789            pDevice->pImmediateContext->CopySubresourceRegion(pDstResource, DstSubresource, DstX, DstY, DstZ,
    23902790                                                              pSrcResource, SrcSubresource, &SrcBox);
     2791
     2792            pBackendSurface->cidDrawing = pSurface->idAssociatedContext;
    23912793        }
    23922794    }
     
    23942796    {
    23952797        UINT const Subresource = 0; /* Buffers have only one subresource. */
    2396         pDevice->pImmediateContext->Unmap(pBackendSurface->u.Buffer.pBuffer, Subresource);
     2798        pDevice->pImmediateContext->Unmap(pBackendSurface->u.pBuffer, Subresource);
    23972799    }
    23982800    else
     
    24872889    ASSERT_GUEST_RETURN(clipRect.w && clipRect.h, VERR_INVALID_PARAMETER);
    24882890
     2891    /* Wait for the surface to finish drawing. */
     2892    if (pBackendSurface->cidDrawing != SVGA_ID_INVALID)
     2893    {
     2894        dxContextWait(pBackendSurface->cidDrawing, pState);
     2895        pBackendSurface->cidDrawing = SVGA_ID_INVALID;
     2896    }
     2897
    24892898    /* Copy the screen texture to the shared surface. */
    24902899    DWORD result = pHwScreen->pDXGIKeyedMutex->AcquireSync(0, 10000);
    24912900    if (result == WAIT_OBJECT_0)
    24922901    {
    2493         ID3D11Query *pQuery = 0;
    2494         D3D11_QUERY_DESC qd;
    2495         RT_ZERO(qd);
    2496         qd.Query = D3D11_QUERY_EVENT;
    2497         HRESULT hr2 = pBackend->device.pDevice->CreateQuery(&qd, &pQuery);
    2498         Assert(hr2 == S_OK); RT_NOREF(hr2);
    2499 
    2500         pBackend->device.pImmediateContext->CopyResource(pHwScreen->pTexture, pBackendSurface->u.ScreenTarget.pTexture);
    2501 
    2502         pBackend->device.pImmediateContext->Flush();
    2503 
    2504         pBackend->device.pImmediateContext->End(pQuery);
    2505 
    2506         BOOL queryData;
    2507         while (pBackend->device.pImmediateContext->GetData(pQuery, &queryData, sizeof(queryData), 0) != S_OK)
    2508         {
    2509             RTThreadYield();
    2510         }
    2511         D3D_RELEASE(pQuery);
     2902        pBackend->device.pImmediateContext->CopyResource(pHwScreen->pTexture, pBackendSurface->u.pTexture2D);
     2903
     2904        dxDeviceFlush(&pBackend->device);
    25122905
    25132906        result = pHwScreen->pDXGIKeyedMutex->ReleaseSync(1);
     
    30973490                               uint32_t cCopyBoxes, SVGA3dCopyBox *pBox)
    30983491{
    3099     RT_NOREF(dest, src, cCopyBoxes, pBox);
     3492    RT_NOREF(cCopyBoxes, pBox);
     3493
     3494    LogFunc(("src sid %d -> dst sid %d\n", src.sid, dest.sid));
    31003495
    31013496    PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
    31023497    AssertReturn(pState, VERR_INVALID_STATE);
    31033498
    3104     AssertFailed();
    3105     return VERR_NOT_IMPLEMENTED;
     3499    PVMSVGA3DBACKEND pBackend = pState->pBackend;
     3500
     3501    PVMSVGA3DSURFACE pSrcSurface;
     3502    int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, src.sid, &pSrcSurface);
     3503    AssertRCReturn(rc, rc);
     3504
     3505    PVMSVGA3DSURFACE pDstSurface;
     3506    rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, dest.sid, &pDstSurface);
     3507    AssertRCReturn(rc, rc);
     3508
     3509    LogFunc(("src%s cid %d -> dst%s cid %d\n",
     3510             pSrcSurface->pBackendSurface ? "" : " sysmem",
     3511             pSrcSurface ? pSrcSurface->idAssociatedContext : SVGA_ID_INVALID,
     3512             pDstSurface->pBackendSurface ? "" : " sysmem",
     3513             pDstSurface ? pDstSurface->idAssociatedContext : SVGA_ID_INVALID));
     3514
     3515    //DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
     3516    //AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
     3517
     3518    if (pSrcSurface->pBackendSurface)
     3519    {
     3520        if (pDstSurface->pBackendSurface == NULL)
     3521        {
     3522            /* Create the target if it can be used as a device context shared resource (render or screen target). */
     3523            if (dxIsSurfaceShareable(pDstSurface))
     3524            {
     3525                rc = vmsvga3dBackSurfaceCreateTexture(pThisCC, NULL, pDstSurface);
     3526                AssertRCReturn(rc, rc);
     3527            }
     3528        }
     3529
     3530        if (pDstSurface->pBackendSurface)
     3531        {
     3532            /* Surface -> Surface. */
     3533            /* Expect both of them to be shared surfaces created by the backend context. */
     3534            Assert(pSrcSurface->idAssociatedContext == DX_CID_BACKEND && pDstSurface->idAssociatedContext == DX_CID_BACKEND);
     3535
     3536            DXDEVICE *pDXDevice = &pBackend->device;
     3537
     3538            /* Clip the box. */
     3539            PVMSVGA3DMIPMAPLEVEL pSrcMipLevel;
     3540            rc = vmsvga3dMipmapLevel(pSrcSurface, src.face, src.mipmap, &pSrcMipLevel);
     3541            ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
     3542
     3543            PVMSVGA3DMIPMAPLEVEL pDstMipLevel;
     3544            rc = vmsvga3dMipmapLevel(pDstSurface, dest.face, dest.mipmap, &pDstMipLevel);
     3545            ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
     3546
     3547            SVGA3dCopyBox clipBox = *pBox;
     3548            vmsvgaR3ClipCopyBox(&pSrcMipLevel->mipmapSize, &pDstMipLevel->mipmapSize, &clipBox);
     3549
     3550            UINT DstSubresource = vmsvga3dCalcSubresource(dest.mipmap, dest.face, pDstSurface->cLevels);
     3551            UINT DstX = clipBox.x;
     3552            UINT DstY = clipBox.y;
     3553            UINT DstZ = clipBox.z;
     3554
     3555            UINT SrcSubresource = vmsvga3dCalcSubresource(src.mipmap, src.face, pSrcSurface->cLevels);
     3556            D3D11_BOX SrcBox;
     3557            SrcBox.left   = clipBox.srcx;
     3558            SrcBox.top    = clipBox.srcy;
     3559            SrcBox.front  = clipBox.srcz;
     3560            SrcBox.right  = clipBox.srcx + clipBox.w;
     3561            SrcBox.bottom = clipBox.srcy + clipBox.h;
     3562            SrcBox.back   = clipBox.srcz + clipBox.d;
     3563
     3564            Assert(cCopyBoxes == 1); /** @todo */
     3565
     3566            ID3D11Resource *pDstResource;
     3567            ID3D11Resource *pSrcResource;
     3568            pDstResource = dxResource(pState, pDstSurface, NULL);
     3569            pSrcResource = dxResource(pState, pSrcSurface, NULL);
     3570
     3571            pDXDevice->pImmediateContext->CopySubresourceRegion(pDstResource, DstSubresource, DstX, DstY, DstZ,
     3572                                                                pSrcResource, SrcSubresource, &SrcBox);
     3573
     3574            pDstSurface->pBackendSurface->cidDrawing = DX_CID_BACKEND;
     3575        }
     3576        else
     3577        {
     3578            /* Surface -> Memory. */
     3579            AssertFailed(); /** @todo implement */
     3580        }
     3581    }
     3582    else
     3583    {
     3584        /* Memory -> Surface. */
     3585        AssertFailed(); /** @todo implement */
     3586    }
     3587
     3588    return rc;
    31063589}
    31073590
     
    34053888    pSurface->pBackendSurface = NULL;
    34063889
     3890    LogFunc(("sid=%u\n", pSurface->id));
     3891
    34073892    if (pBackendSurface->enmResType == VMSVGA3D_RESTYPE_SCREEN_TARGET)
    34083893    {
    3409         D3D_RELEASE(pBackendSurface->u.ScreenTarget.pStagingTexture);
    3410         D3D_RELEASE(pBackendSurface->u.ScreenTarget.pDynamicTexture);
    3411         D3D_RELEASE(pBackendSurface->u.ScreenTarget.pTexture);
    3412     }
    3413     else if (pBackendSurface->enmResType == VMSVGA3D_RESTYPE_TEXTURE)
    3414     {
    3415         D3D_RELEASE(pBackendSurface->u.Texture.pStagingTexture);
    3416         D3D_RELEASE(pBackendSurface->u.Texture.pDynamicTexture);
    3417         D3D_RELEASE(pBackendSurface->u.Texture.pTexture);
     3894        D3D_RELEASE(pBackendSurface->pStagingTexture);
     3895        D3D_RELEASE(pBackendSurface->pDynamicTexture);
     3896        D3D_RELEASE(pBackendSurface->u.pTexture2D);
     3897    }
     3898    else if (   pBackendSurface->enmResType == VMSVGA3D_RESTYPE_TEXTURE
     3899             || pBackendSurface->enmResType == VMSVGA3D_RESTYPE_CUBE_TEXTURE)
     3900    {
     3901        D3D_RELEASE(pBackendSurface->pStagingTexture);
     3902        D3D_RELEASE(pBackendSurface->pDynamicTexture);
     3903        D3D_RELEASE(pBackendSurface->u.pTexture2D);
     3904    }
     3905    else if (pBackendSurface->enmResType == VMSVGA3D_RESTYPE_TEXTURE_3D)
     3906    {
     3907        D3D_RELEASE(pBackendSurface->pStagingTexture3D);
     3908        D3D_RELEASE(pBackendSurface->pDynamicTexture3D);
     3909        D3D_RELEASE(pBackendSurface->u.pTexture3D);
    34183910    }
    34193911    else if (pBackendSurface->enmResType == VMSVGA3D_RESTYPE_BUFFER)
    34203912    {
    3421         D3D_RELEASE(pBackendSurface->u.Buffer.pBuffer);
     3913        D3D_RELEASE(pBackendSurface->u.pBuffer);
    34223914    }
    34233915    else
     
    34273919
    34283920    RTMemFree(pBackendSurface);
     3921
     3922    /* No context has created the surface, because the surface does not exist anymore. */
     3923    pSurface->idAssociatedContext = SVGA_ID_INVALID;
    34293924}
    34303925
     
    35254020
    35264021        VMSVGA3D_MAPPED_SURFACE map;
    3527         rc = vmsvga3dSurfaceMap(pThisCC, pSurface->idAssociatedContext, &image, &box, VMSVGA3D_SURFACE_MAP_WRITE_DISCARD, &map);
     4022        rc = vmsvga3dBackSurfaceMap(pThisCC, &image, &box, VMSVGA3D_SURFACE_MAP_WRITE_DISCARD, &map);
    35284023        if (RT_SUCCESS(rc))
    35294024        {
     
    35534048            //vmsvga3dMapWriteBmpFile(&map, "Dynamic");
    35544049
    3555             vmsvga3dSurfaceUnmap(pThisCC, &image, &map, /* fWritten = */ true);
     4050            vmsvga3dBackSurfaceUnmap(pThisCC, &image, &map, /* fWritten = */ true);
    35564051        }
    35574052#if 0
    35584053//ASMBreakpoint();
    3559             rc = vmsvga3dSurfaceMap(pThisCC, &image, NULL, VMSVGA3D_SURFACE_MAP_READ, &map);
     4054            rc = vmsvga3dBackSurfaceMap(pThisCC, &image, NULL, VMSVGA3D_SURFACE_MAP_READ, &map);
    35604055            if (RT_SUCCESS(rc))
    35614056            {
    35624057                vmsvga3dMapWriteBmpFile(&map, "Staging");
    35634058
    3564                 vmsvga3dSurfaceUnmap(pThisCC, &image, &map, /* fWritten =  */ false);
     4059                vmsvga3dBackSurfaceUnmap(pThisCC, &image, &map, /* fWritten =  */ false);
    35654060            }
    35664061#endif
     
    36044099
    36054100        VMSVGA3D_MAPPED_SURFACE map;
    3606         rc = vmsvga3dSurfaceMap(pThisCC, pSurface->idAssociatedContext, &image, &box, enmMap, &map);
     4101        rc = vmsvga3dBackSurfaceMap(pThisCC, &image, &box, enmMap, &map);
    36074102        if (RT_SUCCESS(rc))
    36084103        {
     
    36294124
    36304125            bool const fWritten = (transfer == SVGA3D_WRITE_HOST_VRAM);
    3631             vmsvga3dSurfaceUnmap(pThisCC, &image, &map, fWritten);
     4126            vmsvga3dBackSurfaceUnmap(pThisCC, &image, &map, fWritten);
    36324127        }
    36334128    }
     
    37174212    pDXContext->pBackendDXContext = pBackendDXContext;
    37184213
     4214    LogFunc(("cid %d\n", pDXContext->cid));
     4215
    37194216    int rc = dxDeviceCreate(pBackend, &pBackendDXContext->device);
    37204217    return rc;
     
    37264223    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    37274224
     4225    LogFunc(("cid %d\n", pDXContext->cid));
     4226
    37284227    if (pDXContext->pBackendDXContext)
    37294228    {
    3730         dxDeviceDestroy(pBackend, &pDXContext->pBackendDXContext->device);
    3731 
    3732         RTMemFree(pDXContext->pBackendDXContext);
     4229        /* Clean up context resources. */
     4230        VMSVGA3DBACKENDDXCONTEXT *pBackendDXContext = pDXContext->pBackendDXContext;
     4231
     4232        if (pBackendDXContext->papRenderTargetView)
     4233            DX_RELEASE_ARRAY(pBackendDXContext->cRenderTargetView, pBackendDXContext->papRenderTargetView);
     4234        if (pBackendDXContext->papDepthStencilView)
     4235            DX_RELEASE_ARRAY(pBackendDXContext->cDepthStencilView, pBackendDXContext->papDepthStencilView);
     4236        if (pBackendDXContext->papShaderResourceView)
     4237            DX_RELEASE_ARRAY(pBackendDXContext->cShaderResourceView, pBackendDXContext->papShaderResourceView);
     4238        if (pBackendDXContext->paElementLayout)
     4239        {
     4240            for (uint32_t i = 0; i < pBackendDXContext->cElementLayout; ++i)
     4241                D3D_RELEASE(pBackendDXContext->paElementLayout[i].pElementLayout);
     4242        }
     4243        if (pBackendDXContext->papBlendState)
     4244            DX_RELEASE_ARRAY(pBackendDXContext->cBlendState, pBackendDXContext->papBlendState);
     4245        if (pBackendDXContext->papDepthStencilState)
     4246            DX_RELEASE_ARRAY(pBackendDXContext->cDepthStencilState, pBackendDXContext->papDepthStencilState);
     4247        if (pBackendDXContext->papRasterizerState)
     4248            DX_RELEASE_ARRAY(pBackendDXContext->cRasterizerState, pBackendDXContext->papRasterizerState);
     4249        if (pBackendDXContext->papSamplerState)
     4250            DX_RELEASE_ARRAY(pBackendDXContext->cSamplerState, pBackendDXContext->papSamplerState);
     4251        if (pBackendDXContext->papQuery)
     4252            DX_RELEASE_ARRAY(pBackendDXContext->cQuery, pBackendDXContext->papQuery);
     4253        if (pBackendDXContext->paShader)
     4254        {
     4255            for (uint32_t i = 0; i < pBackendDXContext->cShader; ++i)
     4256                D3D_RELEASE(pBackendDXContext->paShader[i].pShader);
     4257        }
     4258
     4259        RTMemFreeZ(pBackendDXContext->papBlendState, sizeof(pBackendDXContext->papBlendState[0]) * pBackendDXContext->cBlendState);
     4260        RTMemFreeZ(pBackendDXContext->papDepthStencilState, sizeof(pBackendDXContext->papDepthStencilState[0]) * pBackendDXContext->cDepthStencilState);
     4261        RTMemFreeZ(pBackendDXContext->papSamplerState, sizeof(pBackendDXContext->papSamplerState[0]) * pBackendDXContext->cSamplerState);
     4262        RTMemFreeZ(pBackendDXContext->papRasterizerState, sizeof(pBackendDXContext->papRasterizerState[0]) * pBackendDXContext->cRasterizerState);
     4263        RTMemFreeZ(pBackendDXContext->paElementLayout, sizeof(pBackendDXContext->paElementLayout[0]) * pBackendDXContext->cElementLayout);
     4264        RTMemFreeZ(pBackendDXContext->papRenderTargetView, sizeof(pBackendDXContext->papRenderTargetView[0]) * pBackendDXContext->cRenderTargetView);
     4265        RTMemFreeZ(pBackendDXContext->papDepthStencilView, sizeof(pBackendDXContext->papDepthStencilView[0]) * pBackendDXContext->cDepthStencilView);
     4266        RTMemFreeZ(pBackendDXContext->papShaderResourceView, sizeof(pBackendDXContext->papShaderResourceView[0]) * pBackendDXContext->cShaderResourceView);
     4267        RTMemFreeZ(pBackendDXContext->papQuery, sizeof(pBackendDXContext->papQuery[0]) * pBackendDXContext->cQuery);
     4268        RTMemFreeZ(pBackendDXContext->paShader, sizeof(pBackendDXContext->paShader[0]) * pBackendDXContext->cShader);
     4269
     4270        /* Destroy backend surfaces which belong to this context. */
     4271        /** @todo The context should have a list of surfaces (and also shared resources). */
     4272        for (uint32_t sid = 0; sid < pThisCC->svga.p3dState->cSurfaces; ++sid)
     4273        {
     4274            PVMSVGA3DSURFACE const pSurface = pThisCC->svga.p3dState->papSurfaces[sid];
     4275            if (   pSurface
     4276                && pSurface->id == sid)
     4277            {
     4278                if (pSurface->idAssociatedContext == pDXContext->cid)
     4279                {
     4280                    Assert(pSurface->pBackendSurface);
     4281                    vmsvga3dBackSurfaceDestroy(pThisCC, pSurface);
     4282                }
     4283                else if (pSurface->idAssociatedContext == DX_CID_BACKEND)
     4284                {
     4285                    /* May have shared resources in this context. */
     4286                    Assert(pSurface->pBackendSurface);
     4287                    DXSHAREDTEXTURE *pSharedTexture = (DXSHAREDTEXTURE *)RTAvlU32Get(&pSurface->pBackendSurface->SharedTextureTree, pDXContext->cid);
     4288                    if (pSharedTexture)
     4289                    {
     4290                        Assert(pSharedTexture->sid == sid);
     4291                        RTAvlU32Remove(&pSurface->pBackendSurface->SharedTextureTree, pDXContext->cid);
     4292                        D3D_RELEASE(pSharedTexture->pTexture);
     4293                        RTMemFreeZ(pSharedTexture, sizeof(*pSharedTexture));
     4294                    }
     4295                }
     4296            }
     4297        }
     4298
     4299        dxDeviceDestroy(pBackend, &pBackendDXContext->device);
     4300
     4301        RTMemFreeZ(pBackendDXContext, sizeof(*pBackendDXContext));
    37334302        pDXContext->pBackendDXContext = NULL;
    37344303    }
     
    37484317{
    37494318    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    3750 
    3751     RT_NOREF(pBackend, pDXContext);
    3752     AssertFailed(); /** @todo Implement */
    3753     return VERR_NOT_IMPLEMENTED;
     4319    RT_NOREF(pBackend, pDXContext);
     4320    return VINF_SUCCESS;
    37544321}
    37554322
     
    38154382        boxDst.d = 1;
    38164383
    3817         rc = vmsvgaR3UpdateGBSurfaceEx(pThisCC, pDXContext->cid, &imageId, &boxDst, &ptSrc);
     4384        rc = vmsvgaR3UpdateGBSurfaceEx(pThisCC, &imageId, &boxDst, &ptSrc);
    38184385        AssertRCReturn(rc, rc);
    38194386    }
    38204387
    3821     dxConstantBufferSet(pDevice, slot, type, pSurface->pBackendSurface->u.Buffer.pBuffer);
     4388    dxConstantBufferSet(pDevice, slot, type, pSurface->pBackendSurface->u.pBuffer);
    38224389    return VINF_SUCCESS;
    38234390}
     
    38654432    {
    38664433        pDXShader = &pDXContext->pBackendDXContext->paShader[pShader->id];
     4434        Assert(pDXShader->enmShaderType >= SVGA3D_SHADERTYPE_MIN && pDXShader->enmShaderType < SVGA3D_SHADERTYPE_MAX);
    38674435        uint32_t const idxShaderState = pDXShader->enmShaderType - SVGA3D_SHADERTYPE_MIN;
    38684436        pDXContext->svgaDXContext.shaderState[idxShaderState].shaderId = pShader->id;
     
    39804548    }
    39814549
     4550    /* Note which surfaces are being drawn. */
     4551    dxTrackRenderTargets(pThisCC, pDXContext);
     4552
    39824553    return VINF_SUCCESS;
    39834554}
     
    39954566
    39964567    pDevice->pImmediateContext->DrawIndexed(indexCount, startIndexLocation, baseVertexLocation);
     4568
     4569    /* Note which surfaces are being drawn. */
     4570    dxTrackRenderTargets(pThisCC, pDXContext);
     4571
    39974572    return VINF_SUCCESS;
    39984573}
    39994574
    40004575
    4001 static DECLCALLBACK(int) vmsvga3dBackDXDrawInstanced(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    4002 {
     4576static DECLCALLBACK(int) vmsvga3dBackDXDrawInstanced(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext,
     4577                                                     uint32_t vertexCountPerInstance, uint32_t instanceCount, uint32_t startVertexLocation, uint32_t startInstanceLocation)
     4578{
     4579    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     4580    RT_NOREF(pBackend);
     4581
     4582    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
     4583    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
     4584
    40034585    Assert(pDXContext->svgaDXContext.inputAssembly.topology != SVGA3D_PRIMITIVE_TRIANGLEFAN);
    4004     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4005 
    4006     RT_NOREF(pBackend, pDXContext);
    4007     AssertFailed(); /** @todo Implement */
    4008     return VERR_NOT_IMPLEMENTED;
    4009 }
    4010 
    4011 
    4012 static DECLCALLBACK(int) vmsvga3dBackDXDrawIndexedInstanced(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    4013 {
     4586
     4587    pDevice->pImmediateContext->DrawInstanced(vertexCountPerInstance, instanceCount, startVertexLocation, startInstanceLocation);
     4588
     4589    /* Note which surfaces are being drawn. */
     4590    dxTrackRenderTargets(pThisCC, pDXContext);
     4591
     4592    return VINF_SUCCESS;
     4593}
     4594
     4595
     4596static DECLCALLBACK(int) vmsvga3dBackDXDrawIndexedInstanced(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext,
     4597                                                            uint32_t indexCountPerInstance, uint32_t instanceCount, uint32_t startIndexLocation, int32_t baseVertexLocation, uint32_t startInstanceLocation)
     4598{
     4599    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     4600    RT_NOREF(pBackend);
     4601
     4602    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
     4603    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
     4604
    40144605    Assert(pDXContext->svgaDXContext.inputAssembly.topology != SVGA3D_PRIMITIVE_TRIANGLEFAN);
    4015     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4016 
    4017     RT_NOREF(pBackend, pDXContext);
    4018     AssertFailed(); /** @todo Implement */
    4019     return VERR_NOT_IMPLEMENTED;
     4606
     4607    pDevice->pImmediateContext->DrawIndexedInstanced(indexCountPerInstance, instanceCount, startIndexLocation, baseVertexLocation, startInstanceLocation);
     4608
     4609    /* Note which surfaces are being drawn. */
     4610    dxTrackRenderTargets(pThisCC, pDXContext);
     4611
     4612    return VINF_SUCCESS;
    40204613}
    40214614
     
    41194712                box.d = 1;
    41204713
    4121                 rc = vmsvgaR3UpdateGBSurface(pThisCC, pDXContext->cid, &imageId, &box);
     4714                rc = vmsvgaR3UpdateGBSurface(pThisCC, &imageId, &box);
    41224715                AssertRCReturn(rc, rc);
    41234716            }
    41244717
    4125             paResources[idxVertexBuffer] = pSurface->pBackendSurface->u.Buffer.pBuffer;
     4718            paResources[idxVertexBuffer] = pSurface->pBackendSurface->u.pBuffer;
    41264719            paStride[idxVertexBuffer] = paVertexBuffer[i].stride;
    41274720            paOffset[idxVertexBuffer] = paVertexBuffer[i].offset;
     
    41394732    }
    41404733
    4141     pDevice->pImmediateContext->IASetVertexBuffers(startBuffer, cVertexBuffer, paResources, paStride, paOffset);
     4734    pDevice->pImmediateContext->IASetVertexBuffers(startBuffer, cVertexBuffer,
     4735                                                   &paResources[startBuffer], &paStride[startBuffer], &paOffset[startBuffer]);
    41424736
    41434737    return VINF_SUCCESS;
     
    41864780            box.d = 1;
    41874781
    4188             rc = vmsvgaR3UpdateGBSurface(pThisCC, pDXContext->cid, &imageId, &box);
     4782            rc = vmsvgaR3UpdateGBSurface(pThisCC, &imageId, &box);
    41894783            AssertRCReturn(rc, rc);
    41904784        }
    41914785
    4192         pResource = pSurface->pBackendSurface->u.Buffer.pBuffer;
     4786        pResource = pSurface->pBackendSurface->u.pBuffer;
    41934787        enmDxgiFormat = vmsvgaDXSurfaceFormat2Dxgi(format);
    41944788        AssertReturn(enmDxgiFormat == DXGI_FORMAT_R16_UINT || enmDxgiFormat == DXGI_FORMAT_R32_UINT, VERR_INVALID_PARAMETER);
     
    45395133             (pDstSurface->surfaceFlags & SVGA3D_SURFACE_SCREENTARGET) ? " st" : ""));
    45405134
     5135    /* Clip the box. */
     5136    /** @todo Use [src|dst]SubResource to index p[Src|Dst]Surface->paMipmapLevels array directly. */
     5137    uint32_t iSrcFace;
     5138    uint32_t iSrcMipmap;
     5139    vmsvga3dCalcMipmapAndFace(pSrcSurface->cLevels, srcSubResource, &iSrcMipmap, &iSrcFace);
     5140
     5141    uint32_t iDstFace;
     5142    uint32_t iDstMipmap;
     5143    vmsvga3dCalcMipmapAndFace(pDstSurface->cLevels, dstSubResource, &iDstMipmap, &iDstFace);
     5144
     5145    PVMSVGA3DMIPMAPLEVEL pSrcMipLevel;
     5146    rc = vmsvga3dMipmapLevel(pSrcSurface, iSrcFace, iSrcMipmap, &pSrcMipLevel);
     5147    ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
     5148
     5149    PVMSVGA3DMIPMAPLEVEL pDstMipLevel;
     5150    rc = vmsvga3dMipmapLevel(pDstSurface, iDstFace, iDstMipmap, &pDstMipLevel);
     5151    ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
     5152
     5153    SVGA3dCopyBox clipBox = *pBox;
     5154    vmsvgaR3ClipCopyBox(&pSrcMipLevel->mipmapSize, &pDstMipLevel->mipmapSize, &clipBox);
     5155
    45415156    UINT DstSubresource = dstSubResource;
    4542     UINT DstX = pBox->x;
    4543     UINT DstY = pBox->y;
    4544     UINT DstZ = pBox->z;
     5157    UINT DstX = clipBox.x;
     5158    UINT DstY = clipBox.y;
     5159    UINT DstZ = clipBox.z;
    45455160
    45465161    UINT SrcSubresource = srcSubResource;
    45475162    D3D11_BOX SrcBox;
    4548     SrcBox.left   = pBox->x;
    4549     SrcBox.top    = pBox->y;
    4550     SrcBox.front  = pBox->z;
    4551     SrcBox.right  = pBox->x + pBox->w;
    4552     SrcBox.bottom = pBox->y + pBox->h;
    4553     SrcBox.back   = pBox->z + pBox->d;
     5163    SrcBox.left   = clipBox.srcx;
     5164    SrcBox.top    = clipBox.srcy;
     5165    SrcBox.front  = clipBox.srcz;
     5166    SrcBox.right  = clipBox.srcx + clipBox.w;
     5167    SrcBox.bottom = clipBox.srcy + clipBox.h;
     5168    SrcBox.back   = clipBox.srcz + clipBox.d;
    45545169
    45555170    ID3D11Resource *pDstResource;
    45565171    ID3D11Resource *pSrcResource;
    45575172
    4558     if (pDstSurface->idAssociatedContext == pSrcSurface->idAssociatedContext)
    4559     {
    4560         /* Use the context which created the surfaces. */
    4561         if (pSrcSurface->idAssociatedContext != SVGA_ID_INVALID)
    4562         {
    4563             /* One of DXContexts has created the resources. */
    4564             if (pDXContext->cid != pSrcSurface->idAssociatedContext)
    4565             {
    4566                 /** @todo test */
    4567                 /* Get the context which has created the surfaces. */
    4568                 rc = vmsvga3dDXContextFromCid(pThisCC->svga.p3dState, pSrcSurface->idAssociatedContext, &pDXContext);
    4569                 AssertRCReturn(rc, rc);
    4570 
    4571                 pDevice = &pDXContext->pBackendDXContext->device;
    4572                 AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    4573             }
    4574 
    4575             pDstResource = dxResource(pDstSurface, pDXContext);
    4576             pSrcResource = dxResource(pSrcSurface, pDXContext);
    4577             pDevice->pImmediateContext->CopySubresourceRegion(pDstResource, DstSubresource, DstX, DstY, DstZ,
    4578                                                               pSrcResource, SrcSubresource, &SrcBox);
    4579         }
    4580         else
    4581         {
    4582             /* Backend context has created the resources. */
    4583             pDstResource = pDstSurface->pBackendSurface->u.Resource.pResource;
    4584             pSrcResource = pSrcSurface->pBackendSurface->u.Resource.pResource;
    4585             pBackend->device.pImmediateContext->CopySubresourceRegion(pDstResource, DstSubresource, DstX, DstY, DstZ,
    4586                                                                       pSrcResource, SrcSubresource, &SrcBox);
    4587         }
    4588     }
    4589     else
    4590     {
    4591         /* Different contexts. */
    4592 
    4593         /* One of the surfaces must be shared. */
    4594         AssertReturn(   pSrcSurface->idAssociatedContext == SVGA_ID_INVALID
    4595                      || pDstSurface->idAssociatedContext == SVGA_ID_INVALID, VERR_NOT_SUPPORTED);
    4596 
    4597         /* Use a context which created the not shared resource to do the copy. */
    4598         if (pSrcSurface->idAssociatedContext == SVGA_ID_INVALID)
    4599         {
    4600             if (pDXContext->cid != pDstSurface->idAssociatedContext)
    4601             {
    4602                 /* Get the context which has created the destination resource. */
    4603                 rc = vmsvga3dDXContextFromCid(pThisCC->svga.p3dState, pDstSurface->idAssociatedContext, &pDXContext);
    4604                 AssertRCReturn(rc, rc);
    4605 
    4606                 pDevice = &pDXContext->pBackendDXContext->device;
    4607                 AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    4608             }
    4609 
    4610             pDstResource = dxResource(pDstSurface, pDXContext);
    4611             pSrcResource = dxResource(pSrcSurface, pDXContext);
    4612             pDevice->pImmediateContext->CopySubresourceRegion(pDstResource, DstSubresource, DstX, DstY, DstZ,
    4613                                                               pSrcResource, SrcSubresource, &SrcBox);
    4614         }
    4615         else if (pDstSurface->idAssociatedContext == SVGA_ID_INVALID)
    4616         {
    4617             if (pDXContext->cid != pSrcSurface->idAssociatedContext)
    4618             {
    4619                 /* Get the context which has created the source surface. */
    4620                 rc = vmsvga3dDXContextFromCid(pThisCC->svga.p3dState, pSrcSurface->idAssociatedContext, &pDXContext);
    4621                 AssertRCReturn(rc, rc);
    4622 
    4623                 pDevice = &pDXContext->pBackendDXContext->device;
    4624                 AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    4625             }
    4626 
    4627             pDstResource = dxResource(pDstSurface, pDXContext);
    4628             pSrcResource = dxResource(pSrcSurface, pDXContext);
    4629             pDevice->pImmediateContext->CopySubresourceRegion(pDstResource, DstSubresource, DstX, DstY, DstZ,
    4630                                                               pSrcResource, SrcSubresource, &SrcBox);
    4631         }
    4632         else
    4633         {
    4634             AssertFailedReturn(VERR_NOT_SUPPORTED);
    4635         }
    4636     }
     5173    pDstResource = dxResource(pThisCC->svga.p3dState, pDstSurface, pDXContext);
     5174    pSrcResource = dxResource(pThisCC->svga.p3dState, pSrcSurface, pDXContext);
     5175
     5176    pDevice->pImmediateContext->CopySubresourceRegion(pDstResource, DstSubresource, DstX, DstY, DstZ,
     5177                                                      pSrcResource, SrcSubresource, &SrcBox);
     5178
     5179    pDstSurface->pBackendSurface->cidDrawing = pDXContext->cid;
    46375180    return VINF_SUCCESS;
    46385181}
     
    46745217
    46755218
    4676 static DECLCALLBACK(int) vmsvga3dBackDXUpdateSubResource(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    4677 {
    4678     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4679 
    4680     RT_NOREF(pBackend, pDXContext);
    4681     AssertFailed(); /** @todo Implement */
    4682     return VERR_NOT_IMPLEMENTED;
    4683 }
    4684 
    4685 
    4686 static DECLCALLBACK(int) vmsvga3dBackDXReadbackSubResource(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    4687 {
    4688     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4689 
    4690     RT_NOREF(pBackend, pDXContext);
    4691     AssertFailed(); /** @todo Implement */
    4692     return VERR_NOT_IMPLEMENTED;
    4693 }
    4694 
    4695 
    4696 static DECLCALLBACK(int) vmsvga3dBackDXInvalidateSubResource(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    4697 {
    4698     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4699 
    4700     RT_NOREF(pBackend, pDXContext);
    4701     AssertFailed(); /** @todo Implement */
    4702     return VERR_NOT_IMPLEMENTED;
    4703 }
    4704 
    4705 
    4706 static DECLCALLBACK(int) vmsvga3dBackDXDefineShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId, SVGACOTableDXSRViewEntry const *pEntry)
    4707 {
    4708     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4709     RT_NOREF(pBackend);
    4710 
    4711     DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    4712     AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    4713 
     5219static int dxDefineShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId, SVGACOTableDXSRViewEntry const *pEntry)
     5220{
    47145221    /* Get corresponding resource for pEntry->sid. Create the surface if does not yet exist. */
    47155222    PVMSVGA3DSURFACE pSurface;
     
    47245231    }
    47255232
    4726     HRESULT hr = dxShaderResourceViewCreate(pDXContext, pEntry, pSurface, &pDXContext->pBackendDXContext->papShaderResourceView[shaderResourceViewId]);
     5233    HRESULT hr = dxShaderResourceViewCreate(pThisCC, pDXContext, pEntry, pSurface, &pDXContext->pBackendDXContext->papShaderResourceView[shaderResourceViewId]);
    47275234    if (SUCCEEDED(hr))
    47285235        return VINF_SUCCESS;
     
    47315238
    47325239
     5240static DECLCALLBACK(int) vmsvga3dBackDXDefineShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId, SVGACOTableDXSRViewEntry const *pEntry)
     5241{
     5242    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5243    RT_NOREF(pBackend);
     5244
     5245    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
     5246    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
     5247
     5248    return dxDefineShaderResourceView(pThisCC, pDXContext, shaderResourceViewId, pEntry);
     5249}
     5250
     5251
    47335252static DECLCALLBACK(int) vmsvga3dBackDXDestroyShaderResourceView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId)
    47345253{
     
    47415260
    47425261
    4743 static DECLCALLBACK(int) vmsvga3dBackDXDefineRenderTargetView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dRenderTargetViewId renderTargetViewId, SVGACOTableDXRTViewEntry const *pEntry)
    4744 {
    4745     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4746     RT_NOREF(pBackend);
    4747 
    4748     DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    4749     AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    4750 
     5262static int dxDefineRenderTargetView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dRenderTargetViewId renderTargetViewId, SVGACOTableDXRTViewEntry const *pEntry)
     5263{
    47515264    /* Get corresponding resource for pEntry->sid. Create the surface if does not yet exist. */
    47525265    PVMSVGA3DSURFACE pSurface;
     
    47615274    }
    47625275
    4763     HRESULT hr = dxRenderTargetViewCreate(pDXContext, pEntry, pSurface, &pDXContext->pBackendDXContext->papRenderTargetView[renderTargetViewId]);
     5276    HRESULT hr = dxRenderTargetViewCreate(pThisCC, pDXContext, pEntry, pSurface, &pDXContext->pBackendDXContext->papRenderTargetView[renderTargetViewId]);
    47645277    if (SUCCEEDED(hr))
    47655278        return VINF_SUCCESS;
     5279
    47665280    return VERR_INVALID_STATE;
     5281}
     5282
     5283
     5284static DECLCALLBACK(int) vmsvga3dBackDXDefineRenderTargetView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dRenderTargetViewId renderTargetViewId, SVGACOTableDXRTViewEntry const *pEntry)
     5285{
     5286    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5287    RT_NOREF(pBackend);
     5288
     5289    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
     5290    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
     5291
     5292    return dxDefineRenderTargetView(pThisCC, pDXContext, renderTargetViewId, pEntry);
    47675293}
    47685294
     
    47785304
    47795305
    4780 static DECLCALLBACK(int) vmsvga3dBackDXDefineDepthStencilView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dDepthStencilViewId depthStencilViewId, SVGACOTableDXDSViewEntry const *pEntry)
    4781 {
    4782     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4783     RT_NOREF(pBackend);
    4784 
    4785     DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    4786     AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    4787 
     5306static int dxDefineDepthStencilView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dDepthStencilViewId depthStencilViewId, SVGACOTableDXDSViewEntry const *pEntry)
     5307{
    47885308    /* Get corresponding resource for pEntry->sid. Create the surface if does not yet exist. */
    47895309    PVMSVGA3DSURFACE pSurface;
     
    47915311    AssertRCReturn(rc, rc);
    47925312
     5313    if (   pSurface->pBackendSurface != NULL
     5314        && pDXContext->cid != pSurface->idAssociatedContext)
     5315    {
     5316        /* Supposed to be per context. */
     5317//AssertFailed(); // test
     5318        vmsvga3dBackSurfaceDestroy(pThisCC, pSurface);
     5319    }
     5320
    47935321    if (pSurface->pBackendSurface == NULL)
    47945322    {
     
    47985326    }
    47995327
    4800     HRESULT hr = dxDepthStencilViewCreate(pDevice, pEntry, pSurface->pBackendSurface, &pDXContext->pBackendDXContext->papDepthStencilView[depthStencilViewId]);
     5328    HRESULT hr = dxDepthStencilViewCreate(pThisCC, pDXContext, pEntry, pSurface, &pDXContext->pBackendDXContext->papDepthStencilView[depthStencilViewId]);
    48015329    if (SUCCEEDED(hr))
    48025330        return VINF_SUCCESS;
     
    48045332}
    48055333
     5334static DECLCALLBACK(int) vmsvga3dBackDXDefineDepthStencilView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dDepthStencilViewId depthStencilViewId, SVGACOTableDXDSViewEntry const *pEntry)
     5335{
     5336    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5337    RT_NOREF(pBackend);
     5338
     5339    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
     5340    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
     5341
     5342    return dxDefineDepthStencilView(pThisCC, pDXContext, depthStencilViewId, pEntry);
     5343}
     5344
    48065345
    48075346static DECLCALLBACK(int) vmsvga3dBackDXDestroyDepthStencilView(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dDepthStencilViewId depthStencilViewId)
     
    48155354
    48165355
    4817 static DECLCALLBACK(int) vmsvga3dBackDXDefineElementLayout(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dElementLayoutId elementLayoutId, SVGACOTableDXElementLayoutEntry const *pEntry)
    4818 {
    4819     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4820     DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    4821     AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    4822 
    4823     RT_NOREF(pBackend, elementLayoutId, pEntry);
    4824 
    4825     /* Not much can be done here because ID3D11Device::CreateInputLayout requires
    4826      * a pShaderBytecodeWithInputSignature which is not known at this moment.
    4827      * InputLayout object will be created in SVGA_3D_CMD_DX_SET_INPUT_LAYOUT.
    4828      */
    4829 
    4830     DXELEMENTLAYOUT *pDXElementLayout = &pDXContext->pBackendDXContext->paElementLayout[pEntry->elid];
     5356static int dxDefineElementLayout(PVMSVGA3DDXCONTEXT pDXContext, SVGA3dElementLayoutId elementLayoutId, SVGACOTableDXElementLayoutEntry const *pEntry)
     5357{
     5358    DXELEMENTLAYOUT *pDXElementLayout = &pDXContext->pBackendDXContext->paElementLayout[elementLayoutId];
    48315359    D3D_RELEASE(pDXElementLayout->pElementLayout);
    48325360
     
    48605388
    48615389
    4862 static DECLCALLBACK(int) vmsvga3dBackDXDestroyElementLayout(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    4863 {
    4864     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4865 
    4866     RT_NOREF(pBackend, pDXContext);
    4867     AssertFailed(); /** @todo Implement */
    4868     return VERR_NOT_IMPLEMENTED;
    4869 }
    4870 
    4871 
    4872 static DECLCALLBACK(int) vmsvga3dBackDXDefineBlendState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext,
    4873                                                         SVGA3dBlendStateId blendId, SVGACOTableDXBlendStateEntry const *pEntry)
     5390static DECLCALLBACK(int) vmsvga3dBackDXDefineElementLayout(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dElementLayoutId elementLayoutId, SVGACOTableDXElementLayoutEntry const *pEntry)
    48745391{
    48755392    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     
    48785395
    48795396    RT_NOREF(pBackend);
     5397
     5398    /* Not much can be done here because ID3D11Device::CreateInputLayout requires
     5399     * a pShaderBytecodeWithInputSignature which is not known at this moment.
     5400     * InputLayout object will be created in SVGA_3D_CMD_DX_SET_INPUT_LAYOUT.
     5401     */
     5402
     5403    Assert(elementLayoutId == pEntry->elid);
     5404
     5405    return dxDefineElementLayout(pDXContext, elementLayoutId, pEntry);
     5406}
     5407
     5408
     5409static DECLCALLBACK(int) vmsvga3dBackDXDestroyElementLayout(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
     5410{
     5411    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5412
     5413    RT_NOREF(pBackend, pDXContext);
     5414    AssertFailed(); /** @todo Implement */
     5415    return VERR_NOT_IMPLEMENTED;
     5416}
     5417
     5418
     5419static int dxDefineBlendState(PVMSVGA3DDXCONTEXT pDXContext,
     5420                              SVGA3dBlendStateId blendId, SVGACOTableDXBlendStateEntry const *pEntry)
     5421{
     5422    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
     5423    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    48805424
    48815425    HRESULT hr = dxBlendStateCreate(pDevice, pEntry, &pDXContext->pBackendDXContext->papBlendState[blendId]);
     
    48855429}
    48865430
     5431static DECLCALLBACK(int) vmsvga3dBackDXDefineBlendState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext,
     5432                                                        SVGA3dBlendStateId blendId, SVGACOTableDXBlendStateEntry const *pEntry)
     5433{
     5434    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5435    RT_NOREF(pBackend);
     5436
     5437    return dxDefineBlendState(pDXContext, blendId, pEntry);
     5438}
     5439
    48875440
    48885441static DECLCALLBACK(int) vmsvga3dBackDXDestroyBlendState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
     
    48965449
    48975450
    4898 static DECLCALLBACK(int) vmsvga3dBackDXDefineDepthStencilState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dDepthStencilStateId depthStencilId, SVGACOTableDXDepthStencilEntry *pEntry)
    4899 {
    4900     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5451static int dxDefineDepthStencilState(PVMSVGA3DDXCONTEXT pDXContext, SVGA3dDepthStencilStateId depthStencilId, SVGACOTableDXDepthStencilEntry const *pEntry)
     5452{
    49015453    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    49025454    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    4903 
    4904     RT_NOREF(pBackend);
    49055455
    49065456    HRESULT hr = dxDepthStencilStateCreate(pDevice, pEntry, &pDXContext->pBackendDXContext->papDepthStencilState[depthStencilId]);
     
    49115461
    49125462
     5463static DECLCALLBACK(int) vmsvga3dBackDXDefineDepthStencilState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dDepthStencilStateId depthStencilId, SVGACOTableDXDepthStencilEntry const *pEntry)
     5464{
     5465    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5466    RT_NOREF(pBackend);
     5467
     5468    return dxDefineDepthStencilState(pDXContext, depthStencilId, pEntry);
     5469}
     5470
     5471
    49135472static DECLCALLBACK(int) vmsvga3dBackDXDestroyDepthStencilState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    49145473{
     
    49215480
    49225481
    4923 static DECLCALLBACK(int) vmsvga3dBackDXDefineRasterizerState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dRasterizerStateId rasterizerId, SVGACOTableDXRasterizerStateEntry const *pEntry)
    4924 {
    4925     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5482static int dxDefineRasterizerState(PVMSVGA3DDXCONTEXT pDXContext, SVGA3dRasterizerStateId rasterizerId, SVGACOTableDXRasterizerStateEntry const *pEntry)
     5483{
    49265484    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    49275485    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    4928 
    4929     RT_NOREF(pBackend);
    49305486
    49315487    HRESULT hr = dxRasterizerStateCreate(pDevice, pEntry, &pDXContext->pBackendDXContext->papRasterizerState[rasterizerId]);
     
    49365492
    49375493
     5494static DECLCALLBACK(int) vmsvga3dBackDXDefineRasterizerState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dRasterizerStateId rasterizerId, SVGACOTableDXRasterizerStateEntry const *pEntry)
     5495{
     5496    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5497    RT_NOREF(pBackend);
     5498
     5499    return dxDefineRasterizerState(pDXContext, rasterizerId, pEntry);
     5500}
     5501
     5502
    49385503static DECLCALLBACK(int) vmsvga3dBackDXDestroyRasterizerState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    49395504{
     
    49465511
    49475512
    4948 static DECLCALLBACK(int) vmsvga3dBackDXDefineSamplerState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dSamplerId samplerId, SVGACOTableDXSamplerEntry const *pEntry)
    4949 {
    4950     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5513static int dxDefineSamplerState(PVMSVGA3DDXCONTEXT pDXContext, SVGA3dSamplerId samplerId, SVGACOTableDXSamplerEntry const *pEntry)
     5514{
    49515515    DXDEVICE *pDevice = &pDXContext->pBackendDXContext->device;
    49525516    AssertReturn(pDevice->pDevice, VERR_INVALID_STATE);
    4953 
    4954     RT_NOREF(pBackend);
    49555517
    49565518    HRESULT hr = dxSamplerStateCreate(pDevice, pEntry, &pDXContext->pBackendDXContext->papSamplerState[samplerId]);
     
    49615523
    49625524
     5525static DECLCALLBACK(int) vmsvga3dBackDXDefineSamplerState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dSamplerId samplerId, SVGACOTableDXSamplerEntry const *pEntry)
     5526{
     5527    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     5528    RT_NOREF(pBackend);
     5529
     5530    return dxDefineSamplerState(pDXContext, samplerId, pEntry);
     5531}
     5532
     5533
    49635534static DECLCALLBACK(int) vmsvga3dBackDXDestroySamplerState(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    49645535{
     
    49715542
    49725543
    4973 static DECLCALLBACK(int) vmsvga3dBackDXDefineShader(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, PVMSVGA3DSHADER pShader)
    4974 {
    4975     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    4976 
     5544
     5545static int dxDefineShader(PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderId shaderId, SVGACOTableDXShaderEntry const *pEntry)
     5546{
     5547    DXSHADER *pDXShader = &pDXContext->pBackendDXContext->paShader[shaderId];
     5548    pDXShader->enmShaderType = pEntry->type;
     5549
     5550    PVMSVGA3DSHADER pShader = &pDXContext->paShader[shaderId];
     5551    pShader->u.pvBackendShader = pDXShader;
     5552
     5553    return VINF_SUCCESS;
     5554}
     5555
     5556
     5557static DECLCALLBACK(int) vmsvga3dBackDXDefineShader(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderId shaderId, SVGACOTableDXShaderEntry const *pEntry)
     5558{
     5559    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    49775560    RT_NOREF(pBackend);
    49785561
    4979     DXSHADER *pDXShader = &pDXContext->pBackendDXContext->paShader[pShader->id];
     5562    DXSHADER *pDXShader = &pDXContext->pBackendDXContext->paShader[shaderId];
    49805563    D3D_RELEASE(pDXShader->pShader);
    4981     pDXShader->enmShaderType = pShader->type;
    4982     pShader->u.pvBackendShader = pDXShader;
    4983     return VINF_SUCCESS;
     5564
     5565    return dxDefineShader(pDXContext, shaderId, pEntry);
    49845566}
    49855567
     
    50065588
    50075589    DXSHADER *pDXShader = &pDXContext->pBackendDXContext->paShader[pShader->id];
     5590    Assert(pDXShader->enmShaderType == pShader->type);
     5591
    50085592    if (pDXShader->pvDXBC)
    50095593    {
     
    50555639
    50565640            HRESULT hr = dxShaderCreate(pDevice, pShader, pDXShader);
    5057             if (FAILED(hr))
     5641            if (SUCCEEDED(hr))
     5642            {
     5643            }
     5644            else
    50585645                rc = VERR_INVALID_STATE;
    50595646        }
     
    50965683
    50975684
    5098 static int dxCOTableRealloc(void **ppvCOTable, uint32_t *pcCOTable, uint32_t cbEntry, uint32_t cEntries, uint32_t cValidEntries)
     5685static int dxCOTableRealloc(void **ppvCOTable, uint32_t *pcCOTable, uint32_t cbEntry, uint32_t cEntries)
    50995686{
    51005687    if (*pcCOTable != cEntries)
     
    51055692            void *pvNew = RTMemRealloc(*ppvCOTable, cEntries * cbEntry);
    51065693            AssertReturn(pvNew, VERR_NO_MEMORY);
    5107             memset((uint8_t *)pvNew + cValidEntries * cbEntry, 0, (cEntries - cValidEntries) * cbEntry);
    51085694            *ppvCOTable = pvNew;
    51095695        }
     
    51175703    }
    51185704
     5705    if (*ppvCOTable)
     5706        memset(*ppvCOTable, 0, cEntries * cbEntry);
     5707
    51195708    return VINF_SUCCESS;
    51205709}
    51215710
    5122 static DECLCALLBACK(int) vmsvga3dBackDXSetCOTable(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGACOTableType type, uint32_t cEntries, uint32_t cValidEntries)
     5711static DECLCALLBACK(int) vmsvga3dBackDXSetCOTable(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGACOTableType type, uint32_t cValidEntries)
    51235712{
    51245713    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     
    51295718    int rc = VINF_SUCCESS;
    51305719
    5131     /* Allocate paBlendState for example. */
    5132     /* Keep first cValidEntries. */
     5720    /*
     5721     * 1) Release current backend table, if exists;
     5722     * 2) Reallocate memory for the new backend table;
     5723     * 3) If cValidEntries is not zero, then re-define corresponding backend table elements.
     5724     */
    51335725    switch (type)
    51345726    {
    51355727        case SVGA_COTABLE_RTVIEW:
     5728            /* Clear current entries. */
    51365729            if (pBackendDXContext->papRenderTargetView)
     5730                DX_RELEASE_ARRAY(pBackendDXContext->cRenderTargetView, pBackendDXContext->papRenderTargetView);
     5731
     5732            rc = dxCOTableRealloc((void **)&pBackendDXContext->papRenderTargetView, &pBackendDXContext->cRenderTargetView,
     5733                                  sizeof(pBackendDXContext->papRenderTargetView[0]), pDXContext->cot.cRTView);
     5734            AssertRCBreak(rc);
     5735
     5736            for (uint32_t i = 0; i < cValidEntries; ++i)
    51375737            {
    5138                 /* Clear obsolete entries. */
    5139                 DX_RELEASE_ARRAY(pBackendDXContext->cRenderTargetView - cValidEntries, &pBackendDXContext->papRenderTargetView[cValidEntries]);
     5738                SVGACOTableDXRTViewEntry const *pEntry = &pDXContext->cot.paRTView[i];
     5739                if (ASMMemFirstNonZero(pEntry, sizeof(*pEntry)) == NULL)
     5740                    continue; /* Skip uninitialized entry. */
     5741
     5742                dxDefineRenderTargetView(pThisCC, pDXContext, i, pEntry);
    51405743            }
    5141 
    5142             rc = dxCOTableRealloc((void **)&pBackendDXContext->papRenderTargetView, &pBackendDXContext->cRenderTargetView,
    5143                                   sizeof(pBackendDXContext->papRenderTargetView[0]), cEntries, cValidEntries);
    51445744            break;
    51455745        case SVGA_COTABLE_DSVIEW:
    51465746            if (pBackendDXContext->papDepthStencilView)
     5747                DX_RELEASE_ARRAY(pBackendDXContext->cDepthStencilView, pBackendDXContext->papDepthStencilView);
     5748
     5749            rc = dxCOTableRealloc((void **)&pBackendDXContext->papDepthStencilView, &pBackendDXContext->cDepthStencilView,
     5750                                  sizeof(pBackendDXContext->papDepthStencilView[0]), pDXContext->cot.cDSView);
     5751            AssertRCBreak(rc);
     5752
     5753            for (uint32_t i = 0; i < cValidEntries; ++i)
    51475754            {
    5148                 /* Clear obsolete entries. */
    5149                 DX_RELEASE_ARRAY(pBackendDXContext->cDepthStencilView - cValidEntries, &pBackendDXContext->papDepthStencilView[cValidEntries]);
     5755                SVGACOTableDXDSViewEntry const *pEntry = &pDXContext->cot.paDSView[i];
     5756                if (ASMMemFirstNonZero(pEntry, sizeof(*pEntry)) == NULL)
     5757                    continue; /* Skip uninitialized entry. */
     5758
     5759                dxDefineDepthStencilView(pThisCC, pDXContext, i, pEntry);
    51505760            }
    5151 
    5152             rc = dxCOTableRealloc((void **)&pBackendDXContext->papDepthStencilView, &pBackendDXContext->cDepthStencilView,
    5153                                   sizeof(pBackendDXContext->papDepthStencilView[0]), cEntries, cValidEntries);
    51545761            break;
    51555762        case SVGA_COTABLE_SRVIEW:
    51565763            if (pBackendDXContext->papShaderResourceView)
     5764                DX_RELEASE_ARRAY(pBackendDXContext->cShaderResourceView, pBackendDXContext->papShaderResourceView);
     5765
     5766            rc = dxCOTableRealloc((void **)&pBackendDXContext->papShaderResourceView, &pBackendDXContext->cShaderResourceView,
     5767                                  sizeof(pBackendDXContext->papShaderResourceView[0]), pDXContext->cot.cSRView);
     5768            AssertRCBreak(rc);
     5769
     5770            for (uint32_t i = 0; i < cValidEntries; ++i)
    51575771            {
    5158                 /* Clear obsolete entries. */
    5159                 DX_RELEASE_ARRAY(pBackendDXContext->cShaderResourceView - cValidEntries, &pBackendDXContext->papShaderResourceView[cValidEntries]);
     5772                SVGACOTableDXSRViewEntry const *pEntry = &pDXContext->cot.paSRView[i];
     5773                if (ASMMemFirstNonZero(pEntry, sizeof(*pEntry)) == NULL)
     5774                    continue; /* Skip uninitialized entry. */
     5775
     5776                dxDefineShaderResourceView(pThisCC, pDXContext, i, pEntry);
    51605777            }
    5161 
    5162             rc = dxCOTableRealloc((void **)&pBackendDXContext->papShaderResourceView, &pBackendDXContext->cShaderResourceView,
    5163                                   sizeof(pBackendDXContext->papShaderResourceView[0]), cEntries, cValidEntries);
    51645778            break;
    51655779        case SVGA_COTABLE_ELEMENTLAYOUT:
    51665780            if (pBackendDXContext->paElementLayout)
    51675781            {
    5168                 /* Clear obsolete entries. */
    5169                 for (uint32_t i = cValidEntries; i < pBackendDXContext->cElementLayout; ++i)
     5782                for (uint32_t i = 0; i < pBackendDXContext->cElementLayout; ++i)
    51705783                    D3D_RELEASE(pBackendDXContext->paElementLayout[i].pElementLayout);
    51715784            }
    51725785
    51735786            rc = dxCOTableRealloc((void **)&pBackendDXContext->paElementLayout, &pBackendDXContext->cElementLayout,
    5174                                   sizeof(pBackendDXContext->paElementLayout[0]), cEntries, cValidEntries);
     5787                                  sizeof(pBackendDXContext->paElementLayout[0]), pDXContext->cot.cElementLayout);
     5788            AssertRCBreak(rc);
     5789
     5790            for (uint32_t i = 0; i < cValidEntries; ++i)
     5791            {
     5792                SVGACOTableDXElementLayoutEntry const *pEntry = &pDXContext->cot.paElementLayout[i];
     5793                if (ASMMemFirstNonZero(pEntry, sizeof(*pEntry)) == NULL)
     5794                    continue; /* Skip uninitialized entry. */
     5795
     5796                dxDefineElementLayout(pDXContext, i, pEntry);
     5797            }
    51755798            break;
    51765799        case SVGA_COTABLE_BLENDSTATE:
    51775800            if (pBackendDXContext->papBlendState)
     5801                DX_RELEASE_ARRAY(pBackendDXContext->cBlendState, pBackendDXContext->papBlendState);
     5802
     5803            rc = dxCOTableRealloc((void **)&pBackendDXContext->papBlendState, &pBackendDXContext->cBlendState,
     5804                                  sizeof(pBackendDXContext->papBlendState[0]), pDXContext->cot.cBlendState);
     5805            AssertRCBreak(rc);
     5806
     5807            for (uint32_t i = 0; i < cValidEntries; ++i)
    51785808            {
    5179                 /* Clear obsolete entries. */
    5180                 DX_RELEASE_ARRAY(pBackendDXContext->cBlendState - cValidEntries, &pBackendDXContext->papBlendState[cValidEntries]);
     5809                SVGACOTableDXBlendStateEntry const *pEntry = &pDXContext->cot.paBlendState[i];
     5810                if (ASMMemFirstNonZero(pEntry, sizeof(*pEntry)) == NULL)
     5811                    continue; /* Skip uninitialized entry. */
     5812
     5813                dxDefineBlendState(pDXContext, i, pEntry);
    51815814            }
    5182 
    5183             rc = dxCOTableRealloc((void **)&pBackendDXContext->papBlendState, &pBackendDXContext->cBlendState,
    5184                                   sizeof(pBackendDXContext->papBlendState[0]), cEntries, cValidEntries);
    51855815            break;
    51865816        case SVGA_COTABLE_DEPTHSTENCIL:
    51875817            if (pBackendDXContext->papDepthStencilState)
     5818                DX_RELEASE_ARRAY(pBackendDXContext->cDepthStencilState, pBackendDXContext->papDepthStencilState);
     5819
     5820            rc = dxCOTableRealloc((void **)&pBackendDXContext->papDepthStencilState, &pBackendDXContext->cDepthStencilState,
     5821                                  sizeof(pBackendDXContext->papDepthStencilState[0]), pDXContext->cot.cDepthStencil);
     5822            AssertRCBreak(rc);
     5823
     5824            for (uint32_t i = 0; i < cValidEntries; ++i)
    51885825            {
    5189                 /* Clear obsolete entries. */
    5190                 DX_RELEASE_ARRAY(pBackendDXContext->cDepthStencilState - cValidEntries, &pBackendDXContext->papDepthStencilState[cValidEntries]);
     5826                SVGACOTableDXDepthStencilEntry const *pEntry = &pDXContext->cot.paDepthStencil[i];
     5827                if (ASMMemFirstNonZero(pEntry, sizeof(*pEntry)) == NULL)
     5828                    continue; /* Skip uninitialized entry. */
     5829
     5830                dxDefineDepthStencilState(pDXContext, i, pEntry);
    51915831            }
    5192 
    5193             rc = dxCOTableRealloc((void **)&pBackendDXContext->papDepthStencilState, &pBackendDXContext->cDepthStencilState,
    5194                                   sizeof(pBackendDXContext->papDepthStencilState[0]), cEntries, cValidEntries);
    51955832            break;
    51965833        case SVGA_COTABLE_RASTERIZERSTATE:
    51975834            if (pBackendDXContext->papRasterizerState)
     5835                DX_RELEASE_ARRAY(pBackendDXContext->cRasterizerState, pBackendDXContext->papRasterizerState);
     5836
     5837            rc = dxCOTableRealloc((void **)&pBackendDXContext->papRasterizerState, &pBackendDXContext->cRasterizerState,
     5838                                  sizeof(pBackendDXContext->papRasterizerState[0]), pDXContext->cot.cRasterizerState);
     5839            AssertRCBreak(rc);
     5840
     5841            for (uint32_t i = 0; i < cValidEntries; ++i)
    51985842            {
    5199                 /* Clear obsolete entries. */
    5200                 DX_RELEASE_ARRAY(pBackendDXContext->cRasterizerState - cValidEntries, &pBackendDXContext->papRasterizerState[cValidEntries]);
     5843                SVGACOTableDXRasterizerStateEntry const *pEntry = &pDXContext->cot.paRasterizerState[i];
     5844                if (ASMMemFirstNonZero(pEntry, sizeof(*pEntry)) == NULL)
     5845                    continue; /* Skip uninitialized entry. */
     5846
     5847                dxDefineRasterizerState(pDXContext, i, pEntry);
    52015848            }
    5202 
    5203             rc = dxCOTableRealloc((void **)&pBackendDXContext->papRasterizerState, &pBackendDXContext->cRasterizerState,
    5204                                   sizeof(pBackendDXContext->papRasterizerState[0]), cEntries, cValidEntries);
    52055849            break;
    52065850        case SVGA_COTABLE_SAMPLER:
    52075851            if (pBackendDXContext->papSamplerState)
     5852                DX_RELEASE_ARRAY(pBackendDXContext->cSamplerState, pBackendDXContext->papSamplerState);
     5853
     5854            rc = dxCOTableRealloc((void **)&pBackendDXContext->papSamplerState, &pBackendDXContext->cSamplerState,
     5855                                  sizeof(pBackendDXContext->papSamplerState[0]), pDXContext->cot.cSampler);
     5856            AssertRCBreak(rc);
     5857
     5858            for (uint32_t i = 0; i < cValidEntries; ++i)
    52085859            {
    5209                 /* Clear obsolete entries. */
    5210                 DX_RELEASE_ARRAY(pBackendDXContext->cSamplerState - cValidEntries, &pBackendDXContext->papSamplerState[cValidEntries]);
     5860                SVGACOTableDXSamplerEntry const *pEntry = &pDXContext->cot.paSampler[i];
     5861                if (ASMMemFirstNonZero(pEntry, sizeof(*pEntry)) == NULL)
     5862                    continue; /* Skip uninitialized entry. */
     5863
     5864                dxDefineSamplerState(pDXContext, i, pEntry);
    52115865            }
    5212 
    5213             rc = dxCOTableRealloc((void **)&pBackendDXContext->papSamplerState, &pBackendDXContext->cSamplerState,
    5214                                   sizeof(pBackendDXContext->papSamplerState[0]), cEntries, cValidEntries);
    52155866            break;
    52165867        case SVGA_COTABLE_STREAMOUTPUT:
     
    52195870        case SVGA_COTABLE_DXQUERY:
    52205871            if (pBackendDXContext->papQuery)
     5872                DX_RELEASE_ARRAY(pBackendDXContext->cQuery, pBackendDXContext->papQuery);
     5873
     5874            rc = dxCOTableRealloc((void **)&pBackendDXContext->papQuery, &pBackendDXContext->cQuery,
     5875                                  sizeof(pBackendDXContext->papQuery[0]), pDXContext->cot.cQuery);
     5876            AssertRCBreak(rc);
     5877
     5878            for (uint32_t i = 0; i < cValidEntries; ++i)
    52215879            {
    5222                 /* Clear obsolete entries. */
    5223                 DX_RELEASE_ARRAY(pBackendDXContext->cQuery - cValidEntries, &pBackendDXContext->papQuery[cValidEntries]);
     5880                SVGACOTableDXQueryEntry const *pEntry = &pDXContext->cot.paQuery[i];
     5881                if (ASMMemFirstNonZero(pEntry, sizeof(*pEntry)) == NULL)
     5882                    continue; /* Skip uninitialized entry. */
     5883
     5884                AssertFailed(); /** @todo implement */
    52245885            }
    5225 
    5226             rc = dxCOTableRealloc((void **)&pBackendDXContext->papQuery, &pBackendDXContext->cQuery,
    5227                                   sizeof(pBackendDXContext->papQuery[0]), cEntries, cValidEntries);
    52285886            break;
    52295887        case SVGA_COTABLE_DXSHADER:
    52305888            if (pBackendDXContext->paShader)
    52315889            {
    5232                 /* Clear obsolete entries. */
    5233                 for (uint32_t i = cValidEntries; i < pBackendDXContext->cShader; ++i)
     5890                for (uint32_t i = 0; i < pBackendDXContext->cShader; ++i)
    52345891                    D3D_RELEASE(pBackendDXContext->paShader[i].pShader);
    52355892            }
    52365893
    52375894            rc = dxCOTableRealloc((void **)&pBackendDXContext->paShader, &pBackendDXContext->cShader,
    5238                                   sizeof(pBackendDXContext->paShader[0]), cEntries, cValidEntries);
     5895                                  sizeof(pBackendDXContext->paShader[0]), pDXContext->cot.cShader);
     5896            AssertRCBreak(rc);
     5897
     5898            for (uint32_t i = 0; i < cValidEntries; ++i)
     5899            {
     5900                SVGACOTableDXShaderEntry const *pEntry = &pDXContext->cot.paShader[i];
     5901                /** @todo The caller must verify the COTable content using same rules as when a new entry is defined. */
     5902                if (ASMMemFirstNonZero(pEntry, sizeof(*pEntry)) == NULL)
     5903                    continue; /* Skip uninitialized entry. */
     5904
     5905                /** @todo this should be in the commd DX code (the caller). */
     5906                PVMSVGA3DSHADER pShader = &pDXContext->paShader[i];
     5907                pShader->id                = i;
     5908                pShader->cid               = pDXContext->cid;
     5909                pShader->type              = pEntry->type;
     5910                pShader->cbData            = pEntry->sizeInBytes;
     5911                pShader->pShaderProgram    = NULL;
     5912                pShader->u.pvBackendShader = NULL;
     5913
     5914                dxDefineShader(pDXContext, i, pEntry);
     5915            }
    52395916            break;
    52405917        case SVGA_COTABLE_UAVIEW:
     
    52575934
    52585935
    5259 static DECLCALLBACK(int) vmsvga3dBackDXTransferFromBuffer(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    5260 {
    5261     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    5262 
    5263     RT_NOREF(pBackend, pDXContext);
    5264     AssertFailed(); /** @todo Implement */
    5265     return VERR_NOT_IMPLEMENTED;
    5266 }
    5267 
    5268 
    52695936static DECLCALLBACK(int) vmsvga3dBackDXSurfaceCopyAndReadback(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    52705937{
     
    52985965
    52995966static DECLCALLBACK(int) vmsvga3dBackDXReadbackAllQuery(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    5300 {
    5301     PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
    5302 
    5303     RT_NOREF(pBackend, pDXContext);
    5304     AssertFailed(); /** @todo Implement */
    5305     return VERR_NOT_IMPLEMENTED;
    5306 }
    5307 
    5308 
    5309 static DECLCALLBACK(int) vmsvga3dBackDXPredTransferFromBuffer(PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext)
    53105967{
    53115968    PVMSVGA3DBACKEND pBackend = pThisCC->svga.p3dState->pBackend;
     
    58586515                p->pfnDXPresentBlt                = vmsvga3dBackDXPresentBlt;
    58596516                p->pfnDXGenMips                   = vmsvga3dBackDXGenMips;
    5860                 p->pfnDXUpdateSubResource         = vmsvga3dBackDXUpdateSubResource;
    5861                 p->pfnDXReadbackSubResource       = vmsvga3dBackDXReadbackSubResource;
    5862                 p->pfnDXInvalidateSubResource     = vmsvga3dBackDXInvalidateSubResource;
    58636517                p->pfnDXDefineShaderResourceView  = vmsvga3dBackDXDefineShaderResourceView;
    58646518                p->pfnDXDestroyShaderResourceView = vmsvga3dBackDXDestroyShaderResourceView;
     
    58856539                p->pfnDXSetCOTable                = vmsvga3dBackDXSetCOTable;
    58866540                p->pfnDXBufferCopy                = vmsvga3dBackDXBufferCopy;
    5887                 p->pfnDXTransferFromBuffer        = vmsvga3dBackDXTransferFromBuffer;
    58886541                p->pfnDXSurfaceCopyAndReadback    = vmsvga3dBackDXSurfaceCopyAndReadback;
    58896542                p->pfnDXMoveQuery                 = vmsvga3dBackDXMoveQuery;
    58906543                p->pfnDXBindAllQuery              = vmsvga3dBackDXBindAllQuery;
    58916544                p->pfnDXReadbackAllQuery          = vmsvga3dBackDXReadbackAllQuery;
    5892                 p->pfnDXPredTransferFromBuffer    = vmsvga3dBackDXPredTransferFromBuffer;
    58936545                p->pfnDXMobFence64                = vmsvga3dBackDXMobFence64;
    58946546                p->pfnDXBindAllShader             = vmsvga3dBackDXBindAllShader;
     
    59556607            {
    59566608                VMSVGA3DBACKENDFUNCSMAP *p = (VMSVGA3DBACKENDFUNCSMAP *)pvInterfaceFuncs;
    5957                 p->pfnSurfaceMap   = vmsvga3dSurfaceMap;
    5958                 p->pfnSurfaceUnmap = vmsvga3dSurfaceUnmap;
     6609                p->pfnSurfaceMap   = vmsvga3dBackSurfaceMap;
     6610                p->pfnSurfaceUnmap = vmsvga3dBackSurfaceUnmap;
    59596611            }
    59606612        }
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win.cpp

    r89171 r91361  
    27432743
    27442744                rc = vmsvga3dSurfaceDefine(pThisCC, sid, surfaceFlags, format, multisampleCount, autogenFilter,
    2745                                            cMipLevels, &pMipLevelSize[0]);
     2745                                           cMipLevels, &pMipLevelSize[0], /* fAllocMipLevels = */ true);
    27462746                AssertRC(rc);
    27472747
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d.cpp

    r89163 r91361  
    4141#include "DevVGA-SVGA-internal.h"
    4242
     43
     44static int vmsvga3dSurfaceAllocMipLevels(PVMSVGA3DSURFACE pSurface)
     45{
     46    /* Allocate buffer to hold the surface data until we can move it into a D3D object */
     47    for (uint32_t i = 0; i < pSurface->cLevels * pSurface->cFaces; ++i)
     48    {
     49        PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
     50        AssertReturn(pMipmapLevel->pSurfaceData == NULL, VERR_INVALID_STATE);
     51        pMipmapLevel->pSurfaceData = RTMemAllocZ(pMipmapLevel->cbSurface);
     52        AssertReturn(pMipmapLevel->pSurfaceData, VERR_NO_MEMORY);
     53    }
     54    return VINF_SUCCESS;
     55}
     56
     57
     58static void vmsvga3dSurfaceFreeMipLevels(PVMSVGA3DSURFACE pSurface)
     59{
     60    for (uint32_t i = 0; i < pSurface->cLevels * pSurface->cFaces; ++i)
     61    {
     62        PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
     63        RTMemFreeZ(pMipmapLevel->pSurfaceData, pMipmapLevel->cbSurface);
     64        pMipmapLevel->pSurfaceData = NULL;
     65    }
     66}
    4367
    4468
     
    5983int vmsvga3dSurfaceDefine(PVGASTATECC pThisCC, uint32_t sid, SVGA3dSurface1Flags surfaceFlags, SVGA3dSurfaceFormat format,
    6084                          uint32_t multisampleCount, SVGA3dTextureFilter autogenFilter,
    61                           uint32_t numMipLevels, SVGA3dSize const *pMipLevel0Size)
     85                          uint32_t numMipLevels, SVGA3dSize const *pMipLevel0Size, bool fAllocMipLevels)
    6286{
    6387    PVMSVGA3DSURFACE pSurface;
     
    351375    Assert(!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface));
    352376
    353     /* Allocate buffer to hold the surface data until we can move it into a D3D object */
    354     for (uint32_t i = 0; i < numMipLevels * pSurface->cFaces; ++i)
    355     {
    356         PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
    357         pMipmapLevel->pSurfaceData = RTMemAllocZ(pMipmapLevel->cbSurface);
    358         AssertReturn(pMipmapLevel->pSurfaceData, VERR_NO_MEMORY);
     377    if (fAllocMipLevels)
     378    {
     379        rc = vmsvga3dSurfaceAllocMipLevels(pSurface);
     380        AssertRCReturn(rc, rc);
    359381    }
    360382
     
    403425    if (pSurface->paMipmapLevels)
    404426    {
    405         for (uint32_t i = 0; i < pSurface->cLevels * pSurface->cFaces; ++i)
    406             RTMemFreeZ(pSurface->paMipmapLevels[i].pSurfaceData, pSurface->paMipmapLevels[i].cbSurface);
     427        vmsvga3dSurfaceFreeMipLevels(pSurface);
    407428        RTMemFree(pSurface->paMipmapLevels);
    408429    }
     
    552573         * the copy of the data we've got in VMSVGA3DMIMAPLEVEL::pSurfaceData.
    553574         */
    554         AssertReturn(pMipLevel->pSurfaceData, VERR_INTERNAL_ERROR);
     575        if (!pMipLevel->pSurfaceData)
     576        {
     577            rc = vmsvga3dSurfaceAllocMipLevels(pSurface);
     578            AssertRCReturn(rc, rc);
     579        }
    555580    }
    556581    else if (vmsvga3dIsLegacyBackend(pThisCC))
     
    11051130    if (face == SVGA_ID_INVALID && mipmap == SVGA_ID_INVALID)
    11061131    {
     1132        /* This is a notification that "All images can be lost", i.e. the backend surface is not needed anymore. */
     1133        PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     1134        if (pSvgaR3State->pFuncs3D)
     1135            pSvgaR3State->pFuncs3D->pfnSurfaceDestroy(pThisCC, pSurface);
     1136
    11071137        for (uint32_t i = 0; i < pSurface->cLevels * pSurface->cFaces; ++i)
    11081138        {
     
    13391369}
    13401370
     1371
     1372/*
     1373 *
     1374 * Map
     1375 *
     1376 */
     1377
     1378int vmsvga3dSurfaceMap(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, SVGA3dBox const *pBox,
     1379                       VMSVGA3D_SURFACE_MAP enmMapType, VMSVGA3D_MAPPED_SURFACE *pMap)
     1380{
     1381    PVMSVGA3DSURFACE pSurface;
     1382    int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, pImage->sid, &pSurface);
     1383    AssertRCReturn(rc, rc);
     1384
     1385    if (VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
     1386    {
     1387        PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     1388        AssertReturn(pSvgaR3State->pFuncsMap, VERR_NOT_IMPLEMENTED);
     1389        return pSvgaR3State->pFuncsMap->pfnSurfaceMap(pThisCC, pImage, pBox, enmMapType, pMap);
     1390    }
     1391
     1392    PVMSVGA3DMIPMAPLEVEL pMipLevel;
     1393    rc = vmsvga3dMipmapLevel(pSurface, pImage->face, pImage->mipmap, &pMipLevel);
     1394    ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
     1395
     1396    if (!pMipLevel->pSurfaceData)
     1397    {
     1398        rc = vmsvga3dSurfaceAllocMipLevels(pSurface);
     1399        AssertRCReturn(rc, rc);
     1400    }
     1401
     1402    SVGA3dBox clipBox;
     1403    if (pBox)
     1404    {
     1405        clipBox = *pBox;
     1406        vmsvgaR3ClipBox(&pMipLevel->mipmapSize, &clipBox);
     1407        ASSERT_GUEST_RETURN(clipBox.w && clipBox.h && clipBox.d, VERR_INVALID_PARAMETER);
     1408    }
     1409    else
     1410    {
     1411        clipBox.x = 0;
     1412        clipBox.y = 0;
     1413        clipBox.z = 0;
     1414        clipBox.w = pMipLevel->mipmapSize.width;
     1415        clipBox.h = pMipLevel->mipmapSize.height;
     1416        clipBox.d = pMipLevel->mipmapSize.depth;
     1417    }
     1418
     1419    /// @todo Zero the box?
     1420    //if (enmMapType == VMSVGA3D_SURFACE_MAP_WRITE_DISCARD)
     1421    //    RT_BZERO(.);
     1422
     1423    pMap->enmMapType   = enmMapType;
     1424    pMap->box          = clipBox;
     1425    pMap->cbPixel      = pSurface->cbBlock;
     1426    pMap->cbRowPitch   = pMipLevel->cbSurfacePitch;
     1427    pMap->cbDepthPitch = pMipLevel->cbSurfacePlane;
     1428    pMap->pvData       = (uint8_t *)pMipLevel->pSurfaceData
     1429                       + pMap->box.x * pMap->cbPixel
     1430                       + pMap->box.y * pMap->cbRowPitch
     1431                       + pMap->box.z * pMap->cbDepthPitch;
     1432
     1433    return VINF_SUCCESS;
     1434}
     1435
     1436int vmsvga3dSurfaceUnmap(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, VMSVGA3D_MAPPED_SURFACE *pMap, bool fWritten)
     1437{
     1438    PVMSVGA3DSURFACE pSurface;
     1439    int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, pImage->sid, &pSurface);
     1440    AssertRCReturn(rc, rc);
     1441
     1442    if (VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
     1443    {
     1444        PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
     1445        AssertReturn(pSvgaR3State->pFuncsMap, VERR_NOT_IMPLEMENTED);
     1446        return pSvgaR3State->pFuncsMap->pfnSurfaceUnmap(pThisCC, pImage, pMap, fWritten);
     1447    }
     1448
     1449    PVMSVGA3DMIPMAPLEVEL pMipLevel;
     1450    rc = vmsvga3dMipmapLevel(pSurface, pImage->face, pImage->mipmap, &pMipLevel);
     1451    ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
     1452
     1453    if (   fWritten
     1454        && (   pMap->enmMapType == VMSVGA3D_SURFACE_MAP_WRITE
     1455            || pMap->enmMapType == VMSVGA3D_SURFACE_MAP_READ_WRITE
     1456            || pMap->enmMapType == VMSVGA3D_SURFACE_MAP_WRITE_DISCARD))
     1457    {
     1458        pMipLevel->fDirty = true;
     1459        pSurface->fDirty = true;
     1460    }
     1461
     1462    return VINF_SUCCESS;
     1463}
     1464
     1465
     1466int vmsvga3dCalcSurfaceMipmapAndFace(PVGASTATECC pThisCC, uint32_t sid, uint32_t iSubresource, uint32_t *piMipmap, uint32_t *piFace)
     1467{
     1468    PVMSVGA3DSURFACE pSurface;
     1469    int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, sid, &pSurface);
     1470    AssertRCReturn(rc, rc);
     1471
     1472    vmsvga3dCalcMipmapAndFace(pSurface->cLevels, iSubresource, piMipmap, piFace);
     1473    return VINF_SUCCESS;
     1474}
     1475
     1476
     1477uint32_t vmsvga3dCalcSubresourceOffset(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage)
     1478{
     1479    PVMSVGA3DSURFACE pSurface;
     1480    int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, pImage->sid, &pSurface);
     1481    AssertRCReturn(rc, 0);
     1482
     1483    /** @todo Store cbArraySlice in the surface structure. */
     1484    uint32_t cbArraySlice = 0;
     1485    for (uint32_t i = 0; i < pSurface->cLevels; ++i)
     1486    {
     1487        PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[i];
     1488        cbArraySlice += pMipLevel->cbSurface;
     1489    }
     1490
     1491    uint32_t offMipLevel = 0;
     1492    for (uint32_t i = 0; i < pImage->mipmap; ++i)
     1493    {
     1494        PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
     1495        offMipLevel += pMipmapLevel->cbSurface;
     1496    }
     1497
     1498    uint32_t offSubresource = cbArraySlice * pImage->face + offMipLevel;
     1499    /** @todo Multisample?  */
     1500    return offSubresource;
     1501}
     1502
     1503
     1504/*
     1505 * Calculates memory layout of a surface box for memcpy:
     1506 */
     1507int vmsvga3dGetBoxDimensions(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, SVGA3dBox const *pBox,
     1508                             VMSGA3D_BOX_DIMENSIONS *pResult)
     1509{
     1510    PVMSVGA3DSURFACE pSurface;
     1511    int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, pImage->sid, &pSurface);
     1512    AssertRCReturn(rc, rc);
     1513
     1514    PVMSVGA3DMIPMAPLEVEL pMipLevel;
     1515    rc = vmsvga3dMipmapLevel(pSurface, pImage->face, pImage->mipmap, &pMipLevel);
     1516    ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
     1517
     1518    /* Clip the box. */
     1519    SVGA3dBox clipBox;
     1520    if (pBox)
     1521    {
     1522        clipBox = *pBox;
     1523        vmsvgaR3ClipBox(&pMipLevel->mipmapSize, &clipBox);
     1524        ASSERT_GUEST_RETURN(clipBox.w && clipBox.h && clipBox.d, VERR_INVALID_PARAMETER);
     1525    }
     1526    else
     1527    {
     1528        clipBox.x = 0;
     1529        clipBox.y = 0;
     1530        clipBox.z = 0;
     1531        clipBox.w = pMipLevel->mipmapSize.width;
     1532        clipBox.h = pMipLevel->mipmapSize.height;
     1533        clipBox.d = pMipLevel->mipmapSize.depth;
     1534    }
     1535
     1536    /** @todo Calculate offSubresource here, when pSurface will have cbArraySlice field. */
     1537    pResult->offSubresource = vmsvga3dCalcSubresourceOffset(pThisCC, pImage);
     1538    pResult->offBox   = (clipBox.x / pSurface->cxBlock) * pSurface->cbBlock
     1539                      + (clipBox.y / pSurface->cyBlock) * pMipLevel->cbSurfacePitch
     1540                      + clipBox.z * pMipLevel->cbSurfacePlane;
     1541    pResult->cbRow    = (clipBox.w / pSurface->cxBlock) * pSurface->cbBlock;
     1542    pResult->cbPitch  = pMipLevel->cbSurfacePitch;
     1543    pResult->cyBlocks = clipBox.h / pSurface->cyBlock;
     1544    pResult->cDepth   = clipBox.d;
     1545
     1546    return VINF_SUCCESS;
     1547}
     1548
     1549
    13411550/*
    13421551 * Whether a legacy 3D backend is used.
  • trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d.h

    r89163 r91361  
    7878int vmsvga3dSurfaceDefine(PVGASTATECC pThisCC, uint32_t sid, SVGA3dSurface1Flags surfaceFlags, SVGA3dSurfaceFormat format,
    7979                          uint32_t multisampleCount, SVGA3dTextureFilter autogenFilter,
    80                           uint32_t cMipLevels, SVGA3dSize const *pMipLevel0Size);
     80                          uint32_t cMipLevels, SVGA3dSize const *pMipLevel0Size, bool fAllocMipLevels);
    8181int vmsvga3dSurfaceDestroy(PVGASTATECC pThisCC, uint32_t sid);
    8282int vmsvga3dSurfaceCopy(PVGASTATECC pThisCC, SVGA3dSurfaceImageId dest, SVGA3dSurfaceImageId src,
     
    123123
    124124int vmsvga3dSurfaceInvalidate(PVGASTATECC pThisCC, uint32_t sid, uint32_t face, uint32_t mipmap);
     125
     126int vmsvga3dSurfaceMap(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, SVGA3dBox const *pBox,
     127                           VMSVGA3D_SURFACE_MAP enmMapType, VMSVGA3D_MAPPED_SURFACE *pMap);
     128int vmsvga3dSurfaceUnmap(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, VMSVGA3D_MAPPED_SURFACE *pMap, bool fWritten);
     129
     130uint32_t vmsvga3dCalcSubresourceOffset(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage);
     131
     132typedef struct VMSGA3D_BOX_DIMENSIONS
     133{
     134    uint32_t offSubresource; /* Offset of the miplevel. */
     135    uint32_t offBox;         /* Offset of the box in the miplevel. */
     136    uint32_t cbRow;          /* Bytes per row. */
     137    int32_t  cbPitch;        /* Bytes between rows. */
     138    uint32_t cyBlocks;       /* Number of rows. */
     139    uint32_t cDepth;         /* Number of planes. */
     140} VMSGA3D_BOX_DIMENSIONS;
     141
     142int vmsvga3dGetBoxDimensions(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, SVGA3dBox const *pBox,
     143                             VMSGA3D_BOX_DIMENSIONS *pResult);
     144
     145DECLINLINE(void) vmsvga3dCalcMipmapSize(SVGA3dSize const *pSize0, uint32_t iMipmap, SVGA3dSize *pSize)
     146{
     147    pSize->width  = RT_MAX(pSize0->width  >> iMipmap, 1);
     148    pSize->height = RT_MAX(pSize0->height >> iMipmap, 1);
     149    pSize->depth  = RT_MAX(pSize0->depth  >> iMipmap, 1);
     150}
     151
     152DECLINLINE(uint32_t) vmsvga3dCalcSubresource(uint32_t iMipLevel, uint32_t iFace, uint32_t cMipLevels)
     153{
     154    /* Same as in D3D */
     155    return iMipLevel + iFace * cMipLevels;
     156}
     157
     158DECLINLINE(void) vmsvga3dCalcMipmapAndFace(uint32_t cMipLevels, uint32_t iSubresource, uint32_t *piMipmap, uint32_t *piFace)
     159{
     160    *piFace = iSubresource / cMipLevels;
     161    *piMipmap = iSubresource % cMipLevels;
     162}
     163
     164int vmsvga3dCalcSurfaceMipmapAndFace(PVGASTATECC pThisCC, uint32_t sid, uint32_t iSubresource, uint32_t *piMipmap, uint32_t *piFace);
     165
    125166
    126167/* DevVGA-SVGA3d-shared.h: */
     
    344385typedef struct
    345386{
    346     DECLCALLBACKMEMBER(int, pfnSurfaceMap,   (PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dSurfaceImageId const *pImage, SVGA3dBox const *pBox, VMSVGA3D_SURFACE_MAP enmMapType, VMSVGA3D_MAPPED_SURFACE *pMap));
     387    DECLCALLBACKMEMBER(int, pfnSurfaceMap,   (PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, SVGA3dBox const *pBox, VMSVGA3D_SURFACE_MAP enmMapType, VMSVGA3D_MAPPED_SURFACE *pMap));
    347388    DECLCALLBACKMEMBER(int, pfnSurfaceUnmap, (PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, VMSVGA3D_MAPPED_SURFACE *pMap, bool fWritten));
    348389} VMSVGA3DBACKENDFUNCSMAP;
     
    364405    DECLCALLBACKMEMBER(int, pfnDXDraw,                      (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, uint32_t vertexCount, uint32_t startVertexLocation));
    365406    DECLCALLBACKMEMBER(int, pfnDXDrawIndexed,               (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, uint32_t indexCount, uint32_t startIndexLocation, int32_t baseVertexLocation));
    366     DECLCALLBACKMEMBER(int, pfnDXDrawInstanced,             (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    367     DECLCALLBACKMEMBER(int, pfnDXDrawIndexedInstanced,      (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
     407    DECLCALLBACKMEMBER(int, pfnDXDrawInstanced,             (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, uint32_t vertexCountPerInstance, uint32_t instanceCount, uint32_t startVertexLocation, uint32_t startInstanceLocation));
     408    DECLCALLBACKMEMBER(int, pfnDXDrawIndexedInstanced,      (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, uint32_t indexCountPerInstance, uint32_t instanceCount, uint32_t startIndexLocation, int32_t baseVertexLocation, uint32_t startInstanceLocation));
    368409    DECLCALLBACKMEMBER(int, pfnDXDrawAuto,                  (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    369410    DECLCALLBACKMEMBER(int, pfnDXSetInputLayout,            (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dElementLayoutId elementLayoutId));
     
    392433    DECLCALLBACKMEMBER(int, pfnDXPresentBlt,                (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    393434    DECLCALLBACKMEMBER(int, pfnDXGenMips,                   (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId));
    394     DECLCALLBACKMEMBER(int, pfnDXUpdateSubResource,         (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    395     DECLCALLBACKMEMBER(int, pfnDXReadbackSubResource,       (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    396     DECLCALLBACKMEMBER(int, pfnDXInvalidateSubResource,     (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    397435    DECLCALLBACKMEMBER(int, pfnDXDefineShaderResourceView,  (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId, SVGACOTableDXSRViewEntry const *pEntry));
    398436    DECLCALLBACKMEMBER(int, pfnDXDestroyShaderResourceView, (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderResourceViewId shaderResourceViewId));
     
    405443    DECLCALLBACKMEMBER(int, pfnDXDefineBlendState,          (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dBlendStateId blendId, SVGACOTableDXBlendStateEntry const *pEntry));
    406444    DECLCALLBACKMEMBER(int, pfnDXDestroyBlendState,         (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    407     DECLCALLBACKMEMBER(int, pfnDXDefineDepthStencilState,   (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dDepthStencilStateId depthStencilId, SVGACOTableDXDepthStencilEntry *pEntry));
     445    DECLCALLBACKMEMBER(int, pfnDXDefineDepthStencilState,   (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dDepthStencilStateId depthStencilId, SVGACOTableDXDepthStencilEntry const *pEntry));
    408446    DECLCALLBACKMEMBER(int, pfnDXDestroyDepthStencilState,  (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    409447    DECLCALLBACKMEMBER(int, pfnDXDefineRasterizerState,     (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dRasterizerStateId rasterizerId, SVGACOTableDXRasterizerStateEntry const *pEntry));
     
    411449    DECLCALLBACKMEMBER(int, pfnDXDefineSamplerState,        (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dSamplerId samplerId, SVGACOTableDXSamplerEntry const *pEntry));
    412450    DECLCALLBACKMEMBER(int, pfnDXDestroySamplerState,       (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    413     DECLCALLBACKMEMBER(int, pfnDXDefineShader,              (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, PVMSVGA3DSHADER pShader));
     451    DECLCALLBACKMEMBER(int, pfnDXDefineShader,              (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGA3dShaderId shaderId, SVGACOTableDXShaderEntry const *pEntry));
    414452    DECLCALLBACKMEMBER(int, pfnDXDestroyShader,             (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    415453    DECLCALLBACKMEMBER(int, pfnDXBindShader,                (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, PVMSVGA3DSHADER pShader, void const *pvShaderBytecode));
     
    417455    DECLCALLBACKMEMBER(int, pfnDXDestroyStreamOutput,       (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    418456    DECLCALLBACKMEMBER(int, pfnDXSetStreamOutput,           (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    419     DECLCALLBACKMEMBER(int, pfnDXSetCOTable,                (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGACOTableType type, uint32_t cEntries, uint32_t cValidEntries));
     457    DECLCALLBACKMEMBER(int, pfnDXSetCOTable,                (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext, SVGACOTableType type, uint32_t cValidEntries));
    420458    DECLCALLBACKMEMBER(int, pfnDXBufferCopy,                (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    421     DECLCALLBACKMEMBER(int, pfnDXTransferFromBuffer,        (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    422459    DECLCALLBACKMEMBER(int, pfnDXSurfaceCopyAndReadback,    (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    423460    DECLCALLBACKMEMBER(int, pfnDXMoveQuery,                 (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    424461    DECLCALLBACKMEMBER(int, pfnDXBindAllQuery,              (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    425462    DECLCALLBACKMEMBER(int, pfnDXReadbackAllQuery,          (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    426     DECLCALLBACKMEMBER(int, pfnDXPredTransferFromBuffer,    (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    427463    DECLCALLBACKMEMBER(int, pfnDXMobFence64,                (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
    428464    DECLCALLBACKMEMBER(int, pfnDXBindAllShader,             (PVGASTATECC pThisCC, PVMSVGA3DDXCONTEXT pDXContext));
     
    490526int vmsvga3dDXDestroyContext(PVGASTATECC pThisCC, uint32_t cid);
    491527int vmsvga3dDXBindContext(PVGASTATECC pThisCC, uint32_t cid, SVGADXContextMobFormat *pSvgaDXContext);
    492 int vmsvga3dDXReadbackContext(PVGASTATECC pThisCC, uint32_t idDXContext);
     528int vmsvga3dDXReadbackContext(PVGASTATECC pThisCC, uint32_t idDXContext, SVGADXContextMobFormat *pSvgaDXContext);
    493529int vmsvga3dDXInvalidateContext(PVGASTATECC pThisCC, uint32_t idDXContext);
    494530int vmsvga3dDXSetSingleConstantBuffer(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXSetSingleConstantBuffer const *pCmd);
     
    498534int vmsvga3dDXDraw(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXDraw const *pCmd);
    499535int vmsvga3dDXDrawIndexed(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXDrawIndexed const *pCmd);
    500 int vmsvga3dDXDrawInstanced(PVGASTATECC pThisCC, uint32_t idDXContext);
    501 int vmsvga3dDXDrawIndexedInstanced(PVGASTATECC pThisCC, uint32_t idDXContext);
     536int vmsvga3dDXDrawInstanced(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXDrawInstanced const *pCmd);
     537int vmsvga3dDXDrawIndexedInstanced(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXDrawIndexedInstanced const *pCmd);
    502538int vmsvga3dDXDrawAuto(PVGASTATECC pThisCC, uint32_t idDXContext);
    503539int vmsvga3dDXSetInputLayout(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dElementLayoutId elementLayoutId);
     
    526562int vmsvga3dDXPresentBlt(PVGASTATECC pThisCC, uint32_t idDXContext);
    527563int vmsvga3dDXGenMips(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXGenMips const *pCmd);
    528 int vmsvga3dDXUpdateSubResource(PVGASTATECC pThisCC, uint32_t idDXContext);
    529 int vmsvga3dDXReadbackSubResource(PVGASTATECC pThisCC, uint32_t idDXContext);
    530 int vmsvga3dDXInvalidateSubResource(PVGASTATECC pThisCC, uint32_t idDXContext);
    531564int vmsvga3dDXDefineShaderResourceView(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXDefineShaderResourceView const *pCmd);
    532565int vmsvga3dDXDestroyShaderResourceView(PVGASTATECC pThisCC, uint32_t idDXContext, SVGA3dCmdDXDestroyShaderResourceView const *pCmd);
     
    554587int vmsvga3dDXReadbackCOTable(PVGASTATECC pThisCC, SVGA3dCmdDXReadbackCOTable const *pCmd);
    555588int vmsvga3dDXBufferCopy(PVGASTATECC pThisCC, uint32_t idDXContext);
    556 int vmsvga3dDXTransferFromBuffer(PVGASTATECC pThisCC, uint32_t idDXContext);
    557589int vmsvga3dDXSurfaceCopyAndReadback(PVGASTATECC pThisCC, uint32_t idDXContext);
    558590int vmsvga3dDXMoveQuery(PVGASTATECC pThisCC, uint32_t idDXContext);
    559591int vmsvga3dDXBindAllQuery(PVGASTATECC pThisCC, uint32_t idDXContext);
    560592int vmsvga3dDXReadbackAllQuery(PVGASTATECC pThisCC, uint32_t idDXContext);
    561 int vmsvga3dDXPredTransferFromBuffer(PVGASTATECC pThisCC, uint32_t idDXContext);
    562593int vmsvga3dDXMobFence64(PVGASTATECC pThisCC, uint32_t idDXContext);
    563594int vmsvga3dDXBindAllShader(PVGASTATECC pThisCC, uint32_t idDXContext);
  • trunk/src/VBox/Devices/Makefile.kmk

    r91331 r91361  
    331331       Graphics/DevVGA-SVGA3d-dx-shader.cpp
    332332   if "$(KBUILD_TARGET)" == "win"
     333    VBoxDD_DEFS         += VMSVGA3D_DX_BACKEND
    333334    VBoxDD_SOURCES      += \
    334335       Graphics/DevVGA-SVGA3d-win-dx.cpp
Note: See TracChangeset for help on using the changeset viewer.

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