Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedLibrary.java @ 5f35f1cb

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.main;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.opengl.GLES30;
27
import android.opengl.GLES31;
28
import android.util.Log;
29

    
30
import org.distorted.library.R;
31
import org.distorted.library.effect.Effect;
32
import org.distorted.library.effectqueue.EffectQueue;
33
import org.distorted.library.effectqueue.EffectQueuePostprocess;
34
import org.distorted.library.effect.EffectType;
35
import org.distorted.library.effect.FragmentEffect;
36
import org.distorted.library.effect.PostprocessEffect;
37
import org.distorted.library.effect.VertexEffect;
38
import org.distorted.library.effectqueue.EffectQueueVertex;
39
import org.distorted.library.mesh.DeferredJobs;
40
import org.distorted.library.mesh.MeshBase;
41
import org.distorted.library.message.EffectMessageSender;
42
import org.distorted.library.program.DistortedProgram;
43
import org.distorted.library.program.VertexCompilationException;
44

    
45
import java.io.InputStream;
46
import java.nio.ByteBuffer;
47
import java.nio.ByteOrder;
48
import java.nio.FloatBuffer;
49
import java.nio.IntBuffer;
50
import java.util.regex.Matcher;
51
import java.util.regex.Pattern;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
/**
55
 * A singleton class used to control various global dialog_settings.
56
 */
