Project

General

Profile

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

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

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.getBucket();
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
 * This is not really part of the public API. Has to be public only because it is a part of the
307
 * DistortedSlave interface, which should really be a class that we extend here instead but
308
 * Java has no multiple inheritance.
309
 *
310
 * @y.exclude
311
 */
312
  public void doWork()
313
    {
314
    int num = mJobs.size();
315
    Job job;
316

    
317
    for(int i=0; i<num; i++)
318
      {
319
      job = mJobs.remove(0);
320

    
321
      switch(job.type)
322
        {
323
        case MIPMAP: int level = job.level;
324
                     mP.mQualityLevel = level;
325
                     mP.mQualityScale = 1.0f;
326
                     for(int j=0; j<level; j++) mP.mQualityScale*=EffectQuality.MULTIPLIER;
327
                     break;
328
        }
329
      }
330
    }
331

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

    
334
  private void displayNormals(MeshObject mesh)
335
    {
336
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
337
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
338
    DistortedRenderState.switchOffDrawing();
339
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, mesh.numVertices);
340
    DistortedRenderState.restoreDrawing();
341
    GLES30.glEndTransformFeedback();
342
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
343

    
344
    mNormalProgram.useProgram();
345
    GLES30.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
346
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
347
    GLES30.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
348
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
349
    GLES30.glLineWidth(8.0f);
350
    GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*mesh.numVertices);
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
356
    {
357
    float halfZ = halfW*mesh.zFactor;
358

    
359
    mM.compute(currTime);
360
    mV.compute(currTime,halfW,halfH,halfZ);
361
    mF.compute(currTime,halfW,halfH);
362

    
363
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
364

    
365
    mMainProgram.useProgram();
366
    GLES30.glUniform1i(mMainTextureH, 0);
367

    
368
    if( Distorted.GLSL >= 300 )
369
      {
370
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
371
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
372
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
373
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
374
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
375
      }
376
    else
377
      {
378
      mesh.mVertAttribs.position(0);
379
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
380
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE);
381
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
382
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE);
383
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
384
      }
385

    
386
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
387
    mV.send();
388
    mF.send();
389

    
390
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
391

    
392
    if( mesh.mShowNormals ) displayNormals(mesh);
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396
   
397
  static void blitPriv(DistortedOutputSurface surface)
398
    {
399
    mBlitProgram.useProgram();
400

    
401
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
402
    GLES30.glUniform1i(mBlitTextureH, 0);
403
    GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
404
    GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
405
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
406
    }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
  static void blitDepthPriv(DistortedOutputSurface surface)
