Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 9ed80185

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