commit 41b3ada0f851cf72dc51b4daed64827e5548f750
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sun Aug 23 12:25:32 2020 +0100

    Simplify AssociationUniformBlock

diff --git a/src/main/java/org/distorted/library/main/DistortedLibrary.java b/src/main/java/org/distorted/library/main/DistortedLibrary.java
index 0095544..0a1347d 100644
--- a/src/main/java/org/distorted/library/main/DistortedLibrary.java
+++ b/src/main/java/org/distorted/library/main/DistortedLibrary.java
@@ -36,7 +36,6 @@ import org.distorted.library.effect.FragmentEffect;
 import org.distorted.library.effect.PostprocessEffect;
 import org.distorted.library.effect.VertexEffect;
 import org.distorted.library.effectqueue.EffectQueueVertex;
-import org.distorted.library.mesh.AssociationUniformBlock;
 import org.distorted.library.mesh.DeferredJobs;
 import org.distorted.library.mesh.MeshBase;
 import org.distorted.library.message.EffectMessageSender;
@@ -305,11 +304,6 @@ public class DistortedLibrary
     mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
     mBlitDepthDepthH        = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
     mBlitDepthTexCorrH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
-
-    int[] params = new int[1];
-    int index = GLES30.glGetUniformBlockIndex(mMainProgramH, "meshAssociation");
-    GLES30.glGetActiveUniformBlockiv( mMainProgramH, index, GLES30.GL_UNIFORM_BLOCK_DATA_SIZE, params, 0);
-    AssociationUniformBlock.setAssociationSize(params[0]);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/mesh/AssociationUniformBlock.java b/src/main/java/org/distorted/library/mesh/AssociationUniformBlock.java
index 9f60fc0..0fb489f 100644
--- a/src/main/java/org/distorted/library/mesh/AssociationUniformBlock.java
+++ b/src/main/java/org/distorted/library/mesh/AssociationUniformBlock.java
@@ -27,33 +27,24 @@ import static org.distorted.library.mesh.MeshBase.MAX_EFFECT_COMPONENTS;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-public class AssociationUniformBlock
+class AssociationUniformBlock
   {
-  private static int mAssociationSize = 8*MAX_EFFECT_COMPONENTS;
+  private static final int ASSOCIATION_SIZE = 16*MAX_EFFECT_COMPONENTS;
   private static final int DEFAULT_ASSOCIATION = 0xffffffff;
 
   private InternalBuffer mUBO;
-
   private int[] mAssociations;
-  private int mAssociationBlock;
-  private boolean mNeedAdjustAssociation;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Public only because DistortedLibrary needs to see this to call setAssociationSize
- *
- * @y.exclude
- */
+
   AssociationUniformBlock()
     {
-    mNeedAdjustAssociation = true;
-    mAssociationBlock = computeAssociationBlockSize();
-    mAssociations= new int[mAssociationSize/4];
+    mAssociations= new int[ASSOCIATION_SIZE/4];
 
     for(int i=0; i<MAX_EFFECT_COMPONENTS; i++)
       {
-      mAssociations[getAndIndex(i)] = DEFAULT_ASSOCIATION;
-      mAssociations[getEquIndex(i)] = i;
+      mAssociations[4*i  ] = DEFAULT_ASSOCIATION;
+      mAssociations[4*i+2] = i;
       }
 
     mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_READ);
@@ -63,9 +54,6 @@ public class AssociationUniformBlock
 
   AssociationUniformBlock( AssociationUniformBlock original)
     {
-    mNeedAdjustAssociation = original.mNeedAdjustAssociation;
-    mAssociationBlock = original.mAssociationBlock;
-
     int size = original.mAssociations.length;
     mAssociations= new int[size];
     System.arraycopy(original.mAssociations, 0, mAssociations, 0, size);
@@ -73,102 +61,19 @@ public class AssociationUniformBlock
     mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_READ);
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * @y.exclude
- */
-   public static void setAssociationSize(int size)
-     {
-     if( size>0 ) mAssociationSize = size;  // if we are not using any VertexEffects, there
-                                            // will be no 'meshAssociation' UBO in the shader,
-                                            // and thus its detected size will be 0. Do not change
-                                            // the initial guess then - we still might need the
-                                            // associations for applying the MatrixEffects to a
-                                            // mesh, for one.
-     }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-   private int getAndIndex(int component)
+   boolean matchesAssociation( int comp, int andAssoc, int equAssoc)
      {
-     return mAssociationBlock*component;
+     return (andAssoc & mAssociations[4*comp]) != 0 || (equAssoc == mAssociations[4*comp+2]);
      }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-   private int getEquIndex(int component)
+   void setEffectAssociationNow(int comp, int andAssociation, int equAssociation)
      {
-     return mAssociationBlock*(MAX_EFFECT_COMPONENTS+component);
-     }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-   private int computeAssociationBlockSize()
-     {
-     return 1 + (mAssociationSize/4 - 2*MAX_EFFECT_COMPONENTS) / (2*MAX_EFFECT_COMPONENTS-1);
-     }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-   private int getAndAssoc(int comp)
-     {
-     return mAssociations[getAndIndex(comp)];
-     }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-   private int getEquAssoc(int comp)
-     {
-     return mAssociations[getEquIndex(comp)];
-     }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// The problem here is that at MeshBase object creation time, we might not know the size of the
-// 'meshAssociation' Uniform Block Object in the vertex shader (the UBO is packed according to the
-// 'shared' layout, which means the size of the block is known only after linking the shaders).
-//
-// We thus, in the constructor, guess that the layout will be tight (so we will need 8*MAX_EFFECT_C
-// bytes there), allocate mAssociation already, and if later on, as a result of linking the shaders,
-// we get a call to setAssociationSize() which changes the size to something else, we need to reallocate.
-//
-// It can happen that even before the linking the value of mAssociationSize is already 'right' because
-// this is a static variable and it might persist from an earlier run. All the better then; this should
-// never be wrong.
-
-   private void adjustAssociation()
-     {
-     int oldLen = mAssociations.length;
-
-     if( mAssociationSize != 4*oldLen )
-       {
-       int[] tmp = new int[oldLen];
-       System.arraycopy(mAssociations, 0, tmp, 0, oldLen);
-
-       int newLen = mAssociationSize/4;
-       mAssociations = new int[newLen];
-       mAssociationBlock = computeAssociationBlockSize();
-
-       for(int i=0; i<oldLen/2; i++)
-         {
-         mAssociations[getAndIndex(i)] = tmp[i];                         // oldLen must be equal to
-         mAssociations[getEquIndex(i)] = tmp[MAX_EFFECT_COMPONENTS+i];   // 8*MAX_EFFECT_COM
-         }
-       }
-     }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-   boolean matchesAssociation( int component, int andAssoc, int equAssoc)
-     {
-     return (andAssoc & mAssociations[getAndIndex(component)]) != 0 || (equAssoc == mAssociations[getEquIndex(component)]);
-     }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
-     {
-     mAssociations[getAndIndex(component)] = andAssociation;
-     mAssociations[getEquIndex(component)] = equAssociation;
+     mAssociations[4*comp  ] = andAssociation;
+     mAssociations[4*comp+2] = equAssociation;
 
      mUBO.invalidate();
      }
@@ -177,20 +82,14 @@ public class AssociationUniformBlock
 
    int getIndex()
      {
-     if( mNeedAdjustAssociation )
-       {
-       mNeedAdjustAssociation = false;
-       adjustAssociation();
-       }
-
-     return mUBO.createImmediatelyInt( mAssociationSize, mAssociations);
+     return mUBO.createImmediatelyInt( ASSOCIATION_SIZE, mAssociations);
      }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
    void copy( int compTo, AssociationUniformBlock assocFrom, int compFrom)
      {
-     mAssociations[getAndIndex(compTo)] = assocFrom.getAndAssoc(compFrom);
-     mAssociations[getEquIndex(compTo)] = assocFrom.getEquAssoc(compFrom);
+     mAssociations[4*compTo  ] = assocFrom.mAssociations[4*compFrom  ];
+     mAssociations[4*compTo+2] = assocFrom.mAssociations[4*compFrom+2];
      }
   }
