Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedLibrary.java @ 07206c71

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

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

    
100
  /**
101
   * Work around bugs in ARM Mali driver by, instead to a single FBO, rendering to a circular queue
102
   * of FBO_QUEUE_SIZE FBOs. (otherwise we sometimes get a 'full pipeline flush' and the end result
103
   * might be missing part of the Objects)
104
   *
105
   * This bug only exists on Mali driver r12.
106
   *
107
   * https://community.arm.com/graphics/f/discussions/10285/opengl-es-3-1-on-mali-t880-flashes
108
   */
109
  static final int FBO_QUEUE_SIZE = 4;
110

    
111
  private static boolean mInitialized=false;
112

    
113
  //////////////////////////////////////////////////////////////////////////////////////////////
114
  /// MAIN PROGRAM ///
115
  private static DistortedProgram mMainProgram;
116
  private static int mMainTextureH;
117

    
118
  /// NORMAL PROGRAM /////
119
  private static DistortedProgram mNormalProgram;
120
  private static int mNormalMVPMatrixH;
121

    
122
  /// MAIN OIT PROGRAM ///
123
  private static DistortedProgram mMainOITProgram;
124
  private static int mMainOITTextureH;
125
  private static int mMainOITSizeH;
126
  private static int mMainOITNumRecordsH;
127

    
128
  /// BLIT PROGRAM ///
129
  private static DistortedProgram mBlitProgram;
130
  private static int mBlitTextureH;
131
  private static int mBlitDepthH;
132
  private static final FloatBuffer mQuadPositions;
133

    
134
  /// FULL PROGRAM ///
135
  private static DistortedProgram mFullProgram;
136

    
137
  static
138
    {
139
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
140
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
141
    mQuadPositions.put(positionData).position(0);
142
    }
143

    
144
  /// BLIT DEPTH PROGRAM ///
145
  private static DistortedProgram mBlitDepthProgram;
146
  private static int mBlitDepthTextureH;
147
  private static int mBlitDepthDepthTextureH;
148
  private static int mBlitDepthDepthH;
149
  private static int mBlitDepthTexCorrH;
150

    
151
  /// OIT SSBO BUFFER ///
152
  private static int[] mLinkedListSSBO = new int[1];
153
  private static int[] mAtomicCounter = new int[DistortedLibrary.FBO_QUEUE_SIZE];
154
  private static int   mCurrBuffer;
155

    
156
  static
157
    {
158
    mLinkedListSSBO[0]= -1;
159
    mCurrBuffer       =  0;
160

    
161
    for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++)  mAtomicCounter[i] = -1;
162
    }
163

    
164
  ///////////////////////////////////////////////////////////////
165
  // meaning: allocate 1.0 screenful of places for transparent
166
  // fragments in the SSBO backing up the OIT render method.
167
  private static float mBufferSize=1.0f;
168

    
169
  /// OIT CLEAR PROGRAM ///
170
  private static DistortedProgram mOITClearProgram;
171
  private static int mOITClearDepthH;
172
  private static int mOITClearTexCorrH;
173
  private static int mOITClearSizeH;
174

    
175
  /// OIT BUILD PROGRAM ///
176
  private static DistortedProgram mOITBuildProgram;
177
  private static int mOITBuildTextureH;
178
  private static int mOITBuildDepthTextureH;
179
  private static int mOITBuildDepthH;
180
  private static int mOITBuildTexCorrH;
181
  private static int mOITBuildSizeH;
182
  private static int mOITBuildNumRecordsH;
183

    
184
  /// OIT COLLAPSE PROGRAM ///
185
  private static DistortedProgram mOITCollapseProgram;
186
  private static int mOITCollapseDepthTextureH;
187
  private static int mOITCollapseDepthH;
188
  private static int mOITCollapseTexCorrH;
189
  private static int mOITCollapseSizeH;
190

    
191
  /// OIT RENDER PROGRAM ///
192
  private static DistortedProgram mOITRenderProgram;
193
  private static int mOITRenderDepthH;
194
  private static int mOITRenderTexCorrH;
