Project

General

Profile

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

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

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
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
/**
27
 * Implements OpenGL buffer object such as GL_ARRAY_BUFFER or GL_TRANSFORM_FEEDBACK_BUFFER.
28
 * Main point: deal with Android lifecycle and recreate the buffer on loss of OpenGL context.
29
 * <p>
30
 * Not part of public API, do not document (public only because has to be used in Meshes)
31
 *
32
 * @y.exclude
33
 */
34 7602a827 Leszek Koltunski
public class InternalBuffer extends InternalObject
35 466450b5 Leszek Koltunski
  {
36 6c00149d Leszek Koltunski
  public int[] mIndex;
37 466450b5 Leszek Koltunski
38
  private int mTarget, mSize, mUsage;
39
  private Buffer mBuffer;
40
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43 7602a827 Leszek Koltunski
  public InternalBuffer(int target, int usage)
44 466450b5 Leszek Koltunski
    {
45 7602a827 Leszek Koltunski
    super(InternalObject.TYPE_USER);
46 466450b5 Leszek Koltunski
47
    mIndex  = new int[1];
48
    mTarget = target;
49
    mUsage  = usage;
50 ccd98f1c Leszek Koltunski
    mBuffer = null;
51
    mSize   = 0;
52 466450b5 Leszek Koltunski
53
    recreate();
54
    }
55
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57 ccd98f1c Leszek Koltunski
// mark for creation only now, when we do have all data ready, in order to avoid the situation
58
// when create() would be called before this.
59 466450b5 Leszek Koltunski
60 6c00149d Leszek Koltunski
  public void setData(int size, Buffer buffer)
61 466450b5 Leszek Koltunski
    {
62
    mSize   = size;
63
    mBuffer = buffer;
64 ccd98f1c Leszek Koltunski
65
    markForCreation();
66 466450b5 Leszek Koltunski
    }
67
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
// must be called from a thread holding OpenGL Context
70
71
  void create()
72
    {
73
    if( mIndex[0]<0 )
74
      {
75
      GLES31.glGenBuffers( 1, mIndex, 0);
76
      GLES31.glBindBuffer( mTarget, mIndex[0]);
77
      GLES31.glBufferData( mTarget, mSize, mBuffer, mUsage);
78
      GLES31.glBindBuffer( mTarget, 0);
79
      }
80
    }
81
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
// must be called from a thread holding OpenGL Context
84
85
  void delete()
86
    {
87
    if( mIndex[0]>=0 )
88
      {
89
      GLES31.glDeleteBuffers(1, mIndex, 0);
90
      mIndex[0] = -1;
91
      }
92
    }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96
  void recreate()
97
    {
98
    mIndex[0] = -1;
99
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
// debugging only
103
104
  String printDetails()
105
    {
106
    return getClass().getSimpleName();
107
    }
108
  }