Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedLibrary.java @ 644c21f2

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 d333eb6b Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 d333eb6b Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 d333eb6b Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
22 30beb34f Leszek Koltunski
import android.app.ActivityManager;
23 7845dc66 Leszek Koltunski
import android.content.Context;
24 30beb34f Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 d6e94c84 Leszek Koltunski
import android.content.res.Resources;
26 edea9cf3 Leszek Koltunski
import android.opengl.GLES31;
27 bfe45b4a Leszek Koltunski
import android.util.Log;
28 310e14fb leszek
29 bfe45b4a Leszek Koltunski
import org.distorted.library.R;
30 26a4e5f6 leszek
import org.distorted.library.effect.Effect;
31 809dcae3 Leszek Koltunski
import org.distorted.library.effectqueue.EffectQueue;
32
import org.distorted.library.effectqueue.EffectQueuePostprocess;
33 bfe45b4a Leszek Koltunski
import org.distorted.library.effect.EffectType;
34
import org.distorted.library.effect.FragmentEffect;
35 aa2f0486 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffect;
36 bfe45b4a Leszek Koltunski
import org.distorted.library.effect.VertexEffect;
37
import org.distorted.library.mesh.MeshBase;
38 ed06301f Leszek Koltunski
import org.distorted.library.message.EffectMessageSender;
39 bfe45b4a Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
40
41
import java.io.InputStream;
42
import java.nio.ByteBuffer;
43
import java.nio.ByteOrder;
44
import java.nio.FloatBuffer;
45
import java.nio.IntBuffer;
46 6a06a912 Leszek Koltunski
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
/**
49 f953bee0 Leszek Koltunski
 * A singleton class used to control various global dialog_settings.
50 6a06a912 Leszek Koltunski
 */
51 7602a827 Leszek Koltunski
public class DistortedLibrary
52 39cbf9dc Leszek Koltunski
  {
53 c25273e0 Leszek Koltunski
  public static final int GLSL = 310;
54
  public static final String GLSL_VERSION= "#version 310 es\n";
55 6a06a912 Leszek Koltunski
  /**
56 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
57
   * backing up our DistortedTexture.
58 6a06a912 Leszek Koltunski
   * <p>
59 cacc63de Leszek Koltunski
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
60 6a06a912 Leszek Koltunski
   * apply different effects. Used in the copy constructor.
61
   */
62 29a06526 Leszek Koltunski
  public static final int CLONE_SURFACE = 0x1;
63 6a06a912 Leszek Koltunski
  /**
64 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
65 6a06a912 Leszek Koltunski
   * <p>
66 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
67 6a06a912 Leszek Koltunski
   */
68 015642fb Leszek Koltunski
  public static final int CLONE_MATRIX = 0x2;
69 6a06a912 Leszek Koltunski
  /**
70 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
71 6a06a912 Leszek Koltunski
   * <p>
72 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
73 6a06a912 Leszek Koltunski
   */
74
  public static final int CLONE_VERTEX  = 0x4;
75
  /**
76 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
77 6a06a912 Leszek Koltunski
   * <p>
78 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
79 6a06a912 Leszek Koltunski
   */
80
  public static final int CLONE_FRAGMENT= 0x8;
81 d6e94c84 Leszek Koltunski
   /**
82
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
83
   * <p>
84
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
85
   */
86
  public static final int CLONE_POSTPROCESS= 0x10;
87 6a06a912 Leszek Koltunski
  /**
88 a09ada4c Leszek Koltunski
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
89 6a06a912 Leszek Koltunski
   * <p>
90 cacc63de Leszek Koltunski
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
91
   * on the screen with (optionally) different Effects.
92 6a06a912 Leszek Koltunski
   */
93 d6e94c84 Leszek Koltunski
  public static final int CLONE_CHILDREN= 0x20;
94 b3618cb5 Leszek Koltunski
95 586b5fa1 Leszek Koltunski
  /**
96
   * Work around bugs in ARM Mali driver by, instead to a single FBO, rendering to a circular queue
97
   * of FBO_QUEUE_SIZE FBOs. (otherwise we sometimes get a 'full pipeline flush' and the end result
98
   * might be missing part of the Objects)
99 6672d895 Leszek Koltunski
   *
100 12f9e4bb Leszek Koltunski
   * This bug only exists on Mali driver r12.
101 6672d895 Leszek Koltunski
   *
102
   * https://community.arm.com/graphics/f/discussions/10285/opengl-es-3-1-on-mali-t880-flashes
103 586b5fa1 Leszek Koltunski
   */
104 973e99d7 Leszek Koltunski
  static final int FBO_QUEUE_SIZE = 4;
105 586b5fa1 Leszek Koltunski
106 55c14a19 Leszek Koltunski
  private static boolean mInitialized=false;
107 6a06a912 Leszek Koltunski
108 bfe45b4a Leszek Koltunski
  //////////////////////////////////////////////////////////////////////////////////////////////
109
  /// MAIN PROGRAM ///
110
  private static DistortedProgram mMainProgram;
111
  private static int mMainTextureH;
112
113
  /// NORMAL PROGRAM /////
114
  private static DistortedProgram mNormalProgram;
115
  private static int mNormalMVPMatrixH;
116
117
  /// MAIN OIT PROGRAM ///
118
  private static DistortedProgram mMainOITProgram;
119
  private static int mMainOITTextureH;
120
  private static int mMainOITSizeH;
121
  private static int mMainOITNumRecordsH;
122
123
  /// BLIT PROGRAM ///
124
  private static DistortedProgram mBlitProgram;
125
  private static int mBlitTextureH;
126
  private static int mBlitDepthH;
127
  private static final FloatBuffer mQuadPositions;
128
129
  static
130
    {
131
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
132
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
133
    mQuadPositions.put(positionData).position(0);
134
    }
135
136
  /// BLIT DEPTH PROGRAM ///
137
  private static DistortedProgram mBlitDepthProgram;
138
  private static int mBlitDepthTextureH;
139
  private static int mBlitDepthDepthTextureH;
140
  private static int mBlitDepthDepthH;
141
  private static int mBlitDepthTexCorrH;
142
143
  /// OIT SSBO BUFFER ///
144
  private static int[] mLinkedListSSBO = new int[1];
145 7602a827 Leszek Koltunski
  private static int[] mAtomicCounter = new int[DistortedLibrary.FBO_QUEUE_SIZE];
146 bfe45b4a Leszek Koltunski
  private static int   mCurrBuffer;
147
148
  static
149
    {
150
    mLinkedListSSBO[0]= -1;
151
    mCurrBuffer       =  0;
152
153 7602a827 Leszek Koltunski
    for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++)  mAtomicCounter[i] = -1;
154 bfe45b4a Leszek Koltunski
    }