195
  private static int mOITRenderSizeH;
196

    
197
  /// END PROGRAMS //////
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
// private: hide this from Javadoc
201

    
202
  private DistortedLibrary()
203
    {
204

    
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  private static void createPrograms(Resources resources)
210
    {
211
    // MAIN PROGRAM ////////////////////////////////////
212
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
213
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
214

    
215
    int numF = FragmentEffect.getNumEnabled();
216
    int numV = VertexEffect.getNumEnabled();
217

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

    
221
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxComponents() + "\n";
222

    
223
    String enabledEffectV= VertexEffect.getGLSL();
224
    String enabledEffectF= FragmentEffect.getGLSL();
225

    
226
    String[] feedback = { "v_Position", "v_endPosition" };
227

    
228
    try
229
      {
230
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
231
                                          enabledEffectV, enabledEffectF, mGLSL, feedback);
232
      }
233
    catch(Exception e)
234
      {
235
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
236
      throw new RuntimeException(e.getMessage());
237
      }
238

    
239
    int mainProgramH = mMainProgram.getProgramHandle();
240
    EffectQueue.getUniforms(mainProgramH,0);
241
    MeshBase.getUniforms(mainProgramH,0);
242
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
243

    
244
    // BLIT PROGRAM ////////////////////////////////////
245
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
246
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
247

    
248
    try
249
      {
250
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
251
      }
252
    catch(Exception e)
253
      {
254
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
255
      throw new RuntimeException(e.getMessage());
256
      }
257

    
258
    int blitProgramH = mBlitProgram.getProgramHandle();
259
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
260
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
261

    
262
    // BLIT DEPTH PROGRAM ////////////////////////////////////
263
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
264
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
265

    
266
    try
267
      {
268
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
269
      }
270
    catch(Exception e)
271
      {
272
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
273
      throw new RuntimeException(e.getMessage());
274
      }
275

    
276
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
277
    mBlitDepthTextureH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Texture");
278
    mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
279
    mBlitDepthDepthH        = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
280
    mBlitDepthTexCorrH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
281

    
282
    // NORMAL PROGRAM //////////////////////////////////////
283
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
284
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
285

    
286
    try
287
      {
288
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
289
      }
290
    catch(Exception e)
291
      {
292
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
293
      throw new RuntimeException(e.getMessage());
294
      }
295

    
296
    int normalProgramH = mNormalProgram.getProgramHandle();
297
    mNormalMVPMatrixH  = GLES30.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
  private static void createFullProgram(Resources resources)
303
    {
304
    final InputStream fullVertStream = resources.openRawResource(R.raw.main_vertex_shader);
305
    final InputStream fullFragStream = resources.openRawResource(R.raw.main_fragment_shader);
306

    
307
    int numV = VertexEffect.getAllEnabled();
308

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

    
312
    fullVertHeader += "#define MAX_COMPON " + MeshBase.getMaxComponents() + "\n";
313

    
314
    String enabledEffectV= VertexEffect.getAllGLSL();
315
    String enabledEffectF= "{}";
316

    
317
    fullVertHeader += "#define PREAPPLY\n";
318

    
319
    String[] feedback = { "v_Position", "v_endPosition", "v_Inflate" };
320

    
321
    try
322
      {
323
      mFullProgram = new DistortedProgram(fullVertStream, fullFragStream, fullVertHeader, fullFragHeader,
324
                                          enabledEffectV, enabledEffectF, mGLSL, feedback);
325
      }
326
    catch(Exception e)
327
      {
328
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile FULL program: "+e.getMessage());
329
      throw new RuntimeException(e.getMessage());
330
      }
331

    
332
    int fullProgramH = mFullProgram.getProgramHandle();
333
    EffectQueue.getUniforms(fullProgramH,3);
334
    MeshBase.getUniforms(fullProgramH,3);
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
  private static void createProgramsOIT(Resources resources)
340
    {
341
    // MAIN OIT PROGRAM ////////////////////////////////
342
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
343
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
344

    
345
    int numF = FragmentEffect.getNumEnabled();
346
    int numV = VertexEffect.getNumEnabled();
347

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

    
351
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxComponents() + "\n";
352

    
353
    String enabledEffectV= VertexEffect.getGLSL();
354
    String enabledEffectF= FragmentEffect.getGLSL();
355

    
356
    try
357
      {
358
      mMainOITProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
359
                                             enabledEffectV, enabledEffectF, mGLSL, null);
360
      }
361
    catch(Exception e)
362
      {
363
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN OIT program: "+e.getMessage());
364
      throw new RuntimeException(e.getMessage());
365
      }
366

    
367
    int mainOITProgramH = mMainOITProgram.getProgramHandle();
368
    EffectQueue.getUniforms(mainOITProgramH,1);
369
    MeshBase.getUniforms(mainOITProgramH,1);
370
    mMainOITTextureH    = GLES30.glGetUniformLocation( mainOITProgramH, "u_Texture");
371
    mMainOITSizeH       = GLES30.glGetUniformLocation( mainOITProgramH, "u_Size");
372
    mMainOITNumRecordsH = GLES30.glGetUniformLocation( mainOITProgramH, "u_numRecords");
373

    
374
    // OIT CLEAR PROGRAM ////////////////////////////////////
375
    final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
376
    final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader);
377

    
378
    try
379
      {
380
      mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
381
      }
382
    catch(Exception e)
383
      {
384
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage());
385
      throw new RuntimeException(e.getMessage());
386
      }
