Project

General

Profile

« Previous | Next » 

Revision 41b3ada0

Added by Leszek Koltunski over 4 years ago

Simplify AssociationUniformBlock

View differences:

src/main/java/org/distorted/library/main/DistortedLibrary.java
36 36
import org.distorted.library.effect.PostprocessEffect;
37 37
import org.distorted.library.effect.VertexEffect;
38 38
import org.distorted.library.effectqueue.EffectQueueVertex;
39
import org.distorted.library.mesh.AssociationUniformBlock;
40 39
import org.distorted.library.mesh.DeferredJobs;
41 40
import org.distorted.library.mesh.MeshBase;
42 41
import org.distorted.library.message.EffectMessageSender;
......
305 304
    mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
306 305
    mBlitDepthDepthH        = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
307 306
    mBlitDepthTexCorrH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
308

  
309
    int[] params = new int[1];
310
    int index = GLES30.glGetUniformBlockIndex(mMainProgramH, "meshAssociation");
311
    GLES30.glGetActiveUniformBlockiv( mMainProgramH, index, GLES30.GL_UNIFORM_BLOCK_DATA_SIZE, params, 0);
312
    AssociationUniformBlock.setAssociationSize(params[0]);
313 307
    }
314 308

  
315 309
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/mesh/AssociationUniformBlock.java
27 27

  
28 28
///////////////////////////////////////////////////////////////////////////////////////////////////
29 29

  
30
public class AssociationUniformBlock
30
class AssociationUniformBlock
31 31
  {
32
  private static int mAssociationSize = 8*MAX_EFFECT_COMPONENTS;
32
  private static final int ASSOCIATION_SIZE = 16*MAX_EFFECT_COMPONENTS;
33 33
  private static final int DEFAULT_ASSOCIATION = 0xffffffff;
34 34

  
35 35
  private InternalBuffer mUBO;
36

  
37 36
  private int[] mAssociations;
38
  private int mAssociationBlock;
39
  private boolean mNeedAdjustAssociation;
40 37

  
41 38
///////////////////////////////////////////////////////////////////////////////////////////////////
42
/**
43
 * Public only because DistortedLibrary needs to see this to call setAssociationSize
44
 *
45
 * @y.exclude
46
 */
39

  
47 40
  AssociationUniformBlock()
48 41
    {
49
    mNeedAdjustAssociation = true;
50
    mAssociationBlock = computeAssociationBlockSize();
51
    mAssociations= new int[mAssociationSize/4];
42
    mAssociations= new int[ASSOCIATION_SIZE/4];
52 43

  
53 44
    for(int i=0; i<MAX_EFFECT_COMPONENTS; i++)
54 45
      {
55
      mAssociations[getAndIndex(i)] = DEFAULT_ASSOCIATION;
56
      mAssociations[getEquIndex(i)] = i;
46
      mAssociations[4*i  ] = DEFAULT_ASSOCIATION;
47
      mAssociations[4*i+2] = i;
57 48
      }
58 49

  
59 50
    mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_READ);
......
63 54

  
64 55
  AssociationUniformBlock( AssociationUniformBlock original)
65 56
    {
66
    mNeedAdjustAssociation = original.mNeedAdjustAssociation;
67
    mAssociationBlock = original.mAssociationBlock;
68

  
69 57
    int size = original.mAssociations.length;
70 58
    mAssociations= new int[size];
71 59
    System.arraycopy(original.mAssociations, 0, mAssociations, 0, size);
......
73 61
    mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_READ);
74 62
    }
75 63

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
/**
78
 * @y.exclude
79
 */
80
   public static void setAssociationSize(int size)
81
     {
82
     if( size>0 ) mAssociationSize = size;  // if we are not using any VertexEffects, there
83
                                            // will be no 'meshAssociation' UBO in the shader,
84
                                            // and thus its detected size will be 0. Do not change
85
                                            // the initial guess then - we still might need the
86
                                            // associations for applying the MatrixEffects to a
87
                                            // mesh, for one.
88
     }
89

  
90 64
///////////////////////////////////////////////////////////////////////////////////////////////////
91 65

  
92
   private int getAndIndex(int component)
66
   boolean matchesAssociation( int comp, int andAssoc, int equAssoc)
93 67
     {
94
     return mAssociationBlock*component;
68
     return (andAssoc & mAssociations[4*comp]) != 0 || (equAssoc == mAssociations[4*comp+2]);
95 69
     }
96 70

  
97 71
///////////////////////////////////////////////////////////////////////////////////////////////////
98 72

  
99
   private int getEquIndex(int component)
73
   void setEffectAssociationNow(int comp, int andAssociation, int equAssociation)
100 74
     {
101
     return mAssociationBlock*(MAX_EFFECT_COMPONENTS+component);
102
     }
103

  
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

  
106
   private int computeAssociationBlockSize()
107
     {
108
     return 1 + (mAssociationSize/4 - 2*MAX_EFFECT_COMPONENTS) / (2*MAX_EFFECT_COMPONENTS-1);
109
     }
110

  
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

  
113
   private int getAndAssoc(int comp)
114
     {
115
     return mAssociations[getAndIndex(comp)];
116
     }
