Project

General

Profile

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

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

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