Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 5b959cc5

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import android.opengl.GLES30;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * Class which represents a OpenGL Framebuffer object.
27
 * <p>
28
 * User is able to create offscreen FBOs and both a) render to them b) use their COLOR0 attachment as
29
 * an input texture. Attaching Depths and/or Stencils is also possible.
30
 */
31
public class DistortedFramebuffer extends DistortedOutputSurface implements DistortedInputSurface
32
  {
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
// Must be called from a thread holding OpenGL Context
36

    
37
  void create()
38
    {
39
    if( mColorCreated==NOT_CREATED_YET )
40
      {
41
      GLES30.glGenTextures(1, mColorH, 0);
42
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
43
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
44
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
45
      GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
46
      GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
47
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mWidth, mHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
48
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
49

    
50
      GLES30.glGenFramebuffers(1, mFBOH, 0);
51
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
52
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
53
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
54

    
55
      mColorCreated = checkStatus("color");
56
      }
57
    if( mDepthStencilCreated==NOT_CREATED_YET ) // we need to create a new DEPTH or STENCIL attachment
58
      {
59
      GLES30.glGenTextures(1, mDepthStencilH, 0);
60
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[0]);
61
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
62
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
63
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
64
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST);
65

    
66
      if( mDepthStencil==DEPTH_NO_STENCIL )
67
        {
68
        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mWidth, mHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_INT, null);
69
        }
70
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
71
        {
72
        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);
73
        }
74

    
75
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
76
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
77

    
78
      if( mDepthStencil==DEPTH_NO_STENCIL )
79
        {
80
        GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthStencilH[0], 0);
81
        }
82
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
83
        {
84
        GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_STENCIL_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthStencilH[0], 0);
85
        }
86

    
87
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
88

    
89
      mDepthStencilCreated = checkStatus("depth");
90
      }
91
    if( mDepthStencilCreated==DONT_CREATE && mDepthStencilH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
92
      {
93
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
94
      mDepthStencilH[0]=0;
95
      }
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  private int checkStatus(String message)
101
    {
102
    int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
103

    
104
    if(status != GLES30.GL_FRAMEBUFFER_COMPLETE)
105
      {
106
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
107

    
108
      GLES30.glDeleteTextures(1, mColorH, 0);
109
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
110
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
111
      mFBOH[0]= 0;
112

    
113
      return FAILED_TO_CREATE;
114
      }
115

    
116
    return CREATED;
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
// Must be called from a thread holding OpenGL Context
121

    
122
  void delete()
123
    {
124
    if( mColorH[0]>0 )
125
      {
126
      GLES30.glDeleteTextures(1, mColorH, 0);
127
      mColorH[0] = 0;
128
      mColorCreated = NOT_CREATED_YET;
129
      }
130

    
131
    if( mDepthStencilH[0]>0 )
132
      {
133
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
134
      mDepthStencilH[0]=0;
135
      mDepthStencilCreated = NOT_CREATED_YET;
136
      }
137

    
138
    GLES30.glDeleteFramebuffers(1, mFBOH, 0);
139
    mFBOH[0] = 0;
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
// called from onDestroy(); mark OpenGL assets as 'not created'
144

    
145
  void recreate()
146
    {
147
    if( mColorCreated!=DONT_CREATE )
148
      {
149
      mColorCreated = NOT_CREATED_YET;
150
      mColorH[0] = 0;
151
      }
152
    if( mDepthStencilCreated!=DONT_CREATE )
153
      {
154
      mDepthStencilCreated = NOT_CREATED_YET;
155
      mDepthStencilH[0] = 0;
156
      }
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
// create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
161
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
162
// inside a Tree of DistortedNodes (TREE)
163
// SYSTEM surfaces do not get removed in onDestroy().
164

    
165
  DistortedFramebuffer(int depthStencil, int type, int width, int height)
166
    {
167
    super(width,height,NOT_CREATED_YET,depthStencil,NOT_CREATED_YET, type);
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
// For setting the Depth texture as input to fragment shaders that merge many planes. (currently: blur2)
172

    
173
  boolean setAsDepth()
174
    {
175
    if( mDepthStencilH[0]>0 )
176
      {
177
      GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
178
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[0]);
179
      return true;
180
      }
181

    
182
    return false;
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
// PUBLIC API
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
/**
189
 * Create a new offscreen Framebuffer.
190
 *
191
 * @param width Width of the COLOR attachment.
192
 * @param height Height of the COLOR attachment.
193
 * @param depthStencil Add DEPTH or STENCIL attachment?
194
 *                     Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
195
 */
196
  @SuppressWarnings("unused")
197
  public DistortedFramebuffer(int width, int height, int depthStencil)
198
    {
199
    super(width,height,NOT_CREATED_YET,depthStencil,NOT_CREATED_YET,TYPE_USER);
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
/**
205
 * Create a new offscreen Framebuffer. No DEPTH or STENCIL buffer will be created.
206
 *
207
 * @param width Width of the COLOR attachment.
208
 * @param height Height of the COLOR attachment.
209
 */
210
  @SuppressWarnings("unused")
211
  public DistortedFramebuffer(int width, int height)
212
    {
213
    super(width,height,NOT_CREATED_YET, NO_DEPTH_NO_STENCIL,NOT_CREATED_YET,TYPE_USER);
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217
/**
218
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
219
 *
220
 * @return <code>true</code> if successful.
221
 */
222
  public boolean setAsInput()
223
    {
224
    if( mColorH[0]>0 )
225
      {
226
      GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
227
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
228
      return true;
229
      }
230

    
231
    return false;
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
/**
236
 * Enable.disable DEPTH and STENCIL buffers.
237
 *
238
 * @param depthStencil Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
239
 */
240
  public void enableDepthStencil(int depthStencil)
241
    {
242
    if( depthStencil != mDepthStencil )
243
      {
244
      mDepthStencil = depthStencil;
245

    
246
      if( depthStencil!= NO_DEPTH_NO_STENCIL && mDepthStencilCreated==DONT_CREATE )
247
        {
248
        mDepthStencilCreated = NOT_CREATED_YET;
249
        markForCreation();
250
        }
251
      if( depthStencil== NO_DEPTH_NO_STENCIL && mDepthStencilCreated!=DONT_CREATE )
252
        {
253
        mDepthStencilCreated = DONT_CREATE;
254
        markForCreation();
255
        }
256
      }
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260
/**
261
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
262
 * <p>
263
 * Catch: this will only work if the library has had time to actually create the texture. Remember
264
 * that the texture gets created only on first render, thus creating a Texture object and immediately
265
 * calling this method will return an invalid (negative) result.
266
 *
267
 * @return If there was not a single render between creation of the Object and calling this method on
268
 *         it, return a negative value. Otherwise, return ID of COLOR attachment 0.
269
 */
270
  public int getTextureID()
271
    {
272
    return mColorH[0];
273
    }
274
  }
(4-4/26)