Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedFramebuffer.java @ a4b182d4

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

    
36
  void prepareDebug(long time) {}
37
  void renderDebug(long time)  {}
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
// Must be called from a thread holding OpenGL Context
41

    
42
  void createSurface()
43
    {
44
    if( mColorCreated==NOT_CREATED_YET )
45
      {
46
      GLES31.glGenTextures( mNumColors, mColorH, 0);
47
      GLES31.glGenFramebuffers(1, mFBOH, 0);
48
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
49

    
50
      for(int i=0; i<mNumColors; i++)
51
        {
52
        GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[i]);
53
        GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_WRAP_S, GLES31.GL_REPEAT);
54
        GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_WRAP_T, GLES31.GL_REPEAT);
55
        GLES31.glTexParameterf(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MIN_FILTER, GLES31.GL_NEAREST);
56
        GLES31.glTexParameterf(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MAG_FILTER, GLES31.GL_LINEAR);
57
        GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_RGBA, mRealWidth, mRealHeight, 0, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, null);
58
        }
59
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[0], 0);
60
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
61
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
62

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

    
74
      if( mDepthStencil==DEPTH_NO_STENCIL )
75
        {
76
        GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_DEPTH_COMPONENT, mRealWidth, mRealHeight, 0, GLES31.GL_DEPTH_COMPONENT, GLES31.GL_UNSIGNED_INT, null);
77
        }
78
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
79
        {
80
        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);
81
        }
82

    
83
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
84
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
85

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

    
95
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
96

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

    
109
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
110
      mDepthStencilH[0]=0;
111
      }
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private int checkStatus(String message)
117
    {
118
    int status = GLES31.glCheckFramebufferStatus(GLES31.GL_FRAMEBUFFER);
119

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

    
124
      GLES31.glDeleteTextures(1, mColorH, 0);
125
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
126
      GLES31.glDeleteFramebuffers(1, mFBOH, 0);
127
      mFBOH[0]= 0;
128

    
129
      return FAILED_TO_CREATE;
130
      }
131

    
132
    return CREATED;
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
// Must be called from a thread holding OpenGL Context
137

    
138
  void deleteSurface()
139
    {
140
    if( mColorH[0]>0 )
141
      {
142
      GLES31.glDeleteTextures(1, mColorH, 0);
143
      mColorH[0] = 0;
144
      mColorCreated = NOT_CREATED_YET;
145
      }
146

    
147
    if( mDepthStencilH[0]>0 )
148
      {
149
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
150
      mDepthStencilH[0]=0;
151
      mDepthStencilCreated = NOT_CREATED_YET;
152
      }
153

    
154
    GLES31.glDeleteFramebuffers(1, mFBOH, 0);
155
    mFBOH[0] = 0;
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
// called from onDestroy(); mark OpenGL assets as 'not created'
160

    
161
  void recreateSurface()
162
    {
163
    if( mColorCreated!=DONT_CREATE )
164
      {
165
      mColorCreated = NOT_CREATED_YET;
166
      mColorH[0] = 0;
167
      }
168
    if( mDepthStencilCreated!=DONT_CREATE )
169
      {
170
      mDepthStencilCreated = NOT_CREATED_YET;
171
      mDepthStencilH[0] = 0;
172
      }
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
// create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
177
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
178
// inside a Tree of DistortedNodes (TREE)
179
// SYSTEM surfaces do not get removed in onDestroy().
180

    
181
  DistortedFramebuffer(int numcolors, int depthStencil, int type, int width, int height)
182
    {
183
    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET, type);
184
    }
185

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

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

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

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

    
233
    return false;
234
    }
235

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

    
251
    return false;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

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

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

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