155
156
  ///////////////////////////////////////////////////////////////
157
  // meaning: allocate 1.0 screenful of places for transparent
158
  // fragments in the SSBO backing up the OIT render method.
159
  private static float mBufferSize=1.0f;
160
161
  /// OIT CLEAR PROGRAM ///
162
  private static DistortedProgram mOITClearProgram;
163
  private static int mOITClearDepthH;
164
  private static int mOITClearTexCorrH;
165
  private static int mOITClearSizeH;
166
167
  /// OIT BUILD PROGRAM ///
168
  private static DistortedProgram mOITBuildProgram;
169
  private static int mOITBuildTextureH;
170
  private static int mOITBuildDepthTextureH;
171
  private static int mOITBuildDepthH;
172
  private static int mOITBuildTexCorrH;
173
  private static int mOITBuildSizeH;
174
  private static int mOITBuildNumRecordsH;
175
176
  /// OIT COLLAPSE PROGRAM ///
177
  private static DistortedProgram mOITCollapseProgram;
178
  private static int mOITCollapseDepthTextureH;
179
  private static int mOITCollapseDepthH;
180
  private static int mOITCollapseTexCorrH;
181
  private static int mOITCollapseSizeH;
182
183
  /// OIT RENDER PROGRAM ///
184
  private static DistortedProgram mOITRenderProgram;
185
  private static int mOITRenderDepthH;
186
  private static int mOITRenderTexCorrH;
187
  private static int mOITRenderSizeH;
188
189
  /// END PROGRAMS //////
190
191 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
192 c638c1b0 Leszek Koltunski
// private: hide this from Javadoc
193 6a06a912 Leszek Koltunski
194 7602a827 Leszek Koltunski
  private DistortedLibrary()
195 6a06a912 Leszek Koltunski
    {
196 8eccf334 Leszek Koltunski
197 6a06a912 Leszek Koltunski
    }
198
199 bfe45b4a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201 809dcae3 Leszek Koltunski
  private static void createPrograms(Resources resources)
