Project

General

Profile

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

library / src / main / java / org / distorted / library / uniformblock / UniformBlockAssociation.java @ 073e5a7a

1 97755c02 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 de77a6c5 Leszek Koltunski
package org.distorted.library.uniformblock;
21 97755c02 Leszek Koltunski
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 de77a6c5 Leszek Koltunski
/**
30
 * Not part of public API, do not document
31
 *
32
 * @y.exclude
33
 */
34
public class UniformBlockAssociation
35 97755c02 Leszek Koltunski
  {
36
  private static final int DEFAULT_ASSOCIATION = 0xffffffff;
37 073e5a7a Leszek Koltunski
  private static final int DEFAULT_STRIDE = 4;
38 80961fc1 Leszek Koltunski
  private static final int LOC_AND = 0;
39
  private static final int LOC_EQU = 1;
40 97755c02 Leszek Koltunski
41 78ff6ea9 Leszek Koltunski
  private final InternalBuffer mUBO;
42 80961fc1 Leszek Koltunski
  private int[] mAssociations;
43
  private int mStride;
44 97755c02 Leszek Koltunski
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46 41b3ada0 Leszek Koltunski
47 de77a6c5 Leszek Koltunski
  public UniformBlockAssociation()
48 97755c02 Leszek Koltunski
    {
49 80961fc1 Leszek Koltunski
    mStride = DEFAULT_STRIDE;
50
    mAssociations= new int[mStride*MAX_EFFECT_COMPONENTS];
51 97755c02 Leszek Koltunski
52
    for(int i=0; i<MAX_EFFECT_COMPONENTS; i++)
53
      {
54 80961fc1 Leszek Koltunski
      mAssociations[mStride*i+LOC_AND] = DEFAULT_ASSOCIATION;
55
      mAssociations[mStride*i+LOC_EQU] = i;
56 97755c02 Leszek Koltunski
      }
57
58
    mUBO = new InternalBuffer(GLES30.GL_UNIFORM_BUFFER, GLES30.GL_STATIC_READ);
59
    }
60
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 de77a6c5 Leszek Koltunski
  public UniformBlockAssociation(UniformBlockAssociation original)
64 97755c02 Leszek Koltunski
    {
65 80961fc1 Leszek Koltunski
    mStride = original.mStride;
66 97755c02 Leszek Koltunski
    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 80961fc1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 97755c02 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94 de77a6c5 Leszek Koltunski
  public boolean matchesAssociation( int comp, int andAssoc, int equAssoc)
95 728a7820 Leszek Koltunski
    {
96 80961fc1 Leszek Koltunski
    return (andAssoc & mAssociations[mStride*comp+LOC_AND]) != 0 || (equAssoc == mAssociations[mStride*comp+LOC_EQU]);
97 728a7820 Leszek Koltunski
    }
98 97755c02 Leszek Koltunski
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101 de77a6c5 Leszek Koltunski
  public void setEffectAssociationNow(int comp, int andAssociation, int equAssociation)
102 728a7820 Leszek Koltunski
    {
103 80961fc1 Leszek Koltunski
    mAssociations[mStride*comp+LOC_AND] = andAssociation;
104
    mAssociations[mStride*comp+LOC_EQU] = equAssociation;
105 97755c02 Leszek Koltunski
106 728a7820 Leszek Koltunski
    mUBO.invalidate();
107
    }
108 97755c02 Leszek Koltunski
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111 de77a6c5 Leszek Koltunski
  public int getIndex()
112 728a7820 Leszek Koltunski
    {
113 80961fc1 Leszek Koltunski
    return mUBO.createImmediatelyInt( 4*mStride*MAX_EFFECT_COMPONENTS, mAssociations);
114 728a7820 Leszek Koltunski
    }
115 97755c02 Leszek Koltunski
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118 de77a6c5 Leszek Koltunski
  public void copy(int compTo, UniformBlockAssociation assocFrom, int compFrom)
119 728a7820 Leszek Koltunski
    {
120 80961fc1 Leszek Koltunski
    mAssociations[mStride*compTo+LOC_AND] = assocFrom.mAssociations[mStride*compFrom+LOC_AND];
121
    mAssociations[mStride*compTo+LOC_EQU] = assocFrom.mAssociations[mStride*compFrom+LOC_EQU];
122 728a7820 Leszek Koltunski
    }
123
124 8b36dabf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126 de77a6c5 Leszek Koltunski
  public void markForDeletion()
127 8b36dabf Leszek Koltunski
    {
128
    mUBO.markForDeletion();
129
    }
130
131 728a7820 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
132
133 de77a6c5 Leszek Koltunski
  public void print()
134 728a7820 Leszek Koltunski
    {
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 80961fc1 Leszek Koltunski
      builder.append(mAssociations[mStride*i+LOC_AND]);
143 728a7820 Leszek Koltunski
      builder.append(' ');
144 80961fc1 Leszek Koltunski
      builder.append(mAssociations[mStride*i+LOC_EQU]);
145 728a7820 Leszek Koltunski
      builder.append(' ');
146
      }
147
148
    String res = builder.toString();
149
150
    android.util.Log.e("uba", res);
151
    }
152 97755c02 Leszek Koltunski
  }