Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedFramebuffer.java @ 2dbed690

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.main;
21

    
22
import android.opengl.GLES31;
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
      GLES31.glGenTextures( mNumColors, mColorH, 0);
42

    
43
      for(int i=0; i<mNumColors; i++)
44
        {
45
        GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[i]);
46
        GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_WRAP_S, GLES31.GL_REPEAT);
47
        GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_WRAP_T, GLES31.GL_REPEAT);
48
        GLES31.glTexParameterf(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MIN_FILTER, GLES31.GL_NEAREST);
49
        GLES31.glTexParameterf(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MAG_FILTER, GLES31.GL_LINEAR);
50
        GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_RGBA, mRealWidth, mRealHeight, 0, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, null);
51
        }
52
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
53

    
54
      GLES31.glGenFramebuffers(1, mFBOH, 0);
55
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
56
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[0], 0);
57
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
58

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

    
71
      if( mDepthStencil==DEPTH_NO_STENCIL )
72
        {
73
        GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_DEPTH_COMPONENT, mRealWidth, mRealHeight, 0, GLES31.GL_DEPTH_COMPONENT, GLES31.GL_UNSIGNED_INT, null);
74
        }
75
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
76
        {
77
        GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_DEPTH24_STENCIL8, mRealWidth, mRealHeight, 0, GLES31.GL_DEPTH_STENCIL, GLES31.GL_UNSIGNED_INT_24_8, null);
78
        }
79

    
80
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
81

    
82
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
83

    
84
      if( mDepthStencil==DEPTH_NO_STENCIL )
85
        {
86
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_ATTACHMENT, GLES31.GL_TEXTURE_2D, mDepthStencilH[0], 0);
87
        }
88
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
89
        {
90
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_STENCIL_ATTACHMENT, GLES31.GL_TEXTURE_2D, mDepthStencilH[0], 0);
91
        }
92

    
93
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
94

    
95
      mDepthStencilCreated = checkStatus("depth");
96
      }
97
    if( mDepthStencilCreated==DONT_CREATE && mDepthStencilH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
98
      {
99
      // OpenGL ES 3.0.5 spec, chapter 4.4.2.4 :
100
      // "Note that the texture image is specifically not detached from any other framebuffer objects.
101
      //  Detaching the texture image from any other framebuffer objects is the responsibility of the application."
102
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
103
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_ATTACHMENT        , GLES31.GL_TEXTURE_2D, 0, 0);
104
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_STENCIL_ATTACHMENT, GLES31.GL_TEXTURE_2D, 0, 0);
105
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
106

    
107
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
108
      mDepthStencilH[0]=0;
109
      }
110
    if( mDepthStencilCreated==COPIED && mDepthStencilH[0]>0 )
