1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2018 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.main;
|
22
|
|
23
|
import android.opengl.GLES30;
|
24
|
|
25
|
import java.nio.Buffer;
|
26
|
import java.nio.ByteBuffer;
|
27
|
import java.nio.ByteOrder;
|
28
|
import java.nio.FloatBuffer;
|
29
|
import java.nio.IntBuffer;
|
30
|
|
31
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32
|
/**
|
33
|
* Implements OpenGL buffer object such as GL_ARRAY_BUFFER or GL_TRANSFORM_FEEDBACK_BUFFER.
|
34
|
* Main point: deal with Android lifecycle and recreate the buffer on loss of OpenGL context.
|
35
|
* <p>
|
36
|
* Not part of public API, do not document (public only because has to be used in Meshes)
|
37
|
*
|
38
|
* @y.exclude
|
39
|
*/
|
40
|
public class InternalBuffer extends InternalObject
|
41
|
{
|
42
|
private static final int DONE = 0;
|
43
|
private static final int RECREATE = 1;
|
44
|
private static final int UPDATE = 2;
|
45
|
|
46
|
private int mStatus, mSize;
|
47
|
private final int[] mIndex;
|
48
|
private final int mTarget, mUsage;
|
49
|
private Buffer mBuffer;
|
50
|
|
51
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52
|
|
53
|
public InternalBuffer(int target, int usage)
|
54
|
{
|
55
|
super(InternalObject.TYPE_USER, InternalObject.STORAGE_PRIVATE );
|
56
|
|
57
|
mIndex = new int[1];
|
58
|
mTarget = target;
|
59
|
mUsage = usage;
|
60
|
mBuffer = null;
|
61
|
mSize = 0;
|
62
|
mStatus = RECREATE;
|
63
|
}
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
// must be called from a thread holding OpenGL Context.
|
67
|
|
68
|
public int createImmediatelyFloat(int size, float[] buffer)
|
69
|
{
|
70
|
if( (mStatus & RECREATE) != 0 )
|
71
|
{
|
72
|
mSize= size;
|
73
|
|
74
|
if( buffer!=null )
|
75
|
{
|
76
|
FloatBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
77
|
buf.put(buffer).position(0);
|
78
|
mBuffer = buf;
|
79
|
}
|
80
|
else
|
81
|
{
|
82
|
mBuffer = null;
|
83
|
}
|
84
|
|
85
|
GLES30.glGenBuffers( 1, mIndex, 0);
|
86
|
GLES30.glBindBuffer( mTarget, mIndex[0]);
|
87
|
GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
|
88
|
GLES30.glBindBuffer( mTarget, 0);
|
89
|
|
90
|
markWasCreatedImmediately();
|
91
|
}
|
92
|
else if( (mStatus & UPDATE) != 0 )
|
93
|
{
|
94
|
updateFloat(buffer);
|
95
|
}
|
96
|
|
97
|
mStatus = DONE;
|
98
|
|
99
|
return mIndex[0];
|
100
|
}
|
101
|
|
102
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103
|
// must be called from a thread holding OpenGL Context.
|
104
|
|
105
|
public int createImmediatelyInt(int size, int[] buffer)
|
106
|
{
|
107
|
if( (mStatus & RECREATE) != 0 )
|
108
|
{
|
109
|
mSize= size;
|
110
|
|
111
|
if( buffer!=null )
|
112
|
{
|
113
|
IntBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()).asIntBuffer();
|
114
|
buf.put(buffer).position(0);
|
115
|
mBuffer = buf;
|
116
|
}
|
117
|
else
|
118
|
{
|
119
|
mBuffer = null;
|
120
|
}
|
121
|
|
122
|
GLES30.glGenBuffers( 1, mIndex, 0);
|
123
|
GLES30.glBindBuffer( mTarget, mIndex[0]);
|
124
|
GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
|
125
|
GLES30.glBindBuffer( mTarget, 0);
|
126
|
|
127
|
markWasCreatedImmediately();
|
128
|
}
|
129
|
else if( (mStatus & UPDATE) != 0 )
|
130
|
{
|
131
|
updateInt(buffer);
|
132
|
}
|
133
|
|
134
|
mStatus = DONE;
|
135
|
|
136
|
return mIndex[0];
|
137
|
}
|
138
|
|
139
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
140
|
// buffer non-null!!
|
141
|
|
142
|
public void updateFloat(float[] buffer)
|
143
|
{
|
144
|
((FloatBuffer)mBuffer).put(buffer).position(0);
|
145
|
|
146
|
GLES30.glBindBuffer( mTarget, mIndex[0]);
|
147
|
GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
|
148
|
GLES30.glBindBuffer( mTarget, 0);
|
149
|
|
150
|
mStatus &= (~UPDATE);
|
151
|
}
|
152
|
|
153
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
154
|
// buffer non-null!!
|
155
|
|
156
|
public void updateInt(int[] buffer)
|
157
|
{
|
158
|
((IntBuffer)mBuffer).put(buffer).position(0);
|
159
|
|
160
|
GLES30.glBindBuffer( mTarget, mIndex[0]);
|
161
|
GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
|
162
|
GLES30.glBindBuffer( mTarget, 0);
|
163
|
|
164
|
mStatus &= (~UPDATE);
|
165
|
}
|
166
|
|
167
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168
|
|
169
|
public void invalidate()
|
170
|
{
|
171
|
mStatus |= UPDATE;
|
172
|
}
|
173
|
|
174
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175
|
// Intentionally empty, no need to do anything here since it will be done in createImmediatelyXXX().
|
176
|
// In fact, recreating a Mesh's mVBO1 here - rather than in createImmediatelyFloat - was the reason
|
177
|
// of the 'disappearing cube after the mesh has changed from nice to simple' bug. I don't quite
|
178
|
// understand why TBH.
|
179
|
|
180
|
void create()
|
181
|
{
|
182
|
|
183
|
}
|
184
|
|
185
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186
|
// must be called from a thread holding OpenGL Context
|
187
|
|
188
|
void delete()
|
189
|
{
|
190
|
GLES30.glDeleteBuffers(1, mIndex, 0);
|
191
|
mStatus |= RECREATE;
|
192
|
removeFromDone();
|
193
|
}
|
194
|
|
195
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
196
|
|
197
|
public void recreate()
|
198
|
{
|
199
|
mStatus |= RECREATE;
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
// debugging only
|
204
|
|
205
|
String printDetails()
|
206
|
{
|
207
|
return getClass().getSimpleName();
|
208
|
}
|
209
|
}
|