57
public class DistortedLibrary
58
  {
59
  private static int mGLSL;
60
  private static String mGLSL_VERSION;
61
  private static boolean mOITCompilationSuccessful;
62
  /**
63
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
64
   * backing up our DistortedTexture.
65
   * <p>
66
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
67
   * apply different effects. Used in the copy constructor.
68
   */
69
  public static final int CLONE_SURFACE = 0x1;
70
  /**
71
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
72
   * <p>
73
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
74
   */
75
  public static final int CLONE_MATRIX = 0x2;
76
  /**
77
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
78
   * <p>
79
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
80
   */
81
  public static final int CLONE_VERTEX  = 0x4;
82
  /**
83
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
84
   * <p>
85
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
86
   */
87
  public static final int CLONE_FRAGMENT= 0x8;
88
   /**
89
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
90
   * <p>
91
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
92
   */
93
  public static final int CLONE_POSTPROCESS= 0x10;
94
  /**
95
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
96
   * <p>
97
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
98
   * on the screen with (optionally) different Effects.
99
   */
100
  public static final int CLONE_CHILDREN= 0x20;
101

    
102
  /**
103
   * When creating a DistortedScreen (which needs to have mFBOQueueSize FBOs attached), pass this
104
   * constant for 'numOfFBOs' and the number of backing FBOs will be taken from 'mFBOQueueSize'
105
   * (the value of which is most likely unknown at the time of creation of the Screen)
106
   */
107
  public static final int WAIT_FOR_FBO_QUEUE_SIZE = -1;
108
  /**
109
   * Work around bugs in ARM Mali driver by, instead to a single FBO, rendering to a circular queue
110
   * of mFBOQueueSize FBOs. (otherwise we sometimes get a 'full pipeline flush' and the end result
111
   * might be missing part of the Objects)
112
   *
113
   * This bug only exists on Mali driver r12. (or more precisely it's there in r12 but fixed in r22)
114
   *
115
   * https://community.arm.com/graphics/f/discussions/10285/opengl-es-3-1-on-mali-t880-flashes
116
   */
117
  private static int mFBOQueueSize;
118

    
119
  private static boolean mInitialized=false;
120

    
121
  //////////////////////////////////////////////////////////////////////////////////////////////
122
  /// MAIN PROGRAM ///
123
  private static DistortedProgram mMainProgram;
124
  private static int mMainTextureH;
125

    
126
  /// NORMAL PROGRAM /////
127
  private static DistortedProgram mNormalProgram;
128
  private static int mNormalMVPMatrixH;
129

    
130
  /// MAIN OIT PROGRAM ///
131
  private static DistortedProgram mMainOITProgram;
132
  private static int mMainOITTextureH;
133
  private static int mMainOITSizeH;
134
  private static int mMainOITNumRecordsH;
135

    
136
  /// BLIT PROGRAM ///
137
  private static DistortedProgram mBlitProgram;
138
  private static int mBlitTextureH;
139
  private static int mBlitDepthH;
140
  private static final FloatBuffer mQuadPositions;
141

    
142
  /// FULL PROGRAM ///
143
  private static DistortedProgram mFullProgram;
144

    
145
  static
146
    {
147
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
148
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
149
    mQuadPositions.put(positionData).position(0);
150
    }
151

    
152
  /// BLIT DEPTH PROGRAM ///
153
  private static DistortedProgram mBlitDepthProgram;
154
  private static int mBlitDepthTextureH;
155
  private static int mBlitDepthDepthTextureH;
156
  private static int mBlitDepthDepthH;
157
  private static int mBlitDepthTexCorrH;
158

    
159
  /// OIT SSBO BUFFER ///
160
  private static int[] mLinkedListSSBO = new int[1];
161
  private static int[] mAtomicCounter;
162
  private static int   mCurrBuffer;
163

    
164
  static
165
    {
166
    mLinkedListSSBO[0]= -1;
167
    mCurrBuffer       =  0;
168
    }
169

    
170
  ///////////////////////////////////////////////////////////////
171
  // meaning: allocate 1.0 screenful of places for transparent
172
  // fragments in the SSBO backing up the OIT render method.
173
  private static float mBufferSize=1.0f;
174

    
175
  /// OIT CLEAR PROGRAM ///
176
  private static DistortedProgram mOITClearProgram;
177
  private static int mOITClearDepthH;
178
  private static int mOITClearTexCorrH;
179
  private static int mOITClearSizeH;
180

    
181
  /// OIT BUILD PROGRAM ///
182
  private static DistortedProgram mOITBuildProgram;
183
  private static int mOITBuildTextureH;
184
  private static int mOITBuildDepthTextureH;
185
  private static int mOITBuildDepthH;
186
  private static int mOITBuildTexCorrH;
187
  private static int mOITBuildSizeH;
188
  private static int mOITBuildNumRecordsH;
189

    
190
  /// OIT COLLAPSE PROGRAM ///
191
  private static DistortedProgram mOITCollapseProgram;
192
  private static int mOITCollapseDepthTextureH;
193
  private static int mOITCollapseDepthH;
194
  private static int mOITCollapseTexCorrH;
195
  private static int mOITCollapseSizeH;
196

    
197
  /// OIT RENDER PROGRAM ///
198
  private static DistortedProgram mOITRenderProgram;
199
  private static int mOITRenderDepthH;
200
  private static int mOITRenderTexCorrH;
201
  private static int mOITRenderSizeH;
202

    
203
  /// END PROGRAMS //////
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
// private: hide this from Javadoc
207

    
208
  private DistortedLibrary()
209
    {
210

    
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  private static void createPrograms(Resources resources)
216
    {
217
    // MAIN PROGRAM ////////////////////////////////////
218
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
219
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
220

    
221
    int numF = FragmentEffect.getNumEnabled();
222
    int numV = VertexEffect.getNumEnabled();
223

    
224
    String mainVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
225
    String mainFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
226

    
227
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
228

    
229
    String enabledEffectV= VertexEffect.getGLSL();
230
    String enabledEffectF= FragmentEffect.getGLSL();
231

    
232
    String[] feedback = { "v_Position", "v_endPosition" };
233

    
234
    try
235
      {
236
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
237
                                          enabledEffectV, enabledEffectF, mGLSL, feedback);
238
      }
239
    catch(Exception e)
240
      {
241
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
242
      throw new RuntimeException(e.getMessage());
243
      }
244

    
245
    int mainProgramH = mMainProgram.getProgramHandle();
246
    EffectQueue.getUniforms(mainProgramH,0);
247
    MeshBase.getUniforms(mainProgramH,0);
248
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
249

    
250
    // BLIT PROGRAM ////////////////////////////////////
251
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
252
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
253

    
254
    try
255
      {
256
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
257
      }
258
    catch(Exception e)
259
      {
260
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
261
      throw new RuntimeException(e.getMessage());
262
      }
263

    
264
    int blitProgramH = mBlitProgram.getProgramHandle();
265
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
266
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
267

    
268
    // BLIT DEPTH PROGRAM ////////////////////////////////////
269
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
270
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
271

    
272
    try
273
      {
274
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
275
      }
276
    catch(Exception e)
277
      {
278
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
279
      throw new RuntimeException(e.getMessage());
280
      }
281

    
282
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
283
    mBlitDepthTextureH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Texture");
284
    mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
285
    mBlitDepthDepthH        = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
286
    mBlitDepthTexCorrH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
287

    
288
    // NORMAL PROGRAM //////////////////////////////////////
289
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
290
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
291

    
292
    try
293
      {
294
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
295
      }
296
    catch(Exception e)
297
      {
298
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
299
      throw new RuntimeException(e.getMessage());
300
      }
301

    
302
    int normalProgramH = mNormalProgram.getProgramHandle();
303
    mNormalMVPMatrixH  = GLES30.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  private static void createFullProgram(Resources resources)
309
    {
310
    final InputStream fullVertStream = resources.openRawResource(R.raw.main_vertex_shader);
311
    final InputStream fullFragStream = resources.openRawResource(R.raw.main_fragment_shader);
312

    
313
    int numV = VertexEffect.getAllEnabled();
314

    
315
    String fullVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n");
316
    String fullFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " +                                         0   + "\n");
317

    
318
    fullVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
319

    
320
    String enabledEffectV= VertexEffect.getAllGLSL();
321
    String enabledEffectF= "{}";
322

    
323
    fullVertHeader += "#define PREAPPLY\n";
324

    
325
    String[] feedback = { "v_Position", "v_endPosition", "v_Inflate" };
326

    
327
    try
328
      {
329
      mFullProgram = new DistortedProgram(fullVertStream, fullFragStream, fullVertHeader, fullFragHeader,
330
                                          enabledEffectV, enabledEffectF, mGLSL, feedback);
331
      }
332
    catch(Exception e)
333
      {
334
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile FULL program: "+e.getMessage());
335
      throw new RuntimeException(e.getMessage());
336
      }
337

    
338
    int fullProgramH = mFullProgram.getProgramHandle();
339
    EffectQueue.getUniforms(fullProgramH,3);
340
    MeshBase.getUniforms(fullProgramH,3);
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  private static void createProgramsOIT(Resources resources)
346
    {
347
    // MAIN OIT PROGRAM ////////////////////////////////
348
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
349
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
350

    
351
    int numF = FragmentEffect.getNumEnabled();
352
    int numV = VertexEffect.getNumEnabled();
353

    
354
    String mainVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n") + ("#define OIT\n");
355
    String mainFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n") + ("#define OIT\n");
356

    
357
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
358

    
359
    String enabledEffectV= VertexEffect.getGLSL();
360
    String enabledEffectF= FragmentEffect.getGLSL();
361

    
362
    try
363
      {
364
      mMainOITProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
365
                                             enabledEffectV, enabledEffectF, mGLSL, null);
366
      }
367
    catch(Exception e)
368
      {
369
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN OIT program: "+e.getMessage());
370
      throw new RuntimeException(e.getMessage());
371
      }
372

    
373
    int mainOITProgramH = mMainOITProgram.getProgramHandle();
374
    EffectQueue.getUniforms(mainOITProgramH,1);
375
    MeshBase.getUniforms(mainOITProgramH,1);
376
    mMainOITTextureH    = GLES30.glGetUniformLocation( mainOITProgramH, "u_Texture");
377
    mMainOITSizeH       = GLES30.glGetUniformLocation( mainOITProgramH, "u_Size");
378
    mMainOITNumRecordsH = GLES30.glGetUniformLocation( mainOITProgramH, "u_numRecords");
379

    
380
    // OIT CLEAR PROGRAM ////////////////////////////////////
381
    final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
382
    final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader);
383

    
384
    try
385
      {
386
      mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
387
      }
388
    catch(Exception e)
389
      {
390
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage());
391
      throw new RuntimeException(e.getMessage());
392
      }
393

    
394
    int oitClearProgramH   = mOITClearProgram.getProgramHandle();
395
    mOITClearDepthH        = GLES30.glGetUniformLocation( oitClearProgramH, "u_Depth");
396
    mOITClearTexCorrH      = GLES30.glGetUniformLocation( oitClearProgramH, "u_TexCorr");
397
    mOITClearSizeH         = GLES30.glGetUniformLocation( oitClearProgramH, "u_Size");
398

    
399
    // OIT BUILD PROGRAM ////////////////////////////////////
400
    final InputStream oitBuildVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
401
    final InputStream oitBuildFragStream = resources.openRawResource(R.raw.oit_build_fragment_shader);
402

    
403
    try
404
      {
405
      mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
406
      }
407
    catch(Exception e)
408
      {
409
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage());
410
      throw new RuntimeException(e.getMessage());
411
      }
