Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedEffects.java @ 27cd6b98

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 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
24
import android.util.Log;
25 6a06a912 Leszek Koltunski
26 fe82a979 Leszek Koltunski
import org.distorted.library.R;
27
import org.distorted.library.effect.Effect;
28 da9b3f07 Leszek Koltunski
import org.distorted.library.effect.EffectName;
29
import org.distorted.library.effect.EffectType;
30 7cd24173 leszek
import org.distorted.library.effect.FragmentEffect;
31
import org.distorted.library.effect.VertexEffect;
32 e458a4ba Leszek Koltunski
import org.distorted.library.message.EffectListener;
33 55c14a19 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
34 c90b9e01 Leszek Koltunski
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 a4835695 Leszek Koltunski
40 c90b9e01 Leszek Koltunski
import java.io.InputStream;
41 c638c1b0 Leszek Koltunski
import java.nio.ByteBuffer;
42
import java.nio.ByteOrder;
43
import java.nio.FloatBuffer;
44
45 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
46 b329f352 Leszek Koltunski
/**
47 faa3ff56 Leszek Koltunski
 * Class containing Matrix, Vertex, Fragment and Postprocessing effect queues.
48 b73dcaa7 Leszek Koltunski
 * <p>
49 faa3ff56 Leszek Koltunski
 * The queues hold actual effects to be applied to a given (InputSurface,MeshObject) combo.
50 b329f352 Leszek Koltunski
 */
