Project

General

Profile

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

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

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, mAtomicBuf;
89
private static IntBuffer mIntBuf, mAtomicIntBuf;
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.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
196
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
197
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
198
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
199
      }
200

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

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

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

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

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

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

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

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

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

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

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  EffectQueuePostprocess getPostprocess()
297
    {
298
    return mP;
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

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

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

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

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

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

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

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

    
343
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
344

    
345
    mMainProgram.useProgram();
346
    GLES31.glUniform1i(mMainTextureH, 0);
347

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

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

    
358
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
359

    
360
    if( mesh.mShowNormals ) displayNormals(mesh);
361
    }
362

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

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

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

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

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

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

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

    
400
    //analyzeBuffer(surface.mWidth, surface.mHeight);
401

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

    
407
    // reset atomic counter to 0
408
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
409

    
410
    mAtomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
411
                                                      GLES31.GL_MAP_READ_BIT|GLES31.GL_MAP_WRITE_BIT);
412
    if( mAtomicBuf!=null )
413
      {
414
      mAtomicIntBuf = mAtomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
415

    
416
      int counter = mAtomicIntBuf.get(0);
417
      mAtomicIntBuf.put(0, 0);
418
      //android.util.Log.e("counter", "now = "+counter+" w="+surface.mWidth+" h="+surface.mHeight
419
      //                             +" diff="+(counter-surface.mWidth*surface.mHeight));
420
      }
421
    else
422
      {
423
      android.util.Log.e("counter", "failed to map buffer");
424
      }
425

    
426
    GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
427
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
  private static void analyzeBuffer(int w, int h)
433
    {
434
    int ptr, index;
435
    int errors = 0;
436

    
437
    GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
438
    mBuf = (ByteBuffer)GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, mBufferSize*4, GLES31.GL_MAP_READ_BIT);
439
    mIntBuf = mBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
440

    
441
    for(int col=0; col<w; col++)
442
      for(int row=0; row<h; row++)
443
        {
444
        index = col+row*w;
445
        ptr = mIntBuf.get(index);
446

    
447
        if( ptr!=0 )
448
          {
449
          if( ptr>0 && ptr<mBufferSize )
450
            {
451
            ptr = mIntBuf.get(ptr);
452
            if( ptr != index )
453
              {
454
              android.util.Log.d("surface", "col="+col+" row="+row+" val="+ptr+" expected: "+index);
455
              errors++;
456
              }
457
            }
458
          else
459
            {
460
            android.util.Log.d("surface", "overflow!");
461
            }
462
          }
463
        }
464

    
465
    GLES31.glUnmapBuffer(GLES31.GL_SHADER_STORAGE_BUFFER);
466
    GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
467

    
468
    if( errors>0 ) android.util.Log.e("surface", "errors: "+errors);
469
    }
470

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472

    
473
  private void releasePriv()