412

    
413
    int oitBuildProgramH   = mOITBuildProgram.getProgramHandle();
414
    mOITBuildTextureH      = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Texture");
415
    mOITBuildDepthTextureH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture");
416
    mOITBuildDepthH        = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Depth");
417
    mOITBuildTexCorrH      = GLES30.glGetUniformLocation( oitBuildProgramH, "u_TexCorr");
418
    mOITBuildSizeH         = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Size");
419
    mOITBuildNumRecordsH   = GLES30.glGetUniformLocation( oitBuildProgramH, "u_numRecords");
420

    
421
    // OIT COLLAPSE PROGRAM ///////////////////////////
422
    final InputStream oitCollapseVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
423
    final InputStream oitCollapseFragStream = resources.openRawResource(R.raw.oit_collapse_fragment_shader);
424

    
425
    try
426
      {
427
      mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
428
      }
429
    catch(Exception e)
430
      {
431
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage());
432
      throw new RuntimeException(e.getMessage());
433
      }
434

    
435
    int oitCollapseProgramH   = mOITCollapseProgram.getProgramHandle();
436
    mOITCollapseDepthTextureH = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture");
437
    mOITCollapseDepthH        = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Depth");
438
    mOITCollapseTexCorrH      = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr");
439
    mOITCollapseSizeH         = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Size");
