commit 43814a57a365c17a0be000bc9beefe75865e64a4
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Thu Nov 26 11:29:23 2020 +0100

    Fix the fact that the static DistortedEffects.mAllQueues was global, shared between all Activities.
    Completely replace this mechanism with a non-static list of links from a VertexEffect to all VertexEffectQueues this effect is a member of.

diff --git a/src/main/java/org/distorted/library/effect/Effect.java b/src/main/java/org/distorted/library/effect/Effect.java
index b8b0487..99e4666 100644
--- a/src/main/java/org/distorted/library/effect/Effect.java
+++ b/src/main/java/org/distorted/library/effect/Effect.java
@@ -19,6 +19,7 @@
 
 package org.distorted.library.effect;
 
+import org.distorted.library.effectqueue.EffectQueue;
 import org.distorted.library.message.EffectListener;
 
 import java.lang.reflect.Method;
@@ -58,6 +59,11 @@ public abstract class Effect
     for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  public abstract void addQueue(EffectQueue queue);
+  public abstract void remQueue(EffectQueue queue);
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   Effect(EffectName name)
diff --git a/src/main/java/org/distorted/library/effect/FragmentEffect.java b/src/main/java/org/distorted/library/effect/FragmentEffect.java
index 44f1950..97bbe53 100644
--- a/src/main/java/org/distorted/library/effect/FragmentEffect.java
+++ b/src/main/java/org/distorted/library/effect/FragmentEffect.java
@@ -19,6 +19,7 @@
 
 package org.distorted.library.effect;
 
