commit 5f7e4f2cc306cbd287f42480e9313297983b8fdd
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Tue Jul 24 16:00:50 2018 +0100

    Hide the FBO_QUEUE thing inside the library. The queue is running internally now, without public setAsOutputFBO() thing (simplifies writing postprocess effects)

diff --git a/src/main/java/org/distorted/library/effect/PostprocessEffect.java b/src/main/java/org/distorted/library/effect/PostprocessEffect.java
index 933c5c6..cf55fa2 100644
--- a/src/main/java/org/distorted/library/effect/PostprocessEffect.java
+++ b/src/main/java/org/distorted/library/effect/PostprocessEffect.java
@@ -139,7 +139,7 @@ public abstract class PostprocessEffect extends Effect implements DistortedMaste
  *
  * @y.exclude
  */
-  public abstract int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers, int fbo);
+  public abstract int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers);
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
diff --git a/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java b/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
index fbdf36f..6f25bea 100644
--- a/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
+++ b/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
@@ -141,7 +141,7 @@ public class PostprocessEffectBlur extends PostprocessEffect
  *
  * @y.exclude
  */
-  public int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers, int fbo)
+  public int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers)
     {
     if( mProgram1 ==null)
       {
@@ -160,7 +160,7 @@ public class PostprocessEffectBlur extends PostprocessEffect
 
     DistortedFramebuffer buffer = (DistortedFramebuffer)buffers[mQualityLevel];
 
-    buffer.setAsOutputFBO(fbo);
+    buffer.setAsOutput();
 
     float w= buffer.getWidth();
     float h= buffer.getHeight();
@@ -183,8 +183,8 @@ public class PostprocessEffectBlur extends PostprocessEffect
     for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
 
     mProgram1.useProgram();
-    buffer.bindForOutput(2*fbo+1);
-    buffer.setAsInput(2*fbo);
+    buffer.bindForOutput(1);
+    buffer.setAsInput(0);
 
     GLES31.glColorMask(true,true,true,true);
     GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
@@ -204,8 +204,8 @@ public class PostprocessEffectBlur extends PostprocessEffect
     for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
 
     mProgram2.useProgram();
-    buffer.bindForOutput(2*fbo);
-    buffer.setAsInput(2*fbo+1);
+    buffer.bindForOutput(0);
+    buffer.setAsInput(1);
 
     GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
 
diff --git a/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java b/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
index 8a42473..13cdbe9 100644
--- a/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
+++ b/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
@@ -145,7 +145,7 @@ public class PostprocessEffectGlow extends PostprocessEffect
  *
  * @y.exclude
  */
-  public int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers, int fbo)
+  public int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers)
     {
     if( mProgram1 ==null)
       {
@@ -181,15 +181,15 @@ public class PostprocessEffectGlow extends PostprocessEffect
     int offset = radius + radius*radius/4;
     radius = (radius+1)/2;
 
-    outBuffer.setAsOutputFBO(fbo);
     GLES31.glViewport(0, 0, (int)w, (int)h);
 
     // horizontal glow
     for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
 
     mProgram1.useProgram();
-    outBuffer.bindForOutput(2*fbo+1);
-    inBuffer.setAsInput(2*fbo);
+    outBuffer.setAsOutput();
+    outBuffer.bindForOutput(1);
+    inBuffer.setAsInput(0);
 
     GLES31.glColorMask(true,true,true,true);
     GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
@@ -210,8 +210,13 @@ public class PostprocessEffectGlow extends PostprocessEffect
     for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
 
     mProgram2.useProgram();
-    outBuffer.bindForOutput(2*fbo);
-    outBuffer.setAsInput(2*fbo+1);
+    outBuffer.bindForOutput(0);
+    outBuffer.setAsInput(1);
+
+    if( mQualityLevel==0 )
+      {
+      GLES31.glEnable(GLES31.GL_BLEND);
+      }
 
     GLES31.glUniform1f ( mProgram2.mUniform[0] , n );
     GLES31.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
@@ -224,14 +229,19 @@ public class PostprocessEffectGlow extends PostprocessEffect
     GLES31.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadTexture);
     GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
 
+    if( mQualityLevel==0 )
+      {
+      GLES31.glDisable(GLES31.GL_BLEND);
+      }
+
     DistortedRenderState.unuseStencilMark();
 
     // blit back to inBuffer if we have to
     if( mQualityLevel>0 )
       {
-      inBuffer.setAsOutputFBO(fbo);
-      inBuffer.bindForOutput(2*fbo);
-      outBuffer.setAsInput(2*fbo);
+      inBuffer.setAsOutput();
+      inBuffer.bindForOutput(0);
+      outBuffer.setAsInput(0);
       GLES31.glEnable(GLES31.GL_BLEND);
       DistortedEffects.blitPriv(inBuffer);
       GLES31.glDisable(GLES31.GL_BLEND);
diff --git a/src/main/java/org/distorted/library/main/DistortedFramebuffer.java b/src/main/java/org/distorted/library/main/DistortedFramebuffer.java
index 3f5eee9..8665c30 100644
--- a/src/main/java/org/distorted/library/main/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/main/DistortedFramebuffer.java
@@ -254,14 +254,15 @@ public class DistortedFramebuffer extends DistortedOutputSurface
 /**
  * Bind the underlying rectangle of pixels as a OpenGL Texture.
  *
+ * @param texture The Texture number to bind (and thus read from).
  * @return <code>true</code> if successful.
  */
   public boolean setAsInput(int texture)
     {
-    if( texture>=0 && texture<mNumFBOs*mNumColors && mColorH[texture]>0 )
+    if( texture>=0 && texture<mNumColors && mColorH[texture]>0 )
       {
       GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
-      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[texture]);
+      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[2*mCurrFBO+texture]);
       return true;
       }
 
@@ -269,12 +270,16 @@ public class DistortedFramebuffer extends DistortedOutputSurface
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-
+  /**
+   * Attach the texture'th Texture to COLOR0 attachment.
+   *
+   * @param texture The Texture number to attach (and subsequently use to render to)
+   */
   public void bindForOutput(int texture)
     {
-    if( texture>=0 && texture<mNumFBOs*mNumColors && mColorH[texture]>0 )
+    if( texture>=0 && texture<mNumColors && mColorH[texture]>0 )
       {
-      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[texture], 0);
+      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[2*mCurrFBO+texture], 0);
       }
     }
 