440

    
441
    // OIT RENDER PROGRAM ///////////////////////////
442
    final InputStream oitRenderVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
443
    final InputStream oitRenderFragStream = resources.openRawResource(R.raw.oit_render_fragment_shader);
444

    
445
    try
446
      {
447
      mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
448
      }
449
    catch(Exception e)
450
      {
451
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage());
452
      throw new RuntimeException(e.getMessage());
453
      }
454

    
455
    int oitRenderProgramH   = mOITRenderProgram.getProgramHandle();
456
    mOITRenderDepthH        = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Depth");
457
    mOITRenderTexCorrH      = GLES30.glGetUniformLocation( oitRenderProgramH, "u_TexCorr");
458
    mOITRenderSizeH         = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Size");
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
  private static void displayNormals(EffectQueue[] queues, MeshBase mesh)
464
    {
465
    int num = mesh.getNumVertices();
466
    int tfo = mesh.getTFO();
467

    
468
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
469
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
470
    InternalRenderState.switchOffDrawing();
471
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
472
    InternalRenderState.restoreDrawing();
473
    GLES30.glEndTransformFeedback();
474
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
475

    
476
    mNormalProgram.useProgram();
477
    GLES30.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, EffectQueue.getMVP(queues) , 0);
478
    mesh.bindTransformAttribs(mNormalProgram);
479
    GLES30.glLineWidth(8.0f);
480
    GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*num);
481
    }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484
// execute all VertexEffects and adjust all vertices
485

    
486
  public static void adjustVertices(MeshBase mesh, EffectQueueVertex queue)
