1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library.uniformblock;
|
21
|
|
22
|
import android.opengl.GLES30;
|
23
|
|
24
|
import org.distorted.library.main.InternalBuffer;
|
25
|
import org.distorted.library.mesh.MeshBase;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
/**
|
29
|
* Not part of public API, do not document
|
30
|
*
|
31
|
* @y.exclude
|
32
|
*/
|
33
|
public class UniformBlockAssociation
|
34
|
{
|
35
|
private static final int DEFAULT_ASSOCIATION = 0xffffffff;
|
36
|
private static final int DEFAULT_STRIDE = 4;
|
37
|
private static final int LOC_AND = 0;
|
38
|
private static final int LOC_EQU = 1;
|
39
|
|
40
|
private final InternalBuffer mUBO;
|
41
|
private final int mMax;
|
42
|
private int[] mAssociations;
|
43
|
private int mStride;
|
44
|
|
45
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
46
|
|
47
|
public UniformBlockAssociation()
|
48
|
{
|
49
|
mMax = MeshBase.getMaxEffComponents();
|
50
|
mStride = DEFAULT_STRIDE;
|
51
|
mAssociations= new int[mStride*mMax];
|
52
|
|
53
|
for(int i=0; i<mMax; i++)
|
54
|
{
|
55
|
mAssociations[mStride*i+LOC_AND] = DEFAULT_ASSOCIATION;
|
56
|
mAssociations[mStride*i+LOC_EQU] = i;
|
57
|
}
|
58
|
|
59
|
mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_READ);
|
60
|
}
|
61
|
|
62
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63
|
|
64
|
public UniformBlockAssociation(UniformBlockAssociation original)
|
65
|
{
|
66
|
mMax = original.mMax;
|
67
|
mStride = original.mStride;
|
68
|
int size = original.mAssociations.length;
|
69
|
mAssociations= new int[size];
|
70
|
System.arraycopy(original.mAssociations, 0, mAssociations, 0, size);
|
71
|
|
72
|
mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_READ);
|
73
|
}
|
74
|
|
75
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76
|
// stride can be 0, if we just tried compiling a vertex shader which has NUM_VERTEX=0.
|
77
|
// stride==1 we also don't like because then we have an exception ( stride*mMax-1 < stride*(mMax-1)+1 )
|
78
|
|
79
|
public void correctStride(int stride)
|
80
|
{
|
81
|
if( mStride != stride && stride>1 )
|
82
|
{
|
83
|
int[] tmp = new int[stride*mMax];
|
84
|
|
85
|
for(int i=0; i<mMax; i++)
|
86
|
{
|
87
|
tmp[stride*i+LOC_AND] = mAssociations[mStride*i+LOC_AND];
|
88
|
tmp[stride*i+LOC_EQU] = mAssociations[mStride*i+LOC_EQU];
|
89
|
}
|
90
|
|
91
|
mAssociations = tmp;
|
92
|
mStride = stride;
|
93
|
}
|
94
|
}
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
|
98
|
public boolean matchesAssociation( int comp, int andAssoc, int equAssoc)
|
99
|
{
|
100
|
return (andAssoc & mAssociations[mStride*comp+LOC_AND]) != 0 || (equAssoc == mAssociations[mStride*comp+LOC_EQU]);
|
101
|
}
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
|
105
|
public void setEffectAssociationNow(int comp, int andAssociation, int equAssociation)
|
106
|
{
|
107
|
mAssociations[mStride*comp+LOC_AND] = andAssociation;
|
108
|
mAssociations[mStride*comp+LOC_EQU] = equAssociation;
|
109
|
|
110
|
mUBO.invalidate();
|
111
|
}
|
112
|
|
113
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
114
|
|
115
|
public int getIndex()
|
116
|
{
|
117
|
return mUBO.createImmediatelyInt( 4*mStride*mMax, mAssociations);
|
118
|
}
|
119
|
|
120
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
121
|
|
122
|
public void copy(int compTo, UniformBlockAssociation assocFrom, int compFrom)
|
123
|
{
|
124
|
mAssociations[mStride*compTo+LOC_AND] = assocFrom.mAssociations[mStride*compFrom+LOC_AND];
|
125
|
mAssociations[mStride*compTo+LOC_EQU] = assocFrom.mAssociations[mStride*compFrom+LOC_EQU];
|
126
|
}
|
127
|
|
128
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129
|
|
130
|
public void markForDeletion()
|
131
|
{
|
132
|
mUBO.markForDeletion();
|
133
|
}
|
134
|
|
135
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
|
137
|
public void print()
|
138
|
{
|
139
|
StringBuilder builder = new StringBuilder();
|
140
|
|
141
|
builder.append(mUBO.getID());
|
142
|
builder.append(' ');
|
143
|
|
144
|
for(int i=0; i<8; i++)
|
145
|
{
|
146
|
builder.append(mAssociations[mStride*i+LOC_AND]);
|
147
|
builder.append(' ');
|
148
|
builder.append(mAssociations[mStride*i+LOC_EQU]);
|
149
|
builder.append(' ');
|
150
|
}
|
151
|
|
152
|
String res = builder.toString();
|
153
|
|
154
|
android.util.Log.e("uba", res);
|
155
|
}
|
156
|
}
|