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