Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedEffects.java @ 466450b5

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 a4835695 Leszek Koltunski
35 c90b9e01 Leszek Koltunski
import java.io.InputStream;
36 c638c1b0 Leszek Koltunski
import java.nio.ByteBuffer;
37
import java.nio.ByteOrder;
38
import java.nio.FloatBuffer;
39 8777ce17 Leszek Koltunski
import java.nio.IntBuffer;
40 c638c1b0 Leszek Koltunski
41 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42 b329f352 Leszek Koltunski
/**
43 faa3ff56 Leszek Koltunski
 * Class containing Matrix, Vertex, Fragment and Postprocessing effect queues.
44 b73dcaa7 Leszek Koltunski
 * <p>
45 faa3ff56 Leszek Koltunski
 * The queues hold actual effects to be applied to a given (InputSurface,MeshObject) combo.
46 b329f352 Leszek Koltunski
 */
47 86d322b5 Leszek Koltunski
public class DistortedEffects
48 d425545a Leszek Koltunski
  {
49 1aedf874 leszek
  /// MAIN PROGRAM ///
50
  private static DistortedProgram mMainProgram;
51
  private static int mMainTextureH;
52 8fa96e69 Leszek Koltunski
53 c1e24646 leszek
  /// BLIT PROGRAM ///
54
  private static DistortedProgram mBlitProgram;
55
  private static int mBlitTextureH;
56 c2c08950 leszek
  private static int mBlitDepthH;
57 c90b9e01 Leszek Koltunski
  private static final FloatBuffer mQuadPositions;
58
59
  static
60
    {
61
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
62
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
63
    mQuadPositions.put(positionData).position(0);
64
    }
65
66 97020807 Leszek Koltunski
  /// BLIT DEPTH PROGRAM ///
67
  private static DistortedProgram mBlitDepthProgram;
68
  private static int mBlitDepthTextureH;
69
  private static int mBlitDepthDepthTextureH;
70
  private static int mBlitDepthDepthH;
71
  private static int mBlitDepthTexCorrH;
72
73 c1a38ba3 Leszek Koltunski
  /// NORMAL PROGRAM /////
74
  private static DistortedProgram mNormalProgram;
75
  private static int mNormalMVPMatrixH;
76
77 33f59f22 Leszek Koltunski
  /// OIT SSBO BUFFER ///
78
  private static int[] mLinkedListSSBO = new int[1];
79 12f9e4bb Leszek Koltunski
  private static int[] mAtomicCounter = new int[Distorted.FBO_QUEUE_SIZE];
80
  private static int   mCurrBuffer;
81 33f59f22 Leszek Koltunski
82
  static
83
    {
84
    mLinkedListSSBO[0]= -1;
85 12f9e4bb Leszek Koltunski
    mCurrBuffer       =  0;
86
87
    for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++)  mAtomicCounter[i] = -1;
88 33f59f22 Leszek Koltunski
    }
89
90 344ac0e4 Leszek Koltunski
  ///////////////////////////////////////////////////////////////
91 acb44099 Leszek Koltunski
  // meaning: allocate 1.0 screenful of places for transparent
92
  // fragments in the SSBO backing up the OIT render method.
93
  private static float mBufferSize=1.0f;
94 33f59f22 Leszek Koltunski
95 56c6ca24 Leszek Koltunski
  /// OIT CLEAR PROGRAM ///
96
  private static DistortedProgram mOITClearProgram;
97
  private static int mOITClearDepthH;
98
  private static int mOITClearTexCorrH;
99
  private static int mOITClearSizeH;
100
101
  /// OIT BUILD PROGRAM ///
102
  private static DistortedProgram mOITBuildProgram;
103
  private static int mOITBuildTextureH;
104
  private static int mOITBuildDepthTextureH;
105
  private static int mOITBuildDepthH;
106
  private static int mOITBuildTexCorrH;
107
  private static int mOITBuildSizeH;
108
  private static int mOITBuildNumRecordsH;
109 8777ce17 Leszek Koltunski
110 33f59f22 Leszek Koltunski
  /// OIT COLLAPSE PROGRAM ///
111
  private static DistortedProgram mOITCollapseProgram;
112
  private static int mOITCollapseDepthTextureH;
113
  private static int mOITCollapseDepthH;
114
  private static int mOITCollapseTexCorrH;
115
  private static int mOITCollapseSizeH;
116 cee0369a Leszek Koltunski
117 56c6ca24 Leszek Koltunski
  /// OIT RENDER PROGRAM ///
118
  private static DistortedProgram mOITRenderProgram;
119
  private static int mOITRenderDepthH;
120
  private static int mOITRenderTexCorrH;
121
  private static int mOITRenderSizeH;
122 7170e4eb leszek
123 c1a38ba3 Leszek Koltunski
  /// MAIN OIT PROGRAM ///
124
  private static DistortedProgram mMainOITProgram;
125
  private static int mMainOITTextureH;
126
  private static int mMainOITSizeH;
127
  private static int mMainOITNumRecordsH;
128 3fc9327a Leszek Koltunski
  /// END PROGRAMS //////
129 c638c1b0 Leszek Koltunski
130 8e34674e Leszek Koltunski
  private static long mNextID =0;
131 4e2382f3 Leszek Koltunski
  private long mID;
