Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedEffects.java @ 8a57da61

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
    mM.compute(currTime);
278
    mV.compute(currTime,halfW,halfH,halfZ);
279
    mF.compute(currTime,halfW,halfH);
280
    mP.compute(currTime);
281

    
282
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
283

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

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

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

    
310
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
311

    
312
    if( mesh.mShowNormals ) displayNormals(mesh);
313
    }
314

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

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

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
  static void blitDepthPriv(DistortedOutputSurface surface)
335
    {
336
    mBlitDepthProgram.useProgram();
337

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

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

    
355
    mM = null;
356
    mV = null;
357
    mF = null;
358
    mP = null;
359
    }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

    
363
  static void onDestroy()
364
    {
365
    mNextID = 0;
366
    }
367

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

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

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

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

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

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

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

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

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

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

    
493
    return 0;
494
    }
495

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

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

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

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

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

    
588
    return false;
589
    }
590
  }
(2-2/22)