Project

General

Profile

Download (6.12 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / uniformblock / UniformBlockAssociation.java @ 8c57d77b

1
////////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.uniformblock;
22

    
23
import android.opengl.GLES30;
24

    
25
import org.distorted.library.main.DistortedLibrary;
26
import org.distorted.library.main.InternalBuffer;
27
import org.distorted.library.mesh.MeshBase;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
/**
31
 * Not part of public API, do not document
32
 *
33
 * @y.exclude
34
 */
35
public class UniformBlockAssociation
36
  {
37
  private static final int DEFAULT_ASSOCIATION = 0xffffffff;
38
  private static final int DEFAULT_STRIDE = 4;
39
  private static final int LOC_AND = 0;
40
  private static final int LOC_EQU = 1;
41

    
42
  private final InternalBuffer mUBO;
43
  private final int mMax;
44
  private int[] mAssociations;
45
  private int mStride;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  public UniformBlockAssociation()
50
    {
51
    mMax = MeshBase.getMaxEffComponents();
52
    mStride = DEFAULT_STRIDE;
53
    mAssociations= new int[mStride*mMax];
54

    
55
    for(int i=0; i<mMax; i++)
56
      {
57
      mAssociations[mStride*i+LOC_AND] = DEFAULT_ASSOCIATION;
58
      mAssociations[mStride*i+LOC_EQU] = i;
59
      }
60

    
61
    mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_DRAW);
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  public UniformBlockAssociation(UniformBlockAssociation original)
67
    {
68
    mMax = original.mMax;
69
    mStride = original.mStride;
70
    int size = original.mAssociations.length;
71
    mAssociations= new int[size];
72
    System.arraycopy(original.mAssociations, 0, mAssociations, 0, size);
73

    
74
    mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_DRAW);
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
// stride can be 0, if we just tried compiling a vertex shader which has NUM_VERTEX=0.
79
// stride==1 we also don't like because then we have an exception ( stride*mMax-1 < stride*(mMax-1)+1 )
80

    
81
  public void correctStride(int stride)
82
    {
83
    if( mStride != stride && stride>1 )
84
      {
85
      int[] tmp = new int[stride*mMax];
86

    
87
      for(int i=0; i<mMax; i++)
88
        {
89
        tmp[stride*i+LOC_AND] = mAssociations[mStride*i+LOC_AND];
90
        tmp[stride*i+LOC_EQU] = mAssociations[mStride*i+LOC_EQU];
91
        }
92

    
93
      mAssociations = tmp;
94
      mStride = stride;
95
      }
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  public boolean matchesAssociation( int comp, int andAssoc, int equAssoc)
101
    {
102
    return (andAssoc & mAssociations[mStride*comp+LOC_AND]) != 0 || (equAssoc == mAssociations[mStride*comp+LOC_EQU]);
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  public void setEffectAssociationNow(int comp, int andAssociation, int equAssociation)
108
    {
109
    mAssociations[mStride*comp+LOC_AND] = andAssociation;
110
    mAssociations[mStride*comp+LOC_EQU] = equAssociation;
111

    
112
    mUBO.invalidate();
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  public int getIndex()
118
    {
119
    return mUBO.createImmediatelyInt( 4*mStride*mMax, mAssociations);
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  public void copy(int compTo, UniformBlockAssociation assocFrom, int compFrom)
125
    {
126
    mAssociations[mStride*compTo+LOC_AND] = assocFrom.mAssociations[mStride*compFrom+LOC_AND];
127
    mAssociations[mStride*compTo+LOC_EQU] = assocFrom.mAssociations[mStride*compFrom+LOC_EQU];
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  public void markForDeletion()
133
    {
134
    mUBO.markForDeletion();
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  public void print()
140
    {
141
    StringBuilder builder = new StringBuilder();
142

    
143
    builder.append(mUBO.getID());
144
    builder.append(' ');
145

    
146
    for(int i=0; i<8; i++)
147
      {
148
      builder.append(mAssociations[mStride*i+LOC_AND]);
149
      builder.append(' ');
150
      builder.append(mAssociations[mStride*i+LOC_EQU]);
151
      builder.append(' ');
152
      }
153

    
154
    String res = builder.toString();
155

    
156
    DistortedLibrary.logMessage("UniformBlockAssociation: "+res);
157
    }
158
  }
(1-1/4)