132 8e34674e Leszek Koltunski
133 310e14fb leszek
  private EffectQueueMatrix mM;
134 3fc9327a Leszek Koltunski
  private EffectQueueFragment mF;
135 310e14fb leszek
  private EffectQueueVertex mV;
136 80b3acf6 Leszek Koltunski
  private EffectQueuePostprocess mP;
137
138
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
139 3d590d8d Leszek Koltunski
140 9361b337 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
141 55c14a19 Leszek Koltunski
142 c25273e0 Leszek Koltunski
  static void createPrograms(Resources resources)
143 55c14a19 Leszek Koltunski
    {
144 cab7c165 Leszek Koltunski
    // MAIN PROGRAM ////////////////////////////////////
145 1aedf874 leszek
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
146
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
147
148 7cd24173 leszek
    int numF = FragmentEffect.getNumEnabled();
149
    int numV = VertexEffect.getNumEnabled();
150 c90b9e01 Leszek Koltunski
151 7cd24173 leszek
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
152
    String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
153
    String enabledEffectV= VertexEffect.getGLSL();
154
    String enabledEffectF= FragmentEffect.getGLSL();
155 c90b9e01 Leszek Koltunski
156 03cb451d Leszek Koltunski
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
157
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
158 7cd24173 leszek
    //android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
159
    //android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
160 c90b9e01 Leszek Koltunski
161 3fc9327a Leszek Koltunski
    String[] feedback = { "v_Position", "v_endPosition" };
162 cab7c165 Leszek Koltunski
163 cee0369a Leszek Koltunski
    try
164
      {
165
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
166
                                          enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
167
      }
168
    catch(Exception e)
169
      {
170
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
171
      throw new RuntimeException(e.getMessage());
172
      }
173 c90b9e01 Leszek Koltunski
174 c1e24646 leszek
    int mainProgramH = mMainProgram.getProgramHandle();
175 86c352e0 Leszek Koltunski
    EffectQueueFragment.getUniforms(mainProgramH,0);
176
    EffectQueueVertex.getUniforms(mainProgramH,0);
177
    EffectQueueMatrix.getUniforms(mainProgramH,0);
178 e6519ac8 Leszek Koltunski
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
179 c1e24646 leszek
180 1aedf874 leszek
    // BLIT PROGRAM ////////////////////////////////////
181 c1e24646 leszek
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
182
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
183
184 f2367b75 Leszek Koltunski
    try
185
      {
186 56c6ca24 Leszek Koltunski
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
187 f2367b75 Leszek Koltunski
      }
188
    catch(Exception e)
189
      {
190 cee0369a Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
191 f2367b75 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
192
      }
193 c1e24646 leszek
194
    int blitProgramH = mBlitProgram.getProgramHandle();
195 e6519ac8 Leszek Koltunski
    mBlitTextureH  = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
196
    mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
197 c90b9e01 Leszek Koltunski
198 97020807 Leszek Koltunski
    // BLIT DEPTH PROGRAM ////////////////////////////////////
199
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
200
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
201
202
    try
203
      {
204
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
205
      }
206
    catch(Exception e)
207
      {
208
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
209
      throw new RuntimeException(e.getMessage());
210
      }
211
212
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
213
    mBlitDepthTextureH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
214
    mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
215
    mBlitDepthDepthH        = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
216
    mBlitDepthTexCorrH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
217
218 c25273e0 Leszek Koltunski
    // NORMAL PROGRAM //////////////////////////////////////
219
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
220
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
221
222
    try
223
      {
224
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
225
      }
226
    catch(Exception e)
227
      {
228
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
229
      throw new RuntimeException(e.getMessage());
230
      }
231
232
    int normalProgramH = mNormalProgram.getProgramHandle();
233
    mNormalMVPMatrixH  = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
234
    }
235
236 031fbe7a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
237 c25273e0 Leszek Koltunski
238
  static void createProgramsOIT(Resources resources)
239
    {
240 c1a38ba3 Leszek Koltunski
    // MAIN OIT PROGRAM ////////////////////////////////
241
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
242
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
243
244
    int numF = FragmentEffect.getNumEnabled();
245
    int numV = VertexEffect.getNumEnabled();
246
247
    String mainVertHeader= Distorted.GLSL_VERSION +
248
                           ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n") +
249
                           ("#define OIT\n");
250
    String mainFragHeader= Distorted.GLSL_VERSION +
251
                           ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n") +
252
                           ("#define OIT\n");
253
254
    String enabledEffectV= VertexEffect.getGLSL();
255
    String enabledEffectF= FragmentEffect.getGLSL();
256
257
    try
258
      {
259
      mMainOITProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
260 86c352e0 Leszek Koltunski
                                             enabledEffectV, enabledEffectF, Distorted.GLSL, null);
261 c1a38ba3 Leszek Koltunski
      }
262
    catch(Exception e)
263
      {
264
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN OIT program: "+e.getMessage());
265
      throw new RuntimeException(e.getMessage());
266
      }
267
268
    int mainOITProgramH = mMainOITProgram.getProgramHandle();
269 86c352e0 Leszek Koltunski
    EffectQueueFragment.getUniforms(mainOITProgramH,1);
270
    EffectQueueVertex.getUniforms(mainOITProgramH,1);
