Project

General

Profile

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

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

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