Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 0f011027

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 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;
21

    
22
import android.opengl.GLES30;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * Class which represents a OpenGL Framebuffer object.
27
 * <p>
28
 * User is able to create offscreen FBOs and both a) render to them b) use their COLOR0 attachment as
29
 * an input texture. Attaching Depths and/or Stencils is also possible.
30
 */
31
public class DistortedFramebuffer extends DistortedOutputSurface implements DistortedInputSurface
32
  {
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
// Must be called from a thread holding OpenGL Context
36

    
37
  void create()
38
    {
39
    if( mColorCreated==NOT_CREATED_YET )
40
      {
41
      GLES30.glGenTextures( mNumColors, mColorH, 0);
42
      GLES30.glGenFramebuffers(1, mFBOH, 0);
43
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
44

    
45
      for(int i=0; i<mNumColors; i++)
46
        {
47
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[i]);
48
        GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
49
        GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
50
        GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
51
        GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
52
        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mWidth, mHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
53
        }
54
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
55
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
56
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
57

    
58
      mColorCreated = checkStatus("color");
59
      }
60
    if( mDepthStencilCreated==NOT_CREATED_YET ) // we need to create a new DEPTH or STENCIL attachment
61
      {
62
      GLES30.glGenTextures(1, mDepthStencilH, 0);
63
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[0]);
64
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
65
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
66
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
67
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST);
68

    
69
      if( mDepthStencil==DEPTH_NO_STENCIL )
70
        {
71
        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mWidth, mHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_INT, null);
72
        }
73
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
74
        {
75
        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH24_STENCIL8, mWidth, mHeight, 0, GLES30.GL_DEPTH_STENCIL, GLES30.GL_UNSIGNED_INT_24_8, null);
76
        }
77

    
78
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
79
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
80

    
81
      if( mDepthStencil==DEPTH_NO_STENCIL )
82
        {
83
        GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthStencilH[0], 0);
84
        }
85
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
86
        {
87
        GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_STENCIL_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthStencilH[0], 0);
88
        }
89

    
90
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
91

    
92
      mDepthStencilCreated = checkStatus("depth");
93
      }
94
    if( mDepthStencilCreated==DONT_CREATE && mDepthStencilH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
95
      {
96
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
97
      mDepthStencilH[0]=0;
98
      }
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  private int checkStatus(String message)
104
    {
105
    int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
106

    
107
    if(status != GLES30.GL_FRAMEBUFFER_COMPLETE)
108
      {
109
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
110

    
111
      GLES30.glDeleteTextures(1, mColorH, 0);
112
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
113
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
114
      mFBOH[0]= 0;
115

    
116
      return FAILED_TO_CREATE;
117
      }
118

    
119
    return CREATED;
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
// Must be called from a thread holding OpenGL Context
124

    
125
  void delete()
126
    {
127
    if( mColorH[0]>0 )
128
      {
129
      GLES30.glDeleteTextures(1, mColorH, 0);
130
      mColorH[0] = 0;
131
      mColorCreated = NOT_CREATED_YET;
132
      }
133

    
134
    if( mDepthStencilH[0]>0 )
135
      {
136
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
137
      mDepthStencilH[0]=0;
138
      mDepthStencilCreated = NOT_CREATED_YET;
139
      }
140

    
141
    GLES30.glDeleteFramebuffers(1, mFBOH, 0);
142
    mFBOH[0] = 0;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
// called from onDestroy(); mark OpenGL assets as 'not created'
147

    
148
  void recreate()
149
    {
150
    if( mColorCreated!=DONT_CREATE )
151
      {
152
      mColorCreated = NOT_CREATED_YET;
153
      mColorH[0] = 0;
154
      }
155
    if( mDepthStencilCreated!=DONT_CREATE )
156
      {
157
      mDepthStencilCreated = NOT_CREATED_YET;
158
      mDepthStencilH[0] = 0;
159
      }
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
// create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
164
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
165
// inside a Tree of DistortedNodes (TREE)
166
// SYSTEM surfaces do not get removed in onDestroy().
167

    
168
  DistortedFramebuffer(int numcolors, int depthStencil, int type, int width, int height)
169
    {
170
    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET, type);
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
// For setting the Depth texture as input to fragment shaders that merge many planes. (currently: blur2)
175

    
176
  boolean setAsDepth()
177
    {
178
    if( mDepthStencilH[0]>0 )
179
      {
180
      GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
181
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[0]);
182
      return true;
183
      }
184

    
185
    return false;
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
// PUBLIC API
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
/**
192
 * Create new offscreen Framebuffer with configurable number of COLOR, DEPTH and STENCIL attachments.
193
 *
194
 * @param width        Width of all the COLOR attachments.
195
 * @param height       Height of all the COLOR attachments.
196
 * @param numcolors    How many COLOR attachments to create?
197
 * @param depthStencil Add DEPTH or STENCIL attachment?
198
 *                     Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
199
 */
200
  @SuppressWarnings("unused")
201
  public DistortedFramebuffer(int width, int height, int numcolors, int depthStencil)
202
    {
203
    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET,TYPE_USER);
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
/**
209
 * Create new offscreen Framebuffer with COLOR0 attachment only.
210
 *
211
 * @param width Width of the COLOR0 attachment.
212
 * @param height Height of the COLOR0 attachment.
213
 */
214
  @SuppressWarnings("unused")
215
  public DistortedFramebuffer(int width, int height)
216
    {
217
    super(width,height,NOT_CREATED_YET, 1, NO_DEPTH_NO_STENCIL,NOT_CREATED_YET,TYPE_USER);
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
/**
222
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
223
 *
224
 * @return <code>true</code> if successful.
225
 */
226
  public boolean setAsInput()
227
    {
228
    if( mColorH[0]>0 )
229
      {
230
      GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
231
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
232
      return true;
233
      }
234

    
235
    return false;
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
/**
240
 * Enable.disable DEPTH and STENCIL buffers.
241
 *
242
 * @param depthStencil Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
243
 */
244
  public void enableDepthStencil(int depthStencil)
245
    {
246
    if( depthStencil != mDepthStencil )
247
      {
248
      mDepthStencil = depthStencil;
249

    
250
      if( depthStencil!= NO_DEPTH_NO_STENCIL && mDepthStencilCreated==DONT_CREATE )
251
        {
252
        mDepthStencilCreated = NOT_CREATED_YET;
253
        markForCreation();
254
        }
255
      if( depthStencil== NO_DEPTH_NO_STENCIL && mDepthStencilCreated!=DONT_CREATE )
256
        {
257
        mDepthStencilCreated = DONT_CREATE;
258
        markForCreation();
259
        }
260
      }
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264
/**
265
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
266
 * <p>
267
 * Catch: this will only work if the library has had time to actually create the texture. Remember
268
 * that the texture gets created only on first render, thus creating a Texture object and immediately
269
 * calling this method will return an invalid (negative) result.
270
 *
271
 * @return If there was not a single render between creation of the Object and calling this method on
272
 *         it, return a negative value. Otherwise, return ID of COLOR attachment 0.
273
 */
274
  public int getTextureID()
275
    {
276
    return mColorH[0];
277
    }
278
  }
(4-4/26)