271
    EffectQueueMatrix.getUniforms(mainOITProgramH,1);
272 c1a38ba3 Leszek Koltunski
    mMainOITTextureH    = GLES31.glGetUniformLocation( mainOITProgramH, "u_Texture");
273
    mMainOITSizeH       = GLES31.glGetUniformLocation( mainOITProgramH, "u_Size");
274
    mMainOITNumRecordsH = GLES31.glGetUniformLocation( mainOITProgramH, "u_numRecords");
275
276 56c6ca24 Leszek Koltunski
    // OIT CLEAR PROGRAM ////////////////////////////////////
277
    final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
278
    final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader);
279 7170e4eb leszek
280
    try
281
      {
282 56c6ca24 Leszek Koltunski
      mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
283 7170e4eb leszek
      }
284
    catch(Exception e)
285
      {
286 56c6ca24 Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage());
287 7170e4eb leszek
      throw new RuntimeException(e.getMessage());
288
      }
289
290 56c6ca24 Leszek Koltunski
    int oitClearProgramH   = mOITClearProgram.getProgramHandle();
291
    mOITClearDepthH        = GLES31.glGetUniformLocation( oitClearProgramH, "u_Depth");
292
    mOITClearTexCorrH      = GLES31.glGetUniformLocation( oitClearProgramH, "u_TexCorr");
293
    mOITClearSizeH         = GLES31.glGetUniformLocation( oitClearProgramH, "u_Size");
294
295
    // OIT BUILD PROGRAM ////////////////////////////////////
296
    final InputStream oitBuildVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
297
    final InputStream oitBuildFragStream = resources.openRawResource(R.raw.oit_build_fragment_shader);
298
299
    try
300
      {
301
      mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
302
      }
303
    catch(Exception e)
304
      {
305
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage());
306
      throw new RuntimeException(e.getMessage());
307
      }
308
309
    int oitBuildProgramH   = mOITBuildProgram.getProgramHandle();
310
    mOITBuildTextureH      = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Texture");
311
    mOITBuildDepthTextureH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture");
312
    mOITBuildDepthH        = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Depth");
313
    mOITBuildTexCorrH      = GLES31.glGetUniformLocation( oitBuildProgramH, "u_TexCorr");
314
    mOITBuildSizeH         = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Size");
315
    mOITBuildNumRecordsH   = GLES31.glGetUniformLocation( oitBuildProgramH, "u_numRecords");
316 8777ce17 Leszek Koltunski
317 33f59f22 Leszek Koltunski
    // OIT COLLAPSE PROGRAM ///////////////////////////
318
    final InputStream oitCollapseVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
319
    final InputStream oitCollapseFragStream = resources.openRawResource(R.raw.oit_collapse_fragment_shader);
320
321
    try
322
      {
323
      mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
324
      }
325
    catch(Exception e)
326
      {
327
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage());
328
      throw new RuntimeException(e.getMessage());
329
      }
330
331
    int oitCollapseProgramH   = mOITCollapseProgram.getProgramHandle();
332
    mOITCollapseDepthTextureH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture");
333
    mOITCollapseDepthH        = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_Depth");
334
    mOITCollapseTexCorrH      = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr");
335
    mOITCollapseSizeH         = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_Size");
336
337
    // OIT RENDER PROGRAM ///////////////////////////
338 56c6ca24 Leszek Koltunski
    final InputStream oitRenderVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
339
    final InputStream oitRenderFragStream = resources.openRawResource(R.raw.oit_render_fragment_shader);
340 8777ce17 Leszek Koltunski
341
    try
342
      {
343 56c6ca24 Leszek Koltunski
      mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
344 8777ce17 Leszek Koltunski
      }
345
    catch(Exception e)
346
      {
347 56c6ca24 Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage());
348 8777ce17 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
349
      }
350
351 56c6ca24 Leszek Koltunski
    int oitRenderProgramH   = mOITRenderProgram.getProgramHandle();
352
    mOITRenderDepthH        = GLES31.glGetUniformLocation( oitRenderProgramH, "u_Depth");
353
    mOITRenderTexCorrH      = GLES31.glGetUniformLocation( oitRenderProgramH, "u_TexCorr");
354
    mOITRenderSizeH         = GLES31.glGetUniformLocation( oitRenderProgramH, "u_Size");
355 55c14a19 Leszek Koltunski
    }
356
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
359 421c2728 Leszek Koltunski
  private void initializeEffectLists(DistortedEffects d, int flags)
360 d425545a Leszek Koltunski
    {
361 015642fb Leszek Koltunski
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
362 6a06a912 Leszek Koltunski
      {
363 d425545a Leszek Koltunski
      mM = d.mM;
364
      matrixCloned = true;
365
      }
366
    else
367
      {
368 4e2382f3 Leszek Koltunski
      mM = new EffectQueueMatrix(mID);
369 d425545a Leszek Koltunski
      matrixCloned = false;
370
      }
371 6a06a912 Leszek Koltunski
    
372 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
373
      {
374
      mV = d.mV;
375
      vertexCloned = true;
376
      }
377
    else
378
      {
379 4e2382f3 Leszek Koltunski
      mV = new EffectQueueVertex(mID);
380 d425545a Leszek Koltunski
      vertexCloned = false;
381
      }
382 6a06a912 Leszek Koltunski
    
383 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
384
      {
385
      mF = d.mF;
386
      fragmentCloned = true;
387 6a06a912 Leszek Koltunski
      }
388 d425545a Leszek Koltunski
    else
389
      {
390 4e2382f3 Leszek Koltunski
      mF = new EffectQueueFragment(mID);
391 d425545a Leszek Koltunski
      fragmentCloned = false;
392
      }
393 80b3acf6 Leszek Koltunski
394
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
395
      {
396
      mP = d.mP;
397
      postprocessCloned = true;
398
      }
399
    else
400
      {
401
      mP = new EffectQueuePostprocess(mID);
402
      postprocessCloned = false;
403
      }
404
    }
