Project

General

Profile

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

library / src / main / java / org / distorted / library / uniformblock / UniformBlockAssociation.java @ 7958d843

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

    
78
  public void correctStride(int stride)
79
    {
80
    if( mStride != stride && stride!=0 )
81
      {
82
      int[] tmp = new int[stride*mMax];
83

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

    
90
      mAssociations = tmp;
91
      mStride = stride;
92
      }
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

    
109
    mUBO.invalidate();
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  public void markForDeletion()
130
    {
131
    mUBO.markForDeletion();
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  public void print()
137
    {
138
    StringBuilder builder = new StringBuilder();
139

    
140
    builder.append(mUBO.getID());
141
    builder.append(' ');
142

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

    
151
    String res = builder.toString();
152

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