387

    
388
    int oitClearProgramH   = mOITClearProgram.getProgramHandle();
389
    mOITClearDepthH        = GLES30.glGetUniformLocation( oitClearProgramH, "u_Depth");
390
    mOITClearTexCorrH      = GLES30.glGetUniformLocation( oitClearProgramH, "u_TexCorr");
391
    mOITClearSizeH         = GLES30.glGetUniformLocation( oitClearProgramH, "u_Size");
392

    
393
    // OIT BUILD PROGRAM ////////////////////////////////////
394
    final InputStream oitBuildVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
395
    final InputStream oitBuildFragStream = resources.openRawResource(R.raw.oit_build_fragment_shader);
396

    
397
    try
398
      {
399
      mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
400
      }
401
    catch(Exception e)
402
      {
403
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage());
404
      throw new RuntimeException(e.getMessage());
405
      }
406

    
407
    int oitBuildProgramH   = mOITBuildProgram.getProgramHandle();
408
    mOITBuildTextureH      = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Texture");
409
    mOITBuildDepthTextureH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture");
410
    mOITBuildDepthH        = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Depth");
411
    mOITBuildTexCorrH      = GLES30.glGetUniformLocation( oitBuildProgramH, "u_TexCorr");
412
    mOITBuildSizeH         = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Size");
413
    mOITBuildNumRecordsH   = GLES30.glGetUniformLocation( oitBuildProgramH, "u_numRecords");
414

    
415
    // OIT COLLAPSE PROGRAM ///////////////////////////
416
    final InputStream oitCollapseVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
417
    final InputStream oitCollapseFragStream = resources.openRawResource(R.raw.oit_collapse_fragment_shader);
418

    
419
    try
420
      {
421
      mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
422
      }
423
    catch(Exception e)
424
      {
425
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage());
426
      throw new RuntimeException(e.getMessage());
427
      }
428

    
429
    int oitCollapseProgramH   = mOITCollapseProgram.getProgramHandle();
430
    mOITCollapseDepthTextureH = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture");
431
    mOITCollapseDepthH        = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Depth");
432
    mOITCollapseTexCorrH      = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr");
433
    mOITCollapseSizeH         = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Size");
434

    
435
    // OIT RENDER PROGRAM ///////////////////////////
436
    final InputStream oitRenderVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
437
    final InputStream oitRenderFragStream = resources.openRawResource(R.raw.oit_render_fragment_shader);
438

    
439
    try
440
      {
441
      mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
442
      }
443
    catch(Exception e)
444
      {
445
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage());
446
      throw new RuntimeException(e.getMessage());