405
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407
408 70b6a155 Leszek Koltunski
  EffectQueuePostprocess getPostprocess()
409 80b3acf6 Leszek Koltunski
    {
410 70b6a155 Leszek Koltunski
    return mP;
411 80b3acf6 Leszek Koltunski
    }
412
413 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
414
415
  void newNode(DistortedNode node)
416
    {
417
    mM.newNode(node);
418
    mF.newNode(node);
419
    mV.newNode(node);
420
    mP.newNode(node);
421
    }
422
423 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
424 c90b9e01 Leszek Koltunski
425 3fc9327a Leszek Koltunski
  private void displayNormals(MeshObject mesh)
426 c90b9e01 Leszek Koltunski
    {
427 466450b5 Leszek Koltunski
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.getTFO() );
428 e6519ac8 Leszek Koltunski
    GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
429 604b2899 Leszek Koltunski
    DistortedRenderState.switchOffDrawing();
430 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
431 604b2899 Leszek Koltunski
    DistortedRenderState.restoreDrawing();
432 e6519ac8 Leszek Koltunski
    GLES31.glEndTransformFeedback();
433
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
434 604b2899 Leszek Koltunski
435 3fc9327a Leszek Koltunski
    mNormalProgram.useProgram();
436 e6519ac8 Leszek Koltunski
    GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
437 466450b5 Leszek Koltunski
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getTFO() );
438 e6519ac8 Leszek Koltunski
    GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
439
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
440
    GLES31.glLineWidth(8.0f);
441
    GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*mesh.numVertices);
442 c90b9e01 Leszek Koltunski
    }
443 1aedf874 leszek
444 c1a38ba3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
445
446 a13dde77 Leszek Koltunski
  void send(float halfW, float halfH, float halfZ, float margin, DistortedOutputSurface surface, int variant)
447
    {
448
    mM.send(surface,halfW,halfH,halfZ,margin,variant);
449
    mV.send(variant);
450
    }
451
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453
454
  void drawPrivOIT(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime)
455 c1a38ba3 Leszek Koltunski
    {
456
    float halfZ = halfW*mesh.zFactor;
457
458
    mM.compute(currTime);
459
    mV.compute(currTime,halfW,halfH,halfZ);
460
    mF.compute(currTime,halfW,halfH);
461
    mP.compute(currTime);
462
463
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
464
465
    mMainOITProgram.useProgram();
466
    GLES31.glUniform1i(mMainOITTextureH, 0);
467
    GLES31.glUniform2ui(mMainOITSizeH, surface.mWidth, surface.mHeight);
468 acb44099 Leszek Koltunski
    GLES31.glUniform1ui(mMainOITNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
469 c1a38ba3 Leszek Koltunski
470 466450b5 Leszek Koltunski
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getVBO() );
471 c1a38ba3 Leszek Koltunski
    GLES31.glVertexAttribPointer(mMainOITProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
472
    GLES31.glVertexAttribPointer(mMainOITProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
473
    GLES31.glVertexAttribPointer(mMainOITProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
474
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
475
476 a13dde77 Leszek Koltunski
    mM.send(surface,halfW,halfH,halfZ,0,1);
477 86c352e0 Leszek Koltunski
    mV.send(1);
478
    mF.send(1);
479 c1a38ba3 Leszek Koltunski
480
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
481
482 99730f26 Leszek Koltunski
    if( mesh.mShowNormals )
483
      {
484
      mMainProgram.useProgram();
485
      mM.send(surface,halfW,halfH,halfZ,0,0);
486
      mV.send(0);
487
      mF.send(0);
488
      displayNormals(mesh);
489
      }
490 c1a38ba3 Leszek Koltunski
    }
491
492 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
493
494 a13dde77 Leszek Koltunski
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime)
495 d425545a Leszek Koltunski
    {
496 c1e24646 leszek
    float halfZ = halfW*mesh.zFactor;
497 638b5b5c leszek
498 8fbd0237 Leszek Koltunski
    mM.compute(currTime);
499
    mV.compute(currTime,halfW,halfH,halfZ);
500
    mF.compute(currTime,halfW,halfH);
501 1149be8f leszek
    mP.compute(currTime);
502 8fbd0237 Leszek Koltunski
503 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
504 d6e94c84 Leszek Koltunski
505 b9798977 leszek
    mMainProgram.useProgram();
506 e6519ac8 Leszek Koltunski
    GLES31.glUniform1i(mMainTextureH, 0);
507 12f45260 Leszek Koltunski
508 466450b5 Leszek Koltunski
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getVBO() );
509 8777ce17 Leszek Koltunski
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
510
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
511
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
512
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
513 cab7c165 Leszek Koltunski
514 a13dde77 Leszek Koltunski
    mM.send(surface,halfW,halfH,halfZ,0,0);
515 86c352e0 Leszek Koltunski
    mV.send(0);
516
    mF.send(0);
517 42571056 Leszek Koltunski
518 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
519 c90b9e01 Leszek Koltunski
520 3fc9327a Leszek Koltunski
    if( mesh.mShowNormals ) displayNormals(mesh);
521 d425545a Leszek Koltunski
    }
522 6a06a912 Leszek Koltunski
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524 7266d8ef Leszek Koltunski
/**
525
 * Only for use by the library itself.
526
 *
527
 * @y.exclude
528
 */
529
  public static void blitPriv(DistortedOutputSurface surface)
530 d425545a Leszek Koltunski
    {
531 c1e24646 leszek
    mBlitProgram.useProgram();
532
533 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
534
    GLES31.glUniform1i(mBlitTextureH, 0);
535
    GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
536
    GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
537
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
538 d425545a Leszek Koltunski
    }
539 7170e4eb leszek
540 97020807 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
541
542
  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
543
    {
544
    mBlitDepthProgram.useProgram();
545
546
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
547
    GLES31.glUniform1i(mBlitDepthTextureH, 0);
548
    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
549
    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
550
    GLES31.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
551
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
552
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
553
    }
554
555 375b3950 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
556
557 12f9e4bb Leszek Koltunski
  private static int printPreviousBuffer()
558 375b3950 Leszek Koltunski
    {
559 12f9e4bb Leszek Koltunski
    int counter = 0;
560
561
    ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
562
                                                                GLES31.GL_MAP_READ_BIT);
563
    if( atomicBuf!=null )
564
      {
565
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
566
      counter = atomicIntBuf.get(0);
567
      }
568
    else
569 acb44099 Leszek Koltunski
      {
570 12f9e4bb Leszek Koltunski
      android.util.Log.e("effects", "print: failed to map atomic buffer");
571 acb44099 Leszek Koltunski
      }
572
573 12f9e4bb Leszek Koltunski
    GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
574 375b3950 Leszek Koltunski
575 12f9e4bb Leszek Koltunski
    return counter;
576
    }