487
    {
488
    if( mFullProgram!=null )
489
      {
490
      int num = mesh.getNumVertices();
491
      int tfo = mesh.getTFO();
492

    
493
      mFullProgram.useProgram();
494
      mesh.bindVertexAttribs(mFullProgram);
495
      queue.compute(1);
496
      queue.send(0.0f,3);
497
      mesh.send(3);
498

    
499
      GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
500
      GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
501
      InternalRenderState.switchOffDrawing();
502
      GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
503
      InternalRenderState.restoreDrawing();
504
      GLES30.glEndTransformFeedback();
505
      mesh.copyTransformToVertex();
506
      GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
507
      }
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

    
512
  static void drawPrivOIT(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
513
    {
514
    EffectQueue[] queues = effects.getQueues();
515

    
516
    EffectQueue.compute(queues, currTime);
517
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
518

    
519
    DistortedLibrary.mMainOITProgram.useProgram();
520
    GLES30.glUniform1i(DistortedLibrary.mMainOITTextureH, 0);
521
    GLES30.glUniform2ui(DistortedLibrary.mMainOITSizeH, surface.mWidth, surface.mHeight);
522
    GLES30.glUniform1ui(DistortedLibrary.mMainOITNumRecordsH, (int)(DistortedLibrary.mBufferSize*surface.mWidth*surface.mHeight) );
523
    mesh.bindVertexAttribs(DistortedLibrary.mMainOITProgram);
524
    mesh.send(1);
525

    
526
    float inflate     = mesh.getInflate();
527
    float distance    = surface.mDistance;
528
    float mipmap      = surface.mMipmap;
529
    float[] projection= surface.mProjectionMatrix;
530

    
531
    EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 1 );
532
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
533

    
534
    if( mesh.getShowNormals() )
535
      {
536
      DistortedLibrary.mMainProgram.useProgram();
537
      mesh.send(0);
538
      EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 0 );
539
      displayNormals(queues,mesh);
540
      }
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
  static void drawPriv(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
546
    {
547
    if( mMainProgram!=null )
548
      {
549
      EffectQueue[] queues = effects.getQueues();
550

    
551
      EffectQueue.compute(queues, currTime);
552
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
553

    
554
      mMainProgram.useProgram();
555
      GLES30.glUniform1i(DistortedLibrary.mMainTextureH, 0);
556
      mesh.bindVertexAttribs(DistortedLibrary.mMainProgram);
557
      mesh.send(0);
558

    
559
      float inflate     = mesh.getInflate();
560
      float distance    = surface.mDistance;
561
      float mipmap      = surface.mMipmap;
562
      float[] projection= surface.mProjectionMatrix;
563

    
564
      EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 0 );
565
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
566

    
567
      if( mesh.getShowNormals() ) displayNormals(queues,mesh);
568
      }
569
    }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
  static void blitPriv(InternalOutputSurface surface)
574
    {
575
    if( mBlitProgram!=null )
576
      {
577
      mBlitProgram.useProgram();
578

    
579
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
580
      GLES30.glUniform1i(mBlitTextureH, 0);
581
      GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
582
      GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
583
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
584
      }
585
    }
586

    
587
///////////////////////////////////////////////////////////////////////////////////////////////////
588

    
589
  static void blitDepthPriv(InternalOutputSurface surface, float corrW, float corrH)
590
    {
591
    if( mBlitDepthProgram!=null )
592
      {
593
      mBlitDepthProgram.useProgram();
594

    
595
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
596
      GLES30.glUniform1i(mBlitDepthTextureH, 0);
597
      GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
598
      GLES30.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
599
      GLES30.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
600
      GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
601
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
602
      }
603
    }
604

    
605
///////////////////////////////////////////////////////////////////////////////////////////////////
606
// yes it is safe to be mixing 3.0 and 3.1 like that, senior members of the OpenGL discussions forum assert
607

    
608
  private static int printPreviousBuffer()
609
    {
610
    int counter = 0;
611

    
612
    ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
613
                                                                GLES30.GL_MAP_READ_BIT);
614
    if( atomicBuf!=null )
615
      {
616
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
617
      counter = atomicIntBuf.get(0);
618
      }
619
    else
620
      {
621
      Log.e("effects", "print: failed to map atomic buffer");
622
      }
623

    
624
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
625

    
626
    return counter;
627
    }
628

    
629
///////////////////////////////////////////////////////////////////////////////////////////////////
630

    
631
  private static void zeroBuffer()
632
    {
633
    ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
634
        GLES30.GL_MAP_WRITE_BIT|GLES30.GL_MAP_INVALIDATE_BUFFER_BIT);
635
    if( atomicBuf!=null )
636
      {
637
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
638
      atomicIntBuf.put(0,0);
639
      }
640
    else
641
      {
642
      Log.e("effects", "zero: failed to map atomic buffer");
643
      }
644

    
645
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
646
    }
647

    
648
///////////////////////////////////////////////////////////////////////////////////////////////////
649
// reset atomic counter to 0
650

    
651
  static int zeroOutAtomic()
652
    {
653
    int counter = 0;
654

    
655
    if( mAtomicCounter==null )
656
      {
657
      mAtomicCounter = new int[mFBOQueueSize];
658

    
659
      GLES30.glGenBuffers(mFBOQueueSize,mAtomicCounter,0);
660

    
661
      for(int i=0; i<mFBOQueueSize; i++)
662
        {
663
        GLES30.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]);
664
        GLES30.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES30.GL_DYNAMIC_DRAW);
