Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalBuffer.java @ 246d021c

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
import java.nio.ByteBuffer;
24
import java.nio.ByteOrder;
25
import java.nio.FloatBuffer;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
/**
29
 * Implements OpenGL buffer object such as GL_ARRAY_BUFFER or GL_TRANSFORM_FEEDBACK_BUFFER.
30
 * Main point: deal with Android lifecycle and recreate the buffer on loss of OpenGL context.
31
 * <p>
32
 * Not part of public API, do not document (public only because has to be used in Meshes)
33
 *
34
 * @y.exclude
35
 */
36
public class InternalBuffer extends InternalObject
37
  {
38
  private final int[] mIndex;
39
  private int mTarget, mSize, mUsage;
40
  private FloatBuffer mBuffer;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  public InternalBuffer(int target, int usage)
45
    {
46
    super(InternalObject.TYPE_USER);
47

    
48
    mIndex  = new int[1];
49
    mTarget = target;
50
    mUsage  = usage;
51
    mBuffer = null;
52
    mSize   = 0;
53

    
54
    recreate();
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
// must be called from a thread holding OpenGL Context.
59

    
60
  public int createImmediately(int size, float[] buffer)
61
    {
62
    if( mIndex[0]<0 )
63
      {
64
      mSize= size;
65

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

    
77
      GLES30.glGenBuffers( 1, mIndex, 0);
78
      GLES30.glBindBuffer( mTarget, mIndex[0]);
79
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
80
      GLES30.glBindBuffer( mTarget, 0);
81

    
82
      markWasCreatedImmediately();
83
      }
84

    
85
    return mIndex[0];
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
// buffer non-null!!
90

    
91
  public void update(float[] buffer)
92
    {
93
    mBuffer.put(buffer).position(0);
94

    
95
    GLES30.glBindBuffer( mTarget, mIndex[0]);
96
    GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
97
    GLES30.glBindBuffer( mTarget, 0);
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  public void invalidate()
103
    {
104
    recreate();
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
// must be called from a thread holding OpenGL Context
109

    
110
  void create()
111
    {
112
    if( mIndex[0]<0 )
113
      {
114
      GLES30.glGenBuffers( 1, mIndex, 0);
115
      GLES30.glBindBuffer( mTarget, mIndex[0]);
116
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
117
      GLES30.glBindBuffer( mTarget, 0);
118
      }
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
// must be called from a thread holding OpenGL Context
123

    
124
  void delete()
125
    {
126
    if( mIndex[0]>=0 )
127
      {
128
      GLES30.glDeleteBuffers(1, mIndex, 0);
129
      mIndex[0] = -1;
130
      }
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  void recreate()
136
    {
137
    mIndex[0] = -1;
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
// debugging only
142

    
143
  String printDetails()
144
    {
145
    return getClass().getSimpleName();
146
    }
147
  }
(7-7/14)