Index: /trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-ogl.cpp
===================================================================
--- /trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-ogl.cpp	(revision 54785)
+++ /trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-ogl.cpp	(revision 54786)
@@ -6116,9 +6116,19 @@
 
         /* Active the right texture unit for subsequent texture state changes. */
-        if (pTextureState[i].stage != currentStage)
-        {
-            pState->ext.glActiveTexture(GL_TEXTURE0 + pTextureState[i].stage);
-            VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
-            currentStage = pTextureState[i].stage;
+        if (pTextureState[i].stage != currentStage || i == 0)
+        {
+            /** @todo Is this the appropriate limit for all kinds of textures?  It is the
+             * size of aSidActiveTexture and for binding/unbinding we cannot exceed it. */
+            if (pTextureState[i].stage < SVGA3D_MAX_TEXTURE_STAGE)
+            {
+                pState->ext.glActiveTexture(GL_TEXTURE0 + pTextureState[i].stage);
+                VMSVGA3D_CHECK_LAST_ERROR(pState, pContext);
+                currentStage = pTextureState[i].stage;
+            }
+            else
+            {
+                AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x\n", i, pTextureState[i].stage, pTextureState[i].name));
+                continue;
+            }
         }
 
@@ -6162,5 +6172,5 @@
             {
                 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x replacing=%x\n",
-                     pTextureState[i].stage, pTextureState[i].value, pContext->aSidActiveTexture[currentStage]));
+                     currentStage, pTextureState[i].value, pContext->aSidActiveTexture[currentStage]));
 
                 pContext->aSidActiveTexture[currentStage] = SVGA3D_INVALID_ID;
@@ -6182,5 +6192,5 @@
 
                 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x (%d,%d) replacing=%x\n",
-                     pTextureState[i].stage, pTextureState[i].value, pSurface->pMipmapLevels[0].size.width,
+                     currentStage, pTextureState[i].value, pSurface->pMipmapLevels[0].size.width,
                      pSurface->pMipmapLevels[0].size.height, pContext->aSidActiveTexture[currentStage]));
 
@@ -6264,5 +6274,4 @@
             else
                 val = vmsvga3dTextureFilter2OGL((SVGA3dTextureFilter)minFilter);
-
             break;
         }
Index: /trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win.cpp
===================================================================
--- /trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win.cpp	(revision 54785)
+++ /trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-win.cpp	(revision 54786)
@@ -4705,11 +4705,21 @@
         D3DTEXTURESTAGESTATETYPE textureType = D3DTSS_FORCE_DWORD;
         D3DSAMPLERSTATETYPE      samplerType = D3DSAMP_FORCE_DWORD;
-
-        Log(("vmsvga3dSetTextureState: cid=%x stage=%d type=%s (%x) val=%x\n", cid, pTextureState[i].stage, vmsvga3dTextureStateToString(pTextureState[i].name), pTextureState[i].name, pTextureState[i].value));
+        uint32_t                 currentStage = pTextureState[i].stage;
+
+        Log(("vmsvga3dSetTextureState: cid=%x stage=%d type=%s (%x) val=%x\n", cid, currentStage, vmsvga3dTextureStateToString(pTextureState[i].name), pTextureState[i].name, pTextureState[i].value));
+
+        /** @todo Is this the appropriate limit for all kinds of textures?  It is the
+         * size of aSidActiveTexture and for binding/unbinding we cannot exceed it. */
+        if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_TEXTURE_STAGE))
+        {
+            AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x\n", i, pTextureState[i].stage, pTextureState[i].name));
+            continue;
+        }
+
         /* Record the texture state for vm state saving. */
-        if (    pTextureState[i].stage < SVGA3D_MAX_TEXTURE_STAGE
+        if (    currentStage < SVGA3D_MAX_TEXTURE_STAGE
             &&  pTextureState[i].name < SVGA3D_TS_MAX)
         {
-            pContext->state.aTextureState[pTextureState[i].stage][pTextureState[i].name] = pTextureState[i];
+            pContext->state.aTextureState[currentStage][pTextureState[i].name] = pTextureState[i];
         }
 
@@ -4799,10 +4809,9 @@
             if (pTextureState[i].value == SVGA3D_INVALID_ID)
             {
-                Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x\n", pTextureState[i].stage, pTextureState[i].value));
-
-                pContext->aSidActiveTexture[pTextureState[i].stage] = SVGA3D_INVALID_ID;
+                Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x\n", currentStage, pTextureState[i].value));
+
+                pContext->aSidActiveTexture[currentStage] = SVGA3D_INVALID_ID;
                 /* Unselect the currently associated texture. */
-                hr = pContext->pDevice->SetTexture(pTextureState[i].stage,
-                                                   NULL);
+                hr = pContext->pDevice->SetTexture(currentStage, NULL);
                 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetTextureState: SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
             }
@@ -4817,5 +4826,5 @@
                 PVMSVGA3DSURFACE pSurface = &pState->paSurface[sid];
 
-                Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x (%d,%d)\n", pTextureState[i].stage, pTextureState[i].value, pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height));
+                Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x (%d,%d)\n", currentStage, pTextureState[i].value, pSurface->pMipmapLevels[0].size.width, pSurface->pMipmapLevels[0].size.height));
 
                 if (!pSurface->u.pTexture)
@@ -4840,15 +4849,13 @@
                     AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
 
-                    hr = pContext->pDevice->SetTexture(pTextureState[i].stage,
-                                                       pSharedSurface->u.pTexture);
+                    hr = pContext->pDevice->SetTexture(currentStage, pSharedSurface->u.pTexture);
                 }
                 else
 #endif
-                    hr = pContext->pDevice->SetTexture(pTextureState[i].stage,
-                                                       pSurface->u.pTexture);
+                    hr = pContext->pDevice->SetTexture(currentStage, pSurface->u.pTexture);
 
                 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetTextureState: SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
 
-                pContext->aSidActiveTexture[pTextureState[i].stage] = sid;
+                pContext->aSidActiveTexture[currentStage] = sid;
             }
             /* Finished; continue with the next one. */
@@ -4931,10 +4938,10 @@
         if (textureType != D3DTSS_FORCE_DWORD)
         {
-            hr = pContext->pDevice->SetTextureStageState(pTextureState[i].stage, textureType, val);
+            hr = pContext->pDevice->SetTextureStageState(currentStage, textureType, val);
         }
         else
         {
             Assert(samplerType != D3DSAMP_FORCE_DWORD);
-            hr = pContext->pDevice->SetSamplerState(pTextureState[i].stage, samplerType, val);
+            hr = pContext->pDevice->SetSamplerState(currentStage, samplerType, val);
         }
 
