commit d5b709dfd4054f5ffa53461c4d3cbe60d41a48c3
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Thu May 2 19:26:19 2019 +0100

    Cut another interdependency between the Queues and the rest: it is no longer necessary to add DNodes to PostprocessQueue to rearrange the Nodes by buckets. The rearranging is now done on next render instead.

diff --git a/src/main/java/org/distorted/library/main/DistortedChildrenList.java b/src/main/java/org/distorted/library/main/DistortedChildrenList.java
index 0964d19..2248aed 100644
--- a/src/main/java/org/distorted/library/main/DistortedChildrenList.java
+++ b/src/main/java/org/distorted/library/main/DistortedChildrenList.java
@@ -53,7 +53,6 @@ class DistortedChildrenList implements DistortedMaster.Slave
   public interface Parent
     {
     void adjustIsomorphism();
-    DistortedChildrenList getChildren();
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -82,12 +81,17 @@ class DistortedChildrenList implements DistortedMaster.Slave
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  void removeChild(DistortedNode node)
+  void rearrangeByBuckets(int index,long bucket)
     {
-    if( mChildren.remove(node) )
+    DistortedNode child = mChildren.remove(index);
+    int i;
+
+    for(i=0; i<index; i++)
       {
-      mNumChildren--;
+      if( mChildren.get(i).getBucket() > bucket ) break;
       }
+
+    mChildren.add(i,child);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -96,18 +100,15 @@ class DistortedChildrenList implements DistortedMaster.Slave
 // We want to keep same buckets next to each other, while avoiding changes in order of the children
 // (if possible!) We want to keep bucket=0 (i.e. the non-postprocessed children) at the beginning.
 
-  void addSortingByBuckets(DistortedNode newChild)
+  private void addSortingByBuckets(DistortedNode newChild)
     {
     int i;
-    EffectQueue queue = newChild.getEffects().getQueues()[3];
-    long bucket = queue.getID();
+    long bucket = newChild.getBucket();
     boolean sameBucket = false;
 
     for(i=0; i<mNumChildren; i++)
       {
-      queue = mChildren.get(i).getEffects().getQueues()[3];
-
-      if( queue.getID() == bucket )
+      if( mChildren.get(i).getBucket() == bucket )
         {
         sameBucket=true;
         }
diff --git a/src/main/java/org/distorted/library/main/DistortedEffects.java b/src/main/java/org/distorted/library/main/DistortedEffects.java
index a2b69c0..10443b4 100644
--- a/src/main/java/org/distorted/library/main/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/main/DistortedEffects.java
@@ -43,20 +43,6 @@ public class DistortedEffects
     return mQueues;
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void newNode(DistortedNode node)
-    {
-    EffectQueue.newNode(mQueues,node);
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void removeNode(DistortedNode node)
-    {
-    EffectQueue.removeNode(mQueues,node);
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   static void onDestroy()
diff --git a/src/main/java/org/distorted/library/main/DistortedNode.java b/src/main/java/org/distorted/library/main/DistortedNode.java
index 1272633..51fecd9 100644
--- a/src/main/java/org/distorted/library/main/DistortedNode.java
+++ b/src/main/java/org/distorted/library/main/DistortedNode.java
@@ -59,8 +59,13 @@ public class DistortedNode implements DistortedChildrenList.Parent
       mData.mFBO.markForDeletion();
       mData.mFBO = null;
       }
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    mEffects.removeNode(this);
+  long getBucket()
+    {
+    return mEffects.getQueues()[3].getID();
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -140,18 +145,6 @@ public class DistortedNode implements DistortedChildrenList.Parent
     if( mParent!=null ) mParent.adjustIsomorphism();
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * This is not really part of the public API. Has to be public only because it is a part of the
- * DistortedChildrenList.Parent interface.
- *
- * @y.exclude
- */
-  public DistortedChildrenList getChildren()
-    {
-    return mChildren;
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // return the total number of render calls issued
 
@@ -215,9 +208,16 @@ public class DistortedNode implements DistortedChildrenList.Parent
 
     if( numChildren>0 && mData.notRenderedYetAtThisTime(currTime) )
       {
+      DistortedNode node;
+      long oldBucket=0, newBucket;
+
       for (int i=0; i<numChildren; i++)
         {
-        numRenders += mChildren.getChild(i).renderRecursive(currTime);
+        node = mChildren.getChild(i);
+        newBucket = node.getBucket();
+        numRenders += node.renderRecursive(currTime);
+        if( newBucket<oldBucket ) mChildren.rearrangeByBuckets(i,newBucket);
+        else oldBucket=newBucket;
         }
 
       if( mData.mFBO==null ) mData.mFBO = allocateNewFBO();
@@ -251,18 +251,6 @@ public class DistortedNode implements DistortedChildrenList.Parent
     mParent = parent;
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void sort()
-    {
-    if( mParent!=null )
-      {
-      DistortedChildrenList siblings = mParent.getChildren();
-      siblings.removeChild(this);
-      siblings.addSortingByBuckets(this);
-      }
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -288,7 +276,6 @@ public class DistortedNode implements DistortedChildrenList.Parent
     mFboDepthStencil = DistortedFramebuffer.DEPTH_NO_STENCIL;
 
     mData = DistortedNodeData.returnData(generateIDList());
-    mEffects.newNode(this);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////  
@@ -350,7 +337,6 @@ public class DistortedNode implements DistortedChildrenList.Parent
       }
 
     mData = DistortedNodeData.returnData(generateIDList());
-    mEffects.newNode(this);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
index d1da899..6ee376a 100644
--- a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
@@ -352,7 +352,7 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  void clear()
+  private void clear()
     {
     DistortedRenderState.colorDepthStencilOn();
     GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
@@ -503,27 +503,11 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
- * This is not really part of the public API. Has to be public only because it is a part of the
- * DistortedChildrenList.Parent interface.
+ * Not part of the public API.
  *
  * @y.exclude
  */
-  public DistortedChildrenList getChildren()
-    {
-    return mChildren;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * This is not really part of the public API. Has to be public only because it is a part of the
- * DistortedChildrenList.Parent interface.
- *
- * @y.exclude
- */
-  public void adjustIsomorphism()
-    {
-
-    }
+  public void adjustIsomorphism() { }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
@@ -599,10 +583,16 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
     DistortedRenderState.reset();
 
     int numRenders=0, numChildren = mChildren.getNumChildren();
+    DistortedNode node;
+    long oldBucket=0, newBucket;
 
     for(int i=0; i<numChildren; i++)
       {
-      numRenders += mChildren.getChild(i).renderRecursive(time);
+      node = mChildren.getChild(i);
+      newBucket = node.getBucket();
+      numRenders += node.renderRecursive(time);
+      if( newBucket<oldBucket ) mChildren.rearrangeByBuckets(i,newBucket);
+      else oldBucket=newBucket;
       }
 
     numRenders += renderChildren(time,numChildren,mChildren,fbo, mRenderWayOIT);
diff --git a/src/main/java/org/distorted/library/main/EffectQueue.java b/src/main/java/org/distorted/library/main/EffectQueue.java
index 759904f..cfa81ad 100644
--- a/src/main/java/org/distorted/library/main/EffectQueue.java
+++ b/src/main/java/org/distorted/library/main/EffectQueue.java
@@ -53,7 +53,6 @@ abstract class EffectQueue implements DistortedMaster.Slave
   private static long mNextID;
   private static HashMap<ArrayList<Long>,Long> mMapID = new HashMap<>(); // maps lists of Effect IDs (longs) to a
                                                                          // single long - the queue ID.
-  private ArrayList<DistortedNode> mNodes = null;
   private long mID;
   private int mIndex;
   private boolean mCreated;
@@ -97,8 +96,6 @@ abstract class EffectQueue implements DistortedMaster.Slave
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// queues - guaranteed to be an array of EffectType.LENGTH length.
-// Change this when creating a new ty[e of effect!
 
   static void allocateQueues(EffectQueue[] queues, EffectQueue[] from, int flags, long id)
     {
@@ -109,8 +106,6 @@ abstract class EffectQueue implements DistortedMaster.Slave
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// queues - guaranteed to be an array of EffectType.LENGTH length.
-// Change this when creating a new ty[e of effect!
 
   static void compute(EffectQueue[] queues, long currTime, float halfW, float halfH, float halfZ )
     {
@@ -121,8 +116,6 @@ abstract class EffectQueue implements DistortedMaster.Slave
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// queues - guaranteed to be an array of EffectType.LENGTH length.
-// Change this when creating a new ty[e of effect!
 
   static void send(EffectQueue[] queues, DistortedOutputSurface surface, float inflate, float halfW, float halfH, float halfZ, int variant )
     {
@@ -132,35 +125,13 @@ abstract class EffectQueue implements DistortedMaster.Slave
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// queues - guaranteed to be an array of EffectType.LENGTH length.
-// Change this when creating a new ty[e of effect!
 
   static float[] getMVP(EffectQueue[] queues)
     {
-    return ((EffectQueueMatrix  )queues[0]).getMVP();
+    return ((EffectQueueMatrix)queues[0]).getMVP();
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-// queues - guaranteed to be an array of EffectType.LENGTH length.
-// Change this when creating a new ty[e of effect!
-
-  static void newNode(EffectQueue[] queues, DistortedNode node)
-    {
-    queues[3].newNode(node);
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// queues - guaranteed to be an array of EffectType.LENGTH length.
-// Change this when creating a new ty[e of effect!
-
-  static void removeNode(EffectQueue[] queues, DistortedNode node)
-    {
-    queues[3].removeNode(node);
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// queues - guaranteed to be an array of EffectType.LENGTH length.
-// Change this when creating a new ty[e of effect!
 
   static void getUniforms(int programH, int variant)
     {
@@ -174,7 +145,7 @@ abstract class EffectQueue implements DistortedMaster.Slave
 // (this is a speedup: then both queues can be applied once, which seriously speeds up stuff -
 // especially important in case of postprocessing)
 
-  private void regenerateIDandSort()
+  private void regenerateID()
     {
     if( mNumEffects>0 )
       {
@@ -196,25 +167,6 @@ abstract class EffectQueue implements DistortedMaster.Slave
       {
       mID = 0;
       }
-
-    int numNodes = (mNodes==null ? 0: mNodes.size());
-    for(int i=0; i<numNodes; i++) mNodes.get(i).sort();
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private void newNode(DistortedNode node)
-    {
-    if( mNodes==null ) mNodes = new ArrayList<>();
-
-    mNodes.add(node);
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private void removeNode(DistortedNode node)
-    {
-    mNodes.remove(node);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -465,6 +417,6 @@ abstract class EffectQueue implements DistortedMaster.Slave
         }
       }
 
-    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateIDandSort();
+    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
     }
   }
