Project

General

Profile

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

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

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
  private static int mCountIndexH;
58

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

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

    
72
  /// BLIT DEPTH PROGRAM ///
73
  private static DistortedProgram mBlitDepthProgram;
74
  private static int mBlitDepthTextureH;
75
  private static int mBlitDepthDepthTextureH;
76
  private static int mBlitDepthDepthH;
77
  private static int mBlitDepthTexCorrH;
78
  private static int mBlitDepthSizeH;
79
  private static int mBlitDepthNumRecordsH;
80

    
81
  private static int[] mLinkedListSSBO = new int[1];
82
  private static int[] mAtomicCounter = new int[1];
83

    
84
  static
85
    {
86
    mLinkedListSSBO[0]= -1;
87
    mAtomicCounter[0] = -1;
88
    }
89

    
90
  private static int mBufferSize=(0x1<<23);  // 8 million entries
91
  private static IntBuffer mIntBuffer;
92

    
93
  /// BLIT DEPTH RENDER PROGRAM ///
94
  private static DistortedProgram mBlitDepthRenderProgram;
95
  private static int mBlitDepthRenderDepthTextureH;
96
  private static int mBlitDepthRenderDepthH;
97
  private static int mBlitDepthRenderTexCorrH;
98
  private static int mBlitDepthRenderSizeH;
99

    
100
  /// NORMAL PROGRAM /////
101
  private static DistortedProgram mNormalProgram;
102
  private static int mNormalMVPMatrixH;
103
  /// END PROGRAMS //////
104

    
105
  private static long mNextID =0;
106
  private long mID;
107

    
108
  private EffectQueueMatrix mM;
109
  private EffectQueueFragment mF;
110
  private EffectQueueVertex mV;
111
  private EffectQueuePostprocess mP;
112

    
113
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

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

    
124
    int numF = FragmentEffect.getNumEnabled();
125
    int numV = VertexEffect.getNumEnabled();
126

    
127
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
128
    String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
129
    String enabledEffectV= VertexEffect.getGLSL();
130
    String enabledEffectF= FragmentEffect.getGLSL();
131

    
132
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
133
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
134
    //android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
135
    //android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
136

    
137
    String[] feedback = { "v_Position", "v_endPosition" };
