Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedEffects.java @ ae2802b1

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.content.res.Resources;
23
import android.opengl.GLES31;
24
import android.util.Log;
25

    
26
import org.distorted.library.R;
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.EffectName;
29
import org.distorted.library.effect.EffectType;
30
import org.distorted.library.effect.FragmentEffect;
31
import org.distorted.library.effect.VertexEffect;
32
import org.distorted.library.message.EffectListener;
33
import org.distorted.library.program.DistortedProgram;
34
import org.distorted.library.program.FragmentCompilationException;
35
import org.distorted.library.program.FragmentUniformsException;
36
import org.distorted.library.program.LinkingException;
37
import org.distorted.library.program.VertexCompilationException;
38
import org.distorted.library.program.VertexUniformsException;
39

    
40
import java.io.InputStream;
41
import java.nio.ByteBuffer;
42
import java.nio.ByteOrder;
43
import java.nio.FloatBuffer;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
/**
47
 * Class containing Matrix, Vertex, Fragment and Postprocessing effect queues.
48
 * <p>
49
 * The queues hold actual effects to be applied to a given (InputSurface,MeshObject) combo.
50
 */
51
public class DistortedEffects
52
  {
53
  /// MAIN PROGRAM ///
54
  private static DistortedProgram mMainProgram;
55
  private static int mMainTextureH;
56
  private static int mCountIndexH;
57

    
58
  /// BLIT PROGRAM ///
59
  private static DistortedProgram mBlitProgram;
60
  private static int mBlitTextureH;
61
  private static int mBlitDepthH;
62
  private static final FloatBuffer mQuadPositions;
63

    
64
  static
65
    {
66
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
67
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
68
    mQuadPositions.put(positionData).position(0);
69
    }
70

    
71
  /// BLIT DEPTH PROGRAM ///
72
  private static DistortedProgram mBlitDepthProgram;
73
  private static int mBlitDepthTextureH;
74
  private static int mBlitDepthDepthTextureH;
75
  private static int mBlitDepthDepthH;
76
  private static int mBlitDepthTexCorrH;
77

    
78
  /// NORMAL PROGRAM /////
79
  private static DistortedProgram mNormalProgram;
80
  private static int mNormalMVPMatrixH;
81
  /// END PROGRAMS //////
82

    
83
  private static long mNextID =0;
84
  private long mID;
85

    
86
  private EffectQueueMatrix mM;
87
  private EffectQueueFragment mF;
88
  private EffectQueueVertex mV;
89
  private EffectQueuePostprocess mP;
90

    
91
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  static void createProgram(Resources resources)
96
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
97
    {
98
    // MAIN PROGRAM ////////////////////////////////////
99
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
100
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
101

    
102
    int numF = FragmentEffect.getNumEnabled();
103
    int numV = VertexEffect.getNumEnabled();
104

    
105
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
106
    String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
107
    String enabledEffectV= VertexEffect.getGLSL();
108
    String enabledEffectF= FragmentEffect.getGLSL();
109

    
110
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
111
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
112
    //android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
113
    //android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
114

    
115
    String[] feedback = { "v_Position", "v_endPosition" };
116

    
117
    mMainProgram = new DistortedProgram( mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
118
                                         enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
119

    
120
    int mainProgramH = mMainProgram.getProgramHandle();
121
    EffectQueueFragment.getUniforms(mainProgramH);
122
    EffectQueueVertex.getUniforms(mainProgramH);
123
    EffectQueueMatrix.getUniforms(mainProgramH);
124
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
125
    mCountIndexH = GLES31.glGetUniformLocation( mainProgramH, "u_currentIndex");
126

    
127
    // BLIT PROGRAM ////////////////////////////////////
128
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
129
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
130

    
131
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
132
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
133

    
134
    try
135
      {
136
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
137
      }
138
    catch(Exception e)
139
      {
140
      Log.e("EFFECTS", "exception trying to compile BLIT program: "+e.getMessage());
141
      throw new RuntimeException(e.getMessage());
142
      }
143

    
144
    int blitProgramH = mBlitProgram.getProgramHandle();
145
    mBlitTextureH  = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
146
    mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
147

    
148
    // BLIT DEPTH PROGRAM ////////////////////////////////////
149
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
150
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
151

    
152
    try
153
      {
154
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
155
      }
156
    catch(Exception e)
157
      {
158
      Log.e("EFFECTS", "exception trying to compile BLIT DEPTH program: "+e.getMessage());
159
      throw new RuntimeException(e.getMessage());
160
      }
161

    
162
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
163
    mBlitDepthTextureH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
164
    mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
165
    mBlitDepthDepthH        = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
166
    mBlitDepthTexCorrH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
167

    
168
    // NORMAL PROGRAM //////////////////////////////////////
169
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
170
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
171

    
172
    try
173
      {
174
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
175
      }
176
    catch(Exception e)
177
      {
178
      Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
179
      throw new RuntimeException(e.getMessage());
180
      }
181

    
182
    int normalProgramH = mNormalProgram.getProgramHandle();
183
    mNormalMVPMatrixH  = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void initializeEffectLists(DistortedEffects d, int flags)
189
    {
190
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
191
      {
192
      mM = d.mM;
193
      matrixCloned = true;
194
      }
195
    else
196
      {
197
      mM = new EffectQueueMatrix(mID);
198
      matrixCloned = false;
199
      }
200
    
201
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
202
      {
203
      mV = d.mV;
204
      vertexCloned = true;
205
      }
206
    else
207
      {
208
      mV = new EffectQueueVertex(mID);
209
      vertexCloned = false;
210
      }
211
    
212
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
213
      {
214
      mF = d.mF;
215
      fragmentCloned = true;
216
      }
217
    else
218
      {
219
      mF = new EffectQueueFragment(mID);
220
      fragmentCloned = false;
221
      }
222

    
223
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
224
      {
225
      mP = d.mP;
226
      postprocessCloned = true;
227
      }
228
    else
229
      {
230
      mP = new EffectQueuePostprocess(mID);
231
      postprocessCloned = false;
232
      }
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  EffectQueuePostprocess getPostprocess()
238
    {
239
    return mP;
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  void newNode(DistortedNode node)
245
    {
246
    mM.newNode(node);
247
    mF.newNode(node);
248
    mV.newNode(node);
249
    mP.newNode(node);
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  private void displayNormals(MeshObject mesh)
255
    {
256
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
257
    GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
258
    DistortedRenderState.switchOffDrawing();
259
    GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
260
    DistortedRenderState.restoreDrawing();
261
    GLES31.glEndTransformFeedback();
262
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
263

    
264
    mNormalProgram.useProgram();
265
    GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
266
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
267
    GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
268
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
269
    GLES31.glLineWidth(8.0f);
270
    GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*mesh.numVertices);
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
276
    {
277
    float halfZ = halfW*mesh.zFactor;
278

    
279
    mM.compute(currTime);
280
    mV.compute(currTime,halfW,halfH,halfZ);
281
    mF.compute(currTime,halfW,halfH);
282
    mP.compute(currTime);
283

    
284
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
285

    
286
    mMainProgram.useProgram();
287
    GLES31.glUniform1i(mMainTextureH, 0);
288
    GLES31.glUniform1i(mCountIndexH, surface.getNewCounter() );
289

    
290
    if( Distorted.GLSL >= 300 )
291
      {
292
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
293
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
294
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
295
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
296
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
297
      }
298
    else
299
      {
300
      mesh.mVertAttribs.position(0);
301
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
302
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE);
303
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
304
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE);
305
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
306
      }
307

    
308
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
309
    mV.send();
310
    mF.send();
311

    
312
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
313

    
314
    if( mesh.mShowNormals ) displayNormals(mesh);
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318
/**
319
 * Only for use by the library itself.
320
 *
321
 * @y.exclude
322
 */
323
  public static void blitPriv(DistortedOutputSurface surface)
324
    {
325
    mBlitProgram.useProgram();
326

    
327
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
328
    GLES31.glUniform1i(mBlitTextureH, 0);
329
    GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
330
    GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
331
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
337
    {
338
    mBlitDepthProgram.useProgram();
339

    
340
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
341
    GLES31.glUniform1i(mBlitDepthTextureH, 0);
342
    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
343
    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
344
    GLES31.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
345
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
346
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
   
351
  private void releasePriv()
352
    {
353
    if( !matrixCloned   )   mM.abortAll(false);
354
    if( !vertexCloned   )   mV.abortAll(false);
355
    if( !fragmentCloned )   mF.abortAll(false);
356
    if( !postprocessCloned) mP.abortAll(false);
357

    
358
    mM = null;
359
    mV = null;
360
    mF = null;
361
    mP = null;
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
  static void onDestroy()
367
    {
368
    mNextID = 0;
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372
// PUBLIC API
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374
/**
375
 * Create empty effect queue.
376
 */
377
  public DistortedEffects()
378
    {
379
    mID = ++mNextID;
380
    initializeEffectLists(this,0);
381
    }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384
/**
385
 * Copy constructor.
386
 * <p>
387
 * Whatever we do not clone gets created just like in the default constructor.
388
 *
389
 * @param dc    Source object to create our object from
390
 * @param flags A bitmask of values specifying what to copy.
391
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
392
 */
393
  public DistortedEffects(DistortedEffects dc, int flags)
394
    {
395
    mID = ++mNextID;
396
    initializeEffectLists(dc,flags);
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400
/**
401
 * Releases all resources. After this call, the queue should not be used anymore.
402
 */
403
  @SuppressWarnings("unused")
404
  public synchronized void delete()
405
    {
406
    releasePriv();
407
    }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410
/**
411
 * Returns unique ID of this instance.
412
 *
413
 * @return ID of the object.
414
 */
415
  public long getID()
416
      {
417
      return mID;
418
      }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421
/**
422
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
423
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
424
 * 
425
 * @param el A class implementing the EffectListener interface that wants to get notifications.
426
 */
427
  @SuppressWarnings("unused")
428
  public void registerForMessages(EffectListener el)
429
    {
430
    mM.registerForMessages(el);
431
    mV.registerForMessages(el);
432
    mF.registerForMessages(el);
433
    mP.registerForMessages(el);
434
    }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
/**
438
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
439
 * 
440
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
441
 */
442
  @SuppressWarnings("unused")
443
  public void deregisterForMessages(EffectListener el)
444
    {
445
    mM.deregisterForMessages(el);
446
    mV.deregisterForMessages(el);
447
    mF.deregisterForMessages(el);
448
    mP.deregisterForMessages(el);
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452
/**
453
 * Aborts all Effects.
454
 * @return Number of effects aborted.
455
 */
456
  public int abortAllEffects()
457
    {
458
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462
/**
463
 * Aborts all Effects of a given type, for example all MATRIX Effects.
464
 * 
465
 * @param type one of the constants defined in {@link EffectType}
466
 * @return Number of effects aborted.
467
 */
468
  public int abortByType(EffectType type)
469
    {
470
    switch(type)
471
      {
472
      case MATRIX     : return mM.abortAll(true);
473
      case VERTEX     : return mV.abortAll(true);
474
      case FRAGMENT   : return mF.abortAll(true);
475
      case POSTPROCESS: return mP.abortAll(true);
476
      default         : return 0;
477
      }
478
    }
479

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481
/**
482
 * Aborts an Effect by its ID.
483
 *
484
 * @param id the Id of the Effect to be removed, as returned by getID().
485
 * @return Number of effects aborted.
486
 */
487
  public int abortById(long id)
488
    {
489
    long type = id&EffectType.MASK;
490

    
491
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
492
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
493
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
494
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
495

    
496
    return 0;
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500
/**
501
 * Aborts a single Effect.
502
 * 
503
 * @param effect the Effect we want to abort.
504
 * @return number of Effects aborted. Always either 0 or 1.
505
 */
506
  public int abortEffect(Effect effect)
507
    {
508
    switch(effect.getType())
509
      {
510
      case MATRIX     : return mM.removeEffect(effect);
511
      case VERTEX     : return mV.removeEffect(effect);
512
      case FRAGMENT   : return mF.removeEffect(effect);
513
      case POSTPROCESS: return mP.removeEffect(effect);
514
      default         : return 0;
515
      }
516
    }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519
/**
520
 * Abort all Effects of a given name, for example all rotations.
521
 * 
522
 * @param name one of the constants defined in {@link EffectName}
523
 * @return number of Effects aborted.
524
 */
525
  public int abortByName(EffectName name)
526
    {
527
    switch(name.getType())
528
      {
529
      case MATRIX     : return mM.removeByName(name);
530
      case VERTEX     : return mV.removeByName(name);
531
      case FRAGMENT   : return mF.removeByName(name);
532
      case POSTPROCESS: return mP.removeByName(name);
533
      default                : return 0;
534
      }
535
    }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538
/**
539
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
540
 * single (InputSurface,MeshObject) combo.
541
 *
542
 * @param type {@link EffectType}
543
 * @return The maximum number of effects of a given type.
544
 */
545
  @SuppressWarnings("unused")
546
  public static int getMax(EffectType type)
547
    {
548
    return EffectQueue.getMax(type.ordinal());
549
    }
550

    
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552
/**
553
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
554
 * This can fail if:
555
 * <ul>
556
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
557
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
558
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
559
 *     time only decreasing the value of 'max' is permitted.
560
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
561
 * </ul>
562
 *
563
 * @param type {@link EffectType}
564
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
565
 *            than Byte.MAX_VALUE
566
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
567
 */
568
  @SuppressWarnings("unused")
569
  public static boolean setMax(EffectType type, int max)
570
    {
571
    return EffectQueue.setMax(type.ordinal(),max);
572
    }
573

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575
/**
576
 * Add a new Effect to our queue.
577
 *
578
 * @param effect The Effect to add.
579
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
580
 */
581
  public boolean apply(Effect effect)
582
    {
583
    switch(effect.getType())
584
      {
585
      case MATRIX      : return mM.add(effect);
586
      case VERTEX      : return mV.add(effect);
587
      case FRAGMENT    : return mF.add(effect);
588
      case POSTPROCESS : return mP.add(effect);
589
      }
590

    
591
    return false;
592
    }
593
  }
(2-2/22)