117

  
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

  
120
   private int getEquAssoc(int comp)
121
     {
122
     return mAssociations[getEquIndex(comp)];
123
     }
124

  
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
// The problem here is that at MeshBase object creation time, we might not know the size of the
127
// 'meshAssociation' Uniform Block Object in the vertex shader (the UBO is packed according to the
128
// 'shared' layout, which means the size of the block is known only after linking the shaders).
129
//
130
// We thus, in the constructor, guess that the layout will be tight (so we will need 8*MAX_EFFECT_C
131
// bytes there), allocate mAssociation already, and if later on, as a result of linking the shaders,
132
// we get a call to setAssociationSize() which changes the size to something else, we need to reallocate.
133
//
134
// It can happen that even before the linking the value of mAssociationSize is already 'right' because
135
// this is a static variable and it might persist from an earlier run. All the better then; this should
136
// never be wrong.
137

  
138
   private void adjustAssociation()
139
     {
140
     int oldLen = mAssociations.length;
141

  
142
     if( mAssociationSize != 4*oldLen )
143
       {
144
       int[] tmp = new int[oldLen];
145
       System.arraycopy(mAssociations, 0, tmp, 0, oldLen);
146

  
147
       int newLen = mAssociationSize/4;
148
       mAssociations = new int[newLen];
149
       mAssociationBlock = computeAssociationBlockSize();
150

  
151
       for(int i=0; i<oldLen/2; i++)
152
         {
153
         mAssociations[getAndIndex(i)] = tmp[i];                         // oldLen must be equal to
154
         mAssociations[getEquIndex(i)] = tmp[MAX_EFFECT_COMPONENTS+i];   // 8*MAX_EFFECT_COM
155
         }
156
       }
157
     }
158

  
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

  
161
   boolean matchesAssociation( int component, int andAssoc, int equAssoc)
162
     {
163
     return (andAssoc & mAssociations[getAndIndex(component)]) != 0 || (equAssoc == mAssociations[getEquIndex(component)]);
164
     }
165

  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

  
168
   void setEffectAssociationNow(int component, int andAssociation, int equAssociation)
169
     {
170
     mAssociations[getAndIndex(component)] = andAssociation;
171
     mAssociations[getEquIndex(component)] = equAssociation;
75
     mAssociations[4*comp  ] = andAssociation;
76
     mAssociations[4*comp+2] = equAssociation;
172 77

  
173 78
     mUBO.invalidate();
174 79
     }
......
177 82

  
178 83
   int getIndex()
179 84
     {
180
     if( mNeedAdjustAssociation )
181
       {
182
       mNeedAdjustAssociation = false;
183
       adjustAssociation();
184
       }
185

  
186
     return mUBO.createImmediatelyInt( mAssociationSize, mAssociations);
85
     return mUBO.createImmediatelyInt( ASSOCIATION_SIZE, mAssociations);
187 86
     }
188 87

  
189 88
///////////////////////////////////////////////////////////////////////////////////////////////////
190 89

  
191 90
   void copy( int compTo, AssociationUniformBlock assocFrom, int compFrom)
192 91
     {
193
     mAssociations[getAndIndex(compTo)] = assocFrom.getAndAssoc(compFrom);
194
     mAssociations[getEquIndex(compTo)] = assocFrom.getEquAssoc(compFrom);
92
     mAssociations[4*compTo  ] = assocFrom.mAssociations[4*compFrom  ];
93
     mAssociations[4*compTo+2] = assocFrom.mAssociations[4*compFrom+2];
195 94
     }
196 95
  }
src/main/java/org/distorted/library/mesh/MeshBase.java
46 46
 */
47 47
public abstract class MeshBase
48 48
   {
49
   private static final int UBO_BINDING = 1;
49
   private static final int UBO_BINDING = 3;
50 50
           static final int MAX_EFFECT_COMPONENTS= 100;
51 51

  
52 52
   // sizes of attributes of an individual vertex.
src/main/res/raw/main_vertex_shader.glsl
56 56
                                     // The first vec4 is the Interpolated values,
57 57
                                     // second vec4: first float - cache, next 3: Center, the third -  the Region.
58 58

  
59
layout (shared, binding=1) uniform meshAssociation
59
layout (std140, binding=3) uniform meshAssociation
60 60
  {
61
  int vComAndAssoc[MAX_COMPON]; // 'logical AND' association of the component.
62
  int vComEquAssoc[MAX_COMPON]; // 'equal' association of the component.
61
  ivec4 vComAssoc[MAX_COMPON]; // component Associations
63 62
  };
64 63

  
65 64
//////////////////////////////////////////////////////////////////////////////////////////////
......
119 118

  
120 119
  for(int i=0; i<vNumEffects; i++)
121 120
    {
122
    if( ((vComAndAssoc[component] & vEffAndAssoc[i]) != 0) || (vComEquAssoc[component] == vEffEquAssoc[i]) )
121
    if( ((vComAssoc[component].x & vEffAndAssoc[i]) != 0) || (vComAssoc[component].z == vEffEquAssoc[i]) )
123 122
      {
124 123
      // ENABLED EFFECTS WILL BE INSERTED HERE
125 124

  

Also available in: Unified diff