Revision 41b3ada0
Added by Leszek Koltunski over 3 years ago
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 |
} |
Also available in: Unified diff
Simplify AssociationUniformBlock