51 86d322b5 Leszek Koltunski
public class DistortedEffects
52 d425545a Leszek Koltunski
  {
53 1aedf874 leszek
  /// MAIN PROGRAM ///
54
  private static DistortedProgram mMainProgram;
55
  private static int mMainTextureH;
56 12f45260 Leszek Koltunski
  private static int mCountIndexH;
57 8fa96e69 Leszek Koltunski
58 c1e24646 leszek
  /// BLIT PROGRAM ///
59
  private static DistortedProgram mBlitProgram;
60
  private static int mBlitTextureH;
61 c2c08950 leszek
  private static int mBlitDepthH;
62 c90b9e01 Leszek Koltunski
  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 7170e4eb leszek
  /// BLIT DEPTH PROGRAM ///
72
  private static DistortedProgram mBlitDepthProgram;
73
  private static int mBlitDepthTextureH;
74
  private static int mBlitDepthDepthTextureH;
75
  private static int mBlitDepthDepthH;
76
77 3fc9327a Leszek Koltunski
  /// NORMAL PROGRAM /////
78
  private static DistortedProgram mNormalProgram;
79
  private static int mNormalMVPMatrixH;
80
  /// END PROGRAMS //////
81 c638c1b0 Leszek Koltunski
82 8e34674e Leszek Koltunski
  private static long mNextID =0;
83 4e2382f3 Leszek Koltunski
  private long mID;
84 8e34674e Leszek Koltunski
85 310e14fb leszek
  private EffectQueueMatrix mM;
86 3fc9327a Leszek Koltunski
  private EffectQueueFragment mF;
87 310e14fb leszek
  private EffectQueueVertex mV;
88 80b3acf6 Leszek Koltunski
  private EffectQueuePostprocess mP;
89
90
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
91 3d590d8d Leszek Koltunski
92 9361b337 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
93 55c14a19 Leszek Koltunski
94 c90b9e01 Leszek Koltunski
  static void createProgram(Resources resources)
95
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
96 55c14a19 Leszek Koltunski
    {
97 cab7c165 Leszek Koltunski
    // MAIN PROGRAM ////////////////////////////////////
98 1aedf874 leszek
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
99
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
100
101 7cd24173 leszek
    int numF = FragmentEffect.getNumEnabled();
102
    int numV = VertexEffect.getNumEnabled();
103 c90b9e01 Leszek Koltunski
104 7cd24173 leszek
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
105
    String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
106
    String enabledEffectV= VertexEffect.getGLSL();
107
    String enabledEffectF= FragmentEffect.getGLSL();
108 c90b9e01 Leszek Koltunski
109 03cb451d Leszek Koltunski
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
110
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
111 7cd24173 leszek
    //android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
112
    //android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
113 c90b9e01 Leszek Koltunski
114 3fc9327a Leszek Koltunski
    String[] feedback = { "v_Position", "v_endPosition" };
115 cab7c165 Leszek Koltunski
116 7cd24173 leszek
    mMainProgram = new DistortedProgram( mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
117
                                         enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
118 c90b9e01 Leszek Koltunski
119 c1e24646 leszek
    int mainProgramH = mMainProgram.getProgramHandle();
120 c90b9e01 Leszek Koltunski
    EffectQueueFragment.getUniforms(mainProgramH);
121 1aedf874 leszek
    EffectQueueVertex.getUniforms(mainProgramH);
122
    EffectQueueMatrix.getUniforms(mainProgramH);
123 e6519ac8 Leszek Koltunski
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
124 12f45260 Leszek Koltunski
    mCountIndexH = GLES31.glGetUniformLocation( mainProgramH, "u_currentIndex");
125 c1e24646 leszek
126 1aedf874 leszek
    // BLIT PROGRAM ////////////////////////////////////
127 c1e24646 leszek
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
128
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
129
130 94f6d472 Leszek Koltunski
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
131
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
132 c1e24646 leszek
133 f2367b75 Leszek Koltunski
    try
134
      {
135 94f6d472 Leszek Koltunski
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
136 f2367b75 Leszek Koltunski
      }
137
    catch(Exception e)
138
      {
139 e6519ac8 Leszek Koltunski
      Log.e("EFFECTS", "exception trying to compile BLIT program: "+e.getMessage());
140 f2367b75 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
141
      }
142 c1e24646 leszek
143
    int blitProgramH = mBlitProgram.getProgramHandle();
144 e6519ac8 Leszek Koltunski
    mBlitTextureH  = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
145
    mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
146 c90b9e01 Leszek Koltunski
147 7170e4eb leszek
    // BLIT DEPTH PROGRAM ////////////////////////////////////
148
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
149
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
150
151
    try
152
      {
153
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
154
      }
155
    catch(Exception e)
156
      {
157 e6519ac8 Leszek Koltunski
      Log.e("EFFECTS", "exception trying to compile BLIT DEPTH program: "+e.getMessage());
158 7170e4eb leszek
      throw new RuntimeException(e.getMessage());
159
      }
160
161
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
162 e6519ac8 Leszek Koltunski
    mBlitDepthTextureH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
163
    mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
164
    mBlitDepthDepthH        = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
165 7170e4eb leszek
166 3fc9327a Leszek Koltunski
    // NORMAL PROGRAM //////////////////////////////////////
167
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
168
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
169 c90b9e01 Leszek Koltunski
170 f2367b75 Leszek Koltunski
    try
171
      {
172 3fc9327a Leszek Koltunski
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
173 f2367b75 Leszek Koltunski
      }
174
    catch(Exception e)
175
      {
176 e6519ac8 Leszek Koltunski
      Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
177 f2367b75 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
178
      }
179 c90b9e01 Leszek Koltunski
180 3fc9327a Leszek Koltunski
    int normalProgramH = mNormalProgram.getProgramHandle();
181 e6519ac8 Leszek Koltunski
    mNormalMVPMatrixH  = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
182 55c14a19 Leszek Koltunski
    }
183
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
186 421c2728 Leszek Koltunski
  private void initializeEffectLists(DistortedEffects d, int flags)
187 d425545a Leszek Koltunski
    {
188 015642fb Leszek Koltunski
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
189 6a06a912 Leszek Koltunski
      {
190 d425545a Leszek Koltunski
      mM = d.mM;
191
      matrixCloned = true;
192
      }
193
    else
194
      {
195 4e2382f3 Leszek Koltunski
      mM = new EffectQueueMatrix(mID);
196 d425545a Leszek Koltunski
      matrixCloned = false;
197
      }
198 6a06a912 Leszek Koltunski
    
199 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
200
      {
201
      mV = d.mV;
202
      vertexCloned = true;
203
      }
204
    else
205
      {
206 4e2382f3 Leszek Koltunski
      mV = new EffectQueueVertex(mID);
207 d425545a Leszek Koltunski
      vertexCloned = false;
208
      }
209 6a06a912 Leszek Koltunski
    
210 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
211
      {
212
      mF = d.mF;
213
      fragmentCloned = true;
214 6a06a912 Leszek Koltunski
      }
215 d425545a Leszek Koltunski
    else
216
      {
217 4e2382f3 Leszek Koltunski
      mF = new EffectQueueFragment(mID);
218 d425545a Leszek Koltunski
      fragmentCloned = false;
219
      }
220 80b3acf6 Leszek Koltunski
221
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
222
      {
223
      mP = d.mP;
224
      postprocessCloned = true;
225
      }
226
    else
227
      {
228
      mP = new EffectQueuePostprocess(mID);
229
      postprocessCloned = false;
230
      }
231
    }
232
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
235 70b6a155 Leszek Koltunski
  EffectQueuePostprocess getPostprocess()
236 80b3acf6 Leszek Koltunski
    {
237 70b6a155 Leszek Koltunski
    return mP;
238 80b3acf6 Leszek Koltunski
    }
239
240 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
241
242
  void newNode(DistortedNode node)
243
    {
244
    mM.newNode(node);
245
    mF.newNode(node);
246
    mV.newNode(node);
247
    mP.newNode(node);
248
    }
249
250 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
251 c90b9e01 Leszek Koltunski
252 3fc9327a Leszek Koltunski
  private void displayNormals(MeshObject mesh)
253 c90b9e01 Leszek Koltunski
    {
254 e6519ac8 Leszek Koltunski
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
255
    GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
256 604b2899 Leszek Koltunski
    DistortedRenderState.switchOffDrawing();
257 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
258 604b2899 Leszek Koltunski
    DistortedRenderState.restoreDrawing();
259 e6519ac8 Leszek Koltunski
    GLES31.glEndTransformFeedback();
260
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
261 604b2899 Leszek Koltunski
262 3fc9327a Leszek Koltunski
    mNormalProgram.useProgram();
263 e6519ac8 Leszek Koltunski
    GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
264
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
265
    GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
266
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
267
    GLES31.glLineWidth(8.0f);
268
    GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*mesh.numVertices);
269 c90b9e01 Leszek Koltunski
    }
270 1aedf874 leszek
271 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273 270c27bc Leszek Koltunski
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
274 d425545a Leszek Koltunski
    {
275 c1e24646 leszek
    float halfZ = halfW*mesh.zFactor;
276 638b5b5c leszek
277 27cd6b98 Leszek Koltunski
    android.util.Log.d("surface", "SurfaceID "+ surface.getID()+" transparent fragments: "+ surface.returnOldCounter() );
278
279 8fbd0237 Leszek Koltunski
    mM.compute(currTime);
280
    mV.compute(currTime,halfW,halfH,halfZ);
281
    mF.compute(currTime,halfW,halfH);
282 1149be8f leszek
    mP.compute(currTime);
283 8fbd0237 Leszek Koltunski
284 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
285 d6e94c84 Leszek Koltunski
286 b9798977 leszek
    mMainProgram.useProgram();
287 e6519ac8 Leszek Koltunski
    GLES31.glUniform1i(mMainTextureH, 0);
288 12f45260 Leszek Koltunski
    GLES31.glUniform1i(mCountIndexH, surface.resetNewCounter() );
289
290 420836fc leszek
    if( Distorted.GLSL >= 300 )
291
      {
292 e6519ac8 Leszek Koltunski
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
293
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
294
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
295
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
296
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
297 420836fc leszek
      }
298
    else
299
      {
300
      mesh.mVertAttribs.position(0);
301 e6519ac8 Leszek Koltunski
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
302 420836fc leszek
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE);
303 e6519ac8 Leszek Koltunski
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
304 420836fc leszek
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE);
305 e6519ac8 Leszek Koltunski
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
306 420836fc leszek
      }
