Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedOutputSurface.java @ 406e2f6b

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