202 bfe45b4a Leszek Koltunski
    {
203
    // MAIN PROGRAM ////////////////////////////////////
204
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
205
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
206
207
    int numF = FragmentEffect.getNumEnabled();
208
    int numV = VertexEffect.getNumEnabled();
209
210 7602a827 Leszek Koltunski
    String mainVertHeader= DistortedLibrary.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
211
    String mainFragHeader= DistortedLibrary.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
212 bfe45b4a Leszek Koltunski
    String enabledEffectV= VertexEffect.getGLSL();
213
    String enabledEffectF= FragmentEffect.getGLSL();
214
215
    String[] feedback = { "v_Position", "v_endPosition" };
216
217
    try
218
      {
219
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
220 7602a827 Leszek Koltunski
                                          enabledEffectV, enabledEffectF, DistortedLibrary.GLSL, feedback);
221 bfe45b4a Leszek Koltunski
      }
222
    catch(Exception e)
223
      {
224
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
225
      throw new RuntimeException(e.getMessage());
226
      }
227
228
    int mainProgramH = mMainProgram.getProgramHandle();
229
    EffectQueue.getUniforms(mainProgramH,0);
230
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
231
232
    // BLIT PROGRAM ////////////////////////////////////
233
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
234
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
235
236
    try
237
      {
238 7602a827 Leszek Koltunski
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL);
239 bfe45b4a Leszek Koltunski
      }
240
    catch(Exception e)
241
      {
242
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
243
      throw new RuntimeException(e.getMessage());
244
      }
245
246
    int blitProgramH = mBlitProgram.getProgramHandle();
247
    mBlitTextureH  = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
248
    mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
249
250
    // BLIT DEPTH PROGRAM ////////////////////////////////////
251
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
252
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
253
254
    try
255
      {
256 7602a827 Leszek Koltunski
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL);
257 bfe45b4a Leszek Koltunski
      }
258
    catch(Exception e)
259
      {
260
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
261
      throw new RuntimeException(e.getMessage());
262
      }
263
264
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
265
    mBlitDepthTextureH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
266
    mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
267
    mBlitDepthDepthH        = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
268
    mBlitDepthTexCorrH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
269
270
    // NORMAL PROGRAM //////////////////////////////////////
271
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
272
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
273
274
    try
275
      {
276 7602a827 Leszek Koltunski
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL);
277 bfe45b4a Leszek Koltunski
      }
278
    catch(Exception e)
279
      {
280
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
281
      throw new RuntimeException(e.getMessage());
282
      }
283
284
    int normalProgramH = mNormalProgram.getProgramHandle();
285
    mNormalMVPMatrixH  = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
286
    }
287
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290 809dcae3 Leszek Koltunski
  private static void createProgramsOIT(Resources resources)
291 bfe45b4a Leszek Koltunski
    {
292
    // MAIN OIT PROGRAM ////////////////////////////////
293
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
294
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
295
296
    int numF = FragmentEffect.getNumEnabled();
297
    int numV = VertexEffect.getNumEnabled();
298
299 7602a827 Leszek Koltunski
    String mainVertHeader= DistortedLibrary.GLSL_VERSION +
300 bfe45b4a Leszek Koltunski
                           ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n") +
301
                           ("#define OIT\n");
302 7602a827 Leszek Koltunski
    String mainFragHeader= DistortedLibrary.GLSL_VERSION +
303 bfe45b4a Leszek Koltunski
                           ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n") +
304
                           ("#define OIT\n");
305
306
    String enabledEffectV= VertexEffect.getGLSL();
307
    String enabledEffectF= FragmentEffect.getGLSL();
308
309
    try
310
      {
311
      mMainOITProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
312 7602a827 Leszek Koltunski
                                             enabledEffectV, enabledEffectF, DistortedLibrary.GLSL, null);
313 bfe45b4a Leszek Koltunski
      }
314
    catch(Exception e)
315
      {
316
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN OIT program: "+e.getMessage());
317
      throw new RuntimeException(e.getMessage());
318
      }
319
320
    int mainOITProgramH = mMainOITProgram.getProgramHandle();
321
    EffectQueue.getUniforms(mainOITProgramH,1);
322
    mMainOITTextureH    = GLES31.glGetUniformLocation( mainOITProgramH, "u_Texture");
323
    mMainOITSizeH       = GLES31.glGetUniformLocation( mainOITProgramH, "u_Size");
324
    mMainOITNumRecordsH = GLES31.glGetUniformLocation( mainOITProgramH, "u_numRecords");
325
326
    // OIT CLEAR PROGRAM ////////////////////////////////////
327
    final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
328
    final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader);
329
330
    try