307 cab7c165 Leszek Koltunski
308 270c27bc Leszek Koltunski
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
309 8fbd0237 Leszek Koltunski
    mV.send();
310
    mF.send();
311 42571056 Leszek Koltunski
312 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
313 c90b9e01 Leszek Koltunski
314 3fc9327a Leszek Koltunski
    if( mesh.mShowNormals ) displayNormals(mesh);
315 d425545a Leszek Koltunski
    }
316 6a06a912 Leszek Koltunski
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318 7266d8ef Leszek Koltunski
/**
319
 * Only for use by the library itself.
320
 *
321
 * @y.exclude
322
 */
323
  public static void blitPriv(DistortedOutputSurface surface)
324 d425545a Leszek Koltunski
    {
325 c1e24646 leszek
    mBlitProgram.useProgram();
326
327 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
328
    GLES31.glUniform1i(mBlitTextureH, 0);
329
    GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
330
    GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
331
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
332 d425545a Leszek Koltunski
    }
333 7170e4eb leszek
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335
336
  static void blitDepthPriv(DistortedOutputSurface surface)
337
    {
338
    mBlitDepthProgram.useProgram();
339
340 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
341
    GLES31.glUniform1i(mBlitDepthTextureH, 0);
342
    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
343
    GLES31.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
344
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
345
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
346 7170e4eb leszek
    }
