/* $Id: DevVGA-SVGA3d-savedstate.cpp 86839 2020-11-09 23:21:56Z vboxsync $ */ /** @file * DevSVGA3d - VMWare SVGA device, 3D parts - Saved state and assocated stuff. */ /* * Copyright (C) 2013-2020 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. */ /********************************************************************************************************************************* * Header Files * *********************************************************************************************************************************/ #define LOG_GROUP LOG_GROUP_DEV_VMSVGA #include #include #include #include #include #include /* required by DevVGA.h */ #include /* required by DevVGA.h */ /* should go BEFORE any other DevVGA include to make all DevVGA.h config defines be visible */ #include "DevVGA.h" #include "DevVGA-SVGA.h" #include "DevVGA-SVGA3d.h" #define VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS #include "DevVGA-SVGA3d-internal.h" /** * Reinitializes an active context. * * @returns VBox status code. * @param pThisCC The VGA/VMSVGA state for ring-3. * @param pContext The freshly loaded context to reinitialize. */ static int vmsvga3dLoadReinitContext(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext) { int rc; uint32_t cid = pContext->id; Assert(cid != SVGA3D_INVALID_ID); /* First set the render targets as they change the internal state (reset viewport etc) */ Log(("vmsvga3dLoadReinitContext: Recreate render targets BEGIN [cid=%#x]\n", cid)); for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aRenderTargets); j++) { if (pContext->state.aRenderTargets[j] != SVGA3D_INVALID_ID) { SVGA3dSurfaceImageId target; target.sid = pContext->state.aRenderTargets[j]; target.face = 0; target.mipmap = 0; rc = vmsvga3dSetRenderTarget(pThisCC, cid, (SVGA3dRenderTargetType)j, target); AssertRCReturn(rc, rc); } } Log(("vmsvga3dLoadReinitContext: Recreate render targets END\n")); /* Recreate the render state */ Log(("vmsvga3dLoadReinitContext: Recreate render state BEGIN\n")); for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aRenderState); j++) { SVGA3dRenderState *pRenderState = &pContext->state.aRenderState[j]; if (pRenderState->state != SVGA3D_RS_INVALID) vmsvga3dSetRenderState(pThisCC, pContext->id, 1, pRenderState); } Log(("vmsvga3dLoadReinitContext: Recreate render state END\n")); /* Recreate the texture state */ Log(("vmsvga3dLoadReinitContext: Recreate texture state BEGIN\n")); for (uint32_t iStage = 0; iStage < RT_ELEMENTS(pContext->state.aTextureStates); ++iStage) { for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTextureStates[0]); ++j) { SVGA3dTextureState *pTextureState = &pContext->state.aTextureStates[iStage][j]; if (pTextureState->name != SVGA3D_TS_INVALID) vmsvga3dSetTextureState(pThisCC, pContext->id, 1, pTextureState); } } Log(("vmsvga3dLoadReinitContext: Recreate texture state END\n")); /* Reprogram the clip planes. */ for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aClipPlane); j++) { if (pContext->state.aClipPlane[j].fValid == true) vmsvga3dSetClipPlane(pThisCC, cid, j, pContext->state.aClipPlane[j].plane); } /* Reprogram the light data. */ for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aLightData); j++) { if (pContext->state.aLightData[j].fValidData == true) vmsvga3dSetLightData(pThisCC, cid, j, &pContext->state.aLightData[j].data); if (pContext->state.aLightData[j].fEnabled) vmsvga3dSetLightEnabled(pThisCC, cid, j, true); } /* Recreate the transform state. */ if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_TRANSFORM) { for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTransformState); j++) { if (pContext->state.aTransformState[j].fValid == true) vmsvga3dSetTransform(pThisCC, cid, (SVGA3dTransformType)j, pContext->state.aTransformState[j].matrix); } } /* Reprogram the material data. */ if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_MATERIAL) { for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aMaterial); j++) { if (pContext->state.aMaterial[j].fValid == true) vmsvga3dSetMaterial(pThisCC, cid, (SVGA3dFace)j, &pContext->state.aMaterial[j].material); } } if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT) vmsvga3dSetScissorRect(pThisCC, cid, &pContext->state.RectScissor); if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_ZRANGE) vmsvga3dSetZRange(pThisCC, cid, pContext->state.zRange); if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT) vmsvga3dSetViewPort(pThisCC, cid, &pContext->state.RectViewPort); if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VERTEXSHADER) vmsvga3dShaderSet(pThisCC, pContext, cid, SVGA3D_SHADERTYPE_VS, pContext->state.shidVertex); if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_PIXELSHADER) vmsvga3dShaderSet(pThisCC, pContext, cid, SVGA3D_SHADERTYPE_PS, pContext->state.shidPixel); Log(("vmsvga3dLoadReinitContext: returns [cid=%#x]\n", cid)); return VINF_SUCCESS; } static int vmsvga3dLoadVMSVGA3DSURFACEPreMipLevels(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, VMSVGA3DSURFACE *pSurface) { struct VMSVGA3DSURFACEPreMipLevels { uint32_t id; #ifdef VMSVGA3D_OPENGL uint32_t idWeakContextAssociation; #else uint32_t idAssociatedContext; #endif uint32_t surfaceFlags; SVGA3dSurfaceFormat format; #ifdef VMSVGA3D_OPENGL GLint internalFormatGL; GLint formatGL; GLint typeGL; #endif SVGA3dSurfaceFace faces[SVGA3D_MAX_SURFACE_FACES]; uint32_t cFaces; uint32_t multiSampleCount; SVGA3dTextureFilter autogenFilter; uint32_t cbBlock; }; static SSMFIELD const s_aVMSVGA3DSURFACEFieldsPreMipLevels[] = { SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, id), #ifdef VMSVGA3D_OPENGL SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, idWeakContextAssociation), #else SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, idAssociatedContext), #endif SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, surfaceFlags), SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, format), #ifdef VMSVGA3D_OPENGL SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, internalFormatGL), SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, formatGL), SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, typeGL), #endif SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, faces), SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, cFaces), SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, multiSampleCount), SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, autogenFilter), #ifdef VMSVGA3D_DIRECT3D SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, format), /* Yes, the 'format' field is duplicated. */ #endif SSMFIELD_ENTRY(struct VMSVGA3DSURFACEPreMipLevels, cbBlock), SSMFIELD_ENTRY_TERM() }; struct VMSVGA3DSURFACEPreMipLevels surfacePreMipLevels; int rc = pDevIns->pHlpR3->pfnSSMGetStructEx(pSSM, &surfacePreMipLevels, sizeof(surfacePreMipLevels), 0, s_aVMSVGA3DSURFACEFieldsPreMipLevels, NULL); if (RT_SUCCESS(rc)) { pSurface->id = surfacePreMipLevels.id; #ifdef VMSVGA3D_OPENGL pSurface->idWeakContextAssociation = surfacePreMipLevels.idWeakContextAssociation; #else pSurface->idAssociatedContext = surfacePreMipLevels.idAssociatedContext; #endif pSurface->surfaceFlags = surfacePreMipLevels.surfaceFlags; pSurface->format = surfacePreMipLevels.format; #ifdef VMSVGA3D_OPENGL pSurface->internalFormatGL = surfacePreMipLevels.internalFormatGL; pSurface->formatGL = surfacePreMipLevels.formatGL; pSurface->typeGL = surfacePreMipLevels.typeGL; #endif pSurface->cLevels = surfacePreMipLevels.faces[0].numMipLevels; pSurface->cFaces = surfacePreMipLevels.cFaces; pSurface->multiSampleCount = surfacePreMipLevels.multiSampleCount; pSurface->autogenFilter = surfacePreMipLevels.autogenFilter; pSurface->cbBlock = surfacePreMipLevels.cbBlock; } return rc; } int vmsvga3dLoadExec(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass) { RT_NOREF(pDevIns, pThis, uPass); PVMSVGA3DSTATE pState = pThisCC->svga.p3dState; AssertReturn(pState, VERR_NO_MEMORY); PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3; int rc; uint32_t cContexts, cSurfaces; LogFlow(("vmsvga3dLoadExec:\n")); #ifndef RT_OS_DARWIN /** @todo r=bird: this is normally done on the EMT, so for DARWIN we do that when loading saved state too now. See DevVGA-SVGA.cpp */ /* Must initialize now as the recreation calls below rely on an initialized 3d subsystem. */ vmsvga3dPowerOn(pDevIns, pThis, pThisCC); #endif /* Get the generic 3d state first. */ rc = pHlp->pfnSSMGetStructEx(pSSM, pState, sizeof(*pState), 0, g_aVMSVGA3DSTATEFields, NULL); AssertRCReturn(rc, rc); cContexts = pState->cContexts; cSurfaces = pState->cSurfaces; pState->cContexts = 0; pState->cSurfaces = 0; /* Fetch all active contexts. */ for (uint32_t i = 0; i < cContexts; i++) { PVMSVGA3DCONTEXT pContext; uint32_t cid; /* Get the context id */ rc = pHlp->pfnSSMGetU32(pSSM, &cid); AssertRCReturn(rc, rc); if (cid != SVGA3D_INVALID_ID) { uint32_t cPixelShaderConst, cVertexShaderConst, cPixelShaders, cVertexShaders; LogFlow(("vmsvga3dLoadExec: Loading cid=%#x\n", cid)); #ifdef VMSVGA3D_OPENGL if (cid == VMSVGA3D_SHARED_CTX_ID) { i--; /* Not included in cContexts. */ pContext = &pState->SharedCtx; if (pContext->id != VMSVGA3D_SHARED_CTX_ID) { rc = vmsvga3dContextDefineOgl(pThisCC, VMSVGA3D_SHARED_CTX_ID, VMSVGA3D_DEF_CTX_F_SHARED_CTX); AssertRCReturn(rc, rc); } } else #endif { rc = vmsvga3dContextDefine(pThisCC, cid); AssertRCReturn(rc, rc); pContext = pState->papContexts[i]; } AssertReturn(pContext->id == cid, VERR_INTERNAL_ERROR); rc = pHlp->pfnSSMGetStructEx(pSSM, pContext, sizeof(*pContext), 0, g_aVMSVGA3DCONTEXTFields, NULL); AssertRCReturn(rc, rc); cPixelShaders = pContext->cPixelShaders; cVertexShaders = pContext->cVertexShaders; cPixelShaderConst = pContext->state.cPixelShaderConst; cVertexShaderConst = pContext->state.cVertexShaderConst; pContext->cPixelShaders = 0; pContext->cVertexShaders = 0; pContext->state.cPixelShaderConst = 0; pContext->state.cVertexShaderConst = 0; /* Fetch all pixel shaders. */ for (uint32_t j = 0; j < cPixelShaders; j++) { VMSVGA3DSHADER shader; uint32_t shid; /* Fetch the id first. */ rc = pHlp->pfnSSMGetU32(pSSM, &shid); AssertRCReturn(rc, rc); if (shid != SVGA3D_INVALID_ID) { uint32_t *pData; /* Fetch a copy of the shader struct. */ rc = pHlp->pfnSSMGetStructEx(pSSM, &shader, sizeof(shader), 0, g_aVMSVGA3DSHADERFields, NULL); AssertRCReturn(rc, rc); pData = (uint32_t *)RTMemAlloc(shader.cbData); AssertReturn(pData, VERR_NO_MEMORY); rc = pHlp->pfnSSMGetMem(pSSM, pData, shader.cbData); AssertRCReturn(rc, rc); rc = vmsvga3dShaderDefine(pThisCC, cid, shid, shader.type, shader.cbData, pData); AssertRCReturn(rc, rc); RTMemFree(pData); } } /* Fetch all vertex shaders. */ for (uint32_t j = 0; j < cVertexShaders; j++) { VMSVGA3DSHADER shader; uint32_t shid; /* Fetch the id first. */ rc = pHlp->pfnSSMGetU32(pSSM, &shid); AssertRCReturn(rc, rc); if (shid != SVGA3D_INVALID_ID) { uint32_t *pData; /* Fetch a copy of the shader struct. */ rc = pHlp->pfnSSMGetStructEx(pSSM, &shader, sizeof(shader), 0, g_aVMSVGA3DSHADERFields, NULL); AssertRCReturn(rc, rc); pData = (uint32_t *)RTMemAlloc(shader.cbData); AssertReturn(pData, VERR_NO_MEMORY); rc = pHlp->pfnSSMGetMem(pSSM, pData, shader.cbData); AssertRCReturn(rc, rc); rc = vmsvga3dShaderDefine(pThisCC, cid, shid, shader.type, shader.cbData, pData); AssertRCReturn(rc, rc); RTMemFree(pData); } } /* Fetch pixel shader constants. */ for (uint32_t j = 0; j < cPixelShaderConst; j++) { VMSVGASHADERCONST ShaderConst; rc = pHlp->pfnSSMGetStructEx(pSSM, &ShaderConst, sizeof(ShaderConst), 0, g_aVMSVGASHADERCONSTFields, NULL); AssertRCReturn(rc, rc); if (ShaderConst.fValid) { rc = vmsvga3dShaderSetConst(pThisCC, cid, j, SVGA3D_SHADERTYPE_PS, ShaderConst.ctype, 1, ShaderConst.value); AssertRCReturn(rc, rc); } } /* Fetch vertex shader constants. */ for (uint32_t j = 0; j < cVertexShaderConst; j++) { VMSVGASHADERCONST ShaderConst; rc = pHlp->pfnSSMGetStructEx(pSSM, &ShaderConst, sizeof(ShaderConst), 0, g_aVMSVGASHADERCONSTFields, NULL); AssertRCReturn(rc, rc); if (ShaderConst.fValid) { rc = vmsvga3dShaderSetConst(pThisCC, cid, j, SVGA3D_SHADERTYPE_VS, ShaderConst.ctype, 1, ShaderConst.value); AssertRCReturn(rc, rc); } } if (uVersion >= VGA_SAVEDSTATE_VERSION_VMSVGA_TEX_STAGES) { /* Load texture stage and samplers state. */ /* Number of stages/samplers. */ uint32_t cStages; rc = pHlp->pfnSSMGetU32(pSSM, &cStages); AssertRCReturn(rc, rc); /* Number of states. */ uint32_t cTextureStates; rc = pHlp->pfnSSMGetU32(pSSM, &cTextureStates); AssertRCReturn(rc, rc); for (uint32_t iStage = 0; iStage < cStages; ++iStage) { for (uint32_t j = 0; j < cTextureStates; ++j) { SVGA3dTextureState textureState; pHlp->pfnSSMGetU32(pSSM, &textureState.stage); uint32_t u32Name; pHlp->pfnSSMGetU32(pSSM, &u32Name); textureState.name = (SVGA3dTextureStateName)u32Name; rc = pHlp->pfnSSMGetU32(pSSM, &textureState.value); AssertRCReturn(rc, rc); if ( iStage < RT_ELEMENTS(pContext->state.aTextureStates) && j < RT_ELEMENTS(pContext->state.aTextureStates[0])) { pContext->state.aTextureStates[iStage][j] = textureState; } } } } if (uVersion >= VGA_SAVEDSTATE_VERSION_VMSVGA) { VMSVGA3DQUERY query; RT_ZERO(query); rc = pHlp->pfnSSMGetStructEx(pSSM, &query, sizeof(query), 0, g_aVMSVGA3DQUERYFields, NULL); AssertRCReturn(rc, rc); switch (query.enmQueryState) { case VMSVGA3DQUERYSTATE_BUILDING: /* Start collecting data. */ vmsvga3dQueryBegin(pThisCC, cid, SVGA3D_QUERYTYPE_OCCLUSION); /* Partial result. */ pContext->occlusion.u32QueryResult = query.u32QueryResult; break; case VMSVGA3DQUERYSTATE_ISSUED: /* Guest ended the query but did not read result. Result is restored. */ query.enmQueryState = VMSVGA3DQUERYSTATE_SIGNALED; RT_FALL_THRU(); case VMSVGA3DQUERYSTATE_SIGNALED: /* Create the query object. */ vmsvga3dOcclusionQueryCreate(pState, pContext); /* Update result and state. */ pContext->occlusion.enmQueryState = query.enmQueryState; pContext->occlusion.u32QueryResult = query.u32QueryResult; break; default: AssertFailed(); RT_FALL_THRU(); case VMSVGA3DQUERYSTATE_NULL: RT_ZERO(pContext->occlusion); break; } } } } #ifdef VMSVGA3D_OPENGL /* Make the shared context the current one. */ if (pState->SharedCtx.id == VMSVGA3D_SHARED_CTX_ID) VMSVGA3D_SET_CURRENT_CONTEXT(pState, &pState->SharedCtx); #endif /* Fetch all surfaces. */ for (uint32_t i = 0; i < cSurfaces; i++) { uint32_t sid; /* Fetch the id first. */ rc = pHlp->pfnSSMGetU32(pSSM, &sid); AssertRCReturn(rc, rc); if (sid != SVGA3D_INVALID_ID) { VMSVGA3DSURFACE surface; LogFlow(("vmsvga3dLoadExec: Loading sid=%#x\n", sid)); /* Fetch the surface structure first. */ if (RT_LIKELY(uVersion >= VGA_SAVEDSTATE_VERSION_VMSVGA_MIPLEVELS)) rc = pHlp->pfnSSMGetStructEx(pSSM, &surface, sizeof(surface), 0, g_aVMSVGA3DSURFACEFields, NULL); else rc = vmsvga3dLoadVMSVGA3DSURFACEPreMipLevels(pDevIns, pSSM, &surface); AssertRCReturn(rc, rc); { uint32_t cMipLevels = surface.cLevels * surface.cFaces; PVMSVGA3DMIPMAPLEVEL pMipmapLevel = (PVMSVGA3DMIPMAPLEVEL)RTMemAlloc(cMipLevels * sizeof(VMSVGA3DMIPMAPLEVEL)); AssertReturn(pMipmapLevel, VERR_NO_MEMORY); SVGA3dSize *pMipmapLevelSize = (SVGA3dSize *)RTMemAlloc(cMipLevels * sizeof(SVGA3dSize)); AssertReturn(pMipmapLevelSize, VERR_NO_MEMORY); /* Load the mip map level info. */ for (uint32_t face=0; face < surface.cFaces; face++) { for (uint32_t j = 0; j < surface.cLevels; j++) { uint32_t idx = j + face * surface.cLevels; /* Load the mip map level struct. */ rc = pHlp->pfnSSMGetStructEx(pSSM, &pMipmapLevel[idx], sizeof(pMipmapLevel[idx]), 0, g_aVMSVGA3DMIPMAPLEVELFields, NULL); AssertRCReturn(rc, rc); pMipmapLevelSize[idx] = pMipmapLevel[idx].mipmapSize; } } rc = vmsvga3dSurfaceDefine(pThisCC, sid, surface.surfaceFlags, surface.format, surface.multiSampleCount, surface.autogenFilter, surface.cLevels, &pMipmapLevelSize[0]); AssertRCReturn(rc, rc); RTMemFree(pMipmapLevelSize); RTMemFree(pMipmapLevel); } PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid]; Assert(pSurface->id == sid); pSurface->fDirty = false; /* Load the mip map level data. */ for (uint32_t j = 0; j < pSurface->cLevels * pSurface->cFaces; j++) { PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[j]; bool fDataPresent = false; /* vmsvga3dSurfaceDefine already allocated the surface data buffer. */ Assert(pMipmapLevel->cbSurface); AssertReturn(pMipmapLevel->pSurfaceData, VERR_INTERNAL_ERROR); /* Fetch the data present boolean first. */ rc = pHlp->pfnSSMGetBool(pSSM, &fDataPresent); AssertRCReturn(rc, rc); Log(("Surface sid=%u: load mipmap level %d with %x bytes data (present=%d).\n", sid, j, pMipmapLevel->cbSurface, fDataPresent)); if (fDataPresent) { rc = pHlp->pfnSSMGetMem(pSSM, pMipmapLevel->pSurfaceData, pMipmapLevel->cbSurface); AssertRCReturn(rc, rc); pMipmapLevel->fDirty = true; pSurface->fDirty = true; } else { pMipmapLevel->fDirty = false; } } } } #ifdef VMSVGA3D_OPENGL /* Reinitialize the shared context. */ LogFlow(("vmsvga3dLoadExec: pState->SharedCtx.id=%#x\n", pState->SharedCtx.id)); if (pState->SharedCtx.id == VMSVGA3D_SHARED_CTX_ID) { rc = vmsvga3dLoadReinitContext(pThisCC, &pState->SharedCtx); AssertRCReturn(rc, rc); } #endif /* Reinitialize all active contexts. */ for (uint32_t i = 0; i < pState->cContexts; i++) { PVMSVGA3DCONTEXT pContext = pState->papContexts[i]; if (pContext->id != SVGA3D_INVALID_ID) { rc = vmsvga3dLoadReinitContext(pThisCC, pContext); AssertRCReturn(rc, rc); } } LogFlow(("vmsvga3dLoadExec: return success\n")); return VINF_SUCCESS; } static int vmsvga3dSaveContext(PCPDMDEVHLPR3 pHlp, PVGASTATECC pThisCC, PSSMHANDLE pSSM, PVMSVGA3DCONTEXT pContext) { PVMSVGA3DSTATE pState = pThisCC->svga.p3dState; uint32_t cid = pContext->id; /* Save the id first. */ int rc = pHlp->pfnSSMPutU32(pSSM, cid); AssertRCReturn(rc, rc); if (cid != SVGA3D_INVALID_ID) { /* Save a copy of the context structure first. */ rc = pHlp->pfnSSMPutStructEx(pSSM, pContext, sizeof(*pContext), 0, g_aVMSVGA3DCONTEXTFields, NULL); AssertRCReturn(rc, rc); /* Save all pixel shaders. */ for (uint32_t j = 0; j < pContext->cPixelShaders; j++) { PVMSVGA3DSHADER pShader = &pContext->paPixelShader[j]; /* Save the id first. */ rc = pHlp->pfnSSMPutU32(pSSM, pShader->id); AssertRCReturn(rc, rc); if (pShader->id != SVGA3D_INVALID_ID) { uint32_t cbData = pShader->cbData; /* Save a copy of the shader struct. */ rc = pHlp->pfnSSMPutStructEx(pSSM, pShader, sizeof(*pShader), 0, g_aVMSVGA3DSHADERFields, NULL); AssertRCReturn(rc, rc); Log(("Save pixelshader shid=%d with %x bytes code.\n", pShader->id, cbData)); rc = pHlp->pfnSSMPutMem(pSSM, pShader->pShaderProgram, cbData); AssertRCReturn(rc, rc); } } /* Save all vertex shaders. */ for (uint32_t j = 0; j < pContext->cVertexShaders; j++) { PVMSVGA3DSHADER pShader = &pContext->paVertexShader[j]; /* Save the id first. */ rc = pHlp->pfnSSMPutU32(pSSM, pShader->id); AssertRCReturn(rc, rc); if (pShader->id != SVGA3D_INVALID_ID) { uint32_t cbData = pShader->cbData; /* Save a copy of the shader struct. */ rc = pHlp->pfnSSMPutStructEx(pSSM, pShader, sizeof(*pShader), 0, g_aVMSVGA3DSHADERFields, NULL); AssertRCReturn(rc, rc); Log(("Save vertex shader shid=%d with %x bytes code.\n", pShader->id, cbData)); /* Fetch the shader code and save it. */ rc = pHlp->pfnSSMPutMem(pSSM, pShader->pShaderProgram, cbData); AssertRCReturn(rc, rc); } } /* Save pixel shader constants. */ for (uint32_t j = 0; j < pContext->state.cPixelShaderConst; j++) { rc = pHlp->pfnSSMPutStructEx(pSSM, &pContext->state.paPixelShaderConst[j], sizeof(pContext->state.paPixelShaderConst[j]), 0, g_aVMSVGASHADERCONSTFields, NULL); AssertRCReturn(rc, rc); } /* Save vertex shader constants. */ for (uint32_t j = 0; j < pContext->state.cVertexShaderConst; j++) { rc = pHlp->pfnSSMPutStructEx(pSSM, &pContext->state.paVertexShaderConst[j], sizeof(pContext->state.paVertexShaderConst[j]), 0, g_aVMSVGASHADERCONSTFields, NULL); AssertRCReturn(rc, rc); } /* Save texture stage and samplers state. */ /* Number of stages/samplers. */ rc = pHlp->pfnSSMPutU32(pSSM, RT_ELEMENTS(pContext->state.aTextureStates)); AssertRCReturn(rc, rc); /* Number of texture states. */ rc = pHlp->pfnSSMPutU32(pSSM, RT_ELEMENTS(pContext->state.aTextureStates[0])); AssertRCReturn(rc, rc); for (uint32_t iStage = 0; iStage < RT_ELEMENTS(pContext->state.aTextureStates); ++iStage) { for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTextureStates[0]); ++j) { SVGA3dTextureState *pTextureState = &pContext->state.aTextureStates[iStage][j]; pHlp->pfnSSMPutU32(pSSM, pTextureState->stage); pHlp->pfnSSMPutU32(pSSM, pTextureState->name); rc = pHlp->pfnSSMPutU32(pSSM, pTextureState->value); AssertRCReturn(rc, rc); } } /* Occlusion query. */ if (!VMSVGA3DQUERY_EXISTS(&pContext->occlusion)) { pContext->occlusion.enmQueryState = VMSVGA3DQUERYSTATE_NULL; } switch (pContext->occlusion.enmQueryState) { case VMSVGA3DQUERYSTATE_BUILDING: /* Stop collecting data. Fetch partial result. Save result. */ vmsvga3dOcclusionQueryEnd(pState, pContext); RT_FALL_THRU(); case VMSVGA3DQUERYSTATE_ISSUED: /* Fetch result. Save result. */ pContext->occlusion.u32QueryResult = 0; vmsvga3dOcclusionQueryGetData(pState, pContext, &pContext->occlusion.u32QueryResult); RT_FALL_THRU(); case VMSVGA3DQUERYSTATE_SIGNALED: /* Save result. Nothing to do here. */ break; default: AssertFailed(); RT_FALL_THRU(); case VMSVGA3DQUERYSTATE_NULL: pContext->occlusion.enmQueryState = VMSVGA3DQUERYSTATE_NULL; pContext->occlusion.u32QueryResult = 0; break; } rc = pHlp->pfnSSMPutStructEx(pSSM, &pContext->occlusion, sizeof(pContext->occlusion), 0, g_aVMSVGA3DQUERYFields, NULL); AssertRCReturn(rc, rc); } return VINF_SUCCESS; } int vmsvga3dSaveExec(PPDMDEVINS pDevIns, PVGASTATECC pThisCC, PSSMHANDLE pSSM) { PVMSVGA3DSTATE pState = pThisCC->svga.p3dState; AssertReturn(pState, VERR_NO_MEMORY); PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3; int rc; /* Save a copy of the generic 3d state first. */ rc = pHlp->pfnSSMPutStructEx(pSSM, pState, sizeof(*pState), 0, g_aVMSVGA3DSTATEFields, NULL); AssertRCReturn(rc, rc); #ifdef VMSVGA3D_OPENGL /* Save the shared context. */ if (pState->SharedCtx.id == VMSVGA3D_SHARED_CTX_ID) { rc = vmsvga3dSaveContext(pHlp, pThisCC, pSSM, &pState->SharedCtx); AssertRCReturn(rc, rc); } #endif /* Save all active contexts. */ for (uint32_t i = 0; i < pState->cContexts; i++) { rc = vmsvga3dSaveContext(pHlp, pThisCC, pSSM, pState->papContexts[i]); AssertRCReturn(rc, rc); } /* Save all active surfaces. */ for (uint32_t sid = 0; sid < pState->cSurfaces; sid++) { PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid]; /* Save the id first. */ rc = pHlp->pfnSSMPutU32(pSSM, pSurface->id); AssertRCReturn(rc, rc); if (pSurface->id != SVGA3D_INVALID_ID) { /* Save a copy of the surface structure first. */ rc = pHlp->pfnSSMPutStructEx(pSSM, pSurface, sizeof(*pSurface), 0, g_aVMSVGA3DSURFACEFields, NULL); AssertRCReturn(rc, rc); /* Save the mip map level info. */ for (uint32_t face=0; face < pSurface->cFaces; face++) { for (uint32_t i = 0; i < pSurface->cLevels; i++) { uint32_t idx = i + face * pSurface->cLevels; PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[idx]; /* Save a copy of the mip map level struct. */ rc = pHlp->pfnSSMPutStructEx(pSSM, pMipmapLevel, sizeof(*pMipmapLevel), 0, g_aVMSVGA3DMIPMAPLEVELFields, NULL); AssertRCReturn(rc, rc); } } /* Save the mip map level data. */ for (uint32_t face=0; face < pSurface->cFaces; face++) { for (uint32_t i = 0; i < pSurface->cLevels; i++) { uint32_t idx = i + face * pSurface->cLevels; PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[idx]; Log(("Surface sid=%u: save mipmap level %d with %x bytes data.\n", sid, i, pMipmapLevel->cbSurface)); if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface)) { if (pMipmapLevel->fDirty) { /* Data follows */ rc = pHlp->pfnSSMPutBool(pSSM, true); AssertRCReturn(rc, rc); Assert(pMipmapLevel->cbSurface); rc = pHlp->pfnSSMPutMem(pSSM, pMipmapLevel->pSurfaceData, pMipmapLevel->cbSurface); AssertRCReturn(rc, rc); } else { /* No data follows */ rc = pHlp->pfnSSMPutBool(pSSM, false); AssertRCReturn(rc, rc); } } else { #ifdef VMSVGA3D_DIRECT3D void *pData; bool fRenderTargetTexture = false; bool fTexture = false; bool fSkipSave = false; HRESULT hr; Assert(pMipmapLevel->cbSurface); pData = RTMemAllocZ(pMipmapLevel->cbSurface); AssertReturn(pData, VERR_NO_MEMORY); switch (pSurface->enmD3DResType) { case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE: case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE: AssertFailed(); /// @todo fSkipSave = true; break; case VMSVGA3D_D3DRESTYPE_SURFACE: case VMSVGA3D_D3DRESTYPE_TEXTURE: { if (pSurface->surfaceFlags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL) { /** @todo unable to easily fetch depth surface data in d3d 9 */ fSkipSave = true; break; } fTexture = (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE); fRenderTargetTexture = fTexture && (pSurface->surfaceFlags & SVGA3D_SURFACE_HINT_RENDERTARGET); D3DLOCKED_RECT LockedRect; if (fTexture) { if (pSurface->bounce.pTexture) { if ( !pSurface->fDirty && fRenderTargetTexture && i == 0 /* only the first time */) { IDirect3DSurface9 *pSrc, *pDest; /** @todo stricter checks for associated context */ uint32_t cid = pSurface->idAssociatedContext; PVMSVGA3DCONTEXT pContext; rc = vmsvga3dContextFromCid(pState, cid, &pContext); AssertRCReturn(rc, rc); hr = pSurface->bounce.pTexture->GetSurfaceLevel(i, &pDest); AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSaveExec: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR); hr = pSurface->u.pTexture->GetSurfaceLevel(i, &pSrc); AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSaveExec: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR); hr = pContext->pDevice->GetRenderTargetData(pSrc, pDest); AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSaveExec: GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR); pSrc->Release(); pDest->Release(); } hr = pSurface->bounce.pTexture->LockRect(i, /* texture level */ &LockedRect, NULL, D3DLOCK_READONLY); } else hr = pSurface->u.pTexture->LockRect(i, /* texture level */ &LockedRect, NULL, D3DLOCK_READONLY); } else hr = pSurface->u.pSurface->LockRect(&LockedRect, NULL, D3DLOCK_READONLY); AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSaveExec: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR); /* Copy the data one line at a time in case the internal pitch is different. */ for (uint32_t j = 0; j < pMipmapLevel->cBlocksY; ++j) { uint8_t *pu8Dst = (uint8_t *)pData + j * pMipmapLevel->cbSurfacePitch; const uint8_t *pu8Src = (uint8_t *)LockedRect.pBits + j * LockedRect.Pitch; memcpy(pu8Dst, pu8Src, pMipmapLevel->cbSurfacePitch); } if (fTexture) { if (pSurface->bounce.pTexture) { hr = pSurface->bounce.pTexture->UnlockRect(i); AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSaveExec: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR); } else hr = pSurface->u.pTexture->UnlockRect(i); } else hr = pSurface->u.pSurface->UnlockRect(); AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSaveExec: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR); break; } case VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER: case VMSVGA3D_D3DRESTYPE_INDEX_BUFFER: { /* Current type of the buffer. */ const bool fVertex = (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER); uint8_t *pD3DData; if (fVertex) hr = pSurface->u.pVertexBuffer->Lock(0, 0, (void **)&pD3DData, D3DLOCK_READONLY); else hr = pSurface->u.pIndexBuffer->Lock(0, 0, (void **)&pD3DData, D3DLOCK_READONLY); AssertMsg(hr == D3D_OK, ("vmsvga3dSaveExec: Lock %s failed with %x\n", (fVertex) ? "vertex" : "index", hr)); memcpy(pData, pD3DData, pMipmapLevel->cbSurface); if (fVertex) hr = pSurface->u.pVertexBuffer->Unlock(); else hr = pSurface->u.pIndexBuffer->Unlock(); AssertMsg(hr == D3D_OK, ("vmsvga3dSaveExec: Unlock %s failed with %x\n", (fVertex) ? "vertex" : "index", hr)); break; } default: AssertFailed(); break; } if (!fSkipSave) { /* Data follows */ rc = pHlp->pfnSSMPutBool(pSSM, true); AssertRCReturn(rc, rc); /* And write the surface data. */ rc = pHlp->pfnSSMPutMem(pSSM, pData, pMipmapLevel->cbSurface); AssertRCReturn(rc, rc); } else { /* No data follows */ rc = pHlp->pfnSSMPutBool(pSSM, false); AssertRCReturn(rc, rc); } RTMemFree(pData); #elif defined(VMSVGA3D_DX) /** @todo */ #elif defined(VMSVGA3D_OPENGL) void *pData = NULL; PVMSVGA3DCONTEXT pContext = &pState->SharedCtx; VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext); Assert(pMipmapLevel->cbSurface); switch (pSurface->enmOGLResType) { default: AssertFailed(); RT_FALL_THRU(); case VMSVGA3D_OGLRESTYPE_RENDERBUFFER: /** @todo fetch data from the renderbuffer. Not used currently. */ /* No data follows */ rc = pHlp->pfnSSMPutBool(pSSM, false); AssertRCReturn(rc, rc); break; case VMSVGA3D_OGLRESTYPE_TEXTURE: { GLint activeTexture; pData = RTMemAllocZ(pMipmapLevel->cbSurface); AssertReturn(pData, VERR_NO_MEMORY); glGetIntegerv(GL_TEXTURE_BINDING_2D, &activeTexture); VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext); glBindTexture(GL_TEXTURE_2D, pSurface->oglId.texture); VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext); /* Set row length and alignment of the output data. */ VMSVGAPACKPARAMS SavedParams; vmsvga3dOglSetPackParams(pState, pContext, pSurface, &SavedParams); glGetTexImage(GL_TEXTURE_2D, i, pSurface->formatGL, pSurface->typeGL, pData); VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext); vmsvga3dOglRestorePackParams(pState, pContext, pSurface, &SavedParams); /* Data follows */ rc = pHlp->pfnSSMPutBool(pSSM, true); AssertRCReturn(rc, rc); /* And write the surface data. */ rc = pHlp->pfnSSMPutMem(pSSM, pData, pMipmapLevel->cbSurface); AssertRCReturn(rc, rc); /* Restore the old active texture. */ glBindTexture(GL_TEXTURE_2D, activeTexture); VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext); break; } case VMSVGA3D_OGLRESTYPE_BUFFER: { uint8_t *pBufferData; pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pSurface->oglId.buffer); VMSVGA3D_CHECK_LAST_ERROR(pState, pContext); pBufferData = (uint8_t *)pState->ext.glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY); VMSVGA3D_CHECK_LAST_ERROR(pState, pContext); Assert(pBufferData); /* Data follows */ rc = pHlp->pfnSSMPutBool(pSSM, true); AssertRCReturn(rc, rc); /* And write the surface data. */ rc = pHlp->pfnSSMPutMem(pSSM, pBufferData, pMipmapLevel->cbSurface); AssertRCReturn(rc, rc); pState->ext.glUnmapBuffer(GL_ARRAY_BUFFER); VMSVGA3D_CHECK_LAST_ERROR(pState, pContext); pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0); VMSVGA3D_CHECK_LAST_ERROR(pState, pContext); } } if (pData) RTMemFree(pData); #else #error "Unexpected 3d backend" #endif } } } } } return VINF_SUCCESS; } int vmsvga3dSaveShaderConst(PVMSVGA3DCONTEXT pContext, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t val1, uint32_t val2, uint32_t val3, uint32_t val4) { /* Choose a sane upper limit. */ AssertReturn(reg < _32K, VERR_INVALID_PARAMETER); if (type == SVGA3D_SHADERTYPE_VS) { if (pContext->state.cVertexShaderConst <= reg) { pContext->state.paVertexShaderConst = (PVMSVGASHADERCONST)RTMemRealloc(pContext->state.paVertexShaderConst, sizeof(VMSVGASHADERCONST) * (reg + 1)); AssertReturn(pContext->state.paVertexShaderConst, VERR_NO_MEMORY); for (uint32_t i = pContext->state.cVertexShaderConst; i < reg + 1; i++) pContext->state.paVertexShaderConst[i].fValid = false; pContext->state.cVertexShaderConst = reg + 1; } pContext->state.paVertexShaderConst[reg].fValid = true; pContext->state.paVertexShaderConst[reg].ctype = ctype; pContext->state.paVertexShaderConst[reg].value[0] = val1; pContext->state.paVertexShaderConst[reg].value[1] = val2; pContext->state.paVertexShaderConst[reg].value[2] = val3; pContext->state.paVertexShaderConst[reg].value[3] = val4; } else { Assert(type == SVGA3D_SHADERTYPE_PS); if (pContext->state.cPixelShaderConst <= reg) { pContext->state.paPixelShaderConst = (PVMSVGASHADERCONST)RTMemRealloc(pContext->state.paPixelShaderConst, sizeof(VMSVGASHADERCONST) * (reg + 1)); AssertReturn(pContext->state.paPixelShaderConst, VERR_NO_MEMORY); for (uint32_t i = pContext->state.cPixelShaderConst; i < reg + 1; i++) pContext->state.paPixelShaderConst[i].fValid = false; pContext->state.cPixelShaderConst = reg + 1; } pContext->state.paPixelShaderConst[reg].fValid = true; pContext->state.paPixelShaderConst[reg].ctype = ctype; pContext->state.paPixelShaderConst[reg].value[0] = val1; pContext->state.paPixelShaderConst[reg].value[1] = val2; pContext->state.paPixelShaderConst[reg].value[2] = val3; pContext->state.paPixelShaderConst[reg].value[3] = val4; } return VINF_SUCCESS; }