665
        zeroBuffer();
666
        }
667
      }
668

    
669
    // reading the value of the buffer on every frame would slow down rendering by
670
    // dialog_about 3%; doing it only once every 5 frames affects speed by less than 1%.
671
    if( mCurrBuffer==0 )
672
      {
673
      GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
674
      counter = printPreviousBuffer();
675
      }
676

    
677
    if( ++mCurrBuffer>=mFBOQueueSize ) mCurrBuffer = 0;
678

    
679
    GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
680
    zeroBuffer();
681

    
682
    return counter;
683
    }
684

    
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686
// Pass1 of the OIT algorithm. Clear per-pixel head-pointers.
687

    
688
  static void oitClear(InternalOutputSurface surface, int counter)
689
    {
690
    if( mOITClearProgram!=null )
691
      {
692
      if( mLinkedListSSBO[0]<0 )
693
        {
694
        GLES30.glGenBuffers(1,mLinkedListSSBO,0);
695

    
696
        int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
697
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
698
        GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
699
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
700

    
701
        GLES30.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
702
        }
703

    
704
      // See if we have overflown the SSBO in one of the previous frames.
705
      // If yes, assume we need to make the SSBO larger.
706
      float overflow = counter/(mBufferSize*surface.mWidth*surface.mHeight);
707

    
708
      if( overflow>1.0f )
709
        {
710
        //android.util.Log.e("effects", "previous frame rendered "+counter+
711
        //                   " fragments, but there was only "+(mBufferSize*surface.mWidth*surface.mHeight)+" space");
712

    
713
        mBufferSize *= (int)(overflow+1.0f);
714
        int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
715
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
716
        GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
717
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
718
        }
719

    
720
      mOITClearProgram.useProgram();
721

    
722
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
723
      GLES30.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f );   // corrections do not really matter here - only present because of common vertex shader.
724
      GLES30.glUniform1f( mOITClearDepthH , 1.0f);          // likewise depth
725
      GLES30.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight);
726
      GLES30.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
727
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
728
      }
729
    }
730

    
731
///////////////////////////////////////////////////////////////////////////////////////////////////
732
// Pass2 of the OIT algorithm - build per-pixel linked lists.
733

    
734
  static void oitBuild(InternalOutputSurface surface, float corrW, float corrH)
735
    {
736
    if( mOITBuildProgram!=null )
737
      {
738
      mOITBuildProgram.useProgram();
739

    
740
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
741
      GLES30.glUniform1i(mOITBuildTextureH, 0);
742
      GLES30.glUniform1i(mOITBuildDepthTextureH, 1);
743
      GLES30.glUniform2f(mOITBuildTexCorrH, corrW, corrH );
744
      GLES30.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight);
745
      GLES30.glUniform1ui(mOITBuildNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
746
      GLES30.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear);
747
      GLES30.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
748
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
749
      }
750
    }
751

    
752
///////////////////////////////////////////////////////////////////////////////////////////////////
753
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list.
754

    
755
  static void oitCollapse(InternalOutputSurface surface, float corrW, float corrH)
756
    {
757
    if( mOITCollapseProgram!=null )
758
      {
759
      mOITCollapseProgram.useProgram();
760

    
761
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
762
      GLES30.glUniform1i(mOITCollapseDepthTextureH, 1);
763
      GLES30.glUniform2f(mOITCollapseTexCorrH, corrW, corrH );
764
      GLES30.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight);
765
      GLES30.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear);
766
      GLES30.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
767
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
768
      }
769
    }
770

    
771
///////////////////////////////////////////////////////////////////////////////////////////////////
772
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists.
773

    
774
  static void oitRender(InternalOutputSurface surface, float corrW, float corrH)
775
    {
776
    if( mOITRenderProgram!=null )
777
      {
778
      mOITRenderProgram.useProgram();
779

    
780
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
781
      GLES30.glUniform2f(mOITRenderTexCorrH, corrW, corrH );
782
      GLES30.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight);
783
      GLES30.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear);
784
      GLES30.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
785
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
786
      }
787
    }
788

    
789
///////////////////////////////////////////////////////////////////////////////////////////////////
790

    
791
  static void setSSBOSize(float size)
792
    {
793
    mBufferSize = size;
794
    }
