Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 86e99907

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