Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedEffects.java @ 27cd6b98

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

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

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

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

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

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

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

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  EffectQueuePostprocess getPostprocess()
236
    {
237
    return mP;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

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

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

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

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

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

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

    
277
    android.util.Log.d("surface", "SurfaceID "+ surface.getID()+" transparent fragments: "+ surface.returnOldCounter() );
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.resetNewCounter() );
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)
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.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
344
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
345
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
346
    }
347

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

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

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

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

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

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

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

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

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

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

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

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

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

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

    
495
    return 0;
496
    }
497

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

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

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

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

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

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