Project

General

Profile

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

library / src / main / java / org / distorted / library / uniformblock / UniformBlockAssociation.java @ d23592bb

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.InternalBuffer;
26
import org.distorted.library.mesh.MeshBase;
27

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

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

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

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

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

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

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

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

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

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

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

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

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

    
111
    mUBO.invalidate();
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

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

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

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

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

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

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

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

    
153
    String res = builder.toString();
154

    
155
    android.util.Log.e("uba", res);
156
    }
157
  }
(1-1/4)