Project

General

Profile

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

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

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, Fragment and Postprocessing effect queues.
49
 * <p>
50
 * The queues hold actual effects to be applied to a given (InputSurface,MeshObject) combo.
51
 */
52
public class DistortedEffects implements DistortedSlave
53
  {
54
  private static final int MIPMAP = 0;
55

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

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

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

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

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

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

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

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

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

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

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

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

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

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

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

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

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

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

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

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

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

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

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

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

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

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

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

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

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

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

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

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

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

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

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

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

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

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

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

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

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

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

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

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

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

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

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

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

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

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

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

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

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

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

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

    
552
    return 0;
553
    }
554

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

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

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594
/**
595
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
596
 * single (InputSurface,MeshObject) combo.
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)