third_party/virtualbox/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win.cpp
/**
 * Destroy an existing 3d context
 *
 * @returns VBox status code.
 * @param   pThis           VGA device instance data.
 * @param   cid             Context id
 */
Destroy an existing 3d context
@returns VBox status code.
@param   pThis           VGA device instance data.
@param   cid             Context id
Destroy an existing 3d context @returns VBox status code . @param pThis VGA device instance data . @param cid Context id
int vmsvga3dContextDestroy(PVGASTATE pThis, uint32_t cid)
{
    PVMSVGA3DSTATE pState = pThis->svga.p3dState;
    AssertReturn(pState, VERR_NO_MEMORY);
    AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
    if (    cid < pState->cContexts
        &&  pState->papContexts[cid]->id == cid)
    {
        PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
        Log(("vmsvga3dContextDestroy id %x\n", cid));
        for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
        {
            PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
            if (    pSurface->id == sid
                &&  pSurface->idAssociatedContext == cid)
            {
                int rc;
                LogFunc(("Remove all dependencies for surface sid=%x\n", sid));
                uint32_t            surfaceFlags = pSurface->surfaceFlags;
                SVGA3dSurfaceFormat format = pSurface->format;
                SVGA3dSurfaceFace   face[SVGA3D_MAX_SURFACE_FACES];
                uint32_t            multisampleCount = pSurface->multiSampleCount;
                SVGA3dTextureFilter autogenFilter = pSurface->autogenFilter;
                SVGA3dSize         *pMipLevelSize;
                uint32_t            cFaces = pSurface->cFaces;
                pMipLevelSize = (SVGA3dSize *)RTMemAllocZ(pSurface->faces[0].numMipLevels * pSurface->cFaces * sizeof(SVGA3dSize));
                AssertReturn(pMipLevelSize, VERR_NO_MEMORY);
                for (uint32_t face=0; face < pSurface->cFaces; face++)
                {
                    for (uint32_t i = 0; i < pSurface->faces[0].numMipLevels; i++)
                    {
                        uint32_t idx = i + face * pSurface->faces[0].numMipLevels;
                        memcpy(&pMipLevelSize[idx], &pSurface->pMipmapLevels[idx].mipmapSize, sizeof(SVGA3dSize));
                    }
                }
                memcpy(face, pSurface->faces, sizeof(pSurface->faces));
                Assert(pSurface->pSharedObjectTree == NULL);
                rc = vmsvga3dSurfaceDestroy(pThis, sid);
                AssertRC(rc);
                rc = vmsvga3dSurfaceDefine(pThis, sid, surfaceFlags, format, face, multisampleCount, autogenFilter, face[0].numMipLevels * cFaces, pMipLevelSize);
                AssertRC(rc);
                Assert(!pSurface->u.pSurface);
            }
            else
            {
                PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTAvlU32Get(&pSurface->pSharedObjectTree, cid);
                if (pSharedSurface)
                {
                    LogFunc(("Remove shared dependency for surface sid=%x\n", sid));
                    switch (pSurface->enmD3DResType)
                    {
                    case VMSVGA3D_D3DRESTYPE_TEXTURE:
                        Assert(pSharedSurface->u.pTexture);
                        D3D_RELEASE(pSharedSurface->u.pTexture);
                        break;
                    case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
                        Assert(pSharedSurface->u.pCubeTexture);
                        D3D_RELEASE(pSharedSurface->u.pCubeTexture);
                        break;
                    case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
                        Assert(pSharedSurface->u.pVolumeTexture);
                        D3D_RELEASE(pSharedSurface->u.pVolumeTexture);
                        break;
                    default:
                        AssertFailed();
                        break;
                    }
                    RTAvlU32Remove(&pSurface->pSharedObjectTree, cid);
                    RTMemFree(pSharedSurface);
                }
            }
        }
        for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
        {
            if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
                vmsvga3dShaderDestroy(pThis, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
        }
        if (pContext->paPixelShader)
            RTMemFree(pContext->paPixelShader);
        for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
        {
            if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
                vmsvga3dShaderDestroy(pThis, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
        }
        if (pContext->paVertexShader)
            RTMemFree(pContext->paVertexShader);
        if (pContext->state.paVertexShaderConst)
            RTMemFree(pContext->state.paVertexShaderConst);
        if (pContext->state.paPixelShaderConst)
            RTMemFree(pContext->state.paPixelShaderConst);
        vmsvga3dOcclusionQueryDelete(pState, pContext);
        D3D_RELEASE(pContext->pDevice);
        int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
        AssertRC(rc);
        memset(pContext, 0, sizeof(*pContext));
        pContext->id = SVGA3D_INVALID_ID;
    }
    else
        AssertFailed();
    return VINF_SUCCESS;
}
int vmsvga3dContextDestroy ( PVGASTATE pThis , uint32_t cid ) { PVMSVGA3DSTATE pState = pThis -> svga . p3dState ; AssertReturn ( pState , VERR_NO_MEMORY ) ; AssertReturn ( cid < SVGA3D_MAX_CONTEXT_IDS , VERR_INVALID_PARAMETER ) ; if ( cid < pState -> cContexts && pState -> papContexts [ cid ] -> id == cid ) { PVMSVGA3DCONTEXT pContext = pState -> papContexts [ cid ] ; Log ( ( " \n " , cid ) ) ; for ( uint32_t sid = 0 ; sid < pState -> cSurfaces ; sid ++ ) { PVMSVGA3DSURFACE pSurface = pState -> papSurfaces [ sid ] ; if ( pSurface -> id == sid && pSurface -> idAssociatedContext == cid ) { int rc ; LogFunc ( ( " \n " , sid ) ) ; uint32_t surfaceFlags = pSurface -> surfaceFlags ; SVGA3dSurfaceFormat format = pSurface -> format ; SVGA3dSurfaceFace face [ SVGA3D_MAX_SURFACE_FACES ] ; uint32_t multisampleCount = pSurface -> multiSampleCount ; SVGA3dTextureFilter autogenFilter = pSurface -> autogenFilter ; SVGA3dSize * pMipLevelSize ; uint32_t cFaces = pSurface -> cFaces ; pMipLevelSize = ( SVGA3dSize * ) RTMemAllocZ ( pSurface -> faces [ 0 ] . numMipLevels * pSurface -> cFaces * sizeof ( SVGA3dSize ) ) ; AssertReturn ( pMipLevelSize , VERR_NO_MEMORY ) ; for ( uint32_t face = 0 ; face < pSurface -> cFaces ; face ++ ) { for ( uint32_t i = 0 ; i < pSurface -> faces [ 0 ] . numMipLevels ; i ++ ) { uint32_t idx = i + face * pSurface -> faces [ 0 ] . numMipLevels ; memcpy ( & pMipLevelSize [ idx ] , & pSurface -> pMipmapLevels [ idx ] . mipmapSize , sizeof ( SVGA3dSize ) ) ; } } memcpy ( face , pSurface -> faces , sizeof ( pSurface -> faces ) ) ; Assert ( pSurface -> pSharedObjectTree == NULL ) ; rc = vmsvga3dSurfaceDestroy ( pThis , sid ) ; AssertRC ( rc ) ; rc = vmsvga3dSurfaceDefine ( pThis , sid , surfaceFlags , format , face , multisampleCount , autogenFilter , face [ 0 ] . numMipLevels * cFaces , pMipLevelSize ) ; AssertRC ( rc ) ; Assert ( ! pSurface -> u . pSurface ) ; } else { PVMSVGA3DSHAREDSURFACE pSharedSurface = ( PVMSVGA3DSHAREDSURFACE ) RTAvlU32Get ( & pSurface -> pSharedObjectTree , cid ) ; if ( pSharedSurface ) { LogFunc ( ( " \n " , sid ) ) ; switch ( pSurface -> enmD3DResType ) { case VMSVGA3D_D3DRESTYPE_TEXTURE : Assert ( pSharedSurface -> u . pTexture ) ; D3D_RELEASE ( pSharedSurface -> u . pTexture ) ; break ; case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE : Assert ( pSharedSurface -> u . pCubeTexture ) ; D3D_RELEASE ( pSharedSurface -> u . pCubeTexture ) ; break ; case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE : Assert ( pSharedSurface -> u . pVolumeTexture ) ; D3D_RELEASE ( pSharedSurface -> u . pVolumeTexture ) ; break ; default : AssertFailed ( ) ; break ; } RTAvlU32Remove ( & pSurface -> pSharedObjectTree , cid ) ; RTMemFree ( pSharedSurface ) ; } } } for ( uint32_t i = 0 ; i < pContext -> cPixelShaders ; i ++ ) { if ( pContext -> paPixelShader [ i ] . id != SVGA3D_INVALID_ID ) vmsvga3dShaderDestroy ( pThis , pContext -> paPixelShader [ i ] . cid , pContext -> paPixelShader [ i ] . id , pContext -> paPixelShader [ i ] . type ) ; } if ( pContext -> paPixelShader ) RTMemFree ( pContext -> paPixelShader ) ; for ( uint32_t i = 0 ; i < pContext -> cVertexShaders ; i ++ ) { if ( pContext -> paVertexShader [ i ] . id != SVGA3D_INVALID_ID ) vmsvga3dShaderDestroy ( pThis , pContext -> paVertexShader [ i ] . cid , pContext -> paVertexShader [ i ] . id , pContext -> paVertexShader [ i ] . type ) ; } if ( pContext -> paVertexShader ) RTMemFree ( pContext -> paVertexShader ) ; if ( pContext -> state . paVertexShaderConst ) RTMemFree ( pContext -> state . paVertexShaderConst ) ; if ( pContext -> state . paPixelShaderConst ) RTMemFree ( pContext -> state . paPixelShaderConst ) ; vmsvga3dOcclusionQueryDelete ( pState , pContext ) ; D3D_RELEASE ( pContext -> pDevice ) ; int rc = vmsvga3dSendThreadMessage ( pState -> pWindowThread , pState -> WndRequestSem , WM_VMSVGA3D_DESTROYWINDOW , ( WPARAM ) pContext -> hwnd , 0 ) ; AssertRC ( rc ) ; memset ( pContext , 0 , sizeof ( * pContext ) ) ; pContext -> id = SVGA3D_INVALID_ID ; } else AssertFailed ( ) ; return VINF_SUCCESS ; }
Destroy an existing 3d context
 @returns VBox status code.
Destroy an existing 3d context @returns VBox status code .
/* Check for all surfaces that are associated with this context to remove all dependencies */ /* Recreate the surface with the original settings; destroys the contents, but that seems fairly safe since the context is also destroyed. */ /** @todo not safe with shared objects */ /* Check for a shared surface object. */ /* Destroy all leftover pixel shaders. */ /* Destroy all leftover vertex shaders. */ /* Release the D3D device object */ /* Destroy the window we've created. */