Project

General

Profile

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

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

1 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 f6fb3c6d Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 f6fb3c6d 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 f6fb3c6d 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 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 f6fb3c6d Leszek Koltunski
22 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
23 f6fb3c6d Leszek Koltunski
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25 dedacd82 Leszek Koltunski
/**
26
 * Class which represents a OpenGL Framebuffer object.
27 71c3fecc Leszek Koltunski
 * <p>
28 c5369f1b leszek
 * User is able to create offscreen FBOs and both a) render to them b) use their COLOR0 attachment as
29 2ed1c692 leszek
 * an input texture. Attaching Depths and/or Stencils is also possible.
30 dedacd82 Leszek Koltunski
 */
31 7602a827 Leszek Koltunski
public class DistortedFramebuffer extends InternalOutputSurface
32 f6fb3c6d Leszek Koltunski
  {
33 bd3da5b2 Leszek Koltunski
34 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
35 6537ba91 Leszek Koltunski
// Must be called from a thread holding OpenGL Context
36 f6fb3c6d Leszek Koltunski
37 341151fc Leszek Koltunski
  void create()
38 f6fb3c6d Leszek Koltunski
    {
39 9d845904 Leszek Koltunski
    //////////////////////////////////////////////////////////////
40
    // COLOR
41
42 c7da4e65 leszek
    if( mColorCreated==NOT_CREATED_YET )
43 7cf783cb Leszek Koltunski
      {
44 9d845904 Leszek Koltunski
      GLES31.glGenTextures( mNumFBOs*mNumColors, mColorH, 0);
45
      GLES31.glGenFramebuffers(mNumFBOs, mFBOH, 0);
46 9ed80185 Leszek Koltunski
47 9d845904 Leszek Koltunski
      for(int i=0; i<mNumFBOs; i++)
48 9ed80185 Leszek Koltunski
        {
49 9d845904 Leszek Koltunski
        GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[i]);
50
51
        for(int j=0; j<mNumColors; j++)
52
          {
53
          GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[i*mNumColors+j]);
54
          GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_WRAP_S, GLES31.GL_REPEAT);
55
          GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_WRAP_T, GLES31.GL_REPEAT);
56
          GLES31.glTexParameterf(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MIN_FILTER, GLES31.GL_NEAREST);
57
          GLES31.glTexParameterf(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MAG_FILTER, GLES31.GL_LINEAR);
58
          GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_RGBA, mRealWidth, mRealHeight, 0, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, null);
59
          }
60
61
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[i*mNumColors], 0);
62
        GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
63 9ed80185 Leszek Koltunski
        }
64 dedacd82 Leszek Koltunski
65 9d845904 Leszek Koltunski
      // TODO
66 c7da4e65 leszek
      mColorCreated = checkStatus("color");
67 9d845904 Leszek Koltunski
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
68 7cf783cb Leszek Koltunski
      }
69 9d845904 Leszek Koltunski
70
    //////////////////////////////////////////////////////////////
71
    // DEPTH / STENCIL
72
73 89de975c leszek
    if( mDepthStencilCreated==NOT_CREATED_YET ) // we need to create a new DEPTH or STENCIL attachment
74 8c327653 Leszek Koltunski
      {
75 9d845904 Leszek Koltunski
      GLES31.glGenTextures(mNumFBOs, mDepthStencilH, 0);
76
77
      for(int i=0; i<mNumFBOs; i++)
78 65e83759 Leszek Koltunski
        {
79 9d845904 Leszek Koltunski
        GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mDepthStencilH[i]);
80
        GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_WRAP_S, GLES31.GL_REPEAT);
81
        GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_WRAP_T, GLES31.GL_REPEAT);
82
        GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MIN_FILTER, GLES31.GL_NEAREST);
83
        GLES31.glTexParameteri(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MAG_FILTER, GLES31.GL_NEAREST);
84 89de975c leszek
85 9d845904 Leszek Koltunski
        if (mDepthStencil == DEPTH_NO_STENCIL)
86
          {
87
          GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_DEPTH_COMPONENT, mRealWidth, mRealHeight, 0, GLES31.GL_DEPTH_COMPONENT, GLES31.GL_UNSIGNED_INT, null);
