Project

General

Profile

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

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

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
22 c90b9e01 Leszek Koltunski
import android.content.res.Resources;
23 194ab46f Leszek Koltunski
import android.opengl.GLES30;
24 6a06a912 Leszek Koltunski
25 fe82a979 Leszek Koltunski
import org.distorted.library.R;
26
import org.distorted.library.effect.Effect;
27 da9b3f07 Leszek Koltunski
import org.distorted.library.effect.EffectName;
28
import org.distorted.library.effect.EffectType;
29 e458a4ba Leszek Koltunski
import org.distorted.library.message.EffectListener;
30 55c14a19 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
31 c90b9e01 Leszek Koltunski
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 a4835695 Leszek Koltunski
37 c90b9e01 Leszek Koltunski
import java.io.InputStream;
38 c638c1b0 Leszek Koltunski
import java.nio.ByteBuffer;
39
import java.nio.ByteOrder;
40
import java.nio.FloatBuffer;
41 6bb59aad Leszek Koltunski
import java.util.ArrayList;
42 c638c1b0 Leszek Koltunski
43 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
44 b329f352 Leszek Koltunski
/**
45 13687207 leszek
 * Class containing Matrix,Vertex and Fragment effect queues. Postprocessing queue is held in a separate
46
 * class.
47 b73dcaa7 Leszek Koltunski
 * <p>
48 26df012c Leszek Koltunski
 * The queues hold actual effects to be applied to a given (DistortedTexture,MeshObject) combo.
49 b329f352 Leszek Koltunski
 */
