Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedEffects.java @ 3521c6fe

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.GLES30;
24

    
25
import org.distorted.library.R;
26
import org.distorted.library.effect.Effect;
27
import org.distorted.library.effect.EffectName;
28
import org.distorted.library.effect.EffectQuality;
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
import java.util.ArrayList;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
/**
48
 * Class containing Matrix,Vertex and Fragment effect queues. Postprocessing queue is held in a separate
49
 * class.
50
 * <p>
51
 * The queues hold actual effects to be applied to a given (DistortedTexture,MeshObject) combo.
52
 */
53
public class DistortedEffects implements DistortedSlave
54
  {
55
  private static final int MIPMAP = 0;
56

    
57
  /// MAIN PROGRAM ///
58
  private static DistortedProgram mMainProgram;
59
  private static int mMainTextureH;
60

    
61
  /// BLIT PROGRAM ///
62
  private static DistortedProgram mBlitProgram;
63
  private static int mBlitTextureH;
64
  private static int mBlitDepthH;
65
  private static final FloatBuffer mQuadPositions;
66

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

    
74
  /// BLIT DEPTH PROGRAM ///
75
  private static DistortedProgram mBlitDepthProgram;
76
  private static int mBlitDepthTextureH;
77
  private static int mBlitDepthDepthTextureH;
78
  private static int mBlitDepthDepthH;
79

    
80
  /// NORMAL PROGRAM /////
81
  private static DistortedProgram mNormalProgram;
82
  private static int mNormalMVPMatrixH;
83
  /// END PROGRAMS //////
84

    
85
  private static long mNextID =0;
86
  private long mID;
87

    
88
  private EffectQueueMatrix mM;
89
  private EffectQueueFragment mF;
90
  private EffectQueueVertex mV;
91
  private EffectQueuePostprocess mP;
92

    
93
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
94

    
95
  private class Job
96
    {
97
    int type;
98
    int level;
99

    
100
    Job(int t, int l)
101
      {
102
      type = t;
103
      level= l;
104
      }
105
    }
106

    
107
  private ArrayList<Job> mJobs = new ArrayList<>();
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  static void createProgram(Resources resources)
112
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
113
    {
114
    // MAIN PROGRAM ////////////////////////////////////
115
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
116
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
117

    
118
    int numF = FragmentEffect.getNumEnabled();
119
    int numV = VertexEffect.getNumEnabled();
120

    
121
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
122
    String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
123
    String enabledEffectV= VertexEffect.getGLSL();
124
    String enabledEffectF= FragmentEffect.getGLSL();
125

    
126
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
127
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
128
    //android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
129
    //android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
130

    
131
    String[] feedback = { "v_Position", "v_endPosition" };
132

    
133
    mMainProgram = new DistortedProgram( mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
134
                                         enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
135

    
136
    int mainProgramH = mMainProgram.getProgramHandle();
137
    EffectQueueFragment.getUniforms(mainProgramH);
138
    EffectQueueVertex.getUniforms(mainProgramH);
139
    EffectQueueMatrix.getUniforms(mainProgramH);
140
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
141

    
142
    // BLIT PROGRAM ////////////////////////////////////
143
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
144
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
145

    
146
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
147
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
148

    
149
    try
150
      {
151
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
152
      }
153
    catch(Exception e)
154
      {
155
      android.util.Log.e("EFFECTS", "exception trying to compile BLIT program: "+e.getMessage());
156
      throw new RuntimeException(e.getMessage());
157
      }
158

    
159
    int blitProgramH = mBlitProgram.getProgramHandle();
160
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
161
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
162

    
163
    // BLIT DEPTH PROGRAM ////////////////////////////////////
164
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
165
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
166

    
167
    try
168
      {
169
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
170
      }
171
    catch(Exception e)
172
      {
173
      android.util.Log.e("EFFECTS", "exception trying to compile BLIT DEPTH program: "+e.getMessage());
174
      throw new RuntimeException(e.getMessage());
175
      }
176

    
177
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
178
    mBlitDepthTextureH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Texture");
179
    mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
180
    mBlitDepthDepthH        = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
181

    
182
    // NORMAL PROGRAM //////////////////////////////////////
183
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
184
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
185

    
186
    try
187
      {
188
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
189
      }
190
    catch(Exception e)
191
      {
192
      android.util.Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
193
      throw new RuntimeException(e.getMessage());
194
      }
195

    
196
    int normalProgramH = mNormalProgram.getProgramHandle();
197
    mNormalMVPMatrixH  = GLES30.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private void initializeEffectLists(DistortedEffects d, int flags)
203
    {
204
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
205
      {
206
      mM = d.mM;
207
      matrixCloned = true;
208
      }
209
    else
210
      {
211
      mM = new EffectQueueMatrix(mID);
212
      matrixCloned = false;
213
      }
214
    
215
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
216
      {
217
      mV = d.mV;
218
      vertexCloned = true;
219
      }
220
    else
221
      {
222
      mV = new EffectQueueVertex(mID);
223
      vertexCloned = false;
224
      }
225
    
226
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
227
      {
228
      mF = d.mF;
229
      fragmentCloned = true;
230
      }
231
    else
232
      {
233
      mF = new EffectQueueFragment(mID);
234
      fragmentCloned = false;
235
      }
236

    
237
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
238
      {
239
      mP = d.mP;
240
      postprocessCloned = true;
241
      }
242
    else
243
      {
244
      mP = new EffectQueuePostprocess(mID);
245
      postprocessCloned = false;
246
      }
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  int postprocess(DistortedOutputSurface surface)
252
    {
253
    return mP.postprocess(surface);
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  long getBucket()
259
    {
260
    return mP.getID();
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  int getQuality()
266
    {
267
    return mP.mQualityLevel;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  int getHalo()
273
    {
274
    return mP.getHalo();
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  void newNode(DistortedNode node)
280
    {
281
    mM.newNode(node);
282
    mF.newNode(node);
283
    mV.newNode(node);
284
    mP.newNode(node);
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
/**
289
 * This is not really part of the public API. Has to be public only because it is a part of the
290
 * DistortedSlave interface, which should really be a class that we extend here instead but
291
 * Java has no multiple inheritance.
292
 *
293
 * @y.exclude
294
 */
295
  public void doWork()
296
    {
297
    int num = mJobs.size();
298
    Job job;
299

    
300
    for(int i=0; i<num; i++)
301
      {
302
      job = mJobs.remove(0);
303

    
304
      switch(job.type)
305
        {
306
        case MIPMAP: int level = job.level;
307
                     mP.mQualityLevel = level;
308
                     mP.mQualityScale = 1.0f;
309
                     for(int j=0; j<level; j++) mP.mQualityScale*= EffectQuality.MULTIPLIER;
310
                     break;
311
        }
312
      }
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  private void displayNormals(MeshObject mesh)
318
    {
319
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
320
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
321
    DistortedRenderState.switchOffDrawing();
322
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, mesh.numVertices);
323
    DistortedRenderState.restoreDrawing();
324
    GLES30.glEndTransformFeedback();
325
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
326

    
327
    mNormalProgram.useProgram();
328
    GLES30.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
329
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
330
    GLES30.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
331
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
332
    GLES30.glLineWidth(8.0f);
333
    GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*mesh.numVertices);
334
    }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
339
    {
340
    float halfZ = halfW*mesh.zFactor;
341

    
342
    mM.compute(currTime);
343
    mV.compute(currTime,halfW,halfH,halfZ);
344
    mF.compute(currTime,halfW,halfH);
345
    mP.compute(currTime);
346

    
347
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
348

    
349
    mMainProgram.useProgram();
350
    GLES30.glUniform1i(mMainTextureH, 0);
351

    
352
    if( Distorted.GLSL >= 300 )
353
      {
354
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
355
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
356
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
357
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
358
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
359
      }
360
    else
361
      {
362
      mesh.mVertAttribs.position(0);
363
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
364
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE);
365
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
366
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE);
367
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
368
      }
369

    
370
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
371
    mV.send();
372
    mF.send();
373

    
374
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
375

    
376
    if( mesh.mShowNormals ) displayNormals(mesh);
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380
   
381
  static void blitPriv(DistortedOutputSurface surface)
382
    {
383
    mBlitProgram.useProgram();
384

    
385
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
386
    GLES30.glUniform1i(mBlitTextureH, 0);
387
    GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
388
    GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
389
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  static void blitDepthPriv(DistortedOutputSurface surface)
395
    {
396
    mBlitDepthProgram.useProgram();
397

    
398
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
399
    GLES30.glUniform1i(mBlitDepthTextureH, 0);
400
    GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
401
    GLES30.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
402
    GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
403
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407
   
408
  private void releasePriv()
409
    {
410
    if( !matrixCloned   )   mM.abortAll(false);
411
    if( !vertexCloned   )   mV.abortAll(false);
412
    if( !fragmentCloned )   mF.abortAll(false);
413
    if( !postprocessCloned) mP.abortAll(false);
414

    
415
    mM = null;
416
    mV = null;
417
    mF = null;
418
    mP = null;
419
    }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

    
423
  static void onDestroy()
424
    {
425
    mNextID = 0;
426
    }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429
// PUBLIC API
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431
/**
432
 * Create empty effect queue.
433
 */
434
  public DistortedEffects()
435
    {
436
    mID = ++mNextID;
437
    initializeEffectLists(this,0);
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441
/**
442
 * Copy constructor.
443
 * <p>
444
 * Whatever we do not clone gets created just like in the default constructor.
445
 *
446
 * @param dc    Source object to create our object from
447
 * @param flags A bitmask of values specifying what to copy.
448
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
449
 */
450
  public DistortedEffects(DistortedEffects dc, int flags)
451
    {
452
    mID = ++mNextID;
453
    initializeEffectLists(dc,flags);
454
    }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457
/**
458
 * Releases all resources. After this call, the queue should not be used anymore.
459
 */
460
  @SuppressWarnings("unused")
461
  public synchronized void delete()
462
    {
463
    releasePriv();
464
    }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467
/**
468
 * Returns unique ID of this instance.
469
 *
470
 * @return ID of the object.
471
 */
472
  public long getID()
473
      {
474
      return mID;
475
      }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478
/**
479
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
480
 * to one of the Effects in those queues. Nothing will happen if 'el' is already in the list.
481
 * 
482
 * @param el A class implementing the EffectListener interface that wants to get notifications.
483
 */
484
  @SuppressWarnings("unused")
485
  public void registerForMessages(EffectListener el)
486
    {
487
    mM.registerForMessages(el);
488
    mV.registerForMessages(el);
489
    mF.registerForMessages(el);
490
    mP.registerForMessages(el);
491
    }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494
/**
495
 * Removes the calling class from the list of Listeners.
496
 * 
497
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
498
 */
499
  @SuppressWarnings("unused")
500
  public void deregisterForMessages(EffectListener el)
501
    {
502
    mM.deregisterForMessages(el);
503
    mV.deregisterForMessages(el);
504
    mF.deregisterForMessages(el);
505
    mP.deregisterForMessages(el);
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509
/**
510
 * Aborts all Effects.
511
 * @return Number of effects aborted.
512
 */
513
  public int abortAllEffects()
514
    {
515
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
516
    }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519
/**
520
 * Aborts all Effects of a given type, for example all MATRIX Effects.
521
 * 
522
 * @param type one of the constants defined in {@link EffectType}
523
 * @return Number of effects aborted.
524
 */
525
  public int abortByType(EffectType type)
526
    {
527
    switch(type)
528
      {
529
      case MATRIX     : return mM.abortAll(true);
530
      case VERTEX     : return mV.abortAll(true);
531
      case FRAGMENT   : return mF.abortAll(true);
532
      case POSTPROCESS: return mP.abortAll(true);
533
      default         : return 0;
534
      }
535
    }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538
/**
539
 * Aborts all Effect by its ID.
540
 *
541
 * @param id the Id of the Effect to be removed, as returned by getID().
542
 * @return Number of effects aborted.
543
 */
544
  public int abortById(long id)
545
    {
546
    long type = id&EffectType.MASK;
547

    
548
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
549
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
550
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
551
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
552

    
553
    return 0;
554
    }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557
/**
558
 * Aborts a single Effect.
559
 * 
560
 * @param effect the Effect we want to abort.
561
 * @return number of Effects aborted. Always either 0 or 1.
562
 */
563
  public int abortEffect(Effect effect)
564
    {
565
    switch(effect.getType())
566
      {
567
      case MATRIX     : return mM.removeEffect(effect);
568
      case VERTEX     : return mV.removeEffect(effect);
569
      case FRAGMENT   : return mF.removeEffect(effect);
570
      case POSTPROCESS: return mP.removeEffect(effect);
571
      default         : return 0;
572
      }
573
    }
574

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576
/**
577
 * Abort all Effects of a given name, for example all rotations.
578
 * 
579
 * @param name one of the constants defined in {@link EffectName}
580
 * @return number of Effects aborted.
581
 */
582
  public int abortByName(EffectName name)
583
    {
584
    switch(name.getType())
585
      {
586
      case MATRIX     : return mM.removeByName(name);
587
      case VERTEX     : return mV.removeByName(name);
588
      case FRAGMENT   : return mF.removeByName(name);
589
      case POSTPROCESS: return mP.removeByName(name);
590
      default                : return 0;
591
      }
592
    }
593

    
594
///////////////////////////////////////////////////////////////////////////////////////////////////
595
/**
596
 * Returns the maximum number of effects of a given type.
597
 *
598
 * @param type {@link EffectType}
599
 * @return The maximum number of effects of a given type.
600
 */
601
  @SuppressWarnings("unused")
602
  public static int getMax(EffectType type)
603
    {
604
    return EffectQueue.getMax(type.ordinal());
605
    }
606

    
607
///////////////////////////////////////////////////////////////////////////////////////////////////
608
/**
609
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
610
 * This can fail if:
611
 * <ul>
612
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
613
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
614
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
615
 *     time only decreasing the value of 'max' is permitted.
616
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
617
 * </ul>
618
 *
619
 * @param type {@link EffectType}
620
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
621
 *            than Byte.MAX_VALUE
622
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
623
 */
624
  @SuppressWarnings("unused")
625
  public static boolean setMax(EffectType type, int max)
626
    {
627
    return EffectQueue.setMax(type.ordinal(),max);
628
    }
629

    
630
///////////////////////////////////////////////////////////////////////////////////////////////////
631
/**
632
 * The higher the quality, the better the effect will look like and the slower it will be.
633
 * <p>
634
 * This works by rendering into smaller and smaller intermediate buffers. Each step renders into a
635
 * buffer that's half the size of the previous one.
636
 * <p>
637
 * We cannot this during mid-render - thus, give it to the Master to assign us back this job on the
638
 * next render.
639
 */
640
  public void setPostprocessingQuality(EffectQuality quality)
641
    {
642
    mJobs.add(new Job(MIPMAP,quality.getLevel()));
643
    DistortedMaster.newSlave(this);
644
    }
645

    
646
///////////////////////////////////////////////////////////////////////////////////////////////////
647
/**
648
 * Add a new Effect to our queue.
649
 *
650
 * @param effect The Effect to add.
651
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
652
 */
653
  public boolean apply(Effect effect)
654
    {
655
    switch(effect.getType())
656
      {
657
      case MATRIX      : return mM.add(effect);
658
      case VERTEX      : return mV.add(effect);
659
      case FRAGMENT    : return mF.add(effect);
660
      case POSTPROCESS : return mP.add(effect);
661
      }
662

    
663
    return false;
664
    }
665
  }
(2-2/22)