88
          }
89
        else if (mDepthStencil == BOTH_DEPTH_STENCIL)
90
          {
91
          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);
92
          }
93
        }
94 e6519ac8 Leszek Koltunski
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
95 8c327653 Leszek Koltunski
96 9d845904 Leszek Koltunski
      for(int i=0; i<mNumFBOs; i++)
97 65e83759 Leszek Koltunski
        {
98 9d845904 Leszek Koltunski
        GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[i]);
99 89de975c leszek
100 9d845904 Leszek Koltunski
        if (mDepthStencil == DEPTH_NO_STENCIL)
101
          {
102
          GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_ATTACHMENT, GLES31.GL_TEXTURE_2D, mDepthStencilH[i], 0);
103
          }
104
        else if (mDepthStencil == BOTH_DEPTH_STENCIL)
105
          {
106
          GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_STENCIL_ATTACHMENT, GLES31.GL_TEXTURE_2D, mDepthStencilH[i], 0);
107
          }
108
        }
109 89de975c leszek
110 9d845904 Leszek Koltunski
      // TODO
111 89de975c leszek
      mDepthStencilCreated = checkStatus("depth");
112 9d845904 Leszek Koltunski
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
113 8c327653 Leszek Koltunski
      }
114 9d845904 Leszek Koltunski
115
    //////////////////////////////////////////////////////////////
116
    // DETACH
117
118
    // TODO
119 89de975c leszek
    if( mDepthStencilCreated==DONT_CREATE && mDepthStencilH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
120 8c327653 Leszek Koltunski
      {
121 68f96a01 leszek
      // OpenGL ES 3.0.5 spec, chapter 4.4.2.4 :
122
      // "Note that the texture image is specifically not detached from any other framebuffer objects.
123
      //  Detaching the texture image from any other framebuffer objects is the responsibility of the application."
124 75f95f8d leszek
125 9d845904 Leszek Koltunski
      for(int i=0; i<mNumFBOs; i++)
126
        {
127
        GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[i]);
128
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_ATTACHMENT, GLES31.GL_TEXTURE_2D, 0, 0);
129
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_STENCIL_ATTACHMENT, GLES31.GL_TEXTURE_2D, 0, 0);
130
        mDepthStencilH[i]=0;
131
        }
132
133
      GLES31.glDeleteTextures(mNumFBOs, mDepthStencilH, 0);
134
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
135 8c327653 Leszek Koltunski
      }
136 8337fa41 Leszek Koltunski
    }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139 9d845904 Leszek Koltunski
// TODO
140 8337fa41 Leszek Koltunski
141 c7da4e65 leszek
  private int checkStatus(String message)
142 8337fa41 Leszek Koltunski
    {
143 e6519ac8 Leszek Koltunski
    int status = GLES31.glCheckFramebufferStatus(GLES31.GL_FRAMEBUFFER);
144 8337fa41 Leszek Koltunski
145 e6519ac8 Leszek Koltunski
    if(status != GLES31.GL_FRAMEBUFFER_COMPLETE)
146 8337fa41 Leszek Koltunski
      {
147
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
148
149 e6519ac8 Leszek Koltunski
      GLES31.glDeleteTextures(1, mColorH, 0);
150
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
151
      GLES31.glDeleteFramebuffers(1, mFBOH, 0);
152 c7da4e65 leszek
      mFBOH[0]= 0;
153 8337fa41 Leszek Koltunski
154 c7da4e65 leszek
      return FAILED_TO_CREATE;
155 8337fa41 Leszek Koltunski
      }
156 8c327653 Leszek Koltunski
157 c7da4e65 leszek
    return CREATED;
158 f6fb3c6d Leszek Koltunski
    }
159
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161 6537ba91 Leszek Koltunski
// Must be called from a thread holding OpenGL Context
162 f6fb3c6d Leszek Koltunski
163 341151fc Leszek Koltunski
  void delete()
