Project

General

Profile

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

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

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

    
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
public class InternalBuffer extends InternalObject
38
  {
39
  private final int[] mIndex;
40
  private int mTarget, mSize, mUsage;
41
  private Buffer mBuffer;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

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

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

    
55
    recreate();
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  public int getIndex()
61
    {
62
    return mIndex[0];
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
// must be called from a thread holding OpenGL Context.
67

    
68
  public int createImmediately(int size, float[] buffer)
69
    {
70
    if( mIndex[0]<0 )
71
      {
72
      mSize= size;
73

    
74
      if( buffer!=null )
75
        {
76
        FloatBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()).asFloatBuffer();
77
        buf.put(buffer).position(0);
78
        mBuffer = buf;
79
        }
80
      else
81
        {
82
        mBuffer = null;
83
        }
84

    
85
      GLES30.glGenBuffers( 1, mIndex, 0);
86
      GLES30.glBindBuffer( mTarget, mIndex[0]);
87
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
88
      GLES30.glBindBuffer( mTarget, 0);
89

    
90
      markWasCreatedImmediately();
91
      }
92

    
93
    return mIndex[0];
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  public void invalidate()
99
    {
100
    recreate();
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
// must be called from a thread holding OpenGL Context
105

    
106
  void create()
107
    {
108
    if( mIndex[0]<0 )
109
      {
110
      GLES30.glGenBuffers( 1, mIndex, 0);
111
      GLES30.glBindBuffer( mTarget, mIndex[0]);
112
      GLES30.glBufferData( mTarget, mSize, mBuffer, mUsage);
113
      GLES30.glBindBuffer( mTarget, 0);
114
      }
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// must be called from a thread holding OpenGL Context
119

    
120
  void delete()
121
    {
122
    if( mIndex[0]>=0 )
123
      {
124
      GLES30.glDeleteBuffers(1, mIndex, 0);
125
      mIndex[0] = -1;
126
      }
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  void recreate()
132
    {
133
    mIndex[0] = -1;
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// debugging only
138

    
139
  String printDetails()
140
    {
141
    return getClass().getSimpleName();
142
    }
143
  }
(7-7/14)