Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalBuffer.java @ 728a7820

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 RECREATE = 1;
42
  private static final int UPDATE   = 2;
43

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

    
55
    mIndex  = new int[1];
56
    mTarget = target;
57
    mUsage  = usage;
58
    mBuffer = null;
59
    mSize   = 0;
60

    
61
    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 = 0;
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 = 0;
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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// buffer non-null!!
152

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

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

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  public void invalidate()
165
    {
166
    mStatus |= UPDATE;
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
// must be called from a thread holding OpenGL Context
171

    
172
  void create()
173
    {
174
    GLES30.glGenBuffers( 1, mIndex, 0);
175
    GLES30.glBindBuffer( mTarget, mIndex[0]);
176
    GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
177
    GLES30.glBindBuffer( mTarget, 0);
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
// must be called from a thread holding OpenGL Context
182

    
183
  void delete()
184
    {
185
    GLES30.glDeleteBuffers(1, mIndex, 0);
186
    mStatus |= RECREATE;
187
    removeFromDone();
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  public void recreate()
193
    {
194
    mStatus |= RECREATE;
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
// debugging only
199

    
200
  String printDetails()
201
    {
202
    return getClass().getSimpleName();
203
    }
204
  }
(7-7/16)