347
348 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
349
   
350 8e34674e Leszek Koltunski
  private void releasePriv()
351 d425545a Leszek Koltunski
    {
352 80b3acf6 Leszek Koltunski
    if( !matrixCloned   )   mM.abortAll(false);
353
    if( !vertexCloned   )   mV.abortAll(false);
354
    if( !fragmentCloned )   mF.abortAll(false);
355
    if( !postprocessCloned) mP.abortAll(false);
356 d425545a Leszek Koltunski
357 4e2382f3 Leszek Koltunski
    mM = null;
358
    mV = null;
359
    mF = null;
360 80b3acf6 Leszek Koltunski
    mP = null;
361 8e34674e Leszek Koltunski
    }
362
363 1942537e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
364
365 7b8086eb Leszek Koltunski
  static void onDestroy()
366 1942537e Leszek Koltunski
    {
367
    mNextID = 0;
368
    }
369
370 8e34674e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
371
// PUBLIC API
372 ada90d33 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
373 d425545a Leszek Koltunski
/**
374 4e2382f3 Leszek Koltunski
 * Create empty effect queue.
375 d425545a Leszek Koltunski
 */
376 421c2728 Leszek Koltunski
  public DistortedEffects()
377 d425545a Leszek Koltunski
    {
378 c7da4e65 leszek
    mID = ++mNextID;
379 4e2382f3 Leszek Koltunski
    initializeEffectLists(this,0);
380 d425545a Leszek Koltunski
    }
381 ada90d33 Leszek Koltunski
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383 d425545a Leszek Koltunski
/**
384 4e2382f3 Leszek Koltunski
 * Copy constructor.
385 d425545a Leszek Koltunski
 * <p>
386
 * Whatever we do not clone gets created just like in the default constructor.
387
 *
388
 * @param dc    Source object to create our object from
389
 * @param flags A bitmask of values specifying what to copy.
390 e6ab30eb Leszek Koltunski
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
391 d425545a Leszek Koltunski
 */
392 421c2728 Leszek Koltunski
  public DistortedEffects(DistortedEffects dc, int flags)
393 d425545a Leszek Koltunski
    {
394 c7da4e65 leszek
    mID = ++mNextID;
395 4e2382f3 Leszek Koltunski
    initializeEffectLists(dc,flags);
396 d425545a Leszek Koltunski
    }
397 ada90d33 Leszek Koltunski
398 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
399
/**
400 07305c87 Leszek Koltunski
 * Releases all resources. After this call, the queue should not be used anymore.
401 6a06a912 Leszek Koltunski
 */
402 13687207 leszek
  @SuppressWarnings("unused")
403 8e34674e Leszek Koltunski
  public synchronized void delete()
404 d425545a Leszek Koltunski
    {
405
    releasePriv();
406
    }
407 6a06a912 Leszek Koltunski
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409
/**
410 4e2382f3 Leszek Koltunski
 * Returns unique ID of this instance.
411
 *
412
 * @return ID of the object.
413 6a06a912 Leszek Koltunski
 */
414 4e2382f3 Leszek Koltunski
  public long getID()