50 80b3acf6 Leszek Koltunski
public class DistortedEffects implements DistortedSlave
51 d425545a Leszek Koltunski
  {
52 80b3acf6 Leszek Koltunski
  private static final int MIPMAP = 0;
53
54 1aedf874 leszek
  /// MAIN PROGRAM ///
55
  private static DistortedProgram mMainProgram;
56
  private static int mMainTextureH;
57 c3651001 Leszek Koltunski
  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 8fa96e69 Leszek Koltunski
68 c1e24646 leszek
  /// BLIT PROGRAM ///
69
  private static DistortedProgram mBlitProgram;
70
  private static int mBlitTextureH;
71 c2c08950 leszek
  private static int mBlitDepthH;
72 c90b9e01 Leszek Koltunski
  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 7170e4eb leszek
  /// 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 3fc9327a Leszek Koltunski
  /// NORMAL PROGRAM /////
88
  private static DistortedProgram mNormalProgram;
89
  private static int mNormalMVPMatrixH;
90
  /// END PROGRAMS //////
91 c638c1b0 Leszek Koltunski
92 8e34674e Leszek Koltunski
  private static long mNextID =0;
93 4e2382f3 Leszek Koltunski
  private long mID;
94 8e34674e Leszek Koltunski
95 310e14fb leszek
  private EffectQueueMatrix mM;
96 3fc9327a Leszek Koltunski
  private EffectQueueFragment mF;
97 310e14fb leszek
  private EffectQueueVertex mV;
98 80b3acf6 Leszek Koltunski
  private EffectQueuePostprocess mP;
99
100
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
101 3d590d8d Leszek Koltunski
102 80b3acf6 Leszek Koltunski
  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 985ea9c5 Leszek Koltunski
116 9361b337 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
117 55c14a19 Leszek Koltunski
118 c90b9e01 Leszek Koltunski
  static void createProgram(Resources resources)
119
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
120 55c14a19 Leszek Koltunski
    {
121 cab7c165 Leszek Koltunski
    // MAIN PROGRAM ////////////////////////////////////
122 1aedf874 leszek
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
123
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
124
125 94f6d472 Leszek Koltunski
    String mainVertHeader= Distorted.GLSL_VERSION;
126
    String mainFragHeader= Distorted.GLSL_VERSION;
127 c90b9e01 Leszek Koltunski
128 c3651001 Leszek Koltunski
    EffectName name;
129
    EffectType type;
130 03cb451d Leszek Koltunski
    boolean foundF = false;
131
    boolean foundV = false;
132 c90b9e01 Leszek Koltunski
133 c3651001 Leszek Koltunski
    for(int i=0; i<mEffectEnabled.length; i++)
134 c90b9e01 Leszek Koltunski
      {
135 c3651001 Leszek Koltunski
      if( mEffectEnabled[i] )
136 03cb451d Leszek Koltunski
        {
137 c3651001 Leszek Koltunski
        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 03cb451d Leszek Koltunski
        }
151 c90b9e01 Leszek Koltunski
      }
152
153 fe6fe99a leszek
    mainVertHeader += ("#define NUM_VERTEX "   + ( foundV ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
154
    mainFragHeader += ("#define NUM_FRAGMENT " + ( foundF ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
155 c90b9e01 Leszek Koltunski
156 03cb451d Leszek Koltunski
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
157
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
158 c90b9e01 Leszek Koltunski
159 3fc9327a Leszek Koltunski
    String[] feedback = { "v_Position", "v_endPosition" };
160 cab7c165 Leszek Koltunski
161
    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL, feedback);
162 c90b9e01 Leszek Koltunski
163 c1e24646 leszek
    int mainProgramH = mMainProgram.getProgramHandle();
164 c90b9e01 Leszek Koltunski
    EffectQueueFragment.getUniforms(mainProgramH);
165 1aedf874 leszek
    EffectQueueVertex.getUniforms(mainProgramH);
166
    EffectQueueMatrix.getUniforms(mainProgramH);
167 c1e24646 leszek
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
168
169 1aedf874 leszek
    // BLIT PROGRAM ////////////////////////////////////
170 c1e24646 leszek
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
171
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
172
173 94f6d472 Leszek Koltunski
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
174
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
175 c1e24646 leszek
176 f2367b75 Leszek Koltunski
    try
177
      {
178 94f6d472 Leszek Koltunski
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
179 f2367b75 Leszek Koltunski
      }
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 c1e24646 leszek
186
    int blitProgramH = mBlitProgram.getProgramHandle();
187
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
188 c2c08950 leszek
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
189 c90b9e01 Leszek Koltunski
190 7170e4eb leszek
    // 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 3fc9327a Leszek Koltunski
    // 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 c90b9e01 Leszek Koltunski
213 f2367b75 Leszek Koltunski
    try
214
      {
215 3fc9327a Leszek Koltunski
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
216 f2367b75 Leszek Koltunski
      }
217
    catch(Exception e)
218
      {
219 3fc9327a Leszek Koltunski
      android.util.Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
220 f2367b75 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
221
      }
222 c90b9e01 Leszek Koltunski
223 3fc9327a Leszek Koltunski
    int normalProgramH = mNormalProgram.getProgramHandle();
224
    mNormalMVPMatrixH  = GLES30.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
225 55c14a19 Leszek Koltunski
    }
226
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
229 421c2728 Leszek Koltunski
  private void initializeEffectLists(DistortedEffects d, int flags)
230 d425545a Leszek Koltunski
    {
231 015642fb Leszek Koltunski
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
232 6a06a912 Leszek Koltunski
      {
233 d425545a Leszek Koltunski
      mM = d.mM;
234
      matrixCloned = true;
235
      }
236
    else
237
      {
238 4e2382f3 Leszek Koltunski
      mM = new EffectQueueMatrix(mID);
239 d425545a Leszek Koltunski
      matrixCloned = false;
240
      }
241 6a06a912 Leszek Koltunski
    
242 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
243
      {
244
      mV = d.mV;
245
      vertexCloned = true;
246
      }
247
    else
248
      {
249 4e2382f3 Leszek Koltunski
      mV = new EffectQueueVertex(mID);
250 d425545a Leszek Koltunski
      vertexCloned = false;
251
      }
252 6a06a912 Leszek Koltunski
    
253 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
254
      {
255
      mF = d.mF;
256
      fragmentCloned = true;
257 6a06a912 Leszek Koltunski
      }
258 d425545a Leszek Koltunski
    else
259
      {
260 4e2382f3 Leszek Koltunski
      mF = new EffectQueueFragment(mID);
261 d425545a Leszek Koltunski
      fragmentCloned = false;
262
      }
263 80b3acf6 Leszek Koltunski
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 d425545a Leszek Koltunski
    }
331 6a06a912 Leszek Koltunski
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333 c90b9e01 Leszek Koltunski
334 3fc9327a Leszek Koltunski
  private void displayNormals(MeshObject mesh)
335 c90b9e01 Leszek Koltunski
    {
336 604b2899 Leszek Koltunski
    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 3fc9327a Leszek Koltunski
    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 c90b9e01 Leszek Koltunski
    }
352 1aedf874 leszek
353 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
354
355 270c27bc Leszek Koltunski
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
356 d425545a Leszek Koltunski
    {
357 c1e24646 leszek
    float halfZ = halfW*mesh.zFactor;
358 638b5b5c leszek
359 8fbd0237 Leszek Koltunski
    mM.compute(currTime);
360
    mV.compute(currTime,halfW,halfH,halfZ);
361
    mF.compute(currTime,halfW,halfH);
362
363 8426bd6a Leszek Koltunski
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
364 d6e94c84 Leszek Koltunski
365 b9798977 leszek
    mMainProgram.useProgram();
366 cab7c165 Leszek Koltunski
    GLES30.glUniform1i(mMainTextureH, 0);
367
368 420836fc leszek
    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 cab7c165 Leszek Koltunski
386 270c27bc Leszek Koltunski
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
387 8fbd0237 Leszek Koltunski
    mV.send();
388
    mF.send();
389 42571056 Leszek Koltunski
390 a51fe521 Leszek Koltunski
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
391 c90b9e01 Leszek Koltunski
392 3fc9327a Leszek Koltunski
    if( mesh.mShowNormals ) displayNormals(mesh);
393 d425545a Leszek Koltunski
    }
394 6a06a912 Leszek Koltunski
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396
   
397 8426bd6a Leszek Koltunski
  static void blitPriv(DistortedOutputSurface surface)
398 d425545a Leszek Koltunski
    {
399 c1e24646 leszek
    mBlitProgram.useProgram();
400
401 8426bd6a Leszek Koltunski
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
402 c1e24646 leszek
    GLES30.glUniform1i(mBlitTextureH, 0);
403 8426bd6a Leszek Koltunski
    GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
404 c1e24646 leszek
    GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
405
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
406 d425545a Leszek Koltunski
    }
407 7170e4eb leszek
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 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
423
   
424 8e34674e Leszek Koltunski
  private void releasePriv()
425 d425545a Leszek Koltunski
    {
426 80b3acf6 Leszek Koltunski
    if( !matrixCloned   )   mM.abortAll(false);
427
    if( !vertexCloned   )   mV.abortAll(false);
428
    if( !fragmentCloned )   mF.abortAll(false);
429
    if( !postprocessCloned) mP.abortAll(false);
430 d425545a Leszek Koltunski
431 4e2382f3 Leszek Koltunski
    mM = null;
432
    mV = null;
433
    mF = null;
434 80b3acf6 Leszek Koltunski
    mP = null;
435 8e34674e Leszek Koltunski
    }
436
437 1942537e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
438
439 7b8086eb Leszek Koltunski
  static void onDestroy()
440 1942537e Leszek Koltunski
    {
441
    mNextID = 0;
442 c3651001 Leszek Koltunski
443
    for(int i=0; i<EffectName.LENGTH; i++)
444
      {
445
      mEffectEnabled[i] = false;
446
      }
447 1942537e Leszek Koltunski
    }
448
449 8e34674e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
450
// PUBLIC API
451 ada90d33 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
452 d425545a Leszek Koltunski
/**
453 4e2382f3 Leszek Koltunski
 * Create empty effect queue.
454 d425545a Leszek Koltunski
 */
455 421c2728 Leszek Koltunski
  public DistortedEffects()
456 d425545a Leszek Koltunski
    {
457 c7da4e65 leszek
    mID = ++mNextID;
458 4e2382f3 Leszek Koltunski
    initializeEffectLists(this,0);
459 d425545a Leszek Koltunski
    }
460 ada90d33 Leszek Koltunski
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462 d425545a Leszek Koltunski
/**
463 4e2382f3 Leszek Koltunski
 * Copy constructor.
464 d425545a Leszek Koltunski
 * <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 e6ab30eb Leszek Koltunski
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
470 d425545a Leszek Koltunski
 */
471 421c2728 Leszek Koltunski
  public DistortedEffects(DistortedEffects dc, int flags)
472 d425545a Leszek Koltunski
    {
473 c7da4e65 leszek
    mID = ++mNextID;
474 4e2382f3 Leszek Koltunski
    initializeEffectLists(dc,flags);
475 d425545a Leszek Koltunski
    }
476 ada90d33 Leszek Koltunski
477 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
478
/**
479 07305c87 Leszek Koltunski
 * Releases all resources. After this call, the queue should not be used anymore.
480 6a06a912 Leszek Koltunski
 */
481 13687207 leszek
  @SuppressWarnings("unused")
482 8e34674e Leszek Koltunski
  public synchronized void delete()
483 d425545a Leszek Koltunski
    {
484
    releasePriv();
485
    }
486 6a06a912 Leszek Koltunski
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488
/**
489 4e2382f3 Leszek Koltunski
 * Returns unique ID of this instance.
490
 *
491
 * @return ID of the object.
492 6a06a912 Leszek Koltunski
 */
493 4e2382f3 Leszek Koltunski
  public long getID()
494 d425545a Leszek Koltunski
      {
495 4e2382f3 Leszek Koltunski
      return mID;
496 d425545a Leszek Koltunski
      }
497 4e2382f3 Leszek Koltunski
498 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
499
/**
500
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
501 07305c87 Leszek Koltunski
 * to one of the Effects in those queues. Nothing will happen if 'el' is already in the list.
502 6a06a912 Leszek Koltunski
 * 
503
 * @param el A class implementing the EffectListener interface that wants to get notifications.
504
 */
505 13687207 leszek
  @SuppressWarnings("unused")
506 3fc994b2 Leszek Koltunski
  public void registerForMessages(EffectListener el)
507 d425545a Leszek Koltunski
    {
508 3fc994b2 Leszek Koltunski
    mV.registerForMessages(el);
509
    mF.registerForMessages(el);
510
    mM.registerForMessages(el);
511 80b3acf6 Leszek Koltunski
    mP.registerForMessages(el);
512 d425545a Leszek Koltunski
    }
513 6a06a912 Leszek Koltunski
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 13687207 leszek
  @SuppressWarnings("unused")
521 3fc994b2 Leszek Koltunski
  public void deregisterForMessages(EffectListener el)
522 d425545a Leszek Koltunski
    {
523 3fc994b2 Leszek Koltunski
    mV.deregisterForMessages(el);
524
    mF.deregisterForMessages(el);
525
    mM.deregisterForMessages(el);
526 80b3acf6 Leszek Koltunski
    mP.deregisterForMessages(el);
527 d425545a Leszek Koltunski
    }
528 6a06a912 Leszek Koltunski
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530
/**
531 d07f2950 Leszek Koltunski
 * Aborts all Effects.
532
 * @return Number of effects aborted.
533 6a06a912 Leszek Koltunski
 */
534 d425545a Leszek Koltunski
  public int abortAllEffects()
535 13687207 leszek
    {
536
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
537
    }
538 6a06a912 Leszek Koltunski
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540
/**
541 d07f2950 Leszek Koltunski
 * Aborts all Effects of a given type, for example all MATRIX Effects.
542 6a06a912 Leszek Koltunski
 * 
543 da9b3f07 Leszek Koltunski
 * @param type one of the constants defined in {@link EffectType}
544 d07f2950 Leszek Koltunski
 * @return Number of effects aborted.
545 6a06a912 Leszek Koltunski
 */
546 da9b3f07 Leszek Koltunski
  public int abortByType(EffectType type)
547 d425545a Leszek Koltunski
    {
548
    switch(type)
549 6a06a912 Leszek Koltunski
      {
550 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.abortAll(true);
551
      case VERTEX     : return mV.abortAll(true);
552
      case FRAGMENT   : return mF.abortAll(true);
553 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.abortAll(true);
554 da9b3f07 Leszek Koltunski
      default         : return 0;
555 6a06a912 Leszek Koltunski
      }
556 d425545a Leszek Koltunski
    }
557 47316d20 leszek
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 2ef5dd9e leszek
    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 80b3acf6 Leszek Koltunski
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
573 47316d20 leszek
574
    return 0;
575
    }
576
577 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
578
/**
579
 * Aborts a single Effect.
580
 * 
581 6bb59aad Leszek Koltunski
 * @param effect the Effect we want to abort.
582 476bbc81 Leszek Koltunski
 * @return number of Effects aborted. Always either 0 or 1.
583 6a06a912 Leszek Koltunski
 */
584 6bb59aad Leszek Koltunski
  public int abortEffect(Effect effect)
585 d425545a Leszek Koltunski
    {
586 6bb59aad Leszek Koltunski
    switch(effect.getType())
587
      {
588 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeEffect(effect);
589
      case VERTEX     : return mV.removeEffect(effect);
590
      case FRAGMENT   : return mF.removeEffect(effect);
591 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeEffect(effect);
592 da9b3f07 Leszek Koltunski
      default         : return 0;
593 6bb59aad Leszek Koltunski
      }
594 d425545a Leszek Koltunski
    }
595 6a06a912 Leszek Koltunski
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597
/**
598 e8c81a8e Leszek Koltunski
 * Abort all Effects of a given name, for example all rotations.
599 6a06a912 Leszek Koltunski
 * 
600 da9b3f07 Leszek Koltunski
 * @param name one of the constants defined in {@link EffectName}
601 476bbc81 Leszek Koltunski
 * @return number of Effects aborted.
602 6a06a912 Leszek Koltunski
 */
603 da9b3f07 Leszek Koltunski
  public int abortByName(EffectName name)
604 d425545a Leszek Koltunski
    {
605 da9b3f07 Leszek Koltunski
    switch(name.getType())
606 6a06a912 Leszek Koltunski
      {
607 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeByName(name);
608
      case VERTEX     : return mV.removeByName(name);
609
      case FRAGMENT   : return mF.removeByName(name);
610 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeByName(name);
611 6bb59aad Leszek Koltunski
      default                : return 0;
612 6a06a912 Leszek Koltunski
      }
613 d425545a Leszek Koltunski
    }
614 432442f9 Leszek Koltunski
615 03cb451d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 da9b3f07 Leszek Koltunski
 * @param name one of the constants defined in {@link EffectName}
624 03cb451d Leszek Koltunski
 */
625 da9b3f07 Leszek Koltunski
  public static void enableEffect(EffectName name)
626 03cb451d Leszek Koltunski
    {
627 c3651001 Leszek Koltunski
    mEffectEnabled[name.ordinal()] = true;
628 03cb451d Leszek Koltunski
    }
629
630 432442f9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
631
/**
632 fe6fe99a leszek
 * Returns the maximum number of effects of a given type.
633 432442f9 Leszek Koltunski
 *
634 fe6fe99a leszek
 * @param type {@link EffectType}
635
 * @return The maximum number of effects of a given type.
636 432442f9 Leszek Koltunski
 */
637 13687207 leszek
  @SuppressWarnings("unused")
638 fe6fe99a leszek
  public static int getMax(EffectType type)
639 432442f9 Leszek Koltunski
    {
640 fe6fe99a leszek
    return EffectQueue.getMax(type.ordinal());
641 432442f9 Leszek Koltunski
    }
642
643
///////////////////////////////////////////////////////////////////////////////////////////////////
644
/**
645 fe6fe99a leszek
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
646 432442f9 Leszek Koltunski
 * 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 fe6fe99a leszek
 * @param type {@link EffectType}
656
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
657 80b3acf6 Leszek Koltunski
 *            than Byte.MAX_VALUE
658
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
659
 */
660
  @SuppressWarnings("unused")
661 fe6fe99a leszek
  public static boolean setMax(EffectType type, int max)
662 80b3acf6 Leszek Koltunski
    {
663 fe6fe99a leszek
    return EffectQueue.setMax(type.ordinal(),max);
664 80b3acf6 Leszek Koltunski
    }
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 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
683
/**
684 6bb59aad Leszek Koltunski
 * Add a new Effect to our queue.
685 f2fe7e28 Leszek Koltunski
 *
686 6bb59aad Leszek Koltunski
 * @param effect The Effect to add.
687 ae77d55e Leszek Koltunski
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
688 6a06a912 Leszek Koltunski
 */
689 ae77d55e Leszek Koltunski
  public boolean apply(Effect effect)
690 d425545a Leszek Koltunski
    {
691 6bb59aad Leszek Koltunski
    switch(effect.getType())
692
      {
693 ae77d55e Leszek Koltunski
      case VERTEX      : return mV.add(effect);
694
      case FRAGMENT    : return mF.add(effect);
695
      case MATRIX      : return mM.add(effect);
696 80b3acf6 Leszek Koltunski
      case POSTPROCESS : return mP.add(effect);
697 6bb59aad Leszek Koltunski
      }
698 ae77d55e Leszek Koltunski
699
    return false;
700 4fde55a0 Leszek Koltunski
    }
701 f2fe7e28 Leszek Koltunski
  }