commit 60c1c622e91fea21f16ed38c51d850836fc4abd5
Author: leszek <leszek@koltunski.pl>
Date:   Tue Apr 11 00:02:32 2017 +0100

    Move the Postprocessing buffers to OutputSurface.

diff --git a/src/main/java/org/distorted/library/DistortedEffects.java b/src/main/java/org/distorted/library/DistortedEffects.java
index e6c75b7..3123cc9 100644
--- a/src/main/java/org/distorted/library/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/DistortedEffects.java
@@ -297,9 +297,9 @@ public class DistortedEffects
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  DistortedFramebuffer getPostprocessingBuffer()
+  EffectQueuePostprocess getPostprocess()
     {
-    return mP.mNumEffects==0 ? null : mP.mMainBuffer;
+    return mP;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index 930bcd7..f4c63f8 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -31,13 +31,6 @@ import android.opengl.GLES30;
 public class DistortedFramebuffer extends DistortedOutputSurface implements DistortedInputSurface
   {
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  DistortedFramebuffer getBuffer()
-    {
-    return this;
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Must be called from a thread holding OpenGL Context
 // Watch out - this has the side-effect of binding a Texture and a Framebuffer!
@@ -135,31 +128,6 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
     if( mDepthCreated!=DONT_CREATE ) mDepthCreated = NOT_CREATED_YET;
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// if new size fits into the size of the underlying Texture, just change the projection without
-// reallocating the Texture. Otherwise, we need to reallocate.
-//
-// Must be called from a thread holding the OpenGL context.
-
-  void resizeFast(int width, int height)
-    {
-    if( mWidth!=width || mHeight!=height )
-      {
-      mWidth = width;
-      mHeight= height;
-      createProjection();
-
-      if( width> mSizeX || height> mSizeY)
-        {
-        mSizeX = width;
-        mSizeY = height;
-        delete();
-        }
-      }
-
-    create();
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
 // that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
diff --git a/src/main/java/org/distorted/library/DistortedNode.java b/src/main/java/org/distorted/library/DistortedNode.java
index dd7630e..c50b3e7 100644
--- a/src/main/java/org/distorted/library/DistortedNode.java
+++ b/src/main/java/org/distorted/library/DistortedNode.java
@@ -220,9 +220,9 @@ public class DistortedNode implements DistortedAttacheable
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  DistortedFramebuffer getPostprocessingBuffer()
+  EffectQueuePostprocess getPostprocess()
     {
-    return mEffects.getPostprocessingBuffer();
+    return mEffects.getPostprocess();
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/DistortedOutputSurface.java b/src/main/java/org/distorted/library/DistortedOutputSurface.java
index a682dac..040dac1 100644
--- a/src/main/java/org/distorted/library/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/DistortedOutputSurface.java
@@ -30,6 +30,8 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
   private ArrayList<DistortedNode> mChildren;
   private int mNumChildren;   // ==mChildren.length(), but we only create mChildren if the first one gets added
 
+  DistortedFramebuffer mBuffer1, mBuffer2;
+
   private long mTime;
   private float mFOV;
   int mWidth,mHeight,mDepth;
@@ -43,10 +45,6 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
   private float mClearR, mClearG, mClearB, mClearA;
   private float mClearDepth;
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  abstract DistortedFramebuffer getBuffer();
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, int type)
@@ -123,56 +121,50 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
     {
     int numRenders = 0;
     DistortedNode child;
-    DistortedFramebuffer fbo;
-    DistortedOutputSurface surface;
+    EffectQueuePostprocess lastP=null, currP=null;
+    long lastB=0, currB=0;
 
-    // 1. Render all children that have postprocessing effects to their own buffer FBOs
+    // 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.
+    //
+    // TODO: in order for this to be efficient, the children really need to be sorted
+    // to keep the same Buckets together.
 
     for(int i=0; i<num; i++)
       {
       child = children.get(i);
-      fbo   = child.getPostprocessingBuffer();
+      currP = child.getPostprocess();
+      currB = currP.getBucket();
 
-      if( fbo!=null )
+      if( i>0 && currB!=lastB )
         {
-        fbo.resizeFast(mWidth,mHeight);
-        numRenders += child.draw(time,fbo);
+        numRenders += lastP.postprocess(time,this);
         }
-      }
-
-    // 2. If we have rendered anything so far, and we are a Screen, then render to an
-    //    intermediate FBO instead.
-
-    surface = this;//numRenders>0 ? getBuffer() : this;
-
-    // 3. Render all children without postprocessing effects to buffer
-
-    for(int i=0; i<num; i++)
-      {
-      child = children.get(i);
-      fbo   = child.getPostprocessingBuffer();
 
-      if( fbo==null )
+      if( currB==0 )
         {
-        numRenders += child.draw(time,surface);
+        numRenders += child.draw(time,this);
         }
-      }
+      else
+        {
+        if( mBuffer1==null )
+          {
+          mBuffer1 = new DistortedFramebuffer(mDepthCreated!=DONT_CREATE, DistortedSurface.TYPE_TREE, mWidth, mHeight);
+          mBuffer2 = new DistortedFramebuffer(false                     , DistortedSurface.TYPE_TREE, mWidth, mHeight);
+          }
 
-    // 4. For all postprocessing fbo,
-    //       postprocess fbo
-    //       merge to buffer
+        numRenders += child.draw(time,mBuffer1);
+        }
 
-    for(int i=0; i<num; i++)
-      {
-      numRenders += children.get(i).postprocess(time,surface);
+      lastP = currP;
+      lastB = currB;
       }
 
-    // 5. finally blit to this if we have to
-
-    if( surface!=this && ((DistortedFramebuffer)surface).setAsInput() )
+    if( currB!=0 )
       {
-      numRenders++;
-      DistortedEffects.blitPriv(this);
+      numRenders += currP.postprocess(time,this);
       }
 
     return numRenders;
diff --git a/src/main/java/org/distorted/library/DistortedScreen.java b/src/main/java/org/distorted/library/DistortedScreen.java
index 7396c40..ac26741 100644
--- a/src/main/java/org/distorted/library/DistortedScreen.java
+++ b/src/main/java/org/distorted/library/DistortedScreen.java
@@ -27,7 +27,6 @@ package org.distorted.library;
  */
 public class DistortedScreen extends DistortedOutputSurface
   {
-  private DistortedFramebuffer mBuffer = null;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // here we don't manage underlying OpenGL assets ourselves
@@ -36,23 +35,6 @@ public class DistortedScreen extends DistortedOutputSurface
   void delete()   {}
   void recreate() {}
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  DistortedFramebuffer getBuffer()
-    {
-    if( mBuffer==null )
-      {
-      mBuffer = new DistortedFramebuffer(true,DistortedSurface.TYPE_SYST,mWidth,mHeight);
-      }
-
-    if( mBuffer.mWidth != mWidth || mBuffer.mHeight != mHeight )
-      {
-      mBuffer.resizeFast(mWidth,mHeight);
-      }
-
-    return mBuffer;
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/EffectQueuePostprocess.java b/src/main/java/org/distorted/library/EffectQueuePostprocess.java
index 28a713a..87aca72 100644
--- a/src/main/java/org/distorted/library/EffectQueuePostprocess.java
+++ b/src/main/java/org/distorted/library/EffectQueuePostprocess.java
@@ -65,9 +65,6 @@ class EffectQueuePostprocess extends EffectQueue
     mQuadTexture.put(textureNor).position(0);
     }
 
-  private static DistortedFramebuffer mPostBuffer = new DistortedFramebuffer(true,DistortedSurface.TYPE_SYST,1,1);
-                 DistortedFramebuffer mMainBuffer = new DistortedFramebuffer(true,DistortedSurface.TYPE_TREE,1,1);
-
   // BLUR effect
   private static final float GAUSSIAN[] =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
     {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
@@ -162,7 +159,16 @@ class EffectQueuePostprocess extends EffectQueue
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-  
+// TODO: change this into a EQP <--> long Map.
+// For now, just returning number of effects is sufficient.
+
+  long getBucket()
+    {
+    return mNumEffects;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
   synchronized boolean compute(long currTime)
     {
     if( currTime==mTime ) return false;
@@ -260,7 +266,7 @@ class EffectQueuePostprocess extends EffectQueue
     {
     if( mNumEffects>0 && compute(time) )
       {
-      mMainBuffer.setAsInput();
+      surface.mBuffer1.setAsInput();
       float w = surface.mWidth;
       float h = surface.mHeight;
 
@@ -275,8 +281,7 @@ class EffectQueuePostprocess extends EffectQueue
 
       // horizontal blur
       mBlur1Program.useProgram();
-      mPostBuffer.resizeFast( (int)w, (int)h);
-      mPostBuffer.setAsOutput(time);
+      surface.mBuffer2.setAsOutput(time);
 
       GLES30.glUniform1fv( mWeights1H, radius+1, weightsCache,offset);
       GLES30.glUniform1i( mRadius1H, radius);
@@ -290,8 +295,8 @@ class EffectQueuePostprocess extends EffectQueue
 
       // vertical blur
       mBlur2Program.useProgram();
-      mPostBuffer.setAsInput();
-      mMainBuffer.setAsDepth();
+      surface.mBuffer2.setAsInput();
+      surface.mBuffer1.setAsDepth();
       surface.setAsOutput(time);
 
       GLES30.glUniform1fv( mWeights2H, radius+1, weightsCache,offset);