795

    
796
///////////////////////////////////////////////////////////////////////////////////////////////////
797

    
798
  static int getQueueSize()
799
    {
800
    return mFBOQueueSize;
801
    }
802

    
803
///////////////////////////////////////////////////////////////////////////////////////////////////
804
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
805
// PowerVR GE8100 / GE8300 compiler fails to compile OIT programs.
806

    
807
  private static void detectBuggyDriversAndSetQueueSize(int queueSize)
808
    {
809
    String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
810
    String version = GLES30.glGetString(GLES30.GL_VERSION);
811
    String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
812

    
813
    mFBOQueueSize = 1;
814

    
815
    if( vendor.contains("ARM") )
816
      {
817
      try
818
        {
819
        String regex = ".*r(\\d+)p\\d.*";
820
        Pattern pattern = Pattern.compile(regex);
821
        Matcher matcher = pattern.matcher(version);
822

    
823
        if( matcher.find() )
824
          {
825
          String driverVersion = matcher.group(1);
826

    
827
          if( driverVersion!=null )
828
            {
829
            int drvVersion = Integer.parseInt(driverVersion);
830

    
831
            if( drvVersion<22 )
832
              {
833
              Log.e("DISTORTED", "You are running this on a ARM Mali driver r"+driverVersion+".\n" +
834
                    "This is a buggy driver, please update to r22. Inserting workaround which uses a lot of memory.");
835

    
836
              mFBOQueueSize = queueSize;
837
              }
838
            }
839
          }
840
        }
841
      catch(Exception ex)
842
        {
843
        android.util.Log.e("library", "exception trying to pattern match version: "+ex.toString());
844
        }
845
      }
846
    else if( vendor.contains("Imagination") )
847
      {
848
      if( renderer.contains("GE8") )
849
        {
850
        Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
851
        Log.e("DISTORTED", "GLSL Version "+GLES30.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
852
        }
853
      }
854
    }
855

    
856
///////////////////////////////////////////////////////////////////////////////////////////////////
857
/**
858
 * Return OpenGL ES version supported by the hardware we are running on.
859
 * There are only three possibilities: 300 (OpenGL ES 3.0) or 310 (at least OpenGL ES 3.1)
860
 * or 200 (OpenGL ES 2.0)
861
 */
862
  public static int getGLSL()
863
    {
864
    return mGLSL;
865
    }
866

    
867
///////////////////////////////////////////////////////////////////////////////////////////////////
868
/**
869
 * Have we successfully managed to compile the OIT programs?
870
 * Some hardware ( Imagination GE8100 / 8300, driver build 1.8@4490469 present in my HTC phone and,
871
 * sadly, Amazon Fire Stick 4K ) fails to compile the shaders. See relevant thread in Imagination's
872
 * support forum:
873
 *
874
 * https://forums.imgtec.com/t/ge8100-in-htc-desire-12-android-7-1-1-fragment-shader-fails-to-compile/2708
875
 */
876
  public static boolean OITCompilationSuccessful()
877
    {
878
    return mOITCompilationSuccessful;
879
    }
880

    
881
///////////////////////////////////////////////////////////////////////////////////////////////////
882
/**
883
 * Have we called onCreate yet, ie have we initialized the library?
884
 * @return <code>true</code> if the library is initialized and ready for action.
885
 */
886
  public static boolean isInitialized()
887
    {
888
    return mInitialized;
889
    }
890

    
891
///////////////////////////////////////////////////////////////////////////////////////////////////
892
/**
893
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
894
 * I.e. best called from GLSurfaceView.onCreate().
895
 * <p>
896
 * Needs to be called from a thread holding the OpenGL context.
897
 *
898
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
899
 */
900
  public static void onCreate(final Context context) throws Exception
901
    {
902
    onCreate(context,4);
903
    }
904

    
905
///////////////////////////////////////////////////////////////////////////////////////////////////
906
/**
907
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
908
 * I.e. best called from GLSurfaceView.onCreate().
909
 * <p>
910
 * Needs to be called from a thread holding the OpenGL context.
911
 *   
912
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
913
 * @param queueSize the size of the FBO queue, a workaround for the bug on Mali drivers.
914
 */
915
  public static void onCreate(final Context context, int queueSize) throws Exception
916
    {
917
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
918
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
919

    
920
    int glESversion = configurationInfo.reqGlEsVersion;
921
    int major = glESversion >> 16;
922
    int minor = glESversion & 0xff;
923

    
924
    if( major< 3 )
925
      {
926
      mGLSL = 100*major + 10*minor;
927
      throw new VertexCompilationException("at least OpenGL ES 3.0 required, this device supports only "+major+"."+minor);
928
      }
929
    else
930
      {
931
      mGLSL = (major==3 && minor==0) ? 300 : 310;
932
      }
933

    
934
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+major+"."+minor);
935
    mGLSL_VERSION = "#version "+mGLSL+" es\n";
936

    
937
    mInitialized = true;
938
    mOITCompilationSuccessful = false;
939

    
940
    detectBuggyDriversAndSetQueueSize(queueSize);
941

    
942
    EffectMessageSender.startSending();
943

    
944
    final Resources resources = context.getResources();
945

    
946
    Exception exception=null;
947

    
948
    try
949
      {
950
      createPrograms(resources);
951
      }
952
    catch(Exception ex)
953
      {
954
      exception = ex;
955
      }
956

    
957
    try
958
      {
959
      createFullProgram(resources);
960
      }
961
    catch(Exception ex)
962
      {
963
      exception = ex;
964
      }
965

    
966
    if( mGLSL>=310)
967
      {
968
      try
969
        {
970
        createProgramsOIT(resources);
971
        mOITCompilationSuccessful = true;
972
        }
973
      catch(Exception ex)
974
        {
975
        exception = ex;
976
        }
977
      }
978

    
979
    try
980
      {
981
      EffectQueuePostprocess.createPrograms(resources, mGLSL);
982
      }
983
    catch(Exception ex)
984
      {
985
      exception = ex;
986
      }
987

    
988
    try
989
      {
990
      PostprocessEffect.createPrograms(mGLSL);
991
      }
992
    catch(Exception ex)
993
      {
994
      exception = ex;
995
      }
996

    
997
    if( exception!=null)
998
      {
999
      throw exception;
1000
      }
1001
    }