diff --git a/src/main/java/org/distorted/library/mesh/MeshBase.java b/src/main/java/org/distorted/library/mesh/MeshBase.java
index 67577ee..f18c358 100644
--- a/src/main/java/org/distorted/library/mesh/MeshBase.java
+++ b/src/main/java/org/distorted/library/mesh/MeshBase.java
@@ -46,7 +46,7 @@ import java.util.ArrayList;
  */
 public abstract class MeshBase
    {
-   private static final int UBO_BINDING = 1;
+   private static final int UBO_BINDING = 3;
            static final int MAX_EFFECT_COMPONENTS= 100;
 
    // sizes of attributes of an individual vertex.
diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 61a1d08..0742f88 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -56,10 +56,9 @@ uniform vec4 vUniforms[3*NUM_VERTEX];// i-th effect is 3 consecutive vec4's: [3*
                                      // The first vec4 is the Interpolated values,
                                      // second vec4: first float - cache, next 3: Center, the third -  the Region.
 
-layout (shared, binding=1) uniform meshAssociation
+layout (std140, binding=3) uniform meshAssociation
   {
-  int vComAndAssoc[MAX_COMPON]; // 'logical AND' association of the component.
-  int vComEquAssoc[MAX_COMPON]; // 'equal' association of the component.
+  ivec4 vComAssoc[MAX_COMPON]; // component Associations
   };
 
 //////////////////////////////////////////////////////////////////////////////////////////////
@@ -119,7 +118,7 @@ void main()
 
   for(int i=0; i<vNumEffects; i++)
     {
-    if( ((vComAndAssoc[component] & vEffAndAssoc[i]) != 0) || (vComEquAssoc[component] == vEffEquAssoc[i]) )
+    if( ((vComAssoc[component].x & vEffAndAssoc[i]) != 0) || (vComAssoc[component].z == vEffEquAssoc[i]) )
       {
       // ENABLED EFFECTS WILL BE INSERTED HERE
 