+import org.distorted.library.effectqueue.EffectQueue;
 import org.distorted.library.type.Static3D;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -98,6 +99,28 @@ public abstract class FragmentEffect extends Effect
     mGLSL = "";
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Only for use by the library itself.
+ *
+ * @y.exclude
+ */
+  public void addQueue(EffectQueue queue)
+    {
+    // NO OP
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Only for use by the library itself.
+ *
+ * @y.exclude
+ */
+  public void remQueue(EffectQueue queue)
+    {
+    // NO OP
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/effect/MatrixEffect.java b/src/main/java/org/distorted/library/effect/MatrixEffect.java
index a2289c0..ef2f689 100644
--- a/src/main/java/org/distorted/library/effect/MatrixEffect.java
+++ b/src/main/java/org/distorted/library/effect/MatrixEffect.java
@@ -19,6 +19,8 @@
 
 package org.distorted.library.effect;
 
+import org.distorted.library.effectqueue.EffectQueue;
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Abstract class that represents an Effect that works by modifying the ModelView matrix.
@@ -44,6 +46,28 @@ public abstract class MatrixEffect extends Effect
  */
   public abstract void apply(float[] matrixP, float[] matrixV, float[] uniforms, int index);
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Only for use by the library itself.
+ *
+ * @y.exclude
+ */
+  public void addQueue(EffectQueue queue)
+    {
+    // NO OP
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Only for use by the library itself.
+ *
+ * @y.exclude
+ */
+  public void remQueue(EffectQueue queue)
+    {
+    // NO OP
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // empty function for completeness
 
diff --git a/src/main/java/org/distorted/library/effect/PostprocessEffect.java b/src/main/java/org/distorted/library/effect/PostprocessEffect.java
index f75c792..a128dd7 100644
--- a/src/main/java/org/distorted/library/effect/PostprocessEffect.java
+++ b/src/main/java/org/distorted/library/effect/PostprocessEffect.java
@@ -19,6 +19,7 @@
 
 package org.distorted.library.effect;
 
+import org.distorted.library.effectqueue.EffectQueue;
 import org.distorted.library.main.DistortedFramebuffer;
 import org.distorted.library.main.InternalMaster;
 import org.distorted.library.program.DistortedProgram;
@@ -165,6 +166,28 @@ public abstract class PostprocessEffect extends Effect implements InternalMaster
     return mQualityLevel;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Only for use by the library itself.
+ *
+ * @y.exclude
+ */
+  public void addQueue(EffectQueue queue)
+    {
+    // NO OP
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Only for use by the library itself.
+ *
+ * @y.exclude
+ */
+  public void remQueue(EffectQueue queue)
+    {
+    // NO OP
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * This is not really part of the public API. Has to be public only because it is a part of the
diff --git a/src/main/java/org/distorted/library/effect/VertexEffect.java b/src/main/java/org/distorted/library/effect/VertexEffect.java
index 5591a58..bd84263 100644
--- a/src/main/java/org/distorted/library/effect/VertexEffect.java
+++ b/src/main/java/org/distorted/library/effect/VertexEffect.java
@@ -19,10 +19,11 @@
 
 package org.distorted.library.effect;
 
-import org.distorted.library.main.DistortedEffects;
+import org.distorted.library.effectqueue.EffectQueue;
 import org.distorted.library.type.Static4D;
 
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
@@ -51,6 +52,8 @@ public abstract class VertexEffect extends Effect
 
   final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
 
+  ArrayList<EffectQueue> mQueues;
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   VertexEffect(EffectName name)
@@ -194,6 +197,32 @@ public abstract class VertexEffect extends Effect
     return mNumEnabled;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Only for use by the library itself.
+ *
+ * @y.exclude
+ */
+  public void addQueue(EffectQueue queue)
+    {
+    if( mQueues==null ) mQueues = new ArrayList<>();
+
+    mQueues.add(queue);
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Only for use by the library itself.
+ *
+ * @y.exclude
+ */
+  public void remQueue(EffectQueue queue)
+    {
+    if( mQueues==null ) mQueues = new ArrayList<>();
+
+    mQueues.remove(queue);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Set Mesh association.
@@ -214,6 +243,13 @@ public abstract class VertexEffect extends Effect
     mAndAssociation = andAssociation;
     mEquAssociation = equAssociation;
 
-    DistortedEffects.setAssociation(getID());
+    long id = getID();
+    int numQueues = mQueues==null ? 0: mQueues.size();
+
+    for(int i=0; i<numQueues; i++)
+      {
+      EffectQueue queue = mQueues.get(i);
+      queue.setAssociation(id);
+      }
     }
   }
\ No newline at end of file
diff --git a/src/main/java/org/distorted/library/effectqueue/EffectQueue.java b/src/main/java/org/distorted/library/effectqueue/EffectQueue.java
index 84e2501..5a83492 100644
--- a/src/main/java/org/distorted/library/effectqueue/EffectQueue.java
+++ b/src/main/java/org/distorted/library/effectqueue/EffectQueue.java
@@ -260,6 +260,7 @@ public abstract class EffectQueue implements InternalMaster.Slave
     if( mNumEffects>pos )
       {
       mNumEffects--;
+      mEffects[pos].remQueue(this);
       System.arraycopy(mEffects, pos+1, mEffects, pos, mNumEffects-pos);
       System.arraycopy(mIntUniforms, mNumIntUniforms*(pos+1), mIntUniforms, mNumIntUniforms*pos, mNumIntUniforms*(mNumEffects-pos) );
       mEffects[mNumEffects] = null;
@@ -273,6 +274,8 @@ public abstract class EffectQueue implements InternalMaster.Slave
     mEffects[pos]                     = effect;
     mIntUniforms[mNumIntUniforms*pos] = effect.getName().ordinal();
 
+    effect.addQueue(this);
+
     if( mIndex==EffectType.VERTEX.ordinal() )
       {
       effect.writeAssociations(mIntUniforms, mNumIntUniforms*pos+1, mNumIntUniforms*pos+3);
@@ -410,6 +413,19 @@ public abstract class EffectQueue implements InternalMaster.Slave
       }
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  public void setAssociation(long effectID)
+    {
+    for(int j=0; j<mNumEffects; j++)
+      {
+      if (mEffects[j].getID() == effectID)
+        {
+        mEffects[j].writeAssociations(mIntUniforms, mNumIntUniforms*j+1, mNumIntUniforms*j+3);
+        }
+      }
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   public void doWork()
diff --git a/src/main/java/org/distorted/library/effectqueue/EffectQueueVertex.java b/src/main/java/org/distorted/library/effectqueue/EffectQueueVertex.java
index f44d2d4..6aff552 100644
--- a/src/main/java/org/distorted/library/effectqueue/EffectQueueVertex.java
+++ b/src/main/java/org/distorted/library/effectqueue/EffectQueueVertex.java
@@ -67,23 +67,6 @@ public class EffectQueueVertex extends EffectQueue
     mUniformsH[variant]  = GLES30.glGetUniformLocation( mProgramH, "vUniforms");
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Not part of public API, do not document (public only because has to be called from DistortedEffects)
- *
- * @y.exclude
- */
-  public void setAssociation(long effectID)
-    {
-    for(int j=0; j<mNumEffects; j++)
-      {
-      if (mEffects[j].getID() == effectID)
-        {
-        mEffects[j].writeAssociations(mIntUniforms, NUM_INT_UNIFORMS*j+1, NUM_INT_UNIFORMS*j+3);
-        }
-      }
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Not part of public API, do not document (public only because has to be used in Meshes)
diff --git a/src/main/java/org/distorted/library/main/DistortedEffects.java b/src/main/java/org/distorted/library/main/DistortedEffects.java
index dbe15cd..80f8e18 100644
--- a/src/main/java/org/distorted/library/main/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/main/DistortedEffects.java
@@ -23,9 +23,6 @@ import org.distorted.library.effect.Effect;
 import org.distorted.library.effect.EffectName;
 import org.distorted.library.effectqueue.EffectQueue;
 import org.distorted.library.effect.EffectType;
-import org.distorted.library.effectqueue.EffectQueueVertex;
-
-import java.util.ArrayList;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
@@ -35,7 +32,6 @@ import java.util.ArrayList;
  */
 public class DistortedEffects
   {
-  private static ArrayList<DistortedEffects> mAllEffectQueues = new ArrayList<>();
   private static long mNextID =0;
   private long mID;
   private EffectQueue[] mQueues;
@@ -54,23 +50,6 @@ public class DistortedEffects
   static void onDestroy()
     {
     mNextID =  0;
-    mAllEffectQueues.clear();
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * @y.exclude
- */
-  public static void setAssociation(long effectID)
-    {
-    EffectQueue[] queues;
-    int numQueues = mAllEffectQueues.size();
-
-    for(int i=0; i<numQueues; i++)
-      {
-      queues = mAllEffectQueues.get(i).getQueues();
-      ((EffectQueueVertex)queues[1]).setAssociation(effectID);
-      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -84,7 +63,6 @@ public class DistortedEffects
     mID = ++mNextID;
     mQueues = new EffectQueue[EffectType.LENGTH];
     EffectQueue.allocateQueues(mQueues,null,0);
-    mAllEffectQueues.add(this);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -102,7 +80,6 @@ public class DistortedEffects
     mID = ++mNextID;
     mQueues = new EffectQueue[EffectType.LENGTH];
     EffectQueue.allocateQueues(mQueues,dc.getQueues(),flags);
-    mAllEffectQueues.add(this);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/main/InternalBuffer.java b/src/main/java/org/distorted/library/main/InternalBuffer.java
index 1545b7f..e5fff78 100644
--- a/src/main/java/org/distorted/library/main/InternalBuffer.java
+++ b/src/main/java/org/distorted/library/main/InternalBuffer.java
@@ -38,6 +38,7 @@ import java.nio.IntBuffer;
  */
 public class InternalBuffer extends InternalObject
   {
+  private static final int DONE     = 0;
   private static final int RECREATE = 1;
   private static final int UPDATE   = 2;
 
@@ -57,8 +58,7 @@ public class InternalBuffer extends InternalObject
     mUsage  = usage;
     mBuffer = null;
     mSize   = 0;
-
-    recreate();
+    mStatus = RECREATE;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -93,7 +93,7 @@ public class InternalBuffer extends InternalObject
       updateFloat(buffer);
       }
 
-    mStatus = 0;
+    mStatus = DONE;
 
     return mIndex[0];
     }
@@ -130,7 +130,7 @@ public class InternalBuffer extends InternalObject
       updateInt(buffer);
       }
 
-    mStatus = 0;
+    mStatus = DONE;
 
     return mIndex[0];
     }
@@ -145,6 +145,8 @@ public class InternalBuffer extends InternalObject
     GLES30.glBindBuffer( mTarget, mIndex[0]);
     GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
     GLES30.glBindBuffer( mTarget, 0);
+
+    mStatus &= (~UPDATE);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -157,6 +159,8 @@ public class InternalBuffer extends InternalObject
     GLES30.glBindBuffer( mTarget, mIndex[0]);
     GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
     GLES30.glBindBuffer( mTarget, 0);
+
+    mStatus &= (~UPDATE);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -175,6 +179,8 @@ public class InternalBuffer extends InternalObject
     GLES30.glBindBuffer( mTarget, mIndex[0]);
     GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
     GLES30.glBindBuffer( mTarget, 0);
+
+    mStatus = DONE;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
