Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedFramebuffer.java @ 5f35f1cb

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