Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalBuffer.java @ 0bd9f644

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 final int[] mIndex;
42
  private int mTarget, mSize, mUsage;
43
  private Buffer mBuffer;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  public InternalBuffer(int target, int usage)
48
    {
49
    super(InternalObject.TYPE_USER);
50

    
51
    mIndex  = new int[1];
52
    mTarget = target;
53
    mUsage  = usage;
54
    mBuffer = null;
55
    mSize   = 0;
56

    
57
    recreate();
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
// must be called from a thread holding OpenGL Context.
62

    
63
  public int createImmediatelyFloat(int size, float[] buffer)
64
    {
65
    if( mIndex[0]<0 )
66
      {
67
      mSize= size;
68

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

    
80
      GLES30.glGenBuffers( 1, mIndex, 0);
81
      GLES30.glBindBuffer( mTarget, mIndex[0]);
82
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
83
      GLES30.glBindBuffer( mTarget, 0);
84

    
85
      markWasCreatedImmediately();
86
      }
87

    
88
    return mIndex[0];
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
// must be called from a thread holding OpenGL Context.
93

    
94
  public int createImmediatelyInt(int size, int[] buffer)
95
    {
96
    if( mIndex[0]<0 )
97
      {
98
      mSize= size;
99

    
100
      if( buffer!=null )
101
        {
102
        IntBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()).asIntBuffer();
103
        buf.put(buffer).position(0);
104
        mBuffer = buf;
105
        }
106
      else
107
        {
108
        mBuffer = null;
109
        }
110

    
111
      GLES30.glGenBuffers( 1, mIndex, 0);
112
      GLES30.glBindBuffer( mTarget,  mIndex[0]);
113
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
114
      GLES30.glBindBuffer( mTarget,  0);
115

    
116
      markWasCreatedImmediately();
117
      }
118

    
119
    return mIndex[0];
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
// buffer non-null!!
124

    
125
  public void updateFloat(float[] buffer)
126
    {
127
    ((FloatBuffer)mBuffer).put(buffer).position(0);
128

    
129
    GLES30.glBindBuffer( mTarget, mIndex[0]);
130
    GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
131
    GLES30.glBindBuffer( mTarget, 0);
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
// buffer non-null!!
136

    
137
  public void updateInt(int[] buffer)
138
    {
139
    ((IntBuffer)mBuffer).put(buffer).position(0);
140

    
141
    GLES30.glBindBuffer( mTarget, mIndex[0]);
142
    GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
143
    GLES30.glBindBuffer( mTarget, 0);
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  public void invalidate()
149
    {
150
    recreate();
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// must be called from a thread holding OpenGL Context
155

    
156
  void create()
157
    {
158
    if( mIndex[0]<0 )
159
      {
160
      GLES30.glGenBuffers( 1, mIndex, 0);
161
      GLES30.glBindBuffer( mTarget, mIndex[0]);
162
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
163
      GLES30.glBindBuffer( mTarget, 0);
164
      }
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
// must be called from a thread holding OpenGL Context
169

    
170
  void delete()
171
    {
172
    if( mIndex[0]>=0 )
173
      {
174
      GLES30.glDeleteBuffers(1, mIndex, 0);
175
      mIndex[0] = -1;
176
      }
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  void recreate()
182
    {
183
    mIndex[0] = -1;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// debugging only
188

    
189
  String printDetails()
190
    {
191
    return getClass().getSimpleName();
192
    }
193
  }
(7-7/14)