577
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579
580
  private static void zeroBuffer()
581
    {
582 375b3950 Leszek Koltunski
    ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
583 12f9e4bb Leszek Koltunski
        GLES31.GL_MAP_WRITE_BIT|GLES31.GL_MAP_INVALIDATE_BUFFER_BIT);
584 375b3950 Leszek Koltunski
    if( atomicBuf!=null )
585
      {
586
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
587 c92d84ec Leszek Koltunski
      atomicIntBuf.put(0,0);
588 375b3950 Leszek Koltunski
      }
589
    else
590
      {
591 12f9e4bb Leszek Koltunski
      android.util.Log.e("effects", "zero: failed to map atomic buffer");
592 375b3950 Leszek Koltunski
      }
593
594
    GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
595 12f9e4bb Leszek Koltunski
    }
596
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598
// reset atomic counter to 0
599
600
  static int zeroOutAtomic()
601
    {
602
    int counter = 0;
603
604
    if( mAtomicCounter[0]<0 )
605
      {
606
      GLES31.glGenBuffers(Distorted.FBO_QUEUE_SIZE,mAtomicCounter,0);
607
608
      for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++)
609
        {
610
        GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]);
611
        GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES31.GL_DYNAMIC_DRAW);
612
        zeroBuffer();
613
        }
614
      }
615
616
    // reading the value of the buffer on every frame would slow down rendering by
617
    // about 3%; doing it only once every 5 frames affects speed by less than 1%.
618
    if( mCurrBuffer==0 )
619
      {
620
      GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
621
      counter = printPreviousBuffer();
622
      }
623
624
    if( ++mCurrBuffer>=Distorted.FBO_QUEUE_SIZE ) mCurrBuffer = 0;
625
626
    GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
627
    zeroBuffer();
628
629
    return counter;
630 375b3950 Leszek Koltunski
    }
631
632 7170e4eb leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
633 56c6ca24 Leszek Koltunski
// Pass1 of the OIT algorithm. Clear per-pixel head-poiners.
634
635 12f9e4bb Leszek Koltunski
  static void oitClear(DistortedOutputSurface surface, int counter)
636 56c6ca24 Leszek Koltunski
    {
637 acb44099 Leszek Koltunski
    if( mLinkedListSSBO[0]<0 )
638
      {
639
      GLES31.glGenBuffers(1,mLinkedListSSBO,0);
640 12f9e4bb Leszek Koltunski
641
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
642 acb44099 Leszek Koltunski
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
643
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
644 12f9e4bb Leszek Koltunski
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
645
646 acb44099 Leszek Koltunski
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
647 12f9e4bb Leszek Koltunski
      }
648
649
    // See if we have overflown the SSBO in one of the previous frames.
650
    // If yes, assume we need to make the SSBO larger.
651
    float overflow = counter/(mBufferSize*surface.mWidth*surface.mHeight);
652
653
    if( overflow>1.0f )
654
      {
655
      //android.util.Log.e("effects", "previous frame rendered "+counter+
656
      //                   " fragments, but there was only "+(mBufferSize*surface.mWidth*surface.mHeight)+" space");
657
658
      mBufferSize *= (int)(overflow+1.0f);
659
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
660
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
661
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
662 acb44099 Leszek Koltunski
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
663
      }
664
665 56c6ca24 Leszek Koltunski
    mOITClearProgram.useProgram();
666
667
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
668
    GLES31.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f );   // corrections do not really matter here - only present because of common vertex shader.
