Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedEffects.java @ 6b0b4f60

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.EffectType;
29
import org.distorted.library.message.EffectListener;
30
import org.distorted.library.program.DistortedProgram;
31
import org.distorted.library.program.FragmentCompilationException;
32
import org.distorted.library.program.FragmentUniformsException;
33
import org.distorted.library.program.LinkingException;
34
import org.distorted.library.program.VertexCompilationException;
35
import org.distorted.library.program.VertexUniformsException;
36

    
37
import java.io.InputStream;
38
import java.nio.ByteBuffer;
39
import java.nio.ByteOrder;
40
import java.nio.FloatBuffer;
41
import java.util.ArrayList;
42

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

    
54
  /// MAIN PROGRAM ///
55
  private static DistortedProgram mMainProgram;
56
  private static int mMainTextureH;
57
  private static boolean[] mEffectEnabled = new boolean[EffectName.LENGTH];
58

    
59
  static
60
    {
61
    int len = EffectName.LENGTH;
62
    for(int i=0; i<len; i++)
63
      {
64
      mEffectEnabled[i] = false;
65
      }
66
    }
67

    
68
  /// BLIT PROGRAM ///
69
  private static DistortedProgram mBlitProgram;
70
  private static int mBlitTextureH;
71
  private static int mBlitDepthH;
72
  private static final FloatBuffer mQuadPositions;
73

    
74
  static
75
    {
76
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
77
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
78
    mQuadPositions.put(positionData).position(0);
79
    }
80

    
81
  /// BLIT DEPTH PROGRAM ///
82
  private static DistortedProgram mBlitDepthProgram;
83
  private static int mBlitDepthTextureH;
84
  private static int mBlitDepthDepthTextureH;
85
  private static int mBlitDepthDepthH;
86

    
87
  /// NORMAL PROGRAM /////
88
  private static DistortedProgram mNormalProgram;
89
  private static int mNormalMVPMatrixH;
90
  /// END PROGRAMS //////
91

    
92
  private static long mNextID =0;
93
  private long mID;
94

    
95
  private EffectQueueMatrix mM;
96
  private EffectQueueFragment mF;
97
  private EffectQueueVertex mV;
98
  private EffectQueuePostprocess mP;
99

    
100
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
101

    
102
  private class Job
103
    {
104
    int type;
105
    int level;
106

    
107
    Job(int t, int l)
108
      {
109
      type = t;
110
      level= l;
111
      }
112
    }
