Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalBuffer.java @ 178983f4

1 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 2e569ff6 Leszek Koltunski
// Copyright 2018 Leszek Koltunski  leszek@koltunski.pl                                          //
3 466450b5 Leszek Koltunski
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 466450b5 Leszek Koltunski
//                                                                                               //
6 2e569ff6 Leszek Koltunski
// 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 466450b5 Leszek Koltunski
//                                                                                               //
11 2e569ff6 Leszek Koltunski
// This library is distributed in the hope that it will be useful,                               //
12 466450b5 Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13 2e569ff6 Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 466450b5 Leszek Koltunski
//                                                                                               //
16 2e569ff6 Leszek Koltunski
// 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 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21
package org.distorted.library.main;
22
23 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
24 0bd9f644 Leszek Koltunski
25
import java.nio.Buffer;
26 22e60fba Leszek Koltunski
import java.nio.ByteBuffer;
27
import java.nio.ByteOrder;
28
import java.nio.FloatBuffer;
29 0bd9f644 Leszek Koltunski
import java.nio.IntBuffer;
30 466450b5 Leszek Koltunski
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 7602a827 Leszek Koltunski
public class InternalBuffer extends InternalObject
41 466450b5 Leszek Koltunski
  {
42 43814a57 Leszek Koltunski
  private static final int DONE     = 0;
43 728a7820 Leszek Koltunski
  private static final int RECREATE = 1;
44
  private static final int UPDATE   = 2;
45
46 de77a6c5 Leszek Koltunski
  private int mStatus, mSize;
47 7e53a35f Leszek Koltunski
  private final int[] mIndex;
48 de77a6c5 Leszek Koltunski
  private final int mTarget, mUsage;
49 0bd9f644 Leszek Koltunski
  private Buffer mBuffer;
50 466450b5 Leszek Koltunski
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 7602a827 Leszek Koltunski
  public InternalBuffer(int target, int usage)
54 466450b5 Leszek Koltunski
    {
55 9ec374e8 Leszek Koltunski
    super(InternalObject.TYPE_USER, InternalObject.STORAGE_PRIVATE );
56 466450b5 Leszek Koltunski
57
    mIndex  = new int[1];
58
    mTarget = target;
59
    mUsage  = usage;
60 ccd98f1c Leszek Koltunski
    mBuffer = null;
61
    mSize   = 0;
62 43814a57 Leszek Koltunski
    mStatus = RECREATE;
63 466450b5 Leszek Koltunski
    }
64
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66 22e60fba Leszek Koltunski
// must be called from a thread holding OpenGL Context.
67 466450b5 Leszek Koltunski
68 0bd9f644 Leszek Koltunski
  public int createImmediatelyFloat(int size, float[] buffer)
69 466450b5 Leszek Koltunski
    {
70 728a7820 Leszek Koltunski
    if( (mStatus & RECREATE) != 0 )
71 22e60fba Leszek Koltunski
      {
72
      mSize= size;
73 ccd98f1c Leszek Koltunski
74 22e60fba Leszek Koltunski
      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 b7074bc6 Leszek Koltunski
      GLES30.glGenBuffers( 1, mIndex, 0);
86
      GLES30.glBindBuffer( mTarget, mIndex[0]);
87
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
88
      GLES30.glBindBuffer( mTarget, 0);
89 22e60fba Leszek Koltunski
90
      markWasCreatedImmediately();
91
      }
92 728a7820 Leszek Koltunski
    else if( (mStatus & UPDATE) != 0 )
93
      {
94
      updateFloat(buffer);
95
      }
96
97 43814a57 Leszek Koltunski
    mStatus = DONE;
98 7e53a35f Leszek Koltunski
99
    return mIndex[0];
100 22e60fba Leszek Koltunski
    }
101
102 0bd9f644 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
103
// must be called from a thread holding OpenGL Context.
104
105
  public int createImmediatelyInt(int size, int[] buffer)
106
    {
107 728a7820 Leszek Koltunski
    if( (mStatus & RECREATE) != 0 )
108 0bd9f644 Leszek Koltunski
      {
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 728a7820 Leszek Koltunski
    else if( (mStatus & UPDATE) != 0  )
130
      {
131
      updateInt(buffer);
132
      }
133
134 43814a57 Leszek Koltunski
    mStatus = DONE;
135 0bd9f644 Leszek Koltunski
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 43814a57 Leszek Koltunski
150
    mStatus &= (~UPDATE);
151 0bd9f644 Leszek Koltunski
    }
152
153 342a6773 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// buffer non-null!!
155
156 0bd9f644 Leszek Koltunski
  public void updateInt(int[] buffer)
157 342a6773 Leszek Koltunski
    {
158 0bd9f644 Leszek Koltunski
    ((IntBuffer)mBuffer).put(buffer).position(0);
159 342a6773 Leszek Koltunski
160
    GLES30.glBindBuffer( mTarget, mIndex[0]);
161
    GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
162
    GLES30.glBindBuffer( mTarget, 0);
163 43814a57 Leszek Koltunski
164
    mStatus &= (~UPDATE);
165 342a6773 Leszek Koltunski
    }
166
167 22e60fba Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168
169
  public void invalidate()
170
    {
171 728a7820 Leszek Koltunski
    mStatus |= UPDATE;
172 466450b5 Leszek Koltunski
    }
173
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175 b0ac5b29 Leszek Koltunski
// 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 466450b5 Leszek Koltunski
180
  void create()
181
    {
182 43814a57 Leszek Koltunski
183 466450b5 Leszek Koltunski
    }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
// must be called from a thread holding OpenGL Context
187
188
  void delete()
189
    {
190 728a7820 Leszek Koltunski
    GLES30.glDeleteBuffers(1, mIndex, 0);
191
    mStatus |= RECREATE;
192
    removeFromDone();
193 466450b5 Leszek Koltunski
    }
194
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
197 728a7820 Leszek Koltunski
  public void recreate()
198 466450b5 Leszek Koltunski
    {
199 728a7820 Leszek Koltunski
    mStatus |= RECREATE;
200 466450b5 Leszek Koltunski
    }
201
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
// debugging only
204
205
  String printDetails()
206
    {
207
    return getClass().getSimpleName();
208
    }
209
  }