447
      }
448

    
449
    int oitRenderProgramH   = mOITRenderProgram.getProgramHandle();
450
    mOITRenderDepthH        = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Depth");
451
    mOITRenderTexCorrH      = GLES30.glGetUniformLocation( oitRenderProgramH, "u_TexCorr");
452
    mOITRenderSizeH         = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Size");
453
    }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456

    
457
  private static void displayNormals(EffectQueue[] queues, MeshBase mesh)
458
    {
459
    int num = mesh.getNumVertices();
460
    int tfo = mesh.getTFO();
461

    
462
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
463
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
464
    InternalRenderState.switchOffDrawing();
465
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
466
    InternalRenderState.restoreDrawing();
467
    GLES30.glEndTransformFeedback();
468
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
469

    
470
    mNormalProgram.useProgram();
471
    GLES30.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, EffectQueue.getMVP(queues) , 0);
472
    mesh.bindTransformAttribs(mNormalProgram);
473
    GLES30.glLineWidth(8.0f);
474
    GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*num);
475
    }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478
// execute all VertexEffects and adjust all vertices
479

    
480
  public static void adjustVertices(MeshBase mesh, EffectQueueVertex queue)
481
    {
482
    if( mFullProgram!=null )
483
      {
484
      GLES30.glViewport(0, 0, 500, 500 ); // this is fine, we don't need viewport at all
485

    
486
      int num = mesh.getNumVertices();
487
      int tfo = mesh.getTFO();
488

    
489
      mFullProgram.useProgram();
490
      mesh.bindVertexAttribs(mFullProgram);
491
      queue.compute(1);
492
      queue.send(0.0f,3);
493
      mesh.send(3);
494

    
495
      GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
496
      GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
497
      InternalRenderState.switchOffDrawing();
498
      GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
499
      InternalRenderState.restoreDrawing();
500
      GLES30.glEndTransformFeedback();
501
      mesh.copyTransformToVertex();
502
      GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
503
      }
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

    
508
  static void drawPrivOIT(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
509
    {
510
    EffectQueue[] queues = effects.getQueues();
511

    
512
    EffectQueue.compute(queues, currTime);
513
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
514

    
515
    DistortedLibrary.mMainOITProgram.useProgram();
516
    GLES30.glUniform1i(DistortedLibrary.mMainOITTextureH, 0);
517
    GLES30.glUniform2ui(DistortedLibrary.mMainOITSizeH, surface.mWidth, surface.mHeight);
518
    GLES30.glUniform1ui(DistortedLibrary.mMainOITNumRecordsH, (int)(DistortedLibrary.mBufferSize*surface.mWidth*surface.mHeight) );
519
    mesh.bindVertexAttribs(DistortedLibrary.mMainOITProgram);
520
    mesh.send(1);
521

    
522
    float inflate     = mesh.getInflate();
523
    float distance    = surface.mDistance;
524
    float mipmap      = surface.mMipmap;
525
    float[] projection= surface.mProjectionMatrix;
526

    
527
    EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 1 );
528
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
529

    
530
    if( mesh.getShowNormals() )
531
      {
532
      DistortedLibrary.mMainProgram.useProgram();
533
      mesh.send(0);
534
      EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 0 );
535
      displayNormals(queues,mesh);
536
      }
537
    }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

    
541
  static void drawPriv(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
542
    {
543
    if( mMainProgram!=null )
544
      {
545
      EffectQueue[] queues = effects.getQueues();
546

    
547
      EffectQueue.compute(queues, currTime);
548
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
549

    
550
      mMainProgram.useProgram();
551
      GLES30.glUniform1i(DistortedLibrary.mMainTextureH, 0);
552
      mesh.bindVertexAttribs(DistortedLibrary.mMainProgram);
553
      mesh.send(0);
554

    
555
      float inflate     = mesh.getInflate();
556
      float distance    = surface.mDistance;
557
      float mipmap      = surface.mMipmap;
558
      float[] projection= surface.mProjectionMatrix;
559

    
560
      EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 0 );
561
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
562

    
563
      if( mesh.getShowNormals() ) displayNormals(queues,mesh);
564
      }
565
    }