669
    GLES31.glUniform1f( mOITClearDepthH , 1.0f);          // likewise depth
670 344ac0e4 Leszek Koltunski
    GLES31.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight);
671 56c6ca24 Leszek Koltunski
    GLES31.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
672
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
673
    }
674
675
///////////////////////////////////////////////////////////////////////////////////////////////////
676
// Pass2 of the OIT algorithm - build per-pixel linked lists.
677 7170e4eb leszek
678 56c6ca24 Leszek Koltunski
  static void oitBuild(DistortedOutputSurface surface, float corrW, float corrH)
679 7170e4eb leszek
    {
680 56c6ca24 Leszek Koltunski
    mOITBuildProgram.useProgram();
681 7170e4eb leszek
682 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
683 56c6ca24 Leszek Koltunski
    GLES31.glUniform1i(mOITBuildTextureH, 0);
684
    GLES31.glUniform1i(mOITBuildDepthTextureH, 1);
685
    GLES31.glUniform2f(mOITBuildTexCorrH, corrW, corrH );
686 344ac0e4 Leszek Koltunski
    GLES31.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight);
687 acb44099 Leszek Koltunski
    GLES31.glUniform1ui(mOITBuildNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
688 56c6ca24 Leszek Koltunski
    GLES31.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear);
689
    GLES31.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
690 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
691 8777ce17 Leszek Koltunski
    }
692
693
///////////////////////////////////////////////////////////////////////////////////////////////////
694 33f59f22 Leszek Koltunski
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list.
695
696
  static void oitCollapse(DistortedOutputSurface surface, float corrW, float corrH)
697
    {
698
    mOITCollapseProgram.useProgram();
699
700
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
701
    GLES31.glUniform1i(mOITCollapseDepthTextureH, 1);
702
    GLES31.glUniform2f(mOITCollapseTexCorrH, corrW, corrH );
703 344ac0e4 Leszek Koltunski
    GLES31.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight);
704 33f59f22 Leszek Koltunski
    GLES31.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear);
705
    GLES31.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
706
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
707
    }
708
709
///////////////////////////////////////////////////////////////////////////////////////////////////
710
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists.
711 8777ce17 Leszek Koltunski
712 56c6ca24 Leszek Koltunski
  static void oitRender(DistortedOutputSurface surface, float corrW, float corrH)
713 8777ce17 Leszek Koltunski
    {
714 56c6ca24 Leszek Koltunski
    mOITRenderProgram.useProgram();
715 8777ce17 Leszek Koltunski
716
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
717 56c6ca24 Leszek Koltunski
    GLES31.glUniform2f(mOITRenderTexCorrH, corrW, corrH );
718 344ac0e4 Leszek Koltunski
    GLES31.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight);
719 56c6ca24 Leszek Koltunski
    GLES31.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear);
720
    GLES31.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
721 8777ce17 Leszek Koltunski
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
722 7170e4eb leszek
    }
723
724 cee0369a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
725
726 8e34674e Leszek Koltunski
  private void releasePriv()
727 d425545a Leszek Koltunski
    {
728 344ac0e4 Leszek Koltunski
    if( !matrixCloned      ) mM.abortAll(false);
729
    if( !vertexCloned      ) mV.abortAll(false);
730
    if( !fragmentCloned    ) mF.abortAll(false);
731
    if( !postprocessCloned ) mP.abortAll(false);
732 d425545a Leszek Koltunski
733 4e2382f3 Leszek Koltunski
    mM = null;
734
    mV = null;
735
    mF = null;
736 80b3acf6 Leszek Koltunski
    mP = null;
737 8e34674e Leszek Koltunski
    }
738
739 1942537e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
740
741 78e89fb5 Leszek Koltunski
  static void onPause()
742 1942537e Leszek Koltunski
    {
743 8777ce17 Leszek Koltunski
    mLinkedListSSBO[0]= -1;
744 12f9e4bb Leszek Koltunski
745
    for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++) mAtomicCounter[i] = -1;
746 1942537e Leszek Koltunski
    }
747
748 78e89fb5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
749
750
  static void onDestroy()
751
    {
752
    mNextID =  0;
753
    }
754
755 12f9e4bb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
756
757
  static void setSSBOSize(float size)
758
    {
759
    mBufferSize = size;
760
    }
761
762 8e34674e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
763
// PUBLIC API
764 ada90d33 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
765 d425545a Leszek Koltunski
/**
766 4e2382f3 Leszek Koltunski
 * Create empty effect queue.
767 d425545a Leszek Koltunski
 */
768 421c2728 Leszek Koltunski
  public DistortedEffects()
769 d425545a Leszek Koltunski
    {
770 c7da4e65 leszek
    mID = ++mNextID;
771 4e2382f3 Leszek Koltunski
    initializeEffectLists(this,0);
772 d425545a Leszek Koltunski
    }