415 d425545a Leszek Koltunski
      {
416 4e2382f3 Leszek Koltunski
      return mID;
417 d425545a Leszek Koltunski
      }
418 4e2382f3 Leszek Koltunski
419 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
420
/**
421
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
422 faa3ff56 Leszek Koltunski
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
423 6a06a912 Leszek Koltunski
 * 
424
 * @param el A class implementing the EffectListener interface that wants to get notifications.
425
 */
426 13687207 leszek
  @SuppressWarnings("unused")
427 3fc994b2 Leszek Koltunski
  public void registerForMessages(EffectListener el)
428 d425545a Leszek Koltunski
    {
429 26a4e5f6 leszek
    mM.registerForMessages(el);
430 3fc994b2 Leszek Koltunski
    mV.registerForMessages(el);
431
    mF.registerForMessages(el);
432 80b3acf6 Leszek Koltunski
    mP.registerForMessages(el);
433 d425545a Leszek Koltunski
    }
434 6a06a912 Leszek Koltunski
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436
/**
437 faa3ff56 Leszek Koltunski
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
438 6a06a912 Leszek Koltunski
 * 
439
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
440
 */
441 13687207 leszek
  @SuppressWarnings("unused")
442 3fc994b2 Leszek Koltunski
  public void deregisterForMessages(EffectListener el)
443 d425545a Leszek Koltunski
    {
444 26a4e5f6 leszek
    mM.deregisterForMessages(el);
445 3fc994b2 Leszek Koltunski
    mV.deregisterForMessages(el);
446
    mF.deregisterForMessages(el);
447 80b3acf6 Leszek Koltunski
    mP.deregisterForMessages(el);
448 d425545a Leszek Koltunski
    }
449 6a06a912 Leszek Koltunski
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451
/**
452 d07f2950 Leszek Koltunski
 * Aborts all Effects.
453
 * @return Number of effects aborted.
454 6a06a912 Leszek Koltunski
 */
455 d425545a Leszek Koltunski
  public int abortAllEffects()
456 13687207 leszek
    {
457
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
458
    }
459 6a06a912 Leszek Koltunski
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461
/**
462 d07f2950 Leszek Koltunski
 * Aborts all Effects of a given type, for example all MATRIX Effects.
463 6a06a912 Leszek Koltunski
 * 
464 da9b3f07 Leszek Koltunski
 * @param type one of the constants defined in {@link EffectType}
465 d07f2950 Leszek Koltunski
 * @return Number of effects aborted.
466 6a06a912 Leszek Koltunski
 */
467 da9b3f07 Leszek Koltunski
  public int abortByType(EffectType type)
468 d425545a Leszek Koltunski
    {
469
    switch(type)
470 6a06a912 Leszek Koltunski
      {
471 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.abortAll(true);
472
      case VERTEX     : return mV.abortAll(true);
473
      case FRAGMENT   : return mF.abortAll(true);
474 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.abortAll(true);
475 da9b3f07 Leszek Koltunski
      default         : return 0;
476 6a06a912 Leszek Koltunski
      }
477 d425545a Leszek Koltunski
    }
478 47316d20 leszek
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480
/**
481 faa3ff56 Leszek Koltunski
 * Aborts an Effect by its ID.
482 47316d20 leszek
 *
483
 * @param id the Id of the Effect to be removed, as returned by getID().
484
 * @return Number of effects aborted.
485
 */
486
  public int abortById(long id)
487
    {
488
    long type = id&EffectType.MASK;
489
490 2ef5dd9e leszek
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
491
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
492
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
493 80b3acf6 Leszek Koltunski
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
494 47316d20 leszek
495
    return 0;
496
    }
497
498 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
499
/**
500
 * Aborts a single Effect.
501
 * 
502 6bb59aad Leszek Koltunski
 * @param effect the Effect we want to abort.
503 476bbc81 Leszek Koltunski
 * @return number of Effects aborted. Always either 0 or 1.
504 6a06a912 Leszek Koltunski
 */
505 6bb59aad Leszek Koltunski
  public int abortEffect(Effect effect)