111
      {
112
      mDepthStencilCreated = DONT_CREATE;
113
      mDepthStencilH[0]=0;
114
      }
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  private int checkStatus(String message)
120
    {
121
    int status = GLES31.glCheckFramebufferStatus(GLES31.GL_FRAMEBUFFER);
122

    
123
    if(status != GLES31.GL_FRAMEBUFFER_COMPLETE)
124
      {
125
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
126

    
127
      GLES31.glDeleteTextures(1, mColorH, 0);
128
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
129
      GLES31.glDeleteFramebuffers(1, mFBOH, 0);
130
      mFBOH[0]= 0;
131

    
132
      return FAILED_TO_CREATE;
133
      }
134

    
135
    return CREATED;
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
// the Framebuffer was created without DEPTH or STENCIL but now we want to copy it from some other.
140
// Framebuffer 'from' MUST be already created!!
141
// Must be called from a thread holding OpenGL Context
142
//
143
// 'Private' , library-only function used only for the postprocessing buffers!
144

    
145
  void copyDepthAndStencil(DistortedFramebuffer from)
146
    {
147
    mDepthStencilCreated = COPIED;
148
    mDepthStencilH[0] = from.mDepthStencilH[0];
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
// Must be called from a thread holding OpenGL Context
153

    
154
  void delete()
155
    {
156
    if( mColorH[0]>0 )
157
      {
158
      GLES31.glDeleteTextures(1, mColorH, 0);
159
      mColorH[0] = 0;
160
      mColorCreated = NOT_CREATED_YET;
161
      }
162

    
163
    if( mDepthStencilH[0]>0 )
164
      {
165
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
166
      mDepthStencilH[0]=0;
167
      mDepthStencilCreated = NOT_CREATED_YET;
168
      }
169

    
170
    GLES31.glDeleteFramebuffers(1, mFBOH, 0);
171
    mFBOH[0] = 0;
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
// called from onDestroy(); mark OpenGL assets as 'not created'
176

    
177
  void recreate()
178
    {
179
    if( mColorCreated!=DONT_CREATE )
180
      {
181
      mColorCreated = NOT_CREATED_YET;
182
      mColorH[0] = 0;
183
      }
184
    if( mDepthStencilCreated!=DONT_CREATE )
185
      {
186
      mDepthStencilCreated = NOT_CREATED_YET;
187
      mDepthStencilH[0] = 0;
188
      }
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
// create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
193
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
194
// inside a Tree of DistortedNodes (TREE)
195
// SYSTEM surfaces do not get removed in onDestroy().
196

    
197
  DistortedFramebuffer(int numcolors, int depthStencil, int type, int width, int height)
198
    {
199
    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET, type);
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
// PUBLIC API
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
/**
206
 * Create new offscreen Framebuffer with configurable number of COLOR, DEPTH and STENCIL attachments.
207
 *
208
 * @param width        Width of all the COLOR attachments.
209
 * @param height       Height of all the COLOR attachments.
210
 * @param numcolors    How many COLOR attachments to create?
211
 * @param depthStencil Add DEPTH or STENCIL attachment?
212
 *                     Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
213
 */
214
  @SuppressWarnings("unused")
215
  public DistortedFramebuffer(int width, int height, int numcolors, int depthStencil)
216
    {
217
    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,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
      GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
231
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[0]);
232
      return true;
233
      }
234

    
235
    return false;
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
/**
240
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
241
 *
242
 * @return <code>true</code> if successful.
243
 */
244
  public boolean setAsInput(int texture)
245
    {
246
    if( texture>=0 && texture<mNumColors && mColorH[texture]>0 )
247
      {
248
      GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
249
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[texture]);
250
      return true;
251
      }
252

    
253
    return false;
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  public void bindForOutput(int texture)
259
    {
260
    if( texture>=0 && texture<mNumColors && mColorH[texture]>0 )
261
      {
262
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[texture], 0);
263
      }
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
/**
268
 * Enable.disable DEPTH and STENCIL buffers.
269
 *
270
 * @param depthStencil Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
271
 */
272
  public void enableDepthStencil(int depthStencil)
273
    {
274
    if( depthStencil != mDepthStencil )
275
      {
276
      mDepthStencil = depthStencil;
277

    
278
      if( depthStencil!= NO_DEPTH_NO_STENCIL && mDepthStencilCreated==DONT_CREATE )
279
        {
280
        mDepthStencilCreated = NOT_CREATED_YET;
281
        markForCreation();
282
        }
283
      if( depthStencil== NO_DEPTH_NO_STENCIL && mDepthStencilCreated!=DONT_CREATE )
284
        {
285
        mDepthStencilCreated = DONT_CREATE;
286
        markForCreation();
287
        }
288
      }
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
/**
293
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
294
 * <p>
295
 * Catch: this will only work if the library has had time to actually create the texture. Remember
296
 * that the texture gets created only on first render, thus creating a Texture object and immediately
297
 * calling this method will return an invalid (negative) result.
298
 *
299
 * @return If there was not a single render between creation of the Object and calling this method on
300
 *         it, return a negative value. Otherwise, return ID of COLOR attachment 0.
301
 */
302
  public int getTextureID()
303
    {
304
    return mColorH[0];
305
    }
306
  }
(3-3/21)