773 ada90d33 Leszek Koltunski
774
///////////////////////////////////////////////////////////////////////////////////////////////////
775 d425545a Leszek Koltunski
/**
776 4e2382f3 Leszek Koltunski
 * Copy constructor.
777 d425545a Leszek Koltunski
 * <p>
778
 * Whatever we do not clone gets created just like in the default constructor.
779
 *
780
 * @param dc    Source object to create our object from
781
 * @param flags A bitmask of values specifying what to copy.
782 e6ab30eb Leszek Koltunski
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
783 d425545a Leszek Koltunski
 */
784 421c2728 Leszek Koltunski
  public DistortedEffects(DistortedEffects dc, int flags)
785 d425545a Leszek Koltunski
    {
786 c7da4e65 leszek
    mID = ++mNextID;
787 4e2382f3 Leszek Koltunski
    initializeEffectLists(dc,flags);
788 d425545a Leszek Koltunski
    }
789 ada90d33 Leszek Koltunski
790 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
791
/**
792 07305c87 Leszek Koltunski
 * Releases all resources. After this call, the queue should not be used anymore.
793 6a06a912 Leszek Koltunski
 */
794 13687207 leszek
  @SuppressWarnings("unused")
795 8e34674e Leszek Koltunski
  public synchronized void delete()
796 d425545a Leszek Koltunski
    {
797
    releasePriv();
798
    }
799 6a06a912 Leszek Koltunski
800
///////////////////////////////////////////////////////////////////////////////////////////////////
801
/**
802 4e2382f3 Leszek Koltunski
 * Returns unique ID of this instance.
803
 *
804
 * @return ID of the object.
805 6a06a912 Leszek Koltunski
 */
806 4e2382f3 Leszek Koltunski
  public long getID()
807 d425545a Leszek Koltunski
      {
808 4e2382f3 Leszek Koltunski
      return mID;
809 d425545a Leszek Koltunski
      }
810 4e2382f3 Leszek Koltunski
811 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
812
/**
813
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
814 faa3ff56 Leszek Koltunski
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
815 6a06a912 Leszek Koltunski
 * 
816
 * @param el A class implementing the EffectListener interface that wants to get notifications.
817
 */
818 13687207 leszek
  @SuppressWarnings("unused")
819 3fc994b2 Leszek Koltunski
  public void registerForMessages(EffectListener el)
820 d425545a Leszek Koltunski
    {
821 26a4e5f6 leszek
    mM.registerForMessages(el);
822 3fc994b2 Leszek Koltunski
    mV.registerForMessages(el);
823
    mF.registerForMessages(el);
824 80b3acf6 Leszek Koltunski
    mP.registerForMessages(el);
825 d425545a Leszek Koltunski
    }
826 6a06a912 Leszek Koltunski
827
///////////////////////////////////////////////////////////////////////////////////////////////////
828
/**
829 faa3ff56 Leszek Koltunski
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
830 6a06a912 Leszek Koltunski
 * 
831
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
832
 */
833 13687207 leszek
  @SuppressWarnings("unused")
834 3fc994b2 Leszek Koltunski
  public void deregisterForMessages(EffectListener el)
835 d425545a Leszek Koltunski
    {
836 26a4e5f6 leszek
    mM.deregisterForMessages(el);
837 3fc994b2 Leszek Koltunski
    mV.deregisterForMessages(el);
838
    mF.deregisterForMessages(el);
839 80b3acf6 Leszek Koltunski
    mP.deregisterForMessages(el);
840 d425545a Leszek Koltunski
    }
841 6a06a912 Leszek Koltunski
842
///////////////////////////////////////////////////////////////////////////////////////////////////
843
/**
844 d07f2950 Leszek Koltunski
 * Aborts all Effects.
845
 * @return Number of effects aborted.
846 6a06a912 Leszek Koltunski
 */
847 d425545a Leszek Koltunski
  public int abortAllEffects()
848 13687207 leszek
    {
849
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
850
    }
851 6a06a912 Leszek Koltunski
852
///////////////////////////////////////////////////////////////////////////////////////////////////
853
/**
854 d07f2950 Leszek Koltunski
 * Aborts all Effects of a given type, for example all MATRIX Effects.
855 6a06a912 Leszek Koltunski
 * 
856 da9b3f07 Leszek Koltunski
 * @param type one of the constants defined in {@link EffectType}
857 d07f2950 Leszek Koltunski
 * @return Number of effects aborted.
858 6a06a912 Leszek Koltunski
 */
859 da9b3f07 Leszek Koltunski
  public int abortByType(EffectType type)
860 d425545a Leszek Koltunski
    {
861
    switch(type)
862 6a06a912 Leszek Koltunski
      {
863 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.abortAll(true);
864
      case VERTEX     : return mV.abortAll(true);
865
      case FRAGMENT   : return mF.abortAll(true);
866 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.abortAll(true);
867 da9b3f07 Leszek Koltunski
      default         : return 0;
868 6a06a912 Leszek Koltunski
      }
869 d425545a Leszek Koltunski
    }
