Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalBuffer.java @ 22e60fba

1 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 466450b5 Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 466450b5 Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// Distorted 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
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 466450b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.library.main;
21
22
import android.opengl.GLES31;
23
import java.nio.Buffer;
24 22e60fba Leszek Koltunski
import java.nio.ByteBuffer;
25
import java.nio.ByteOrder;
26
import java.nio.FloatBuffer;
27 466450b5 Leszek Koltunski
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * Implements OpenGL buffer object such as GL_ARRAY_BUFFER or GL_TRANSFORM_FEEDBACK_BUFFER.
31
 * Main point: deal with Android lifecycle and recreate the buffer on loss of OpenGL context.
32
 * <p>
33
 * Not part of public API, do not document (public only because has to be used in Meshes)
34
 *
35
 * @y.exclude
36
 */
37 7602a827 Leszek Koltunski
public class InternalBuffer extends InternalObject
38 466450b5 Leszek Koltunski
  {
39 22e60fba Leszek Koltunski
  public final int[] mIndex;
40 466450b5 Leszek Koltunski
41
  private int mTarget, mSize, mUsage;
42
  private Buffer mBuffer;
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 7602a827 Leszek Koltunski
  public InternalBuffer(int target, int usage)
47 466450b5 Leszek Koltunski
    {
48 7602a827 Leszek Koltunski
    super(InternalObject.TYPE_USER);
49 466450b5 Leszek Koltunski
50
    mIndex  = new int[1];
51
    mTarget = target;
52
    mUsage  = usage;
53 ccd98f1c Leszek Koltunski
    mBuffer = null;
54
    mSize   = 0;
55 466450b5 Leszek Koltunski
56
    recreate();
57
    }
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60 22e60fba Leszek Koltunski
// must be called from a thread holding OpenGL Context.
61 466450b5 Leszek Koltunski
62 22e60fba Leszek Koltunski
  public void createImmediately(int size, float[] buffer)
63 466450b5 Leszek Koltunski
    {
64 22e60fba Leszek Koltunski
    if( mIndex[0]<0 )
65
      {
66
      mSize= size;
67 ccd98f1c Leszek Koltunski
68 22e60fba Leszek Koltunski
      if( buffer!=null )
69
        {
70
        FloatBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()).asFloatBuffer();
71
        buf.put(buffer).position(0);
72
        mBuffer = buf;
73
        }
74
      else
75
        {
76
        mBuffer = null;
77
        }
78
79
      GLES31.glGenBuffers( 1, mIndex, 0);
80
      GLES31.glBindBuffer( mTarget, mIndex[0]);
81
      GLES31.glBufferData( mTarget, mSize, mBuffer, mUsage);
82
      GLES31.glBindBuffer( mTarget, 0);
83
84
      markWasCreatedImmediately();
85
      }
86
    }
87
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90
  public void invalidate()
91
    {
92
    recreate();
93 466450b5 Leszek Koltunski
    }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// must be called from a thread holding OpenGL Context
97
98
  void create()
99
    {
100
    if( mIndex[0]<0 )
101
      {
102
      GLES31.glGenBuffers( 1, mIndex, 0);
103
      GLES31.glBindBuffer( mTarget, mIndex[0]);
104
      GLES31.glBufferData( mTarget, mSize, mBuffer, mUsage);
105
      GLES31.glBindBuffer( mTarget, 0);
106
      }
107
    }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
// must be called from a thread holding OpenGL Context
111
112
  void delete()
113
    {
114
    if( mIndex[0]>=0 )
115
      {
116
      GLES31.glDeleteBuffers(1, mIndex, 0);
117
      mIndex[0] = -1;
118
      }
119
    }
120
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
123
  void recreate()
124
    {
125
    mIndex[0] = -1;
126
    }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
// debugging only
130
131
  String printDetails()
132
    {
133
    return getClass().getSimpleName();
134
    }
135
  }