diff --git a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
index 14a584b..fba996a 100644
--- a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
@@ -94,6 +94,8 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
                     // mWidth,mHeight are the sizes of the Viewport, those -
                     // sizes of the backing up texture.
 
+  int mCurrFBO;     // internal current FBO (see Distorted.FBO_QUEUE_SIZE)
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   DistortedOutputSurface(int width, int height, int createColor, int numfbos, int numcolors, int depthStencil, int fbo, int type)
@@ -101,6 +103,7 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
     super(width,height,createColor,numfbos,numcolors,type);
 
     mRenderWayOIT = false;
+    mCurrFBO      = 0;
 
     mDepthStencilH = new int[numfbos];
     mFBOH          = new int[numfbos];
@@ -275,7 +278,7 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
   private int blitWithDepth(long currTime, DistortedOutputSurface buffer,int fbo)
     {
     GLES31.glViewport(0, 0, mWidth, mHeight);
-    setAsOutputFBO(currTime,fbo);
+    setAsOutput(currTime);
     GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
     GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo]);
     GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
@@ -298,7 +301,7 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
     GLES31.glClearDepthf(1.0f);
     GLES31.glClearStencil(0);
 
-    buffer.setAsOutputFBO(fbo);
+    buffer.setAsOutput();
     GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo+1], 0);
     GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
     GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo  ], 0);
@@ -321,7 +324,7 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
   private int oitBuild(long time, DistortedOutputSurface buffer, int fbo)
     {
     GLES31.glViewport(0, 0, mWidth, mHeight);
-    setAsOutputFBO(time,fbo);
+    setAsOutput(time);
     GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
     GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo]);
     GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
@@ -363,7 +366,7 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
       GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
       }
 
-    setAsOutputFBO(currTime,fbo);
+    setAsOutput(currTime);
     DistortedRenderState.switchColorDepthOnStencilOff();
     DistortedEffects.oitRender(this, corrW, corrH);
     DistortedRenderState.restoreColorDepthStencil();
