Project

General

Profile

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

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

1 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 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 af4cc5db Leszek Koltunski
public class DistortedFramebuffer extends DistortedOutputSurface implements DistortedInputSurface
32 f6fb3c6d Leszek Koltunski
  {
33 bd3da5b2 Leszek Koltunski
34 86e99907 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36
  void prepareDebug(long time) {}
37
  void renderDebug(long time)  {}
38
39 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
40 6537ba91 Leszek Koltunski
// Must be called from a thread holding OpenGL Context
41 f6fb3c6d Leszek Koltunski
42 12f45260 Leszek Koltunski
  void createSurface()
43 f6fb3c6d Leszek Koltunski
    {
44 c7da4e65 leszek
    if( mColorCreated==NOT_CREATED_YET )
45 7cf783cb Leszek Koltunski
      {
46 e6519ac8 Leszek Koltunski
      GLES31.glGenTextures( mNumColors, mColorH, 0);
47
      GLES31.glGenFramebuffers(1, mFBOH, 0);
48
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
49 9ed80185 Leszek Koltunski
50
      for(int i=0; i<mNumColors; i++)
51
        {
52 e6519ac8 Leszek Koltunski
        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 a4b182d4 Leszek Koltunski
        GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_RGBA, mRealWidth, mRealHeight, 0, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, null);
58 9ed80185 Leszek Koltunski
        }
59 e6519ac8 Leszek Koltunski
      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 dedacd82 Leszek Koltunski
63 c7da4e65 leszek
      mColorCreated = checkStatus("color");
64 7cf783cb Leszek Koltunski
      }
65 89de975c leszek
    if( mDepthStencilCreated==NOT_CREATED_YET ) // we need to create a new DEPTH or STENCIL attachment
66 8c327653 Leszek Koltunski
      {
67 e6519ac8 Leszek Koltunski
      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 8c327653 Leszek Koltunski
74 89de975c leszek
      if( mDepthStencil==DEPTH_NO_STENCIL )
75 65e83759 Leszek Koltunski
        {
76 a4b182d4 Leszek Koltunski
        GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_DEPTH_COMPONENT, mRealWidth, mRealHeight, 0, GLES31.GL_DEPTH_COMPONENT, GLES31.GL_UNSIGNED_INT, null);
77 65e83759 Leszek Koltunski
        }
78 89de975c leszek
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
79 65e83759 Leszek Koltunski
        {
80 a4b182d4 Leszek Koltunski
        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 65e83759 Leszek Koltunski
        }
82 89de975c leszek
83 e6519ac8 Leszek Koltunski
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
84
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
85 8c327653 Leszek Koltunski
86 89de975c leszek
      if( mDepthStencil==DEPTH_NO_STENCIL )
87 65e83759 Leszek Koltunski
        {
88 e6519ac8 Leszek Koltunski
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_ATTACHMENT, GLES31.GL_TEXTURE_2D, mDepthStencilH[0], 0);
89 65e83759 Leszek Koltunski
        }
90 89de975c leszek
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
91 65e83759 Leszek Koltunski
        {
92 e6519ac8 Leszek Koltunski
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_DEPTH_STENCIL_ATTACHMENT, GLES31.GL_TEXTURE_2D, mDepthStencilH[0], 0);
93 65e83759 Leszek Koltunski
        }
94 89de975c leszek
95 e6519ac8 Leszek Koltunski
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
96 89de975c leszek
97
      mDepthStencilCreated = checkStatus("depth");
98 8c327653 Leszek Koltunski
      }
99 89de975c leszek
    if( mDepthStencilCreated==DONT_CREATE && mDepthStencilH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
100 8c327653 Leszek Koltunski
      {
101 68f96a01 leszek
      // 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 e6519ac8 Leszek Koltunski
      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 75f95f8d leszek
109 e6519ac8 Leszek Koltunski
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
110 89de975c leszek
      mDepthStencilH[0]=0;
111 8c327653 Leszek Koltunski
      }
112 8337fa41 Leszek Koltunski
    }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116 c7da4e65 leszek
  private int checkStatus(String message)
