Index: trunk/src/VBox/Additions/common/crOpenGL/feedback/feedback.py
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/feedback/feedback.py	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/feedback/feedback.py	(revision 78375)
@@ -94,5 +94,5 @@
 	}
 
-	return crStateRenderMode( mode );
+	return crStateRenderMode(&feedback_spu.StateTracker, mode );
 }
 
@@ -100,10 +100,10 @@
 {
 	if (feedback_spu.render_mode == GL_FEEDBACK)
-		crStateFeedbackBegin( mode );
-	else if (feedback_spu.render_mode == GL_SELECT)
-		crStateSelectBegin( mode );
+		crStateFeedbackBegin(&feedback_spu.StateTracker, mode );
+	else if (feedback_spu.render_mode == GL_SELECT)
+		crStateSelectBegin(&feedback_spu.StateTracker, mode );
 	else
 	{
-		crStateBegin( mode );
+		crStateBegin(&feedback_spu.StateTracker, mode );
 		feedback_spu.super.Begin( mode );
 	}
@@ -113,10 +113,10 @@
 {
 	if (feedback_spu.render_mode == GL_FEEDBACK)
-		crStateFeedbackEnd( );
-	else if (feedback_spu.render_mode == GL_SELECT)
-		crStateSelectEnd( );
+		crStateFeedbackEnd(&feedback_spu.StateTracker);
+	else if (feedback_spu.render_mode == GL_SELECT)
+		crStateSelectEnd(&feedback_spu.StateTracker);
 	else
 	{
-		crStateEnd( );
+		crStateEnd(&feedback_spu.StateTracker);
 		feedback_spu.super.End( );
 	}
@@ -125,10 +125,10 @@
 static void FEEDBACKSPU_APIENTRY feedbackspu_Bitmap ( GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, const GLubyte *bitmap )
 {
-	crStateBitmap( width, height, xorig, yorig, xmove, ymove, bitmap );
-
-	if (feedback_spu.render_mode == GL_FEEDBACK)
-		crStateFeedbackBitmap( width, height, xorig, yorig, xmove, ymove, bitmap );
-	else if (feedback_spu.render_mode == GL_SELECT)
-		crStateSelectBitmap( width, height, xorig, yorig, xmove, ymove, bitmap );
+	crStateBitmap(&feedback_spu.StateTracker, width, height, xorig, yorig, xmove, ymove, bitmap );
+
+	if (feedback_spu.render_mode == GL_FEEDBACK)
+		crStateFeedbackBitmap(&feedback_spu.StateTracker, width, height, xorig, yorig, xmove, ymove, bitmap );
+	else if (feedback_spu.render_mode == GL_SELECT)
+		crStateSelectBitmap(&feedback_spu.StateTracker, width, height, xorig, yorig, xmove, ymove, bitmap );
 	else
 		feedback_spu.super.Bitmap( width, height, xorig, yorig, xmove, ymove, bitmap );
@@ -138,7 +138,7 @@
 {
 	if (feedback_spu.render_mode == GL_FEEDBACK)
-		crStateFeedbackCopyPixels( x, y, width, height, type );
-	else if (feedback_spu.render_mode == GL_SELECT)
-		crStateSelectCopyPixels( x, y, width, height, type );
+		crStateFeedbackCopyPixels(&feedback_spu.StateTracker, x, y, width, height, type );
+	else if (feedback_spu.render_mode == GL_SELECT)
+		crStateSelectCopyPixels(&feedback_spu.StateTracker, x, y, width, height, type );
 	else
 		feedback_spu.super.CopyPixels( x, y, width, height, type );
@@ -148,7 +148,7 @@
 {
 	if (feedback_spu.render_mode == GL_FEEDBACK)
-		crStateFeedbackDrawPixels( width, height, format, type, pixels );
-	else if (feedback_spu.render_mode == GL_SELECT)
-		crStateSelectDrawPixels( width, height, format, type, pixels );
+		crStateFeedbackDrawPixels(&feedback_spu.StateTracker, width, height, format, type, pixels );
+	else if (feedback_spu.render_mode == GL_SELECT)
+		crStateSelectDrawPixels(&feedback_spu.StateTracker, width, height, format, type, pixels );
 	else
 		feedback_spu.super.DrawPixels( width, height, format, type, pixels );
@@ -161,8 +161,8 @@
 	    pname == GL_FEEDBACK_BUFFER_TYPE ||
 	    pname == GL_SELECTION_BUFFER_SIZE)
-		crStateFeedbackGetBooleanv( pname, params );
-	else
-	if (pname == GL_VIEWPORT && feedback_spu.default_viewport)
-		crStateGetBooleanv( pname, params );
+		crStateFeedbackGetBooleanv(&feedback_spu.StateTracker, pname, params );
+	else
+	if (pname == GL_VIEWPORT && feedback_spu.default_viewport)
+		crStateGetBooleanv(&feedback_spu.StateTracker, pname, params );
 	else
 		feedback_spu.super.GetBooleanv( pname, params );
@@ -175,8 +175,8 @@
 	    pname == GL_FEEDBACK_BUFFER_TYPE ||
 	    pname == GL_SELECTION_BUFFER_SIZE)
-		crStateFeedbackGetDoublev( pname, params );
-	else
-	if (pname == GL_VIEWPORT && feedback_spu.default_viewport)
-		crStateGetDoublev( pname, params );
+		crStateFeedbackGetDoublev(&feedback_spu.StateTracker, pname, params );
+	else
+	if (pname == GL_VIEWPORT && feedback_spu.default_viewport)
+		crStateGetDoublev(&feedback_spu.StateTracker, pname, params );
 	else
 		feedback_spu.super.GetDoublev( pname, params );
@@ -189,8 +189,8 @@
 	    pname == GL_FEEDBACK_BUFFER_TYPE ||
 	    pname == GL_SELECTION_BUFFER_SIZE)
-		crStateFeedbackGetFloatv( pname, params );
-	else
-	if (pname == GL_VIEWPORT && feedback_spu.default_viewport)
-		crStateGetFloatv( pname, params );
+		crStateFeedbackGetFloatv(&feedback_spu.StateTracker, pname, params );
+	else
+	if (pname == GL_VIEWPORT && feedback_spu.default_viewport)
+		crStateGetFloatv(&feedback_spu.StateTracker, pname, params );
 	else
 		feedback_spu.super.GetFloatv( pname, params );
@@ -203,8 +203,8 @@
 	    pname == GL_FEEDBACK_BUFFER_TYPE ||
 	    pname == GL_SELECTION_BUFFER_SIZE)
-		crStateFeedbackGetIntegerv( pname, params );
-	else
-	if (pname == GL_VIEWPORT && feedback_spu.default_viewport)
-		crStateGetIntegerv( pname, params );
+		crStateFeedbackGetIntegerv(&feedback_spu.StateTracker, pname, params );
+	else
+	if (pname == GL_VIEWPORT && feedback_spu.default_viewport)
+		crStateGetIntegerv(&feedback_spu.StateTracker, pname, params );
 	else
 		feedback_spu.super.GetIntegerv( pname, params );
Index: trunk/src/VBox/Additions/common/crOpenGL/feedback/feedback_context.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/feedback/feedback_context.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/feedback/feedback_context.c	(revision 78375)
@@ -50,5 +50,5 @@
     }
 
-    feedback_spu.context[slot].clientState = crStateCreateContext(NULL, visual, NULL);
+    feedback_spu.context[slot].clientState = crStateCreateContext(&feedback_spu.StateTracker, NULL, visual, NULL);
     feedback_spu.context[slot].clientCtx = ctx;
 
@@ -77,7 +77,7 @@
         CRASSERT(slot < feedback_spu.numContexts);
 
-        crStateMakeCurrent(feedback_spu.context[slot].clientState);
+        crStateMakeCurrent(&feedback_spu.StateTracker, feedback_spu.context[slot].clientState);
 
-        crStateGetIntegerv(GL_RENDER_MODE, &oldmode);
+        crStateGetIntegerv(&feedback_spu.StateTracker, GL_RENDER_MODE, &oldmode);
 
         if (oldmode!=feedback_spu.render_mode)
@@ -88,5 +88,5 @@
     else
     {
-        crStateMakeCurrent(NULL);
+        crStateMakeCurrent(&feedback_spu.StateTracker, NULL);
     }
 
@@ -107,5 +107,5 @@
         CRASSERT(slot < feedback_spu.numContexts);
 
-        crStateDestroyContext(feedback_spu.context[slot].clientState);
+        crStateDestroyContext(&feedback_spu.StateTracker, feedback_spu.context[slot].clientState);
 
         feedback_spu.context[slot].clientState = NULL;
Index: trunk/src/VBox/Additions/common/crOpenGL/feedback/feedback_state.py
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/feedback/feedback_state.py	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/feedback/feedback_state.py	(revision 78375)
@@ -29,5 +29,8 @@
         print('%s FEEDBACKSPU_APIENTRY feedbackspu_%s(%s)' % (return_type, func_name, apiutil.MakeDeclarationString(params)))
         print('{')
-        print('\tcrState%s(%s);' % (func_name, apiutil.MakeCallString(params)))
+        if len(params) == 0:
+            print('\tcrState%s(&feedback_spu.StateTracker);' % (func_name,))
+        else:
+            print('\tcrState%s(&feedback_spu.StateTracker, %s);' % (func_name, apiutil.MakeCallString(params)))
         print('')
         print('\tfeedback_spu.super.%s(%s);' % (func_name, apiutil.MakeCallString(params)))
Index: trunk/src/VBox/Additions/common/crOpenGL/feedback/feedbackspu.h
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/feedback/feedbackspu.h	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/feedback/feedbackspu.h	(revision 78375)
@@ -43,4 +43,7 @@
 
     CRmutex mutex;
+
+    /** The state tracker state. */
+    CRStateTracker StateTracker;
 } feedbackSPU;
 
Index: trunk/src/VBox/Additions/common/crOpenGL/feedback/feedbackspu_init.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/feedback/feedbackspu_init.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/feedback/feedbackspu_init.c	(revision 78375)
@@ -45,8 +45,8 @@
 
 	/* create/init default state tracker */
-	crStateInit();
+	crStateInit(&feedback_spu.StateTracker);
 
-    feedback_spu.defaultctx = crStateCreateContext(NULL, 0, NULL);
-    crStateSetCurrent(feedback_spu.defaultctx);
+    feedback_spu.defaultctx = crStateCreateContext(&feedback_spu.StateTracker, NULL, 0, NULL);
+    crStateSetCurrent(&feedback_spu.StateTracker, feedback_spu.defaultctx);
 
     feedback_spu.numContexts = 0;
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu.h
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu.h	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu.h	(revision 78375)
@@ -109,4 +109,7 @@
     int numContexts;
     ContextInfo context[CR_MAX_CONTEXTS];
+
+    /** State tracker state. */
+    CRStateTracker    StateTracker;
 } PackSPU;
 
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_beginend.py
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_beginend.py	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_beginend.py	(revision 78375)
@@ -36,5 +36,5 @@
         GLboolean serverArrays = GL_FALSE;
         if (ctx->clientState->extensions.ARB_vertex_buffer_object)
-            serverArrays = crStateUseServerArrays();
+            serverArrays = crStateUseServerArrays(&pack_spu.StateTracker);
         if (serverArrays) {
             CRClientState *clientState = &(ctx->clientState->client);
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_bufferobject.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_bufferobject.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_bufferobject.c	(revision 78375)
@@ -31,5 +31,5 @@
 
     CRASSERT(GL_TRUE == ctx->clientState->bufferobject.retainBufferData);
-    buffer = crStateMapBufferARB(target, access);
+    buffer = crStateMapBufferARB(&pack_spu.StateTracker, target, access);
 
 #ifdef CR_ARB_pixel_buffer_object
@@ -67,5 +67,5 @@
 #endif
 
-    crStateGetBufferSubDataARB(target, offset, size, data);
+    crStateGetBufferSubDataARB(&pack_spu.StateTracker, target, offset, size, data);
 }
 
@@ -86,5 +86,5 @@
 
     CRASSERT(GL_TRUE == ctx->clientState->bufferobject.retainBufferData);
-    crStateUnmapBufferARB( target );
+    crStateUnmapBufferARB(&pack_spu.StateTracker, target );
 
     return GL_TRUE;
@@ -96,5 +96,5 @@
 {
     /*crDebug("packspu_BufferDataARB size:%d", size);*/
-    crStateBufferDataARB(target, size, data, usage);
+    crStateBufferDataARB(&pack_spu.StateTracker,target, size, data, usage);
     crPackBufferDataARB(target, size, data, usage);
 }
@@ -104,5 +104,5 @@
 {
     /*crDebug("packspu_BufferSubDataARB size:%d", size);*/
-    crStateBufferSubDataARB(target, offset, size, data);
+    crStateBufferSubDataARB(&pack_spu.StateTracker,target, offset, size, data);
     crPackBufferSubDataARB(target, offset, size, data);
 }
@@ -112,5 +112,5 @@
 packspu_GetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
 {
-    crStateGetBufferPointervARB( target, pname, params );
+    crStateGetBufferPointervARB(&pack_spu.StateTracker, target, pname, params );
 }
 
@@ -119,5 +119,5 @@
 packspu_GetBufferParameterivARB( GLenum target, GLenum pname, GLint * params )
 {
-    crStateGetBufferParameterivARB( target, pname, params );
+    crStateGetBufferParameterivARB(&pack_spu.StateTracker, target, pname, params );
 }
 
@@ -128,5 +128,5 @@
 packspu_BindBufferARB( GLenum target, GLuint buffer )
 {
-    crStateBindBufferARB(target, buffer);
+    crStateBindBufferARB(&pack_spu.StateTracker, target, buffer);
     crPackBindBufferARB(target, buffer);
 }
@@ -145,10 +145,10 @@
     CRPACKSPU_WRITEBACK_WAIT(thread, writeback);
 
-    crStateRegBuffers(n, buffer);
+    crStateRegBuffers(&pack_spu.StateTracker,n, buffer);
 }
 
 void PACKSPU_APIENTRY packspu_DeleteBuffersARB( GLsizei n, const GLuint * buffer )
 {
-    crStateDeleteBuffersARB( n, buffer );
+    crStateDeleteBuffersARB(&pack_spu.StateTracker, n, buffer );
     crPackDeleteBuffersARB(n, buffer);
 }
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_client.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_client.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_client.c	(revision 78375)
@@ -19,5 +19,5 @@
     }
 #endif
-    crStateFogCoordPointerEXT( type, stride, pointer );
+    crStateFogCoordPointerEXT(&pack_spu.StateTracker, type, stride, pointer );
 }
 
@@ -30,5 +30,5 @@
     }
 #endif
-    crStateColorPointer( size, type, stride, pointer );
+    crStateColorPointer(&pack_spu.StateTracker, size, type, stride, pointer );
 }
 
@@ -41,5 +41,5 @@
     }
 #endif
-    crStateSecondaryColorPointerEXT( size, type, stride, pointer );
+    crStateSecondaryColorPointerEXT(&pack_spu.StateTracker, size, type, stride, pointer );
 }
 
@@ -53,5 +53,5 @@
     }
 #endif
-    crStateVertexPointer( size, type, stride, pointer );
+    crStateVertexPointer(&pack_spu.StateTracker, size, type, stride, pointer );
 }
 
@@ -64,5 +64,5 @@
     }
 #endif
-    crStateTexCoordPointer( size, type, stride, pointer );
+    crStateTexCoordPointer(&pack_spu.StateTracker, size, type, stride, pointer );
 }
 
@@ -75,5 +75,5 @@
     }
 #endif
-    crStateNormalPointer( type, stride, pointer );
+    crStateNormalPointer(&pack_spu.StateTracker, type, stride, pointer );
 }
 
@@ -86,5 +86,5 @@
     }
 #endif
-    crStateEdgeFlagPointer( stride, pointer );
+    crStateEdgeFlagPointer(&pack_spu.StateTracker, stride, pointer );
 }
 
@@ -97,5 +97,5 @@
     }
 #endif
-    crStateVertexAttribPointerARB( index, size, type, normalized, stride, pointer );
+    crStateVertexAttribPointerARB(&pack_spu.StateTracker, index, size, type, normalized, stride, pointer );
 }
 
@@ -108,5 +108,5 @@
     }
 #endif
-    crStateVertexAttribPointerNV( index, size, type, stride, pointer );
+    crStateVertexAttribPointerNV(&pack_spu.StateTracker, index, size, type, stride, pointer );
 }
 
@@ -119,10 +119,10 @@
     }
 #endif
-    crStateIndexPointer(type, stride, pointer);
+    crStateIndexPointer(&pack_spu.StateTracker, type, stride, pointer);
 }
 
 void PACKSPU_APIENTRY packspu_GetPointerv( GLenum pname, GLvoid **params )
 {
-    crStateGetPointerv( pname, params );
+    crStateGetPointerv(&pack_spu.StateTracker, pname, params );
 }
 
@@ -138,5 +138,5 @@
     /*crDebug("packspu_InterleavedArrays");*/
 
-    crStateInterleavedArrays( format, stride, pointer );
+    crStateInterleavedArrays(&pack_spu.StateTracker, format, stride, pointer );
 }
 
@@ -219,5 +219,5 @@
 #ifdef DEBUG
     {
-        CRContext *pCurState = crStateGetCurrent();
+        CRContext *pCurState = crStateGetCurrent(&pack_spu.StateTracker);
 
         Assert(g == pCurState);
@@ -282,5 +282,5 @@
     {
         CRContext *g = pRestoreCtx->pCtx->clientState;
-        CRContext *pCurState = crStateGetCurrent();
+        CRContext *pCurState = crStateGetCurrent(&pack_spu.StateTracker);
 
         Assert(g == pCurState);
@@ -319,5 +319,5 @@
         if (ctx->clientState->extensions.ARB_vertex_buffer_object)
         {
-            serverArrays = crStateUseServerArrays();
+            serverArrays = crStateUseServerArrays(&pack_spu.StateTracker);
             if (ctx->fCheckZerroVertAttr)
                 cZvaValues = crStateNeedDummyZeroVertexArray(thread->currentContext->clientState, &thread->packer->current, aAttrib);
@@ -395,9 +395,9 @@
     {
         GET_CONTEXT(ctx);
-        elementsBuffer = crStateGetCurrent()->bufferobject.elementsBuffer;
+        elementsBuffer = crStateGetCurrent(&pack_spu.StateTracker)->bufferobject.elementsBuffer;
         /*crDebug("DrawElements count=%d, indices=%p", count, indices);*/
         if (ctx->clientState->extensions.ARB_vertex_buffer_object)
         {
-            serverArrays = crStateUseServerArrays();
+            serverArrays = crStateUseServerArrays(&pack_spu.StateTracker);
             if (ctx->fCheckZerroVertAttr)
                 cZvaValues = crStateNeedDummyZeroVertexArray(thread->currentContext->clientState, &thread->packer->current, aAttrib);
@@ -452,5 +452,5 @@
         if ((max-min)<(GLuint)(2*count))
         {
-            crStateLockArraysEXT(min, max-min+1);
+            crStateLockArraysEXT(&pack_spu.StateTracker, min, max-min+1);
 
             serverArrays = crStateUseServerArrays();
@@ -461,5 +461,5 @@
             else
             {
-                crStateUnlockArraysEXT();
+                crStateUnlockArraysEXT(&pack_spu.StateTracker);
             }
         }
@@ -539,5 +539,5 @@
         if (ctx->clientState->extensions.ARB_vertex_buffer_object)
         {
-             serverArrays = crStateUseServerArrays();
+             serverArrays = crStateUseServerArrays(&pack_spu.StateTracker);
              if (ctx->fCheckZerroVertAttr)
                  cZvaValues = crStateNeedDummyZeroVertexArray(thread->currentContext->clientState, &thread->packer->current, aAttrib);
@@ -608,5 +608,5 @@
         if (ctx->clientState->extensions.ARB_vertex_buffer_object)
         {
-             serverArrays = crStateUseServerArrays();
+             serverArrays = crStateUseServerArrays(&pack_spu.StateTracker);
              if (ctx->fCheckZerroVertAttr)
                  cZvaValues = crStateNeedDummyZeroVertexArray(thread->currentContext->clientState, &thread->packer->current, aAttrib);
@@ -617,6 +617,6 @@
     if (!serverArrays && !ctx->clientState->client.array.locked && (count>3))
     {
-        crStateLockArraysEXT(first, count);
-        serverArrays = crStateUseServerArrays();
+        crStateLockArraysEXT(&pack_spu.StateTracker, first, count);
+        serverArrays = crStateUseServerArrays(&pack_spu.StateTracker);
         if (serverArrays)
         {
@@ -625,5 +625,5 @@
         else
         {
-            crStateUnlockArraysEXT();
+            crStateUnlockArraysEXT(&pack_spu.StateTracker);
         }
     }
@@ -714,5 +714,5 @@
 void PACKSPU_APIENTRY packspu_EnableClientState( GLenum array )
 {
-    crStateEnableClientState(array);
+    crStateEnableClientState(&pack_spu.StateTracker, array);
     crPackEnableClientState(array);
 }
@@ -720,5 +720,5 @@
 void PACKSPU_APIENTRY packspu_DisableClientState( GLenum array )
 {
-    crStateDisableClientState(array);
+    crStateDisableClientState(&pack_spu.StateTracker, array);
     crPackDisableClientState(array);
 }
@@ -726,5 +726,5 @@
 void PACKSPU_APIENTRY packspu_ClientActiveTextureARB( GLenum texUnit )
 {
-    crStateClientActiveTextureARB(texUnit);
+    crStateClientActiveTextureARB(&pack_spu.StateTracker, texUnit);
     crPackClientActiveTextureARB(texUnit);
 }
@@ -732,5 +732,5 @@
 void PACKSPU_APIENTRY packspu_EnableVertexAttribArrayARB(GLuint index)
 {
-    crStateEnableVertexAttribArrayARB(index);
+    crStateEnableVertexAttribArrayARB(&pack_spu.StateTracker, index);
     crPackEnableVertexAttribArrayARB(index);
 }
@@ -739,5 +739,5 @@
 void PACKSPU_APIENTRY packspu_DisableVertexAttribArrayARB(GLuint index)
 {
-    crStateDisableVertexAttribArrayARB(index);
+    crStateDisableVertexAttribArrayARB(&pack_spu.StateTracker, index);
     crPackDisableVertexAttribArrayARB(index);
 }
@@ -747,5 +747,5 @@
     if (cap!=GL_LIGHT_MODEL_TWO_SIDE)
     {
-        crStateEnable(cap);
+        crStateEnable(&pack_spu.StateTracker, cap);
         crPackEnable(cap);
     }
@@ -758,5 +758,5 @@
             g_glmts1_warn=1;
         }
-        crStateLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
+        crStateLightModeli(&pack_spu.StateTracker, GL_LIGHT_MODEL_TWO_SIDE, 1);
         crPackLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
     }
@@ -768,5 +768,5 @@
     if (cap!=GL_LIGHT_MODEL_TWO_SIDE)
     {
-        crStateDisable(cap);
+        crStateDisable(&pack_spu.StateTracker, cap);
         crPackDisable(cap);
     }
@@ -779,5 +779,5 @@
             g_glmts0_warn=1;
         }
-        crStateLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
+        crStateLightModeli(&pack_spu.StateTracker, GL_LIGHT_MODEL_TWO_SIDE, 0);
         crPackLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
     }
@@ -786,5 +786,5 @@
 GLboolean PACKSPU_APIENTRY packspu_IsEnabled(GLenum cap)
 {
-    GLboolean res = crStateIsEnabled(cap);
+    GLboolean res = crStateIsEnabled(&pack_spu.StateTracker, cap);
 #ifdef DEBUG
     {
@@ -804,5 +804,5 @@
 void PACKSPU_APIENTRY packspu_PushClientAttrib( GLbitfield mask )
 {
-    crStatePushClientAttrib(mask);
+    crStatePushClientAttrib(&pack_spu.StateTracker, mask);
     crPackPushClientAttrib(mask);
 }
@@ -810,5 +810,5 @@
 void PACKSPU_APIENTRY packspu_PopClientAttrib( void )
 {
-    crStatePopClientAttrib();
+    crStatePopClientAttrib(&pack_spu.StateTracker);
     crPackPopClientAttrib();
 }
@@ -818,5 +818,5 @@
     if (first>=0 && count>0)
     {
-        crStateLockArraysEXT(first, count);
+        crStateLockArraysEXT(&pack_spu.StateTracker, first, count);
         /*Note: this is a workaround for quake3 based apps.
           It's modifying vertex data between glLockArraysEXT and glDrawElements calls,
@@ -838,4 +838,4 @@
     }
 
-    crStateUnlockArraysEXT();
-}
+    crStateUnlockArraysEXT(&pack_spu.StateTracker);
+}
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_context.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_context.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_context.c	(revision 78375)
@@ -394,5 +394,5 @@
     /* Fill in the new context info */
     /* XXX fix-up sharedCtx param here */
-    pack_spu.context[slot].clientState = crStateCreateContext(NULL, visual, NULL);
+    pack_spu.context[slot].clientState = crStateCreateContext(&pack_spu.StateTracker, NULL, visual, NULL);
     pack_spu.context[slot].clientState->bufferobject.retainBufferData = GL_TRUE;
     pack_spu.context[slot].serverCtx = serverCtx;
@@ -442,5 +442,5 @@
 
         crPackDestroyContext( context->serverCtx );
-        crStateDestroyContext( context->clientState );
+        crStateDestroyContext(&pack_spu.StateTracker, context->clientState );
 
         context->clientState = NULL;
@@ -463,5 +463,5 @@
             crPackSetContext(NULL);
         }
-        crStateMakeCurrent( NULL );
+        crStateMakeCurrent(&pack_spu.StateTracker, NULL);
     }
     else
@@ -540,10 +540,10 @@
         }
 
-        crStateMakeCurrent( newCtx->clientState );
+        crStateMakeCurrent(&pack_spu.StateTracker, newCtx->clientState );
         //crStateSetCurrentPointers(newCtx->clientState, &thread->packer->current);
         serverCtx = pack_spu.context[slot].serverCtx;
     }
     else {
-        crStateMakeCurrent( NULL );
+        crStateMakeCurrent(&pack_spu.StateTracker, NULL );
         if (CRPACKSPU_IS_WDDM_CRHGSMI())
         {
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_framebuffer.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_framebuffer.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_framebuffer.c	(revision 78375)
@@ -25,5 +25,5 @@
 packspu_FramebufferTexture1DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
 {
-    crStateFramebufferTexture1DEXT(target, attachment, textarget, texture, level);
+    crStateFramebufferTexture1DEXT(&pack_spu.StateTracker, target, attachment, textarget, texture, level);
     crPackFramebufferTexture1DEXT(target, attachment, textarget, texture, level);
 }
@@ -32,5 +32,5 @@
 packspu_FramebufferTexture2DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
 {
-    crStateFramebufferTexture2DEXT(target, attachment, textarget, texture, level);
+    crStateFramebufferTexture2DEXT(&pack_spu.StateTracker, target, attachment, textarget, texture, level);
     crPackFramebufferTexture2DEXT(target, attachment, textarget, texture, level);
 }
@@ -39,5 +39,5 @@
 packspu_FramebufferTexture3DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset)
 {
-    crStateFramebufferTexture3DEXT(target, attachment, textarget, texture, level, zoffset);
+    crStateFramebufferTexture3DEXT(&pack_spu.StateTracker, target, attachment, textarget, texture, level, zoffset);
     crPackFramebufferTexture3DEXT(target, attachment, textarget, texture, level, zoffset);
 }
@@ -46,5 +46,5 @@
 packspu_BindFramebufferEXT(GLenum target, GLuint framebuffer)
 {
-        crStateBindFramebufferEXT(target, framebuffer);
+    crStateBindFramebufferEXT(&pack_spu.StateTracker, target, framebuffer);
     crPackBindFramebufferEXT(target, framebuffer);
 }
@@ -53,5 +53,5 @@
 packspu_DeleteFramebuffersEXT(GLsizei n, const GLuint * framebuffers)
 {
-        crStateDeleteFramebuffersEXT(n, framebuffers);
+    crStateDeleteFramebuffersEXT(&pack_spu.StateTracker, n, framebuffers);
     crPackDeleteFramebuffersEXT(n, framebuffers);
 }
@@ -60,5 +60,5 @@
 packspu_DeleteRenderbuffersEXT(GLsizei n, const GLuint * renderbuffers)
 {
-        crStateDeleteRenderbuffersEXT(n, renderbuffers);
+    crStateDeleteRenderbuffersEXT(&pack_spu.StateTracker, n, renderbuffers);
     crPackDeleteRenderbuffersEXT(n, renderbuffers);
 }
@@ -67,5 +67,5 @@
 packspu_FramebufferRenderbufferEXT(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)
 {
-        crStateFramebufferRenderbufferEXT(target, attachment, renderbuffertarget, renderbuffer);
+    crStateFramebufferRenderbufferEXT(&pack_spu.StateTracker, target, attachment, renderbuffertarget, renderbuffer);
     crPackFramebufferRenderbufferEXT(target, attachment, renderbuffertarget, renderbuffer);
 }
@@ -74,5 +74,5 @@
 packspu_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
 {
-    crStateBindRenderbufferEXT(target, renderbuffer);
+    crStateBindRenderbufferEXT(&pack_spu.StateTracker, target, renderbuffer);
     crPackBindRenderbufferEXT(target, renderbuffer);
 }
@@ -81,7 +81,7 @@
 packspu_CheckFramebufferStatusEXT(GLenum target)
 {
-        GET_THREAD(thread);
-        int writeback = 1;
-    GLenum status = crStateCheckFramebufferStatusEXT(target);
+    GET_THREAD(thread);
+    int writeback = 1;
+    GLenum status = crStateCheckFramebufferStatusEXT(&pack_spu.StateTracker, target);
 
     if (status!=GL_FRAMEBUFFER_UNDEFINED)
@@ -92,8 +92,8 @@
     crPackCheckFramebufferStatusEXT(target, &status, &writeback);
 
-        packspuFlush((void *) thread);
+    packspuFlush((void *) thread);
     CRPACKSPU_WRITEBACK_WAIT(thread, writeback);
 
-    crStateSetFramebufferStatus(target, status);
+    crStateSetFramebufferStatus(&pack_spu.StateTracker, target, status);
     return status;
 }
@@ -112,5 +112,5 @@
     CRPACKSPU_WRITEBACK_WAIT(thread, writeback);
 
-    crStateRegFramebuffers(n, framebuffers);
+    crStateRegFramebuffers(&pack_spu.StateTracker, n, framebuffers);
 }
 
@@ -128,4 +128,4 @@
     CRPACKSPU_WRITEBACK_WAIT(thread, writeback);
 
-    crStateRegRenderbuffers(n, renderbuffers);
+    crStateRegRenderbuffers(&pack_spu.StateTracker, n, renderbuffers);
 }
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_get.py
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_get.py	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_get.py	(revision 78375)
@@ -118,5 +118,5 @@
                 %s localparams;
                 localparams = (%s) crAlloc(__numValues(pname) * sizeof(*localparams));
-                crState%s(pname, localparams);
+                crState%s(&pack_spu.StateTracker, pname, localparams);
                 crPack%s(%s, &writeback);
                 packspuFlush( (void *) thread );
@@ -136,5 +136,5 @@
 #endif
             {
-                crState%s(pname, params);
+                crState%s(&pack_spu.StateTracker, pname, params);
                 return;
             }
@@ -150,5 +150,5 @@
         %s localparams;
         localparams = (%s) crAlloc(__numValues(pname) * sizeof(*localparams));
-        crState%s(index, pname, localparams);
+        crState%s(&pack_spu.StateTracker, index, pname, localparams);
         crPack%s(index, %s, &writeback);
         packspuFlush( (void *) thread );
@@ -164,5 +164,5 @@
         crFree(localparams);
 #else
-        crState%s(pname, params);
+        crState%s(&pack_spu.StateTracker, pname, params);
 #endif
         return;
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_getstring.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_getstring.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_getstring.c	(revision 78375)
@@ -149,5 +149,5 @@
 #endif
             {
-                return crStateGetString(name);
+                return crStateGetString(&pack_spu.StateTracker, name);
             }
         case GL_RENDERER:
@@ -161,5 +161,5 @@
 #endif
             {
-                return crStateGetString(name);
+                return crStateGetString(&pack_spu.StateTracker, name);
             }
 
@@ -188,5 +188,5 @@
 #endif
         default:
-            return crStateGetString(name);
+            return crStateGetString(&pack_spu.StateTracker, name);
     }
 }
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_glsl.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_glsl.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_glsl.c	(revision 78375)
@@ -36,5 +36,5 @@
     packspuFlush((void *) thread);
     CRPACKSPU_WRITEBACK_WAIT(thread, writeback);
-    crStateCreateProgram(return_val);
+    crStateCreateProgram(&pack_spu.StateTracker, return_val);
 
     return return_val;
@@ -59,5 +59,5 @@
 GLint PACKSPU_APIENTRY packspu_GetUniformLocation(GLuint program, const char * name)
 {
-    if (!crStateIsProgramUniformsCached(program))
+    if (!crStateIsProgramUniformsCached(&pack_spu.StateTracker, program))
     {
         GET_THREAD(thread);
@@ -82,7 +82,7 @@
         CRPACKSPU_WRITEBACK_WAIT(thread, writeback);
 
-        crStateGLSLProgramCacheUniforms(program, pData[0], &pData[1]);
-
-        CRASSERT(crStateIsProgramUniformsCached(program));
+        crStateGLSLProgramCacheUniforms(&pack_spu.StateTracker, program, pData[0], &pData[1]);
+
+        CRASSERT(crStateIsProgramUniformsCached(&pack_spu.StateTracker, program));
 
         crFree(pData);
@@ -90,5 +90,5 @@
 
     /*crDebug("packspu_GetUniformLocation(%d, %s)=%i", program, name, crStateGetUniformLocation(program, name));*/
-    return crStateGetUniformLocation(program, name);
+    return crStateGetUniformLocation(&pack_spu.StateTracker, program, name);
 }
 
@@ -114,5 +114,5 @@
         return packspu_GetAttribLocationUnchached(program, name);
 
-    if (!crStateIsProgramAttribsCached(program))
+    if (!crStateIsProgramAttribsCached(&pack_spu.StateTracker, program))
     {
         GET_THREAD(thread);
@@ -137,7 +137,7 @@
         CRPACKSPU_WRITEBACK_WAIT(thread, writeback);
 
-        crStateGLSLProgramCacheAttribs(program, pData[0], &pData[1]);
-
-        CRASSERT(crStateIsProgramAttribsCached(program));
+        crStateGLSLProgramCacheAttribs(&pack_spu.StateTracker, program, pData[0], &pData[1]);
+
+        CRASSERT(crStateIsProgramAttribsCached(&pack_spu.StateTracker, program));
 
         crFree(pData);
@@ -145,5 +145,5 @@
 
     /*crDebug("packspu_GetAttribLocation(%d, %s)=%i", program, name, crStateGetAttribLocation(program, name));*/
-    return crStateGetAttribLocation(program, name);
+    return crStateGetAttribLocation(&pack_spu.StateTracker, program, name);
 }
 
@@ -168,5 +168,5 @@
 void PACKSPU_APIENTRY packspu_DeleteProgram(GLuint program)
 {
-    crStateDeleteProgram(program);
+    crStateDeleteProgram(&pack_spu.StateTracker, program);
     crPackDeleteProgram(program);
 }
@@ -174,5 +174,5 @@
 void PACK_APIENTRY packspu_DeleteObjectARB(VBoxGLhandleARB obj)
 {
-    GLuint hwid = crStateGetProgramHWID(obj);
+    GLuint hwid = crStateGetProgramHWID(&pack_spu.StateTracker, obj);
 
     CRASSERT(obj);
@@ -182,5 +182,5 @@
     if (hwid)
     {
-        crStateDeleteProgram(obj);
+        crStateDeleteProgram(&pack_spu.StateTracker, obj);
     }
 
@@ -206,5 +206,5 @@
 #endif
 
-    crStateLinkProgram(program);
+    crStateLinkProgram(&pack_spu.StateTracker, program);
     crPackLinkProgram(program);
 
@@ -214,5 +214,5 @@
     if (!linkStatus)
     {
-        CRContext *ctx = crStateGetCurrent();
+        CRContext *ctx = crStateGetCurrent(&pack_spu.StateTracker);
         packspu_RecCheckInitRec();
         crRecDumpProgram(&pack_spu.Recorder, ctx, program, program);
@@ -235,5 +235,5 @@
     if (!compileStatus)
     {
-        CRContext *ctx = crStateGetCurrent();
+        CRContext *ctx = crStateGetCurrent(&pack_spu.StateTracker);
         packspu_RecCheckInitRec();
         crRecDumpShader(&pack_spu.Recorder, ctx, shader, shader);
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_init.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_init.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_init.c	(revision 78375)
@@ -7,5 +7,4 @@
 #include "cr_mem.h"
 #include "cr_spu.h"
-#include "cr_glstate.h"
 #include "packspu.h"
 #include "cr_packfunctions.h"
@@ -21,4 +20,5 @@
 
 PackSPU pack_spu;
+DECLHIDDEN(PCRStateTracker) g_pStateTracker;
 
 CRtsd _PackTSD;
@@ -93,5 +93,6 @@
 
     packspuCreateFunctions();
-    crStateInit();
+    crStateInit(&pack_spu.StateTracker);
+    g_pStateTracker = &pack_spu.StateTracker;
 
     return &pack_functions;
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_misc.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_misc.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_misc.c	(revision 78375)
@@ -98,5 +98,5 @@
     GLint buffer;
 
-    crStateGetIntegerv(GL_DRAW_BUFFER, &buffer);
+    crStateGetIntegerv(&pack_spu.StateTracker, GL_DRAW_BUFFER, &buffer);
     /*Usually buffer==GL_BACK, so put this extra check to simplify boolean eval on runtime*/
     return  (buffer != GL_BACK)
@@ -116,5 +116,5 @@
     hadtoflush = packspuSyncOnFlushes();
 
-    crStateDrawBuffer(mode);
+    crStateDrawBuffer(&pack_spu.StateTracker, mode);
     crPackDrawBuffer(mode);
 
@@ -197,5 +197,5 @@
 void PACKSPU_APIENTRY packspu_NewList(GLuint list, GLenum mode)
 {
-    crStateNewList(list, mode);
+    crStateNewList(&pack_spu.StateTracker, list, mode);
     crPackNewList(list, mode);
 }
@@ -203,5 +203,5 @@
 void PACKSPU_APIENTRY packspu_EndList()
 {
-    crStateEndList();
+    crStateEndList(&pack_spu.StateTracker);
     crPackEndList();
 }
@@ -366,5 +366,5 @@
     crPackGetPolygonStipple( mask, &writeback );
 #ifdef CR_ARB_pixel_buffer_object
-    if (!crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (!crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
 #endif
     {
@@ -381,5 +381,5 @@
     crPackGetPixelMapfv( map, values, &writeback );
 #ifdef CR_ARB_pixel_buffer_object
-    if (!crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (!crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
 #endif
     {
@@ -397,5 +397,5 @@
 
 #ifdef CR_ARB_pixel_buffer_object
-    if (!crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (!crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
 #endif
     {
@@ -412,5 +412,5 @@
     crPackGetPixelMapusv( map, values, &writeback );
 #ifdef CR_ARB_pixel_buffer_object
-    if (!crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (!crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
 #endif
     {
@@ -449,10 +449,10 @@
             return;
         case GL_SHARE_CONTEXT_RESOURCES_CR:
-            crStateShareContext(value);
+            crStateShareContext(&pack_spu.StateTracker, value);
             break;
         case GL_RCUSAGE_TEXTURE_SET_CR:
         {
             Assert(value);
-            crStateSetTextureUsed(value, GL_TRUE);
+            crStateSetTextureUsed(&pack_spu.StateTracker, value, GL_TRUE);
             break;
         }
@@ -462,10 +462,10 @@
 #ifdef DEBUG
             {
-                CRContext *pCurState = crStateGetCurrent();
+                CRContext *pCurState = crStateGetCurrent(&pack_spu.StateTracker);
                 CRTextureObj *tobj = (CRTextureObj*)crHashtableSearch(pCurState->shared->textureTable, value);
                 Assert(tobj);
             }
 #endif
-            crStateSetTextureUsed(value, GL_FALSE);
+            crStateSetTextureUsed(&pack_spu.StateTracker, value, GL_FALSE);
             break;
         }
@@ -481,5 +481,5 @@
     int writeback = 1;
     GLenum return_val = (GLenum) 0;
-    CRContext *pCurState = crStateGetCurrent();
+    CRContext *pCurState = crStateGetCurrent(&pack_spu.StateTracker);
     NOREF(pCurState); /* it's unused, but I don't know about side effects.. */
 
@@ -620,5 +620,5 @@
     crSetTSD(&_PackTSD, NULL);
 
-    crStateVBoxAttachThread();
+    crStateVBoxAttachThread(&pack_spu.StateTracker);
 }
 
@@ -701,5 +701,5 @@
     }
 
-    crStateVBoxDetachThread();
+    crStateVBoxDetachThread(&pack_spu.StateTracker);
 }
 
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_pixel.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_pixel.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_pixel.c	(revision 78375)
@@ -269,10 +269,10 @@
      * image in a canonical layout (see util/pixel.c).
      */
-    crStatePixelStoref( pname, param );
+    crStatePixelStoref(&pack_spu.StateTracker, pname, param );
 }
 
 void PACKSPU_APIENTRY packspu_PixelStorei( GLenum pname, GLint param )
 {
-    crStatePixelStorei( pname, param );
+    crStatePixelStorei(&pack_spu.StateTracker, pname, param );
 }
 
@@ -283,5 +283,5 @@
     CRClientState *clientState = &(ctx->clientState->client);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -290,5 +290,5 @@
     crPackDrawPixels( width, height, format, type, pixels, &(clientState->unpack) );
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -303,5 +303,5 @@
     int writeback;
 
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         packspu_ApplyPackState();
@@ -310,5 +310,5 @@
     crPackReadPixels(x, y, width, height, format, type, pixels, &(clientState->pack), &writeback);
 
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         packspu_RestorePackState();
@@ -316,5 +316,5 @@
 
 #ifdef CR_ARB_pixel_buffer_object
-    if (!crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (!crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
 #endif
     {
@@ -340,5 +340,5 @@
     CRClientState *clientState = &(ctx->clientState->client);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -347,5 +347,5 @@
     crPackBitmap(width, height, xorig, yorig, xmove, ymove, bitmap, &(clientState->unpack));
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -360,5 +360,5 @@
     if (!packspu_CheckTexImageParams(internalformat, format, type))
     {
-        if (pixels || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+        if (pixels || crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
         {
             crWarning("packspu_TexImage1D invalid internalFormat(%x)/format(%x)/type(%x)", internalformat, format, type);
@@ -370,5 +370,5 @@
     }
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -376,5 +376,5 @@
 
     crPackTexImage1D( target, level, internalformat, width, border, format, type, pixels, &(clientState->unpack) );
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -389,5 +389,5 @@
     if (!packspu_CheckTexImageParams(internalformat, format, type))
     {
-        if (pixels || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+        if (pixels || crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
         {
             crWarning("packspu_TexImage2D invalid internalFormat(%x)/format(%x)/type(%x)", internalformat, format, type);
@@ -399,5 +399,5 @@
     }
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -405,5 +405,5 @@
 
     crPackTexImage2D( target, level, internalformat, width, height, border, format, type, pixels, &(clientState->unpack) );
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -417,5 +417,5 @@
     CRClientState *clientState = &(ctx->clientState->client);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -423,5 +423,5 @@
 
     crPackTexImage3DEXT( target, level, internalformat, width, height, depth, border, format, type, pixels, &(clientState->unpack) );
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -436,5 +436,5 @@
     CRClientState *clientState = &(ctx->clientState->client);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -442,5 +442,5 @@
 
     crPackTexImage3D( target, level, internalformat, width, height, depth, border, format, type, pixels, &(clientState->unpack) );
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -460,5 +460,5 @@
     }
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -466,5 +466,5 @@
 
     crPackTexSubImage1D( target, level, xoffset, width, format, type, pixels, &(clientState->unpack) );
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -483,5 +483,5 @@
     }
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -489,5 +489,5 @@
 
     crPackTexSubImage2D( target, level, xoffset, yoffset, width, height, format, type, pixels, &(clientState->unpack) );
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -501,5 +501,5 @@
     CRClientState *clientState = &(ctx->clientState->client);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -507,5 +507,5 @@
 
     crPackTexSubImage3D( target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels, &(clientState->unpack) );
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -533,5 +533,5 @@
      */
 
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         packspu_ApplyPackState();
@@ -539,5 +539,5 @@
 
     crPackGetTexImage( target, level, format, type, pixels, &(clientState->pack), &writeback );
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         packspu_RestorePackState();
@@ -545,5 +545,5 @@
 
 #ifdef CR_ARB_pixel_buffer_object
-    if (!crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (!crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
 #endif
     {
@@ -558,5 +558,5 @@
     int writeback = 1;
 
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         packspu_ApplyPackState();
@@ -564,5 +564,5 @@
 
     crPackGetCompressedTexImageARB( target, level, img, &writeback );
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         packspu_RestorePackState();
@@ -570,5 +570,5 @@
 
 #ifdef CR_ARB_pixel_buffer_object
-    if (!crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (!crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
 #endif
     {
@@ -582,5 +582,5 @@
                                 GLint border, GLsizei imagesize, const GLvoid *data)
 {
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -589,5 +589,5 @@
     crPackCompressedTexImage1DARB(target, level, internalformat, width, border, imagesize, data);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -599,5 +599,5 @@
                                 GLsizei height, GLint border, GLsizei imagesize, const GLvoid *data)
 {
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -606,5 +606,5 @@
     crPackCompressedTexImage2DARB(target, level, internalformat, width, height, border, imagesize, data);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -616,5 +616,5 @@
                                 GLsizei height, GLsizei depth, GLint border, GLsizei imagesize, const GLvoid *data)
 {
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -623,5 +623,5 @@
     crPackCompressedTexImage3DARB(target, level, internalformat, width, height, depth, border, imagesize, data);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -633,5 +633,5 @@
                                    GLenum format, GLsizei imagesize, const GLvoid *data)
 {
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -640,5 +640,5 @@
     crPackCompressedTexSubImage1DARB(target, level, xoffset, width, format, imagesize, data);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -650,5 +650,5 @@
                                    GLsizei width, GLsizei height, GLenum format, GLsizei imagesize, const GLvoid *data)
 {
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -657,5 +657,5 @@
     crPackCompressedTexSubImage2DARB(target, level, xoffset, yoffset, width, height, format, imagesize, data);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_RestoreUnpackState();
@@ -668,5 +668,5 @@
                                    GLsizei imagesize, const GLvoid *data)
 {
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         packspu_ApplyUnpackState();
@@ -675,7 +675,7 @@
     crPackCompressedTexSubImage3DARB(target, level, xoffset, yoffset, zoffset, width, height, depth, format, imagesize, data);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
-    {
-        packspu_RestoreUnpackState();
-    }
-}
+    if (crStateIsBufferBound(&pack_spu.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
+    {
+        packspu_RestoreUnpackState();
+    }
+}
Index: trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_texture.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_texture.c	(revision 78374)
+++ trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_texture.c	(revision 78375)
@@ -24,5 +24,5 @@
 void PACKSPU_APIENTRY packspu_ActiveTextureARB(GLenum texture)
 {
-    crStateActiveTextureARB(texture);
+    crStateActiveTextureARB(&pack_spu.StateTracker, texture);
     crPackActiveTextureARB(texture);
 }
@@ -30,5 +30,5 @@
 void PACKSPU_APIENTRY packspu_BindTexture(GLenum target, GLuint texture)
 {
-    crStateBindTexture(target, texture);
+    crStateBindTexture(&pack_spu.StateTracker, target, texture);
     crPackBindTexture(target, texture);
 }
@@ -36,5 +36,5 @@
 void PACKSPU_APIENTRY packspu_DeleteTextures(GLsizei n, const GLuint * textures)
 {
-    crStateDeleteTextures(n, textures);
+    crStateDeleteTextures(&pack_spu.StateTracker, n, textures);
     crPackDeleteTextures(n, textures);
 }
@@ -52,4 +52,4 @@
     packspuFlush( (void *) thread );
     CRPACKSPU_WRITEBACK_WAIT(thread, writeback);
-    crStateRegTextures(n, textures);
+    crStateRegTextures(&pack_spu.StateTracker, n, textures);
 }
Index: trunk/src/VBox/GuestHost/OpenGL/include/cr_glstate.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/cr_glstate.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/cr_glstate.h	(revision 78375)
@@ -192,4 +192,7 @@
 #endif
 
+    /** The state tracker the context is attached to. */
+    PCRStateTracker    pStateTracker;
+
     /** For buffering vertices for selection/feedback */
     /*@{*/
@@ -202,16 +205,34 @@
 
 
-DECLEXPORT(void) crStateInit(void);
-DECLEXPORT(void) crStateDestroy(void);
-DECLEXPORT(void) crStateVBoxDetachThread(void);
-DECLEXPORT(void) crStateVBoxAttachThread(void);
-DECLEXPORT(CRContext *) crStateCreateContext(const CRLimitsState *limits, GLint visBits, CRContext *share);
-DECLEXPORT(CRContext *) crStateCreateContextEx(const CRLimitsState *limits, GLint visBits, CRContext *share, GLint presetID);
-DECLEXPORT(void) crStateMakeCurrent(CRContext *ctx);
-DECLEXPORT(void) crStateSetCurrent(CRContext *ctx);
-DECLEXPORT(void) crStateCleanupCurrent(void);
-DECLEXPORT(CRContext *) crStateGetCurrent(void);
-DECLEXPORT(void) crStateDestroyContext(CRContext *ctx);
-DECLEXPORT(GLboolean) crStateEnableDiffOnMakeCurrent(GLboolean fEnable);
+/**
+ * CR state tracker.
+ */
+typedef struct CRStateTracker
+{
+    bool             fContextTLSInit;
+    CRtsd            contextTSD;
+    CRStateBits      *pCurrentBits;
+    CRContext        *apAvailableContexts[CR_MAX_CONTEXTS];
+    uint32_t         cContexts;
+    CRSharedState    *pSharedState;
+    CRContext        *pDefaultContext;
+    GLboolean        fVBoxEnableDiffOnMakeCurrent;
+    SPUDispatchTable diff_api;
+} CRStateTracker;
+/** Pointer to a state tracker state. */
+typedef CRStateTracker *PCRStateTracker;
+
+DECLEXPORT(void) crStateInit(PCRStateTracker pState);
+DECLEXPORT(void) crStateDestroy(PCRStateTracker pState);
+DECLEXPORT(void) crStateVBoxDetachThread(PCRStateTracker pState);
+DECLEXPORT(void) crStateVBoxAttachThread(PCRStateTracker pState);
+DECLEXPORT(CRContext *) crStateCreateContext(PCRStateTracker pState, const CRLimitsState *limits, GLint visBits, CRContext *share);
+DECLEXPORT(CRContext *) crStateCreateContextEx(PCRStateTracker pState, const CRLimitsState *limits, GLint visBits, CRContext *share, GLint presetID);
+DECLEXPORT(void) crStateMakeCurrent(PCRStateTracker pState, CRContext *ctx);
+DECLEXPORT(void) crStateSetCurrent(PCRStateTracker pState, CRContext *ctx);
+DECLEXPORT(void) crStateCleanupCurrent(PCRStateTracker pState);
+DECLEXPORT(CRContext *) crStateGetCurrent(PCRStateTracker pState);
+DECLEXPORT(void) crStateDestroyContext(PCRStateTracker pState, CRContext *ctx);
+DECLEXPORT(GLboolean) crStateEnableDiffOnMakeCurrent(PCRStateTracker pState, GLboolean fEnable);
 
 void crStateSwitchPrepare(CRContext *toCtx, CRContext *fromCtx, GLuint idDrawFBO, GLuint idReadFBO);
@@ -219,8 +240,8 @@
 
 void crStateSyncHWErrorState(CRContext *ctx);
-GLenum crStateCleanHWErrorState(void);
-
-#define CR_STATE_CLEAN_HW_ERR_WARN(_s) do {\
-            GLenum _err = crStateCleanHWErrorState(); \
+GLenum crStateCleanHWErrorState(PCRStateTracker pState);
+
+#define CR_STATE_CLEAN_HW_ERR_WARN(a_pState, _s) do {\
+            GLenum _err = crStateCleanHWErrorState((a_pState)); \
             if (_err != GL_NO_ERROR) { \
                 static int _cErrPrints = 0; \
@@ -232,18 +253,18 @@
         } while (0)
 
-DECLEXPORT(void) crStateFlushFunc( CRStateFlushFunc ff );
-DECLEXPORT(void) crStateFlushArg( void *arg );
-DECLEXPORT(void) crStateDiffAPI( SPUDispatchTable *api );
-DECLEXPORT(void) crStateUpdateColorBits( void );
-
-DECLEXPORT(void) crStateSetCurrentPointers( CRContext *ctx, CRCurrentStatePointers *current );
-DECLEXPORT(void) crStateResetCurrentPointers( CRCurrentStatePointers *current );
-
-DECLEXPORT(void) crStateSetExtensionString( CRContext *ctx, const GLubyte *extensions );
-
-DECLEXPORT(void) crStateDiffContext( CRContext *from, CRContext *to );
-DECLEXPORT(void) crStateSwitchContext( CRContext *from, CRContext *to );
-
-DECLEXPORT(unsigned int) crStateHlpComponentsCount( GLenum pname );
+DECLEXPORT(void) crStateFlushFunc(PCRStateTracker pState, CRStateFlushFunc ff);
+DECLEXPORT(void) crStateFlushArg(PCRStateTracker pState, void *arg );
+DECLEXPORT(void) crStateDiffAPI(PCRStateTracker pState, SPUDispatchTable *api);
+DECLEXPORT(void) crStateUpdateColorBits(PCRStateTracker pState);
+
+DECLEXPORT(void) crStateSetCurrentPointers(CRContext *ctx, CRCurrentStatePointers *current);
+DECLEXPORT(void) crStateResetCurrentPointers(CRCurrentStatePointers *current);
+
+DECLEXPORT(void) crStateSetExtensionString(CRContext *ctx, const GLubyte *extensions);
+
+DECLEXPORT(void) crStateDiffContext(CRContext *from, CRContext *to);
+DECLEXPORT(void) crStateSwitchContext(CRContext *from, CRContext *to);
+
+DECLEXPORT(unsigned int) crStateHlpComponentsCount(GLenum pname);
 
 typedef struct CRFBDataElement
@@ -277,5 +298,5 @@
 
 DECLEXPORT(void) crStateGetTextureObjectAndImage(CRContext *g, GLenum texTarget, GLint level,
-                                     CRTextureObj **obj, CRTextureLevel **img);
+                                                 CRTextureObj **obj, CRTextureLevel **img);
 
 
@@ -287,42 +308,31 @@
 typedef FNCRSTATE_CONTEXT_GET *PFNCRSTATE_CONTEXT_GET;
 DECLEXPORT(int32_t) crStateLoadContext(CRContext *pContext, CRHashTable * pCtxTable, PFNCRSTATE_CONTEXT_GET pfnCtxGet, PSSMHANDLE pSSM, uint32_t u32Version);
-DECLEXPORT(void) crStateFreeShared(CRContext *pContext, CRSharedState *s);
-
-DECLEXPORT(int32_t) crStateLoadGlobals(PSSMHANDLE pSSM, uint32_t u32Version);
-DECLEXPORT(int32_t) crStateSaveGlobals(PSSMHANDLE pSSM);
-
-DECLEXPORT(CRSharedState *) crStateGlobalSharedAcquire(void);
-DECLEXPORT(void) crStateGlobalSharedRelease(void);
-#endif
-
-DECLEXPORT(void) crStateSetTextureUsed(GLuint texture, GLboolean used);
-DECLEXPORT(void) crStatePinTexture(GLuint texture, GLboolean pin);
-DECLEXPORT(void) crStateDeleteTextureCallback(void *texObj);
+DECLEXPORT(void) crStateFreeShared(PCRStateTracker pState, CRContext *pContext, CRSharedState *s);
+
+DECLEXPORT(int32_t) crStateLoadGlobals(PCRStateTracker pState, PSSMHANDLE pSSM, uint32_t u32Version);
+DECLEXPORT(int32_t) crStateSaveGlobals(PCRStateTracker pState, PSSMHANDLE pSSM);
+
+DECLEXPORT(CRSharedState *) crStateGlobalSharedAcquire(PCRStateTracker pState);
+DECLEXPORT(void) crStateGlobalSharedRelease(PCRStateTracker pState);
+#endif
+
+DECLEXPORT(void) crStateSetTextureUsed(PCRStateTracker pState, GLuint texture, GLboolean used);
+DECLEXPORT(void) crStatePinTexture(PCRStateTracker pState, GLuint texture, GLboolean pin);
+DECLEXPORT(void) crStateDeleteTextureCallback(void *texObj, void *pvUser);
 
    /* XXX move these! */
 
-DECLEXPORT(void) STATE_APIENTRY
-crStateChromiumParameteriCR( GLenum target, GLint value );
-
-DECLEXPORT(void) STATE_APIENTRY
-crStateChromiumParameterfCR( GLenum target, GLfloat value );
-
-DECLEXPORT(void) STATE_APIENTRY
-crStateChromiumParametervCR( GLenum target, GLenum type, GLsizei count, const GLvoid *values );
-
-DECLEXPORT(void) STATE_APIENTRY
-crStateGetChromiumParametervCR( GLenum target, GLuint index, GLenum type,
-                                GLsizei count, GLvoid *values );
-
-DECLEXPORT(void) STATE_APIENTRY
-crStateReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
-                   GLenum format, GLenum type, GLvoid *pixels );
-
-DECLEXPORT(void) STATE_APIENTRY crStateShareContext(GLboolean value);
+DECLEXPORT(void) STATE_APIENTRY crStateChromiumParameteriCR(PCRStateTracker pState, GLenum target, GLint value );
+DECLEXPORT(void) STATE_APIENTRY crStateChromiumParameterfCR(PCRStateTracker pState, GLenum target, GLfloat value );
+DECLEXPORT(void) STATE_APIENTRY crStateChromiumParametervCR(PCRStateTracker pState, GLenum target, GLenum type, GLsizei count, const GLvoid *values );
+DECLEXPORT(void) STATE_APIENTRY crStateGetChromiumParametervCR(PCRStateTracker pState, GLenum target, GLuint index, GLenum type,
+                                                               GLsizei count, GLvoid *values );
+DECLEXPORT(void) STATE_APIENTRY crStateReadPixels(PCRStateTracker pState, GLint x, GLint y, GLsizei width, GLsizei height,
+                                                  GLenum format, GLenum type, GLvoid *pixels );
+DECLEXPORT(void) STATE_APIENTRY crStateShareContext(PCRStateTracker pState, GLboolean value);
 DECLEXPORT(void) STATE_APIENTRY crStateShareLists(CRContext *pContext1, CRContext *pContext2);
 DECLEXPORT(void) STATE_APIENTRY crStateSetSharedContext(CRContext *pCtx);
 DECLEXPORT(GLboolean) STATE_APIENTRY crStateContextIsShared(CRContext *pCtx);
-
-DECLEXPORT(void) STATE_APIENTRY crStateQueryHWState(GLuint fbFbo, GLuint bbFbo);
+DECLEXPORT(void) STATE_APIENTRY crStateQueryHWState(PCRStateTracker pState, GLuint fbFbo, GLuint bbFbo);
 #ifdef __cplusplus
 }
Index: trunk/src/VBox/GuestHost/OpenGL/include/cr_pack.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/cr_pack.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/cr_pack.h	(revision 78375)
@@ -12,4 +12,7 @@
 #include "cr_protocol.h"
 #include "cr_opcodes.h"
+#ifndef IN_RING0
+# include "cr_glstate.h"
+#endif
 #include "state/cr_statetypes.h"
 #include "state/cr_currentpointers.h"
@@ -98,4 +101,7 @@
 # define CR_PACKER_CONTEXT_ARGCTX(C)
 extern CRtsd _PackerTSD;
+#ifndef IN_RING0
+extern DECLHIDDEN(PCRStateTracker) g_pStateTracker; /** Hack to make the state tracker available to pack_client.c which uses crStateGetCurrent(). */
+#endif
 # define CR_GET_PACKER_CONTEXT(C) CRPackContext *C = (CRPackContext *) crGetTSD(&_PackerTSD)
 # define CR_LOCK_PACKER_CONTEXT(PC) crLockMutex(&((PC)->mutex))
Index: trunk/src/VBox/GuestHost/OpenGL/include/cr_server.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/cr_server.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/cr_server.h	(revision 78375)
@@ -408,4 +408,7 @@
     /* visBits -> dummy mural association */
     CRHashTable *dummyMuralTable;
+
+    /** State tracker state. */
+    CRStateTracker StateTracker;
 
     GLboolean fRootVrOn;
@@ -594,4 +597,7 @@
 extern int crVBoxServerHostCtl(VBOXCRCMDCTL *pCtl, uint32_t cbCtl);
 
+extern DECLEXPORT(void) crVBoxServerDetachThread(void);
+extern DECLEXPORT(void) crVBoxServerAttachThread(void);
+
 #ifdef __cplusplus
 }
Index: trunk/src/VBox/GuestHost/OpenGL/include/cr_unpack.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/cr_unpack.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/cr_unpack.h	(revision 78375)
@@ -13,4 +13,5 @@
 #include "cr_mem.h"
 #include "cr_opcodes.h"
+#include "cr_glstate.h"
 
 #include <iprt/types.h>
@@ -42,4 +43,6 @@
      * on error if one unpacker detected out of bounds buffer access). */
     int                         rcUnpack;
+    /** Attached state tracker. */
+    PCRStateTracker             pStateTracker;
 } CrUnpackerState;
 /** Pointer to an unpacker state. */
Index: trunk/src/VBox/GuestHost/OpenGL/include/state/cr_bufferobject.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/state/cr_bufferobject.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/state/cr_bufferobject.h	(revision 78375)
@@ -54,15 +54,17 @@
 
 	CRBufferObject *nullBuffer;  /* name = 0 */
+    /** Attached state tracker. */
+    PCRStateTracker pStateTracker;
 } CRBufferObjectState;
 
 DECLEXPORT(CRBufferObject *) crStateGetBoundBufferObject(GLenum target, CRBufferObjectState *b);
-DECLEXPORT(GLboolean) crStateIsBufferBound(GLenum target);
+DECLEXPORT(GLboolean) crStateIsBufferBound(PCRStateTracker pState, GLenum target);
 struct CRContext;
 DECLEXPORT(GLboolean) crStateIsBufferBoundForCtx(struct CRContext *g, GLenum target);
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateBufferHWIDtoID(GLuint hwid);
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetBufferHWID(GLuint id);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateBufferHWIDtoID(PCRStateTracker pState, GLuint hwid);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetBufferHWID(PCRStateTracker pState, GLuint id);
 
-DECLEXPORT(void) crStateRegBuffers(GLsizei n, GLuint *buffers);
+DECLEXPORT(void) crStateRegBuffers(PCRStateTracker pState, GLsizei n, GLuint *buffers);
 #ifdef __cplusplus
 }
Index: trunk/src/VBox/GuestHost/OpenGL/include/state/cr_client.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/state/cr_client.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/state/cr_client.h	(revision 78375)
@@ -139,6 +139,6 @@
 DECLEXPORT(void) crStateClientDestroy(struct CRContext *g);
 
-DECLEXPORT(GLboolean) crStateUseServerArrays(void);
-DECLEXPORT(GLboolean) crStateUseServerArrayElements(void);
+DECLEXPORT(GLboolean) crStateUseServerArrays(PCRStateTracker pState);
+DECLEXPORT(GLboolean) crStateUseServerArrayElements(PCRStateTracker pState);
 DECLEXPORT(CRClientPointer*) crStateGetClientPointerByIndex(int index, CRVertexArrays *array);
 
Index: trunk/src/VBox/GuestHost/OpenGL/include/state/cr_current.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/state/cr_current.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/state/cr_current.h	(revision 78375)
@@ -88,5 +88,5 @@
 DECLEXPORT(void) crStateCurrentInit( CRContext *ctx );
 
-DECLEXPORT(void) crStateCurrentRecover( void );
+DECLEXPORT(void) crStateCurrentRecover(PCRStateTracker pState);
 
 DECLEXPORT(void) crStateCurrentRecoverNew(CRContext *g, CRCurrentStatePointers  *current);
@@ -97,5 +97,5 @@
                                     CRContext *fromCtx, CRContext *toCtx);
 
-DECLEXPORT(void) crStateRasterPosUpdate(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
+DECLEXPORT(void) crStateRasterPosUpdate(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
 
 DECLEXPORT(GLuint) crStateNeedDummyZeroVertexArray(CRContext *g, CRCurrentStatePointers  *current, GLfloat *pZva);
Index: trunk/src/VBox/GuestHost/OpenGL/include/state/cr_framebuffer.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/state/cr_framebuffer.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/state/cr_framebuffer.h	(revision 78375)
@@ -75,29 +75,29 @@
 DECLEXPORT(void) STATE_APIENTRY crStateFramebufferObjectReenableHW(CRContext *fromCtx, CRContext *toCtx, GLuint idDrawFBO, GLuint idReadFBO);
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetFramebufferHWID(GLuint id);
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetRenderbufferHWID(GLuint id);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetFramebufferHWID(PCRStateTracker pState, GLuint id);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetRenderbufferHWID(PCRStateTracker pState, GLuint id);
 
-DECLEXPORT(void) STATE_APIENTRY crStateBindRenderbufferEXT(GLenum target, GLuint renderbuffer);
-DECLEXPORT(void) STATE_APIENTRY crStateDeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers);
-DECLEXPORT(void) STATE_APIENTRY crStateRenderbufferStorageEXT(GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
-DECLEXPORT(void) STATE_APIENTRY crStateGetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params);
-DECLEXPORT(void) STATE_APIENTRY crStateBindFramebufferEXT(GLenum target, GLuint framebuffer);
-DECLEXPORT(void) STATE_APIENTRY crStateDeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers);
-DECLEXPORT(void) STATE_APIENTRY crStateFramebufferTexture1DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
-DECLEXPORT(void) STATE_APIENTRY crStateFramebufferTexture2DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
-DECLEXPORT(void) STATE_APIENTRY crStateFramebufferTexture3DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset);
-DECLEXPORT(void) STATE_APIENTRY crStateFramebufferRenderbufferEXT(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer);
-DECLEXPORT(void) STATE_APIENTRY crStateGetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment, GLenum pname, GLint *params);
-DECLEXPORT(void) STATE_APIENTRY crStateGenerateMipmapEXT(GLenum target);
+DECLEXPORT(void) STATE_APIENTRY crStateBindRenderbufferEXT(PCRStateTracker pState, GLenum target, GLuint renderbuffer);
+DECLEXPORT(void) STATE_APIENTRY crStateDeleteRenderbuffersEXT(PCRStateTracker pState, GLsizei n, const GLuint *renderbuffers);
+DECLEXPORT(void) STATE_APIENTRY crStateRenderbufferStorageEXT(PCRStateTracker pState, GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
+DECLEXPORT(void) STATE_APIENTRY crStateGetRenderbufferParameterivEXT(PCRStateTracker pState, GLenum target, GLenum pname, GLint *params);
+DECLEXPORT(void) STATE_APIENTRY crStateBindFramebufferEXT(PCRStateTracker pState, GLenum target, GLuint framebuffer);
+DECLEXPORT(void) STATE_APIENTRY crStateDeleteFramebuffersEXT(PCRStateTracker pState, GLsizei n, const GLuint *framebuffers);
+DECLEXPORT(void) STATE_APIENTRY crStateFramebufferTexture1DEXT(PCRStateTracker pState, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
+DECLEXPORT(void) STATE_APIENTRY crStateFramebufferTexture2DEXT(PCRStateTracker pState, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level);
+DECLEXPORT(void) STATE_APIENTRY crStateFramebufferTexture3DEXT(PCRStateTracker pState, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset);
+DECLEXPORT(void) STATE_APIENTRY crStateFramebufferRenderbufferEXT(PCRStateTracker pState, GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer);
+DECLEXPORT(void) STATE_APIENTRY crStateGetFramebufferAttachmentParameterivEXT(PCRStateTracker pState, GLenum target, GLenum attachment, GLenum pname, GLint *params);
+DECLEXPORT(void) STATE_APIENTRY crStateGenerateMipmapEXT(PCRStateTracker pState, GLenum target);
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateFBOHWIDtoID(GLuint hwid);
-DECLEXPORT(GLuint) STATE_APIENTRY crStateRBOHWIDtoID(GLuint hwid);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateFBOHWIDtoID(PCRStateTracker pState, GLuint hwid);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateRBOHWIDtoID(PCRStateTracker pState, GLuint hwid);
 
-DECLEXPORT(void) crStateRegFramebuffers(GLsizei n, GLuint *buffers);
-DECLEXPORT(void) crStateRegRenderbuffers(GLsizei n, GLuint *buffers);
+DECLEXPORT(void) crStateRegFramebuffers(PCRStateTracker pState, GLsizei n, GLuint *buffers);
+DECLEXPORT(void) crStateRegRenderbuffers(PCRStateTracker pState, GLsizei n, GLuint *buffers);
 
 #ifdef IN_GUEST
-DECLEXPORT(GLenum) STATE_APIENTRY crStateCheckFramebufferStatusEXT(GLenum target);
-DECLEXPORT(GLenum) STATE_APIENTRY crStateSetFramebufferStatus(GLenum target, GLenum status);
+DECLEXPORT(GLenum) STATE_APIENTRY crStateCheckFramebufferStatusEXT(PCRStateTracker pState, GLenum target);
+DECLEXPORT(GLenum) STATE_APIENTRY crStateSetFramebufferStatus(PCRStateTracker pState, GLenum target, GLenum status);
 #endif
 
Index: trunk/src/VBox/GuestHost/OpenGL/include/state/cr_glsl.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/state/cr_glsl.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/state/cr_glsl.h	(revision 78375)
@@ -93,25 +93,25 @@
 DECLEXPORT(void) STATE_APIENTRY crStateGLSLSwitch(CRContext *from, CRContext *to);
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetShaderHWID(GLuint id);
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetProgramHWID(GLuint id);
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGLSLProgramHWIDtoID(GLuint hwid);
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGLSLShaderHWIDtoID(GLuint hwid);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetShaderHWID(PCRStateTracker pState, GLuint id);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetProgramHWID(PCRStateTracker pState, GLuint id);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGLSLProgramHWIDtoID(PCRStateTracker pState, GLuint hwid);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGLSLShaderHWIDtoID(PCRStateTracker pState, GLuint hwid);
 
-DECLEXPORT(GLint) STATE_APIENTRY crStateGetUniformSize(GLenum type);
-DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsIntUniform(GLenum type);
+DECLEXPORT(GLint) STATE_APIENTRY crStateGetUniformSize(PCRStateTracker pState, GLenum type);
+DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsIntUniform(PCRStateTracker pState, GLenum type);
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateCreateShader(GLuint id, GLenum type);
-DECLEXPORT(GLuint) STATE_APIENTRY crStateCreateProgram(GLuint id);
-DECLEXPORT(GLuint) STATE_APIENTRY crStateDeleteObjectARB( VBoxGLhandleARB obj );
+DECLEXPORT(GLuint) STATE_APIENTRY crStateCreateShader(PCRStateTracker pState, GLuint id, GLenum type);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateCreateProgram(PCRStateTracker pState, GLuint id);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateDeleteObjectARB(PCRStateTracker pState, VBoxGLhandleARB obj );
 
-DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsProgramUniformsCached(GLuint program);
-DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsProgramAttribsCached(GLuint program);
+DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsProgramUniformsCached(PCRStateTracker pState, GLuint program);
+DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsProgramAttribsCached(PCRStateTracker pState, GLuint program);
 
 #ifdef IN_GUEST
-DECLEXPORT(void) STATE_APIENTRY crStateGLSLProgramCacheUniforms(GLuint program, GLsizei cbData, GLvoid *pData);
-DECLEXPORT(void) STATE_APIENTRY crStateGLSLProgramCacheAttribs(GLuint program, GLsizei cbData, GLvoid *pData);
+DECLEXPORT(void) STATE_APIENTRY crStateGLSLProgramCacheUniforms(PCRStateTracker pState, GLuint program, GLsizei cbData, GLvoid *pData);
+DECLEXPORT(void) STATE_APIENTRY crStateGLSLProgramCacheAttribs(PCRStateTracker pState, GLuint program, GLsizei cbData, GLvoid *pData);
 #else
-DECLEXPORT(void) STATE_APIENTRY crStateGLSLProgramCacheUniforms(GLuint program, GLsizei maxcbData, GLsizei *cbData, GLvoid *pData);
-DECLEXPORT(void) STATE_APIENTRY crStateGLSLProgramCacheAttribs(GLuint program, GLsizei maxcbData, GLsizei *cbData, GLvoid *pData);
+DECLEXPORT(void) STATE_APIENTRY crStateGLSLProgramCacheUniforms(PCRStateTracker pState, GLuint program, GLsizei maxcbData, GLsizei *cbData, GLvoid *pData);
+DECLEXPORT(void) STATE_APIENTRY crStateGLSLProgramCacheAttribs(PCRStateTracker pState, GLuint program, GLsizei maxcbData, GLsizei *cbData, GLvoid *pData);
 #endif
 
Index: trunk/src/VBox/GuestHost/OpenGL/include/state/cr_lighting.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/state/cr_lighting.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/state/cr_lighting.h	(revision 78375)
@@ -84,5 +84,5 @@
                                        CRContext *fromCtx, CRContext *toCtx);
 
-DECLEXPORT(void) crStateColorMaterialRecover( void );
+DECLEXPORT(void) crStateColorMaterialRecover(PCRStateTracker pState);
 
 #ifdef __cplusplus
Index: trunk/src/VBox/GuestHost/OpenGL/include/state/cr_stateerror.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/state/cr_stateerror.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/state/cr_stateerror.h	(revision 78375)
@@ -12,5 +12,5 @@
 #include <iprt/cdefs.h>
 
-DECLEXPORT(void) crStateError( int line, const char *file, GLenum err, const char *format, ... );
+DECLEXPORT(void) crStateError(PCRStateTracker pState, int line, const char *file, GLenum err, const char *format, ... );
 
 #endif /* CR_STATE_ERROR_H */
Index: trunk/src/VBox/GuestHost/OpenGL/include/state/cr_statetypes.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/state/cr_statetypes.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/state/cr_statetypes.h	(revision 78375)
@@ -15,4 +15,6 @@
 extern "C" {
 #endif
+
+typedef struct CRStateTracker *PCRStateTracker;
 
 typedef GLfloat GLdefault;
Index: trunk/src/VBox/GuestHost/OpenGL/include/state/cr_texture.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/state/cr_texture.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/state/cr_texture.h	(revision 78375)
@@ -217,5 +217,5 @@
 DECLEXPORT(CRTextureObj *) crStateTextureAllocate(GLuint name);
     /*void crStateTextureDelete(GLuint name);*/
-DECLEXPORT(CRTextureObj *) crStateTextureGet(GLenum target, GLuint textureid);
+DECLEXPORT(CRTextureObj *) crStateTextureGet(PCRStateTracker pState, GLenum target, GLuint textureid);
 DECLEXPORT(int) crStateTextureGetSize(GLenum target, GLenum level);
 DECLEXPORT(const GLvoid *) crStateTextureGetData(GLenum target, GLenum level);
@@ -238,9 +238,9 @@
 DECLEXPORT(void) crStateDeleteTextureObject(CRTextureObj *tobj);
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateTextureHWIDtoID(GLuint hwid);
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetTextureHWID(GLuint id);
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetTextureObjHWID(CRTextureObj *tobj);
-
-void crStateRegTextures(GLsizei n, GLuint *names);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateTextureHWIDtoID(PCRStateTracker pState, GLuint hwid);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetTextureHWID(PCRStateTracker pState, GLuint id);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetTextureObjHWID(PCRStateTracker pState, CRTextureObj *tobj);
+
+void crStateRegTextures(PCRStateTracker pState, GLsizei n, GLuint *names);
 
 #ifdef __cplusplus
Index: trunk/src/VBox/GuestHost/OpenGL/include/state/cr_transform.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/include/state/cr_transform.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/include/state/cr_transform.h	(revision 78375)
@@ -71,5 +71,5 @@
 DECLEXPORT(void) crStateInitMatrixStack(CRMatrixStack *stack, int maxDepth);
 
-DECLEXPORT(void) crStateLoadMatrix(const CRmatrix *m);
+DECLEXPORT(void) crStateLoadMatrix(PCRStateTracker pState, const CRmatrix *m);
 
 DECLEXPORT(void) crStateTransformUpdateTransform(CRTransformState *t);
Index: trunk/src/VBox/GuestHost/OpenGL/packer/pack_client.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/packer/pack_client.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/packer/pack_client.c	(revision 78375)
@@ -308,5 +308,5 @@
     unsigned int unit, attr;
     const CRVertexArrays *array = &(c->array);
-    const GLboolean vpEnabled = crStateGetCurrent()->program.vpEnabled;
+    const GLboolean vpEnabled = crStateGetCurrent(g_pStateTracker)->program.vpEnabled;
 
     /*crDebug("crPackExpandArrayElement(%i)", index);*/
@@ -669,5 +669,5 @@
     GLsizei indexsize;
 #ifdef CR_ARB_vertex_buffer_object
-    CRBufferObject *elementsBuffer = crStateGetCurrent()->bufferobject.elementsBuffer;
+    CRBufferObject *elementsBuffer = crStateGetCurrent(g_pStateTracker)->bufferobject.elementsBuffer;
     packet_length += sizeof(GLint);
     if (elementsBuffer && elementsBuffer->id)
@@ -708,5 +708,5 @@
 void PACK_APIENTRY
 crPackDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
-                                                GLenum type, const GLvoid *indices)
+                        GLenum type, const GLvoid *indices)
 {
     unsigned char *data_ptr, *start_ptr;
@@ -716,5 +716,5 @@
 
 #ifdef CR_ARB_vertex_buffer_object
-    CRBufferObject *elementsBuffer = crStateGetCurrent()->bufferobject.elementsBuffer;
+    CRBufferObject *elementsBuffer = crStateGetCurrent(g_pStateTracker)->bufferobject.elementsBuffer;
     packet_length += sizeof(GLint);
     if (elementsBuffer && elementsBuffer->id)
@@ -762,10 +762,10 @@
 void
 crPackExpandDrawElements(GLenum mode, GLsizei count, GLenum type,
-                                                 const GLvoid *indices, CRClientState *c, const GLfloat *pZva)
+                         const GLvoid *indices, CRClientState *c, const GLfloat *pZva)
 {
     int i;
     GLubyte *p = (GLubyte *)indices;
 #ifdef CR_ARB_vertex_buffer_object
-    CRBufferObject *elementsBuffer = crStateGetCurrent()->bufferobject.elementsBuffer;
+    CRBufferObject *elementsBuffer = crStateGetCurrent(g_pStateTracker)->bufferobject.elementsBuffer;
 #endif
 
@@ -1065,5 +1065,5 @@
 void PACK_APIENTRY crPackLockArraysEXT(GLint first, GLint count)
 {
-    CRContext *g = crStateGetCurrent();
+    CRContext *g = crStateGetCurrent(g_pStateTracker);
     CRClientState *c = &g->client;
     unsigned char *data_ptr, *start_ptr;
Index: trunk/src/VBox/GuestHost/OpenGL/packer/pack_pixelmap.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/packer/pack_pixelmap.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/packer/pack_pixelmap.c	(revision 78375)
@@ -11,5 +11,5 @@
 static unsigned char * __gl_HandlePixelMapData(GLenum map, GLsizei mapsize, int size_of_value, const GLvoid *values)
 {
-    int nodata = (values == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int nodata = (values == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
     int packet_length = 
         sizeof( map ) + 
Index: trunk/src/VBox/GuestHost/OpenGL/packer/pack_pixels.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/packer/pack_pixels.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/packer/pack_pixels.c	(revision 78375)
@@ -19,5 +19,5 @@
     unsigned char *data_ptr;
     int packet_length, imagesize;
-    int noimagedata = (pixels == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (pixels == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     packet_length = 
@@ -115,5 +115,5 @@
                                 const GLubyte *bitmap, const CRPixelPackState *unpack )
 {
-    const int noimagedata = (bitmap == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    const int noimagedata = (bitmap == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
     unsigned char *data_ptr;
     int data_length = 0;
Index: trunk/src/VBox/GuestHost/OpenGL/packer/pack_stipple.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/packer/pack_stipple.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/packer/pack_stipple.c	(revision 78375)
@@ -14,5 +14,5 @@
     CR_GET_PACKER_CONTEXT(pc);
     unsigned char *data_ptr;
-    int nodata = crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int nodata = crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
     int packet_length = sizeof(int);
 
Index: trunk/src/VBox/GuestHost/OpenGL/packer/pack_texture.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/packer/pack_texture.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/packer/pack_texture.c	(revision 78375)
@@ -20,5 +20,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (pixels == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (pixels == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     packet_length =
@@ -63,5 +63,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    const int noimagedata = (pixels == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    const int noimagedata = (pixels == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
     const int is_distrib = ((type == GL_TRUE) || (type == GL_FALSE));
     int distrib_buf_len = 0;
@@ -139,5 +139,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (pixels == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (pixels == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
     int is_distrib = ( (type == GL_TRUE) || (type == GL_FALSE) ) ;
     int distrib_buf_len = 0;
@@ -215,5 +215,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (pixels == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (pixels == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
     int is_distrib = ( (type == GL_TRUE) || (type == GL_FALSE) ) ;
     int distrib_buf_len = 0;
@@ -561,5 +561,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (pixels == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (pixels == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     packet_length =
@@ -614,5 +614,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (pixels == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (pixels == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     packet_length =
@@ -660,5 +660,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (pixels == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (pixels == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     packet_length =
@@ -732,5 +732,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (data == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (data == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     /* All extended opcodes have their first 8 bytes predefined:
@@ -777,5 +777,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (data == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (data == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     /* All extended opcodes have their first 8 bytes predefined:
@@ -825,5 +825,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (data == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (data == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     /* All extended opcodes have their first 8 bytes predefined:
@@ -873,5 +873,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (data == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (data == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     /* All extended opcodes have their first 8 bytes predefined:
@@ -917,5 +917,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (data == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (data == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     /* All extended opcodes have their first 8 bytes predefined:
@@ -965,5 +965,5 @@
     unsigned char *data_ptr;
     int packet_length;
-    int noimagedata = (data == NULL) || crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    int noimagedata = (data == NULL) || crStateIsBufferBound(g_pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     /* All extended opcodes have their first 8 bytes predefined:
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/gendiffcode.py
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/gendiffcode.py	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/gendiffcode.py	(revision 78375)
@@ -20,4 +20,5 @@
 		CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
 	CR%(Name)sState *from = &(fromCtx->%(name)s);
 	CR%(Name)sState *to = &(toCtx->%(name)s);"""%vars())
@@ -28,4 +29,5 @@
 		CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
 	CR%(Name)sState *from = &(fromCtx->%(name)s);
 	CR%(Name)sState *to = &(toCtx->%(name)s);"""%vars())
@@ -58,4 +60,5 @@
 	print("""	unsigned int j, i;
 	CRbitvalue nbitID[CR_MAX_BITARRAY];
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
 	for (j = 0; j<CR_MAX_BITARRAY; j++)
 		nbitID[j] = ~bitID[j];
@@ -151,6 +154,6 @@
 			if members[0] != "*" and guardbit[0:6] == "enable":
 				print(tab+"glAble able[2];")
-				print(tab+"able[0] = diff_api.Disable;")
-				print(tab+"able[1] = diff_api.Enable;")
+				print(tab+"able[0] = pState->diff_api.Disable;")
+				print(tab+"able[1] = pState->diff_api.Enable;")
 
 		current_dependency = dependency
@@ -226,5 +229,5 @@
 								i += 1
 
-						sys.stdout.write(tab+"diff_api.%(func)s("%vars())
+						sys.stdout.write(tab+"pState->diff_api.%(func)s("%vars())
 						for funcarg in funcargs:
 							sys.stdout.write(funcarg+", ")
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state.h
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state.h	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state.h	(revision 78375)
@@ -12,5 +12,5 @@
 #define CRSTATE_CHECKERR_RET(expr, result, message, ret)         \
     if (expr) {                                             \
-        crStateError(__LINE__, __FILE__, result, message);  \
+        crStateError(pState, __LINE__, __FILE__, result, message);  \
         return ret;                                             \
     }
@@ -22,25 +22,17 @@
 typedef struct _crCheckIDHWID {
     GLuint id, hwid;
+    PCRStateTracker pState;
 } crCheckIDHWID_t;
 
-extern SPUDispatchTable diff_api;
-extern CRStateBits *__currentBits;
-
-#define GetCurrentBits() __currentBits
+#define GetCurrentBits(a_pState) (a_pState)->pCurrentBits
 
 #include <cr_threads.h>
 
-extern CRtsd __contextTSD;
-#define GetCurrentContext() VBoxTlsRefGetCurrent(CRContext, &__contextTSD)
+#define GetCurrentContext(a_pState) VBoxTlsRefGetCurrent(CRContext, &((a_pState)->contextTSD))
 
 /* NOTE: below SetCurrentContext stuff is supposed to be used only internally!!
  * it is placed here only to simplify things since some code besides state_init.c
  * (i.e. state_glsl.c) is using it */
-#define SetCurrentContext(_ctx) VBoxTlsRefSetCurrent(CRContext, &__contextTSD, _ctx)
-
-extern GLboolean g_bVBoxEnableDiffOnMakeCurrent;
-
-extern CRContext *g_pAvailableContexts[CR_MAX_CONTEXTS];
-extern uint32_t g_cContexts;
+#define SetCurrentContext(a_pState, _ctx) VBoxTlsRefSetCurrent(CRContext, &((a_pState)->contextTSD), _ctx)
 
 extern void crStateTextureInitTextureObj (CRContext *ctx, CRTextureObj *tobj, GLuint name, GLenum target);
@@ -66,10 +58,10 @@
 void crStateClientSwitch(CRClientBits *cb, CRbitvalue *bitID,   CRContext *from, CRContext *to);
 
-void crStateFreeBufferObject(void *data);
-void crStateFreeFBO(void *data);
-void crStateFreeRBO(void *data);
+void crStateFreeBufferObject(void *data, void *pvUser);
+void crStateFreeFBO(void *data, void *pvUser);
+void crStateFreeRBO(void *data, void *pvUser);
 
 void crStateGenNames(CRContext *g, CRHashTable *table, GLsizei n, GLuint *names);
 void crStateRegNames(CRContext *g, CRHashTable *table, GLsizei n, GLuint *names);
-void crStateOnTextureUsageRelease(CRSharedState *pS, CRTextureObj *pObj);
+void crStateOnTextureUsageRelease(PCRStateTracker pState, CRSharedState *pS, CRTextureObj *pObj);
 #endif
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_attrib.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_attrib.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_attrib.c	(revision 78375)
@@ -83,10 +83,10 @@
 
 static void
-copy_texobj(CRTextureObj *dest, CRTextureObj *src, GLboolean copyName)
+copy_texobj(PCRStateTracker pState, CRTextureObj *dest, CRTextureObj *src, GLboolean copyName)
 {
     if (copyName)
     {
         dest->id = src->id;
-        dest->hwid = crStateGetTextureObjHWID(src);
+        dest->hwid = crStateGetTextureObjHWID(pState, src);
     }
 
@@ -109,9 +109,9 @@
 }
 
-void STATE_APIENTRY crStatePushAttrib(GLbitfield mask)
+void STATE_APIENTRY crStatePushAttrib(PCRStateTracker pState, GLbitfield mask)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRAttribState *a = &(g->attrib);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRAttribBits *ab = &(sb->attrib);
     unsigned int i;
@@ -119,5 +119,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glPushAttrib called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glPushAttrib called in Begin/End");
         return;
     }
@@ -125,5 +125,5 @@
     if (a->attribStackDepth == CR_MAX_ATTRIB_STACK_DEPTH - 1)
     {
-        crStateError(__LINE__, __FILE__, GL_STACK_OVERFLOW, "glPushAttrib called with a full stack!" );
+        crStateError(pState, __LINE__, __FILE__, GL_STACK_OVERFLOW, "glPushAttrib called with a full stack!" );
         return;
     }
@@ -462,14 +462,14 @@
             copy_texunit(&tState->unit[i], &g->texture.unit[i]);
             /* texture object state */
-            copy_texobj(&tState->unit[i].Saved1D, g->texture.unit[i].currentTexture1D, GL_TRUE);
-            copy_texobj(&tState->unit[i].Saved2D, g->texture.unit[i].currentTexture2D, GL_TRUE);
+            copy_texobj(pState, &tState->unit[i].Saved1D, g->texture.unit[i].currentTexture1D, GL_TRUE);
+            copy_texobj(pState, &tState->unit[i].Saved2D, g->texture.unit[i].currentTexture2D, GL_TRUE);
 #ifdef CR_OPENGL_VERSION_1_2
-            copy_texobj(&tState->unit[i].Saved3D, g->texture.unit[i].currentTexture3D, GL_TRUE);
+            copy_texobj(pState, &tState->unit[i].Saved3D, g->texture.unit[i].currentTexture3D, GL_TRUE);
 #endif
 #ifdef CR_ARB_texture_cube_map
-            copy_texobj(&tState->unit[i].SavedCubeMap, g->texture.unit[i].currentTextureCubeMap, GL_TRUE);
+            copy_texobj(pState, &tState->unit[i].SavedCubeMap, g->texture.unit[i].currentTextureCubeMap, GL_TRUE);
 #endif
 #ifdef CR_NV_texture_rectangle
-            copy_texobj(&tState->unit[i].SavedRect, g->texture.unit[i].currentTextureRect, GL_TRUE);
+            copy_texobj(pState, &tState->unit[i].SavedRect, g->texture.unit[i].currentTextureRect, GL_TRUE);
 #endif
         }
@@ -512,9 +512,9 @@
 }
 
-void STATE_APIENTRY crStatePopAttrib(void) 
+void STATE_APIENTRY crStatePopAttrib(PCRStateTracker pState) 
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRAttribState *a = &(g->attrib);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRAttribBits *ab = &(sb->attrib);
     CRbitvalue mask;
@@ -523,5 +523,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glPopAttrib called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glPopAttrib called in Begin/End");
         return;
     }
@@ -529,5 +529,5 @@
     if (a->attribStackDepth == 0)
     {
-        crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty stack!" );
+        crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty stack!" );
         return;
     }
@@ -541,5 +541,5 @@
         if (a->accumBufferStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty accum buffer stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty accum buffer stack!" );
             return;
         }
@@ -553,5 +553,5 @@
         if (a->colorBufferStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty color buffer stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty color buffer stack!" );
             return;
         }
@@ -604,5 +604,5 @@
         if (a->currentStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty current stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty current stack!" );
             return;
         }
@@ -626,5 +626,5 @@
         if (a->depthBufferStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty depth buffer stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty depth buffer stack!" );
             return;
         }
@@ -644,5 +644,5 @@
         if (a->enableStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty enable stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty enable stack!" );
             return;
         }
@@ -738,5 +738,5 @@
         if (a->evalStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty eval stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty eval stack!" );
             return;
         }
@@ -787,5 +787,5 @@
         if (a->fogStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty fog stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty fog stack!" );
             return;
         }
@@ -811,5 +811,5 @@
         if (a->hintStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty hint stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty hint stack!" );
             return;
         }
@@ -842,5 +842,5 @@
         if (a->lightingStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty lighting stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty lighting stack!" );
             return;
         }
@@ -903,5 +903,5 @@
         if (a->lineStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty line stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty line stack!" );
             return;
         }
@@ -921,5 +921,5 @@
         if (a->listStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty list stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty list stack!" );
             return;
         }
@@ -932,5 +932,5 @@
         if (a->pixelModeStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty pixel mode stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty pixel mode stack!" );
             return;
         }
@@ -955,5 +955,5 @@
         if (a->pointStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty point stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty point stack!" );
             return;
         }
@@ -977,5 +977,5 @@
         if (a->polygonStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty polygon stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty polygon stack!" );
             return;
         }
@@ -1003,5 +1003,5 @@
         if (a->polygonStippleStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty polygon stipple stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty polygon stipple stack!" );
             return;
         }
@@ -1015,5 +1015,5 @@
         if (a->scissorStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty scissor stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty scissor stack!" );
             return;
         }
@@ -1032,5 +1032,5 @@
         if (a->stencilBufferStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty stencil stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty stencil stack!" );
             return;
         }
@@ -1065,5 +1065,5 @@
         if (a->textureStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty texture stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty texture stack!" );
             return;
         }
@@ -1076,19 +1076,19 @@
             copy_texunit(&g->texture.unit[i], &tState->unit[i]);
             /* first, restore the bindings! */
-            g->texture.unit[i].currentTexture1D = crStateTextureGet(GL_TEXTURE_1D, tState->unit[i].Saved1D.id);
-            copy_texobj(g->texture.unit[i].currentTexture1D, &tState->unit[i].Saved1D, GL_FALSE);
-            g->texture.unit[i].currentTexture2D = crStateTextureGet(GL_TEXTURE_2D, tState->unit[i].Saved2D.id);
-            copy_texobj(g->texture.unit[i].currentTexture2D, &tState->unit[i].Saved2D, GL_FALSE);
+            g->texture.unit[i].currentTexture1D = crStateTextureGet(pState, GL_TEXTURE_1D, tState->unit[i].Saved1D.id);
+            copy_texobj(pState, g->texture.unit[i].currentTexture1D, &tState->unit[i].Saved1D, GL_FALSE);
+            g->texture.unit[i].currentTexture2D = crStateTextureGet(pState, GL_TEXTURE_2D, tState->unit[i].Saved2D.id);
+            copy_texobj(pState, g->texture.unit[i].currentTexture2D, &tState->unit[i].Saved2D, GL_FALSE);
 #ifdef CR_OPENGL_VERSION_1_2
-            g->texture.unit[i].currentTexture3D = crStateTextureGet(GL_TEXTURE_3D, tState->unit[i].Saved3D.id);
-            copy_texobj(g->texture.unit[i].currentTexture3D, &tState->unit[i].Saved3D, GL_FALSE);
+            g->texture.unit[i].currentTexture3D = crStateTextureGet(pState, GL_TEXTURE_3D, tState->unit[i].Saved3D.id);
+            copy_texobj(pState, g->texture.unit[i].currentTexture3D, &tState->unit[i].Saved3D, GL_FALSE);
 #endif
 #ifdef CR_ARB_texture_cube_map
-            g->texture.unit[i].currentTextureCubeMap = crStateTextureGet(GL_TEXTURE_CUBE_MAP_ARB, tState->unit[i].SavedCubeMap.id);
-            copy_texobj(g->texture.unit[i].currentTextureCubeMap, &tState->unit[i].SavedCubeMap, GL_FALSE);
+            g->texture.unit[i].currentTextureCubeMap = crStateTextureGet(pState, GL_TEXTURE_CUBE_MAP_ARB, tState->unit[i].SavedCubeMap.id);
+            copy_texobj(pState, g->texture.unit[i].currentTextureCubeMap, &tState->unit[i].SavedCubeMap, GL_FALSE);
 #endif
 #ifdef CR_NV_texture_rectangle
-            g->texture.unit[i].currentTextureRect = crStateTextureGet(GL_TEXTURE_CUBE_MAP_ARB, tState->unit[i].SavedRect.id);
-            copy_texobj(g->texture.unit[i].currentTextureRect, &tState->unit[i].SavedRect, GL_FALSE);
+            g->texture.unit[i].currentTextureRect = crStateTextureGet(pState, GL_TEXTURE_CUBE_MAP_ARB, tState->unit[i].SavedRect.id);
+            copy_texobj(pState, g->texture.unit[i].currentTextureRect, &tState->unit[i].SavedRect, GL_FALSE);
 #endif
         }
@@ -1130,10 +1130,10 @@
         if (a->transformStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty transform stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty transform stack!" );
             return;
         }
         a->transformStackDepth--;
         g->transform.matrixMode = a->transformStack[a->transformStackDepth].matrixMode;
-        crStateMatrixMode(g->transform.matrixMode);
+        crStateMatrixMode(pState, g->transform.matrixMode);
         for (i = 0 ; i < g->limits.maxClipPlanes ; i++)
         {
@@ -1154,5 +1154,5 @@
         if (a->viewportStackDepth == 0)
         {
-            crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty viewport stack!" );
+            crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "glPopAttrib called with an empty viewport stack!" );
             return;
         }
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_buffer.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_buffer.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_buffer.c	(revision 78375)
@@ -13,5 +13,5 @@
 {
     CRBufferState *b = &ctx->buffer;
-    CRStateBits *sb          = GetCurrentBits();
+    CRStateBits *sb          = GetCurrentBits(ctx->pStateTracker);
     CRBufferBits *bb = &(sb->buffer);
     GLcolorf zero_colorf = {0.0f, 0.0f, 0.0f, 0.0f};
@@ -83,14 +83,14 @@
 }
 
-void STATE_APIENTRY crStateAlphaFunc (GLenum func, GLclampf ref) 
-{
-    CRContext *g             = GetCurrentContext();
+void STATE_APIENTRY crStateAlphaFunc (PCRStateTracker pState, GLenum func, GLclampf ref) 
+{
+    CRContext *g             = GetCurrentContext(pState);
     CRBufferState *b         = &(g->buffer);
-    CRStateBits *sb          = GetCurrentBits();
-    CRBufferBits *bb = &(sb->buffer);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glAlphaFunc called in begin/end");
+    CRStateBits *sb          = GetCurrentBits(pState);
+    CRBufferBits *bb = &(sb->buffer);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glAlphaFunc called in begin/end");
         return;
     }
@@ -110,5 +110,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glAlphaFunc:  Invalid func: %d", func);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glAlphaFunc:  Invalid func: %d", func);
             return;
     }
@@ -123,14 +123,14 @@
 }
 
-void STATE_APIENTRY crStateDepthFunc (GLenum func) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sb = GetCurrentBits();
-    CRBufferBits *bb = &(sb->buffer);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDepthFunc called in begin/end");
+void STATE_APIENTRY crStateDepthFunc (PCRStateTracker pState, GLenum func) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRBufferBits *bb = &(sb->buffer);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glDepthFunc called in begin/end");
         return;
     }
@@ -150,5 +150,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glDepthFunc:  Invalid func: %d", func);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glDepthFunc:  Invalid func: %d", func);
             return;
     }
@@ -160,14 +160,14 @@
 }
 
-void STATE_APIENTRY crStateBlendFunc (GLenum sfactor, GLenum dfactor) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sb = GetCurrentBits();
-    CRBufferBits *bb = &(sb->buffer);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glBlendFunc called in begin/end");
+void STATE_APIENTRY crStateBlendFunc (PCRStateTracker pState, GLenum sfactor, GLenum dfactor) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRBufferBits *bb = &(sb->buffer);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glBlendFunc called in begin/end");
         return;
     }
@@ -197,5 +197,5 @@
         RT_FALL_THRU();
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid sfactor passed to glBlendFunc: %d", sfactor);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid sfactor passed to glBlendFunc: %d", sfactor);
             return;
     }
@@ -222,5 +222,5 @@
         RT_FALL_THRU();
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid dfactor passed to glBlendFunc: %d", dfactor);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid dfactor passed to glBlendFunc: %d", dfactor);
             return;
     }
@@ -234,14 +234,14 @@
 }
 
-void STATE_APIENTRY crStateBlendColorEXT( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sb = GetCurrentBits();
-    CRBufferBits *bb = &(sb->buffer);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "BlendColorEXT called inside a Begin/End" );
+void STATE_APIENTRY crStateBlendColorEXT(PCRStateTracker pState, GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha )
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRBufferBits *bb = &(sb->buffer);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "BlendColorEXT called inside a Begin/End" );
         return;
     }
@@ -256,14 +256,14 @@
 
 #ifdef CR_EXT_blend_func_separate
-void STATE_APIENTRY crStateBlendFuncSeparateEXT( GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorA, GLenum dfactorA )
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sb = GetCurrentBits();
-    CRBufferBits *bb = &(sb->buffer);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "BlendFuncSeparateEXT called inside a Begin/End" );
+void STATE_APIENTRY crStateBlendFuncSeparateEXT(PCRStateTracker pState, GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorA, GLenum dfactorA )
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRBufferBits *bb = &(sb->buffer);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "BlendFuncSeparateEXT called inside a Begin/End" );
         return;
     }
@@ -293,5 +293,5 @@
         RT_FALL_THRU();
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid sfactorRGB passed to glBlendFuncSeparateEXT: %d", sfactorRGB);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid sfactorRGB passed to glBlendFuncSeparateEXT: %d", sfactorRGB);
             return;
     }
@@ -319,5 +319,5 @@
         RT_FALL_THRU();
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid sfactorA passed to glBlendFuncSeparateEXT: %d", sfactorA);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid sfactorA passed to glBlendFuncSeparateEXT: %d", sfactorA);
             return;
     }
@@ -347,5 +347,5 @@
         RT_FALL_THRU();
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid dfactorRGB passed to glBlendFuncSeparateEXT: %d", dfactorRGB);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid dfactorRGB passed to glBlendFuncSeparateEXT: %d", dfactorRGB);
             return;
     }
@@ -375,5 +375,5 @@
         RT_FALL_THRU();
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid dfactorA passed to glBlendFuncSeparateEXT: %d", dfactorA);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid dfactorA passed to glBlendFuncSeparateEXT: %d", dfactorA);
             return;
     }
@@ -388,14 +388,14 @@
 #endif
 
-void STATE_APIENTRY crStateBlendEquationEXT( GLenum mode )
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sb = GetCurrentBits();
+void STATE_APIENTRY crStateBlendEquationEXT(PCRStateTracker pState, GLenum mode )
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRBufferBits *bb = &(sb->buffer);
 
     if( g->current.inBeginEnd )
     {
-        crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "BlendEquationEXT called inside a Begin/End" );
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "BlendEquationEXT called inside a Begin/End" );
         return;
     }
@@ -419,5 +419,5 @@
 #endif /* defined(CR_EXT_blend_minmax) || defined(CR_EXT_blend_subtract) || defined(CR_EXT_blend_logic_op) */
         default:
-            crStateError( __LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                 "BlendEquationEXT: mode called with illegal parameter: 0x%x", (GLenum) mode );
             return;
@@ -427,14 +427,14 @@
 }
 
-void STATE_APIENTRY crStateLogicOp (GLenum opcode) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sb = GetCurrentBits();
-    CRBufferBits *bb = &(sb->buffer);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glLogicOp called in begin/end");
+void STATE_APIENTRY crStateLogicOp (PCRStateTracker pState, GLenum opcode) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRBufferBits *bb = &(sb->buffer);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glLogicOp called in begin/end");
         return;
     }
@@ -462,5 +462,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glLogicOp called with bogus opcode: %d", opcode);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glLogicOp called with bogus opcode: %d", opcode);
             return;
     }
@@ -472,14 +472,14 @@
 }
 
-void STATE_APIENTRY crStateDrawBuffer (GLenum mode) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sb = GetCurrentBits();
-    CRBufferBits *bb = &(sb->buffer);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDrawBuffer called in begin/end");
+void STATE_APIENTRY crStateDrawBuffer (PCRStateTracker pState, GLenum mode) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRBufferBits *bb = &(sb->buffer);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glDrawBuffer called in begin/end");
         return;
     }
@@ -506,5 +506,5 @@
             if (g->framebufferobject.drawFB)
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDrawBuffer invalid mode while fbo is active");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glDrawBuffer invalid mode while fbo is active");
                 return;
             }
@@ -515,5 +515,5 @@
                 if (!g->framebufferobject.drawFB)
                 {
-                    crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDrawBuffer invalid mode while fbo is inactive");
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glDrawBuffer invalid mode while fbo is inactive");
                     return;
                 }
@@ -521,5 +521,5 @@
             else
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glDrawBuffer called with bogus mode: %d", mode);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glDrawBuffer called with bogus mode: %d", mode);
                 return;
             }
@@ -538,14 +538,14 @@
 }
 
-void STATE_APIENTRY crStateReadBuffer (GLenum mode) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sb = GetCurrentBits();
-    CRBufferBits *bb = &(sb->buffer);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer called in begin/end");
+void STATE_APIENTRY crStateReadBuffer (PCRStateTracker pState, GLenum mode) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRBufferBits *bb = &(sb->buffer);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer called in begin/end");
         return;
     }
@@ -572,5 +572,5 @@
             if (g->framebufferobject.readFB)
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer invalid mode while fbo is active");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer invalid mode while fbo is active");
                 return;
             }
@@ -581,5 +581,5 @@
                 if (!g->framebufferobject.readFB)
                 {
-                    crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer invalid mode while fbo is inactive");
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer invalid mode while fbo is inactive");
                     return;
                 }
@@ -591,5 +591,5 @@
             else
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glReadBuffer called with bogus mode: %d", mode);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glReadBuffer called with bogus mode: %d", mode);
                 return;
             }
@@ -608,14 +608,14 @@
 }
 
-void STATE_APIENTRY crStateIndexMask (GLuint mask) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sp = GetCurrentBits();
+void STATE_APIENTRY crStateIndexMask (PCRStateTracker pState, GLuint mask) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sp = GetCurrentBits(pState);
     CRBufferBits *bb = &(sp->buffer);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer called in begin/end");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer called in begin/end");
         return;
     }
@@ -628,14 +628,14 @@
 }
 
-void STATE_APIENTRY crStateColorMask (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sp = GetCurrentBits();
+void STATE_APIENTRY crStateColorMask (PCRStateTracker pState, GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sp = GetCurrentBits(pState);
     CRBufferBits *bb = &(sp->buffer);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer called in begin/end");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glReadBuffer called in begin/end");
         return;
     }
@@ -651,14 +651,14 @@
 }
 
-void STATE_APIENTRY crStateClearColor (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sp = GetCurrentBits();
+void STATE_APIENTRY crStateClearColor (PCRStateTracker pState, GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sp = GetCurrentBits(pState);
     CRBufferBits *bb = &(sp->buffer);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glClearColor called in begin/end");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glClearColor called in begin/end");
         return;
     }
@@ -683,14 +683,14 @@
 }
 
-void STATE_APIENTRY crStateClearIndex (GLfloat c) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sp = GetCurrentBits();
+void STATE_APIENTRY crStateClearIndex (PCRStateTracker pState, GLfloat c) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sp = GetCurrentBits(pState);
     CRBufferBits *bb = &(sp->buffer);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glClearIndex called in begin/end");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glClearIndex called in begin/end");
         return;
     }
@@ -701,14 +701,14 @@
 }
 
-void STATE_APIENTRY crStateClearDepth (GLclampd depth) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sp = GetCurrentBits();
+void STATE_APIENTRY crStateClearDepth (PCRStateTracker pState, GLclampd depth) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sp = GetCurrentBits(pState);
     CRBufferBits *bb = &(sp->buffer);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glClearDepth called in begin/end");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glClearDepth called in begin/end");
         return;
     }
@@ -724,14 +724,14 @@
 }
 
-void STATE_APIENTRY crStateClearAccum (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) 
-{
-    CRContext *g = GetCurrentContext();
-    CRBufferState *b = &(g->buffer);
-    CRStateBits *sp = GetCurrentBits();
+void STATE_APIENTRY crStateClearAccum (PCRStateTracker pState, GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRBufferState *b = &(g->buffer);
+    CRStateBits *sp = GetCurrentBits(pState);
     CRBufferBits *bb = &(sp->buffer);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glClearAccum called in begin/end");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glClearAccum called in begin/end");
         return;
     }
@@ -756,14 +756,14 @@
 }
 
-void STATE_APIENTRY crStateDepthMask (GLboolean b) 
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateDepthMask (PCRStateTracker pState, GLboolean b) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRBufferState *bs = &(g->buffer);
-    CRStateBits *sp = GetCurrentBits();
+    CRStateBits *sp = GetCurrentBits(pState);
     CRBufferBits *bb = &(sp->buffer);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "DepthMask called in begin/end");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "DepthMask called in begin/end");
         return;
     }
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_bufferobject.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_bufferobject.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_bufferobject.c	(revision 78375)
@@ -28,13 +28,13 @@
 }
 
-void STATE_APIENTRY crStateGenBuffersARB(GLsizei n, GLuint *buffers)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGenBuffersARB(PCRStateTracker pState, GLsizei n, GLuint *buffers)
+{
+    CRContext *g = GetCurrentContext(pState);
     crStateGenNames(g, g->shared->buffersTable, n, buffers);
 }
 
-void crStateRegBuffers(GLsizei n, GLuint *buffers)
-{
-    CRContext *g = GetCurrentContext();
+void crStateRegBuffers(PCRStateTracker pState, GLsizei n, GLuint *buffers)
+{
+    CRContext *g = GetCurrentContext(pState);
     crStateRegNames(g, g->shared->buffersTable, n, buffers);
 }
@@ -61,7 +61,7 @@
 }
 
-GLboolean crStateIsBufferBound(GLenum target)
-{
-    CRContext *g = GetCurrentContext();
+GLboolean crStateIsBufferBound(PCRStateTracker pState, GLenum target)
+{
+    CRContext *g = GetCurrentContext(pState);
     return crStateIsBufferBoundForCtx(g, target);
 }
@@ -86,12 +86,12 @@
 }
 
-DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsBufferARB( GLuint buffer )
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsBufferARB(PCRStateTracker pState, GLuint buffer )
+{
+    CRContext *g = GetCurrentContext(pState);
 
     FLUSH();
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glIsBufferARB called in begin/end");
         return GL_FALSE;
@@ -103,5 +103,5 @@
 void crStateBufferObjectInit (CRContext *ctx)
 {
-    CRStateBits *sb          = GetCurrentBits();
+    CRStateBits *sb          = GetCurrentBits(ctx->pStateTracker);
     CRBufferObjectBits *bb = &sb->bufferobject;
     CRBufferObjectState *b = &ctx->bufferobject;
@@ -114,4 +114,6 @@
     RESET(bb->packBinding, ctx->bitid);
 #endif
+
+    b->pStateTracker = ctx->pStateTracker;
 
 #ifdef IN_GUEST
@@ -134,13 +136,14 @@
 }
 
-void crStateFreeBufferObject(void *data)
+void crStateFreeBufferObject(void *data, void *pvUser)
 {
     CRBufferObject *pObj = (CRBufferObject *)data;
+    PCRStateTracker pState = (PCRStateTracker)pvUser;
     if (pObj->data) crFree(pObj->data);
 
 #ifndef IN_GUEST
-    if (diff_api.DeleteBuffersARB)
-    {
-        diff_api.DeleteBuffersARB(1, &pObj->hwid);
+    if (pState->diff_api.DeleteBuffersARB)
+    {
+        pState->diff_api.DeleteBuffersARB(1, &pObj->hwid);
     }
 #endif
@@ -165,7 +168,7 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateBufferHWIDtoID(GLuint hwid)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLuint) STATE_APIENTRY crStateBufferHWIDtoID(PCRStateTracker pState, GLuint hwid)
+{
+    CRContext *g = GetCurrentContext(pState);
     crCheckIDHWID_t parms;
 
@@ -177,7 +180,7 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetBufferHWID(GLuint id)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetBufferHWID(PCRStateTracker pState, GLuint id)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRBufferObject *pObj = (CRBufferObject *) crHashtableSearch(g->shared->buffersTable, id);
 
@@ -186,14 +189,14 @@
 
 void STATE_APIENTRY
-crStateBindBufferARB (GLenum target, GLuint buffer)
-{
-    CRContext *g = GetCurrentContext();
+crStateBindBufferARB (PCRStateTracker pState, GLenum target, GLuint buffer)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRBufferObjectState *b = &(g->bufferobject);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRBufferObjectBits *bb = &(sb->bufferobject);
     CRBufferObject *oldObj, *newObj;
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glBindBufferARB called in begin/end");
         return;
@@ -205,5 +208,5 @@
     if (!oldObj)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glBindBufferARB(target)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glBindBufferARB(target)");
         return;
     }
@@ -219,5 +222,5 @@
             CRSTATE_CHECKERR(!newObj, GL_OUT_OF_MEMORY, "glBindBuffer");
 #ifndef IN_GUEST
-            diff_api.GenBuffersARB(1, &newObj->hwid);
+            pState->diff_api.GenBuffersARB(1, &newObj->hwid);
             if (!newObj->hwid)
             {
@@ -268,5 +271,5 @@
         /*we shouldn't reach this point*/
         CRASSERT(false);
-        crHashtableDelete(g->shared->buffersTable, (unsigned long) oldObj->id, crStateFreeBufferObject);
+        crHashtableDeleteEx(g->shared->buffersTable, (unsigned long) oldObj->id, crStateFreeBufferObject, pState);
     }
 
@@ -279,8 +282,8 @@
 }
 
-static void ctStateBuffersRefsCleanup(CRContext *ctx, CRBufferObject *obj, CRbitvalue *neg_bitid)
+static void ctStateBuffersRefsCleanup(PCRStateTracker pState, CRContext *ctx, CRBufferObject *obj, CRbitvalue *neg_bitid)
 {
     CRBufferObjectState *b = &(ctx->bufferobject);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRBufferObjectBits *bb = &(sb->bufferobject);
     int j, k;
@@ -347,7 +350,7 @@
 
 void STATE_APIENTRY
-crStateDeleteBuffersARB(GLsizei n, const GLuint *buffers)
-{
-    CRContext *g = GetCurrentContext();
+crStateDeleteBuffersARB(PCRStateTracker pState, GLsizei n, const GLuint *buffers)
+{
+    CRContext *g = GetCurrentContext(pState);
     int i;
 
@@ -355,5 +358,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glDeleteBuffersARB called in Begin/End");
         return;
@@ -361,5 +364,5 @@
 
     if (n < 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glDeleteBuffersARB(n < 0)");
         return;
@@ -373,5 +376,5 @@
                 int j;
 
-                ctStateBuffersRefsCleanup(g, obj, g->neg_bitid);
+                ctStateBuffersRefsCleanup(pState, g, obj, g->neg_bitid);
 
                 CR_STATE_SHAREDOBJ_USAGE_FOREACH_USED_IDX(obj, j)
@@ -381,9 +384,9 @@
                      * This is why g_pAvailableContexts[j] could be NULL
                      * also g_pAvailableContexts[0] will hold default context, which we should discard */
-                    CRContext *ctx = g_pAvailableContexts[j];
+                    CRContext *ctx = pState->apAvailableContexts[j];
                     if (j && ctx)
                     {
-                        ctStateBuffersRefsCleanup(ctx, obj, g->neg_bitid); /* <- yes, use g->neg_bitid, i.e. neg_bitid of the current context to ensure others bits get dirtified,
-                                                                            * but not the current context ones*/
+                        ctStateBuffersRefsCleanup(pState, ctx, obj, g->neg_bitid); /* <- yes, use g->neg_bitid, i.e. neg_bitid of the current context to ensure others bits get dirtified,
+                                                                                    * but not the current context ones*/
                     }
                     else
@@ -391,5 +394,5 @@
                 }
 
-                crHashtableDelete(g->shared->buffersTable, buffers[i], crStateFreeBufferObject);
+                crHashtableDeleteEx(g->shared->buffersTable, buffers[i], crStateFreeBufferObject, pState);
             }
         }
@@ -398,10 +401,10 @@
 
 void STATE_APIENTRY
-crStateBufferDataARB(GLenum target, GLsizeiptrARB size, const GLvoid * data, GLenum usage)
-{
-    CRContext *g = GetCurrentContext();
+crStateBufferDataARB(PCRStateTracker pState, GLenum target, GLsizeiptrARB size, const GLvoid * data, GLenum usage)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRBufferObjectState *b = &g->bufferobject;
     CRBufferObject *obj;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRBufferObjectBits *bb = &sb->bufferobject;
 
@@ -409,5 +412,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glBufferDataARB called in begin/end");
         return;
@@ -415,5 +418,5 @@
 
     if (size < 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glBufferDataARB(size < 0)");
         return;
@@ -433,5 +436,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glBufferDataARB(usage)");
             return;
@@ -441,15 +444,15 @@
     if (!obj)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glBufferDataARB(target)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glBufferDataARB(target)");
         return;
     }
 
     if (obj->id == 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glBufferDataARB");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glBufferDataARB");
         return;
     }
 
     if (obj->pointer) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glBufferDataARB(buffer is mapped)");
         return;
@@ -469,5 +472,5 @@
         obj->data = crAlloc(size);
         if (!obj->data) {
-            crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glBufferDataARB");
+            crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY, "glBufferDataARB");
             return;
         }
@@ -484,10 +487,10 @@
 
 void STATE_APIENTRY
-crStateBufferSubDataARB(GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid * data)
-{
-    CRContext *g = GetCurrentContext();
+crStateBufferSubDataARB(PCRStateTracker pState, GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid * data)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRBufferObjectState *b = &g->bufferobject;
     CRBufferObject *obj;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRBufferObjectBits *bb = &sb->bufferobject;
 
@@ -495,5 +498,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glBufferSubDataARB called in begin/end");
         return;
@@ -503,10 +506,10 @@
     if (!obj)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glBufferSubDataARB(target)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glBufferSubDataARB(target)");
         return;
     }
 
     if (obj->id == 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glBufferSubDataARB");
         return;
@@ -514,5 +517,5 @@
 
     if (obj->pointer) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glBufferSubDataARB(buffer is mapped)");
         return;
@@ -520,5 +523,5 @@
 
     if (size < 0 || offset < 0 || (unsigned int)offset + size > obj->size) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glBufferSubDataARB(bad offset and/or size)");
         return;
@@ -540,7 +543,7 @@
 
 void STATE_APIENTRY
-crStateGetBufferSubDataARB(GLenum target, GLintptrARB offset, GLsizeiptrARB size, void * data)
-{
-    CRContext *g = GetCurrentContext();
+crStateGetBufferSubDataARB(PCRStateTracker pState, GLenum target, GLintptrARB offset, GLsizeiptrARB size, void * data)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRBufferObjectState *b = &g->bufferobject;
     CRBufferObject *obj;
@@ -549,5 +552,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetBufferSubDataARB called in begin/end");
         return;
@@ -557,10 +560,10 @@
     if (!obj)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetBufferSubDataARB(target)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetBufferSubDataARB(target)");
         return;
     }
 
     if (obj->id == 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetBufferSubDataARB");
         return;
@@ -568,5 +571,5 @@
 
     if (obj->pointer) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetBufferSubDataARB(buffer is mapped)");
         return;
@@ -574,5 +577,5 @@
 
     if (size < 0 || offset < 0 || (unsigned int)offset + size > obj->size) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetBufferSubDataARB(bad offset and/or size)");
         return;
@@ -586,7 +589,7 @@
 
 void * STATE_APIENTRY
-crStateMapBufferARB(GLenum target, GLenum access)
-{
-    CRContext *g = GetCurrentContext();
+crStateMapBufferARB(PCRStateTracker pState, GLenum target, GLenum access)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRBufferObjectState *b = &g->bufferobject;
     CRBufferObject *obj;
@@ -595,5 +598,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glMapBufferARB called in begin/end");
         return NULL;
@@ -603,10 +606,10 @@
     if (!obj)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMapBufferARB(target)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMapBufferARB(target)");
         return NULL;
     }
 
     if (obj->id == 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glMapBufferARB");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glMapBufferARB");
         return GL_FALSE;
     }
@@ -619,5 +622,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glMapBufferARB(access)");
             return NULL;
@@ -632,10 +635,10 @@
 
 GLboolean STATE_APIENTRY
-crStateUnmapBufferARB(GLenum target)
-{
-    CRContext *g = GetCurrentContext();
+crStateUnmapBufferARB(PCRStateTracker pState, GLenum target)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRBufferObjectState *b = &g->bufferobject;
     CRBufferObject *obj;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRBufferObjectBits *bb = &sb->bufferobject;
 
@@ -643,5 +646,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glUnmapBufferARB called in begin/end");
         return GL_FALSE;
@@ -651,15 +654,15 @@
     if (!obj)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glUnmapBufferARB(target)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glUnmapBufferARB(target)");
         return GL_FALSE;
     }
 
     if (obj->id == 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glUnmapBufferARB");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glUnmapBufferARB");
         return GL_FALSE;
     }
 
     if (!obj->pointer) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glUnmapBufferARB");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glUnmapBufferARB");
         return GL_FALSE;
     }
@@ -680,7 +683,7 @@
 
 void STATE_APIENTRY
-crStateGetBufferParameterivARB(GLenum target, GLenum pname, GLint *params)
-{
-    CRContext *g = GetCurrentContext();
+crStateGetBufferParameterivARB(PCRStateTracker pState, GLenum target, GLenum pname, GLint *params)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRBufferObjectState *b = &g->bufferobject;
     CRBufferObject *obj;
@@ -689,5 +692,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetBufferParameterivARB called in begin/end");
         return;
@@ -697,5 +700,5 @@
     if (!obj)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetBufferParameterivARB(target)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetBufferParameterivARB(target)");
         return;
     }
@@ -715,5 +718,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glGetBufferParameterivARB(pname)");
             return;
@@ -723,7 +726,7 @@
 
 void STATE_APIENTRY
-crStateGetBufferPointervARB(GLenum target, GLenum pname, GLvoid **params)
-{
-    CRContext *g = GetCurrentContext();
+crStateGetBufferPointervARB(PCRStateTracker pState, GLenum target, GLenum pname, GLvoid **params)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRBufferObjectState *b = &g->bufferobject;
     CRBufferObject *obj;
@@ -732,5 +735,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetBufferPointervARB called in begin/end");
         return;
@@ -740,10 +743,10 @@
     if (!obj)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetBufferPointervARB(target)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetBufferPointervARB(target)");
         return;
     }
 
     if (pname != GL_BUFFER_MAP_POINTER_ARB) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetBufferPointervARB(pname)");
         return;
     }
@@ -763,5 +766,5 @@
  */
 static GLboolean
-HaveBufferObjectExtension(void)
+HaveBufferObjectExtension(PCRStateTracker pState)
 {
     static GLint haveBufferObjectExt = -1;
@@ -772,10 +775,10 @@
          * GetString into the diff'ing table in order for this to really work.
          */
-        if (!diff_api.GetString) {
+        if (!pState->diff_api.GetString) {
             haveBufferObjectExt = 0;
             return 0;
         }
-        CRASSERT(diff_api.GetString);
-        ext = (const char *) diff_api.GetString(GL_EXTENSIONS);
+        CRASSERT(pState->diff_api.GetString);
+        ext = (const char *) pState->diff_api.GetString(GL_EXTENSIONS);
         if (crStrstr(ext, "GL_ARB_vertex_buffer_object") ||
                 crStrstr(ext, "GL_ARB_pixel_buffer_object")) {
@@ -793,7 +796,10 @@
                                       GLboolean bSwitch)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRBufferObjectState *from = &(fromCtx->bufferobject);
     const CRBufferObjectState *to = &(toCtx->bufferobject);
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     /* ARRAY_BUFFER */
     if (CHECKDIRTY(bb->arrayBinding, bitID)) 
@@ -802,5 +808,5 @@
         {
             GLuint bufferID = to->arrayBuffer ? to->arrayBuffer->hwid : 0;
-            diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, bufferID);
+            pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, bufferID);
             if (bSwitch)
             {
@@ -825,13 +831,13 @@
         {
             /* update whole buffer */
-            diff_api.BufferDataARB(GL_ARRAY_BUFFER_ARB, bufObj->size,
-                                   bufObj->data, bufObj->usage);
+            pState->diff_api.BufferDataARB(GL_ARRAY_BUFFER_ARB, bufObj->size,
+                                           bufObj->data, bufObj->usage);
         }
         else 
         {
             /* update sub buffer */
-            diff_api.BufferSubDataARB(GL_ARRAY_BUFFER_ARB,
-                                      bufObj->dirtyStart, bufObj->dirtyLength,
-                                      (char *) bufObj->data + bufObj->dirtyStart);
+            pState->diff_api.BufferSubDataARB(GL_ARRAY_BUFFER_ARB,
+                                              bufObj->dirtyStart, bufObj->dirtyLength,
+                                              (char *) bufObj->data + bufObj->dirtyStart);
         }
         if (bSwitch) FILLDIRTY(bufObj->dirty);
@@ -845,5 +851,5 @@
         {
             GLuint bufferID = to->elementsBuffer ? to->elementsBuffer->hwid : 0;
-            diff_api.BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferID);
+            pState->diff_api.BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferID);
             if (bSwitch)
             {
@@ -868,13 +874,13 @@
         {
             /* update whole buffer */
-            diff_api.BufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufObj->size,
-                                   bufObj->data, bufObj->usage);
+            pState->diff_api.BufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufObj->size,
+                                           bufObj->data, bufObj->usage);
         }
         else 
         {
             /* update sub buffer */
-            diff_api.BufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
-                                      bufObj->dirtyStart, bufObj->dirtyLength,
-                                      (char *) bufObj->data + bufObj->dirtyStart);
+            pState->diff_api.BufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
+                                              bufObj->dirtyStart, bufObj->dirtyLength,
+                                              (char *) bufObj->data + bufObj->dirtyStart);
         }
         if (bSwitch) FILLDIRTY(bufObj->dirty);
@@ -889,5 +895,5 @@
         {
             GLuint bufferID = to->packBuffer ? to->packBuffer->hwid : 0;
-            diff_api.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, bufferID);
+            pState->diff_api.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, bufferID);
             if (bSwitch)
             {
@@ -912,13 +918,13 @@
         {
             /* update whole buffer */
-            diff_api.BufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, bufObj->size,
-                                   bufObj->data, bufObj->usage);
+            pState->diff_api.BufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, bufObj->size,
+                                           bufObj->data, bufObj->usage);
         }
         else 
         {
             /* update sub buffer */
-            diff_api.BufferSubDataARB(GL_PIXEL_PACK_BUFFER_ARB,
-                                      bufObj->dirtyStart, bufObj->dirtyLength,
-                                      (char *) bufObj->data + bufObj->dirtyStart);
+            pState->diff_api.BufferSubDataARB(GL_PIXEL_PACK_BUFFER_ARB,
+                                              bufObj->dirtyStart, bufObj->dirtyLength,
+                                              (char *) bufObj->data + bufObj->dirtyStart);
         }
         if (bSwitch) FILLDIRTY(bufObj->dirty);
@@ -932,5 +938,5 @@
         {
             GLuint bufferID = to->unpackBuffer ? to->unpackBuffer->hwid : 0;
-            diff_api.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, bufferID);
+            pState->diff_api.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, bufferID);
             if (bSwitch)
             {
@@ -955,13 +961,13 @@
         {
             /* update whole buffer */
-            diff_api.BufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, bufObj->size,
-                                   bufObj->data, bufObj->usage);
+            pState->diff_api.BufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, bufObj->size,
+                                           bufObj->data, bufObj->usage);
         }
         else 
         {
             /* update sub buffer */
-            diff_api.BufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB,
-                                      bufObj->dirtyStart, bufObj->dirtyLength,
-                                      (char *) bufObj->data + bufObj->dirtyStart);
+            pState->diff_api.BufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB,
+                                              bufObj->dirtyStart, bufObj->dirtyLength,
+                                              (char *) bufObj->data + bufObj->dirtyStart);
         }
         if (bSwitch) FILLDIRTY(bufObj->dirty);
@@ -974,8 +980,8 @@
                              CRContext *fromCtx, CRContext *toCtx)
 {
-    /*CRBufferObjectState *from = &(fromCtx->bufferobject); - unused
-    const CRBufferObjectState *to = &(toCtx->bufferobject); - unused */
-
-    if (!HaveBufferObjectExtension())
+    PCRStateTracker pState = fromCtx->pStateTracker;
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
+    if (!HaveBufferObjectExtension(pState))
         return;
 
@@ -991,5 +997,5 @@
     if (pBufferObj->id && !pBufferObj->hwid)
     {
-        diff_api.GenBuffersARB(1, &pBufferObj->hwid);
+        pState->pStateTracker->diff_api.GenBuffersARB(1, &pBufferObj->hwid);
         CRASSERT(pBufferObj->hwid);
     }
@@ -1004,6 +1010,6 @@
           hurt performance." 
          */
-        diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, pBufferObj->hwid);
-        diff_api.BufferDataARB(GL_ARRAY_BUFFER_ARB, pBufferObj->size, pBufferObj->data, pBufferObj->usage);
+        pState->pStateTracker->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, pBufferObj->hwid);
+        pState->pStateTracker->diff_api.BufferDataARB(GL_ARRAY_BUFFER_ARB, pBufferObj->size, pBufferObj->data, pBufferObj->usage);
 
         if (!pState->retainBufferData)
@@ -1021,9 +1027,11 @@
                                CRContext *fromCtx, CRContext *toCtx)
 {
-    /*const CRBufferObjectState *from = &(fromCtx->bufferobject); - unused */
     CRBufferObjectState *to = &(toCtx->bufferobject);
+    PCRStateTracker pState = fromCtx->pStateTracker;
     int i;
 
-    if (!HaveBufferObjectExtension())
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
+    if (!HaveBufferObjectExtension(pState))
         return;
 
@@ -1040,6 +1048,6 @@
         if (cp->buffer && (cp->buffer->id || locked))
         {
-            diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
-            diff_api.VertexPointer(cp->size, cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
+            pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
+            pState->diff_api.VertexPointer(cp->size, cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
         }
 
@@ -1047,6 +1055,6 @@
         if (cp->buffer && (cp->buffer->id || locked))
         {
-            diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
-            diff_api.ColorPointer(cp->size, cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
+            pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
+            pState->diff_api.ColorPointer(cp->size, cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
         }
 
@@ -1054,6 +1062,6 @@
         if (cp->buffer && (cp->buffer->id || locked))
         {
-            diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
-            diff_api.FogCoordPointerEXT(cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
+            pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
+            pState->diff_api.FogCoordPointerEXT(cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
         }
 
@@ -1061,6 +1069,6 @@
         if (cp->buffer && (cp->buffer->id || locked))
         {
-            diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
-            diff_api.SecondaryColorPointerEXT(cp->size, cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
+            pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
+            pState->diff_api.SecondaryColorPointerEXT(cp->size, cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
         }
 
@@ -1068,6 +1076,6 @@
         if (cp->buffer && (cp->buffer->id || locked))
         {
-            diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
-            diff_api.EdgeFlagPointer(cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
+            pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
+            pState->diff_api.EdgeFlagPointer(cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
         }
 
@@ -1075,6 +1083,6 @@
         if (cp->buffer && (cp->buffer->id || locked))
         {
-            diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
-            diff_api.IndexPointer(cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
+            pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
+            pState->diff_api.IndexPointer(cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
         }
 
@@ -1082,6 +1090,6 @@
         if (cp->buffer && (cp->buffer->id || locked))
         {
-            diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
-            diff_api.NormalPointer(cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
+            pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
+            pState->diff_api.NormalPointer(cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
         }
 
@@ -1091,13 +1099,13 @@
             if (cp->buffer && (cp->buffer->id || locked))
             {
-                if (diff_api.ActiveTextureARB)
-                    diff_api.ActiveTextureARB(i+GL_TEXTURE0_ARB);
-                diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
-                diff_api.TexCoordPointer(cp->size, cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
-            }
-        }
-
-        if (diff_api.ActiveTextureARB)
-            diff_api.ActiveTextureARB(toCtx->client.curClientTextureUnit+GL_TEXTURE0_ARB);
+                if (pState->diff_api.ActiveTextureARB)
+                    pState->diff_api.ActiveTextureARB(i+GL_TEXTURE0_ARB);
+                pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
+                pState->diff_api.TexCoordPointer(cp->size, cp->type, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
+            }
+        }
+
+        if (pState->diff_api.ActiveTextureARB)
+            pState->diff_api.ActiveTextureARB(toCtx->client.curClientTextureUnit+GL_TEXTURE0_ARB);
 
 #ifdef CR_NV_vertex_program
@@ -1107,14 +1115,14 @@
             if (cp->buffer && (cp->buffer->id || locked))
             {
-                diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
-                diff_api.VertexAttribPointerARB(i, cp->size, cp->type, cp->normalized, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
-            }
-        }
-#endif
-        diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, to->arrayBuffer->hwid);
-        diff_api.BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, to->elementsBuffer->hwid);
+                pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, cp->buffer->hwid);
+                pState->diff_api.VertexAttribPointerARB(i, cp->size, cp->type, cp->normalized, cp->stride, cp->p CRVBOX_HOST_ONLY_PARAM(-1 /*fRealPtr*/));
+            }
+        }
+#endif
+        pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, to->arrayBuffer->hwid);
+        pState->diff_api.BindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, to->elementsBuffer->hwid);
 #ifdef CR_ARB_pixel_buffer_object
-        diff_api.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, to->packBuffer->hwid);
-        diff_api.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, to->unpackBuffer->hwid);
+        pState->diff_api.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, to->packBuffer->hwid);
+        pState->diff_api.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, to->unpackBuffer->hwid);
 #endif
     }
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_client.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_client.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_client.c	(revision 78375)
@@ -376,5 +376,5 @@
  */
 
-void STATE_APIENTRY crStatePixelStoref (GLenum pname, GLfloat param)
+void STATE_APIENTRY crStatePixelStoref (PCRStateTracker pState, GLenum pname, GLfloat param)
 {
 
@@ -386,22 +386,22 @@
         case GL_UNPACK_SWAP_BYTES:
         case GL_UNPACK_LSB_FIRST:
-            crStatePixelStorei( pname, param == 0.0f ? 0: 1 );
+            crStatePixelStorei(pState, pname, param == 0.0f ? 0: 1 );
             break;
         default:
-            crStatePixelStorei( pname, (GLint) param );
-            break;
-    }
-}
-
-void STATE_APIENTRY crStatePixelStorei (GLenum pname, GLint param)
-{
-    CRContext *g    = GetCurrentContext();
+            crStatePixelStorei(pState, pname, (GLint) param );
+            break;
+    }
+}
+
+void STATE_APIENTRY crStatePixelStorei (PCRStateTracker pState, GLenum pname, GLint param)
+{
+    CRContext *g    = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "PixelStore{if} called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "PixelStore{if} called in Begin/End");
         return;
     }
@@ -421,5 +421,5 @@
             if (param < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative Row Length: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative Row Length: %f", param);
                 return;
             }
@@ -431,5 +431,5 @@
             if (param < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative Image Height: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative Image Height: %f", param);
                 return;
             }
@@ -441,5 +441,5 @@
             if (param < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative Skip Images: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative Skip Images: %f", param);
                 return;
             }
@@ -450,5 +450,5 @@
             if (param < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative Skip Pixels: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative Skip Pixels: %f", param);
                 return;
             }
@@ -459,5 +459,5 @@
             if (param < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative Row Skip: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative Row Skip: %f", param);
                 return;
             }
@@ -471,5 +471,5 @@
                     ((GLint) param) != 8) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Invalid Alignment: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Invalid Alignment: %f", param);
                 return;
             }
@@ -489,5 +489,5 @@
             if (param < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative Row Length: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative Row Length: %f", param);
                 return;
             }
@@ -499,5 +499,5 @@
             if (param < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative Image Height: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative Image Height: %f", param);
                 return;
             }
@@ -509,5 +509,5 @@
             if (param < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative Skip Images: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative Skip Images: %f", param);
                 return;
             }
@@ -518,5 +518,5 @@
             if (param < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative Skip Pixels: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative Skip Pixels: %f", param);
                 return;
             }
@@ -527,5 +527,5 @@
             if (param < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative Row Skip: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative Row Skip: %f", param);
                 return;
             }
@@ -539,5 +539,5 @@
                     ((GLint) param) != 8) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Invalid Alignment: %f", param);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Invalid Alignment: %f", param);
                 return;
             }
@@ -546,5 +546,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Unknown glPixelStore Pname: %d", pname);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Unknown glPixelStore Pname: %d", pname);
             return;
     }
@@ -553,8 +553,8 @@
 
 
-static void setClientState(CRClientState *c, CRClientBits *cb, 
-        CRbitvalue *neg_bitid, GLenum array, GLboolean state) 
-{
-    CRContext *g = GetCurrentContext();
+static void setClientState(CRContext *g, CRClientState *c, CRClientBits *cb, 
+                           CRbitvalue *neg_bitid, GLenum array, GLboolean state) 
+{
+    PCRStateTracker pState = g->pStateTracker;
 
     switch (array) 
@@ -612,5 +612,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid Enum passed to Enable/Disable Client State: SECONDARY_COLOR_ARRAY_EXT - EXT_secondary_color is not enabled." );
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid Enum passed to Enable/Disable Client State: SECONDARY_COLOR_ARRAY_EXT - EXT_secondary_color is not enabled." );
                 return;
             }
@@ -618,5 +618,5 @@
 #endif
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid Enum passed to Enable/Disable Client State: 0x%x", array );
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid Enum passed to Enable/Disable Client State: 0x%x", array );
             return;
     }
@@ -625,33 +625,33 @@
 }
 
-void STATE_APIENTRY crStateEnableClientState (GLenum array) 
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateEnableClientState (PCRStateTracker pState, GLenum array) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
     FLUSH();
 
-    setClientState(c, cb, g->neg_bitid, array, GL_TRUE);
-}
-
-void STATE_APIENTRY crStateDisableClientState (GLenum array) 
-{
-    CRContext *g = GetCurrentContext();
+    setClientState(g, c, cb, g->neg_bitid, array, GL_TRUE);
+}
+
+void STATE_APIENTRY crStateDisableClientState (PCRStateTracker pState, GLenum array) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
     FLUSH();
 
-    setClientState(c, cb, g->neg_bitid, array, GL_FALSE);
+    setClientState(g, c, cb, g->neg_bitid, array, GL_FALSE);
 }
 
 static void
-crStateClientSetPointer(CRClientPointer *cp, GLint size, GLenum type, GLboolean normalized, GLsizei stride,
+crStateClientSetPointer(CRContext *g, CRClientPointer *cp, GLint size, GLenum type, GLboolean normalized, GLsizei stride,
                         const GLvoid *pointer  CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
 {
-    CRContext *g = GetCurrentContext();
+    PCRStateTracker pState = g->pStateTracker;
 
 #ifdef CR_EXT_compiled_vertex_array
@@ -701,5 +701,5 @@
             break;
         default:
-            crStateError( __LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                         "Unknown type of vertex array: %d", type );
             return;
@@ -729,9 +729,9 @@
 }
 
-void STATE_APIENTRY crStateVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateVertexPointer(PCRStateTracker pState, GLint size, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
@@ -740,5 +740,5 @@
     if (size != 2 && size != 3 && size != 4)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glVertexPointer: invalid size: %d", size);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glVertexPointer: invalid size: %d", size);
         return;
     }
@@ -746,14 +746,14 @@
             type != GL_FLOAT && type != GL_DOUBLE)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glVertexPointer: invalid type: 0x%x", type);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glVertexPointer: invalid type: 0x%x", type);
         return;
     }
     if (stride < 0) 
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glVertexPointer: stride was negative: %d", stride);
-        return;
-    }
-
-    crStateClientSetPointer(&(c->array.v), size, type, GL_FALSE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glVertexPointer: stride was negative: %d", stride);
+        return;
+    }
+
+    crStateClientSetPointer(g, &(c->array.v), size, type, GL_FALSE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
     DIRTY(cb->dirty, g->neg_bitid);
     DIRTY(cb->clientPointer, g->neg_bitid);
@@ -761,9 +761,9 @@
 }
 
-void STATE_APIENTRY crStateColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateColorPointer(PCRStateTracker pState, GLint size, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
@@ -772,5 +772,5 @@
     if (size != 3 && size != 4)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glColorPointer: invalid size: %d", size);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glColorPointer: invalid size: %d", size);
         return;
     }
@@ -780,14 +780,14 @@
             type != GL_FLOAT && type != GL_DOUBLE)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glColorPointer: invalid type: 0x%x", type);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glColorPointer: invalid type: 0x%x", type);
         return;
     }
     if (stride < 0) 
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glColorPointer: stride was negative: %d", stride);
-        return;
-    }
-
-    crStateClientSetPointer(&(c->array.c), size, type, GL_TRUE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glColorPointer: stride was negative: %d", stride);
+        return;
+    }
+
+    crStateClientSetPointer(g, &(c->array.c), size, type, GL_TRUE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
     DIRTY(cb->dirty, g->neg_bitid);
     DIRTY(cb->clientPointer, g->neg_bitid);
@@ -795,9 +795,9 @@
 }
 
-void STATE_APIENTRY crStateSecondaryColorPointerEXT(GLint size, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateSecondaryColorPointerEXT(PCRStateTracker pState, GLint size, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
@@ -821,5 +821,5 @@
     if ((size != 3) && (size != 4))
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glSecondaryColorPointerEXT: invalid size: %d", size);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glSecondaryColorPointerEXT: invalid size: %d", size);
         return;
     }
@@ -829,14 +829,14 @@
             type != GL_FLOAT && type != GL_DOUBLE)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glSecondaryColorPointerEXT: invalid type: 0x%x", type);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glSecondaryColorPointerEXT: invalid type: 0x%x", type);
         return;
     }
     if (stride < 0) 
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glSecondaryColorPointerEXT: stride was negative: %d", stride);
-        return;
-    }
-
-    crStateClientSetPointer(&(c->array.s), size, type, GL_TRUE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glSecondaryColorPointerEXT: stride was negative: %d", stride);
+        return;
+    }
+
+    crStateClientSetPointer(g, &(c->array.s), size, type, GL_TRUE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
     DIRTY(cb->dirty, g->neg_bitid);
     DIRTY(cb->clientPointer, g->neg_bitid);
@@ -844,9 +844,9 @@
 }
 
-void STATE_APIENTRY crStateIndexPointer(GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateIndexPointer(PCRStateTracker pState, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
@@ -856,14 +856,14 @@
             type != GL_FLOAT && type != GL_DOUBLE)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glIndexPointer: invalid type: 0x%x", type);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glIndexPointer: invalid type: 0x%x", type);
         return;
     }
     if (stride < 0) 
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glIndexPointer: stride was negative: %d", stride);
-        return;
-    }
-
-    crStateClientSetPointer(&(c->array.i), 1, type, GL_TRUE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glIndexPointer: stride was negative: %d", stride);
+        return;
+    }
+
+    crStateClientSetPointer(g, &(c->array.i), 1, type, GL_TRUE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
     DIRTY(cb->dirty, g->neg_bitid);
     DIRTY(cb->clientPointer, g->neg_bitid);
@@ -871,9 +871,9 @@
 }
 
-void STATE_APIENTRY crStateNormalPointer(GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateNormalPointer(PCRStateTracker pState, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
@@ -884,14 +884,14 @@
             type != GL_DOUBLE)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glNormalPointer: invalid type: 0x%x", type);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glNormalPointer: invalid type: 0x%x", type);
         return;
     }
     if (stride < 0) 
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glNormalPointer: stride was negative: %d", stride);
-        return;
-    }
-
-    crStateClientSetPointer(&(c->array.n), 3, type, GL_TRUE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glNormalPointer: stride was negative: %d", stride);
+        return;
+    }
+
+    crStateClientSetPointer(g, &(c->array.n), 3, type, GL_TRUE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
     DIRTY(cb->dirty, g->neg_bitid);
     DIRTY(cb->clientPointer, g->neg_bitid);
@@ -899,9 +899,9 @@
 }
 
-void STATE_APIENTRY crStateTexCoordPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateTexCoordPointer(PCRStateTracker pState, GLint size, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
@@ -910,5 +910,5 @@
     if (size != 1 && size != 2 && size != 3 && size != 4)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glTexCoordPointer: invalid size: %d", size);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glTexCoordPointer: invalid size: %d", size);
         return;
     }
@@ -916,14 +916,14 @@
             type != GL_FLOAT && type != GL_DOUBLE)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glTexCoordPointer: invalid type: 0x%x", type);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glTexCoordPointer: invalid type: 0x%x", type);
         return;
     }
     if (stride < 0) 
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glTexCoordPointer: stride was negative: %d", stride);
-        return;
-    }
-
-    crStateClientSetPointer(&(c->array.t[c->curClientTextureUnit]), size, type, GL_FALSE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glTexCoordPointer: stride was negative: %d", stride);
+        return;
+    }
+
+    crStateClientSetPointer(g, &(c->array.t[c->curClientTextureUnit]), size, type, GL_FALSE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
     DIRTY(cb->dirty, g->neg_bitid);
     DIRTY(cb->clientPointer, g->neg_bitid);
@@ -931,9 +931,9 @@
 }
 
-void STATE_APIENTRY crStateEdgeFlagPointer(GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateEdgeFlagPointer(PCRStateTracker pState, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
@@ -942,9 +942,9 @@
     if (stride < 0) 
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glTexCoordPointer: stride was negative: %d", stride);
-        return;
-    }
-
-    crStateClientSetPointer(&(c->array.e), 1, GL_UNSIGNED_BYTE, GL_FALSE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glTexCoordPointer: stride was negative: %d", stride);
+        return;
+    }
+
+    crStateClientSetPointer(g, &(c->array.e), 1, GL_UNSIGNED_BYTE, GL_FALSE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
     DIRTY(cb->dirty, g->neg_bitid);
     DIRTY(cb->clientPointer, g->neg_bitid);
@@ -952,9 +952,9 @@
 }
 
-void STATE_APIENTRY crStateFogCoordPointerEXT(GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateFogCoordPointerEXT(PCRStateTracker pState, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
@@ -966,14 +966,14 @@
             type != GL_FLOAT && type != GL_DOUBLE)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glFogCoordPointerEXT: invalid type: 0x%x", type);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glFogCoordPointerEXT: invalid type: 0x%x", type);
         return;
     }
     if (stride < 0) 
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glFogCoordPointerEXT: stride was negative: %d", stride);
-        return;
-    }
-
-    crStateClientSetPointer(&(c->array.f), 1, type, GL_FALSE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glFogCoordPointerEXT: stride was negative: %d", stride);
+        return;
+    }
+
+    crStateClientSetPointer(g, &(c->array.f), 1, type, GL_FALSE, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
     DIRTY(cb->dirty, g->neg_bitid);
     DIRTY(cb->clientPointer, g->neg_bitid);
@@ -982,5 +982,5 @@
 
 
-void STATE_APIENTRY crStateVertexAttribPointerNV(GLuint index, GLint size, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+void STATE_APIENTRY crStateVertexAttribPointerNV(PCRStateTracker pState, GLuint index, GLint size, GLenum type, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
 {
     GLboolean normalized = GL_FALSE;
@@ -988,18 +988,18 @@
     if (type != GL_UNSIGNED_BYTE && type != GL_SHORT &&
             type != GL_FLOAT && type != GL_DOUBLE) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glVertexAttribPointerNV: invalid type: 0x%x", type);
         return;
     }
-    crStateVertexAttribPointerARB(index, size, type, normalized, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
-}
-
-
-void STATE_APIENTRY crStateVertexAttribPointerARB(GLuint index, GLint size, GLenum type, GLboolean normalized,
+    crStateVertexAttribPointerARB(pState, index, size, type, normalized, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
+}
+
+
+void STATE_APIENTRY crStateVertexAttribPointerARB(PCRStateTracker pState, GLuint index, GLint size, GLenum type, GLboolean normalized,
                                                   GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
@@ -1008,10 +1008,10 @@
     if (index >= CR_MAX_VERTEX_ATTRIBS)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glVertexAttribPointerARB: invalid index: %d", (int) index);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glVertexAttribPointerARB: invalid index: %d", (int) index);
         return;
     }
     if (size != 1 && size != 2 && size != 3 && size != 4)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glVertexAttribPointerARB: invalid size: %d", size);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glVertexAttribPointerARB: invalid size: %d", size);
         return;
     }
@@ -1021,14 +1021,14 @@
             type != GL_FLOAT && type != GL_DOUBLE)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glVertexAttribPointerARB: invalid type: 0x%x", type);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glVertexAttribPointerARB: invalid type: 0x%x", type);
         return;
     }
     if (stride < 0) 
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glVertexAttribPointerARB: stride was negative: %d", stride);
-        return;
-    }
-
-    crStateClientSetPointer(&(c->array.a[index]), size, type, normalized, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glVertexAttribPointerARB: stride was negative: %d", stride);
+        return;
+    }
+
+    crStateClientSetPointer(g, &(c->array.a[index]), size, type, normalized, stride, p CRVBOX_HOST_ONLY_PARAM(fRealPtr));
     DIRTY(cb->dirty, g->neg_bitid);
     DIRTY(cb->clientPointer, g->neg_bitid);
@@ -1037,10 +1037,10 @@
 
 
-void STATE_APIENTRY crStateGetVertexAttribPointervNV(GLuint index, GLenum pname, GLvoid **pointer)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetVertexAttribPointervNV(PCRStateTracker pState, GLuint index, GLenum pname, GLvoid **pointer)
+{
+    CRContext *g = GetCurrentContext(pState);
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetVertexAttribPointervNV called in Begin/End");
         return;
@@ -1048,5 +1048,5 @@
 
     if (index >= CR_MAX_VERTEX_ATTRIBS) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glGetVertexAttribPointervNV(index)");
         return;
@@ -1054,5 +1054,5 @@
 
     if (pname != GL_ATTRIB_ARRAY_POINTER_NV) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetVertexAttribPointervNV(pname)");
         return;
@@ -1063,7 +1063,7 @@
 
 
-void STATE_APIENTRY crStateGetVertexAttribPointervARB(GLuint index, GLenum pname, GLvoid **pointer)
-{
-    crStateGetVertexAttribPointervNV(index, pname, pointer);
+void STATE_APIENTRY crStateGetVertexAttribPointervARB(PCRStateTracker pState, GLuint index, GLenum pname, GLvoid **pointer)
+{
+    crStateGetVertexAttribPointervNV(pState, index, pname, pointer);
 }
 
@@ -1076,9 +1076,9 @@
 ** lets me use the same glDrawArrays method.
 */
-void STATE_APIENTRY crStateInterleavedArrays(GLenum format, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateInterleavedArrays(PCRStateTracker pState, GLenum format, GLsizei stride, const GLvoid *p CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
     CRClientPointer *cp;
@@ -1091,5 +1091,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glInterleavedArrays called in begin/end");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glInterleavedArrays called in begin/end");
         return;
     }
@@ -1099,5 +1099,5 @@
     if (stride < 0)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glInterleavedArrays: stride < 0: %d", stride);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glInterleavedArrays: stride < 0: %d", stride);
         return;
     }
@@ -1121,5 +1121,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glInterleavedArrays: Unrecognized format: %d", format);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glInterleavedArrays: Unrecognized format: %d", format);
             return;
     }
@@ -1200,5 +1200,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glInterleavedArrays: Unrecognized format: %d", format);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glInterleavedArrays: Unrecognized format: %d", format);
             return;
     }
@@ -1250,5 +1250,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glInterleavedArrays: Unrecognized format: %d", format);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glInterleavedArrays: Unrecognized format: %d", format);
             return;
     }
@@ -1331,5 +1331,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glInterleavedArrays: Unrecognized format: %d", format);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glInterleavedArrays: Unrecognized format: %d", format);
             return;
     }
@@ -1383,5 +1383,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glInterleavedArrays: Unrecognized format: %d", format);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glInterleavedArrays: Unrecognized format: %d", format);
             return;
     }
@@ -1394,12 +1394,12 @@
 }
 
-void STATE_APIENTRY crStateGetPointerv(GLenum pname, GLvoid * * params) 
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetPointerv(PCRStateTracker pState, GLenum pname, GLvoid * * params) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                 "GetPointerv called in begin/end");
         return;
@@ -1437,5 +1437,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid Enum passed to glGetPointerv: SECONDARY_COLOR_ARRAY_EXT - EXT_secondary_color is not enabled." );
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid Enum passed to glGetPointerv: SECONDARY_COLOR_ARRAY_EXT - EXT_secondary_color is not enabled." );
                 return;
             }
@@ -1447,5 +1447,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                     "glGetPointerv: invalid pname: %d", pname);
             return;
@@ -1454,11 +1454,11 @@
 
 
-void STATE_APIENTRY crStatePushClientAttrib( GLbitfield mask )
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStatePushClientAttrib(PCRStateTracker pState, GLbitfield mask )
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glPushClientAttrib called in Begin/End");
         return;
@@ -1466,5 +1466,5 @@
 
     if (c->attribStackDepth == CR_MAX_CLIENT_ATTRIB_STACK_DEPTH - 1) {
-        crStateError(__LINE__, __FILE__, GL_STACK_OVERFLOW,
+        crStateError(pState, __LINE__, __FILE__, GL_STACK_OVERFLOW,
                                  "glPushClientAttrib called with a full stack!" );
         return;
@@ -1489,14 +1489,14 @@
 
 
-void STATE_APIENTRY crStatePopClientAttrib( void )
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStatePopClientAttrib(PCRStateTracker pState)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
     CRbitvalue mask;
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glPopClientAttrib called in Begin/End");
         return;
@@ -1504,5 +1504,5 @@
 
     if (c->attribStackDepth == 0) {
-        crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW,
+        crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW,
                                  "glPopClientAttrib called with an empty stack!" );
         return;
@@ -1551,7 +1551,7 @@
 }
 
-void STATE_APIENTRY crStateLockArraysEXT(GLint first, GLint count)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateLockArraysEXT(PCRStateTracker pState, GLint first, GLint count)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
     int i;
@@ -1583,7 +1583,7 @@
 }
 
-void STATE_APIENTRY crStateUnlockArraysEXT(void)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateUnlockArraysEXT(PCRStateTracker pState)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
     int i;
@@ -1606,16 +1606,17 @@
 }
 
-void STATE_APIENTRY crStateVertexArrayRangeNV(GLsizei length, const GLvoid *pointer CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
+void STATE_APIENTRY crStateVertexArrayRangeNV(PCRStateTracker pState, GLsizei length, const GLvoid *pointer CRVBOX_HOST_ONLY_PARAM(int fRealPtr))
 {
   /* XXX todo */
     crWarning("crStateVertexArrayRangeNV not implemented");
-    (void)length; (void)pointer CRVBOX_HOST_ONLY_PARAM((void)fRealPtr);
-}
-
-
-void STATE_APIENTRY crStateFlushVertexArrayRangeNV(void)
+    RT_NOREF(pState); (void)length; (void)pointer CRVBOX_HOST_ONLY_PARAM((void)fRealPtr);
+}
+
+
+void STATE_APIENTRY crStateFlushVertexArrayRangeNV(PCRStateTracker pState)
 {
   /* XXX todo */
     crWarning("crStateFlushVertexArrayRangeNV not implemented");
+    RT_NOREF(pState);
 }
 
@@ -1650,8 +1651,8 @@
  * (via GL_ARB_vertex_buffer_object).
  */
-GLboolean crStateUseServerArrays(void)
+GLboolean crStateUseServerArrays(PCRStateTracker pState)
 {
 #if defined(CR_ARB_vertex_buffer_object) && !defined(CR_NO_SERVER_ARRAYS)
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
     int i;
@@ -1761,8 +1762,8 @@
  */
 GLboolean
-crStateUseServerArrayElements(void)
+crStateUseServerArrayElements(PCRStateTracker pState)
 {
 #ifdef CR_ARB_vertex_buffer_object
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     if (g->bufferobject.elementsBuffer &&
             g->bufferobject.elementsBuffer->id > 0)
@@ -1781,4 +1782,5 @@
                                     CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRClientState *from = &(fromCtx->client);
     const CRClientState *to = &(toCtx->client);
@@ -1788,8 +1790,10 @@
     const GLint idHwInitialBuffer = idHwArrayBuffer;
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
 #ifdef DEBUG_misha
     {
         GLint tstHwBuffer = -1;
-        diff_api.GetIntegerv(GL_ARRAY_BUFFER_BINDING, &tstHwBuffer);
+        pState->diff_api.GetIntegerv(GL_ARRAY_BUFFER_BINDING, &tstHwBuffer);
         CRASSERT(idHwInitialBuffer == tstHwBuffer);
     }
@@ -1808,8 +1812,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.VertexPointer(to->array.v.size, to->array.v.type, to->array.v.stride, to->array.v.p CRVBOX_HOST_ONLY_PARAM(to->array.v.fRealPtr));
+                pState->diff_api.VertexPointer(to->array.v.size, to->array.v.type, to->array.v.stride, to->array.v.p CRVBOX_HOST_ONLY_PARAM(to->array.v.fRealPtr));
                 from->array.v.size = to->array.v.size;
                 from->array.v.type = to->array.v.type;
@@ -1829,8 +1833,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.NormalPointer(to->array.n.type, to->array.n.stride, to->array.n.p CRVBOX_HOST_ONLY_PARAM(to->array.n.fRealPtr));
+                pState->diff_api.NormalPointer(to->array.n.type, to->array.n.stride, to->array.n.p CRVBOX_HOST_ONLY_PARAM(to->array.n.fRealPtr));
                 from->array.n.type = to->array.n.type;
                 from->array.n.stride = to->array.n.stride;
@@ -1850,8 +1854,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.ColorPointer(to->array.c.size, to->array.c.type, to->array.c.stride, to->array.c.p CRVBOX_HOST_ONLY_PARAM(to->array.c.fRealPtr));
+                pState->diff_api.ColorPointer(to->array.c.size, to->array.c.type, to->array.c.stride, to->array.c.p CRVBOX_HOST_ONLY_PARAM(to->array.c.fRealPtr));
                 from->array.c.size = to->array.c.size;
                 from->array.c.type = to->array.c.type;
@@ -1871,8 +1875,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.IndexPointer(to->array.i.type, to->array.i.stride, to->array.i.p CRVBOX_HOST_ONLY_PARAM(to->array.i.fRealPtr));
+                pState->diff_api.IndexPointer(to->array.i.type, to->array.i.stride, to->array.i.p CRVBOX_HOST_ONLY_PARAM(to->array.i.fRealPtr));
                 from->array.i.type = to->array.i.type;
                 from->array.i.stride = to->array.i.stride;
@@ -1893,10 +1897,10 @@
                     if (idHwArrayBufferUsed != idHwArrayBuffer)
                     {
-                        diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                        pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                         idHwArrayBuffer = idHwArrayBufferUsed;
                     }
-                    diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + i);
+                    pState->diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + i);
                     curClientTextureUnit = i;
-                    diff_api.TexCoordPointer(to->array.t[i].size, to->array.t[i].type, to->array.t[i].stride, to->array.t[i].p CRVBOX_HOST_ONLY_PARAM(to->array.t[i].fRealPtr));
+                    pState->diff_api.TexCoordPointer(to->array.t[i].size, to->array.t[i].type, to->array.t[i].stride, to->array.t[i].p CRVBOX_HOST_ONLY_PARAM(to->array.t[i].fRealPtr));
                     from->array.t[i].size = to->array.t[i].size;
                     from->array.t[i].type = to->array.t[i].type;
@@ -1916,8 +1920,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.EdgeFlagPointer(to->array.e.stride, to->array.e.p CRVBOX_HOST_ONLY_PARAM(to->array.e.fRealPtr));
+                pState->diff_api.EdgeFlagPointer(to->array.e.stride, to->array.e.p CRVBOX_HOST_ONLY_PARAM(to->array.e.fRealPtr));
                 from->array.e.stride = to->array.e.stride;
                 from->array.e.p = to->array.e.p;
@@ -1936,8 +1940,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.SecondaryColorPointerEXT(to->array.s.size, to->array.s.type, to->array.s.stride, to->array.s.p CRVBOX_HOST_ONLY_PARAM(to->array.s.fRealPtr));
+                pState->diff_api.SecondaryColorPointerEXT(to->array.s.size, to->array.s.type, to->array.s.stride, to->array.s.p CRVBOX_HOST_ONLY_PARAM(to->array.s.fRealPtr));
                 from->array.s.size = to->array.s.size;
                 from->array.s.type = to->array.s.type;
@@ -1957,8 +1961,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.FogCoordPointerEXT(to->array.f.type, to->array.f.stride, to->array.f.p CRVBOX_HOST_ONLY_PARAM(to->array.f.fRealPtr));
+                pState->diff_api.FogCoordPointerEXT(to->array.f.type, to->array.f.stride, to->array.f.p CRVBOX_HOST_ONLY_PARAM(to->array.f.fRealPtr));
                 from->array.f.type = to->array.f.type;
                 from->array.f.stride = to->array.f.stride;
@@ -1981,9 +1985,9 @@
                     if (idHwArrayBufferUsed != idHwArrayBuffer)
                     {
-                        diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                        pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                         idHwArrayBuffer = idHwArrayBufferUsed;
                     }
-                    diff_api.VertexAttribPointerARB(i, to->array.a[i].size, to->array.a[i].type, to->array.a[i].normalized,
-                                                    to->array.a[i].stride, to->array.a[i].p CRVBOX_HOST_ONLY_PARAM(to->array.a[i].fRealPtr));
+                    pState->diff_api.VertexAttribPointerARB(i, to->array.a[i].size, to->array.a[i].type, to->array.a[i].normalized,
+                                                            to->array.a[i].stride, to->array.a[i].p CRVBOX_HOST_ONLY_PARAM(to->array.a[i].fRealPtr));
                     from->array.a[i].size = to->array.a[i].size;
                     from->array.a[i].type = to->array.a[i].type;
@@ -2001,5 +2005,5 @@
     if (idHwArrayBuffer != idHwInitialBuffer)
     {
-        diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwInitialBuffer);
+        pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwInitialBuffer);
     }
 
@@ -2007,6 +2011,6 @@
         /* update vertex array enable/disable flags */
         glAble able[2];
-        able[0] = diff_api.DisableClientState;
-        able[1] = diff_api.EnableClientState;
+        able[0] = pState->diff_api.DisableClientState;
+        able[1] = pState->diff_api.EnableClientState;
         if (from->array.v.enabled != to->array.v.enabled) {
             able[to->array.v.enabled](GL_VERTEX_ARRAY);
@@ -2027,5 +2031,5 @@
         for (i = 0; (unsigned int)i < toCtx->limits.maxTextureUnits; i++) {
             if (from->array.t[i].enabled != to->array.t[i].enabled) {
-                diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + i);
+                pState->diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + i);
                 curClientTextureUnit = i;
                 able[to->array.t[i].enabled](GL_TEXTURE_COORD_ARRAY);
@@ -2048,7 +2052,7 @@
             if (from->array.a[i].enabled != to->array.a[i].enabled) {
                 if (to->array.a[i].enabled)
-                    diff_api.EnableVertexAttribArrayARB(i);
+                    pState->diff_api.EnableVertexAttribArrayARB(i);
                 else 
-                    diff_api.DisableVertexAttribArrayARB(i);
+                    pState->diff_api.DisableVertexAttribArrayARB(i);
                 from->array.a[i].enabled = to->array.a[i].enabled;
             }
@@ -2059,5 +2063,5 @@
     if (to->curClientTextureUnit != curClientTextureUnit)
     {
-        diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + to->curClientTextureUnit);
+        pState->diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + to->curClientTextureUnit);
     }
 }
@@ -2068,4 +2072,5 @@
                                         CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     const CRClientState *from = &(fromCtx->client);
     const CRClientState *to = &(toCtx->client);
@@ -2075,8 +2080,10 @@
     const GLint idHwInitialBuffer = idHwArrayBuffer;
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
 #ifdef DEBUG_misha
     {
         GLint tstHwBuffer = -1;
-        diff_api.GetIntegerv(GL_ARRAY_BUFFER_BINDING, &tstHwBuffer);
+        pState->diff_api.GetIntegerv(GL_ARRAY_BUFFER_BINDING, &tstHwBuffer);
         CRASSERT(idHwInitialBuffer == tstHwBuffer);
     }
@@ -2094,8 +2101,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.VertexPointer(to->array.v.size, to->array.v.type, to->array.v.stride, to->array.v.p CRVBOX_HOST_ONLY_PARAM(to->array.v.fRealPtr));
+                pState->diff_api.VertexPointer(to->array.v.size, to->array.v.type, to->array.v.stride, to->array.v.p CRVBOX_HOST_ONLY_PARAM(to->array.v.fRealPtr));
                 FILLDIRTY(cb->v);
                 FILLDIRTY(cb->clientPointer);
@@ -2113,8 +2120,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.NormalPointer(to->array.n.type, to->array.n.stride, to->array.n.p CRVBOX_HOST_ONLY_PARAM(to->array.n.fRealPtr));
+                pState->diff_api.NormalPointer(to->array.n.type, to->array.n.stride, to->array.n.p CRVBOX_HOST_ONLY_PARAM(to->array.n.fRealPtr));
                 FILLDIRTY(cb->n);
                 FILLDIRTY(cb->clientPointer);
@@ -2133,8 +2140,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.ColorPointer(to->array.c.size, to->array.c.type, to->array.c.stride, to->array.c.p CRVBOX_HOST_ONLY_PARAM(to->array.c.fRealPtr));
+                pState->diff_api.ColorPointer(to->array.c.size, to->array.c.type, to->array.c.stride, to->array.c.p CRVBOX_HOST_ONLY_PARAM(to->array.c.fRealPtr));
                 FILLDIRTY(cb->c);
                 FILLDIRTY(cb->clientPointer);
@@ -2152,8 +2159,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.IndexPointer(to->array.i.type, to->array.i.stride, to->array.i.p CRVBOX_HOST_ONLY_PARAM(to->array.i.fRealPtr));
+                pState->diff_api.IndexPointer(to->array.i.type, to->array.i.stride, to->array.i.p CRVBOX_HOST_ONLY_PARAM(to->array.i.fRealPtr));
                 FILLDIRTY(cb->i);
                 FILLDIRTY(cb->dirty);
@@ -2173,10 +2180,10 @@
                     if (idHwArrayBufferUsed != idHwArrayBuffer)
                     {
-                        diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                        pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                         idHwArrayBuffer = idHwArrayBufferUsed;
                     }
-                    diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + i);
+                    pState->diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + i);
                     curClientTextureUnit = i;
-                    diff_api.TexCoordPointer(to->array.t[i].size, to->array.t[i].type, to->array.t[i].stride, to->array.t[i].p CRVBOX_HOST_ONLY_PARAM(to->array.t[i].fRealPtr));
+                    pState->diff_api.TexCoordPointer(to->array.t[i].size, to->array.t[i].type, to->array.t[i].stride, to->array.t[i].p CRVBOX_HOST_ONLY_PARAM(to->array.t[i].fRealPtr));
                     FILLDIRTY(cb->t[i]);
                     FILLDIRTY(cb->clientPointer);
@@ -2194,8 +2201,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.EdgeFlagPointer(to->array.e.stride, to->array.e.p CRVBOX_HOST_ONLY_PARAM(to->array.e.fRealPtr));
+                pState->diff_api.EdgeFlagPointer(to->array.e.stride, to->array.e.p CRVBOX_HOST_ONLY_PARAM(to->array.e.fRealPtr));
                 FILLDIRTY(cb->e);
                 FILLDIRTY(cb->clientPointer);
@@ -2214,8 +2221,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.SecondaryColorPointerEXT(to->array.s.size, to->array.s.type, to->array.s.stride, to->array.s.p CRVBOX_HOST_ONLY_PARAM(to->array.s.fRealPtr));
+                pState->diff_api.SecondaryColorPointerEXT(to->array.s.size, to->array.s.type, to->array.s.stride, to->array.s.p CRVBOX_HOST_ONLY_PARAM(to->array.s.fRealPtr));
                 FILLDIRTY(cb->s);
                 FILLDIRTY(cb->clientPointer);
@@ -2233,8 +2240,8 @@
                 if (idHwArrayBufferUsed != idHwArrayBuffer)
                 {
-                    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                     idHwArrayBuffer = idHwArrayBufferUsed;
                 }
-                diff_api.FogCoordPointerEXT(to->array.f.type, to->array.f.stride, to->array.f.p CRVBOX_HOST_ONLY_PARAM(to->array.f.fRealPtr));
+                pState->diff_api.FogCoordPointerEXT(to->array.f.type, to->array.f.stride, to->array.f.p CRVBOX_HOST_ONLY_PARAM(to->array.f.fRealPtr));
                 FILLDIRTY(cb->f);
                 FILLDIRTY(cb->clientPointer);
@@ -2256,9 +2263,9 @@
                     if (idHwArrayBufferUsed != idHwArrayBuffer)
                     {
-                        diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
+                        pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwArrayBufferUsed);
                         idHwArrayBuffer = idHwArrayBufferUsed;
                     }
-                    diff_api.VertexAttribPointerARB(i, to->array.a[i].size, to->array.a[i].type, to->array.a[i].normalized,
-                                                    to->array.a[i].stride, to->array.a[i].p  CRVBOX_HOST_ONLY_PARAM(to->array.a[i].fRealPtr));
+                    pState->diff_api.VertexAttribPointerARB(i, to->array.a[i].size, to->array.a[i].type, to->array.a[i].normalized,
+                                                            to->array.a[i].stride, to->array.a[i].p  CRVBOX_HOST_ONLY_PARAM(to->array.a[i].fRealPtr));
                     FILLDIRTY(cb->a[i]);
                     FILLDIRTY(cb->clientPointer);
@@ -2273,5 +2280,5 @@
     if (idHwArrayBuffer != idHwInitialBuffer)
     {
-        diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwInitialBuffer);
+        pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, idHwInitialBuffer);
     }
 
@@ -2279,6 +2286,6 @@
         /* update vertex array enable/disable flags */
         glAble able[2];
-        able[0] = diff_api.DisableClientState;
-        able[1] = diff_api.EnableClientState;
+        able[0] = pState->diff_api.DisableClientState;
+        able[1] = pState->diff_api.EnableClientState;
         if (from->array.v.enabled != to->array.v.enabled) {
             able[to->array.v.enabled](GL_VERTEX_ARRAY);
@@ -2303,5 +2310,5 @@
         for (i = 0; (unsigned int)i < toCtx->limits.maxTextureUnits; i++) {
             if (from->array.t[i].enabled != to->array.t[i].enabled) {
-                diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + i);
+                pState->diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + i);
                 curClientTextureUnit = i;
                 able[to->array.t[i].enabled](GL_TEXTURE_COORD_ARRAY);
@@ -2328,7 +2335,7 @@
             if (from->array.a[i].enabled != to->array.a[i].enabled) {
                 if (to->array.a[i].enabled)
-                    diff_api.EnableVertexAttribArrayARB(i);
+                    pState->diff_api.EnableVertexAttribArrayARB(i);
                 else
-                    diff_api.DisableVertexAttribArrayARB(i);
+                    pState->diff_api.DisableVertexAttribArrayARB(i);
                 FILLDIRTY(cb->enableClientState);
                 FILLDIRTY(cb->dirty);
@@ -2340,5 +2347,5 @@
     if (to->curClientTextureUnit != curClientTextureUnit)
     {
-        diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + to->curClientTextureUnit);
+        pState->diff_api.ClientActiveTextureARB(GL_TEXTURE0_ARB + to->curClientTextureUnit);
     }
 
@@ -2347,5 +2354,5 @@
         if (from->unpack.rowLength != to->unpack.rowLength)
         {
-            diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, to->unpack.rowLength);
+            pState->diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, to->unpack.rowLength);
             FILLDIRTY(cb->unpack);
             FILLDIRTY(cb->dirty);
@@ -2353,5 +2360,5 @@
         if (from->unpack.skipRows != to->unpack.skipRows)
         {
-            diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, to->unpack.skipRows);
+            pState->diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, to->unpack.skipRows);
             FILLDIRTY(cb->unpack);
             FILLDIRTY(cb->dirty);
@@ -2359,5 +2366,5 @@
         if (from->unpack.skipPixels != to->unpack.skipPixels)
         {
-            diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, to->unpack.skipPixels);
+            pState->diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, to->unpack.skipPixels);
             FILLDIRTY(cb->unpack);
             FILLDIRTY(cb->dirty);
@@ -2365,5 +2372,5 @@
         if (from->unpack.alignment != to->unpack.alignment)
         {
-            diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, to->unpack.alignment);
+            pState->diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, to->unpack.alignment);
             FILLDIRTY(cb->unpack);
             FILLDIRTY(cb->dirty);
@@ -2371,5 +2378,5 @@
         if (from->unpack.imageHeight != to->unpack.imageHeight)
         {
-            diff_api.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, to->unpack.imageHeight);
+            pState->diff_api.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, to->unpack.imageHeight);
             FILLDIRTY(cb->unpack);
             FILLDIRTY(cb->dirty);
@@ -2377,5 +2384,5 @@
         if (from->unpack.skipImages != to->unpack.skipImages)
         {
-            diff_api.PixelStorei(GL_UNPACK_SKIP_IMAGES, to->unpack.skipImages);
+            pState->diff_api.PixelStorei(GL_UNPACK_SKIP_IMAGES, to->unpack.skipImages);
             FILLDIRTY(cb->unpack);
             FILLDIRTY(cb->dirty);
@@ -2383,5 +2390,5 @@
         if (from->unpack.swapBytes != to->unpack.swapBytes)
         {
-            diff_api.PixelStorei(GL_UNPACK_SWAP_BYTES, to->unpack.swapBytes);
+            pState->diff_api.PixelStorei(GL_UNPACK_SWAP_BYTES, to->unpack.swapBytes);
             FILLDIRTY(cb->unpack);
             FILLDIRTY(cb->dirty);
@@ -2389,5 +2396,5 @@
         if (from->unpack.psLSBFirst != to->unpack.psLSBFirst)
         {
-            diff_api.PixelStorei(GL_UNPACK_LSB_FIRST, to->unpack.psLSBFirst);
+            pState->diff_api.PixelStorei(GL_UNPACK_LSB_FIRST, to->unpack.psLSBFirst);
             FILLDIRTY(cb->unpack);
             FILLDIRTY(cb->dirty);
@@ -2400,5 +2407,5 @@
         if (from->pack.rowLength != to->pack.rowLength)
         {
-            diff_api.PixelStorei(GL_PACK_ROW_LENGTH, to->pack.rowLength);
+            pState->diff_api.PixelStorei(GL_PACK_ROW_LENGTH, to->pack.rowLength);
             FILLDIRTY(cb->pack);
             FILLDIRTY(cb->dirty);
@@ -2406,5 +2413,5 @@
         if (from->pack.skipRows != to->pack.skipRows)
         {
-            diff_api.PixelStorei(GL_PACK_SKIP_ROWS, to->pack.skipRows);
+            pState->diff_api.PixelStorei(GL_PACK_SKIP_ROWS, to->pack.skipRows);
             FILLDIRTY(cb->pack);
             FILLDIRTY(cb->dirty);
@@ -2412,5 +2419,5 @@
         if (from->pack.skipPixels != to->pack.skipPixels)
         {
-            diff_api.PixelStorei(GL_PACK_SKIP_PIXELS, to->pack.skipPixels);
+            pState->diff_api.PixelStorei(GL_PACK_SKIP_PIXELS, to->pack.skipPixels);
             FILLDIRTY(cb->pack);
             FILLDIRTY(cb->dirty);
@@ -2418,5 +2425,5 @@
         if (from->pack.alignment != to->pack.alignment)
         {
-            diff_api.PixelStorei(GL_PACK_ALIGNMENT, to->pack.alignment);
+            pState->diff_api.PixelStorei(GL_PACK_ALIGNMENT, to->pack.alignment);
             FILLDIRTY(cb->pack);
             FILLDIRTY(cb->dirty);
@@ -2424,5 +2431,5 @@
         if (from->pack.imageHeight != to->pack.imageHeight)
         {
-            diff_api.PixelStorei(GL_PACK_IMAGE_HEIGHT, to->pack.imageHeight);
+            pState->diff_api.PixelStorei(GL_PACK_IMAGE_HEIGHT, to->pack.imageHeight);
             FILLDIRTY(cb->pack);
             FILLDIRTY(cb->dirty);
@@ -2430,5 +2437,5 @@
         if (from->pack.skipImages != to->pack.skipImages)
         {
-            diff_api.PixelStorei(GL_PACK_SKIP_IMAGES, to->pack.skipImages);
+            pState->diff_api.PixelStorei(GL_PACK_SKIP_IMAGES, to->pack.skipImages);
             FILLDIRTY(cb->pack);
             FILLDIRTY(cb->dirty);
@@ -2436,5 +2443,5 @@
         if (from->pack.swapBytes != to->pack.swapBytes)
         {
-            diff_api.PixelStorei(GL_PACK_SWAP_BYTES, to->pack.swapBytes);
+            pState->diff_api.PixelStorei(GL_PACK_SWAP_BYTES, to->pack.swapBytes);
             FILLDIRTY(cb->pack);
             FILLDIRTY(cb->dirty);
@@ -2442,5 +2449,5 @@
         if (from->pack.psLSBFirst != to->pack.psLSBFirst)
         {
-            diff_api.PixelStorei(GL_PACK_LSB_FIRST, to->pack.psLSBFirst);
+            pState->diff_api.PixelStorei(GL_PACK_LSB_FIRST, to->pack.psLSBFirst);
             FILLDIRTY(cb->pack);
             FILLDIRTY(cb->dirty);
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_current.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_current.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_current.c	(revision 78375)
@@ -19,5 +19,5 @@
 {
     CRCurrentState *c = &ctx->current;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
     CRCurrentBits *cb = &(sb->current);
     static const GLfloat default_normal[4]         = {0.0f, 0.0f, 1.0f, 1.0f};
@@ -70,19 +70,19 @@
 }
 
-void STATE_APIENTRY crStateColor3f( GLfloat r, GLfloat g, GLfloat b )
-{
-    crStateColor4f(r, g, b, 1.0F);
-}
-
-void STATE_APIENTRY crStateColor3fv( const GLfloat *color )
-{
-    crStateColor4f( color[0], color[1], color[2], 1.0F );
-}
-
-void STATE_APIENTRY crStateColor4f( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateColor3f(PCRStateTracker pState, GLfloat r, GLfloat g, GLfloat b )
+{
+    crStateColor4f(pState, r, g, b, 1.0F);
+}
+
+void STATE_APIENTRY crStateColor3fv(PCRStateTracker pState, const GLfloat *color )
+{
+    crStateColor4f(pState, color[0], color[1], color[2], 1.0F );
+}
+
+void STATE_APIENTRY crStateColor4f(PCRStateTracker pState, GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
+{
+    CRContext *g = GetCurrentContext(pState);
     CRCurrentState *c = &(g->current);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRCurrentBits *cb = &(sb->current);
 
@@ -98,7 +98,7 @@
 }
 
-void STATE_APIENTRY crStateColor4fv( const GLfloat *color )
-{
-    crStateColor4f( color[0], color[1], color[2], color[3] );
+void STATE_APIENTRY crStateColor4fv(PCRStateTracker pState, const GLfloat *color )
+{
+    crStateColor4f(pState, color[0], color[1], color[2], color[3] );
 }
 
@@ -118,12 +118,12 @@
 }
 
-void STATE_APIENTRY crStateBegin( GLenum mode )
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateBegin(PCRStateTracker pState, GLenum mode )
+{
+    CRContext *g = GetCurrentContext(pState);
     CRCurrentState *c = &(g->current);
 
     if (mode > GL_POLYGON)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Begin called with invalid mode: %d", mode);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Begin called with invalid mode: %d", mode);
         return;
     }
@@ -131,5 +131,5 @@
     if (c->inBeginEnd)
     {
-        crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "glBegin called inside Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glBegin called inside Begin/End");
         return;
     }
@@ -141,12 +141,12 @@
 }
 
-void STATE_APIENTRY crStateEnd( void )
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateEnd(PCRStateTracker pState)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRCurrentState *c = &(g->current);
 
     if (!c->inBeginEnd)
     {
-        crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "glEnd called outside Begin/End" );
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glEnd called outside Begin/End" );
         return;
     }
@@ -158,4 +158,5 @@
                                                      CRContext *fromCtx, CRContext *toCtx )
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     const CRCurrentState *from = &(fromCtx->current);
     const CRCurrentState *to = &(toCtx->current);
@@ -163,4 +164,6 @@
     unsigned int i, j;
     CRbitvalue nbitID[CR_MAX_BITARRAY];
+
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
 
     for (j=0;j<CR_MAX_BITARRAY;j++)
@@ -177,5 +180,5 @@
           if (toX != fromX || toY != fromY || toZ != fromZ) {
                 /* Use glWindowPos (which updates raster color) */
-                diff_api.WindowPos3fvARB(to->rasterAttrib[VERT_ATTRIB_POS]);
+                pState->diff_api.WindowPos3fvARB(to->rasterAttrib[VERT_ATTRIB_POS]);
               FILLDIRTY(c->rasterPos);
               FILLDIRTY(c->dirty);
@@ -195,5 +198,5 @@
     if (CHECKDIRTY(c->colorIndex, bitID)) {
         if (to->colorIndex != from->colorIndex) {
-            diff_api.Indexf(to->colorIndex);
+            pState->diff_api.Indexf(to->colorIndex);
             FILLDIRTY(c->colorIndex);
             FILLDIRTY(c->dirty);
@@ -204,5 +207,5 @@
     if (CHECKDIRTY(c->edgeFlag, bitID)) {
         if (to->edgeFlag != from->edgeFlag) {
-            diff_api.EdgeFlag(to->edgeFlag);
+            pState->diff_api.EdgeFlag(to->edgeFlag);
             FILLDIRTY(c->edgeFlag);
             FILLDIRTY(c->dirty);
@@ -223,5 +226,5 @@
                     && CHECKDIRTY(c->vertexAttrib[i], bitID)) {
                 if (COMPARE_VECTOR (from->vertexAttrib[i], to->vertexAttribPre[i])) {
-                    diff_api.VertexAttrib4fvARB(i, &(to->vertexAttrib[i][0]));
+                    pState->diff_api.VertexAttrib4fvARB(i, &(to->vertexAttrib[i][0]));
                     FILLDIRTY(c->vertexAttrib[i]);
                     FILLDIRTY(c->dirty);
@@ -242,5 +245,5 @@
         if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR0], bitID)) {
             if (COMPARE_COLOR(from->vertexAttrib[VERT_ATTRIB_COLOR0],to->vertexAttrib[VERT_ATTRIB_COLOR0])) {
-                diff_api.Color4fv ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_COLOR0]));
+                pState->diff_api.Color4fv ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_COLOR0]));
                 FILLDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR0]);
                 FILLDIRTY(c->dirty);
@@ -253,5 +256,5 @@
         if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR1], bitID)) {
             if (COMPARE_COLOR(from->vertexAttrib[VERT_ATTRIB_COLOR1],to->vertexAttrib[VERT_ATTRIB_COLOR1])) {
-                diff_api.SecondaryColor3fvEXT ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_COLOR1]));
+                pState->diff_api.SecondaryColor3fvEXT ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_COLOR1]));
                 FILLDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR1]);
                 FILLDIRTY(c->dirty);
@@ -265,5 +268,5 @@
         if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_FOG], bitID)) {
             if (from->vertexAttrib[VERT_ATTRIB_FOG][0] != to->vertexAttrib[VERT_ATTRIB_FOG][0] ) {
-                diff_api.FogCoordfvEXT ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_FOG][0] ));
+                pState->diff_api.FogCoordfvEXT ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_FOG][0] ));
                 FILLDIRTY(c->vertexAttrib[VERT_ATTRIB_FOG]);
                 FILLDIRTY(c->dirty);
@@ -275,5 +278,5 @@
         if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_NORMAL], bitID)) {
             if (COMPARE_VECTOR (from->vertexAttrib[VERT_ATTRIB_NORMAL], to->vertexAttrib[VERT_ATTRIB_NORMAL])) {
-                diff_api.Normal3fv ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_NORMAL][0]));
+                pState->diff_api.Normal3fv ((GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_NORMAL][0]));
                 FILLDIRTY(c->vertexAttrib[VERT_ATTRIB_NORMAL]);
                 FILLDIRTY(c->dirty);
@@ -285,5 +288,5 @@
             if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_TEX0 + i], bitID)) {
                 if (COMPARE_TEXCOORD (from->vertexAttrib[VERT_ATTRIB_TEX0 + i], to->vertexAttribPre[VERT_ATTRIB_TEX0 + i])) {
-                    diff_api.MultiTexCoord4fvARB (i+GL_TEXTURE0_ARB, (GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_TEX0+ i][0]));
+                    pState->diff_api.MultiTexCoord4fvARB (i+GL_TEXTURE0_ARB, (GLfloat *) &(to->vertexAttrib[VERT_ATTRIB_TEX0+ i][0]));
                     FILLDIRTY(c->vertexAttrib[VERT_ATTRIB_TEX0 + i]);
                     FILLDIRTY(c->dirty);
@@ -301,8 +304,11 @@
                     CRContext *fromCtx, CRContext *toCtx )
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRCurrentState *from = &(fromCtx->current);
     const CRCurrentState *to = &(toCtx->current);
     unsigned int i, j;
     CRbitvalue nbitID[CR_MAX_BITARRAY];
+
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
 
     for (j=0;j<CR_MAX_BITARRAY;j++)
@@ -320,5 +326,5 @@
             if (toX != fromX || toY != fromY || toZ != fromZ) {
                 /* Use glWindowPos (which updates raster color) */
-                diff_api.WindowPos3fvARB(to->rasterAttrib[VERT_ATTRIB_POS]);
+                pState->diff_api.WindowPos3fvARB(to->rasterAttrib[VERT_ATTRIB_POS]);
                 from->rasterAttrib[VERT_ATTRIB_POS][0] = toX;
                 from->rasterAttrib[VERT_ATTRIB_POS][1] = toY;
@@ -342,5 +348,5 @@
     if (CHECKDIRTY(c->edgeFlag, bitID)) {
         if (from->edgeFlag != to->edgeFlagPre) {
-            diff_api.EdgeFlag (to->edgeFlagPre);
+            pState->diff_api.EdgeFlag (to->edgeFlagPre);
         }
         from->edgeFlag = to->edgeFlag;
@@ -351,5 +357,5 @@
     if (CHECKDIRTY(c->colorIndex, bitID)) {
         if (from->colorIndex != to->colorIndexPre) {
-            diff_api.Indexf (to->colorIndex);
+            pState->diff_api.Indexf (to->colorIndex);
         }
         from->colorIndex = to->colorIndex;
@@ -370,5 +376,5 @@
                     && CHECKDIRTY(c->vertexAttrib[i], bitID)) {
                 if (COMPARE_VECTOR (from->vertexAttrib[i], to->vertexAttribPre[i])) {
-                    diff_api.VertexAttrib4fvARB(i, &(to->vertexAttribPre[i][0]));
+                    pState->diff_api.VertexAttrib4fvARB(i, &(to->vertexAttribPre[i][0]));
                 }
                 COPY_4V(from->vertexAttrib[i] , to->vertexAttrib[i]);
@@ -386,5 +392,5 @@
         if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR0], bitID)) {
             if (COMPARE_COLOR(from->vertexAttrib[VERT_ATTRIB_COLOR0],to->vertexAttribPre[VERT_ATTRIB_COLOR0])) {
-                diff_api.Color4fv ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_COLOR0]));
+                pState->diff_api.Color4fv ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_COLOR0]));
             }
             COPY_4V(from->vertexAttrib[VERT_ATTRIB_COLOR0] , to->vertexAttrib[VERT_ATTRIB_COLOR0]);
@@ -395,5 +401,5 @@
         if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_COLOR1], bitID)) {
             if (COMPARE_COLOR(from->vertexAttrib[VERT_ATTRIB_COLOR1],to->vertexAttribPre[VERT_ATTRIB_COLOR1])) {
-                diff_api.SecondaryColor3fvEXT ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_COLOR1]));
+                pState->diff_api.SecondaryColor3fvEXT ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_COLOR1]));
             }
             COPY_4V(from->vertexAttrib[VERT_ATTRIB_COLOR1] , to->vertexAttrib[VERT_ATTRIB_COLOR1]);
@@ -405,5 +411,5 @@
         if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_FOG], bitID)) {
             if (from->vertexAttrib[VERT_ATTRIB_FOG]  != to->vertexAttribPre[VERT_ATTRIB_FOG]) {
-                diff_api.FogCoordfvEXT ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_FOG]));
+                pState->diff_api.FogCoordfvEXT ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_FOG]));
             }
             COPY_4V(from->vertexAttrib[VERT_ATTRIB_FOG]  , to->vertexAttrib[VERT_ATTRIB_FOG]);
@@ -414,5 +420,5 @@
         if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_NORMAL], bitID)) {
             if (COMPARE_VECTOR (from->vertexAttrib[VERT_ATTRIB_NORMAL], to->vertexAttribPre[VERT_ATTRIB_NORMAL])) {
-                diff_api.Normal3fv ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_NORMAL]));
+                pState->diff_api.Normal3fv ((GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_NORMAL]));
             }
             COPY_4V(from->vertexAttrib[VERT_ATTRIB_NORMAL] , to->vertexAttrib[VERT_ATTRIB_NORMAL]);
@@ -424,5 +430,5 @@
             if (CHECKDIRTY(c->vertexAttrib[VERT_ATTRIB_TEX0 + i], bitID)) {
                 if (COMPARE_TEXCOORD (from->vertexAttrib[VERT_ATTRIB_TEX0 + i], to->vertexAttribPre[VERT_ATTRIB_TEX0 + i])) {
-                    diff_api.MultiTexCoord4fvARB (GL_TEXTURE0_ARB + i, (GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_TEX0 + i]));
+                    pState->diff_api.MultiTexCoord4fvARB (GL_TEXTURE0_ARB + i, (GLfloat *) &(to->vertexAttribPre[VERT_ATTRIB_TEX0 + i]));
                 }
                 COPY_4V(from->vertexAttrib[VERT_ATTRIB_TEX0 + i] , to->vertexAttrib[VERT_ATTRIB_TEX0 + i]);
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_current.py
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_current.py	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_current.py	(revision 78375)
@@ -79,11 +79,11 @@
 print('''
 
-void crStateCurrentRecover( void )
+void crStateCurrentRecover(PCRStateTracker pState)
 {
 	const unsigned char *v;
 	convert_func convert=NULL;
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRCurrentState *c = &(g->current);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRCurrentBits *cb = &(sb->current);
 	static const GLfloat color_default[4]			= {0.0f, 0.0f, 0.0f, 1.0f};
@@ -295,7 +295,7 @@
     const unsigned char *v;
     convert_func convert=NULL;
-    CRCurrentState *c = &(g->current);
-    CRStateBits *sb = GetCurrentBits();
-    CRCurrentBits *cb = &(sb->current);
+    CRCurrentState *c;
+    CRStateBits *sb;
+    CRCurrentBits *cb;
 
     static const GLfloat vertexAttrib_default[4] = {0.0f, 0.0f, 0.0f, 1.0f};
@@ -309,4 +309,8 @@
     if (!g || !current)
         return;
+
+    c = &(g->current);
+    sb = GetCurrentBits(g->pStateTracker);
+    cb = &(sb->current);
 
     /* silence warnings */
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_diff.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_diff.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_diff.c	(revision 78375)
@@ -14,6 +14,9 @@
 void crStateDiffContext( CRContext *from, CRContext *to )
 {
+    PCRStateTracker pState = from->pStateTracker;
 	CRbitvalue *bitID = from->bitid;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
+
+    CRASSERT(from->pStateTracker == to->pStateTracker);
 
 	/*crDebug( "Diffing two contexts!" ); */
@@ -143,20 +146,21 @@
 int crStateAcquireFBImage(CRContext *to, CRFBData *data)
 {
+    PCRStateTracker pState = to->pStateTracker;
     /*CRBufferState *pBuf = &to->buffer; - unused */
     CRPixelPackState packing = to->client.pack;
     uint32_t i;
 
-    diff_api.PixelStorei(GL_PACK_SKIP_ROWS, 0);
-    diff_api.PixelStorei(GL_PACK_SKIP_PIXELS, 0);
-    diff_api.PixelStorei(GL_PACK_ALIGNMENT, 1);
-    diff_api.PixelStorei(GL_PACK_ROW_LENGTH, 0);
-    diff_api.PixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
-    diff_api.PixelStorei(GL_PACK_SKIP_IMAGES, 0);
-    diff_api.PixelStorei(GL_PACK_SWAP_BYTES, 0);
-    diff_api.PixelStorei(GL_PACK_LSB_FIRST, 0);
+    pState->diff_api.PixelStorei(GL_PACK_SKIP_ROWS, 0);
+    pState->diff_api.PixelStorei(GL_PACK_SKIP_PIXELS, 0);
+    pState->diff_api.PixelStorei(GL_PACK_ALIGNMENT, 1);
+    pState->diff_api.PixelStorei(GL_PACK_ROW_LENGTH, 0);
+    pState->diff_api.PixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
+    pState->diff_api.PixelStorei(GL_PACK_SKIP_IMAGES, 0);
+    pState->diff_api.PixelStorei(GL_PACK_SWAP_BYTES, 0);
+    pState->diff_api.PixelStorei(GL_PACK_LSB_FIRST, 0);
 
     if (to->bufferobject.packBuffer->hwid>0)
     {
-        diff_api.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
+        pState->diff_api.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
     }
 
@@ -169,13 +173,13 @@
             if (!to->buffer.depthTest)
             {
-                diff_api.Enable(GL_DEPTH_TEST);
+                pState->diff_api.Enable(GL_DEPTH_TEST);
             }
             if (to->pixel.depthScale != 1.0f)
             {
-                diff_api.PixelTransferf (GL_DEPTH_SCALE, 1.0f);
+                pState->diff_api.PixelTransferf (GL_DEPTH_SCALE, 1.0f);
             }
             if (to->pixel.depthBias != 0.0f)
             {
-                diff_api.PixelTransferf (GL_DEPTH_BIAS, 0.0f);
+                pState->diff_api.PixelTransferf (GL_DEPTH_BIAS, 0.0f);
             }
         }
@@ -184,26 +188,26 @@
         	if (!to->stencil.stencilTest)
             {
-                diff_api.Enable(GL_STENCIL_TEST);
+                pState->diff_api.Enable(GL_STENCIL_TEST);
             }
             if (to->pixel.mapStencil)
             {
-                diff_api.PixelTransferi (GL_MAP_STENCIL, GL_FALSE);
+                pState->diff_api.PixelTransferi (GL_MAP_STENCIL, GL_FALSE);
             }
             if (to->pixel.indexOffset)
             {
-                diff_api.PixelTransferi (GL_INDEX_OFFSET, 0);
+                pState->diff_api.PixelTransferi (GL_INDEX_OFFSET, 0);
             }
             if (to->pixel.indexShift)
             {
-                diff_api.PixelTransferi (GL_INDEX_SHIFT, 0);
-            }
-        }
-
-        diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, el->idFBO);
+                pState->diff_api.PixelTransferi (GL_INDEX_SHIFT, 0);
+            }
+        }
+
+        pState->diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, el->idFBO);
 
         if (el->enmBuffer)
-            diff_api.ReadBuffer(el->enmBuffer);
-
-        diff_api.ReadPixels(el->posX, el->posY, el->width, el->height, el->enmFormat, el->enmType, el->pvData);
+            pState->diff_api.ReadBuffer(el->enmBuffer);
+
+        pState->diff_api.ReadPixels(el->posX, el->posY, el->width, el->height, el->enmFormat, el->enmType, el->pvData);
         crDebug("Acquired %d;%d;%d;%d;%d;0x%p fb image", el->enmBuffer, el->width, el->height, el->enmFormat, el->enmType, el->pvData);
 
@@ -212,13 +216,13 @@
             if (to->pixel.depthScale != 1.0f)
             {
-                diff_api.PixelTransferf (GL_DEPTH_SCALE, to->pixel.depthScale);
+                pState->diff_api.PixelTransferf (GL_DEPTH_SCALE, to->pixel.depthScale);
             }
             if (to->pixel.depthBias != 0.0f)
             {
-                diff_api.PixelTransferf (GL_DEPTH_BIAS, to->pixel.depthBias);
+                pState->diff_api.PixelTransferf (GL_DEPTH_BIAS, to->pixel.depthBias);
             }
             if (!to->buffer.depthTest)
             {
-                diff_api.Disable(GL_DEPTH_TEST);
+                pState->diff_api.Disable(GL_DEPTH_TEST);
             }
         }
@@ -227,17 +231,17 @@
             if (to->pixel.indexOffset)
             {
-                diff_api.PixelTransferi (GL_INDEX_OFFSET, to->pixel.indexOffset);
+                pState->diff_api.PixelTransferi (GL_INDEX_OFFSET, to->pixel.indexOffset);
             }
             if (to->pixel.indexShift)
             {
-                diff_api.PixelTransferi (GL_INDEX_SHIFT, to->pixel.indexShift);
+                pState->diff_api.PixelTransferi (GL_INDEX_SHIFT, to->pixel.indexShift);
             }
             if (to->pixel.mapStencil)
             {
-                diff_api.PixelTransferi (GL_MAP_STENCIL, GL_TRUE);
+                pState->diff_api.PixelTransferi (GL_MAP_STENCIL, GL_TRUE);
             }
             if (!to->stencil.stencilTest)
             {
-                diff_api.Disable(GL_STENCIL_TEST);
+                pState->diff_api.Disable(GL_STENCIL_TEST);
             }
         }
@@ -246,32 +250,32 @@
     if (to->bufferobject.packBuffer->hwid>0)
     {
-        diff_api.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, to->bufferobject.packBuffer->hwid);
+        pState->diff_api.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, to->bufferobject.packBuffer->hwid);
     }
     if (to->framebufferobject.readFB)
     {
         CRASSERT(to->framebufferobject.readFB->hwid);
-        diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, to->framebufferobject.readFB->hwid);
-        diff_api.ReadBuffer(to->framebufferobject.readFB->readbuffer);
+        pState->diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, to->framebufferobject.readFB->hwid);
+        pState->diff_api.ReadBuffer(to->framebufferobject.readFB->readbuffer);
 
     }
     else if (data->idOverrrideFBO)
     {
-        diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, data->idOverrrideFBO);
-        diff_api.ReadBuffer(GL_COLOR_ATTACHMENT0);
+        pState->diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, data->idOverrrideFBO);
+        pState->diff_api.ReadBuffer(GL_COLOR_ATTACHMENT0);
     }
     else
     {
-        diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
-        diff_api.ReadBuffer(to->buffer.readBuffer);
-    }
-
-    diff_api.PixelStorei(GL_PACK_SKIP_ROWS, packing.skipRows);
-    diff_api.PixelStorei(GL_PACK_SKIP_PIXELS, packing.skipPixels);
-    diff_api.PixelStorei(GL_PACK_ALIGNMENT, packing.alignment);
-    diff_api.PixelStorei(GL_PACK_ROW_LENGTH, packing.rowLength);
-    diff_api.PixelStorei(GL_PACK_IMAGE_HEIGHT, packing.imageHeight);
-    diff_api.PixelStorei(GL_PACK_SKIP_IMAGES, packing.skipImages);
-    diff_api.PixelStorei(GL_PACK_SWAP_BYTES, packing.swapBytes);
-    diff_api.PixelStorei(GL_PACK_LSB_FIRST, packing.psLSBFirst);
+        pState->diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
+        pState->diff_api.ReadBuffer(to->buffer.readBuffer);
+    }
+
+    pState->diff_api.PixelStorei(GL_PACK_SKIP_ROWS, packing.skipRows);
+    pState->diff_api.PixelStorei(GL_PACK_SKIP_PIXELS, packing.skipPixels);
+    pState->diff_api.PixelStorei(GL_PACK_ALIGNMENT, packing.alignment);
+    pState->diff_api.PixelStorei(GL_PACK_ROW_LENGTH, packing.rowLength);
+    pState->diff_api.PixelStorei(GL_PACK_IMAGE_HEIGHT, packing.imageHeight);
+    pState->diff_api.PixelStorei(GL_PACK_SKIP_IMAGES, packing.skipImages);
+    pState->diff_api.PixelStorei(GL_PACK_SWAP_BYTES, packing.swapBytes);
+    pState->diff_api.PixelStorei(GL_PACK_LSB_FIRST, packing.psLSBFirst);
     return VINF_SUCCESS;
 }
@@ -279,179 +283,180 @@
 void crStateApplyFBImage(CRContext *to, CRFBData *data)
 {
-    {
-        /*CRBufferState *pBuf = &to->buffer; - unused */
-        CRPixelPackState unpack = to->client.unpack;
-        uint32_t i;
-
-        diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
-        diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
-        diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
-        diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
-        diff_api.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
-        diff_api.PixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
-        diff_api.PixelStorei(GL_UNPACK_SWAP_BYTES, 0);
-        diff_api.PixelStorei(GL_UNPACK_LSB_FIRST, 0);
-
-        if (to->bufferobject.unpackBuffer->hwid>0)
-        {
-            diff_api.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
-        }
-
-        diff_api.Disable(GL_ALPHA_TEST);
-        diff_api.Disable(GL_SCISSOR_TEST);
-        diff_api.Disable(GL_BLEND);
-        diff_api.Disable(GL_COLOR_LOGIC_OP);
-        diff_api.Disable(GL_DEPTH_TEST);
-        diff_api.Disable(GL_STENCIL_TEST);
-
-        for (i = 0; i < data->cElements; ++i)
-        {
-            CRFBDataElement *el = &data->aElements[i];
+    PCRStateTracker pState = to->pStateTracker;
+    CRPixelPackState unpack = to->client.unpack;
+    uint32_t i;
+
+    pState->diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
+    pState->diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
+    pState->diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
+    pState->diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+    pState->diff_api.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
+    pState->diff_api.PixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
+    pState->diff_api.PixelStorei(GL_UNPACK_SWAP_BYTES, 0);
+    pState->diff_api.PixelStorei(GL_UNPACK_LSB_FIRST, 0);
+
+    if (to->bufferobject.unpackBuffer->hwid>0)
+    {
+        pState->diff_api.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
+    }
+
+    pState->diff_api.Disable(GL_ALPHA_TEST);
+    pState->diff_api.Disable(GL_SCISSOR_TEST);
+    pState->diff_api.Disable(GL_BLEND);
+    pState->diff_api.Disable(GL_COLOR_LOGIC_OP);
+    pState->diff_api.Disable(GL_DEPTH_TEST);
+    pState->diff_api.Disable(GL_STENCIL_TEST);
+
+    for (i = 0; i < data->cElements; ++i)
+    {
+        CRFBDataElement *el = &data->aElements[i];
 #if 0
-            char fname[200];
-            sprintf(fname, "./img_apply_%p_%d_%d.tga", to, i, el->enmFormat);
-            crDumpNamedTGA(fname, el->width, el->height, el->pvData);
-#endif
-
-            /* Before SSM version SHCROGL_SSM_VERSION_WITH_SEPARATE_DEPTH_STENCIL_BUFFERS
-             * saved state file contined invalid DEPTH/STENCIL data. In order to prevent
-             * crashes and improper guest App behavior, this data should be ignored. */
-            if (   data->u32Version < SHCROGL_SSM_VERSION_WITH_SEPARATE_DEPTH_STENCIL_BUFFERS
-                && (   el->enmFormat == GL_DEPTH_COMPONENT
-                    || el->enmFormat == GL_STENCIL_INDEX
-                    || el->enmFormat == GL_DEPTH_STENCIL))
-                continue;
-
-            if (el->enmFormat == GL_DEPTH_COMPONENT || el->enmFormat == GL_DEPTH_STENCIL)
-            {
-                diff_api.Enable(GL_DEPTH_TEST);
-                if (to->pixel.depthScale != 1.0f)
-                {
-                    diff_api.PixelTransferf (GL_DEPTH_SCALE, 1.0f);
-                }
-                if (to->pixel.depthBias != 0.0f)
-                {
-                    diff_api.PixelTransferf (GL_DEPTH_BIAS, 0.0f);
-                }
-            }
-            if (el->enmFormat == GL_STENCIL_INDEX || el->enmFormat == GL_DEPTH_STENCIL)
-            {
-                diff_api.Enable(GL_STENCIL_TEST);
-                if (to->pixel.mapStencil)
-                {
-                    diff_api.PixelTransferi (GL_MAP_STENCIL, GL_FALSE);
-                }
-                if (to->pixel.indexOffset)
-                {
-                    diff_api.PixelTransferi (GL_INDEX_OFFSET, 0);
-                }
-                if (to->pixel.indexShift)
-                {
-                    diff_api.PixelTransferi (GL_INDEX_SHIFT, 0);
-                }
-            }
-
-            diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, el->idFBO);
-
-            if (el->enmBuffer)
-                diff_api.DrawBuffer(el->enmBuffer);
-
-            diff_api.WindowPos2iARB(el->posX, el->posY);
-            diff_api.DrawPixels(el->width, el->height, el->enmFormat, el->enmType, el->pvData);
-            crDebug("Applied %d;%d;%d;%d;%d;0x%p fb image", el->enmBuffer, el->width, el->height, el->enmFormat, el->enmType, el->pvData);
-
-            if (el->enmFormat == GL_DEPTH_COMPONENT || el->enmFormat == GL_DEPTH_STENCIL)
-            {
-                if (to->pixel.depthScale != 1.0f)
-                {
-                    diff_api.PixelTransferf (GL_DEPTH_SCALE, to->pixel.depthScale);
-                }
-                if (to->pixel.depthBias != 0.0f)
-                {
-                    diff_api.PixelTransferf (GL_DEPTH_BIAS, to->pixel.depthBias);
-                }
-                diff_api.Disable(GL_DEPTH_TEST);
-            }
-            if (el->enmFormat == GL_STENCIL_INDEX || el->enmFormat == GL_DEPTH_STENCIL)
-            {
-                if (to->pixel.indexOffset)
-                {
-                    diff_api.PixelTransferi (GL_INDEX_OFFSET, to->pixel.indexOffset);
-                }
-                if (to->pixel.indexShift)
-                {
-                    diff_api.PixelTransferi (GL_INDEX_SHIFT, to->pixel.indexShift);
-                }
-                if (to->pixel.mapStencil)
-                {
-                    diff_api.PixelTransferi (GL_MAP_STENCIL, GL_TRUE);
-                }
-                diff_api.Disable(GL_STENCIL_TEST);
-            }
-        }
-
-        diff_api.WindowPos3fvARB(to->current.rasterAttrib[VERT_ATTRIB_POS]);
-        if (to->bufferobject.unpackBuffer->hwid>0)
-        {
-            diff_api.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, to->bufferobject.unpackBuffer->hwid);
-        }
-        if (to->framebufferobject.drawFB)
-        {
-            CRASSERT(to->framebufferobject.drawFB->hwid);
-            diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, to->framebufferobject.drawFB->hwid);
-            diff_api.DrawBuffer(to->framebufferobject.drawFB->drawbuffer[0]);
-        }
-        else if (data->idOverrrideFBO)
-        {
-            diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, data->idOverrrideFBO);
-            diff_api.DrawBuffer(GL_COLOR_ATTACHMENT0);
-        }
-        else
-        {
-            diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
-            diff_api.DrawBuffer(to->buffer.drawBuffer);
-        }
-        if (to->buffer.alphaTest)
-        {
-            diff_api.Enable(GL_ALPHA_TEST);
-        }
-        if (to->viewport.scissorTest)
-        {
-            diff_api.Enable(GL_SCISSOR_TEST);
-        }
-        if (to->buffer.blend)
-        {
-            diff_api.Enable(GL_BLEND);
-        }
-        if (to->buffer.logicOp)
-        {
-            diff_api.Enable(GL_COLOR_LOGIC_OP);
-        }
-        if (to->buffer.depthTest)
-        {
-            diff_api.Enable(GL_DEPTH_TEST);
-        }
-        if (to->stencil.stencilTest)
-        {
-            diff_api.Enable(GL_STENCIL_TEST);
-        }
-
-        diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, unpack.skipRows);
-        diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, unpack.skipPixels);
-        diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, unpack.alignment);
-        diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, unpack.rowLength);
-        diff_api.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, unpack.imageHeight);
-        diff_api.PixelStorei(GL_UNPACK_SKIP_IMAGES, unpack.skipImages);
-        diff_api.PixelStorei(GL_UNPACK_SWAP_BYTES, unpack.swapBytes);
-        diff_api.PixelStorei(GL_UNPACK_LSB_FIRST, unpack.psLSBFirst);
-
-        diff_api.Finish();
-    }
+        char fname[200];
+        sprintf(fname, "./img_apply_%p_%d_%d.tga", to, i, el->enmFormat);
+        crDumpNamedTGA(fname, el->width, el->height, el->pvData);
+#endif
+
+        /* Before SSM version SHCROGL_SSM_VERSION_WITH_SEPARATE_DEPTH_STENCIL_BUFFERS
+         * saved state file contined invalid DEPTH/STENCIL data. In order to prevent
+         * crashes and improper guest App behavior, this data should be ignored. */
+        if (   data->u32Version < SHCROGL_SSM_VERSION_WITH_SEPARATE_DEPTH_STENCIL_BUFFERS
+            && (   el->enmFormat == GL_DEPTH_COMPONENT
+                || el->enmFormat == GL_STENCIL_INDEX
+                || el->enmFormat == GL_DEPTH_STENCIL))
+            continue;
+
+        if (el->enmFormat == GL_DEPTH_COMPONENT || el->enmFormat == GL_DEPTH_STENCIL)
+        {
+            pState->diff_api.Enable(GL_DEPTH_TEST);
+            if (to->pixel.depthScale != 1.0f)
+            {
+                pState->diff_api.PixelTransferf (GL_DEPTH_SCALE, 1.0f);
+            }
+            if (to->pixel.depthBias != 0.0f)
+            {
+                pState->diff_api.PixelTransferf (GL_DEPTH_BIAS, 0.0f);
+            }
+        }
+        if (el->enmFormat == GL_STENCIL_INDEX || el->enmFormat == GL_DEPTH_STENCIL)
+        {
+            pState->diff_api.Enable(GL_STENCIL_TEST);
+            if (to->pixel.mapStencil)
+            {
+                pState->diff_api.PixelTransferi (GL_MAP_STENCIL, GL_FALSE);
+            }
+            if (to->pixel.indexOffset)
+            {
+                pState->diff_api.PixelTransferi (GL_INDEX_OFFSET, 0);
+            }
+            if (to->pixel.indexShift)
+            {
+                pState->diff_api.PixelTransferi (GL_INDEX_SHIFT, 0);
+            }
+        }
+
+        pState->diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, el->idFBO);
+
+        if (el->enmBuffer)
+            pState->diff_api.DrawBuffer(el->enmBuffer);
+
+        pState->diff_api.WindowPos2iARB(el->posX, el->posY);
+        pState->diff_api.DrawPixels(el->width, el->height, el->enmFormat, el->enmType, el->pvData);
+        crDebug("Applied %d;%d;%d;%d;%d;0x%p fb image", el->enmBuffer, el->width, el->height, el->enmFormat, el->enmType, el->pvData);
+
+        if (el->enmFormat == GL_DEPTH_COMPONENT || el->enmFormat == GL_DEPTH_STENCIL)
+        {
+            if (to->pixel.depthScale != 1.0f)
+            {
+                pState->diff_api.PixelTransferf (GL_DEPTH_SCALE, to->pixel.depthScale);
+            }
+            if (to->pixel.depthBias != 0.0f)
+            {
+                pState->diff_api.PixelTransferf (GL_DEPTH_BIAS, to->pixel.depthBias);
+            }
+            pState->diff_api.Disable(GL_DEPTH_TEST);
+        }
+        if (el->enmFormat == GL_STENCIL_INDEX || el->enmFormat == GL_DEPTH_STENCIL)
+        {
+            if (to->pixel.indexOffset)
+            {
+                pState->diff_api.PixelTransferi (GL_INDEX_OFFSET, to->pixel.indexOffset);
+            }
+            if (to->pixel.indexShift)
+            {
+                pState->diff_api.PixelTransferi (GL_INDEX_SHIFT, to->pixel.indexShift);
+            }
+            if (to->pixel.mapStencil)
+            {
+                pState->diff_api.PixelTransferi (GL_MAP_STENCIL, GL_TRUE);
+            }
+            pState->diff_api.Disable(GL_STENCIL_TEST);
+        }
+    }
+
+    pState->diff_api.WindowPos3fvARB(to->current.rasterAttrib[VERT_ATTRIB_POS]);
+    if (to->bufferobject.unpackBuffer->hwid>0)
+    {
+        pState->diff_api.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, to->bufferobject.unpackBuffer->hwid);
+    }
+    if (to->framebufferobject.drawFB)
+    {
+        CRASSERT(to->framebufferobject.drawFB->hwid);
+        pState->diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, to->framebufferobject.drawFB->hwid);
+        pState->diff_api.DrawBuffer(to->framebufferobject.drawFB->drawbuffer[0]);
+    }
+    else if (data->idOverrrideFBO)
+    {
+        pState->diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, data->idOverrrideFBO);
+        pState->diff_api.DrawBuffer(GL_COLOR_ATTACHMENT0);
+    }
+    else
+    {
+        pState->diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
+        pState->diff_api.DrawBuffer(to->buffer.drawBuffer);
+    }
+    if (to->buffer.alphaTest)
+    {
+        pState->diff_api.Enable(GL_ALPHA_TEST);
+    }
+    if (to->viewport.scissorTest)
+    {
+        pState->diff_api.Enable(GL_SCISSOR_TEST);
+    }
+    if (to->buffer.blend)
+    {
+        pState->diff_api.Enable(GL_BLEND);
+    }
+    if (to->buffer.logicOp)
+    {
+        pState->diff_api.Enable(GL_COLOR_LOGIC_OP);
+    }
+    if (to->buffer.depthTest)
+    {
+        pState->diff_api.Enable(GL_DEPTH_TEST);
+    }
+    if (to->stencil.stencilTest)
+    {
+        pState->diff_api.Enable(GL_STENCIL_TEST);
+    }
+
+    pState->diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, unpack.skipRows);
+    pState->diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, unpack.skipPixels);
+    pState->diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, unpack.alignment);
+    pState->diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, unpack.rowLength);
+    pState->diff_api.PixelStorei(GL_UNPACK_IMAGE_HEIGHT, unpack.imageHeight);
+    pState->diff_api.PixelStorei(GL_UNPACK_SKIP_IMAGES, unpack.skipImages);
+    pState->diff_api.PixelStorei(GL_UNPACK_SWAP_BYTES, unpack.swapBytes);
+    pState->diff_api.PixelStorei(GL_UNPACK_LSB_FIRST, unpack.psLSBFirst);
+
+    pState->diff_api.Finish();
 }
 
 void crStateSwitchContext( CRContext *from, CRContext *to )
 {
+    PCRStateTracker pState = from->pStateTracker;
 	CRbitvalue *bitID = to->bitid;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
+
+    CRASSERT(from->pStateTracker == to->pStateTracker);
 
 	if (CHECKDIRTY(sb->attrib.dirty, bitID))
@@ -590,5 +595,5 @@
 {
     GLenum err;
-    while ((err = diff_api.GetError()) != GL_NO_ERROR)
+    while ((err = ctx->pStateTracker->diff_api.GetError()) != GL_NO_ERROR)
     {
         if (ctx->error != GL_NO_ERROR)
@@ -597,8 +602,8 @@
 }
 
-GLenum crStateCleanHWErrorState(void)
+GLenum crStateCleanHWErrorState(PCRStateTracker pState)
 {
     GLenum err;
-    while ((err = diff_api.GetError()) != GL_NO_ERROR)
+    while ((err = pState->diff_api.GetError()) != GL_NO_ERROR)
     {
         static int cErrPrints = 0;
@@ -618,8 +623,13 @@
 void crStateSwitchPrepare(CRContext *toCtx, CRContext *fromCtx, GLuint idDrawFBO, GLuint idReadFBO)
 {
+    PCRStateTracker pState = toCtx->pStateTracker ? toCtx->pStateTracker : fromCtx->pStateTracker;
+
+    CRASSERT(pState);
+    CRASSERT(!toCtx || !fromCtx || toCtx->pStateTracker == fromCtx->pStateTracker);
+
     if (!fromCtx)
         return;
 
-    if (g_bVBoxEnableDiffOnMakeCurrent && toCtx && toCtx != fromCtx)
+    if (pState->fVBoxEnableDiffOnMakeCurrent && toCtx && toCtx != fromCtx)
         crStateSyncHWErrorState(fromCtx);
 
@@ -631,4 +641,9 @@
 void crStateSwitchPostprocess(CRContext *toCtx, CRContext *fromCtx, GLuint idDrawFBO, GLuint idReadFBO)
 {
+    PCRStateTracker pState = toCtx->pStateTracker ? toCtx->pStateTracker : fromCtx->pStateTracker;
+
+    CRASSERT(pState);
+    CRASSERT(!toCtx || !fromCtx || toCtx->pStateTracker == fromCtx->pStateTracker);
+
     if (!toCtx)
         return;
@@ -638,7 +653,7 @@
 #endif
 
-    if (g_bVBoxEnableDiffOnMakeCurrent && fromCtx && toCtx != fromCtx)
-    {
-        CR_STATE_CLEAN_HW_ERR_WARN("error on make current");
-    }
-}
+    if (pState->fVBoxEnableDiffOnMakeCurrent && fromCtx && toCtx != fromCtx)
+    {
+        CR_STATE_CLEAN_HW_ERR_WARN(pState, "error on make current");
+    }
+}
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_enable.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_enable.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_enable.c	(revision 78375)
@@ -13,4 +13,5 @@
 				GLenum cap, GLboolean val)
 {
+    PCRStateTracker pState = g->pStateTracker;
 	unsigned int i;
 	i = cap - GL_CLIP_PLANE0;
@@ -56,6 +57,6 @@
 				 * for an example of why). */
 
-				crStateCurrentRecover( );
-				crStateColorMaterialRecover( );
+				crStateCurrentRecover(pState);
+				crStateColorMaterialRecover(pState);
 			}
 			g->lighting.colorMaterial = val;
@@ -71,5 +72,5 @@
 			}
 			else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(GL_COLOR_SUM_EXT) - No support for secondary color!");
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(GL_COLOR_SUM_EXT) - No support for secondary color!");
 				return;
 			}
@@ -164,5 +165,5 @@
 			}
 			else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(GL_REGISTER_COMBINERS_NV) - No support for NV_register_combiners");
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(GL_REGISTER_COMBINERS_NV) - No support for NV_register_combiners");
 				return;
 			}
@@ -177,5 +178,5 @@
 			}
 			else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(GL_PER_STAGE_CONSTANTS_NV) - No support for NV_register_combiners2");
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(GL_PER_STAGE_CONSTANTS_NV) - No support for NV_register_combiners2");
 				return;
 			}
@@ -190,5 +191,5 @@
 			}
 			else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
 				return;
 			}
@@ -237,5 +238,5 @@
 			}
 			else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
 				return;
 			}
@@ -273,5 +274,5 @@
 			if (g->texture.curTextureUnit != 0)
 			{
-				crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "Map stuff was enabled while the current texture unit was not GL_TEXTURE0_ARB!" );
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "Map stuff was enabled while the current texture unit was not GL_TEXTURE0_ARB!" );
 				return;
 			}
@@ -335,5 +336,5 @@
 			}
 			else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
 				return;
 			}
@@ -351,5 +352,5 @@
 			}
 			else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
 				return;
 			}
@@ -367,5 +368,5 @@
 			}
 			else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
 				return;
 			}
@@ -427,5 +428,5 @@
 			}
 			else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
 				return;
 			}
@@ -440,5 +441,5 @@
 			}
 			else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable(0x%x)", cap);
 				return;
 			}
@@ -493,7 +494,7 @@
 #endif
 			if (val)
-				crStateEnableClientState(cap);
+				crStateEnableClientState(pState, cap);
 			else
-				crStateDisableClientState(cap);
+				crStateDisableClientState(pState, cap);
 			break;
 #ifdef CR_EXT_stencil_two_side
@@ -505,5 +506,5 @@
 #endif
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable called with bogus cap: 0x%x", cap);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEnable/glDisable called with bogus cap: 0x%x", cap);
 			return;
 	}
@@ -511,12 +512,12 @@
 
 
-void STATE_APIENTRY crStateEnable (GLenum cap)
+void STATE_APIENTRY crStateEnable (PCRStateTracker pState, GLenum cap)
 {
-	CRContext *g = GetCurrentContext();
-	CRStateBits *sb = GetCurrentBits();
+	CRContext *g = GetCurrentContext(pState);
+	CRStateBits *sb = GetCurrentBits(pState);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glEnable called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glEnable called in begin/end");
 		return;
 	}
@@ -528,12 +529,12 @@
 
 
-void STATE_APIENTRY crStateDisable (GLenum cap)
+void STATE_APIENTRY crStateDisable (PCRStateTracker pState, GLenum cap)
 {
-	CRContext *g = GetCurrentContext();
-	CRStateBits *sb = GetCurrentBits();
+	CRContext *g = GetCurrentContext(pState);
+	CRStateBits *sb = GetCurrentBits(pState);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDisable called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glDisable called in begin/end");
 		return;
 	}
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_error.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_error.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_error.c	(revision 78375)
@@ -5,6 +5,6 @@
  */
 
+#include "state/cr_statetypes.h"
 #include "state/cr_stateerror.h"
-#include "state/cr_statetypes.h"
 #include "state.h"
 #include "cr_error.h"
@@ -14,7 +14,7 @@
 #include <iprt/env.h>
 
-void crStateError( int line, const char *file, GLenum error, const char *format, ... )
+void crStateError(PCRStateTracker pState, int line, const char *file, GLenum error, const char *format, ... )
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	char errstr[8096];
 	va_list args;
@@ -70,12 +70,12 @@
 
 
-GLenum STATE_APIENTRY crStateGetError(void)
+GLenum STATE_APIENTRY crStateGetError(PCRStateTracker pState)
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	GLenum e = g->error;
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 									"glStateGetError() called between glBegin/glEnd" );
 		return 0;
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_evaluators.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_evaluators.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_evaluators.c	(revision 78375)
@@ -41,5 +41,5 @@
 {
 	CREvaluatorState *e = &ctx->eval;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
 	CREvaluatorBits *eb = &(sb->eval);
 	GLint i;
@@ -63,5 +63,5 @@
 {
 	CREvaluatorState *e = &ctx->eval;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
 	CREvaluatorBits *eb = &(sb->eval);
 	GLint i;
@@ -111,5 +111,5 @@
 {
 	CREvaluatorState *e = &ctx->eval;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
 	CREvaluatorBits *eb = &(sb->eval);
 	static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
@@ -324,10 +324,10 @@
  */
 static void
-map1(GLenum target, GLfloat u1, GLfloat u2, GLint ustride,
+map1(PCRStateTracker pState, GLenum target, GLfloat u1, GLfloat u2, GLint ustride,
 		 GLint uorder, const GLvoid * points, GLenum type)
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CREvaluatorState *e = &(g->eval);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CREvaluatorBits *eb = &(sb->eval);
 	CRTextureState *t = &(g->texture);
@@ -337,5 +337,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "Map1d called in begin/end");
 		return;
@@ -347,13 +347,13 @@
 
 	if (u1 == u2) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMap1d(u1==u2)");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMap1d(u1==u2)");
 		return;
 	}
 	if (uorder < 1 || uorder > MAX_EVAL_ORDER) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMap1d(bad uorder)");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMap1d(bad uorder)");
 		return;
 	}
 	if (!points) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
 								 "glMap1d(null points)");
 		return;
@@ -372,5 +372,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMap1d(bad target)");
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMap1d(bad target)");
             return;
     }
@@ -381,10 +381,10 @@
 
 	if (k == 0) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMap1d(k=0)");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMap1d(k=0)");
 		return;
 	}
 
 	if (ustride < k) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMap1d(bad ustride");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMap1d(bad ustride");
 		return;
 	}
@@ -392,5 +392,5 @@
 	if (t->curTextureUnit != 0) {
 		/* See OpenGL 1.2.1 spec, section F.2.13 */
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glMap1d(current texture unit must be zero)");
 		return;
@@ -418,24 +418,24 @@
 
 void STATE_APIENTRY
-crStateMap1f(GLenum target, GLfloat u1, GLfloat u2,
+crStateMap1f(PCRStateTracker pState, GLenum target, GLfloat u1, GLfloat u2,
 						 GLint stride, GLint order, const GLfloat * points)
 {
-	map1(target, u1, u2, stride, order, points, GL_FLOAT);
+	map1(pState, target, u1, u2, stride, order, points, GL_FLOAT);
 }
 
 void STATE_APIENTRY
-crStateMap1d(GLenum target, GLdouble u1, GLdouble u2, GLint stride,
+crStateMap1d(PCRStateTracker pState, GLenum target, GLdouble u1, GLdouble u2, GLint stride,
 						 GLint order, const GLdouble * points)
 {
-	map1(target, (GLfloat) u1, (GLfloat) u2, stride, order, points, GL_DOUBLE);
+	map1(pState, target, (GLfloat) u1, (GLfloat) u2, stride, order, points, GL_DOUBLE);
 }
 
 static void
-map2(GLenum target, GLfloat u1, GLfloat u2, GLint ustride, GLint uorder,
+map2(PCRStateTracker pState, GLenum target, GLfloat u1, GLfloat u2, GLint ustride, GLint uorder,
 		 GLfloat v1, GLfloat v2, GLint vstride, GLint vorder,
 		 const GLvoid * points, GLenum type)
 {
-	CRContext *g = GetCurrentContext();
-	CRStateBits *sb = GetCurrentBits();
+	CRContext *g = GetCurrentContext(pState);
+	CRStateBits *sb = GetCurrentBits(pState);
 	CREvaluatorState *e = &(g->eval);
 	CREvaluatorBits *eb = &(sb->eval);
@@ -448,5 +448,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glMap2d()");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glMap2d()");
 		return;
 	}
@@ -455,20 +455,20 @@
 
 	if (u1 == u2) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
 		return;
 	}
 
 	if (v1 == v2) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
 		return;
 	}
 
 	if (uorder < 1 || uorder > MAX_EVAL_ORDER) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
 		return;
 	}
 
 	if (vorder < 1 || vorder > MAX_EVAL_ORDER) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
 		return;
 	}
@@ -486,5 +486,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMap2d()");
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMap2d()");
             return;
     }
@@ -500,14 +500,14 @@
 
 	if (k == 0) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMap2d()");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMap2d()");
 		return;
 	}
 
 	if (ustride < k) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
 		return;
 	}
 	if (vstride < k) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMap2d()");
 		return;
 	}
@@ -520,5 +520,5 @@
 	if (t->curTextureUnit != 0) {
 		/* See OpenGL 1.2.1 spec, section F.2.13 */
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glMap2d()");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glMap2d()");
 		return;
 	}
@@ -550,29 +550,27 @@
 
 void STATE_APIENTRY
-crStateMap2f(GLenum target, GLfloat u1, GLfloat u2,
+crStateMap2f(PCRStateTracker pState, GLenum target, GLfloat u1, GLfloat u2,
 						 GLint ustride, GLint uorder,
 						 GLfloat v1, GLfloat v2,
 						 GLint vstride, GLint vorder, const GLfloat * points)
 {
-	map2(target, u1, u2, ustride, uorder, v1, v2, vstride, vorder,
-			 points, GL_FLOAT);
+	map2(pState, target, u1, u2, ustride, uorder, v1, v2, vstride, vorder, points, GL_FLOAT);
 }
 
 
 void STATE_APIENTRY
-crStateMap2d(GLenum target, GLdouble u1, GLdouble u2,
+crStateMap2d(PCRStateTracker pState, GLenum target, GLdouble u1, GLdouble u2,
 						 GLint ustride, GLint uorder,
 						 GLdouble v1, GLdouble v2,
 						 GLint vstride, GLint vorder, const GLdouble * points)
 {
-	map2(target, (GLfloat) u1, (GLfloat) u2, ustride, uorder,
-			 (GLfloat) v1, (GLfloat) v2, vstride, vorder, points, GL_DOUBLE);
+	map2(pState, target, (GLfloat) u1, (GLfloat) u2, ustride, uorder, (GLfloat) v1, (GLfloat) v2, vstride, vorder, points, GL_DOUBLE);
 }
 
 void STATE_APIENTRY
-crStateGetMapdv(GLenum target, GLenum query, GLdouble * v)
-{
-	CRContext *g = GetCurrentContext();
-	CRStateBits *sb = GetCurrentBits();
+crStateGetMapdv(PCRStateTracker pState, GLenum target, GLenum query, GLdouble * v)
+{
+	CRContext *g = GetCurrentContext(pState);
+	CRStateBits *sb = GetCurrentBits(pState);
 	CREvaluatorState *e = &(g->eval);
 	GLint size;
@@ -581,5 +579,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "Map1d called in begin/end");
 		return;
@@ -594,5 +592,5 @@
 
 		if (i < 0 || i >= GLEVAL_TOT) {
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "GetMapdv: invalid target: %d", target);
 			return;
@@ -617,5 +615,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "GetMapdv: invalid target: %d", target);
 			return;
@@ -638,5 +636,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "GetMapdv: invalid target: %d", target);
 			return;
@@ -646,8 +644,8 @@
 
 void STATE_APIENTRY
-crStateGetMapfv(GLenum target, GLenum query, GLfloat * v)
-{
-	CRContext *g = GetCurrentContext();
-	CRStateBits *sb = GetCurrentBits();
+crStateGetMapfv(PCRStateTracker pState, GLenum target, GLenum query, GLfloat * v)
+{
+	CRContext *g = GetCurrentContext(pState);
+	CRStateBits *sb = GetCurrentBits(pState);
 	CREvaluatorState *e = &(g->eval);
 	GLint size;
@@ -656,5 +654,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "Map1d called in begin/end");
 		return;
@@ -667,5 +665,5 @@
 		i = target - GL_MAP2_COLOR_4;
 		if (i < 0 || i >= GLEVAL_TOT) {
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "GetMapfv: invalid target: %d", target);
 			return;
@@ -689,5 +687,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "GetMapfv: invalid target: %d", target);
 			return;
@@ -710,5 +708,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "GetMapfv: invalid target: %d", target);
 			return;
@@ -718,8 +716,8 @@
 
 void STATE_APIENTRY
-crStateGetMapiv(GLenum target, GLenum query, GLint * v)
-{
-	CRContext *g = GetCurrentContext();
-	CRStateBits *sb = GetCurrentBits();
+crStateGetMapiv(PCRStateTracker pState, GLenum target, GLenum query, GLint * v)
+{
+	CRContext *g = GetCurrentContext(pState);
+	CRStateBits *sb = GetCurrentBits(pState);
 	CREvaluatorState *e = &(g->eval);
 	GLint size;
@@ -728,5 +726,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "Map1d called in begin/end");
 		return;
@@ -739,5 +737,5 @@
 		i = target - GL_MAP2_COLOR_4;
 		if (i < 0 || i >= GLEVAL_TOT) {
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "GetMapiv: invalid target: %d", target);
 			return;
@@ -761,5 +759,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "GetMapiv: invalid target: %d", target);
 			return;
@@ -782,5 +780,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "GetMapiv: invalid target: %d", target);
 			return;
@@ -790,13 +788,13 @@
 
 void STATE_APIENTRY
-crStateMapGrid1f(GLint un, GLfloat u1, GLfloat u2)
-{
-	CRContext *g = GetCurrentContext();
-	CRStateBits *sb = GetCurrentBits();
+crStateMapGrid1f(PCRStateTracker pState, GLint un, GLfloat u1, GLfloat u2)
+{
+	CRContext *g = GetCurrentContext(pState);
+	CRStateBits *sb = GetCurrentBits(pState);
 	CREvaluatorState *e = &(g->eval);
 	CREvaluatorBits *eb = &(sb->eval);
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "Map1d called in begin/end");
 		return;
@@ -806,5 +804,5 @@
 
 	if (un < 1) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMapGrid1f(bad un)");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMapGrid1f(bad un)");
 		return;
 	}
@@ -819,21 +817,21 @@
 
 void STATE_APIENTRY
-crStateMapGrid1d(GLint un, GLdouble u1, GLdouble u2)
-{
-	crStateMapGrid1f(un, (GLfloat) u1, (GLfloat) u2);
+crStateMapGrid1d(PCRStateTracker pState, GLint un, GLdouble u1, GLdouble u2)
+{
+	crStateMapGrid1f(pState, un, (GLfloat) u1, (GLfloat) u2);
 }
 
 
 void STATE_APIENTRY
-crStateMapGrid2f(GLint un, GLfloat u1, GLfloat u2,
+crStateMapGrid2f(PCRStateTracker pState, GLint un, GLfloat u1, GLfloat u2,
 								 GLint vn, GLfloat v1, GLfloat v2)
 {
-	CRContext *g = GetCurrentContext();
-	CRStateBits *sb = GetCurrentBits();
+	CRContext *g = GetCurrentContext(pState);
+	CRStateBits *sb = GetCurrentBits(pState);
 	CREvaluatorState *e = &(g->eval);
 	CREvaluatorBits *eb = &(sb->eval);
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "Map1d called in begin/end");
 		return;
@@ -843,9 +841,9 @@
 
 	if (un < 1) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMapGrid2f(bad un)");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMapGrid2f(bad un)");
 		return;
 	}
 	if (vn < 1) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMapGrid2f(bad vn)");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMapGrid2f(bad vn)");
 		return;
 	}
@@ -863,8 +861,8 @@
 
 void STATE_APIENTRY
-crStateMapGrid2d(GLint un, GLdouble u1, GLdouble u2,
+crStateMapGrid2d(PCRStateTracker pState, GLint un, GLdouble u1, GLdouble u2,
 								 GLint vn, GLdouble v1, GLdouble v2)
 {
-	crStateMapGrid2f(un, (GLfloat) u1, (GLfloat) u2,
+	crStateMapGrid2f(pState, un, (GLfloat) u1, (GLfloat) u2,
 									 vn, (GLfloat) v1, (GLfloat) v2);
 }
@@ -874,4 +872,5 @@
 											 CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
 	CREvaluatorState *from = &(fromCtx->eval);
 	CREvaluatorState *to = &(toCtx->eval);
@@ -879,4 +878,6 @@
 	CRbitvalue nbitID[CR_MAX_BITARRAY];
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
 	for (j = 0; j < CR_MAX_BITARRAY; j++)
 		nbitID[j] = ~bitID[j];
@@ -885,6 +886,6 @@
 		if (from->autoNormal != to->autoNormal) {
 			glAble able[2];
-			able[0] = diff_api.Disable;
-			able[1] = diff_api.Enable;
+			able[0] = pState->diff_api.Disable;
+			able[1] = pState->diff_api.Enable;
 			able[to->autoNormal] (GL_AUTO_NORMAL);
 			FILLDIRTY(e->enable);
@@ -902,7 +903,7 @@
 				crMemcmp((const void *) from->eval1D[i].coeff,
 						 (const void *) to->eval1D[i].coeff, size)) {
-				diff_api.Map1f(i + GL_MAP1_COLOR_4, to->eval1D[i].u1,
-							   to->eval1D[i].u2, gleval_sizes[i], to->eval1D[i].order,
-							   to->eval1D[i].coeff);
+				pState->diff_api.Map1f(i + GL_MAP1_COLOR_4, to->eval1D[i].u1,
+							           to->eval1D[i].u2, gleval_sizes[i], to->eval1D[i].order,
+							           to->eval1D[i].coeff);
 				FILLDIRTY(e->dirty);
 				FILLDIRTY(e->eval1D[i]);
@@ -924,10 +925,10 @@
 				crMemcmp((const void *) from->eval2D[i].coeff,
 						 (const void *) to->eval2D[i].coeff, size)) {
-				diff_api.Map2f(i + GL_MAP2_COLOR_4,
-							   to->eval2D[i].u1, to->eval2D[i].u2,
-							   gleval_sizes[i], to->eval2D[i].uorder,
-                               to->eval2D[i].v1, to->eval2D[i].v2,
-                               gleval_sizes[i], to->eval2D[i].vorder,
-                               to->eval2D[i].coeff);
+				pState->diff_api.Map2f(i + GL_MAP2_COLOR_4,
+							           to->eval2D[i].u1, to->eval2D[i].u2,
+							           gleval_sizes[i], to->eval2D[i].uorder,
+                                       to->eval2D[i].v1, to->eval2D[i].v2,
+                                       gleval_sizes[i], to->eval2D[i].vorder,
+                                       to->eval2D[i].coeff);
 				FILLDIRTY(e->dirty);
 				FILLDIRTY(e->eval2D[i]);
@@ -940,5 +941,5 @@
 				from->u21D != to->u21D ||
                 from->un1D != to->un1D) {
-			diff_api.MapGrid1d(to->un1D, to->u11D, to->u21D);
+			pState->diff_api.MapGrid1d(to->un1D, to->u11D, to->u21D);
 			FILLDIRTY(e->dirty);
 			FILLDIRTY(e->grid1D);
@@ -953,6 +954,6 @@
 			from->v22D != to->v22D ||
             from->vn2D != to->vn2D) {
-			diff_api.MapGrid2d(to->un2D, to->u12D, to->u22D,
-							   to->vn2D, to->v12D, to->v22D);
+			pState->diff_api.MapGrid2d(to->un2D, to->u12D, to->u22D,
+							           to->vn2D, to->v12D, to->v22D);
 			FILLDIRTY(e->dirty);
 			FILLDIRTY(e->grid1D);
@@ -967,4 +968,5 @@
                      CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
 	CREvaluatorState *from = &(fromCtx->eval);
 	CREvaluatorState *to = &(toCtx->eval);
@@ -973,9 +975,11 @@
 	CRbitvalue nbitID[CR_MAX_BITARRAY];
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
 	for (j = 0; j < CR_MAX_BITARRAY; j++)
 		nbitID[j] = ~bitID[j];
 
-	able[0] = diff_api.Disable;
-	able[1] = diff_api.Enable;
+	able[0] = pState->diff_api.Disable;
+	able[1] = pState->diff_api.Enable;
 
 	if (CHECKDIRTY(e->enable, bitID)) {
@@ -1002,7 +1006,7 @@
 				crMemcmp((const void *) from->eval1D[i].coeff,
 						 (const void *) to->eval1D[i].coeff, size)) {
-				diff_api.Map1f(i + GL_MAP1_COLOR_4, to->eval1D[i].u1,
-							   to->eval1D[i].u2, gleval_sizes[i], to->eval1D[i].order,
-							   to->eval1D[i].coeff);
+				pState->diff_api.Map1f(i + GL_MAP1_COLOR_4, to->eval1D[i].u1,
+							           to->eval1D[i].u2, gleval_sizes[i], to->eval1D[i].order,
+							           to->eval1D[i].coeff);
 				from->eval1D[i].order = to->eval1D[i].order;
 				from->eval1D[i].u1    = to->eval1D[i].u1;
@@ -1034,10 +1038,10 @@
 				crMemcmp((const void *) from->eval2D[i].coeff,
 						 (const void *) to->eval2D[i].coeff, size)) {
-				diff_api.Map2f(i + GL_MAP2_COLOR_4,
-							   to->eval2D[i].u1, to->eval2D[i].u2,
-                               gleval_sizes[i], to->eval2D[i].uorder,
-                               to->eval2D[i].v1, to->eval2D[i].v2,
-                               gleval_sizes[i], to->eval2D[i].vorder,
-                               to->eval2D[i].coeff);
+				pState->diff_api.Map2f(i + GL_MAP2_COLOR_4,
+							           to->eval2D[i].u1, to->eval2D[i].u2,
+                                       gleval_sizes[i], to->eval2D[i].uorder,
+                                       to->eval2D[i].v1, to->eval2D[i].v2,
+                                       gleval_sizes[i], to->eval2D[i].vorder,
+                                       to->eval2D[i].coeff);
 				from->eval2D[i].uorder = to->eval2D[i].uorder;
 				from->eval2D[i].vorder = to->eval2D[i].vorder;
@@ -1056,5 +1060,5 @@
 			from->u21D != to->u21D ||
             from->un1D != to->un1D) {
-			diff_api.MapGrid1d(to->un1D, to->u11D, to->u21D);
+			pState->diff_api.MapGrid1d(to->un1D, to->u11D, to->u21D);
 			from->u11D = to->u11D;
 			from->u21D = to->u21D;
@@ -1070,6 +1074,6 @@
 				from->v22D != to->v22D ||
                 from->vn2D != to->vn2D) {
-			diff_api.MapGrid2d(to->un2D, to->u12D, to->u22D,
-							   to->vn2D, to->v12D, to->v22D);
+			pState->diff_api.MapGrid2d(to->un2D, to->u12D, to->u22D,
+							           to->vn2D, to->v12D, to->v22D);
 			from->u12D = to->u12D;
 			from->u22D = to->u22D;
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_feedback.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_feedback.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_feedback.c	(revision 78375)
@@ -120,7 +120,6 @@
  */
 static GLuint
-clip_point(const CRVertex *v)
-{
-	CRContext *g = GetCurrentContext();
+clip_point(CRContext *g, const CRVertex *v)
+{
 	GLuint mask = 0;
 	GLuint i;
@@ -165,5 +164,5 @@
  */
 static GLboolean
-clip_line(const CRVertex *v0in, const CRVertex *v1in,
+clip_line(CRContext *g, const CRVertex *v0in, const CRVertex *v1in,
 					CRVertex *v0new, CRVertex *v1new)
 {
@@ -174,6 +173,6 @@
 	/* XXX need to do user-clip planes */
 
-	code0 = clip_point(v0in);
-	code1 = clip_point(v1in);
+	code0 = clip_point(g, v0in);
+	code1 = clip_point(g, v1in);
 	if (code0 & code1)
 		return GL_FALSE;  /* totally clipped */
@@ -462,7 +461,6 @@
  */
 static void
-feedback_vertex(const CRVertex *v)
-{
-	CRContext *g = GetCurrentContext();
+feedback_vertex(CRContext *g, const CRVertex *v)
+{
 	CRFeedbackState *f = &(g->feedback);
 	CRTransformState *t = &(g->transform);
@@ -514,7 +512,6 @@
 
 static void
-feedback_rasterpos(void)
-{
-	CRContext *g = GetCurrentContext();
+feedback_rasterpos(CRContext *g)
+{
 	CRVertex *tv = g->vBuffer + g->vCount;
 	CRVertex v;
@@ -535,19 +532,18 @@
 	COPY_4V(v.attrib[VERT_ATTRIB_TEX0] , tv->attrib[VERT_ATTRIB_TEX0]);
 
-	feedback_vertex(&v);
+	feedback_vertex(g, &v);
 }
 
 
 static void
-feedback_point(const CRVertex *v)
-{
-	CRContext *g = GetCurrentContext();
+feedback_point(CRContext *g, const CRVertex *v)
+{
 	CRFeedbackState *f = &(g->feedback);
-	if (clip_point(v) == 0)
+	if (clip_point(g, v) == 0)
 	{
 		CRVertex c = *v;
 		MAP_POINT(c.winPos, c.clipPos, g->viewport);
 		FEEDBACK_TOKEN((GLfloat) GL_POINT_TOKEN);
-		feedback_vertex(&c);
+		feedback_vertex(g, &c);
 	}
 }
@@ -555,10 +551,9 @@
 
 static void
-feedback_line(const CRVertex *v0, const CRVertex *v1, GLboolean reset)
-{
-	CRContext *g = GetCurrentContext();
+feedback_line(CRContext *g, const CRVertex *v0, const CRVertex *v1, GLboolean reset)
+{
 	CRFeedbackState *f = &(g->feedback);
 	CRVertex c0, c1;
-	if (clip_line(v0, v1, &c0, &c1))
+	if (clip_line(g, v0, v1, &c0, &c1))
 	{
 		MAP_POINT(c0.winPos, c0.clipPos, g->viewport);
@@ -568,6 +563,6 @@
 		else
 			FEEDBACK_TOKEN((GLfloat) GL_LINE_TOKEN);
-		feedback_vertex(&c0);
-		feedback_vertex(&c1);
+		feedback_vertex(g, &c0);
+		feedback_vertex(g, &c1);
 	}
 }
@@ -575,7 +570,6 @@
 
 static void
-feedback_triangle(const CRVertex *v0, const CRVertex *v1, const CRVertex *v2)
-{
-	CRContext *g = GetCurrentContext();
+feedback_triangle(CRContext *g, const CRVertex *v0, const CRVertex *v1, const CRVertex *v2)
+{
 	CRFeedbackState *f = &(g->feedback);
 	CRVertex vlist[3], vclipped[8];
@@ -590,13 +584,13 @@
 	for (i = 0; i < n; i++) {
 		MAP_POINT(vclipped[i].winPos, vclipped[i].clipPos, g->viewport);
-		feedback_vertex(vclipped + i);
-	}
-}
-
-
-void STATE_APIENTRY
-crStateFeedbackVertex4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-	CRContext *g = GetCurrentContext();
+		feedback_vertex(g, vclipped + i);
+	}
+}
+
+
+void STATE_APIENTRY
+crStateFeedbackVertex4f(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRTransformState *t = &(g->transform);
 	CRPolygonState *p = &(g->polygon);
@@ -621,5 +615,5 @@
 	case GL_POINTS:
 		CRASSERT(g->vCount == 0);
-		feedback_point(v);
+		feedback_point(g, v);
 		break;
 	case GL_LINES:
@@ -631,5 +625,5 @@
 		{
 			CRASSERT(g->vCount == 1);
-			feedback_line(g->vBuffer + 0, g->vBuffer + 1, g->lineReset);
+			feedback_line(g, g->vBuffer + 0, g->vBuffer + 1, g->lineReset);
 			g->vCount = 0;
 		}
@@ -643,5 +637,5 @@
 		{
 			CRASSERT(g->vCount == 1);
-			feedback_line(g->vBuffer + 0, g->vBuffer + 1, g->lineReset);
+			feedback_line(g, g->vBuffer + 0, g->vBuffer + 1, g->lineReset);
 			g->vBuffer[0] = g->vBuffer[1];
 			g->lineReset = GL_FALSE;
@@ -657,5 +651,5 @@
 		else if (g->vCount == 1)
 		{
-			feedback_line(g->vBuffer + 0, g->vBuffer + 1, g->lineReset);
+			feedback_line(g, g->vBuffer + 0, g->vBuffer + 1, g->lineReset);
 			g->lineReset = GL_FALSE;
 			g->lineLoop = GL_TRUE;
@@ -667,5 +661,5 @@
 			CRASSERT(g->lineReset == GL_FALSE);
 			g->lineLoop = GL_FALSE;
-			feedback_line(g->vBuffer + 1, g->vBuffer + 2, g->lineReset);
+			feedback_line(g, g->vBuffer + 1, g->vBuffer + 2, g->lineReset);
 			g->vBuffer[1] = g->vBuffer[2];
 			/* leave g->vCount at 2 */
@@ -680,5 +674,5 @@
 		{
 			CRASSERT(g->vCount == 2);
-			feedback_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			feedback_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
 			g->vCount = 0;
 		}
@@ -691,5 +685,5 @@
 		else if (g->vCount == 2)
 		{
-			feedback_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			feedback_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
 			g->vCount = 3;
 		}
@@ -697,5 +691,5 @@
 		{
 			CRASSERT(g->vCount == 3);
-			feedback_triangle(g->vBuffer + 1, g->vBuffer + 3, g->vBuffer + 2);
+			feedback_triangle(g, g->vBuffer + 1, g->vBuffer + 3, g->vBuffer + 2);
 			g->vBuffer[0] = g->vBuffer[2];
 			g->vBuffer[1] = g->vBuffer[3];
@@ -711,5 +705,5 @@
 		{
 			CRASSERT(g->vCount == 2);
-			feedback_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			feedback_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
 			g->vBuffer[1] = g->vBuffer[2];
 			/* leave g->vCount = 2 */
@@ -724,6 +718,6 @@
 		{
 			CRASSERT(g->vCount == 3);
-			feedback_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
-			feedback_triangle(g->vBuffer + 0, g->vBuffer + 2, g->vBuffer + 3);
+			feedback_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			feedback_triangle(g, g->vBuffer + 0, g->vBuffer + 2, g->vBuffer + 3);
 			g->vCount = 0;
 		}
@@ -737,6 +731,6 @@
 		{
 			CRASSERT(g->vCount == 3);
-			feedback_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
-			feedback_triangle(g->vBuffer + 1, g->vBuffer + 3, g->vBuffer + 2);
+			feedback_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			feedback_triangle(g, g->vBuffer + 1, g->vBuffer + 3, g->vBuffer + 2);
 			g->vBuffer[0] = g->vBuffer[2];
 			g->vBuffer[1] = g->vBuffer[3];
@@ -749,5 +743,5 @@
 		case GL_POINT:
 			CRASSERT(g->vCount == 0);
-			feedback_point(v);
+			feedback_point(g, v);
 			break;
 		case GL_LINE:
@@ -759,5 +753,5 @@
 			else if (g->vCount == 1)
 			{
-				feedback_line(g->vBuffer + 0, g->vBuffer + 1, g->lineReset);
+				feedback_line(g, g->vBuffer + 0, g->vBuffer + 1, g->lineReset);
 				g->lineReset = GL_FALSE;
 				g->lineLoop = GL_TRUE;
@@ -769,5 +763,5 @@
 				CRASSERT(g->lineReset == GL_FALSE);
 				g->lineLoop = GL_FALSE;
-				feedback_line(g->vBuffer + 1, g->vBuffer + 2, g->lineReset);
+				feedback_line(g, g->vBuffer + 1, g->vBuffer + 2, g->lineReset);
 				g->vBuffer[1] = g->vBuffer[2];
 				/* leave g->vCount at 2 */
@@ -783,5 +777,5 @@
 			{
 				CRASSERT(g->vCount == 2);
-				feedback_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+				feedback_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
 				g->vBuffer[1] = g->vBuffer[2];
 				/* leave g->vCount = 2 */
@@ -799,9 +793,9 @@
 
 void STATE_APIENTRY
-crStateFeedbackBegin(GLenum mode)
-{
-	CRContext *g = GetCurrentContext();
-
-	crStateBegin(mode);
+crStateFeedbackBegin(PCRStateTracker pState, GLenum mode)
+{
+	CRContext *g = GetCurrentContext(pState);
+
+	crStateBegin(pState, mode);
 
 	g->vCount = 0;
@@ -812,7 +806,7 @@
 
 void STATE_APIENTRY
-crStateFeedbackEnd(void)
-{
-	CRContext *g = GetCurrentContext();
+crStateFeedbackEnd(PCRStateTracker pState)
+{
+	CRContext *g = GetCurrentContext(pState);
 
 	if ( (g->current.mode == GL_LINE_LOOP ||
@@ -822,22 +816,22 @@
 		/* draw the last line segment */
 		if (g->lineLoop)
-			feedback_line(g->vBuffer + 1, g->vBuffer + 0, GL_FALSE);
+			feedback_line(g, g->vBuffer + 1, g->vBuffer + 0, GL_FALSE);
 		else
-			feedback_line(g->vBuffer + 2, g->vBuffer + 0, GL_FALSE);
-	}
-
-	crStateEnd();
-}
-
-
-void STATE_APIENTRY
-crStateFeedbackBuffer(GLsizei size, GLenum type, GLfloat * buffer)
-{
-	CRContext *g = GetCurrentContext();
+			feedback_line(g, g->vBuffer + 2, g->vBuffer + 0, GL_FALSE);
+	}
+
+	crStateEnd(pState);
+}
+
+
+void STATE_APIENTRY
+crStateFeedbackBuffer(PCRStateTracker pState, GLsizei size, GLenum type, GLfloat * buffer)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRFeedbackState *f = &(g->feedback);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "FeedbackBuffer called in begin/end");
 		return;
@@ -846,5 +840,5 @@
 	if (g->renderMode == GL_FEEDBACK)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "Invalid Operation GL_FEEDBACK");
 		return;
@@ -852,5 +846,5 @@
 	if (size < 0)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
 								 "Invalid Value size < 0");
 		return;
@@ -858,5 +852,5 @@
 	if (!buffer)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
 								 "Invalid Value buffer = NULL");
 		f->bufferSize = 0;
@@ -884,5 +878,5 @@
 		break;
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "invalid type");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "invalid type");
 		return;
 	}
@@ -896,12 +890,12 @@
 
 void STATE_APIENTRY
-crStatePassThrough(GLfloat token)
-{
-	CRContext *g = GetCurrentContext();
+crStatePassThrough(PCRStateTracker pState, GLfloat token)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRFeedbackState *f = &(g->feedback);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "PassThrough called in begin/end");
 		return;
@@ -923,7 +917,7 @@
  */
 void STATE_APIENTRY
-crStateFeedbackGetBooleanv(GLenum pname, GLboolean * params)
-{
-	CRContext *g = GetCurrentContext();
+crStateFeedbackGetBooleanv(PCRStateTracker pState, GLenum pname, GLboolean * params)
+{
+	CRContext *g = GetCurrentContext(pState);
 
 	switch (pname)
@@ -946,7 +940,7 @@
 
 void STATE_APIENTRY
-crStateFeedbackGetDoublev(GLenum pname, GLdouble * params)
-{
-	CRContext *g = GetCurrentContext();
+crStateFeedbackGetDoublev(PCRStateTracker pState, GLenum pname, GLdouble * params)
+{
+	CRContext *g = GetCurrentContext(pState);
 
 	switch (pname)
@@ -969,7 +963,7 @@
 
 void STATE_APIENTRY
-crStateFeedbackGetFloatv(GLenum pname, GLfloat * params)
-{
-	CRContext *g = GetCurrentContext();
+crStateFeedbackGetFloatv(PCRStateTracker pState, GLenum pname, GLfloat * params)
+{
+	CRContext *g = GetCurrentContext(pState);
 
 	switch (pname)
@@ -992,7 +986,7 @@
 
 void STATE_APIENTRY
-crStateFeedbackGetIntegerv(GLenum pname, GLint * params)
-{
-	CRContext *g = GetCurrentContext();
+crStateFeedbackGetIntegerv(PCRStateTracker pState, GLenum pname, GLint * params)
+{
+	CRContext *g = GetCurrentContext(pState);
 
 	switch (pname)
@@ -1015,8 +1009,8 @@
 
 void STATE_APIENTRY
-crStateFeedbackDrawPixels(GLsizei width, GLsizei height, GLenum format,
+crStateFeedbackDrawPixels(PCRStateTracker pState, GLsizei width, GLsizei height, GLenum format,
 													GLenum type, const GLvoid * pixels)
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRFeedbackState *f = &(g->feedback);
 
@@ -1029,12 +1023,12 @@
 	FEEDBACK_TOKEN((GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN);
 
-	feedback_rasterpos();
-}
-
-void STATE_APIENTRY
-crStateFeedbackCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height,
+	feedback_rasterpos(g);
+}
+
+void STATE_APIENTRY
+crStateFeedbackCopyPixels(PCRStateTracker pState, GLint x, GLint y, GLsizei width, GLsizei height,
 													GLenum type)
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRFeedbackState *f = &(g->feedback);
 
@@ -1046,13 +1040,13 @@
 
 	FEEDBACK_TOKEN((GLfloat) (GLint) GL_COPY_PIXEL_TOKEN);
-	feedback_rasterpos();
-}
-
-void STATE_APIENTRY
-crStateFeedbackBitmap(GLsizei width, GLsizei height, GLfloat xorig,
+	feedback_rasterpos(g);
+}
+
+void STATE_APIENTRY
+crStateFeedbackBitmap(PCRStateTracker pState, GLsizei width, GLsizei height, GLfloat xorig,
 											GLfloat yorig, GLfloat xmove, GLfloat ymove,
 											const GLubyte * bitmap)
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRFeedbackState *f = &(g->feedback);
 
@@ -1065,5 +1059,5 @@
 	FEEDBACK_TOKEN((GLfloat) (GLint) GL_BITMAP_TOKEN);
 
-	feedback_rasterpos();
+	feedback_rasterpos(g);
 
 	if (g->current.rasterValid)
@@ -1076,153 +1070,153 @@
 
 void STATE_APIENTRY
-crStateFeedbackVertex4fv(const GLfloat * v)
-{
-	crStateFeedbackVertex4f(v[0], v[1], v[2], v[3]);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex4s(GLshort v0, GLshort v1, GLshort v2, GLshort v3)
-{
-	crStateFeedbackVertex4f((GLfloat) v0, (GLfloat) v1, (GLfloat) v2,
+crStateFeedbackVertex4fv(PCRStateTracker pState, const GLfloat * v)
+{
+	crStateFeedbackVertex4f(pState, v[0], v[1], v[2], v[3]);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex4s(PCRStateTracker pState, GLshort v0, GLshort v1, GLshort v2, GLshort v3)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v0, (GLfloat) v1, (GLfloat) v2,
 													(GLfloat) v3);
 }
 
 void STATE_APIENTRY
-crStateFeedbackVertex4sv(const GLshort * v)
-{
-	crStateFeedbackVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2],
+crStateFeedbackVertex4sv(PCRStateTracker pState, const GLshort * v)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2],
 													(GLfloat) v[3]);
 }
 
 void STATE_APIENTRY
-crStateFeedbackVertex4i(GLint v0, GLint v1, GLint v2, GLint v3)
-{
-	crStateFeedbackVertex4f((GLfloat) v0, (GLfloat) v1, (GLfloat) v2,
+crStateFeedbackVertex4i(PCRStateTracker pState, GLint v0, GLint v1, GLint v2, GLint v3)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v0, (GLfloat) v1, (GLfloat) v2,
 													(GLfloat) v3);
 }
 
 void STATE_APIENTRY
-crStateFeedbackVertex4iv(const GLint * v)
-{
-	crStateFeedbackVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2],
+crStateFeedbackVertex4iv(PCRStateTracker pState, const GLint * v)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2],
 													(GLfloat) v[3]);
 }
 
 void STATE_APIENTRY
-crStateFeedbackVertex4d(GLdouble v0, GLdouble v1, GLdouble v2, GLdouble v3)
-{
-	crStateFeedbackVertex4f((GLfloat) v0, (GLfloat) v1, (GLfloat) v2,
+crStateFeedbackVertex4d(PCRStateTracker pState, GLdouble v0, GLdouble v1, GLdouble v2, GLdouble v3)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v0, (GLfloat) v1, (GLfloat) v2,
 													(GLfloat) v3);
 }
 
 void STATE_APIENTRY
-crStateFeedbackVertex4dv(const GLdouble * v)
-{
-	crStateFeedbackVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2],
+crStateFeedbackVertex4dv(PCRStateTracker pState, const GLdouble * v)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2],
 													(GLfloat) v[3]);
 }
 
 void STATE_APIENTRY
-crStateFeedbackVertex2i(GLint v0, GLint v1)
-{
-	crStateFeedbackVertex4f((GLfloat) v0, (GLfloat) v1, 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex2iv(const GLint * v)
-{
-	crStateFeedbackVertex4f((GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex2s(GLshort v0, GLshort v1)
-{
-	crStateFeedbackVertex4f((GLfloat) v0, (GLfloat) v1, 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex2sv(const GLshort * v)
-{
-	crStateFeedbackVertex4f((GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex2f(GLfloat v0, GLfloat v1)
-{
-	crStateFeedbackVertex4f(v0, v1, 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex2fv(const GLfloat * v)
-{
-	crStateFeedbackVertex4f(v[0], v[1], 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex2d(GLdouble v0, GLdouble v1)
-{
-	crStateFeedbackVertex4f((GLfloat) v0, (GLfloat) v1, 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex2dv(const GLdouble * v)
-{
-	crStateFeedbackVertex4f((GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex3i(GLint v0, GLint v1, GLint v2)
-{
-	crStateFeedbackVertex4f((GLfloat) v0, (GLfloat) v1, (GLfloat) v2, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex3iv(const GLint * v)
-{
-	crStateFeedbackVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex3s(GLshort v0, GLshort v1, GLshort v2)
-{
-	crStateFeedbackVertex4f((GLfloat) v0, (GLfloat) v1, (GLfloat) v2, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex3sv(const GLshort * v)
-{
-	crStateFeedbackVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2],
+crStateFeedbackVertex2i(PCRStateTracker pState, GLint v0, GLint v1)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v0, (GLfloat) v1, 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex2iv(PCRStateTracker pState, const GLint * v)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex2s(PCRStateTracker pState, GLshort v0, GLshort v1)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v0, (GLfloat) v1, 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex2sv(PCRStateTracker pState, const GLshort * v)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex2f(PCRStateTracker pState, GLfloat v0, GLfloat v1)
+{
+	crStateFeedbackVertex4f(pState, v0, v1, 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex2fv(PCRStateTracker pState, const GLfloat * v)
+{
+	crStateFeedbackVertex4f(pState, v[0], v[1], 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex2d(PCRStateTracker pState, GLdouble v0, GLdouble v1)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v0, (GLfloat) v1, 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex2dv(PCRStateTracker pState, const GLdouble * v)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex3i(PCRStateTracker pState, GLint v0, GLint v1, GLint v2)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v0, (GLfloat) v1, (GLfloat) v2, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex3iv(PCRStateTracker pState, const GLint * v)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex3s(PCRStateTracker pState, GLshort v0, GLshort v1, GLshort v2)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v0, (GLfloat) v1, (GLfloat) v2, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex3sv(PCRStateTracker pState, const GLshort * v)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2],
 													1.0f);
 }
 
 void STATE_APIENTRY
-crStateFeedbackVertex3f(GLfloat v0, GLfloat v1, GLfloat v2)
-{
-	crStateFeedbackVertex4f(v0, v1, v2, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex3fv(const GLfloat * v)
-{
-	crStateFeedbackVertex4f(v[0], v[1], v[2], 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex3d(GLdouble v0, GLdouble v1, GLdouble v2)
-{
-	crStateFeedbackVertex4f((GLfloat) v0, (GLfloat) v1, (GLfloat) v2, 1.0f);
-}
-
-void STATE_APIENTRY
-crStateFeedbackVertex3dv(const GLdouble * v)
-{
-	crStateFeedbackVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2],
+crStateFeedbackVertex3f(PCRStateTracker pState, GLfloat v0, GLfloat v1, GLfloat v2)
+{
+	crStateFeedbackVertex4f(pState, v0, v1, v2, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex3fv(PCRStateTracker pState, const GLfloat * v)
+{
+	crStateFeedbackVertex4f(pState, v[0], v[1], v[2], 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex3d(PCRStateTracker pState, GLdouble v0, GLdouble v1, GLdouble v2)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v0, (GLfloat) v1, (GLfloat) v2, 1.0f);
+}
+
+void STATE_APIENTRY
+crStateFeedbackVertex3dv(PCRStateTracker pState, const GLdouble * v)
+{
+	crStateFeedbackVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2],
 													1.0f);
 }
 
 void STATE_APIENTRY
-crStateFeedbackTexCoord4f( GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3 )
-{
-	CRContext *g = GetCurrentContext();
+crStateFeedbackTexCoord4f(PCRStateTracker pState, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3 )
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRVertex *v = g->vBuffer + g->vCount;
 
@@ -1235,240 +1229,240 @@
  
 void STATE_APIENTRY
-crStateFeedbackTexCoord4fv( const GLfloat *v )
-{
-	crStateFeedbackTexCoord4f( v[0], v[1], v[2], v[3] );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord4s( GLshort v0, GLshort v1, GLshort v2, GLshort v3 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, (GLfloat)v3 );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord4sv( const GLshort *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], (GLfloat)v[1], (GLfloat)v[2], (GLfloat)v[3] );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord4i( GLint v0, GLint v1, GLint v2, GLint v3 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, (GLfloat)v3 );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord4iv( const GLint *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], (GLfloat)v[1], (GLfloat)v[2], (GLfloat)v[3] );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord4d( GLdouble v0, GLdouble v1, GLdouble v2, GLdouble v3 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, (GLfloat)v3 );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord4dv( const GLdouble *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], (GLfloat)v[1], (GLfloat)v[2], (GLfloat)v[3] );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord1i( GLint v0 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, 0.0f, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord1iv( const GLint *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], 0.0f, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord1s( GLshort v0 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, 0.0f, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord1sv( const GLshort *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], 0.0f, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord1f( GLfloat v0 )
-{
-	crStateFeedbackTexCoord4f( v0, 0.0f, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord1fv( const GLfloat *v )
-{
-	crStateFeedbackTexCoord4f( v[0], 0.0f, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord1d( GLdouble v0 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, 0.0f, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord1dv( const GLdouble *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], 0.0f, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord2i( GLint v0, GLint v1 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, (GLfloat)v1, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord2iv( const GLint *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], (GLfloat)v[1], 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord2s( GLshort v0, GLshort v1 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, (GLfloat)v1, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord2sv( const GLshort *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], (GLfloat)v[1], 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord2f( GLfloat v0, GLfloat v1 )
-{
-	crStateFeedbackTexCoord4f( v0, v1, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord2fv( const GLfloat *v )
-{
-	crStateFeedbackTexCoord4f( v[0], v[1], 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord2d( GLdouble v0, GLdouble v1 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, (GLfloat)v1, 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord2dv( const GLdouble *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], (GLfloat)v[1], 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord3i( GLint v0, GLint v1, GLint v2 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord3iv( const GLint *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], (GLfloat)v[1], 0.0f, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord3s( GLshort v0, GLshort v1, GLshort v2 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord3sv( const GLshort *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], (GLfloat)v[1], (GLfloat)v[2], 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord3f( GLfloat v0, GLfloat v1, GLfloat v2 )
-{
-	crStateFeedbackTexCoord4f( v0, v1, v2, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord3fv( const GLfloat *v )
-{
-	crStateFeedbackTexCoord4f( v[0], v[1], v[2], 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord3d( GLdouble v0, GLdouble v1, GLdouble v2 )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackTexCoord3dv( const GLdouble *v )
-{
-	crStateFeedbackTexCoord4f( (GLfloat)v[0], (GLfloat)v[1], (GLfloat)v[2], 1.0f );
-}
-
-void STATE_APIENTRY
-crStateFeedbackRectf(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1)
-{
-   crStateFeedbackBegin(GL_QUADS);
-   crStateFeedbackVertex2f(x0, y0);
-   crStateFeedbackVertex2f(x0, y1);
-   crStateFeedbackVertex2f(x1, y1);
-   crStateFeedbackVertex2f(x1, y0);
-   crStateFeedbackEnd();
-}
-
-void STATE_APIENTRY
-crStateFeedbackRecti(GLint x0, GLint y0, GLint x1, GLint y1)
-{
-   crStateFeedbackRectf((GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
-}
-
-void STATE_APIENTRY
-crStateFeedbackRectd(GLdouble x0, GLdouble y0, GLdouble x1, GLdouble y1)
-{
-   crStateFeedbackRectf((GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
-}
-
-void STATE_APIENTRY
-crStateFeedbackRects(GLshort x0, GLshort y0, GLshort x1, GLshort y1)
-{
-   crStateFeedbackRectf((GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
-}
-
-void STATE_APIENTRY
-crStateFeedbackRectiv(const GLint *v0, const GLint *v1)
-{
-   crStateFeedbackRectf((GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
-}
-
-void STATE_APIENTRY
-crStateFeedbackRectfv(const GLfloat *v0, const GLfloat *v1)
-{
-   crStateFeedbackRectf(v0[0], v0[1], v1[0], v1[1]);
-}
-
-void STATE_APIENTRY
-crStateFeedbackRectdv(const GLdouble *v0, const GLdouble *v1)
-{
-   crStateFeedbackRectf((GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
-}
-
-void STATE_APIENTRY
-crStateFeedbackRectsv(const GLshort *v0, const GLshort *v1)
-{
-   crStateFeedbackRectf((GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
+crStateFeedbackTexCoord4fv(PCRStateTracker pState, const GLfloat *v )
+{
+	crStateFeedbackTexCoord4f(pState, v[0], v[1], v[2], v[3] );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord4s(PCRStateTracker pState, GLshort v0, GLshort v1, GLshort v2, GLshort v3 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, (GLfloat)v3 );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord4sv(PCRStateTracker pState, const GLshort *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], (GLfloat)v[1], (GLfloat)v[2], (GLfloat)v[3] );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord4i(PCRStateTracker pState, GLint v0, GLint v1, GLint v2, GLint v3 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, (GLfloat)v3 );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord4iv(PCRStateTracker pState, const GLint *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], (GLfloat)v[1], (GLfloat)v[2], (GLfloat)v[3] );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord4d(PCRStateTracker pState, GLdouble v0, GLdouble v1, GLdouble v2, GLdouble v3 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, (GLfloat)v3 );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord4dv(PCRStateTracker pState, const GLdouble *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], (GLfloat)v[1], (GLfloat)v[2], (GLfloat)v[3] );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord1i(PCRStateTracker pState, GLint v0 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, 0.0f, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord1iv(PCRStateTracker pState, const GLint *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], 0.0f, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord1s(PCRStateTracker pState, GLshort v0 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, 0.0f, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord1sv(PCRStateTracker pState, const GLshort *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], 0.0f, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord1f(PCRStateTracker pState, GLfloat v0 )
+{
+	crStateFeedbackTexCoord4f(pState, v0, 0.0f, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord1fv(PCRStateTracker pState, const GLfloat *v )
+{
+	crStateFeedbackTexCoord4f(pState, v[0], 0.0f, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord1d(PCRStateTracker pState, GLdouble v0 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, 0.0f, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord1dv(PCRStateTracker pState, const GLdouble *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], 0.0f, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord2i(PCRStateTracker pState, GLint v0, GLint v1 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, (GLfloat)v1, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord2iv(PCRStateTracker pState, const GLint *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], (GLfloat)v[1], 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord2s(PCRStateTracker pState, GLshort v0, GLshort v1 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, (GLfloat)v1, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord2sv(PCRStateTracker pState, const GLshort *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], (GLfloat)v[1], 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord2f(PCRStateTracker pState, GLfloat v0, GLfloat v1 )
+{
+	crStateFeedbackTexCoord4f(pState, v0, v1, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord2fv(PCRStateTracker pState, const GLfloat *v )
+{
+	crStateFeedbackTexCoord4f(pState, v[0], v[1], 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord2d(PCRStateTracker pState, GLdouble v0, GLdouble v1 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, (GLfloat)v1, 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord2dv(PCRStateTracker pState, const GLdouble *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], (GLfloat)v[1], 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord3i(PCRStateTracker pState, GLint v0, GLint v1, GLint v2 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord3iv(PCRStateTracker pState, const GLint *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], (GLfloat)v[1], 0.0f, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord3s(PCRStateTracker pState, GLshort v0, GLshort v1, GLshort v2 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord3sv(PCRStateTracker pState, const GLshort *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], (GLfloat)v[1], (GLfloat)v[2], 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord3f(PCRStateTracker pState, GLfloat v0, GLfloat v1, GLfloat v2 )
+{
+	crStateFeedbackTexCoord4f(pState, v0, v1, v2, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord3fv(PCRStateTracker pState, const GLfloat *v )
+{
+	crStateFeedbackTexCoord4f(pState, v[0], v[1], v[2], 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord3d(PCRStateTracker pState, GLdouble v0, GLdouble v1, GLdouble v2 )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v0, (GLfloat)v1, (GLfloat)v2, 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackTexCoord3dv(PCRStateTracker pState, const GLdouble *v )
+{
+	crStateFeedbackTexCoord4f(pState, (GLfloat)v[0], (GLfloat)v[1], (GLfloat)v[2], 1.0f );
+}
+
+void STATE_APIENTRY
+crStateFeedbackRectf(PCRStateTracker pState, GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1)
+{
+   crStateFeedbackBegin(pState, GL_QUADS);
+   crStateFeedbackVertex2f(pState, x0, y0);
+   crStateFeedbackVertex2f(pState, x0, y1);
+   crStateFeedbackVertex2f(pState, x1, y1);
+   crStateFeedbackVertex2f(pState, x1, y0);
+   crStateFeedbackEnd(pState);
+}
+
+void STATE_APIENTRY
+crStateFeedbackRecti(PCRStateTracker pState, GLint x0, GLint y0, GLint x1, GLint y1)
+{
+   crStateFeedbackRectf(pState, (GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
+}
+
+void STATE_APIENTRY
+crStateFeedbackRectd(PCRStateTracker pState, GLdouble x0, GLdouble y0, GLdouble x1, GLdouble y1)
+{
+   crStateFeedbackRectf(pState, (GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
+}
+
+void STATE_APIENTRY
+crStateFeedbackRects(PCRStateTracker pState, GLshort x0, GLshort y0, GLshort x1, GLshort y1)
+{
+   crStateFeedbackRectf(pState, (GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
+}
+
+void STATE_APIENTRY
+crStateFeedbackRectiv(PCRStateTracker pState, const GLint *v0, const GLint *v1)
+{
+   crStateFeedbackRectf(pState, (GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
+}
+
+void STATE_APIENTRY
+crStateFeedbackRectfv(PCRStateTracker pState, const GLfloat *v0, const GLfloat *v1)
+{
+   crStateFeedbackRectf(pState, v0[0], v0[1], v1[0], v1[1]);
+}
+
+void STATE_APIENTRY
+crStateFeedbackRectdv(PCRStateTracker pState, const GLdouble *v0, const GLdouble *v1)
+{
+   crStateFeedbackRectf(pState, (GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
+}
+
+void STATE_APIENTRY
+crStateFeedbackRectsv(PCRStateTracker pState, const GLshort *v0, const GLshort *v1)
+{
+   crStateFeedbackRectf(pState, (GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
 }
 
@@ -1479,12 +1473,12 @@
 
 void STATE_APIENTRY
-crStateSelectBuffer(GLsizei size, GLuint * buffer)
-{
-	CRContext *g = GetCurrentContext();
+crStateSelectBuffer(PCRStateTracker pState, GLsizei size, GLuint * buffer)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRSelectionState *se = &(g->selection);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "SelectBuffer called in begin/end");
 		return;
@@ -1493,5 +1487,5 @@
 	if (g->renderMode == GL_SELECT)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "SelectBuffer called with RenderMode = GL_SELECT");
 		return;
@@ -1545,12 +1539,12 @@
 
 void STATE_APIENTRY
-crStateInitNames(void)
-{
-	CRContext *g = GetCurrentContext();
+crStateInitNames(PCRStateTracker pState)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRSelectionState *se = &(g->selection);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "InitNames called in begin/end");
 		return;
@@ -1576,12 +1570,12 @@
 
 void STATE_APIENTRY
-crStateLoadName(GLuint name)
-{
-	CRContext *g = GetCurrentContext();
+crStateLoadName(PCRStateTracker pState, GLuint name)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRSelectionState *se = &(g->selection);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "LoadName called in begin/end");
 		return;
@@ -1596,5 +1590,5 @@
 	if (se->nameStackDepth == 0)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "nameStackDepth = 0");
 		return;
@@ -1619,12 +1613,12 @@
 
 void STATE_APIENTRY
-crStatePushName(GLuint name)
-{
-	CRContext *g = GetCurrentContext();
+crStatePushName(PCRStateTracker pState, GLuint name)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRSelectionState *se = &(g->selection);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "PushName called in begin/end");
 		return;
@@ -1645,5 +1639,5 @@
 	if (se->nameStackDepth >= MAX_NAME_STACK_DEPTH)
 	{
-		crStateError(__LINE__, __FILE__, GL_STACK_OVERFLOW,
+		crStateError(pState, __LINE__, __FILE__, GL_STACK_OVERFLOW,
 								 "nameStackDepth overflow");
 	}
@@ -1653,12 +1647,12 @@
 
 void STATE_APIENTRY
-crStatePopName(void)
-{
-	CRContext *g = GetCurrentContext();
+crStatePopName(PCRStateTracker pState)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRSelectionState *se = &(g->selection);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "PopName called in begin/end");
 		return;
@@ -1679,5 +1673,5 @@
 	if (se->nameStackDepth == 0)
 	{
-		crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW,
+		crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW,
 								 "nameStackDepth underflow");
 	}
@@ -1688,7 +1682,6 @@
 
 static void
-update_hitflag(GLfloat z)
-{
-	CRContext *g = GetCurrentContext();
+update_hitflag(CRContext *g, GLfloat z)
+{
 	CRSelectionState *se = &(g->selection);
 
@@ -1704,21 +1697,18 @@
 
 static void
-select_rasterpos(void)
-{
-	CRContext *g = GetCurrentContext();
-   
+select_rasterpos(CRContext *g)
+{
 	if (g->current.rasterValid)
-		update_hitflag(g->current.rasterAttrib[VERT_ATTRIB_POS][2]);
+		update_hitflag(g, g->current.rasterAttrib[VERT_ATTRIB_POS][2]);
 }
 
 static void
-select_point(const CRVertex *v)
-{
-	CRContext *g = GetCurrentContext();
-	if (clip_point(v) == 0)
+select_point(CRContext *g, const CRVertex *v)
+{
+	if (clip_point(g, v) == 0)
 	{
 		CRVertex c = *v;
 		MAP_POINT(c.winPos, c.clipPos, g->viewport);
-		update_hitflag(c.winPos.z);
+		update_hitflag(g, c.winPos.z);
 	}
 }
@@ -1726,14 +1716,13 @@
 
 static void
-select_line(const CRVertex *v0, const CRVertex *v1)
-{
-	CRContext *g = GetCurrentContext();
+select_line(CRContext *g, const CRVertex *v0, const CRVertex *v1)
+{
 	CRVertex c0, c1;
-	if (clip_line(v0, v1, &c0, &c1))
+	if (clip_line(g, v0, v1, &c0, &c1))
 	{
 		MAP_POINT(c0.winPos, c0.clipPos, g->viewport);
 		MAP_POINT(c1.winPos, c1.clipPos, g->viewport);
-		update_hitflag(c0.winPos.z);
-		update_hitflag(c1.winPos.z);
+		update_hitflag(g, c0.winPos.z);
+		update_hitflag(g, c1.winPos.z);
 	}
 }
@@ -1741,9 +1730,8 @@
 
 static void
-select_triangle(const CRVertex *v0,
+select_triangle(CRContext *g, const CRVertex *v0,
 								const CRVertex *v1,
 								const CRVertex *v2)
 {
-	CRContext *g = GetCurrentContext();
 	CRVertex vlist[3], vclipped[8];
 	GLuint i, n;
@@ -1755,13 +1743,13 @@
 	for (i = 0; i < n; i++) {
 		MAP_POINT(vclipped[i].winPos, vclipped[i].clipPos, g->viewport);
-		update_hitflag(vclipped[i].winPos.z);
-	}
-}
-
-
-void STATE_APIENTRY
-crStateSelectVertex4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-	CRContext *g = GetCurrentContext();
+		update_hitflag(g, vclipped[i].winPos.z);
+	}
+}
+
+
+void STATE_APIENTRY
+crStateSelectVertex4f(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRTransformState *t = &(g->transform);
 	CRVertex *v = g->vBuffer + g->vCount;
@@ -1785,5 +1773,5 @@
 	case GL_POINTS:
 		CRASSERT(g->vCount == 0);
-		select_point(v);
+		select_point(g, v);
 		break;
 	case GL_LINES:
@@ -1795,5 +1783,5 @@
 		{
 			CRASSERT(g->vCount == 1);
-			select_line(g->vBuffer + 0, g->vBuffer + 1);
+			select_line(g, g->vBuffer + 0, g->vBuffer + 1);
 			g->vCount = 0;
 		}
@@ -1807,5 +1795,5 @@
 		{
 			CRASSERT(g->vCount == 1);
-			select_line(g->vBuffer + 0, g->vBuffer + 1);
+			select_line(g, g->vBuffer + 0, g->vBuffer + 1);
 			g->vBuffer[0] = g->vBuffer[1];
 			/* leave g->vCount at 1 */
@@ -1820,5 +1808,5 @@
 		else if (g->vCount == 1)
 		{
-			select_line(g->vBuffer + 0, g->vBuffer + 1);
+			select_line(g, g->vBuffer + 0, g->vBuffer + 1);
 			g->lineLoop = GL_TRUE;
 			g->vCount = 2;
@@ -1828,5 +1816,5 @@
 			CRASSERT(g->vCount == 2);
 			g->lineLoop = GL_FALSE;
-			select_line(g->vBuffer + 1, g->vBuffer + 2);
+			select_line(g, g->vBuffer + 1, g->vBuffer + 2);
 			g->vBuffer[1] = g->vBuffer[2];
 			/* leave g->vCount at 2 */
@@ -1841,5 +1829,5 @@
 		{
 			CRASSERT(g->vCount == 2);
-			select_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			select_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
 			g->vCount = 0;
 		}
@@ -1852,5 +1840,5 @@
 		else if (g->vCount == 2)
 		{
-			select_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			select_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
 			g->vCount = 3;
 		}
@@ -1858,5 +1846,5 @@
 		{
 			CRASSERT(g->vCount == 3);
-			select_triangle(g->vBuffer + 1, g->vBuffer + 3, g->vBuffer + 2);
+			select_triangle(g, g->vBuffer + 1, g->vBuffer + 3, g->vBuffer + 2);
 			g->vBuffer[0] = g->vBuffer[2];
 			g->vBuffer[1] = g->vBuffer[3];
@@ -1872,5 +1860,5 @@
 		{
 			CRASSERT(g->vCount == 2);
-			select_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			select_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
 			g->vBuffer[1] = g->vBuffer[2];
 			/* leave g->vCount = 2 */
@@ -1885,6 +1873,6 @@
 		{
 			CRASSERT(g->vCount == 3);
-			select_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
-			select_triangle(g->vBuffer + 0, g->vBuffer + 2, g->vBuffer + 3);
+			select_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			select_triangle(g, g->vBuffer + 0, g->vBuffer + 2, g->vBuffer + 3);
 			g->vCount = 0;
 		}
@@ -1898,6 +1886,6 @@
 		{
 			CRASSERT(g->vCount == 3);
-			select_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
-			select_triangle(g->vBuffer + 1, g->vBuffer + 3, g->vBuffer + 2);
+			select_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			select_triangle(g, g->vBuffer + 1, g->vBuffer + 3, g->vBuffer + 2);
 			g->vBuffer[0] = g->vBuffer[2];
 			g->vBuffer[1] = g->vBuffer[3];
@@ -1914,5 +1902,5 @@
 		{
 			CRASSERT(g->vCount == 2);
-			select_triangle(g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
+			select_triangle(g, g->vBuffer + 0, g->vBuffer + 1, g->vBuffer + 2);
 			g->vBuffer[1] = g->vBuffer[2];
 			/* leave g->vCount = 2 */
@@ -1925,17 +1913,18 @@
 
 void STATE_APIENTRY
-crStateSelectRasterPos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-	crStateRasterPos4f( x, y, z, w );
-
-	select_rasterpos();
-}
-
-void STATE_APIENTRY
-crStateSelectBegin(GLenum mode)
-{
-	CRContext *g = GetCurrentContext();
-
-	crStateBegin(mode);
+crStateSelectRasterPos4f(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+{
+	CRContext *g = GetCurrentContext(pState);
+	crStateRasterPos4f(pState, x, y, z, w );
+
+	select_rasterpos(g);
+}
+
+void STATE_APIENTRY
+crStateSelectBegin(PCRStateTracker pState, GLenum mode)
+{
+	CRContext *g = GetCurrentContext(pState);
+
+	crStateBegin(pState, mode);
 
 	g->vCount = 0;
@@ -1946,351 +1935,351 @@
 
 void STATE_APIENTRY
-crStateSelectEnd(void)
-{
-	CRContext *g = GetCurrentContext();
+crStateSelectEnd(PCRStateTracker pState)
+{
+	CRContext *g = GetCurrentContext(pState);
 
 	if (g->current.mode == GL_LINE_LOOP && g->vCount == 2)
 	{
 		/* draw the last line segment */
-		select_line(g->vBuffer + 1, g->vBuffer + 0);
-	}
-
-	crStateEnd();
-}
-
-void STATE_APIENTRY
-crStateSelectVertex2d(GLdouble x, GLdouble y)
-{
-	crStateSelectVertex4f((GLfloat) x, (GLfloat) y, 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex2dv(const GLdouble * v)
-{
-	crStateSelectVertex4f((GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex2f(GLfloat x, GLfloat y)
-{
-	crStateSelectVertex4f(x, y, 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex2fv(const GLfloat * v)
-{
-	crStateSelectVertex4f(v[0], v[1], 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex2i(GLint x, GLint y)
-{
-	crStateSelectVertex4f((GLfloat) x, (GLfloat) y, 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex2iv(const GLint * v)
-{
-	crStateSelectVertex4f((GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex2s(GLshort x, GLshort y)
-{
-	crStateSelectVertex4f((GLfloat) x, (GLfloat) y, 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex2sv(const GLshort * v)
-{
-	crStateSelectVertex4f((GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex3d(GLdouble x, GLdouble y, GLdouble z)
-{
-	crStateSelectVertex4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex3dv(const GLdouble * v)
-{
-	crStateSelectVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex3f(GLfloat x, GLfloat y, GLfloat z)
-{
-	crStateSelectVertex4f(x, y, z, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex3fv(const GLfloat * v)
-{
-	crStateSelectVertex4f(v[0], v[1], v[2], 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex3i(GLint x, GLint y, GLint z)
-{
-	crStateSelectVertex4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex3iv(const GLint * v)
-{
-	crStateSelectVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex3s(GLshort x, GLshort y, GLshort z)
-{
-	crStateSelectVertex4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex3sv(const GLshort * v)
-{
-	crStateSelectVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex4d(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
-{
-	crStateSelectVertex4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex4dv(const GLdouble * v)
-{
-	crStateSelectVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex4fv(const GLfloat * v)
-{
-	crStateSelectVertex4f(v[0], v[1], v[2], v[3]);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex4i(GLint x, GLint y, GLint z, GLint w)
-{
-	crStateSelectVertex4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex4iv(const GLint * v)
-{
-	crStateSelectVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex4s(GLshort x, GLshort y, GLshort z, GLshort w)
-{
-	crStateSelectVertex4f(x, y, z, w);
-}
-
-void STATE_APIENTRY
-crStateSelectVertex4sv(const GLshort * v)
-{
-	crStateSelectVertex4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos2d(GLdouble x, GLdouble y)
-{
-	crStateSelectRasterPos4f((GLfloat) x, (GLfloat) y, 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos2dv(const GLdouble * v)
-{
-	crStateSelectRasterPos4f((GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos2f(GLfloat x, GLfloat y)
-{
-	crStateSelectRasterPos4f(x, y, 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos2fv(const GLfloat * v)
-{
-	crStateSelectRasterPos4f(v[0], v[1], 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos2i(GLint x, GLint y)
-{
-	crStateSelectRasterPos4f((GLfloat) x, (GLfloat) y, 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos2iv(const GLint * v)
-{
-	crStateSelectRasterPos4f((GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos2s(GLshort x, GLshort y)
-{
-	crStateSelectRasterPos4f((GLfloat) x, (GLfloat) y, 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos2sv(const GLshort * v)
-{
-	crStateSelectRasterPos4f((GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos3d(GLdouble x, GLdouble y, GLdouble z)
-{
-	crStateSelectRasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos3dv(const GLdouble * v)
-{
-	crStateSelectRasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos3f(GLfloat x, GLfloat y, GLfloat z)
-{
-	crStateSelectRasterPos4f(x, y, z, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos3fv(const GLfloat * v)
-{
-	crStateSelectRasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos3i(GLint x, GLint y, GLint z)
-{
-	crStateSelectRasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos3iv(const GLint * v)
-{
-	crStateSelectRasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos3s(GLshort x, GLshort y, GLshort z)
-{
-	crStateSelectRasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos3sv(const GLshort * v)
-{
-	crStateSelectRasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , 1.0);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos4d(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
-{
-	crStateSelectRasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos4dv(const GLdouble * v)
-{
-	crStateSelectRasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , (GLfloat) v[3]);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos4fv(const GLfloat * v)
-{
-	crStateSelectRasterPos4f(v[0], v[1], v[2], v[3]);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos4i(GLint x, GLint y, GLint z, GLint w)
-{
-	crStateSelectRasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos4iv(const GLint * v)
-{
-	crStateSelectRasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos4s(GLshort x, GLshort y, GLshort z, GLshort w)
-{
-	crStateSelectRasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-void STATE_APIENTRY
-crStateSelectRasterPos4sv(const GLshort * v)
-{
-	crStateSelectRasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , (GLfloat) v[3]);
-}
-
-void STATE_APIENTRY
-crStateSelectRectf(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1)
-{
-   crStateSelectBegin(GL_QUADS);
-   crStateSelectVertex2f(x0, y0);
-   crStateSelectVertex2f(x0, y1);
-   crStateSelectVertex2f(x1, y1);
-   crStateSelectVertex2f(x1, y0);
-   crStateSelectEnd();
-}
-
-void STATE_APIENTRY
-crStateSelectRecti(GLint x0, GLint y0, GLint x1, GLint y1)
-{
-   crStateSelectRectf((GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
-}
-
-void STATE_APIENTRY
-crStateSelectRectd(GLdouble x0, GLdouble y0, GLdouble x1, GLdouble y1)
-{
-   crStateSelectRectf((GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
-}
-
-void STATE_APIENTRY
-crStateSelectRects(GLshort x0, GLshort y0, GLshort x1, GLshort y1)
-{
-   crStateSelectRectf((GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
-}
-
-void STATE_APIENTRY
-crStateSelectRectiv(const GLint *v0, const GLint *v1)
-{
-   crStateSelectRectf((GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
-}
-
-void STATE_APIENTRY
-crStateSelectRectfv(const GLfloat *v0, const GLfloat *v1)
-{
-   crStateSelectRectf(v0[0], v0[1], v1[0], v1[1]);
-}
-
-void STATE_APIENTRY
-crStateSelectRectdv(const GLdouble *v0, const GLdouble *v1)
-{
-   crStateSelectRectf((GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
-}
-
-void STATE_APIENTRY
-crStateSelectRectsv(const GLshort *v0, const GLshort *v1)
-{
-   crStateSelectRectf((GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
+		select_line(g, g->vBuffer + 1, g->vBuffer + 0);
+	}
+
+	crStateEnd(pState);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex2d(PCRStateTracker pState, GLdouble x, GLdouble y)
+{
+	crStateSelectVertex4f(pState, (GLfloat) x, (GLfloat) y, 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex2dv(PCRStateTracker pState, const GLdouble * v)
+{
+	crStateSelectVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex2f(PCRStateTracker pState, GLfloat x, GLfloat y)
+{
+	crStateSelectVertex4f(pState, x, y, 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex2fv(PCRStateTracker pState, const GLfloat * v)
+{
+	crStateSelectVertex4f(pState, v[0], v[1], 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex2i(PCRStateTracker pState, GLint x, GLint y)
+{
+	crStateSelectVertex4f(pState, (GLfloat) x, (GLfloat) y, 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex2iv(PCRStateTracker pState, const GLint * v)
+{
+	crStateSelectVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex2s(PCRStateTracker pState, GLshort x, GLshort y)
+{
+	crStateSelectVertex4f(pState, (GLfloat) x, (GLfloat) y, 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex2sv(PCRStateTracker pState, const GLshort * v)
+{
+	crStateSelectVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex3d(PCRStateTracker pState, GLdouble x, GLdouble y, GLdouble z)
+{
+	crStateSelectVertex4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex3dv(PCRStateTracker pState, const GLdouble * v)
+{
+	crStateSelectVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex3f(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z)
+{
+	crStateSelectVertex4f(pState, x, y, z, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex3fv(PCRStateTracker pState, const GLfloat * v)
+{
+	crStateSelectVertex4f(pState, v[0], v[1], v[2], 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex3i(PCRStateTracker pState, GLint x, GLint y, GLint z)
+{
+	crStateSelectVertex4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex3iv(PCRStateTracker pState, const GLint * v)
+{
+	crStateSelectVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex3s(PCRStateTracker pState, GLshort x, GLshort y, GLshort z)
+{
+	crStateSelectVertex4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex3sv(PCRStateTracker pState, const GLshort * v)
+{
+	crStateSelectVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex4d(PCRStateTracker pState, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
+{
+	crStateSelectVertex4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex4dv(PCRStateTracker pState, const GLdouble * v)
+{
+	crStateSelectVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex4fv(PCRStateTracker pState, const GLfloat * v)
+{
+	crStateSelectVertex4f(pState, v[0], v[1], v[2], v[3]);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex4i(PCRStateTracker pState, GLint x, GLint y, GLint z, GLint w)
+{
+	crStateSelectVertex4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex4iv(PCRStateTracker pState, const GLint * v)
+{
+	crStateSelectVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex4s(PCRStateTracker pState, GLshort x, GLshort y, GLshort z, GLshort w)
+{
+	crStateSelectVertex4f(pState, x, y, z, w);
+}
+
+void STATE_APIENTRY
+crStateSelectVertex4sv(PCRStateTracker pState, const GLshort * v)
+{
+	crStateSelectVertex4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos2d(PCRStateTracker pState, GLdouble x, GLdouble y)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) x, (GLfloat) y, 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos2dv(PCRStateTracker pState, const GLdouble * v)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos2f(PCRStateTracker pState, GLfloat x, GLfloat y)
+{
+	crStateSelectRasterPos4f(pState, x, y, 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos2fv(PCRStateTracker pState, const GLfloat * v)
+{
+	crStateSelectRasterPos4f(pState, v[0], v[1], 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos2i(PCRStateTracker pState, GLint x, GLint y)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) x, (GLfloat) y, 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos2iv(PCRStateTracker pState, const GLint * v)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos2s(PCRStateTracker pState, GLshort x, GLshort y)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) x, (GLfloat) y, 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos2sv(PCRStateTracker pState, const GLshort * v)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos3d(PCRStateTracker pState, GLdouble x, GLdouble y, GLdouble z)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos3dv(PCRStateTracker pState, const GLdouble * v)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos3f(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z)
+{
+	crStateSelectRasterPos4f(pState, x, y, z, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos3fv(PCRStateTracker pState, const GLfloat * v)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos3i(PCRStateTracker pState, GLint x, GLint y, GLint z)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos3iv(PCRStateTracker pState, const GLint * v)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos3s(PCRStateTracker pState, GLshort x, GLshort y, GLshort z)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos3sv(PCRStateTracker pState, const GLshort * v)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , 1.0);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos4d(PCRStateTracker pState, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos4dv(PCRStateTracker pState, const GLdouble * v)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , (GLfloat) v[3]);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos4fv(PCRStateTracker pState, const GLfloat * v)
+{
+	crStateSelectRasterPos4f(pState, v[0], v[1], v[2], v[3]);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos4i(PCRStateTracker pState, GLint x, GLint y, GLint z, GLint w)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos4iv(PCRStateTracker pState, const GLint * v)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos4s(PCRStateTracker pState, GLshort x, GLshort y, GLshort z, GLshort w)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+void STATE_APIENTRY
+crStateSelectRasterPos4sv(PCRStateTracker pState, const GLshort * v)
+{
+	crStateSelectRasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2] , (GLfloat) v[3]);
+}
+
+void STATE_APIENTRY
+crStateSelectRectf(PCRStateTracker pState, GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1)
+{
+   crStateSelectBegin(pState, GL_QUADS);
+   crStateSelectVertex2f(pState, x0, y0);
+   crStateSelectVertex2f(pState, x0, y1);
+   crStateSelectVertex2f(pState, x1, y1);
+   crStateSelectVertex2f(pState, x1, y0);
+   crStateSelectEnd(pState);
+}
+
+void STATE_APIENTRY
+crStateSelectRecti(PCRStateTracker pState, GLint x0, GLint y0, GLint x1, GLint y1)
+{
+   crStateSelectRectf(pState, (GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
+}
+
+void STATE_APIENTRY
+crStateSelectRectd(PCRStateTracker pState, GLdouble x0, GLdouble y0, GLdouble x1, GLdouble y1)
+{
+   crStateSelectRectf(pState, (GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
+}
+
+void STATE_APIENTRY
+crStateSelectRects(PCRStateTracker pState, GLshort x0, GLshort y0, GLshort x1, GLshort y1)
+{
+   crStateSelectRectf(pState, (GLfloat) x0, (GLfloat) y0, (GLfloat) x1, (GLfloat) y1);
+}
+
+void STATE_APIENTRY
+crStateSelectRectiv(PCRStateTracker pState, const GLint *v0, const GLint *v1)
+{
+   crStateSelectRectf(pState, (GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
+}
+
+void STATE_APIENTRY
+crStateSelectRectfv(PCRStateTracker pState, const GLfloat *v0, const GLfloat *v1)
+{
+   crStateSelectRectf(pState, v0[0], v0[1], v1[0], v1[1]);
+}
+
+void STATE_APIENTRY
+crStateSelectRectdv(PCRStateTracker pState, const GLdouble *v0, const GLdouble *v1)
+{
+   crStateSelectRectf(pState, (GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
+}
+
+void STATE_APIENTRY
+crStateSelectRectsv(PCRStateTracker pState, const GLshort *v0, const GLshort *v1)
+{
+   crStateSelectRectf(pState, (GLfloat) v0[0], (GLfloat) v0[1], (GLfloat) v1[0], (GLfloat) v1[1]);
 }
 
 
 GLint STATE_APIENTRY
-crStateRenderMode(GLenum mode)
-{
-	CRContext *g = GetCurrentContext();
+crStateRenderMode(PCRStateTracker pState, GLenum mode)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRFeedbackState *f = &(g->feedback);
 	CRSelectionState *se = &(g->selection);
@@ -2299,5 +2288,5 @@
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "RenderMode called in begin/end");
 		return 0;
@@ -2343,5 +2332,5 @@
 		break;
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "invalid rendermode");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "invalid rendermode");
 		return 0;
 	}
@@ -2355,5 +2344,5 @@
 		{
 			/* haven't called glSelectBuffer yet */
-			crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 									 "buffersize = 0");
 		}
@@ -2363,10 +2352,10 @@
 		{
 			/* haven't called glFeedbackBuffer yet */
-			crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 									 "buffersize = 0");
 		}
 		break;
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "invalid rendermode");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "invalid rendermode");
 		return 0;
 	}
@@ -2379,7 +2368,8 @@
 
 void STATE_APIENTRY
-crStateSelectDrawPixels(GLsizei width, GLsizei height, GLenum format,
+crStateSelectDrawPixels(PCRStateTracker pState, GLsizei width, GLsizei height, GLenum format,
 												GLenum type, const GLvoid * pixels)
 {
+	CRContext *g = GetCurrentContext(pState);
 	(void) width;
 	(void) height;
@@ -2387,11 +2377,12 @@
 	(void) type;
 	(void) pixels;
-	select_rasterpos();
-}
-
-void STATE_APIENTRY
-crStateSelectCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height,
+	select_rasterpos(g);
+}
+
+void STATE_APIENTRY
+crStateSelectCopyPixels(PCRStateTracker pState, GLint x, GLint y, GLsizei width, GLsizei height,
 												GLenum type)
 {
+	CRContext *g = GetCurrentContext(pState);
 	(void) x;
 	(void) y;
@@ -2399,13 +2390,13 @@
 	(void) height;
 	(void) type;
-	select_rasterpos();
-}
-
-void STATE_APIENTRY
-crStateSelectBitmap(GLsizei width, GLsizei height, GLfloat xorig,
+	select_rasterpos(g);
+}
+
+void STATE_APIENTRY
+crStateSelectBitmap(PCRStateTracker pState, GLsizei width, GLsizei height, GLfloat xorig,
 										GLfloat yorig, GLfloat xmove, GLfloat ymove,
 										const GLubyte * bitmap)
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	(void) width;
 	(void) height;
@@ -2414,5 +2405,5 @@
 	(void) bitmap;
 
-	select_rasterpos();
+	select_rasterpos(g);
 	if (g->current.rasterValid)
 	{
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_fence.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_fence.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_fence.c	(revision 78375)
@@ -11,40 +11,40 @@
 
 
-void APIENTRY crStateGenFencesNV(GLsizei n, GLuint *fences)
+void APIENTRY crStateGenFencesNV(PCRStateTracker pState, GLsizei n, GLuint *fences)
 {
-    (void)n; (void)fences;
+    RT_NOREF(pState, n, fences);
 }
 
 
-void APIENTRY crStateDeleteFencesNV(GLsizei n, const GLuint *fences)
+void APIENTRY crStateDeleteFencesNV(PCRStateTracker pState, GLsizei n, const GLuint *fences)
 {
-    (void)n; (void)fences;
+    RT_NOREF(pState, n, fences);
 }
 
-void APIENTRY crStateSetFenceNV(GLuint fence, GLenum condition)
+void APIENTRY crStateSetFenceNV(PCRStateTracker pState, GLuint fence, GLenum condition)
 {
-    (void)fence; (void)condition;
+    RT_NOREF(pState, fence, condition);
 }
 
-GLboolean APIENTRY crStateTestFenceNV(GLuint fence)
+GLboolean APIENTRY crStateTestFenceNV(PCRStateTracker pState, GLuint fence)
 {
-    (void)fence;
+    RT_NOREF(pState, fence);
     return GL_FALSE;
 }
 
-void APIENTRY crStateFinishFenceNV(GLuint fence)
+void APIENTRY crStateFinishFenceNV(PCRStateTracker pState, GLuint fence)
 {
-    (void)fence;
+    RT_NOREF(pState, fence);
 }
 
-GLboolean APIENTRY crStateIsFenceNV(GLuint fence)
+GLboolean APIENTRY crStateIsFenceNV(PCRStateTracker pState, GLuint fence)
 {
-    (void)fence;
+    RT_NOREF(pState, fence);
     return GL_FALSE;
 }
 
-void APIENTRY crStateGetFenceivNV(GLuint fence, GLenum pname, GLint *params)
+void APIENTRY crStateGetFenceivNV(PCRStateTracker pState, GLuint fence, GLenum pname, GLint *params)
 {
-    (void)fence; (void)pname; (void)params;
+    RT_NOREF(pState, fence, pname, params);
 }
 
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_flush.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_flush.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_flush.c	(revision 78375)
@@ -8,27 +8,25 @@
 #include "cr_spu.h"
 
-SPUDispatchTable diff_api;
-
-void crStateFlushFunc( CRStateFlushFunc func )
+void crStateFlushFunc(PCRStateTracker pState, CRStateFlushFunc func )
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 
 	g->flush_func = func;
 }
 
-void crStateFlushArg( void *arg )
+void crStateFlushArg(PCRStateTracker pState, void *arg )
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 
 	g->flush_arg = arg;
 }
 
-void crStateDiffAPI( SPUDispatchTable *api )
+void crStateDiffAPI(PCRStateTracker pState, SPUDispatchTable *api )
 {
-	if (!diff_api.AlphaFunc) 
+	if (!pState->diff_api.AlphaFunc) 
 	{
 		/* Called when starting up Chromium */
-		crSPUInitDispatchTable( &(diff_api) );
+		crSPUInitDispatchTable( &pState->diff_api );
 	}
-	crSPUCopyDispatchTable( &(diff_api), api );
+	crSPUCopyDispatchTable( &pState->diff_api, api );
 }
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_fog.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_fog.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_fog.c	(revision 78375)
@@ -13,5 +13,5 @@
 {
 	CRFogState *f = &ctx->fog;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
 	CRFogBits *fb = &(sb->fog);
 	GLcolorf black = {0.0f, 0.0f, 0.0f, 0.0f};
@@ -43,16 +43,16 @@
 }
 
-void STATE_APIENTRY crStateFogf(GLenum pname, GLfloat param) 
+void STATE_APIENTRY crStateFogf(PCRStateTracker pState, GLenum pname, GLfloat param) 
 {
-	crStateFogfv( pname, &param );
+	crStateFogfv(pState, pname, &param );
 }
 
-void STATE_APIENTRY crStateFogi(GLenum pname, GLint param) 
+void STATE_APIENTRY crStateFogi(PCRStateTracker pState, GLenum pname, GLint param) 
 {
 	GLfloat f_param = (GLfloat) param;
-	crStateFogfv( pname, &f_param );
+	crStateFogfv(pState, pname, &f_param );
 }
 
-void STATE_APIENTRY crStateFogiv(GLenum pname, const GLint *param) 
+void STATE_APIENTRY crStateFogiv(PCRStateTracker pState, GLenum pname, const GLint *param) 
 {
 	GLcolor f_color;
@@ -66,5 +66,5 @@
 		case GL_FOG_INDEX:
 			f_param = (GLfloat) (*param);
-			crStateFogfv( pname, &f_param );
+			crStateFogfv(pState, pname, &f_param );
 			break;
 		case GL_FOG_COLOR:
@@ -73,10 +73,10 @@
 			f_color.b = ((GLfloat) param[2]) / ((GLfloat) CR_MAXINT);
 			f_color.a = ((GLfloat) param[3]) / ((GLfloat) CR_MAXINT);
-			crStateFogfv( pname, (GLfloat *) &f_color );
+			crStateFogfv(pState, pname, (GLfloat *) &f_color );
 			break;
 #ifdef CR_NV_fog_distance
 		case GL_FOG_DISTANCE_MODE_NV:
 			f_param = (GLfloat) (*param);
-			crStateFogfv( pname, &f_param );
+			crStateFogfv(pState, pname, &f_param );
 			break;
 #endif
@@ -84,23 +84,23 @@
 		case GL_FOG_COORDINATE_SOURCE_EXT:
 			f_param = (GLfloat) (*param);
-			crStateFogfv( pname, &f_param );
+			crStateFogfv(pState, pname, &f_param );
 			break;
 #endif
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Invalid glFog Param: %d", param);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Invalid glFog Param: %d", param);
 			return;
 	}
 }
 
-void STATE_APIENTRY crStateFogfv(GLenum pname, const GLfloat *param) 
+void STATE_APIENTRY crStateFogfv(PCRStateTracker pState, GLenum pname, const GLfloat *param) 
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRFogState *f = &(g->fog);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRFogBits *fb = &(sb->fog);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glFogfv called in Begin/End");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glFogfv called in Begin/End");
 		return;
 	}
@@ -115,5 +115,5 @@
 				if (e != GL_LINEAR && e != GL_EXP && e != GL_EXP2)
 				{
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid param for glFog: %d", e);
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid param for glFog: %d", e);
 					return;
 				}
@@ -157,5 +157,5 @@
 					param[0] != GL_EYE_PLANE_ABSOLUTE_NV )
 				{
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 						"Fogfv: GL_FOG_DISTANCE_MODE_NV called with illegal parameter: 0x%x", (GLenum) param[0]);
 					return;
@@ -166,5 +166,5 @@
 			else
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Invalid glFog Param: %d", param);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Invalid glFog Param: %d", param);
 				return;
 			}
@@ -178,5 +178,5 @@
 						(GLenum) param[0] != GL_FRAGMENT_DEPTH_EXT)
 				{
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 						"Fogfv: GL_FOG_COORDINATE_SOURCE_EXT called with illegal parameter: 0x%x", (GLenum) param[0]);
 					return;
@@ -187,5 +187,5 @@
 			else
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Invalid glFog Param: 0x%x", (GLint) param[0]);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Invalid glFog Param: 0x%x", (GLint) param[0]);
 				return;
 			}
@@ -193,5 +193,5 @@
 #endif
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Invalid glFog Param: %d", param);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Invalid glFog Param: %d", param);
 			return;
 	}
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_framebuffer.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_framebuffer.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_framebuffer.c	(revision 78375)
@@ -34,25 +34,25 @@
 }
 
-void STATE_APIENTRY crStateGenFramebuffersEXT(GLsizei n, GLuint *buffers)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGenFramebuffersEXT(PCRStateTracker pState, GLsizei n, GLuint *buffers)
+{
+    CRContext *g = GetCurrentContext(pState);
     crStateGenNames(g, g->shared->fbTable, n, buffers);
 }
 
-void STATE_APIENTRY crStateGenRenderbuffersEXT(GLsizei n, GLuint *buffers)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGenRenderbuffersEXT(PCRStateTracker pState, GLsizei n, GLuint *buffers)
+{
+    CRContext *g = GetCurrentContext(pState);
     crStateGenNames(g, g->shared->rbTable, n, buffers);
 }
 
-void crStateRegFramebuffers(GLsizei n, GLuint *buffers)
-{
-    CRContext *g = GetCurrentContext();
+void crStateRegFramebuffers(PCRStateTracker pState, GLsizei n, GLuint *buffers)
+{
+    CRContext *g = GetCurrentContext(pState);
     crStateRegNames(g, g->shared->fbTable, n, buffers);
 }
 
-void crStateRegRenderbuffers(GLsizei n, GLuint *buffers)
-{
-    CRContext *g = GetCurrentContext();
+void crStateRegRenderbuffers(PCRStateTracker pState, GLsizei n, GLuint *buffers)
+{
+    CRContext *g = GetCurrentContext(pState);
     crStateRegNames(g, g->shared->rbTable, n, buffers);
 }
@@ -63,9 +63,10 @@
 crStateFramebufferAllocate(CRContext *ctx, GLuint name)
 {
+    PCRStateTracker pState = ctx->pStateTracker;
     CRFramebufferObject *buffer = (CRFramebufferObject*) crCalloc(sizeof(CRFramebufferObject));
     CRSTATE_CHECKERR_RET(!buffer, GL_OUT_OF_MEMORY, "crStateFramebufferAllocate", NULL);
     buffer->id = name;
 #ifndef IN_GUEST
-    diff_api.GenFramebuffersEXT(1, &buffer->hwid);
+    ctx->pStateTracker->diff_api.GenFramebuffersEXT(1, &buffer->hwid);
     if (!buffer->hwid)
     {
@@ -88,9 +89,10 @@
 crStateRenderbufferAllocate(CRContext *ctx, GLuint name)
 {
+    PCRStateTracker pState = ctx->pStateTracker;
     CRRenderbufferObject *buffer = (CRRenderbufferObject*) crCalloc(sizeof(CRRenderbufferObject));
     CRSTATE_CHECKERR_RET(!buffer, GL_OUT_OF_MEMORY, "crStateRenderbufferAllocate", NULL);
     buffer->id = name;
 #ifndef IN_GUEST
-    diff_api.GenRenderbuffersEXT(1, &buffer->hwid);
+    ctx->pStateTracker->diff_api.GenRenderbuffersEXT(1, &buffer->hwid);
     if (!buffer->hwid)
     {
@@ -110,13 +112,16 @@
 }
 
-void crStateFreeFBO(void *data)
-{
+void crStateFreeFBO(void *data, void *pvUser)
+{
+    PCRStateTracker pState = (PCRStateTracker)pvUser;
     CRFramebufferObject *pObj = (CRFramebufferObject *)data;
 
 #ifndef IN_GUEST
-    if (diff_api.DeleteFramebuffersEXT)
-    {
-        diff_api.DeleteFramebuffersEXT(1, &pObj->hwid);
-    }
+    if (pState->diff_api.DeleteFramebuffersEXT)
+    {
+        pState->diff_api.DeleteFramebuffersEXT(1, &pObj->hwid);
+    }
+#else
+    RT_NOREF(pState);
 #endif
 
@@ -124,13 +129,16 @@
 }
 
-void crStateFreeRBO(void *data)
-{
+void crStateFreeRBO(void *data, void *pvUser)
+{
+    PCRStateTracker pState = (PCRStateTracker)pvUser;
     CRRenderbufferObject *pObj = (CRRenderbufferObject *)data;
 
 #ifndef IN_GUEST
-    if (diff_api.DeleteRenderbuffersEXT)
-    {
-        diff_api.DeleteRenderbuffersEXT(1, &pObj->hwid);
-    }
+    if (pState->diff_api.DeleteRenderbuffersEXT)
+    {
+        pState->diff_api.DeleteRenderbuffersEXT(1, &pObj->hwid);
+    }
+#else
+    RT_NOREF(pState);
 #endif
 
@@ -149,7 +157,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateBindRenderbufferEXT(GLenum target, GLuint renderbuffer)
-{
-    CRContext *g = GetCurrentContext();
+crStateBindRenderbufferEXT(PCRStateTracker pState, GLenum target, GLuint renderbuffer)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRFramebufferObjectState *fbo = &g->framebufferobject;
 
@@ -170,5 +178,5 @@
 }
 
-static void crStateCheckFBOAttachments(CRFramebufferObject *pFBO, GLuint rbo, GLenum target)
+static void crStateCheckFBOAttachments(PCRStateTracker pState, CRFramebufferObject *pFBO, GLuint rbo, GLenum target)
 {
     CRFBOAttachmentPoint *ap;
@@ -183,5 +191,5 @@
         if (ap->type==GL_RENDERBUFFER_EXT && ap->name==rbo)
         {
-            crStateFramebufferRenderbufferEXT(target, u+GL_COLOR_ATTACHMENT0_EXT, 0, 0);
+            crStateFramebufferRenderbufferEXT(pState, target, u+GL_COLOR_ATTACHMENT0_EXT, 0, 0);
 #ifdef IN_GUEST
             pFBO->status = GL_FRAMEBUFFER_UNDEFINED;
@@ -193,5 +201,5 @@
     if (ap->type==GL_RENDERBUFFER_EXT && ap->name==rbo)
     {
-        crStateFramebufferRenderbufferEXT(target, GL_DEPTH_ATTACHMENT_EXT, 0, 0);
+        crStateFramebufferRenderbufferEXT(pState, target, GL_DEPTH_ATTACHMENT_EXT, 0, 0);
 #ifdef IN_GUEST
         pFBO->status = GL_FRAMEBUFFER_UNDEFINED;
@@ -201,5 +209,5 @@
     if (ap->type==GL_RENDERBUFFER_EXT && ap->name==rbo)
     {
-        crStateFramebufferRenderbufferEXT(target, GL_STENCIL_ATTACHMENT_EXT, 0, 0);
+        crStateFramebufferRenderbufferEXT(pState, target, GL_STENCIL_ATTACHMENT_EXT, 0, 0);
 #ifdef IN_GUEST
         pFBO->status = GL_FRAMEBUFFER_UNDEFINED;
@@ -218,6 +226,6 @@
 
     /* check the attachments of current framebuffers */
-    crStateCheckFBOAttachments(fbo->readFB, fboId, GL_READ_FRAMEBUFFER);
-    crStateCheckFBOAttachments(fbo->drawFB, fboId, GL_DRAW_FRAMEBUFFER);
+    crStateCheckFBOAttachments(g->pStateTracker, fbo->readFB, fboId, GL_READ_FRAMEBUFFER);
+    crStateCheckFBOAttachments(g->pStateTracker, fbo->drawFB, fboId, GL_DRAW_FRAMEBUFFER);
 
     CR_STATE_SHAREDOBJ_USAGE_CLEAR(rbo, g);
@@ -225,7 +233,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateDeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
-{
-    CRContext *g = GetCurrentContext();
+crStateDeleteRenderbuffersEXT(PCRStateTracker pState, GLsizei n, const GLuint *renderbuffers)
+{
+    CRContext *g = GetCurrentContext(pState);
     /*CRFramebufferObjectState *fbo = &g->framebufferobject; - unused */
     int i;
@@ -251,5 +259,5 @@
                      * This is why g_pAvailableContexts[j] could be NULL
                      * also g_pAvailableContexts[0] will hold default context, which we should discard */
-                    CRContext *ctx = g_pAvailableContexts[j];
+                    CRContext *ctx = pState->apAvailableContexts[j];
                     if (j && ctx)
                     {
@@ -265,5 +273,5 @@
                         CR_STATE_SHAREDOBJ_USAGE_CLEAR_IDX(rbo, j);
                 }
-                crHashtableDelete(g->shared->rbTable, renderbuffers[i], crStateFreeRBO);
+                crHashtableDeleteEx(g->shared->rbTable, renderbuffers[i], crStateFreeRBO, pState);
             }
         }
@@ -272,7 +280,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateRenderbufferStorageEXT(GLenum target, GLenum internalformat, GLsizei width, GLsizei height)
-{
-    CRContext *g = GetCurrentContext();
+crStateRenderbufferStorageEXT(PCRStateTracker pState, GLenum target, GLenum internalformat, GLsizei width, GLsizei height)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRFramebufferObjectState *fbo = &g->framebufferobject;
     CRRenderbufferObject *rb = fbo->renderbuffer;
@@ -288,7 +296,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateGetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
-{
-    CRContext *g = GetCurrentContext();
+crStateGetRenderbufferParameterivEXT(PCRStateTracker pState, GLenum target, GLenum pname, GLint *params)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRFramebufferObjectState *fbo = &g->framebufferobject;
     CRRenderbufferObject *rb = fbo->renderbuffer;
@@ -371,7 +379,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateBindFramebufferEXT(GLenum target, GLuint framebuffer)
-{
-    CRContext *g = GetCurrentContext();
+crStateBindFramebufferEXT(PCRStateTracker pState, GLenum target, GLuint framebuffer)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRFramebufferObjectState *fbo = &g->framebufferobject;
     CRFramebufferObject *pFBO=NULL;
@@ -430,7 +438,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateDeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
-{
-    CRContext *g = GetCurrentContext();
+crStateDeleteFramebuffersEXT(PCRStateTracker pState, GLsizei n, const GLuint *framebuffers)
+{
+    CRContext *g = GetCurrentContext(pState);
     int i;
 
@@ -456,5 +464,5 @@
                      * This is why g_pAvailableContexts[j] could be NULL
                      * also g_pAvailableContexts[0] will hold default context, which we should discard */
-                    CRContext *ctx = g_pAvailableContexts[j];
+                    CRContext *ctx = pState->apAvailableContexts[j];
                     if (j && ctx)
                     {
@@ -473,5 +481,5 @@
                         CR_STATE_SHAREDOBJ_USAGE_CLEAR_IDX(fb, j);
                 }
-                crHashtableDelete(g->shared->fbTable, framebuffers[i], crStateFreeFBO);
+                crHashtableDeleteEx(g->shared->fbTable, framebuffers[i], crStateFreeFBO, pState);
             }
         }
@@ -523,8 +531,8 @@
 }
 
-static GLuint crStateFramebufferTextureCheck(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level,
+static GLuint crStateFramebufferTextureCheck(CRContext *g, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level,
                     CRFBOAttachmentPoint **aap, CRTextureObj **tobj)
 {
-    CRContext *g = GetCurrentContext();
+    PCRStateTracker pState = g->pStateTracker;
     CRFramebufferObjectState *fbo = &g->framebufferobject;
     CRFramebufferObject *apFBOs[2];
@@ -565,18 +573,18 @@
         case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
             maxtexsizelog2 = crLog2Floor(g->limits.maxCubeMapTextureSize);
-            *tobj = crStateTextureGet(GL_TEXTURE_CUBE_MAP_ARB, texture);
+            *tobj = crStateTextureGet(pState, GL_TEXTURE_CUBE_MAP_ARB, texture);
             break;
         case GL_TEXTURE_RECTANGLE_ARB:
             maxtexsizelog2 = 0;
-            *tobj = crStateTextureGet(textarget, texture);
+            *tobj = crStateTextureGet(pState, textarget, texture);
             break;
         case GL_TEXTURE_3D:
             maxtexsizelog2 = crLog2Floor(g->limits.max3DTextureSize);
-            *tobj = crStateTextureGet(textarget, texture);
+            *tobj = crStateTextureGet(pState, textarget, texture);
             break;
         case GL_TEXTURE_2D:
         case GL_TEXTURE_1D:
             maxtexsizelog2 = crLog2Floor(g->limits.maxTextureSize);
-            *tobj = crStateTextureGet(textarget, texture);
+            *tobj = crStateTextureGet(pState, textarget, texture);
             break;
         default:
@@ -611,7 +619,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateFramebufferTexture1DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
-{
-    CRContext *g = GetCurrentContext();
+crStateFramebufferTexture1DEXT(PCRStateTracker pState, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
+{
+    CRContext *g = GetCurrentContext(pState);
     /*CRFramebufferObjectState *fbo = &g->framebufferobject; - unused */
     CRFBOAttachmentPoint *aap[2];
@@ -619,5 +627,5 @@
     CRTextureObj *tobj;
 
-    cap = crStateFramebufferTextureCheck(target, attachment, textarget, texture, level, aap, &tobj);
+    cap = crStateFramebufferTextureCheck(g, target, attachment, textarget, texture, level, aap, &tobj);
     if (!cap) return;
 
@@ -645,7 +653,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateFramebufferTexture2DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
-{
-    CRContext *g = GetCurrentContext();
+crStateFramebufferTexture2DEXT(PCRStateTracker pState, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
+{
+    CRContext *g = GetCurrentContext(pState);
     /* CRFramebufferObjectState *fbo = &g->framebufferobject; - unused */
     CRFBOAttachmentPoint *aap[2];
@@ -653,5 +661,5 @@
     CRTextureObj *tobj;
 
-    cap = crStateFramebufferTextureCheck(target, attachment, textarget, texture, level, aap, &tobj);
+    cap = crStateFramebufferTextureCheck(g, target, attachment, textarget, texture, level, aap, &tobj);
     if (!cap) return;
 
@@ -683,7 +691,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateFramebufferTexture3DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset)
-{
-    CRContext *g = GetCurrentContext();
+crStateFramebufferTexture3DEXT(PCRStateTracker pState, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset)
+{
+    CRContext *g = GetCurrentContext(pState);
     /* CRFramebufferObjectState *fbo = &g->framebufferobject; - unused */
     CRFBOAttachmentPoint *aap[2];
@@ -691,5 +699,5 @@
     CRTextureObj *tobj;
 
-    cap = crStateFramebufferTextureCheck(target, attachment, textarget, texture, level, aap, &tobj);
+    cap = crStateFramebufferTextureCheck(g, target, attachment, textarget, texture, level, aap, &tobj);
     if (!cap) return;
 
@@ -719,7 +727,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateFramebufferRenderbufferEXT(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)
-{
-    CRContext *g = GetCurrentContext();
+crStateFramebufferRenderbufferEXT(PCRStateTracker pState, GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRFramebufferObjectState *fbo = &g->framebufferobject;
     CRFramebufferObject *apFBOs[2];
@@ -783,7 +791,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateGetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment, GLenum pname, GLint *params)
-{
-    CRContext *g = GetCurrentContext();
+crStateGetFramebufferAttachmentParameterivEXT(PCRStateTracker pState, GLenum target, GLenum attachment, GLenum pname, GLint *params)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRFramebufferObjectState *fbo = &g->framebufferobject;
     CRFramebufferObject *apFBOs[2];
@@ -839,12 +847,12 @@
 }
 
-DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsFramebufferEXT( GLuint framebuffer )
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsFramebufferEXT(PCRStateTracker pState, GLuint framebuffer )
+{
+    CRContext *g = GetCurrentContext(pState);
 
     FLUSH();
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glIsFramebufferEXT called in begin/end");
         return GL_FALSE;
@@ -854,7 +862,7 @@
 }
 
-DECLEXPORT(GLboolean)  STATE_APIENTRY crStateIsRenderbufferEXT( GLuint renderbuffer )
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLboolean)  STATE_APIENTRY crStateIsRenderbufferEXT(PCRStateTracker pState, GLuint renderbuffer )
+{
+    CRContext *g = GetCurrentContext(pState);
 
 
@@ -862,5 +870,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glIsRenderbufferEXT called in begin/end");
         return GL_FALSE;
@@ -871,6 +879,7 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateGenerateMipmapEXT(GLenum target)
-{
+crStateGenerateMipmapEXT(PCRStateTracker pState, GLenum target)
+{
+    RT_NOREF(pState);
     (void)target;
     /** @todo */
@@ -879,13 +888,14 @@
 static void crStateSyncRenderbuffersCB(unsigned long key, void *data1, void *data2)
 {
+    PCRStateTracker pState = (PCRStateTracker)data2;
     CRRenderbufferObject *pRBO = (CRRenderbufferObject*) data1;
-    (void)key; (void)data2;
-
-    diff_api.GenRenderbuffersEXT(1, &pRBO->hwid);
+    (void)key;
+
+    pState->diff_api.GenRenderbuffersEXT(1, &pRBO->hwid);
 
     if (pRBO->width && pRBO->height)
     {
-        diff_api.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, pRBO->hwid);
-        diff_api.RenderbufferStorageEXT(GL_RENDERBUFFER_EXT, pRBO->internalformat, pRBO->width, pRBO->height);
+        pState->diff_api.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, pRBO->hwid);
+        pState->diff_api.RenderbufferStorageEXT(GL_RENDERBUFFER_EXT, pRBO->internalformat, pRBO->width, pRBO->height);
     }
 }
@@ -893,4 +903,5 @@
 static void crStateSyncAP(CRFBOAttachmentPoint *pAP, GLenum ap, CRContext *ctx)
 {
+    PCRStateTracker pState = ctx->pStateTracker;
     CRRenderbufferObject *pRBO;
     CRTextureObj *tobj;
@@ -909,15 +920,15 @@
                 {
                     case GL_TEXTURE_1D:
-                        diff_api.FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT, ap, tobj->target, crStateGetTextureObjHWID(tobj), pAP->level);
+                        pState->diff_api.FramebufferTexture1DEXT(GL_FRAMEBUFFER_EXT, ap, tobj->target, crStateGetTextureObjHWID(pState, tobj), pAP->level);
                         break;
                     case GL_TEXTURE_2D:
                     case GL_TEXTURE_RECTANGLE_ARB:
-                        diff_api.FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ap, tobj->target, crStateGetTextureObjHWID(tobj), pAP->level);
+                        pState->diff_api.FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ap, tobj->target, crStateGetTextureObjHWID(pState, tobj), pAP->level);
                         break;
                     case GL_TEXTURE_CUBE_MAP_ARB:
-                        diff_api.FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ap, pAP->face, crStateGetTextureObjHWID(tobj), pAP->level);
+                        pState->diff_api.FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, ap, pAP->face, crStateGetTextureObjHWID(pState, tobj), pAP->level);
                         break;
                     case GL_TEXTURE_3D:
-                        diff_api.FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT, ap, tobj->target, crStateGetTextureObjHWID(tobj), pAP->level, pAP->zoffset);
+                        pState->diff_api.FramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT, ap, tobj->target, crStateGetTextureObjHWID(pState, tobj), pAP->level, pAP->zoffset);
                         break;
                     default:
@@ -932,5 +943,5 @@
         case GL_RENDERBUFFER_EXT:
             pRBO = (CRRenderbufferObject*) crHashtableSearch(ctx->shared->rbTable, pAP->name);
-            diff_api.FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, ap, GL_RENDERBUFFER_EXT, pRBO->hwid);
+            pState->diff_api.FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, ap, GL_RENDERBUFFER_EXT, pRBO->hwid);
             break;
         case GL_NONE:
@@ -945,10 +956,11 @@
     CRFramebufferObject *pFBO = (CRFramebufferObject*) data1;
     CRContext *ctx = (CRContext*) data2;
+    PCRStateTracker pState = ctx->pStateTracker;
     GLint i;
     (void)key;
 
-    diff_api.GenFramebuffersEXT(1, &pFBO->hwid);
-
-    diff_api.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, pFBO->hwid);
+    pState->diff_api.GenFramebuffersEXT(1, &pFBO->hwid);
+
+    pState->diff_api.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, pFBO->hwid);
 
     for (i=0; i<CR_MAX_COLOR_ATTACHMENTS; ++i)
@@ -964,26 +976,28 @@
 crStateFramebufferObjectSwitch(CRContext *from, CRContext *to)
 {
+    PCRStateTracker pState = to->pStateTracker;
+
     if (to->shared->bFBOResyncNeeded)
     {
         to->shared->bFBOResyncNeeded = GL_FALSE;
 
-        crHashtableWalk(to->shared->rbTable, crStateSyncRenderbuffersCB, NULL);
+        crHashtableWalk(to->shared->rbTable, crStateSyncRenderbuffersCB, pState);
         crHashtableWalk(to->shared->fbTable, crStateSyncFramebuffersCB, to);
 
         if (to->framebufferobject.drawFB==to->framebufferobject.readFB)
         {
-            diff_api.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, to->framebufferobject.drawFB?
+            pState->diff_api.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, to->framebufferobject.drawFB?
                 to->framebufferobject.drawFB->hwid:0);
         }
         else
         {
-            diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, to->framebufferobject.drawFB?
+            pState->diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, to->framebufferobject.drawFB?
                 to->framebufferobject.drawFB->hwid:0);
 
-            diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, to->framebufferobject.readFB?
+            pState->diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, to->framebufferobject.readFB?
                 to->framebufferobject.readFB->hwid:0);
         }
 
-        diff_api.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, to->framebufferobject.renderbuffer?
+        pState->diff_api.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, to->framebufferobject.renderbuffer?
             to->framebufferobject.renderbuffer->hwid:0);
     }
@@ -995,23 +1009,23 @@
             if (to->framebufferobject.drawFB==to->framebufferobject.readFB)
             {
-                diff_api.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, to->framebufferobject.drawFB?
+                pState->diff_api.BindFramebufferEXT(GL_FRAMEBUFFER_EXT, to->framebufferobject.drawFB?
                     to->framebufferobject.drawFB->hwid:0);
             }
             else
             {
-                diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, to->framebufferobject.drawFB?
+                pState->diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, to->framebufferobject.drawFB?
                     to->framebufferobject.drawFB->hwid:0);
 
-                diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, to->framebufferobject.readFB?
+                pState->diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, to->framebufferobject.readFB?
                     to->framebufferobject.readFB->hwid:0);
             }
 
-            diff_api.DrawBuffer(to->framebufferobject.drawFB?to->framebufferobject.drawFB->drawbuffer[0]:to->buffer.drawBuffer);
-            diff_api.ReadBuffer(to->framebufferobject.readFB?to->framebufferobject.readFB->readbuffer:to->buffer.readBuffer);
+            pState->diff_api.DrawBuffer(to->framebufferobject.drawFB?to->framebufferobject.drawFB->drawbuffer[0]:to->buffer.drawBuffer);
+            pState->diff_api.ReadBuffer(to->framebufferobject.readFB?to->framebufferobject.readFB->readbuffer:to->buffer.readBuffer);
         }
 
         if (to->framebufferobject.renderbuffer!=from->framebufferobject.renderbuffer)
         {
-            diff_api.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, to->framebufferobject.renderbuffer?
+            pState->diff_api.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, to->framebufferobject.renderbuffer?
                 to->framebufferobject.renderbuffer->hwid:0);
         }
@@ -1022,9 +1036,10 @@
 crStateFramebufferObjectDisableHW(CRContext *ctx, GLuint idDrawFBO, GLuint idReadFBO)
 {
+    PCRStateTracker pState = ctx->pStateTracker;
     GLenum idDrawBuffer = 0, idReadBuffer = 0;
 
     if (ctx->framebufferobject.drawFB || idDrawFBO)
     {
-        diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
+        pState->diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
         idDrawBuffer = ctx->buffer.drawBuffer;
     }
@@ -1032,15 +1047,15 @@
     if (ctx->framebufferobject.readFB || idReadFBO)
     {
-        diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
+        pState->diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
         idReadBuffer = ctx->buffer.readBuffer;
     }
 
     if (idDrawBuffer)
-        diff_api.DrawBuffer(idDrawBuffer);
+        pState->diff_api.DrawBuffer(idDrawBuffer);
     if (idReadBuffer)
-        diff_api.ReadBuffer(idReadBuffer);
+        pState->diff_api.ReadBuffer(idReadBuffer);
 
     if (ctx->framebufferobject.renderbuffer)
-        diff_api.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
+        pState->diff_api.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
 }
 
@@ -1048,4 +1063,5 @@
 crStateFramebufferObjectReenableHW(CRContext *fromCtx, CRContext *toCtx, GLuint idDrawFBO, GLuint idReadFBO)
 {
+    PCRStateTracker pState = fromCtx ? fromCtx->pStateTracker : toCtx->pStateTracker;
     GLuint idReadBuffer = 0, idDrawBuffer = 0;
     if (!fromCtx)
@@ -1055,10 +1071,10 @@
             && fromCtx->framebufferobject.drawFB == toCtx->framebufferobject.drawFB)  /* .. and it was NOT restored properly in crStateFramebufferObjectSwitch */
     {
-        diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, toCtx->framebufferobject.drawFB->hwid);
+        pState->diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, toCtx->framebufferobject.drawFB->hwid);
         idDrawBuffer = toCtx->framebufferobject.drawFB->drawbuffer[0];
     }
     else if (idDrawFBO && !toCtx->framebufferobject.drawFB)
     {
-        diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, idDrawFBO);
+        pState->diff_api.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, idDrawFBO);
         idDrawBuffer = GL_COLOR_ATTACHMENT0;
     }
@@ -1067,29 +1083,29 @@
             && fromCtx->framebufferobject.readFB == toCtx->framebufferobject.readFB) /* .. and it was NOT restored properly in crStateFramebufferObjectSwitch */
     {
-        diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, toCtx->framebufferobject.readFB->hwid);
+        pState->diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, toCtx->framebufferobject.readFB->hwid);
         idReadBuffer = toCtx->framebufferobject.readFB->readbuffer;
     }
     else if (idReadFBO && !toCtx->framebufferobject.readFB)
     {
-        diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, idReadFBO);
+        pState->diff_api.BindFramebufferEXT(GL_READ_FRAMEBUFFER, idReadFBO);
         idReadBuffer = GL_COLOR_ATTACHMENT0;
     }
 
     if (idDrawBuffer)
-        diff_api.DrawBuffer(idDrawBuffer);
+        pState->diff_api.DrawBuffer(idDrawBuffer);
     if (idReadBuffer)
-        diff_api.ReadBuffer(idReadBuffer);
+        pState->diff_api.ReadBuffer(idReadBuffer);
 
     if (fromCtx->framebufferobject.renderbuffer /* <- the FBO state was reset in crStateFramebufferObjectDisableHW */
             && fromCtx->framebufferobject.renderbuffer==toCtx->framebufferobject.renderbuffer) /* .. and it was NOT restored properly in crStateFramebufferObjectSwitch */
     {
-        diff_api.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, toCtx->framebufferobject.renderbuffer->hwid);
-    }
-}
-
-
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetFramebufferHWID(GLuint id)
-{
-    CRContext *g = GetCurrentContext();
+        pState->diff_api.BindRenderbufferEXT(GL_RENDERBUFFER_EXT, toCtx->framebufferobject.renderbuffer->hwid);
+    }
+}
+
+
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetFramebufferHWID(PCRStateTracker pState, GLuint id)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRFramebufferObject *pFBO = (CRFramebufferObject*) crHashtableSearch(g->shared->fbTable, id);
 #if 0 /*def DEBUG_misha*/
@@ -1099,7 +1115,7 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetRenderbufferHWID(GLuint id)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetRenderbufferHWID(PCRStateTracker pState, GLuint id)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRRenderbufferObject *pRBO = (CRRenderbufferObject*) crHashtableSearch(g->shared->rbTable, id);
 
@@ -1127,7 +1143,7 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateFBOHWIDtoID(GLuint hwid)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLuint) STATE_APIENTRY crStateFBOHWIDtoID(PCRStateTracker pState, GLuint hwid)
+{
+    CRContext *g = GetCurrentContext(pState);
     crCheckIDHWID_t parms;
 
@@ -1139,7 +1155,7 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateRBOHWIDtoID(GLuint hwid)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLuint) STATE_APIENTRY crStateRBOHWIDtoID(PCRStateTracker pState, GLuint hwid)
+{
+    CRContext *g = GetCurrentContext(pState);
     crCheckIDHWID_t parms;
 
@@ -1152,8 +1168,8 @@
 
 #ifdef IN_GUEST
-DECLEXPORT(GLenum) STATE_APIENTRY crStateCheckFramebufferStatusEXT(GLenum target)
+DECLEXPORT(GLenum) STATE_APIENTRY crStateCheckFramebufferStatusEXT(PCRStateTracker pState, GLenum target)
 {
     GLenum status = GL_FRAMEBUFFER_UNDEFINED;
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRFramebufferObjectState *fbo = &g->framebufferobject;
     CRFramebufferObject *pFBO=NULL;
@@ -1177,7 +1193,7 @@
 }
 
-DECLEXPORT(GLenum) STATE_APIENTRY crStateSetFramebufferStatus(GLenum target, GLenum status)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLenum) STATE_APIENTRY crStateSetFramebufferStatus(PCRStateTracker pState, GLenum target, GLenum status)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRFramebufferObjectState *fbo = &g->framebufferobject;
     CRFramebufferObject *pFBO=NULL;
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_funcs.py
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_funcs.py	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_funcs.py	(revision 78375)
@@ -34,5 +34,8 @@
 #endif
 
-#define STATE_UNUSED(x) ((void)x)""")
+#define STATE_UNUSED(x) ((void)x)
+
+/** Forward declaration of the stae tracker. */
+typedef struct CRStateTracker *PCRStateTracker;""")
 
 
@@ -41,16 +44,24 @@
 	return_type = apiutil.ReturnType(func_name)
 	params = apiutil.Parameters(func_name)
-	print('DECLEXPORT(%s) STATE_APIENTRY crState%s(%s);' % (return_type, func_name, apiutil.MakeDeclarationStringForDispatcher(params)))
+	if len(params) == 0:
+		print('DECLEXPORT(%s) STATE_APIENTRY crState%s(PCRStateTracker pState);' % (return_type, func_name))
+	else:
+		print('DECLEXPORT(%s) STATE_APIENTRY crState%s(PCRStateTracker pState, %s);' % (return_type, func_name, apiutil.MakeDeclarationStringForDispatcher(params)))
 
 for func_name in apiutil.AllSpecials( "state_feedback" ):
 	return_type = apiutil.ReturnType(func_name)
 	params = apiutil.Parameters(func_name)
-	print('DECLEXPORT(%s) STATE_APIENTRY crStateFeedback%s(%s);' % (return_type, func_name, apiutil.MakeDeclarationStringForDispatcher(params)))
+	if len(params) == 0:
+		print('DECLEXPORT(%s) STATE_APIENTRY crStateFeedback%s(PCRStateTracker pState);' % (return_type, func_name))
+	else:
+		print('DECLEXPORT(%s) STATE_APIENTRY crStateFeedback%s(PCRStateTracker pState, %s);' % (return_type, func_name, apiutil.MakeDeclarationStringForDispatcher(params)))
 
 for func_name in apiutil.AllSpecials( "state_select" ):
 	return_type = apiutil.ReturnType(func_name)
 	params = apiutil.Parameters(func_name)
-	print('DECLEXPORT(%s) STATE_APIENTRY crStateSelect%s(%s);' % (return_type, func_name, apiutil.MakeDeclarationStringForDispatcher(params)))
-
+	if len(params) == 0:
+		print('DECLEXPORT(%s) STATE_APIENTRY crStateSelect%s(PCRStateTracker pState);' % (return_type, func_name))
+	else:
+		print('DECLEXPORT(%s) STATE_APIENTRY crStateSelect%s(PCRStateTracker pState, %s);' % (return_type, func_name, apiutil.MakeDeclarationStringForDispatcher(params)))
 
 print("""
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_get.py
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_get.py	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_get.py	(revision 78375)
@@ -146,9 +146,9 @@
 header = """
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 			"glGet called in Begin/End");
 		return;
@@ -162,8 +162,8 @@
 	{
 #if 0
-		crStateError(__LINE__,__FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__,__FILE__, GL_INVALID_OPERATION,
 			"Unimplemented glGet of a 'current' value" );
 #else
-		crStateCurrentRecover();/* &g->current, &sb->current, g->bitID );*/
+		crStateCurrentRecover(pState);/* &g->current, &sb->current, g->bitID );*/
 		
 #endif
@@ -175,5 +175,5 @@
 for rettype in types:
 	print('')
-	print('void STATE_APIENTRY crStateGet%sv( GLenum pname, %s *params )' % ( rettype, ctypes[rettype] ))
+	print('void STATE_APIENTRY crStateGet%sv(PCRStateTracker pState, GLenum pname, %s *params )' % ( rettype, ctypes[rettype] ))
 	print(header)
 
@@ -189,5 +189,5 @@
 				i += 1
 		except:
-			print('\t\t\tcrStateError(__LINE__,__FILE__,GL_INVALID_OPERATION, "Unimplemented glGet!");')
+			print('\t\t\tcrStateError(pState, __LINE__,__FILE__,GL_INVALID_OPERATION, "Unimplemented glGet!");')
 		print("\t\t\tbreak;")
 
@@ -216,9 +216,9 @@
 				i += 1
 		except:
-			print('\t\t\tcrStateError(__LINE__,__FILE__,GL_INVALID_OPERATION, "Unimplemented glGet!");')
+			print('\t\t\tcrStateError(pState, __LINE__,__FILE__,GL_INVALID_OPERATION, "Unimplemented glGet!");')
 		if ext != 'OPENGL_VERSION_1_2':
 			print("\t\t\t}")
 			print("\t\t\telse {")
-			print('\t\t\t\tcrStateError(__LINE__,__FILE__,GL_INVALID_ENUM, "glGet%sv");' % rettype)
+			print('\t\t\t\tcrStateError(pState, __LINE__,__FILE__,GL_INVALID_ENUM, "glGet%sv");' % rettype)
 			print("\t\t\t}")
 		print("\t\t\tbreak;")
@@ -227,5 +227,5 @@
 
 	print('\t\tdefault:')
-	print('\t\t\tcrStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGet: Unknown enum: 0x%x", pname);')
+	print('\t\t\tcrStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGet: Unknown enum: 0x%x", pname);')
 	print('\t\t\treturn;')
 	print('\t}')
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_getstring.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_getstring.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_getstring.c	(revision 78375)
@@ -14,7 +14,7 @@
 
 
-const GLubyte * STATE_APIENTRY crStateGetString( GLenum name )
+const GLubyte * STATE_APIENTRY crStateGetString(PCRStateTracker pState, GLenum name )
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	if (!g)
 		return NULL;
@@ -47,5 +47,5 @@
 #endif
 		default:
-			crStateError( __LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									"calling glGetString() with invalid name" );
 			return NULL;
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_glsl.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_glsl.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_glsl.c	(revision 78375)
@@ -26,8 +26,16 @@
 #include <stdio.h> /*sprintf*/
 
-static CRGLSLShader* crStateGetShaderObj(GLuint id)
-{
-    CRContext *g = GetCurrentContext();
-
+/**
+ * Helper for walking the shaders hash table.
+ */
+typedef struct CRStateGLSLShaderWalkArgs
+{
+    CRGLSLProgram *pProgram;
+    PCRStateTracker pState;
+} CRStateGLSLShaderWalkArgs;
+typedef CRStateGLSLShaderWalkArgs *PCRStateGLSLShaderWalkArgs;
+
+static CRGLSLShader* crStateGetShaderObj(CRContext *g, GLuint id)
+{
     if (!g)
     {
@@ -38,8 +46,6 @@
 }
 
-static CRGLSLProgram* crStateGetProgramObj(GLuint id)
-{
-    CRContext *g = GetCurrentContext();
-
+static CRGLSLProgram* crStateGetProgramObj(CRContext *g, GLuint id)
+{
     if (!g)
     {
@@ -118,6 +124,7 @@
 }
 
-static void crStateShaderDecRefCount(void *data)
-{
+static void crStateShaderDecRefCount(void *data, void *pvUser)
+{
+    CRContext *g = (CRContext *)pvUser;
     CRGLSLShader *pShader = (CRGLSLShader *) data;
 
@@ -127,8 +134,5 @@
 
     if (0==pShader->refCount && pShader->deleted)
-    {
-        CRContext *g = GetCurrentContext();
         crHashtableDelete(g->glsl.shaders, pShader->id, crStateFreeGLSLShader);
-    }
 }
 
@@ -140,9 +144,9 @@
     (void) key; (void)ctx;
 
-    pRealShader = crStateGetShaderObj(pShader->id);
+    pRealShader = crStateGetShaderObj(ctx, pShader->id);
 
     if (pRealShader)
     {
-        crStateShaderDecRefCount(pRealShader);
+        crStateShaderDecRefCount(pRealShader, ctx);
     }
     else
@@ -152,14 +156,14 @@
 }
 
-static void crStateFreeGLSLProgram(void *data)
-{
+static void crStateFreeGLSLProgram(void *data, void *pvUser)
+{
+    CRContext *pCtx = (CRContext *)pvUser;
     CRGLSLProgram* pProgram = (CRGLSLProgram *) data;
 
-    crFreeHashtable(pProgram->currentState.attachedShaders, crStateShaderDecRefCount);
+    crFreeHashtableEx(pProgram->currentState.attachedShaders, crStateShaderDecRefCount, pCtx);
 
     if (pProgram->activeState.attachedShaders)
     {
-        CRContext *g = GetCurrentContext();
-        crHashtableWalk(pProgram->activeState.attachedShaders, crStateFakeDecRefCountCB, g);
+        crHashtableWalk(pProgram->activeState.attachedShaders, crStateFakeDecRefCountCB, pCtx);
         crFreeHashtable(pProgram->activeState.attachedShaders, crStateFreeGLSLShader);
     }
@@ -188,5 +192,5 @@
 DECLEXPORT(void) STATE_APIENTRY crStateGLSLDestroy(CRContext *ctx)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(ctx->pStateTracker);
 
     /** @todo hack to allow crStateFreeGLSLProgram to work correctly,
@@ -196,10 +200,10 @@
     if (g)
         VBoxTlsRefAddRef(g); /* <- ensure the g is not destroyed by the following SetCurrentContext call */
-    SetCurrentContext(ctx);
-
-    crFreeHashtable(ctx->glsl.programs, crStateFreeGLSLProgram);
+    SetCurrentContext(ctx->pStateTracker, ctx);
+
+    crFreeHashtableEx(ctx->glsl.programs, crStateFreeGLSLProgram, ctx);
     crFreeHashtable(ctx->glsl.shaders, crStateFreeGLSLShader);
 
-    SetCurrentContext(g);
+    SetCurrentContext(ctx->pStateTracker, g);
     if (g)
         VBoxTlsRefRelease(g);
@@ -207,7 +211,8 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetShaderHWID(GLuint id)
-{
-    CRGLSLShader *pShader = crStateGetShaderObj(id);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetShaderHWID(PCRStateTracker pState, GLuint id)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLShader *pShader = crStateGetShaderObj(g, id);
 #ifdef IN_GUEST
     CRASSERT(!pShader || pShader->hwid == id);
@@ -216,7 +221,8 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetProgramHWID(GLuint id)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(id);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetProgramHWID(PCRStateTracker pState, GLuint id)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, id);
 #ifdef IN_GUEST
     CRASSERT(!pProgram || pProgram->hwid == id);
@@ -245,7 +251,7 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGLSLShaderHWIDtoID(GLuint hwid)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGLSLShaderHWIDtoID(PCRStateTracker pState, GLuint hwid)
+{
+    CRContext *g = GetCurrentContext(pState);
     crCheckIDHWID_t parms;
 
@@ -257,7 +263,7 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGLSLProgramHWIDtoID(GLuint hwid)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGLSLProgramHWIDtoID(PCRStateTracker pState, GLuint hwid)
+{
+    CRContext *g = GetCurrentContext(pState);
     crCheckIDHWID_t parms;
 
@@ -269,27 +275,27 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateDeleteObjectARB( VBoxGLhandleARB obj )
-{
-    GLuint hwId = crStateGetProgramHWID(obj);
+DECLEXPORT(GLuint) STATE_APIENTRY crStateDeleteObjectARB(PCRStateTracker pState,  VBoxGLhandleARB obj )
+{
+    GLuint hwId = crStateGetProgramHWID(pState, obj);
     if (hwId)
     {
-        crStateDeleteProgram(obj);
+        crStateDeleteProgram(pState, obj);
     }
     else
     {
-        hwId = crStateGetShaderHWID(obj);
-        crStateDeleteShader(obj);
+        hwId = crStateGetShaderHWID(pState, obj);
+        crStateDeleteShader(pState, obj);
     }
     return hwId;
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateCreateShader(GLuint hwid, GLenum type)
+DECLEXPORT(GLuint) STATE_APIENTRY crStateCreateShader(PCRStateTracker pState, GLuint hwid, GLenum type)
 {
     CRGLSLShader *pShader;
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     GLuint stateId = hwid;
 
 #ifdef IN_GUEST
-    CRASSERT(!crStateGetShaderObj(stateId));
+    CRASSERT(!crStateGetShaderObj(g, stateId));
 #else
     /* the proogram and shader names must not intersect because DeleteObjectARB must distinguish between them
@@ -303,5 +309,5 @@
     }
 
-    Assert((pShader = crStateGetShaderObj(stateId)) == NULL);
+    Assert((pShader = crStateGetShaderObj(g, stateId)) == NULL);
 #endif
 
@@ -326,17 +332,17 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateCreateProgram(GLuint hwid)
+DECLEXPORT(GLuint) STATE_APIENTRY crStateCreateProgram(PCRStateTracker pState, GLuint hwid)
 {
     CRGLSLProgram *pProgram;
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     GLuint stateId = hwid;
 
 #ifdef IN_GUEST
-    pProgram = crStateGetProgramObj(stateId);
+    pProgram = crStateGetProgramObj(g, stateId);
     if (pProgram)
     {
         crWarning("Program object %d already exists!", stateId);
-        crStateDeleteProgram(stateId);
-        CRASSERT(!crStateGetProgramObj(stateId));
+        crStateDeleteProgram(pState, stateId);
+        CRASSERT(!crStateGetProgramObj(g, stateId));
     }
 #else
@@ -385,7 +391,8 @@
 }
 
-DECLEXPORT(void) STATE_APIENTRY crStateCompileShader(GLuint shader)
-{
-    CRGLSLShader *pShader = crStateGetShaderObj(shader);
+DECLEXPORT(void) STATE_APIENTRY crStateCompileShader(PCRStateTracker pState, GLuint shader)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLShader *pShader = crStateGetShaderObj(g, shader);
     if (!pShader)
     {
@@ -403,7 +410,8 @@
 }
 
-DECLEXPORT(void) STATE_APIENTRY crStateDeleteShader(GLuint shader)
-{
-    CRGLSLShader *pShader = crStateGetShaderObj(shader);
+DECLEXPORT(void) STATE_APIENTRY crStateDeleteShader(PCRStateTracker pState, GLuint shader)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLShader *pShader = crStateGetShaderObj(g, shader);
     if (!pShader)
     {
@@ -416,5 +424,4 @@
     if (0==pShader->refCount)
     {
-        CRContext *g = GetCurrentContext();
         crHashtableDelete(g->glsl.shaders, shader, crStateFreeGLSLShader);
         /* since we use programs table for key allocation key allocation, we need to
@@ -425,7 +432,8 @@
 }
 
-DECLEXPORT(void) STATE_APIENTRY crStateAttachShader(GLuint program, GLuint shader)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+DECLEXPORT(void) STATE_APIENTRY crStateAttachShader(PCRStateTracker pState, GLuint program, GLuint shader)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
     CRGLSLShader *pShader;
 
@@ -442,5 +450,5 @@
     }
 
-    pShader = crStateGetShaderObj(shader);
+    pShader = crStateGetShaderObj(g, shader);
 
     if (!pShader)
@@ -455,7 +463,8 @@
 }
 
-DECLEXPORT(void) STATE_APIENTRY crStateDetachShader(GLuint program, GLuint shader)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+DECLEXPORT(void) STATE_APIENTRY crStateDetachShader(PCRStateTracker pState, GLuint program, GLuint shader)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
     CRGLSLShader *pShader;
 
@@ -479,17 +488,14 @@
 
     if (0==pShader->refCount)
-    {
-        CRContext *g = GetCurrentContext();
         crHashtableDelete(g->glsl.shaders, shader, crStateFreeGLSLShader);
-    }
-}
-
-DECLEXPORT(void) STATE_APIENTRY crStateUseProgram(GLuint program)
-{
-    CRContext *g = GetCurrentContext();
+}
+
+DECLEXPORT(void) STATE_APIENTRY crStateUseProgram(PCRStateTracker pState, GLuint program)
+{
+    CRContext *g = GetCurrentContext(pState);
 
     if (program>0)
     {
-        CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+        CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
 
         if (!pProgram)
@@ -507,8 +513,8 @@
 }
 
-DECLEXPORT(void) STATE_APIENTRY crStateDeleteProgram(GLuint program)
-{
-    CRContext *g = GetCurrentContext();
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+DECLEXPORT(void) STATE_APIENTRY crStateDeleteProgram(PCRStateTracker pState, GLuint program)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
 
     if (!pProgram)
@@ -523,10 +529,11 @@
     }
 
-    crHashtableDelete(g->glsl.programs, program, crStateFreeGLSLProgram);
-}
-
-DECLEXPORT(void) STATE_APIENTRY crStateValidateProgram(GLuint program)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+    crHashtableDeleteEx(g->glsl.programs, program, crStateFreeGLSLProgram, g);
+}
+
+DECLEXPORT(void) STATE_APIENTRY crStateValidateProgram(PCRStateTracker pState, GLuint program)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
 
     if (!pProgram)
@@ -542,5 +549,6 @@
 {
     CRGLSLShader *pRealShader = (CRGLSLShader *) data1;
-    CRGLSLProgram *pProgram = (CRGLSLProgram *) data2;
+    PCRStateGLSLShaderWalkArgs pArgs = (PCRStateGLSLShaderWalkArgs)data2;
+    PCRStateTracker pState = pArgs->pState;
     CRGLSLShader *pShader;
     GLint sLen=0;
@@ -558,18 +566,20 @@
     crMemcpy(pShader, pRealShader, sizeof(*pShader));
 
-    diff_api.GetShaderiv(pShader->hwid, GL_SHADER_SOURCE_LENGTH, &sLen);
+    pState->diff_api.GetShaderiv(pShader->hwid, GL_SHADER_SOURCE_LENGTH, &sLen);
     if (sLen>0)
     {
         pShader->source = (GLchar*) crAlloc(sLen);
-        diff_api.GetShaderSource(pShader->hwid, sLen, NULL, pShader->source);
-    }
-
-    crHashtableAdd(pProgram->activeState.attachedShaders, key, pShader);
-}
-
-DECLEXPORT(void) STATE_APIENTRY crStateLinkProgram(GLuint program)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+        pState->diff_api.GetShaderSource(pShader->hwid, sLen, NULL, pShader->source);
+    }
+
+    crHashtableAdd(pArgs->pProgram->activeState.attachedShaders, key, pShader);
+}
+
+DECLEXPORT(void) STATE_APIENTRY crStateLinkProgram(PCRStateTracker pState, GLuint program)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
     GLuint i;
+    CRStateGLSLShaderWalkArgs Args;
 
     if (!pProgram)
@@ -584,5 +594,5 @@
     if (pProgram->activeState.attachedShaders)
     {
-        crHashtableWalk(pProgram->activeState.attachedShaders, crStateFakeDecRefCountCB, NULL);
+        crHashtableWalk(pProgram->activeState.attachedShaders, crStateFakeDecRefCountCB, g);
         crFreeHashtable(pProgram->activeState.attachedShaders, crStateFreeGLSLShader);
         pProgram->activeState.attachedShaders = NULL;
@@ -603,5 +613,7 @@
         return;
     }
-    crHashtableWalk(pProgram->currentState.attachedShaders, crStateCopyShaderCB, pProgram);
+    Args.pProgram = pProgram;
+    Args.pState   = pState;
+    crHashtableWalk(pProgram->currentState.attachedShaders, crStateCopyShaderCB, &Args);
 
     /*that's not a bug, note the memcpy above*/
@@ -624,7 +636,8 @@
 }
 
-DECLEXPORT(void) STATE_APIENTRY crStateBindAttribLocation(GLuint program, GLuint index, const char * name)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+DECLEXPORT(void) STATE_APIENTRY crStateBindAttribLocation(PCRStateTracker pState, GLuint program, GLuint index, const char * name)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
     GLuint i;
     CRGLSLAttrib *pAttribs;
@@ -670,7 +683,9 @@
 }
 
-DECLEXPORT(GLint) STATE_APIENTRY crStateGetUniformSize(GLenum type)
+DECLEXPORT(GLint) STATE_APIENTRY crStateGetUniformSize(PCRStateTracker pState, GLenum type)
 {
     GLint size;
+
+    RT_NOREF(pState);
 
     switch (type)
@@ -760,6 +775,8 @@
 }
 
-DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsIntUniform(GLenum type)
-{
+DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsIntUniform(PCRStateTracker pState, GLenum type)
+{
+    RT_NOREF(pState);
+
     if (GL_INT==type
         || GL_INT_VEC2==type
@@ -784,7 +801,8 @@
 }
 
-DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsProgramUniformsCached(GLuint program)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsProgramUniformsCached(PCRStateTracker pState, GLuint program)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
 
     if (!pProgram)
@@ -802,7 +820,8 @@
 }
 
-DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsProgramAttribsCached(GLuint program)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+DECLEXPORT(GLboolean) STATE_APIENTRY crStateIsProgramAttribsCached(PCRStateTracker pState, GLuint program)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
 
     if (!pProgram)
@@ -824,7 +843,8 @@
 #ifdef IN_GUEST
 DECLEXPORT(void) STATE_APIENTRY
-crStateGLSLProgramCacheUniforms(GLuint program, GLsizei cbData, GLvoid *pData)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+crStateGLSLProgramCacheUniforms(PCRStateTracker pState, GLuint program, GLsizei cbData, GLvoid *pData)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
     char *pCurrent = pData;
     GLsizei cbRead, cbName;
@@ -900,7 +920,8 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateGLSLProgramCacheAttribs(GLuint program, GLsizei cbData, GLvoid *pData)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+crStateGLSLProgramCacheAttribs(PCRStateTracker pState, GLuint program, GLsizei cbData, GLvoid *pData)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
     char *pCurrent = pData;
     GLsizei cbRead, cbName;
@@ -998,7 +1019,8 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateGLSLProgramCacheUniforms(GLuint program, GLsizei maxcbData, GLsizei *cbData, GLvoid *pData)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+crStateGLSLProgramCacheUniforms(PCRStateTracker pState, GLuint program, GLsizei maxcbData, GLsizei *cbData, GLvoid *pData)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
     GLint maxUniformLen = 0, activeUniforms=0, fakeUniformsCount, i, j;
     char *pCurrent = pData;
@@ -1015,7 +1037,7 @@
      * "If no active uniform variable exist, 0 is returned."
      */
-    diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniformLen);
+    pState->diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniformLen);
     if (maxUniformLen > 0)
-        diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_UNIFORMS, &activeUniforms);
+        pState->diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_UNIFORMS, &activeUniforms);
 
     *cbData = 0;
@@ -1050,6 +1072,6 @@
         for (i=0; i<activeUniforms; ++i)
         {
-            diff_api.GetActiveUniform(pProgram->hwid, i, maxUniformLen, &cbName, &size, &type, name);
-            location = diff_api.GetUniformLocation(pProgram->hwid, name);
+            pState->diff_api.GetActiveUniform(pProgram->hwid, i, maxUniformLen, &cbName, &size, &type, name);
+            location = pState->diff_api.GetUniformLocation(pProgram->hwid, name);
 
             if (!crStateGLSLProgramCacheOneUniform(location, cbName, name, &pCurrent, &cbWritten, maxcbData))
@@ -1088,5 +1110,5 @@
                     cbName = crStrlen(name);
 
-                    location = diff_api.GetUniformLocation(pProgram->hwid, name);
+                    location = pState->diff_api.GetUniformLocation(pProgram->hwid, name);
 
                     if (!crStateGLSLProgramCacheOneUniform(location, cbName, name, &pCurrent, &cbWritten, maxcbData))
@@ -1134,7 +1156,8 @@
 
 DECLEXPORT(void) STATE_APIENTRY
-crStateGLSLProgramCacheAttribs(GLuint program, GLsizei maxcbData, GLsizei *cbData, GLvoid *pData)
-{
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+crStateGLSLProgramCacheAttribs(PCRStateTracker pState, GLuint program, GLsizei maxcbData, GLsizei *cbData, GLvoid *pData)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
     GLint maxAttribLen, activeAttribs=0, fakeAttribsCount, i, j;
     char *pCurrent = pData;
@@ -1147,6 +1170,6 @@
     }
 
-    diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxAttribLen);
-    diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_ATTRIBUTES, &activeAttribs);
+    pState->diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxAttribLen);
+    pState->diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_ATTRIBUTES, &activeAttribs);
 
     *cbData = 0;
@@ -1181,6 +1204,6 @@
         for (i=0; i<activeAttribs; ++i)
         {
-            diff_api.GetActiveAttrib(pProgram->hwid, i, maxAttribLen, &cbName, &size, &type, name);
-            location = diff_api.GetAttribLocation(pProgram->hwid, name);
+            pState->diff_api.GetActiveAttrib(pProgram->hwid, i, maxAttribLen, &cbName, &size, &type, name);
+            location = pState->diff_api.GetAttribLocation(pProgram->hwid, name);
 
             if (!crStateGLSLProgramCacheOneAttrib(location, cbName, name, &pCurrent, &cbWritten, maxcbData))
@@ -1219,5 +1242,5 @@
                     cbName = crStrlen(name);
 
-                    location = diff_api.GetAttribLocation(pProgram->hwid, name);
+                    location = pState->diff_api.GetAttribLocation(pProgram->hwid, name);
 
                     if (!crStateGLSLProgramCacheOneAttrib(location, cbName, name, &pCurrent, &cbWritten, maxcbData))
@@ -1242,8 +1265,9 @@
 #endif
 
-DECLEXPORT(GLint) STATE_APIENTRY crStateGetUniformLocation(GLuint program, const char * name)
+DECLEXPORT(GLint) STATE_APIENTRY crStateGetUniformLocation(PCRStateTracker pState, GLuint program, const char * name)
 {
 #ifdef IN_GUEST
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
     GLint result=-1;
     GLuint i;
@@ -1272,5 +1296,5 @@
     return result;
 #else
-    RT_NOREF(program, name);
+    RT_NOREF(pState, program, name);
     crWarning("crStateGetUniformLocation called on host side!!");
     return -1;
@@ -1278,8 +1302,9 @@
 }
 
-DECLEXPORT(GLint) STATE_APIENTRY crStateGetAttribLocation(GLuint program, const char * name)
+DECLEXPORT(GLint) STATE_APIENTRY crStateGetAttribLocation(PCRStateTracker pState, GLuint program, const char * name)
 {
 #ifdef IN_GUEST
-    CRGLSLProgram *pProgram = crStateGetProgramObj(program);
+    CRContext *g = GetCurrentContext(pState);
+    CRGLSLProgram *pProgram = crStateGetProgramObj(g, program);
     GLint result=-1;
     GLuint i;
@@ -1308,5 +1333,5 @@
     return result;
 #else
-    RT_NOREF(program, name);
+    RT_NOREF(pState, program, name);
     crWarning("crStateGetAttribLocation called on host side!!");
     return -1;
@@ -1318,7 +1343,8 @@
     CRGLSLShader *pShader = (CRGLSLShader*) data1;
     CRContext *ctx = (CRContext *) data2;
+    PCRStateTracker pState = ctx->pStateTracker;
     (void)ctx; (void)key;
 
-    pShader->hwid = diff_api.CreateShader(pShader->type);
+    pShader->hwid = pState->diff_api.CreateShader(pShader->type);
 }
 
@@ -1338,12 +1364,12 @@
 {
     CRGLSLShader *pShader = (CRGLSLShader*) data1;
+    PCRStateTracker pState = (PCRStateTracker)data2;
     (void) key;
-    (void) data2;
 
     if (pShader->source)
     {
-        diff_api.ShaderSource(pShader->hwid, 1, (const char**)&pShader->source, NULL);
+        pState->diff_api.ShaderSource(pShader->hwid, 1, (const char**)&pShader->source, NULL);
         if (pShader->compiled)
-            diff_api.CompileShader(pShader->hwid);
+            pState->diff_api.CompileShader(pShader->hwid);
         crFree(pShader->source);
         pShader->source = NULL;
@@ -1351,5 +1377,5 @@
 
     if (pShader->deleted)
-        diff_api.DeleteShader(pShader->hwid);
+        pState->diff_api.DeleteShader(pShader->hwid);
 }
 
@@ -1357,15 +1383,16 @@
 {
     CRGLSLShader *pShader = (CRGLSLShader*) data1;
-    CRGLSLProgram *pProgram = (CRGLSLProgram *) data2;
+    PCRStateGLSLShaderWalkArgs pArgs = (PCRStateGLSLShaderWalkArgs)data2;
+    PCRStateTracker pState = pArgs->pState;
     (void) key;
 
     if (pShader->source)
     {
-        diff_api.ShaderSource(pShader->hwid, 1, (const char**)&pShader->source, NULL);
+        pState->diff_api.ShaderSource(pShader->hwid, 1, (const char**)&pShader->source, NULL);
         if (pShader->compiled)
-            diff_api.CompileShader(pShader->hwid);
-    }
-
-    diff_api.AttachShader(pProgram->hwid, pShader->hwid);
+            pState->diff_api.CompileShader(pShader->hwid);
+    }
+
+    pState->diff_api.AttachShader(pArgs->pProgram->hwid, pShader->hwid);
 }
 
@@ -1373,8 +1400,8 @@
 {
     CRGLSLShader *pShader = (CRGLSLShader*) data1;
-    CRGLSLProgram *pProgram = (CRGLSLProgram *) data2;
+    PCRStateGLSLShaderWalkArgs pArgs = (PCRStateGLSLShaderWalkArgs)data2;
     (void) key;
 
-    diff_api.DetachShader(pProgram->hwid, pShader->hwid);
+    pArgs->pState->diff_api.DetachShader(pArgs->pProgram->hwid, pShader->hwid);
 }
 
@@ -1383,8 +1410,14 @@
     CRGLSLProgram *pProgram = (CRGLSLProgram*) data1;
     CRContext *ctx = (CRContext *) data2;
+    PCRStateTracker pState = ctx->pStateTracker;
     GLuint i;
+    CRStateGLSLShaderWalkArgs Args;
+
+    Args.pProgram = pProgram;
+    Args.pState   = pState;
+
     (void)key;
 
-    pProgram->hwid = diff_api.CreateProgram();
+    pProgram->hwid = pState->diff_api.CreateProgram();
 
     if (pProgram->linked)
@@ -1393,18 +1426,18 @@
 
         crHashtableWalk(pProgram->activeState.attachedShaders, crStateFixAttachedShaderHWIDsCB, ctx);
-        crHashtableWalk(pProgram->activeState.attachedShaders, crStateAttachShaderCB, pProgram);
+        crHashtableWalk(pProgram->activeState.attachedShaders, crStateAttachShaderCB, &Args);
 
         for (i=0; i<pProgram->activeState.cAttribs; ++i)
         {
-            diff_api.BindAttribLocation(pProgram->hwid, pProgram->activeState.pAttribs[i].index, pProgram->activeState.pAttribs[i].name);
+            pState->diff_api.BindAttribLocation(pProgram->hwid, pProgram->activeState.pAttribs[i].index, pProgram->activeState.pAttribs[i].name);
         }
 
         if (pProgram->validated)
-            diff_api.ValidateProgram(pProgram->hwid);
-
-        diff_api.LinkProgram(pProgram->hwid);
-    }
-
-    diff_api.UseProgram(pProgram->hwid);
+            pState->diff_api.ValidateProgram(pProgram->hwid);
+
+        pState->diff_api.LinkProgram(pProgram->hwid);
+    }
+
+    pState->diff_api.UseProgram(pProgram->hwid);
 
     for (i=0; i<pProgram->cUniforms; ++i)
@@ -1414,43 +1447,43 @@
         GLint *pIdata = (GLint*)pProgram->pUniforms[i].data;
 
-        location = diff_api.GetUniformLocation(pProgram->hwid, pProgram->pUniforms[i].name);
+        location = pState->diff_api.GetUniformLocation(pProgram->hwid, pProgram->pUniforms[i].name);
         switch (pProgram->pUniforms[i].type)
         {
             case GL_FLOAT:
-                diff_api.Uniform1fv(location, 1, pFdata);
+                pState->diff_api.Uniform1fv(location, 1, pFdata);
                 break;
             case GL_FLOAT_VEC2:
-                diff_api.Uniform2fv(location, 1, pFdata);
+                pState->diff_api.Uniform2fv(location, 1, pFdata);
                 break;
             case GL_FLOAT_VEC3:
-                diff_api.Uniform3fv(location, 1, pFdata);
+                pState->diff_api.Uniform3fv(location, 1, pFdata);
                 break;
             case GL_FLOAT_VEC4:
-                diff_api.Uniform4fv(location, 1, pFdata);
+                pState->diff_api.Uniform4fv(location, 1, pFdata);
                 break;
             case GL_INT:
             case GL_BOOL:
-                diff_api.Uniform1iv(location, 1, pIdata);
+                pState->diff_api.Uniform1iv(location, 1, pIdata);
                 break;
             case GL_INT_VEC2:
             case GL_BOOL_VEC2:
-                diff_api.Uniform2iv(location, 1, pIdata);
+                pState->diff_api.Uniform2iv(location, 1, pIdata);
                 break;
             case GL_INT_VEC3:
             case GL_BOOL_VEC3:
-                diff_api.Uniform3iv(location, 1, pIdata);
+                pState->diff_api.Uniform3iv(location, 1, pIdata);
                 break;
             case GL_INT_VEC4:
             case GL_BOOL_VEC4:
-                diff_api.Uniform4iv(location, 1, pIdata);
+                pState->diff_api.Uniform4iv(location, 1, pIdata);
                 break;
             case GL_FLOAT_MAT2:
-                diff_api.UniformMatrix2fv(location, 1, GL_FALSE, pFdata);
+                pState->diff_api.UniformMatrix2fv(location, 1, GL_FALSE, pFdata);
                 break;
             case GL_FLOAT_MAT3:
-                diff_api.UniformMatrix3fv(location, 1, GL_FALSE, pFdata);
+                pState->diff_api.UniformMatrix3fv(location, 1, GL_FALSE, pFdata);
                 break;
             case GL_FLOAT_MAT4:
-                diff_api.UniformMatrix4fv(location, 1, GL_FALSE, pFdata);
+                pState->diff_api.UniformMatrix4fv(location, 1, GL_FALSE, pFdata);
                 break;
             case GL_SAMPLER_1D:
@@ -1462,24 +1495,24 @@
             case GL_SAMPLER_2D_RECT_ARB:
             case GL_SAMPLER_2D_RECT_SHADOW_ARB:
-                diff_api.Uniform1iv(location, 1, pIdata);
+                pState->diff_api.Uniform1iv(location, 1, pIdata);
                 break;
 #ifdef CR_OPENGL_VERSION_2_1
             case GL_FLOAT_MAT2x3:
-                diff_api.UniformMatrix2x3fv(location, 1, GL_FALSE, pFdata);
+                pState->diff_api.UniformMatrix2x3fv(location, 1, GL_FALSE, pFdata);
                 break;
             case GL_FLOAT_MAT2x4:
-                diff_api.UniformMatrix2x4fv(location, 1, GL_FALSE, pFdata);
+                pState->diff_api.UniformMatrix2x4fv(location, 1, GL_FALSE, pFdata);
                 break;
             case GL_FLOAT_MAT3x2:
-                diff_api.UniformMatrix3x2fv(location, 1, GL_FALSE, pFdata);
+                pState->diff_api.UniformMatrix3x2fv(location, 1, GL_FALSE, pFdata);
                 break;
             case GL_FLOAT_MAT3x4:
-                diff_api.UniformMatrix3x4fv(location, 1, GL_FALSE, pFdata);
+                pState->diff_api.UniformMatrix3x4fv(location, 1, GL_FALSE, pFdata);
                 break;
             case GL_FLOAT_MAT4x2:
-                diff_api.UniformMatrix4x2fv(location, 1, GL_FALSE, pFdata);
+                pState->diff_api.UniformMatrix4x2fv(location, 1, GL_FALSE, pFdata);
                 break;
             case GL_FLOAT_MAT4x3:
-                diff_api.UniformMatrix4x3fv(location, 1, GL_FALSE, pFdata);
+                pState->diff_api.UniformMatrix4x3fv(location, 1, GL_FALSE, pFdata);
                 break;
 #endif
@@ -1496,9 +1529,10 @@
 
     crHashtableWalk(pProgram->activeState.attachedShaders, crStateDetachShaderCB, pProgram);
-    crHashtableWalk(pProgram->currentState.attachedShaders, crStateAttachShaderCB, pProgram);
+    crHashtableWalk(pProgram->currentState.attachedShaders, crStateAttachShaderCB, &Args);
 }
 
 DECLEXPORT(void) STATE_APIENTRY crStateGLSLSwitch(CRContext *from, CRContext *to)
 {
+    PCRStateTracker pState = to->pStateTracker;
     GLboolean fForceUseProgramSet = GL_FALSE;
     if (to->glsl.bResyncNeeded)
@@ -1513,10 +1547,10 @@
         fForceUseProgramSet = GL_TRUE;
 
-        crHashtableWalk(to->glsl.shaders, crStateGLSLSyncShadersCB, NULL);
+        crHashtableWalk(to->glsl.shaders, crStateGLSLSyncShadersCB, pState);
     }
 
     if (to->glsl.activeProgram != from->glsl.activeProgram || fForceUseProgramSet)
     {
-        diff_api.UseProgram(to->glsl.activeProgram ? to->glsl.activeProgram->hwid : 0);
-    }
-}
+        pState->diff_api.UseProgram(to->glsl.activeProgram ? to->glsl.activeProgram->hwid : 0);
+    }
+}
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_hint.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_hint.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_hint.c	(revision 78375)
@@ -13,5 +13,5 @@
 {
 	CRHintState *h = &ctx->hint;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
 	CRHintBits *hb = &(sb->hint);
 
@@ -41,14 +41,14 @@
 }
 
-void STATE_APIENTRY crStateHint(GLenum target, GLenum mode) 
+void STATE_APIENTRY crStateHint(PCRStateTracker pState, GLenum target, GLenum mode) 
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRHintState *h = &(g->hint);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRHintBits *hb = &(sb->hint);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 					 "glHint called in Begin/End");
 		return;
@@ -58,5 +58,5 @@
 
 	if (mode != GL_FASTEST && mode != GL_NICEST && mode != GL_DONT_CARE) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 					 "glHint(mode)");
 		return;
@@ -91,5 +91,5 @@
 		}
 		else {
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 						 "glHint(target)");
 			return;
@@ -104,5 +104,5 @@
 		}
 		else {
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 						 "glHint(target)");
 			return;
@@ -117,5 +117,5 @@
 		}
 		else {
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 						 "glHint(target)");
 			return;
@@ -124,5 +124,5 @@
 #endif
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 					 "glHint(target)");
 		return;
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_init.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_init.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_init.c	(revision 78375)
@@ -11,18 +11,4 @@
 
 #include <iprt/asm.h>
-
-static bool __isContextTLSInited = false;
-CRtsd __contextTSD;
-
-CRStateBits *__currentBits = NULL;
-CRContext *g_pAvailableContexts[CR_MAX_CONTEXTS];
-uint32_t g_cContexts = 0;
-
-static CRSharedState *gSharedState=NULL;
-
-static CRContext *defaultContext = NULL;
-
-GLboolean g_bVBoxEnableDiffOnMakeCurrent = GL_TRUE;
-
 
 /**
@@ -50,8 +36,10 @@
  */
 DECLEXPORT(void)
-crStateDeleteTextureCallback(void *texObj)
-{
+crStateDeleteTextureCallback(void *texObj, void *pvUser)
+{
+    PCRStateTracker pState = (PCRStateTracker)pvUser;
+
 #ifndef IN_GUEST
-    diff_api.DeleteTextures(1, &((CRTextureObj *)texObj)->hwid);
+    pState->diff_api.DeleteTextures(1, &((CRTextureObj *)texObj)->hwid);
 #endif
     crStateDeleteTextureObject((CRTextureObj *) texObj);
@@ -60,12 +48,13 @@
 typedef struct CR_STATE_RELEASEOBJ
 {
-    CRContext *pCtx;
-    CRSharedState *s;
+    PCRStateTracker pState;
+    CRContext       *pCtx;
+    CRSharedState   *s;
 } CR_STATE_RELEASEOBJ, *PCR_STATE_RELEASEOBJ;
 
-void crStateOnTextureUsageRelease(CRSharedState *pS, CRTextureObj *pObj)
+void crStateOnTextureUsageRelease(PCRStateTracker pState, CRSharedState *pS, CRTextureObj *pObj)
 {
     if (!pObj->pinned)
-        crHashtableDelete(pS->textureTable, pObj->id, crStateDeleteTextureCallback);
+        crHashtableDeleteEx(pS->textureTable, pObj->id, crStateDeleteTextureCallback, pState);
     else
         Assert(crHashtableSearch(pS->textureTable, pObj->id));
@@ -79,5 +68,5 @@
         return;
 
-    crStateOnTextureUsageRelease(pS, pObj);
+    crStateOnTextureUsageRelease(pCtx->pStateTracker, pS, pObj);
 }
 
@@ -89,5 +78,5 @@
         return;
 
-    if (!gSharedState)
+    if (!pCtx->pStateTracker->pSharedState)
     {
         WARN(("no global shared"));
@@ -95,5 +84,5 @@
     }
 
-    crStateOnTextureUsageRelease(gSharedState, pObj);
+    crStateOnTextureUsageRelease(pCtx->pStateTracker, pCtx->pStateTracker->pSharedState, pObj);
 }
 
@@ -103,5 +92,5 @@
     CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pCtx);
     if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
-        crHashtableDelete(pS->buffersTable, pObj->id, crStateFreeBufferObject);
+        crHashtableDeleteEx(pS->buffersTable, pObj->id, crStateFreeBufferObject, pCtx->pStateTracker);
 }
 
@@ -111,5 +100,5 @@
     CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pCtx);
     if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
-        crHashtableDelete(pS->fbTable, pObj->id, crStateFreeFBO);
+        crHashtableDeleteEx(pS->fbTable, pObj->id, crStateFreeFBO, pCtx->pStateTracker);
 }
 
@@ -119,5 +108,5 @@
     CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pCtx);
     if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
-        crHashtableDelete(pS->rbTable, pObj->id, crStateFreeRBO);
+        crHashtableDeleteEx(pS->rbTable, pObj->id, crStateFreeRBO, pCtx->pStateTracker);
 }
 
@@ -159,7 +148,7 @@
  */
 #ifndef IN_GUEST
-DECLEXPORT(void) crStateFreeShared(CRContext *pContext, CRSharedState *s)
+DECLEXPORT(void) crStateFreeShared(PCRStateTracker pState, CRContext *pContext, CRSharedState *s)
 #else
-static void crStateFreeShared(CRContext *pContext, CRSharedState *s)
+static void crStateFreeShared(PCRStateTracker pState, CRContext *pContext, CRSharedState *s)
 #endif
 {
@@ -168,13 +157,11 @@
     Assert(refCount >= 0);
     if (refCount <= 0) {
-        if (s==gSharedState)
-        {
-            gSharedState = NULL;
-        }
-        crFreeHashtable(s->textureTable, crStateDeleteTextureCallback);
+        if (s == pState->pSharedState)
+            pState->pSharedState = NULL;
+        crFreeHashtableEx(s->textureTable, crStateDeleteTextureCallback, pState);
         crFreeHashtable(s->dlistTable, crFree); /* call crFree for each entry */
-        crFreeHashtable(s->buffersTable, crStateFreeBufferObject);
-        crFreeHashtable(s->fbTable, crStateFreeFBO);
-        crFreeHashtable(s->rbTable, crStateFreeRBO);
+        crFreeHashtableEx(s->buffersTable, crStateFreeBufferObject, pState);
+        crFreeHashtableEx(s->fbTable, crStateFreeFBO, pState);
+        crFreeHashtableEx(s->rbTable, crStateFreeRBO, pState);
         crFree(s);
     }
@@ -183,6 +170,7 @@
         /* evaluate usage bits*/
         CR_STATE_RELEASEOBJ CbData;
-        CbData.pCtx = pContext;
-        CbData.s = s;
+        CbData.pState = pState;
+        CbData.pCtx   = pContext;
+        CbData.s      = s;
         crHashtableWalk(s->textureTable, ReleaseTextureCallback, &CbData);
         crHashtableWalk(s->buffersTable, ReleaseBufferObjectCallback , &CbData);
@@ -194,31 +182,30 @@
 #ifndef IN_GUEST
 
-DECLEXPORT(CRSharedState *) crStateGlobalSharedAcquire(void)
-{
-    if (!gSharedState)
+DECLEXPORT(CRSharedState *) crStateGlobalSharedAcquire(PCRStateTracker pState)
+{
+    if (!pState->pSharedState)
     {
         crWarning("No Global Shared State!");
         return NULL;
     }
-    ASMAtomicIncS32(&gSharedState->refCount);
-    return gSharedState;
-}
-
-DECLEXPORT(void) crStateGlobalSharedRelease(void)
-{
-    crStateFreeShared(NULL, gSharedState);
+    ASMAtomicIncS32(&pState->pSharedState->refCount);
+    return pState->pSharedState;
+}
+
+DECLEXPORT(void) crStateGlobalSharedRelease(PCRStateTracker pState)
+{
+    crStateFreeShared(pState, NULL, pState->pSharedState);
 }
 
 #endif /* IN_GUEST */
 
-DECLEXPORT(void) STATE_APIENTRY
-crStateShareContext(GLboolean value)
-{
-    CRContext *pCtx = GetCurrentContext();
+DECLEXPORT(void) STATE_APIENTRY crStateShareContext(PCRStateTracker pState, GLboolean value)
+{
+    CRContext *pCtx = GetCurrentContext(pState);
     CRASSERT(pCtx && pCtx->shared);
 
     if (value)
     {
-        if (pCtx->shared == gSharedState)
+        if (pCtx->shared == pState->pSharedState)
         {
             return;
@@ -227,18 +214,18 @@
         crDebug("Context(%i) shared", pCtx->id);
 
-        if (!gSharedState)
-        {
-            gSharedState = pCtx->shared;
+        if (!pState->pSharedState)
+        {
+            pState->pSharedState = pCtx->shared;
         }
         else
         {
-            crStateFreeShared(pCtx, pCtx->shared);
-            pCtx->shared = gSharedState;
-            ASMAtomicIncS32(&gSharedState->refCount);
+            crStateFreeShared(pState, pCtx, pCtx->shared);
+            pCtx->shared = pState->pSharedState;
+            ASMAtomicIncS32(&pState->pSharedState->refCount);
         }
     }
     else
     {
-        if (pCtx->shared != gSharedState)
+        if (pCtx->shared != pState->pSharedState)
         {
             return;
@@ -247,7 +234,7 @@
         crDebug("Context(%i) unshared", pCtx->id);
 
-        if (gSharedState->refCount==1)
-        {
-            gSharedState = NULL;
+        if (pState->pSharedState->refCount==1)
+        {
+            pState->pSharedState = NULL;
         }
         else
@@ -255,11 +242,10 @@
             pCtx->shared = crStateAllocShared();
             pCtx->shared->id = pCtx->id;
-            crStateFreeShared(pCtx, gSharedState);
-        }
-    }
-}
-
-DECLEXPORT(void) STATE_APIENTRY
-crStateShareLists(CRContext *pContext1, CRContext *pContext2)
+            crStateFreeShared(pState, pCtx, pState->pSharedState);
+        }
+    }
+}
+
+DECLEXPORT(void) STATE_APIENTRY crStateShareLists(CRContext *pContext1, CRContext *pContext2)
 {
     CRASSERT(pContext1->shared);
@@ -271,24 +257,24 @@
     }
 
-    crStateFreeShared(pContext1, pContext1->shared);
+    crStateFreeShared(pContext1->pStateTracker, pContext1, pContext1->shared);
     pContext1->shared = pContext2->shared;
     ASMAtomicIncS32(&pContext2->shared->refCount);
 }
 
-DECLEXPORT(GLboolean) STATE_APIENTRY
-crStateContextIsShared(CRContext *pCtx)
-{
-    return pCtx->shared==gSharedState;
-}
-
-DECLEXPORT(void) STATE_APIENTRY
-crStateSetSharedContext(CRContext *pCtx)
-{
-    if (gSharedState)
-    {
-        crWarning("crStateSetSharedContext: shared is being changed from %p to %p", gSharedState, pCtx->shared);
-    }
-
-    gSharedState = pCtx->shared;
+DECLEXPORT(GLboolean) STATE_APIENTRY crStateContextIsShared(CRContext *pCtx)
+{
+    return pCtx->shared == pCtx->pStateTracker->pSharedState;
+}
+
+DECLEXPORT(void) STATE_APIENTRY crStateSetSharedContext(CRContext *pCtx)
+{
+    PCRStateTracker pState = pCtx->pStateTracker;
+
+    if (pState->pSharedState)
+    {
+        crWarning("crStateSetSharedContext: shared is being changed from %p to %p", pState->pSharedState, pCtx->shared);
+    }
+
+    pState->pSharedState = pCtx->shared;
 }
 
@@ -304,5 +290,5 @@
  */
 static CRContext *
-crStateCreateContextId(int i, const CRLimitsState *limits, GLint visBits, CRContext *shareCtx)
+crStateCreateContextId(PCRStateTracker pState, int i, const CRLimitsState *limits, GLint visBits, CRContext *shareCtx)
 {
     CRContext *ctx;
@@ -312,5 +298,5 @@
     (void)limits;
 
-    if (g_pAvailableContexts[i] != NULL)
+    if (pState->apAvailableContexts[i] != NULL)
     {
         crWarning("trying to create context with used id");
@@ -324,8 +310,9 @@
         return NULL;
     }
-    g_pAvailableContexts[i] = ctx;
-    ++g_cContexts;
-    CRASSERT(g_cContexts < RT_ELEMENTS(g_pAvailableContexts));
+    pState->apAvailableContexts[i] = ctx;
+    pState->cContexts++;
+    CRASSERT(pState->cContexts < RT_ELEMENTS(pState->apAvailableContexts));
     ctx->id = i;
+    ctx->pStateTracker = pState;
     VBoxTlsRefInit(ctx, crStateContextDtor);
     ctx->flush_func = NULL;
@@ -432,12 +419,14 @@
 crStateFreeContext(CRContext *ctx)
 {
+    PCRStateTracker pState = ctx->pStateTracker;
+
 #ifndef DEBUG_misha
-    CRASSERT(g_pAvailableContexts[ctx->id] == ctx);
+    CRASSERT(pState->apAvailableContexts[ctx->id] == ctx);
 #endif
-    if (g_pAvailableContexts[ctx->id] == ctx)
-    {
-        g_pAvailableContexts[ctx->id] = NULL;
-        --g_cContexts;
-        CRASSERT(g_cContexts < RT_ELEMENTS(g_pAvailableContexts));
+    if (pState->apAvailableContexts[ctx->id] == ctx)
+    {
+        pState->apAvailableContexts[ctx->id] = NULL;
+        pState->cContexts--;
+        CRASSERT(pState->cContexts < RT_ELEMENTS(pState->apAvailableContexts));
     }
     else
@@ -458,5 +447,5 @@
     crStateTextureDestroy( ctx );
     crStateTransformDestroy( ctx );
-    crStateFreeShared(ctx, ctx->shared);
+    crStateFreeShared(pState, ctx, ctx->shared);
     crStateFramebufferObjectDestroy(ctx);
     crStateGLSLDestroy(ctx);
@@ -483,13 +472,14 @@
  * pointer somewhere.
  */
-void crStateInit(void)
+void crStateInit(PCRStateTracker pState)
 {
     unsigned int i;
 
     /* Purely initialize the context bits */
-    if (!__currentBits) {
-        __currentBits = (CRStateBits *) crCalloc( sizeof(CRStateBits) );
-        crStateClientInitBits( &(__currentBits->client) );
-        crStateLightingInitBits( &(__currentBits->lighting) );
+    if (!pState->pCurrentBits) {
+        pState->fVBoxEnableDiffOnMakeCurrent = GL_TRUE;
+        pState->pCurrentBits = (CRStateBits *) crCalloc( sizeof(CRStateBits) );
+        crStateClientInitBits( &(pState->pCurrentBits->client) );
+        crStateLightingInitBits( &(pState->pCurrentBits->lighting) );
     } else
     {
@@ -500,68 +490,68 @@
 
     for (i=0;i<CR_MAX_CONTEXTS;i++)
-        g_pAvailableContexts[i] = NULL;
-    g_cContexts = 0;
-
-    if (!__isContextTLSInited)
+        pState->apAvailableContexts[i] = NULL;
+    pState->cContexts = 0;
+
+    if (!pState->fContextTLSInit)
     {
 # ifndef RT_OS_WINDOWS
         /* tls destructor is implemented for all platforms except windows*/
-        crInitTSDF(&__contextTSD, crStateThreadTlsDtor);
+        crInitTSDF(&pState->contextTSD, crStateThreadTlsDtor);
 # else
         /* windows should do cleanup via DllMain THREAD_DETACH notification */
-        crInitTSD(&__contextTSD);
+        crInitTSD(&pState->contextTSD);
 # endif
-        __isContextTLSInited = 1;
-    }
-
-    if (defaultContext) {
+        pState->fContextTLSInit = true;
+    }
+
+    if (pState->pDefaultContext) {
         /* Free the default/NULL context.
          * Ensures context bits are reset */
-        SetCurrentContext(NULL);
-        VBoxTlsRefRelease(defaultContext);
+        SetCurrentContext(pState, NULL);
+        VBoxTlsRefRelease(pState->pDefaultContext);
     }
 
     /* Reset diff_api */
-    crMemZero(&diff_api, sizeof(SPUDispatchTable));
-
-    Assert(!gSharedState);
-    gSharedState = NULL;
+    crMemZero(&pState->diff_api, sizeof(SPUDispatchTable));
+
+    Assert(!pState->pSharedState);
+    pState->pSharedState = NULL;
 
     /* Allocate the default/NULL context */
-    CRASSERT(g_pAvailableContexts[0] == NULL);
-    defaultContext = crStateCreateContextId(0, NULL, CR_RGB_BIT, NULL);
-    CRASSERT(g_pAvailableContexts[0] == defaultContext);
-    CRASSERT(g_cContexts == 1);
-    SetCurrentContext(defaultContext);
-}
-
-void crStateDestroy(void)
+    CRASSERT(pState->apAvailableContexts[0] == NULL);
+    pState->pDefaultContext = crStateCreateContextId(pState, 0, NULL, CR_RGB_BIT, NULL);
+    CRASSERT(pState->apAvailableContexts[0] == pState->pDefaultContext);
+    CRASSERT(pState->cContexts == 1);
+    SetCurrentContext(pState, pState->pDefaultContext);
+}
+
+void crStateDestroy(PCRStateTracker pState)
 {
     int i;
-    if (__currentBits)
-    {
-        crStateClientDestroyBits(&(__currentBits->client));
-        crStateLightingDestroyBits(&(__currentBits->lighting));
-        crFree(__currentBits);
-        __currentBits = NULL;
-    }
-
-    SetCurrentContext(NULL);
+    if (pState->pCurrentBits)
+    {
+        crStateClientDestroyBits(&(pState->pCurrentBits->client));
+        crStateLightingDestroyBits(&(pState->pCurrentBits->lighting));
+        crFree(pState->pCurrentBits);
+        pState->pCurrentBits = NULL;
+    }
+
+    SetCurrentContext(pState, NULL);
 
     for (i = CR_MAX_CONTEXTS-1; i >= 0; i--)
     {
-        if (g_pAvailableContexts[i])
-        {
-            if (VBoxTlsRefIsFunctional(g_pAvailableContexts[i]))
-                VBoxTlsRefRelease(g_pAvailableContexts[i]);
+        if (pState->apAvailableContexts[i])
+        {
+            if (VBoxTlsRefIsFunctional(pState->apAvailableContexts[i]))
+                VBoxTlsRefRelease(pState->apAvailableContexts[i]);
         }
     }
 
     /* default context was stored in g_pAvailableContexts[0], so it was destroyed already */
-    defaultContext = NULL;
-
-
-    crFreeTSD(&__contextTSD);
-    __isContextTLSInited = 0;
+    pState->pDefaultContext = NULL;
+
+
+    crFreeTSD(&pState->contextTSD);
+    pState->fContextTLSInit = false;
 }
 
@@ -610,18 +600,18 @@
 
 CRContext *
-crStateCreateContext(const CRLimitsState *limits, GLint visBits, CRContext *share)
-{
-    return crStateCreateContextEx(limits, visBits, share, -1);
+crStateCreateContext(PCRStateTracker pState, const CRLimitsState *limits, GLint visBits, CRContext *share)
+{
+    return crStateCreateContextEx(pState, limits, visBits, share, -1);
 }
 
 CRContext *
-crStateCreateContextEx(const CRLimitsState *limits, GLint visBits, CRContext *share, GLint presetID)
+crStateCreateContextEx(PCRStateTracker pState, const CRLimitsState *limits, GLint visBits, CRContext *share, GLint presetID)
 {
     /* Must have created the default context via crStateInit() first */
-    CRASSERT(defaultContext);
+    CRASSERT(pState->pDefaultContext);
 
     if (presetID>0)
     {
-        if(g_pAvailableContexts[presetID])
+        if(pState->apAvailableContexts[presetID])
         {
             crWarning("requesting to create context with already allocated id");
@@ -635,5 +625,5 @@
         for (i = 1 ; i < CR_MAX_CONTEXTS ; i++)
         {
-            if (!g_pAvailableContexts[i])
+            if (!pState->apAvailableContexts[i])
             {
                 presetID = i;
@@ -651,30 +641,30 @@
     }
 
-    return crStateCreateContextId(presetID, limits, visBits, share);
-}
-
-void crStateDestroyContext( CRContext *ctx )
-{
-    CRContext *current = GetCurrentContext();
+    return crStateCreateContextId(pState, presetID, limits, visBits, share);
+}
+
+void crStateDestroyContext(PCRStateTracker pState, CRContext *ctx )
+{
+    CRContext *current = GetCurrentContext(pState);
 
     if (current == ctx) {
         /* destroying the current context - have to be careful here */
-        CRASSERT(defaultContext);
+        CRASSERT(pState->pDefaultContext);
         /* Check to see if the differencer exists first,
            we may not have one, aka the packspu */
-        if (diff_api.AlphaFunc)
-            crStateSwitchContext(current, defaultContext);
-
-        SetCurrentContext(defaultContext);
+        if (pState->diff_api.AlphaFunc)
+            crStateSwitchContext(current, pState->pDefaultContext);
+
+        SetCurrentContext(pState, pState->pDefaultContext);
         /* ensure matrix state is also current */
-        crStateMatrixMode(defaultContext->transform.matrixMode);
+        crStateMatrixMode(pState, pState->pDefaultContext->transform.matrixMode);
     }
 
     VBoxTlsRefMarkDestroy(ctx);
 # ifdef IN_GUEST
-    if (VBoxTlsRefCountGet(ctx) > 1 && ctx->shared == gSharedState)
+    if (VBoxTlsRefCountGet(ctx) > 1 && ctx->shared == pState->pSharedState)
     {
         /* we always need to free the global shared state to prevent the situation when guest thinks the shared objects are still valid, while host destroys them */
-        crStateFreeShared(ctx, ctx->shared);
+        crStateFreeShared(pState, ctx, ctx->shared);
         ctx->shared = crStateAllocShared();
     }
@@ -683,18 +673,18 @@
 }
 
-GLboolean crStateEnableDiffOnMakeCurrent(GLboolean fEnable)
-{
-    GLboolean bOld = g_bVBoxEnableDiffOnMakeCurrent;
-    g_bVBoxEnableDiffOnMakeCurrent = fEnable;
+GLboolean crStateEnableDiffOnMakeCurrent(PCRStateTracker pState, GLboolean fEnable)
+{
+    GLboolean bOld = pState->fVBoxEnableDiffOnMakeCurrent;
+    pState->fVBoxEnableDiffOnMakeCurrent = fEnable;
     return bOld;
 }
 
-void crStateMakeCurrent( CRContext *ctx )
-{
-    CRContext *current = GetCurrentContext();
+void crStateMakeCurrent(PCRStateTracker pState, CRContext *ctx )
+{
+    CRContext *current = GetCurrentContext(pState);
     CRContext *pLocalCtx = ctx;
 
     if (pLocalCtx == NULL)
-        pLocalCtx = defaultContext;
+        pLocalCtx = pState->pDefaultContext;
 
     if (current == pLocalCtx)
@@ -703,15 +693,15 @@
     CRASSERT(pLocalCtx);
 
-    if (g_bVBoxEnableDiffOnMakeCurrent && current) {
+    if (pState->fVBoxEnableDiffOnMakeCurrent && current) {
         /* Check to see if the differencer exists first,
            we may not have one, aka the packspu */
-        if (diff_api.AlphaFunc)
-            crStateSwitchContext( current, pLocalCtx );
-    }
-
-    SetCurrentContext(pLocalCtx);
+        if (pState->diff_api.AlphaFunc)
+            crStateSwitchContext(current, pLocalCtx );
+    }
+
+    SetCurrentContext(pState, pLocalCtx);
 
     /* ensure matrix state is also current */
-    crStateMatrixMode(pLocalCtx->transform.matrixMode);
+    crStateMatrixMode(pState, pLocalCtx->transform.matrixMode);
 }
 
@@ -720,44 +710,44 @@
  * As above, but don't call crStateSwitchContext().
  */
-static void crStateSetCurrentEx( CRContext *ctx, GLboolean fCleanupDefault )
-{
-    CRContext *current = GetCurrentContext();
+static void crStateSetCurrentEx(PCRStateTracker pState, CRContext *ctx, GLboolean fCleanupDefault )
+{
+    CRContext *current = GetCurrentContext(pState);
     CRContext *pLocalCtx = ctx;
 
     if (pLocalCtx == NULL && !fCleanupDefault)
-        pLocalCtx = defaultContext;
+        pLocalCtx = pState->pDefaultContext;
 
     if (current == pLocalCtx)
         return; /* no-op */
 
-    SetCurrentContext(pLocalCtx);
+    SetCurrentContext(pState, pLocalCtx);
     if (pLocalCtx)
     {
         /* ensure matrix state is also current */
-        crStateMatrixMode(pLocalCtx->transform.matrixMode);
-    }
-}
-
-void crStateSetCurrent( CRContext *ctx )
-{
-    crStateSetCurrentEx( ctx, GL_FALSE );
-}
-
-void crStateCleanupCurrent(void)
-{
-    crStateSetCurrentEx( NULL, GL_TRUE );
-}
-
-
-CRContext *crStateGetCurrent(void)
-{
-    return GetCurrentContext();
-}
-
-
-void crStateUpdateColorBits(void)
+        crStateMatrixMode(pState, pLocalCtx->transform.matrixMode);
+    }
+}
+
+void crStateSetCurrent(PCRStateTracker pState, CRContext *ctx )
+{
+    crStateSetCurrentEx(pState, ctx, GL_FALSE );
+}
+
+void crStateCleanupCurrent(PCRStateTracker pState)
+{
+    crStateSetCurrentEx(pState, NULL, GL_TRUE );
+}
+
+
+CRContext *crStateGetCurrent(PCRStateTracker pState)
+{
+    return GetCurrentContext(pState);
+}
+
+
+void crStateUpdateColorBits(PCRStateTracker pState)
 {
     /* This is a hack to force updating the 'current' attribs */
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     FILLDIRTY(sb->current.dirty);
     FILLDIRTY(sb->current.vertexAttrib[VERT_ATTRIB_COLOR0]);
@@ -766,48 +756,54 @@
 
 void STATE_APIENTRY
-crStateChromiumParameteriCR( GLenum target, GLint value )
+crStateChromiumParameteriCR(PCRStateTracker pState, GLenum target, GLint value )
 {
     /* This no-op function helps smooth code-gen */
+    RT_NOREF(pState);
     (void)target; (void)value;
 }
 
 void STATE_APIENTRY
-crStateChromiumParameterfCR( GLenum target, GLfloat value )
+crStateChromiumParameterfCR(PCRStateTracker pState, GLenum target, GLfloat value )
 {
     /* This no-op function helps smooth code-gen */
+    RT_NOREF(pState);
     (void)target; (void)value;
 }
 
 void STATE_APIENTRY
-crStateChromiumParametervCR( GLenum target, GLenum type, GLsizei count, const GLvoid *values )
+crStateChromiumParametervCR(PCRStateTracker pState, GLenum target, GLenum type, GLsizei count, const GLvoid *values )
 {
     /* This no-op function helps smooth code-gen */
+    RT_NOREF(pState);
     (void)target; (void)type; (void)count; (void)values;
 }
 
 void STATE_APIENTRY
-crStateGetChromiumParametervCR( GLenum target, GLuint index, GLenum type, GLsizei count, GLvoid *values )
+crStateGetChromiumParametervCR(PCRStateTracker pState, GLenum target, GLuint index, GLenum type, GLsizei count, GLvoid *values )
 {
     /* This no-op function helps smooth code-gen */
+    RT_NOREF(pState);
     (void)target; (void)index; (void)type; (void)count; (void)values;
 }
 
 void STATE_APIENTRY
-crStateReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
-                                     GLenum format, GLenum type, GLvoid *pixels )
+crStateReadPixels(PCRStateTracker pState, GLint x, GLint y, GLsizei width, GLsizei height,
+                  GLenum format, GLenum type, GLvoid *pixels )
 {
     /* This no-op function helps smooth code-gen */
+    RT_NOREF(pState);
     (void)x; (void)y; (void)width; (void)height; (void)format; (void)type; (void)pixels;
 }
 
-void crStateVBoxDetachThread(void)
+void crStateVBoxDetachThread(PCRStateTracker pState)
 {
     /* release the context ref so that it can be freed */
-    SetCurrentContext(NULL);
-}
-
-
-void crStateVBoxAttachThread(void)
-{
+    SetCurrentContext(pState, NULL);
+}
+
+
+void crStateVBoxAttachThread(PCRStateTracker pState)
+{
+    RT_NOREF(pState);
 }
 
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_isenabled.py
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_isenabled.py	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_isenabled.py	(revision 78375)
@@ -44,11 +44,11 @@
 #include "state/cr_statetypes.h"
 
-GLboolean STATE_APIENTRY crStateIsEnabled( GLenum pname )
+GLboolean STATE_APIENTRY crStateIsEnabled(PCRStateTracker pState, GLenum pname )
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glGet called in Begin/End");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glGet called in Begin/End");
 		return 0;
 	}
@@ -70,5 +70,5 @@
 	print('#endif /* CR_%s */' % ext)
 print("\tdefault:")
-print("\t\tcrStateError(__LINE__, __FILE__, GL_INVALID_ENUM, \"glIsEnabled: Unknown enum: %d\", pname);")
+print("\t\tcrStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, \"glIsEnabled: Unknown enum: %d\", pname);")
 print("\t\treturn 0;")
 print("\t}")
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_lighting.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_lighting.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_lighting.c	(revision 78375)
@@ -29,5 +29,5 @@
 {
 	CRLightingState *l = &ctx->lighting;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
 	CRLightingBits *lb = &(sb->lighting);
 	int i;
@@ -110,14 +110,14 @@
 }
 
-void STATE_APIENTRY crStateShadeModel (GLenum mode)
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateShadeModel (PCRStateTracker pState, GLenum mode)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRLightingState *l = &(g->lighting);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRLightingBits *lb = &(sb->lighting);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "ShadeModel called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "ShadeModel called in begin/end");
 		return;
 	}
@@ -128,5 +128,5 @@
 			mode != GL_FLAT)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "ShadeModel: Bogus mode 0x%x", mode);
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "ShadeModel: Bogus mode 0x%x", mode);
 		return;
 	}
@@ -137,14 +137,14 @@
 }
 
-void STATE_APIENTRY crStateColorMaterial (GLenum face, GLenum mode)
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateColorMaterial (PCRStateTracker pState, GLenum face, GLenum mode)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRLightingState *l = &(g->lighting);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRLightingBits *lb = &(sb->lighting);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "ColorMaterial called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "ColorMaterial called in begin/end");
 		return;
 	}
@@ -156,5 +156,5 @@
 			face != GL_FRONT_AND_BACK)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "ColorMaterial: Bogus face &d", face);
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "ColorMaterial: Bogus face &d", face);
 		return;
 	}
@@ -166,5 +166,5 @@
 			mode != GL_AMBIENT_AND_DIFFUSE)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "ColorMaterial: Bogus mode &d", mode);
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "ColorMaterial: Bogus mode &d", mode);
 		return;
 	}
@@ -180,14 +180,14 @@
 }
 
-void STATE_APIENTRY crStateLightModelfv (GLenum pname, const GLfloat *param)
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateLightModelfv (PCRStateTracker pState, GLenum pname, const GLfloat *param)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRLightingState *l = &(g->lighting);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRLightingBits *lb = &(sb->lighting);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "LightModelfv called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "LightModelfv called in begin/end");
 		return;
 	}
@@ -217,5 +217,5 @@
 			else
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "LightModel: Invalid param for LIGHT_MODEL_COLOR_CONTROL: 0x%x", param[0]);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "LightModel: Invalid param for LIGHT_MODEL_COLOR_CONTROL: 0x%x", param[0]);
 				return;
 			}
@@ -232,5 +232,5 @@
 				else
 				{
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "LightModel: Invalid param for LIGHT_MODEL_COLOR_CONTROL: 0x%x", param[0]);
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "LightModel: Invalid param for LIGHT_MODEL_COLOR_CONTROL: 0x%x", param[0]);
 					return;
 				}
@@ -238,5 +238,5 @@
 			else
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "LightModel( LIGHT_MODEL_COLOR_CONTROL, ...) - EXT_separate_specular_color is unavailable.");
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "LightModel( LIGHT_MODEL_COLOR_CONTROL, ...) - EXT_separate_specular_color is unavailable.");
 				return;
 			}
@@ -245,5 +245,5 @@
 #endif
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "LightModelfv: Invalid pname: 0x%x", pname);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "LightModelfv: Invalid pname: 0x%x", pname);
 			return;
 	}
@@ -252,10 +252,10 @@
 }
 
-void STATE_APIENTRY crStateLightModeliv (GLenum pname, const GLint *param)
+void STATE_APIENTRY crStateLightModeliv (PCRStateTracker pState, GLenum pname, const GLint *param)
 {
 	GLfloat f_param;
 	GLcolor f_color;
 #ifndef CR_OPENGL_VERSION_1_2
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 #endif
 
@@ -265,5 +265,5 @@
 		case GL_LIGHT_MODEL_TWO_SIDE:
 			f_param = (GLfloat) (*param);
-			crStateLightModelfv(pname, &f_param);
+			crStateLightModelfv(pState, pname, &f_param);
 			break;
 		case GL_LIGHT_MODEL_AMBIENT:
@@ -272,10 +272,10 @@
 			f_color.b = ((GLfloat)param[2])/CR_MAXINT;
 			f_color.a = ((GLfloat)param[3])/CR_MAXINT;
-			crStateLightModelfv(pname, (GLfloat *) &f_color);
+			crStateLightModelfv(pState, pname, (GLfloat *) &f_color);
 			break;
 #if defined(CR_OPENGL_VERSION_1_2)
 		case GL_LIGHT_MODEL_COLOR_CONTROL:
 			f_param = (GLfloat) (*param);
-			crStateLightModelfv(pname, &f_param);
+			crStateLightModelfv(pState, pname, &f_param);
 			break;
 #else
@@ -284,7 +284,7 @@
 			if (g->extensions.EXT_separate_specular_color) {
 				f_param = (GLfloat) (*param);
-				crStateLightModelfv(pname, &f_param);
+				crStateLightModelfv(pState, pname, &f_param);
 			} else {
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "LightModeliv(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, ...) - EXT_separate_specular_color not enabled!");
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "LightModeliv(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, ...) - EXT_separate_specular_color not enabled!");
 				return;
 			}
@@ -293,23 +293,23 @@
 #endif
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "LightModeliv: Invalid pname: 0x%x", pname);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "LightModeliv: Invalid pname: 0x%x", pname);
 			return;
 	}
 }
 
-void STATE_APIENTRY crStateLightModelf (GLenum pname, GLfloat param)
-{
-	crStateLightModelfv(pname, &param);
-}
-
-void STATE_APIENTRY crStateLightModeli (GLenum pname, GLint param)
+void STATE_APIENTRY crStateLightModelf (PCRStateTracker pState, GLenum pname, GLfloat param)
+{
+	crStateLightModelfv(pState, pname, &param);
+}
+
+void STATE_APIENTRY crStateLightModeli (PCRStateTracker pState, GLenum pname, GLint param)
 {
 	GLfloat f_param = (GLfloat) param;
-	crStateLightModelfv(pname, &f_param);
-}
-
-void STATE_APIENTRY crStateLightfv (GLenum light, GLenum pname, const GLfloat *param)
-{
-	CRContext *g = GetCurrentContext();
+	crStateLightModelfv(pState, pname, &f_param);
+}
+
+void STATE_APIENTRY crStateLightfv (PCRStateTracker pState, GLenum light, GLenum pname, const GLfloat *param)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRLightingState *l = &(g->lighting);
 	CRTransformState *t = &(g->transform);
@@ -319,5 +319,5 @@
 	CRmatrix inv;
 	CRmatrix *mat;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRLightingBits *lb = &(sb->lighting);
 	CRLightBits *ltb;
@@ -325,5 +325,5 @@
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glLightfv called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glLightfv called in begin/end");
 		return;
 	}
@@ -334,5 +334,5 @@
 	if (i>=g->limits.maxLights)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glLight: invalid light specified: 0x%x", light);
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glLight: invalid light specified: 0x%x", light);
 		return;
 	}
@@ -405,5 +405,5 @@
 			if (*param < 0.0f || *param > 180.0f)
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glLight: spot exponent out of range: %f", *param);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glLight: spot exponent out of range: %f", *param);
 				return;
 			}
@@ -414,5 +414,5 @@
 			if ((*param < 0.0f || *param > 90.0f) && *param != 180.0f)
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glLight: spot cutoff out of range: %f", *param);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glLight: spot cutoff out of range: %f", *param);
 				return;
 			}
@@ -423,5 +423,5 @@
 			if (*param < 0.0f)
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glLight: constant Attenuation negative: %f", *param);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glLight: constant Attenuation negative: %f", *param);
 				return;
 			}
@@ -432,5 +432,5 @@
 			if (*param < 0.0f)
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glLight: linear Attenuation negative: %f", *param);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glLight: linear Attenuation negative: %f", *param);
 				return;
 			}
@@ -441,5 +441,5 @@
 			if (*param < 0.0f)
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glLight: quadratic Attenuation negative: %f", *param);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glLight: quadratic Attenuation negative: %f", *param);
 				return;
 			}
@@ -448,5 +448,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glLight: invalid pname: 0x%x", pname);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glLight: invalid pname: 0x%x", pname);
 			return;
 	}
@@ -455,5 +455,5 @@
 }
 
-void STATE_APIENTRY crStateLightiv (GLenum light, GLenum pname, const GLint *param)
+void STATE_APIENTRY crStateLightiv (PCRStateTracker pState, GLenum light, GLenum pname, const GLint *param)
 {
 	GLfloat f_param;
@@ -470,5 +470,5 @@
 			f_color.b = ((GLfloat)param[2])/CR_MAXINT;
 			f_color.a = ((GLfloat)param[3])/CR_MAXINT;
-			crStateLightfv(light, pname, (GLfloat *) &f_color);
+			crStateLightfv(pState, light, pname, (GLfloat *) &f_color);
 			break;
 		case GL_POSITION:
@@ -478,5 +478,5 @@
 			f_vector.z = (GLfloat) param[2];
 			f_vector.w = (GLfloat) param[3];
-			crStateLightfv(light, pname, (GLfloat *) &f_vector);
+			crStateLightfv(pState, light, pname, (GLfloat *) &f_vector);
 			break;
 		case GL_SPOT_EXPONENT:
@@ -486,28 +486,28 @@
 		case GL_QUADRATIC_ATTENUATION:
 			f_param = (GLfloat) (*param);
-			crStateLightfv(light, pname, &f_param);
+			crStateLightfv(pState, light, pname, &f_param);
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glLight: invalid pname: 0x%x", pname);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glLight: invalid pname: 0x%x", pname);
 			return;
 	}
 }
 
-void STATE_APIENTRY crStateLightf (GLenum light, GLenum pname, GLfloat param)
-{
-	crStateLightfv(light, pname, &param);
-}
-
-void STATE_APIENTRY crStateLighti (GLenum light, GLenum pname, GLint param)
+void STATE_APIENTRY crStateLightf (PCRStateTracker pState, GLenum light, GLenum pname, GLfloat param)
+{
+	crStateLightfv(pState, light, pname, &param);
+}
+
+void STATE_APIENTRY crStateLighti (PCRStateTracker pState, GLenum light, GLenum pname, GLint param)
 {
 	GLfloat f_param = (GLfloat) param;
-	crStateLightfv(light, pname, &f_param);
-}
-
-void STATE_APIENTRY crStateMaterialfv (GLenum face, GLenum pname, const GLfloat *param)
-{
-	CRContext *g = GetCurrentContext();
+	crStateLightfv(pState, light, pname, &f_param);
+}
+
+void STATE_APIENTRY crStateMaterialfv (PCRStateTracker pState, GLenum face, GLenum pname, const GLfloat *param)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRLightingState *l = &(g->lighting);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRLightingBits *lb = &(sb->lighting);
 
@@ -541,5 +541,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
 					return;
 			}
@@ -567,5 +567,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
 					return;
 			}
@@ -593,5 +593,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
 					return;
 			}
@@ -619,5 +619,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
 					return;
 			}
@@ -645,5 +645,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
 					return;
 			}
@@ -652,5 +652,5 @@
 			if (*param > 180.0f || *param < 0.0f)
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glMaterialfv: param out of range: %f", param);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glMaterialfv: param out of range: %f", param);
 				return;
 			}
@@ -668,5 +668,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
 					return;
 			}
@@ -691,10 +691,10 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad face: 0x%x", face);
 					return;
 			}
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad pname: 0x%x", pname);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialfv: bad pname: 0x%x", pname);
 			return;
 	}
@@ -703,5 +703,5 @@
 }
 
-void STATE_APIENTRY crStateMaterialiv (GLenum face, GLenum pname, const GLint *param)
+void STATE_APIENTRY crStateMaterialiv (PCRStateTracker pState, GLenum face, GLenum pname, const GLint *param)
 {
 	GLfloat f_param;
@@ -719,34 +719,34 @@
 			f_color.b = ((GLfloat) param[2]) / ((GLfloat) CR_MAXINT);
 			f_color.a = ((GLfloat) param[3]) / ((GLfloat) CR_MAXINT);
-			crStateMaterialfv(face, pname, (GLfloat *) &f_color);
+			crStateMaterialfv(pState, face, pname, (GLfloat *) &f_color);
 			break;
 		case GL_SHININESS:
 			f_param = (GLfloat) (*param);
-			crStateMaterialfv(face, pname, (GLfloat *) &f_param);
+			crStateMaterialfv(pState, face, pname, (GLfloat *) &f_param);
 			break;
 		case GL_COLOR_INDEXES :
 			f_param = (GLfloat) (*param);
-			crStateMaterialfv(face, pname, (GLfloat *) &f_param);
+			crStateMaterialfv(pState, face, pname, (GLfloat *) &f_param);
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialiv: bad pname: 0x%x", pname);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glMaterialiv: bad pname: 0x%x", pname);
 			return;
 	}
 }
 
-void STATE_APIENTRY crStateMaterialf (GLenum face, GLenum pname, GLfloat param)
-{
-	crStateMaterialfv(face, pname, &param);
-}
-
-void STATE_APIENTRY crStateMateriali (GLenum face, GLenum pname, GLint param)
+void STATE_APIENTRY crStateMaterialf (PCRStateTracker pState, GLenum face, GLenum pname, GLfloat param)
+{
+	crStateMaterialfv(pState, face, pname, &param);
+}
+
+void STATE_APIENTRY crStateMateriali (PCRStateTracker pState, GLenum face, GLenum pname, GLint param)
 {
 	GLfloat f_param = (GLfloat) param;
-	crStateMaterialfv(face, pname, &f_param);
-}
-
-void STATE_APIENTRY crStateGetLightfv (GLenum light, GLenum pname, GLfloat *param)
-{
-	CRContext *g = GetCurrentContext();
+	crStateMaterialfv(pState, face, pname, &f_param);
+}
+
+void STATE_APIENTRY crStateGetLightfv (PCRStateTracker pState, GLenum light, GLenum pname, GLfloat *param)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRLightingState *l = &(g->lighting);
 	CRLight *lt;
@@ -755,5 +755,5 @@
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 				"glGetLightfv called in begin/end");
 		return;
@@ -763,5 +763,5 @@
 	if (i>=g->limits.maxLights)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 				"glGetLight: invalid light specified: 0x%x", light);
 		return;
@@ -823,5 +823,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 					"glGetLight: invalid pname: 0x%x", pname);
 			return;
@@ -829,7 +829,7 @@
 }
 
-void STATE_APIENTRY crStateGetLightiv (GLenum light, GLenum pname, GLint *param)
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetLightiv (PCRStateTracker pState, GLenum light, GLenum pname, GLint *param)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRLightingState *l = &(g->lighting);
 	CRLight *lt;
@@ -838,5 +838,5 @@
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 				"glGetLightiv called in begin/end");
 		return;
@@ -846,5 +846,5 @@
 	if (i>=g->limits.maxLights)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 				"glGetLight: invalid light specified: 0x%x", light);
 		return;
@@ -906,5 +906,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 					"glGetLight: invalid pname: 0x%x", pname);
 			return;
@@ -912,12 +912,12 @@
 }
 
-void STATE_APIENTRY crStateGetMaterialfv (GLenum face, GLenum pname, GLfloat *param)
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetMaterialfv (PCRStateTracker pState, GLenum face, GLenum pname, GLfloat *param)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRLightingState *l = &(g->lighting);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 				"glGetMaterialfv called in begin/end");
 		return;
@@ -942,5 +942,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialfv: bad face: 0x%x", face);
 					return;
@@ -963,5 +963,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialfv: bad face: 0x%x", face);
 					return;
@@ -984,5 +984,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialfv: bad face: 0x%x", face);
 					return;
@@ -1005,5 +1005,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialfv: bad face: 0x%x", face);
 					return;
@@ -1020,5 +1020,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialfv: bad face: 0x%x", face);
 					return;
@@ -1039,5 +1039,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialfv: bad face: 0x%x", face);
 					return;
@@ -1045,5 +1045,5 @@
 			return;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 					"glGetMaterialfv: bad pname: 0x%x", pname);
 			return;
@@ -1052,12 +1052,12 @@
 
 
-void STATE_APIENTRY crStateGetMaterialiv (GLenum face, GLenum pname, GLint *param)
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetMaterialiv (PCRStateTracker pState, GLenum face, GLenum pname, GLint *param)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRLightingState *l = &(g->lighting);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 				"glGetMaterialiv called in begin/end");
 		return;
@@ -1082,5 +1082,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialiv: bad face: 0x%x", face);
 					return;
@@ -1103,5 +1103,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialiv: bad face: 0x%x", face);
 					return;
@@ -1124,5 +1124,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialiv: bad face: 0x%x", face);
 					return;
@@ -1145,5 +1145,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialiv: bad face: 0x%x", face);
 					return;
@@ -1159,5 +1159,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialiv: bad face: 0x%x", face);
 					return;
@@ -1178,5 +1178,5 @@
 					break;
 				default:
-					crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+					crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 							"glGetMaterialiv: bad face: 0x%x", face);
 					return;
@@ -1184,5 +1184,5 @@
 			return;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 					"glGetMaterialiv: bad pname: 0x%x", pname);
 			return;
@@ -1190,7 +1190,7 @@
 }
 
-void crStateColorMaterialRecover(void)
-{
-	CRContext *g = GetCurrentContext();
+void crStateColorMaterialRecover(PCRStateTracker pState)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRLightingState *l = &(g->lighting);
 	CRCurrentState *c = &(g->current);
@@ -1206,7 +1206,7 @@
 		 * short time.  Without this, kirchner_colormaterial fails.
 		 */
-		crStateFlushFunc(NULL);
-
-		crStateMaterialfv(l->colorMaterialFace, l->colorMaterialMode, &(c->vertexAttrib[VERT_ATTRIB_COLOR0][0]));
-	}
-}
+		crStateFlushFunc(pState, NULL);
+
+		crStateMaterialfv(pState, l->colorMaterialFace, l->colorMaterialMode, &(c->vertexAttrib[VERT_ATTRIB_COLOR0][0]));
+	}
+}
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_lighting.txt
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_lighting.txt	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_lighting.txt	(revision 78375)
@@ -9,7 +9,7 @@
 :shadeModel:shadeModel:ShadeModel
 :lightModel:lightModelAmbient|r,g,b,a:LightModelfv,GL_LIGHT_MODEL_AMBIENT
-:lightModel:lightModelLocalViewer:*diff_api.LightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, to->lightModelLocalViewer==GL_TRUE);
-:lightModel:lightModelTwoSide:*diff_api.LightModeli(GL_LIGHT_MODEL_TWO_SIDE, to->lightModelTwoSide==GL_TRUE);
-:lightModel:lightModelColorControlEXT:*diff_api.LightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, to->lightModelColorControlEXT);
+:lightModel:lightModelLocalViewer:*pState->diff_api.LightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, to->lightModelLocalViewer==GL_TRUE);
+:lightModel:lightModelTwoSide:*pState->diff_api.LightModeli(GL_LIGHT_MODEL_TWO_SIDE, to->lightModelTwoSide==GL_TRUE);
+:lightModel:lightModelColorControlEXT:*pState->diff_api.LightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, to->lightModelColorControlEXT);
 %flush
 :material:ambient[0]|r,g,b,a:Materialfv, GL_FRONT, GL_AMBIENT
@@ -41,18 +41,18 @@
 :attenuation:linearAttenuation:Lightf, GL_LIGHT0+i, GL_LINEAR_ATTENUATION
 :attenuation:quadraticAttenuation:Lightf, GL_LIGHT0+i, GL_QUADRATIC_ATTENUATION
-:position:*diff_api.MatrixMode(GL_MODELVIEW);
-:position:*diff_api.PushMatrix();
-:position:*diff_api.LoadIdentity();
+:position:*pState->diff_api.MatrixMode(GL_MODELVIEW);
+:position:*pState->diff_api.PushMatrix();
+:position:*pState->diff_api.LoadIdentity();
 :position:position|x,y,z,w:Lightfv, GL_LIGHT0+i, GL_POSITION
-:position:*diff_api.PopMatrix();
-:position:*diff_api.MatrixMode(toCtx->transform.matrixMode);
-:spot:*diff_api.MatrixMode(GL_MODELVIEW);
-:spot:*diff_api.PushMatrix();
-:spot:*diff_api.LoadIdentity();
+:position:*pState->diff_api.PopMatrix();
+:position:*pState->diff_api.MatrixMode(toCtx->transform.matrixMode);
+:spot:*pState->diff_api.MatrixMode(GL_MODELVIEW);
+:spot:*pState->diff_api.PushMatrix();
+:spot:*pState->diff_api.LoadIdentity();
 :spot:spotDirection|x,y,z:Lightfv, GL_LIGHT0+i, GL_SPOT_DIRECTION
 :spot:spotExponent:Lightf, GL_LIGHT0+i, GL_SPOT_EXPONENT
 :spot:spotCutoff:Lightf, GL_LIGHT0+i, GL_SPOT_CUTOFF
-:spot:*diff_api.PopMatrix();
-:spot:*diff_api.MatrixMode(toCtx->transform.matrixMode);
+:spot:*pState->diff_api.PopMatrix();
+:spot:*pState->diff_api.MatrixMode(toCtx->transform.matrixMode);
 %flush
 >CLEARDIRTY(lb->dirty, nbitID);
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_line.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_line.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_line.c	(revision 78375)
@@ -13,5 +13,5 @@
 {
 	CRLineState *l = &ctx->line;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
 	CRLineBits *lb = &(sb->line);
 
@@ -35,14 +35,14 @@
 }
 
-void STATE_APIENTRY crStateLineWidth(GLfloat width) 
+void STATE_APIENTRY crStateLineWidth(PCRStateTracker pState, GLfloat width) 
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRLineState *l = &(g->line);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRLineBits *lb = &(sb->line);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glLineWidth called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glLineWidth called in begin/end");
 		return;
 	}
@@ -52,5 +52,5 @@
 	if (width <= 0.0f) 
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glLineWidth called with size <= 0.0: %f", width);
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glLineWidth called with size <= 0.0: %f", width);
 		return;
 	}
@@ -61,14 +61,14 @@
 }
 
-void STATE_APIENTRY crStateLineStipple(GLint factor, GLushort pattern) 
+void STATE_APIENTRY crStateLineStipple(PCRStateTracker pState, GLint factor, GLushort pattern) 
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRLineState *l = &(g->line);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRLineBits *lb = &(sb->line);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 			"glLineStipple called in begin/end");
 		return;
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_lists.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_lists.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_lists.c	(revision 78375)
@@ -20,5 +20,5 @@
 {
     CRListsState *l = &ctx->lists;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
     CRListsBits *lb = &(sb->lists);
 
@@ -57,10 +57,10 @@
 #endif
 
-#define CRSTATE_SET_ENABLED(state, cap) CRSTATE_SET_CAP(state, diff_api.IsEnabled(cap), "%u")
-
-#define CRSTATE_SET_ENUM(state, cap) {GLenum _e=g->state; diff_api.GetIntegerv(cap, (GLint *)&_e); CRSTATE_SET_CAP(state, _e, "%#x");}
-#define CRSTATE_SET_FLOAT(state, cap) {GLfloat _f=g->state; diff_api.GetFloatv(cap, &_f); CRSTATE_SET_CAP(state, _f, "%f");}
-#define CRSTATE_SET_INT(state, cap) {GLint _i=g->state; diff_api.GetIntegerv(cap, &_i); CRSTATE_SET_CAP(state, _i, "%i");}
-#define CRSTATE_SET_BOOL(state, cap) {GLboolean _b=g->state; diff_api.GetBooleanv(cap, &_b); CRSTATE_SET_CAP(state, _b, "%u");}
+#define CRSTATE_SET_ENABLED(state, cap) CRSTATE_SET_CAP(state, pState->diff_api.IsEnabled(cap), "%u")
+
+#define CRSTATE_SET_ENUM(state, cap) {GLenum _e=g->state; pState->diff_api.GetIntegerv(cap, (GLint *)&_e); CRSTATE_SET_CAP(state, _e, "%#x");}
+#define CRSTATE_SET_FLOAT(state, cap) {GLfloat _f=g->state; pState->diff_api.GetFloatv(cap, &_f); CRSTATE_SET_CAP(state, _f, "%f");}
+#define CRSTATE_SET_INT(state, cap) {GLint _i=g->state; pState->diff_api.GetIntegerv(cap, &_i); CRSTATE_SET_CAP(state, _i, "%i");}
+#define CRSTATE_SET_BOOL(state, cap) {GLboolean _b=g->state; pState->diff_api.GetBooleanv(cap, &_b); CRSTATE_SET_CAP(state, _b, "%u");}
 
 #define CRSTATE_SET_COLORF(state, cap)              \
@@ -71,5 +71,5 @@
         value[2]=g->state.b;                        \
         value[3]=g->state.a;                        \
-        diff_api.GetFloatv(cap, &value[0]);         \
+        pState->diff_api.GetFloatv(cap, &value[0]); \
         CRSTATE_SET_CAP(state.r, value[0], "%f");   \
         CRSTATE_SET_CAP(state.g, value[1], "%f");   \
@@ -81,10 +81,10 @@
     {                                                                                       \
         GLint _stex, _hwtex;                                                                \
-        _stex = _hwtex = crStateGetTextureObjHWID(g->state);                                \
-        diff_api.GetIntegerv(cap, &_hwtex);                                                 \
+        _stex = _hwtex = crStateGetTextureObjHWID(pState, g->state);                        \
+        pState->diff_api.GetIntegerv(cap, &_hwtex);                                         \
         if (_stex!=_hwtex)                                                                  \
         {                                                                                   \
             CR_STATE_SETTEX_MSG(#state, _stex, _hwtex);                                     \
-            crStateBindTexture(target, crStateTextureHWIDtoID(_hwtex));                     \
+            crStateBindTexture(pState, target, crStateTextureHWIDtoID(pState, _hwtex));     \
         }                                                                                   \
     }
@@ -97,5 +97,5 @@
         value[2]=g->state.b;                        \
         value[3]=g->state.a;                        \
-        diff_api.func(p1, p2, &value[0]);           \
+        pState->diff_api.func(p1, p2, &value[0]);   \
         CRSTATE_SET_CAP(state.r, value[0], "%f");   \
         CRSTATE_SET_CAP(state.g, value[1], "%f");   \
@@ -111,5 +111,5 @@
         value[2]=g->state.z;                        \
         value[3]=g->state.w;                        \
-        diff_api.func(p1, p2, &value[0]);           \
+        pState->diff_api.func(p1, p2, &value[0]);   \
         CRSTATE_SET_CAP(state.x, value[0], "%f");   \
         CRSTATE_SET_CAP(state.y, value[1], "%f");   \
@@ -119,15 +119,15 @@
 
 #define CRSTATE_SET_TEXGEN_4F(state, coord, pname) _CRSTATE_SET_4F_XYZW(state, coord, pname, GetTexGenfv)
-#define CRSTATE_SET_TEXGEN_I(state, coord, pname) {GLint _i=g->state; diff_api.GetTexGeniv(coord, pname, &_i); CRSTATE_SET_CAP(state, _i, "%i");}
-
-#define CRSTATE_SET_TEXENV_I(state, target, pname) {GLint _i=g->state; diff_api.GetTexEnviv(target, pname, &_i); CRSTATE_SET_CAP(state, _i, "%i");}
-#define CRSTATE_SET_TEXENV_F(state, target, pname) {GLfloat _f=g->state; diff_api.GetTexEnvfv(target, pname, &_f); CRSTATE_SET_CAP(state, _f, "%f");}
+#define CRSTATE_SET_TEXGEN_I(state, coord, pname) {GLint _i=g->state; pState->diff_api.GetTexGeniv(coord, pname, &_i); CRSTATE_SET_CAP(state, _i, "%i");}
+
+#define CRSTATE_SET_TEXENV_I(state, target, pname) {GLint _i=g->state; pState->diff_api.GetTexEnviv(target, pname, &_i); CRSTATE_SET_CAP(state, _i, "%i");}
+#define CRSTATE_SET_TEXENV_F(state, target, pname) {GLfloat _f=g->state; pState->diff_api.GetTexEnvfv(target, pname, &_f); CRSTATE_SET_CAP(state, _f, "%f");}
 #define CRSTATE_SET_TEXENV_COLOR(state, target, pname) _CRSTATE_SET_4F_RGBA(state, target, pname, GetTexEnvfv)
 
 #define CRSTATE_SET_MATERIAL_COLOR(state, face, pname) _CRSTATE_SET_4F_RGBA(state, face, pname, GetMaterialfv)
-#define CRSTATE_SET_MATERIAL_F(state, face, pname) {GLfloat _f=g->state; diff_api.GetMaterialfv(face, pname, &_f); CRSTATE_SET_CAP(state, _f, "%f");}
+#define CRSTATE_SET_MATERIAL_F(state, face, pname) {GLfloat _f=g->state; pState->diff_api.GetMaterialfv(face, pname, &_f); CRSTATE_SET_CAP(state, _f, "%f");}
 
 #define CRSTATE_SET_LIGHT_COLOR(state, light, pname) _CRSTATE_SET_4F_RGBA(state, light, pname, GetLightfv)
-#define CRSTATE_SET_LIGHT_F(state, light, pname) {GLfloat _f=g->state; diff_api.GetLightfv(light, pname, &_f); CRSTATE_SET_CAP(state, _f, "%f");}
+#define CRSTATE_SET_LIGHT_F(state, light, pname) {GLfloat _f=g->state; pState->diff_api.GetLightfv(light, pname, &_f); CRSTATE_SET_CAP(state, _f, "%f");}
 #define CRSTATE_SET_LIGHT_4F(state, light, pname) _CRSTATE_SET_4F_XYZW(state, light, pname, GetLightfv)
 #define CRSTATE_SET_LIGHT_3F(state, light, pname)       \
@@ -137,5 +137,5 @@
         value[1]=g->state.y;                            \
         value[2]=g->state.z;                            \
-        diff_api.GetLightfv(light, pname, &value[0]);   \
+        pState->diff_api.GetLightfv(light, pname, &value[0]); \
         CRSTATE_SET_CAP(state.x, value[0], "%f");       \
         CRSTATE_SET_CAP(state.y, value[1], "%f");       \
@@ -150,5 +150,5 @@
         value[2]=g->state.z;                        \
         value[3]=g->state.w;                        \
-        diff_api.GetClipPlane(plane, &value[0]);    \
+        pState->diff_api.GetClipPlane(plane, &value[0]); \
         CRSTATE_SET_CAP(state.x, value[0], "%G");   \
         CRSTATE_SET_CAP(state.y, value[1], "%G");   \
@@ -162,5 +162,5 @@
         crMatrixGetFloats(&f[0], g->state);                 \
         crMemcpy(&sm[0], &f[0], 16*sizeof(GLfloat));        \
-        diff_api.GetFloatv(cap, &f[0]);                     \
+        pState->diff_api.GetFloatv(cap, &f[0]);             \
         if (crMemcmp(&f[0], &sm[0], 16*sizeof(GLfloat)))    \
         {                                                   \
@@ -170,11 +170,11 @@
     }
 
-void STATE_APIENTRY crStateQueryHWState(GLuint fbFbo, GLuint bbFbo)
+void STATE_APIENTRY crStateQueryHWState(PCRStateTracker pState, GLuint fbFbo, GLuint bbFbo)
 {
-    CRContext *g = GetCurrentContext();
-    CRStateBits *sb = GetCurrentBits();
+    CRContext *g = GetCurrentContext(pState);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRbitvalue /* *bitID=g->bitid, */ *negbitID=g->neg_bitid;
 
-    CRASSERT(g_bVBoxEnableDiffOnMakeCurrent);
+    CRASSERT(pState->fVBoxEnableDiffOnMakeCurrent);
 
     crStateSyncHWErrorState(g);
@@ -224,10 +224,10 @@
         {
             GLuint buf = 0;
-            diff_api.GetIntegerv(GL_DRAW_BUFFER, (GLint *)&buf);
+            pState->diff_api.GetIntegerv(GL_DRAW_BUFFER, (GLint *)&buf);
 
             if (buf == GL_COLOR_ATTACHMENT0_EXT && (bbFbo || fbFbo))
             {
                 GLuint binding = 0;
-                diff_api.GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, (GLint *)&binding);
+                pState->diff_api.GetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, (GLint *)&binding);
                 if (!binding)
                 {
@@ -257,10 +257,10 @@
         {
             GLuint buf = 0;
-            diff_api.GetIntegerv(GL_READ_BUFFER, (GLint *)&buf);
+            pState->diff_api.GetIntegerv(GL_READ_BUFFER, (GLint *)&buf);
 
             if (buf == GL_COLOR_ATTACHMENT0_EXT && (bbFbo || fbFbo))
             {
                 GLuint binding = 0;
-                diff_api.GetIntegerv(GL_READ_FRAMEBUFFER_BINDING, (GLint *)&binding);
+                pState->diff_api.GetIntegerv(GL_READ_FRAMEBUFFER_BINDING, (GLint *)&binding);
                 if (!binding)
                 {
@@ -299,5 +299,5 @@
             value[2]=g->buffer.colorWriteMask.b;
             value[3]=g->buffer.colorWriteMask.a;
-            diff_api.GetBooleanv(GL_COLOR_WRITEMASK, &value[0]);
+            pState->diff_api.GetBooleanv(GL_COLOR_WRITEMASK, &value[0]);
 
             CRSTATE_SET_CAP(buffer.colorWriteMask.r, value[0], "%u");
@@ -397,5 +397,5 @@
             if (activeFace == GL_BACK)
             {
-                diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                 activeFace = GL_FRONT;
             }
@@ -409,5 +409,5 @@
             if (activeFace == GL_BACK)
             {
-                diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                 activeFace = GL_FRONT;
             }
@@ -420,5 +420,5 @@
             if (activeFace == GL_BACK)
             {
-                diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                 activeFace = GL_FRONT;
             }
@@ -440,5 +440,5 @@
             if (activeFace == GL_BACK)
             {
-                diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                 activeFace = GL_FRONT;
             }
@@ -452,5 +452,5 @@
             if (activeFace == GL_BACK)
             {
-                diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                 activeFace = GL_FRONT;
             }
@@ -463,5 +463,5 @@
             if (activeFace == GL_BACK)
             {
-                diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                 activeFace = GL_FRONT;
             }
@@ -496,5 +496,5 @@
                 if (i!=activeUnit)
                 {
-                    diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
+                    pState->diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
                     activeUnit=i;
                 }
@@ -527,5 +527,5 @@
                 if (i!=activeUnit)
                 {
-                    diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
+                    pState->diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
                     activeUnit=i;
                 }
@@ -554,5 +554,5 @@
                 if (i!=activeUnit)
                 {
-                    diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
+                    pState->diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
                     activeUnit=i;
                 }
@@ -568,5 +568,5 @@
                 if (i!=activeUnit)
                 {
-                    diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
+                    pState->diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
                     activeUnit=i;
                 }
@@ -582,5 +582,5 @@
                 if (i!=activeUnit)
                 {
-                    diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
+                    pState->diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
                     activeUnit=i;
                 }
@@ -596,5 +596,5 @@
                 if (i!=activeUnit)
                 {
-                    diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
+                    pState->diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
                     activeUnit=i;
                 }
@@ -622,5 +622,5 @@
         if (activeUnit!=g->texture.curTextureUnit)
         {
-            diff_api.ActiveTextureARB(g->texture.curTextureUnit + GL_TEXTURE0_ARB);
+            pState->diff_api.ActiveTextureARB(g->texture.curTextureUnit + GL_TEXTURE0_ARB);
         }
     }
@@ -754,8 +754,8 @@
             for (i=0; i<g->limits.maxTextureUnits; i++)
             {
-                diff_api.ActiveTextureARB(GL_TEXTURE0_ARB+i);
+                pState->diff_api.ActiveTextureARB(GL_TEXTURE0_ARB+i);
                 CRSTATE_SET_MATRIX(transform.textureStack[i].top, GL_TEXTURE_MATRIX);
             }
-            diff_api.ActiveTextureARB(g->texture.curTextureUnit + GL_TEXTURE0_ARB);
+            pState->diff_api.ActiveTextureARB(g->texture.curTextureUnit + GL_TEXTURE0_ARB);
         }
 
@@ -785,5 +785,5 @@
             value[2] = g->viewport.scissorW;
             value[3] = g->viewport.scissorH;
-            diff_api.GetIntegerv(GL_SCISSOR_BOX, &value[0]);
+            pState->diff_api.GetIntegerv(GL_SCISSOR_BOX, &value[0]);
             CRSTATE_SET_CAP(viewport.scissorX, value[0], "%i");
             CRSTATE_SET_CAP(viewport.scissorY, value[1], "%i");
@@ -799,5 +799,5 @@
             value[2] = g->viewport.viewportW;
             value[3] = g->viewport.viewportH;
-            diff_api.GetIntegerv(GL_VIEWPORT, &value[0]);
+            pState->diff_api.GetIntegerv(GL_VIEWPORT, &value[0]);
             CRSTATE_SET_CAP(viewport.viewportX, value[0], "%i");
             CRSTATE_SET_CAP(viewport.viewportY, value[1], "%i");
@@ -811,5 +811,5 @@
             value[0] = g->viewport.nearClip;
             value[1] = g->viewport.farClip;
-            diff_api.GetFloatv(GL_DEPTH_RANGE, &value[0]);
+            pState->diff_api.GetFloatv(GL_DEPTH_RANGE, &value[0]);
             CRSTATE_SET_CAP(viewport.nearClip, value[0], "%f");
             CRSTATE_SET_CAP(viewport.farClip, value[1], "%f");
@@ -847,6 +847,6 @@
                 uval[0] = g->eval.eval1D[i].u1;
                 uval[1] = g->eval.eval1D[i].u2;
-                diff_api.GetMapiv(i + GL_MAP1_COLOR_4, GL_ORDER, &order);
-                diff_api.GetMapfv(i + GL_MAP1_COLOR_4, GL_DOMAIN, &uval[0]);
+                pState->diff_api.GetMapiv(i + GL_MAP1_COLOR_4, GL_ORDER, &order);
+                pState->diff_api.GetMapfv(i + GL_MAP1_COLOR_4, GL_DOMAIN, &uval[0]);
                 if (order>0)
                 {
@@ -857,5 +857,5 @@
                         continue;
                     }
-                    diff_api.GetMapfv(i + GL_MAP1_COLOR_4, GL_COEFF, coeffs);
+                    pState->diff_api.GetMapfv(i + GL_MAP1_COLOR_4, GL_COEFF, coeffs);
                 }
 
@@ -886,6 +886,6 @@
                 uval[2] = g->eval.eval2D[i].v1;
                 uval[3] = g->eval.eval2D[i].v2;
-                diff_api.GetMapiv(i + GL_MAP2_COLOR_4, GL_ORDER, &order[0]);
-                diff_api.GetMapfv(i + GL_MAP2_COLOR_4, GL_DOMAIN, &uval[0]);
+                pState->diff_api.GetMapiv(i + GL_MAP2_COLOR_4, GL_ORDER, &order[0]);
+                pState->diff_api.GetMapfv(i + GL_MAP2_COLOR_4, GL_DOMAIN, &uval[0]);
                 if (order[0]>0 && order[1]>0)
                 {
@@ -896,5 +896,5 @@
                         continue;
                     }
-                    diff_api.GetMapfv(i + GL_MAP1_COLOR_4, GL_COEFF, coeffs);
+                    pState->diff_api.GetMapfv(i + GL_MAP1_COLOR_4, GL_COEFF, coeffs);
                 }
                 CRSTATE_SET_CAP(eval.eval2D[i].uorder, order[0], "%i");
@@ -927,5 +927,5 @@
             value[0] = g->eval.u11D;
             value[1] = g->eval.u21D;
-            diff_api.GetFloatv(GL_MAP1_GRID_DOMAIN, &value[0]);
+            pState->diff_api.GetFloatv(GL_MAP1_GRID_DOMAIN, &value[0]);
             CRSTATE_SET_CAP(eval.u11D, value[0], "%f");
             CRSTATE_SET_CAP(eval.u21D, value[1], "%f");
@@ -938,5 +938,5 @@
             iv[0] = g->eval.un2D;
             iv[1] = g->eval.vn2D;
-            diff_api.GetIntegerv(GL_MAP1_GRID_SEGMENTS, &iv[0]);
+            pState->diff_api.GetIntegerv(GL_MAP1_GRID_SEGMENTS, &iv[0]);
             CRSTATE_SET_CAP(eval.un2D, iv[0], "%i");
             CRSTATE_SET_CAP(eval.vn2D, iv[1], "%i");
@@ -945,5 +945,5 @@
             value[2] = g->eval.v12D;
             value[3] = g->eval.v22D;
-            diff_api.GetFloatv(GL_MAP2_GRID_DOMAIN, &value[0]);
+            pState->diff_api.GetFloatv(GL_MAP2_GRID_DOMAIN, &value[0]);
             CRSTATE_SET_CAP(eval.u12D, value[0], "%f");
             CRSTATE_SET_CAP(eval.u22D, value[1], "%f");
@@ -1122,5 +1122,5 @@
             value[1] = g->point.distanceAttenuation[1];
             value[2] = g->point.distanceAttenuation[2];
-            diff_api.GetFloatv(GL_POINT_DISTANCE_ATTENUATION, &value[0]);
+            pState->diff_api.GetFloatv(GL_POINT_DISTANCE_ATTENUATION, &value[0]);
             CRSTATE_SET_CAP(point.distanceAttenuation[0], value[0], "%f");
             CRSTATE_SET_CAP(point.distanceAttenuation[1], value[1], "%f");
@@ -1143,8 +1143,8 @@
                     if (activeUnit!=i)
                     {
-                        diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
+                        pState->diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB);
                         activeUnit=i;
                     }
-                    diff_api.GetTexEnviv(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, &val);
+                    pState->diff_api.GetTexEnviv(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, &val);
                     CRSTATE_SET_CAP(point.coordReplacement[i], val, "%i");
                 }
@@ -1153,5 +1153,5 @@
             if (activeUnit!=g->texture.curTextureUnit)
             {
-                diff_api.ActiveTextureARB(g->texture.curTextureUnit + GL_TEXTURE0_ARB);
+                pState->diff_api.ActiveTextureARB(g->texture.curTextureUnit + GL_TEXTURE0_ARB);
             }
         }
@@ -1184,5 +1184,5 @@
             val[0] = g->polygon.frontMode;
             val[1] = g->polygon.backMode;
-            diff_api.GetIntegerv(GL_POLYGON_MODE, &val[0]);
+            pState->diff_api.GetIntegerv(GL_POLYGON_MODE, &val[0]);
             CRSTATE_SET_CAP(polygon.frontMode, val[0], "%#x");
             CRSTATE_SET_CAP(polygon.backMode, val[1], "%#x");
@@ -1195,5 +1195,5 @@
             GLint stipple[32];
             crMemcpy(&stipple[0], &g->polygon.stipple[0], sizeof(stipple));
-            diff_api.GetPolygonStipple((GLubyte*) &stipple[0]);
+            pState->diff_api.GetPolygonStipple((GLubyte*) &stipple[0]);
             if (crMemcmp(&stipple[0], &g->polygon.stipple[0], sizeof(stipple)))
             {
@@ -1208,15 +1208,15 @@
     }
 
-    CR_STATE_CLEAN_HW_ERR_WARN("error on hw sync");
+    CR_STATE_CLEAN_HW_ERR_WARN(pState, "error on hw sync");
 }
 
-void STATE_APIENTRY crStateNewList (GLuint list, GLenum mode) 
+void STATE_APIENTRY crStateNewList (PCRStateTracker pState, GLuint list, GLenum mode) 
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRListsState *l = &(g->lists);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glNewList called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glNewList called in Begin/End");
         return;
     }
@@ -1224,5 +1224,5 @@
     if (list == 0)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glNewList(list=0)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glNewList(list=0)");
         return;
     }
@@ -1231,5 +1231,5 @@
     {
         /* already building a list */
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glNewList called inside display list");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glNewList called inside display list");
         return;
     }
@@ -1237,5 +1237,5 @@
     if (mode != GL_COMPILE && mode != GL_COMPILE_AND_EXECUTE)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glNewList invalid mode");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glNewList invalid mode");
         return;
     }
@@ -1256,12 +1256,12 @@
 }
 
-void STATE_APIENTRY crStateEndList (void)
+void STATE_APIENTRY crStateEndList (PCRStateTracker pState)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRListsState *l = &(g->lists);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glEndList called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glEndList called in Begin/End");
         return;
     }
@@ -1269,5 +1269,5 @@
     if (!l->currentIndex)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glEndList called outside display list");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glEndList called outside display list");
         return;
     }
@@ -1277,12 +1277,12 @@
 }
 
-GLuint STATE_APIENTRY crStateGenLists(GLsizei range)
+GLuint STATE_APIENTRY crStateGenLists(PCRStateTracker pState, GLsizei range)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     GLuint start;
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glGenLists called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glGenLists called in Begin/End");
         return 0;
     }
@@ -1290,5 +1290,5 @@
     if (range < 0)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative range passed to glGenLists: %d", range);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative range passed to glGenLists: %d", range);
         return 0;
     }
@@ -1300,11 +1300,11 @@
 }
     
-void STATE_APIENTRY crStateDeleteLists (GLuint list, GLsizei range)
+void STATE_APIENTRY crStateDeleteLists (PCRStateTracker pState, GLuint list, GLsizei range)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDeleteLists called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glDeleteLists called in Begin/End");
         return;
     }
@@ -1312,5 +1312,5 @@
     if (range < 0)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "Negative range passed to glDeleteLists: %d", range);
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Negative range passed to glDeleteLists: %d", range);
         return;
     }
@@ -1319,11 +1319,11 @@
 }
 
-GLboolean STATE_APIENTRY crStateIsList(GLuint list)
+GLboolean STATE_APIENTRY crStateIsList(PCRStateTracker pState, GLuint list)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
             "GenLists called in Begin/End");
         return GL_FALSE;
@@ -1336,14 +1336,14 @@
 }
     
-void STATE_APIENTRY crStateListBase (GLuint base)
+void STATE_APIENTRY crStateListBase (PCRStateTracker pState, GLuint base)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRListsState *l = &(g->lists);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRListsBits *lb = &(sb->lists);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
             "ListBase called in Begin/End");
         return;
@@ -1361,4 +1361,5 @@
                                     CRContext *fromCtx, CRContext *toCtx )
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRListsState *from = &(fromCtx->lists);
     CRListsState *to = &(toCtx->lists);
@@ -1366,4 +1367,6 @@
     CRbitvalue nbitID[CR_MAX_BITARRAY];
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
@@ -1372,5 +1375,5 @@
     {
         if (from->base != to->base) {
-            diff_api.ListBase(to->base);
+            pState->diff_api.ListBase(to->base);
             from->base = to->base;
         }
@@ -1386,4 +1389,5 @@
                                         CRContext *fromCtx, CRContext *toCtx )
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRListsState *from = &(fromCtx->lists);
     CRListsState *to = &(toCtx->lists);
@@ -1391,4 +1395,6 @@
     CRbitvalue nbitID[CR_MAX_BITARRAY];
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
@@ -1397,5 +1403,5 @@
     {
         if (from->base != to->base) {
-            diff_api.ListBase(to->base);
+            pState->diff_api.ListBase(to->base);
             FILLDIRTY(b->base);
             FILLDIRTY(b->dirty);
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_multisample.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_multisample.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_multisample.c	(revision 78375)
@@ -13,5 +13,5 @@
 {
 	CRMultisampleState *m = &ctx->multisample;
-	CRStateBits *sb       = GetCurrentBits();
+	CRStateBits *sb       = GetCurrentBits(ctx->pStateTracker);
 	CRMultisampleBits *mb = &(sb->multisample);
 
@@ -29,14 +29,14 @@
 }
 
-void STATE_APIENTRY crStateSampleCoverageARB(GLclampf value, GLboolean invert)
+void STATE_APIENTRY crStateSampleCoverageARB(PCRStateTracker pState, GLclampf value, GLboolean invert)
 {
-	CRContext *g          = GetCurrentContext();
+	CRContext *g          = GetCurrentContext(pState);
 	CRMultisampleState *m = &(g->multisample);
-	CRStateBits *sb       = GetCurrentBits();
+	CRStateBits *sb       = GetCurrentBits(pState);
 	CRMultisampleBits *mb = &(sb->multisample);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glStateSampleCoverageARB called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glStateSampleCoverageARB called in begin/end");
 		return;
 	}
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_occlude.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_occlude.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_occlude.c	(revision 78375)
@@ -44,7 +44,7 @@
 
 void STATE_APIENTRY
-crStateDeleteQueriesARB(GLsizei n, const GLuint *ids)
-{
-	CRContext *g = GetCurrentContext();
+crStateDeleteQueriesARB(PCRStateTracker pState, GLsizei n, const GLuint *ids)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CROcclusionState *o = &(g->occlusion);
 	/*CRStateBits *sb = GetCurrentBits();*/
@@ -55,5 +55,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glDeleteQueriesARB called in Begin/End");
 		return;
@@ -73,7 +73,7 @@
 
 void STATE_APIENTRY
-crStateGenQueriesARB(GLsizei n, GLuint * queries)
-{
-	CRContext *g = GetCurrentContext();
+crStateGenQueriesARB(PCRStateTracker pState, GLsizei n, GLuint * queries)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CROcclusionState *o = &(g->occlusion);
 	GLint start;
@@ -82,5 +82,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glGenQueriesARB called in Begin/End");
 		return;
@@ -88,5 +88,5 @@
 
 	if (n < 0) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
 								 "glGenQueriesARB(n < 0)");
 		return;
@@ -100,5 +100,5 @@
 	}
 	else {
-		crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glGenQueriesARB");
+		crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY, "glGenQueriesARB");
 	}
 }
@@ -106,13 +106,13 @@
 
 GLboolean STATE_APIENTRY
-crStateIsQueryARB(GLuint id)
-{
-	CRContext *g = GetCurrentContext();
-	CROcclusionState *o = &(g->occlusion);
-
-	FLUSH();
-
-	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+crStateIsQueryARB(PCRStateTracker pState, GLuint id)
+{
+	CRContext *g = GetCurrentContext(pState);
+	CROcclusionState *o = &(g->occlusion);
+
+	FLUSH();
+
+	if (g->current.inBeginEnd) {
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glIsQueryARB called in begin/end");
 		return GL_FALSE;
@@ -127,7 +127,7 @@
 
 void STATE_APIENTRY
-crStateGetQueryivARB(GLenum target, GLenum pname, GLint *params)
-{
-	CRContext *g = GetCurrentContext();
+crStateGetQueryivARB(PCRStateTracker pState, GLenum target, GLenum pname, GLint *params)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CROcclusionState *o = &(g->occlusion);
 	(void)target;
@@ -136,5 +136,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glGetGetQueryivARB called in begin/end");
 		return;
@@ -149,5 +149,5 @@
 		 break;
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 								 "glGetGetQueryivARB(pname)");
 		return;
@@ -157,7 +157,7 @@
 
 void STATE_APIENTRY
-crStateGetQueryObjectivARB(GLuint id, GLenum pname, GLint *params)
-{
-	CRContext *g = GetCurrentContext();
+crStateGetQueryObjectivARB(PCRStateTracker pState, GLuint id, GLenum pname, GLint *params)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CROcclusionState *o = &(g->occlusion);
 	CROcclusionObject *q;
@@ -166,5 +166,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glGetGetQueryObjectivARB called in begin/end");
 		return;
@@ -173,5 +173,5 @@
 	q = (CROcclusionObject *) crHashtableSearch(o->objects, id);
 	if (!q || q->active) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								"glGetQueryObjectivARB");
 		return;
@@ -186,5 +186,5 @@
 		break;
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 								 "glGetQueryObjectivARB(pname)");
 		return;
@@ -194,7 +194,7 @@
 
 void STATE_APIENTRY
-crStateGetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params)
-{
-	CRContext *g = GetCurrentContext();
+crStateGetQueryObjectuivARB(PCRStateTracker pState, GLuint id, GLenum pname, GLuint *params)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CROcclusionState *o = &(g->occlusion);
 	CROcclusionObject *q;
@@ -203,5 +203,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glGetGetQueryObjectuivARB called in begin/end");
 		return;
@@ -210,5 +210,5 @@
 	q = (CROcclusionObject *) crHashtableSearch(o->objects, id);
 	if (!q || q->active) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								"glGetQueryObjectuivARB");
 		return;
@@ -224,5 +224,5 @@
 		break;
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 								 "glGetQueryObjectuivARB(pname)");
 		return;
@@ -232,7 +232,7 @@
 
 void STATE_APIENTRY
-crStateBeginQueryARB(GLenum target, GLuint id)
-{
-	CRContext *g = GetCurrentContext();
+crStateBeginQueryARB(PCRStateTracker pState, GLenum target, GLuint id)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CROcclusionState *o = &(g->occlusion);
 	CROcclusionObject *q;
@@ -241,5 +241,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glGetGetQueryObjectuivARB called in begin/end");
 		return;
@@ -247,5 +247,5 @@
 
 	if (target != GL_SAMPLES_PASSED_ARB) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 								 "glBeginQueryARB(target)");
 		return;
@@ -253,5 +253,5 @@
 
 	if (o->currentQueryObject) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glBeginQueryARB(target)");
 		return;
@@ -260,5 +260,5 @@
 	q = (CROcclusionObject *) crHashtableSearch(o->objects, id);
 	if (q && q->active) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glBeginQueryARB");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glBeginQueryARB");
 		return;
 	}
@@ -266,5 +266,5 @@
 		q = NewQueryObject(target, id);
 		if (!q) {
-			crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glBeginQueryARB");
+			crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY, "glBeginQueryARB");
 			return;
 		}
@@ -281,7 +281,7 @@
 
 void STATE_APIENTRY
-crStateEndQueryARB(GLenum target)
-{
-	CRContext *g = GetCurrentContext();
+crStateEndQueryARB(PCRStateTracker pState, GLenum target)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CROcclusionState *o = &(g->occlusion);
 	CROcclusionObject *q;
@@ -290,5 +290,5 @@
 
 	if (g->current.inBeginEnd) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glGetGetQueryObjectuivARB called in begin/end");
 		return;
@@ -296,5 +296,5 @@
 
 	if (target != GL_SAMPLES_PASSED_ARB) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glEndQueryARB(target)");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glEndQueryARB(target)");
 		return;
 	}
@@ -302,5 +302,5 @@
 	q = (CROcclusionObject *) crHashtableSearch(o->objects, o->currentQueryObject);
 	if (!q || !q->active) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 								 "glEndQueryARB with glBeginQueryARB");
 		return;
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_pixel.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_pixel.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_pixel.c	(revision 78375)
@@ -14,5 +14,5 @@
 {
     CRPixelState *p = &ctx->pixel;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
     CRPixelBits *pb = &(sb->pixel);
     GLcolorf zero_color = {0.0f, 0.0f, 0.0f, 0.0f};
@@ -58,19 +58,19 @@
 }
 
-void STATE_APIENTRY crStatePixelTransferi (GLenum pname, GLint param)
-{
-    crStatePixelTransferf( pname, (GLfloat) param );
-}
-
-void STATE_APIENTRY crStatePixelTransferf (GLenum pname, GLfloat param)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStatePixelTransferi (PCRStateTracker pState, GLenum pname, GLint param)
+{
+    crStatePixelTransferf(pState, pname, (GLfloat) param );
+}
+
+void STATE_APIENTRY crStatePixelTransferf (PCRStateTracker pState, GLenum pname, GLfloat param)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRPixelState *p = &(g->pixel);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRPixelBits *pb = &(sb->pixel);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "PixelTransfer{if} called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "PixelTransfer{if} called in Begin/End");
         return;
     }
@@ -123,5 +123,5 @@
             break;
         default:
-            crStateError( __LINE__, __FILE__, GL_INVALID_VALUE, "Unknown glPixelTransfer pname: %d", pname );
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "Unknown glPixelTransfer pname: %d", pname );
             return;
     }
@@ -130,14 +130,14 @@
 }
 
-void STATE_APIENTRY crStatePixelZoom (GLfloat xfactor, GLfloat yfactor) 
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStatePixelZoom (PCRStateTracker pState, GLfloat xfactor, GLfloat yfactor) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRPixelState *p = &(g->pixel);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRPixelBits *pb = &(sb->pixel);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "PixelZoom called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "PixelZoom called in Begin/End");
         return;
     }
@@ -152,11 +152,11 @@
 
 
-void STATE_APIENTRY crStateBitmap( GLsizei width, GLsizei height, 
+void STATE_APIENTRY crStateBitmap(PCRStateTracker pState, GLsizei width, GLsizei height, 
         GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, 
         const GLubyte *bitmap)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRCurrentState *c = &(g->current);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRCurrentBits *cb = &(sb->current);
 
@@ -170,5 +170,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, 
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, 
             "Bitmap called in begin/end");
         return;
@@ -177,5 +177,5 @@
     if (width < 0 || height < 0)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
             "Bitmap called with neg dims: %dx%d", width, height);
         return;
@@ -201,15 +201,15 @@
 #define CLAMP(x, min, max)  ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
 
-void STATE_APIENTRY crStatePixelMapfv (GLenum map, GLint mapsize, const GLfloat * values)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStatePixelMapfv (PCRStateTracker pState, GLenum map, GLint mapsize, const GLfloat * values)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRPixelState *p = &(g->pixel);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRPixelBits *pb = &(sb->pixel);
     GLint i;
-    GLboolean unpackbuffer = crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB);
+    GLboolean unpackbuffer = crStateIsBufferBound(pState, GL_PIXEL_UNPACK_BUFFER_ARB);
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "PixelMap called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "PixelMap called in Begin/End");
         return;
     }
@@ -218,5 +218,5 @@
 
     if (mapsize < 0 || mapsize > CR_MAX_PIXEL_MAP_TABLE) {
-       crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "PixelMap(mapsize)");
+       crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "PixelMap(mapsize)");
        return;
     }
@@ -302,5 +302,5 @@
         break;
     default:
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "PixelMap(map)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "PixelMap(map)");
         return;
     }
@@ -310,5 +310,5 @@
 }
 
-void STATE_APIENTRY crStatePixelMapuiv (GLenum map, GLint mapsize, const GLuint * values)
+void STATE_APIENTRY crStatePixelMapuiv (PCRStateTracker pState, GLenum map, GLint mapsize, const GLuint * values)
 {
     if (mapsize < 0 || mapsize > CR_MAX_PIXEL_MAP_TABLE)
@@ -318,5 +318,5 @@
     }
 
-    if (!crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (!crStateIsBufferBound(pState, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         GLfloat fvalues[CR_MAX_PIXEL_MAP_TABLE];
@@ -333,13 +333,13 @@
            }
         }
-        crStatePixelMapfv(map, mapsize, fvalues);
+        crStatePixelMapfv(pState, map, mapsize, fvalues);
     }
     else
     {
-        crStatePixelMapfv(map, mapsize, (const GLfloat*) values);
+        crStatePixelMapfv(pState, map, mapsize, (const GLfloat*) values);
     }
 }
  
-void STATE_APIENTRY crStatePixelMapusv (GLenum map, GLint mapsize, const GLushort * values)
+void STATE_APIENTRY crStatePixelMapusv (PCRStateTracker pState, GLenum map, GLint mapsize, const GLushort * values)
 {
     if (mapsize < 0 || mapsize > CR_MAX_PIXEL_MAP_TABLE)
@@ -349,5 +349,5 @@
     }
 
-    if (!crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (!crStateIsBufferBound(pState, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         GLfloat fvalues[CR_MAX_PIXEL_MAP_TABLE];
@@ -364,21 +364,21 @@
            }
         }
-        crStatePixelMapfv(map, mapsize, fvalues);
+        crStatePixelMapfv(pState, map, mapsize, fvalues);
     }
     else
     {
-        crStatePixelMapfv(map, mapsize, (const GLfloat*) values);
+        crStatePixelMapfv(pState, map, mapsize, (const GLfloat*) values);
     }
 }
 
  
-void STATE_APIENTRY crStateGetPixelMapfv (GLenum map, GLfloat * values)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetPixelMapfv (PCRStateTracker pState, GLenum map, GLfloat * values)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRPixelState *p = &(g->pixel);
     GLint i;
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                      "GetPixelMapfv called in Begin/End");
         return;
@@ -421,12 +421,12 @@
         break;
     default:
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "GetPixelMap(map)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "GetPixelMap(map)");
         return;
     }
 }
  
-void STATE_APIENTRY crStateGetPixelMapuiv (GLenum map, GLuint * values)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetPixelMapuiv (PCRStateTracker pState, GLenum map, GLuint * values)
+{
+    CRContext *g = GetCurrentContext(pState);
     const GLfloat maxUint = 4294967295.0F;
     CRPixelState *p = &(g->pixel);
@@ -434,5 +434,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                      "GetPixelMapuiv called in Begin/End");
         return;
@@ -491,12 +491,12 @@
         break;
     default:
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "GetPixelMapuiv(map)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "GetPixelMapuiv(map)");
         return;
     }
 }
  
-void STATE_APIENTRY crStateGetPixelMapusv (GLenum map, GLushort * values)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetPixelMapusv (PCRStateTracker pState, GLenum map, GLushort * values)
+{
+    CRContext *g = GetCurrentContext(pState);
     const GLfloat maxUshort = 65535.0F;
     CRPixelState *p = &(g->pixel);
@@ -504,5 +504,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                      "GetPixelMapusv called in Begin/End");
         return;
@@ -561,5 +561,5 @@
         break;
     default:
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "GetPixelMapusv(map)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "GetPixelMapusv(map)");
         return;
     }
@@ -569,8 +569,12 @@
                       CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRPixelState *from = &(fromCtx->pixel);
     CRPixelState *to = &(toCtx->pixel);
     int j, i;
     CRbitvalue nbitID[CR_MAX_BITARRAY];
+
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
@@ -580,70 +584,70 @@
         if (from->mapColor != to->mapColor)
         {           
-            diff_api.PixelTransferi (GL_MAP_COLOR, to->mapColor);
+            pState->diff_api.PixelTransferi (GL_MAP_COLOR, to->mapColor);
             from->mapColor = to->mapColor;
         }
         if (from->mapStencil != to->mapStencil)
         {
-            diff_api.PixelTransferi (GL_MAP_STENCIL, to->mapStencil);
+            pState->diff_api.PixelTransferi (GL_MAP_STENCIL, to->mapStencil);
             from->mapStencil = to->mapStencil;
         }
         if (from->indexOffset != to->indexOffset)
         {
-            diff_api.PixelTransferi (GL_INDEX_OFFSET, to->indexOffset);
+            pState->diff_api.PixelTransferi (GL_INDEX_OFFSET, to->indexOffset);
             from->indexOffset = to->indexOffset;
         }
         if (from->indexShift != to->indexShift)
         {
-            diff_api.PixelTransferi (GL_INDEX_SHIFT, to->indexShift);
+            pState->diff_api.PixelTransferi (GL_INDEX_SHIFT, to->indexShift);
             from->indexShift = to->indexShift;
         }
         if (from->scale.r != to->scale.r)
         {
-            diff_api.PixelTransferf (GL_RED_SCALE, to->scale.r);
+            pState->diff_api.PixelTransferf (GL_RED_SCALE, to->scale.r);
             from->scale.r = to->scale.r;
         }
         if (from->scale.g != to->scale.g)
         {
-            diff_api.PixelTransferf (GL_GREEN_SCALE, to->scale.g);
+            pState->diff_api.PixelTransferf (GL_GREEN_SCALE, to->scale.g);
             from->scale.g = to->scale.g;
         }
         if (from->scale.b != to->scale.b)
         {
-            diff_api.PixelTransferf (GL_BLUE_SCALE, to->scale.b);
+            pState->diff_api.PixelTransferf (GL_BLUE_SCALE, to->scale.b);
             from->scale.b = to->scale.b;
         }
         if (from->scale.a != to->scale.a)
         {
-            diff_api.PixelTransferf (GL_ALPHA_SCALE, to->scale.a);
+            pState->diff_api.PixelTransferf (GL_ALPHA_SCALE, to->scale.a);
             from->scale.a = to->scale.a;
         }
         if (from->bias.r != to->bias.r)
         {
-            diff_api.PixelTransferf (GL_RED_BIAS, to->bias.r);
+            pState->diff_api.PixelTransferf (GL_RED_BIAS, to->bias.r);
             from->bias.r = to->bias.r;
         }
         if (from->bias.g != to->bias.g)
         {
-            diff_api.PixelTransferf (GL_GREEN_BIAS, to->bias.g);
+            pState->diff_api.PixelTransferf (GL_GREEN_BIAS, to->bias.g);
             from->bias.g = to->bias.g;
         }
         if (from->bias.b != to->bias.b)
         {
-            diff_api.PixelTransferf (GL_BLUE_BIAS, to->bias.b);
+            pState->diff_api.PixelTransferf (GL_BLUE_BIAS, to->bias.b);
             from->bias.b = to->bias.b;
         }
         if (from->bias.a != to->bias.a)
         {
-            diff_api.PixelTransferf (GL_ALPHA_BIAS, to->bias.a);
+            pState->diff_api.PixelTransferf (GL_ALPHA_BIAS, to->bias.a);
             from->bias.a = to->bias.a;
         }
         if (from->depthScale != to->depthScale)
         {
-            diff_api.PixelTransferf (GL_DEPTH_SCALE, to->depthScale);
+            pState->diff_api.PixelTransferf (GL_DEPTH_SCALE, to->depthScale);
             from->depthScale = to->depthScale;
         }
         if (from->depthBias != to->depthBias)
         {
-            diff_api.PixelTransferf (GL_DEPTH_BIAS, to->depthBias);
+            pState->diff_api.PixelTransferf (GL_DEPTH_BIAS, to->depthBias);
             from->depthBias = to->depthBias;
         }
@@ -655,6 +659,5 @@
             from->yZoom != to->yZoom)
         {
-            diff_api.PixelZoom (to->xZoom,
-                to->yZoom);
+            pState->diff_api.PixelZoom (to->xZoom, to->yZoom);
             from->xZoom = to->xZoom;
             from->yZoom = to->yZoom;
@@ -665,23 +668,23 @@
     {
         if (crMemcmp(to->mapStoS, from->mapStoS, CR_MAX_PIXEL_MAP_TABLE*sizeof(GLfloat)))
-            diff_api.PixelMapfv(GL_PIXEL_MAP_S_TO_S,to->mapStoSsize,(GLfloat*)to->mapStoS);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_S_TO_S,to->mapStoSsize,(GLfloat*)to->mapStoS);
         if (crMemcmp(to->mapItoI, from->mapItoI, CR_MAX_PIXEL_MAP_TABLE*sizeof(GLfloat)))
-            diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_I,to->mapItoIsize,(GLfloat*)to->mapItoI);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_I,to->mapItoIsize,(GLfloat*)to->mapItoI);
         if (crMemcmp(to->mapItoR, from->mapItoR, CR_MAX_PIXEL_MAP_TABLE*sizeof(GLfloat)))
-            diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_R,to->mapItoRsize,(GLfloat*)to->mapItoR);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_R,to->mapItoRsize,(GLfloat*)to->mapItoR);
         if (crMemcmp(to->mapItoG, from->mapItoG, CR_MAX_PIXEL_MAP_TABLE*sizeof(GLfloat)))
-            diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_G,to->mapItoGsize,(GLfloat*)to->mapItoG);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_G,to->mapItoGsize,(GLfloat*)to->mapItoG);
         if (crMemcmp(to->mapItoB, from->mapItoB, CR_MAX_PIXEL_MAP_TABLE*sizeof(GLfloat)))
-            diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_B,to->mapItoBsize,(GLfloat*)to->mapItoB);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_B,to->mapItoBsize,(GLfloat*)to->mapItoB);
         if (crMemcmp(to->mapItoA, from->mapItoA, CR_MAX_PIXEL_MAP_TABLE*sizeof(GLfloat)))
-            diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_A,to->mapItoAsize,(GLfloat*)to->mapItoA);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_A,to->mapItoAsize,(GLfloat*)to->mapItoA);
         if (crMemcmp(to->mapRtoR, from->mapRtoR, CR_MAX_PIXEL_MAP_TABLE*sizeof(GLfloat)))
-            diff_api.PixelMapfv(GL_PIXEL_MAP_R_TO_R,to->mapRtoRsize,(GLfloat*)to->mapRtoR);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_R_TO_R,to->mapRtoRsize,(GLfloat*)to->mapRtoR);
         if (crMemcmp(to->mapGtoG, from->mapGtoG, CR_MAX_PIXEL_MAP_TABLE*sizeof(GLfloat)))
-            diff_api.PixelMapfv(GL_PIXEL_MAP_G_TO_G,to->mapGtoGsize,(GLfloat*)to->mapGtoG);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_G_TO_G,to->mapGtoGsize,(GLfloat*)to->mapGtoG);
         if (crMemcmp(to->mapBtoB, from->mapBtoB, CR_MAX_PIXEL_MAP_TABLE*sizeof(GLfloat)))
-            diff_api.PixelMapfv(GL_PIXEL_MAP_B_TO_B,to->mapBtoBsize,(GLfloat*)to->mapBtoB);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_B_TO_B,to->mapBtoBsize,(GLfloat*)to->mapBtoB);
         if (crMemcmp(to->mapAtoA, from->mapAtoA, CR_MAX_PIXEL_MAP_TABLE*sizeof(GLfloat)))
-            diff_api.PixelMapfv(GL_PIXEL_MAP_A_TO_A,to->mapAtoAsize,(GLfloat*)to->mapAtoA);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_A_TO_A,to->mapAtoAsize,(GLfloat*)to->mapAtoA);
         CLEARDIRTY(b->maps, nbitID);
     }
@@ -692,8 +695,12 @@
                       CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRPixelState *from = &(fromCtx->pixel);
     CRPixelState *to = &(toCtx->pixel);
     int j, i;
     CRbitvalue nbitID[CR_MAX_BITARRAY];
+
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
@@ -703,5 +710,5 @@
         if (from->mapColor != to->mapColor)
         {
-            diff_api.PixelTransferi (GL_MAP_COLOR, to->mapColor);
+            pState->diff_api.PixelTransferi (GL_MAP_COLOR, to->mapColor);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -709,5 +716,5 @@
         if (from->mapStencil != to->mapStencil)
         {
-            diff_api.PixelTransferi (GL_MAP_STENCIL, to->mapStencil);
+            pState->diff_api.PixelTransferi (GL_MAP_STENCIL, to->mapStencil);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -715,5 +722,5 @@
         if (from->indexOffset != to->indexOffset)
         {
-            diff_api.PixelTransferi (GL_INDEX_OFFSET, to->indexOffset);
+            pState->diff_api.PixelTransferi (GL_INDEX_OFFSET, to->indexOffset);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -721,5 +728,5 @@
         if (from->indexShift != to->indexShift)
         {
-            diff_api.PixelTransferi (GL_INDEX_SHIFT, to->indexShift);
+            pState->diff_api.PixelTransferi (GL_INDEX_SHIFT, to->indexShift);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -727,5 +734,5 @@
         if (from->scale.r != to->scale.r)
         {
-            diff_api.PixelTransferf (GL_RED_SCALE, to->scale.r);
+            pState->diff_api.PixelTransferf (GL_RED_SCALE, to->scale.r);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -733,5 +740,5 @@
         if (from->scale.g != to->scale.g)
         {
-            diff_api.PixelTransferf (GL_GREEN_SCALE, to->scale.g);
+            pState->diff_api.PixelTransferf (GL_GREEN_SCALE, to->scale.g);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -739,5 +746,5 @@
         if (from->scale.b != to->scale.b)
         {
-            diff_api.PixelTransferf (GL_BLUE_SCALE, to->scale.b);
+            pState->diff_api.PixelTransferf (GL_BLUE_SCALE, to->scale.b);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -745,5 +752,5 @@
         if (from->scale.a != to->scale.a)
         {
-            diff_api.PixelTransferf (GL_ALPHA_SCALE, to->scale.a);
+            pState->diff_api.PixelTransferf (GL_ALPHA_SCALE, to->scale.a);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -751,5 +758,5 @@
         if (from->bias.r != to->bias.r)
         {
-            diff_api.PixelTransferf (GL_RED_BIAS, to->bias.r);
+            pState->diff_api.PixelTransferf (GL_RED_BIAS, to->bias.r);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -757,5 +764,5 @@
         if (from->bias.g != to->bias.g)
         {
-            diff_api.PixelTransferf (GL_GREEN_BIAS, to->bias.g);
+            pState->diff_api.PixelTransferf (GL_GREEN_BIAS, to->bias.g);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -763,5 +770,5 @@
         if (from->bias.b != to->bias.b)
         {
-            diff_api.PixelTransferf (GL_BLUE_BIAS, to->bias.b);
+            pState->diff_api.PixelTransferf (GL_BLUE_BIAS, to->bias.b);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -769,5 +776,5 @@
         if (from->bias.a != to->bias.a)
         {
-            diff_api.PixelTransferf (GL_ALPHA_BIAS, to->bias.a);
+            pState->diff_api.PixelTransferf (GL_ALPHA_BIAS, to->bias.a);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -775,5 +782,5 @@
         if (from->depthScale != to->depthScale)
         {
-            diff_api.PixelTransferf (GL_DEPTH_SCALE, to->depthScale);
+            pState->diff_api.PixelTransferf (GL_DEPTH_SCALE, to->depthScale);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -781,5 +788,5 @@
         if (from->depthBias != to->depthBias)
         {
-            diff_api.PixelTransferf (GL_DEPTH_BIAS, to->depthBias);
+            pState->diff_api.PixelTransferf (GL_DEPTH_BIAS, to->depthBias);
             FILLDIRTY(b->transfer);
             FILLDIRTY(b->dirty);
@@ -792,6 +799,5 @@
             from->yZoom != to->yZoom)
         {
-            diff_api.PixelZoom (to->xZoom,
-                to->yZoom);
+            pState->diff_api.PixelZoom (to->xZoom, to->yZoom);
             FILLDIRTY(b->zoom);
             FILLDIRTY(b->dirty);
@@ -802,50 +808,50 @@
     {
         if (crMemcmp(to->mapStoS, from->mapStoS, CR_MAX_PIXEL_MAP_TABLE)) {
-            diff_api.PixelMapfv(GL_PIXEL_MAP_S_TO_S,to->mapStoSsize,(GLfloat*)to->mapStoS);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_S_TO_S,to->mapStoSsize,(GLfloat*)to->mapStoS);
             FILLDIRTY(b->maps);
             FILLDIRTY(b->dirty);
         }
         if (crMemcmp(to->mapItoI, from->mapItoI, CR_MAX_PIXEL_MAP_TABLE)) {
-            diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_I,to->mapItoIsize,(GLfloat*)to->mapItoI);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_I,to->mapItoIsize,(GLfloat*)to->mapItoI);
             FILLDIRTY(b->maps);
             FILLDIRTY(b->dirty);
         }
         if (crMemcmp(to->mapItoR, from->mapItoR, CR_MAX_PIXEL_MAP_TABLE)) {
-            diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_R,to->mapItoRsize,(GLfloat*)to->mapItoR);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_R,to->mapItoRsize,(GLfloat*)to->mapItoR);
             FILLDIRTY(b->maps);
             FILLDIRTY(b->dirty);
         }
         if (crMemcmp(to->mapItoG, from->mapItoG, CR_MAX_PIXEL_MAP_TABLE)) {
-            diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_G,to->mapItoGsize,(GLfloat*)to->mapItoG);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_G,to->mapItoGsize,(GLfloat*)to->mapItoG);
             FILLDIRTY(b->maps);
             FILLDIRTY(b->dirty);
         }
         if (crMemcmp(to->mapItoB, from->mapItoB, CR_MAX_PIXEL_MAP_TABLE)) {
-            diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_B,to->mapItoBsize,(GLfloat*)to->mapItoB);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_B,to->mapItoBsize,(GLfloat*)to->mapItoB);
             FILLDIRTY(b->maps);
             FILLDIRTY(b->dirty);
         }
         if (crMemcmp(to->mapItoA, from->mapItoA, CR_MAX_PIXEL_MAP_TABLE)) {
-            diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_A,to->mapItoAsize,(GLfloat*)to->mapItoA);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_I_TO_A,to->mapItoAsize,(GLfloat*)to->mapItoA);
             FILLDIRTY(b->maps);
             FILLDIRTY(b->dirty);
         }
         if (crMemcmp(to->mapRtoR, from->mapRtoR, CR_MAX_PIXEL_MAP_TABLE)) {
-            diff_api.PixelMapfv(GL_PIXEL_MAP_R_TO_R,to->mapRtoRsize,(GLfloat*)to->mapRtoR);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_R_TO_R,to->mapRtoRsize,(GLfloat*)to->mapRtoR);
             FILLDIRTY(b->maps);
             FILLDIRTY(b->dirty);
         }
         if (crMemcmp(to->mapGtoG, from->mapGtoG, CR_MAX_PIXEL_MAP_TABLE)) {
-            diff_api.PixelMapfv(GL_PIXEL_MAP_G_TO_G,to->mapGtoGsize,(GLfloat*)to->mapGtoG);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_G_TO_G,to->mapGtoGsize,(GLfloat*)to->mapGtoG);
             FILLDIRTY(b->maps);
             FILLDIRTY(b->dirty);
         }
         if (crMemcmp(to->mapBtoB, from->mapBtoB, CR_MAX_PIXEL_MAP_TABLE)) {
-            diff_api.PixelMapfv(GL_PIXEL_MAP_B_TO_B,to->mapBtoBsize,(GLfloat*)to->mapBtoB);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_B_TO_B,to->mapBtoBsize,(GLfloat*)to->mapBtoB);
             FILLDIRTY(b->maps);
             FILLDIRTY(b->dirty);
         }
         if (crMemcmp(to->mapAtoA, from->mapAtoA, CR_MAX_PIXEL_MAP_TABLE)) {
-            diff_api.PixelMapfv(GL_PIXEL_MAP_A_TO_A,to->mapAtoAsize,(GLfloat*)to->mapAtoA);
+            pState->diff_api.PixelMapfv(GL_PIXEL_MAP_A_TO_A,to->mapAtoAsize,(GLfloat*)to->mapAtoA);
             FILLDIRTY(b->maps);
             FILLDIRTY(b->dirty);
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_point.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_point.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_point.c	(revision 78375)
@@ -12,5 +12,5 @@
 {
 	CRPointState *p = &ctx->point;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
 	CRPointBits *pb = &(sb->point);
 	int i;
@@ -56,14 +56,14 @@
 }
 
-void STATE_APIENTRY crStatePointSize(GLfloat size) 
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStatePointSize(PCRStateTracker pState, GLfloat size) 
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRPointState *p = &(g->point);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRPointBits *pb = &(sb->point);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glPointSize called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glPointSize called in begin/end");
 		return;
 	}
@@ -73,5 +73,5 @@
 	if (size <= 0.0f) 
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glPointSize called with size <= 0.0: %f", size);
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glPointSize called with size <= 0.0: %f", size);
 		return;
 	}
@@ -82,11 +82,11 @@
 }
 
-void STATE_APIENTRY crStatePointParameterfARB(GLenum pname, GLfloat param)
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStatePointParameterfARB(PCRStateTracker pState, GLenum pname, GLfloat param)
+{
+	CRContext *g = GetCurrentContext(pState);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glPointParameterfARB called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glPointParameterfARB called in begin/end");
 		return;
 	}
@@ -94,17 +94,17 @@
 	FLUSH();
 
-	crStatePointParameterfvARB(pname, &param);
-}
-
-void STATE_APIENTRY crStatePointParameterfvARB(GLenum pname, const GLfloat *params)
-{
-	CRContext *g = GetCurrentContext();
+	crStatePointParameterfvARB(pState, pname, &param);
+}
+
+void STATE_APIENTRY crStatePointParameterfvARB(PCRStateTracker pState, GLenum pname, const GLfloat *params)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRPointState *p = &(g->point);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRPointBits *pb = &(sb->point);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glPointParameterfvARB called in begin/end");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glPointParameterfvARB called in begin/end");
 		return;
 	}
@@ -122,5 +122,5 @@
 		else 
 		{
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glPointParameterfvARB invalid enum: %f", pname);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glPointParameterfvARB invalid enum: %f", pname);
 			return;
 		}
@@ -129,5 +129,5 @@
 		if (g->extensions.ARB_point_parameters) {
 			if (params[0] < 0.0F) {
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glPointParameterfvARB invalid value: %f", params[0]);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glPointParameterfvARB invalid value: %f", params[0]);
 				return;
 			}
@@ -137,5 +137,5 @@
 		else
 		{
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glPointParameterfvARB invalid enum: %f", pname);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glPointParameterfvARB invalid enum: %f", pname);
 			return;
 		}
@@ -144,5 +144,5 @@
 		if (g->extensions.ARB_point_parameters) {
 			if (params[0] < 0.0F) {
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glPointParameterfvARB invalid value: %f", params[0]);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glPointParameterfvARB invalid value: %f", params[0]);
 				return;
 			}
@@ -152,5 +152,5 @@
 		else
 		{
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glPointParameterfvARB invalid enum: %f", pname);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glPointParameterfvARB invalid enum: %f", pname);
 			return;
 		}
@@ -159,5 +159,5 @@
 		if (g->extensions.ARB_point_parameters) {
 			if (params[0] < 0.0F) {
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glPointParameterfvARB invalid value: %f", params[0]);
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glPointParameterfvARB invalid value: %f", params[0]);
 				return;
 			}
@@ -167,5 +167,5 @@
 		else
 		{
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glPointParameterfvARB invalid enum: %f", pname);
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glPointParameterfvARB invalid enum: %f", pname);
 			return;
 		}
@@ -175,5 +175,5 @@
 	    GLenum enmVal = (GLenum)params[0];
         if (enmVal != GL_LOWER_LEFT && enmVal != GL_UPPER_LEFT) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glPointParameterfvARB invalid GL_POINT_SPRITE_COORD_ORIGIN value: %f", params[0]);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glPointParameterfvARB invalid GL_POINT_SPRITE_COORD_ORIGIN value: %f", params[0]);
             return;
         }
@@ -183,5 +183,5 @@
 	}
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glPointParameterfvARB invalid enum: %f", pname);
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glPointParameterfvARB invalid enum: %f", pname);
 		return;
 	}
@@ -190,14 +190,14 @@
 }
 
-void STATE_APIENTRY crStatePointParameteri(GLenum pname, GLint param)
+void STATE_APIENTRY crStatePointParameteri(PCRStateTracker pState, GLenum pname, GLint param)
 {
 	GLfloat f_param = (GLfloat) param;
-	crStatePointParameterfvARB( pname, &f_param );
-}
-
-void STATE_APIENTRY crStatePointParameteriv(GLenum pname, const GLint *params)
+	crStatePointParameterfvARB(pState, pname, &f_param );
+}
+
+void STATE_APIENTRY crStatePointParameteriv(PCRStateTracker pState, GLenum pname, const GLint *params)
 {
 	GLfloat f_param = (GLfloat) (*params);
-	crStatePointParameterfvARB( pname, &f_param );
+	crStatePointParameterfvARB(pState, pname, &f_param );
 }
 
@@ -205,9 +205,13 @@
         CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRPointState *from = &(fromCtx->point);
     CRPointState *to = &(toCtx->point);
     unsigned int j, i;
     CRbitvalue nbitID[CR_MAX_BITARRAY];
-    Assert(0);
+    Assert(0); /** @todo Is this never called? */
+
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
@@ -216,6 +220,6 @@
     {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->pointSmooth != to->pointSmooth)
         {
@@ -229,5 +233,5 @@
         if (from->pointSize != to->pointSize)
         {
-            diff_api.PointSize (to->pointSize);
+            pState->diff_api.PointSize (to->pointSize);
             from->pointSize = to->pointSize;
         }
@@ -238,5 +242,5 @@
         if (from->minSize != to->minSize)
         {
-            diff_api.PointParameterfARB (GL_POINT_SIZE_MIN_ARB, to->minSize);
+            pState->diff_api.PointParameterfARB (GL_POINT_SIZE_MIN_ARB, to->minSize);
             from->minSize = to->minSize;
         }
@@ -247,5 +251,5 @@
         if (from->maxSize != to->maxSize)
         {
-            diff_api.PointParameterfARB (GL_POINT_SIZE_MAX_ARB, to->maxSize);
+            pState->diff_api.PointParameterfARB (GL_POINT_SIZE_MAX_ARB, to->maxSize);
             from->maxSize = to->maxSize;
         }
@@ -256,5 +260,5 @@
         if (from->fadeThresholdSize != to->fadeThresholdSize)
         {
-            diff_api.PointParameterfARB (GL_POINT_FADE_THRESHOLD_SIZE_ARB, to->fadeThresholdSize);
+            pState->diff_api.PointParameterfARB (GL_POINT_FADE_THRESHOLD_SIZE_ARB, to->fadeThresholdSize);
             from->fadeThresholdSize = to->fadeThresholdSize;
         }
@@ -265,5 +269,5 @@
         if (from->spriteCoordOrigin != to->spriteCoordOrigin)
         {
-            diff_api.PointParameterfARB (GL_POINT_SPRITE_COORD_ORIGIN, to->spriteCoordOrigin);
+            pState->diff_api.PointParameterfARB (GL_POINT_SPRITE_COORD_ORIGIN, to->spriteCoordOrigin);
             from->spriteCoordOrigin = to->spriteCoordOrigin;
         }
@@ -273,5 +277,5 @@
     {
         if (from->distanceAttenuation[0] != to->distanceAttenuation[0] || from->distanceAttenuation[1] != to->distanceAttenuation[1] || from->distanceAttenuation[2] != to->distanceAttenuation[2]) {
-            diff_api.PointParameterfvARB (GL_POINT_DISTANCE_ATTENUATION_ARB, to->distanceAttenuation);
+            pState->diff_api.PointParameterfvARB (GL_POINT_DISTANCE_ATTENUATION_ARB, to->distanceAttenuation);
             from->distanceAttenuation[0] = to->distanceAttenuation[0];
             from->distanceAttenuation[1] = to->distanceAttenuation[1];
@@ -283,6 +287,6 @@
     {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->pointSprite != to->pointSprite)
         {
@@ -299,8 +303,8 @@
                 GLint replacement = to->coordReplacement[i];
                 if (activeUnit != i) {
-                     diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB );
+                     pState->diff_api.ActiveTextureARB(i + GL_TEXTURE0_ARB );
                      activeUnit = i;
                 }
-                diff_api.TexEnviv(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, &replacement);
+                pState->diff_api.TexEnviv(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, &replacement);
                 from->coordReplacement[i] = to->coordReplacement[i];
                 CLEARDIRTY(b->coordReplacement[i], nbitID);
@@ -308,5 +312,5 @@
         }
         if (activeUnit != toCtx->texture.curTextureUnit)
-           diff_api.ActiveTextureARB(GL_TEXTURE0 + toCtx->texture.curTextureUnit);
+           pState->diff_api.ActiveTextureARB(GL_TEXTURE0 + toCtx->texture.curTextureUnit);
     }
     CLEARDIRTY(b->dirty, nbitID);
@@ -316,4 +320,5 @@
         CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRPointState *from = &(fromCtx->point);
     CRPointState *to = &(toCtx->point);
@@ -321,4 +326,7 @@
     GLboolean fEnabled;
     CRbitvalue nbitID[CR_MAX_BITARRAY];
+
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
@@ -327,6 +335,6 @@
     {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->pointSmooth != to->pointSmooth)
         {
@@ -341,5 +349,5 @@
         if (from->pointSize != to->pointSize)
         {
-            diff_api.PointSize (to->pointSize);
+            pState->diff_api.PointSize (to->pointSize);
             FILLDIRTY(b->size);
             FILLDIRTY(b->dirty);
@@ -351,5 +359,5 @@
         if (from->minSize != to->minSize)
         {
-            diff_api.PointParameterfARB (GL_POINT_SIZE_MIN_ARB, to->minSize);
+            pState->diff_api.PointParameterfARB (GL_POINT_SIZE_MIN_ARB, to->minSize);
             FILLDIRTY(b->minSize);
             FILLDIRTY(b->dirty);
@@ -361,5 +369,5 @@
         if (from->maxSize != to->maxSize)
         {
-            diff_api.PointParameterfARB (GL_POINT_SIZE_MAX_ARB, to->maxSize);
+            pState->diff_api.PointParameterfARB (GL_POINT_SIZE_MAX_ARB, to->maxSize);
             FILLDIRTY(b->maxSize);
             FILLDIRTY(b->dirty);
@@ -371,5 +379,5 @@
         if (from->fadeThresholdSize != to->fadeThresholdSize)
         {
-            diff_api.PointParameterfARB (GL_POINT_FADE_THRESHOLD_SIZE_ARB, to->fadeThresholdSize);
+            pState->diff_api.PointParameterfARB (GL_POINT_FADE_THRESHOLD_SIZE_ARB, to->fadeThresholdSize);
             FILLDIRTY(b->fadeThresholdSize);
             FILLDIRTY(b->dirty);
@@ -381,5 +389,5 @@
         if (from->spriteCoordOrigin != to->spriteCoordOrigin)
         {
-            diff_api.PointParameterfARB (GL_POINT_SPRITE_COORD_ORIGIN, to->spriteCoordOrigin);
+            pState->diff_api.PointParameterfARB (GL_POINT_SPRITE_COORD_ORIGIN, to->spriteCoordOrigin);
             FILLDIRTY(b->spriteCoordOrigin);
             FILLDIRTY(b->dirty);
@@ -390,5 +398,5 @@
     {
         if (from->distanceAttenuation[0] != to->distanceAttenuation[0] || from->distanceAttenuation[1] != to->distanceAttenuation[1] || from->distanceAttenuation[2] != to->distanceAttenuation[2]) {
-            diff_api.PointParameterfvARB (GL_POINT_DISTANCE_ATTENUATION_ARB, to->distanceAttenuation);
+            pState->diff_api.PointParameterfvARB (GL_POINT_DISTANCE_ATTENUATION_ARB, to->distanceAttenuation);
             FILLDIRTY(b->distanceAttenuation);
             FILLDIRTY(b->dirty);
@@ -404,5 +412,5 @@
                 if (!fEnabled)
                 {
-                    diff_api.Enable(GL_POINT_SPRITE_ARB);
+                    pState->diff_api.Enable(GL_POINT_SPRITE_ARB);
                     fEnabled = GL_TRUE;
                 }
@@ -422,11 +430,11 @@
         }
         if (activeUnit != toCtx->texture.curTextureUnit)
-           diff_api.ActiveTextureARB(GL_TEXTURE0 + toCtx->texture.curTextureUnit);
+           pState->diff_api.ActiveTextureARB(GL_TEXTURE0 + toCtx->texture.curTextureUnit);
     }
     if (CHECKDIRTY(b->enableSprite, bitID))
     {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (fEnabled != to->pointSprite)
         {
@@ -440,6 +448,6 @@
     {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         able[to->pointSprite](GL_POINT_SPRITE_ARB);
     }
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_polygon.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_polygon.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_polygon.c	(revision 78375)
@@ -14,5 +14,5 @@
 {
     CRPolygonState *p = &ctx->polygon;
-        CRStateBits *sb = GetCurrentBits();
+        CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
     CRPolygonBits *pb = &(sb->polygon);
     int i;
@@ -43,14 +43,14 @@
 }
 
-void STATE_APIENTRY crStateCullFace(GLenum mode) 
-{
-    CRContext *g = GetCurrentContext();
-    CRPolygonState *p = &(g->polygon);
-        CRStateBits *sb = GetCurrentBits();
-    CRPolygonBits *pb = &(sb->polygon);
-
-    if (g->current.inBeginEnd) 
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateCullFace(PCRStateTracker pState, GLenum mode) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRPolygonState *p = &(g->polygon);
+        CRStateBits *sb = GetCurrentBits(pState);
+    CRPolygonBits *pb = &(sb->polygon);
+
+    if (g->current.inBeginEnd) 
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                 "glCullFace called in begin/end");
         return;
@@ -61,5 +61,5 @@
     if (mode != GL_FRONT && mode != GL_BACK && mode != GL_FRONT_AND_BACK)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                 "glCullFace called with bogus mode: 0x%x", mode);
         return;
@@ -71,14 +71,14 @@
 }
 
-void STATE_APIENTRY crStateFrontFace (GLenum mode) 
-{
-    CRContext *g = GetCurrentContext();
-    CRPolygonState *p = &(g->polygon);
-        CRStateBits *sb = GetCurrentBits();
-    CRPolygonBits *pb = &(sb->polygon);
-
-    if (g->current.inBeginEnd) 
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateFrontFace (PCRStateTracker pState, GLenum mode) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRPolygonState *p = &(g->polygon);
+        CRStateBits *sb = GetCurrentBits(pState);
+    CRPolygonBits *pb = &(sb->polygon);
+
+    if (g->current.inBeginEnd) 
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                 "glFrontFace called in begin/end");
         return;
@@ -89,5 +89,5 @@
     if (mode != GL_CW && mode != GL_CCW)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                 "glFrontFace called with bogus mode: 0x%x", mode);
         return;
@@ -99,14 +99,14 @@
 }
 
-void  STATE_APIENTRY crStatePolygonMode (GLenum face, GLenum mode) 
-{
-    CRContext *g = GetCurrentContext();
-    CRPolygonState *p = &(g->polygon);
-        CRStateBits *sb = GetCurrentBits();
-    CRPolygonBits *pb = &(sb->polygon);
-
-    if (g->current.inBeginEnd) 
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void  STATE_APIENTRY crStatePolygonMode (PCRStateTracker pState, GLenum face, GLenum mode) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRPolygonState *p = &(g->polygon);
+        CRStateBits *sb = GetCurrentBits(pState);
+    CRPolygonBits *pb = &(sb->polygon);
+
+    if (g->current.inBeginEnd) 
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                 "glPolygonMode called in begin/end");
         return;
@@ -117,5 +117,5 @@
     if (mode != GL_POINT && mode != GL_LINE && mode != GL_FILL)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                 "glPolygonMode called with bogus mode: 0x%x", mode);
         return;
@@ -133,5 +133,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glPolygonMode called with bogus face: 0x%x", face);
             return;
@@ -141,14 +141,14 @@
 }
 
-void STATE_APIENTRY crStatePolygonOffset (GLfloat factor, GLfloat units) 
-{
-    CRContext *g = GetCurrentContext();
-    CRPolygonState *p = &(g->polygon);
-        CRStateBits *sb = GetCurrentBits();
-    CRPolygonBits *pb = &(sb->polygon);
-
-    if (g->current.inBeginEnd) 
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStatePolygonOffset (PCRStateTracker pState, GLfloat factor, GLfloat units) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRPolygonState *p = &(g->polygon);
+        CRStateBits *sb = GetCurrentBits(pState);
+    CRPolygonBits *pb = &(sb->polygon);
+
+    if (g->current.inBeginEnd) 
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                 "glPolygonOffset called in begin/end");
         return;
@@ -164,14 +164,14 @@
 }
 
-void STATE_APIENTRY crStatePolygonStipple (const GLubyte *p) 
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStatePolygonStipple (PCRStateTracker pState, const GLubyte *p) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRPolygonState *poly = &(g->polygon);
-        CRStateBits *sb = GetCurrentBits();
-    CRPolygonBits *pb = &(sb->polygon);
-
-    if (g->current.inBeginEnd) 
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        CRStateBits *sb = GetCurrentBits(pState);
+    CRPolygonBits *pb = &(sb->polygon);
+
+    if (g->current.inBeginEnd) 
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                 "glPolygonStipple called in begin/end");
         return;
@@ -180,5 +180,5 @@
     FLUSH();
 
-    if (!p && !crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (!p && !crStateIsBufferBound(pState, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         crDebug("Void pointer passed to PolygonStipple");
@@ -187,5 +187,5 @@
 
     /** @todo track mask if buffer is bound?*/
-    if (!crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (!crStateIsBufferBound(pState, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         crMemcpy((char*)poly->stipple, (char*)p, 128);
@@ -196,12 +196,12 @@
 }
 
-void STATE_APIENTRY crStateGetPolygonStipple( GLubyte *b )
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetPolygonStipple(PCRStateTracker pState, GLubyte *b )
+{
+    CRContext *g = GetCurrentContext(pState);
     CRPolygonState *poly = &(g->polygon);
 
     if (g->current.inBeginEnd) 
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                 "glGetPolygonStipple called in begin/end");
         return;
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_polygon.txt
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_polygon.txt	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_polygon.txt	(revision 78375)
@@ -14,7 +14,7 @@
 :mode:backMode:PolygonMode,GL_BACK
 :mode:frontMode:PolygonMode,GL_FRONT
--:stipple:stipple:*diff_api.PolygonStipple ((GLubyte *) to->stipple);
+-:stipple:stipple:*pState->diff_api.PolygonStipple ((GLubyte *) to->stipple);
 +:stipple:*int iStripple;
-+:stipple:*diff_api.PolygonStipple ((GLubyte *) to->stipple);
++:stipple:*pState->diff_api.PolygonStipple ((GLubyte *) to->stipple);
 +:stipple:*for (iStripple=0; iStripple<32; iStripple++)
 +:stipple:*{
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_program.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_program.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_program.c	(revision 78375)
@@ -132,15 +132,15 @@
  */
 static CRProgram *
-BindProgram(GLenum target, GLuint id,
+BindProgram(PCRStateTracker pState, GLenum target, GLuint id,
                         GLenum vertexTarget, GLenum fragmentTarget)
 {
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-    CRStateBits *sb = GetCurrentBits();
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
     CRProgram *prog;
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glBindProgram called in Begin/End");
         return NULL;
@@ -155,5 +155,5 @@
         }
         else {
-            crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                      "glBindProgram(bad target)");
             return NULL;
@@ -165,9 +165,9 @@
 
     if (!prog) {
-        crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glBindProgram");
+        crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY, "glBindProgram");
         return NULL;
     }
     else if (prog->target != target) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glBindProgram target mismatch");
         return NULL;
@@ -190,7 +190,7 @@
 
 
-void STATE_APIENTRY crStateBindProgramNV(GLenum target, GLuint id)
-{
-    CRProgram *prog = BindProgram(target, id, GL_VERTEX_PROGRAM_NV,
+void STATE_APIENTRY crStateBindProgramNV(PCRStateTracker pState, GLenum target, GLuint id)
+{
+    CRProgram *prog = BindProgram(pState, target, id, GL_VERTEX_PROGRAM_NV,
                                   GL_FRAGMENT_PROGRAM_NV);
     if (prog) {
@@ -200,7 +200,7 @@
 
 
-void STATE_APIENTRY crStateBindProgramARB(GLenum target, GLuint id)
-{
-    CRProgram *prog = BindProgram(target, id, GL_VERTEX_PROGRAM_ARB,
+void STATE_APIENTRY crStateBindProgramARB(PCRStateTracker pState, GLenum target, GLuint id)
+{
+    CRProgram *prog = BindProgram(pState, target, id, GL_VERTEX_PROGRAM_ARB,
                                   GL_FRAGMENT_PROGRAM_ARB);
     if (prog) {
@@ -210,14 +210,14 @@
 
 
-void STATE_APIENTRY crStateDeleteProgramsARB(GLsizei n, const GLuint *ids)
-{
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-    CRStateBits *sb = GetCurrentBits();
+void STATE_APIENTRY crStateDeleteProgramsARB(PCRStateTracker pState, GLsizei n, const GLuint *ids)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
     GLint i;
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glDeleteProgramsNV called in Begin/End");
         return;
@@ -226,5 +226,5 @@
     if (n < 0)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glDeleteProgramsNV(n)");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glDeleteProgramsNV(n)");
         return;
     }
@@ -253,9 +253,10 @@
 
 
-void STATE_APIENTRY crStateExecuteProgramNV(GLenum target, GLuint id, const GLfloat *params)
+void STATE_APIENTRY crStateExecuteProgramNV(PCRStateTracker pState, GLenum target, GLuint id, const GLfloat *params)
 {
     /* Hmmm, this is really hard to do if we don't actually execute
      * the program in a software simulation.
      */
+    RT_NOREF(pState);
     (void)params;
     (void)target;
@@ -264,7 +265,7 @@
 
 
-void STATE_APIENTRY crStateGenProgramsNV(GLsizei n, GLuint *ids)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGenProgramsNV(PCRStateTracker pState, GLsizei n, GLuint *ids)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
 
@@ -272,18 +273,18 @@
 }
 
-void STATE_APIENTRY crStateGenProgramsARB(GLsizei n, GLuint *ids)
-{
-    crStateGenProgramsNV(n, ids);
-}
-
-
-GLboolean STATE_APIENTRY crStateIsProgramARB(GLuint id)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGenProgramsARB(PCRStateTracker pState, GLsizei n, GLuint *ids)
+{
+    crStateGenProgramsNV(pState, n, ids);
+}
+
+
+GLboolean STATE_APIENTRY crStateIsProgramARB(PCRStateTracker pState, GLuint id)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
     CRProgram *prog;
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glIsProgram called in Begin/End");
         return GL_FALSE;
@@ -291,5 +292,5 @@
 
     if (id == 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glIsProgram(id==0)");
         return GL_FALSE;
@@ -304,7 +305,7 @@
 
 
-GLboolean STATE_APIENTRY crStateAreProgramsResidentNV(GLsizei n, const GLuint *ids, GLboolean *residences)
-{
-    CRContext *g = GetCurrentContext();
+GLboolean STATE_APIENTRY crStateAreProgramsResidentNV(PCRStateTracker pState, GLsizei n, const GLuint *ids, GLboolean *residences)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
     int i;
@@ -312,5 +313,5 @@
 
     if (n < 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glAreProgramsResidentNV(n)");
         return GL_FALSE;
@@ -321,5 +322,5 @@
 
         if (ids[i] == 0) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glAreProgramsResidentNV(id)");
             return GL_FALSE;
@@ -328,5 +329,5 @@
         prog = (CRProgram *) crHashtableSearch(p->programHash, ids[i]);
         if (!prog) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glAreProgramsResidentNV(id)");
             return GL_FALSE;
@@ -351,12 +352,12 @@
 
 
-void STATE_APIENTRY crStateRequestResidentProgramsNV(GLsizei n, const GLuint *ids)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateRequestResidentProgramsNV(PCRStateTracker pState, GLsizei n, const GLuint *ids)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
     GLint i;
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glRequestResidentProgramsNV called in Begin/End");
         return;
@@ -364,5 +365,5 @@
 
     if (n < 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glRequestResidentProgramsNV(n<0)");
         return;
@@ -377,10 +378,10 @@
 
 
-void STATE_APIENTRY crStateLoadProgramNV(GLenum target, GLuint id, GLsizei len,
+void STATE_APIENTRY crStateLoadProgramNV(PCRStateTracker pState, GLenum target, GLuint id, GLsizei len,
                                                                                  const GLubyte *program)
 {
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-    CRStateBits *sb = GetCurrentBits();
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
     CRProgram *prog;
@@ -388,5 +389,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glLoadProgramNV called in Begin/End");
         return;
@@ -394,5 +395,5 @@
 
     if (id == 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glLoadProgramNV(id==0)");
         return;
@@ -402,9 +403,9 @@
 
     if (!prog) {
-        crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glLoadProgramNV");
+        crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY, "glLoadProgramNV");
         return;
     }
     else if (prog && prog->target != target) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glLoadProgramNV(target)");
         return;
@@ -413,5 +414,5 @@
     progCopy = crAlloc(len);
     if (!progCopy) {
-            crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glLoadProgramNV");
+            crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY, "glLoadProgramNV");
             return;
     }
@@ -422,5 +423,5 @@
      && crStrncmp((const char *) program,"!!VP2.0", 7) != 0
      && crStrncmp((const char *) program,"!!VSP1.0", 8) != 0) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glLoadProgramNV");
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glLoadProgramNV");
             crDebug("program = (%s)\n",program);
             return;
@@ -439,10 +440,10 @@
 
 
-void STATE_APIENTRY crStateProgramStringARB(GLenum target, GLenum format,
+void STATE_APIENTRY crStateProgramStringARB(PCRStateTracker pState, GLenum target, GLenum format,
                                                                                         GLsizei len, const GLvoid *string)
 {
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-    CRStateBits *sb = GetCurrentBits();
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
     CRProgram *prog;
@@ -450,5 +451,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glProgramStringARB called in Begin/End");
         return;
@@ -456,5 +457,5 @@
 
     if (format != GL_PROGRAM_FORMAT_ASCII_ARB) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glProgramStringARB(format)");
         return;
@@ -470,5 +471,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glProgramStringARB(target)");
         return;
@@ -480,10 +481,10 @@
     progCopy = crAlloc(len);
     if (!progCopy) {
-        crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glProgramStringARB");
+        crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY, "glProgramStringARB");
         return;
     }
     if (crStrncmp(string,"!!ARBvp1.0", 10) !=  0 
      && crStrncmp(string,"!!ARBfp1.0", 10) != 0) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glProgramStringARB");
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glProgramStringARB");
             return;
     }
@@ -502,12 +503,12 @@
 
 
-void STATE_APIENTRY crStateGetProgramivNV(GLuint id, GLenum pname, GLint *params)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetProgramivNV(PCRStateTracker pState, GLuint id, GLenum pname, GLint *params)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
     CRProgram *prog;
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramivNV called in Begin/End");
         return;
@@ -515,5 +516,5 @@
 
     if (id == 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramivNV(bad id)");
         return;
@@ -522,5 +523,5 @@
     prog = (CRProgram *) crHashtableSearch(p->programHash, id);
     if (!prog) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramivNV(bad id)");
         return;
@@ -538,5 +539,5 @@
         return;
     default:
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetProgramivNV(pname)");
         return;
@@ -545,12 +546,12 @@
 
 
-void STATE_APIENTRY crStateGetProgramStringNV(GLuint id, GLenum pname, GLubyte *program)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetProgramStringNV(PCRStateTracker pState, GLuint id, GLenum pname, GLubyte *program)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
     CRProgram *prog;
 
     if (pname != GL_PROGRAM_STRING_NV) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetProgramStringNV(pname)");
         return;
@@ -558,5 +559,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramStringNV called in Begin/End");
         return;
@@ -564,5 +565,5 @@
 
     if (id == 0) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramStringNV(bad id)");
         return;
@@ -571,5 +572,5 @@
     prog = (CRProgram *) crHashtableSearch(p->programHash, id);
     if (!prog) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramStringNV(bad id)");
         return;
@@ -580,7 +581,7 @@
 
 
-void STATE_APIENTRY crStateGetProgramStringARB(GLenum target, GLenum pname, GLvoid *string)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetProgramStringARB(PCRStateTracker pState, GLenum target, GLenum pname, GLvoid *string)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
     CRProgram *prog;
@@ -593,5 +594,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetProgramStringNV(target)");
         return;
@@ -599,5 +600,5 @@
 
     if (pname != GL_PROGRAM_STRING_NV) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetProgramStringNV(pname)");
         return;
@@ -605,5 +606,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramStringNV called in Begin/End");
         return;
@@ -614,31 +615,30 @@
 
 
-void STATE_APIENTRY crStateProgramParameter4dNV(GLenum target, GLuint index,
-                                GLdouble x, GLdouble y, GLdouble z, GLdouble w)
-{
-    crStateProgramParameter4fNV(target, index, (GLfloat) x, (GLfloat) y,
+void STATE_APIENTRY crStateProgramParameter4dNV(PCRStateTracker pState, GLenum target, GLuint index,
+                                                GLdouble x, GLdouble y, GLdouble z, GLdouble w)
+{
+    crStateProgramParameter4fNV(pState, target, index, (GLfloat) x, (GLfloat) y,
                                                             (GLfloat) z, (GLfloat) w);
 }
 
 
-void STATE_APIENTRY crStateProgramParameter4dvNV(GLenum target, GLuint index,
-                                 const GLdouble *params)
-{
-    crStateProgramParameter4fNV(target, index,
-                                                            (GLfloat) params[0], (GLfloat) params[1],
-                                                            (GLfloat) params[2], (GLfloat) params[3]);
-}
-
-
-void STATE_APIENTRY crStateProgramParameter4fNV(GLenum target, GLuint index,
-                                                                 GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-    CRStateBits *sb = GetCurrentBits();
+void STATE_APIENTRY crStateProgramParameter4dvNV(PCRStateTracker pState, GLenum target, GLuint index,
+                                                 const GLdouble *params)
+{
+    crStateProgramParameter4fNV(pState, target, index, (GLfloat) params[0], (GLfloat) params[1],
+                                (GLfloat) params[2], (GLfloat) params[3]);
+}
+
+
+void STATE_APIENTRY crStateProgramParameter4fNV(PCRStateTracker pState, GLenum target, GLuint index,
+                                                GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glProgramParameterNV called in Begin/End");
         return;
@@ -656,5 +656,5 @@
         }
         else {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glProgramParameterNV(index=%d)", index);
             return;
@@ -662,5 +662,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glProgramParameterNV(target)");
         return;
@@ -669,22 +669,21 @@
 
 
-void STATE_APIENTRY crStateProgramParameter4fvNV(GLenum target, GLuint index,
-                                 const GLfloat *params)
-{
-    crStateProgramParameter4fNV(target, index,
-                                                            params[0], params[1], params[2], params[3]);
-}
-
-
-void STATE_APIENTRY crStateProgramParameters4dvNV(GLenum target, GLuint index,
-                                  GLuint num, const GLdouble *params)
-{
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-    CRStateBits *sb = GetCurrentBits();
+void STATE_APIENTRY crStateProgramParameter4fvNV(PCRStateTracker pState, GLenum target, GLuint index,
+                                                 const GLfloat *params)
+{
+    crStateProgramParameter4fNV(pState, target, index, params[0], params[1], params[2], params[3]);
+}
+
+
+void STATE_APIENTRY crStateProgramParameters4dvNV(PCRStateTracker pState, GLenum target, GLuint index,
+                                                  GLuint num, const GLdouble *params)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glProgramParameters4dvNV called in Begin/End");
         return;
@@ -693,5 +692,5 @@
     if (target == GL_VERTEX_PROGRAM_NV) {
         if (index >= UINT32_MAX - num) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                 "glProgramParameters4dvNV(index+num) integer overflow");
             return;
@@ -710,5 +709,5 @@
         }
         else {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glProgramParameters4dvNV(index+num)");
             return;
@@ -716,5 +715,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glProgramParameterNV(target)");
         return;
@@ -723,14 +722,14 @@
 
 
-void STATE_APIENTRY crStateProgramParameters4fvNV(GLenum target, GLuint index,
-                                  GLuint num, const GLfloat *params)
-{
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-    CRStateBits *sb = GetCurrentBits();
+void STATE_APIENTRY crStateProgramParameters4fvNV(PCRStateTracker pState, GLenum target, GLuint index,
+                                                  GLuint num, const GLfloat *params)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glProgramParameters4dvNV called in Begin/End");
         return;
@@ -739,5 +738,5 @@
     if (target == GL_VERTEX_PROGRAM_NV) {
         if (index >= UINT32_MAX - num) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                 "glProgramParameters4dvNV(index+num) integer overflow");
             return;
@@ -756,5 +755,5 @@
         }
         else {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glProgramParameters4dvNV(index+num)");
             return;
@@ -762,5 +761,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glProgramParameterNV(target)");
         return;
@@ -769,12 +768,12 @@
 
 
-void STATE_APIENTRY crStateGetProgramParameterfvNV(GLenum target, GLuint index,
-                                                                        GLenum pname, GLfloat *params)
-{
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-
-    if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateGetProgramParameterfvNV(PCRStateTracker pState, GLenum target, GLuint index,
+                                                   GLenum pname, GLfloat *params)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+
+    if (g->current.inBeginEnd) {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramParameterfvNV called in Begin/End");
         return;
@@ -790,5 +789,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                          "glGetProgramParameterfvNV(index)");
                 return;
@@ -796,5 +795,5 @@
         }
         else {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glGetProgramParameterfvNV(pname)");
             return;
@@ -802,5 +801,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetProgramParameterfvNV(target)");
         return;
@@ -809,12 +808,12 @@
 
 
-void STATE_APIENTRY crStateGetProgramParameterdvNV(GLenum target, GLuint index,
-                                   GLenum pname, GLdouble *params)
-{
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-
-    if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateGetProgramParameterdvNV(PCRStateTracker pState, GLenum target, GLuint index,
+                                                   GLenum pname, GLdouble *params)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+
+    if (g->current.inBeginEnd) {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramParameterdvNV called in Begin/End");
         return;
@@ -830,5 +829,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                          "glGetProgramParameterdvNV(index)");
                 return;
@@ -836,5 +835,5 @@
         }
         else {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glGetProgramParameterdvNV(pname)");
             return;
@@ -842,5 +841,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetProgramParameterdvNV(target)");
         return;
@@ -849,14 +848,14 @@
 
 
-void STATE_APIENTRY crStateTrackMatrixNV(GLenum target, GLuint address,
-                         GLenum matrix, GLenum transform)
-{
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-    CRStateBits *sb = GetCurrentBits();
+void STATE_APIENTRY crStateTrackMatrixNV(PCRStateTracker pState, GLenum target, GLuint address,
+                                         GLenum matrix, GLenum transform)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetTrackMatrixivNV called in Begin/End");
         return;
@@ -865,5 +864,5 @@
     if (target == GL_VERTEX_PROGRAM_NV) {
         if (address & 0x3 || address >= g->limits.maxVertexProgramEnvParams) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glTrackMatrixNV(address)");
             return;
@@ -896,5 +895,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glTrackMatrixNV(matrix = %x)",matrix);
             return;
@@ -909,5 +908,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glTrackMatrixNV(transform = %x)",transform);
             return;
@@ -920,5 +919,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glTrackMatrixNV(target = %x)",target);
     }
@@ -926,12 +925,12 @@
 
 
-void STATE_APIENTRY crStateGetTrackMatrixivNV(GLenum target, GLuint address,
-                                                             GLenum pname, GLint *params)
-{
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-
-    if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateGetTrackMatrixivNV(PCRStateTracker pState, GLenum target, GLuint address,
+                                              GLenum pname, GLint *params)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+
+    if (g->current.inBeginEnd) {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetTrackMatrixivNV called in Begin/End");
         return;
@@ -940,5 +939,5 @@
     if (target == GL_VERTEX_PROGRAM_NV) {
         if ((address & 0x3) || address >= g->limits.maxVertexProgramEnvParams) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glGetTrackMatrixivNV(address)");
             return;
@@ -951,5 +950,5 @@
         }
         else {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glGetTrackMatrixivNV(pname)");
             return;
@@ -957,5 +956,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetTrackMatrixivNV(target)");
         return;
@@ -964,9 +963,9 @@
 
 
-void STATE_APIENTRY crStateGetVertexAttribdvNV(GLuint index, GLenum pname, GLdouble *params)
+void STATE_APIENTRY crStateGetVertexAttribdvNV(PCRStateTracker pState, GLuint index, GLenum pname, GLdouble *params)
 {
      /* init vars to prevent compiler warnings/errors */
      GLfloat floatParams[4] = { 0.0, 0.0, 0.0, 0.0 };
-     crStateGetVertexAttribfvNV(index, pname, floatParams);
+     crStateGetVertexAttribfvNV(pState, index, pname, floatParams);
      params[0] = floatParams[0];
      if (pname == GL_CURRENT_ATTRIB_NV) {
@@ -978,10 +977,10 @@
 
 
-void STATE_APIENTRY crStateGetVertexAttribfvNV(GLuint index, GLenum pname, GLfloat *params)
-{
-    CRContext *g = GetCurrentContext();
-
-    if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateGetVertexAttribfvNV(PCRStateTracker pState, GLuint index, GLenum pname, GLfloat *params)
+{
+    CRContext *g = GetCurrentContext(pState);
+
+    if (g->current.inBeginEnd) {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetVertexAttribfvNV called in Begin/End");
         return;
@@ -989,5 +988,5 @@
 
     if (index >= CR_MAX_VERTEX_ATTRIBS) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glGetVertexAttribfvNV(index)");
         return;
@@ -1005,19 +1004,19 @@
         break;
     case GL_CURRENT_ATTRIB_NV:
-        crStateCurrentRecover();
+        crStateCurrentRecover(pState);
         COPY_4V(params , g->current.vertexAttrib[index]);
         break;
     default:
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetVertexAttribfvNV");
-        return;
-    }
-}
-
-
-void STATE_APIENTRY crStateGetVertexAttribivNV(GLuint index, GLenum pname, GLint *params)
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetVertexAttribfvNV");
+        return;
+    }
+}
+
+
+void STATE_APIENTRY crStateGetVertexAttribivNV(PCRStateTracker pState, GLuint index, GLenum pname, GLint *params)
 {
      /* init vars to prevent compiler warnings/errors */
      GLfloat floatParams[4] = { 0.0, 0.0, 0.0, 0.0 };
-     crStateGetVertexAttribfvNV(index, pname, floatParams);
+     crStateGetVertexAttribfvNV(pState, index, pname, floatParams);
      params[0] = (GLint) floatParams[0];
      if (pname == GL_CURRENT_ATTRIB_NV) {
@@ -1030,10 +1029,10 @@
 
 
-void STATE_APIENTRY crStateGetVertexAttribfvARB(GLuint index, GLenum pname, GLfloat *params)
-{
-    CRContext *g = GetCurrentContext();
-
-    if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateGetVertexAttribfvARB(PCRStateTracker pState, GLuint index, GLenum pname, GLfloat *params)
+{
+    CRContext *g = GetCurrentContext(pState);
+
+    if (g->current.inBeginEnd) {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetVertexAttribfvARB called in Begin/End");
         return;
@@ -1041,5 +1040,5 @@
 
     if (index >= CR_MAX_VERTEX_ATTRIBS) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glGetVertexAttribfvARB(index)");
         return;
@@ -1063,19 +1062,19 @@
         break;
     case GL_CURRENT_VERTEX_ATTRIB_ARB:
-        crStateCurrentRecover();
+        crStateCurrentRecover(pState);
         COPY_4V(params , g->current.vertexAttrib[index]);
         break;
     default:
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetVertexAttribfvARB");
-        return;
-    }
-}
-
-
-void STATE_APIENTRY crStateGetVertexAttribivARB(GLuint index, GLenum pname, GLint *params)
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetVertexAttribfvARB");
+        return;
+    }
+}
+
+
+void STATE_APIENTRY crStateGetVertexAttribivARB(PCRStateTracker pState, GLuint index, GLenum pname, GLint *params)
 {
      /* init vars to prevent compiler warnings/errors */
      GLfloat floatParams[4] = { 0.0, 0.0, 0.0, 0.0 };
-     crStateGetVertexAttribfvARB(index, pname, floatParams);
+     crStateGetVertexAttribfvARB(pState, index, pname, floatParams);
      params[0] = (GLint) floatParams[0];
      if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
@@ -1087,9 +1086,9 @@
 
 
-void STATE_APIENTRY crStateGetVertexAttribdvARB(GLuint index, GLenum pname, GLdouble *params)
+void STATE_APIENTRY crStateGetVertexAttribdvARB(PCRStateTracker pState, GLuint index, GLenum pname, GLdouble *params)
 {
      /* init vars to prevent compiler warnings/errors */
      GLfloat floatParams[4] = { 0.0, 0.0, 0.0, 0.0 };
-     crStateGetVertexAttribfvARB(index, pname, floatParams);
+     crStateGetVertexAttribfvARB(pState, index, pname, floatParams);
      params[0] = floatParams[0];
      if (pname == GL_CURRENT_VERTEX_ATTRIB_ARB) {
@@ -1107,14 +1106,14 @@
  */
 
-void STATE_APIENTRY crStateProgramNamedParameter4fNV(GLuint id, GLsizei len, const GLubyte *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateProgramNamedParameter4fNV(PCRStateTracker pState, GLuint id, GLsizei len, const GLubyte *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
     CRProgram *prog;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glProgramNamedParameterfNV called in Begin/End");
         return;
@@ -1123,5 +1122,5 @@
     prog = (CRProgram *) crHashtableSearch(p->programHash, id);
     if (!prog) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glProgramNamedParameterNV(bad id %d)", id);
         return;
@@ -1129,5 +1128,5 @@
 
     if (prog->target != GL_FRAGMENT_PROGRAM_NV) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glProgramNamedParameterNV(target)");
         return;
@@ -1140,25 +1139,25 @@
 
 
-void STATE_APIENTRY crStateProgramNamedParameter4dNV(GLuint id, GLsizei len, const GLubyte *name, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
-{
-    crStateProgramNamedParameter4fNV(id, len, name, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-
-void STATE_APIENTRY crStateProgramNamedParameter4fvNV(GLuint id, GLsizei len, const GLubyte *name, const GLfloat v[])
-{
-    crStateProgramNamedParameter4fNV(id, len, name, v[0], v[1], v[2], v[3]);
-}
-
-
-void STATE_APIENTRY crStateProgramNamedParameter4dvNV(GLuint id, GLsizei len, const GLubyte *name, const GLdouble v[])
-{
-    crStateProgramNamedParameter4fNV(id, len, name, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
-}
-
-
-void STATE_APIENTRY crStateGetProgramNamedParameterfvNV(GLuint id, GLsizei len, const GLubyte *name, GLfloat *params)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateProgramNamedParameter4dNV(PCRStateTracker pState, GLuint id, GLsizei len, const GLubyte *name, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
+{
+    crStateProgramNamedParameter4fNV(pState, id, len, name, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+
+void STATE_APIENTRY crStateProgramNamedParameter4fvNV(PCRStateTracker pState, GLuint id, GLsizei len, const GLubyte *name, const GLfloat v[])
+{
+    crStateProgramNamedParameter4fNV(pState, id, len, name, v[0], v[1], v[2], v[3]);
+}
+
+
+void STATE_APIENTRY crStateProgramNamedParameter4dvNV(PCRStateTracker pState, GLuint id, GLsizei len, const GLubyte *name, const GLdouble v[])
+{
+    crStateProgramNamedParameter4fNV(pState, id, len, name, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
+}
+
+
+void STATE_APIENTRY crStateGetProgramNamedParameterfvNV(PCRStateTracker pState, GLuint id, GLsizei len, const GLubyte *name, GLfloat *params)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
     const CRProgram *prog;
@@ -1166,5 +1165,5 @@
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramNamedParameterfNV called in Begin/End");
         return;
@@ -1173,5 +1172,5 @@
     prog = (const CRProgram *) crHashtableSearch(p->programHash, id);
     if (!prog) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramNamedParameterNV(bad id)");
         return;
@@ -1179,5 +1178,5 @@
 
     if (prog->target != GL_FRAGMENT_PROGRAM_NV) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramNamedParameterNV(target)");
         return;
@@ -1186,5 +1185,5 @@
     value = GetProgramSymbol(prog, (const char *)name, len);
     if (!value) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glGetProgramNamedParameterNV(name)");
         return;
@@ -1198,8 +1197,8 @@
 
 
-void STATE_APIENTRY crStateGetProgramNamedParameterdvNV(GLuint id, GLsizei len, const GLubyte *name, GLdouble *params)
+void STATE_APIENTRY crStateGetProgramNamedParameterdvNV(PCRStateTracker pState, GLuint id, GLsizei len, const GLubyte *name, GLdouble *params)
 {
     GLfloat floatParams[4];
-    crStateGetProgramNamedParameterfvNV(id, len, name, floatParams);
+    crStateGetProgramNamedParameterfvNV(pState, id, len, name, floatParams);
     params[0] = floatParams[0];
     params[1] = floatParams[1];
@@ -1209,27 +1208,27 @@
 
 
-void STATE_APIENTRY crStateProgramLocalParameter4dARB(GLenum target, GLuint index, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
-{
-    crStateProgramLocalParameter4fARB(target, index, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-
-void STATE_APIENTRY crStateProgramLocalParameter4dvARB(GLenum target, GLuint index, const GLdouble *params)
-{
-    crStateProgramLocalParameter4fARB(target, index, (GLfloat) params[0], (GLfloat) params[1],
+void STATE_APIENTRY crStateProgramLocalParameter4dARB(PCRStateTracker pState, GLenum target, GLuint index, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
+{
+    crStateProgramLocalParameter4fARB(pState, target, index, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+
+void STATE_APIENTRY crStateProgramLocalParameter4dvARB(PCRStateTracker pState, GLenum target, GLuint index, const GLdouble *params)
+{
+    crStateProgramLocalParameter4fARB(pState, target, index, (GLfloat) params[0], (GLfloat) params[1],
                                                                         (GLfloat) params[2], (GLfloat) params[3]);
 }
 
 
-void STATE_APIENTRY crStateProgramLocalParameter4fARB(GLenum target, GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateProgramLocalParameter4fARB(PCRStateTracker pState, GLenum target, GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
     CRProgram *prog;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glProgramLocalParameterARB called in Begin/End");
         return;
@@ -1238,5 +1237,5 @@
     if (target == GL_FRAGMENT_PROGRAM_ARB || target == GL_FRAGMENT_PROGRAM_NV) {
         if (index >= CR_MAX_FRAGMENT_PROGRAM_LOCAL_PARAMS) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glProgramLocalParameterARB(index)");
             return;
@@ -1246,5 +1245,5 @@
     else if (target == GL_VERTEX_PROGRAM_ARB) {
         if (index >= CR_MAX_VERTEX_PROGRAM_LOCAL_PARAMS) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glProgramLocalParameterARB(index)");
             return;
@@ -1253,5 +1252,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glProgramLocalParameterARB(target)");
         return;
@@ -1270,18 +1269,18 @@
 
 
-void STATE_APIENTRY crStateProgramLocalParameter4fvARB(GLenum target, GLuint index, const GLfloat *params)
-{
-    crStateProgramLocalParameter4fARB(target, index, params[0], params[1], params[2], params[3]);
-}
-
-
-void STATE_APIENTRY crStateGetProgramLocalParameterfvARB(GLenum target, GLuint index, GLfloat *params)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateProgramLocalParameter4fvARB(PCRStateTracker pState, GLenum target, GLuint index, const GLfloat *params)
+{
+    crStateProgramLocalParameter4fARB(pState, target, index, params[0], params[1], params[2], params[3]);
+}
+
+
+void STATE_APIENTRY crStateGetProgramLocalParameterfvARB(PCRStateTracker pState, GLenum target, GLuint index, GLfloat *params)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRProgramState *p = &(g->program);
     const CRProgram *prog = NULL;
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramLocalParameterARB called in Begin/End");
         return;
@@ -1291,5 +1290,5 @@
         prog = p->currentFragmentProgram;
         if (index >= g->limits.maxFragmentProgramLocalParams) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glGetProgramLocalParameterARB(index)");
             return;
@@ -1303,5 +1302,5 @@
         prog = p->currentVertexProgram;
         if (index >= g->limits.maxVertexProgramLocalParams) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glGetProgramLocalParameterARB(index)");
             return;
@@ -1309,10 +1308,10 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetProgramLocalParameterARB(target)");
         return;
     }
     if (!prog) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramLocalParameterARB(no program)");
         return;
@@ -1320,5 +1319,5 @@
 
     if (!prog) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramLocalParameterARB(no program)");
         return;
@@ -1334,8 +1333,8 @@
 
 
-void STATE_APIENTRY crStateGetProgramLocalParameterdvARB(GLenum target, GLuint index, GLdouble *params)
+void STATE_APIENTRY crStateGetProgramLocalParameterdvARB(PCRStateTracker pState, GLenum target, GLuint index, GLdouble *params)
 {
     GLfloat floatParams[4];
-    crStateGetProgramLocalParameterfvARB(target, index, floatParams);
+    crStateGetProgramLocalParameterfvARB(pState, target, index, floatParams);
     params[0] = floatParams[0];
     params[1] = floatParams[1];
@@ -1346,12 +1345,12 @@
 
 
-void STATE_APIENTRY crStateGetProgramivARB(GLenum target, GLenum pname, GLint *params)
+void STATE_APIENTRY crStateGetProgramivARB(PCRStateTracker pState, GLenum target, GLenum pname, GLint *params)
 {
     CRProgram *prog;
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-
-    if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+
+    if (g->current.inBeginEnd) {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramivARB called in Begin/End");
         return;
@@ -1365,5 +1364,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetProgramivARB(target)");
         return;
@@ -1502,5 +1501,5 @@
     case GL_PROGRAM_ALU_INSTRUCTIONS_ARB:
         if (target != GL_FRAGMENT_PROGRAM_ARB || !g->extensions.ARB_fragment_program) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "crStateGetProgramivARB(target or pname)");
             return;
@@ -1510,5 +1509,5 @@
     case GL_PROGRAM_TEX_INSTRUCTIONS_ARB:
         if (target != GL_FRAGMENT_PROGRAM_ARB || !g->extensions.ARB_fragment_program) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "crStateGetProgramivARB(target or pname)");
             return;
@@ -1518,5 +1517,5 @@
     case GL_PROGRAM_TEX_INDIRECTIONS_ARB:
         if (target != GL_FRAGMENT_PROGRAM_ARB || !g->extensions.ARB_fragment_program) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "crStateGetProgramivARB(target or pname)");
             return;
@@ -1527,5 +1526,5 @@
         /* XXX same as GL_PROGRAM_ALU_INSTRUCTIONS_ARB? */
         if (target != GL_FRAGMENT_PROGRAM_ARB || !g->extensions.ARB_fragment_program) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "crStateGetProgramivARB(target or pname)");
             return;
@@ -1536,5 +1535,5 @@
         /* XXX same as GL_PROGRAM_ALU_INSTRUCTIONS_ARB? */
         if (target != GL_FRAGMENT_PROGRAM_ARB || !g->extensions.ARB_fragment_program) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "crStateGetProgramivARB(target or pname)");
             return;
@@ -1544,5 +1543,5 @@
     case GL_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB:
         if (target != GL_FRAGMENT_PROGRAM_ARB || !g->extensions.ARB_fragment_program) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "crStateGetProgramivARB(target or pname)");
             return;
@@ -1553,5 +1552,5 @@
     case GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB:
         if (target != GL_FRAGMENT_PROGRAM_ARB || !g->extensions.ARB_fragment_program) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "crStateGetProgramivARB(target or pname)");
             return;
@@ -1562,5 +1561,5 @@
     case GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB:
         if (target != GL_FRAGMENT_PROGRAM_ARB || !g->extensions.ARB_fragment_program) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "crStateGetProgramivARB(target or pname)");
             return;
@@ -1571,5 +1570,5 @@
     case GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB:
         if (target != GL_FRAGMENT_PROGRAM_ARB || !g->extensions.ARB_fragment_program) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "crStateGetProgramivARB(target or pname)");
             return;
@@ -1578,5 +1577,5 @@
         break;
     default:
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "crStateGetProgramivARB(pname)");
         return;
@@ -1586,13 +1585,13 @@
 
 /* XXX maybe move these two functions into state_client.c? */
-void STATE_APIENTRY crStateDisableVertexAttribArrayARB(GLuint index)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateDisableVertexAttribArrayARB(PCRStateTracker pState, GLuint index)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
     if (index >= g->limits.maxVertexProgramAttribs) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glDisableVertexAttribArrayARB(index)");
         return;
@@ -1604,13 +1603,13 @@
 
 
-void STATE_APIENTRY crStateEnableVertexAttribArrayARB(GLuint index)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateEnableVertexAttribArrayARB(PCRStateTracker pState, GLuint index)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRClientBits *cb = &(sb->client);
 
     if (index >= g->limits.maxVertexProgramAttribs) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glEnableVertexAttribArrayARB(index)");
         return;
@@ -1622,8 +1621,8 @@
 
 
-void STATE_APIENTRY crStateGetProgramEnvParameterdvARB(GLenum target, GLuint index, GLdouble *params)
+void STATE_APIENTRY crStateGetProgramEnvParameterdvARB(PCRStateTracker pState, GLenum target, GLuint index, GLdouble *params)
 {
      GLfloat fparams[4];
-     crStateGetProgramEnvParameterfvARB(target, index, fparams);
+     crStateGetProgramEnvParameterfvARB(pState, target, index, fparams);
      params[0] = fparams[0];
      params[1] = fparams[1];
@@ -1632,11 +1631,11 @@
 }
 
-void STATE_APIENTRY crStateGetProgramEnvParameterfvARB(GLenum target, GLuint index, GLfloat *params)
-{
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-
-    if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateGetProgramEnvParameterfvARB(PCRStateTracker pState, GLenum target, GLuint index, GLfloat *params)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+
+    if (g->current.inBeginEnd) {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetProgramEnvParameterARB called in Begin/End");
         return;
@@ -1645,5 +1644,5 @@
     if (target == GL_FRAGMENT_PROGRAM_ARB || target == GL_FRAGMENT_PROGRAM_NV) {
         if (index >= g->limits.maxFragmentProgramEnvParams) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glGetProgramEnvParameterARB(index)");
             return;
@@ -1660,5 +1659,5 @@
              ) {
         if (index >= g->limits.maxVertexProgramEnvParams) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glGetProgramEnvParameterARB(index)");
             return;
@@ -1670,5 +1669,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetProgramEnvParameterARB(target)");
         return;
@@ -1677,23 +1676,23 @@
 
 
-void STATE_APIENTRY crStateProgramEnvParameter4dARB(GLenum target, GLuint index, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
-{
-     crStateProgramEnvParameter4fARB(target, index, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-void STATE_APIENTRY crStateProgramEnvParameter4dvARB(GLenum target, GLuint index, const GLdouble *params)
-{
-     crStateProgramEnvParameter4fARB(target, index, (GLfloat) params[0], (GLfloat) params[1], (GLfloat) params[2], (GLfloat) params[3]);
-}
-
-void STATE_APIENTRY crStateProgramEnvParameter4fARB(GLenum target, GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-    CRContext *g = GetCurrentContext();
-    CRProgramState *p = &(g->program);
-    CRStateBits *sb = GetCurrentBits();
+void STATE_APIENTRY crStateProgramEnvParameter4dARB(PCRStateTracker pState, GLenum target, GLuint index, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
+{
+     crStateProgramEnvParameter4fARB(pState, target, index, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+void STATE_APIENTRY crStateProgramEnvParameter4dvARB(PCRStateTracker pState, GLenum target, GLuint index, const GLdouble *params)
+{
+     crStateProgramEnvParameter4fARB(pState, target, index, (GLfloat) params[0], (GLfloat) params[1], (GLfloat) params[2], (GLfloat) params[3]);
+}
+
+void STATE_APIENTRY crStateProgramEnvParameter4fARB(PCRStateTracker pState, GLenum target, GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRProgramState *p = &(g->program);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRProgramBits *pb = &(sb->program);
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glProgramEnvParameterARB called in Begin/End");
         return;
@@ -1702,5 +1701,5 @@
     if (target == GL_FRAGMENT_PROGRAM_ARB || target == GL_FRAGMENT_PROGRAM_NV) {
         if (index >= g->limits.maxFragmentProgramEnvParams) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glProgramEnvParameterARB(index)");
             return;
@@ -1719,5 +1718,5 @@
              ) {
         if (index >= g->limits.maxVertexProgramEnvParams) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glProgramEnvParameterARB(index)");
             return;
@@ -1731,5 +1730,5 @@
     }
     else {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glProgramEnvParameterARB(target)");
         return;
@@ -1739,7 +1738,7 @@
 }
 
-void STATE_APIENTRY crStateProgramEnvParameter4fvARB(GLenum target, GLuint index, const GLfloat *params)
-{
-     crStateProgramEnvParameter4fARB(target, index, params[0], params[1], params[2], params[3]);
+void STATE_APIENTRY crStateProgramEnvParameter4fvARB(PCRStateTracker pState, GLenum target, GLuint index, const GLfloat *params)
+{
+     crStateProgramEnvParameter4fARB(pState, target, index, params[0], params[1], params[2], params[3]);
 }
 
@@ -1751,5 +1750,5 @@
 {
     CRProgramState *p = &(ctx->program);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
     CRProgramBits *pb = &(sb->program);
     GLuint i;
@@ -1819,4 +1818,5 @@
                                      CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRProgramState *from = &(fromCtx->program);
     CRProgramState *to = &(toCtx->program);
@@ -1824,4 +1824,6 @@
     CRbitvalue nbitID[CR_MAX_BITARRAY];
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     CRASSERT(from->currentVertexProgram);
     CRASSERT(to->currentVertexProgram);
@@ -1837,6 +1839,6 @@
         CRProgram *toProg = to->currentVertexProgram;
 
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->vpEnabled != to->vpEnabled) {
             if (toProg->isARBprogram)
@@ -1860,6 +1862,6 @@
     if (CHECKDIRTY(b->fpEnable, bitID)) {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->fpEnabled != to->fpEnabled) {
             able[to->fpEnabled](GL_FRAGMENT_PROGRAM_NV);
@@ -1879,5 +1881,5 @@
                 if (from->TrackMatrix[i] != to->TrackMatrix[i] ||
                         from->TrackMatrixTransform[i] != to->TrackMatrixTransform[i])   {
-                    diff_api.TrackMatrixNV(GL_VERTEX_PROGRAM_NV, i * 4,
+                    pState->diff_api.TrackMatrixNV(GL_VERTEX_PROGRAM_NV, i * 4,
                                                                  to->TrackMatrix[i],
                                                                  to->TrackMatrixTransform[i]);
@@ -1898,7 +1900,7 @@
             if (fromProg->id != toProg->id) {
                 if (toProg->isARBprogram)
-                    diff_api.BindProgramARB(GL_VERTEX_PROGRAM_ARB, toProg->id);
+                    pState->diff_api.BindProgramARB(GL_VERTEX_PROGRAM_ARB, toProg->id);
                 else
-                    diff_api.BindProgramNV(GL_VERTEX_PROGRAM_NV, toProg->id);
+                    pState->diff_api.BindProgramNV(GL_VERTEX_PROGRAM_NV, toProg->id);
                 from->currentVertexProgram = toProg;
             }
@@ -1910,8 +1912,8 @@
             if (CHECKDIRTY(toProg->dirtyProgram, bitID)) {
                 if (toProg->isARBprogram) {
-                    diff_api.ProgramStringARB( GL_VERTEX_PROGRAM_ARB, toProg->format, toProg->length, toProg->string );
+                    pState->diff_api.ProgramStringARB( GL_VERTEX_PROGRAM_ARB, toProg->format, toProg->length, toProg->string );
                 }
                 else {
-                    diff_api.LoadProgramNV( GL_VERTEX_PROGRAM_NV, toProg->id, toProg->length, toProg->string );
+                    pState->diff_api.LoadProgramNV( GL_VERTEX_PROGRAM_NV, toProg->id, toProg->length, toProg->string );
                 }
                 CLEARDIRTY(toProg->dirtyProgram, nbitID);
@@ -1923,8 +1925,8 @@
                     if (CHECKDIRTY(b->vertexEnvParameter[i], bitID)) {
                         if (toProg->isARBprogram)
-                            diff_api.ProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i,
+                            pState->diff_api.ProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i,
                                                                                                  to->vertexParameters[i]);
                         else
-                            diff_api.ProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, i,
+                            pState->diff_api.ProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, i,
                                                                                          to->vertexParameters[i]);
                         if (fromProg) {
@@ -1943,7 +1945,7 @@
                     if (CHECKDIRTY(toProg->dirtyParam[i], bitID)) {
                         if (toProg->isARBprogram)
-                            diff_api.ProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i, toProg->parameters[i]);
+                            pState->diff_api.ProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i, toProg->parameters[i]);
                         else
-                            diff_api.ProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_NV, i, toProg->parameters[i]);
+                            pState->diff_api.ProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_NV, i, toProg->parameters[i]);
                         CLEARDIRTY(toProg->dirtyParam[i], nbitID);
                     }
@@ -1963,5 +1965,5 @@
         if (CHECKDIRTY(b->fpBinding, bitID)) {
             if (fromProg->id != toProg->id) {
-                diff_api.BindProgramNV(GL_FRAGMENT_PROGRAM_NV, toProg->id);
+                pState->diff_api.BindProgramNV(GL_FRAGMENT_PROGRAM_NV, toProg->id);
                 from->currentFragmentProgram = toProg;
             }
@@ -1972,5 +1974,5 @@
             /* fragment program text */
             if (CHECKDIRTY(toProg->dirtyProgram, bitID)) {
-                diff_api.LoadProgramNV( GL_FRAGMENT_PROGRAM_NV, toProg->id,
+                pState->diff_api.LoadProgramNV( GL_FRAGMENT_PROGRAM_NV, toProg->id,
                                                                 toProg->length, toProg->string );
                 CLEARDIRTY(toProg->dirtyProgram, nbitID);
@@ -1981,5 +1983,5 @@
                 for (i = 0; i < toCtx->limits.maxFragmentProgramEnvParams; i++) {
                     if (CHECKDIRTY(b->fragmentEnvParameter[i], bitID)) {
-                        diff_api.ProgramParameter4fvNV(GL_FRAGMENT_PROGRAM_NV, i,
+                        pState->diff_api.ProgramParameter4fvNV(GL_FRAGMENT_PROGRAM_NV, i,
                                                                                      to->fragmentParameters[i]);
                         if (fromProg) {
@@ -1999,5 +2001,5 @@
                     if (CHECKDIRTY(symbol->dirty, bitID)) {
                         GLint len = crStrlen(symbol->name);
-                        diff_api.ProgramNamedParameter4fvNV(toProg->id, len,
+                        pState->diff_api.ProgramNamedParameter4fvNV(toProg->id, len,
                                                             (const GLubyte *) symbol->name,
                                                             symbol->value);
@@ -2017,5 +2019,5 @@
                 for (i = 0; i < CR_MAX_FRAGMENT_PROGRAM_LOCAL_PARAMS; i++) {
                     if (CHECKDIRTY(toProg->dirtyParam[i], bitID)) {
-                        diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_NV, i,
+                        pState->diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_NV, i,
                                                                                                  toProg->parameters[i]);
                         if (fromProg) {
@@ -2035,5 +2037,5 @@
         if (CHECKDIRTY(b->fpBinding, bitID)) {
             if (fromProg->id != toProg->id) {
-                diff_api.BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, toProg->id);
+                pState->diff_api.BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, toProg->id);
                 from->currentFragmentProgram = toProg;
             }
@@ -2044,5 +2046,5 @@
             /* fragment program text */
             if (CHECKDIRTY(toProg->dirtyProgram, bitID)) {
-                diff_api.ProgramStringARB( GL_FRAGMENT_PROGRAM_ARB, toProg->format,
+                pState->diff_api.ProgramStringARB( GL_FRAGMENT_PROGRAM_ARB, toProg->format,
                                                                          toProg->length, toProg->string );
                 CLEARDIRTY(toProg->dirtyProgram, nbitID);
@@ -2053,5 +2055,5 @@
                 for (i = 0; i < toCtx->limits.maxFragmentProgramEnvParams; i++) {
                     if (CHECKDIRTY(b->fragmentEnvParameter[i], bitID)) {
-                        diff_api.ProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i,
+                        pState->diff_api.ProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i,
                                                                                              to->fragmentParameters[i]);
                         if (fromProg) {
@@ -2069,5 +2071,5 @@
                 for (i = 0; i < CR_MAX_FRAGMENT_PROGRAM_LOCAL_PARAMS; i++) {
                     if (CHECKDIRTY(toProg->dirtyParam[i], bitID)) {
-                        diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i,
+                        pState->diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i,
                                                                                                  toProg->parameters[i]);
                         if (fromProg) {
@@ -2090,4 +2092,5 @@
                                          CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRProgramState *from = &(fromCtx->program);
     CRProgramState *to = &(toCtx->program);
@@ -2096,4 +2099,5 @@
     GLenum whichVert = fromCtx->extensions.ARB_vertex_program && toCtx->extensions.ARB_vertex_program  ? GL_VERTEX_PROGRAM_ARB : GL_VERTEX_PROGRAM_NV;
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
 
     for (j=0;j<CR_MAX_BITARRAY;j++)
@@ -2103,6 +2107,6 @@
     if (CHECKDIRTY(b->vpEnable, bitID)) {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->vpEnabled != to->vpEnabled) {
             able[to->vpEnabled](whichVert);
@@ -2120,6 +2124,6 @@
     if (CHECKDIRTY(b->fpEnable, bitID)) {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->fpEnabled != to->fpEnabled) {
             able[to->fpEnabled](GL_FRAGMENT_PROGRAM_NV);
@@ -2136,5 +2140,5 @@
             if (CHECKDIRTY(b->trackMatrix[i], bitID))   {
                 if (from->TrackMatrix[i] != to->TrackMatrix[i]) {
-                    diff_api.TrackMatrixNV(GL_VERTEX_PROGRAM_NV, i * 4,
+                    pState->diff_api.TrackMatrixNV(GL_VERTEX_PROGRAM_NV, i * 4,
                                                                  to->TrackMatrix[i],
                                                                  to->TrackMatrixTransform[i]);
@@ -2152,7 +2156,7 @@
             if (fromProg->id != toProg->id) {
                 if (toProg->isARBprogram)
-                    diff_api.BindProgramARB(GL_VERTEX_PROGRAM_ARB, toProg->id);
+                    pState->diff_api.BindProgramARB(GL_VERTEX_PROGRAM_ARB, toProg->id);
                 else
-                    diff_api.BindProgramNV(GL_VERTEX_PROGRAM_NV, toProg->id);
+                    pState->diff_api.BindProgramNV(GL_VERTEX_PROGRAM_NV, toProg->id);
             }
             DIRTY(b->vpBinding, nbitID);
@@ -2163,7 +2167,7 @@
             if (CHECKDIRTY(toProg->dirtyProgram, bitID)) {
                 if (toProg->isARBprogram)
-                    diff_api.ProgramStringARB(GL_VERTEX_PROGRAM_ARB, toProg->format, toProg->length, toProg->string);
+                    pState->diff_api.ProgramStringARB(GL_VERTEX_PROGRAM_ARB, toProg->format, toProg->length, toProg->string);
                 else
-                    diff_api.LoadProgramNV(GL_VERTEX_PROGRAM_NV, toProg->id, toProg->length, toProg->string);
+                    pState->diff_api.LoadProgramNV(GL_VERTEX_PROGRAM_NV, toProg->id, toProg->length, toProg->string);
 
                 DIRTY(toProg->dirtyProgram, nbitID);
@@ -2175,7 +2179,7 @@
                     if (CHECKDIRTY(b->vertexEnvParameter[i], bitID)) {
                         if (toProg->isARBprogram)
-                            diff_api.ProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i, to->vertexParameters[i]);
+                            pState->diff_api.ProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i, to->vertexParameters[i]);
                         else
-                            diff_api.ProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, i, to->vertexParameters[i]);
+                            pState->diff_api.ProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV, i, to->vertexParameters[i]);
 
                         DIRTY(b->vertexEnvParameter[i], nbitID);
@@ -2192,7 +2196,7 @@
 
                         if (toProg->isARBprogram)
-                            diff_api.ProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i, toProg->parameters[i]);
+                            pState->diff_api.ProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i, toProg->parameters[i]);
                         else
-                            diff_api.ProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_NV, i, toProg->parameters[i]);
+                            pState->diff_api.ProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_NV, i, toProg->parameters[i]);
                     }
                 }
@@ -2211,5 +2215,5 @@
         if (CHECKDIRTY(b->fpBinding, bitID)) {
             if (fromProg->id != toProg->id) {
-                diff_api.BindProgramNV(GL_FRAGMENT_PROGRAM_NV, toProg->id);
+                pState->diff_api.BindProgramNV(GL_FRAGMENT_PROGRAM_NV, toProg->id);
             }
             DIRTY(b->fpBinding, nbitID);
@@ -2219,5 +2223,5 @@
             /* fragment program text */
             if (CHECKDIRTY(toProg->dirtyProgram, bitID)) {
-                diff_api.LoadProgramNV(GL_FRAGMENT_PROGRAM_NV, toProg->id, toProg->length, toProg->string);
+                pState->diff_api.LoadProgramNV(GL_FRAGMENT_PROGRAM_NV, toProg->id, toProg->length, toProg->string);
                 DIRTY(toProg->dirtyProgram, nbitID);
             }
@@ -2227,5 +2231,5 @@
                 for (i = 0; i < toCtx->limits.maxFragmentProgramEnvParams; i++) {
                     if (CHECKDIRTY(b->fragmentEnvParameter[i], bitID)) {
-                        diff_api.ProgramParameter4fvNV(GL_FRAGMENT_PROGRAM_NV, i,
+                        pState->diff_api.ProgramParameter4fvNV(GL_FRAGMENT_PROGRAM_NV, i,
                                                        to->fragmentParameters[i]);
                         DIRTY(b->fragmentEnvParameter[i], nbitID);
@@ -2241,5 +2245,5 @@
                     if (CHECKDIRTY(symbol->dirty, bitID)) {
                         GLint len = crStrlen(symbol->name);
-                        diff_api.ProgramNamedParameter4fvNV(toProg->id, len,
+                        pState->diff_api.ProgramNamedParameter4fvNV(toProg->id, len,
                                                             (const GLubyte *) symbol->name,
                                                             symbol->value);
@@ -2255,7 +2259,7 @@
                     if (CHECKDIRTY(toProg->dirtyParam[i], bitID)) {
                         if (toProg->isARBprogram)
-                            diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, toProg->parameters[i]);
+                            pState->diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, toProg->parameters[i]);
                         else
-                            diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_NV, i, toProg->parameters[i]);
+                            pState->diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_NV, i, toProg->parameters[i]);
                     }
                 }
@@ -2270,5 +2274,5 @@
         if (CHECKDIRTY(b->fpBinding, bitID)) {
             if (fromProg->id != toProg->id) {
-                diff_api.BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, toProg->id);
+                pState->diff_api.BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, toProg->id);
             }
             DIRTY(b->fpBinding, nbitID);
@@ -2278,5 +2282,5 @@
             /* fragment program text */
             if (CHECKDIRTY(toProg->dirtyProgram, bitID)) {
-                diff_api.ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, toProg->format, toProg->length, toProg->string);
+                pState->diff_api.ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, toProg->format, toProg->length, toProg->string);
                 DIRTY(toProg->dirtyProgram, nbitID);
             }
@@ -2286,5 +2290,5 @@
                 for (i = 0; i < toCtx->limits.maxFragmentProgramEnvParams; i++) {
                     if (CHECKDIRTY(b->fragmentEnvParameter[i], bitID)) {
-                        diff_api.ProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, to->fragmentParameters[i]);
+                        pState->diff_api.ProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, to->fragmentParameters[i]);
                         DIRTY(b->fragmentEnvParameter[i], nbitID);
                     }
@@ -2297,5 +2301,5 @@
                 for (i = 0; i < CR_MAX_FRAGMENT_PROGRAM_LOCAL_PARAMS; i++) {
                     if (CHECKDIRTY(toProg->dirtyParam[i], bitID)) {
-                        diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, toProg->parameters[i]);
+                        pState->diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, toProg->parameters[i]);
                         DIRTY(toProg->dirtyParam[i], nbitID);
                     }
@@ -2322,4 +2326,5 @@
 {
     CRContext *pContext = (CRContext *) pCtx;
+    PCRStateTracker pState = pContext->pStateTracker;
     CRProgram *pProgram = (CRProgram *) pProg;
     uint32_t i;
@@ -2328,6 +2333,6 @@
     if (pProgram->isARBprogram)
     {
-        diff_api.BindProgramARB(pProgram->target, pProgram->id);
-        diff_api.ProgramStringARB(pProgram->target, pProgram->format, pProgram->length, pProgram->string);
+        pState->diff_api.BindProgramARB(pProgram->target, pProgram->id);
+        pState->diff_api.ProgramStringARB(pProgram->target, pProgram->format, pProgram->length, pProgram->string);
 
         if (GL_VERTEX_PROGRAM_ARB == pProgram->target)
@@ -2336,10 +2341,10 @@
             for (i = 0; i < pContext->limits.maxVertexProgramEnvParams; i++) 
             {
-                diff_api.ProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i, pContext->program.vertexParameters[i]);
+                pState->diff_api.ProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i, pContext->program.vertexParameters[i]);
             }
             /* vertex program local parameters */
             for (i = 0; i < pContext->limits.maxVertexProgramLocalParams; i++) 
             {
-                diff_api.ProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i, pProgram->parameters[i]);
+                pState->diff_api.ProgramLocalParameter4fvARB(GL_VERTEX_PROGRAM_ARB, i, pProgram->parameters[i]);
             }
         }
@@ -2349,10 +2354,10 @@
             for (i = 0; i < pContext->limits.maxFragmentProgramEnvParams; i++) 
             {
-                diff_api.ProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, pContext->program.fragmentParameters[i]);
+                pState->diff_api.ProgramEnvParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, pContext->program.fragmentParameters[i]);
             }
             /* vertex program local parameters */
             for (i = 0; i < CR_MAX_FRAGMENT_PROGRAM_LOCAL_PARAMS; i++) 
             {
-                diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, pProgram->parameters[i]);
+                pState->diff_api.ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, pProgram->parameters[i]);
             }
         }
@@ -2364,5 +2369,5 @@
     else
     {
-        diff_api.BindProgramNV(pProgram->target, pProgram->id);
+        pState->diff_api.BindProgramNV(pProgram->target, pProgram->id);
     }
 }
@@ -2370,4 +2375,5 @@
 void crStateDiffAllPrograms(CRContext *g, CRbitvalue *bitID, GLboolean bForceUpdate)
 {
+    PCRStateTracker pState = g->pStateTracker;
     CRProgram *pOrigVP, *pOrigFP;
 
@@ -2382,11 +2388,11 @@
     /* restore original bindings */
     if (pOrigVP->isARBprogram)
-        diff_api.BindProgramARB(pOrigVP->target, pOrigVP->id);
+        pState->diff_api.BindProgramARB(pOrigVP->target, pOrigVP->id);
     else
-        diff_api.BindProgramNV(pOrigVP->target, pOrigVP->id);
+        pState->diff_api.BindProgramNV(pOrigVP->target, pOrigVP->id);
 
     if (pOrigFP->isARBprogram)
-        diff_api.BindProgramARB(pOrigFP->target, pOrigFP->id);
+        pState->diff_api.BindProgramARB(pOrigFP->target, pOrigFP->id);
     else
-        diff_api.BindProgramNV(pOrigFP->target, pOrigFP->id);
-}
+        pState->diff_api.BindProgramNV(pOrigFP->target, pOrigFP->id);
+}
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_rasterpos.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_rasterpos.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_rasterpos.c	(revision 78375)
@@ -17,7 +17,7 @@
  */
 void
-crStateRasterPosUpdate(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-	CRContext *g = GetCurrentContext();
+crStateRasterPosUpdate(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRCurrentState *c = &(g->current);
 	CRTransformState *t = &(g->transform);
@@ -28,5 +28,5 @@
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "RasterPos called in Begin/End");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "RasterPos called in Begin/End");
 		return;
 	}
@@ -35,5 +35,5 @@
 
 	/* update current color, texcoord, etc from the CurrentStatePointers */
-	crStateCurrentRecover();
+	crStateCurrentRecover(pState);
 
 	p.x = x;
@@ -82,11 +82,11 @@
 
 /* As above, but set dirty flags */
-static void RasterPos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-	CRContext *g = GetCurrentContext();
-	CRStateBits *sb = GetCurrentBits();
+static void RasterPos4f(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+{
+	CRContext *g = GetCurrentContext(pState);
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRCurrentBits *cb = &(sb->current);
 
-	crStateRasterPosUpdate(x, y, z, w);
+	crStateRasterPosUpdate(pState, x, y, z, w);
 
 	DIRTY(cb->dirty, g->neg_bitid);
@@ -94,122 +94,122 @@
 }
 
-void STATE_APIENTRY crStateRasterPos2d(GLdouble x, GLdouble y)
-{
-	RasterPos4f((GLfloat) x, (GLfloat) y, 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos2f(GLfloat x, GLfloat y)
-{
-	RasterPos4f(x, y, 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos2i(GLint x, GLint y)
-{
-	RasterPos4f((GLfloat) x, (GLfloat) y, 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos2s(GLshort x, GLshort y)
-{
-	RasterPos4f((GLfloat) x, (GLfloat) y, 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos3d(GLdouble x, GLdouble y, GLdouble z)
-{
-	RasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos3f(GLfloat x, GLfloat y, GLfloat z)
-{
-	RasterPos4f(x, y, z, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos3i(GLint x, GLint y, GLint z)
-{
-	RasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos3s(GLshort x, GLshort y, GLshort z)
-{
-	RasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos4d(GLdouble x, GLdouble y, GLdouble z, GLdouble w)
-{
-	RasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-void STATE_APIENTRY crStateRasterPos4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
-{
-	RasterPos4f(x, y, z, w);
-}
-
-void STATE_APIENTRY crStateRasterPos4i(GLint x, GLint y, GLint z, GLint w)
-{
-	RasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-void STATE_APIENTRY crStateRasterPos4s(GLshort x, GLshort y, GLshort z, GLshort w)
-{
-	RasterPos4f((GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
-}
-
-void STATE_APIENTRY crStateRasterPos2dv(const GLdouble *v)
-{
-	RasterPos4f((GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos2fv(const GLfloat *v)
-{
-	RasterPos4f(v[0], v[1], 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos2iv(const GLint *v)
-{
-	RasterPos4f((GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos2sv(const GLshort *v)
-{
-	RasterPos4f((GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos3dv(const GLdouble *v)
-{
-	RasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos3fv(const GLfloat *v)
-{
-	RasterPos4f(v[0], v[1], v[2], 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos3iv(const GLint *v)
-{
-	RasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos3sv(const GLshort *v)
-{
-	RasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0f);
-}
-
-void STATE_APIENTRY crStateRasterPos4dv(const GLdouble *v)
-{
-	RasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
-}
-
-void STATE_APIENTRY crStateRasterPos4fv(const GLfloat *v)
-{
-	RasterPos4f(v[0], v[1], v[2], v[3]);
-}
-
-void STATE_APIENTRY crStateRasterPos4iv(const GLint *v)
-{
-	RasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
-}
-
-void STATE_APIENTRY crStateRasterPos4sv(const GLshort *v)
-{
-	RasterPos4f((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
+void STATE_APIENTRY crStateRasterPos2d(PCRStateTracker pState, GLdouble x, GLdouble y)
+{
+	RasterPos4f(pState, (GLfloat) x, (GLfloat) y, 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos2f(PCRStateTracker pState, GLfloat x, GLfloat y)
+{
+	RasterPos4f(pState, x, y, 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos2i(PCRStateTracker pState, GLint x, GLint y)
+{
+	RasterPos4f(pState, (GLfloat) x, (GLfloat) y, 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos2s(PCRStateTracker pState, GLshort x, GLshort y)
+{
+	RasterPos4f(pState, (GLfloat) x, (GLfloat) y, 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos3d(PCRStateTracker pState, GLdouble x, GLdouble y, GLdouble z)
+{
+	RasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos3f(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z)
+{
+	RasterPos4f(pState, x, y, z, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos3i(PCRStateTracker pState, GLint x, GLint y, GLint z)
+{
+	RasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos3s(PCRStateTracker pState, GLshort x, GLshort y, GLshort z)
+{
+	RasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos4d(PCRStateTracker pState, GLdouble x, GLdouble y, GLdouble z, GLdouble w)
+{
+	RasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+void STATE_APIENTRY crStateRasterPos4f(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
+{
+	RasterPos4f(pState, x, y, z, w);
+}
+
+void STATE_APIENTRY crStateRasterPos4i(PCRStateTracker pState, GLint x, GLint y, GLint z, GLint w)
+{
+	RasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+void STATE_APIENTRY crStateRasterPos4s(PCRStateTracker pState, GLshort x, GLshort y, GLshort z, GLshort w)
+{
+	RasterPos4f(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z, (GLfloat) w);
+}
+
+void STATE_APIENTRY crStateRasterPos2dv(PCRStateTracker pState, const GLdouble *v)
+{
+	RasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos2fv(PCRStateTracker pState, const GLfloat *v)
+{
+	RasterPos4f(pState, v[0], v[1], 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos2iv(PCRStateTracker pState, const GLint *v)
+{
+	RasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos2sv(PCRStateTracker pState, const GLshort *v)
+{
+	RasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0f, 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos3dv(PCRStateTracker pState, const GLdouble *v)
+{
+	RasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos3fv(PCRStateTracker pState, const GLfloat *v)
+{
+	RasterPos4f(pState, v[0], v[1], v[2], 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos3iv(PCRStateTracker pState, const GLint *v)
+{
+	RasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos3sv(PCRStateTracker pState, const GLshort *v)
+{
+	RasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], 1.0f);
+}
+
+void STATE_APIENTRY crStateRasterPos4dv(PCRStateTracker pState, const GLdouble *v)
+{
+	RasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
+}
+
+void STATE_APIENTRY crStateRasterPos4fv(PCRStateTracker pState, const GLfloat *v)
+{
+	RasterPos4f(pState, v[0], v[1], v[2], v[3]);
+}
+
+void STATE_APIENTRY crStateRasterPos4iv(PCRStateTracker pState, const GLint *v)
+{
+	RasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
+}
+
+void STATE_APIENTRY crStateRasterPos4sv(PCRStateTracker pState, const GLshort *v)
+{
+	RasterPos4f(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2], (GLfloat) v[3]);
 }
 
@@ -219,9 +219,9 @@
 
 static void
-crStateWindowPosUpdate(GLfloat x, GLfloat y, GLfloat z)
-{
-	CRContext *g = GetCurrentContext();
+crStateWindowPosUpdate(PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRCurrentState *c = &(g->current);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRCurrentBits *cb = &(sb->current);
 	int i;
@@ -229,5 +229,5 @@
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "WindowPos called in Begin/End");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "WindowPos called in Begin/End");
 		return;
 	}
@@ -246,82 +246,82 @@
 
 
-void STATE_APIENTRY crStateWindowPos2dARB (GLdouble x, GLdouble y)
-{
-	crStateWindowPosUpdate((GLfloat) x, (GLfloat) y, 0.0);
-}
-
-void STATE_APIENTRY crStateWindowPos2dvARB (const GLdouble *v)
-{
-	crStateWindowPosUpdate((GLfloat) v[0], (GLfloat) v[1], 0.0);
-}
-
-void STATE_APIENTRY crStateWindowPos2fARB (GLfloat x, GLfloat y)
-{
-	crStateWindowPosUpdate(x, y, 0.0);
-}
-
-void STATE_APIENTRY crStateWindowPos2fvARB (const GLfloat *v)
-{
-	crStateWindowPosUpdate(v[0], v[1], 0.0);
-}
-
-void STATE_APIENTRY crStateWindowPos2iARB (GLint x, GLint y)
-{
-	crStateWindowPosUpdate((GLfloat) x, (GLfloat) y, 0.0);
-}
-
-void STATE_APIENTRY crStateWindowPos2ivARB (const GLint *v)
-{
-	crStateWindowPosUpdate((GLfloat) v[0], (GLfloat) v[1], 0.0);
-}
-
-void STATE_APIENTRY crStateWindowPos2sARB (GLshort x, GLshort y)
-{
-	crStateWindowPosUpdate((GLfloat) x, (GLfloat) y, 0.0);
-}
-
-void STATE_APIENTRY crStateWindowPos2svARB (const GLshort *v)
-{
-	crStateWindowPosUpdate((GLfloat) v[0], (GLfloat) v[1], 0.0);
-}
-
-void STATE_APIENTRY crStateWindowPos3dARB (GLdouble x, GLdouble y, GLdouble z)
-{
-	crStateWindowPosUpdate((GLfloat) x, (GLfloat) y, (GLfloat) z);
-}
-
-void STATE_APIENTRY crStateWindowPos3dvARB (const GLdouble *v)
-{
-	crStateWindowPosUpdate((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2]);
-}
-
-void STATE_APIENTRY crStateWindowPos3fARB (GLfloat x, GLfloat y, GLfloat z)
-{
-	crStateWindowPosUpdate(x, y, z);
-}
-
-void STATE_APIENTRY crStateWindowPos3fvARB (const GLfloat *v)
-{
-	crStateWindowPosUpdate(v[0], v[1], v[2]);
-}
-
-void STATE_APIENTRY crStateWindowPos3iARB (GLint x, GLint y, GLint z)
-{
-	crStateWindowPosUpdate((GLfloat) x, (GLfloat) y, (GLfloat) z);
-}
-
-void STATE_APIENTRY crStateWindowPos3ivARB (const GLint *v)
-{
-	crStateWindowPosUpdate((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2]);
-}
-
-void STATE_APIENTRY crStateWindowPos3sARB (GLshort x, GLshort y, GLshort z)
-{
-	crStateWindowPosUpdate((GLfloat) x, (GLfloat) y, (GLfloat) z);
-}
-
-void STATE_APIENTRY crStateWindowPos3svARB (const GLshort *v)
-{
-	crStateWindowPosUpdate((GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2]);
-}
-
+void STATE_APIENTRY crStateWindowPos2dARB (PCRStateTracker pState, GLdouble x, GLdouble y)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) x, (GLfloat) y, 0.0);
+}
+
+void STATE_APIENTRY crStateWindowPos2dvARB (PCRStateTracker pState, const GLdouble *v)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0);
+}
+
+void STATE_APIENTRY crStateWindowPos2fARB (PCRStateTracker pState, GLfloat x, GLfloat y)
+{
+	crStateWindowPosUpdate(pState, x, y, 0.0);
+}
+
+void STATE_APIENTRY crStateWindowPos2fvARB (PCRStateTracker pState, const GLfloat *v)
+{
+	crStateWindowPosUpdate(pState, v[0], v[1], 0.0);
+}
+
+void STATE_APIENTRY crStateWindowPos2iARB (PCRStateTracker pState, GLint x, GLint y)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) x, (GLfloat) y, 0.0);
+}
+
+void STATE_APIENTRY crStateWindowPos2ivARB (PCRStateTracker pState, const GLint *v)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0);
+}
+
+void STATE_APIENTRY crStateWindowPos2sARB (PCRStateTracker pState, GLshort x, GLshort y)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) x, (GLfloat) y, 0.0);
+}
+
+void STATE_APIENTRY crStateWindowPos2svARB (PCRStateTracker pState, const GLshort *v)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) v[0], (GLfloat) v[1], 0.0);
+}
+
+void STATE_APIENTRY crStateWindowPos3dARB (PCRStateTracker pState, GLdouble x, GLdouble y, GLdouble z)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z);
+}
+
+void STATE_APIENTRY crStateWindowPos3dvARB (PCRStateTracker pState, const GLdouble *v)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2]);
+}
+
+void STATE_APIENTRY crStateWindowPos3fARB (PCRStateTracker pState, GLfloat x, GLfloat y, GLfloat z)
+{
+	crStateWindowPosUpdate(pState, x, y, z);
+}
+
+void STATE_APIENTRY crStateWindowPos3fvARB (PCRStateTracker pState, const GLfloat *v)
+{
+	crStateWindowPosUpdate(pState, v[0], v[1], v[2]);
+}
+
+void STATE_APIENTRY crStateWindowPos3iARB (PCRStateTracker pState, GLint x, GLint y, GLint z)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z);
+}
+
+void STATE_APIENTRY crStateWindowPos3ivARB (PCRStateTracker pState, const GLint *v)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2]);
+}
+
+void STATE_APIENTRY crStateWindowPos3sARB (PCRStateTracker pState, GLshort x, GLshort y, GLshort z)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) x, (GLfloat) y, (GLfloat) z);
+}
+
+void STATE_APIENTRY crStateWindowPos3svARB (PCRStateTracker pState, const GLshort *v)
+{
+	crStateWindowPosUpdate(pState, (GLfloat) v[0], (GLfloat) v[1], (GLfloat) v[2]);
+}
+
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_regcombiner.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_regcombiner.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_regcombiner.c	(revision 78375)
@@ -15,5 +15,5 @@
 {
 	CRRegCombinerState *reg = &ctx->regcombiner;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
 	CRRegCombinerBits *rb = &(sb->regcombiner);
 #ifndef CR_NV_register_combiners
@@ -117,9 +117,9 @@
 }
 
-void STATE_APIENTRY crStateCombinerParameterfvNV( GLenum pname, const GLfloat *params )
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateCombinerParameterfvNV(PCRStateTracker pState, GLenum pname, const GLfloat *params )
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRRegCombinerState *r = &(g->regcombiner);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRRegCombinerBits *rb = &(sb->regcombiner);
 
@@ -143,5 +143,5 @@
 			if( *params < 1 || *params > g->limits.maxGeneralCombiners )
 			{
-				crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "CombinerParameter passed invalid NUM_GENERAL_COMBINERS param: %d", (GLint)*params );
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "CombinerParameter passed invalid NUM_GENERAL_COMBINERS param: %d", (GLint)*params );
 				return;
 			}
@@ -154,5 +154,5 @@
 			break;
 		default:
-			crStateError( __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerParameter passed bogus pname: 0x%x", pname );
+			crStateError(pState,  __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerParameter passed bogus pname: 0x%x", pname );
 			return;
 	}
@@ -161,5 +161,5 @@
 }
 
-void STATE_APIENTRY crStateCombinerParameterivNV( GLenum pname, const GLint *params )
+void STATE_APIENTRY crStateCombinerParameterivNV(PCRStateTracker pState, GLenum pname, const GLint *params )
 {
 	GLfloat fparams[4];
@@ -178,8 +178,8 @@
 		*fparams = (GLfloat) *params;
 	}
-	crStateCombinerParameterfvNV( pname, fparams );
-}
-
-void STATE_APIENTRY crStateCombinerParameterfNV( GLenum pname, GLfloat param )
+	crStateCombinerParameterfvNV(pState, pname, fparams );
+}
+
+void STATE_APIENTRY crStateCombinerParameterfNV(PCRStateTracker pState, GLenum pname, GLfloat param )
 {
 	GLfloat fparam[1];
@@ -187,11 +187,11 @@
 	if( pname == GL_CONSTANT_COLOR0_NV || pname == GL_CONSTANT_COLOR1_NV )
 	{
-		crStateError( __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid pname (CONSTANT_COLOR%d) passed to CombinerParameterfNV: 0x%x", (GLint)param-GL_CONSTANT_COLOR0_NV, pname );
-		return;
-	}
-	crStateCombinerParameterfvNV( pname, fparam );
-}
-
-void STATE_APIENTRY crStateCombinerParameteriNV( GLenum pname, GLint param )
+		crStateError(pState,  __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid pname (CONSTANT_COLOR%d) passed to CombinerParameterfNV: 0x%x", (GLint)param-GL_CONSTANT_COLOR0_NV, pname );
+		return;
+	}
+	crStateCombinerParameterfvNV(pState, pname, fparam );
+}
+
+void STATE_APIENTRY crStateCombinerParameteriNV(PCRStateTracker pState, GLenum pname, GLint param )
 {
 	GLfloat fparam[1];
@@ -199,35 +199,35 @@
 	if( pname == GL_CONSTANT_COLOR0_NV || pname == GL_CONSTANT_COLOR1_NV )
 	{
-		crStateError( __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid pname (CONSTANT_COLOR%d) passed to CombinerParameteriNV: 0x%x", param-GL_CONSTANT_COLOR0_NV, pname );
-		return;
-	}
-	crStateCombinerParameterfvNV( pname, fparam );
-}
-
-void STATE_APIENTRY crStateCombinerInputNV( GLenum stage, GLenum portion, GLenum variable, GLenum input, GLenum mapping, GLenum componentUsage )
-{
-	CRContext *g = GetCurrentContext();
+		crStateError(pState,  __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid pname (CONSTANT_COLOR%d) passed to CombinerParameteriNV: 0x%x", param-GL_CONSTANT_COLOR0_NV, pname );
+		return;
+	}
+	crStateCombinerParameterfvNV(pState, pname, fparam );
+}
+
+void STATE_APIENTRY crStateCombinerInputNV(PCRStateTracker pState, GLenum stage, GLenum portion, GLenum variable, GLenum input, GLenum mapping, GLenum componentUsage )
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRRegCombinerState *r = &(g->regcombiner);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRRegCombinerBits *rb = &(sb->regcombiner);
 
 	if( stage < GL_COMBINER0_NV || stage >= GL_COMBINER0_NV + g->limits.maxGeneralCombiners )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus stage: 0x%x", stage );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus stage: 0x%x", stage );
 		return;
 	}
 	if( input != GL_ZERO && input != GL_CONSTANT_COLOR0_NV && input != GL_CONSTANT_COLOR1_NV && input != GL_FOG && input != GL_PRIMARY_COLOR_NV && input != GL_SECONDARY_COLOR_NV && input != GL_SPARE0_NV && input != GL_SPARE1_NV && ( input < GL_TEXTURE0_ARB || input >= GL_TEXTURE0_ARB + g->limits.maxTextureUnits ))
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus input: 0x%x", input );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus input: 0x%x", input );
 		return;
 	}
 	if( mapping != GL_UNSIGNED_IDENTITY_NV && mapping != GL_UNSIGNED_INVERT_NV && mapping != GL_EXPAND_NORMAL_NV && mapping != GL_EXPAND_NEGATE_NV && mapping != GL_HALF_BIAS_NORMAL_NV && mapping != GL_HALF_BIAS_NEGATE_NV && mapping != GL_SIGNED_IDENTITY_NV && mapping != GL_SIGNED_NEGATE_NV )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus mapping: 0x%x", mapping );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus mapping: 0x%x", mapping );
 		return;
 	}
 	if( componentUsage != GL_RGB && componentUsage != GL_ALPHA && componentUsage != GL_BLUE )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus componentUsage: 0x%x", componentUsage );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus componentUsage: 0x%x", componentUsage );
 		return;
 	}
@@ -235,10 +235,10 @@
 	if(( componentUsage == GL_RGB && portion == GL_ALPHA )||( componentUsage == GL_BLUE && portion == GL_RGB ))
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "Incompatible portion and componentUsage passed to CombinerInputNV: portion = 0x%x, componentUsage = 0x%x", portion, componentUsage );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "Incompatible portion and componentUsage passed to CombinerInputNV: portion = 0x%x, componentUsage = 0x%x", portion, componentUsage );
 		return;
 	}
 	if( componentUsage == GL_ALPHA && input == GL_FOG )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerInputNV can not have input of GL_FOG if componentUsage is GL_ALPHA" );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerInputNV can not have input of GL_FOG if componentUsage is GL_ALPHA" );
 		return;
 	}
@@ -270,5 +270,5 @@
 				break;
 			default:
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus variable: 0x%x", variable );
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus variable: 0x%x", variable );
 				return;
 		}
@@ -299,5 +299,5 @@
 				break;
 			default:
-				crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus variable: 0x%x", variable );
+				crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus variable: 0x%x", variable );
 				return;
 		}
@@ -305,5 +305,5 @@
 	else
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus portion: 0x%x", portion );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerInputNV passed bogus portion: 0x%x", portion );
 		return;
 	}
@@ -313,9 +313,9 @@
 }
 
-void STATE_APIENTRY crStateCombinerOutputNV( GLenum stage, GLenum portion, GLenum abOutput, GLenum cdOutput, GLenum sumOutput, GLenum scale, GLenum bias, GLboolean abDotProduct, GLboolean cdDotProduct, GLboolean muxSum )
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateCombinerOutputNV(PCRStateTracker pState, GLenum stage, GLenum portion, GLenum abOutput, GLenum cdOutput, GLenum sumOutput, GLenum scale, GLenum bias, GLboolean abDotProduct, GLboolean cdDotProduct, GLboolean muxSum )
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRRegCombinerState *r = &(g->regcombiner);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRRegCombinerBits *rb = &(sb->regcombiner);
 
@@ -330,30 +330,30 @@
 	if( stage < GL_COMBINER0_NV || stage >= GL_COMBINER0_NV + g->limits.maxGeneralCombiners )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus stage: 0x%x", stage );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus stage: 0x%x", stage );
 		return;
 	}
 	if( abOutput != GL_DISCARD_NV && abOutput != GL_PRIMARY_COLOR_NV && abOutput != GL_SECONDARY_COLOR_NV && abOutput != GL_SPARE0_NV && abOutput != GL_SPARE1_NV && ( abOutput < GL_TEXTURE0_ARB || abOutput >= g->limits.maxTextureUnits + GL_TEXTURE0_ARB ))
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus abOutput: 0x%x", abOutput );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus abOutput: 0x%x", abOutput );
 		return;
 	}
 	if( cdOutput != GL_DISCARD_NV && cdOutput != GL_PRIMARY_COLOR_NV && cdOutput != GL_SECONDARY_COLOR_NV && cdOutput != GL_SPARE0_NV && cdOutput != GL_SPARE1_NV && ( cdOutput < GL_TEXTURE0_ARB || cdOutput >= g->limits.maxTextureUnits + GL_TEXTURE0_ARB ))
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus cdOutput: 0x%x", cdOutput );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus cdOutput: 0x%x", cdOutput );
 		return;
 	}
 	if( sumOutput != GL_DISCARD_NV && sumOutput != GL_PRIMARY_COLOR_NV && sumOutput != GL_SECONDARY_COLOR_NV && sumOutput != GL_SPARE0_NV && sumOutput != GL_SPARE1_NV && sumOutput != GL_TEXTURE0_ARB && sumOutput != GL_TEXTURE1_ARB )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus sumOutput: 0x%x", sumOutput );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus sumOutput: 0x%x", sumOutput );
 		return;
 	}
 	if( scale != GL_NONE && scale != GL_SCALE_BY_TWO_NV && scale != GL_SCALE_BY_FOUR_NV && scale != GL_SCALE_BY_ONE_HALF_NV )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerOutputNV passed bogus scale: 0x%x", scale );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerOutputNV passed bogus scale: 0x%x", scale );
 		return;
 	}
 	if( bias != GL_NONE && bias != GL_BIAS_BY_NEGATIVE_ONE_HALF_NV )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerOutputNV passed bogus bias: 0x%x", bias );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerOutputNV passed bogus bias: 0x%x", bias );
 		return;
 	}
@@ -361,10 +361,10 @@
 	if( bias == GL_BIAS_BY_NEGATIVE_ONE_HALF_NV && ( scale == GL_SCALE_BY_ONE_HALF_NV || scale == GL_SCALE_BY_FOUR_NV ))
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerOutputNV can't accept bias of -1/2 if scale is by 1/2 or 4" );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerOutputNV can't accept bias of -1/2 if scale is by 1/2 or 4" );
 		return;
 	}
 	if(( abOutput == cdOutput && abOutput != GL_DISCARD_NV )||( abOutput == sumOutput && abOutput != GL_DISCARD_NV )||( cdOutput == sumOutput && cdOutput != GL_DISCARD_NV ))
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerOutputNV register output names must be unique unless discarded: abOutput = 0x%x, cdOutput = 0x%x, sumOutput = 0x%x", abOutput, cdOutput, sumOutput );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerOutputNV register output names must be unique unless discarded: abOutput = 0x%x, cdOutput = 0x%x, sumOutput = 0x%x", abOutput, cdOutput, sumOutput );
 		return;
 	}
@@ -373,10 +373,10 @@
 		if( portion == GL_ALPHA )
 		{
-			crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "CombinerOutputNV can not do Dot Products when portion = GL_ALPHA" );
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "CombinerOutputNV can not do Dot Products when portion = GL_ALPHA" );
 			return;
 		}
 		if( sumOutput != GL_DISCARD_NV )
 		{
-			crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerOutputNV can not do Dot Products when sumOutput is not discarded" );
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "CombinerOutputNV can not do Dot Products when sumOutput is not discarded" );
 			return;
 		}
@@ -408,5 +408,5 @@
 	else
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus portion: 0x%x", portion );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus portion: 0x%x", portion );
 		return;
 	}
@@ -416,24 +416,24 @@
 }
 
-void STATE_APIENTRY crStateFinalCombinerInputNV( GLenum variable, GLenum input, GLenum mapping, GLenum componentUsage )
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateFinalCombinerInputNV(PCRStateTracker pState, GLenum variable, GLenum input, GLenum mapping, GLenum componentUsage )
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRRegCombinerState *r = &(g->regcombiner);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRRegCombinerBits *rb = &(sb->regcombiner);
 
 	if( input != GL_ZERO && input != GL_CONSTANT_COLOR0_NV && input != GL_CONSTANT_COLOR1_NV && input != GL_FOG && input != GL_PRIMARY_COLOR_NV && input != GL_SECONDARY_COLOR_NV && input != GL_SPARE0_NV && input != GL_SPARE1_NV && ( input < GL_TEXTURE0_ARB || input >= GL_TEXTURE0_ARB + g->limits.maxTextureUnits ) && input != GL_E_TIMES_F_NV && input != GL_SPARE0_PLUS_SECONDARY_COLOR_NV )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "FinalCombinerInputNV passed bogus input: 0x%x", input );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "FinalCombinerInputNV passed bogus input: 0x%x", input );
 		return;
 	}
 	if( mapping != GL_UNSIGNED_IDENTITY_NV && mapping != GL_UNSIGNED_INVERT_NV && mapping != GL_EXPAND_NORMAL_NV && mapping != GL_EXPAND_NEGATE_NV && mapping != GL_HALF_BIAS_NORMAL_NV && mapping != GL_HALF_BIAS_NEGATE_NV && mapping != GL_SIGNED_IDENTITY_NV && mapping != GL_SIGNED_NEGATE_NV )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "FinalCombinerInputNV passed bogus mapping: 0x%x", mapping );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "FinalCombinerInputNV passed bogus mapping: 0x%x", mapping );
 		return;
 	}
 	if( componentUsage != GL_RGB && componentUsage != GL_ALPHA && componentUsage != GL_BLUE )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "FinalCombinerInputNV passed bogus componentUsage: 0x%x", componentUsage );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "FinalCombinerInputNV passed bogus componentUsage: 0x%x", componentUsage );
 		return;
 	}
@@ -441,5 +441,5 @@
 	if( componentUsage == GL_ALPHA && ( input == GL_E_TIMES_F_NV || input == GL_SPARE0_PLUS_SECONDARY_COLOR_NV ))
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "FinalCombinerInputNV does not allow componentUsage of ALPHA when input is E_TIMES_F or SPARE0_PLUS_SECONDARY_COLOR" );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "FinalCombinerInputNV does not allow componentUsage of ALPHA when input is E_TIMES_F or SPARE0_PLUS_SECONDARY_COLOR" );
 		return;
 	}
@@ -480,5 +480,5 @@
 			if( componentUsage != GL_ALPHA )
 			{
-				crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION, "FinalCombinerInputNV can not have variable G when componentUsage is RGB or BLUE" );
+				crStateError(pState,  __LINE__, __FILE__, GL_INVALID_OPERATION, "FinalCombinerInputNV can not have variable G when componentUsage is RGB or BLUE" );
 				return;
 			}
@@ -488,5 +488,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus variable: 0x%x", variable );
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerOutputNV passed bogus variable: 0x%x", variable );
 			return;
 	}
@@ -498,6 +498,7 @@
 
 /* XXX Unfinished RegCombiner State functions */
-void STATE_APIENTRY crStateGetCombinerOutputParameterfvNV( GLenum stage, GLenum portion, GLenum pname, GLfloat *params )
-{
+void STATE_APIENTRY crStateGetCombinerOutputParameterfvNV(PCRStateTracker pState, GLenum stage, GLenum portion, GLenum pname, GLfloat *params )
+{
+    RT_NOREF(pState);
 	(void) stage;
 	(void) portion;
@@ -506,6 +507,7 @@
 }
 
-void STATE_APIENTRY crStateGetCombinerOutputParameterivNV( GLenum stage, GLenum portion, GLenum pname, GLint *params )
-{
+void STATE_APIENTRY crStateGetCombinerOutputParameterivNV(PCRStateTracker pState, GLenum stage, GLenum portion, GLenum pname, GLint *params )
+{
+    RT_NOREF(pState);
 	(void) stage;
 	(void) portion;
@@ -514,6 +516,7 @@
 }
 
-void STATE_APIENTRY crStateGetFinalCombinerInputParameterfvNV( GLenum variable, GLenum pname, GLfloat *params )
-{
+void STATE_APIENTRY crStateGetFinalCombinerInputParameterfvNV(PCRStateTracker pState, GLenum variable, GLenum pname, GLfloat *params )
+{
+    RT_NOREF(pState);
 	(void) variable;
 	(void) pname;
@@ -521,6 +524,7 @@
 }
 
-void STATE_APIENTRY crStateGetFinalCombinerInputParameterivNV( GLenum variable, GLenum pname, GLint *params )
-{
+void STATE_APIENTRY crStateGetFinalCombinerInputParameterivNV(PCRStateTracker pState, GLenum variable, GLenum pname, GLint *params )
+{
+    RT_NOREF(pState);
 	(void) variable;
 	(void) pname;
@@ -529,7 +533,7 @@
 
 
-void STATE_APIENTRY crStateGetCombinerInputParameterivNV( GLenum stage, GLenum portion, GLenum variable, GLenum pname, GLint *params )
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetCombinerInputParameterivNV(PCRStateTracker pState, GLenum stage, GLenum portion, GLenum variable, GLenum pname, GLint *params )
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRRegCombinerState *r = &(g->regcombiner);
 	int i = stage - GL_COMBINER0_NV;
@@ -538,5 +542,5 @@
 	if (g->current.inBeginEnd) 
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 				"glGetCombinerParameterivNV called in begin/end");
 		return;
@@ -544,5 +548,5 @@
 
 	if (i < 0 || i >= CR_MAX_GENERAL_COMBINERS) {
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 								 "GetCombinerInputParameterivNV(stage=0x%x)", stage);
 		return;
@@ -572,5 +576,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "glGetCombinerInputParameterivNV(variable=0x%x)", variable);
 			return;
@@ -600,5 +604,5 @@
 			break;
 		default:
-			crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+			crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 									 "glGetCombinerInputParameterivNV(variable=0x%x)", variable);
 			return;
@@ -606,5 +610,5 @@
 	}
 	else {
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 								 "glGetCombinerInputParameterivNV(portion=0x%x)", portion);
 	}
@@ -620,5 +624,5 @@
 		return;
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
 								 "glGetCombinerInputParameterivNV(pname=0x%x)", pname);
 		return;
@@ -627,17 +631,17 @@
 
 
-void STATE_APIENTRY crStateGetCombinerInputParameterfvNV( GLenum stage, GLenum portion, GLenum variable, GLenum pname, GLfloat *params )
+void STATE_APIENTRY crStateGetCombinerInputParameterfvNV(PCRStateTracker pState, GLenum stage, GLenum portion, GLenum variable, GLenum pname, GLfloat *params )
 {
 		GLint iparams;
-		crStateGetCombinerInputParameterivNV(stage, portion, variable, pname, &iparams);
+		crStateGetCombinerInputParameterivNV(pState, stage, portion, variable, pname, &iparams);
 		*params = (GLfloat) iparams;
 }
 
 
-void STATE_APIENTRY crStateCombinerStageParameterfvNV( GLenum stage, GLenum pname, const GLfloat *params )
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateCombinerStageParameterfvNV(PCRStateTracker pState, GLenum stage, GLenum pname, const GLfloat *params )
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRRegCombinerState *r = &(g->regcombiner);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRRegCombinerBits *rb = &(sb->regcombiner);
 	
@@ -645,5 +649,5 @@
 	if( stage >= g->limits.maxGeneralCombiners )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "CombinerStageParameterfvNV passed bogus stage: 0x%x", stage+GL_COMBINER0_NV );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerStageParameterfvNV passed bogus stage: 0x%x", stage+GL_COMBINER0_NV );
 		return;
 	}
@@ -666,5 +670,5 @@
 			break;
 		default:
-			crStateError( __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerStageParameter passed bogus pname: 0x%x", pname );
+			crStateError(pState,  __LINE__, __FILE__, GL_INVALID_ENUM, "CombinerStageParameter passed bogus pname: 0x%x", pname );
 			return;
 	}
@@ -673,7 +677,7 @@
 }
 
-void STATE_APIENTRY crStateGetCombinerStageParameterfvNV( GLenum stage, GLenum pname, GLfloat *params )
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetCombinerStageParameterfvNV(PCRStateTracker pState, GLenum stage, GLenum pname, GLfloat *params )
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRRegCombinerState *r = &(g->regcombiner);
 
@@ -681,5 +685,5 @@
 	if( stage >= g->limits.maxGeneralCombiners )
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "GetCombinerStageParameterfvNV passed bogus stage: 0x%x", stage+GL_COMBINER0_NV );
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "GetCombinerStageParameterfvNV passed bogus stage: 0x%x", stage+GL_COMBINER0_NV );
 		return;
 	}
@@ -699,5 +703,5 @@
 			break;
 		default:
-			crStateError( __LINE__, __FILE__, GL_INVALID_ENUM, "GetCombinerStageParameter passed bogus pname: 0x%x", pname );
+			crStateError(pState,  __LINE__, __FILE__, GL_INVALID_ENUM, "GetCombinerStageParameter passed bogus pname: 0x%x", pname );
 			return;
 	}
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_regcombiner.txt
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_regcombiner.txt	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_regcombiner.txt	(revision 78375)
@@ -14,23 +14,23 @@
 :regCombinerStageColor0[i]:stageConstantColor0[i]|r,g,b,a:CombinerStageParameterfvNV, GL_COMBINER0_NV+i, GL_CONSTANT_COLOR0_NV
 :regCombinerStageColor1[i]:stageConstantColor1[i]|r,g,b,a:CombinerStageParameterfvNV, GL_COMBINER0_NV+i, GL_CONSTANT_COLOR1_NV
-:regCombinerInput[i]:rgb[i].a,rgb[i].aMapping,rgb[i].aPortion:*diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_RGB, GL_VARIABLE_A_NV, to->rgb[i].a, to->rgb[i].aMapping, to->rgb[i].aPortion );
-:regCombinerInput[i]:rgb[i].b,rgb[i].bMapping,rgb[i].bPortion:*diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_RGB, GL_VARIABLE_B_NV, to->rgb[i].b, to->rgb[i].bMapping, to->rgb[i].bPortion );
-:regCombinerInput[i]:rgb[i].c,rgb[i].cMapping,rgb[i].cPortion:*diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_RGB, GL_VARIABLE_C_NV, to->rgb[i].c, to->rgb[i].cMapping, to->rgb[i].cPortion );
-:regCombinerInput[i]:rgb[i].d,rgb[i].dMapping,rgb[i].dPortion:*diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_RGB, GL_VARIABLE_D_NV, to->rgb[i].d, to->rgb[i].dMapping, to->rgb[i].dPortion );
-:regCombinerInput[i]:alpha[i].a,alpha[i].aMapping,alpha[i].aPortion:*diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_ALPHA, GL_VARIABLE_A_NV, to->alpha[i].a, to->alpha[i].aMapping, to->alpha[i].aPortion );
-:regCombinerInput[i]:alpha[i].b,alpha[i].bMapping,alpha[i].bPortion:*diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_ALPHA, GL_VARIABLE_B_NV, to->alpha[i].b, to->alpha[i].bMapping, to->alpha[i].bPortion );
-:regCombinerInput[i]:alpha[i].c,alpha[i].cMapping,alpha[i].cPortion:*diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_ALPHA, GL_VARIABLE_C_NV, to->alpha[i].c, to->alpha[i].cMapping, to->alpha[i].cPortion );
-:regCombinerInput[i]:alpha[i].d,alpha[i].dMapping,alpha[i].dPortion:*diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_ALPHA, GL_VARIABLE_D_NV, to->alpha[i].d, to->alpha[i].dMapping, to->alpha[i].dPortion );
-:regCombinerOutput[i]:rgb[i]|abOutput,cdOutput,sumOutput,scale,bias,abDotProduct,cdDotProduct,muxSum:*diff_api.CombinerOutputNV( GL_COMBINER0_NV+i, GL_RGB, to->rgb[i].abOutput, to->rgb[i].cdOutput, to->rgb[i].sumOutput, to->rgb[i].scale, to->rgb[i].bias, to->rgb[i].abDotProduct, to->rgb[i].cdDotProduct, to->rgb[i].muxSum );
-:regCombinerOutput[i]:alpha[i]|abOutput,cdOutput,sumOutput,scale,bias,abDotProduct,cdDotProduct,muxSum:*diff_api.CombinerOutputNV( GL_COMBINER0_NV+i, GL_ALPHA, to->alpha[i].abOutput, to->alpha[i].cdOutput, to->alpha[i].sumOutput, to->alpha[i].scale, to->alpha[i].bias, to->alpha[i].abDotProduct, to->alpha[i].cdDotProduct, to->alpha[i].muxSum );
+:regCombinerInput[i]:rgb[i].a,rgb[i].aMapping,rgb[i].aPortion:*pState->diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_RGB, GL_VARIABLE_A_NV, to->rgb[i].a, to->rgb[i].aMapping, to->rgb[i].aPortion );
+:regCombinerInput[i]:rgb[i].b,rgb[i].bMapping,rgb[i].bPortion:*pState->diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_RGB, GL_VARIABLE_B_NV, to->rgb[i].b, to->rgb[i].bMapping, to->rgb[i].bPortion );
+:regCombinerInput[i]:rgb[i].c,rgb[i].cMapping,rgb[i].cPortion:*pState->diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_RGB, GL_VARIABLE_C_NV, to->rgb[i].c, to->rgb[i].cMapping, to->rgb[i].cPortion );
+:regCombinerInput[i]:rgb[i].d,rgb[i].dMapping,rgb[i].dPortion:*pState->diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_RGB, GL_VARIABLE_D_NV, to->rgb[i].d, to->rgb[i].dMapping, to->rgb[i].dPortion );
+:regCombinerInput[i]:alpha[i].a,alpha[i].aMapping,alpha[i].aPortion:*pState->diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_ALPHA, GL_VARIABLE_A_NV, to->alpha[i].a, to->alpha[i].aMapping, to->alpha[i].aPortion );
+:regCombinerInput[i]:alpha[i].b,alpha[i].bMapping,alpha[i].bPortion:*pState->diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_ALPHA, GL_VARIABLE_B_NV, to->alpha[i].b, to->alpha[i].bMapping, to->alpha[i].bPortion );
+:regCombinerInput[i]:alpha[i].c,alpha[i].cMapping,alpha[i].cPortion:*pState->diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_ALPHA, GL_VARIABLE_C_NV, to->alpha[i].c, to->alpha[i].cMapping, to->alpha[i].cPortion );
+:regCombinerInput[i]:alpha[i].d,alpha[i].dMapping,alpha[i].dPortion:*pState->diff_api.CombinerInputNV( GL_COMBINER0_NV+i, GL_ALPHA, GL_VARIABLE_D_NV, to->alpha[i].d, to->alpha[i].dMapping, to->alpha[i].dPortion );
+:regCombinerOutput[i]:rgb[i]|abOutput,cdOutput,sumOutput,scale,bias,abDotProduct,cdDotProduct,muxSum:*pState->diff_api.CombinerOutputNV( GL_COMBINER0_NV+i, GL_RGB, to->rgb[i].abOutput, to->rgb[i].cdOutput, to->rgb[i].sumOutput, to->rgb[i].scale, to->rgb[i].bias, to->rgb[i].abDotProduct, to->rgb[i].cdDotProduct, to->rgb[i].muxSum );
+:regCombinerOutput[i]:alpha[i]|abOutput,cdOutput,sumOutput,scale,bias,abDotProduct,cdDotProduct,muxSum:*pState->diff_api.CombinerOutputNV( GL_COMBINER0_NV+i, GL_ALPHA, to->alpha[i].abOutput, to->alpha[i].cdOutput, to->alpha[i].sumOutput, to->alpha[i].scale, to->alpha[i].bias, to->alpha[i].abDotProduct, to->alpha[i].cdDotProduct, to->alpha[i].muxSum );
 %flush
 >}
-:regCombinerFinalInput:a,aMapping,aPortion:*diff_api.FinalCombinerInputNV( GL_VARIABLE_A_NV, to->a, to->aMapping, to->aPortion );
-:regCombinerFinalInput:b,bMapping,bPortion:*diff_api.FinalCombinerInputNV( GL_VARIABLE_B_NV, to->b, to->bMapping, to->bPortion );
-:regCombinerFinalInput:c,cMapping,cPortion:*diff_api.FinalCombinerInputNV( GL_VARIABLE_C_NV, to->c, to->cMapping, to->cPortion );
-:regCombinerFinalInput:d,dMapping,dPortion:*diff_api.FinalCombinerInputNV( GL_VARIABLE_D_NV, to->d, to->dMapping, to->dPortion );
-:regCombinerFinalInput:e,eMapping,ePortion:*diff_api.FinalCombinerInputNV( GL_VARIABLE_E_NV, to->e, to->eMapping, to->ePortion );
-:regCombinerFinalInput:f,fMapping,fPortion:*diff_api.FinalCombinerInputNV( GL_VARIABLE_F_NV, to->f, to->fMapping, to->fPortion );
-:regCombinerFinalInput:g,gMapping,gPortion:*diff_api.FinalCombinerInputNV( GL_VARIABLE_G_NV, to->g, to->gMapping, to->gPortion );
+:regCombinerFinalInput:a,aMapping,aPortion:*pState->diff_api.FinalCombinerInputNV( GL_VARIABLE_A_NV, to->a, to->aMapping, to->aPortion );
+:regCombinerFinalInput:b,bMapping,bPortion:*pState->diff_api.FinalCombinerInputNV( GL_VARIABLE_B_NV, to->b, to->bMapping, to->bPortion );
+:regCombinerFinalInput:c,cMapping,cPortion:*pState->diff_api.FinalCombinerInputNV( GL_VARIABLE_C_NV, to->c, to->cMapping, to->cPortion );
+:regCombinerFinalInput:d,dMapping,dPortion:*pState->diff_api.FinalCombinerInputNV( GL_VARIABLE_D_NV, to->d, to->dMapping, to->dPortion );
+:regCombinerFinalInput:e,eMapping,ePortion:*pState->diff_api.FinalCombinerInputNV( GL_VARIABLE_E_NV, to->e, to->eMapping, to->ePortion );
+:regCombinerFinalInput:f,fMapping,fPortion:*pState->diff_api.FinalCombinerInputNV( GL_VARIABLE_F_NV, to->f, to->fMapping, to->fPortion );
+:regCombinerFinalInput:g,gMapping,gPortion:*pState->diff_api.FinalCombinerInputNV( GL_VARIABLE_G_NV, to->g, to->gMapping, to->gPortion );
 %flush
 %target=to
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_snapshot.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_snapshot.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_snapshot.c	(revision 78375)
@@ -51,4 +51,14 @@
  */
 
+/**
+ * Helper for walking the hash tables.
+ */
+typedef struct CRStateSnapshotWalkArgs
+{
+    PSSMHANDLE      pSSM;
+    PCRStateTracker pState;
+} CRStateSnapshotWalkArgs;
+typedef CRStateSnapshotWalkArgs *PCRStateSnapshotWalkArgs;
+
 static int32_t crStateAllocAndSSMR3GetMem(PSSMHANDLE pSSM, void **pBuffer, size_t cbBuffer)
 {
@@ -396,5 +406,5 @@
 }
 
-static int32_t crStateSaveTextureObjData(CRTextureObj *pTexture, PSSMHANDLE pSSM)
+static int32_t crStateSaveTextureObjData(PCRStateTracker pState, CRTextureObj *pTexture, PSSMHANDLE pSSM)
 {
     int32_t rc, face, i;
@@ -433,5 +443,5 @@
                 {
                     GLenum getEnum;
-                    diff_api.BindTexture(pTexture->target, pTexture->hwid);
+                    pState->diff_api.BindTexture(pTexture->target, pTexture->hwid);
                     bound = 1;
 
@@ -465,5 +475,5 @@
                     {
                         GLint curTex;
-                        diff_api.GetIntegerv(getEnum, &curTex);
+                        pState->diff_api.GetIntegerv(getEnum, &curTex);
                         if ((GLuint)curTex != pTexture->hwid)
                         {
@@ -506,6 +516,6 @@
                         crDebug("get image: compressed %i, face %i, level %i, width %i, height %i, bytes %i",
                                 ptl->compressed, face, i, ptl->width, ptl->height, ptl->bytes);
-                        diff_api.GetTexLevelParameteriv(target, i, GL_TEXTURE_WIDTH, &w);
-                        diff_api.GetTexLevelParameteriv(target, i, GL_TEXTURE_HEIGHT, &h);
+                        pState->diff_api.GetTexLevelParameteriv(target, i, GL_TEXTURE_WIDTH, &w);
+                        pState->diff_api.GetTexLevelParameteriv(target, i, GL_TEXTURE_HEIGHT, &h);
                         if (w!=ptl->width || h!=ptl->height)
                         {
@@ -523,9 +533,9 @@
                         if (!ptl->compressed)
                         {
-                            diff_api.GetTexImage(target, i, ptl->format, ptl->type, pImg);
+                            pState->diff_api.GetTexImage(target, i, ptl->format, ptl->type, pImg);
                         }
                         else
                         {
-                            diff_api.GetCompressedTexImageARB(target, i, pImg);
+                            pState->diff_api.GetCompressedTexImageARB(target, i, pImg);
                         }
                     }
@@ -602,5 +612,7 @@
 {
     CRTextureObj *pTexture = (CRTextureObj *) data1;
-    PSSMHANDLE pSSM = (PSSMHANDLE) data2;
+    PCRStateSnapshotWalkArgs pArgs = (PCRStateSnapshotWalkArgs)data2;
+    PCRStateTracker pState = pArgs->pState;
+    PSSMHANDLE pSSM = pArgs->pSSM;
     int32_t rc;
 
@@ -611,5 +623,5 @@
     rc = SSMR3PutMem(pSSM, pTexture, sizeof(*pTexture));
     CRASSERT(rc == VINF_SUCCESS);
-    rc = crStateSaveTextureObjData(pTexture, pSSM);
+    rc = crStateSaveTextureObjData(pState, pTexture, pSSM);
     CRASSERT(rc == VINF_SUCCESS);
 }
@@ -857,5 +869,7 @@
 {
     CRBufferObject *pBufferObj = (CRBufferObject *) data1;
-    PSSMHANDLE pSSM = (PSSMHANDLE) data2;
+    PCRStateSnapshotWalkArgs pArgs = (PCRStateSnapshotWalkArgs)data2;
+    PCRStateTracker pState = pArgs->pState;
+    PSSMHANDLE pSSM = pArgs->pSSM;
     int32_t rc;
 
@@ -877,6 +891,6 @@
     else if (pBufferObj->id!=0 && pBufferObj->size>0)
     {
-        diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, pBufferObj->hwid);
-        pBufferObj->pointer = diff_api.MapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_ONLY_ARB);
+        pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, pBufferObj->hwid);
+        pBufferObj->pointer = pState->diff_api.MapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_ONLY_ARB);
         rc = SSMR3PutMem(pSSM, &pBufferObj->pointer, sizeof(pBufferObj->pointer));
         CRASSERT(rc == VINF_SUCCESS);
@@ -886,5 +900,5 @@
             CRASSERT(rc == VINF_SUCCESS);
         }
-        diff_api.UnmapBufferARB(GL_ARRAY_BUFFER_ARB);
+        pState->diff_api.UnmapBufferARB(GL_ARRAY_BUFFER_ARB);
         pBufferObj->pointer = NULL;
     }
@@ -1049,5 +1063,7 @@
 {
     CRGLSLShader *pShader = (CRGLSLShader*) data1;
-    PSSMHANDLE pSSM = (PSSMHANDLE) data2;
+    PCRStateSnapshotWalkArgs pArgs = (PCRStateSnapshotWalkArgs)data2;
+    PCRStateTracker pState = pArgs->pState;
+    PSSMHANDLE pSSM = pArgs->pSSM;
     int32_t rc;
 
@@ -1067,9 +1083,9 @@
         GLchar *source=NULL;
 
-        diff_api.GetShaderiv(pShader->hwid, GL_SHADER_SOURCE_LENGTH, &sLen);
+        pState->diff_api.GetShaderiv(pShader->hwid, GL_SHADER_SOURCE_LENGTH, &sLen);
         if (sLen>0)
         {
             source = (GLchar*) crAlloc(sLen);
-            diff_api.GetShaderSource(pShader->hwid, sLen, NULL, source);
+            pState->diff_api.GetShaderSource(pShader->hwid, sLen, NULL, source);
         }
 
@@ -1128,5 +1144,7 @@
 {
     CRGLSLProgram *pProgram = (CRGLSLProgram*) data1;
-    PSSMHANDLE pSSM = (PSSMHANDLE) data2;
+    PCRStateSnapshotWalkArgs pArgs = (PCRStateSnapshotWalkArgs)data2;
+    PCRStateTracker pState = pArgs->pState;
+    PSSMHANDLE pSSM = pArgs->pSSM;
     int32_t rc;
     uint32_t ui32;
@@ -1153,5 +1171,5 @@
         rc = SSMR3PutU32(pSSM, ui32);
         CRASSERT(rc == VINF_SUCCESS);
-        crHashtableWalk(pProgram->currentState.attachedShaders, crStateSaveGLSLShaderCB, pSSM);
+        crHashtableWalk(pProgram->currentState.attachedShaders, crStateSaveGLSLShaderCB, pArgs);
     }
 
@@ -1159,6 +1177,6 @@
     crStateSaveGLSLProgramAttribs(&pProgram->activeState, pSSM);
 
-    diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniformLen);
-    diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_UNIFORMS, &activeUniforms);
+    pState->diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniformLen);
+    pState->diff_api.GetProgramiv(pProgram->hwid, GL_ACTIVE_UNIFORMS, &activeUniforms);
 
     if (!maxUniformLen)
@@ -1184,5 +1202,5 @@
     for (i=0; i<activeUniforms; ++i)
     {
-        diff_api.GetActiveUniform(pProgram->hwid, i, maxUniformLen, NULL, &size, &type, name);
+        pState->diff_api.GetActiveUniform(pProgram->hwid, i, maxUniformLen, NULL, &size, &type, name);
         uniformsCount += size;
     }
@@ -1200,5 +1218,5 @@
         for (i=0; i<activeUniforms; ++i)
         {
-            diff_api.GetActiveUniform(pProgram->hwid, i, maxUniformLen, NULL, &size, &type, name);
+            pState->diff_api.GetActiveUniform(pProgram->hwid, i, maxUniformLen, NULL, &size, &type, name);
 
             if (size>1)
@@ -1217,5 +1235,5 @@
                     sprintf(pIndexStr, "[%i]", j);
                 }
-                location = diff_api.GetUniformLocation(pProgram->hwid, name);
+                location = pState->diff_api.GetUniformLocation(pProgram->hwid, name);
 
                 rc = SSMR3PutMem(pSSM, &type, sizeof(type));
@@ -1224,14 +1242,14 @@
                 crStateSaveString(name, pSSM);
 
-                if (crStateIsIntUniform(type))
+                if (crStateIsIntUniform(pState, type))
                 {
-                    diff_api.GetUniformiv(pProgram->hwid, location, &idata[0]);
-                    rc = SSMR3PutMem(pSSM, &idata[0], crStateGetUniformSize(type)*sizeof(idata[0]));
+                    pState->diff_api.GetUniformiv(pProgram->hwid, location, &idata[0]);
+                    rc = SSMR3PutMem(pSSM, &idata[0], crStateGetUniformSize(pState, type)*sizeof(idata[0]));
                     CRASSERT(rc == VINF_SUCCESS);
                 }
                 else
                 {
-                    diff_api.GetUniformfv(pProgram->hwid, location, &fdata[0]);
-                    rc = SSMR3PutMem(pSSM, &fdata[0], crStateGetUniformSize(type)*sizeof(fdata[0]));
+                    pState->diff_api.GetUniformfv(pProgram->hwid, location, &fdata[0]);
+                    rc = SSMR3PutMem(pSSM, &fdata[0], crStateGetUniformSize(pState, type)*sizeof(fdata[0]));
                     CRASSERT(rc == VINF_SUCCESS);
                 }
@@ -1473,7 +1491,12 @@
 int32_t crStateSaveContext(CRContext *pContext, PSSMHANDLE pSSM)
 {
+    PCRStateTracker pState = pContext->pStateTracker;
     int32_t rc, i;
     uint32_t ui32, j;
     GLboolean bSaveShared = GL_TRUE;
+    CRStateSnapshotWalkArgs Args;
+
+    Args.pSSM = pSSM;
+    Args.pState = pState;
 
     CRASSERT(pContext && pSSM);
@@ -1539,25 +1562,25 @@
 
     /* Save textures */
-    rc = crStateSaveTextureObjData(&pContext->texture.base1D, pSSM);
-    AssertRCReturn(rc, rc);
-    rc = crStateSaveTextureObjData(&pContext->texture.base2D, pSSM);
-    AssertRCReturn(rc, rc);
-    rc = crStateSaveTextureObjData(&pContext->texture.base3D, pSSM);
-    AssertRCReturn(rc, rc);
-    rc = crStateSaveTextureObjData(&pContext->texture.proxy1D, pSSM);
-    AssertRCReturn(rc, rc);
-    rc = crStateSaveTextureObjData(&pContext->texture.proxy2D, pSSM);
-    AssertRCReturn(rc, rc);
-    rc = crStateSaveTextureObjData(&pContext->texture.proxy3D, pSSM);
+    rc = crStateSaveTextureObjData(pState, &pContext->texture.base1D, pSSM);
+    AssertRCReturn(rc, rc);
+    rc = crStateSaveTextureObjData(pState, &pContext->texture.base2D, pSSM);
+    AssertRCReturn(rc, rc);
+    rc = crStateSaveTextureObjData(pState, &pContext->texture.base3D, pSSM);
+    AssertRCReturn(rc, rc);
+    rc = crStateSaveTextureObjData(pState, &pContext->texture.proxy1D, pSSM);
+    AssertRCReturn(rc, rc);
+    rc = crStateSaveTextureObjData(pState, &pContext->texture.proxy2D, pSSM);
+    AssertRCReturn(rc, rc);
+    rc = crStateSaveTextureObjData(pState, &pContext->texture.proxy3D, pSSM);
 #ifdef CR_ARB_texture_cube_map
-    rc = crStateSaveTextureObjData(&pContext->texture.baseCubeMap, pSSM);
-    AssertRCReturn(rc, rc);
-    rc = crStateSaveTextureObjData(&pContext->texture.proxyCubeMap, pSSM);
+    rc = crStateSaveTextureObjData(pState, &pContext->texture.baseCubeMap, pSSM);
+    AssertRCReturn(rc, rc);
+    rc = crStateSaveTextureObjData(pState, &pContext->texture.proxyCubeMap, pSSM);
     AssertRCReturn(rc, rc);
 #endif
 #ifdef CR_NV_texture_rectangle
-    rc = crStateSaveTextureObjData(&pContext->texture.baseRect, pSSM);
-    AssertRCReturn(rc, rc);
-    rc = crStateSaveTextureObjData(&pContext->texture.proxyRect, pSSM);
+    rc = crStateSaveTextureObjData(pState, &pContext->texture.baseRect, pSSM);
+    AssertRCReturn(rc, rc);
+    rc = crStateSaveTextureObjData(pState, &pContext->texture.proxyRect, pSSM);
     AssertRCReturn(rc, rc);
 #endif
@@ -1582,12 +1605,12 @@
             pTexUnit = &pContext->texture.unit[pContext->texture.curTextureUnit];
 
-            diff_api.BindTexture(GL_TEXTURE_1D, pTexUnit->currentTexture1D->hwid);
-            diff_api.BindTexture(GL_TEXTURE_2D, pTexUnit->currentTexture2D->hwid);
-            diff_api.BindTexture(GL_TEXTURE_3D, pTexUnit->currentTexture3D->hwid);
+            pState->diff_api.BindTexture(GL_TEXTURE_1D, pTexUnit->currentTexture1D->hwid);
+            pState->diff_api.BindTexture(GL_TEXTURE_2D, pTexUnit->currentTexture2D->hwid);
+            pState->diff_api.BindTexture(GL_TEXTURE_3D, pTexUnit->currentTexture3D->hwid);
 #ifdef CR_ARB_texture_cube_map
-            diff_api.BindTexture(GL_TEXTURE_CUBE_MAP_ARB, pTexUnit->currentTextureCubeMap->hwid);
+            pState->diff_api.BindTexture(GL_TEXTURE_CUBE_MAP_ARB, pTexUnit->currentTextureCubeMap->hwid);
 #endif
 #ifdef CR_NV_texture_rectangle
-            diff_api.BindTexture(GL_TEXTURE_RECTANGLE_NV, pTexUnit->currentTextureRect->hwid);
+            pState->diff_api.BindTexture(GL_TEXTURE_RECTANGLE_NV, pTexUnit->currentTextureRect->hwid);
 #endif
         }
@@ -1676,14 +1699,14 @@
 
     /* Save default one*/
-    crStateSaveBufferObjectCB(0, pContext->bufferobject.nullBuffer, pSSM);
+    crStateSaveBufferObjectCB(0, pContext->bufferobject.nullBuffer, &Args);
 
     if (bSaveShared)
     {
         /* Save all the rest */
-        crHashtableWalk(pContext->shared->buffersTable, crStateSaveBufferObjectCB, pSSM);
+        crHashtableWalk(pContext->shared->buffersTable, crStateSaveBufferObjectCB, &Args);
     }
 
     /* Restore binding */
-    diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, pContext->bufferobject.arrayBuffer->hwid);
+    pState->diff_api.BindBufferARB(GL_ARRAY_BUFFER_ARB, pContext->bufferobject.arrayBuffer->hwid);
 
     /* Save pointers */
@@ -1794,5 +1817,5 @@
 }
 
-int32_t crStateSaveGlobals(PSSMHANDLE pSSM)
+int32_t crStateSaveGlobals(PCRStateTracker pState, PSSMHANDLE pSSM)
 {
     /* don't need that for now */
@@ -1812,18 +1835,18 @@
 #undef CRSTATE_BITS_OP
 #else
-    RT_NOREF(pSSM);
+    RT_NOREF(pState, pSSM);
 #endif
     return VINF_SUCCESS;
 }
 
-int32_t crStateLoadGlobals(PSSMHANDLE pSSM, uint32_t u32Version)
+int32_t crStateLoadGlobals(PCRStateTracker pState, PSSMHANDLE pSSM, uint32_t u32Version)
 {
     CRStateBits *pBits;
     int rc;
-    CRASSERT(g_cContexts >= 1);
-    if (g_cContexts <= 1)
+    CRASSERT(pState->cContexts >= 1);
+    if (pState->cContexts <= 1)
         return VINF_SUCCESS;
 
-    pBits = GetCurrentBits();
+    pBits = GetCurrentBits(pState);
 
     if (u32Version >= SHCROGL_SSM_VERSION_WITH_STATE_BITS)
@@ -1879,4 +1902,5 @@
 int32_t crStateLoadContext(CRContext *pContext, CRHashTable * pCtxTable, PFNCRSTATE_CONTEXT_GET pfnCtxGet, PSSMHANDLE pSSM, uint32_t u32Version)
 {
+    PCRStateTracker pState = pContext->pStateTracker;
     CRContext* pTmpContext;
     int32_t rc, i, j;
@@ -2040,5 +2064,5 @@
             CRASSERT(pContext->shared->refCount==1);
             bLoadShared = GL_FALSE;
-            crStateFreeShared(pContext, pContext->shared);
+            crStateFreeShared(pState, pContext, pContext->shared);
             pContext->shared = NULL;
             pTmpContext->shared->refCount++;
@@ -2657,10 +2681,10 @@
                 pProgram->pUniforms[k].name = crStateLoadString(pSSM);
 
-                if (crStateIsIntUniform(pProgram->pUniforms[k].type))
+                if (crStateIsIntUniform(pState, pProgram->pUniforms[k].type))
                 {
                     itemsize = sizeof(GLint);
                 } else itemsize = sizeof(GLfloat);
 
-                datasize = crStateGetUniformSize(pProgram->pUniforms[k].type)*itemsize;
+                datasize = crStateGetUniformSize(pState, pProgram->pUniforms[k].type)*itemsize;
                 pProgram->pUniforms[k].data = crAlloc((unsigned int /* this case is just so stupid */)datasize);
                 if (!pProgram->pUniforms[k].data) return VERR_NO_MEMORY;
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_stencil.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_stencil.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_stencil.c	(revision 78375)
@@ -11,5 +11,5 @@
 
 
-static GLint crStateStencilBufferGetIdxAndCount(CRStencilState *s, GLenum face, GLint *pIdx, GLint *pBitsIdx)
+static GLint crStateStencilBufferGetIdxAndCount(PCRStateTracker pState, CRStencilState *s, GLenum face, GLint *pIdx, GLint *pBitsIdx)
 {
     switch (face)
@@ -39,5 +39,5 @@
             return 1;
         default:
-            crStateError(__LINE__,__FILE__,GL_INVALID_ENUM, "crStateStencilBufferGetIdxAndCount");
+            crStateError(pState, __LINE__,__FILE__,GL_INVALID_ENUM, "crStateStencilBufferGetIdxAndCount");
             return 0;
     }
@@ -68,5 +68,5 @@
 {
 	CRStencilState *s = &ctx->stencil;
-	CRStateBits *stateb = GetCurrentBits();
+	CRStateBits *stateb = GetCurrentBits(ctx->pStateTracker);
 	CRStencilBits *sb = &(stateb->stencil);
 	int i;
@@ -108,9 +108,9 @@
 }
 
-static void crStateStencilFuncPerform(GLenum face, GLenum func, GLint ref, GLuint mask)
-{
-    CRContext *g = GetCurrentContext();
+static void crStateStencilFuncPerform(PCRStateTracker pState, GLenum face, GLenum func, GLint ref, GLuint mask)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRStencilState *s = &(g->stencil);
-    CRStateBits *stateb = GetCurrentBits();
+    CRStateBits *stateb = GetCurrentBits(pState);
     CRStencilBits *sb = &(stateb->stencil);
     GLint idx, bitsIdx, count, i;
@@ -119,5 +119,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
             "glStencilFunc called in begin/end");
         return;
@@ -135,10 +135,10 @@
         func != GL_ALWAYS)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
             "glStencilFunc called with bogu func: %d", func);
         return;
     }
 
-    count = crStateStencilBufferGetIdxAndCount(s, face, &idx, &bitsIdx);
+    count = crStateStencilBufferGetIdxAndCount(pState, s, face, &idx, &bitsIdx);
     if (count)
     {
@@ -153,5 +153,5 @@
 }
 
-void STATE_APIENTRY crStateStencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask)
+void STATE_APIENTRY crStateStencilFuncSeparate(PCRStateTracker pState, GLenum face, GLenum func, GLint ref, GLuint mask)
 {
     if (!face)
@@ -159,13 +159,13 @@
         /* crStateStencilFuncPerform accepts 0 value, while glStencilFuncSeparate does not,
          * filter it out here */
-        crStateError(__LINE__,__FILE__,GL_INVALID_ENUM, "crStateStencilFuncSeparate");
+        crStateError(pState, __LINE__,__FILE__,GL_INVALID_ENUM, "crStateStencilFuncSeparate");
         return;
     }
-    crStateStencilFuncPerform(face, func, ref, mask);
-}
-
-void STATE_APIENTRY crStateStencilFunc(GLenum func, GLint ref, GLuint mask)
-{
-    crStateStencilFuncPerform(0, func, ref, mask);
+    crStateStencilFuncPerform(pState, face, func, ref, mask);
+}
+
+void STATE_APIENTRY crStateStencilFunc(PCRStateTracker pState, GLenum func, GLint ref, GLuint mask)
+{
+    crStateStencilFuncPerform(pState, 0, func, ref, mask);
 }
 
@@ -178,9 +178,9 @@
 }
 
-static void crStateStencilOpPerform (GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
-{
-	CRContext *g = GetCurrentContext();
+static void crStateStencilOpPerform (PCRStateTracker pState, GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRStencilState *s = &(g->stencil);
-	CRStateBits *stateb = GetCurrentBits();
+	CRStateBits *stateb = GetCurrentBits(pState);
 	CRStencilBits *sb = &(stateb->stencil);
     GLint idx, bitsIdx, count, i;
@@ -188,5 +188,5 @@
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, 
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, 
 			"glStencilOp called in begin/end");
 		return;
@@ -208,5 +208,5 @@
 		break;
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, 
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, 
 			"glStencilOp called with bogus fail: %d", fail);
 		return;
@@ -226,5 +226,5 @@
 		break;
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, 
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, 
 			"glStencilOp called with bogus zfail: %d", zfail);
 		return;
@@ -244,10 +244,10 @@
 		break;
 	default:
-		crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, 
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, 
 			"glStencilOp called with bogus zpass: %d", zpass);
 		return;
 	}
 
-    count = crStateStencilBufferGetIdxAndCount(s, face, &idx, &bitsIdx);
+    count = crStateStencilBufferGetIdxAndCount(pState, s, face, &idx, &bitsIdx);
     if (count)
     {
@@ -263,5 +263,5 @@
 }
 
-void STATE_APIENTRY crStateStencilOpSeparate (GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
+void STATE_APIENTRY crStateStencilOpSeparate (PCRStateTracker pState, GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
 {
     if (!face)
@@ -269,25 +269,25 @@
         /* crStateStencilOpPerform accepts 0 value, while glStencilOpSeparate does not,
          * filter it out here */
-        crStateError(__LINE__,__FILE__,GL_INVALID_ENUM, "crStateStencilOpSeparate");
+        crStateError(pState, __LINE__,__FILE__,GL_INVALID_ENUM, "crStateStencilOpSeparate");
         return;
     }
-    crStateStencilOpPerform (0, fail, zfail, zpass);
-}
-
-void STATE_APIENTRY crStateStencilOp (GLenum fail, GLenum zfail, GLenum zpass)
-{
-    crStateStencilOpPerform (0, fail, zfail, zpass);
-}
-
-void STATE_APIENTRY crStateClearStencil (GLint c) 
-{
-	CRContext *g = GetCurrentContext();
+    crStateStencilOpPerform (pState, 0, fail, zfail, zpass);
+}
+
+void STATE_APIENTRY crStateStencilOp (PCRStateTracker pState, GLenum fail, GLenum zfail, GLenum zpass)
+{
+    crStateStencilOpPerform (pState, 0, fail, zfail, zpass);
+}
+
+void STATE_APIENTRY crStateClearStencil (PCRStateTracker pState, GLint c) 
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRStencilState *s = &(g->stencil);
-	CRStateBits *stateb = GetCurrentBits();
+	CRStateBits *stateb = GetCurrentBits(pState);
 	CRStencilBits *sb = &(stateb->stencil);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, 
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, 
 			"glClearStencil called in begin/end");
 		return;
@@ -302,14 +302,14 @@
 }
 
-void STATE_APIENTRY crStateStencilMask (GLuint mask) 
-{
-	CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateStencilMask (PCRStateTracker pState, GLuint mask) 
+{
+	CRContext *g = GetCurrentContext(pState);
 	CRStencilState *s = &(g->stencil);
-	CRStateBits *stateb = GetCurrentBits();
+	CRStateBits *stateb = GetCurrentBits(pState);
 	CRStencilBits *sb = &(stateb->stencil);
 
 	if (g->current.inBeginEnd)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, 
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, 
 			"glStencilMask called in begin/end");
 		return;
@@ -324,9 +324,9 @@
 }
 
-void STATE_APIENTRY crStateActiveStencilFaceEXT (GLenum face)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateActiveStencilFaceEXT (PCRStateTracker pState, GLenum face)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRStencilState *s = &(g->stencil);
-    CRStateBits *stateb = GetCurrentBits();
+    CRStateBits *stateb = GetCurrentBits(pState);
     CRStencilBits *sb = &(stateb->stencil);
 
@@ -338,5 +338,5 @@
         break;
     default:
-        crStateError(__LINE__,__FILE__,GL_INVALID_ENUM, "crStateActiveStencilFaceEXT");
+        crStateError(pState, __LINE__,__FILE__,GL_INVALID_ENUM, "crStateActiveStencilFaceEXT");
         return;
     }
@@ -348,5 +348,5 @@
 #ifdef CRSTATE_DEBUG_STENCIL_ERR
 #define CRSTATE_CLEARERR() do { \
-            while (diff_api.GetError() != GL_NO_ERROR) {} \
+            while (pState->diff_api.GetError() != GL_NO_ERROR) {} \
         } while (0)
 
@@ -355,5 +355,5 @@
             CRSTATE_CLEARERR(); \
             _op; \
-            while ((_glErr = diff_api.GetError()) != GL_NO_ERROR) { Assert(0);} \
+            while ((_glErr = pState->diff_api.GetError()) != GL_NO_ERROR) { Assert(0);} \
         }while (0)
 #else
@@ -388,4 +388,5 @@
         CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRStencilState *from = &(fromCtx->stencil);
     CRStencilState *to = &(toCtx->stencil);
@@ -395,4 +396,7 @@
     GLchar frontMatch = -1, backMatch = -1, toFrontBackMatch = -1;
     CRbitvalue nbitID[CR_MAX_BITARRAY];
+
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
@@ -402,6 +406,6 @@
     {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->stencilTest != to->stencilTest)
         {
@@ -415,6 +419,6 @@
     {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->stencilTwoSideEXT != to->stencilTwoSideEXT)
         {
@@ -429,5 +433,5 @@
         if (from->clearValue != to->clearValue)
         {
-            diff_api.ClearStencil (to->clearValue);
+            pState->diff_api.ClearStencil (to->clearValue);
             from->clearValue = to->clearValue;
         }
@@ -466,9 +470,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                     activeFace = GL_FRONT;
                 }
 
-                diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                pState->diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask);
@@ -483,9 +487,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                     activeFace = GL_FRONT;
                 }
 
-                diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                pState->diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask);
@@ -498,9 +502,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                     activeFace = GL_FRONT;
                 }
 
-                diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
+                pState->diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].ref,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].mask);
@@ -525,9 +529,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask);
@@ -546,9 +550,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask);
@@ -564,9 +568,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].mask);
@@ -590,9 +594,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask);
@@ -611,9 +615,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask);
@@ -629,9 +633,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].mask);
@@ -650,9 +654,9 @@
             if (activeFace == GL_FRONT)
             {
-                diff_api.ActiveStencilFaceEXT(GL_BACK);
+                pState->diff_api.ActiveStencilFaceEXT(GL_BACK);
                 activeFace = GL_BACK;
             }
 
-            diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].func,
+            pState->diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].func,
                 to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].ref,
                 to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].mask);
@@ -697,9 +701,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                     activeFace = GL_FRONT;
                 }
 
-                diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                pState->diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass);
@@ -715,9 +719,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                     activeFace = GL_FRONT;
                 }
 
-                diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                pState->diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass);
@@ -729,9 +733,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                     activeFace = GL_FRONT;
                 }
 
-                diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
+                pState->diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthFail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass);
@@ -754,9 +758,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass);
@@ -775,9 +779,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass);
@@ -794,9 +798,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass);
@@ -820,9 +824,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass);
@@ -841,9 +845,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass);
@@ -860,9 +864,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    pState->diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass);
@@ -881,9 +885,9 @@
             if (activeFace == GL_FRONT)
             {
-                diff_api.ActiveStencilFaceEXT(GL_BACK);
+                pState->diff_api.ActiveStencilFaceEXT(GL_BACK);
                 activeFace = GL_BACK;
             }
 
-            diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].fail,
+            pState->diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].fail,
                                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].passDepthFail,
                                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].passDepthPass);
@@ -900,5 +904,5 @@
     if (activeFace != to->activeStencilFace)
     {
-        diff_api.ActiveStencilFaceEXT(activeFace);
+        pState->diff_api.ActiveStencilFaceEXT(activeFace);
     }
 
@@ -918,5 +922,5 @@
         if (from->writeMask != to->writeMask)
         {
-            diff_api.StencilMask (to->writeMask);
+            pState->diff_api.StencilMask (to->writeMask);
             from->writeMask = to->writeMask;
         }
@@ -929,4 +933,5 @@
         CRContext *fromCtx, CRContext *toCtx)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRStencilState *from = &(fromCtx->stencil);
     CRStencilState *to = &(toCtx->stencil);
@@ -936,4 +941,7 @@
     GLchar frontMatch = -1, backMatch = -1, toFrontBackMatch = -1;
     CRbitvalue nbitID[CR_MAX_BITARRAY];
+
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
@@ -943,6 +951,6 @@
     {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->stencilTest != to->stencilTest)
         {
@@ -956,6 +964,6 @@
     {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->stencilTwoSideEXT != to->stencilTwoSideEXT)
         {
@@ -970,5 +978,5 @@
         if (from->clearValue != to->clearValue)
         {
-            CRSTATE_CHECKGLERR(diff_api.ClearStencil (to->clearValue));
+            CRSTATE_CHECKGLERR(pState->diff_api.ClearStencil (to->clearValue));
             FILLDIRTY(b->clearValue);
             FILLDIRTY(b->dirty);
@@ -1007,9 +1015,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                     activeFace = GL_FRONT;
                 }
 
-                CRSTATE_CHECKGLERR(diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                CRSTATE_CHECKGLERR(pState->diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
@@ -1022,9 +1030,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                     activeFace = GL_FRONT;
                 }
 
-                CRSTATE_CHECKGLERR(diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                CRSTATE_CHECKGLERR(pState->diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
@@ -1035,9 +1043,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
                     activeFace = GL_FRONT;
                 }
 
-                CRSTATE_CHECKGLERR(diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
+                CRSTATE_CHECKGLERR(pState->diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].ref,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].mask));
@@ -1061,9 +1069,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
@@ -1079,9 +1087,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
@@ -1095,9 +1103,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].mask));
@@ -1122,9 +1130,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
@@ -1140,9 +1148,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
@@ -1156,9 +1164,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
+                        pState->diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].ref,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].mask));
@@ -1178,9 +1186,9 @@
             if (activeFace == GL_FRONT)
             {
-                CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_BACK));
+                CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_BACK));
                 activeFace = GL_BACK;
             }
 
-            CRSTATE_CHECKGLERR(diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].func,
+            CRSTATE_CHECKGLERR(pState->diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].func,
                 to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].ref,
                 to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].mask));
@@ -1227,9 +1235,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                    CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_FRONT));
                     activeFace = GL_FRONT;
                 }
 
-                CRSTATE_CHECKGLERR(diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                CRSTATE_CHECKGLERR(pState->diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
@@ -1242,9 +1250,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                    CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_FRONT));
                     activeFace = GL_FRONT;
                 }
 
-                CRSTATE_CHECKGLERR(diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                CRSTATE_CHECKGLERR(pState->diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
@@ -1255,9 +1263,9 @@
                 if (activeFace == GL_BACK)
                 {
-                    CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                    CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_FRONT));
                     activeFace = GL_FRONT;
                 }
 
-                CRSTATE_CHECKGLERR(diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
+                CRSTATE_CHECKGLERR(pState->diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthFail,
                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass));
@@ -1281,9 +1289,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                        CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
@@ -1299,9 +1307,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                        CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
@@ -1315,9 +1323,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
+                        CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass));
@@ -1343,9 +1351,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                        CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
@@ -1361,9 +1369,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                        CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
@@ -1377,9 +1385,9 @@
                     if (activeFace == GL_BACK)
                     {
-                        CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
-                        activeFace = GL_FRONT;
-                    }
-
-                    CRSTATE_CHECKGLERR(diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
+                        CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(pState->diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthFail,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass));
@@ -1400,9 +1408,9 @@
             if (activeFace == GL_FRONT)
             {
-                CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_BACK));
+                CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(GL_BACK));
                 activeFace = GL_BACK;
             }
 
-            CRSTATE_CHECKGLERR(diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].fail,
+            CRSTATE_CHECKGLERR(pState->diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].fail,
                                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].passDepthFail,
                                     to->buffers[CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK].passDepthPass));
@@ -1420,5 +1428,5 @@
     if (activeFace != to->activeStencilFace)
     {
-        CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(activeFace));
+        CRSTATE_CHECKGLERR(pState->diff_api.ActiveStencilFaceEXT(activeFace));
     }
 
@@ -1439,5 +1447,5 @@
         if (from->writeMask != to->writeMask)
         {
-            CRSTATE_CHECKGLERR(diff_api.StencilMask (to->writeMask));
+            CRSTATE_CHECKGLERR(pState->diff_api.StencilMask (to->writeMask));
             FILLDIRTY(b->writeMask);
             FILLDIRTY(b->dirty);
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_texdiff.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_texdiff.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_texdiff.c	(revision 78375)
@@ -15,5 +15,5 @@
 
 
-#define UNIMPLEMENTED() crStateError(__LINE__,__FILE__,GL_INVALID_OPERATION, "Unimplemented something or other" )
+#define UNIMPLEMENTED() crStateError(pState, __LINE__,__FILE__,GL_INVALID_OPERATION, "Unimplemented something or other" )
 
 
@@ -98,4 +98,5 @@
                                                      CRContext *fromCtx, CRContext *toCtx )
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRTextureState *from = &(fromCtx->texture);
     const CRTextureState *to = &(toCtx->texture);
@@ -105,8 +106,10 @@
     unsigned int activeUnit = (unsigned int) -1;
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
-    able[0] = diff_api.Disable;
-    able[1] = diff_api.Enable;
+    able[0] = pState->diff_api.Disable;
+    able[1] = pState->diff_api.Enable;
 
     for (i = 0; i < fromCtx->limits.maxTextureUnits; i++)
@@ -115,5 +118,5 @@
         {
             if (activeUnit != i) {
-                diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
+                pState->diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
                 activeUnit = i;
             }
@@ -182,10 +185,10 @@
         {
             if (activeUnit != i) {
-                diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
+                pState->diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
                 activeUnit = i;
             }
             if (from->unit[i].currentTexture1D->hwid != to->unit[i].currentTexture1D->hwid)
             {
-                diff_api.BindTexture(GL_TEXTURE_1D, crStateGetTextureObjHWID(to->unit[i].currentTexture1D));
+                pState->diff_api.BindTexture(GL_TEXTURE_1D, crStateGetTextureObjHWID(pState, to->unit[i].currentTexture1D));
                 FILLDIRTY(tb->current[i]);
                 FILLDIRTY(tb->dirty);
@@ -193,5 +196,5 @@
             if (from->unit[i].currentTexture2D->hwid != to->unit[i].currentTexture2D->hwid)
             {
-                diff_api.BindTexture(GL_TEXTURE_2D, crStateGetTextureObjHWID(to->unit[i].currentTexture2D));
+                pState->diff_api.BindTexture(GL_TEXTURE_2D, crStateGetTextureObjHWID(pState, to->unit[i].currentTexture2D));
                 FILLDIRTY(tb->current[i]);
                 FILLDIRTY(tb->dirty);
@@ -200,5 +203,5 @@
             if (from->unit[i].currentTexture3D->hwid != to->unit[i].currentTexture3D->hwid)
             {
-                diff_api.BindTexture(GL_TEXTURE_3D, crStateGetTextureObjHWID(to->unit[i].currentTexture3D));
+                pState->diff_api.BindTexture(GL_TEXTURE_3D, crStateGetTextureObjHWID(pState, to->unit[i].currentTexture3D));
                 FILLDIRTY(tb->current[i]);
                 FILLDIRTY(tb->dirty);
@@ -209,5 +212,5 @@
                 from->unit[i].currentTextureCubeMap->hwid != to->unit[i].currentTextureCubeMap->hwid)
             {
-                diff_api.BindTexture(GL_TEXTURE_CUBE_MAP_ARB, crStateGetTextureObjHWID(to->unit[i].currentTextureCubeMap));
+                pState->diff_api.BindTexture(GL_TEXTURE_CUBE_MAP_ARB, crStateGetTextureObjHWID(pState, to->unit[i].currentTextureCubeMap));
                 FILLDIRTY(tb->current[i]);
                 FILLDIRTY(tb->dirty);
@@ -218,5 +221,5 @@
                 from->unit[i].currentTextureRect->hwid != to->unit[i].currentTextureRect->hwid)
             {
-                diff_api.BindTexture(GL_TEXTURE_RECTANGLE_NV, crStateGetTextureObjHWID(to->unit[i].currentTextureRect));
+                pState->diff_api.BindTexture(GL_TEXTURE_RECTANGLE_NV, crStateGetTextureObjHWID(pState, to->unit[i].currentTextureRect));
                 FILLDIRTY(tb->current[i]);
                 FILLDIRTY(tb->dirty);
@@ -229,5 +232,5 @@
         {
             if (activeUnit != i) {
-                diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
+                pState->diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
                 activeUnit = i;
             }
@@ -242,5 +245,5 @@
                 f[2] = to->unit[i].objSCoeff.z;
                 f[3] = to->unit[i].objSCoeff.w;
-                diff_api.TexGenfv (GL_S, GL_OBJECT_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_S, GL_OBJECT_PLANE, (const GLfloat *) f);
                 FILLDIRTY(tb->objGen[i]);
                 FILLDIRTY(tb->dirty);
@@ -255,5 +258,5 @@
                 f[2] = to->unit[i].objTCoeff.z;
                 f[3] = to->unit[i].objTCoeff.w;
-                diff_api.TexGenfv (GL_T, GL_OBJECT_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_T, GL_OBJECT_PLANE, (const GLfloat *) f);
                 FILLDIRTY(tb->objGen[i]);
                 FILLDIRTY(tb->dirty);
@@ -268,5 +271,5 @@
                 f[2] = to->unit[i].objRCoeff.z;
                 f[3] = to->unit[i].objRCoeff.w;
-                diff_api.TexGenfv (GL_R, GL_OBJECT_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_R, GL_OBJECT_PLANE, (const GLfloat *) f);
                 FILLDIRTY(tb->objGen[i]);
                 FILLDIRTY(tb->dirty);
@@ -281,5 +284,5 @@
                 f[2] = to->unit[i].objQCoeff.z;
                 f[3] = to->unit[i].objQCoeff.w;
-                diff_api.TexGenfv (GL_Q, GL_OBJECT_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_Q, GL_OBJECT_PLANE, (const GLfloat *) f);
                 FILLDIRTY(tb->objGen[i]);
                 FILLDIRTY(tb->dirty);
@@ -290,10 +293,10 @@
         {
             if (activeUnit != i) {
-                diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
+                pState->diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
                 activeUnit = i;
             }
-            diff_api.MatrixMode(GL_MODELVIEW);
-            diff_api.PushMatrix();
-            diff_api.LoadIdentity();
+            pState->diff_api.MatrixMode(GL_MODELVIEW);
+            pState->diff_api.PushMatrix();
+            pState->diff_api.LoadIdentity();
             if (from->unit[i].eyeSCoeff.x != to->unit[i].eyeSCoeff.x ||
                 from->unit[i].eyeSCoeff.y != to->unit[i].eyeSCoeff.y ||
@@ -305,5 +308,5 @@
                 f[2] = to->unit[i].eyeSCoeff.z;
                 f[3] = to->unit[i].eyeSCoeff.w;
-                diff_api.TexGenfv (GL_S, GL_EYE_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_S, GL_EYE_PLANE, (const GLfloat *) f);
                 FILLDIRTY(tb->eyeGen[i]);
                 FILLDIRTY(tb->dirty);
@@ -318,5 +321,5 @@
                 f[2] = to->unit[i].eyeTCoeff.z;
                 f[3] = to->unit[i].eyeTCoeff.w;
-                diff_api.TexGenfv (GL_T, GL_EYE_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_T, GL_EYE_PLANE, (const GLfloat *) f);
                 FILLDIRTY(tb->eyeGen[i]);
                 FILLDIRTY(tb->dirty);
@@ -331,5 +334,5 @@
                 f[2] = to->unit[i].eyeRCoeff.z;
                 f[3] = to->unit[i].eyeRCoeff.w;
-                diff_api.TexGenfv (GL_R, GL_EYE_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_R, GL_EYE_PLANE, (const GLfloat *) f);
                 FILLDIRTY(tb->eyeGen[i]);
                 FILLDIRTY(tb->dirty);
@@ -344,9 +347,9 @@
                 f[2] = to->unit[i].eyeQCoeff.z;
                 f[3] = to->unit[i].eyeQCoeff.w;
-                diff_api.TexGenfv (GL_Q, GL_EYE_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_Q, GL_EYE_PLANE, (const GLfloat *) f);
                 FILLDIRTY(tb->eyeGen[i]);
                 FILLDIRTY(tb->dirty);
             }
-            diff_api.PopMatrix();
+            pState->diff_api.PopMatrix();
             CLEARDIRTY(tb->eyeGen[i], nbitID);
         }
@@ -354,5 +357,5 @@
         {
             if (activeUnit != i) {
-                diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
+                pState->diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
                 activeUnit = i;
             }
@@ -362,8 +365,8 @@
                 from->unit[i].gen.q != to->unit[i].gen.q) 
             {
-                diff_api.TexGeni (GL_S, GL_TEXTURE_GEN_MODE, to->unit[i].gen.s);
-                diff_api.TexGeni (GL_T, GL_TEXTURE_GEN_MODE, to->unit[i].gen.t);
-                diff_api.TexGeni (GL_R, GL_TEXTURE_GEN_MODE, to->unit[i].gen.r);
-                diff_api.TexGeni (GL_Q, GL_TEXTURE_GEN_MODE, to->unit[i].gen.q);    
+                pState->diff_api.TexGeni (GL_S, GL_TEXTURE_GEN_MODE, to->unit[i].gen.s);
+                pState->diff_api.TexGeni (GL_T, GL_TEXTURE_GEN_MODE, to->unit[i].gen.t);
+                pState->diff_api.TexGeni (GL_R, GL_TEXTURE_GEN_MODE, to->unit[i].gen.r);
+                pState->diff_api.TexGeni (GL_Q, GL_TEXTURE_GEN_MODE, to->unit[i].gen.q);    
                 FILLDIRTY(tb->genMode[i]);
                 FILLDIRTY(tb->dirty);
@@ -377,10 +380,10 @@
         {
             if (activeUnit != i) {
-                diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
+                pState->diff_api.ActiveTextureARB( i + GL_TEXTURE0_ARB );
                 activeUnit = i;
             }
             if (from->unit[i].envMode != to->unit[i].envMode) 
             {
-                diff_api.TexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, to->unit[i].envMode);
+                pState->diff_api.TexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, to->unit[i].envMode);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -396,5 +399,5 @@
                 f[2] = to->unit[i].envColor.b;
                 f[3] = to->unit[i].envColor.a;
-                diff_api.TexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (const GLfloat *) f);
+                pState->diff_api.TexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (const GLfloat *) f);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -402,5 +405,5 @@
             if (from->unit[i].combineModeRGB != to->unit[i].combineModeRGB)
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, to->unit[i].combineModeRGB);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, to->unit[i].combineModeRGB);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -408,5 +411,5 @@
             if (from->unit[i].combineModeA != to->unit[i].combineModeA)
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, to->unit[i].combineModeA);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, to->unit[i].combineModeA);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -414,5 +417,5 @@
             if (from->unit[i].combineSourceRGB[0] != to->unit[i].combineSourceRGB[0])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, to->unit[i].combineSourceRGB[0]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, to->unit[i].combineSourceRGB[0]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -420,5 +423,5 @@
             if (from->unit[i].combineSourceRGB[1] != to->unit[i].combineSourceRGB[1])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, to->unit[i].combineSourceRGB[1]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, to->unit[i].combineSourceRGB[1]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -426,5 +429,5 @@
             if (from->unit[i].combineSourceRGB[2] != to->unit[i].combineSourceRGB[2])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, to->unit[i].combineSourceRGB[2]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, to->unit[i].combineSourceRGB[2]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -432,5 +435,5 @@
             if (from->unit[i].combineSourceA[0] != to->unit[i].combineSourceA[0])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, to->unit[i].combineSourceA[0]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, to->unit[i].combineSourceA[0]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -438,5 +441,5 @@
             if (from->unit[i].combineSourceA[1] != to->unit[i].combineSourceA[1])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, to->unit[i].combineSourceA[1]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, to->unit[i].combineSourceA[1]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -444,5 +447,5 @@
             if (from->unit[i].combineSourceA[2] != to->unit[i].combineSourceA[2])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, to->unit[i].combineSourceA[2]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, to->unit[i].combineSourceA[2]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -450,5 +453,5 @@
             if (from->unit[i].combineOperandRGB[0] != to->unit[i].combineOperandRGB[0])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, to->unit[i].combineOperandRGB[0]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, to->unit[i].combineOperandRGB[0]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -456,5 +459,5 @@
             if (from->unit[i].combineOperandRGB[1] != to->unit[i].combineOperandRGB[1])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, to->unit[i].combineOperandRGB[1]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, to->unit[i].combineOperandRGB[1]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -462,5 +465,5 @@
             if (from->unit[i].combineOperandRGB[2] != to->unit[i].combineOperandRGB[2])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, to->unit[i].combineOperandRGB[2]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, to->unit[i].combineOperandRGB[2]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -468,5 +471,5 @@
             if (from->unit[i].combineOperandA[0] != to->unit[i].combineOperandA[0])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, to->unit[i].combineOperandA[0]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, to->unit[i].combineOperandA[0]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -474,5 +477,5 @@
             if (from->unit[i].combineOperandA[1] != to->unit[i].combineOperandA[1])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, to->unit[i].combineOperandA[1]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, to->unit[i].combineOperandA[1]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -480,5 +483,5 @@
             if (from->unit[i].combineOperandA[2] != to->unit[i].combineOperandA[2])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, to->unit[i].combineOperandA[2]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, to->unit[i].combineOperandA[2]);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -486,5 +489,5 @@
             if (from->unit[i].combineScaleRGB != to->unit[i].combineScaleRGB)
             {
-                diff_api.TexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, to->unit[i].combineScaleRGB);
+                pState->diff_api.TexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, to->unit[i].combineScaleRGB);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -492,5 +495,5 @@
             if (from->unit[i].combineScaleA != to->unit[i].combineScaleA)
             {
-                diff_api.TexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, to->unit[i].combineScaleA);
+                pState->diff_api.TexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, to->unit[i].combineScaleA);
                 FILLDIRTY(tb->envBit[i]);
                 FILLDIRTY(tb->dirty);
@@ -510,7 +513,7 @@
     /* After possible fiddling put them back now */
     if (activeUnit != toCtx->texture.curTextureUnit) {
-        diff_api.ActiveTextureARB( toCtx->texture.curTextureUnit + GL_TEXTURE0_ARB );
+        pState->diff_api.ActiveTextureARB( toCtx->texture.curTextureUnit + GL_TEXTURE0_ARB );
     }
-    diff_api.MatrixMode(toCtx->transform.matrixMode);
+    pState->diff_api.MatrixMode(toCtx->transform.matrixMode);
 }
 
@@ -523,5 +526,5 @@
 int crStateTextureCheckDirtyImages(CRContext *from, CRContext *to, GLenum target, int textureUnit)
 {
-    CRContext *g         = GetCurrentContext();
+    CRContext *g         = GetCurrentContext(from->pStateTracker);
     CRTextureState *tsto;
     CRbitvalue *bitID;
@@ -532,4 +535,5 @@
     CRASSERT(to);
     CRASSERT(from);
+    CRASSERT(from->pStateTracker == to->pStateTracker);
 
     tsto = &(to->texture);
@@ -601,14 +605,15 @@
                          CRTextureObj *tobj, GLboolean alwaysDirty)
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRTextureState *from = &(fromCtx->texture);
     glAble able[2];
     int u = 0; /* always use texture unit 0 for diff'ing */
-    GLuint hwid = crStateGetTextureObjHWID(tobj);
+    GLuint hwid = crStateGetTextureObjHWID(pState, tobj);
 
     if (!hwid)
         return;
 
-    able[0] = diff_api.Disable;
-    able[1] = diff_api.Enable;
+    able[0] = pState->diff_api.Disable;
+    able[1] = pState->diff_api.Enable;
 
 #if 0
@@ -619,10 +624,10 @@
     /* Set active texture unit, and bind this texture object */
     if (from->curTextureUnit != u) {
-        diff_api.ActiveTextureARB( u + GL_TEXTURE0_ARB );
+        pState->diff_api.ActiveTextureARB( u + GL_TEXTURE0_ARB );
         from->curTextureUnit = u;
     }
 #endif
 
-    diff_api.BindTexture(tobj->target, hwid);
+    pState->diff_api.BindTexture(tobj->target, hwid);
 
     if (alwaysDirty || CHECKDIRTY(tobj->paramsBit[u], bitID)) 
@@ -633,38 +638,38 @@
         f[2] = tobj->borderColor.b;
         f[3] = tobj->borderColor.a;
-        diff_api.TexParameteri(tobj->target, GL_TEXTURE_BASE_LEVEL, tobj->baseLevel);
-        diff_api.TexParameteri(tobj->target, GL_TEXTURE_MAX_LEVEL, tobj->maxLevel);
-        diff_api.TexParameteri(tobj->target, GL_TEXTURE_MIN_FILTER, tobj->minFilter);
-        diff_api.TexParameteri(tobj->target, GL_TEXTURE_MAG_FILTER, tobj->magFilter);
-        diff_api.TexParameteri(tobj->target, GL_TEXTURE_WRAP_S, tobj->wrapS);
-        diff_api.TexParameteri(tobj->target, GL_TEXTURE_WRAP_T, tobj->wrapT);
+        pState->diff_api.TexParameteri(tobj->target, GL_TEXTURE_BASE_LEVEL, tobj->baseLevel);
+        pState->diff_api.TexParameteri(tobj->target, GL_TEXTURE_MAX_LEVEL, tobj->maxLevel);
+        pState->diff_api.TexParameteri(tobj->target, GL_TEXTURE_MIN_FILTER, tobj->minFilter);
+        pState->diff_api.TexParameteri(tobj->target, GL_TEXTURE_MAG_FILTER, tobj->magFilter);
+        pState->diff_api.TexParameteri(tobj->target, GL_TEXTURE_WRAP_S, tobj->wrapS);
+        pState->diff_api.TexParameteri(tobj->target, GL_TEXTURE_WRAP_T, tobj->wrapT);
 #ifdef CR_OPENGL_VERSION_1_2
-        diff_api.TexParameteri(tobj->target, GL_TEXTURE_WRAP_R, tobj->wrapR);
-        diff_api.TexParameterf(tobj->target, GL_TEXTURE_PRIORITY, tobj->priority);
-#endif
-        diff_api.TexParameterfv(tobj->target, GL_TEXTURE_BORDER_COLOR, (const GLfloat *) f);
+        pState->diff_api.TexParameteri(tobj->target, GL_TEXTURE_WRAP_R, tobj->wrapR);
+        pState->diff_api.TexParameterf(tobj->target, GL_TEXTURE_PRIORITY, tobj->priority);
+#endif
+        pState->diff_api.TexParameterfv(tobj->target, GL_TEXTURE_BORDER_COLOR, (const GLfloat *) f);
 #ifdef CR_EXT_texture_filter_anisotropic
         if (fromCtx->extensions.EXT_texture_filter_anisotropic) {
-            diff_api.TexParameterf(tobj->target, GL_TEXTURE_MAX_ANISOTROPY_EXT, tobj->maxAnisotropy);
+            pState->diff_api.TexParameterf(tobj->target, GL_TEXTURE_MAX_ANISOTROPY_EXT, tobj->maxAnisotropy);
         }
 #endif
 #ifdef CR_ARB_depth_texture
         if (fromCtx->extensions.ARB_depth_texture)
-            diff_api.TexParameteri(tobj->target, GL_DEPTH_TEXTURE_MODE_ARB, tobj->depthMode);
+            pState->diff_api.TexParameteri(tobj->target, GL_DEPTH_TEXTURE_MODE_ARB, tobj->depthMode);
 #endif
 #ifdef CR_ARB_shadow
         if (fromCtx->extensions.ARB_shadow) {
-            diff_api.TexParameteri(tobj->target, GL_TEXTURE_COMPARE_MODE_ARB, tobj->compareMode);
-            diff_api.TexParameteri(tobj->target, GL_TEXTURE_COMPARE_FUNC_ARB, tobj->compareFunc);
+            pState->diff_api.TexParameteri(tobj->target, GL_TEXTURE_COMPARE_MODE_ARB, tobj->compareMode);
+            pState->diff_api.TexParameteri(tobj->target, GL_TEXTURE_COMPARE_FUNC_ARB, tobj->compareFunc);
         }
 #endif
 #ifdef CR_ARB_shadow_ambient
         if (fromCtx->extensions.ARB_shadow_ambient) {
-            diff_api.TexParameterf(tobj->target, GL_TEXTURE_COMPARE_FAIL_VALUE_ARB, tobj->compareFailValue);
+            pState->diff_api.TexParameterf(tobj->target, GL_TEXTURE_COMPARE_FAIL_VALUE_ARB, tobj->compareFailValue);
         }
 #endif
 #ifdef CR_SGIS_generate_mipmap
         if (fromCtx->extensions.SGIS_generate_mipmap) {
-            diff_api.TexParameteri(tobj->target, GL_GENERATE_MIPMAP_SGIS, tobj->generateMipmap);
+            pState->diff_api.TexParameteri(tobj->target, GL_GENERATE_MIPMAP_SGIS, tobj->generateMipmap);
         }
 #endif
@@ -688,10 +693,10 @@
                     {
                         if (tl->generateMipmap) {
-                            diff_api.TexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, 1);
+                            pState->diff_api.TexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, 1);
                         }
                         if (tl->width)
                         {
                             if (tl->compressed) {
-                                diff_api.CompressedTexImage1DARB(GL_TEXTURE_1D, lvl,
+                                pState->diff_api.CompressedTexImage1DARB(GL_TEXTURE_1D, lvl,
                                                                  tl->internalFormat, tl->width,
                                                                  tl->border, tl->bytes, tl->img);
@@ -699,12 +704,12 @@
                             else {
                                 /* alignment must be one */
-                                diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
-                                diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
-                                diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
-                                diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
+                                pState->diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
                                 if (tl->generateMipmap) {
-                                    diff_api.TexParameteri(GL_TEXTURE_1D, GL_GENERATE_MIPMAP_SGIS, 1);
+                                    pState->diff_api.TexParameteri(GL_TEXTURE_1D, GL_GENERATE_MIPMAP_SGIS, 1);
                                 }
-                                diff_api.TexImage1D(GL_TEXTURE_1D, lvl,
+                                pState->diff_api.TexImage1D(GL_TEXTURE_1D, lvl,
                                                     tl->internalFormat,
                                                     tl->width, tl->border,
@@ -733,5 +738,5 @@
                     {
                         if (tl->generateMipmap) {
-                            diff_api.TexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, 1);
+                            pState->diff_api.TexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, 1);
                         }
 
@@ -739,5 +744,5 @@
                         {
                             if (tl->compressed) {
-                                diff_api.CompressedTexImage2DARB(GL_TEXTURE_2D, lvl,
+                                pState->diff_api.CompressedTexImage2DARB(GL_TEXTURE_2D, lvl,
                                          tl->internalFormat, tl->width,
                                          tl->height, tl->border,
@@ -746,9 +751,9 @@
                             else {
                                 /* alignment must be one */
-                                diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
-                                diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
-                                diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
-                                diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
-                                diff_api.TexImage2D(GL_TEXTURE_2D, lvl,
+                                pState->diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
+                                pState->diff_api.TexImage2D(GL_TEXTURE_2D, lvl,
                                                     tl->internalFormat,
                                                     tl->width, tl->height, tl->border,
@@ -779,5 +784,5 @@
                     {
                         if (tl->generateMipmap) {
-                            diff_api.TexParameteri(GL_TEXTURE_3D, GL_GENERATE_MIPMAP_SGIS, 1);
+                            pState->diff_api.TexParameteri(GL_TEXTURE_3D, GL_GENERATE_MIPMAP_SGIS, 1);
                         }
 
@@ -785,5 +790,5 @@
                         {
                             if (tl->compressed) {
-                                diff_api.CompressedTexImage3DARB(GL_TEXTURE_3D, lvl,
+                                pState->diff_api.CompressedTexImage3DARB(GL_TEXTURE_3D, lvl,
                                                                  tl->internalFormat, tl->width,
                                                                  tl->height, tl->depth,
@@ -792,9 +797,9 @@
                             else {
                                 /* alignment must be one */
-                                diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
-                                diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
-                                diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
-                                diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
-                                diff_api.TexImage3D(GL_TEXTURE_3D, lvl,
+                                pState->diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
+                                pState->diff_api.TexImage3D(GL_TEXTURE_3D, lvl,
                                                     tl->internalFormat,
                                                     tl->width, tl->height, tl->depth,
@@ -830,5 +835,5 @@
                         {
                             if (tl->compressed) {
-                                diff_api.CompressedTexImage2DARB(GL_TEXTURE_RECTANGLE_NV, lvl,
+                                pState->diff_api.CompressedTexImage2DARB(GL_TEXTURE_RECTANGLE_NV, lvl,
                                                                  tl->internalFormat, tl->width,
                                                                  tl->height, tl->border,
@@ -837,9 +842,9 @@
                             else {
                                 /* alignment must be one */
-                                diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
-                                diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
-                                diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
-                                diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
-                                diff_api.TexImage2D(GL_TEXTURE_RECTANGLE_NV, lvl,
+                                pState->diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
+                                pState->diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
+                                pState->diff_api.TexImage2D(GL_TEXTURE_RECTANGLE_NV, lvl,
                                                     tl->internalFormat,
                                                     tl->width, tl->height, tl->border,
@@ -874,5 +879,5 @@
                         {
                             if (tl->generateMipmap) {
-                                diff_api.TexParameteri(GL_TEXTURE_CUBE_MAP_ARB,
+                                pState->diff_api.TexParameteri(GL_TEXTURE_CUBE_MAP_ARB,
                                                        GL_GENERATE_MIPMAP_SGIS, 1);
                             }
@@ -881,5 +886,5 @@
                             {
                                 if (tl->compressed) {
-                                    diff_api.CompressedTexImage2DARB(target,
+                                    pState->diff_api.CompressedTexImage2DARB(target,
                                                                      lvl, tl->internalFormat,
                                                                      tl->width, tl->height,
@@ -888,9 +893,9 @@
                                 else {
                                     /* alignment must be one */
-                                    diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
-                                    diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
-                                    diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
-                                    diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
-                                    diff_api.TexImage2D(target, lvl,
+                                    pState->diff_api.PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+                                    pState->diff_api.PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
+                                    pState->diff_api.PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
+                                    pState->diff_api.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
+                                    pState->diff_api.TexImage2D(target, lvl,
                                                         tl->internalFormat,
                                                         tl->width, tl->height, tl->border,
@@ -928,4 +933,5 @@
                                         CRContext *fromCtx, CRContext *toCtx )
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     CRTextureState *from = &(fromCtx->texture);
     CRTextureState *to = &(toCtx->texture);
@@ -935,9 +941,11 @@
     const GLboolean haveFragProg = fromCtx->extensions.ARB_fragment_program || fromCtx->extensions.NV_fragment_program;
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
 
-    able[0] = diff_api.Disable;
-    able[1] = diff_api.Enable;
+    able[0] = pState->diff_api.Disable;
+    able[1] = pState->diff_api.Enable;
 
     /* loop over texture units */
@@ -953,5 +961,5 @@
             /* Activate texture unit u if needed */
             if (fromCtx->texture.curTextureUnit != u) {
-                diff_api.ActiveTextureARB( GL_TEXTURE0_ARB + u);
+                pState->diff_api.ActiveTextureARB( GL_TEXTURE0_ARB + u);
                 fromCtx->texture.curTextureUnit = u;
             }
@@ -1011,5 +1019,5 @@
         {
             if (fromCtx->texture.curTextureUnit != u) {
-                diff_api.ActiveTextureARB( u + GL_TEXTURE0_ARB );
+                pState->diff_api.ActiveTextureARB( u + GL_TEXTURE0_ARB );
                 fromCtx->texture.curTextureUnit = u;
             }
@@ -1024,5 +1032,5 @@
                 f[2] = to->unit[u].objSCoeff.z;
                 f[3] = to->unit[u].objSCoeff.w;
-                diff_api.TexGenfv (GL_S, GL_OBJECT_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_S, GL_OBJECT_PLANE, (const GLfloat *) f);
                 from->unit[u].objSCoeff = to->unit[u].objSCoeff;
             }
@@ -1037,5 +1045,5 @@
                 f[2] = to->unit[u].objTCoeff.z;
                 f[3] = to->unit[u].objTCoeff.w;
-                diff_api.TexGenfv (GL_T, GL_OBJECT_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_T, GL_OBJECT_PLANE, (const GLfloat *) f);
                 from->unit[u].objTCoeff = to->unit[u].objTCoeff;
             }
@@ -1050,5 +1058,5 @@
                 f[2] = to->unit[u].objRCoeff.z;
                 f[3] = to->unit[u].objRCoeff.w;
-                diff_api.TexGenfv (GL_R, GL_OBJECT_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_R, GL_OBJECT_PLANE, (const GLfloat *) f);
                 from->unit[u].objRCoeff = to->unit[u].objRCoeff;
             }
@@ -1063,5 +1071,5 @@
                 f[2] = to->unit[u].objQCoeff.z;
                 f[3] = to->unit[u].objQCoeff.w;
-                diff_api.TexGenfv (GL_Q, GL_OBJECT_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_Q, GL_OBJECT_PLANE, (const GLfloat *) f);
                 from->unit[u].objQCoeff = to->unit[u].objQCoeff;
             }
@@ -1071,13 +1079,13 @@
         {
             if (fromCtx->texture.curTextureUnit != u) {
-                diff_api.ActiveTextureARB( u + GL_TEXTURE0_ARB );
+                pState->diff_api.ActiveTextureARB( u + GL_TEXTURE0_ARB );
                 fromCtx->texture.curTextureUnit = u;
             }
             if (fromCtx->transform.matrixMode != GL_MODELVIEW) {
-                diff_api.MatrixMode(GL_MODELVIEW);
+                pState->diff_api.MatrixMode(GL_MODELVIEW);
                 fromCtx->transform.matrixMode = GL_MODELVIEW;
             }
-            diff_api.PushMatrix();
-            diff_api.LoadIdentity();
+            pState->diff_api.PushMatrix();
+            pState->diff_api.LoadIdentity();
             if (from->unit[u].eyeSCoeff.x != to->unit[u].eyeSCoeff.x ||
                     from->unit[u].eyeSCoeff.y != to->unit[u].eyeSCoeff.y ||
@@ -1090,5 +1098,5 @@
                 f[2] = to->unit[u].eyeSCoeff.z;
                 f[3] = to->unit[u].eyeSCoeff.w;
-                diff_api.TexGenfv (GL_S, GL_EYE_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_S, GL_EYE_PLANE, (const GLfloat *) f);
                 from->unit[u].eyeSCoeff = to->unit[u].eyeSCoeff;
             }
@@ -1103,5 +1111,5 @@
                 f[2] = to->unit[u].eyeTCoeff.z;
                 f[3] = to->unit[u].eyeTCoeff.w;
-                diff_api.TexGenfv (GL_T, GL_EYE_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_T, GL_EYE_PLANE, (const GLfloat *) f);
                 from->unit[u].eyeTCoeff = to->unit[u].eyeTCoeff;
             }
@@ -1116,5 +1124,5 @@
                 f[2] = to->unit[u].eyeRCoeff.z;
                 f[3] = to->unit[u].eyeRCoeff.w;
-                diff_api.TexGenfv (GL_R, GL_EYE_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_R, GL_EYE_PLANE, (const GLfloat *) f);
                 from->unit[u].eyeRCoeff = to->unit[u].eyeRCoeff;
             }
@@ -1129,8 +1137,8 @@
                 f[2] = to->unit[u].eyeQCoeff.z;
                 f[3] = to->unit[u].eyeQCoeff.w;
-                diff_api.TexGenfv (GL_Q, GL_EYE_PLANE, (const GLfloat *) f);
+                pState->diff_api.TexGenfv (GL_Q, GL_EYE_PLANE, (const GLfloat *) f);
                 from->unit[u].eyeQCoeff = to->unit[u].eyeQCoeff;
             }
-            diff_api.PopMatrix();
+            pState->diff_api.PopMatrix();
             CLEARDIRTY(tb->eyeGen[u], nbitID);
         }
@@ -1138,5 +1146,5 @@
         {
             if (fromCtx->texture.curTextureUnit != u) {
-                diff_api.ActiveTextureARB( u + GL_TEXTURE0_ARB );
+                pState->diff_api.ActiveTextureARB( u + GL_TEXTURE0_ARB );
                 fromCtx->texture.curTextureUnit = u;
             }
@@ -1146,8 +1154,8 @@
                     from->unit[u].gen.q != to->unit[u].gen.q) 
             {
-                diff_api.TexGeni (GL_S, GL_TEXTURE_GEN_MODE, to->unit[u].gen.s);
-                diff_api.TexGeni (GL_T, GL_TEXTURE_GEN_MODE, to->unit[u].gen.t);
-                diff_api.TexGeni (GL_R, GL_TEXTURE_GEN_MODE, to->unit[u].gen.r);
-                diff_api.TexGeni (GL_Q, GL_TEXTURE_GEN_MODE, to->unit[u].gen.q);    
+                pState->diff_api.TexGeni (GL_S, GL_TEXTURE_GEN_MODE, to->unit[u].gen.s);
+                pState->diff_api.TexGeni (GL_T, GL_TEXTURE_GEN_MODE, to->unit[u].gen.t);
+                pState->diff_api.TexGeni (GL_R, GL_TEXTURE_GEN_MODE, to->unit[u].gen.r);
+                pState->diff_api.TexGeni (GL_Q, GL_TEXTURE_GEN_MODE, to->unit[u].gen.q);    
                 from->unit[u].gen = to->unit[u].gen;
             }
@@ -1160,5 +1168,5 @@
             if (from->unit[u].envMode != to->unit[u].envMode) 
             {
-                diff_api.TexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, to->unit[u].envMode);
+                pState->diff_api.TexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, to->unit[u].envMode);
                 from->unit[u].envMode = to->unit[u].envMode;
             }
@@ -1173,5 +1181,5 @@
                 f[2] = to->unit[u].envColor.b;
                 f[3] = to->unit[u].envColor.a;
-                diff_api.TexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (const GLfloat *) f);
+                pState->diff_api.TexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, (const GLfloat *) f);
                 from->unit[u].envColor = to->unit[u].envColor;
             }
@@ -1179,80 +1187,80 @@
             if (from->unit[u].combineModeRGB != to->unit[u].combineModeRGB)
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, to->unit[u].combineModeRGB);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, to->unit[u].combineModeRGB);
                 from->unit[u].combineModeRGB = to->unit[u].combineModeRGB;
             }
             if (from->unit[u].combineModeA != to->unit[u].combineModeA)
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, to->unit[u].combineModeA);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, to->unit[u].combineModeA);
                 from->unit[u].combineModeA = to->unit[u].combineModeA;
             }
             if (from->unit[u].combineSourceRGB[0] != to->unit[u].combineSourceRGB[0])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, to->unit[u].combineSourceRGB[0]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, to->unit[u].combineSourceRGB[0]);
                 from->unit[u].combineSourceRGB[0] = to->unit[u].combineSourceRGB[0];
             }
             if (from->unit[u].combineSourceRGB[1] != to->unit[u].combineSourceRGB[1])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, to->unit[u].combineSourceRGB[1]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, to->unit[u].combineSourceRGB[1]);
                 from->unit[u].combineSourceRGB[1] = to->unit[u].combineSourceRGB[1];
             }
             if (from->unit[u].combineSourceRGB[2] != to->unit[u].combineSourceRGB[2])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, to->unit[u].combineSourceRGB[2]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, to->unit[u].combineSourceRGB[2]);
                 from->unit[u].combineSourceRGB[2] = to->unit[u].combineSourceRGB[2];
             }
             if (from->unit[u].combineSourceA[0] != to->unit[u].combineSourceA[0])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, to->unit[u].combineSourceA[0]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, to->unit[u].combineSourceA[0]);
                 from->unit[u].combineSourceA[0] = to->unit[u].combineSourceA[0];
             }
             if (from->unit[u].combineSourceA[1] != to->unit[u].combineSourceA[1])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, to->unit[u].combineSourceA[1]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, to->unit[u].combineSourceA[1]);
                 from->unit[u].combineSourceA[1] = to->unit[u].combineSourceA[1];
             }
             if (from->unit[u].combineSourceA[2] != to->unit[u].combineSourceA[2])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, to->unit[u].combineSourceA[2]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, to->unit[u].combineSourceA[2]);
                 from->unit[u].combineSourceA[2] = to->unit[u].combineSourceA[2];
             }
             if (from->unit[u].combineOperandRGB[0] != to->unit[u].combineOperandRGB[0])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, to->unit[u].combineOperandRGB[0]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, to->unit[u].combineOperandRGB[0]);
                 from->unit[u].combineOperandRGB[0] = to->unit[u].combineOperandRGB[0];
             }
             if (from->unit[u].combineOperandRGB[1] != to->unit[u].combineOperandRGB[1])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, to->unit[u].combineOperandRGB[1]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, to->unit[u].combineOperandRGB[1]);
                 from->unit[u].combineOperandRGB[1] = to->unit[u].combineOperandRGB[1];
             }
             if (from->unit[u].combineOperandRGB[2] != to->unit[u].combineOperandRGB[2])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, to->unit[u].combineOperandRGB[2]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, to->unit[u].combineOperandRGB[2]);
                 from->unit[u].combineOperandRGB[2] = to->unit[u].combineOperandRGB[2];
             }
             if (from->unit[u].combineOperandA[0] != to->unit[u].combineOperandA[0])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, to->unit[u].combineOperandA[0]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, to->unit[u].combineOperandA[0]);
                 from->unit[u].combineOperandA[0] = to->unit[u].combineOperandA[0];
             }
             if (from->unit[u].combineOperandA[1] != to->unit[u].combineOperandA[1])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, to->unit[u].combineOperandA[1]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, to->unit[u].combineOperandA[1]);
                 from->unit[u].combineOperandA[1] = to->unit[u].combineOperandA[1];
             }
             if (from->unit[u].combineOperandA[2] != to->unit[u].combineOperandA[2])
             {
-                diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, to->unit[u].combineOperandA[2]);
+                pState->diff_api.TexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, to->unit[u].combineOperandA[2]);
                 from->unit[u].combineOperandA[2] = to->unit[u].combineOperandA[2];
             }
             if (from->unit[u].combineScaleRGB != to->unit[u].combineScaleRGB)
             {
-                diff_api.TexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, to->unit[u].combineScaleRGB);
+                pState->diff_api.TexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, to->unit[u].combineScaleRGB);
                 from->unit[u].combineScaleRGB = to->unit[u].combineScaleRGB;
             }
             if (from->unit[u].combineScaleA != to->unit[u].combineScaleA)
             {
-                diff_api.TexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, to->unit[u].combineScaleA);
+                pState->diff_api.TexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, to->unit[u].combineScaleA);
                 from->unit[u].combineScaleA = to->unit[u].combineScaleA;
             }
@@ -1261,5 +1269,5 @@
             if (from->unit[u].lodBias != to->unit[u].lodBias)
             {
-                diff_api.TexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, to->unit[u].lodBias);
+                pState->diff_api.TexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, to->unit[u].lodBias);
                 from->unit[u].lodBias = to->unit[u].lodBias;
             }
@@ -1323,5 +1331,5 @@
             /* Activate texture unit u if needed */
             if (fromCtx->texture.curTextureUnit != u) {
-                diff_api.ActiveTextureARB( GL_TEXTURE0_ARB + u);
+                pState->diff_api.ActiveTextureARB( GL_TEXTURE0_ARB + u);
                 fromCtx->texture.curTextureUnit = u;
             }
@@ -1332,5 +1340,5 @@
                 if (*fromBinding != tobj)
                 {
-                    diff_api.BindTexture(tobj->target, crStateGetTextureObjHWID(tobj));
+                    pState->diff_api.BindTexture(tobj->target, crStateGetTextureObjHWID(pState, tobj));
                     *fromBinding = tobj;
                 }
@@ -1352,9 +1360,9 @@
     /* After possible fiddling with the active unit, put it back now */
     if (fromCtx->texture.curTextureUnit != toCtx->texture.curTextureUnit) {
-        diff_api.ActiveTextureARB( toCtx->texture.curTextureUnit + GL_TEXTURE0_ARB );
+        pState->diff_api.ActiveTextureARB( toCtx->texture.curTextureUnit + GL_TEXTURE0_ARB );
         fromCtx->texture.curTextureUnit = toCtx->texture.curTextureUnit;
     }
     if (fromCtx->transform.matrixMode != toCtx->transform.matrixMode) {
-        diff_api.MatrixMode(toCtx->transform.matrixMode);
+        pState->diff_api.MatrixMode(toCtx->transform.matrixMode);
         fromCtx->transform.matrixMode = toCtx->transform.matrixMode;
     }
@@ -1404,4 +1412,5 @@
 crStateDiffAllTextureObjects( CRContext *g, CRbitvalue *bitID, GLboolean bForceUpdate )
 {
+    PCRStateTracker pState = g->pStateTracker;
     CRbitvalue nbitID[CR_MAX_BITARRAY];
     struct callback_info info;
@@ -1419,16 +1428,16 @@
     /* save current texture bindings */
     origUnit = g->texture.curTextureUnit;
-    orig1D = crStateGetTextureObjHWID(g->texture.unit[0].currentTexture1D);
-    orig2D = crStateGetTextureObjHWID(g->texture.unit[0].currentTexture2D);
-    orig3D = crStateGetTextureObjHWID(g->texture.unit[0].currentTexture3D);
+    orig1D = crStateGetTextureObjHWID(pState, g->texture.unit[0].currentTexture1D);
+    orig2D = crStateGetTextureObjHWID(pState, g->texture.unit[0].currentTexture2D);
+    orig3D = crStateGetTextureObjHWID(pState, g->texture.unit[0].currentTexture3D);
 #ifdef CR_ARB_texture_cube_map
-    origCube = crStateGetTextureObjHWID(g->texture.unit[0].currentTextureCubeMap);
+    origCube = crStateGetTextureObjHWID(pState, g->texture.unit[0].currentTextureCubeMap);
 #endif
 #ifdef CR_NV_texture_rectangle
-    origRect = crStateGetTextureObjHWID(g->texture.unit[0].currentTextureRect);
+    origRect = crStateGetTextureObjHWID(pState, g->texture.unit[0].currentTextureRect);
 #endif
 
     /* use texture unit 0 for updates */
-    diff_api.ActiveTextureARB(GL_TEXTURE0_ARB);
+    pState->diff_api.ActiveTextureARB(GL_TEXTURE0_ARB);
 
     /* diff all the textures */
@@ -1456,14 +1465,14 @@
     /* restore bindings */
     /* first restore unit 0 bindings the unit 0 is active currently */
-    diff_api.BindTexture(GL_TEXTURE_1D, orig1D);
-    diff_api.BindTexture(GL_TEXTURE_2D, orig2D);
-    diff_api.BindTexture(GL_TEXTURE_3D, orig3D);
+    pState->diff_api.BindTexture(GL_TEXTURE_1D, orig1D);
+    pState->diff_api.BindTexture(GL_TEXTURE_2D, orig2D);
+    pState->diff_api.BindTexture(GL_TEXTURE_3D, orig3D);
 #ifdef CR_ARB_texture_cube_map
-    diff_api.BindTexture(GL_TEXTURE_CUBE_MAP_ARB, origCube);
+    pState->diff_api.BindTexture(GL_TEXTURE_CUBE_MAP_ARB, origCube);
 #endif
 #ifdef CR_NV_texture_rectangle
-    diff_api.BindTexture(GL_TEXTURE_RECTANGLE_NV, origRect);
+    pState->diff_api.BindTexture(GL_TEXTURE_RECTANGLE_NV, origRect);
 #endif
     /* now restore the proper active unit */
-    diff_api.ActiveTextureARB(GL_TEXTURE0_ARB + origUnit);
+    pState->diff_api.ActiveTextureARB(GL_TEXTURE0_ARB + origUnit);
 }
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_teximage.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_teximage.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_teximage.c	(revision 78375)
@@ -335,12 +335,12 @@
  */
 static GLboolean
-ErrorCheckTexImage(GLuint dims, GLenum target, GLint level,
+ErrorCheckTexImage(PCRStateTracker pState, GLuint dims, GLenum target, GLint level,
                                      GLsizei width, GLsizei height, GLsizei depth, GLint border)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glTexImage%uD called in Begin/End", dims);
         return GL_TRUE;
@@ -355,5 +355,5 @@
         case GL_PROXY_TEXTURE_1D:
             if (dims != 1) {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                          "glTexImage(invalid target=0x%x)", target);
                 return GL_TRUE;
@@ -363,5 +363,5 @@
         case GL_PROXY_TEXTURE_2D:
             if (dims != 2) {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                          "glTexImage(invalid target=0x%x)", target);
                 return GL_TRUE;
@@ -371,5 +371,5 @@
         case GL_PROXY_TEXTURE_3D:
             if (dims != 3) {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                          "glTexImage(invalid target=0x%x)", target);
                 return GL_TRUE;
@@ -380,5 +380,5 @@
         case GL_PROXY_TEXTURE_RECTANGLE_NV:
             if (dims != 2 || !g->extensions.NV_texture_rectangle) {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                          "glTexImage2D(invalid target=0x%x)", target);
                 return GL_TRUE;
@@ -395,5 +395,5 @@
         case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
             if (dims != 2 || !g->extensions.ARB_texture_cube_map) {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                          "glTexImage2D(invalid target=0x%x)", target);
                 return GL_TRUE;
@@ -402,5 +402,5 @@
 #endif
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glTexImage%uD(invalid target=0x%x)", dims, target);
             return GL_TRUE;
@@ -412,5 +412,5 @@
     if (level < 0 || level > MaxTextureLevel(g, target)) {
         if (!IsProxyTarget(target))
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glTexImage%uD(level=%d)", dims, level);
         return GL_TRUE;
@@ -422,5 +422,5 @@
     if (border != 0 && border != 1) {
         if (!IsProxyTarget(target))
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glTexImage%uD(border=%d)", dims, border);
         return GL_TRUE;
@@ -430,5 +430,5 @@
              target == GL_TEXTURE_RECTANGLE_NV) && border != 0) {
         if (!IsProxyTarget(target))
-            crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                      "glTexImage2D(border=%d)", border);
         return GL_TRUE;
@@ -441,5 +441,5 @@
         if (!isLegalSize(g, width - 2 * border, g->limits.maxTextureSize)) {
             if (!IsProxyTarget(target))
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                          "glTexImage1D(width=%d)", width);
             return GL_TRUE;
@@ -450,5 +450,5 @@
                 !isLegalSize(g, height - 2 * border, g->limits.maxTextureSize)) {
             if (!IsProxyTarget(target))
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                          "glTexImage2D(width=%d, height=%d)", width, height);
             return GL_TRUE;
@@ -460,5 +460,5 @@
                 !isLegalSize(g, depth - 2 * border, g->limits.max3DTextureSize)) {
             if (!IsProxyTarget(target))
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                          "glTexImage3D(width=%d, height=%d, depth=%d)",
                                          width, height, depth);
@@ -471,5 +471,5 @@
                 height < 0 || height > (int) g->limits.maxRectTextureSize) {
             if (!IsProxyTarget(target))
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                          "glTexImage2D(width=%d, height=%d)", width, height);
             return GL_TRUE;
@@ -482,5 +482,5 @@
                 width != height) {
             if (!IsProxyTarget(target))
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                          "glTexImage2D(width=%d, height=%d)", width, height);
             return GL_TRUE;
@@ -499,14 +499,14 @@
  */
 static GLboolean
-ErrorCheckTexSubImage(GLuint dims, GLenum target, GLint level,
+ErrorCheckTexSubImage(PCRStateTracker pState, GLuint dims, GLenum target, GLint level,
                                             GLint xoffset, GLint yoffset, GLint zoffset,
                                             GLsizei width, GLsizei height, GLsizei depth)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureObj *tobj;
     CRTextureLevel *tl;
 
     if (g->current.inBeginEnd) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glTexSubImage%uD called in Begin/End", dims);
         return GL_TRUE;
@@ -515,5 +515,5 @@
     if (dims == 1) {
         if (target != GL_TEXTURE_1D) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glTexSubImage1D(target=0x%x)", target);
             return GL_TRUE;
@@ -529,5 +529,5 @@
                 target != GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB &&
                 target != GL_TEXTURE_RECTANGLE_NV) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glTexSubImage2D(target=0x%x)", target);
             return GL_TRUE;
@@ -536,5 +536,5 @@
     else if (dims == 3) {
         if (target != GL_TEXTURE_3D) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glTexSubImage3D(target=0x%x)", target);
             return GL_TRUE;
@@ -544,5 +544,5 @@
     /* test level */
     if (level < 0 || level > MaxTextureLevel(g, target)) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glTexSubImage%uD(level=%d)", dims, level);
         return GL_TRUE;
@@ -551,5 +551,5 @@
     crStateGetTextureObjectAndImage(g, target, level, &tobj, &tl);
     if (!tobj || !tl) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glTexSubImage%uD(target or level)", dims);
         return GL_TRUE;
@@ -558,5 +558,5 @@
     /* test x/y/zoffset and size */
     if (xoffset < -tl->border || xoffset + width > tl->width) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glTexSubImage%uD(xoffset=%d + width=%d > %d)",
                                  dims, xoffset, width, tl->width);
@@ -564,5 +564,5 @@
     }
     if (dims > 1 && (yoffset < -tl->border || yoffset + height > tl->height)) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glTexSubImage%uD(yoffset=%d + height=%d > %d)",
                                  dims, yoffset, height, tl->height);
@@ -570,5 +570,5 @@
     }
     if (dims > 2 && (zoffset < -tl->border || zoffset + depth > tl->depth)) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "glTexSubImage%uD(zoffset=%d and/or depth=%d)",
                                  dims, zoffset, depth);
@@ -583,9 +583,9 @@
 
 void STATE_APIENTRY
-crStateTexImage1D(GLenum target, GLint level, GLint internalFormat,
+crStateTexImage1D(PCRStateTracker pState, GLenum target, GLint level, GLint internalFormat,
                                     GLsizei width, GLint border, GLenum format,
                                     GLenum type, const GLvoid * pixels)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
 #ifndef CR_STATE_NO_TEXTURE_IMAGE_STORE
@@ -594,5 +594,5 @@
     CRTextureObj *tobj;
     CRTextureLevel *tl;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
 #ifdef CR_STATE_NO_TEXTURE_IMAGE_STORE
@@ -602,5 +602,5 @@
     FLUSH();
 
-    if (ErrorCheckTexImage(1, target, level, width, 1, 1, border)) {
+    if (ErrorCheckTexImage(pState, 1, target, level, width, 1, 1, border)) {
         if (IsProxyTarget(target)) {
             /* clear all state, but don't generate error */
@@ -631,10 +631,10 @@
         if (!tl->img)
         {
-            crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY,
+            crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY,
                          "glTexImage1D out of memory");
             return;
         }
         if (pixels)
-            crPixelCopy1D((GLvoid *) tl->img, format, type,
+            crPixelCopy1D(pState, (GLvoid *) tl->img, format, type,
                           pixels, format, type, width, &(c->unpack));
     }
@@ -699,7 +699,7 @@
 
 void STATE_APIENTRY
-crStateCopyTexImage2D(GLenum target, GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
-{
-    CRContext *g = GetCurrentContext();
+crStateCopyTexImage2D(PCRStateTracker pState, GLenum target, GLint level, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureObj *tobj = NULL;
     CRTextureLevel *tl = NULL;
@@ -707,5 +707,5 @@
 
     if (level < 0 || level > MaxTextureLevel(g, target)) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                      "crStateCopyTexImage2D: invalid level: %d", level);
         return;
@@ -718,5 +718,5 @@
     if (tobj == NULL || tl == NULL)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                      "crStateCopyTexImage2D: invalid target: 0x%x", target);
         return;
@@ -754,9 +754,9 @@
 
 void STATE_APIENTRY
-crStateTexImage2D(GLenum target, GLint level, GLint internalFormat,
+crStateTexImage2D(PCRStateTracker pState, GLenum target, GLint level, GLint internalFormat,
                   GLsizei width, GLsizei height, GLint border,
                   GLenum format, GLenum type, const GLvoid * pixels)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
 #ifndef CR_STATE_NO_TEXTURE_IMAGE_STORE
@@ -765,5 +765,5 @@
     CRTextureObj *tobj = NULL;
     CRTextureLevel *tl = NULL;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     // Distributed textures are not used by VBox
@@ -776,5 +776,5 @@
      */
     if (!is_distrib
-            && ErrorCheckTexImage(2, target, level, width, height, 1, border)) {
+            && ErrorCheckTexImage(pState, 2, target, level, width, height, 1, border)) {
         if (IsProxyTarget(target)) {
             /* clear all state, but don't generate error */
@@ -818,5 +818,5 @@
         if (!tl->img)
         {
-            crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY,
+            crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY,
                          "glTexImage2D out of memory");
             return;
@@ -892,5 +892,5 @@
 #if defined( CR_OPENGL_VERSION_1_2 ) || defined( GL_EXT_texture3D )
 void STATE_APIENTRY
-crStateTexImage3D(GLenum target, GLint level,
+crStateTexImage3D(PCRStateTracker pState, GLenum target, GLint level,
                                     GLint internalFormat,
                                     GLsizei width, GLsizei height,
@@ -898,5 +898,5 @@
                                     GLenum format, GLenum type, const GLvoid * pixels)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
 #ifndef CR_STATE_NO_TEXTURE_IMAGE_STORE
@@ -905,5 +905,5 @@
     CRTextureObj *tobj = NULL;
     CRTextureLevel *tl = NULL;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     (void)pixels;
@@ -911,5 +911,5 @@
     FLUSH();
 
-    if (ErrorCheckTexImage(3, target, level, width, height, depth, border)) {
+    if (ErrorCheckTexImage(pState, 3, target, level, width, height, depth, border)) {
         if (IsProxyTarget(target)) {
             /* clear all state, but don't generate error */
@@ -940,5 +940,5 @@
         if (!tl->img)
         {
-            crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY,
+            crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY,
                          "glTexImage3D out of memory");
             return;
@@ -979,5 +979,5 @@
 #ifdef GL_EXT_texture3D
 void STATE_APIENTRY
-crStateTexImage3DEXT(GLenum target, GLint level,
+crStateTexImage3DEXT(PCRStateTracker pState, GLenum target, GLint level,
                                          GLenum internalFormat,
                                          GLsizei width, GLsizei height, GLsizei depth,
@@ -985,5 +985,5 @@
                                          const GLvoid * pixels)
 {
-    crStateTexImage3D(target, level, (GLint) internalFormat, width, height,
+    crStateTexImage3D(pState, target, level, (GLint) internalFormat, width, height,
                                         depth, border, format, type, pixels);
 }
@@ -992,14 +992,14 @@
 
 void STATE_APIENTRY
-crStateTexSubImage1D(GLenum target, GLint level, GLint xoffset,
+crStateTexSubImage1D(PCRStateTracker pState, GLenum target, GLint level, GLint xoffset,
                                          GLsizei width, GLenum format,
                                          GLenum type, const GLvoid * pixels)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
 #ifndef CR_STATE_NO_TEXTURE_IMAGE_STORE
     CRClientState *c = &(g->client);
 #endif
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     CRTextureUnit *unit = t->unit + t->curTextureUnit;
@@ -1010,5 +1010,5 @@
     FLUSH();
 
-    if (ErrorCheckTexSubImage(1, target, level, xoffset, 0, 0,
+    if (ErrorCheckTexSubImage(pState, 1, target, level, xoffset, 0, 0,
                                                         width, 1, 1)) {
         return; /* GL error state already set */
@@ -1025,5 +1025,5 @@
     xoffset += tl->border;
 
-    crPixelCopy1D((void *) (tl->img + xoffset * tl->bytesPerPixel),
+    crPixelCopy1D(pState, (void *) (tl->img + xoffset * tl->bytesPerPixel),
                   tl->format, tl->type,
                   pixels, format, type, width, &(c->unpack));
@@ -1047,10 +1047,10 @@
 
 void STATE_APIENTRY
-crStateTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
+crStateTexSubImage2D(PCRStateTracker pState, GLenum target, GLint level, GLint xoffset, GLint yoffset,
                      GLsizei width, GLsizei height,
                      GLenum format, GLenum type, const GLvoid * pixels)
 {
-    CRContext *g = GetCurrentContext();
-    CRStateBits *sb = GetCurrentBits();
+    CRContext *g = GetCurrentContext(pState);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     CRTextureObj *tobj;
@@ -1067,5 +1067,5 @@
     FLUSH();
 
-    if (ErrorCheckTexSubImage(2, target, level, xoffset, yoffset, 0,
+    if (ErrorCheckTexSubImage(pState, 2, target, level, xoffset, yoffset, 0,
                                                         width, height, 1)) {
         return; /* GL error state already set */
@@ -1089,5 +1089,5 @@
     subimg = (GLubyte *) crAlloc(crImageSize(tl->format, tl->type, width, height));
 
-    crPixelCopy2D(width, height, subimg, tl->format, tl->type, NULL,    /* dst */
+    crPixelCopy2D(pState, width, height, subimg, tl->format, tl->type, NULL,    /* dst */
                   pixels, format, type, &(c->unpack));                  /* src */
 
@@ -1127,6 +1127,6 @@
         char *data;
 
-        diff_api.GetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &w);
-        diff_api.GetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &h);
+        pState->diff_api.GetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &w);
+        pState->diff_api.GetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &h);
 
         data = crAlloc(w*h*4);
@@ -1141,12 +1141,12 @@
 #if defined( CR_OPENGL_VERSION_1_2 )
 void STATE_APIENTRY
-crStateTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
+crStateTexSubImage3D(PCRStateTracker pState, GLenum target, GLint level, GLint xoffset, GLint yoffset,
                                          GLint zoffset, GLsizei width, GLsizei height,
                                          GLsizei depth, GLenum format, GLenum type,
                                          const GLvoid * pixels)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     CRTextureUnit *unit = t->unit + t->curTextureUnit;
@@ -1164,5 +1164,5 @@
     FLUSH();
 
-    if (ErrorCheckTexSubImage(3, target, level, xoffset, yoffset, zoffset,
+    if (ErrorCheckTexSubImage(pState, 3, target, level, xoffset, yoffset, zoffset,
                                                         width, height, depth)) {
         return; /* GL error state already set */
@@ -1186,5 +1186,5 @@
         crAlloc(crTextureSize(tl->format, tl->type, width, height, depth));
 
-    crPixelCopy3D(width, height, depth, subimg, tl->format, tl->type, NULL,
+    crPixelCopy3D(pState, width, height, depth, subimg, tl->format, tl->type, NULL,
                   pixels, format, type, &(c->unpack));
 
@@ -1224,14 +1224,14 @@
 
 void STATE_APIENTRY
-crStateCompressedTexImage1DARB(GLenum target, GLint level,
+crStateCompressedTexImage1DARB(PCRStateTracker pState, GLenum target, GLint level,
                                                              GLenum internalFormat, GLsizei width,
                                                              GLint border, GLsizei imageSize,
                                                              const GLvoid * data)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
     CRTextureObj *tobj;
     CRTextureLevel *tl;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     (void)data;
@@ -1239,5 +1239,5 @@
     FLUSH();
 
-    if (ErrorCheckTexImage(1, target, level, width, 1, 1, border)) {
+    if (ErrorCheckTexImage(pState, 1, target, level, width, 1, 1, border)) {
         if (IsProxyTarget(target)) {
             /* clear all state, but don't generate error */
@@ -1268,5 +1268,5 @@
         if (!tl->img)
         {
-            crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY,
+            crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY,
                          "glTexImage1D out of memory");
             return;
@@ -1305,14 +1305,14 @@
 
 void STATE_APIENTRY
-crStateCompressedTexImage2DARB(GLenum target, GLint level,
+crStateCompressedTexImage2DARB(PCRStateTracker pState, GLenum target, GLint level,
                                GLenum internalFormat, GLsizei width,
                                GLsizei height, GLint border,
                                GLsizei imageSize, const GLvoid * data)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
     CRTextureObj *tobj = NULL;
     CRTextureLevel *tl = NULL;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     (void)data;
@@ -1320,5 +1320,5 @@
     FLUSH();
 
-    if (ErrorCheckTexImage(2, target, level, width, height, 1, border)) {
+    if (ErrorCheckTexImage(pState, 2, target, level, width, height, 1, border)) {
         if (IsProxyTarget(target)) {
             /* clear all state, but don't generate error */
@@ -1349,5 +1349,5 @@
         if (!tl->img)
         {
-            crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY,
+            crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY,
                          "glTexImage2D out of memory");
             return;
@@ -1387,14 +1387,14 @@
 
 void STATE_APIENTRY
-crStateCompressedTexImage3DARB(GLenum target, GLint level,
+crStateCompressedTexImage3DARB(PCRStateTracker pState, GLenum target, GLint level,
                                                              GLenum internalFormat, GLsizei width,
                                                              GLsizei height, GLsizei depth, GLint border,
                                                              GLsizei imageSize, const GLvoid * data)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
     CRTextureObj *tobj = NULL;
     CRTextureLevel *tl = NULL;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     (void)data;
@@ -1402,5 +1402,5 @@
     FLUSH();
 
-    if (ErrorCheckTexImage(3, target, level, width, height, depth, border)) {
+    if (ErrorCheckTexImage(pState, 3, target, level, width, height, depth, border)) {
         if (IsProxyTarget(target)) {
             /* clear all state, but don't generate error */
@@ -1431,5 +1431,5 @@
         if (!tl->img)
         {
-            crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY,
+            crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY,
                          "glCompressedTexImage3D out of memory");
             return;
@@ -1469,11 +1469,11 @@
 
 void STATE_APIENTRY
-crStateCompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
+crStateCompressedTexSubImage1DARB(PCRStateTracker pState, GLenum target, GLint level, GLint xoffset,
                                                                     GLsizei width, GLenum format,
                                                                     GLsizei imageSize, const GLvoid * data)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     CRTextureUnit *unit = t->unit + t->curTextureUnit;
@@ -1484,5 +1484,5 @@
     FLUSH();
 
-    if (ErrorCheckTexSubImage(1, target, level, xoffset, 0, 0, width, 1, 1)) {
+    if (ErrorCheckTexSubImage(pState, 1, target, level, xoffset, 0, 0, width, 1, 1)) {
         return; /* GL error state already set */
     }
@@ -1526,12 +1526,12 @@
 
 void STATE_APIENTRY
-crStateCompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
+crStateCompressedTexSubImage2DARB(PCRStateTracker pState, GLenum target, GLint level, GLint xoffset,
                                                                     GLint yoffset, GLsizei width,
                                                                     GLsizei height, GLenum format,
                                                                     GLsizei imageSize, const GLvoid * data)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     CRTextureUnit *unit = t->unit + t->curTextureUnit;
@@ -1550,5 +1550,5 @@
 #endif
 
-    if (ErrorCheckTexSubImage(2, target, level, xoffset, yoffset, 0,
+    if (ErrorCheckTexSubImage(pState, 2, target, level, xoffset, yoffset, 0,
                                                         width, height, 1)) {
         return; /* GL error state already set */
@@ -1588,5 +1588,5 @@
 
 void STATE_APIENTRY
-crStateCompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
+crStateCompressedTexSubImage3DARB(PCRStateTracker pState, GLenum target, GLint level, GLint xoffset,
                                                                     GLint yoffset, GLint zoffset, GLsizei width,
                                                                     GLsizei height, GLsizei depth,
@@ -1594,7 +1594,7 @@
                                                                     const GLvoid * data)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     CRTextureUnit *unit = t->unit + t->curTextureUnit;
@@ -1613,5 +1613,5 @@
 #endif
 
-    if (ErrorCheckTexSubImage(3, target, level, xoffset, yoffset, zoffset,
+    if (ErrorCheckTexSubImage(pState, 3, target, level, xoffset, yoffset, zoffset,
                                                         width, height, depth)) {
         return; /* GL error state already set */
@@ -1652,7 +1652,7 @@
 
 void STATE_APIENTRY
-crStateGetCompressedTexImageARB(GLenum target, GLint level, GLvoid * img)
-{
-    CRContext *g = GetCurrentContext();
+crStateGetCompressedTexImageARB(PCRStateTracker pState, GLenum target, GLint level, GLvoid * img)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureObj *tobj;
     CRTextureLevel *tl;
@@ -1660,5 +1660,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetCompressedTexImage called in begin/end");
         return;
@@ -1667,5 +1667,5 @@
     crStateGetTextureObjectAndImage(g, target, level, &tobj, &tl);
     if (!tobj || !tl) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetCompressedTexImage(invalid target or level)");
         return;
@@ -1673,5 +1673,5 @@
 
     if (!tl->compressed) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetCompressedTexImage(not a compressed texture)");
         return;
@@ -1688,5 +1688,5 @@
     crMemcpy(img, tl->img, tl->bytes);
 #else
-    diff_api.GetCompressedTexImageARB(target, level, img);
+    pState->diff_api.GetCompressedTexImageARB(target, level, img);
 #endif
 }
@@ -1694,8 +1694,8 @@
 
 void STATE_APIENTRY
-crStateGetTexImage(GLenum target, GLint level, GLenum format,
+crStateGetTexImage(PCRStateTracker pState, GLenum target, GLint level, GLenum format,
                    GLenum type, GLvoid * pixels)
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
 #ifndef CR_STATE_NO_TEXTURE_IMAGE_STORE
     CRClientState *c = &(g->client);
@@ -1706,5 +1706,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glGetTexImage called in begin/end");
         return;
@@ -1713,5 +1713,5 @@
     crStateGetTextureObjectAndImage(g, target, level, &tobj, &tl);
     if (!tobj || !tl) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                  "glGetTexImage(invalid target or level)");
         return;
@@ -1742,5 +1742,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glGetTexImage called with bogus format: %d", format);
             return;
@@ -1758,5 +1758,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "glGetTexImage called with bogus type: %d", type);
             return;
@@ -1767,5 +1767,5 @@
     if (target == GL_TEXTURE_3D)
     {
-        crPixelCopy3D(tl->width, tl->height, tl->depth, (GLvoid *) pixels, format,
+        crPixelCopy3D(pState, tl->width, tl->height, tl->depth, (GLvoid *) pixels, format,
                       type, NULL, (tl->img), format, type, &(c->pack));
     }
@@ -1774,9 +1774,9 @@
     if ((target == GL_TEXTURE_1D) || (target == GL_TEXTURE_2D))
     {
-        crPixelCopy2D(tl->width, tl->height, (GLvoid *) pixels, format, type, NULL, /* dst */
+        crPixelCopy2D(pState, tl->width, tl->height, (GLvoid *) pixels, format, type, NULL, /* dst */
                       tl->img, format, type, &(c->pack));                           /* src */
     }
 #else
-    diff_api.GetTexImage(target, level, format, type, pixels);
-#endif
-}
+    pState->diff_api.GetTexImage(target, level, format, type, pixels);
+#endif
+}
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_texture.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_texture.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_texture.c	(revision 78375)
@@ -49,5 +49,5 @@
     CRLimitsState *limits = &ctx->limits;
     CRTextureState *t = &ctx->texture;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
     CRTextureBits *tb = &(sb->texture);
     unsigned int i;
@@ -202,5 +202,5 @@
 
 #ifndef IN_GUEST
-    crStateGetTextureObjHWID(tobj);
+    crStateGetTextureObjHWID(ctx->pStateTracker, tobj);
 #endif
 
@@ -520,7 +520,7 @@
  */
 CRTextureObj *
-crStateTextureGet(GLenum target, GLuint name) 
-{
-    CRContext *g = GetCurrentContext();
+crStateTextureGet(PCRStateTracker pState, GLenum target, GLuint name) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
     CRTextureObj *tobj;
@@ -635,7 +635,7 @@
 }
 
-void crStateRegTextures(GLsizei n, GLuint *names)
-{
-    CRContext *g = GetCurrentContext();
+void crStateRegTextures(PCRStateTracker pState, GLsizei n, GLuint *names)
+{
+    CRContext *g = GetCurrentContext(pState);
     crStateRegNames(g, g->shared->textureTable, n, names);
 }
@@ -643,4 +643,5 @@
 void crStateGenNames(CRContext *g, CRHashTable *table, GLsizei n, GLuint *names)
 {
+    PCRStateTracker pState = g->pStateTracker;
     GLint start;
 
@@ -649,5 +650,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "crStateGenNames called in Begin/End");
         return;
@@ -656,5 +657,5 @@
     if (n < 0)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "Negative n passed to crStateGenNames: %d", n);
         return;
@@ -670,19 +671,19 @@
     else
     {
-        crStateError(__LINE__, __FILE__, GL_OUT_OF_MEMORY, "glGenTextures");
-    }
-}
-
-void STATE_APIENTRY crStateGenTextures(GLsizei n, GLuint *textures) 
-{
-    CRContext *g = GetCurrentContext();
+        crStateError(pState, __LINE__, __FILE__, GL_OUT_OF_MEMORY, "glGenTextures");
+    }
+}
+
+void STATE_APIENTRY crStateGenTextures(PCRStateTracker pState, GLsizei n, GLuint *textures) 
+{
+    CRContext *g = GetCurrentContext(pState);
     crStateGenNames(g, g->shared->textureTable, n, textures);
 }
 
-static void crStateTextureCheckFBOAPs(GLenum target, GLuint texture)
+static void crStateTextureCheckFBOAPs(PCRStateTracker pState, GLenum target, GLuint texture)
 {
     GLuint u;
     CRFBOAttachmentPoint *ap;
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRFramebufferObjectState *fbo = &g->framebufferobject;
     CRFramebufferObject *pFBO;
@@ -696,5 +697,5 @@
         if (ap->type==GL_TEXTURE && ap->name==texture)
         {
-            crStateFramebufferTexture1DEXT(target, u+GL_COLOR_ATTACHMENT0_EXT, 0, 0, 0);
+            crStateFramebufferTexture1DEXT(pState, target, u+GL_COLOR_ATTACHMENT0_EXT, 0, 0, 0);
         }
     }
@@ -703,5 +704,5 @@
     if (ap->type==GL_TEXTURE && ap->name==texture)
     {
-        crStateFramebufferTexture1DEXT(target, GL_DEPTH_ATTACHMENT_EXT, 0, 0, 0);
+        crStateFramebufferTexture1DEXT(pState, target, GL_DEPTH_ATTACHMENT_EXT, 0, 0, 0);
     }
 
@@ -709,5 +710,5 @@
     if (ap->type==GL_TEXTURE && ap->name==texture)
     {
-        crStateFramebufferTexture1DEXT(target, GL_STENCIL_ATTACHMENT_EXT, 0, 0, 0);
+        crStateFramebufferTexture1DEXT(pState, target, GL_STENCIL_ATTACHMENT_EXT, 0, 0, 0);
     }
 }
@@ -715,4 +716,5 @@
 static void crStateCleanupTextureRefs(CRContext *g, CRTextureObj *tObj)
 {
+    PCRStateTracker pState = g->pStateTracker;
     CRTextureState *t = &(g->texture);
     GLuint u;
@@ -751,15 +753,15 @@
 
 #ifdef CR_EXT_framebuffer_object
-        crStateTextureCheckFBOAPs(GL_DRAW_FRAMEBUFFER, tObj->id);
-        crStateTextureCheckFBOAPs(GL_READ_FRAMEBUFFER, tObj->id);
-#endif
-    }
-}
-
-void STATE_APIENTRY crStateDeleteTextures(GLsizei n, const GLuint *textures) 
-{
-    CRContext *g = GetCurrentContext();
+        crStateTextureCheckFBOAPs(pState, GL_DRAW_FRAMEBUFFER, tObj->id);
+        crStateTextureCheckFBOAPs(pState, GL_READ_FRAMEBUFFER, tObj->id);
+#endif
+    }
+}
+
+void STATE_APIENTRY crStateDeleteTextures(PCRStateTracker pState, GLsizei n, const GLuint *textures) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     int i;
@@ -769,5 +771,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glDeleteTextures called in Begin/End");
         return;
@@ -776,5 +778,5 @@
     if (n < 0)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                                  "Negative n passed to glDeleteTextures: %d", n);
         return;
@@ -803,5 +805,5 @@
                  * This is why g_pAvailableContexts[j] could be NULL
                  * also g_pAvailableContexts[0] will hold default context, which we should discard */
-                CRContext *ctx = g_pAvailableContexts[j];
+                CRContext *ctx = pState->apAvailableContexts[j];
                 if (j && ctx)
                 {
@@ -831,7 +833,7 @@
 
 
-void STATE_APIENTRY crStateClientActiveTextureARB( GLenum texture )
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateClientActiveTextureARB(PCRStateTracker pState, GLenum texture )
+{
+    CRContext *g = GetCurrentContext(pState);
     CRClientState *c = &(g->client);
 
@@ -839,5 +841,5 @@
 
     if (!g->extensions.ARB_multitexture) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glClientActiveTextureARB not available");
         return;
@@ -846,5 +848,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glClientActiveTextureARB called in Begin/End");
         return;
@@ -854,5 +856,5 @@
              texture >= GL_TEXTURE0_ARB + g->limits.maxTextureUnits)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "crStateClientActiveTexture: unit = %d (max is %d)",
                                  texture, g->limits.maxTextureUnits );
@@ -862,10 +864,10 @@
     c->curClientTextureUnit = texture - GL_TEXTURE0_ARB;
 
-    DIRTY(GetCurrentBits()->client.dirty, g->neg_bitid);
-}
-
-void STATE_APIENTRY crStateActiveTextureARB( GLenum texture )
-{
-    CRContext *g = GetCurrentContext();
+    DIRTY(GetCurrentBits(pState)->client.dirty, g->neg_bitid);
+}
+
+void STATE_APIENTRY crStateActiveTextureARB(PCRStateTracker pState, GLenum texture )
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
 
@@ -873,5 +875,5 @@
 
     if (!g->extensions.ARB_multitexture) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "glActiveTextureARB not available");
         return;
@@ -880,5 +882,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glActiveTextureARB called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glActiveTextureARB called in Begin/End");
         return;
     }
@@ -886,5 +888,5 @@
     if ( texture < GL_TEXTURE0_ARB || texture >= GL_TEXTURE0_ARB + g->limits.maxTextureUnits)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "Bad texture unit passed to crStateActiveTexture: %d (max is %d)", texture, g->limits.maxTextureUnits );
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "Bad texture unit passed to crStateActiveTexture: %d (max is %d)", texture, g->limits.maxTextureUnits );
         return;
     }
@@ -894,5 +896,5 @@
     /* update the current matrix pointer, etc. */
     if (g->transform.matrixMode == GL_TEXTURE) {
-        crStateMatrixMode(GL_TEXTURE);
+        crStateMatrixMode(pState, GL_TEXTURE);
     }
 }
@@ -903,8 +905,8 @@
 # endif
 
-DECLEXPORT(void) crStatePinTexture(GLuint texture, GLboolean pin)
+DECLEXPORT(void) crStatePinTexture(PCRStateTracker pState, GLuint texture, GLboolean pin)
 {
     CRTextureObj * pTobj;
-    CRSharedState *pShared = crStateGlobalSharedAcquire();
+    CRSharedState *pShared = crStateGlobalSharedAcquire(pState);
     if (pShared)
     {
@@ -929,5 +931,5 @@
             {
                 if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pTobj))
-                    crStateOnTextureUsageRelease(pShared, pTobj);
+                    crStateOnTextureUsageRelease(pState, pShared, pTobj);
             }
         }
@@ -935,5 +937,5 @@
             WARN(("texture %d not defined", texture));
 
-        crStateGlobalSharedRelease();
+        crStateGlobalSharedRelease(pState);
     }
     else
@@ -942,7 +944,7 @@
 #endif
 
-DECLEXPORT(void) crStateSetTextureUsed(GLuint texture, GLboolean used)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(void) crStateSetTextureUsed(PCRStateTracker pState, GLuint texture, GLboolean used)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureObj *tobj;
 
@@ -973,5 +975,5 @@
     else
     {
-        CRStateBits *sb = GetCurrentBits();
+        CRStateBits *sb = GetCurrentBits(pState);
         CRTextureBits *tb = &(sb->texture);
         CRTextureState *t = &(g->texture);
@@ -986,10 +988,10 @@
 }
 
-void STATE_APIENTRY crStateBindTexture(GLenum target, GLuint texture) 
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateBindTexture(PCRStateTracker pState, GLenum target, GLuint texture) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
     CRTextureObj *tobj;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
 
@@ -998,5 +1000,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glBindTexture called in Begin/End");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glBindTexture called in Begin/End");
         return;
     }
@@ -1021,5 +1023,5 @@
             case GL_TEXTURE_CUBE_MAP_ARB:
                 if (!g->extensions.ARB_texture_cube_map) {
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "Invalid target passed to glBindTexture: %d", target);
                     return;
@@ -1031,5 +1033,5 @@
             case GL_TEXTURE_RECTANGLE_NV:
                 if (!g->extensions.NV_texture_rectangle) {
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "Invalid target passed to glBindTexture: %d", target);
                     return;
@@ -1039,5 +1041,5 @@
 #endif
             default:
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid target passed to glBindTexture: %d", target);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid target passed to glBindTexture: %d", target);
                 return;
         }
@@ -1070,5 +1072,5 @@
     {
         crWarning( "You called glBindTexture with a target of 0x%x, but the texture you wanted was target 0x%x [1D: %x 2D: %x 3D: %x cube: %x]", (int) target, (int) tobj->target, GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP );
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "Attempt to bind a texture of different dimensions");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "Attempt to bind a texture of different dimensions");
         return;
     }
@@ -1099,5 +1101,5 @@
 #endif
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                      "Invalid target passed to glBindTexture: %d", target);
             return;
@@ -1110,11 +1112,11 @@
 
 void STATE_APIENTRY
-crStateTexParameterfv(GLenum target, GLenum pname, const GLfloat *param) 
-{
-    CRContext *g = GetCurrentContext();
+crStateTexParameterfv(PCRStateTracker pState, GLenum target, GLenum pname, const GLfloat *param) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureObj *tobj = NULL;
     CRTextureLevel *tl = NULL;
     GLenum e = (GLenum) *param;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     unsigned int i;
@@ -1124,5 +1126,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                     "TexParameterfv called in Begin/End");
         return;
@@ -1131,5 +1133,5 @@
     crStateGetTextureObjectAndImage(g, target, 0, &tobj, &tl);
     if (!tobj) {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "TexParamterfv(invalid target=0x%x)", target);
         return;
@@ -1146,5 +1148,5 @@
                     e != GL_LINEAR_MIPMAP_LINEAR)
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                             "TexParamterfv: GL_TEXTURE_MIN_FILTER invalid param: %d", e);
                 return;
@@ -1155,5 +1157,5 @@
             if (e != GL_NEAREST && e != GL_LINEAR)
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                             "TexParamterfv: GL_TEXTURE_MAG_FILTER invalid param: %d", e);
                 return;
@@ -1191,5 +1193,5 @@
 #endif
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "TexParameterfv: GL_TEXTURE_WRAP_S invalid param: 0x%x", e);
                 return;
@@ -1226,5 +1228,5 @@
 #endif
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "TexParameterfv: GL_TEXTURE_WRAP_T invalid param: 0x%x", e);
                 return;
@@ -1260,5 +1262,5 @@
 #endif
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "TexParameterfv: GL_TEXTURE_WRAP_R invalid param: 0x%x", e);
                 return;
@@ -1277,5 +1279,5 @@
             if (e < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "TexParameterfv: GL_TEXTURE_BASE_LEVEL invalid param: 0x%x", e);
                 return;
@@ -1286,5 +1288,5 @@
             if (e < 0.0f) 
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "TexParameterfv: GL_TEXTURE_MAX_LEVEL invalid param: 0x%x", e);
                 return;
@@ -1304,5 +1306,5 @@
                 if (param[0] < 1.0f)
                 {
-                    crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                         "TexParameterfv: GL_TEXTURE_MAX_ANISOTROPY_EXT called with parameter less than 1: %f", param[0]);
                     return;
@@ -1326,5 +1328,5 @@
                 else
                 {
-                    crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                         "TexParameterfv: GL_DEPTH_TEXTURE_MODE_ARB called with invalid parameter: 0x%x", param[0]);
                     return;
@@ -1342,5 +1344,5 @@
                 else
                 {
-                    crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                         "TexParameterfv: GL_TEXTURE_COMPARE_MODE_ARB called with invalid parameter: 0x%x", param[0]);
                     return;
@@ -1368,5 +1370,5 @@
 #endif
             else {
-                    crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
                         "TexParameterfv: GL_TEXTURE_COMPARE_FUNC_ARB called with invalid parameter: 0x%x", param[0]);
                     return;
@@ -1389,5 +1391,5 @@
 #endif
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                 "TexParamterfv: Invalid pname: %d", pname);
             return;
@@ -1404,5 +1406,5 @@
 
 void STATE_APIENTRY
-crStateTexParameteriv(GLenum target, GLenum pname, const GLint *param) 
+crStateTexParameteriv(PCRStateTracker pState, GLenum target, GLenum pname, const GLint *param) 
 {
     GLfloat f_param;
@@ -1439,5 +1441,5 @@
 #endif
             f_param = (GLfloat) (*param);
-            crStateTexParameterfv( target, pname, &(f_param) );
+            crStateTexParameterfv(pState, target, pname, &(f_param) );
             break;
         case GL_TEXTURE_BORDER_COLOR:
@@ -1446,8 +1448,8 @@
             f_color.b = ((GLfloat) param[2])/CR_MAXINT;
             f_color.a = ((GLfloat) param[3])/CR_MAXINT;
-            crStateTexParameterfv( target, pname, (const GLfloat *) &(f_color) );
+            crStateTexParameterfv(pState, target, pname, (const GLfloat *) &(f_color) );
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                 "TexParamteriv: Invalid pname: %d", pname);
             return;
@@ -1457,23 +1459,23 @@
 
 void STATE_APIENTRY
-crStateTexParameterf(GLenum target, GLenum pname, GLfloat param) 
-{
-    crStateTexParameterfv( target, pname, &param );
+crStateTexParameterf(PCRStateTracker pState, GLenum target, GLenum pname, GLfloat param) 
+{
+    crStateTexParameterfv(pState, target, pname, &param );
 }
 
 
 void STATE_APIENTRY
-crStateTexParameteri(GLenum target, GLenum pname, GLint param) {
+crStateTexParameteri(PCRStateTracker pState, GLenum target, GLenum pname, GLint param) {
     GLfloat f_param = (GLfloat) param;
-    crStateTexParameterfv( target, pname, &f_param );
+    crStateTexParameterfv(pState, target, pname, &f_param );
 }
 
 
 void STATE_APIENTRY
-crStateTexEnvfv(GLenum target, GLenum pname, const GLfloat *param) 
-{
-    CRContext *g = GetCurrentContext();
+crStateTexEnvfv(PCRStateTracker pState, GLenum target, GLenum pname, const GLfloat *param) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
     GLenum e;
@@ -1487,5 +1489,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                     "glTexEnvfv called in begin/end");
         return;
@@ -1495,5 +1497,5 @@
     if (target == GL_TEXTURE_FILTER_CONTROL_EXT) {
         if (!g->extensions.EXT_texture_lod_bias || pname != GL_TEXTURE_LOD_BIAS_EXT) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnv");
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnv");
         }
         else {
@@ -1509,5 +1511,5 @@
     if (target == GL_POINT_SPRITE_ARB) {
         if (!g->extensions.ARB_point_sprite || pname != GL_COORD_REPLACE_ARB) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnv");
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnv");
         }
         else {
@@ -1523,5 +1525,5 @@
     if (target != GL_TEXTURE_ENV)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glTexEnvfv: target != GL_TEXTURE_ENV: %d", target);
         return;
@@ -1539,5 +1541,5 @@
                     e != GL_COMBINE_ARB)
             {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                             "glTexEnvfv: invalid param: %f", *param);
                 return;
@@ -1583,5 +1585,5 @@
 #endif
             else {
-                 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv(param=0x%x", e);
+                 crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv(param=0x%x", e);
                  return;
             }
@@ -1599,5 +1601,5 @@
             }
             else {
-                 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv");
+                 crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv");
                  return;
             }
@@ -1621,5 +1623,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv");
                 return;
             }
@@ -1643,5 +1645,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv");
                 return;
             }
@@ -1660,5 +1662,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv");
                 return;
             }
@@ -1675,5 +1677,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv(param=0x%x)", e);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glTexEnvfv(param=0x%x)", e);
                 return;
             }
@@ -1687,5 +1689,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glTexEnvfv");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glTexEnvfv");
                 return;
             }
@@ -1699,5 +1701,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_VALUE, "glTexEnvfv");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE, "glTexEnvfv");
                 return;
             }
@@ -1706,5 +1708,5 @@
 
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "glTexEnvfv: invalid pname: %d", pname);
             return;
@@ -1717,5 +1719,5 @@
 
 void STATE_APIENTRY
-crStateTexEnviv(GLenum target, GLenum pname, const GLint *param) 
+crStateTexEnviv(PCRStateTracker pState, GLenum target, GLenum pname, const GLint *param) 
 {
     GLfloat f_param;
@@ -1725,5 +1727,5 @@
         case GL_TEXTURE_ENV_MODE:
             f_param = (GLfloat) (*param);
-            crStateTexEnvfv( target, pname, &f_param );
+            crStateTexEnvfv(pState, target, pname, &f_param );
             break;
         case GL_TEXTURE_ENV_COLOR:
@@ -1732,5 +1734,5 @@
             f_color.b = ((GLfloat) param[2]) / CR_MAXINT;
             f_color.a = ((GLfloat) param[3]) / CR_MAXINT;
-            crStateTexEnvfv( target, pname, (const GLfloat *) &f_color );
+            crStateTexEnvfv(pState, target, pname, (const GLfloat *) &f_color );
             break;
 #ifdef CR_ARB_texture_env_combine
@@ -1752,5 +1754,5 @@
         case GL_ALPHA_SCALE:
             f_param = (GLfloat) (*param);
-            crStateTexEnvfv( target, pname, &f_param );
+            crStateTexEnvfv(pState, target, pname, &f_param );
             break;
 #endif
@@ -1758,5 +1760,5 @@
         case GL_TEXTURE_LOD_BIAS_EXT:
             f_param = (GLfloat) (*param);
-            crStateTexEnvfv( target, pname, &f_param);
+            crStateTexEnvfv(pState, target, pname, &f_param);
             break;
 #endif
@@ -1764,10 +1766,10 @@
         case GL_COORD_REPLACE_ARB:
             f_param = (GLfloat) *param;
-            crStateTexEnvfv( target, pname, &f_param);
+            crStateTexEnvfv(pState, target, pname, &f_param);
             break;
 #endif
 
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "glTexEnvfv: invalid pname: %d", pname);
             return;
@@ -1777,27 +1779,27 @@
 
 void STATE_APIENTRY
-crStateTexEnvf(GLenum target, GLenum pname, GLfloat param) 
-{
-    crStateTexEnvfv( target, pname, &param );
+crStateTexEnvf(PCRStateTracker pState, GLenum target, GLenum pname, GLfloat param) 
+{
+    crStateTexEnvfv(pState, target, pname, &param );
 }
 
 
 void STATE_APIENTRY
-crStateTexEnvi(GLenum target, GLenum pname, GLint param) 
+crStateTexEnvi(PCRStateTracker pState, GLenum target, GLenum pname, GLint param) 
 {
     GLfloat f_param = (GLfloat) param;
-    crStateTexEnvfv( target, pname, &f_param );
+    crStateTexEnvfv(pState, target, pname, &f_param );
 }
 
 
 void STATE_APIENTRY
-crStateGetTexEnvfv(GLenum target, GLenum pname, GLfloat *param) 
-{
-    CRContext *g = GetCurrentContext();
+crStateGetTexEnvfv(PCRStateTracker pState, GLenum target, GLenum pname, GLfloat *param) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__,GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__,GL_INVALID_OPERATION,
                     "glGetTexEnvfv called in begin/end");
         return;
@@ -1807,5 +1809,5 @@
     if (target == GL_TEXTURE_FILTER_CONTROL_EXT) {
         if (!g->extensions.EXT_texture_lod_bias || pname != GL_TEXTURE_LOD_BIAS_EXT) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnv");
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnv");
         }
         else {
@@ -1819,5 +1821,5 @@
     if (target == GL_POINT_SPRITE_ARB) {
         if (!g->extensions.ARB_point_sprite || pname != GL_COORD_REPLACE_ARB) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnv");
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnv");
         }
         else {
@@ -1830,5 +1832,5 @@
     if (target != GL_TEXTURE_ENV)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexEnvfv: target != GL_TEXTURE_ENV: %d", target);
         return;
@@ -1850,5 +1852,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1859,5 +1861,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1868,5 +1870,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1877,5 +1879,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1886,5 +1888,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1895,5 +1897,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1904,5 +1906,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1913,5 +1915,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1922,5 +1924,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1931,5 +1933,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1940,5 +1942,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1949,5 +1951,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1958,5 +1960,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1967,5 +1969,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1976,5 +1978,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
@@ -1985,10 +1987,10 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
                 return;
             }
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "glGetTexEnvfv: invalid pname: %d", pname);
             return;
@@ -1998,12 +2000,12 @@
 
 void STATE_APIENTRY
-crStateGetTexEnviv(GLenum target, GLenum pname, GLint *param) 
-{
-    CRContext *g = GetCurrentContext();
+crStateGetTexEnviv(PCRStateTracker pState, GLenum target, GLenum pname, GLint *param) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__,GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__,GL_INVALID_OPERATION,
                     "glGetTexEnviv called in begin/end");
         return;
@@ -2013,5 +2015,5 @@
     if (target == GL_TEXTURE_FILTER_CONTROL_EXT) {
         if (!g->extensions.EXT_texture_lod_bias || pname != GL_TEXTURE_LOD_BIAS_EXT) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnv");
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnv");
         }
         else {
@@ -2025,5 +2027,5 @@
     if (target == GL_POINT_SPRITE_ARB) {
         if (!g->extensions.ARB_point_sprite || pname != GL_COORD_REPLACE_ARB) {
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnv");
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnv");
         }
         else {
@@ -2036,5 +2038,5 @@
     if (target != GL_TEXTURE_ENV)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexEnviv: target != GL_TEXTURE_ENV: %d", target);
         return;
@@ -2056,5 +2058,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2065,5 +2067,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2074,5 +2076,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2083,5 +2085,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2092,5 +2094,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2101,5 +2103,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2110,5 +2112,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2119,5 +2121,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2128,5 +2130,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2137,5 +2139,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2146,5 +2148,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2155,5 +2157,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2164,5 +2166,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2173,5 +2175,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2182,5 +2184,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
@@ -2191,10 +2193,10 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "glGetTexEnviv(pname)");
                 return;
             }
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "glGetTexEnviv: invalid pname: %d", pname);
             return;
@@ -2204,7 +2206,7 @@
 
 void STATE_APIENTRY
-crStateTexGendv(GLenum coord, GLenum pname, const GLdouble *param) 
-{
-    CRContext *g = GetCurrentContext();
+crStateTexGendv(PCRStateTracker pState, GLenum coord, GLenum pname, const GLdouble *param) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
     CRTransformState *trans = &(g->transform);
@@ -2212,5 +2214,5 @@
     GLenum e;
     CRmatrix inv;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTextureBits *tb = &(sb->texture);
 
@@ -2219,5 +2221,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                     "glTexGen called in begin/end");
         return;
@@ -2244,5 +2246,5 @@
                     }
                     else {
-                        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                             "glTexGendv called with bad param: %lf", *param);
                         return;
@@ -2270,5 +2272,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glTexGendv called with bogus pname: %d", pname);
                     return;
@@ -2292,5 +2294,5 @@
                     }
                     else {
-                        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                             "glTexGendv called with bad param: %lf", *param);
                         return;
@@ -2318,5 +2320,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glTexGen called with bogus pname: %d", pname);
                     return;
@@ -2340,5 +2342,5 @@
                     }
                     else {
-                        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                             "glTexGen called with bad param: %lf", *param);
                         return;
@@ -2366,5 +2368,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glTexGen called with bogus pname: %d", pname);
                     return;
@@ -2388,5 +2390,5 @@
                     }
                     else {
-                        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                             "glTexGen called with bad param: %lf", *param);
                         return;
@@ -2414,5 +2416,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glTexGen called with bogus pname: %d", pname);
                     return;
@@ -2420,5 +2422,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "glTexGen called with bogus coord: %d", coord);
             return;
@@ -2428,5 +2430,5 @@
 
 void STATE_APIENTRY
-crStateTexGenfv(GLenum coord, GLenum pname, const GLfloat *param) 
+crStateTexGenfv(PCRStateTracker pState, GLenum coord, GLenum pname, const GLfloat *param) 
 {
     GLdouble d_param;
@@ -2436,5 +2438,5 @@
         case GL_TEXTURE_GEN_MODE:
             d_param = (GLdouble) *param;
-            crStateTexGendv( coord, pname, &d_param );
+            crStateTexGendv(pState, coord, pname, &d_param );
             break;
         case GL_OBJECT_PLANE:
@@ -2444,8 +2446,8 @@
             d_vector.z = (GLdouble) param[2];
             d_vector.w = (GLdouble) param[3];
-            crStateTexGendv( coord, pname, (const double *) &d_vector );
+            crStateTexGendv(pState, coord, pname, (const double *) &d_vector );
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "glTexGen called with bogus pname: %d", pname);
             return;
@@ -2455,5 +2457,5 @@
 
 void STATE_APIENTRY
-crStateTexGeniv(GLenum coord, GLenum pname, const GLint *param) 
+crStateTexGeniv(PCRStateTracker pState, GLenum coord, GLenum pname, const GLint *param) 
 {
     GLdouble d_param;
@@ -2463,5 +2465,5 @@
         case GL_TEXTURE_GEN_MODE:
             d_param = (GLdouble) *param;
-            crStateTexGendv( coord, pname, &d_param );
+            crStateTexGendv(pState, coord, pname, &d_param );
             break;
         case GL_OBJECT_PLANE:
@@ -2471,8 +2473,8 @@
             d_vector.z = (GLdouble) param[2];
             d_vector.w = (GLdouble) param[3];
-            crStateTexGendv( coord, pname, (const double *) &d_vector );
+            crStateTexGendv(pState, coord, pname, (const double *) &d_vector );
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "glTexGen called with bogus pname: %d", pname);
             return;
@@ -2482,35 +2484,35 @@
 
 void STATE_APIENTRY
-crStateTexGend (GLenum coord, GLenum pname, GLdouble param) 
-{
-    crStateTexGendv( coord, pname, &param );
+crStateTexGend (PCRStateTracker pState, GLenum coord, GLenum pname, GLdouble param) 
+{
+    crStateTexGendv(pState, coord, pname, &param );
 }
 
 
 void STATE_APIENTRY
-crStateTexGenf(GLenum coord, GLenum pname, GLfloat param) 
+crStateTexGenf(PCRStateTracker pState, GLenum coord, GLenum pname, GLfloat param) 
 {
     GLdouble d_param = (GLdouble) param;
-    crStateTexGendv( coord, pname, &d_param );
+    crStateTexGendv(pState, coord, pname, &d_param );
 }
 
 
 void STATE_APIENTRY
-crStateTexGeni(GLenum coord, GLenum pname, GLint param) 
+crStateTexGeni(PCRStateTracker pState, GLenum coord, GLenum pname, GLint param) 
 {
     GLdouble d_param = (GLdouble) param;
-    crStateTexGendv( coord, pname, &d_param );
+    crStateTexGendv(pState, coord, pname, &d_param );
 }
 
 
 void STATE_APIENTRY
-crStateGetTexGendv(GLenum coord, GLenum pname, GLdouble *param) 
-{
-    CRContext *g = GetCurrentContext();
+crStateGetTexGendv(PCRStateTracker pState, GLenum coord, GLenum pname, GLdouble *param) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                     "glGetTexGen called in begin/end");
         return;
@@ -2533,5 +2535,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glGetTexGen called with bogus coord: %d", coord);
                     return;
@@ -2565,5 +2567,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glGetTexGen called with bogus coord: %d", coord);
                     return;
@@ -2597,5 +2599,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glGetTexGen called with bogus coord: %d", coord);
                     return;
@@ -2603,5 +2605,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "glGetTexGen called with bogus pname: %d", pname);
             return;
@@ -2611,12 +2613,12 @@
 
 void STATE_APIENTRY
-crStateGetTexGenfv(GLenum coord, GLenum pname, GLfloat *param)
-{
-    CRContext *g = GetCurrentContext();
+crStateGetTexGenfv(PCRStateTracker pState, GLenum coord, GLenum pname, GLfloat *param)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                     "glGetTexGenfv called in begin/end");
         return;
@@ -2639,5 +2641,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glGetTexGenfv called with bogus coord: %d", coord);
                     return;
@@ -2671,5 +2673,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glGetTexGenfv called with bogus coord: %d", coord);
                     return;
@@ -2703,5 +2705,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glGetTexGenfv called with bogus coord: %d", coord);
                     return;
@@ -2709,5 +2711,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "glGetTexGenfv called with bogus pname: %d", pname);
             return;
@@ -2717,12 +2719,12 @@
 
 void STATE_APIENTRY
-crStateGetTexGeniv(GLenum coord, GLenum pname, GLint *param)
-{
-    CRContext *g = GetCurrentContext();
+crStateGetTexGeniv(PCRStateTracker pState, GLenum coord, GLenum pname, GLint *param)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
 
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                     "glGetTexGeniv called in begin/end");
         return;
@@ -2745,5 +2747,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glGetTexGeniv called with bogus coord: %d", coord);
                     return;
@@ -2777,5 +2779,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glGetTexGeniv called with bogus coord: %d", coord);
                     return;
@@ -2809,5 +2811,5 @@
                     break;
                 default:
-                    crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                    crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                                 "glGetTexGeniv called with bogus coord: %d", coord);
                     return;
@@ -2815,5 +2817,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                         "glGetTexGen called with bogus pname: %d", pname);
             return;
@@ -2823,8 +2825,8 @@
 
 void STATE_APIENTRY
-crStateGetTexLevelParameterfv(GLenum target, GLint level, 
+crStateGetTexLevelParameterfv(PCRStateTracker pState, GLenum target, GLint level, 
                                                             GLenum pname, GLfloat *params) 
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
     CRTextureObj *tobj;
@@ -2833,5 +2835,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
             "glGetTexLevelParameterfv called in begin/end");
         return;
@@ -2840,5 +2842,5 @@
     if (level < 0 || level > t->maxLevel)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
             "glGetTexLevelParameterfv: Invalid level: %d", level);
         return;
@@ -2848,5 +2850,5 @@
     if (!timg)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
             "GetTexLevelParameterfv: invalid target: 0x%x or level %d",
             target, level);
@@ -2900,5 +2902,5 @@
 #endif
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                 "GetTexLevelParameterfv: invalid pname: 0x%x",
                 pname);
@@ -2909,8 +2911,8 @@
 
 void STATE_APIENTRY
-crStateGetTexLevelParameteriv(GLenum target, GLint level, 
+crStateGetTexLevelParameteriv(PCRStateTracker pState, GLenum target, GLint level, 
                                                             GLenum pname, GLint *params) 
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureState *t = &(g->texture);
     CRTextureObj *tobj;
@@ -2919,5 +2921,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
             "glGetTexLevelParameteriv called in begin/end");
         return;
@@ -2926,5 +2928,5 @@
     if (level < 0 || level > t->maxLevel)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
             "glGetTexLevelParameteriv: Invalid level: %d", level);
         return;
@@ -2934,5 +2936,5 @@
     if (!timg)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
             "GetTexLevelParameteriv: invalid target: 0x%x",
             target);
@@ -2993,5 +2995,5 @@
 #endif
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                 "GetTexLevelParameteriv: invalid pname: 0x%x",
                 pname);
@@ -3002,7 +3004,7 @@
 
 void STATE_APIENTRY
-crStateGetTexParameterfv(GLenum target, GLenum pname, GLfloat *params) 
-{
-    CRContext *g = GetCurrentContext();
+crStateGetTexParameterfv(PCRStateTracker pState, GLenum target, GLenum pname, GLfloat *params) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureObj *tobj;
     CRTextureLevel *tl;
@@ -3010,5 +3012,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                     "glGetTexParameterfv called in begin/end");
         return;
@@ -3018,5 +3020,5 @@
     if (!tobj)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
             "glGetTexParameterfv: invalid target: 0x%x", target);
         return;
@@ -3057,5 +3059,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameterfv: invalid pname: 0x%x", pname);
                 return;
@@ -3069,5 +3071,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3081,5 +3083,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3091,5 +3093,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3103,5 +3105,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3115,5 +3117,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3146,5 +3148,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                 "glGetTexParameterfv: invalid pname: %d", pname);
             return;
@@ -3154,7 +3156,7 @@
 
 void STATE_APIENTRY
-crStateGetTexParameteriv(GLenum target, GLenum pname, GLint *params) 
-{
-    CRContext *g = GetCurrentContext();
+crStateGetTexParameteriv(PCRStateTracker pState, GLenum target, GLenum pname, GLint *params) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureObj *tobj;
     CRTextureLevel *tl;
@@ -3162,5 +3164,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                     "glGetTexParameter called in begin/end");
         return;
@@ -3170,5 +3172,5 @@
     if (!tobj)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
             "glGetTexParameteriv: invalid target: 0x%x", target);
         return;
@@ -3223,5 +3225,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, 
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, 
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3235,5 +3237,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3247,5 +3249,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3257,5 +3259,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3269,5 +3271,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3281,5 +3283,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                     "glGetTexParameter: invalid pname: 0x%x", pname);
                 return;
@@ -3292,5 +3294,5 @@
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, 
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, 
                 "glGetTexParameter: invalid pname: %d", pname);
             return;
@@ -3300,8 +3302,8 @@
 
 void STATE_APIENTRY
-crStatePrioritizeTextures(GLsizei n, const GLuint *textures,
+crStatePrioritizeTextures(PCRStateTracker pState, GLsizei n, const GLuint *textures,
                                                     const GLclampf *priorities) 
 {
-    CRContext *g = GetCurrentContext();
+    CRContext *g = GetCurrentContext(pState);
     CRTextureObj *tobj;
     GLsizei i;
@@ -3330,7 +3332,8 @@
 
 GLboolean STATE_APIENTRY
-crStateAreTexturesResident(GLsizei n, const GLuint *textures,
+crStateAreTexturesResident(PCRStateTracker pState, GLsizei n, const GLuint *textures,
                                                      GLboolean *residences) 
 {
+    RT_NOREF(pState);
     UNUSED(n);
     UNUSED(textures);
@@ -3342,7 +3345,7 @@
 
 GLboolean STATE_APIENTRY
-crStateIsTexture(GLuint texture)
-{
-    CRContext *g = GetCurrentContext();
+crStateIsTexture(PCRStateTracker pState, GLuint texture)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureObj *tobj;
 
@@ -3357,15 +3360,16 @@
     (void) key;
 
-    if (crStateGetTextureObjHWID(pTex)==pParms->hwid)
+    if (crStateGetTextureObjHWID(pParms->pState, pTex)==pParms->hwid)
         pParms->id = pTex->id;
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateTextureHWIDtoID(GLuint hwid)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLuint) STATE_APIENTRY crStateTextureHWIDtoID(PCRStateTracker pState, GLuint hwid)
+{
+    CRContext *g = GetCurrentContext(pState);
     crCheckIDHWID_t parms;
 
     parms.id = hwid;
     parms.hwid = hwid;
+    parms.pState = pState;
 
     crHashtableWalk(g->shared->textureTable, crStateCheckTextureHWIDCB, &parms);
@@ -3373,7 +3377,7 @@
 }
 
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetTextureHWID(GLuint id)
-{
-    CRContext *g = GetCurrentContext();
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetTextureHWID(PCRStateTracker pState, GLuint id)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTextureObj *tobj;
 
@@ -3396,8 +3400,8 @@
 
 
-    return tobj ? crStateGetTextureObjHWID(tobj) : 0;
-}
-
-DECLEXPORT(GLuint) STATE_APIENTRY crStateGetTextureObjHWID(CRTextureObj *tobj)
+    return tobj ? crStateGetTextureObjHWID(pState, tobj) : 0;
+}
+
+DECLEXPORT(GLuint) STATE_APIENTRY crStateGetTextureObjHWID(PCRStateTracker pState, CRTextureObj *tobj)
 {
     CRASSERT(tobj);
@@ -3406,6 +3410,6 @@
     if (tobj->id && !tobj->hwid)
     {
-        CRASSERT(diff_api.GenTextures);
-        diff_api.GenTextures(1, &tobj->hwid);
+        CRASSERT(pState->diff_api.GenTextures);
+        pState->diff_api.GenTextures(1, &tobj->hwid);
 #if 0 /*def DEBUG_misha*/
         crDebug("tex id(%d), hwid(%d)", tobj->id, tobj->hwid);
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_transform.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_transform.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_transform.c	(revision 78375)
@@ -19,5 +19,5 @@
  */
 static INLINE void
-LOADMATRIX( const CRmatrix *a )
+LOADMATRIX(PCRStateTracker pState, const CRmatrix *a )
 {
     if (a->m00 == 1.0F && a->m01 == 0.0F && a->m02 == 0.0F && a->m03 == 0.0F &&
@@ -25,5 +25,5 @@
             a->m20 == 0.0F && a->m21 == 0.0F && a->m22 == 1.0F && a->m23 == 0.0F &&
             a->m30 == 0.0F && a->m31 == 0.0F && a->m32 == 0.0F && a->m33 == 1.0F) {
-        diff_api.LoadIdentity();
+        pState->diff_api.LoadIdentity();
     }
     else {
@@ -33,5 +33,5 @@
         f[8] = a->m20;  f[9] = a->m21;  f[10] = a->m22; f[11] = a->m23;
         f[12] = a->m30; f[13] = a->m31; f[14] = a->m32; f[15] = a->m33;
-        diff_api.LoadMatrixf(f);
+        pState->diff_api.LoadMatrixf(f);
     }
 }
@@ -116,5 +116,5 @@
     CRLimitsState *limits = &ctx->limits;
     CRTransformState *t = &ctx->transform;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
     CRTransformBits *tb = &(sb->transform);
     unsigned int i;
@@ -215,9 +215,9 @@
 }
 
-void STATE_APIENTRY crStateClipPlane (GLenum plane, const GLdouble *equation)
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateClipPlane (PCRStateTracker pState, GLenum plane, const GLdouble *equation)
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTransformState *t = &g->transform;
-    CRStateBits *sb = GetCurrentBits();
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTransformBits *tb = &(sb->transform);
     GLvectord e;
@@ -232,5 +232,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                      "ClipPlane called in begin/end");
         return;
@@ -242,5 +242,5 @@
     if (i >= g->limits.maxClipPlanes)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM,
                      "ClipPlane called with bad enumerant: %d", plane);
         return;
@@ -254,15 +254,15 @@
 }
 
-void STATE_APIENTRY crStateMatrixMode(GLenum e) 
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateMatrixMode(PCRStateTracker pState, GLenum e) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTransformState *t = &(g->transform);
     CRTextureState *tex = &(g->texture);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "MatrixMode called in begin/end");
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "MatrixMode called in begin/end");
         return;
     }
@@ -307,5 +307,5 @@
             }
             else {
-                crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid matrix mode: %d", e);
+                crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid matrix mode: %d", e);
                 return;
             }
@@ -329,10 +329,10 @@
             }
             else {
-                 crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid matrix mode: %d", e);
+                 crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid matrix mode: %d", e);
                  return;
             }
             break;
         default:
-            crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, "Invalid matrix mode: %d", e);
+            crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, "Invalid matrix mode: %d", e);
             return;
     }
@@ -343,14 +343,14 @@
 }
 
-void STATE_APIENTRY crStateLoadIdentity(void) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateLoadIdentity(PCRStateTracker pState) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "LoadIdentity called in begin/end");
         return;
@@ -366,14 +366,14 @@
 }
 
-void STATE_APIENTRY crStatePopMatrix(void) 
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStatePopMatrix(PCRStateTracker pState) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTransformState *t = &g->transform;
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "PopMatrix called in begin/end");
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "PopMatrix called in begin/end");
         return;
     }
@@ -383,5 +383,5 @@
     if (t->currentStack->depth == 0)
     {
-        crStateError(__LINE__, __FILE__, GL_STACK_UNDERFLOW, "PopMatrix of empty stack.");
+        crStateError(pState, __LINE__, __FILE__, GL_STACK_UNDERFLOW, "PopMatrix of empty stack.");
         return;
     }
@@ -398,14 +398,14 @@
 }
 
-void STATE_APIENTRY crStatePushMatrix(void) 
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStatePushMatrix(PCRStateTracker pState) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTransformState *t = &g->transform;
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "PushMatrix called in begin/end");
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "PushMatrix called in begin/end");
         return;
     }
@@ -415,5 +415,5 @@
     if (t->currentStack->depth + 1 >= t->currentStack->maxDepth)
     {
-        crStateError(__LINE__, __FILE__, GL_STACK_OVERFLOW, "PushMatrix pass the end of allocated stack");
+        crStateError(pState, __LINE__, __FILE__, GL_STACK_OVERFLOW, "PushMatrix pass the end of allocated stack");
         return;
     }
@@ -433,14 +433,14 @@
 
 /* Load a CRMatrix */
-void crStateLoadMatrix(const CRmatrix *m)
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void crStateLoadMatrix(PCRStateTracker pState, const CRmatrix *m)
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "LoadMatrix called in begin/end");
         return;
@@ -458,14 +458,14 @@
 
 
-void STATE_APIENTRY crStateLoadMatrixf(const GLfloat *m1) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateLoadMatrixf(PCRStateTracker pState, const GLfloat *m1) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "LoadMatrixf called in begin/end");
         return;
@@ -481,14 +481,14 @@
 
 
-void STATE_APIENTRY crStateLoadMatrixd(const GLdouble *m1) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateLoadMatrixd(PCRStateTracker pState, const GLdouble *m1) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "LoadMatrixd called in begin/end");
         return;
@@ -504,18 +504,18 @@
 
 
-void STATE_APIENTRY crStateLoadTransposeMatrixfARB(const GLfloat *m1) 
+void STATE_APIENTRY crStateLoadTransposeMatrixfARB(PCRStateTracker pState, const GLfloat *m1) 
 {
    GLfloat tm[16];
    if (!m1) return;
    _math_transposef(tm, m1);
-   crStateLoadMatrixf(tm);
-}
-
-void STATE_APIENTRY crStateLoadTransposeMatrixdARB(const GLdouble *m1) 
+   crStateLoadMatrixf(pState, tm);
+}
+
+void STATE_APIENTRY crStateLoadTransposeMatrixdARB(PCRStateTracker pState, const GLdouble *m1) 
 {
    GLdouble tm[16];
    if (!m1) return;
    _math_transposed(tm, m1);
-   crStateLoadMatrixd(tm);
+   crStateLoadMatrixd(pState, tm);
 }
 
@@ -527,9 +527,9 @@
  ** that this really helps.
  */ 
-void STATE_APIENTRY crStateMultMatrixf(const GLfloat *m1) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
+void STATE_APIENTRY crStateMultMatrixf(PCRStateTracker pState, const GLfloat *m1) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTransformBits *tb = &(sb->transform);
     CRmatrix *m = t->currentStack->top;
@@ -570,5 +570,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "MultMatrixf called in begin/end");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "MultMatrixf called in begin/end");
         return;
     }
@@ -599,9 +599,9 @@
 }
 
-void STATE_APIENTRY crStateMultMatrixd(const GLdouble *m1) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
+void STATE_APIENTRY crStateMultMatrixd(PCRStateTracker pState, const GLdouble *m1) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
     CRTransformBits *tb = &(sb->transform);
     CRmatrix *m = t->currentStack->top;
@@ -641,5 +641,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "MultMatrixd called in begin/end");
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "MultMatrixd called in begin/end");
         return;
     }
@@ -670,30 +670,30 @@
 }
 
-void STATE_APIENTRY crStateMultTransposeMatrixfARB(const GLfloat *m1) 
+void STATE_APIENTRY crStateMultTransposeMatrixfARB(PCRStateTracker pState, const GLfloat *m1) 
 {
    GLfloat tm[16];
    if (!m1) return;
    _math_transposef(tm, m1);
-   crStateMultMatrixf(tm);
-}
-
-void STATE_APIENTRY crStateMultTransposeMatrixdARB(const GLdouble *m1) 
+   crStateMultMatrixf(pState, tm);
+}
+
+void STATE_APIENTRY crStateMultTransposeMatrixdARB(PCRStateTracker pState, const GLdouble *m1) 
 {
    GLdouble tm[16];
    if (!m1) return;
    _math_transposed(tm, m1);
-   crStateMultMatrixd(tm);
-}
-
-void STATE_APIENTRY crStateTranslatef(GLfloat x_arg, GLfloat y_arg, GLfloat z_arg) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+   crStateMultMatrixd(pState, tm);
+}
+
+void STATE_APIENTRY crStateTranslatef(PCRStateTracker pState, GLfloat x_arg, GLfloat y_arg, GLfloat z_arg) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "Translatef called in begin/end");
         return;
@@ -709,14 +709,14 @@
 
 
-void STATE_APIENTRY crStateTranslated(GLdouble x_arg, GLdouble y_arg, GLdouble z_arg) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateTranslated(PCRStateTracker pState, GLdouble x_arg, GLdouble y_arg, GLdouble z_arg) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "Translated called in begin/end");
         return;
@@ -732,14 +732,14 @@
 
 
-void STATE_APIENTRY crStateRotatef(GLfloat ang, GLfloat x, GLfloat y, GLfloat z) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateRotatef(PCRStateTracker pState, GLfloat ang, GLfloat x, GLfloat y, GLfloat z) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "Rotatef called in begin/end");
         return;
@@ -754,14 +754,14 @@
 }
 
-void STATE_APIENTRY crStateRotated(GLdouble ang, GLdouble x, GLdouble y, GLdouble z) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateRotated(PCRStateTracker pState, GLdouble ang, GLdouble x, GLdouble y, GLdouble z) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "Rotated called in begin/end");
         return;
@@ -776,14 +776,14 @@
 }   
 
-void STATE_APIENTRY crStateScalef (GLfloat x_arg, GLfloat y_arg, GLfloat z_arg) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateScalef (PCRStateTracker pState, GLfloat x_arg, GLfloat y_arg, GLfloat z_arg) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "Scalef called in begin/end");
         return;
@@ -798,14 +798,14 @@
 }
 
-void STATE_APIENTRY crStateScaled (GLdouble x_arg, GLdouble y_arg, GLdouble z_arg) 
-{
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+void STATE_APIENTRY crStateScaled (PCRStateTracker pState, GLdouble x_arg, GLdouble y_arg, GLdouble z_arg) 
+{
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "Scaled called in begin/end");
         return;
@@ -820,16 +820,16 @@
 }
 
-void STATE_APIENTRY crStateFrustum(GLdouble left, GLdouble right,
+void STATE_APIENTRY crStateFrustum(PCRStateTracker pState, GLdouble left, GLdouble right,
                                                                      GLdouble bottom, GLdouble top, 
                                                                      GLdouble zNear, GLdouble zFar)
 {
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "Frustum called in begin/end");
         return;
@@ -844,16 +844,16 @@
 }
 
-void STATE_APIENTRY crStateOrtho(GLdouble left, GLdouble right,
+void STATE_APIENTRY crStateOrtho(PCRStateTracker pState, GLdouble left, GLdouble right,
                                                                  GLdouble bottom, GLdouble top,
                                                                  GLdouble zNear, GLdouble zFar)
 {
-    CRContext *g = GetCurrentContext();
-    CRTransformState *t = &(g->transform);
-    CRStateBits *sb = GetCurrentBits();
-    CRTransformBits *tb = &(sb->transform);
-
-    if (g->current.inBeginEnd)
-    {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+    CRContext *g = GetCurrentContext(pState);
+    CRTransformState *t = &(g->transform);
+    CRStateBits *sb = GetCurrentBits(pState);
+    CRTransformBits *tb = &(sb->transform);
+
+    if (g->current.inBeginEnd)
+    {
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
                                  "Ortho called in begin/end");
         return;
@@ -868,7 +868,7 @@
 }
 
-void STATE_APIENTRY crStateGetClipPlane(GLenum plane, GLdouble *equation) 
-{
-    CRContext *g = GetCurrentContext();
+void STATE_APIENTRY crStateGetClipPlane(PCRStateTracker pState, GLenum plane, GLdouble *equation) 
+{
+    CRContext *g = GetCurrentContext(pState);
     CRTransformState *t = &g->transform;
     unsigned int i;
@@ -876,5 +876,5 @@
     if (g->current.inBeginEnd)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
             "glGetClipPlane called in begin/end");
         return;
@@ -884,5 +884,5 @@
     if (i >= g->limits.maxClipPlanes)
     {
-        crStateError(__LINE__, __FILE__, GL_INVALID_ENUM, 
+        crStateError(pState, __LINE__, __FILE__, GL_INVALID_ENUM, 
             "GetClipPlane called with bad enumerant: %d", plane);
         return;
@@ -898,4 +898,5 @@
                                                          CRContext *fromCtx, CRContext *toCtx )
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     const GLuint maxTextureUnits = toCtx->limits.maxTextureUnits;
     CRTransformState *from = &(fromCtx->transform);
@@ -905,4 +906,6 @@
     CRbitvalue nbitID[CR_MAX_BITARRAY];
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
@@ -911,11 +914,11 @@
     {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         if (from->normalize != to->normalize) {
             if (to->normalize == GL_TRUE)
-                diff_api.Enable(GL_NORMALIZE);
+                pState->diff_api.Enable(GL_NORMALIZE);
             else
-                diff_api.Disable(GL_NORMALIZE);
+                pState->diff_api.Disable(GL_NORMALIZE);
             FILLDIRTY(t->enable);
             FILLDIRTY(t->dirty);
@@ -943,7 +946,7 @@
 
     if (CHECKDIRTY(t->clipPlane, bitID)) {
-        diff_api.MatrixMode(GL_MODELVIEW);
-        diff_api.PushMatrix();
-        diff_api.LoadIdentity();
+        pState->diff_api.MatrixMode(GL_MODELVIEW);
+        pState->diff_api.PushMatrix();
+        pState->diff_api.LoadIdentity();
         for (i=0; i<CR_MAX_CLIP_PLANES; i++) {
             if (from->clipPlane[i].x != to->clipPlane[i].x ||
@@ -958,5 +961,5 @@
                 cp[3] = to->clipPlane[i].w;
 
-                diff_api.ClipPlane(GL_CLIP_PLANE0 + i, (const GLdouble *)(cp));
+                pState->diff_api.ClipPlane(GL_CLIP_PLANE0 + i, (const GLdouble *)(cp));
 
                 FILLDIRTY(t->clipPlane);
@@ -964,5 +967,5 @@
             }
         }
-        diff_api.PopMatrix();
+        pState->diff_api.PopMatrix();
         CLEARDIRTY(t->clipPlane, nbitID);
     }
@@ -986,5 +989,5 @@
                 !crMatrixIsEqual(to->modelViewStack.top, from->modelViewStack.top))
         {
-            diff_api.MatrixMode(GL_MODELVIEW);
+            pState->diff_api.MatrixMode(GL_MODELVIEW);
 
             if (fd > td)
@@ -992,5 +995,5 @@
                 for (i = td; i < fd; i++) 
                 {
-                    diff_api.PopMatrix();
+                    pState->diff_api.PopMatrix();
                 }
                 fd = td;
@@ -999,5 +1002,5 @@
             for (i = fd; i <= td; i++)
             {
-                LOADMATRIX(to->modelViewStack.stack + i);
+                LOADMATRIX(pState, to->modelViewStack.stack + i);
                 FILLDIRTY(t->modelviewMatrix);
                 FILLDIRTY(t->dirty);
@@ -1005,5 +1008,5 @@
                 /* Don't want to push on the current matrix */
                 if (i != to->modelViewStack.depth)
-                    diff_api.PushMatrix();
+                    pState->diff_api.PushMatrix();
             }
         }
@@ -1021,5 +1024,5 @@
                 !crMatrixIsEqual(to->projectionStack.top, from->projectionStack.top)) {
 
-            diff_api.MatrixMode(GL_PROJECTION);
+            pState->diff_api.MatrixMode(GL_PROJECTION);
 
             if (fd > td)
@@ -1027,5 +1030,5 @@
                 for (i = td; i < fd; i++) 
                 {
-                    diff_api.PopMatrix();
+                    pState->diff_api.PopMatrix();
                 }
                 fd = td;
@@ -1034,5 +1037,5 @@
             for (i = fd; i <= td; i++)
             {
-                LOADMATRIX(to->projectionStack.stack + i);
+                LOADMATRIX(pState, to->projectionStack.stack + i);
                 FILLDIRTY(t->projectionMatrix);
                 FILLDIRTY(t->dirty);
@@ -1040,5 +1043,5 @@
                 /* Don't want to push on the current matrix */
                 if (i != to->projectionStack.depth)
-                    diff_api.PushMatrix();
+                    pState->diff_api.PushMatrix();
             }
         }
@@ -1061,5 +1064,5 @@
                     !crMatrixIsEqual(to->textureStack[j].top, from->textureStack[j].top))
             {
-                diff_api.MatrixMode(GL_TEXTURE);
+                pState->diff_api.MatrixMode(GL_TEXTURE);
 
                 if (fd > td)
@@ -1067,13 +1070,13 @@
                     for (i = td; i < fd; i++) 
                     {
-                        diff_api.PopMatrix();
+                        pState->diff_api.PopMatrix();
                     }
                     fd = td;
                 }
 
-                diff_api.ActiveTextureARB( j + GL_TEXTURE0_ARB );
+                pState->diff_api.ActiveTextureARB( j + GL_TEXTURE0_ARB );
                 for (i = fd; i <= td; i++)
                 {
-                    LOADMATRIX(to->textureStack[j].stack + i);
+                    LOADMATRIX(pState, to->textureStack[j].stack + i);
                     FILLDIRTY(t->textureMatrix);
                     FILLDIRTY(t->dirty);
@@ -1081,5 +1084,5 @@
                     /* Don't want to push on the current matrix */
                     if (i != to->textureStack[j].depth)
-                        diff_api.PushMatrix();
+                        pState->diff_api.PushMatrix();
                 }
             }
@@ -1088,5 +1091,5 @@
          * proper value now.  
          */
-        diff_api.ActiveTextureARB(GL_TEXTURE0_ARB + toCtx->texture.curTextureUnit);
+        pState->diff_api.ActiveTextureARB(GL_TEXTURE0_ARB + toCtx->texture.curTextureUnit);
         CLEARDIRTY(t->textureMatrix, nbitID);
     }
@@ -1100,5 +1103,5 @@
         if (td != fd || !crMatrixIsEqual(to->colorStack.top, from->colorStack.top))
         {
-            diff_api.MatrixMode(GL_COLOR);
+            pState->diff_api.MatrixMode(GL_COLOR);
 
             if (fd > td)
@@ -1106,5 +1109,5 @@
                 for (i = td; i < fd; i++) 
                 {
-                    diff_api.PopMatrix();
+                    pState->diff_api.PopMatrix();
                 }
                 fd = td;
@@ -1113,5 +1116,5 @@
             for (i = fd; i <= td; i++)
             {
-                LOADMATRIX(to->colorStack.stack + i);
+                LOADMATRIX(pState, to->colorStack.stack + i);
                 FILLDIRTY(t->colorMatrix);
                 FILLDIRTY(t->dirty);
@@ -1119,5 +1122,5 @@
                 /* Don't want to push on the current matrix */
                 if (i != to->colorStack.depth)
-                    diff_api.PushMatrix();
+                    pState->diff_api.PushMatrix();
             }
         }
@@ -1131,5 +1134,5 @@
      * set it to the proper value now.  
      */
-    diff_api.MatrixMode(to->matrixMode);
+    pState->diff_api.MatrixMode(to->matrixMode);
 
     /* sanity tests */
@@ -1143,4 +1146,5 @@
                                             CRContext *fromCtx, CRContext *toCtx )
 {
+    PCRStateTracker pState = fromCtx->pStateTracker;
     const GLuint maxTextureUnits = toCtx->limits.maxTextureUnits;
     CRTransformState *from = &(fromCtx->transform);
@@ -1151,4 +1155,6 @@
     CRbitvalue nbitID[CR_MAX_BITARRAY];
 
+    CRASSERT(fromCtx->pStateTracker == toCtx->pStateTracker);
+
     for (j=0;j<CR_MAX_BITARRAY;j++)
         nbitID[j] = ~bitID[j];
@@ -1156,12 +1162,12 @@
     if (CHECKDIRTY(t->enable, bitID)) {
         glAble able[2];
-        able[0] = diff_api.Disable;
-        able[1] = diff_api.Enable;
+        able[0] = pState->diff_api.Disable;
+        able[1] = pState->diff_api.Enable;
         for (i=0; i<CR_MAX_CLIP_PLANES; i++) {
             if (from->clip[i] != to->clip[i]) {
                 if (to->clip[i] == GL_TRUE)
-                    diff_api.Enable(GL_CLIP_PLANE0 + i);
+                    pState->diff_api.Enable(GL_CLIP_PLANE0 + i);
                 else
-                    diff_api.Disable(GL_CLIP_PLANE0 + i);
+                    pState->diff_api.Disable(GL_CLIP_PLANE0 + i);
                 from->clip[i] = to->clip[i];
             }
@@ -1169,7 +1175,7 @@
         if (from->normalize != to->normalize) {
             if (to->normalize == GL_TRUE)
-                diff_api.Enable(GL_NORMALIZE);
+                pState->diff_api.Enable(GL_NORMALIZE);
             else
-                diff_api.Disable(GL_NORMALIZE);
+                pState->diff_api.Disable(GL_NORMALIZE);
             from->normalize = to->normalize;
         }
@@ -1194,9 +1200,9 @@
     if (CHECKDIRTY(t->clipPlane, bitID)) {
         if (from->matrixMode != GL_MODELVIEW) {
-            diff_api.MatrixMode(GL_MODELVIEW);
+            pState->diff_api.MatrixMode(GL_MODELVIEW);
             from->matrixMode = GL_MODELVIEW;
         }
-        diff_api.PushMatrix();
-        diff_api.LoadIdentity();
+        pState->diff_api.PushMatrix();
+        pState->diff_api.LoadIdentity();
         for (i=0; i<CR_MAX_CLIP_PLANES; i++) {
             if (from->clipPlane[i].x != to->clipPlane[i].x ||
@@ -1211,9 +1217,9 @@
                 cp[3] = to->clipPlane[i].w;
 
-                diff_api.ClipPlane(GL_CLIP_PLANE0 + i, (const GLdouble *)(cp));
+                pState->diff_api.ClipPlane(GL_CLIP_PLANE0 + i, (const GLdouble *)(cp));
                 from->clipPlane[i] = to->clipPlane[i];
             }
         }
-        diff_api.PopMatrix();
+        pState->diff_api.PopMatrix();
         CLEARDIRTY(t->clipPlane, nbitID);
     }
@@ -1232,5 +1238,5 @@
     {
         if (from->matrixMode != GL_MODELVIEW) {
-            diff_api.MatrixMode(GL_MODELVIEW);
+            pState->diff_api.MatrixMode(GL_MODELVIEW);
             from->matrixMode = GL_MODELVIEW;
         }
@@ -1240,5 +1246,5 @@
             for (i = to->modelViewStack.depth; i < from->modelViewStack.depth; i++) 
             {
-                diff_api.PopMatrix();
+                pState->diff_api.PopMatrix();
             }
 
@@ -1248,10 +1254,10 @@
         for (i = from->modelViewStack.depth; i <= to->modelViewStack.depth; i++)
         {
-            LOADMATRIX(to->modelViewStack.stack + i);
+            LOADMATRIX(pState, to->modelViewStack.stack + i);
             from->modelViewStack.stack[i] = to->modelViewStack.stack[i];
 
             /* Don't want to push on the current matrix */
             if (i != to->modelViewStack.depth)
-                diff_api.PushMatrix();
+                pState->diff_api.PushMatrix();
         }
         from->modelViewStack.depth = to->modelViewStack.depth;
@@ -1266,5 +1272,5 @@
     {
         if (from->matrixMode != GL_PROJECTION) {
-            diff_api.MatrixMode(GL_PROJECTION);
+            pState->diff_api.MatrixMode(GL_PROJECTION);
             from->matrixMode = GL_PROJECTION;
         }
@@ -1274,5 +1280,5 @@
             for (i = to->projectionStack.depth; i < from->projectionStack.depth; i++) 
             {
-                diff_api.PopMatrix();
+                pState->diff_api.PopMatrix();
             }
 
@@ -1282,10 +1288,10 @@
         for (i = from->projectionStack.depth; i <= to->projectionStack.depth; i++)
         {
-            LOADMATRIX(to->projectionStack.stack + i);
+            LOADMATRIX(pState, to->projectionStack.stack + i);
             from->projectionStack.stack[i] = to->projectionStack.stack[i];
 
             /* Don't want to push on the current matrix */
             if (i != to->projectionStack.depth)
-                diff_api.PushMatrix();
+                pState->diff_api.PushMatrix();
         }
         from->projectionStack.depth = to->projectionStack.depth;
@@ -1303,5 +1309,5 @@
     {
         if (from->matrixMode != GL_TEXTURE) {
-            diff_api.MatrixMode(GL_TEXTURE);
+            pState->diff_api.MatrixMode(GL_TEXTURE);
             from->matrixMode = GL_TEXTURE;
         }
@@ -1311,10 +1317,10 @@
             {
                 if (textureFrom->curTextureUnit != j) {
-                    diff_api.ActiveTextureARB( j + GL_TEXTURE0_ARB );
+                    pState->diff_api.ActiveTextureARB( j + GL_TEXTURE0_ARB );
                     textureFrom->curTextureUnit = j;
                 }
                 for (i = to->textureStack[j].depth; i < from->textureStack[j].depth; i++) 
                 {
-                    diff_api.PopMatrix();
+                    pState->diff_api.PopMatrix();
                 }
     
@@ -1325,13 +1331,13 @@
             {
                 if (textureFrom->curTextureUnit != j) {
-                    diff_api.ActiveTextureARB( j + GL_TEXTURE0_ARB );
+                    pState->diff_api.ActiveTextureARB( j + GL_TEXTURE0_ARB );
                     textureFrom->curTextureUnit = j;
                 }
-                LOADMATRIX(to->textureStack[j].stack + i);
+                LOADMATRIX(pState, to->textureStack[j].stack + i);
                 from->textureStack[j].stack[i] = to->textureStack[j].stack[i];
 
                 /* Don't want to push on the current matrix */
                 if (i != to->textureStack[j].depth)
-                    diff_api.PushMatrix();
+                    pState->diff_api.PushMatrix();
             }
             from->textureStack[j].depth = to->textureStack[j].depth;
@@ -1341,5 +1347,5 @@
 
         /* Restore proper active texture unit */
-        diff_api.ActiveTextureARB(GL_TEXTURE0_ARB + toCtx->texture.curTextureUnit);
+        pState->diff_api.ActiveTextureARB(GL_TEXTURE0_ARB + toCtx->texture.curTextureUnit);
     }
 
@@ -1349,5 +1355,5 @@
     {
         if (from->matrixMode != GL_COLOR) {
-            diff_api.MatrixMode(GL_COLOR);
+            pState->diff_api.MatrixMode(GL_COLOR);
             from->matrixMode = GL_COLOR;
         }
@@ -1357,5 +1363,5 @@
             for (i = to->colorStack.depth; i < from->colorStack.depth; i++) 
             {
-                diff_api.PopMatrix();
+                pState->diff_api.PopMatrix();
             }
 
@@ -1365,10 +1371,10 @@
         for (i = to->colorStack.depth; i <= to->colorStack.depth; i++)
         {
-            LOADMATRIX(to->colorStack.stack + i);
+            LOADMATRIX(pState, to->colorStack.stack + i);
             from->colorStack.stack[i] = to->colorStack.stack[i];
 
             /* Don't want to push on the current matrix */
             if (i != to->colorStack.depth)
-                diff_api.PushMatrix();
+                pState->diff_api.PushMatrix();
         }
         from->colorStack.depth = to->colorStack.depth;
@@ -1383,5 +1389,5 @@
     /* update MatrixMode now */
     if (from->matrixMode != to->matrixMode) {
-        diff_api.MatrixMode(to->matrixMode);
+        pState->diff_api.MatrixMode(to->matrixMode);
         from->matrixMode = to->matrixMode;
     }
Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_viewport.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_viewport.c	(revision 78374)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_viewport.c	(revision 78375)
@@ -13,5 +13,5 @@
 {
 	CRViewportState *v = &ctx->viewport;
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(ctx->pStateTracker);
 	CRViewportBits *vb = &(sb->viewport);
 	CRTransformBits *tb = &(sb->transform);
@@ -56,10 +56,10 @@
 }
 
-void STATE_APIENTRY crStateViewport(GLint x, GLint y, GLsizei width, 
+void STATE_APIENTRY crStateViewport(PCRStateTracker pState, GLint x, GLint y, GLsizei width, 
 			GLsizei height) 
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRViewportState *v = &(g->viewport);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRViewportBits *vb = &(sb->viewport);
 	CRTransformBits *tb = &(sb->transform);
@@ -67,5 +67,5 @@
 	if (g->current.inBeginEnd)
 	{
-		crStateError( __LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 									"calling glViewport() between glBegin/glEnd" );
 		return;
@@ -76,5 +76,5 @@
 	if (width < 0 || height < 0)
 	{
-		crStateError( __LINE__, __FILE__, GL_INVALID_VALUE,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
 									"glViewport(bad width or height)" );
 		return;
@@ -102,9 +102,9 @@
 }
 
-void STATE_APIENTRY crStateDepthRange(GLclampd znear, GLclampd zfar) 
+void STATE_APIENTRY crStateDepthRange(PCRStateTracker pState, GLclampd znear, GLclampd zfar) 
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRViewportState *v = &(g->viewport);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRViewportBits *vb = &(sb->viewport);
 	CRTransformBits *tb = &(sb->transform);
@@ -112,5 +112,5 @@
 	if (g->current.inBeginEnd)
 	{	
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION, "glDepthRange called in Begin/End");
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION, "glDepthRange called in Begin/End");
 		return;
 	}
@@ -130,15 +130,15 @@
 }
 
-void STATE_APIENTRY crStateScissor (GLint x, GLint y, 
+void STATE_APIENTRY crStateScissor (PCRStateTracker pState, GLint x, GLint y, 
 					 GLsizei width, GLsizei height) 
 {
-	CRContext *g = GetCurrentContext();
+	CRContext *g = GetCurrentContext(pState);
 	CRViewportState *v = &(g->viewport);
-	CRStateBits *sb = GetCurrentBits();
+	CRStateBits *sb = GetCurrentBits(pState);
 	CRViewportBits *vb = &(sb->viewport);
 
 	if (g->current.inBeginEnd) 
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_OPERATION,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_OPERATION,
 			"glScissor called in begin/end");
 		return;
@@ -149,5 +149,5 @@
 	if (width < 0 || height < 0)
 	{
-		crStateError(__LINE__, __FILE__, GL_INVALID_VALUE,
+		crStateError(pState, __LINE__, __FILE__, GL_INVALID_VALUE,
 			"glScissor called with negative width/height: %d,%d",
 			width, height);
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserver/crservice.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserver/crservice.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserver/crservice.cpp	(revision 78375)
@@ -1582,5 +1582,5 @@
         case DLL_THREAD_ATTACH:
         {
-            crStateVBoxAttachThread();
+            crVBoxServerAttachThread();
             break;
         }
@@ -1591,5 +1591,5 @@
         case DLL_THREAD_DETACH:
         {
-            crStateVBoxDetachThread();
+            crVBoxServerDetachThread();
             break;
         }
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/presenter/server_presenter.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/presenter/server_presenter.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/presenter/server_presenter.cpp	(revision 78375)
@@ -975,7 +975,5 @@
 
         crStateReleaseTexture(cr_server.MainContextInfo.pContext, pTobj);
-
-
-        crStateGlobalSharedRelease();
+        crStateGlobalSharedRelease(&cr_server.StateTracker);
     }
 
@@ -1026,5 +1024,5 @@
     }
 
-    CRSharedState *pShared = crStateGlobalSharedAcquire();
+    CRSharedState *pShared = crStateGlobalSharedAcquire(&cr_server.StateTracker);
     if (!pShared)
     {
@@ -1037,5 +1035,5 @@
     {
         LOG(("pTobj is null!"));
-        crStateGlobalSharedRelease();
+        crStateGlobalSharedRelease(&cr_server.StateTracker);
         return NULL;
     }
@@ -1043,9 +1041,9 @@
     Assert(pTobj->id == idTexture);
 
-    GLuint hwid = crStateGetTextureObjHWID(pTobj);
+    GLuint hwid = crStateGetTextureObjHWID(&cr_server.StateTracker, pTobj);
     if (!hwid)
     {
         WARN(("hwId is null!"));
-        crStateGlobalSharedRelease();
+        crStateGlobalSharedRelease(&cr_server.StateTracker);
         return NULL;
     }
@@ -1061,5 +1059,5 @@
     {
         WARN(("crFbTexCreate failed!"));
-        crStateGlobalSharedRelease();
+        crStateGlobalSharedRelease(&cr_server.StateTracker);
         return NULL;
     }
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server.h
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server.h	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server.h	(revision 78375)
@@ -341,5 +341,5 @@
     CRContext *pCurCtx = pCurCtxInfo ? pCurCtxInfo->pContext : NULL;
 
-    CRASSERT(pCurCtx == crStateGetCurrent());
+    CRASSERT(pCurCtx == crStateGetCurrent(&cr_server.StateTracker));
 
     if (pCurrentMural)
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_bufferobject.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_bufferobject.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_bufferobject.cpp	(revision 78375)
@@ -46,5 +46,5 @@
     }
 
-    crStateGenBuffersARB(n, local_buffers);
+    crStateGenBuffersARB(&cr_server.StateTracker, n, local_buffers);
 
     crServerReturnValue( local_buffers, n * sizeof(*local_buffers) );
@@ -60,5 +60,5 @@
     }
 
-    crStateDeleteBuffersARB( n, buffer );
+    crStateDeleteBuffersARB(&cr_server.StateTracker, n, buffer );
 }
 
@@ -101,6 +101,6 @@
 crServerDispatchBindBufferARB(GLenum target, GLuint buffer)
 {
-    crStateBindBufferARB(target, buffer);
-    cr_server.head_spu->dispatch_table.BindBufferARB(target, crStateGetBufferHWID(buffer));
+    crStateBindBufferARB(&cr_server.StateTracker, target, buffer);
+    cr_server.head_spu->dispatch_table.BindBufferARB(target, crStateGetBufferHWID(&cr_server.StateTracker, buffer));
 }
 
@@ -110,5 +110,5 @@
     /* since GenBuffersARB issued to host ogl only on bind + some other ops, the host drivers may not know about them
      * so use state data*/
-    GLboolean retval = crStateIsBufferARB(buffer);
+    GLboolean retval = crStateIsBufferARB(&cr_server.StateTracker, buffer);
     crServerReturnValue( &retval, sizeof(retval) );
     return retval; /* WILL PROBABLY BE IGNORED */
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_clear.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_clear.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_clear.cpp	(revision 78375)
@@ -423,5 +423,5 @@
 		window = 0;
 
-	ctx = crStateGetCurrent();
+	ctx = crStateGetCurrent(&cr_server.StateTracker);
 
 	CRASSERT(cr_server.curClient && cr_server.curClient->currentMural == mural);
@@ -449,5 +449,5 @@
 crServerDispatchFlush(void)
 {
-    CRContext *ctx = crStateGetCurrent();
+    CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
     cr_server.head_spu->dispatch_table.Flush();
 
@@ -470,5 +470,5 @@
 crServerDispatchFinish(void)
 {
-    CRContext *ctx = crStateGetCurrent();
+    CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
 
     cr_server.head_spu->dispatch_table.Finish();
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_context.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_context.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_context.cpp	(revision 78375)
@@ -63,5 +63,5 @@
             return -1;
         }
-        cr_server.MainContextInfo.pContext = crStateCreateContext(&cr_server.limits, visualBits, NULL);
+        cr_server.MainContextInfo.pContext = crStateCreateContext(&cr_server.StateTracker, &cr_server.limits, visualBits, NULL);
         CRASSERT(cr_server.MainContextInfo.pContext);
         cr_server.firstCallCreateContext = GL_FALSE;
@@ -114,5 +114,5 @@
         if (pContextInfo->SpuContext < 0) {
             crWarning("crServerDispatchCreateContext() failed.");
-            crStateEnableDiffOnMakeCurrent(GL_TRUE);
+            crStateEnableDiffOnMakeCurrent(&cr_server.StateTracker, GL_TRUE);
             cr_server.bUseMultipleContexts = GL_FALSE;
             if (!fFirst)
@@ -121,5 +121,5 @@
         else if (fFirst)
         {
-            crStateEnableDiffOnMakeCurrent(GL_FALSE);
+            crStateEnableDiffOnMakeCurrent(&cr_server.StateTracker, GL_FALSE);
         }
     }
@@ -132,7 +132,7 @@
      * dispatch function pointers.
      */
-    newCtx = crStateCreateContextEx(&cr_server.limits, visualBits, NULL, internalID);
+    newCtx = crStateCreateContextEx(&cr_server.StateTracker, &cr_server.limits, visualBits, NULL, internalID);
     if (newCtx) {
-        crStateSetCurrentPointers( newCtx, &(cr_server.current) );
+        crStateSetCurrentPointers(newCtx, &(cr_server.current) );
         crStateResetCurrentPointers(&(cr_server.current));
         retVal = preloadCtxID<0 ? (GLint)crHashtableAllocKeys( cr_server.contextTable, 1 ) : preloadCtxID;
@@ -214,5 +214,5 @@
     crCtxInfo->currentMural = NULL;
     crHashtableDelete(cr_server.contextTable, ctx, NULL);
-    crStateDestroyContext( crCtx );
+    crStateDestroyContext(&cr_server.StateTracker, crCtx);
 
     if (crCtxInfo->CreateInfo.pszDpyName)
@@ -306,5 +306,5 @@
      * crStateSwitchPrepare restores the FBO state to its default values before the context window switch,
      * while crStateSwitchPostprocess restores it back to the original values */
-    oldCtx = crStateGetCurrent();
+    oldCtx = crStateGetCurrent(&cr_server.StateTracker);
     if (oldMural && oldMural->fRedirected && crServerSupportRedirMuralFBO())
     {
@@ -335,8 +335,8 @@
 
     /* This is a hack to force updating the 'current' attribs */
-    crStateUpdateColorBits();
+    crStateUpdateColorBits(&cr_server.StateTracker);
 
     if (ctx)
-        crStateSetCurrentPointers( ctx, &(cr_server.current) );
+        crStateSetCurrentPointers(ctx, &(cr_server.current) );
 
     /* check if being made current for first time, update viewport */
@@ -387,5 +387,5 @@
 
     /* This used to be earlier, after crStateUpdateColorBits() call */
-    crStateMakeCurrent( ctx );
+    crStateMakeCurrent(&cr_server.StateTracker, ctx );
 
     if (mural && mural->fRedirected  && crServerSupportRedirMuralFBO())
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_dispatch.py
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_dispatch.py	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_dispatch.py	(revision 78375)
@@ -37,5 +37,8 @@
     print('void SERVER_DISPATCH_APIENTRY crServerDispatch%s(%s)' % ( func_name, apiutil.MakeDeclarationStringForDispatcher( params ) ))
     print('{')
-    print('\tcrState%s(%s);' % (func_name, apiutil.MakeCallStringForDispatcher( params ) ))
+    if len(params) == 0:
+        print('\tcrState%s(&cr_server.StateTracker);' % (func_name))
+    else:
+        print('\tcrState%s(&cr_server.StateTracker, %s);' % (func_name, apiutil.MakeCallStringForDispatcher( params ) ))
     print('\tcr_server.head_spu->dispatch_table.%s(%s);' % (func_name, apiutil.MakeCallStringForDispatcher( params ) ))
     print('}')
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_framebuffer.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_framebuffer.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_framebuffer.cpp	(revision 78375)
@@ -38,5 +38,5 @@
     local_buffers = (GLuint *)crCalloc(n * sizeof(*local_buffers));
 
-    crStateGenFramebuffersEXT(n, local_buffers);
+    crStateGenFramebuffersEXT(&cr_server.StateTracker, n, local_buffers);
 
     crServerReturnValue(local_buffers, n * sizeof(*local_buffers));
@@ -58,5 +58,5 @@
     local_buffers = (GLuint *)crCalloc(n * sizeof(*local_buffers));
 
-    crStateGenRenderbuffersEXT(n, local_buffers);
+    crStateGenRenderbuffersEXT(&cr_server.StateTracker, n, local_buffers);
 
     crServerReturnValue(local_buffers, n * sizeof(*local_buffers));
@@ -66,18 +66,18 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchFramebufferTexture1DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
 {
-    crStateFramebufferTexture1DEXT(target, attachment, textarget, texture, level);
-    cr_server.head_spu->dispatch_table.FramebufferTexture1DEXT(target, attachment, textarget, crStateGetTextureHWID(texture), level);
+    crStateFramebufferTexture1DEXT(&cr_server.StateTracker, target, attachment, textarget, texture, level);
+    cr_server.head_spu->dispatch_table.FramebufferTexture1DEXT(target, attachment, textarget, crStateGetTextureHWID(&cr_server.StateTracker, texture), level);
 }
 
 void SERVER_DISPATCH_APIENTRY crServerDispatchFramebufferTexture2DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
 {
-    crStateFramebufferTexture2DEXT(target, attachment, textarget, texture, level);
-    cr_server.head_spu->dispatch_table.FramebufferTexture2DEXT(target, attachment, textarget, crStateGetTextureHWID(texture), level);
+    crStateFramebufferTexture2DEXT(&cr_server.StateTracker, target, attachment, textarget, texture, level);
+    cr_server.head_spu->dispatch_table.FramebufferTexture2DEXT(target, attachment, textarget, crStateGetTextureHWID(&cr_server.StateTracker, texture), level);
 }
 
 void SERVER_DISPATCH_APIENTRY crServerDispatchFramebufferTexture3DEXT(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset)
 {
-    crStateFramebufferTexture3DEXT(target, attachment, textarget, texture, level, zoffset);
-    cr_server.head_spu->dispatch_table.FramebufferTexture3DEXT(target, attachment, textarget, crStateGetTextureHWID(texture), level, zoffset);
+    crStateFramebufferTexture3DEXT(&cr_server.StateTracker, target, attachment, textarget, texture, level, zoffset);
+    cr_server.head_spu->dispatch_table.FramebufferTexture3DEXT(target, attachment, textarget, crStateGetTextureHWID(&cr_server.StateTracker, texture), level, zoffset);
 }
 
@@ -87,9 +87,9 @@
     GLint rfb = 0, dfb = 0;
 #endif
-        crStateBindFramebufferEXT(target, framebuffer);
+    crStateBindFramebufferEXT(&cr_server.StateTracker, target, framebuffer);
 
     if (0==framebuffer)
     {
-        CRContext *ctx = crStateGetCurrent();
+        CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
         if (ctx->buffer.drawBuffer == GL_FRONT || ctx->buffer.drawBuffer == GL_FRONT_LEFT || ctx->buffer.drawBuffer == GL_FRONT_RIGHT)
             cr_server.curClient->currentMural->bFbDraw = GL_TRUE;
@@ -149,5 +149,5 @@
     else
     {
-        cr_server.head_spu->dispatch_table.BindFramebufferEXT(target, crStateGetFramebufferHWID(framebuffer));
+        cr_server.head_spu->dispatch_table.BindFramebufferEXT(target, crStateGetFramebufferHWID(&cr_server.StateTracker, framebuffer));
 #ifdef DEBUG_misha
         cr_server.head_spu->dispatch_table.GetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &rfb);
@@ -155,14 +155,14 @@
         if (GL_FRAMEBUFFER_EXT == target)
         {
-            Assert(rfb == crStateGetFramebufferHWID(framebuffer));
-            Assert(dfb == crStateGetFramebufferHWID(framebuffer));
+            Assert(rfb == crStateGetFramebufferHWID(&cr_server.StateTracker, framebuffer));
+            Assert(dfb == crStateGetFramebufferHWID(&cr_server.StateTracker, framebuffer));
         }
         else if (GL_READ_FRAMEBUFFER_EXT == target)
         {
-            Assert(rfb == crStateGetFramebufferHWID(framebuffer));
+            Assert(rfb == crStateGetFramebufferHWID(&cr_server.StateTracker, framebuffer));
         }
         else if (GL_DRAW_FRAMEBUFFER_EXT == target)
         {
-            Assert(dfb == crStateGetFramebufferHWID(framebuffer));
+            Assert(dfb == crStateGetFramebufferHWID(&cr_server.StateTracker, framebuffer));
         }
         else
@@ -176,6 +176,6 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchBindRenderbufferEXT(GLenum target, GLuint renderbuffer)
 {
-        crStateBindRenderbufferEXT(target, renderbuffer);
-        cr_server.head_spu->dispatch_table.BindRenderbufferEXT(target, crStateGetRenderbufferHWID(renderbuffer));
+        crStateBindRenderbufferEXT(&cr_server.StateTracker, target, renderbuffer);
+        cr_server.head_spu->dispatch_table.BindRenderbufferEXT(target, crStateGetRenderbufferHWID(&cr_server.StateTracker, renderbuffer));
 }
 
@@ -188,5 +188,5 @@
     }
 
-    crStateDeleteFramebuffersEXT(n, framebuffers);
+    crStateDeleteFramebuffersEXT(&cr_server.StateTracker, n, framebuffers);
 }
 
@@ -199,5 +199,5 @@
     }
 
-    crStateDeleteRenderbuffersEXT(n, renderbuffers);
+    crStateDeleteRenderbuffersEXT(&cr_server.StateTracker, n, renderbuffers);
 }
 
@@ -205,6 +205,6 @@
 crServerDispatchFramebufferRenderbufferEXT(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)
 {
-        crStateFramebufferRenderbufferEXT(target, attachment, renderbuffertarget, renderbuffer);
-        cr_server.head_spu->dispatch_table.FramebufferRenderbufferEXT(target, attachment, renderbuffertarget, crStateGetRenderbufferHWID(renderbuffer));
+        crStateFramebufferRenderbufferEXT(&cr_server.StateTracker, target, attachment, renderbuffertarget, renderbuffer);
+        cr_server.head_spu->dispatch_table.FramebufferRenderbufferEXT(target, attachment, renderbuffertarget, crStateGetRenderbufferHWID(&cr_server.StateTracker, renderbuffer));
 }
 
@@ -214,5 +214,5 @@
         GLint local_params[1];
         (void) params;
-        crStateGetFramebufferAttachmentParameterivEXT(target, attachment, pname, local_params);
+        crStateGetFramebufferAttachmentParameterivEXT(&cr_server.StateTracker, target, attachment, pname, local_params);
 
         crServerReturnValue(&(local_params[0]), 1*sizeof(GLint));
@@ -223,5 +223,5 @@
     /* since GenFramebuffers/Renderbuffers issued to host ogl only on bind + some other ops, the host drivers may not know about them
      * so use state data*/
-    GLboolean retval = crStateIsFramebufferEXT(framebuffer);
+    GLboolean retval = crStateIsFramebufferEXT(&cr_server.StateTracker, framebuffer);
     crServerReturnValue( &retval, sizeof(retval) );
     return retval; /* WILL PROBABLY BE IGNORED */
@@ -232,5 +232,5 @@
     /* since GenFramebuffers/Renderbuffers issued to host ogl only on bind + some other ops, the host drivers may not know about them
      * so use state data*/
-    GLboolean retval = crStateIsRenderbufferEXT(renderbuffer);
+    GLboolean retval = crStateIsRenderbufferEXT(&cr_server.StateTracker, renderbuffer);
     crServerReturnValue( &retval, sizeof(retval) );
     return retval; /* WILL PROBABLY BE IGNORED */
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_gentextures.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_gentextures.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_gentextures.cpp	(revision 78375)
@@ -31,5 +31,5 @@
     }
 
-    crStateGenTextures(n, local_textures);
+    crStateGenTextures(&cr_server.StateTracker, n, local_textures);
 
     crServerReturnValue(local_textures, n*sizeof(*local_textures));
@@ -112,5 +112,5 @@
     {
         GLuint tID = crServerTranslateProgramID(local_progs[i]);
-        while (crStateIsProgramARB(tID))
+        while (crStateIsProgramARB(&cr_server.StateTracker, tID))
         {
             cr_server.head_spu->dispatch_table.GenProgramsARB(1, &tID);
@@ -138,5 +138,5 @@
     }
 
-    crStateCopyTexImage2D(target, level, internalFormat, x, y, width, height, border);
+    crStateCopyTexImage2D(&cr_server.StateTracker, target, level, internalFormat, x, y, width, height, border);
     cr_server.head_spu->dispatch_table.CopyTexImage2D(target, level, internalFormat, x, y, width, height, border);
 }
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_get.py
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_get.py	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_get.py	(revision 78375)
@@ -138,5 +138,5 @@
         if func_name in convert_bufferid:
             print('\tif (pname==GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_ARB){')
-            print('\t\tlocal_params[0]=(%s)crStateBufferHWIDtoID((GLint)local_params[0]);' % (local_argtype))
+            print('\t\tlocal_params[0]=(%s)crStateBufferHWIDtoID(&cr_server.StateTracker, (GLint)local_params[0]);' % (local_argtype))
             print('\t}')
 
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_getpixelmap.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_getpixelmap.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_getpixelmap.cpp	(revision 78375)
@@ -64,5 +64,5 @@
 {
 #ifdef CR_ARB_pixel_buffer_object
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         GLvoid *pbo_offset;
@@ -91,5 +91,5 @@
 {
 #ifdef CR_ARB_pixel_buffer_object
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         GLvoid *pbo_offset;
@@ -118,5 +118,5 @@
 {
 #ifdef CR_ARB_pixel_buffer_object
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         GLvoid *pbo_offset;
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_getshaders.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_getshaders.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_getshaders.cpp	(revision 78375)
@@ -52,5 +52,5 @@
     }
 
-    cr_server.head_spu->dispatch_table.GetActiveAttrib(crStateGetProgramHWID(program), index, bufSize, &pLocal->length, &pLocal->size, &pLocal->type, (char*)&pLocal[1]);
+    cr_server.head_spu->dispatch_table.GetActiveAttrib(crStateGetProgramHWID(&cr_server.StateTracker, program), index, bufSize, &pLocal->length, &pLocal->size, &pLocal->type, (char*)&pLocal[1]);
     crServerReturnValue(pLocal, pLocal->length+1+sizeof(crGetActive_t));
     crFree(pLocal);
@@ -74,5 +74,5 @@
     }
 
-    cr_server.head_spu->dispatch_table.GetActiveUniform(crStateGetProgramHWID(program), index, bufSize, &pLocal->length, &pLocal->size, &pLocal->type, (char*)&pLocal[1]);
+    cr_server.head_spu->dispatch_table.GetActiveUniform(crStateGetProgramHWID(&cr_server.StateTracker, program), index, bufSize, &pLocal->length, &pLocal->size, &pLocal->type, (char*)&pLocal[1]);
     crServerReturnValue(pLocal, pLocal->length+1+sizeof(crGetActive_t));
     crFree(pLocal);
@@ -96,5 +96,5 @@
     /* initial (fallback )value */
     *pLocal = 0;
-    cr_server.head_spu->dispatch_table.GetAttachedShaders(crStateGetProgramHWID(program), maxCount, pLocal, (GLuint*)&pLocal[1]);
+    cr_server.head_spu->dispatch_table.GetAttachedShaders(crStateGetProgramHWID(&cr_server.StateTracker, program), maxCount, pLocal, (GLuint*)&pLocal[1]);
 
     {
@@ -103,5 +103,5 @@
 
         for (i=0; i<*pLocal; ++i)
-          ids[i] = crStateGLSLShaderHWIDtoID(ids[i]);
+          ids[i] = crStateGLSLShaderHWIDtoID(&cr_server.StateTracker, ids[i]);
     }
 
@@ -127,5 +127,5 @@
     /* initial (fallback )value */
     *pLocal = 0;
-    cr_server.head_spu->dispatch_table.GetAttachedObjectsARB(crStateGetProgramHWID(containerObj), maxCount, pLocal, (VBoxGLhandleARB*)&pLocal[1]);
+    cr_server.head_spu->dispatch_table.GetAttachedObjectsARB(crStateGetProgramHWID(&cr_server.StateTracker, containerObj), maxCount, pLocal, (VBoxGLhandleARB*)&pLocal[1]);
 
     {
@@ -134,5 +134,5 @@
 
         for (i=0; i<*pLocal; ++i)
-          ids[i] = crStateGLSLShaderHWIDtoID(ids[i]);
+          ids[i] = crStateGLSLShaderHWIDtoID(&cr_server.StateTracker, ids[i]);
     }
 
@@ -162,6 +162,6 @@
     *pLocal = 0;
     /** @todo recheck*/
-    hwid = crStateGetProgramHWID(obj);
-    if (!hwid) hwid = crStateGetShaderHWID(obj);
+    hwid = crStateGetProgramHWID(&cr_server.StateTracker, obj);
+    if (!hwid) hwid = crStateGetShaderHWID(&cr_server.StateTracker, obj);
     cr_server.head_spu->dispatch_table.GetInfoLogARB(hwid, maxLength, pLocal, (char*)&pLocal[1]);
     CRASSERT((*pLocal) <= maxLength);
@@ -187,5 +187,5 @@
     /* initial (fallback )value */
     *pLocal = 0;
-    cr_server.head_spu->dispatch_table.GetShaderInfoLog(crStateGetShaderHWID(shader), bufSize, pLocal, (char*)&pLocal[1]);
+    cr_server.head_spu->dispatch_table.GetShaderInfoLog(crStateGetShaderHWID(&cr_server.StateTracker, shader), bufSize, pLocal, (char*)&pLocal[1]);
     crServerReturnValue(pLocal, pLocal[0]+sizeof(GLsizei));
     crFree(pLocal);
@@ -209,5 +209,5 @@
     /* initial (fallback )value */
     *pLocal = 0;
-    cr_server.head_spu->dispatch_table.GetProgramInfoLog(crStateGetProgramHWID(program), bufSize, pLocal, (char*)&pLocal[1]);
+    cr_server.head_spu->dispatch_table.GetProgramInfoLog(crStateGetProgramHWID(&cr_server.StateTracker, program), bufSize, pLocal, (char*)&pLocal[1]);
     CRASSERT(pLocal[0] <= bufSize);
     crServerReturnValue(pLocal, pLocal[0]+sizeof(GLsizei));
@@ -232,5 +232,5 @@
     /* initial (fallback )value */
     *pLocal = 0;
-    cr_server.head_spu->dispatch_table.GetShaderSource(crStateGetShaderHWID(shader), bufSize, pLocal, (char*)&pLocal[1]);
+    cr_server.head_spu->dispatch_table.GetShaderSource(crStateGetShaderHWID(&cr_server.StateTracker, shader), bufSize, pLocal, (char*)&pLocal[1]);
     CRASSERT(pLocal[0] <= bufSize);
     crServerReturnValue(pLocal, pLocal[0]+sizeof(GLsizei));
@@ -258,5 +258,5 @@
     /* initial (fallback )value */
     *pLocal = 0;
-    crStateGLSLProgramCacheUniforms(program, maxcbData, pLocal, (char*)&pLocal[1]);
+    crStateGLSLProgramCacheUniforms(&cr_server.StateTracker, program, maxcbData, pLocal, (char*)&pLocal[1]);
 
     crServerReturnValue(pLocal, (*pLocal)+sizeof(GLsizei));
@@ -284,5 +284,5 @@
     /* initial (fallback )value */
     *pLocal = 0;
-    crStateGLSLProgramCacheAttribs(program, maxcbData, pLocal, (char*)&pLocal[1]);
+    crStateGLSLProgramCacheAttribs(&cr_server.StateTracker, program, maxcbData, pLocal, (char*)&pLocal[1]);
 
     crServerReturnValue(pLocal, (*pLocal)+sizeof(GLsizei));
@@ -296,7 +296,7 @@
 
     /** @todo check if index and location is the same*/
-    cr_server.head_spu->dispatch_table.GetActiveUniform(crStateGetProgramHWID(program), location, 0, NULL, &size, &type, NULL);
-
-    return crStateGetUniformSize(type);
+    cr_server.head_spu->dispatch_table.GetActiveUniform(crStateGetProgramHWID(&cr_server.StateTracker, program), location, 0, NULL, &size, &type, NULL);
+
+    return crStateGetUniformSize(&cr_server.StateTracker, type);
 }
 
@@ -316,5 +316,5 @@
     }
 
-    cr_server.head_spu->dispatch_table.GetUniformfv(crStateGetProgramHWID(program), location, pLocal);
+    cr_server.head_spu->dispatch_table.GetUniformfv(crStateGetProgramHWID(&cr_server.StateTracker, program), location, pLocal);
 
     crServerReturnValue(pLocal, size);
@@ -337,5 +337,5 @@
     }
 
-    cr_server.head_spu->dispatch_table.GetUniformiv(crStateGetProgramHWID(program), location, pLocal);
+    cr_server.head_spu->dispatch_table.GetUniformiv(crStateGetProgramHWID(&cr_server.StateTracker, program), location, pLocal);
 
     crServerReturnValue(pLocal, size);
@@ -347,5 +347,5 @@
     GLuint retval, hwVal;
     hwVal = cr_server.head_spu->dispatch_table.CreateShader(type);
-    retval = crStateCreateShader(hwVal, type);
+    retval = crStateCreateShader(&cr_server.StateTracker, hwVal, type);
     crServerReturnValue(&retval, sizeof(retval));
     return retval; /* ignored */
@@ -356,5 +356,5 @@
     GLuint retval, hwVal;
     hwVal = cr_server.head_spu->dispatch_table.CreateProgram();
-    retval = crStateCreateProgram(hwVal);
+    retval = crStateCreateProgram(&cr_server.StateTracker, hwVal);
     crServerReturnValue(&retval, sizeof(retval));
     return retval; /* ignored */
@@ -364,5 +364,5 @@
 {
     GLboolean retval;
-    retval = cr_server.head_spu->dispatch_table.IsShader(crStateGetShaderHWID(shader));
+    retval = cr_server.head_spu->dispatch_table.IsShader(crStateGetShaderHWID(&cr_server.StateTracker, shader));
     crServerReturnValue(&retval, sizeof(retval));
     return retval; /* ignored */
@@ -372,5 +372,5 @@
 {
     GLboolean retval;
-    retval = cr_server.head_spu->dispatch_table.IsProgram(crStateGetProgramHWID(program));
+    retval = cr_server.head_spu->dispatch_table.IsProgram(crStateGetProgramHWID(&cr_server.StateTracker, program));
     crServerReturnValue(&retval, sizeof(retval));
     return retval; /* ignored */
@@ -380,10 +380,10 @@
 {
     GLfloat local_params[1] = {0};
-    GLuint hwid = crStateGetProgramHWID(obj);
+    GLuint hwid = crStateGetProgramHWID(&cr_server.StateTracker, obj);
     (void) params;
 
     if (!hwid)
     {
-        hwid = crStateGetShaderHWID(obj);
+        hwid = crStateGetShaderHWID(&cr_server.StateTracker, obj);
         if (!hwid)
         {
@@ -399,8 +399,8 @@
 {
     GLint local_params[1] = {0};
-    GLuint hwid = crStateGetProgramHWID(obj);
+    GLuint hwid = crStateGetProgramHWID(&cr_server.StateTracker, obj);
     if (!hwid)
     {
-        hwid = crStateGetShaderHWID(obj);
+        hwid = crStateGetShaderHWID(&cr_server.StateTracker, obj);
         if (!hwid)
         {
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_getteximage.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_getteximage.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_getteximage.cpp	(revision 78375)
@@ -21,5 +21,5 @@
 
 #ifdef CR_ARB_pixel_buffer_object
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         GLvoid *pbo_offset;
@@ -104,5 +104,5 @@
 
 #ifdef CR_ARB_pixel_buffer_object
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         GLvoid *pbo_offset;
@@ -136,5 +136,5 @@
 {
 #ifdef CR_ARB_pixel_buffer_object
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         GLvoid *pbo_offset;
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_glsl.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_glsl.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_glsl.cpp	(revision 78375)
@@ -32,5 +32,5 @@
     GLenum err = cr_server.head_spu->dispatch_table.GetError();
 #endif
-    cr_server.head_spu->dispatch_table.ShaderSource(crStateGetShaderHWID(shader), count, string, length);
+    cr_server.head_spu->dispatch_table.ShaderSource(crStateGetShaderHWID(&cr_server.StateTracker, shader), count, string, length);
 #ifdef DEBUG_misha
     err = cr_server.head_spu->dispatch_table.GetError();
@@ -45,8 +45,8 @@
     GLint iCompileStatus = GL_FALSE;
 #endif
-    crStateCompileShader(shader);
-    cr_server.head_spu->dispatch_table.CompileShader(crStateGetShaderHWID(shader));
-#ifdef DEBUG_misha
-    cr_server.head_spu->dispatch_table.GetShaderiv(crStateGetShaderHWID(shader), GL_COMPILE_STATUS, &iCompileStatus);
+    crStateCompileShader(&cr_server.StateTracker, shader);
+    cr_server.head_spu->dispatch_table.CompileShader(crStateGetShaderHWID(&cr_server.StateTracker, shader));
+#ifdef DEBUG_misha
+    cr_server.head_spu->dispatch_table.GetShaderiv(crStateGetShaderHWID(&cr_server.StateTracker, shader), GL_COMPILE_STATUS, &iCompileStatus);
     Assert(iCompileStatus == GL_TRUE);
 #endif
@@ -56,6 +56,6 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchDeleteShader(GLuint shader)
 {
-    GLuint shaderHW = crStateGetShaderHWID(shader);
-    crStateDeleteShader(shader);
+    GLuint shaderHW = crStateGetShaderHWID(&cr_server.StateTracker, shader);
+    crStateDeleteShader(&cr_server.StateTracker, shader);
     if (shaderHW)
         cr_server.head_spu->dispatch_table.DeleteShader(shaderHW);
@@ -66,18 +66,20 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchAttachShader(GLuint program, GLuint shader)
 {
-    crStateAttachShader(program, shader);
-    cr_server.head_spu->dispatch_table.AttachShader(crStateGetProgramHWID(program), crStateGetShaderHWID(shader));
+    crStateAttachShader(&cr_server.StateTracker, program, shader);
+    cr_server.head_spu->dispatch_table.AttachShader(crStateGetProgramHWID(&cr_server.StateTracker, program),
+                                                    crStateGetShaderHWID(&cr_server.StateTracker, shader));
 }
 
 void SERVER_DISPATCH_APIENTRY crServerDispatchDetachShader(GLuint program, GLuint shader)
 {
-    crStateDetachShader(program, shader);
-    cr_server.head_spu->dispatch_table.DetachShader(crStateGetProgramHWID(program), crStateGetShaderHWID(shader));
+    crStateDetachShader(&cr_server.StateTracker, program, shader);
+    cr_server.head_spu->dispatch_table.DetachShader(crStateGetProgramHWID(&cr_server.StateTracker, program),
+                                                    crStateGetShaderHWID(&cr_server.StateTracker, shader));
 }
 
 void SERVER_DISPATCH_APIENTRY crServerDispatchLinkProgram(GLuint program)
 {
-    crStateLinkProgram(program);
-    cr_server.head_spu->dispatch_table.LinkProgram(crStateGetProgramHWID(program));
+    crStateLinkProgram(&cr_server.StateTracker, program);
+    cr_server.head_spu->dispatch_table.LinkProgram(crStateGetProgramHWID(&cr_server.StateTracker, program));
     CR_SERVER_DUMP_LINK_PROGRAM(program);
 }
@@ -85,12 +87,12 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchUseProgram(GLuint program)
 {
-    crStateUseProgram(program);
-    cr_server.head_spu->dispatch_table.UseProgram(crStateGetProgramHWID(program));
+    crStateUseProgram(&cr_server.StateTracker, program);
+    cr_server.head_spu->dispatch_table.UseProgram(crStateGetProgramHWID(&cr_server.StateTracker, program));
 }
 
 void SERVER_DISPATCH_APIENTRY crServerDispatchDeleteProgram(GLuint program)
 {
-    GLuint hwId = crStateGetProgramHWID(program);
-    crStateDeleteProgram(program);
+    GLuint hwId = crStateGetProgramHWID(&cr_server.StateTracker, program);
+    crStateDeleteProgram(&cr_server.StateTracker, program);
     if (hwId)
         cr_server.head_spu->dispatch_table.DeleteProgram(hwId);
@@ -101,17 +103,17 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchValidateProgram(GLuint program)
 {
-    crStateValidateProgram(program);
-    cr_server.head_spu->dispatch_table.ValidateProgram(crStateGetProgramHWID(program));
+    crStateValidateProgram(&cr_server.StateTracker, program);
+    cr_server.head_spu->dispatch_table.ValidateProgram(crStateGetProgramHWID(&cr_server.StateTracker, program));
 }
 
 void SERVER_DISPATCH_APIENTRY crServerDispatchBindAttribLocation(GLuint program, GLuint index, const char * name)
 {
-    crStateBindAttribLocation(program, index, name);
-    cr_server.head_spu->dispatch_table.BindAttribLocation(crStateGetProgramHWID(program), index, name);
+    crStateBindAttribLocation(&cr_server.StateTracker, program, index, name);
+    cr_server.head_spu->dispatch_table.BindAttribLocation(crStateGetProgramHWID(&cr_server.StateTracker, program), index, name);
 }
 
 void SERVER_DISPATCH_APIENTRY crServerDispatchDeleteObjectARB(VBoxGLhandleARB obj)
 {
-    GLuint hwid =  crStateDeleteObjectARB(obj);
+    GLuint hwid =  crStateDeleteObjectARB(&cr_server.StateTracker, obj);
 
     if (hwid)
@@ -124,5 +126,5 @@
 {
     GLint retval;
-    retval = cr_server.head_spu->dispatch_table.GetAttribLocation(crStateGetProgramHWID(program), name );
+    retval = cr_server.head_spu->dispatch_table.GetAttribLocation(crStateGetProgramHWID(&cr_server.StateTracker, program), name );
     crServerReturnValue( &retval, sizeof(retval) );
     return retval; /* WILL PROBABLY BE IGNORED */
@@ -135,5 +137,5 @@
     if (pname==GL_PROGRAM_OBJECT_ARB)
     {
-        retval = crStateGLSLProgramHWIDtoID(retval);
+        retval = crStateGLSLProgramHWIDtoID(&cr_server.StateTracker, retval);
     }
     crServerReturnValue( &retval, sizeof(retval) );
@@ -144,5 +146,5 @@
 {
     GLint retval;
-    retval = cr_server.head_spu->dispatch_table.GetUniformLocation(crStateGetProgramHWID(program), name);
+    retval = cr_server.head_spu->dispatch_table.GetUniformLocation(crStateGetProgramHWID(&cr_server.StateTracker, program), name);
     crServerReturnValue( &retval, sizeof(retval) );
     return retval; /* WILL PROBABLY BE IGNORED */
@@ -153,5 +155,5 @@
     GLint local_params[1] = {0};
     (void) params;
-    cr_server.head_spu->dispatch_table.GetProgramiv(crStateGetProgramHWID(program), pname, local_params);
+    cr_server.head_spu->dispatch_table.GetProgramiv(crStateGetProgramHWID(&cr_server.StateTracker, program), pname, local_params);
     crServerReturnValue( &(local_params[0]), 1*sizeof(GLint) );
 }
@@ -161,5 +163,5 @@
     GLint local_params[1] = {0};
     (void) params;
-    cr_server.head_spu->dispatch_table.GetShaderiv( crStateGetShaderHWID(shader), pname, local_params );
+    cr_server.head_spu->dispatch_table.GetShaderiv( crStateGetShaderHWID(&cr_server.StateTracker, shader), pname, local_params );
     crServerReturnValue( &(local_params[0]), 1*sizeof(GLint) );
 }
@@ -197,5 +199,5 @@
         pLocalProgs[i] = crServerTranslateProgramID(programs[i]);
     }
-    crStateDeleteProgramsARB(n, pLocalProgs);
+    crStateDeleteProgramsARB(&cr_server.StateTracker, n, pLocalProgs);
     cr_server.head_spu->dispatch_table.DeleteProgramsARB(n, pLocalProgs);
     crFree(pLocalProgs);
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_lists.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_lists.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_lists.cpp	(revision 78375)
@@ -64,5 +64,5 @@
 
     list = TranslateListID( list );
-    crStateNewList( list, mode );
+    crStateNewList(&cr_server.StateTracker, list, mode );
     cr_server.head_spu->dispatch_table.NewList( list, mode );
 }
@@ -84,5 +84,5 @@
             fbFbo = bbFbo = 0;
         }
-        crStateQueryHWState(fbFbo, bbFbo);
+        crStateQueryHWState(&cr_server.StateTracker, fbFbo, bbFbo);
     }
 }
@@ -90,9 +90,9 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchEndList(void)
 {
-    CRContext *g = crStateGetCurrent();
+    CRContext *g = crStateGetCurrent(&cr_server.StateTracker);
     CRListsState *l = &(g->lists);
 
     cr_server.head_spu->dispatch_table.EndList();
-    crStateEndList();
+    crStateEndList(&cr_server.StateTracker);
 
 #ifndef IN_GUEST
@@ -272,5 +272,5 @@
 {
     list = TranslateListID( list );
-    crStateDeleteLists( list, range );
+    crStateDeleteLists(&cr_server.StateTracker, list, range );
     cr_server.head_spu->dispatch_table.DeleteLists( list, range );
 }
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_main.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_main.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_main.cpp	(revision 78375)
@@ -51,4 +51,5 @@
  */
 CRServer cr_server;
+DECLHIDDEN(PCRStateTracker) g_pStateTracker;
 
 int tearingdown = 0; /* can't be static */
@@ -124,5 +125,5 @@
 {
     CRContextInfo *c = (CRContextInfo *) data;
-    crStateDestroyContext(c->pContext);
+    crStateDestroyContext(&cr_server.StateTracker, c->pContext);
     if (c->CreateInfo.pszDpyName)
         crFree(c->CreateInfo.pszDpyName);
@@ -180,5 +181,5 @@
     }
 
-    crStateSetCurrent( NULL );
+    crStateSetCurrent(&cr_server.StateTracker, NULL);
 
     cr_server.curClient = NULL;
@@ -218,8 +219,8 @@
     if (!fContextsDeleted)
     {
-        fOldEnableDiff = crStateEnableDiffOnMakeCurrent(GL_FALSE);
+        fOldEnableDiff = crStateEnableDiffOnMakeCurrent(&cr_server.StateTracker, GL_FALSE);
         if(cr_server.MainContextInfo.pContext)
-            crStateMakeCurrent(cr_server.MainContextInfo.pContext);
-        crStateEnableDiffOnMakeCurrent(fOldEnableDiff);
+            crStateMakeCurrent(&cr_server.StateTracker, cr_server.MainContextInfo.pContext);
+        crStateEnableDiffOnMakeCurrent(&cr_server.StateTracker, fOldEnableDiff);
     }
 
@@ -270,5 +271,5 @@
 #endif
 
-    crStateDestroy();
+    crStateDestroy(&cr_server.StateTracker);
 
     crNetTearDown();
@@ -356,5 +357,6 @@
     cr_server.programTable = crAllocHashtable();
 
-    crStateInit();
+    crStateInit(&cr_server.StateTracker);
+    g_pStateTracker = &cr_server.StateTracker;
 
     crStateLimitsInit( &(cr_server.limits) );
@@ -393,5 +395,5 @@
     if (!cr_server.head_spu)
     {
-        crStateDestroy();
+        crStateDestroy(&cr_server.StateTracker);
         return GL_FALSE;
     }
@@ -399,5 +401,5 @@
     crServerInitDispatch();
     crServerInitTmpCtxDispatch();
-    crStateDiffAPI( &(cr_server.head_spu->dispatch_table) );
+    crStateDiffAPI(&cr_server.StateTracker, &(cr_server.head_spu->dispatch_table) );
 
 #ifdef VBOX_WITH_CRSERVER_DUMPER
@@ -410,5 +412,5 @@
 
     /*Check for PBO support*/
-    if (crStateGetCurrent()->extensions.ARB_pixel_buffer_object)
+    if (crStateGetCurrent(&cr_server.StateTracker)->extensions.ARB_pixel_buffer_object)
     {
         cr_server.bUsePBOForReadback=GL_TRUE;
@@ -1422,5 +1424,5 @@
     crVBoxServerBuildSaveStateGlobal(&Data);
 
-    rc = crStateSaveGlobals(pSSM);
+    rc = crStateSaveGlobals(&cr_server.StateTracker, pSSM);
     AssertRCReturn(rc, rc);
 
@@ -2084,5 +2086,5 @@
     }
 
-    rc = crStateLoadGlobals(pSSM, version);
+    rc = crStateLoadGlobals(&cr_server.StateTracker, pSSM, version);
     AssertLogRelRCReturn(rc, rc);
 
@@ -2604,5 +2606,5 @@
 
     cr_server.head_spu->dispatch_table.MakeCurrent(0, 0, 0);
-    crStateCleanupCurrent();
+    crStateCleanupCurrent(&cr_server.StateTracker);
 
     /* note: we need to clean all contexts, since otherwise renderspu leanup won't work,
@@ -2612,5 +2614,5 @@
     {
         cr_server.head_spu->dispatch_table.DestroyContext(cr_server.MainContextInfo.SpuContext);
-        crStateDestroyContext(cr_server.MainContextInfo.pContext);
+        crStateDestroyContext(&cr_server.StateTracker, cr_server.MainContextInfo.pContext);
         if (cr_server.MainContextInfo.CreateInfo.pszDpyName)
             crFree(cr_server.MainContextInfo.CreateInfo.pszDpyName);
@@ -2630,5 +2632,5 @@
     cr_server.currentMural = NULL;
 
-    crStateDestroy();
+    crStateDestroy(&cr_server.StateTracker);
 //    crStateCleanupCurrent();
 
@@ -2651,6 +2653,6 @@
 
 //    crStateSetCurrent(NULL);
-    crStateInit();
-    crStateDiffAPI( &(cr_server.head_spu->dispatch_table) );
+    crStateInit(&cr_server.StateTracker);
+    crStateDiffAPI(&cr_server.StateTracker, &(cr_server.head_spu->dispatch_table) );
 
     CrPMgrEnable();
@@ -3835,3 +3837,14 @@
 }
 
+void crVBoxServerDetachThread(void)
+{
+    crStateVBoxDetachThread(&cr_server.StateTracker);
+}
+
+void crVBoxServerAttachThread(void)
+{
+    crStateVBoxAttachThread(&cr_server.StateTracker);
+}
+
+
 #endif
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_misc.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_misc.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_misc.cpp	(revision 78375)
@@ -328,17 +328,17 @@
   switch (target) {
     case GL_SHARE_CONTEXT_RESOURCES_CR:
-        crStateShareContext(value);
+        crStateShareContext(&cr_server.StateTracker, value);
         break;
     case GL_RCUSAGE_TEXTURE_SET_CR:
-        crStateSetTextureUsed(value, GL_TRUE);
+        crStateSetTextureUsed(&cr_server.StateTracker, value, GL_TRUE);
         break;
     case GL_RCUSAGE_TEXTURE_CLEAR_CR:
-        crStateSetTextureUsed(value, GL_FALSE);
+        crStateSetTextureUsed(&cr_server.StateTracker, value, GL_FALSE);
         break;
     case GL_PIN_TEXTURE_SET_CR:
-        crStatePinTexture(value, GL_TRUE);
+        crStatePinTexture(&cr_server.StateTracker, value, GL_TRUE);
         break;
     case GL_PIN_TEXTURE_CLEAR_CR:
-        crStatePinTexture(value, GL_FALSE);
+        crStatePinTexture(&cr_server.StateTracker, value, GL_FALSE);
         break;
     case GL_SHARED_DISPLAY_LISTS_CR:
@@ -502,5 +502,5 @@
             GLuint tID, fboID;
             GLenum status;
-            CRContext *ctx = crStateGetCurrent();
+            CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
 
             gl->GenTextures(1, &tID);
@@ -601,5 +601,5 @@
             GLint dRow;
             GLuint pboId, sRow;
-            CRContext *ctx = crStateGetCurrent();
+            CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
 
             gl->GenBuffersARB(1, &pboId);
@@ -897,5 +897,5 @@
     CR_BLITTER_CONTEXT Ctx;
     CRMuralInfo *mural;
-    CRContext *ctx = crStateGetCurrent();
+    CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
     PVBOXVR_TEXTURE pDrawTex, pReadTex;
     VBOXVR_TEXTURE DrawTex, ReadTex;
@@ -992,5 +992,5 @@
                                    GLbitfield mask, GLenum filter)
 {
-    CRContext *ctx = crStateGetCurrent();
+    CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
     bool fTryBlitter = false;
 #ifdef CR_CHECK_BLITS
@@ -1274,7 +1274,7 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchDrawBuffer( GLenum mode )
 {
-    crStateDrawBuffer( mode );
-
-    if (!crStateGetCurrent()->framebufferobject.drawFB)
+    crStateDrawBuffer(&cr_server.StateTracker, mode );
+
+    if (!crStateGetCurrent(&cr_server.StateTracker)->framebufferobject.drawFB)
     {
         if (mode == GL_FRONT || mode == GL_FRONT_LEFT || mode == GL_FRONT_RIGHT)
@@ -1362,9 +1362,9 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchReadBuffer( GLenum mode )
 {
-    crStateReadBuffer( mode );
+    crStateReadBuffer(&cr_server.StateTracker, mode );
 
     if (crServerIsRedirectedToFBO()
             && cr_server.curClient->currentMural->aidFBOs[0]
-            && !crStateGetCurrent()->framebufferobject.readFB)
+            && !crStateGetCurrent(&cr_server.StateTracker)->framebufferobject.readFB)
     {
         CRMuralInfo *mural = cr_server.curClient->currentMural;
@@ -1433,5 +1433,5 @@
 {
     GLenum retval, err;
-    CRContext *ctx = crStateGetCurrent();
+    CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
     retval = ctx->error;
 
@@ -1453,5 +1453,5 @@
 crServerMakeTmpCtxCurrent( GLint window, GLint nativeWindow, GLint context )
 {
-    CRContext *pCtx = crStateGetCurrent();
+    CRContext *pCtx = crStateGetCurrent(&cr_server.StateTracker);
     CRContext *pCurCtx = NULL;
     GLuint idDrawFBO = 0, idReadFBO = 0;
@@ -1890,5 +1890,5 @@
 GLvoid crServerSpriteCoordReplEnable(GLboolean fEnable)
 {
-    CRContext *g = crStateGetCurrent();
+    CRContext *g = crStateGetCurrent(&cr_server.StateTracker);
     CRTextureState *t = &(g->texture);
     GLuint curTextureUnit = t->curTextureUnit;
@@ -1948,8 +1948,8 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchEnd( void )
 {
-    CRContext *g = crStateGetCurrent();
+    CRContext *g = crStateGetCurrent(&cr_server.StateTracker);
     GLenum mode = g->current.mode;
 
-    crStateEnd();
+    crStateEnd(&cr_server.StateTracker);
     cr_server.head_spu->dispatch_table.End();
 
@@ -1963,5 +1963,5 @@
 {
 #ifdef DEBUG
-    CRContext *ctx = crStateGetCurrent();
+    CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
     SPUDispatchTable *gl = &cr_server.head_spu->dispatch_table;
 
@@ -2012,5 +2012,5 @@
     CR_SERVER_DUMP_DRAW_ENTER();
 
-    crStateBegin(mode);
+    crStateBegin(&cr_server.StateTracker, mode);
     cr_server.head_spu->dispatch_table.Begin(mode);
 }
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_muralfbo.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_muralfbo.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_muralfbo.cpp	(revision 78375)
@@ -477,15 +477,17 @@
         if (cr_server.curClient && cr_server.curClient->currentMural == mural)
         {
-            if (!crStateGetCurrent()->framebufferobject.drawFB)
+            CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
+
+            if (!ctx->framebufferobject.drawFB)
             {
                 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, CR_SERVER_FBO_FOR_IDX(mural, mural->iCurDrawBuffer));
             }
-            if (!crStateGetCurrent()->framebufferobject.readFB)
+            if (!ctx->framebufferobject.readFB)
             {
                 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, CR_SERVER_FBO_FOR_IDX(mural, mural->iCurReadBuffer));
             }
 
-            crStateGetCurrent()->buffer.width = 0;
-            crStateGetCurrent()->buffer.height = 0;
+            ctx->buffer.width = 0;
+            ctx->buffer.height = 0;
         }
     }
@@ -494,15 +496,17 @@
         if (cr_server.curClient && cr_server.curClient->currentMural == mural)
         {
-            if (!crStateGetCurrent()->framebufferobject.drawFB)
+            CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
+
+            if (!ctx->framebufferobject.drawFB)
             {
                 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
             }
-            if (!crStateGetCurrent()->framebufferobject.readFB)
+            if (!ctx->framebufferobject.readFB)
             {
                 cr_server.head_spu->dispatch_table.BindFramebufferEXT(GL_READ_FRAMEBUFFER, 0);
             }
 
-            crStateGetCurrent()->buffer.width = mural->width;
-            crStateGetCurrent()->buffer.height = mural->height;
+            ctx->buffer.width = mural->width;
+            ctx->buffer.height = mural->height;
         }
     }
@@ -513,5 +517,5 @@
 static void crServerCreateMuralFBO(CRMuralInfo *mural)
 {
-    CRContext *ctx = crStateGetCurrent();
+    CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
     GLuint uid, i;
     GLenum status;
@@ -540,5 +544,5 @@
     /*Color texture*/
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         gl->BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
@@ -605,10 +609,10 @@
     gl->BindFramebufferEXT(GL_READ_FRAMEBUFFER, uid);
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         gl->BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, ctx->bufferobject.unpackBuffer->hwid);
     }
 
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         gl->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, ctx->bufferobject.packBuffer->hwid);
@@ -825,5 +829,5 @@
 void crServerMuralFBOSwapBuffers(CRMuralInfo *mural)
 {
-    CRContext *ctx = crStateGetCurrent();
+    CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
     GLint iOldCurDrawBuffer = mural->iCurDrawBuffer;
     GLint iOldCurReadBuffer = mural->iCurReadBuffer;
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_projmatrix.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_projmatrix.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_projmatrix.cpp	(revision 78375)
@@ -44,5 +44,5 @@
     const GLenum matMode = cr_server.curClient->currentCtxInfo->pContext->transform.matrixMode;
 
-    crStateLoadMatrixf( m );
+    crStateLoadMatrixf(&cr_server.StateTracker, m );
 
     if (matMode == GL_MODELVIEW && cr_server.viewOverride) {
@@ -60,5 +60,5 @@
     const GLenum matMode = cr_server.curClient->currentCtxInfo->pContext->transform.matrixMode;
 
-    crStateLoadMatrixd( m );
+    crStateLoadMatrixd(&cr_server.StateTracker, m );
 
     if (matMode == GL_MODELVIEW && cr_server.viewOverride) {
@@ -79,9 +79,9 @@
         /* load the overriding projection matrix */
         int eye = crServerGetCurrentEye();
-        crStateLoadMatrix( &cr_server.projectionMatrix[eye] );
+        crStateLoadMatrix(&cr_server.StateTracker, &cr_server.projectionMatrix[eye] );
     }
     else {
         /* the usual case */
-        crStateMultMatrixf( m );
+        crStateMultMatrixf(&cr_server.StateTracker, m );
         cr_server.head_spu->dispatch_table.MultMatrixf( m );
     }
@@ -96,9 +96,9 @@
         /* load the overriding projection matrix */
         int eye = crServerGetCurrentEye();
-        crStateLoadMatrix( &cr_server.projectionMatrix[eye] );
+        crStateLoadMatrix(&cr_server.StateTracker, &cr_server.projectionMatrix[eye] );
     }
     else {
         /* the usual case */
-        crStateMultMatrixd( m );
+        crStateMultMatrixd(&cr_server.StateTracker, m );
         cr_server.head_spu->dispatch_table.MultMatrixd( m );
     }
@@ -111,5 +111,5 @@
     const GLenum matMode = cr_server.curClient->currentCtxInfo->pContext->transform.matrixMode;
 
-    crStateLoadIdentity();
+    crStateLoadIdentity(&cr_server.StateTracker);
 
     if (matMode == GL_MODELVIEW && cr_server.viewOverride) {
@@ -322,5 +322,5 @@
 
     /* pass through */
-    crStateProgramStringARB(target, format, len, string);
+    crStateProgramStringARB(&cr_server.StateTracker, target, format, len, string);
     cr_server.head_spu->dispatch_table.ProgramStringARB(target, format, len, string);
 }
@@ -364,5 +364,5 @@
 
     /* pass through */
-    crStateLoadProgramNV(target, id, len, string);
+    crStateLoadProgramNV(&cr_server.StateTracker, target, id, len, string);
     cr_server.head_spu->dispatch_table.LoadProgramNV(target, id, len, string);
 }
@@ -381,5 +381,5 @@
 
     /* pass through */
-    crStateBindProgramARB(target, id);
+    crStateBindProgramARB(&cr_server.StateTracker, target, id);
     cr_server.head_spu->dispatch_table.BindProgramARB(target, id);
 }
@@ -395,5 +395,5 @@
     }
     /* pass through */
-    crStateBindProgramNV(target, id);
+    crStateBindProgramNV(&cr_server.StateTracker, target, id);
     cr_server.head_spu->dispatch_table.BindProgramNV(target, id);
 }
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_readpixels.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_readpixels.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_readpixels.cpp	(revision 78375)
@@ -33,5 +33,5 @@
 
 #ifdef CR_ARB_pixel_buffer_object
-    if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
     {
         GLvoid *pbo_offset;
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_rpw.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_rpw.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_rpw.cpp	(revision 78375)
@@ -470,5 +470,5 @@
     pContext = cr_server.currentCtxInfo->pContext;
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
@@ -490,5 +490,5 @@
     pEntry->iTexDraw = -pEntry->iTexDraw;
 
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
     {
         cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pContext->bufferobject.unpackBuffer->hwid);
@@ -504,5 +504,5 @@
         }
 
-        if (crStateIsBufferBound(GL_PIXEL_PACK_BUFFER_ARB))
+        if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_PACK_BUFFER_ARB))
         {
             cr_server.head_spu->dispatch_table.BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pContext->bufferobject.packBuffer->hwid);
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_simpleget.py
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_simpleget.py	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_simpleget.py	(revision 78375)
@@ -62,5 +62,5 @@
         CRASSERT(tablesize/sizeof(%s)==1);
         texid = (GLuint) *get_values;
-        *get_values = (%s) crStateTextureHWIDtoID(texid);
+        *get_values = (%s) crStateTextureHWIDtoID(&cr_server.StateTracker, texid);
     }
     else if (GL_CURRENT_PROGRAM==pname)
@@ -69,5 +69,5 @@
         CRASSERT(tablesize/sizeof(%s)==1);
         programid = (GLuint) *get_values;
-        *get_values = (%s) crStateGLSLProgramHWIDtoID(programid);
+        *get_values = (%s) crStateGLSLProgramHWIDtoID(&cr_server.StateTracker, programid);
     }
     else if (GL_FRAMEBUFFER_BINDING_EXT==pname
@@ -85,5 +85,5 @@
         else
         {
-        	fboid = crStateFBOHWIDtoID(fboid);
+        	fboid = crStateFBOHWIDtoID(&cr_server.StateTracker, fboid);
         }
         *get_values = (%s) fboid;
@@ -93,8 +93,8 @@
         if (crServerIsRedirectedToFBO()
             && CR_SERVER_FBO_FOR_IDX(cr_server.curClient->currentMural, cr_server.curClient->currentMural->iCurReadBuffer)
-            && !crStateGetCurrent()->framebufferobject.readFB)
+            && !crStateGetCurrent(&cr_server.StateTracker)->framebufferobject.readFB)
         {
-            *get_values = (%s) crStateGetCurrent()->buffer.readBuffer;
-            Assert(crStateGetCurrent()->buffer.readBuffer == GL_BACK || crStateGetCurrent()->buffer.readBuffer == GL_FRONT);
+            *get_values = (%s) crStateGetCurrent(&cr_server.StateTracker)->buffer.readBuffer;
+            Assert(crStateGetCurrent(&cr_server.StateTracker)->buffer.readBuffer == GL_BACK || crStateGetCurrent(&cr_server.StateTracker)->buffer.readBuffer == GL_FRONT);
         }
     }
@@ -103,8 +103,8 @@
         if (crServerIsRedirectedToFBO()
             && CR_SERVER_FBO_FOR_IDX(cr_server.curClient->currentMural, cr_server.curClient->currentMural->iCurDrawBuffer)
-            && !crStateGetCurrent()->framebufferobject.drawFB)
+            && !crStateGetCurrent(&cr_server.StateTracker)->framebufferobject.drawFB)
         {
-            *get_values = (%s) crStateGetCurrent()->buffer.drawBuffer;
-            Assert(crStateGetCurrent()->buffer.drawBuffer == GL_BACK || crStateGetCurrent()->buffer.drawBuffer == GL_FRONT);
+            *get_values = (%s) crStateGetCurrent(&cr_server.StateTracker)->buffer.drawBuffer;
+            Assert(crStateGetCurrent(&cr_server.StateTracker)->buffer.drawBuffer == GL_BACK || crStateGetCurrent(&cr_server.StateTracker)->buffer.drawBuffer == GL_FRONT);
         }
     }
@@ -114,5 +114,5 @@
         CRASSERT(tablesize/sizeof(%s)==1);
         rbid = (GLuint) *get_values;
-        *get_values = (%s) crStateRBOHWIDtoID(rbid);
+        *get_values = (%s) crStateRBOHWIDtoID(&cr_server.StateTracker, rbid);
     }
     else if (GL_ARRAY_BUFFER_BINDING_ARB==pname
@@ -131,5 +131,5 @@
         CRASSERT(tablesize/sizeof(%s)==1);
         bufid = (GLuint) *get_values;
-        *get_values = (%s) crStateBufferHWIDtoID(bufid);
+        *get_values = (%s) crStateBufferHWIDtoID(&cr_server.StateTracker, bufid);
     }
     else if (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS==pname)
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_stream.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_stream.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_stream.cpp	(revision 78375)
@@ -580,4 +580,5 @@
         UnpackerState.pDispatchTbl     = &(cr_server.dispatch);
         UnpackerState.rcUnpack         = VINF_SUCCESS;
+        UnpackerState.pStateTracker    = &cr_server.StateTracker;
 
         cr_server.pUnpackerState = &UnpackerState;
@@ -695,4 +696,5 @@
         UnpackerState.pDispatchTbl     = &(cr_server.dispatch);
         UnpackerState.rcUnpack         = VINF_SUCCESS;
+        UnpackerState.pStateTracker    = &cr_server.StateTracker;
 
         cr_server.pUnpackerState = &UnpackerState;
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_texture.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_texture.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_texture.cpp	(revision 78375)
@@ -39,5 +39,5 @@
 #if defined(CR_ARB_pixel_buffer_object)
 # define CR_CHECKBUFFER(name, checkptr)                     \
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))   \
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))   \
     {                                                       \
         CR_FIXPTR();                                        \
@@ -53,5 +53,5 @@
 #if defined(CR_ARB_pixel_buffer_object) && !defined(CR_STATE_NO_TEXTURE_IMAGE_STORE)
 # define CR_FINISHBUFFER()                                                                  \
-    if (crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))                                   \
+    if (crStateIsBufferBound(&cr_server.StateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))          \
     {                                                                                       \
         if (!cr_server.head_spu->dispatch_table.UnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB)) \
@@ -64,26 +64,26 @@
 #endif
 
-#define CR_FUNC_SUBIMAGE(name, def, call, ptrname)          \
-void SERVER_DISPATCH_APIENTRY                               \
-crServerDispatch##name def                                  \
-{                                                           \
-    const GLvoid *realptr = ptrname;                        \
-    CR_CHECKBUFFER(name, CR_CHECKPTR(name))                 \
-    crState##name call;                                     \
-    CR_FINISHBUFFER()                                       \
-    realptr = ptrname;                                      \
-    cr_server.head_spu->dispatch_table.name call;           \
-}
-
-#define CR_FUNC_IMAGE(name, def, call, ptrname)             \
-void SERVER_DISPATCH_APIENTRY                               \
-crServerDispatch##name def                                  \
-{                                                           \
-    const GLvoid *realptr = ptrname;                        \
-    CR_CHECKBUFFER(name, CR_NOTHING())                      \
-    crState##name call;                                     \
-    CR_FINISHBUFFER()                                       \
-    realptr = ptrname;                                      \
-    cr_server.head_spu->dispatch_table.name call;           \
+#define CR_FUNC_SUBIMAGE(name, def, state_call, call, ptrname)          \
+void SERVER_DISPATCH_APIENTRY                                           \
+crServerDispatch##name def                                              \
+{                                                                       \
+    const GLvoid *realptr = ptrname;                                    \
+    CR_CHECKBUFFER(name, CR_CHECKPTR(name))                             \
+    crState##name state_call;                                           \
+    CR_FINISHBUFFER()                                                   \
+    realptr = ptrname;                                                  \
+    cr_server.head_spu->dispatch_table.name call;                       \
+}
+
+#define CR_FUNC_IMAGE(name, def, state_call, call, ptrname)             \
+void SERVER_DISPATCH_APIENTRY                                           \
+crServerDispatch##name def                                              \
+{                                                                       \
+    const GLvoid *realptr = ptrname;                                    \
+    CR_CHECKBUFFER(name, CR_NOTHING())                                  \
+    crState##name state_call;                                           \
+    CR_FINISHBUFFER()                                                   \
+    realptr = ptrname;                                                  \
+    cr_server.head_spu->dispatch_table.name call;                       \
 }
 
@@ -91,24 +91,30 @@
 CR_FUNC_SUBIMAGE(CompressedTexSubImage1DARB,
     (GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLsizei imagesize, const GLvoid * data),
+    (&cr_server.StateTracker, target, level, xoffset, width, format, imagesize, realptr),
     (target, level, xoffset, width, format, imagesize, realptr), data)
 
 CR_FUNC_SUBIMAGE(CompressedTexSubImage2DARB,
     (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imagesize, const GLvoid * data),
+    (&cr_server.StateTracker, target, level, xoffset, yoffset, width, height, format, imagesize, realptr),
     (target, level, xoffset, yoffset, width, height, format, imagesize, realptr), data)
 
 CR_FUNC_SUBIMAGE(CompressedTexSubImage3DARB,
     (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imagesize, const GLvoid * data),
+    (&cr_server.StateTracker, target, level, xoffset, yoffset, zoffset, width, height, depth, format, imagesize, realptr),
     (target, level, xoffset, yoffset, zoffset, width, height, depth, format, imagesize, realptr), data)
 
 CR_FUNC_IMAGE(CompressedTexImage1DARB,
     (GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLint border, GLsizei imagesize, const GLvoid * data),
+    (&cr_server.StateTracker, target, level, internalFormat, width, border, imagesize, realptr),
     (target, level, internalFormat, width, border, imagesize, realptr), data)
 
 CR_FUNC_IMAGE(CompressedTexImage2DARB,
     (GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLint border, GLsizei imagesize, const GLvoid * data),
+    (&cr_server.StateTracker, target, level, internalFormat, width, height, border, imagesize, realptr),
     (target, level, internalFormat, width, height, border, imagesize, realptr), data)
 
 CR_FUNC_IMAGE(CompressedTexImage3DARB,
     (GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imagesize, const GLvoid * data),
+    (&cr_server.StateTracker, target, level, internalFormat, width, height, depth, border, imagesize, realptr),
     (target, level, internalFormat, width, height, depth, border, imagesize, realptr), data)
 #endif
@@ -116,24 +122,30 @@
 CR_FUNC_SUBIMAGE(TexSubImage1D,
     (GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid * pixels),
+    (&cr_server.StateTracker, target, level, xoffset, width, format, type, realptr),
     (target, level, xoffset, width, format, type, realptr), pixels)
 
 CR_FUNC_SUBIMAGE(TexSubImage2D,
     (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid * pixels),
+    (&cr_server.StateTracker, target, level, xoffset, yoffset, width, height, format, type, realptr),
     (target, level, xoffset, yoffset, width, height, format, type, realptr), pixels)
 
 CR_FUNC_SUBIMAGE(TexSubImage3D,
     (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid * pixels),
+    (&cr_server.StateTracker, target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, realptr),
     (target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, realptr), pixels)
 
 CR_FUNC_IMAGE(TexImage1D,
     (GLenum target, GLint level, GLint internalFormat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid * pixels),
+    (&cr_server.StateTracker, target, level, internalFormat, width, border, format, type, realptr),
     (target, level, internalFormat, width, border, format, type, realptr), pixels)
 
 CR_FUNC_IMAGE(TexImage2D,
     (GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid * pixels),
+    (&cr_server.StateTracker, target, level, internalFormat, width, height, border, format, type, realptr),
     (target, level, internalFormat, width, height, border, format, type, realptr), pixels)
 
 CR_FUNC_IMAGE(TexImage3D,
     (GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid * pixels),
+    (&cr_server.StateTracker, target, level, internalFormat, width, height, depth, border, format, type, realptr),
     (target, level, internalFormat, width, height, depth, border, format, type, realptr), pixels)
 
@@ -141,5 +153,5 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchTexEnvf( GLenum target, GLenum pname, GLfloat param )
 {
-    crStateTexEnvf( target, pname, param );
+    crStateTexEnvf(&cr_server.StateTracker, target, pname, param );
     if (GL_POINT_SPRITE != target && pname != GL_COORD_REPLACE)
         CR_GLERR_CHECK(cr_server.head_spu->dispatch_table.TexEnvf( target, pname, param ););
@@ -148,5 +160,5 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchTexEnvfv( GLenum target, GLenum pname, const GLfloat * params )
 {
-    crStateTexEnvfv( target, pname, params );
+    crStateTexEnvfv(&cr_server.StateTracker, target, pname, params );
     if (GL_POINT_SPRITE != target && pname != GL_COORD_REPLACE)
         CR_GLERR_CHECK(cr_server.head_spu->dispatch_table.TexEnvfv( target, pname, params ););
@@ -155,5 +167,5 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchTexEnvi( GLenum target, GLenum pname, GLint param )
 {
-    crStateTexEnvi( target, pname, param );
+    crStateTexEnvi(&cr_server.StateTracker, target, pname, param );
     if (GL_POINT_SPRITE != target && pname != GL_COORD_REPLACE)
         CR_GLERR_CHECK(cr_server.head_spu->dispatch_table.TexEnvi( target, pname, param ););
@@ -162,5 +174,5 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchTexEnviv( GLenum target, GLenum pname, const GLint * params )
 {
-    crStateTexEnviv( target, pname, params );
+    crStateTexEnviv(&cr_server.StateTracker, target, pname, params );
     if (GL_POINT_SPRITE != target && pname != GL_COORD_REPLACE)
         CR_GLERR_CHECK(cr_server.head_spu->dispatch_table.TexEnviv( target, pname, params ););
@@ -175,5 +187,5 @@
         cr_server.head_spu->dispatch_table.GetTexEnvfv( target, pname, local_params );
     else
-        crStateGetTexEnvfv( target, pname, local_params );
+        crStateGetTexEnvfv(&cr_server.StateTracker, target, pname, local_params );
 
     cComponents = RT_MIN(crStateHlpComponentsCount(pname), RT_ELEMENTS(local_params));
@@ -189,5 +201,5 @@
         cr_server.head_spu->dispatch_table.GetTexEnviv( target, pname, local_params );
     else
-        crStateGetTexEnviv( target, pname, local_params );
+        crStateGetTexEnviv(&cr_server.StateTracker, target, pname, local_params );
 
     cComponents = RT_MIN(crStateHlpComponentsCount(pname), RT_ELEMENTS(local_params));
@@ -197,6 +209,6 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchBindTexture( GLenum target, GLuint texture )
 {
-    crStateBindTexture( target, texture );
-    cr_server.head_spu->dispatch_table.BindTexture(target, crStateGetTextureHWID(texture));
+    crStateBindTexture(&cr_server.StateTracker, target, texture );
+    cr_server.head_spu->dispatch_table.BindTexture(target, crStateGetTextureHWID(&cr_server.StateTracker, texture));
 }
 
@@ -223,5 +235,5 @@
     for (i = 0; i < n; i++)
     {
-        newTextures[i] = crStateGetTextureHWID(textures[i]);
+        newTextures[i] = crStateGetTextureHWID(&cr_server.StateTracker, textures[i]);
     }
 
@@ -232,5 +244,5 @@
 
 
-    crStateDeleteTextures(n, textures);
+    crStateDeleteTextures(&cr_server.StateTracker, n, textures);
     cr_server.head_spu->dispatch_table.DeleteTextures(n, newTextures);
     crFree(newTextures);
@@ -257,9 +269,9 @@
     }
 
-    crStatePrioritizeTextures(n, textures, priorities);
+    crStatePrioritizeTextures(&cr_server.StateTracker, n, textures, priorities);
 
     for (i = 0; i < n; i++)
     {
-        newTextures[i] = crStateGetTextureHWID(textures[i]);
+        newTextures[i] = crStateGetTextureHWID(&cr_server.StateTracker, textures[i]);
     }
 
@@ -273,5 +285,5 @@
 {
     GLboolean retval;
-    retval = cr_server.head_spu->dispatch_table.IsTexture(crStateGetTextureHWID(texture));
+    retval = cr_server.head_spu->dispatch_table.IsTexture(crStateGetTextureHWID(&cr_server.StateTracker, texture));
     crServerReturnValue( &retval, sizeof(retval) );
     return retval; /* WILL PROBABLY BE IGNORED */
@@ -313,5 +325,5 @@
     for (i = 0; i < n; i++)
     {
-        textures2[i] = crStateGetTextureHWID(textures[i]);
+        textures2[i] = crStateGetTextureHWID(&cr_server.StateTracker, textures[i]);
     }
     retval = cr_server.head_spu->dispatch_table.AreTexturesResident(n, textures2, res);
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_viewport.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_viewport.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_viewport.cpp	(revision 78375)
@@ -276,5 +276,5 @@
 void SERVER_DISPATCH_APIENTRY crServerDispatchViewport( GLint x, GLint y, GLsizei width, GLsizei height )
 {
-	CRContext *ctx = crStateGetCurrent();
+	CRContext *ctx = crStateGetCurrent(&cr_server.StateTracker);
 
 	if (ctx->viewport.viewportX != x ||
@@ -284,5 +284,5 @@
 		 /* Note -- If there are tiles, this will be overridden in the 
 			* process of decoding the BoundsInfo packet, so no worries. */
-		 crStateViewport( x, y, width, height );
+		 crStateViewport(&cr_server.StateTracker, x, y, width, height );
 	}
 
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_window.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_window.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_window.cpp	(revision 78375)
@@ -351,6 +351,6 @@
             && !mural->fRedirected)
     {
-        crStateGetCurrent()->buffer.width = mural->width;
-        crStateGetCurrent()->buffer.height = mural->height;
+        crStateGetCurrent(&cr_server.StateTracker)->buffer.width = mural->width;
+        crStateGetCurrent(&cr_server.StateTracker)->buffer.height = mural->height;
     }
 
Index: trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_winpos.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_winpos.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_winpos.cpp	(revision 78375)
@@ -9,5 +9,5 @@
 static void crServerWindowPos( GLfloat x, GLfloat y, GLfloat z )
 {
-	crStateWindowPos3fARB(x, y, z);
+	crStateWindowPos3fARB(&cr_server.StateTracker, x, y, z);
 	cr_server.head_spu->dispatch_table.WindowPos3fARB(x, y, z);
 }
Index: trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_arrays.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_arrays.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_arrays.cpp	(revision 78375)
@@ -8,5 +8,4 @@
 #include "unpack_extend.h"
 #include "unpacker.h"
-#include "cr_glstate.h"
 
 
@@ -292,5 +291,5 @@
     int numenabled = READ_DATA(pState, sizeof(int) + 12, int);
 
-    CRContext *g = crStateGetCurrent();
+    CRContext *g = crStateGetCurrent(pState->pStateTracker);
     CRClientState *c = &g->client;
     CRClientPointer *cp;
@@ -364,5 +363,5 @@
 {
     int i;
-    CRContext *g = crStateGetCurrent();
+    CRContext *g = crStateGetCurrent(pState->pStateTracker);
     CRClientState *c = &g->client;
     CRClientPointer *cp;
Index: trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_drawpixels.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_drawpixels.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_drawpixels.cpp	(revision 78375)
@@ -22,5 +22,5 @@
     GLvoid *pixels;
 
-    if (noimagedata && !crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (noimagedata && !crStateIsBufferBound(pState->pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
         return;
 
@@ -63,5 +63,5 @@
     GLubyte *bitmap;
 
-    if (noimagedata && !crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (noimagedata && !crStateIsBufferBound(pState->pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
         return;
 
Index: trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_misc.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_misc.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_misc.cpp	(revision 78375)
@@ -54,5 +54,5 @@
     const GLuint *ids = DATA_POINTER(pState, 12, GLuint);
 
-    if (n <= 0 || n >= INT32_MAX / sizeof(GLint) / 4 || !DATA_POINTER_CHECK(pState, 12 + n * sizeof(GLuint)))
+    if (n < 0 || n >= INT32_MAX / sizeof(GLint) / 4 || !DATA_POINTER_CHECK(pState, 12 + n * sizeof(GLuint)))
     {
         crError("crUnpackExtendDeleteQueriesARB: parameter 'n' is out of range");
Index: trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_pixelmap.cpp
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_pixelmap.cpp	(revision 78374)
+++ trunk/src/VBox/HostServices/SharedOpenGL/unpacker/unpack_pixelmap.cpp	(revision 78375)
@@ -17,5 +17,5 @@
     GLfloat *values;
 
-    if (nodata && !crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (nodata && !crStateIsBufferBound(pState->pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
         return;
 
@@ -47,5 +47,5 @@
     GLuint *values;
 
-    if (nodata && !crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (nodata && !crStateIsBufferBound(pState->pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
         return;
 
@@ -77,5 +77,5 @@
     GLushort *values;
 
-    if (nodata && !crStateIsBufferBound(GL_PIXEL_UNPACK_BUFFER_ARB))
+    if (nodata && !crStateIsBufferBound(pState->pStateTracker, GL_PIXEL_UNPACK_BUFFER_ARB))
         return;
 