566

    
567
///////////////////////////////////////////////////////////////////////////////////////////////////
568

    
569
  static void blitPriv(InternalOutputSurface surface)
570
    {
571
    if( mBlitProgram!=null )
572
      {
573
      mBlitProgram.useProgram();
574

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

    
583
///////////////////////////////////////////////////////////////////////////////////////////////////
584

    
585
  static void blitDepthPriv(InternalOutputSurface surface, float corrW, float corrH)
586
    {
587
    if( mBlitDepthProgram!=null )
588
      {
589
      mBlitDepthProgram.useProgram();
590

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

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602
// yes it is safe to be mixing 3.0 and 3.1 like that, senior members of the OpenGL discussions forum assert
603

    
604
  private static int printPreviousBuffer()
605
    {
606
    int counter = 0;
607

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

    
620
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
621

    
622
    return counter;
623
    }
624

    
625
///////////////////////////////////////////////////////////////////////////////////////////////////
626

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

    
641
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
642
    }
643

    
644
///////////////////////////////////////////////////////////////////////////////////////////////////
645
// reset atomic counter to 0
646

    
647
  static int zeroOutAtomic()
648
    {
649
    int counter = 0;
650

    
651
    if( mAtomicCounter[0]<0 )
652
      {
653
      GLES30.glGenBuffers(DistortedLibrary.FBO_QUEUE_SIZE,mAtomicCounter,0);
654

    
655
      for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++)
656
        {
657
        GLES30.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]);
658
        GLES30.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES30.GL_DYNAMIC_DRAW);
659
        zeroBuffer();
660
        }
661
      }
662

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

    
671
    if( ++mCurrBuffer>= DistortedLibrary.FBO_QUEUE_SIZE ) mCurrBuffer = 0;
672

    
673
    GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
674
    zeroBuffer();
675

    
676
    return counter;
677
    }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680
// Pass1 of the OIT algorithm. Clear per-pixel head-pointers.
681

    
682
  static void oitClear(InternalOutputSurface surface, int counter)
683
    {
684
    if( mOITClearProgram!=null )
685
      {
686
      if( mLinkedListSSBO[0]<0 )
687
        {
688
        GLES30.glGenBuffers(1,mLinkedListSSBO,0);
689

    
690
        int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
691
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
692
        GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
693
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
694

    
695
        GLES30.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
696
        }
697

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

    
702
      if( overflow>1.0f )
703
        {
704
        //android.util.Log.e("effects", "previous frame rendered "+counter+
705
        //                   " fragments, but there was only "+(mBufferSize*surface.mWidth*surface.mHeight)+" space");
706

    
707
        mBufferSize *= (int)(overflow+1.0f);
708
        int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
709
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
710
        GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
711
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
712
        }
713

    
714
      mOITClearProgram.useProgram();
715

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

    
725
///////////////////////////////////////////////////////////////////////////////////////////////////
726
// Pass2 of the OIT algorithm - build per-pixel linked lists.
727

    
728
  static void oitBuild(InternalOutputSurface surface, float corrW, float corrH)
729
    {
730
    if( mOITBuildProgram!=null )
731
      {
732
      mOITBuildProgram.useProgram();
733

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

    
746
///////////////////////////////////////////////////////////////////////////////////////////////////
747
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list.
748

    
749
  static void oitCollapse(InternalOutputSurface surface, float corrW, float corrH)
750
    {
751
    if( mOITCollapseProgram!=null )
752
      {
753
      mOITCollapseProgram.useProgram();
754

    
755
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
756
      GLES30.glUniform1i(mOITCollapseDepthTextureH, 1);
757
      GLES30.glUniform2f(mOITCollapseTexCorrH, corrW, corrH );
758
      GLES30.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight);
759
      GLES30.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear);
760
      GLES30.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
761
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
762
      }
763
    }
764

    
765
///////////////////////////////////////////////////////////////////////////////////////////////////
766
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists.
767

    
768
  static void oitRender(InternalOutputSurface surface, float corrW, float corrH)
769
    {
770
    if( mOITRenderProgram!=null )
771
      {
772
      mOITRenderProgram.useProgram();
773

    
774
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
775
      GLES30.glUniform2f(mOITRenderTexCorrH, corrW, corrH );
776
      GLES30.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight);
777
      GLES30.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear);
778
      GLES30.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
779
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
780
      }
781
    }
