Project

General

Profile

Download (29.9 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / main / DistortedOutputSurface.java @ 2dbed690

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