138

    
139
    mMainProgram = new DistortedProgram( mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
140
                                         enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
141

    
142
    int mainProgramH = mMainProgram.getProgramHandle();
143
    EffectQueueFragment.getUniforms(mainProgramH);
144
    EffectQueueVertex.getUniforms(mainProgramH);
145
    EffectQueueMatrix.getUniforms(mainProgramH);
146
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
147
    mCountIndexH = GLES31.glGetUniformLocation( mainProgramH, "u_currentIndex");
148

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

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

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

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

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

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

    
184
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
185
    mBlitDepthTextureH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
186
    mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
187
    mBlitDepthDepthH        = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
188
    mBlitDepthTexCorrH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
189
    mBlitDepthSizeH         = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Size");
190
    mBlitDepthNumRecordsH   = GLES31.glGetUniformLocation( blitDepthProgramH, "u_numRecords");
191

    
192
    mIntBuffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
193
    mIntBuffer.put(0,0);
194

    
195
    if( mLinkedListSSBO[0]<0 )
196
      {
197
      GLES31.glGenBuffers(1,mLinkedListSSBO,0);
198
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
199
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
200
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
201
      }
202

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

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

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

    
225
    int blitDepthRenderProgramH   = mBlitDepthRenderProgram.getProgramHandle();
226
    mBlitDepthRenderDepthTextureH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_DepthTexture");
227
    mBlitDepthRenderDepthH        = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Depth");
228
    mBlitDepthRenderTexCorrH      = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_TexCorr");
229
    mBlitDepthRenderSizeH         = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size");
230

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

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

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

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

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

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

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  EffectQueuePostprocess getPostprocess()
301
    {
302
    return mP;
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

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

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

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

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

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

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

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

    
347
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
348

    
349
    mMainProgram.useProgram();
350
    GLES31.glUniform1i(mMainTextureH, 0);
351
    GLES31.glUniform1i(mCountIndexH, surface.getNewCounter() );
352

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

    
359
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
360
    mV.send();
361
    mF.send();
362

    
363
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
364

    
365
    if( mesh.mShowNormals ) displayNormals(mesh);
366
    }
367

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

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

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
388
    {
389
    mBlitDepthProgram.useProgram();
390

    
391
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
392
    GLES31.glUniform1i(mBlitDepthTextureH, 0);
393
    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
394
    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
395
    GLES31.glUniform2i(mBlitDepthSizeH, surface.mWidth, surface.mHeight);
396
    GLES31.glUniform1ui(mBlitDepthNumRecordsH, (mBufferSize-surface.mWidth*surface.mHeight)/3 );  // see the fragment shader
397
    GLES31.glUniform1f(mBlitDepthDepthH , 1.0f-surface.mNear);
398
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
399
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
400

    
401

    
402
    //android.util.Log.e("effects", "width="+surface.mWidth+" height="+surface.mHeight+
403
    //    " bufferSize: "+mBufferSize+" numRecords: "+((mBufferSize-surface.mWidth*surface.mHeight)/3) );
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407
// render all the transparent pixels from the per-pixel linked lists. This is in the 'merge
408
// postprocessing buckets' stage.
409

    
410
  static void blitDepthRenderPriv(DistortedOutputSurface surface, float corrW, float corrH)
411
    {
412
    mBlitDepthRenderProgram.useProgram();
413

    
414
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
415
    GLES31.glUniform1i(mBlitDepthRenderDepthTextureH, 1);
416
    GLES31.glUniform2f(mBlitDepthRenderTexCorrH, corrW, corrH );
417
    GLES31.glUniform2i(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight);
418
    GLES31.glUniform1f( mBlitDepthRenderDepthH , 1.0f-surface.mNear);
419
    GLES31.glVertexAttribPointer(mBlitDepthRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
420
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
421

    
422
    // reset atomic counter to 0
423
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
424
    GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
425
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
426
    }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429
   
430
  private void releasePriv()
431
    {
432
    if( !matrixCloned   )   mM.abortAll(false);
433
    if( !vertexCloned   )   mV.abortAll(false);
434
    if( !fragmentCloned )   mF.abortAll(false);
435
    if( !postprocessCloned) mP.abortAll(false);
436

    
437
    mM = null;
438
    mV = null;
439
    mF = null;
440
    mP = null;
441
    }
442

    
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444

    
445
  static void onDestroy()
446
    {
447
    mNextID           =  0;
448
    mLinkedListSSBO[0]= -1;
449
    mAtomicCounter[0] = -1;
450
    }
451

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

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

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

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

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

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518
/**
519
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
520
 * 
521
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
522
 */
523
  @SuppressWarnings("unused")
524
  public void deregisterForMessages(EffectListener el)
525
    {
526
    mM.deregisterForMessages(el);
527
    mV.deregisterForMessages(el);
528
    mF.deregisterForMessages(el);
529
    mP.deregisterForMessages(el);
530
    }
531

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

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

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

    
572
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
573
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
574
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
575
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
576

    
577
    return 0;
578
    }
579

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

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

    
618
///////////////////////////////////////////////////////////////////////////////////////////////////
619
/**
620
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
621
 * single (InputSurface,MeshObject) combo.
622
 *
623
 * @param type {@link EffectType}
624
 * @return The maximum number of effects of a given type.
625
 */
626
  @SuppressWarnings("unused")
627
  public static int getMax(EffectType type)
628
    {
629
    return EffectQueue.getMax(type.ordinal());
630
    }
631

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

    
655
///////////////////////////////////////////////////////////////////////////////////////////////////
656
/**
657
 * Add a new Effect to our queue.
658
 *
659
 * @param effect The Effect to add.
660
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
661
 */
662
  public boolean apply(Effect effect)
663
    {
664
    switch(effect.getType())
665
      {
666
      case MATRIX      : return mM.add(effect);
667
      case VERTEX      : return mV.add(effect);
668
      case FRAGMENT    : return mF.add(effect);
669
      case POSTPROCESS : return mP.add(effect);
670
      }
671

    
672
    return false;
673
    }
674
  }
(2-2/22)