commit cc8151c40eaaf5e5cd6b44e4cbf14613efe719d2
Author: leszek <leszek@koltunski.pl>
Date:   Fri May 12 23:55:23 2017 +0100

    1. Correct some bugs in DistortedRenderState
    2. Thus make the Stencil App work
    3. New icon for it

diff --git a/src/main/java/org/distorted/library/DistortedEffects.java b/src/main/java/org/distorted/library/DistortedEffects.java
index 7b7bc50..e2aef60 100644
--- a/src/main/java/org/distorted/library/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/DistortedEffects.java
@@ -303,9 +303,9 @@ public class DistortedEffects
    
   private void releasePriv()
     {
-    if( !matrixCloned     ) mM.abortAll(false);
-    if( !vertexCloned     ) mV.abortAll(false);
-    if( !fragmentCloned   ) mF.abortAll(false);
+    if( !matrixCloned   ) mM.abortAll(false);
+    if( !vertexCloned   ) mV.abortAll(false);
+    if( !fragmentCloned ) mF.abortAll(false);
 
     mM = null;
     mV = null;
diff --git a/src/main/java/org/distorted/library/DistortedRenderState.java b/src/main/java/org/distorted/library/DistortedRenderState.java
index a5f9829..911e486 100644
--- a/src/main/java/org/distorted/library/DistortedRenderState.java
+++ b/src/main/java/org/distorted/library/DistortedRenderState.java
@@ -149,10 +149,13 @@ class DistortedRenderState
 
   void apply()
     {
-    // 1. Write to color buffer?
+    //android.util.Log.e("State", "APPLYING STATE");
 
+    /////////////////////////////////////////////////////
+    // 1. Write to color buffer?
     if( mColorMaskR!=sColorMaskR || mColorMaskG!=sColorMaskG || mColorMaskB!=sColorMaskB || mColorMaskA!=sColorMaskA)
       {
+      //android.util.Log.d("State", "setting color mask");
       sColorMaskR = mColorMaskR;
       sColorMaskG = mColorMaskG;
       sColorMaskB = mColorMaskB;
@@ -160,94 +163,127 @@ class DistortedRenderState
       GLES30.glColorMask(sColorMaskR==1,sColorMaskG==1,sColorMaskB==1,sColorMaskA==1);
       }
 
+    /////////////////////////////////////////////////////
     // 2. Enable Depth test?
-
     if( mDepthTest!=sDepthTest )
       {
       sDepthTest = mDepthTest;
 
-      if( sDepthTest==0 ) GLES30.glDisable(GLES30.GL_DEPTH_TEST);
+      if (sDepthTest == 0)
+        {
+        //android.util.Log.d("State", "disabling depth test");
+        GLES30.glDisable(GLES30.GL_DEPTH_TEST);
+        }
       else
         {
-        GLES30.glEnable (GLES30.GL_DEPTH_TEST);
-
-        if( mDepthFunc!=sDepthFunc )
-          {
-          sDepthFunc = mDepthFunc;
-          GLES30.glDepthFunc(sDepthFunc);
-          }
+        //android.util.Log.d("State", "enable depth test");
+        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
         }
       }
 
-    // 3. Write to Depth buffer?
+    /////////////////////////////////////////////////////
+    // 3. Change Depth Function?
+    if( mDepthFunc!=sDepthFunc )
+      {
+      //android.util.Log.d("State", "setting depth func");
+      sDepthFunc = mDepthFunc;
+      GLES30.glDepthFunc(sDepthFunc);
+      }
 
+    /////////////////////////////////////////////////////
+    // 4. Write to Depth buffer?
     if( mDepthMask!=sDepthMask )
       {
+      //android.util.Log.d("State", "setting depth mask");
       sDepthMask = mDepthMask;
       GLES30.glDepthMask(sDepthMask==1);
       }
 
-    // 4. Enable Blending?
-
+    /////////////////////////////////////////////////////
+    // 5. Enable Blending?
     if( mBlend!=sBlend )
       {
       sBlend = mBlend;
 
-      if( sBlend==0 ) GLES30.glDisable(GLES30.GL_BLEND);
+      if (sBlend == 0)
+        {
+        //android.util.Log.d("State", "disabling blending");
+        GLES30.glDisable(GLES30.GL_BLEND);
+        }
       else
         {
+        //android.util.Log.d("State", "enabling blending");
         GLES30.glEnable(GLES30.GL_BLEND);
-
-        if( mBlendSrc!=sBlendSrc || mBlendDst!=sBlendDst )
-          {
-          sBlendSrc = mBlendSrc;
-          sBlendDst = mBlendDst;
-          GLES30.glBlendFunc(sBlendSrc,sBlendDst);
-          }
         }
       }
 
-    // 5. Enable Stencil Test?
+    /////////////////////////////////////////////////////
+    // 6. Change Blend function?
+    if( mBlendSrc!=sBlendSrc || mBlendDst!=sBlendDst )
+      {
+      //android.util.Log.d("State", "setting blend function");
+      sBlendSrc = mBlendSrc;
+      sBlendDst = mBlendDst;
+      GLES30.glBlendFunc(sBlendSrc,sBlendDst);
+      }
 
+    /////////////////////////////////////////////////////
+    // 7. Enable/Disable Stencil Test?
     if( mStencilTest!=sStencilTest )
       {
       sStencilTest = mStencilTest;
 
-      if( sStencilTest==0 ) GLES30.glDisable(GLES30.GL_STENCIL_TEST);
+      if (sStencilTest == 0)
+        {
+        //android.util.Log.d("State", "disabling stencil test");
+        GLES30.glDisable(GLES30.GL_STENCIL_TEST);
+        }
       else
         {
+        //android.util.Log.d("State", "enabling stencil test");
         GLES30.glEnable(GLES30.GL_STENCIL_TEST);
-
-        if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
-          {
-          sStencilFuncFunc = mStencilFuncFunc;
-          sStencilFuncRef  = mStencilFuncRef ;
-          sStencilFuncMask = mStencilFuncMask;
-          GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
-          }
-
-        if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
-          {
-          sStencilOpSfail = mStencilOpSfail;
-          sStencilOpDpfail= mStencilOpDpfail;
-          sStencilOpDppass= mStencilOpDppass;
-          GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
-          }
         }
       }
 
-    // 6. Write to Stencil buffer?
+//  android.util.Log.d("State", "mFunc="+mStencilFuncFunc+" sFunc="+sStencilFuncFunc);
+//  android.util.Log.d("State", "GL_ALWAYS="+GLES30.GL_ALWAYS+" GL_EQUAL="+GLES30.GL_EQUAL);
+
+    /////////////////////////////////////////////////////
+    // 8. Adjust Stencil function?
+    if( mStencilFuncFunc!=sStencilFuncFunc || mStencilFuncRef!=sStencilFuncRef || mStencilFuncMask!=sStencilFuncMask )
+      {
+      //android.util.Log.d("State", "setting stencil function");
+      sStencilFuncFunc = mStencilFuncFunc;
+      sStencilFuncRef  = mStencilFuncRef ;
+      sStencilFuncMask = mStencilFuncMask;
+      GLES30.glStencilFunc(sStencilFuncFunc,sStencilFuncRef,sStencilFuncMask);
+      }
+
+    /////////////////////////////////////////////////////
+    // 9. Adjust Stencil operation?
+    if( mStencilOpSfail!=sStencilOpSfail || mStencilOpDpfail!=sStencilOpDpfail || mStencilOpDppass!=sStencilOpDppass )
+      {
+      //android.util.Log.d("State", "setting stencil op");
+      sStencilOpSfail = mStencilOpSfail;
+      sStencilOpDpfail= mStencilOpDpfail;
+      sStencilOpDppass= mStencilOpDppass;
+      GLES30.glStencilOp(sStencilOpSfail,sStencilOpDpfail,sStencilOpDppass);
+      }
 
+    /////////////////////////////////////////////////////
+    // 10. Write to Stencil buffer?
     if( mStencilMask!=sStencilMask )
       {
+      //android.util.Log.d("State", "setting stencil mask");
       sStencilMask = mStencilMask;
       GLES30.glStencilMask(sStencilMask);
       }
 
-    // 7. Clear buffers?
-
+    /////////////////////////////////////////////////////
+    // 11. Clear buffers?
     if( mClear!=0 )
       {
+      //android.util.Log.d("State", "clearing buffer");
       GLES30.glClear(mClear);
       }
     }