331
      {
332 7602a827 Leszek Koltunski
      mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL);
333 bfe45b4a Leszek Koltunski
      }
334
    catch(Exception e)
335
      {
336
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage());
337
      throw new RuntimeException(e.getMessage());
338
      }
339
340
    int oitClearProgramH   = mOITClearProgram.getProgramHandle();
341
    mOITClearDepthH        = GLES31.glGetUniformLocation( oitClearProgramH, "u_Depth");
342
    mOITClearTexCorrH      = GLES31.glGetUniformLocation( oitClearProgramH, "u_TexCorr");
343
    mOITClearSizeH         = GLES31.glGetUniformLocation( oitClearProgramH, "u_Size");
344
345
    // OIT BUILD PROGRAM ////////////////////////////////////
346
    final InputStream oitBuildVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
347
    final InputStream oitBuildFragStream = resources.openRawResource(R.raw.oit_build_fragment_shader);
348
349
    try
350
      {
351 7602a827 Leszek Koltunski
      mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL);
352 bfe45b4a Leszek Koltunski
      }
353
    catch(Exception e)
354
      {
355
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage());
356
      throw new RuntimeException(e.getMessage());
357
      }
358
359
    int oitBuildProgramH   = mOITBuildProgram.getProgramHandle();
360
    mOITBuildTextureH      = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Texture");
361
    mOITBuildDepthTextureH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture");
362
    mOITBuildDepthH        = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Depth");
363
    mOITBuildTexCorrH      = GLES31.glGetUniformLocation( oitBuildProgramH, "u_TexCorr");
364
    mOITBuildSizeH         = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Size");
365
    mOITBuildNumRecordsH   = GLES31.glGetUniformLocation( oitBuildProgramH, "u_numRecords");
366
367
    // OIT COLLAPSE PROGRAM ///////////////////////////
368
    final InputStream oitCollapseVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
369
    final InputStream oitCollapseFragStream = resources.openRawResource(R.raw.oit_collapse_fragment_shader);
370
371
    try
372
      {
373 7602a827 Leszek Koltunski
      mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL);
374 bfe45b4a Leszek Koltunski
      }
375
    catch(Exception e)
376
      {
377
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage());
378
      throw new RuntimeException(e.getMessage());
379
      }
380
381
    int oitCollapseProgramH   = mOITCollapseProgram.getProgramHandle();
382
    mOITCollapseDepthTextureH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture");
383
    mOITCollapseDepthH        = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_Depth");
384
    mOITCollapseTexCorrH      = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr");
385
    mOITCollapseSizeH         = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_Size");
386
387
    // OIT RENDER PROGRAM ///////////////////////////
388
    final InputStream oitRenderVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
389
    final InputStream oitRenderFragStream = resources.openRawResource(R.raw.oit_render_fragment_shader);
390
391
    try
392
      {
393 7602a827 Leszek Koltunski
      mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL_VERSION, DistortedLibrary.GLSL);
394 bfe45b4a Leszek Koltunski
      }
395
    catch(Exception e)
396
      {
397
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage());
398
      throw new RuntimeException(e.getMessage());
399
      }
400
401
    int oitRenderProgramH   = mOITRenderProgram.getProgramHandle();
402
    mOITRenderDepthH        = GLES31.glGetUniformLocation( oitRenderProgramH, "u_Depth");
403
    mOITRenderTexCorrH      = GLES31.glGetUniformLocation( oitRenderProgramH, "u_TexCorr");
404
    mOITRenderSizeH         = GLES31.glGetUniformLocation( oitRenderProgramH, "u_Size");
405
    }
406
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408
409
  private static void displayNormals(EffectQueue[] queues, MeshBase mesh)
410
    {
411
    int num = mesh.getNumVertices();
412
    int tfo = mesh.getTFO();
413
414
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
415
    GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
416 7602a827 Leszek Koltunski
    InternalRenderState.switchOffDrawing();
417 bfe45b4a Leszek Koltunski
    GLES31.glDrawArrays( GLES31.GL_POINTS, 0, num );
418 7602a827 Leszek Koltunski
    InternalRenderState.restoreDrawing();
419 bfe45b4a Leszek Koltunski
    GLES31.glEndTransformFeedback();
420
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
421
422 7602a827 Leszek Koltunski
    DistortedLibrary.mNormalProgram.useProgram();
423
    GLES31.glUniformMatrix4fv(DistortedLibrary.mNormalMVPMatrixH, 1, false, EffectQueue.getMVP(queues) , 0);
424
    mesh.bindTransformAttribs(DistortedLibrary.mNormalProgram);
425 bfe45b4a Leszek Koltunski
    GLES31.glLineWidth(8.0f);
426
    GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*num);
