Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedFramebuffer.java @ 2dbed690

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