@@ -383,6 +386,13 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
     DistortedRenderState.colorDepthStencilRestore();
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void setCurrFBO(int fbo)
+    {
+    mCurrFBO = fbo;
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // 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
@@ -395,6 +405,16 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
     EffectQueuePostprocess lastQueue=null, currQueue;
     long lastBucket=0, currBucket;
 
+    setCurrFBO(fbo);
+
+    if( mBuffer!=null )
+      {
+      for (int i=0; i<EffectQuality.LENGTH; i++)
+        {
+        mBuffer[i].setCurrFBO(fbo);
+        }
+      }
+
     if( oit && numChildren>0 )
       {
       oitClear(this);
@@ -408,7 +428,7 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
 
       if( currBucket==0 )
         {
-        setAsOutputFBO(time,fbo);
+        setAsOutput(time);
 
         if( oit )
           {
@@ -422,7 +442,15 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
         }
       else
         {
-        if( mBuffer==null ) createPostprocessingBuffers(mWidth,mHeight,mNear);
+        if( mBuffer==null )
+          {
+          createPostprocessingBuffers(mWidth,mHeight,mNear);
+
+          for (int j=0; j<EffectQuality.LENGTH; j++)
+            {
+            mBuffer[j].setCurrFBO(fbo);
+            }
+          }
 
         if( lastBucket!=currBucket )
           {
@@ -435,11 +463,11 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
             for(int j=bucketChange; j<i; j++)
               {
               child2 = children.get(j);
-              mBuffer[internalQuality].setAsOutputFBO(fbo);
+              mBuffer[internalQuality].setAsOutput();
               numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],lastQueue);
               }
 
-            numRenders += lastQueue.postprocess(mBuffer,fbo);
+            numRenders += lastQueue.postprocess(mBuffer);
 
             if( oit )
               {
@@ -458,7 +486,7 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
           bucketChange    = i;
           }
 
-        mBuffer[quality].setAsOutputFBO(time,fbo);
+        mBuffer[quality].setAsOutput(time);
         child1.drawNoBlend(time,mBuffer[quality]);
 
         if( i==numChildren-1 )
@@ -466,11 +494,11 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
           for(int j=bucketChange; j<numChildren; j++)
             {
             child2 = children.get(j);
-            mBuffer[internalQuality].setAsOutputFBO(fbo);
+            mBuffer[internalQuality].setAsOutput();
             numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],currQueue);
             }
 
-          numRenders += currQueue.postprocess(mBuffer,fbo);
+          numRenders += currQueue.postprocess(mBuffer);
 
           if( oit )
             {
@@ -545,44 +573,6 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
     DistortedRenderState.colorDepthStencilRestore();
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private void setAsOutputFBO(long time, int fbo)
-    {
-    if( fbo>=0 && fbo<mNumFBOs )
-      {
-      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[fbo]);
-
-      if (mTime[fbo] != time)
-        {
-        mTime[fbo] = time;
-        clear();
-        }
-      }
-    else
-      {
-      android.util.Log.e("surface", "error in setAsOutput1, fbo="+fbo);
-      }
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Not part of the Public API.
- *
- * @y.exclude
- */
-  public void setAsOutputFBO(int fbo)
-    {
-    if( fbo>=0 && fbo<mNumFBOs )
-      {
-      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[fbo]);
-      }
-    else
-      {
-      android.util.Log.e("surface", "error in setAsOutput2, fbo="+fbo);
-      }
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -668,11 +658,11 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
  */
   public void setAsOutput(long time)
     {
-    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
+    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[mCurrFBO]);
 
-    if( mTime[0]!=time )
+    if( mTime[mCurrFBO]!=time )
       {
-      mTime[0] = time;
+      mTime[mCurrFBO] = time;
       clear();
       }
     }
@@ -685,7 +675,7 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
  */
   public void setAsOutput()
     {
-    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
+    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[mCurrFBO]);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/main/EffectQueuePostprocess.java b/src/main/java/org/distorted/library/main/EffectQueuePostprocess.java
index 89d6934..d3bb295 100644
--- a/src/main/java/org/distorted/library/main/EffectQueuePostprocess.java
+++ b/src/main/java/org/distorted/library/main/EffectQueuePostprocess.java
@@ -100,7 +100,7 @@ class EffectQueuePostprocess extends EffectQueue
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  int postprocess(DistortedOutputSurface[] buffers, int fbo)
+  int postprocess(DistortedOutputSurface[] buffers)
     {
     int numRenders = 0;
 
@@ -108,7 +108,7 @@ class EffectQueuePostprocess extends EffectQueue
 
     for(int i=0; i<mNumEffects; i++)
       {
-      numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffers, fbo);
+      numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffers);
       }
 
     GLES31.glEnable(GLES31.GL_BLEND);
diff --git a/src/main/res/raw/oit_render_fragment_shader.glsl b/src/main/res/raw/oit_render_fragment_shader.glsl
index 1df11f6..b9d5997 100644
--- a/src/main/res/raw/oit_render_fragment_shader.glsl
+++ b/src/main/res/raw/oit_render_fragment_shader.glsl
@@ -45,7 +45,6 @@ vec4 convert(uint rgba)
 //////////////////////////////////////////////////////////////////////////////////////////////
 // A over B (https://en.wikipedia.org/wiki/Alpha_compositing)
 
-
 vec4 blend(vec4 A,vec4 B)
   {
   float b = B.a * (1.0-A.a);
