Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedLibrary.java @ 36d65d88

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