Project

General

Profile

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

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

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