164 f6fb3c6d Leszek Koltunski
    {
165 c7da4e65 leszek
    if( mColorH[0]>0 )
166 e6cf7d50 Leszek Koltunski
      {
167 9d845904 Leszek Koltunski
      GLES31.glDeleteTextures(mNumFBOs*mNumColors, mColorH, 0);
168 c7da4e65 leszek
      mColorCreated = NOT_CREATED_YET;
169 9d845904 Leszek Koltunski
170
      for(int i=0; i<mNumFBOs*mNumColors; i++) mColorH[i] = 0;
171 5b959cc5 Leszek Koltunski
      }
172 bd3da5b2 Leszek Koltunski
173 5b959cc5 Leszek Koltunski
    if( mDepthStencilH[0]>0 )
174
      {
175 9d845904 Leszek Koltunski
      GLES31.glDeleteTextures(mNumFBOs, mDepthStencilH, 0);
176 5b959cc5 Leszek Koltunski
      mDepthStencilCreated = NOT_CREATED_YET;
177 9d845904 Leszek Koltunski
178
      for(int i=0; i<mNumFBOs; i++) mDepthStencilH[i] = 0;
179 e6cf7d50 Leszek Koltunski
      }
180 5b959cc5 Leszek Koltunski
181 9d845904 Leszek Koltunski
    GLES31.glDeleteFramebuffers(mNumFBOs, mFBOH, 0);
182
    for(int i=0; i<mNumFBOs; i++) mFBOH[i] = 0;
183 bd3da5b2 Leszek Koltunski
    }
184
185 4ebbb17a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
186
// called from onDestroy(); mark OpenGL assets as 'not created'
187
188 341151fc Leszek Koltunski
  void recreate()
189 4ebbb17a Leszek Koltunski
    {
190 05ecc6fe Leszek Koltunski
    if( mColorCreated!=DONT_CREATE )
191
      {
192
      mColorCreated = NOT_CREATED_YET;
193
      mColorH[0] = 0;
194
      }
195 89de975c leszek
    if( mDepthStencilCreated!=DONT_CREATE )
196 05ecc6fe Leszek Koltunski
      {
197 89de975c leszek
      mDepthStencilCreated = NOT_CREATED_YET;
198
      mDepthStencilH[0] = 0;
199 05ecc6fe Leszek Koltunski
      }
200 4ebbb17a Leszek Koltunski
    }
201
202 9d845904 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
203
204
  boolean setAsInput(int fbo, int texture)
205
    {
206
    if( texture>=0 && texture<mNumColors && fbo>=0 && fbo<mNumFBOs && mColorH[mNumColors*fbo + texture]>0 )
207
      {
208
      GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
209
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[mNumColors*fbo + texture]);
210
      return true;
211
      }
212
213
    return false;
214
    }
215
216 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
217 fbe9542f Leszek Koltunski
// create a multi-framebuffer (1 object containing multiple FBOs)
218 af27df87 leszek
219 fbe9542f Leszek Koltunski
  DistortedFramebuffer(int numfbos, int numcolors, int depthStencil, int type, int width, int height)
220 af27df87 leszek
    {
221 fbe9542f Leszek Koltunski
    super(width,height,NOT_CREATED_YET,numfbos,numcolors,depthStencil,NOT_CREATED_YET, type);
222
    markForCreation();
223 9d845904 Leszek Koltunski
    }
224
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226 fbe9542f Leszek Koltunski
// create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
227
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
228
// inside a Tree of DistortedNodes (TREE)
229
// SYSTEM surfaces do not get removed in onDestroy().
230 9d845904 Leszek Koltunski
231 fbe9542f Leszek Koltunski
  DistortedFramebuffer(int numcolors, int depthStencil, int type, int width, int height)
232 9d845904 Leszek Koltunski
    {
233 fbe9542f Leszek Koltunski
    this(1,numcolors,depthStencil,type,width,height);
234 af27df87 leszek
    }
235
236 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
237
// PUBLIC API
238 dedacd82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239 cdd5e827 Leszek Koltunski
/**
240 9ed80185 Leszek Koltunski
 * Create new offscreen Framebuffer with configurable number of COLOR, DEPTH and STENCIL attachments.
241 cdd5e827 Leszek Koltunski
 *
242 9ed80185 Leszek Koltunski
 * @param width        Width of all the COLOR attachments.
243
 * @param height       Height of all the COLOR attachments.
244
 * @param numcolors    How many COLOR attachments to create?
245 89de975c leszek
 * @param depthStencil Add DEPTH or STENCIL attachment?
246 23eecbd9 Leszek Koltunski
 *                     Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
247 cdd5e827 Leszek Koltunski
 */
