Project

General

Profile

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

library / src / main / java / org / distorted / library / uniformblock / UniformBlockAssociation.java @ 80961fc1

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

    
26
import static org.distorted.library.mesh.MeshBase.MAX_EFFECT_COMPONENTS;
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 = 2;
38
  private static final int LOC_AND = 0;
39
  private static final int LOC_EQU = 1;
40

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  public UniformBlockAssociation()
48
    {
49
    mStride = DEFAULT_STRIDE;
50
    mAssociations= new int[mStride*MAX_EFFECT_COMPONENTS];
51

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

    
58
    mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_READ);
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
70
    mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_READ);
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  public void correctStride(int stride)
76
    {
77
    if( mStride != stride )
78
      {
79
      int[] tmp = new int[stride*MAX_EFFECT_COMPONENTS];
80

    
81
      for(int i=0; i<MAX_EFFECT_COMPONENTS; i++)
82
        {
83
        tmp[stride*i+LOC_AND] = mAssociations[mStride*i+LOC_AND];
84
        tmp[stride*i+LOC_EQU] = mAssociations[mStride*i+LOC_EQU];
85
        }
86

    
87
      mAssociations = tmp;
88
      mStride = stride;
89
      }
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

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

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  public void setEffectAssociationNow(int comp, int andAssociation, int equAssociation)
102
    {
103
    mAssociations[mStride*comp+LOC_AND] = andAssociation;
104
    mAssociations[mStride*comp+LOC_EQU] = equAssociation;
105

    
106
    mUBO.invalidate();
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  public int getIndex()
112
    {
113
    return mUBO.createImmediatelyInt( 4*mStride*MAX_EFFECT_COMPONENTS, mAssociations);
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  public void markForDeletion()
127
    {
128
    mUBO.markForDeletion();
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  public void print()
134
    {
135
    StringBuilder builder = new StringBuilder();
136

    
137
    builder.append(mUBO.getID());
138
    builder.append(' ');
139

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

    
148
    String res = builder.toString();
149

    
150
    android.util.Log.e("uba", res);
151
    }
152
  }
(1-1/4)