Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedOutputSurface.java @ 86c352e0

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