Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.main;
21

    
22
import android.content.res.Resources;
23
import android.opengl.GLES31;
24
import android.util.Log;
25

    
26
import org.distorted.library.R;
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.EffectName;
29
import org.distorted.library.effect.EffectType;
30
import org.distorted.library.effect.FragmentEffect;
31
import org.distorted.library.effect.VertexEffect;
32
import org.distorted.library.message.EffectListener;
33
import org.distorted.library.program.DistortedProgram;
34
import org.distorted.library.program.FragmentCompilationException;
35
import org.distorted.library.program.FragmentUniformsException;
36
import org.distorted.library.program.LinkingException;
37
import org.distorted.library.program.VertexCompilationException;
38
import org.distorted.library.program.VertexUniformsException;
39

    
40
import java.io.InputStream;
41
import java.nio.ByteBuffer;
42
import java.nio.ByteOrder;
43
import java.nio.FloatBuffer;
44
import java.nio.IntBuffer;
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
53
  {
54
  /// MAIN PROGRAM ///
55
  private static DistortedProgram mMainProgram;
56
  private static int mMainTextureH;
57

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

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

    
71
  /// BLIT DEPTH PROGRAM ///
72
  private static DistortedProgram mBlitDepthProgram;
73
  private static int mBlitDepthSizeH;
74

    
75
  private static int[] mLinkedListSSBO = new int[1];
76
  private static int[] mAtomicCounter = new int[1];
77

    
78
  static
79
    {
80
    mLinkedListSSBO[0]= -1;
81
    mAtomicCounter[0] = -1;
82
    }
83

    
84
  private static int mBufferSize=(0x1<<23);  // 8 million entries
85

    
86
  private static IntBuffer mIntBuffer;
87

    
88
private static ByteBuffer mBuf;
89
private static IntBuffer mIntBuf;
90

    
91
  /// BLIT DEPTH RENDER PROGRAM ///
92
  private static DistortedProgram mBlitDepthRenderProgram;
93
  private static int mBlitDepthRenderSizeH;
94

    
95
  /// NORMAL PROGRAM /////
96
  private static DistortedProgram mNormalProgram;
97
  private static int mNormalMVPMatrixH;
98
  /// END PROGRAMS //////
99

    
100
  private static long mNextID =0;
101
  private long mID;
102

    
103
  private EffectQueueMatrix mM;
104
  private EffectQueueFragment mF;
105
  private EffectQueueVertex mV;
106
  private EffectQueuePostprocess mP;
107

    
108
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

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

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

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

    
134
    try
135
      {
136
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
137
                                          enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
138
      }
139
    catch(Exception e)
140
      {
141
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
142
      throw new RuntimeException(e.getMessage());
143
      }
144

    
145
    int mainProgramH = mMainProgram.getProgramHandle();
146
    EffectQueueFragment.getUniforms(mainProgramH);
147
    EffectQueueVertex.getUniforms(mainProgramH);
148
    EffectQueueMatrix.getUniforms(mainProgramH);
149
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
150

    
151
    // BLIT PROGRAM ////////////////////////////////////
152
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
153
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
154

    
155
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
156
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
157

    
158
    try
159
      {
160
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
161
      }
162
    catch(Exception e)
163
      {
164
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
165
      throw new RuntimeException(e.getMessage());
166
      }
167

    
168
    int blitProgramH = mBlitProgram.getProgramHandle();
169
    mBlitTextureH  = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
170
    mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
171

    
172
    // BLIT DEPTH PROGRAM ////////////////////////////////////
173
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
174
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
175

    
176
    try
177
      {
178
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
179
      }
180
    catch(Exception e)
181
      {
182
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
183
      throw new RuntimeException(e.getMessage());
184
      }
185

    
186
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
187
    mBlitDepthSizeH         = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Size");
188

    
189
    mIntBuffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
190
    mIntBuffer.put(0,0);
191

    
192
    if( mLinkedListSSBO[0]<0 )
193
      {
194
      GLES31.glGenBuffers(1,mLinkedListSSBO,0);
195
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
196
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
197

    
198
      mBuf = (ByteBuffer)GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, mBufferSize*4, GLES31.GL_MAP_READ_BIT);
199
      mIntBuf = mBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
200

    
201
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
202
      }
203

    
204
    if( mAtomicCounter[0]<0 )
205
      {
206
      GLES31.glGenBuffers(1,mAtomicCounter,0);
207
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
208
      GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
209
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
210
      }
211

    
212
    // BLIT DEPTH RENDER PROGRAM ///////////////////////////
213
    final InputStream blitDepthRenderVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
214
    final InputStream blitDepthRenderFragStream = resources.openRawResource(R.raw.blit_depth_render_fragment_shader);
215

    
216
    try