782

    
783
///////////////////////////////////////////////////////////////////////////////////////////////////
784

    
785
  static void setSSBOSize(float size)
786
    {
787
    mBufferSize = size;
788
    }
789

    
790
///////////////////////////////////////////////////////////////////////////////////////////////////
791
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
792
// PowerVR GE8100 / GE8300 compiler fails to compile OIT programs.
793

    
794
  private static void detectBuggyDrivers()
795
    {
796
    String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
797
    String version = GLES30.glGetString(GLES30.GL_VERSION);
798
    String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
799

    
800
    /*
801
    android.util.Log.e("DISTORTED", "GLSL Version "+GLES30.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
802
    android.util.Log.e("DISTORTED", "GL Version "  +GLES30.glGetString(GLES31.GL_VERSION));
803
    android.util.Log.e("DISTORTED", "GL Vendor "   +GLES30.glGetString(GLES31.GL_VENDOR));
804
    android.util.Log.e("DISTORTED", "GL Renderer " +GLES30.glGetString(GLES31.GL_RENDERER));
805
    */
806

    
807
    if( vendor.contains("ARM") )
808
      {
809
      if( version.contains("r12") )
810
        {
811
        Log.e("DISTORTED", "You are running this on a ARM Mali driver r12.\nThis is a buggy driver, please update to r22. Problems with flashing expected.");
812
        }
813
      }
814
    else if( vendor.contains("Imagination") )
815
      {
816
      if( renderer.contains("GE8") )
817
        {
818
        Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
819
        Log.e("DISTORTED", "GLSL Version "+GLES30.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
820
        }
821
      }
822
    }
823

    
824
///////////////////////////////////////////////////////////////////////////////////////////////////
825
/**
826
 * Return OpenGL ES version supported by the hardware we are running on.
827
 * There are only three possibilities: 300 (OpenGL ES 3.0) or 310 (at least OpenGL ES 3.1)
828
 * or 200 (OpenGL ES 2.0)
829
 */
830
  public static int getGLSL()
831
    {
832
    return mGLSL;
833
    }
834

    
835
///////////////////////////////////////////////////////////////////////////////////////////////////
836
/**
837
 * Have we successfully managed to compile the OIT programs?
838
 * Some hardware ( Imagination GE8100 / 8300, driver build 1.8@4490469 present in my HTC phone and,
839
 * sadly, Amazon Fire Stick 4K ) fails to compile the shaders. See relevant thread in Imagination's
840
 * support forum:
841
 *
842
 * https://forums.imgtec.com/t/ge8100-in-htc-desire-12-android-7-1-1-fragment-shader-fails-to-compile/2708
843
 */
844
  public static boolean OITCompilationSuccessful()
845
    {
846
    return mOITCompilationSuccessful;
847
    }
848

    
849
///////////////////////////////////////////////////////////////////////////////////////////////////
850
/**
851
 * Have we called onCreate yet, ie have we initialized the library?
852
 * @return <code>true</code> if the library is initialized and ready for action.
853
 */
854
  public static boolean isInitialized()
855
    {
856
    return mInitialized;
857
    }
858

    
859
///////////////////////////////////////////////////////////////////////////////////////////////////
860
/**
861
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
862
 * I.e. best called from GLSurfaceView.onCreate().
863
 * <p>
864
 * Needs to be called from a thread holding the OpenGL context.
865
 *   
866
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
867
 */
868
  public static void onCreate(final Context context) throws Exception
869
    {
870
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
871
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
872

    
873
    int glESversion = configurationInfo.reqGlEsVersion;
874
    int major = glESversion >> 16;
875
    int minor = glESversion & 0xff;
876

    
877
    if( major< 3 )
878
      {
879
      mGLSL = 100*major + 10*minor;
880
      throw new VertexCompilationException("at least OpenGL ES 3.0 required, this device supports only "+major+"."+minor);
881
      }
882
    else
883
      {
884
      mGLSL = (major==3 && minor==0) ? 300 : 310;
885
      }
886

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

    
890
    mInitialized = true;
891
    mOITCompilationSuccessful = false;
892

    
893
    detectBuggyDrivers();
894

    
895
    EffectMessageSender.startSending();
896

    
897
    final Resources resources = context.getResources();
898

    
899
    Exception exception=null;
900

    
901
    try
902
      {
903
      createPrograms(resources);
904
      }
905
    catch(Exception ex)
906
      {
907
      exception = ex;
908
      }
909

    
910
    try
911
      {
912
      createFullProgram(resources);
913
      }
914
    catch(Exception ex)
915
      {
916
      exception = ex;
917
      }
918

    
919
    if( mGLSL>=310)
920
      {
921
      try
922
        {
923
        createProgramsOIT(resources);
924
        mOITCompilationSuccessful = true;
925
        }
926
      catch(Exception ex)
927
        {
928
        exception = ex;
929
        }
930
      }
931

    
932
    try
933
      {
934
      EffectQueuePostprocess.createPrograms(resources, mGLSL);
935
      }
936
    catch(Exception ex)
937
      {
938
      exception = ex;
939
      }
940

    
941
    try
942
      {
943
      PostprocessEffect.createPrograms(mGLSL);
944
      }
945
    catch(Exception ex)
946
      {
947
      exception = ex;
948
      }
949

    
950
    if( exception!=null)
951
      {
952
      throw exception;
953
      }
954
    }
955

    
956
///////////////////////////////////////////////////////////////////////////////////////////////////
957
/**
958
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
959
 * Must be called from Activity.onPause().
960
 */
961
  public static void onPause()
962
    {
963
    InternalObject.onPause();
964

    
965
    mLinkedListSSBO[0]= -1;
966

    
967
    for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++) mAtomicCounter[i] = -1;
968
    }
