Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalBuffer.java @ b5be333a

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 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
23 466450b5 Leszek Koltunski
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 7e53a35f Leszek Koltunski
  private final int[] mIndex;
40 466450b5 Leszek Koltunski
  private int mTarget, mSize, mUsage;
41
  private Buffer mBuffer;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 7602a827 Leszek Koltunski
  public InternalBuffer(int target, int usage)
46 466450b5 Leszek Koltunski
    {
47 7602a827 Leszek Koltunski
    super(InternalObject.TYPE_USER);
48 466450b5 Leszek Koltunski
49
    mIndex  = new int[1];
50
    mTarget = target;
51
    mUsage  = usage;
52 ccd98f1c Leszek Koltunski
    mBuffer = null;
53
    mSize   = 0;
54 466450b5 Leszek Koltunski
55
    recreate();
56
    }
57
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59 22e60fba Leszek Koltunski
// must be called from a thread holding OpenGL Context.
60 466450b5 Leszek Koltunski
61 7e53a35f Leszek Koltunski
  public int createImmediately(int size, float[] buffer)
62 466450b5 Leszek Koltunski
    {
63 22e60fba Leszek Koltunski
    if( mIndex[0]<0 )
64
      {
65
      mSize= size;
66 ccd98f1c Leszek Koltunski
67 22e60fba Leszek Koltunski
      if( buffer!=null )
68
        {
69
        FloatBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()).asFloatBuffer();
70
        buf.put(buffer).position(0);
71
        mBuffer = buf;
72
        }
73
      else
74
        {
75
        mBuffer = null;
76
        }
77
78 b7074bc6 Leszek Koltunski
      GLES30.glGenBuffers( 1, mIndex, 0);
79
      GLES30.glBindBuffer( mTarget, mIndex[0]);
80
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
81
      GLES30.glBindBuffer( mTarget, 0);
82 22e60fba Leszek Koltunski
83
      markWasCreatedImmediately();
84
      }
85 7e53a35f Leszek Koltunski
86
    return mIndex[0];
87 22e60fba Leszek Koltunski
    }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91
  public void invalidate()
92
    {
93
    recreate();
94 466450b5 Leszek Koltunski
    }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// must be called from a thread holding OpenGL Context
98
99
  void create()
100
    {
101
    if( mIndex[0]<0 )
102
      {
103 b7074bc6 Leszek Koltunski
      GLES30.glGenBuffers( 1, mIndex, 0);
104
      GLES30.glBindBuffer( mTarget, mIndex[0]);
105
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
106
      GLES30.glBindBuffer( mTarget, 0);
107 466450b5 Leszek Koltunski
      }
108
    }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
// must be called from a thread holding OpenGL Context
112
113
  void delete()
114
    {
115
    if( mIndex[0]>=0 )
116
      {
117 b7074bc6 Leszek Koltunski
      GLES30.glDeleteBuffers(1, mIndex, 0);
118 466450b5 Leszek Koltunski
      mIndex[0] = -1;
119
      }
120
    }
121
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
  void recreate()
125
    {
126
    mIndex[0] = -1;
127
    }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// debugging only
131
132
  String printDetails()
133
    {
134
    return getClass().getSimpleName();
135
    }
136
  }