Index: trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_stencil.c
===================================================================
--- trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_stencil.c	(revision 44331)
+++ trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_stencil.c	(revision 44332)
@@ -342,4 +342,19 @@
 }
 
+#ifdef CRSTATE_DEBUG_STENCIL_ERR
+#define CRSTATE_CLEARERR() do { \
+            while (diff_api.GetError() != GL_NO_ERROR) {} \
+        } while (0)
+
+#define CRSTATE_CHECKGLERR(_op) do {\
+            GLenum _glErr; \
+            CRSTATE_CLEARERR(); \
+            _op; \
+            while ((_glErr = diff_api.GetError()) != GL_NO_ERROR) { Assert(0);} \
+        }while (0)
+#else
+#define CRSTATE_CHECKGLERR(_op) do { _op; } while (0)
+#endif
+
 #define CR_STATE_STENCIL_FUNC_MATCH(_s1, _i1, _s2, _i2) (\
     (_s1)->buffers[(_i1)].func == (_s2)->buffers[(_i2)].func && \
@@ -627,5 +642,5 @@
     if (CHECKDIRTY(b->bufferRefs[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].func, bitID))
     {
-        if (CR_STATE_STENCIL_FUNC_MATCH(from, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK))
+        if (!CR_STATE_STENCIL_FUNC_MATCH(from, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK))
         {
             if (activeFace == GL_FRONT)
@@ -635,8 +650,8 @@
             }
 
-            diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].func,
-                to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].ref,
-                to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].mask);
-            CR_STATE_STENCIL_FUNC_COPY(from, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK);
+            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);
+            CR_STATE_STENCIL_FUNC_COPY(from, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK);
         }
         CLEARDIRTY(b->bufferRefs[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].func, nbitID);
@@ -858,5 +873,5 @@
     if (CHECKDIRTY(b->bufferRefs[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].op, bitID))
     {
-        if (CR_STATE_STENCIL_OP_MATCH(from, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK))
+        if (!CR_STATE_STENCIL_OP_MATCH(from, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK))
         {
             if (activeFace == GL_FRONT)
@@ -866,8 +881,8 @@
             }
 
-            diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].fail,
-                                    to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].passDepthFail,
-                                    to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].passDepthPass);
-            CR_STATE_STENCIL_OP_COPY(from, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK);
+            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);
+            CR_STATE_STENCIL_OP_COPY(from, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK);
         }
         CLEARDIRTY(b->bufferRefs[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].op, nbitID);
@@ -928,5 +943,5 @@
         if (from->stencilTest != to->stencilTest)
         {
-            able[to->stencilTest](GL_STENCIL_TEST);
+            CRSTATE_CHECKGLERR(able[to->stencilTest](GL_STENCIL_TEST));
             FILLDIRTY(b->enable);
             FILLDIRTY(b->dirty);
@@ -941,5 +956,5 @@
         if (from->stencilTwoSideEXT != to->stencilTwoSideEXT)
         {
-            able[to->stencilTwoSideEXT](GL_STENCIL_TEST_TWO_SIDE_EXT);
+            CRSTATE_CHECKGLERR(able[to->stencilTwoSideEXT](GL_STENCIL_TEST_TWO_SIDE_EXT));
             FILLDIRTY(b->enableTwoSideEXT);
             FILLDIRTY(b->dirty);
@@ -951,5 +966,5 @@
         if (from->clearValue != to->clearValue)
         {
-            diff_api.ClearStencil (to->clearValue);
+            CRSTATE_CHECKGLERR(diff_api.ClearStencil (to->clearValue));
             FILLDIRTY(b->clearValue);
             FILLDIRTY(b->dirty);
@@ -992,7 +1007,7 @@
                 }
 
-                diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                CRSTATE_CHECKGLERR(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);
+                    to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
 
                 frontIsSet = GL_TRUE;
@@ -1007,7 +1022,7 @@
                 }
 
-                diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                CRSTATE_CHECKGLERR(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);
+                    to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
                 frontIsSet = GL_TRUE;
             }
@@ -1020,7 +1035,7 @@
                 }
 
-                diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
+                CRSTATE_CHECKGLERR(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);
+                    to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].mask));
                 backIsSet = GL_TRUE;
             }
@@ -1046,7 +1061,7 @@
                     }
 
-                    diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                    CRSTATE_CHECKGLERR(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);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
 
                     frontIsSet = GL_TRUE;
@@ -1064,7 +1079,7 @@
                     }
 
-                    diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                    CRSTATE_CHECKGLERR(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);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
                     frontIsSet = GL_TRUE;
                 }
@@ -1080,7 +1095,7 @@
                     }
 
-                    diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
+                    CRSTATE_CHECKGLERR(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);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].mask));
                     backIsSet = GL_TRUE;
                 }
@@ -1107,7 +1122,7 @@
                     }
 
-                    diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                    CRSTATE_CHECKGLERR(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);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
 
                     frontIsSet = GL_TRUE;
@@ -1125,7 +1140,7 @@
                     }
 