969

    
970
///////////////////////////////////////////////////////////////////////////////////////////////////
971
/**
972
 * Call this so that the Library can release its internal data structures.
973
 * Must be called from Activity.onDestroy(). 
974
 */
975
  public static void onDestroy()
976
    {
977
    InternalObject.onDestroy();
978
    InternalNodeData.onDestroy();
979
    InternalMaster.onDestroy();
980
    InternalOutputSurface.onDestroy();
981
    DistortedEffects.onDestroy();
982
    EffectQueue.onDestroy();
983
    Effect.onDestroy();
984
    DeferredJobs.onDestroy();
985
    EffectMessageSender.stopSending();
986

    
987
    mInitialized = false;
988
    }
989

    
990
///////////////////////////////////////////////////////////////////////////////////////////////////
991
/**
992
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
993
 * single (InputSurface,MeshBase) combo.
994
 *
995
 * @param type {@link EffectType}
996
 * @return The maximum number of effects of a given type.
997
 */
998
  @SuppressWarnings("unused")
999
  public static int getMax(EffectType type)
1000
    {
1001
    return EffectQueue.getMax(type.ordinal());
1002
    }
1003

    
1004
///////////////////////////////////////////////////////////////////////////////////////////////////
1005
/**
1006
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
1007
 * This can fail if:
1008
 * <ul>
1009
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
1010
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
1011
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link DistortedLibrary#onCreate}. After this
1012
 *     time only decreasing the value of 'max' is permitted.
1013
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
1014
 * </ul>
1015
 *
1016
 * @param type {@link EffectType}
1017
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
1018
 *            than Byte.MAX_VALUE
1019
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
1020
 */
1021
  @SuppressWarnings("unused")
1022
  public static boolean setMax(EffectType type, int max)
1023
    {
1024
    return EffectQueue.setMax(type.ordinal(),max);
1025
    }
1026
  }
(3-3/14)