Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalBuffer.java @ b0ac5b29

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 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.main;
21

    
22
import android.opengl.GLES30;
23

    
24
import java.nio.Buffer;
25
import java.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27
import java.nio.FloatBuffer;
28
import java.nio.IntBuffer;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
/**
32
 * Implements OpenGL buffer object such as GL_ARRAY_BUFFER or GL_TRANSFORM_FEEDBACK_BUFFER.
33
 * Main point: deal with Android lifecycle and recreate the buffer on loss of OpenGL context.
34
 * <p>
35
 * Not part of public API, do not document (public only because has to be used in Meshes)
36
 *
37
 * @y.exclude
38
 */
39
public class InternalBuffer extends InternalObject
40
  {
41
  private static final int DONE     = 0;
42
  private static final int RECREATE = 1;
43
  private static final int UPDATE   = 2;
44

    
45
  private int mStatus, mSize;
46
  private final int[] mIndex;
47
  private final int mTarget, mUsage;
48
  private Buffer mBuffer;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  public InternalBuffer(int target, int usage)
53
    {
54
    super(InternalObject.TYPE_USER, InternalObject.STORAGE_PRIVATE );
55

    
56
    mIndex  = new int[1];
57
    mTarget = target;
58
    mUsage  = usage;
59
    mBuffer = null;
60
    mSize   = 0;
61
    mStatus = RECREATE;
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
// must be called from a thread holding OpenGL Context.
66

    
67
  public int createImmediatelyFloat(int size, float[] buffer)
68
    {
69
    if( (mStatus & RECREATE) != 0 )
70
      {
71
      mSize= size;
72

    
73
      if( buffer!=null )
74
        {
75
        FloatBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()).asFloatBuffer();
76
        buf.put(buffer).position(0);
77
        mBuffer = buf;
78
        }
79
      else
80
        {
81
        mBuffer = null;
82
        }
83

    
84
      GLES30.glGenBuffers( 1, mIndex, 0);
85
      GLES30.glBindBuffer( mTarget, mIndex[0]);
86
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
87
      GLES30.glBindBuffer( mTarget, 0);
88

    
89
      markWasCreatedImmediately();
90
      }
91
    else if( (mStatus & UPDATE) != 0 )
92
      {
93
      updateFloat(buffer);
94
      }
95

    
96
    mStatus = DONE;
97

    
98
    return mIndex[0];
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
// must be called from a thread holding OpenGL Context.
103

    
104
  public int createImmediatelyInt(int size, int[] buffer)
105
    {
106
    if( (mStatus & RECREATE) != 0 )
107
      {
108
      mSize= size;
109

    
110
      if( buffer!=null )
111
        {
112
        IntBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()).asIntBuffer();
113
        buf.put(buffer).position(0);
114
        mBuffer = buf;
115
        }
116
      else
117
        {
118
        mBuffer = null;
119
        }
120

    
121
      GLES30.glGenBuffers( 1, mIndex, 0);
122
      GLES30.glBindBuffer( mTarget,  mIndex[0]);
123
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
124
      GLES30.glBindBuffer( mTarget,  0);
125

    
126
      markWasCreatedImmediately();
127
      }
128
    else if( (mStatus & UPDATE) != 0  )
129
      {
130
      updateInt(buffer);
131
      }
132

    
133
    mStatus = DONE;
134

    
135
    return mIndex[0];
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
// buffer non-null!!
140

    
141
  public void updateFloat(float[] buffer)
142
    {
143
    ((FloatBuffer)mBuffer).put(buffer).position(0);
144

    
145
    GLES30.glBindBuffer( mTarget, mIndex[0]);
146
    GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
147
    GLES30.glBindBuffer( mTarget, 0);
148

    
149
    mStatus &= (~UPDATE);
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
// buffer non-null!!
154

    
155
  public void updateInt(int[] buffer)
156
    {
157
    ((IntBuffer)mBuffer).put(buffer).position(0);
158

    
159
    GLES30.glBindBuffer( mTarget, mIndex[0]);
160
    GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
161
    GLES30.glBindBuffer( mTarget, 0);
162

    
163
    mStatus &= (~UPDATE);
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  public void invalidate()
169
    {
170
    mStatus |= UPDATE;
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
// Intentionally empty, no need to do anything here since it will be done in createImmediatelyXXX().
175
// In fact, recreating a Mesh's mVBO1 here - rather than in createImmediatelyFloat - was the reason
176
// of the 'disappearing cube after the mesh has changed from nice to simple' bug. I don't quite
177
// understand why TBH.
178

    
179
  void create()
180
    {
181

    
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// must be called from a thread holding OpenGL Context
186

    
187
  void delete()
188
    {
189
    GLES30.glDeleteBuffers(1, mIndex, 0);
190
    mStatus |= RECREATE;
191
    removeFromDone();
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  public void recreate()
197
    {
198
    mStatus |= RECREATE;
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
// debugging only
203

    
204
  String printDetails()
205
    {
206
    return getClass().getSimpleName();
207
    }
208
  }
(7-7/17)