411
    {
412
    mBlitDepthProgram.useProgram();
413

    
414
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
415
    GLES30.glUniform1i(mBlitDepthTextureH, 0);
416
    GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
417
    GLES30.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
418
    GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
419
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423
   
424
  private void releasePriv()
425
    {
426
    if( !matrixCloned   )   mM.abortAll(false);
427
    if( !vertexCloned   )   mV.abortAll(false);
428
    if( !fragmentCloned )   mF.abortAll(false);
429
    if( !postprocessCloned) mP.abortAll(false);
430

    
431
    mM = null;
432
    mV = null;
433
    mF = null;
434
    mP = null;
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
  static void onDestroy()
440
    {
441
    mNextID = 0;
442

    
443
    for(int i=0; i<EffectName.LENGTH; i++)
444
      {
445
      mEffectEnabled[i] = false;
446
      }
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450
// PUBLIC API
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452
/**
453
 * Create empty effect queue.
454
 */
455
  public DistortedEffects()
456
    {
457
    mID = ++mNextID;
458
    initializeEffectLists(this,0);
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462
/**
463
 * Copy constructor.
464
 * <p>
465
 * Whatever we do not clone gets created just like in the default constructor.
466
 *
467
 * @param dc    Source object to create our object from
468
 * @param flags A bitmask of values specifying what to copy.
469
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
470
 */
471
  public DistortedEffects(DistortedEffects dc, int flags)
472
    {
473
    mID = ++mNextID;
474
    initializeEffectLists(dc,flags);
475
    }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478
/**
479
 * Releases all resources. After this call, the queue should not be used anymore.
480
 */
481
  @SuppressWarnings("unused")
482
  public synchronized void delete()
483
    {
484
    releasePriv();
485
    }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488
/**
489
 * Returns unique ID of this instance.
490
 *
491
 * @return ID of the object.
492
 */
493
  public long getID()
494
      {
495
      return mID;
496
      }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499
/**
500
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
501
 * to one of the Effects in those queues. Nothing will happen if 'el' is already in the list.
502
 * 
503
 * @param el A class implementing the EffectListener interface that wants to get notifications.
504
 */
505
  @SuppressWarnings("unused")
506
  public void registerForMessages(EffectListener el)
507
    {
508
    mV.registerForMessages(el);
509
    mF.registerForMessages(el);
510
    mM.registerForMessages(el);
511
    mP.registerForMessages(el);
512
    }
513

    
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515
/**
516
 * Removes the calling class from the list of Listeners.
517
 * 
518
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
519
 */
520
  @SuppressWarnings("unused")
521
  public void deregisterForMessages(EffectListener el)
522
    {
523
    mV.deregisterForMessages(el);
524
    mF.deregisterForMessages(el);
525
    mM.deregisterForMessages(el);
526
    mP.deregisterForMessages(el);
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530
/**
531
 * Aborts all Effects.
532
 * @return Number of effects aborted.
533
 */
534
  public int abortAllEffects()
535
    {
536
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
537
    }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540
/**
541
 * Aborts all Effects of a given type, for example all MATRIX Effects.
542
 * 
543
 * @param type one of the constants defined in {@link EffectType}
544
 * @return Number of effects aborted.
545
 */
546
  public int abortByType(EffectType type)
547
    {
548
    switch(type)
549
      {
550
      case MATRIX     : return mM.abortAll(true);
551
      case VERTEX     : return mV.abortAll(true);
552
      case FRAGMENT   : return mF.abortAll(true);
553
      case POSTPROCESS: return mP.abortAll(true);
554
      default         : return 0;
555
      }
556
    }
557

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559
/**
560
 * Aborts all Effect by its ID.
561
 *
562
 * @param id the Id of the Effect to be removed, as returned by getID().
563
 * @return Number of effects aborted.
564
 */
565
  public int abortById(long id)
566
    {
567
    long type = id&EffectType.MASK;
568

    
569
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
570
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
571
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
572
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
573

    
574
    return 0;
575
    }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578
/**
579
 * Aborts a single Effect.
580
 * 
581
 * @param effect the Effect we want to abort.
582
 * @return number of Effects aborted. Always either 0 or 1.
583
 */
584
  public int abortEffect(Effect effect)
585
    {
586
    switch(effect.getType())
587
      {
588
      case MATRIX     : return mM.removeEffect(effect);
589
      case VERTEX     : return mV.removeEffect(effect);
590
      case FRAGMENT   : return mF.removeEffect(effect);
591
      case POSTPROCESS: return mP.removeEffect(effect);
592
      default         : return 0;
593
      }
594
    }
595

    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597
/**
598
 * Abort all Effects of a given name, for example all rotations.
599
 * 
600
 * @param name one of the constants defined in {@link EffectName}
601
 * @return number of Effects aborted.
602
 */
603
  public int abortByName(EffectName name)
604
    {
605
    switch(name.getType())
606
      {
607
      case MATRIX     : return mM.removeByName(name);
608
      case VERTEX     : return mV.removeByName(name);
609
      case FRAGMENT   : return mF.removeByName(name);
610
      case POSTPROCESS: return mP.removeByName(name);
611
      default                : return 0;
612
      }
613
    }
614

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

    
630
///////////////////////////////////////////////////////////////////////////////////////////////////
631
/**
632
 * Returns the maximum number of effects of a given type.
633
 *
634
 * @param type {@link EffectType}
635
 * @return The maximum number of effects of a given type.
636
 */
637
  @SuppressWarnings("unused")
638
  public static int getMax(EffectType type)
639
    {
640
    return EffectQueue.getMax(type.ordinal());
641
    }
642

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

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

    
682
///////////////////////////////////////////////////////////////////////////////////////////////////
683
/**
684
 * Add a new Effect to our queue.
685
 *
686
 * @param effect The Effect to add.
687
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
688
 */
689
  public boolean apply(Effect effect)
690
    {
691
    switch(effect.getType())
692
      {
693
      case VERTEX      : return mV.add(effect);
694
      case FRAGMENT    : return mF.add(effect);
695
      case MATRIX      : return mM.add(effect);
696
      case POSTPROCESS : return mP.add(effect);
697
      }
698

    
699
    return false;
700
    }
701
  }
(2-2/23)