506 d425545a Leszek Koltunski
    {
507 6bb59aad Leszek Koltunski
    switch(effect.getType())
508
      {
509 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeEffect(effect);
510
      case VERTEX     : return mV.removeEffect(effect);
511
      case FRAGMENT   : return mF.removeEffect(effect);
512 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeEffect(effect);
513 da9b3f07 Leszek Koltunski
      default         : return 0;
514 6bb59aad Leszek Koltunski
      }
515 d425545a Leszek Koltunski
    }
516 6a06a912 Leszek Koltunski
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518
/**
519 e8c81a8e Leszek Koltunski
 * Abort all Effects of a given name, for example all rotations.
520 6a06a912 Leszek Koltunski
 * 
521 da9b3f07 Leszek Koltunski
 * @param name one of the constants defined in {@link EffectName}
522 476bbc81 Leszek Koltunski
 * @return number of Effects aborted.
523 6a06a912 Leszek Koltunski
 */
524 da9b3f07 Leszek Koltunski
  public int abortByName(EffectName name)
525 d425545a Leszek Koltunski
    {
526 da9b3f07 Leszek Koltunski
    switch(name.getType())
527 6a06a912 Leszek Koltunski
      {
528 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeByName(name);
529
      case VERTEX     : return mV.removeByName(name);
530
      case FRAGMENT   : return mF.removeByName(name);
531 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeByName(name);
532 6bb59aad Leszek Koltunski
      default                : return 0;
533 6a06a912 Leszek Koltunski
      }
534 d425545a Leszek Koltunski
    }
535 432442f9 Leszek Koltunski
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537
/**
538 faa3ff56 Leszek Koltunski
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
539
 * single (InputSurface,MeshObject) combo.
540 432442f9 Leszek Koltunski
 *
541 fe6fe99a leszek
 * @param type {@link EffectType}
542
 * @return The maximum number of effects of a given type.
543 432442f9 Leszek Koltunski
 */
544 13687207 leszek
  @SuppressWarnings("unused")
545 fe6fe99a leszek
  public static int getMax(EffectType type)
546 432442f9 Leszek Koltunski
    {
547 fe6fe99a leszek
    return EffectQueue.getMax(type.ordinal());
548 432442f9 Leszek Koltunski
    }
549
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551
/**
552 fe6fe99a leszek
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
553 432442f9 Leszek Koltunski
 * This can fail if:
554
 * <ul>
555
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
556
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
557
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
558
 *     time only decreasing the value of 'max' is permitted.
559
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
560
 * </ul>
561
 *
562 fe6fe99a leszek
 * @param type {@link EffectType}
563
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
564 80b3acf6 Leszek Koltunski
 *            than Byte.MAX_VALUE
565
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
566
 */
567
  @SuppressWarnings("unused")
568 fe6fe99a leszek
  public static boolean setMax(EffectType type, int max)
569 80b3acf6 Leszek Koltunski
    {
570 fe6fe99a leszek
    return EffectQueue.setMax(type.ordinal(),max);
571 80b3acf6 Leszek Koltunski
    }
572
573 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
574
/**
575 6bb59aad Leszek Koltunski
 * Add a new Effect to our queue.
576 f2fe7e28 Leszek Koltunski
 *
577 6bb59aad Leszek Koltunski
 * @param effect The Effect to add.
578 ae77d55e Leszek Koltunski
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
579 6a06a912 Leszek Koltunski
 */
580 ae77d55e Leszek Koltunski
  public boolean apply(Effect effect)
581 d425545a Leszek Koltunski
    {
582 6bb59aad Leszek Koltunski
    switch(effect.getType())
583
      {
584 26a4e5f6 leszek
      case MATRIX      : return mM.add(effect);
585 ae77d55e Leszek Koltunski
      case VERTEX      : return mV.add(effect);
586
      case FRAGMENT    : return mF.add(effect);
587 80b3acf6 Leszek Koltunski
      case POSTPROCESS : return mP.add(effect);
588 6bb59aad Leszek Koltunski
      }
589 ae77d55e Leszek Koltunski
590
    return false;
591 4fde55a0 Leszek Koltunski
    }
592 f2fe7e28 Leszek Koltunski
  }