113

    
114
  private ArrayList<Job> mJobs = new ArrayList<>();
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  static void createProgram(Resources resources)
119
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
120
    {
121
    // MAIN PROGRAM ////////////////////////////////////
122
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
123
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
124

    
125
    String mainVertHeader= Distorted.GLSL_VERSION;
126
    String mainFragHeader= Distorted.GLSL_VERSION;
127

    
128
    EffectName name;
129
    EffectType type;
130
    boolean foundF = false;
131
    boolean foundV = false;
132

    
133
    for(int i=0; i<mEffectEnabled.length; i++)
134
      {
135
      if( mEffectEnabled[i] )
136
        {
137
        name = EffectName.getName(i);
138
        type = name.getType();
139

    
140
        if( type == EffectType.VERTEX )
141
          {
142
          mainVertHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
143
          foundV = true;
144
          }
145
        else if( type == EffectType.FRAGMENT )
146
          {
147
          mainFragHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
148
          foundF = true;
149
          }
150
        }
151
      }
152

    
153
    mainVertHeader += ("#define NUM_VERTEX "   + ( foundV ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
154
    mainFragHeader += ("#define NUM_FRAGMENT " + ( foundF ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
155

    
156
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
157
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
158

    
159
    String[] feedback = { "v_Position", "v_endPosition" };
160

    
161
    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL, feedback);
162

    
163
    int mainProgramH = mMainProgram.getProgramHandle();
164
    EffectQueueFragment.getUniforms(mainProgramH);
165
    EffectQueueVertex.getUniforms(mainProgramH);
166
    EffectQueueMatrix.getUniforms(mainProgramH);
167
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
168

    
169
    // BLIT PROGRAM ////////////////////////////////////
170
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
171
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
172

    
173
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
174
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
175

    
176
    try
177
      {
178
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
179
      }
180
    catch(Exception e)
181
      {
182
      android.util.Log.e("EFFECTS", "exception trying to compile BLIT program: "+e.getMessage());
183
      throw new RuntimeException(e.getMessage());
184
      }
185

    
186
    int blitProgramH = mBlitProgram.getProgramHandle();
187
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
188
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
189

    
190
    // BLIT DEPTH PROGRAM ////////////////////////////////////
191
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
192
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
193

    
194
    try
195
      {
196
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
197
      }
198
    catch(Exception e)
199
      {
200
      android.util.Log.e("EFFECTS", "exception trying to compile BLIT DEPTH program: "+e.getMessage());
201
      throw new RuntimeException(e.getMessage());
202
      }
203

    
204
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
205
    mBlitDepthTextureH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Texture");
206
    mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
207
    mBlitDepthDepthH        = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
208

    
209
    // NORMAL PROGRAM //////////////////////////////////////
210
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
211
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
212

    
213
    try
214
      {
215
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
216
      }
217
    catch(Exception e)
218
      {
219
      android.util.Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
220
      throw new RuntimeException(e.getMessage());
221
      }
222

    
223
    int normalProgramH = mNormalProgram.getProgramHandle();
224
    mNormalMVPMatrixH  = GLES30.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  private void initializeEffectLists(DistortedEffects d, int flags)
230
    {
231
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
232
      {
233
      mM = d.mM;
234
      matrixCloned = true;
235
      }
236
    else
237
      {
238
      mM = new EffectQueueMatrix(mID);
239
      matrixCloned = false;
240
      }
241
    
242
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
243
      {
244
      mV = d.mV;
245
      vertexCloned = true;
246
      }
247
    else
248
      {
249
      mV = new EffectQueueVertex(mID);
250
      vertexCloned = false;
251
      }
252
    
253
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
254
      {
255
      mF = d.mF;
256
      fragmentCloned = true;
257
      }
258
    else
259
      {
260
      mF = new EffectQueueFragment(mID);
261
      fragmentCloned = false;
262
      }
263

    
264
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
265
      {
266
      mP = d.mP;
267
      postprocessCloned = true;
268
      }
269
    else
270
      {
271
      mP = new EffectQueuePostprocess(mID);
272
      postprocessCloned = false;
273
      }
274
    }
275

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

    
278
  int postprocess(long time, DistortedOutputSurface surface)
279
    {
280
    return mP.postprocess(time,surface);
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  long getBucket()
286
    {
287
    return mP.getID();
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  int getQuality()
293
    {
294
    return mP.mQualityLevel;
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  int getHalo()
300
    {
301
    return mP.getHalo();
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  void newNode(DistortedNode node)
307
    {
308
    mM.newNode(node);
309
    mF.newNode(node);
310
    mV.newNode(node);
311
    mP.newNode(node);
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315
/**
316
 * This is not really part of the public API. Has to be public only because it is a part of the
317
 * DistortedSlave interface, which should really be a class that we extend here instead but
318
 * Java has no multiple inheritance.
319
 *
320
 * @y.exclude
321
 */
322
  public void doWork()
323
    {
324
    int num = mJobs.size();
325
    Job job;
326

    
327
    for(int i=0; i<num; i++)
328
      {
329
      job = mJobs.remove(0);
330

    
331
      switch(job.type)
332
        {
333
        case MIPMAP: int level = job.level;
334
                     mP.mQualityLevel = level;
335
                     mP.mQualityScale = 1.0f;
336
                     for(int j=0; j<level; j++) mP.mQualityScale*=EffectQuality.MULTIPLIER;
337
                     break;
338
        }
339
      }
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
  private void displayNormals(MeshObject mesh)
345
    {
346
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
347
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
348
    DistortedRenderState.switchOffDrawing();
349
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, mesh.numVertices);
350
    DistortedRenderState.restoreDrawing();
351
    GLES30.glEndTransformFeedback();
352
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
353

    
354
    mNormalProgram.useProgram();
355
    GLES30.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
356
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
357
    GLES30.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
358
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
359
    GLES30.glLineWidth(8.0f);
360
    GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*mesh.numVertices);
361
    }
362

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

    
365
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
366
    {
367
    float halfZ = halfW*mesh.zFactor;
368

    
369
    mM.compute(currTime);
370
    mV.compute(currTime,halfW,halfH,halfZ);
371
    mF.compute(currTime,halfW,halfH);
372

    
373
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
374

    
375
    mMainProgram.useProgram();
376
    GLES30.glUniform1i(mMainTextureH, 0);
377

    
378
    if( Distorted.GLSL >= 300 )
379
      {
380
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
381
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
382
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
383
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
384
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
385
      }
386
    else
387
      {
388
      mesh.mVertAttribs.position(0);
389
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
390
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE);
391
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
392
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE);
393
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
394
      }
395

    
396
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
397
    mV.send();
398
    mF.send();
399

    
400
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
401

    
402
    if( mesh.mShowNormals ) displayNormals(mesh);
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
   
407
  static void blitPriv(DistortedOutputSurface surface)
408
    {
409
    mBlitProgram.useProgram();
410

    
411
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
412
    GLES30.glUniform1i(mBlitTextureH, 0);
413
    GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
414
    GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
415
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
416
    }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  static void blitDepthPriv(DistortedOutputSurface surface)
421
    {
422
    mBlitDepthProgram.useProgram();
423

    
424
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
425
    GLES30.glUniform1i(mBlitDepthTextureH, 0);
426
    GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
427
    GLES30.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
428
    GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
429
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
430
    }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433
   
434
  private void releasePriv()
435
    {
436
    if( !matrixCloned   )   mM.abortAll(false);
437
    if( !vertexCloned   )   mV.abortAll(false);
438
    if( !fragmentCloned )   mF.abortAll(false);
439
    if( !postprocessCloned) mP.abortAll(false);
440

    
441
    mM = null;
442
    mV = null;
443
    mF = null;
444
    mP = null;
445
    }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
  static void onDestroy()
450
    {
451
    mNextID = 0;
452

    
453
    for(int i=0; i<EffectName.LENGTH; i++)
454
      {
455
      mEffectEnabled[i] = false;
456
      }
457
    }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460
// PUBLIC API
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462
/**
463
 * Create empty effect queue.
464
 */
465
  public DistortedEffects()
466
    {
467
    mID = ++mNextID;
468
    initializeEffectLists(this,0);
469
    }
470

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472
/**
473
 * Copy constructor.
474
 * <p>
475
 * Whatever we do not clone gets created just like in the default constructor.
476
 *
477
 * @param dc    Source object to create our object from
478
 * @param flags A bitmask of values specifying what to copy.
479
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
480
 */
481
  public DistortedEffects(DistortedEffects dc, int flags)
482
    {
483
    mID = ++mNextID;
484
    initializeEffectLists(dc,flags);
485
    }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488
/**
489
 * Releases all resources. After this call, the queue should not be used anymore.
490
 */
491
  @SuppressWarnings("unused")
492
  public synchronized void delete()
493
    {
494
    releasePriv();
495
    }
496

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498
/**
499
 * Returns unique ID of this instance.
500
 *
501
 * @return ID of the object.
502
 */
503
  public long getID()
504
      {
505
      return mID;
506
      }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509
/**
510
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
511
 * to one of the Effects in those queues. Nothing will happen if 'el' is already in the list.
512
 * 
513
 * @param el A class implementing the EffectListener interface that wants to get notifications.
514
 */
515
  @SuppressWarnings("unused")
516
  public void registerForMessages(EffectListener el)
517
    {
518
    mM.registerForMessages(el);
519
    mV.registerForMessages(el);
520
    mF.registerForMessages(el);
521
    mP.registerForMessages(el);
522
    }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525
/**
526
 * Removes the calling class from the list of Listeners.
527
 * 
528
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
529
 */
530
  @SuppressWarnings("unused")
531
  public void deregisterForMessages(EffectListener el)
532
    {
533
    mM.deregisterForMessages(el);
534
    mV.deregisterForMessages(el);
535
    mF.deregisterForMessages(el);
536
    mP.deregisterForMessages(el);
537
    }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540
/**
541
 * Aborts all Effects.
542
 * @return Number of effects aborted.
543
 */
544
  public int abortAllEffects()
545
    {
546
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
547
    }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
/**
551
 * Aborts all Effects of a given type, for example all MATRIX Effects.
552
 * 
553
 * @param type one of the constants defined in {@link EffectType}
554
 * @return Number of effects aborted.
555
 */
556
  public int abortByType(EffectType type)
557
    {
558
    switch(type)
559
      {
560
      case MATRIX     : return mM.abortAll(true);
561
      case VERTEX     : return mV.abortAll(true);
562
      case FRAGMENT   : return mF.abortAll(true);
563
      case POSTPROCESS: return mP.abortAll(true);
564
      default         : return 0;
565
      }
566
    }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569
/**
570
 * Aborts all Effect by its ID.
571
 *
572
 * @param id the Id of the Effect to be removed, as returned by getID().
573
 * @return Number of effects aborted.
574
 */
575
  public int abortById(long id)
576
    {
577
    long type = id&EffectType.MASK;
578

    
579
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
580
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
581
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
582
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
583

    
584
    return 0;
585
    }
586

    
587
///////////////////////////////////////////////////////////////////////////////////////////////////
588
/**
589
 * Aborts a single Effect.
590
 * 
591
 * @param effect the Effect we want to abort.
592
 * @return number of Effects aborted. Always either 0 or 1.
593
 */
594
  public int abortEffect(Effect effect)
595
    {
596
    switch(effect.getType())
597
      {
598
      case MATRIX     : return mM.removeEffect(effect);
599
      case VERTEX     : return mV.removeEffect(effect);
600
      case FRAGMENT   : return mF.removeEffect(effect);
601
      case POSTPROCESS: return mP.removeEffect(effect);
602
      default         : return 0;
603
      }
604
    }
605

    
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607
/**
608
 * Abort all Effects of a given name, for example all rotations.
609
 * 
610
 * @param name one of the constants defined in {@link EffectName}
611
 * @return number of Effects aborted.
612
 */
613
  public int abortByName(EffectName name)
614
    {
615
    switch(name.getType())
616
      {
617
      case MATRIX     : return mM.removeByName(name);
618
      case VERTEX     : return mV.removeByName(name);
619
      case FRAGMENT   : return mF.removeByName(name);
620
      case POSTPROCESS: return mP.removeByName(name);
621
      default                : return 0;
622
      }
623
    }
624

    
625
///////////////////////////////////////////////////////////////////////////////////////////////////
626
/**
627
 * Enables a given Effect.
628
 * <p>
629
 * By default, all effects are disabled. One has to explicitly enable each effect one intends to use.
630
 * This needs to be called BEFORE shaders get compiled, i.e. before the call to Distorted.onCreate().
631
 * The point: by enabling only the effects we need, we can optimize the shaders.
632
 *
633
 * @param name one of the constants defined in {@link EffectName}
634
 */
635
  public static void enableEffect(EffectName name)
636
    {
637
    mEffectEnabled[name.ordinal()] = true;
638
    }
639

    
640
///////////////////////////////////////////////////////////////////////////////////////////////////
641
/**
642
 * Returns the maximum number of effects of a given type.
643
 *
644
 * @param type {@link EffectType}
645
 * @return The maximum number of effects of a given type.
646
 */
647
  @SuppressWarnings("unused")
648
  public static int getMax(EffectType type)
649
    {
650
    return EffectQueue.getMax(type.ordinal());
651
    }
652

    
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654
/**
655
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
656
 * This can fail if:
657
 * <ul>
658
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
659
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
660
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
661
 *     time only decreasing the value of 'max' is permitted.
662
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
663
 * </ul>
664
 *
665
 * @param type {@link EffectType}
666
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
667
 *            than Byte.MAX_VALUE
668
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
669
 */
670
  @SuppressWarnings("unused")
671
  public static boolean setMax(EffectType type, int max)
672
    {
673
    return EffectQueue.setMax(type.ordinal(),max);
674
    }
675

    
676
///////////////////////////////////////////////////////////////////////////////////////////////////
677
/**
678
 * The higher the quality, the better the effect will look like and the slower it will be.
679
 * <p>
680
 * This works by rendering into smaller and smaller intermediate buffers. Each step renders into a
681
 * buffer that's half the size of the previous one.
682
 * <p>
683
 * We cannot this during mid-render - thus, give it to the Master to assign us back this job on the
684
 * next render.
685
 */
686
  public void setQuality(EffectQuality quality)
687
    {
688
    mJobs.add(new Job(MIPMAP,quality.level));
689
    DistortedMaster.newSlave(this);
690
    }
691

    
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693
/**
694
 * Add a new Effect to our queue.
695
 *
696
 * @param effect The Effect to add.
697
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
698
 */
699
  public boolean apply(Effect effect)
700
    {
701
    switch(effect.getType())
702
      {
703
      case MATRIX      : return mM.add(effect);
704
      case VERTEX      : return mV.add(effect);
705
      case FRAGMENT    : return mF.add(effect);
706
      case POSTPROCESS : return mP.add(effect);
707
      }
708

    
709
    return false;
710
    }
711
  }
(2-2/23)