248
  @SuppressWarnings("unused")
249 9ed80185 Leszek Koltunski
  public DistortedFramebuffer(int width, int height, int numcolors, int depthStencil)
250 cdd5e827 Leszek Koltunski
    {
251 fbe9542f Leszek Koltunski
    this(1,numcolors,depthStencil,TYPE_USER,width,height);
252 cdd5e827 Leszek Koltunski
    }
253
254 1dfc9074 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
255
/**
256
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
257
 *
258 5f7e4f2c Leszek Koltunski
 * @param texture The Texture number to bind (and thus read from).
259 1dfc9074 leszek
 * @return <code>true</code> if successful.
260
 */
261
  public boolean setAsInput(int texture)
262
    {
263 5f7e4f2c Leszek Koltunski
    if( texture>=0 && texture<mNumColors && mColorH[texture]>0 )
264 1dfc9074 leszek
      {
265 e6519ac8 Leszek Koltunski
      GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
266 5f7e4f2c Leszek Koltunski
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[2*mCurrFBO+texture]);
267 1dfc9074 leszek
      return true;
268
      }
269
270
    return false;
271
    }
272
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274 5f7e4f2c Leszek Koltunski
  /**
275
   * Attach the texture'th Texture to COLOR0 attachment.
276
   *
277
   * @param texture The Texture number to attach (and subsequently use to render to)
278
   */
279 1dfc9074 leszek
  public void bindForOutput(int texture)
280
    {
281 5f7e4f2c Leszek Koltunski
    if( texture>=0 && texture<mNumColors && mColorH[texture]>0 )
282 1dfc9074 leszek
      {
283 5f7e4f2c Leszek Koltunski
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[2*mCurrFBO+texture], 0);
284 1dfc9074 leszek
      }
285
    }
286
287 89de975c leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
288
/**
289
 * Enable.disable DEPTH and STENCIL buffers.
290
 *
291 23eecbd9 Leszek Koltunski
 * @param depthStencil Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
292 89de975c leszek
 */
293
  public void enableDepthStencil(int depthStencil)
294
    {
295 65e83759 Leszek Koltunski
    if( depthStencil != mDepthStencil )
296 89de975c leszek
      {
297
      mDepthStencil = depthStencil;
298
299 65e83759 Leszek Koltunski
      if( depthStencil!= NO_DEPTH_NO_STENCIL && mDepthStencilCreated==DONT_CREATE )
300 89de975c leszek
        {
301 65e83759 Leszek Koltunski
        mDepthStencilCreated = NOT_CREATED_YET;
302
        markForCreation();
303 89de975c leszek
        }
304 65e83759 Leszek Koltunski
      if( depthStencil== NO_DEPTH_NO_STENCIL && mDepthStencilCreated!=DONT_CREATE )
305
        {
306
        mDepthStencilCreated = DONT_CREATE;
307
        markForCreation();
308
        }
309 89de975c leszek
      }
310
    }
311
312 d1e740c5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
313
/**
314 09d4f4b1 Leszek Koltunski
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
315
 * <p>
316
 * Catch: this will only work if the library has had time to actually create the texture. Remember
317
 * that the texture gets created only on first render, thus creating a Texture object and immediately
318
 * calling this method will return an invalid (negative) result.
319
 *
320 ab12f06b Leszek Koltunski
 * @return If there was not a single render between creation of the Object and calling this method on
321
 *         it, return a negative value. Otherwise, return ID of COLOR attachment 0.
322 d1e740c5 Leszek Koltunski
 */
323 09d4f4b1 Leszek Koltunski
  public int getTextureID()
324 d1e740c5 Leszek Koltunski
    {
325 133cbb2b Leszek Koltunski
    return mColorH[0];
326 8c327653 Leszek Koltunski
    }
327 f6fb3c6d Leszek Koltunski
  }