Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedLibrary.java @ f482efa3

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