1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski leszek@koltunski.pl //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// This library is free software; you can redistribute it and/or //
|
7
|
// modify it under the terms of the GNU Lesser General Public //
|
8
|
// License as published by the Free Software Foundation; either //
|
9
|
// version 2.1 of the License, or (at your option) any later version. //
|
10
|
// //
|
11
|
// This library 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 GNU //
|
14
|
// Lesser General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU Lesser General Public //
|
17
|
// License along with this library; if not, write to the Free Software //
|
18
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20
|
|
21
|
package org.distorted.library.main;
|
22
|
|
23
|
import android.opengl.GLES30;
|
24
|
import android.util.Log;
|
25
|
|
26
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27
|
/**
|
28
|
* Class which represents a OpenGL Framebuffer object.
|
29
|
* <p>
|
30
|
* User is able to create offscreen FBOs and both a) render to them b) use their COLOR0 attachment as
|
31
|
* an input texture. Attaching Depths and/or Stencils is also possible.
|
32
|
*/
|
33
|
public class DistortedFramebuffer extends InternalOutputSurface
|
34
|
{
|
35
|
|
36
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37
|
// Must be called from a thread holding OpenGL Context
|
38
|
|
39
|
void create()
|
40
|
{
|
41
|
if( mNumFBOs==DistortedLibrary.WAIT_FOR_FBO_QUEUE_SIZE )
|
42
|
{
|
43
|
// only now we know how many FBOs there should be
|
44
|
mNumFBOs = DistortedLibrary.getQueueSize();
|
45
|
allocateColor();
|
46
|
allocateStuffDependantOnNumFBOS();
|
47
|
}
|
48
|
|
49
|
//////////////////////////////////////////////////////////////
|
50
|
// COLOR
|
51
|
|
52
|
if( mColorCreated==NOT_CREATED_YET )
|
53
|
{
|
54
|
GLES30.glGenTextures( mNumFBOs*mNumColors, mColorH, 0);
|
55
|
GLES30.glGenFramebuffers(mNumFBOs, mFBOH, 0);
|
56
|
|
57
|
for(int i=0; i<mNumFBOs; i++)
|
58
|
{
|
59
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[i]);
|
60
|
|
61
|
for(int j=0; j<mNumColors; j++)
|
62
|
{
|
63
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[i*mNumColors+j]);
|
64
|
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.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
|
67
|
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
|
68
|
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mRealWidth, mRealHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
|
69
|
}
|
70
|
|
71
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[i*mNumColors], 0);
|
72
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
|
73
|
}
|
74
|
|
75
|
// TODO
|
76
|
mColorCreated = checkStatus("color");
|
77
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
|
78
|
}
|
79
|
|
80
|
//////////////////////////////////////////////////////////////
|
81
|
// DEPTH / STENCIL
|
82
|
|
83
|
if( mDepthStencilCreated==NOT_CREATED_YET ) // we need to create a new DEPTH or STENCIL attachment
|
84
|
{
|
85
|
GLES30.glGenTextures(mNumFBOs, mDepthStencilH, 0);
|
86
|
|
87
|
for(int i=0; i<mNumFBOs; i++)
|
88
|
{
|
89
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[i]);
|
90
|
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
|
91
|
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
|
92
|
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
|
93
|
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST);
|
94
|
|
95
|
if (mDepthStencil == DEPTH_NO_STENCIL)
|
96
|
{
|
97
|
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mRealWidth, mRealHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_INT, null);
|
98
|
}
|
99
|
else if (mDepthStencil == BOTH_DEPTH_STENCIL)
|
100
|
{
|
101
|
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH24_STENCIL8, mRealWidth, mRealHeight, 0, GLES30.GL_DEPTH_STENCIL, GLES30.GL_UNSIGNED_INT_24_8, null);
|
102
|
}
|
103
|
}
|
104
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
|
105
|
|
106
|
for(int i=0; i<mNumFBOs; i++)
|
107
|
{
|
108
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[i]);
|
109
|
|
110
|
if (mDepthStencil == DEPTH_NO_STENCIL)
|
111
|
{
|
112
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthStencilH[i], 0);
|
113
|
}
|
114
|
else if (mDepthStencil == BOTH_DEPTH_STENCIL)
|
115
|
{
|
116
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_STENCIL_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthStencilH[i], 0);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
// TODO
|
121
|
mDepthStencilCreated = checkStatus("depth");
|
122
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
|
123
|
}
|
124
|
|
125
|
//////////////////////////////////////////////////////////////
|
126
|
// DETACH
|
127
|
|
128
|
// TODO
|
129
|
if( mDepthStencilCreated==DONT_CREATE && mDepthStencilH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
|
130
|
{
|
131
|
// OpenGL ES 3.0.5 spec, chapter 4.4.2.4 :
|
132
|
// "Note that the texture image is specifically not detached from any other framebuffer objects.
|
133
|
// Detaching the texture image from any other framebuffer objects is the responsibility of the application."
|
134
|
|
135
|
for(int i=0; i<mNumFBOs; i++)
|
136
|
{
|
137
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[i]);
|
138
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, 0, 0);
|
139
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_STENCIL_ATTACHMENT, GLES30.GL_TEXTURE_2D, 0, 0);
|
140
|
mDepthStencilH[i]=0;
|
141
|
}
|
142
|
|
143
|
GLES30.glDeleteTextures(mNumFBOs, mDepthStencilH, 0);
|
144
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
|
145
|
}
|
146
|
}
|
147
|
|
148
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149
|
// TODO
|
150
|
|
151
|
private int checkStatus(String message)
|
152
|
{
|
153
|
int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
|
154
|
|
155
|
if(status != GLES30.GL_FRAMEBUFFER_COMPLETE)
|
156
|
{
|
157
|
Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
|
158
|
|
159
|
GLES30.glDeleteTextures(1, mColorH, 0);
|
160
|
GLES30.glDeleteTextures(1, mDepthStencilH, 0);
|
161
|
GLES30.glDeleteFramebuffers(1, mFBOH, 0);
|
162
|
mFBOH[0]= 0;
|
163
|
|
164
|
return FAILED_TO_CREATE;
|
165
|
}
|
166
|
|
167
|
return CREATED;
|
168
|
}
|
169
|
|
170
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171
|
// Must be called from a thread holding OpenGL Context
|
172
|
|
173
|
void delete()
|
174
|
{
|
175
|
if( mColorH[0]>0 )
|
176
|
{
|
177
|
GLES30.glDeleteTextures(mNumFBOs*mNumColors, mColorH, 0);
|
178
|
mColorCreated = NOT_CREATED_YET;
|
179
|
|
180
|
for(int i=0; i<mNumFBOs*mNumColors; i++) mColorH[i] = 0;
|
181
|
}
|
182
|
|
183
|
if( mDepthStencilH[0]>0 )
|
184
|
{
|
185
|
GLES30.glDeleteTextures(mNumFBOs, mDepthStencilH, 0);
|
186
|
mDepthStencilCreated = NOT_CREATED_YET;
|
187
|
|
188
|
for(int i=0; i<mNumFBOs; i++) mDepthStencilH[i] = 0;
|
189
|
}
|
190
|
|
191
|
if( mNumFBOs>0 && mFBOH[0]>0 )
|
192
|
{
|
193
|
GLES30.glDeleteFramebuffers(mNumFBOs, mFBOH, 0);
|
194
|
}
|
195
|
|
196
|
for(int i=0; i<mNumFBOs; i++)
|
197
|
{
|
198
|
mFBOH[i] = 0;
|
199
|
}
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
// called from onDestroy(); mark OpenGL assets as 'not created'
|
204
|
|
205
|
void recreate()
|
206
|
{
|
207
|
if( mColorCreated!=DONT_CREATE )
|
208
|
{
|
209
|
mColorCreated = NOT_CREATED_YET;
|
210
|
mColorH[0] = 0;
|
211
|
}
|
212
|
if( mDepthStencilCreated!=DONT_CREATE )
|
213
|
{
|
214
|
mDepthStencilCreated = NOT_CREATED_YET;
|
215
|
mDepthStencilH[0] = 0;
|
216
|
}
|
217
|
for(int i=0; i<mNumFBOs; i++)
|
218
|
{
|
219
|
mFBOH[i] = 0;
|
220
|
}
|
221
|
}
|
222
|
|
223
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224
|
|
225
|
boolean setAsInput(int fbo, int texture)
|
226
|
{
|
227
|
if( texture>=0 && texture<mNumColors && fbo>=0 && fbo<mNumFBOs && mColorH[mNumColors*fbo + texture]>0 )
|
228
|
{
|
229
|
GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
|
230
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[mNumColors*fbo + texture]);
|
231
|
return true;
|
232
|
}
|
233
|
|
234
|
return false;
|
235
|
}
|
236
|
|
237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238
|
// create a multi-framebuffer (1 object containing multiple FBOs)
|
239
|
|
240
|
DistortedFramebuffer(int numfbos, int numcolors, int depthStencil, int type, int storage, int width, int height)
|
241
|
{
|
242
|
super(width,height,NOT_CREATED_YET,numfbos,numcolors,depthStencil,NOT_CREATED_YET, type, storage);
|
243
|
markForCreation();
|
244
|
}
|
245
|
|
246
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
247
|
// create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
|
248
|
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
|
249
|
// inside a Tree of DistortedNodes (TREE)
|
250
|
// SYSTEM surfaces do not get removed in onDestroy().
|
251
|
|
252
|
DistortedFramebuffer(int numcolors, int depthStencil, int type, int width, int height)
|
253
|
{
|
254
|
this(1,numcolors,depthStencil,type,STORAGE_PRIVATE,width,height);
|
255
|
}
|
256
|
|
257
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
258
|
// PUBLIC API
|
259
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
260
|
/**
|
261
|
* Create new offscreen Framebuffer with configurable number of COLOR, DEPTH and STENCIL attachments.
|
262
|
*
|
263
|
* @param width Width of all the COLOR attachments.
|
264
|
* @param height Height of all the COLOR attachments.
|
265
|
* @param numcolors How many COLOR attachments to create?
|
266
|
* @param depthStencil Add DEPTH or STENCIL attachment?
|
267
|
* Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
|
268
|
*/
|
269
|
@SuppressWarnings("unused")
|
270
|
public DistortedFramebuffer(int width, int height, int numcolors, int depthStencil)
|
271
|
{
|
272
|
this(1,numcolors,depthStencil,TYPE_USER,STORAGE_PRIVATE,width,height);
|
273
|
}
|
274
|
|
275
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
276
|
/**
|
277
|
* Bind the underlying rectangle of pixels as a OpenGL Texture.
|
278
|
*
|
279
|
* @param texture The Texture number to bind (and thus read from).
|
280
|
* @return <code>true</code> if successful.
|
281
|
*/
|
282
|
public boolean setAsInput(int texture)
|
283
|
{
|
284
|
if( texture>=0 && texture<mNumColors && mColorH[texture]>0 )
|
285
|
{
|
286
|
GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
|
287
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[2*mCurrFBO+texture]);
|
288
|
return true;
|
289
|
}
|
290
|
|
291
|
return false;
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
/**
|
296
|
* Attach the texture'th Texture to COLOR0 attachment.
|
297
|
*
|
298
|
* @param texture The Texture number to attach (and subsequently use to render to)
|
299
|
*/
|
300
|
public void bindForOutput(int texture)
|
301
|
{
|
302
|
if( texture>=0 && texture<mNumColors && mColorH[texture]>0 )
|
303
|
{
|
304
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[2*mCurrFBO+texture], 0);
|
305
|
}
|
306
|
}
|
307
|
|
308
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
309
|
/**
|
310
|
* Enable.disable DEPTH and STENCIL buffers.
|
311
|
*
|
312
|
* @param depthStencil Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
|
313
|
*/
|
314
|
public void enableDepthStencil(int depthStencil)
|
315
|
{
|
316
|
if( depthStencil != mDepthStencil )
|
317
|
{
|
318
|
mDepthStencil = depthStencil;
|
319
|
|
320
|
if( depthStencil!= NO_DEPTH_NO_STENCIL && mDepthStencilCreated==DONT_CREATE )
|
321
|
{
|
322
|
mDepthStencilCreated = NOT_CREATED_YET;
|
323
|
markForCreation();
|
324
|
}
|
325
|
if( depthStencil== NO_DEPTH_NO_STENCIL && mDepthStencilCreated!=DONT_CREATE )
|
326
|
{
|
327
|
mDepthStencilCreated = DONT_CREATE;
|
328
|
markForCreation();
|
329
|
}
|
330
|
}
|
331
|
}
|
332
|
|
333
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
334
|
/**
|
335
|
* Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
|
336
|
* <p>
|
337
|
* Catch: this will only work if the library has had time to actually create the texture. Remember
|
338
|
* that the texture gets created only on first render, thus creating a Texture object and immediately
|
339
|
* calling this method will return an invalid (negative) result.
|
340
|
*
|
341
|
* @return If there was not a single render between creation of the Object and calling this method on
|
342
|
* it, return a negative value. Otherwise, return ID of COLOR attachment 0.
|
343
|
*/
|
344
|
public int getTextureID()
|
345
|
{
|
346
|
return mColorH[0];
|
347
|
}
|
348
|
|
349
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
350
|
/**
|
351
|
* Bind one of the underlying FBOs to GL_READ_FRAMEBUFFER.
|
352
|
* Useful for a subsequent glReadBuffer / glReadPixels.
|
353
|
*
|
354
|
* @param fbo which of the underlying FBOs to bind.
|
355
|
* @return <code>true</code> if successful.
|
356
|
*/
|
357
|
public boolean setAsReadFramebuffer(int fbo)
|
358
|
{
|
359
|
if( fbo>=0 && fbo<mNumFBOs )
|
360
|
{
|
361
|
GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, mFBOH[fbo]);
|
362
|
return true;
|
363
|
}
|
364
|
|
365
|
return false;
|
366
|
}
|
367
|
}
|