commit 9455da1777e4c3b16951070dbde5914de9cb784c
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Mon Apr 9 13:03:16 2018 +0100

    Fix for bug #28: looks of the borders of a blurred object depend on if the object is the first in the postprocessing bucket.

diff --git a/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java b/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
index 5aea125..2038c63 100644
--- a/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
+++ b/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
@@ -178,6 +178,10 @@ public class PostprocessEffectBlur extends PostprocessEffect
     buffer.bindForOutput(1);
     buffer.setAsInput(0);
 
+    GLES31.glColorMask(true,true,true,true);
+    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
+    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
+
     GLES31.glUniform1f ( mProgram1.mUniform[0] , n );
     GLES31.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
     GLES31.glUniform1i ( mProgram1.mUniform[2] , 0 );
@@ -195,8 +199,6 @@ public class PostprocessEffectBlur extends PostprocessEffect
     buffer.bindForOutput(0);
     buffer.setAsInput(1);
 
-    GLES31.glColorMask(true,true,true,true);
-    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
     GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
 
     GLES31.glUniform1f ( mProgram2.mUniform[0] , n );
diff --git a/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java b/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
index cac6aea..cd7398b 100644
--- a/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
+++ b/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
@@ -183,6 +183,10 @@ public class PostprocessEffectGlow extends PostprocessEffect
     outBuffer.bindForOutput(1);
     inBuffer.setAsInput(0);
 
+    GLES31.glColorMask(true,true,true,true);
+    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
+    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
+
     GLES31.glUniform1f ( mProgram1.mUniform[0] , n );
     GLES31.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
     GLES31.glUniform1i ( mProgram1.mUniform[2] , 0 );
diff --git a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
index 43ed4ec..f5b84d8 100644
--- a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
@@ -306,7 +306,7 @@ public static final int DEBUG_FPS = 1;
     GLES31.glStencilMask(0xff);
     GLES31.glDepthMask(true);
     GLES31.glColorMask(true,true,true,true);
-    GLES31.glClearColor(0.0f,0.0f,0.0f,0.0f);
+    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
     GLES31.glClearDepthf(1.0f);
     GLES31.glClearStencil(0);
 
@@ -410,7 +410,7 @@ public static final int DEBUG_FPS = 1;
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Render all children, one by one. If there are no postprocessing effects, just render to THIS.
 // Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
-// to a whole buffer and merge it.
+// to a whole buffer (lastQueue.postprocess) and merge it (this.blitWithDepth).
 
   int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children)
     {
@@ -442,6 +442,8 @@ public static final int DEBUG_FPS = 1;
 
             numRenders += lastQueue.postprocess(mBuffer);
             numRenders += blitWithDepth(time, mBuffer[quality]);
+
+            mBuffer[quality].setAsOutputAndClear(time);
             }
 
           internalQuality = currQueue.getInternalQuality();
@@ -502,6 +504,25 @@ public static final int DEBUG_FPS = 1;
     return (float)mHeight/mRealHeight;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Bind this Surface as a Framebuffer we can render to; always clear it's color bit.
+ *
+ * Useful for drawing to the postprocessing buffer, which must sometimes be cleared multiple times
+ * per frame.
+ */
+
+  private void setAsOutputAndClear(long time)
+    {
+    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
+
+    mTime = time;    // have to do this otherwise on the next setAsOutput() we would clear
+    DistortedRenderState.colorDepthStencilOn();
+    GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
+    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
+    DistortedRenderState.colorDepthStencilRestore();
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -603,12 +624,12 @@ public static final int DEBUG_FPS = 1;
       GLES31.glClearStencil(mClearStencil);
       GLES31.glClear(mClear);
       DistortedRenderState.colorDepthStencilRestore();
-
+/*
       if( mSSBO[0]>=0 )
         {
         // yes, this DOES keep on working when 'value' overflows into negative territory.
         int value = mIntBuffer.get(FRAME_DELAY*mSurfaceID+mLastIndex);
-/*
+
         if( value-mLastValue[mLastIndex]!=mLastDiff )
           {
           android.util.Log.d("surface", "id " + mSurfaceID +
@@ -617,7 +638,7 @@ public static final int DEBUG_FPS = 1;
                              + " avg: " + (mAvgSum/RUNNING_AVERAGE)
                             );
           }
-*/
+
         mLastDiff = value-mLastValue[mLastIndex];
         mLastValue[mLastIndex] = value;
 
@@ -627,6 +648,7 @@ public static final int DEBUG_FPS = 1;
         }
 
       if( ++mLastIndex >= FRAME_DELAY ) mLastIndex=0;
+*/
       }
     }
 
diff --git a/src/main/java/org/distorted/library/main/EffectQueue.java b/src/main/java/org/distorted/library/main/EffectQueue.java
index ef9fe4f..04940c8 100644
--- a/src/main/java/org/distorted/library/main/EffectQueue.java
+++ b/src/main/java/org/distorted/library/main/EffectQueue.java
@@ -102,6 +102,9 @@ abstract class EffectQueue implements DistortedMaster.Slave
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
+// Every effect queue has an ID, which should be the same iff two queues hold the same effects.
+// (this is a speedup: then both queues can be applied once, which seriously speeds up stuff -
+// especially important in case of postprocessing)
 
   void regenerateIDandSort()
     {
