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