870 47316d20 leszek
871
///////////////////////////////////////////////////////////////////////////////////////////////////
872
/**
873 faa3ff56 Leszek Koltunski
 * Aborts an Effect by its ID.
874 47316d20 leszek
 *
875
 * @param id the Id of the Effect to be removed, as returned by getID().
876
 * @return Number of effects aborted.
877
 */
878
  public int abortById(long id)
879
    {
880
    long type = id&EffectType.MASK;
881
882 2ef5dd9e leszek
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
883
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
884
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
885 80b3acf6 Leszek Koltunski
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
886 47316d20 leszek
887
    return 0;
888
    }
889
890 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
891
/**
892
 * Aborts a single Effect.
893
 * 
894 6bb59aad Leszek Koltunski
 * @param effect the Effect we want to abort.
895 476bbc81 Leszek Koltunski
 * @return number of Effects aborted. Always either 0 or 1.
896 6a06a912 Leszek Koltunski
 */
897 6bb59aad Leszek Koltunski
  public int abortEffect(Effect effect)
898 d425545a Leszek Koltunski
    {
899 6bb59aad Leszek Koltunski
    switch(effect.getType())
900
      {
901 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeEffect(effect);
902
      case VERTEX     : return mV.removeEffect(effect);
903
      case FRAGMENT   : return mF.removeEffect(effect);
904 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeEffect(effect);
905 da9b3f07 Leszek Koltunski
      default         : return 0;
906 6bb59aad Leszek Koltunski
      }
907 d425545a Leszek Koltunski
    }
908 6a06a912 Leszek Koltunski
909
///////////////////////////////////////////////////////////////////////////////////////////////////
910
/**
911 e8c81a8e Leszek Koltunski
 * Abort all Effects of a given name, for example all rotations.
912 6a06a912 Leszek Koltunski
 * 
913 da9b3f07 Leszek Koltunski
 * @param name one of the constants defined in {@link EffectName}
914 476bbc81 Leszek Koltunski
 * @return number of Effects aborted.
915 6a06a912 Leszek Koltunski
 */
916 da9b3f07 Leszek Koltunski
  public int abortByName(EffectName name)
917 d425545a Leszek Koltunski
    {
918 da9b3f07 Leszek Koltunski
    switch(name.getType())
919 6a06a912 Leszek Koltunski
      {
920 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeByName(name);
921
      case VERTEX     : return mV.removeByName(name);
922
      case FRAGMENT   : return mF.removeByName(name);
923 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeByName(name);
924 6bb59aad Leszek Koltunski
      default                : return 0;
925 6a06a912 Leszek Koltunski
      }
926 d425545a Leszek Koltunski
    }
927 432442f9 Leszek Koltunski
928
///////////////////////////////////////////////////////////////////////////////////////////////////
929
/**
930 faa3ff56 Leszek Koltunski
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
931
 * single (InputSurface,MeshObject) combo.
932 432442f9 Leszek Koltunski
 *
933 fe6fe99a leszek
 * @param type {@link EffectType}
934
 * @return The maximum number of effects of a given type.
935 432442f9 Leszek Koltunski
 */
936 13687207 leszek
  @SuppressWarnings("unused")
937 fe6fe99a leszek
  public static int getMax(EffectType type)
938 432442f9 Leszek Koltunski
    {
939 fe6fe99a leszek
    return EffectQueue.getMax(type.ordinal());
940 432442f9 Leszek Koltunski
    }
941
942
///////////////////////////////////////////////////////////////////////////////////////////////////
943
/**
944 fe6fe99a leszek
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
945 432442f9 Leszek Koltunski
 * This can fail if:
946
 * <ul>
947
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
948
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
949
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
950
 *     time only decreasing the value of 'max' is permitted.
951
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
952
 * </ul>
953
 *
954 fe6fe99a leszek
 * @param type {@link EffectType}
955
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
956 80b3acf6 Leszek Koltunski
 *            than Byte.MAX_VALUE
957
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
958
 */
959
  @SuppressWarnings("unused")
960 fe6fe99a leszek
  public static boolean setMax(EffectType type, int max)
961 80b3acf6 Leszek Koltunski
    {
962 fe6fe99a leszek
    return EffectQueue.setMax(type.ordinal(),max);
963 80b3acf6 Leszek Koltunski
    }
964
965 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
966
/**
967 6bb59aad Leszek Koltunski
 * Add a new Effect to our queue.
968 f2fe7e28 Leszek Koltunski
 *
969 6bb59aad Leszek Koltunski
 * @param effect The Effect to add.
970 ae77d55e Leszek Koltunski
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
971 6a06a912 Leszek Koltunski
 */
972 ae77d55e Leszek Koltunski
  public boolean apply(Effect effect)
973 d425545a Leszek Koltunski
    {
974 6bb59aad Leszek Koltunski
    switch(effect.getType())
975
      {
976 26a4e5f6 leszek
      case MATRIX      : return mM.add(effect);
977 ae77d55e Leszek Koltunski
      case VERTEX      : return mV.add(effect);
978
      case FRAGMENT    : return mF.add(effect);
979 80b3acf6 Leszek Koltunski
      case POSTPROCESS : return mP.add(effect);
980 6bb59aad Leszek Koltunski
      }
981 ae77d55e Leszek Koltunski
982
    return false;
983 4fde55a0 Leszek Koltunski
    }
984 f2fe7e28 Leszek Koltunski
  }