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
|
// must be called from a thread holding OpenGL Context
|
175
|
|
176
|
void create()
|
177
|
{
|
178
|
GLES30.glGenBuffers( 1, mIndex, 0);
|
179
|
GLES30.glBindBuffer( mTarget, mIndex[0]);
|
180
|
GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
|
181
|
GLES30.glBindBuffer( mTarget, 0);
|
182
|
|
183
|
mStatus = DONE;
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
// must be called from a thread holding OpenGL Context
|
188
|
|
189
|
void delete()
|
190
|
{
|
191
|
GLES30.glDeleteBuffers(1, mIndex, 0);
|
192
|
mStatus |= RECREATE;
|
193
|
removeFromDone();
|
194
|
}
|
195
|
|
196
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
197
|
|
198
|
public void recreate()
|
199
|
{
|
200
|
mStatus |= RECREATE;
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
// debugging only
|
205
|
|
206
|
String printDetails()
|
207
|
{
|
208
|
return getClass().getSimpleName();
|
209
|
}
|
210
|
}
|