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.main;
|
21
|
|
22
|
import android.opengl.GLES31;
|
23
|
import android.opengl.Matrix;
|
24
|
|
25
|
import org.distorted.library.effect.EffectQuality;
|
26
|
|
27
|
import java.util.ArrayList;
|
28
|
|
29
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30
|
/**
|
31
|
* This is not really part of the public API.
|
32
|
*
|
33
|
* @y.exclude
|
34
|
*/
|
35
|
public abstract class DistortedOutputSurface extends DistortedSurface implements DistortedMaster.Slave
|
36
|
{
|
37
|
/**
|
38
|
* Do not create DEPTH or STENCIL attachment
|
39
|
*/
|
40
|
public static final int NO_DEPTH_NO_STENCIL = 0;
|
41
|
/**
|
42
|
* Create DEPTH, but not STENCIL
|
43
|
*/
|
44
|
public static final int DEPTH_NO_STENCIL = 1;
|
45
|
/**
|
46
|
* Create both DEPTH and STENCIL
|
47
|
*/
|
48
|
public static final int BOTH_DEPTH_STENCIL = 2;
|
49
|
|
50
|
private static final int ATTACH = 0;
|
51
|
private static final int DETACH = 1;
|
52
|
private static final int DETALL = 2;
|
53
|
private static final int SORT = 3;
|
54
|
|
55
|
private ArrayList<DistortedNode> mChildren;
|
56
|
private int mNumChildren; // ==mChildren.length(), but we only create mChildren if the first one gets added
|
57
|
private boolean mRenderWayOIT;
|
58
|
|
59
|
private class Job
|
60
|
{
|
61
|
int type;
|
62
|
DistortedNode node;
|
63
|
|
64
|
Job(int t, DistortedNode n)
|
65
|
{
|
66
|
type = t;
|
67
|
node = n;
|
68
|
}
|
69
|
}
|
70
|
|
71
|
private ArrayList<Job> mJobs = new ArrayList<>();
|
72
|
|
73
|
// Global buffers used for postprocessing.
|
74
|
private static DistortedOutputSurface[] mBuffer=null;
|
75
|
|
76
|
private float mFOV;
|
77
|
float mDistance, mNear;
|
78
|
float[] mProjectionMatrix;
|
79
|
|
80
|
int mDepthStencilCreated;
|
81
|
int mDepthStencil;
|
82
|
int[] mDepthStencilH;
|
83
|
int[] mFBOH;
|
84
|
private long[] mTime;
|
85
|
|
86
|
private float mClearR, mClearG, mClearB, mClearA;
|
87
|
private float mClearDepth;
|
88
|
private int mClearStencil;
|
89
|
private int mClear;
|
90
|
float mMipmap;
|
91
|
|
92
|
int mRealWidth; // the Surface can be backed up with a texture that is
|
93
|
int mRealHeight; // larger than the viewport we have to it.
|
94
|
// mWidth,mHeight are the sizes of the Viewport, those -
|
95
|
// sizes of the backing up texture.
|
96
|
|
97
|
int mCurrFBO; // internal current FBO (see Distorted.FBO_QUEUE_SIZE)
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
DistortedOutputSurface(int width, int height, int createColor, int numfbos, int numcolors, int depthStencil, int fbo, int type)
|
102
|
{
|
103
|
super(width,height,createColor,numfbos,numcolors,type);
|
104
|
|
105
|
mRenderWayOIT = false;
|
106
|
mCurrFBO = 0;
|
107
|
|
108
|
mDepthStencilH = new int[numfbos];
|
109
|
mFBOH = new int[numfbos];
|
110
|
|
111
|
mTime = new long[numfbos];
|
112
|
for(int i=0; i<mNumFBOs;i++) mTime[i]=0;
|
113
|
|
114
|
mRealWidth = width;
|
115
|
mRealHeight= height;
|
116
|
|
117
|
mProjectionMatrix = new float[16];
|
118
|
|
119
|
mFOV = 60.0f;
|
120
|
mNear= 0.5f;
|
121
|
|
122
|
mDepthStencilCreated= (depthStencil== NO_DEPTH_NO_STENCIL ? DONT_CREATE:NOT_CREATED_YET);
|
123
|
mDepthStencil = depthStencil;
|
124
|
|
125
|
mFBOH[0] = fbo;
|
126
|
mDepthStencilH[0]= 0;
|
127
|
|
128
|
mClearR = 0.0f;
|
129
|
mClearG = 0.0f;
|
130
|
mClearB = 0.0f;
|
131
|
mClearA = 0.0f;
|
132
|
|
133
|
mClearDepth = 1.0f;
|
134
|
mClearStencil = 0;
|
135
|
mClear = GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT;
|
136
|
|
137
|
mMipmap = 1.0f;
|
138
|
|
139
|
createProjection();
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
private void createProjection()
|
145
|
{
|
146
|
if( mWidth>0 && mHeight>1 )
|
147
|
{
|
148
|
if( mFOV>0.0f ) // perspective projection
|
149
|
{
|
150
|
float a = 2.0f*(float)Math.tan(mFOV*Math.PI/360);
|
151
|
float q = mWidth*mNear;
|
152
|
float c = mHeight*mNear;
|
153
|
|
154
|
float left = -q/2;
|
155
|
float right = +q/2;
|
156
|
float bottom = -c/2;
|
157
|
float top = +c/2;
|
158
|
float near = c/a;
|
159
|
|
160
|
mDistance = mHeight/a;
|
161
|
float far = 2*mDistance-near;
|
162
|
|
163
|
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
164
|
}
|
165
|
else // parallel projection
|
166
|
{
|
167
|
float left = -mWidth/2.0f;
|
168
|
float right = +mWidth/2.0f;
|
169
|
float bottom = -mHeight/2.0f;
|
170
|
float top = +mHeight/2.0f;
|
171
|
float near = mWidth+mHeight-mHeight*(1.0f-mNear);
|
172
|
mDistance = mWidth+mHeight;
|
173
|
float far = mWidth+mHeight+mHeight*(1.0f-mNear);
|
174
|
|
175
|
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
176
|
}
|
177
|
}
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
private static void createPostprocessingBuffers(int width, int height, float near)
|
183
|
{
|
184
|
mBuffer = new DistortedOutputSurface[EffectQuality.LENGTH];
|
185
|
float mipmap=1.0f;
|
186
|
|
187
|
for (int j=0; j<EffectQuality.LENGTH; j++)
|
188
|
{
|
189
|
mBuffer[j] = new DistortedFramebuffer(Distorted.FBO_QUEUE_SIZE,2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(width*mipmap), (int)(height*mipmap) );
|
190
|
mBuffer[j].mMipmap = mipmap;
|
191
|
mBuffer[j].mNear = near; // copy mNear as well (for blitting- see PostprocessEffect.apply() )
|
192
|
mBuffer[j].glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
193
|
|
194
|
mipmap *= EffectQuality.MULTIPLIER;
|
195
|
}
|
196
|
|
197
|
DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
|
198
|
|
199
|
GLES31.glStencilMask(0xff);
|
200
|
GLES31.glDepthMask(true);
|
201
|
GLES31.glColorMask(true, true, true, true);
|
202
|
GLES31.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
|
203
|
GLES31.glClearDepthf(1.0f);
|
204
|
GLES31.glClearStencil(0);
|
205
|
|
206
|
for (int j=0; j<EffectQuality.LENGTH; j++)
|
207
|
{
|
208
|
for(int k=0; k<Distorted.FBO_QUEUE_SIZE; k++)
|
209
|
{
|
210
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mBuffer[j].mFBOH[k]);
|
211
|
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[2*k+1], 0);
|
212
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT | GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_STENCIL_BUFFER_BIT);
|
213
|
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[2*k ], 0);
|
214
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
|
215
|
}
|
216
|
}
|
217
|
|
218
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
static synchronized void onDestroy()
|
224
|
{
|
225
|
if( mBuffer!=null )
|
226
|
{
|
227
|
for (int j = 0; j < EffectQuality.LENGTH; j++)
|
228
|
{
|
229
|
mBuffer[j] = null;
|
230
|
}
|
231
|
|
232
|
mBuffer = null;
|
233
|
}
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
// The postprocessing buffers mBuffer[] are generally speaking too large (there's just one static
|
238
|
// set of them) so before we use them for output, we need to adjust the Viewport as if they were
|
239
|
// smaller. That takes care of outputting pixels to them. When we use them as input, we have to
|
240
|
// adjust the texture coords - see the get{Width|Height}Correction functions.
|
241
|
|
242
|
private static void clonePostprocessingViewport(DistortedOutputSurface from)
|
243
|
{
|
244
|
if( mBuffer[0].mWidth != from.mWidth || mBuffer[0].mHeight != from.mHeight )
|
245
|
{
|
246
|
DistortedOutputSurface surface;
|
247
|
|
248
|
for(int i=0; i<EffectQuality.LENGTH; i++)
|
249
|
{
|
250
|
surface = mBuffer[i];
|
251
|
|
252
|
surface.mWidth = (int)(from.mWidth *surface.mMipmap);
|
253
|
surface.mHeight = (int)(from.mHeight*surface.mMipmap);
|
254
|
|
255
|
surface.mNear = from.mNear; // Near plane is independent of the mipmap level
|
256
|
|
257
|
//android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
|
258
|
|
259
|
surface.createProjection();
|
260
|
|
261
|
int maxw = surface.mWidth > surface.mRealWidth ? surface.mWidth : surface.mRealWidth;
|
262
|
int maxh = surface.mHeight > surface.mRealHeight ? surface.mHeight : surface.mRealHeight;
|
263
|
|
264
|
if (maxw > surface.mRealWidth || maxh > surface.mRealHeight)
|
265
|
{
|
266
|
surface.mRealWidth = maxw;
|
267
|
surface.mRealHeight = maxh;
|
268
|
|
269
|
surface.recreate();
|
270
|
surface.create();
|
271
|
}
|
272
|
}
|
273
|
}
|
274
|
}
|
275
|
|
276
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
277
|
|
278
|
private int blitWithDepth(long currTime, DistortedOutputSurface buffer,int fbo)
|
279
|
{
|
280
|
GLES31.glViewport(0, 0, mWidth, mHeight);
|
281
|
setAsOutput(currTime);
|
282
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
|
283
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo]);
|
284
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
|
285
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[fbo]);
|
286
|
|
287
|
GLES31.glDisable(GLES31.GL_STENCIL_TEST);
|
288
|
GLES31.glStencilMask(0x00);
|
289
|
|
290
|
DistortedEffects.blitDepthPriv(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
|
291
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
|
292
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
|
293
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
|
294
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
|
295
|
|
296
|
// clear buffers
|
297
|
GLES31.glStencilMask(0xff);
|
298
|
GLES31.glDepthMask(true);
|
299
|
GLES31.glColorMask(true,true,true,true);
|
300
|
GLES31.glClearColor(0.0f,0.0f,0.0f,0.0f);
|
301
|
GLES31.glClearDepthf(1.0f);
|
302
|
GLES31.glClearStencil(0);
|
303
|
|
304
|
buffer.setAsOutput();
|
305
|
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo+1], 0);
|
306
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
|
307
|
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo ], 0);
|
308
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
|
309
|
|
310
|
return 1;
|
311
|
}
|
312
|
|
313
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
314
|
|
315
|
private static void oitClear(DistortedOutputSurface buffer)
|
316
|
{
|
317
|
int counter = DistortedEffects.zeroOutAtomic();
|
318
|
DistortedEffects.oitClear(buffer,counter);
|
319
|
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
|
320
|
}
|
321
|
|
322
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
323
|
|
324
|
private int oitBuild(long time, DistortedOutputSurface buffer, int fbo)
|
325
|
{
|
326
|
GLES31.glViewport(0, 0, mWidth, mHeight);
|
327
|
setAsOutput(time);
|
328
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
|
329
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo]);
|
330
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
|
331
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[fbo]);
|
332
|
|
333
|
DistortedRenderState.colorDepthStencilOn();
|
334
|
DistortedRenderState.enableDepthTest();
|
335
|
|
336
|
DistortedEffects.oitBuild(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
|
337
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
|
338
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
|
339
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
|
340
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
|
341
|
|
342
|
DistortedRenderState.colorDepthStencilRestore();
|
343
|
DistortedRenderState.restoreDepthTest();
|
344
|
|
345
|
return 1;
|
346
|
}
|
347
|
|
348
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
349
|
// two phases: 1. collapse the SSBO 2. blend the ssbo's color
|
350
|
|
351
|
private int oitRender(long currTime, int fbo)
|
352
|
{
|
353
|
float corrW = getWidthCorrection();
|
354
|
float corrH = getHeightCorrection();
|
355
|
|
356
|
// Do the Collapse Pass only if we do have a Depth attachment.
|
357
|
// Otherwise there's no point (in fact we then would create a feedback loop!)
|
358
|
|
359
|
if( mDepthStencilH[fbo] != 0 )
|
360
|
{
|
361
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
|
362
|
GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
|
363
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mDepthStencilH[fbo]);
|
364
|
DistortedRenderState.switchOffColorDepthStencil();
|
365
|
DistortedEffects.oitCollapse(this, corrW, corrH);
|
366
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
|
367
|
}
|
368
|
|
369
|
setAsOutput(currTime);
|
370
|
DistortedRenderState.switchColorDepthOnStencilOff();
|
371
|
DistortedEffects.oitRender(this, corrW, corrH);
|
372
|
DistortedRenderState.restoreColorDepthStencil();
|
373
|
|
374
|
return 1;
|
375
|
}
|
376
|
|
377
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
378
|
|
379
|
void clear()
|
380
|
{
|
381
|
DistortedRenderState.colorDepthStencilOn();
|
382
|
GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
|
383
|
GLES31.glClearDepthf(mClearDepth);
|
384
|
GLES31.glClearStencil(mClearStencil);
|
385
|
GLES31.glClear(mClear);
|
386
|
DistortedRenderState.colorDepthStencilRestore();
|
387
|
}
|
388
|
|
389
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
390
|
|
391
|
void setCurrFBO(int fbo)
|
392
|
{
|
393
|
mCurrFBO = fbo;
|
394
|
}
|
395
|
|
396
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
397
|
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
|
398
|
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
|
399
|
// to a whole buffer (lastQueue.postprocess) and merge it (this.oitBuild).
|
400
|
|
401
|
int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children, int fbo, boolean oit)
|
402
|
{
|
403
|
int quality=0, numRenders=0, bucketChange=0;
|
404
|
DistortedNode child;
|
405
|
EffectQueuePostprocess lastQueue=null, currQueue;
|
406
|
long lastBucket=0, currBucket;
|
407
|
|
408
|
setCurrFBO(fbo);
|
409
|
|
410
|
if( mBuffer!=null )
|
411
|
{
|
412
|
for (int i=0; i<EffectQuality.LENGTH; i++)
|
413
|
{
|
414
|
mBuffer[i].setCurrFBO(fbo);
|
415
|
}
|
416
|
}
|
417
|
|
418
|
if( oit && numChildren>0 )
|
419
|
{
|
420
|
oitClear(this);
|
421
|
}
|
422
|
|
423
|
for(int i=0; i<numChildren; i++)
|
424
|
{
|
425
|
child = children.get(i);
|
426
|
currQueue = child.getPostprocessQueue();
|
427
|
currBucket= currQueue.getID();
|
428
|
|
429
|
if( currBucket==0 )
|
430
|
{
|
431
|
setAsOutput(time);
|
432
|
|
433
|
if( oit )
|
434
|
{
|
435
|
numRenders += child.drawOIT(time, this);
|
436
|
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
|
437
|
}
|
438
|
else
|
439
|
{
|
440
|
numRenders += child.draw(time, this);
|
441
|
}
|
442
|
}
|
443
|
else
|
444
|
{
|
445
|
if( mBuffer==null )
|
446
|
{
|
447
|
createPostprocessingBuffers(mWidth,mHeight,mNear);
|
448
|
|
449
|
for (int j=0; j<EffectQuality.LENGTH; j++)
|
450
|
{
|
451
|
mBuffer[j].setCurrFBO(fbo);
|
452
|
}
|
453
|
}
|
454
|
|
455
|
if( lastBucket!=currBucket )
|
456
|
{
|
457
|
if( lastBucket==0 )
|
458
|
{
|
459
|
clonePostprocessingViewport(this);
|
460
|
}
|
461
|
else
|
462
|
{
|
463
|
for(int j=bucketChange; j<i; j++) numRenders += lastQueue.preprocess( mBuffer,children.get(j) );
|
464
|
numRenders += lastQueue.postprocess(mBuffer);
|
465
|
|
466
|
if( oit )
|
467
|
{
|
468
|
numRenders += oitBuild(time, mBuffer[quality], fbo);
|
469
|
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
|
470
|
}
|
471
|
else
|
472
|
{
|
473
|
numRenders += blitWithDepth(time, mBuffer[quality],fbo);
|
474
|
}
|
475
|
mBuffer[quality].clearBuffer(fbo);
|
476
|
}
|
477
|
|
478
|
quality = currQueue.getQuality();
|
479
|
bucketChange= i;
|
480
|
}
|
481
|
|
482
|
mBuffer[quality].setAsOutput(time);
|
483
|
child.drawNoBlend(time,mBuffer[quality]);
|
484
|
|
485
|
if( i==numChildren-1 )
|
486
|
{
|
487
|
for(int j=bucketChange; j<numChildren; j++) numRenders += currQueue.preprocess( mBuffer,children.get(j) );
|
488
|
numRenders += currQueue.postprocess(mBuffer);
|
489
|
|
490
|
if( oit )
|
491
|
{
|
492
|
numRenders += oitBuild(time, mBuffer[quality], fbo);
|
493
|
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT | GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
|
494
|
mBuffer[quality].clearBuffer(fbo);
|
495
|
}
|
496
|
else
|
497
|
{
|
498
|
numRenders += blitWithDepth(time, mBuffer[quality],fbo);
|
499
|
}
|
500
|
}
|
501
|
} // end else (postprocessed child)
|
502
|
|
503
|
lastQueue = currQueue;
|
504
|
lastBucket= currBucket;
|
505
|
} // end main for loop
|
506
|
|
507
|
if( oit && numChildren>0 )
|
508
|
{
|
509
|
numRenders += oitRender(time, fbo); // merge the OIT linked list
|
510
|
}
|
511
|
|
512
|
return numRenders;
|
513
|
}
|
514
|
|
515
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
516
|
|
517
|
ArrayList<DistortedNode> getChildren()
|
518
|
{
|
519
|
return mChildren;
|
520
|
}
|
521
|
|
522
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
523
|
/**
|
524
|
* Not part of the Public API.
|
525
|
*
|
526
|
* @y.exclude
|
527
|
*/
|
528
|
public float getWidthCorrection()
|
529
|
{
|
530
|
return (float)mWidth/mRealWidth;
|
531
|
}
|
532
|
|
533
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
534
|
/**
|
535
|
* Not part of the Public API.
|
536
|
*
|
537
|
* @y.exclude
|
538
|
*/
|
539
|
public float getHeightCorrection()
|
540
|
{
|
541
|
return (float)mHeight/mRealHeight;
|
542
|
}
|
543
|
|
544
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
545
|
|
546
|
private void clearBuffer(int fbo)
|
547
|
{
|
548
|
DistortedRenderState.colorDepthStencilOn();
|
549
|
|
550
|
GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
|
551
|
GLES31.glClearDepthf(mClearDepth);
|
552
|
GLES31.glClearStencil(mClearStencil);
|
553
|
|
554
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[fbo]);
|
555
|
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[2*fbo+1], 0);
|
556
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
|
557
|
GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[2*fbo ], 0);
|
558
|
GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
|
559
|
|
560
|
DistortedRenderState.colorDepthStencilRestore();
|
561
|
}
|
562
|
|
563
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
564
|
// PUBLIC API
|
565
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
566
|
/**
|
567
|
* Draws all the attached children to this OutputSurface's 0th FBO.
|
568
|
* <p>
|
569
|
* Must be called from a thread holding OpenGL Context.
|
570
|
*
|
571
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
572
|
* @return Number of objects rendered.
|
573
|
*/
|
574
|
public int render(long time)
|
575
|
{
|
576
|
return render(time,0);
|
577
|
}
|
578
|
|
579
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
580
|
/**
|
581
|
* Draws all the attached children to this OutputSurface.
|
582
|
* <p>
|
583
|
* Must be called from a thread holding OpenGL Context.
|
584
|
*
|
585
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
|
586
|
* @param fbo The surface can have many FBOs backing it up - render this to FBO number 'fbo'.
|
587
|
* @return Number of objects rendered.
|
588
|
*/
|
589
|
public int render(long time, int fbo)
|
590
|
{
|
591
|
// change tree topology (attach and detach children)
|
592
|
/*
|
593
|
boolean changed1 =
|
594
|
*/
|
595
|
DistortedMaster.toDo();
|
596
|
/*
|
597
|
if( changed1 )
|
598
|
{
|
599
|
for(int i=0; i<mNumChildren; i++)
|
600
|
{
|
601
|
mChildren.get(i).debug(0);
|
602
|
}
|
603
|
|
604
|
DistortedNode.debugMap();
|
605
|
}
|
606
|
*/
|
607
|
// create and delete all underlying OpenGL resources
|
608
|
// Watch out: FIRST change topology, only then deal
|
609
|
// with OpenGL resources. That's because changing Tree
|
610
|
// can result in additional Framebuffers that would need
|
611
|
// to be created immediately, before the calls to drawRecursive()
|
612
|
/*
|
613
|
boolean changed2 =
|
614
|
*/
|
615
|
toDo();
|
616
|
/*
|
617
|
if( changed2 )
|
618
|
{
|
619
|
DistortedObject.debugLists();
|
620
|
}
|
621
|
*/
|
622
|
// mark OpenGL state as unknown
|
623
|
DistortedRenderState.reset();
|
624
|
|
625
|
int numRenders=0;
|
626
|
|
627
|
for(int i=0; i<mNumChildren; i++)
|
628
|
{
|
629
|
numRenders += mChildren.get(i).renderRecursive(time);
|
630
|
}
|
631
|
|
632
|
numRenders += renderChildren(time,mNumChildren,mChildren,fbo, mRenderWayOIT);
|
633
|
|
634
|
return numRenders;
|
635
|
}
|
636
|
|
637
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
638
|
/**
|
639
|
* Bind this Surface as a Framebuffer we can render to.
|
640
|
*
|
641
|
* @param time Present time, in milliseconds. The point: looking at this param the library can figure
|
642
|
* out if this is the first time during present frame that this FBO is being set as output.
|
643
|
* If so, the library, in addition to binding the Surface for output, also clears the
|
644
|
* Surface's color and depth attachments.
|
645
|
*/
|
646
|
public void setAsOutput(long time)
|
647
|
{
|
648
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[mCurrFBO]);
|
649
|
|
650
|
if( mTime[mCurrFBO]!=time )
|
651
|
{
|
652
|
mTime[mCurrFBO] = time;
|
653
|
clear();
|
654
|
}
|
655
|
}
|
656
|
|
657
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
658
|
/**
|
659
|
* Bind this Surface as a Framebuffer we can render to.
|
660
|
* <p>
|
661
|
* This version does not attempt to clear anything.
|
662
|
*/
|
663
|
public void setAsOutput()
|
664
|
{
|
665
|
GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[mCurrFBO]);
|
666
|
}
|
667
|
|
668
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
669
|
/**
|
670
|
* Return the Near plane of the Projection included in the Surface.
|
671
|
*
|
672
|
* @return the Near plane.
|
673
|
*/
|
674
|
public float getNear()
|
675
|
{
|
676
|
return mNear;
|
677
|
}
|
678
|
|
679
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
680
|
/**
|
681
|
* Set mipmap level.
|
682
|
* <p>
|
683
|
* Trick for speeding up your renders - one can create a pyramid of OutputSurface objects, each next
|
684
|
* one some constant FACTOR smaller than the previous (0.5 is the common value), then set the Mipmap
|
685
|
* Level of the i-th object to be FACTOR^i (we start counting from 0). When rendering any scene into
|
686
|
* such prepared OutputSurface, the library will make sure to scale any Effects used so that the end
|
687
|
* scene will end up looking identical no matter which object we render to. Identical, that is, except
|
688
|
* for the loss of quality and gain in speed associated with rendering to a smaller Surface.
|
689
|
* <p>
|
690
|
* Example: if you create two FBOs, one 1000x1000 and another 500x500 in size, and set the second one
|
691
|
* mipmap to 0.5 (the first one's is 1.0 by default), define Effects to be a single move by (100,100),
|
692
|
* and render a skinned Mesh into both FBO, the end result will look proportionally the same, because
|
693
|
* in the second case the move vector (100,100) will be auto-scaled to (50,50).
|
694
|
*
|
695
|
* @param mipmap The mipmap level. Acceptable range: 0<mipmap<infinity, although mipmap>1
|
696
|
* does not make any sense (that would result in loss of speed and no gain in quality)
|
697
|
*/
|
698
|
public void setMipmap(float mipmap)
|
699
|
{
|
700
|
mMipmap = mipmap;
|
701
|
}
|
702
|
|
703
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
704
|
/**
|
705
|
* Set the (R,G,B,A) values of GLES31.glClearColor() to set up color with which to clear
|
706
|
* this Surface at the beginning of each frame.
|
707
|
*
|
708
|
* @param r the Red component. Default: 0.0f
|
709
|
* @param g the Green component. Default: 0.0f
|
710
|
* @param b the Blue component. Default: 0.0f
|
711
|
* @param a the Alpha component. Default: 0.0f
|
712
|
*/
|
713
|
public void glClearColor(float r, float g, float b, float a)
|
714
|
{
|
715
|
mClearR = r;
|
716
|
mClearG = g;
|
717
|
mClearB = b;
|
718
|
mClearA = a;
|
719
|
}
|
720
|
|
721
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
722
|
/**
|
723
|
* Uses glClearDepthf() to set up a value with which to clear
|
724
|
* the Depth buffer of our Surface at the beginning of each frame.
|
725
|
*
|
726
|
* @param d the Depth. Default: 1.0f
|
727
|
*/
|
728
|
public void glClearDepthf(float d)
|
729
|
{
|
730
|
mClearDepth = d;
|
731
|
}
|
732
|
|
733
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
734
|
/**
|
735
|
* Uses glClearStencil() to set up a value with which to clear the
|
736
|
* Stencil buffer of our Surface at the beginning of each frame.
|
737
|
*
|
738
|
* @param s the Stencil. Default: 0
|
739
|
*/
|
740
|
public void glClearStencil(int s)
|
741
|
{
|
742
|
mClearStencil = s;
|
743
|
}
|
744
|
|
745
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
746
|
/**
|
747
|
* Which buffers to Clear at the beginning of each frame?
|
748
|
* <p>
|
749
|
* Valid values: 0, or bitwise OR of one or more values from the set GL_COLOR_BUFFER_BIT,
|
750
|
* GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT.
|
751
|
* Default: GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT.
|
752
|
*
|
753
|
* @param mask bitwise OR of BUFFER_BITs to clear.
|
754
|
*/
|
755
|
public void glClear(int mask)
|
756
|
{
|
757
|
mClear = mask;
|
758
|
}
|
759
|
|
760
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
761
|
/**
|
762
|
* Create new Projection matrix.
|
763
|
*
|
764
|
* @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
|
765
|
* Valid values: 0<=fov<180. FOV==0 means 'parallel projection'.
|
766
|
* @param near Distance between the screen plane and the near plane.
|
767
|
* Valid vaules: 0<near<1. When near==0 (illegal!), the Near Plane is exactly at the tip of
|
768
|
* the pyramid. When near==1 (illegal!) the near plane is equivalent to the screen plane.
|
769
|
*/
|
770
|
public void setProjection(float fov, float near)
|
771
|
{
|
772
|
if( fov < 180.0f && fov >=0.0f )
|
773
|
{
|
774
|
mFOV = fov;
|
775
|
}
|
776
|
|
777
|
if( near< 1.0f && near> 0.0f )
|
778
|
{
|
779
|
mNear= near;
|
780
|
}
|
781
|
else if( near<=0.0f )
|
782
|
{
|
783
|
mNear = 0.01f;
|
784
|
}
|
785
|
else if( near>=1.0f )
|
786
|
{
|
787
|
mNear=0.99f;
|
788
|
}
|
789
|
|
790
|
if( mBuffer!=null )
|
791
|
{
|
792
|
for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
|
793
|
}
|
794
|
|
795
|
createProjection();
|
796
|
}
|
797
|
|
798
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
799
|
/**
|
800
|
* Resize the underlying Framebuffer.
|
801
|
* <p>
|
802
|
* This method can be safely called mid-render as it doesn't interfere with rendering.
|
803
|
*
|
804
|
* @param width The new width.
|
805
|
* @param height The new height.
|
806
|
*/
|
807
|
public void resize(int width, int height)
|
808
|
{
|
809
|
if( mWidth!=width || mHeight!=height )
|
810
|
{
|
811
|
mWidth = mRealWidth = width;
|
812
|
mHeight= mRealHeight= height;
|
813
|
|
814
|
createProjection();
|
815
|
|
816
|
if( mColorCreated==CREATED )
|
817
|
{
|
818
|
markForCreation();
|
819
|
recreate();
|
820
|
}
|
821
|
}
|
822
|
}
|
823
|
|
824
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
825
|
/**
|
826
|
* Return true if the Surface contains a DEPTH attachment.
|
827
|
*
|
828
|
* @return <bold>true</bold> if the Surface contains a DEPTH attachment.
|
829
|
*/
|
830
|
public boolean hasDepth()
|
831
|
{
|
832
|
return mDepthStencilCreated==CREATED;
|
833
|
}
|
834
|
|
835
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
836
|
/**
|
837
|
* Return true if the Surface contains a STENCIL attachment.
|
838
|
*
|
839
|
* @return <bold>true</bold> if the Surface contains a STENCIL attachment.
|
840
|
*/
|
841
|
public boolean hasStencil()
|
842
|
{
|
843
|
return (mDepthStencilCreated==CREATED && mDepthStencil==BOTH_DEPTH_STENCIL);
|
844
|
}
|
845
|
|
846
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
847
|
/**
|
848
|
* When rendering this Node, should we use the Order Independent Transparency render more?
|
849
|
* <p>
|
850
|
* There are two modes of rendering: the fast 'normal' way, which however renders transparent
|
851
|
* fragments in different ways depending on which fragments get rendered first, or the slower
|
852
|
* 'oit' way, which renders transparent fragments correctly regardless of their order.
|
853
|
*
|
854
|
* @param oit True if we want to render more slowly, but in a way which accounts for transparency.
|
855
|
*/
|
856
|
public void setOrderIndependentTransparency(boolean oit)
|
857
|
{
|
858
|
mRenderWayOIT = oit;
|
859
|
}
|
860
|
|
861
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
862
|
/**
|
863
|
* When rendering this Node, should we use the Order Independent Transparency render more?
|
864
|
* <p>
|
865
|
* There are two modes of rendering: the fast 'normal' way, which however renders transparent
|
866
|
* fragments in different ways depending on which fragments get rendered first, or the slower
|
867
|
* 'oit' way, which renders transparent fragments correctly regardless of their order.
|
868
|
*
|
869
|
* @param oit True if we want to render more slowly, but in a way which accounts for transparency.
|
870
|
* @param initialSize Initial number of transparent fragments we expect, in screenfuls.
|
871
|
* I.e '1.0' means 'the scene we are going to render contains about 1 screen
|
872
|
* worth of transparent fragments'. Valid values: 0.0 < initialSize < 10.0
|
873
|
* Even if you get this wrong, the library will detect that there are more
|
874
|
* transparent fragments than it has space for and readjust its internal buffers,
|
875
|
* but only after a few frames during which one will probably see missing objects.
|
876
|
*/
|
877
|
public void setOrderIndependentTransparency(boolean oit, float initialSize)
|
878
|
{
|
879
|
mRenderWayOIT = oit;
|
880
|
|
881
|
if( initialSize>0.0f && initialSize<10.0f )
|
882
|
DistortedEffects.setSSBOSize(initialSize);
|
883
|
}
|
884
|
|
885
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
886
|
/**
|
887
|
* Adds a new child to the last position in the list of our Surface's children.
|
888
|
* <p>
|
889
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
890
|
* DistortedMaster (by calling doWork())
|
891
|
*
|
892
|
* @param node The new Node to add.
|
893
|
*/
|
894
|
public void attach(DistortedNode node)
|
895
|
{
|
896
|
mJobs.add(new Job(ATTACH,node));
|
897
|
DistortedMaster.newSlave(this);
|
898
|
}
|
899
|
|
900
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
901
|
/**
|
902
|
* Adds a new child to the last position in the list of our Surface's children.
|
903
|
* <p>
|
904
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
905
|
* DistortedMaster (by calling doWork())
|
906
|
*
|
907
|
* @param surface InputSurface to initialize our child Node with.
|
908
|
* @param effects DistortedEffects to initialize our child Node with.
|
909
|
* @param mesh MeshObject to initialize our child Node with.
|
910
|
* @return the newly constructed child Node, or null if we couldn't allocate resources.
|
911
|
*/
|
912
|
public DistortedNode attach(DistortedSurface surface, DistortedEffects effects, MeshObject mesh)
|
913
|
{
|
914
|
DistortedNode node = new DistortedNode(surface,effects,mesh);
|
915
|
mJobs.add(new Job(ATTACH,node));
|
916
|
DistortedMaster.newSlave(this);
|
917
|
return node;
|
918
|
}
|
919
|
|
920
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
921
|
/**
|
922
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
923
|
* <p>
|
924
|
* A bit questionable method as there can be many different Nodes attached as children, some
|
925
|
* of them having the same Effects but - for instance - different Mesh. Use with care.
|
926
|
* <p>
|
927
|
* We cannot do this mid-render - actual detachment will be done just before the next render, by the
|
928
|
* DistortedMaster (by calling doWork())
|
929
|
*
|
930
|
* @param effects DistortedEffects to remove.
|
931
|
*/
|
932
|
public void detach(DistortedEffects effects)
|
933
|
{
|
934
|
long id = effects.getID();
|
935
|
DistortedNode node;
|
936
|
boolean detached = false;
|
937
|
|
938
|
for(int i=0; i<mNumChildren; i++)
|
939
|
{
|
940
|
node = mChildren.get(i);
|
941
|
|
942
|
if( node.getEffects().getID()==id )
|
943
|
{
|
944
|
detached = true;
|
945
|
mJobs.add(new Job(DETACH,node));
|
946
|
DistortedMaster.newSlave(this);
|
947
|
break;
|
948
|
}
|
949
|
}
|
950
|
|
951
|
if( !detached )
|
952
|
{
|
953
|
// if we failed to detach any, it still might be the case that
|
954
|
// there's an ATTACH job that we need to cancel.
|
955
|
int num = mJobs.size();
|
956
|
Job job;
|
957
|
|
958
|
for(int i=0; i<num; i++)
|
959
|
{
|
960
|
job = mJobs.get(i);
|
961
|
|
962
|
if( job.type==ATTACH && job.node.getEffects()==effects )
|
963
|
{
|
964
|
mJobs.remove(i);
|
965
|
break;
|
966
|
}
|
967
|
}
|
968
|
}
|
969
|
}
|
970
|
|
971
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
972
|
/**
|
973
|
* Removes the first occurrence of a specified child from the list of children of our Surface.
|
974
|
* <p>
|
975
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
976
|
* DistortedMaster (by calling doWork())
|
977
|
*
|
978
|
* @param node The Node to remove.
|
979
|
*/
|
980
|
public void detach(DistortedNode node)
|
981
|
{
|
982
|
mJobs.add(new Job(DETACH,node));
|
983
|
DistortedMaster.newSlave(this);
|
984
|
}
|
985
|
|
986
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
987
|
/**
|
988
|
* Removes all children Nodes.
|
989
|
* <p>
|
990
|
* We cannot do this mid-render - actual attachment will be done just before the next render, by the
|
991
|
* DistortedMaster (by calling doWork())
|
992
|
*/
|
993
|
public void detachAll()
|
994
|
{
|
995
|
mJobs.add(new Job(DETALL,null));
|
996
|
DistortedMaster.newSlave(this);
|
997
|
}
|
998
|
|
999
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1000
|
/**
|
1001
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
1002
|
* DistortedSlave interface, which should really be a class that we extend here instead but
|
1003
|
* Java has no multiple inheritance.
|
1004
|
*
|
1005
|
* @y.exclude
|
1006
|
*/
|
1007
|
public void doWork()
|
1008
|
{
|
1009
|
int num = mJobs.size();
|
1010
|
Job job;
|
1011
|
|
1012
|
for(int i=0; i<num; i++)
|
1013
|
{
|
1014
|
job = mJobs.remove(0);
|
1015
|
|
1016
|
switch(job.type)
|
1017
|
{
|
1018
|
case ATTACH: if( mChildren==null ) mChildren = new ArrayList<>(2);
|
1019
|
job.node.setSurfaceParent(this);
|
1020
|
DistortedMaster.addSortingByBuckets(mChildren,job.node);
|
1021
|
mNumChildren++;
|
1022
|
break;
|
1023
|
case DETACH: if( mNumChildren>0 && mChildren.remove(job.node) )
|
1024
|
{
|
1025
|
job.node.setSurfaceParent(null);
|
1026
|
mNumChildren--;
|
1027
|
}
|
1028
|
break;
|
1029
|
case DETALL: if( mNumChildren>0 )
|
1030
|
{
|
1031
|
DistortedNode tmp;
|
1032
|
|
1033
|
for(int j=mNumChildren-1; j>=0; j--)
|
1034
|
{
|
1035
|
tmp = mChildren.remove(j);
|
1036
|
tmp.setSurfaceParent(null);
|
1037
|
}
|
1038
|
|
1039
|
mNumChildren = 0;
|
1040
|
}
|
1041
|
break;
|
1042
|
case SORT : mChildren.remove(job.node);
|
1043
|
DistortedMaster.addSortingByBuckets(mChildren,job.node);
|
1044
|
break;
|
1045
|
}
|
1046
|
}
|
1047
|
}
|
1048
|
}
|