1002

    
1003
///////////////////////////////////////////////////////////////////////////////////////////////////
1004
/**
1005
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
1006
 * Must be called from Activity.onPause().
1007
 */
1008
  public static void onPause()
1009
    {
1010
    InternalObject.onPause();
1011

    
1012
    mLinkedListSSBO[0]= -1;
1013
    mAtomicCounter = null;
1014
    }
1015

    
1016
///////////////////////////////////////////////////////////////////////////////////////////////////
1017
/**
1018
 * Call this so that the Library can release its internal data structures.
1019
 * Must be called from Activity.onDestroy(). 
1020
 */
1021
  public static void onDestroy()
1022
    {
1023
    InternalObject.onDestroy();
1024
    InternalNodeData.onDestroy();
1025
    InternalMaster.onDestroy();
1026
    InternalOutputSurface.onDestroy();
1027
    DistortedEffects.onDestroy();
1028
    EffectQueue.onDestroy();
1029
    Effect.onDestroy();
1030
    DeferredJobs.onDestroy();
1031
    EffectMessageSender.stopSending();
1032

    
1033
    mInitialized = false;
1034
    }
1035

    
1036
///////////////////////////////////////////////////////////////////////////////////////////////////
1037
/**
1038
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
1039
 * single (InputSurface,MeshBase) combo.
1040
 *
1041
 * @param type {@link EffectType}
1042
 * @return The maximum number of effects of a given type.
1043
 */
1044
  @SuppressWarnings("unused")
1045
  public static int getMax(EffectType type)
1046
    {
1047
    return EffectQueue.getMax(type.ordinal());
1048
    }
1049

    
1050
///////////////////////////////////////////////////////////////////////////////////////////////////
1051
/**
1052
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
1053
 * This can fail if:
1054
 * <ul>
1055
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
1056
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
1057
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link DistortedLibrary#onCreate}. After this
1058
 *     time only decreasing the value of 'max' is permitted.
1059
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
1060
 * </ul>
1061
 *
1062
 * @param type {@link EffectType}
1063
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
1064
 *            than Byte.MAX_VALUE
1065
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
1066
 */
1067
  @SuppressWarnings("unused")
1068
  public static boolean setMax(EffectType type, int max)
1069
    {
1070
    return EffectQueue.setMax(type.ordinal(),max);
1071
    }
1072
  }
(3-3/14)