Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedLibrary.java @ 5f35f1cb

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