Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalOutputSurface.java @ f7c72bd1

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