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