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