Project

General

Profile

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

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

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