427
    }
428
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430
431 c90aca24 Leszek Koltunski
  static void drawPrivOIT(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
432 bfe45b4a Leszek Koltunski
    {
433 c90aca24 Leszek Koltunski
    EffectQueue[] queues = effects.getQueues();
434
435 644c21f2 Leszek Koltunski
    EffectQueue.compute(queues, currTime);
436 bfe45b4a Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
437
438 7602a827 Leszek Koltunski
    DistortedLibrary.mMainOITProgram.useProgram();
439
    GLES31.glUniform1i(DistortedLibrary.mMainOITTextureH, 0);
440
    GLES31.glUniform2ui(DistortedLibrary.mMainOITSizeH, surface.mWidth, surface.mHeight);
441
    GLES31.glUniform1ui(DistortedLibrary.mMainOITNumRecordsH, (int)(DistortedLibrary.mBufferSize*surface.mWidth*surface.mHeight) );
442
    mesh.bindVertexAttribs(DistortedLibrary.mMainOITProgram);
443 bfe45b4a Leszek Koltunski
444 644c21f2 Leszek Koltunski
    float halfX       = mesh.getStretchX() / 2.0f;
445
    float halfY       = mesh.getStretchY() / 2.0f;
446
    float halfZ       = mesh.getStretchZ() / 2.0f;
447 809dcae3 Leszek Koltunski
    float inflate     = mesh.getInflate();
448
    int width         = surface.mWidth;
449
    int height        = surface.mHeight;
450
    float distance    = surface.mDistance;
451
    float mipmap      = surface.mMipmap;
452
    float[] projection= surface.mProjectionMatrix;
453 bfe45b4a Leszek Koltunski
454 c90aca24 Leszek Koltunski
    EffectQueue.send(queues, width, height, distance, mipmap, projection, inflate, halfX, halfY, halfZ, 1 );
455 bfe45b4a Leszek Koltunski
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
456
457
    if( mesh.getShowNormals() )
458
      {
459 7602a827 Leszek Koltunski
      DistortedLibrary.mMainProgram.useProgram();
460 c90aca24 Leszek Koltunski
      EffectQueue.send(queues, width, height, distance, mipmap, projection, inflate, halfX, halfY, halfZ, 0 );
461 bfe45b4a Leszek Koltunski
      displayNormals(queues,mesh);
462
      }
463
    }
464
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466
467 c90aca24 Leszek Koltunski
  static void drawPriv(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
468 bfe45b4a Leszek Koltunski
    {
469 c90aca24 Leszek Koltunski
    EffectQueue[] queues = effects.getQueues();
470 bfe45b4a Leszek Koltunski
471 644c21f2 Leszek Koltunski
    EffectQueue.compute(queues, currTime);
472 bfe45b4a Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
473 46b572b5 Leszek Koltunski
474 7602a827 Leszek Koltunski
    DistortedLibrary.mMainProgram.useProgram();
475
    GLES31.glUniform1i(DistortedLibrary.mMainTextureH, 0);
476
    mesh.bindVertexAttribs(DistortedLibrary.mMainProgram);
477 809dcae3 Leszek Koltunski
478 644c21f2 Leszek Koltunski
    float halfX       = mesh.getStretchX() / 2.0f;
479
    float halfY       = mesh.getStretchY() / 2.0f;
480
    float halfZ       = mesh.getStretchZ() / 2.0f;
481 809dcae3 Leszek Koltunski
    float inflate     = mesh.getInflate();
482
    int width         = surface.mWidth;
483
    int height        = surface.mHeight;
484
    float distance    = surface.mDistance;
485
    float mipmap      = surface.mMipmap;
486
    float[] projection= surface.mProjectionMatrix;
487
488 c90aca24 Leszek Koltunski
    EffectQueue.send(queues, width, height, distance, mipmap, projection, inflate, halfX, halfY, halfZ, 0 );
489 bfe45b4a Leszek Koltunski
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
490
491
    if( mesh.getShowNormals() ) displayNormals(queues,mesh);
492
    }
493
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495
496 7602a827 Leszek Koltunski
  static void blitPriv(InternalOutputSurface surface)
497 bfe45b4a Leszek Koltunski
    {
498
    mBlitProgram.useProgram();
499
500
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
501
    GLES31.glUniform1i(mBlitTextureH, 0);
502
    GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
503
    GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
504
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
505
    }
506
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508
509 7602a827 Leszek Koltunski
  static void blitDepthPriv(InternalOutputSurface surface, float corrW, float corrH)
510 bfe45b4a Leszek Koltunski
    {
511
    mBlitDepthProgram.useProgram();
512
513
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
514
    GLES31.glUniform1i(mBlitDepthTextureH, 0);
515
    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
516
    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
517
    GLES31.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
518
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
519
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
520
    }
521
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523
524
  private static int printPreviousBuffer()
525
    {
526
    int counter = 0;
527
528
    ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
529
                                                                GLES31.GL_MAP_READ_BIT);
530
    if( atomicBuf!=null )
531
      {
532
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
533
      counter = atomicIntBuf.get(0);
534
      }
535
    else
536
      {
537
      android.util.Log.e("effects", "print: failed to map atomic buffer");
538
      }
539
540
    GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
541
542
    return counter;
543
    }
544
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546
547
  private static void zeroBuffer()
548
    {
549
    ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
550
        GLES31.GL_MAP_WRITE_BIT|GLES31.GL_MAP_INVALIDATE_BUFFER_BIT);
551
    if( atomicBuf!=null )
552
      {
553
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
554
      atomicIntBuf.put(0,0);
555
      }
556
    else
557
      {
558
      android.util.Log.e("effects", "zero: failed to map atomic buffer");
559
      }
560
561
    GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
562
    }