217
      {
218
      mBlitDepthRenderProgram = new DistortedProgram(blitDepthRenderVertStream,blitDepthRenderFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
219
      }
220
    catch(Exception e)
221
      {
222
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH RENDER program: "+e.getMessage());
223
      throw new RuntimeException(e.getMessage());
224
      }
225

    
226
    int blitDepthRenderProgramH   = mBlitDepthRenderProgram.getProgramHandle();
227
    mBlitDepthRenderSizeH         = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size");
228

    
229
    // NORMAL PROGRAM //////////////////////////////////////
230
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
231
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
232

    
233
    try
234
      {
235
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
236
      }
237
    catch(Exception e)
238
      {
239
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
240
      throw new RuntimeException(e.getMessage());
241
      }
242

    
243
    int normalProgramH = mNormalProgram.getProgramHandle();
244
    mNormalMVPMatrixH  = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  private void initializeEffectLists(DistortedEffects d, int flags)
250
    {
251
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
252
      {
253
      mM = d.mM;
254
      matrixCloned = true;
255
      }
256
    else
257
      {
258
      mM = new EffectQueueMatrix(mID);
259
      matrixCloned = false;
260
      }
261
    
262
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
263
      {
264
      mV = d.mV;
265
      vertexCloned = true;
266
      }
267
    else
268
      {
269
      mV = new EffectQueueVertex(mID);
270
      vertexCloned = false;
271
      }
272
    
273
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
274
      {
275
      mF = d.mF;
276
      fragmentCloned = true;
277
      }
278
    else
279
      {
280
      mF = new EffectQueueFragment(mID);
281
      fragmentCloned = false;
282
      }
283

    
284
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
285
      {
286
      mP = d.mP;
287
      postprocessCloned = true;
288
      }
289
    else
290
      {
291
      mP = new EffectQueuePostprocess(mID);
292
      postprocessCloned = false;
293
      }
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  EffectQueuePostprocess getPostprocess()
299
    {
300
    return mP;
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

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

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

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

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

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

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

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

    
345
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
346

    
347
    mMainProgram.useProgram();
348
    GLES31.glUniform1i(mMainTextureH, 0);
349

    
350
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
351
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
352
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
353
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
354
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
355

    
356
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
357
    mV.send();
358
    mF.send();
359

    
360
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
361

    
362
    if( mesh.mShowNormals ) displayNormals(mesh);
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366
/**
367
 * Only for use by the library itself.
368
 *
369
 * @y.exclude
370
 */
371
  public static void blitPriv(DistortedOutputSurface surface)
372
    {
373
    mBlitProgram.useProgram();
374

    
375
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
376
    GLES31.glUniform1i(mBlitTextureH, 0);
377
    GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
378
    GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
379
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
385
    {
386
    mBlitDepthProgram.useProgram();
387

    
388
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
389
    GLES31.glUniform2f(mBlitDepthSizeH, surface.mWidth, surface.mHeight);
390
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
391
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
392
    }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
// render all the transparent pixels from the per-pixel linked lists. This is in the 'merge
396
// postprocessing buckets' stage.
397

    
398
  static void blitDepthRenderPriv(DistortedOutputSurface surface, float corrW, float corrH)
399
    {
400
    mBlitDepthRenderProgram.useProgram();
401

    
402
    //analyzeBuffer(surface.mWidth, surface.mHeight);
403

    
404
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
405
    GLES31.glUniform2f(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight);
406
    GLES31.glVertexAttribPointer(mBlitDepthRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
407
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
408

    
409
    // reset atomic counter to 0
410
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
411
    GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
412
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417

    
418
  private static void analyzeBuffer(int w, int h)
419
    {
420
    int ptr;
421
    final int GREEN = 1;
422
    final int RED   = 2;
423

    
424
    int left1=0, left2=0;
425
    int right1=0, right2=0;
426
    int overflow = 0;
427

    
428
    for(int row=0; row<h; row++)
429
      for(int col=0; col<w; col++)
430
        {
431
        ptr = mIntBuf.get( col+row*w );
432

    
433
        if( ptr!=0 )
434
          {
435
          if( ptr>0 && ptr<mBufferSize )
436
            {
437
            ptr = mIntBuf.get(ptr);
438

    
439
            if ((2*col>w) && ptr != RED)
440
              {
441
              if (ptr==GREEN) right1++;
442
              else            right2++;
443
              }
444
            if ((2*col<=w) && ptr != GREEN)
445
              {
446
              if (ptr==RED) left1++;
447
              else          left2++;
448
              }
449
            }
450
          else
451
            {
452
            overflow++;
453
            }
454
          }
455
        }
456

    
457
    android.util.Log.e("surface", " left1: "+left1+" left2: "+left2+" right1: "+right1+" right2: "+right2+" overflow: "+overflow);
458
    }
459

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

    
462
  private void releasePriv()
463
    {
464
    if( !matrixCloned   )   mM.abortAll(false);
465
    if( !vertexCloned   )   mV.abortAll(false);
466
    if( !fragmentCloned )   mF.abortAll(false);
467
    if( !postprocessCloned) mP.abortAll(false);
468

    
469
    mM = null;
470
    mV = null;
471
    mF = null;
472
    mP = null;
473
    }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
  static void onDestroy()
478
    {
479
    mNextID           =  0;
480
    mLinkedListSSBO[0]= -1;
481
    mAtomicCounter[0] = -1;
482
    }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485
// PUBLIC API
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487
/**
488
 * Create empty effect queue.
489
 */
490
  public DistortedEffects()
491
    {
492
    mID = ++mNextID;
493
    initializeEffectLists(this,0);
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497
/**
498
 * Copy constructor.
499
 * <p>
500
 * Whatever we do not clone gets created just like in the default constructor.
501
 *
502
 * @param dc    Source object to create our object from
503
 * @param flags A bitmask of values specifying what to copy.
504
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
505
 */
506
  public DistortedEffects(DistortedEffects dc, int flags)
507
    {
508
    mID = ++mNextID;
509
    initializeEffectLists(dc,flags);
510
    }
511

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513
/**
514
 * Releases all resources. After this call, the queue should not be used anymore.
515
 */
516
  @SuppressWarnings("unused")
517
  public synchronized void delete()
518
    {
519
    releasePriv();
520
    }
521

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523
/**
524
 * Returns unique ID of this instance.
525
 *
526
 * @return ID of the object.
527
 */
528
  public long getID()
529
      {
530
      return mID;
531
      }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534
/**
535
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
536
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
537
 * 
538
 * @param el A class implementing the EffectListener interface that wants to get notifications.
539
 */
540
  @SuppressWarnings("unused")
541
  public void registerForMessages(EffectListener el)
542
    {
543
    mM.registerForMessages(el);
544
    mV.registerForMessages(el);
545
    mF.registerForMessages(el);
546
    mP.registerForMessages(el);
547
    }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
/**
551
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
552
 * 
553
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
554
 */
555
  @SuppressWarnings("unused")
556
  public void deregisterForMessages(EffectListener el)
557
    {
558
    mM.deregisterForMessages(el);
559
    mV.deregisterForMessages(el);
560
    mF.deregisterForMessages(el);
561
    mP.deregisterForMessages(el);
562
    }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565
/**
566
 * Aborts all Effects.
567
 * @return Number of effects aborted.
568
 */
569
  public int abortAllEffects()
570
    {
571
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
572
    }
573

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575
/**
576
 * Aborts all Effects of a given type, for example all MATRIX Effects.
577
 * 
578
 * @param type one of the constants defined in {@link EffectType}
579
 * @return Number of effects aborted.
580
 */
581
  public int abortByType(EffectType type)
582
    {
583
    switch(type)
584
      {
585
      case MATRIX     : return mM.abortAll(true);
586
      case VERTEX     : return mV.abortAll(true);
587
      case FRAGMENT   : return mF.abortAll(true);
588
      case POSTPROCESS: return mP.abortAll(true);
589
      default         : return 0;
590
      }
591
    }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594
/**
595
 * Aborts an Effect by its ID.
596
 *
597
 * @param id the Id of the Effect to be removed, as returned by getID().
598
 * @return Number of effects aborted.
599
 */
600
  public int abortById(long id)
601
    {
602
    long type = id&EffectType.MASK;
603

    
604
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
605
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
606
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
607
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
608

    
609
    return 0;
610
    }
611

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613
/**
614
 * Aborts a single Effect.
615
 * 
616
 * @param effect the Effect we want to abort.
617
 * @return number of Effects aborted. Always either 0 or 1.
618
 */
619
  public int abortEffect(Effect effect)
620
    {
621
    switch(effect.getType())
622
      {
623
      case MATRIX     : return mM.removeEffect(effect);
624
      case VERTEX     : return mV.removeEffect(effect);
625
      case FRAGMENT   : return mF.removeEffect(effect);
626
      case POSTPROCESS: return mP.removeEffect(effect);
627
      default         : return 0;
628
      }
629
    }
630

    
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632
/**
633
 * Abort all Effects of a given name, for example all rotations.
634
 * 
635
 * @param name one of the constants defined in {@link EffectName}
636
 * @return number of Effects aborted.
637
 */
638
  public int abortByName(EffectName name)
639
    {
640
    switch(name.getType())
641
      {
642
      case MATRIX     : return mM.removeByName(name);
643
      case VERTEX     : return mV.removeByName(name);
644
      case FRAGMENT   : return mF.removeByName(name);
645
      case POSTPROCESS: return mP.removeByName(name);
646
      default                : return 0;
647
      }
648
    }
649

    
650
///////////////////////////////////////////////////////////////////////////////////////////////////
651
/**
652
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
653
 * single (InputSurface,MeshObject) combo.
654
 *
655
 * @param type {@link EffectType}
656
 * @return The maximum number of effects of a given type.
657
 */
658
  @SuppressWarnings("unused")
659
  public static int getMax(EffectType type)
660
    {
661
    return EffectQueue.getMax(type.ordinal());
662
    }
663

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

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

    
704
    return false;
705
    }
706
  }
(2-2/21)