Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 9ed80185

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
        GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0+i, GLES30.GL_TEXTURE_2D, mColorH[i], 0);
54
        }
55

    
56
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
57
      GLES30.glBindFramebuffer(GLES30.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
      GLES30.glGenTextures(1, mDepthStencilH, 0);
64
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[0]);
65
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
66
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
67
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
68
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST);
69

    
70
      if( mDepthStencil==DEPTH_NO_STENCIL )
71
        {
72
        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mWidth, mHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_INT, null);
73
        }
74
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
75
        {
76
        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);
77
        }
78

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

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

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

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

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

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

    
117
      return FAILED_TO_CREATE;
118
      }
119

    
120
    return CREATED;
121
    }
122

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

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

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

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

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

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

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

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

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

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

    
186
    return false;
187
    }
188

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

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

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

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

    
236
    return false;
237
    }
238

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

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

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