563
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565
// reset atomic counter to 0
566
567
  static int zeroOutAtomic()
568
    {
569
    int counter = 0;
570
571
    if( mAtomicCounter[0]<0 )
572
      {
573 7602a827 Leszek Koltunski
      GLES31.glGenBuffers(DistortedLibrary.FBO_QUEUE_SIZE,mAtomicCounter,0);
574 bfe45b4a Leszek Koltunski
575 7602a827 Leszek Koltunski
      for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++)
576 bfe45b4a Leszek Koltunski
        {
577
        GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]);
578
        GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES31.GL_DYNAMIC_DRAW);
579
        zeroBuffer();
580
        }
581
      }
582
583
    // reading the value of the buffer on every frame would slow down rendering by
584 f953bee0 Leszek Koltunski
    // dialog_about 3%; doing it only once every 5 frames affects speed by less than 1%.
585 bfe45b4a Leszek Koltunski
    if( mCurrBuffer==0 )
586
      {
587
      GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
588
      counter = printPreviousBuffer();
589
      }
590
591 7602a827 Leszek Koltunski
    if( ++mCurrBuffer>= DistortedLibrary.FBO_QUEUE_SIZE ) mCurrBuffer = 0;
592 bfe45b4a Leszek Koltunski
593
    GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
594
    zeroBuffer();
595
596
    return counter;
597
    }
598
599
///////////////////////////////////////////////////////////////////////////////////////////////////
600 809dcae3 Leszek Koltunski
// Pass1 of the OIT algorithm. Clear per-pixel head-pointers.
601 bfe45b4a Leszek Koltunski
602 7602a827 Leszek Koltunski
  static void oitClear(InternalOutputSurface surface, int counter)
603 bfe45b4a Leszek Koltunski
    {
604
    if( mLinkedListSSBO[0]<0 )
605
      {
606
      GLES31.glGenBuffers(1,mLinkedListSSBO,0);
607
608
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
609
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
610
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
611
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
612
613
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
614
      }
615
616
    // See if we have overflown the SSBO in one of the previous frames.
617
    // If yes, assume we need to make the SSBO larger.
618
    float overflow = counter/(mBufferSize*surface.mWidth*surface.mHeight);
619
620
    if( overflow>1.0f )
621
      {
622
      //android.util.Log.e("effects", "previous frame rendered "+counter+
623
      //                   " fragments, but there was only "+(mBufferSize*surface.mWidth*surface.mHeight)+" space");
624
625
      mBufferSize *= (int)(overflow+1.0f);
626
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
627
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
628
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
629
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
630
      }
631
632
    mOITClearProgram.useProgram();
633
634
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
635
    GLES31.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f );   // corrections do not really matter here - only present because of common vertex shader.
636
    GLES31.glUniform1f( mOITClearDepthH , 1.0f);          // likewise depth
637
    GLES31.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight);
638
    GLES31.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
639
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
640
    }