117 8337fa41 Leszek Koltunski
    {
118 e6519ac8 Leszek Koltunski
    int status = GLES31.glCheckFramebufferStatus(GLES31.GL_FRAMEBUFFER);
119 8337fa41 Leszek Koltunski
120 e6519ac8 Leszek Koltunski
    if(status != GLES31.GL_FRAMEBUFFER_COMPLETE)
121 8337fa41 Leszek Koltunski
      {
122
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
123
124 e6519ac8 Leszek Koltunski
      GLES31.glDeleteTextures(1, mColorH, 0);
125
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
126
      GLES31.glDeleteFramebuffers(1, mFBOH, 0);
127 c7da4e65 leszek
      mFBOH[0]= 0;
128 8337fa41 Leszek Koltunski
129 c7da4e65 leszek
      return FAILED_TO_CREATE;
130 8337fa41 Leszek Koltunski
      }
131 8c327653 Leszek Koltunski
132 c7da4e65 leszek
    return CREATED;
133 f6fb3c6d Leszek Koltunski
    }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136 6537ba91 Leszek Koltunski
// Must be called from a thread holding OpenGL Context
137 f6fb3c6d Leszek Koltunski
138 12f45260 Leszek Koltunski
  void deleteSurface()
139 f6fb3c6d Leszek Koltunski
    {
140 c7da4e65 leszek
    if( mColorH[0]>0 )
141 e6cf7d50 Leszek Koltunski
      {
142 e6519ac8 Leszek Koltunski
      GLES31.glDeleteTextures(1, mColorH, 0);
143 c7da4e65 leszek
      mColorH[0] = 0;
144
      mColorCreated = NOT_CREATED_YET;
145 5b959cc5 Leszek Koltunski
      }
146 bd3da5b2 Leszek Koltunski
147 5b959cc5 Leszek Koltunski
    if( mDepthStencilH[0]>0 )
148
      {
149 e6519ac8 Leszek Koltunski
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
150 5b959cc5 Leszek Koltunski
      mDepthStencilH[0]=0;
151
      mDepthStencilCreated = NOT_CREATED_YET;
152 e6cf7d50 Leszek Koltunski
      }
153 5b959cc5 Leszek Koltunski
154 e6519ac8 Leszek Koltunski
    GLES31.glDeleteFramebuffers(1, mFBOH, 0);
155 5b959cc5 Leszek Koltunski
    mFBOH[0] = 0;
156 bd3da5b2 Leszek Koltunski
    }
157
158 4ebbb17a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
159
// called from onDestroy(); mark OpenGL assets as 'not created'
160
161 12f45260 Leszek Koltunski
  void recreateSurface()
162 4ebbb17a Leszek Koltunski
    {
163 05ecc6fe Leszek Koltunski
    if( mColorCreated!=DONT_CREATE )
164
      {
165
      mColorCreated = NOT_CREATED_YET;
166
      mColorH[0] = 0;
167
      }
168 89de975c leszek
    if( mDepthStencilCreated!=DONT_CREATE )
169 05ecc6fe Leszek Koltunski
      {
170 89de975c leszek
      mDepthStencilCreated = NOT_CREATED_YET;
171
      mDepthStencilH[0] = 0;
172 05ecc6fe Leszek Koltunski
      }
173 4ebbb17a Leszek Koltunski
    }
174
175 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
176 09ab7524 Leszek Koltunski
// 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 af27df87 leszek
181 9ed80185 Leszek Koltunski
  DistortedFramebuffer(int numcolors, int depthStencil, int type, int width, int height)
182 af27df87 leszek
    {
183 9ed80185 Leszek Koltunski
    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET, type);
184 af27df87 leszek
    }
185
186 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// PUBLIC API
188 dedacd82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
189 cdd5e827 Leszek Koltunski
/**
190 9ed80185 Leszek Koltunski
 * Create new offscreen Framebuffer with configurable number of COLOR, DEPTH and STENCIL attachments.
191 cdd5e827 Leszek Koltunski
 *
192 9ed80185 Leszek Koltunski
 * @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 89de975c leszek
 * @param depthStencil Add DEPTH or STENCIL attachment?
196 23eecbd9 Leszek Koltunski
 *                     Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
197 cdd5e827 Leszek Koltunski
 */