-                    diff_api.StencilFuncSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].func,
+                    CRSTATE_CHECKGLERR(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);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].mask));
                     frontIsSet = GL_TRUE;
                 }
@@ -1141,7 +1156,7 @@
                     }
 
-                    diff_api.StencilFuncSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].func,
+                    CRSTATE_CHECKGLERR(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);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].mask));
                     backIsSet = GL_TRUE;
                 }
@@ -1155,15 +1170,15 @@
     if (CHECKDIRTY(b->bufferRefs[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].func, bitID))
     {
-        if (CR_STATE_STENCIL_FUNC_MATCH(from, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK))
+        if (!CR_STATE_STENCIL_FUNC_MATCH(from, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK))
         {
             if (activeFace == GL_FRONT)
             {
-                diff_api.ActiveStencilFaceEXT(GL_BACK);
+                CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_BACK));
                 activeFace = GL_BACK;
             }
 
-            diff_api.StencilFunc (to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].func,
-                to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].ref,
-                to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].mask);
+            CRSTATE_CHECKGLERR(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));
 
             FILLDIRTY(b->bufferRefs[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].func);
@@ -1208,11 +1223,11 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
                     activeFace = GL_FRONT;
                 }
 
-                diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                CRSTATE_CHECKGLERR(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);
+                    to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
 
                 frontIsSet = GL_TRUE;
@@ -1223,11 +1238,11 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
                     activeFace = GL_FRONT;
                 }
 
-                diff_api.StencilOpSeparate (GL_FRONT, to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                CRSTATE_CHECKGLERR(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);
+                    to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
                 frontIsSet = GL_TRUE;
             }
@@ -1236,11 +1251,11 @@
                 if (activeFace == GL_BACK)
                 {
-                    diff_api.ActiveStencilFaceEXT(GL_FRONT);
+                    CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
                     activeFace = GL_FRONT;
                 }
 
-                diff_api.StencilOpSeparate (GL_BACK, to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].fail,
+                CRSTATE_CHECKGLERR(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);
+                    to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass));
                 backIsSet = GL_TRUE;
             }
@@ -1262,11 +1277,11 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                        CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(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);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
 
                     frontIsSet = GL_TRUE;
@@ -1280,11 +1295,11 @@
                     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,
+                        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,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
-                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
                     frontIsSet = GL_TRUE;
                 }
@@ -1296,11 +1311,11 @@
                     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,
+                        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,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthFail,
-                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass));
                     backIsSet = GL_TRUE;
                 }
@@ -1324,11 +1339,11 @@
                     if (activeFace == GL_BACK)
                     {
-                        diff_api.ActiveStencilFaceEXT(GL_FRONT);
-                        activeFace = GL_FRONT;
-                    }
-
-                    diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].fail,
+                        CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_FRONT));
+                        activeFace = GL_FRONT;
+                    }
+
+                    CRSTATE_CHECKGLERR(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);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
 
                     frontIsSet = GL_TRUE;
@@ -1342,11 +1357,11 @@
                     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,
+                        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,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthFail,
-                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_FRONT].passDepthPass));
                     frontIsSet = GL_TRUE;
                 }
@@ -1358,11 +1373,11 @@
                     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,
+                        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,
                         to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthFail,
-                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass);
+                        to->buffers[CRSTATE_STENCIL_BUFFER_ID_BACK].passDepthPass));
                     backIsSet = GL_TRUE;
                 }
@@ -1377,15 +1392,15 @@
     if (CHECKDIRTY(b->bufferRefs[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].op, bitID))
     {
-        if (CR_STATE_STENCIL_OP_MATCH(from, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK))
+        if (!CR_STATE_STENCIL_OP_MATCH(from, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK, to, CRSTATE_STENCIL_BUFFER_ID_TWO_SIDE_BACK))
         {
             if (activeFace == GL_FRONT)
             {
-                diff_api.ActiveStencilFaceEXT(GL_BACK);
+                CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(GL_BACK));
                 activeFace = GL_BACK;
             }
 
-            diff_api.StencilOp (to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].fail,
-                                    to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].passDepthFail,
-                                    to->buffers[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].passDepthPass);
+            CRSTATE_CHECKGLERR(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));
 
             FILLDIRTY(b->bufferRefs[CRSTATE_STENCIL_BUFFER_REF_ID_TWO_SIDE_BACK].op);
@@ -1401,5 +1416,5 @@
     if (activeFace != to->activeStencilFace)
     {
-        diff_api.ActiveStencilFaceEXT(activeFace);
+        CRSTATE_CHECKGLERR(diff_api.ActiveStencilFaceEXT(activeFace));
     }
 
@@ -1420,5 +1435,5 @@
         if (from->writeMask != to->writeMask)
         {
-            diff_api.StencilMask (to->writeMask);
+            CRSTATE_CHECKGLERR(diff_api.StencilMask (to->writeMask));
             FILLDIRTY(b->writeMask);
             FILLDIRTY(b->dirty);