641
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643
// Pass2 of the OIT algorithm - build per-pixel linked lists.
644
645 7602a827 Leszek Koltunski
  static void oitBuild(InternalOutputSurface surface, float corrW, float corrH)
646 bfe45b4a Leszek Koltunski
    {
647
    mOITBuildProgram.useProgram();
648
649
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
650
    GLES31.glUniform1i(mOITBuildTextureH, 0);
651
    GLES31.glUniform1i(mOITBuildDepthTextureH, 1);
652
    GLES31.glUniform2f(mOITBuildTexCorrH, corrW, corrH );
653
    GLES31.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight);
654
    GLES31.glUniform1ui(mOITBuildNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
655
    GLES31.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear);
656
    GLES31.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
657
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
658
    }
659
660
///////////////////////////////////////////////////////////////////////////////////////////////////
661
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list.
662
663 7602a827 Leszek Koltunski
  static void oitCollapse(InternalOutputSurface surface, float corrW, float corrH)
664 bfe45b4a Leszek Koltunski
    {
665
    mOITCollapseProgram.useProgram();
666
667
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
668
    GLES31.glUniform1i(mOITCollapseDepthTextureH, 1);
669
    GLES31.glUniform2f(mOITCollapseTexCorrH, corrW, corrH );
670
    GLES31.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight);
671
    GLES31.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear);
672
    GLES31.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
673
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
674
    }
675
676
///////////////////////////////////////////////////////////////////////////////////////////////////
677
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists.
678
679 7602a827 Leszek Koltunski
  static void oitRender(InternalOutputSurface surface, float corrW, float corrH)
680 bfe45b4a Leszek Koltunski
    {
681
    mOITRenderProgram.useProgram();
682
683
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
684
    GLES31.glUniform2f(mOITRenderTexCorrH, corrW, corrH );
685
    GLES31.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight);
686
    GLES31.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear);
687
    GLES31.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
688
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
689
    }
690
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692
693
  static void setSSBOSize(float size)
694
    {
695
    mBufferSize = size;
696
    }
697
698 edea9cf3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
699
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
700
// PowerVR GE8100 compiler fails to compile OIT programs.
701
702 c43abe6c Leszek Koltunski
  private static void detectBuggyDrivers()
703 edea9cf3 Leszek Koltunski
    {
704
    String vendor  = GLES31.glGetString(GLES31.GL_VENDOR);
705
    String version = GLES31.glGetString(GLES31.GL_VERSION);
706
    String renderer= GLES31.glGetString(GLES31.GL_RENDERER);
707
708
    /*
709
    android.util.Log.e("DISTORTED", "GLSL Version "+GLES31.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
710
    android.util.Log.e("DISTORTED", "GL Version "  +GLES31.glGetString(GLES31.GL_VERSION));
711
    android.util.Log.e("DISTORTED", "GL Vendor "   +GLES31.glGetString(GLES31.GL_VENDOR));
712
    android.util.Log.e("DISTORTED", "GL Renderer " +GLES31.glGetString(GLES31.GL_RENDERER));
713
    */
714
715
    if( vendor.contains("ARM") )
716
      {
717
      if( version.contains("r12") )
718
        {
719 c1a38ba3 Leszek Koltunski
        android.util.Log.e("DISTORTED", "You are running this on a ARM Mali driver r12.\nThis is a buggy driver, please update to r22. Problems with flashing expected.");
720 edea9cf3 Leszek Koltunski
        }
721
      }
722
    else if( vendor.contains("Imagination") )
723
      {
724
      if( renderer.contains("GE8") )
725
        {
726 c1a38ba3 Leszek Koltunski
        android.util.Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
727 edea9cf3 Leszek Koltunski
        }
728
      }
729
    }
730
731 421c2728 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
732 310e14fb leszek
/**
733
 * Have we called onCreate yet, ie have we initialized the library?
734 1dfc9074 leszek
 * @return <code>true</code> if the library is initialized and ready for action.
735 310e14fb leszek
 */
736
  public static boolean isInitialized()
737 421c2728 Leszek Koltunski
    {
738 55c14a19 Leszek Koltunski
    return mInitialized;
739 421c2728 Leszek Koltunski
    }
740
741 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
742
/**
743 c43abe6c Leszek Koltunski
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
744 432442f9 Leszek Koltunski
 * I.e. best called from GLSurfaceView.onCreate().
745 6a06a912 Leszek Koltunski
 * <p>
746 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
747 6a06a912 Leszek Koltunski
 *   
748 015642fb Leszek Koltunski
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
749 6a06a912 Leszek Koltunski
 */