198
  @SuppressWarnings("unused")
199 9ed80185 Leszek Koltunski
  public DistortedFramebuffer(int width, int height, int numcolors, int depthStencil)
200 cdd5e827 Leszek Koltunski
    {
201 9ed80185 Leszek Koltunski
    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET,TYPE_USER);
202 cdd5e827 Leszek Koltunski
    }
203
204 133cbb2b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
205
206 dedacd82 Leszek Koltunski
/**
207 9ed80185 Leszek Koltunski
 * Create new offscreen Framebuffer with COLOR0 attachment only.
208 dedacd82 Leszek Koltunski
 *
209 9ed80185 Leszek Koltunski
 * @param width Width of the COLOR0 attachment.
210
 * @param height Height of the COLOR0 attachment.
211 dedacd82 Leszek Koltunski
 */
212 1942537e Leszek Koltunski
  @SuppressWarnings("unused")
213 ed13a5de Leszek Koltunski
  public DistortedFramebuffer(int width, int height)
214 f6fb3c6d Leszek Koltunski
    {
215 9ed80185 Leszek Koltunski
    super(width,height,NOT_CREATED_YET, 1, NO_DEPTH_NO_STENCIL,NOT_CREATED_YET,TYPE_USER);
216 f6fb3c6d Leszek Koltunski
    }
217
218 b448e6b9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
219 dedacd82 Leszek Koltunski
/**
220 c5369f1b leszek
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
221 af4cc5db Leszek Koltunski
 *
222 a436ccc5 leszek
 * @return <code>true</code> if successful.
223 dedacd82 Leszek Koltunski
 */
224 c5369f1b leszek
  public boolean setAsInput()
225 b448e6b9 Leszek Koltunski
    {
226 c5369f1b leszek
    if( mColorH[0]>0 )
227
      {
228 e6519ac8 Leszek Koltunski
      GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
229
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[0]);
230 c5369f1b leszek
      return true;
231
      }
232
233
    return false;
234
    }
235 2e49718d Leszek Koltunski
236 1dfc9074 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e6519ac8 Leszek Koltunski
      GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
247
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mColorH[texture]);
248 1dfc9074 leszek
      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 e6519ac8 Leszek Koltunski
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[texture], 0);
261 1dfc9074 leszek
      }
262
    }
263
264 89de975c leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
265
/**
266
 * Enable.disable DEPTH and STENCIL buffers.
267
 *
268 23eecbd9 Leszek Koltunski
 * @param depthStencil Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
269 89de975c leszek
 */
270
  public void enableDepthStencil(int depthStencil)
271
    {
272 65e83759 Leszek Koltunski
    if( depthStencil != mDepthStencil )
273 89de975c leszek
      {
274
      mDepthStencil = depthStencil;
275
276 65e83759 Leszek Koltunski
      if( depthStencil!= NO_DEPTH_NO_STENCIL && mDepthStencilCreated==DONT_CREATE )
277 89de975c leszek
        {
278 65e83759 Leszek Koltunski
        mDepthStencilCreated = NOT_CREATED_YET;
279
        markForCreation();
280 89de975c leszek
        }
281 65e83759 Leszek Koltunski
      if( depthStencil== NO_DEPTH_NO_STENCIL && mDepthStencilCreated!=DONT_CREATE )
282
        {
283
        mDepthStencilCreated = DONT_CREATE;
284
        markForCreation();
285
        }
286 89de975c leszek
      }
287
    }
288
289 d1e740c5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
290
/**
291 09d4f4b1 Leszek Koltunski
 * 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 ab12f06b Leszek Koltunski
 * @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 d1e740c5 Leszek Koltunski
 */
300 09d4f4b1 Leszek Koltunski
  public int getTextureID()
301 d1e740c5 Leszek Koltunski
    {
302 133cbb2b Leszek Koltunski
    return mColorH[0];
303 8c327653 Leszek Koltunski
    }
304 f6fb3c6d Leszek Koltunski
  }