474
    {
475
    if( !matrixCloned   )   mM.abortAll(false);
476
    if( !vertexCloned   )   mV.abortAll(false);
477
    if( !fragmentCloned )   mF.abortAll(false);
478
    if( !postprocessCloned) mP.abortAll(false);
479

    
480
    mM = null;
481
    mV = null;
482
    mF = null;
483
    mP = null;
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  static void onDestroy()
489
    {
490
    mNextID           =  0;
491
    mLinkedListSSBO[0]= -1;
492
    mAtomicCounter[0] = -1;
493
    }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496
// PUBLIC API
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498
/**
499
 * Create empty effect queue.
500
 */
501
  public DistortedEffects()
502
    {
503
    mID = ++mNextID;
504
    initializeEffectLists(this,0);
505
    }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508
/**
509
 * Copy constructor.
510
 * <p>
511
 * Whatever we do not clone gets created just like in the default constructor.
512
 *
513
 * @param dc    Source object to create our object from
514
 * @param flags A bitmask of values specifying what to copy.
515
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
516
 */
517
  public DistortedEffects(DistortedEffects dc, int flags)
518
    {
519
    mID = ++mNextID;
520
    initializeEffectLists(dc,flags);
521
    }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524
/**
525
 * Releases all resources. After this call, the queue should not be used anymore.
526
 */
527
  @SuppressWarnings("unused")
528
  public synchronized void delete()
529
    {
530
    releasePriv();
531
    }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534
/**
535
 * Returns unique ID of this instance.
536
 *
537
 * @return ID of the object.
538
 */
539
  public long getID()
540
      {
541
      return mID;
542
      }
543

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545
/**
546
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
547
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
548
 * 
549
 * @param el A class implementing the EffectListener interface that wants to get notifications.
550
 */
551
  @SuppressWarnings("unused")
552
  public void registerForMessages(EffectListener el)
553
    {
554
    mM.registerForMessages(el);
555
    mV.registerForMessages(el);
556
    mF.registerForMessages(el);
557
    mP.registerForMessages(el);
558
    }
559

    
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561
/**
562
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
563
 * 
564
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
565
 */
566
  @SuppressWarnings("unused")
567
  public void deregisterForMessages(EffectListener el)
568
    {
569
    mM.deregisterForMessages(el);
570
    mV.deregisterForMessages(el);
571
    mF.deregisterForMessages(el);
572
    mP.deregisterForMessages(el);
573
    }
574

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576
/**
577
 * Aborts all Effects.
578
 * @return Number of effects aborted.
579
 */
580
  public int abortAllEffects()
581
    {
582
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
583
    }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586
/**
587
 * Aborts all Effects of a given type, for example all MATRIX Effects.
588
 * 
589
 * @param type one of the constants defined in {@link EffectType}
590
 * @return Number of effects aborted.
591
 */
592
  public int abortByType(EffectType type)
593
    {
594
    switch(type)
595
      {
596
      case MATRIX     : return mM.abortAll(true);
597
      case VERTEX     : return mV.abortAll(true);
598
      case FRAGMENT   : return mF.abortAll(true);
599
      case POSTPROCESS: return mP.abortAll(true);
600
      default         : return 0;
601
      }
602
    }
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605
/**
606
 * Aborts an Effect by its ID.
607
 *
608
 * @param id the Id of the Effect to be removed, as returned by getID().
609
 * @return Number of effects aborted.
610
 */
611
  public int abortById(long id)
612
    {
613
    long type = id&EffectType.MASK;
614

    
615
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
616
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
617
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
618
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
619

    
620
    return 0;
621
    }
622

    
623
///////////////////////////////////////////////////////////////////////////////////////////////////
624
/**
625
 * Aborts a single Effect.
626
 * 
627
 * @param effect the Effect we want to abort.
628
 * @return number of Effects aborted. Always either 0 or 1.
629
 */
630
  public int abortEffect(Effect effect)
631
    {
632
    switch(effect.getType())
633
      {
634
      case MATRIX     : return mM.removeEffect(effect);
635
      case VERTEX     : return mV.removeEffect(effect);
636
      case FRAGMENT   : return mF.removeEffect(effect);
637
      case POSTPROCESS: return mP.removeEffect(effect);
638
      default         : return 0;
639
      }
640
    }
641

    
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643
/**
644
 * Abort all Effects of a given name, for example all rotations.
645
 * 
646
 * @param name one of the constants defined in {@link EffectName}
647
 * @return number of Effects aborted.
648
 */
649
  public int abortByName(EffectName name)
650
    {
651
    switch(name.getType())
652
      {
653
      case MATRIX     : return mM.removeByName(name);
654
      case VERTEX     : return mV.removeByName(name);
655
      case FRAGMENT   : return mF.removeByName(name);
656
      case POSTPROCESS: return mP.removeByName(name);
657
      default                : return 0;
658
      }
659
    }
660

    
661
///////////////////////////////////////////////////////////////////////////////////////////////////
662
/**
663
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
664
 * single (InputSurface,MeshObject) combo.
665
 *
666
 * @param type {@link EffectType}
667
 * @return The maximum number of effects of a given type.
668
 */
669
  @SuppressWarnings("unused")
670
  public static int getMax(EffectType type)
671
    {
672
    return EffectQueue.getMax(type.ordinal());
673
    }
674

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

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

    
715
    return false;
716
    }
717
  }
(2-2/21)