750 edea9cf3 Leszek Koltunski
  public static void onCreate(final Context context) throws Exception
751 d6e94c84 Leszek Koltunski
    {
752 30beb34f Leszek Koltunski
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
753
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
754
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
755 edea9cf3 Leszek Koltunski
756
    mInitialized = true;
757
758
    detectBuggyDrivers();
759
760 0e924ba5 Leszek Koltunski
    EffectMessageSender.startSending();
761
762 d6e94c84 Leszek Koltunski
    final Resources resources = context.getResources();
763 c90b9e01 Leszek Koltunski
764 edea9cf3 Leszek Koltunski
    Exception exception=null;
765
766
    try
767
      {
768 bfe45b4a Leszek Koltunski
      createPrograms(resources);
769 edea9cf3 Leszek Koltunski
      }
770
    catch(Exception ex)
771
      {
772
      exception = ex;
773
      }
774
775
    try
776
      {
777 bfe45b4a Leszek Koltunski
      createProgramsOIT(resources);
778 edea9cf3 Leszek Koltunski
      }
779
    catch(Exception ex)
780
      {
781
      exception = ex;
782
      }
783
784 a13dde77 Leszek Koltunski
    try
785
      {
786
      EffectQueuePostprocess.createPrograms(resources);
787
      }
788
    catch(Exception ex)
789
      {
790
      exception = ex;
791
      }
792
793 edea9cf3 Leszek Koltunski
    try
794
      {
795
      PostprocessEffect.createPrograms();
796
      }
797
    catch(Exception ex)
798
      {
799
      exception = ex;
800
      }
801
802
    if( exception!=null)
803
      {
804
      throw exception;
805
      }
806 6a06a912 Leszek Koltunski
    }
807
808 05ecc6fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
809
/**
810 78e89fb5 Leszek Koltunski
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
811 05ecc6fe Leszek Koltunski
 * Must be called from Activity.onPause().
812
 */
813
  public static void onPause()
814
    {
815 7602a827 Leszek Koltunski
    InternalObject.onPause();
816 bfe45b4a Leszek Koltunski
817
    mLinkedListSSBO[0]= -1;
818
819 7602a827 Leszek Koltunski
    for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++) mAtomicCounter[i] = -1;
820 05ecc6fe Leszek Koltunski
    }
821
822 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
823
/**
824
 * Call this so that the Library can release its internal data structures.
825
 * Must be called from Activity.onDestroy(). 
826
 */
827
  public static void onDestroy()
828
    {
829 7602a827 Leszek Koltunski
    InternalObject.onDestroy();
830
    InternalNodeData.onDestroy();
831
    InternalMaster.onDestroy();
832
    InternalOutputSurface.onDestroy();
833 46b572b5 Leszek Koltunski
    DistortedEffects.onDestroy();
834 42571056 Leszek Koltunski
    EffectQueue.onDestroy();
835 26a4e5f6 leszek
    Effect.onDestroy();
836 6a06a912 Leszek Koltunski
    EffectMessageSender.stopSending();
837 f8686932 Leszek Koltunski
838 55c14a19 Leszek Koltunski
    mInitialized = false;
839 6a06a912 Leszek Koltunski
    }
840 bfe45b4a Leszek Koltunski
841
///////////////////////////////////////////////////////////////////////////////////////////////////
842
/**
843
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
844
 * single (InputSurface,MeshBase) combo.
845
 *
846
 * @param type {@link EffectType}
847
 * @return The maximum number of effects of a given type.
848
 */
849
  @SuppressWarnings("unused")
850
  public static int getMax(EffectType type)
851
    {
852
    return EffectQueue.getMax(type.ordinal());
853
    }
854
855
///////////////////////////////////////////////////////////////////////////////////////////////////
856
/**
857
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
858
 * This can fail if:
859
 * <ul>
860
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
861
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
862 7602a827 Leszek Koltunski
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link DistortedLibrary#onCreate}. After this
863 bfe45b4a Leszek Koltunski
 *     time only decreasing the value of 'max' is permitted.
864
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
865
 * </ul>
866
 *
867
 * @param type {@link EffectType}
868
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
869
 *            than Byte.MAX_VALUE
870
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
871
 */
872
  @SuppressWarnings("unused")
873
  public static boolean setMax(EffectType type, int max)
874
    {
875
    return EffectQueue.setMax(type.ordinal(),max);
876
    }
877 f8f6d457 leszek
  }