Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedLibrary.java @ 82b62e04

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 2e569ff6 Leszek Koltunski
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3 d333eb6b Leszek Koltunski
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 d333eb6b Leszek Koltunski
//                                                                                               //
6 2e569ff6 Leszek Koltunski
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10 d333eb6b Leszek Koltunski
//                                                                                               //
11 2e569ff6 Leszek Koltunski
// This library 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 2e569ff6 Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 d333eb6b Leszek Koltunski
//                                                                                               //
16 2e569ff6 Leszek Koltunski
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21 fe82a979 Leszek Koltunski
package org.distorted.library.main;
22 6a06a912 Leszek Koltunski
23 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
24 edea9cf3 Leszek Koltunski
import android.opengl.GLES31;
25 8c57d77b Leszek Koltunski
26
import java.io.InputStream;
27
import java.nio.ByteBuffer;
28
import java.nio.ByteOrder;
29
import java.nio.FloatBuffer;
30
import java.nio.IntBuffer;
31
import java.util.regex.Matcher;
32
import java.util.regex.Pattern;
33 310e14fb leszek
34 bfe45b4a Leszek Koltunski
import org.distorted.library.R;
35 26a4e5f6 leszek
import org.distorted.library.effect.Effect;
36 809dcae3 Leszek Koltunski
import org.distorted.library.effectqueue.EffectQueue;
37
import org.distorted.library.effectqueue.EffectQueuePostprocess;
38 bfe45b4a Leszek Koltunski
import org.distorted.library.effect.EffectType;
39
import org.distorted.library.effect.FragmentEffect;
40 aa2f0486 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffect;
41 bfe45b4a Leszek Koltunski
import org.distorted.library.effect.VertexEffect;
42 07206c71 Leszek Koltunski
import org.distorted.library.effectqueue.EffectQueueVertex;
43
import org.distorted.library.mesh.DeferredJobs;
44 bfe45b4a Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
45 ed06301f Leszek Koltunski
import org.distorted.library.message.EffectMessageSender;
46 bfe45b4a Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
47 b7074bc6 Leszek Koltunski
import org.distorted.library.program.VertexCompilationException;
48 0273ef2a Leszek Koltunski
import org.distorted.library.type.Dynamic;
49 bfe45b4a Leszek Koltunski
50 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
51
/**
52 f953bee0 Leszek Koltunski
 * A singleton class used to control various global dialog_settings.
53 6a06a912 Leszek Koltunski
 */
54 7602a827 Leszek Koltunski
public class DistortedLibrary
55 39cbf9dc Leszek Koltunski
  {
56 6a06a912 Leszek Koltunski
  /**
57 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
58
   * backing up our DistortedTexture.
59 6a06a912 Leszek Koltunski
   * <p>
60 cacc63de Leszek Koltunski
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
61 6a06a912 Leszek Koltunski
   * apply different effects. Used in the copy constructor.
62
   */
63 29a06526 Leszek Koltunski
  public static final int CLONE_SURFACE = 0x1;
64 6a06a912 Leszek Koltunski
  /**
65 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
66 6a06a912 Leszek Koltunski
   * <p>
67 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
68 6a06a912 Leszek Koltunski
   */
69 015642fb Leszek Koltunski
  public static final int CLONE_MATRIX = 0x2;
70 6a06a912 Leszek Koltunski
  /**
71 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
72 6a06a912 Leszek Koltunski
   * <p>
73 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
74 6a06a912 Leszek Koltunski
   */
75
  public static final int CLONE_VERTEX  = 0x4;
76
  /**
77 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
78 6a06a912 Leszek Koltunski
   * <p>
79 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
80 6a06a912 Leszek Koltunski
   */
81
  public static final int CLONE_FRAGMENT= 0x8;
82 d6e94c84 Leszek Koltunski
   /**
83
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
84
   * <p>
85
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
86
   */
87
  public static final int CLONE_POSTPROCESS= 0x10;
88 6a06a912 Leszek Koltunski
  /**
89 a09ada4c Leszek Koltunski
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
90 6a06a912 Leszek Koltunski
   * <p>
91 cacc63de Leszek Koltunski
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
92
   * on the screen with (optionally) different Effects.
93 6a06a912 Leszek Koltunski
   */
94 d6e94c84 Leszek Koltunski
  public static final int CLONE_CHILDREN= 0x20;
95 b3618cb5 Leszek Koltunski
96 5f35f1cb Leszek Koltunski
  /**
97
   * When creating a DistortedScreen (which needs to have mFBOQueueSize FBOs attached), pass this
98
   * constant for 'numOfFBOs' and the number of backing FBOs will be taken from 'mFBOQueueSize'
99
   * (the value of which is most likely unknown at the time of creation of the Screen)
100
   */
101
  public static final int WAIT_FOR_FBO_QUEUE_SIZE = -1;
102 586b5fa1 Leszek Koltunski
  /**
103
   * Work around bugs in ARM Mali driver by, instead to a single FBO, rendering to a circular queue
104 5f35f1cb Leszek Koltunski
   * of mFBOQueueSize FBOs. (otherwise we sometimes get a 'full pipeline flush' and the end result
105 586b5fa1 Leszek Koltunski
   * might be missing part of the Objects)
106 6672d895 Leszek Koltunski
   *
107 5f35f1cb Leszek Koltunski
   * This bug only exists on Mali driver r12. (or more precisely it's there in r12 but fixed in r22)
108 6672d895 Leszek Koltunski
   *
109
   * https://community.arm.com/graphics/f/discussions/10285/opengl-es-3-1-on-mali-t880-flashes
110 586b5fa1 Leszek Koltunski
   */
111 5f35f1cb Leszek Koltunski
  private static int mFBOQueueSize;
112 9ec374e8 Leszek Koltunski
  private static int mGLSL;
113
  private static String mGLSL_VERSION;
114 46d463a4 Leszek Koltunski
  private static boolean mOITCompilationAttempted, mNeedsTransformFeedback;
115 78ff6ea9 Leszek Koltunski
116
  private static int mMaxTextureSize         = Integer.MAX_VALUE;
117
  private static int mMaxNumberOfVerUniforms = Integer.MAX_VALUE;
118
  private static int mMaxNumberOfFraUniforms = Integer.MAX_VALUE;
119 6a06a912 Leszek Koltunski
120 a9d4bb9a Leszek Koltunski
  private static boolean mBuggyUBOs;
121
  private static String mVendor, mVersion, mRenderer;
122 d2dc25ad Leszek Koltunski
  private static boolean mFastCompilationTF;
123 a9d4bb9a Leszek Koltunski
124 bfe45b4a Leszek Koltunski
  //////////////////////////////////////////////////////////////////////////////////////////////
125
  /// MAIN PROGRAM ///
126
  private static DistortedProgram mMainProgram;
127
  private static int mMainTextureH;
128 62c869ad Leszek Koltunski
  private static int mTransformFeedbackH;
129 bfe45b4a Leszek Koltunski
130
  /// NORMAL PROGRAM /////
131
  private static DistortedProgram mNormalProgram;
132 62c869ad Leszek Koltunski
  private static int mNormalProjectionH;
133 bfe45b4a Leszek Koltunski
134
  /// MAIN OIT PROGRAM ///
135
  private static DistortedProgram mMainOITProgram;
136
  private static int mMainOITTextureH;
137
  private static int mMainOITSizeH;
138
  private static int mMainOITNumRecordsH;
139
140
  /// BLIT PROGRAM ///
141
  private static DistortedProgram mBlitProgram;
142
  private static int mBlitTextureH;
143
  private static int mBlitDepthH;
144
  private static final FloatBuffer mQuadPositions;
145
146 f046b159 Leszek Koltunski
  /// FULL PROGRAM ///
147
  private static DistortedProgram mFullProgram;
148
149 bfe45b4a Leszek Koltunski
  static
150
    {
151
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
152
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
153
    mQuadPositions.put(positionData).position(0);
154
    }
155
156
  /// BLIT DEPTH PROGRAM ///
157
  private static DistortedProgram mBlitDepthProgram;
158
  private static int mBlitDepthTextureH;
159
  private static int mBlitDepthDepthTextureH;
160
  private static int mBlitDepthTexCorrH;
161
162 0bd9f644 Leszek Koltunski
  /// Program Handles ///
163
  private static int mMainProgramH, mFullProgramH, mMainOITProgramH;
164
165 bfe45b4a Leszek Koltunski
  /// OIT SSBO BUFFER ///
166
  private static int[] mLinkedListSSBO = new int[1];
167 5f35f1cb Leszek Koltunski
  private static int[] mAtomicCounter;
168 bfe45b4a Leszek Koltunski
  private static int   mCurrBuffer;
169
170
  static
171
    {
172
    mLinkedListSSBO[0]= -1;
173
    mCurrBuffer       =  0;
174
    }
175
176
  ///////////////////////////////////////////////////////////////
177
  // meaning: allocate 1.0 screenful of places for transparent
178
  // fragments in the SSBO backing up the OIT render method.
179
  private static float mBufferSize=1.0f;
180
181
  /// OIT CLEAR PROGRAM ///
182
  private static DistortedProgram mOITClearProgram;
183
  private static int mOITClearDepthH;
184
  private static int mOITClearTexCorrH;
185
  private static int mOITClearSizeH;
186
187
  /// OIT BUILD PROGRAM ///
188
  private static DistortedProgram mOITBuildProgram;
189
  private static int mOITBuildTextureH;
190
  private static int mOITBuildDepthTextureH;
191
  private static int mOITBuildDepthH;
192
  private static int mOITBuildTexCorrH;
193
  private static int mOITBuildSizeH;
194
  private static int mOITBuildNumRecordsH;
195
196
  /// OIT COLLAPSE PROGRAM ///
197
  private static DistortedProgram mOITCollapseProgram;
198
  private static int mOITCollapseDepthTextureH;
199
  private static int mOITCollapseDepthH;
200
  private static int mOITCollapseTexCorrH;
201
  private static int mOITCollapseSizeH;
202
203
  /// OIT RENDER PROGRAM ///
204
  private static DistortedProgram mOITRenderProgram;
205
  private static int mOITRenderDepthH;
206
  private static int mOITRenderTexCorrH;
207
  private static int mOITRenderSizeH;
208
209
  /// END PROGRAMS //////
210
211 d99fcc9c Leszek Koltunski
  /**
212
   * Every application using the library must implement this interface so that the library can send
213
   * it exceptions that arise. The exceptions may come at any time, for example the library will
214
   * compile its OIT problem only on the first attempt to use the OIT
215
   * Those will mainly be hardware-related: shaders do not compile on particular hardware, the required
216
   * OpenGL ES 3.0 is not supported, etc.
217 8c57d77b Leszek Koltunski
   *
218
   * Additionally, the User must be able to provide version of the OpenGL supported by the underlying
219
   * hardware and InputStreams to given local files.
220 d99fcc9c Leszek Koltunski
   */
221 8c57d77b Leszek Koltunski
  public interface LibraryUser
222 d99fcc9c Leszek Koltunski
    {
223
    void distortedException(Exception ex);
224 8c57d77b Leszek Koltunski
    InputStream localFile(int fileID);
225
    void logMessage(String message);
226 d99fcc9c Leszek Koltunski
    }
227
228 8c57d77b Leszek Koltunski
  private static LibraryUser mUser;
229 d99fcc9c Leszek Koltunski
230 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
231 c638c1b0 Leszek Koltunski
// private: hide this from Javadoc
232 6a06a912 Leszek Koltunski
233 7602a827 Leszek Koltunski
  private DistortedLibrary()
234 6a06a912 Leszek Koltunski
    {
235 8eccf334 Leszek Koltunski
236 6a06a912 Leszek Koltunski
    }
237
238 bfe45b4a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240 d99fcc9c Leszek Koltunski
  private static void createMainProgram()
241 bfe45b4a Leszek Koltunski
    {
242
    // MAIN PROGRAM ////////////////////////////////////
243 8c57d77b Leszek Koltunski
    final InputStream mainVertStream = mUser.localFile(R.raw.main_vertex_shader);
244
    final InputStream mainFragStream = mUser.localFile(R.raw.main_fragment_shader);
245 bfe45b4a Leszek Koltunski
246
    int numF = FragmentEffect.getNumEnabled();
247
    int numV = VertexEffect.getNumEnabled();
248
249 b7074bc6 Leszek Koltunski
    String mainVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
250
    String mainFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
251 36d65d88 Leszek Koltunski
252 e8925fcd Leszek Koltunski
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
253 46d463a4 Leszek Koltunski
    if( MeshBase.getUseCenters() ) mainVertHeader += "#define COMP_CENTERS\n";
254 eff6e3d3 Leszek Koltunski
    if( mBuggyUBOs )               mainVertHeader += "#define BUGGY_UBOS\n";
255 36d65d88 Leszek Koltunski
256 bfe45b4a Leszek Koltunski
    String enabledEffectV= VertexEffect.getGLSL();
257
    String enabledEffectF= FragmentEffect.getGLSL();
258
259
    String[] feedback = { "v_Position", "v_endPosition" };
260
261
    try
262
      {
263 97b6c85e Leszek Koltunski
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader,
264
                                          mainFragHeader, enabledEffectV, enabledEffectF,
265
                                          mGLSL, mNeedsTransformFeedback ? feedback : null );
266 bfe45b4a Leszek Koltunski
      }
267
    catch(Exception e)
268
      {
269 8c57d77b Leszek Koltunski
      mUser.logMessage(e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
270 bfe45b4a Leszek Koltunski
      throw new RuntimeException(e.getMessage());
271
      }
272
273 0bd9f644 Leszek Koltunski
    mMainProgramH = mMainProgram.getProgramHandle();
274
    EffectQueue.getUniforms(mMainProgramH,0);
275 9f9924f8 Leszek Koltunski
    MeshBase.getUniforms(mMainProgramH,0);
276 0bd9f644 Leszek Koltunski
    mMainTextureH= GLES30.glGetUniformLocation( mMainProgramH, "u_Texture");
277
    mTransformFeedbackH= GLES30.glGetUniformLocation( mMainProgramH, "u_TransformFeedback");
278 bfe45b4a Leszek Koltunski
279
    // BLIT PROGRAM ////////////////////////////////////
280 8c57d77b Leszek Koltunski
    final InputStream blitVertStream = mUser.localFile(R.raw.blit_vertex_shader);
281
    final InputStream blitFragStream = mUser.localFile(R.raw.blit_fragment_shader);
282 bfe45b4a Leszek Koltunski
283
    try
284
      {
285 b7074bc6 Leszek Koltunski
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
286 bfe45b4a Leszek Koltunski
      }
287
    catch(Exception e)
288
      {
289 8c57d77b Leszek Koltunski
      mUser.logMessage(e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
290 bfe45b4a Leszek Koltunski
      throw new RuntimeException(e.getMessage());
291
      }
292
293
    int blitProgramH = mBlitProgram.getProgramHandle();
294 b7074bc6 Leszek Koltunski
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
295
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
296 bfe45b4a Leszek Koltunski
297
    // BLIT DEPTH PROGRAM ////////////////////////////////////
298 8c57d77b Leszek Koltunski
    final InputStream blitDepthVertStream = mUser.localFile(R.raw.blit_depth_vertex_shader);
299
    final InputStream blitDepthFragStream = mUser.localFile(R.raw.blit_depth_fragment_shader);
300 bfe45b4a Leszek Koltunski
301
    try
302
      {
303 b7074bc6 Leszek Koltunski
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
304 bfe45b4a Leszek Koltunski
      }
305
    catch(Exception e)
306
      {
307 8c57d77b Leszek Koltunski
      mUser.logMessage(e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
308 bfe45b4a Leszek Koltunski
      throw new RuntimeException(e.getMessage());
309
      }
310
311
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
312 b7074bc6 Leszek Koltunski
    mBlitDepthTextureH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Texture");
313
    mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
314
    mBlitDepthTexCorrH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
315 d99fcc9c Leszek Koltunski
    }
316
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318 bfe45b4a Leszek Koltunski
319 8c57d77b Leszek Koltunski
  private static void createNormalProgram()
320 d99fcc9c Leszek Koltunski
    {
321 bfe45b4a Leszek Koltunski
    // NORMAL PROGRAM //////////////////////////////////////
322 8c57d77b Leszek Koltunski
    final InputStream normalVertexStream   = mUser.localFile(R.raw.normal_vertex_shader);
323
    final InputStream normalFragmentStream = mUser.localFile(R.raw.normal_fragment_shader);
324 bfe45b4a Leszek Koltunski
325
    try
326
      {
327 b7074bc6 Leszek Koltunski
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
328 bfe45b4a Leszek Koltunski
      }
329
    catch(Exception e)
330
      {
331 8c57d77b Leszek Koltunski
      mUser.logMessage(e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
332 bfe45b4a Leszek Koltunski
      throw new RuntimeException(e.getMessage());
333
      }
334
335
    int normalProgramH = mNormalProgram.getProgramHandle();
336 62c869ad Leszek Koltunski
    mNormalProjectionH = GLES30.glGetUniformLocation( normalProgramH, "u_Projection");
337 bfe45b4a Leszek Koltunski
    }
338
339 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
340
341 8c57d77b Leszek Koltunski
  private static void createFullProgram()
342 f046b159 Leszek Koltunski
    {
343 8c57d77b Leszek Koltunski
    final InputStream fullVertStream = mUser.localFile(R.raw.main_vertex_shader);
344
    final InputStream fullFragStream = mUser.localFile(R.raw.main_fragment_shader);
345 f046b159 Leszek Koltunski
346
    int numV = VertexEffect.getAllEnabled();
347
348
    String fullVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n");
349
    String fullFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " +                                         0   + "\n");
350 36d65d88 Leszek Koltunski
351 e8925fcd Leszek Koltunski
    fullVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
352 46d463a4 Leszek Koltunski
    if( MeshBase.getUseCenters() ) fullVertHeader += "#define COMP_CENTERS\n";
353 eff6e3d3 Leszek Koltunski
    if( mBuggyUBOs )               fullVertHeader += "#define BUGGY_UBOS\n";
354 36d65d88 Leszek Koltunski
355 f046b159 Leszek Koltunski
    String enabledEffectV= VertexEffect.getAllGLSL();
356
    String enabledEffectF= "{}";
357
358 667884b0 Leszek Koltunski
    fullVertHeader += "#define PREAPPLY\n";
359
360 a2878a67 Leszek Koltunski
    String[] feedback = { "v_Position", "v_endPosition" };
361 f046b159 Leszek Koltunski
362
    try
363
      {
364
      mFullProgram = new DistortedProgram(fullVertStream, fullFragStream, fullVertHeader, fullFragHeader,
365
                                          enabledEffectV, enabledEffectF, mGLSL, feedback);
366
      }
367
    catch(Exception e)
368
      {
369 8c57d77b Leszek Koltunski
      mUser.logMessage(e.getClass().getSimpleName()+" trying to compile FULL program: "+e.getMessage());
370 f046b159 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
371
      }
372
373 0bd9f644 Leszek Koltunski
    mFullProgramH = mFullProgram.getProgramHandle();
374
    EffectQueue.getUniforms(mFullProgramH,3);
375 9f9924f8 Leszek Koltunski
    MeshBase.getUniforms(mFullProgramH,3);
376 f046b159 Leszek Koltunski
    }
377
378 bfe45b4a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
379
380 8c57d77b Leszek Koltunski
  private static void createOITProgram()
381 bfe45b4a Leszek Koltunski
    {
382
    // MAIN OIT PROGRAM ////////////////////////////////
383 8c57d77b Leszek Koltunski
    final InputStream mainVertStream = mUser.localFile(R.raw.main_vertex_shader);
384
    final InputStream mainFragStream = mUser.localFile(R.raw.main_fragment_shader);
385 bfe45b4a Leszek Koltunski
386
    int numF = FragmentEffect.getNumEnabled();
387
    int numV = VertexEffect.getNumEnabled();
388
389 b7074bc6 Leszek Koltunski
    String mainVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n") + ("#define OIT\n");
390
    String mainFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n") + ("#define OIT\n");
391 bfe45b4a Leszek Koltunski
392 e8925fcd Leszek Koltunski
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
393 46d463a4 Leszek Koltunski
    if( MeshBase.getUseCenters() ) mainVertHeader += "#define COMP_CENTERS\n";
394 eff6e3d3 Leszek Koltunski
    if( mBuggyUBOs )               mainVertHeader += "#define BUGGY_UBOS\n";
395 36d65d88 Leszek Koltunski
396 bfe45b4a Leszek Koltunski
    String enabledEffectV= VertexEffect.getGLSL();
397
    String enabledEffectF= FragmentEffect.getGLSL();
398
399
    try
400
      {
401
      mMainOITProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
402 b7074bc6 Leszek Koltunski
                                             enabledEffectV, enabledEffectF, mGLSL, null);
403 bfe45b4a Leszek Koltunski
      }
404
    catch(Exception e)
405
      {
406 8c57d77b Leszek Koltunski
      mUser.logMessage(e.getClass().getSimpleName()+" trying to compile MAIN OIT program: "+e.getMessage());
407 bfe45b4a Leszek Koltunski
      throw new RuntimeException(e.getMessage());
408
      }
409
410 0bd9f644 Leszek Koltunski
    mMainOITProgramH = mMainOITProgram.getProgramHandle();
411
    EffectQueue.getUniforms(mMainOITProgramH,1);
412 9f9924f8 Leszek Koltunski
    MeshBase.getUniforms(mMainOITProgramH,1);
413 0bd9f644 Leszek Koltunski
    mMainOITTextureH    = GLES30.glGetUniformLocation( mMainOITProgramH, "u_Texture");
414
    mMainOITSizeH       = GLES30.glGetUniformLocation( mMainOITProgramH, "u_Size");
415
    mMainOITNumRecordsH = GLES30.glGetUniformLocation( mMainOITProgramH, "u_numRecords");
416 bfe45b4a Leszek Koltunski
417
    // OIT CLEAR PROGRAM ////////////////////////////////////
418 8c57d77b Leszek Koltunski
    final InputStream oitClearVertStream = mUser.localFile(R.raw.oit_vertex_shader);
419
    final InputStream oitClearFragStream = mUser.localFile(R.raw.oit_clear_fragment_shader);
420 bfe45b4a Leszek Koltunski
421
    try
422
      {
423 b7074bc6 Leszek Koltunski
      mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
424 bfe45b4a Leszek Koltunski
      }
425
    catch(Exception e)
426
      {
427 8c57d77b Leszek Koltunski
      mUser.logMessage(e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage());
428 bfe45b4a Leszek Koltunski
      throw new RuntimeException(e.getMessage());
429
      }
430
431
    int oitClearProgramH   = mOITClearProgram.getProgramHandle();
432 b7074bc6 Leszek Koltunski
    mOITClearDepthH        = GLES30.glGetUniformLocation( oitClearProgramH, "u_Depth");
433
    mOITClearTexCorrH      = GLES30.glGetUniformLocation( oitClearProgramH, "u_TexCorr");
434
    mOITClearSizeH         = GLES30.glGetUniformLocation( oitClearProgramH, "u_Size");
435 bfe45b4a Leszek Koltunski
436
    // OIT BUILD PROGRAM ////////////////////////////////////
437 8c57d77b Leszek Koltunski
    final InputStream oitBuildVertStream = mUser.localFile(R.raw.oit_vertex_shader);
438
    final InputStream oitBuildFragStream = mUser.localFile(R.raw.oit_build_fragment_shader);
439 bfe45b4a Leszek Koltunski
440
    try
441
      {
442 b7074bc6 Leszek Koltunski
      mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
443 bfe45b4a Leszek Koltunski
      }
444
    catch(Exception e)
445
      {
446 8c57d77b Leszek Koltunski
      mUser.logMessage(e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage());
447 bfe45b4a Leszek Koltunski
      throw new RuntimeException(e.getMessage());
448
      }
449
450
    int oitBuildProgramH   = mOITBuildProgram.getProgramHandle();
451 b7074bc6 Leszek Koltunski
    mOITBuildTextureH      = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Texture");
452
    mOITBuildDepthTextureH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture");
453
    mOITBuildDepthH        = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Depth");
454
    mOITBuildTexCorrH      = GLES30.glGetUniformLocation( oitBuildProgramH, "u_TexCorr");
455
    mOITBuildSizeH         = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Size");
456
    mOITBuildNumRecordsH   = GLES30.glGetUniformLocation( oitBuildProgramH, "u_numRecords");
457 bfe45b4a Leszek Koltunski
458
    // OIT COLLAPSE PROGRAM ///////////////////////////
459 8c57d77b Leszek Koltunski
    final InputStream oitCollapseVertStream = mUser.localFile(R.raw.oit_vertex_shader);
460
    final InputStream oitCollapseFragStream = mUser.localFile(R.raw.oit_collapse_fragment_shader);
461 bfe45b4a Leszek Koltunski
462
    try
463
      {
464 b7074bc6 Leszek Koltunski
      mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
465 bfe45b4a Leszek Koltunski
      }
466
    catch(Exception e)
467
      {
468 8c57d77b Leszek Koltunski
      mUser.logMessage(e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage());
469 bfe45b4a Leszek Koltunski
      throw new RuntimeException(e.getMessage());
470
      }
471
472
    int oitCollapseProgramH   = mOITCollapseProgram.getProgramHandle();
473 b7074bc6 Leszek Koltunski
    mOITCollapseDepthTextureH = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture");
474
    mOITCollapseDepthH        = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Depth");
475
    mOITCollapseTexCorrH      = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr");
476
    mOITCollapseSizeH         = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Size");
477 bfe45b4a Leszek Koltunski
478
    // OIT RENDER PROGRAM ///////////////////////////
479 8c57d77b Leszek Koltunski
    final InputStream oitRenderVertStream = mUser.localFile(R.raw.oit_vertex_shader);
480
    final InputStream oitRenderFragStream = mUser.localFile(R.raw.oit_render_fragment_shader);
481 bfe45b4a Leszek Koltunski
482
    try
483
      {
484 b7074bc6 Leszek Koltunski
      mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
485 bfe45b4a Leszek Koltunski
      }
486
    catch(Exception e)
487
      {
488 8c57d77b Leszek Koltunski
      mUser.logMessage(e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage());
489 bfe45b4a Leszek Koltunski
      throw new RuntimeException(e.getMessage());
490
      }
491
492
    int oitRenderProgramH   = mOITRenderProgram.getProgramHandle();
493 b7074bc6 Leszek Koltunski
    mOITRenderDepthH        = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Depth");
494
    mOITRenderTexCorrH      = GLES30.glGetUniformLocation( oitRenderProgramH, "u_TexCorr");
495
    mOITRenderSizeH         = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Size");
496 bfe45b4a Leszek Koltunski
    }
497
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499
500 62c869ad Leszek Koltunski
  private static void displayNormals(float[] projection, MeshBase mesh)
501 bfe45b4a Leszek Koltunski
    {
502 d99fcc9c Leszek Koltunski
    if( mNormalProgram==null )
503
      {
504
      try
505
        {
506 8c57d77b Leszek Koltunski
        createNormalProgram();
507 d99fcc9c Leszek Koltunski
        }
508
      catch(Exception ex)
509
        {
510 8c57d77b Leszek Koltunski
        mUser.distortedException(ex);
511 d99fcc9c Leszek Koltunski
        return;
512
        }
513
      }
514
515 bfe45b4a Leszek Koltunski
    int num = mesh.getNumVertices();
516
    int tfo = mesh.getTFO();
517
518 62c869ad Leszek Koltunski
    GLES30.glUniform1i(DistortedLibrary.mTransformFeedbackH, 1);
519 b7074bc6 Leszek Koltunski
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
520
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
521 7602a827 Leszek Koltunski
    InternalRenderState.switchOffDrawing();
522 b7074bc6 Leszek Koltunski
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
523 7602a827 Leszek Koltunski
    InternalRenderState.restoreDrawing();
524 b7074bc6 Leszek Koltunski
    GLES30.glEndTransformFeedback();
525
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
526 c02f32f7 Leszek Koltunski
    GLES30.glUniform1i(DistortedLibrary.mTransformFeedbackH, 0);
527 bfe45b4a Leszek Koltunski
528 f046b159 Leszek Koltunski
    mNormalProgram.useProgram();
529 62c869ad Leszek Koltunski
    GLES30.glUniformMatrix4fv(mNormalProjectionH, 1, false, projection, 0);
530 f046b159 Leszek Koltunski
    mesh.bindTransformAttribs(mNormalProgram);
531 b7074bc6 Leszek Koltunski
    GLES30.glLineWidth(8.0f);
532
    GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*num);
533 b11171e8 Leszek Koltunski
    mNormalProgram.stopUsingProgram();
534 bfe45b4a Leszek Koltunski
    }
535
536 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
537 0bd9f644 Leszek Koltunski
/**
538
 * Execute all VertexEffects and adjust all vertices
539
 *
540
 * @y.exclude
541
 */
542 07206c71 Leszek Koltunski
  public static void adjustVertices(MeshBase mesh, EffectQueueVertex queue)
543 f046b159 Leszek Koltunski
    {
544 d99fcc9c Leszek Koltunski
    if( mFullProgram==null )
545 f046b159 Leszek Koltunski
      {
546 d99fcc9c Leszek Koltunski
      try
547
        {
548 8c57d77b Leszek Koltunski
        createFullProgram();
549 d99fcc9c Leszek Koltunski
        }
550
      catch(Exception ex)
551
        {
552 8c57d77b Leszek Koltunski
        mUser.distortedException(ex);
553 d99fcc9c Leszek Koltunski
        return;
554
        }
555 f046b159 Leszek Koltunski
      }
556 d99fcc9c Leszek Koltunski
557
    int num = mesh.getNumVertices();
558
    int tfo = mesh.getTFO();
559
560
    mFullProgram.useProgram();
561
    mesh.bindVertexAttribs(mFullProgram);
562 835b197e Leszek Koltunski
    queue.compute(1,0);
563 78ff6ea9 Leszek Koltunski
    queue.send(0.0f,mFullProgramH,3);
564 9f9924f8 Leszek Koltunski
    mesh.send(mFullProgramH,3);
565 d99fcc9c Leszek Koltunski
566
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
567
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
568
    InternalRenderState.switchOffDrawing();
569
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
570
    InternalRenderState.restoreDrawing();
571
    GLES30.glEndTransformFeedback();
572
    mesh.copyTransformToVertex();
573
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
574 b11171e8 Leszek Koltunski
    mFullProgram.stopUsingProgram();
575 f046b159 Leszek Koltunski
    }
576
577 bfe45b4a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
578
579 835b197e Leszek Koltunski
  static void drawPrivOIT(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime, long step)
580 bfe45b4a Leszek Koltunski
    {
581 06ddd60d Leszek Koltunski
    if( mMainOITProgram!=null )
582
      {
583
      EffectQueue[] queues = effects.getQueues();
584 c90aca24 Leszek Koltunski
585 835b197e Leszek Koltunski
      EffectQueue.compute(queues, currTime, step);
586 06ddd60d Leszek Koltunski
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
587 bfe45b4a Leszek Koltunski
588 06ddd60d Leszek Koltunski
      mMainOITProgram.useProgram();
589
      GLES30.glUniform1i(mMainOITTextureH, 0);
590
      GLES30.glUniform2ui(mMainOITSizeH, surface.mWidth, surface.mHeight);
591
      GLES30.glUniform1ui(mMainOITNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
592
      mesh.bindVertexAttribs(mMainOITProgram);
593 9f9924f8 Leszek Koltunski
      mesh.send(mMainOITProgramH,1);
594 bfe45b4a Leszek Koltunski
595 06ddd60d Leszek Koltunski
      float inflate     = mesh.getInflate();
596
      float distance    = surface.mDistance;
597
      float mipmap      = surface.mMipmap;
598
      float[] projection= surface.mProjectionMatrix;
599 bfe45b4a Leszek Koltunski
600 78ff6ea9 Leszek Koltunski
      EffectQueue.send(queues, mMainOITProgramH, distance, mipmap, projection, inflate, 1 );
601 06ddd60d Leszek Koltunski
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
602 b11171e8 Leszek Koltunski
      if( mesh.getShowNormals() ) displayNormals(projection,mesh);
603 82b62e04 Leszek Koltunski
      mMainOITProgram.stopUsingProgram();
604 bfe45b4a Leszek Koltunski
      }
605
    }
606
607
///////////////////////////////////////////////////////////////////////////////////////////////////
608
609 835b197e Leszek Koltunski
  static void drawPriv(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime, long step)
610 bfe45b4a Leszek Koltunski
    {
611 f046b159 Leszek Koltunski
    if( mMainProgram!=null )
612 f482efa3 Leszek Koltunski
      {
613
      EffectQueue[] queues = effects.getQueues();
614 bfe45b4a Leszek Koltunski
615 835b197e Leszek Koltunski
      EffectQueue.compute(queues, currTime, step);
616 f482efa3 Leszek Koltunski
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
617 46b572b5 Leszek Koltunski
618 f482efa3 Leszek Koltunski
      mMainProgram.useProgram();
619
      GLES30.glUniform1i(DistortedLibrary.mMainTextureH, 0);
620
      mesh.bindVertexAttribs(DistortedLibrary.mMainProgram);
621 9f9924f8 Leszek Koltunski
      mesh.send(mMainProgramH,0);
622 809dcae3 Leszek Koltunski
623 f482efa3 Leszek Koltunski
      float inflate     = mesh.getInflate();
624
      float distance    = surface.mDistance;
625
      float mipmap      = surface.mMipmap;
626
      float[] projection= surface.mProjectionMatrix;
627 809dcae3 Leszek Koltunski
628 78ff6ea9 Leszek Koltunski
      EffectQueue.send(queues, mMainProgramH, distance, mipmap, projection, inflate, 0 );
629 f482efa3 Leszek Koltunski
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
630 62c869ad Leszek Koltunski
      if( mesh.getShowNormals() ) displayNormals(projection,mesh);
631 3c535636 Leszek Koltunski
      mMainProgram.stopUsingProgram();
632 f482efa3 Leszek Koltunski
      }
633 bfe45b4a Leszek Koltunski
    }
634
635
///////////////////////////////////////////////////////////////////////////////////////////////////
636
637 7602a827 Leszek Koltunski
  static void blitPriv(InternalOutputSurface surface)
638 bfe45b4a Leszek Koltunski
    {
639 f482efa3 Leszek Koltunski
    if( mBlitProgram!=null )
640
      {
641
      mBlitProgram.useProgram();
642
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
643
      GLES30.glUniform1i(mBlitTextureH, 0);
644
      GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
645
      GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
646
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
647 b11171e8 Leszek Koltunski
      mBlitProgram.stopUsingProgram();
648 f482efa3 Leszek Koltunski
      }
649 bfe45b4a Leszek Koltunski
    }
650
651
///////////////////////////////////////////////////////////////////////////////////////////////////
652
653 7602a827 Leszek Koltunski
  static void blitDepthPriv(InternalOutputSurface surface, float corrW, float corrH)
654 bfe45b4a Leszek Koltunski
    {
655 f482efa3 Leszek Koltunski
    if( mBlitDepthProgram!=null )
656
      {
657
      mBlitDepthProgram.useProgram();
658
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
659
      GLES30.glUniform1i(mBlitDepthTextureH, 0);
660
      GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
661
      GLES30.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
662
      GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
663
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
664 b11171e8 Leszek Koltunski
      mBlitDepthProgram.stopUsingProgram();
665 f482efa3 Leszek Koltunski
      }
666 bfe45b4a Leszek Koltunski
    }
667
668
///////////////////////////////////////////////////////////////////////////////////////////////////
669 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
670 bfe45b4a Leszek Koltunski
671
  private static int printPreviousBuffer()
672
    {
673
    int counter = 0;
674
675 b7074bc6 Leszek Koltunski
    ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
676
                                                                GLES30.GL_MAP_READ_BIT);
677 bfe45b4a Leszek Koltunski
    if( atomicBuf!=null )
678
      {
679
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
680
      counter = atomicIntBuf.get(0);
681
      }
682
    else
683
      {
684 8c57d77b Leszek Koltunski
      mUser.logMessage("printPreviousBuffer: failed to map atomic buffer");
685 bfe45b4a Leszek Koltunski
      }
686
687 b7074bc6 Leszek Koltunski
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
688 bfe45b4a Leszek Koltunski
689
    return counter;
690
    }
691
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693
694
  private static void zeroBuffer()
695
    {
696 b7074bc6 Leszek Koltunski
    ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
697 fd836a41 Leszek Koltunski
                                                                GLES30.GL_MAP_WRITE_BIT|GLES30.GL_MAP_INVALIDATE_BUFFER_BIT);
698 bfe45b4a Leszek Koltunski
    if( atomicBuf!=null )
699
      {
700
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
701
      atomicIntBuf.put(0,0);
702
      }
703
    else
704
      {
705 8c57d77b Leszek Koltunski
      mUser.logMessage("zeroBuffer: failed to map atomic buffer");
706 bfe45b4a Leszek Koltunski
      }
707
708 b7074bc6 Leszek Koltunski
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
709 bfe45b4a Leszek Koltunski
    }
710
711
///////////////////////////////////////////////////////////////////////////////////////////////////
712
// reset atomic counter to 0
713
714
  static int zeroOutAtomic()
715
    {
716
    int counter = 0;
717
718 5f35f1cb Leszek Koltunski
    if( mAtomicCounter==null )
719 bfe45b4a Leszek Koltunski
      {
720 5f35f1cb Leszek Koltunski
      mAtomicCounter = new int[mFBOQueueSize];
721
722
      GLES30.glGenBuffers(mFBOQueueSize,mAtomicCounter,0);
723 bfe45b4a Leszek Koltunski
724 5f35f1cb Leszek Koltunski
      for(int i=0; i<mFBOQueueSize; i++)
725 bfe45b4a Leszek Koltunski
        {
726 b7074bc6 Leszek Koltunski
        GLES30.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]);
727
        GLES30.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES30.GL_DYNAMIC_DRAW);
728 bfe45b4a Leszek Koltunski
        zeroBuffer();
729
        }
730
      }
731
732
    // reading the value of the buffer on every frame would slow down rendering by
733 d99fcc9c Leszek Koltunski
    // about 3%; doing it only once every 5 frames affects speed by less than 1%.
734 bfe45b4a Leszek Koltunski
    if( mCurrBuffer==0 )
735
      {
736 b7074bc6 Leszek Koltunski
      GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
737 bfe45b4a Leszek Koltunski
      counter = printPreviousBuffer();
738
      }
739
740 5f35f1cb Leszek Koltunski
    if( ++mCurrBuffer>=mFBOQueueSize ) mCurrBuffer = 0;
741 bfe45b4a Leszek Koltunski
742 b7074bc6 Leszek Koltunski
    GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
743 bfe45b4a Leszek Koltunski
    zeroBuffer();
744
745
    return counter;
746
    }
747
748
///////////////////////////////////////////////////////////////////////////////////////////////////
749 809dcae3 Leszek Koltunski
// Pass1 of the OIT algorithm. Clear per-pixel head-pointers.
750 bfe45b4a Leszek Koltunski
751 7602a827 Leszek Koltunski
  static void oitClear(InternalOutputSurface surface, int counter)
752 bfe45b4a Leszek Koltunski
    {
753 d99fcc9c Leszek Koltunski
    if( mOITClearProgram==null )
754 bfe45b4a Leszek Koltunski
      {
755 d99fcc9c Leszek Koltunski
      if( mGLSL>=310 && !mOITCompilationAttempted )
756 f482efa3 Leszek Koltunski
        {
757 d99fcc9c Leszek Koltunski
        mOITCompilationAttempted = true;
758 bfe45b4a Leszek Koltunski
759 d99fcc9c Leszek Koltunski
        try
760
          {
761 8c57d77b Leszek Koltunski
          createOITProgram();
762 d99fcc9c Leszek Koltunski
          }
763
        catch(Exception ex)
764
          {
765 8c57d77b Leszek Koltunski
          mUser.distortedException(ex);
766 d99fcc9c Leszek Koltunski
          return;
767
          }
768 f482efa3 Leszek Koltunski
        }
769 d99fcc9c Leszek Koltunski
      else
770 f482efa3 Leszek Koltunski
        {
771 d99fcc9c Leszek Koltunski
        return;
772 f482efa3 Leszek Koltunski
        }
773 d99fcc9c Leszek Koltunski
      }
774 bfe45b4a Leszek Koltunski
775 d99fcc9c Leszek Koltunski
    if( mLinkedListSSBO[0]<0 )
776
      {
777
      GLES30.glGenBuffers(1,mLinkedListSSBO,0);
778 bfe45b4a Leszek Koltunski
779 d99fcc9c Leszek Koltunski
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
780
      GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
781
      GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
782
      GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
783
784
      GLES30.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
785
      }
786
787
    // See if we have overflown the SSBO in one of the previous frames.
788
    // If yes, assume we need to make the SSBO larger.
789
    float overflow = counter/(mBufferSize*surface.mWidth*surface.mHeight);
790
791
    if( overflow>1.0f )
792
      {
793
      mBufferSize *= (int)(overflow+1.0f);
794
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
795
      GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
796
      GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
797
      GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
798 f482efa3 Leszek Koltunski
      }
799 d99fcc9c Leszek Koltunski
800
    mOITClearProgram.useProgram();
801
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
802
    GLES30.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f );   // corrections do not really matter here - only present because of common vertex shader.
803
    GLES30.glUniform1f( mOITClearDepthH , 1.0f);          // likewise depth
804
    GLES30.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight);
805
    GLES30.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
806
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
807 b11171e8 Leszek Koltunski
    mOITClearProgram.stopUsingProgram();
808 bfe45b4a Leszek Koltunski
    }
809
810
///////////////////////////////////////////////////////////////////////////////////////////////////
811
// Pass2 of the OIT algorithm - build per-pixel linked lists.
812
813 7602a827 Leszek Koltunski
  static void oitBuild(InternalOutputSurface surface, float corrW, float corrH)
814 bfe45b4a Leszek Koltunski
    {
815 f482efa3 Leszek Koltunski
    if( mOITBuildProgram!=null )
816
      {
817
      mOITBuildProgram.useProgram();
818
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
819
      GLES30.glUniform1i(mOITBuildTextureH, 0);
820
      GLES30.glUniform1i(mOITBuildDepthTextureH, 1);
821
      GLES30.glUniform2f(mOITBuildTexCorrH, corrW, corrH );
822
      GLES30.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight);
823
      GLES30.glUniform1ui(mOITBuildNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
824
      GLES30.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear);
825
      GLES30.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
826
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
827 b11171e8 Leszek Koltunski
      mOITBuildProgram.stopUsingProgram();
828 f482efa3 Leszek Koltunski
      }
829 bfe45b4a Leszek Koltunski
    }
830
831
///////////////////////////////////////////////////////////////////////////////////////////////////
832
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list.
833
834 7602a827 Leszek Koltunski
  static void oitCollapse(InternalOutputSurface surface, float corrW, float corrH)
835 bfe45b4a Leszek Koltunski
    {
836 f482efa3 Leszek Koltunski
    if( mOITCollapseProgram!=null )
837
      {
838
      mOITCollapseProgram.useProgram();
839
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
840
      GLES30.glUniform1i(mOITCollapseDepthTextureH, 1);
841
      GLES30.glUniform2f(mOITCollapseTexCorrH, corrW, corrH );
842
      GLES30.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight);
843
      GLES30.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear);
844
      GLES30.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
845
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
846 b11171e8 Leszek Koltunski
      mOITCollapseProgram.stopUsingProgram();
847 f482efa3 Leszek Koltunski
      }
848 bfe45b4a Leszek Koltunski
    }
849
850
///////////////////////////////////////////////////////////////////////////////////////////////////
851
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists.
852
853 7602a827 Leszek Koltunski
  static void oitRender(InternalOutputSurface surface, float corrW, float corrH)
854 bfe45b4a Leszek Koltunski
    {
855 f482efa3 Leszek Koltunski
    if( mOITRenderProgram!=null )
856
      {
857
      mOITRenderProgram.useProgram();
858
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
859
      GLES30.glUniform2f(mOITRenderTexCorrH, corrW, corrH );
860
      GLES30.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight);
861
      GLES30.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear);
862
      GLES30.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
863
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
864 b11171e8 Leszek Koltunski
      mOITRenderProgram.stopUsingProgram();
865 f482efa3 Leszek Koltunski
      }
866 bfe45b4a Leszek Koltunski
    }
867
868
///////////////////////////////////////////////////////////////////////////////////////////////////
869
870
  static void setSSBOSize(float size)
871
    {
872
    mBufferSize = size;
873
    }
874
875 5f35f1cb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
876
877
  static int getQueueSize()
878
    {
879
    return mFBOQueueSize;
880
    }
881
882 d2dc25ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
883
884
  private static int parseQualcommDriverVersion(String version)
885
    {
886
    int at = version.indexOf('@');
887
888
    if( at>=0 )
889
      {
890
      int dot = version.indexOf('.',at);
891
892
      if( dot>at+1 )
893
        {
894
        String ver = version.substring(at+1,dot);
895
        return Integer.parseInt(ver);
896
        }
897
      }
898
899
    return 0;
900
    }
901
902 edea9cf3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
903
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
904 b7074bc6 Leszek Koltunski
// PowerVR GE8100 / GE8300 compiler fails to compile OIT programs.
905 edea9cf3 Leszek Koltunski
906 5f35f1cb Leszek Koltunski
  private static void detectBuggyDriversAndSetQueueSize(int queueSize)
907 edea9cf3 Leszek Koltunski
    {
908 a9d4bb9a Leszek Koltunski
    mVendor  = GLES30.glGetString(GLES30.GL_VENDOR);
909
    mVersion = GLES30.glGetString(GLES30.GL_VERSION);
910
    mRenderer= GLES30.glGetString(GLES30.GL_RENDERER);
911 edea9cf3 Leszek Koltunski
912 5f35f1cb Leszek Koltunski
    mFBOQueueSize = 1;
913 d2dc25ad Leszek Koltunski
    mFastCompilationTF = true;
914 edea9cf3 Leszek Koltunski
915 a9d4bb9a Leszek Koltunski
    if( mVendor.contains("ARM") )
916 edea9cf3 Leszek Koltunski
      {
917 5f35f1cb Leszek Koltunski
      try
918 edea9cf3 Leszek Koltunski
        {
919 5f35f1cb Leszek Koltunski
        String regex = ".*r(\\d+)p\\d.*";
920
        Pattern pattern = Pattern.compile(regex);
921 a9d4bb9a Leszek Koltunski
        Matcher matcher = pattern.matcher(mVersion);
922 5f35f1cb Leszek Koltunski
923
        if( matcher.find() )
924
          {
925
          String driverVersion = matcher.group(1);
926
927
          if( driverVersion!=null )
928
            {
929
            int drvVersion = Integer.parseInt(driverVersion);
930
931
            if( drvVersion<22 )
932
              {
933 8c57d77b Leszek Koltunski
              mUser.logMessage("You are running this on a ARM Mali driver r"+driverVersion+".\n" +
934 5f35f1cb Leszek Koltunski
                    "This is a buggy driver, please update to r22. Inserting workaround which uses a lot of memory.");
935
936
              mFBOQueueSize = queueSize;
937
              }
938
            }
939
          }
940
        }
941
      catch(Exception ex)
942
        {
943 8c57d77b Leszek Koltunski
        mUser.logMessage("DistortedLibrary: exception trying to pattern match version: "+ex.toString());
944 edea9cf3 Leszek Koltunski
        }
945
      }
946 a9d4bb9a Leszek Koltunski
    else if( mVendor.contains("Imagination") )
947 edea9cf3 Leszek Koltunski
      {
948 a9d4bb9a Leszek Koltunski
      if( mRenderer.contains("GE8") )
949 edea9cf3 Leszek Koltunski
        {
950 8c57d77b Leszek Koltunski
        mUser.logMessage("You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
951
        mUser.logMessage("GLSL Version "+GLES30.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
952 edea9cf3 Leszek Koltunski
        }
953
      }
954 a9d4bb9a Leszek Koltunski
    else if( mVendor.contains("Qualcomm"))
955 97b6c85e Leszek Koltunski
      {
956 d2dc25ad Leszek Koltunski
      int driverVersion = parseQualcommDriverVersion(mVersion);
957
958
      if( mRenderer.contains("308") && (driverVersion==331 || driverVersion==415) )
959 97b6c85e Leszek Koltunski
        {
960 8c57d77b Leszek Koltunski
        mUser.logMessage("You are running this on an Adreno 308 driver 331 or 415.\nStrange shit might happen.");
961 eff6e3d3 Leszek Koltunski
        mBuggyUBOs = true;
962 97b6c85e Leszek Koltunski
        }
963 d2dc25ad Leszek Koltunski
      if( mGLSL<=300 && driverVersion<415 ) // This is only on old enough drivers, 84,95,100,104,140 and known bad, 415 is known good.
964
        {
965 8c57d77b Leszek Koltunski
        mUser.logMessage("Slow compilation of Transform Feedback programs!");
966 d2dc25ad Leszek Koltunski
        mFastCompilationTF = false;
967
        }
968 97b6c85e Leszek Koltunski
      }
969 edea9cf3 Leszek Koltunski
    }
970
971 3c26029b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
972
973
  private static int getVersion()
974
    {
975
    int[] major = new int[1];
976
    int[] minor = new int[1];
977
    GLES30.glGetIntegerv(GLES30.GL_MAJOR_VERSION, major, 0);
978
    GLES30.glGetIntegerv(GLES30.GL_MINOR_VERSION, minor, 0);
979
    return major[0] * 100 + minor[0] * 10;
980
    }
981
982 8c57d77b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
983
/**
984
 * Not an external API
985
 *
986
 * @y.exclude
987
 */
988
989
  public static void logMessage(String message)
990
    {
991
    mUser.logMessage(message);
992
    }
993
994 b7074bc6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
995
/**
996
 * Return OpenGL ES version supported by the hardware we are running on.
997 60fdf71d Leszek Koltunski
 * There are only three possibilities: 300 (OpenGL ES 3.0) or 310 (at least OpenGL ES 3.1)
998
 * or 200 (OpenGL ES 2.0)
999 b7074bc6 Leszek Koltunski
 */
1000 60fdf71d Leszek Koltunski
  public static int getGLSL()
1001 b7074bc6 Leszek Koltunski
    {
1002
    return mGLSL;
1003
    }
1004
1005 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1006
/**
1007 c43abe6c Leszek Koltunski
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
1008 9519d1b1 Leszek Koltunski
 * I.e. best called from GLSurfaceView.Renderer.onSurfaceCreated().
1009 6a06a912 Leszek Koltunski
 * <p>
1010 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
1011 5f35f1cb Leszek Koltunski
 *
1012 8c57d77b Leszek Koltunski
 * @param user The Code which will be using this library, which must implement the LibraryUser interface.
1013 6a06a912 Leszek Koltunski
 */
1014 8c57d77b Leszek Koltunski
  public static void onSurfaceCreated(final LibraryUser user)
1015 5f35f1cb Leszek Koltunski
    {
1016 8c57d77b Leszek Koltunski
    onSurfaceCreated(user,4);
1017 5f35f1cb Leszek Koltunski
    }
1018
1019
///////////////////////////////////////////////////////////////////////////////////////////////////
1020
/**
1021
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
1022 9519d1b1 Leszek Koltunski
 * I.e. best called from GLSurfaceView.Renderer.onSurfaceCreated().
1023 5f35f1cb Leszek Koltunski
 * <p>
1024
 * Needs to be called from a thread holding the OpenGL context.
1025
 *   
1026 8c57d77b Leszek Koltunski
 * @param user The Code which will be using this library, which must implement the LibraryUser interface.
1027 9519d1b1 Leszek Koltunski
 * @param queueSize the size of the FBO queue, a workaround for the bug on Mali drivers. Use a small integer - 1,...,4
1028 5f35f1cb Leszek Koltunski
 */
1029 8c57d77b Leszek Koltunski
  public static void onSurfaceCreated(final LibraryUser user, int queueSize)
1030 d6e94c84 Leszek Koltunski
    {
1031 8c57d77b Leszek Koltunski
    mUser = user;
1032 3c26029b Leszek Koltunski
    int version = getVersion();
1033 8c57d77b Leszek Koltunski
    int major = version/100;
1034
    int minor = ((version/10)%10);
1035 d99fcc9c Leszek Koltunski
1036 b7074bc6 Leszek Koltunski
    if( major< 3 )
1037
      {
1038
      mGLSL = 100*major + 10*minor;
1039 d99fcc9c Leszek Koltunski
      VertexCompilationException ex = new VertexCompilationException("at least OpenGL ES 3.0 required, this device supports only "+major+"."+minor);
1040 8c57d77b Leszek Koltunski
      mUser.distortedException(ex);
1041 b7074bc6 Leszek Koltunski
      }
1042
    else
1043
      {
1044
      mGLSL = (major==3 && minor==0) ? 300 : 310;
1045
      }
1046
1047 78ff6ea9 Leszek Koltunski
    int[] tmp = new int[1];
1048
    GLES30.glGetIntegerv(GLES30.GL_MAX_TEXTURE_SIZE, tmp, 0);
1049
    mMaxTextureSize = tmp[0];
1050
    GLES30.glGetIntegerv(GLES30.GL_MAX_VERTEX_UNIFORM_VECTORS  , tmp, 0);
1051
    mMaxNumberOfVerUniforms = tmp[0];
1052
    GLES30.glGetIntegerv(GLES30.GL_MAX_FRAGMENT_UNIFORM_VECTORS, tmp, 0);
1053
    mMaxNumberOfFraUniforms = tmp[0];
1054 2bd3d0fb Leszek Koltunski
1055 8c57d77b Leszek Koltunski
    mUser.logMessage("Using OpenGL ES "+major+"."+minor);
1056 8f011621 Leszek Koltunski
/*
1057 8c57d77b Leszek Koltunski
    mUser.logMessage("max texture size: "+mMaxTextureSize);
1058
    mUser.logMessage("max num vert: "+mMaxNumberOfVerUniforms);
1059
    mUser.logMessage("max num frag: "+mMaxNumberOfFraUniforms);
1060 8f011621 Leszek Koltunski
*/
1061 b7074bc6 Leszek Koltunski
    mGLSL_VERSION = "#version "+mGLSL+" es\n";
1062 edea9cf3 Leszek Koltunski
1063 9ec374e8 Leszek Koltunski
    InternalStackFrameList.setInitialized(true);
1064 d99fcc9c Leszek Koltunski
    mOITCompilationAttempted = false;
1065 edea9cf3 Leszek Koltunski
1066 5f35f1cb Leszek Koltunski
    detectBuggyDriversAndSetQueueSize(queueSize);
1067 0e924ba5 Leszek Koltunski
    EffectMessageSender.startSending();
1068
1069 edea9cf3 Leszek Koltunski
    try
1070
      {
1071 d99fcc9c Leszek Koltunski
      createMainProgram();
1072 edea9cf3 Leszek Koltunski
      }
1073
    catch(Exception ex)
1074
      {
1075 8c57d77b Leszek Koltunski
      mUser.distortedException(ex);
1076 edea9cf3 Leszek Koltunski
      }
1077
1078 f046b159 Leszek Koltunski
    try
1079
      {
1080 8c57d77b Leszek Koltunski
      final InputStream vertStream = mUser.localFile(R.raw.main_vertex_shader);
1081
      final InputStream fragStream = mUser.localFile(R.raw.preprocess_fragment_shader);
1082
      EffectQueuePostprocess.createPrograms(vertStream, fragStream, mGLSL);
1083 f046b159 Leszek Koltunski
      }
1084
    catch(Exception ex)
1085
      {
1086 8c57d77b Leszek Koltunski
      mUser.distortedException(ex);
1087 a13dde77 Leszek Koltunski
      }
1088
1089 edea9cf3 Leszek Koltunski
    try
1090
      {
1091 b7074bc6 Leszek Koltunski
      PostprocessEffect.createPrograms(mGLSL);
1092 edea9cf3 Leszek Koltunski
      }
1093
    catch(Exception ex)
1094
      {
1095 8c57d77b Leszek Koltunski
      mUser.distortedException(ex);
1096 edea9cf3 Leszek Koltunski
      }
1097 6a06a912 Leszek Koltunski
    }
1098
1099 97460908 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1100
/**
1101
 * Switch face culling on/off
1102
 */
1103
  public static void setCull(boolean on)
1104
    {
1105
    if( on )
1106
      {
1107
      GLES30.glEnable(GLES30.GL_CULL_FACE);
1108
      GLES30.glCullFace(GLES30.GL_FRONT);
1109
      }
1110
    else
1111
      {
1112
      GLES30.glDisable(GLES30.GL_CULL_FACE);
1113
      }
1114
    }
1115
1116 9ec374e8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1117
/**
1118
 * Call this so that the Library can initialize its internal data structures.
1119
 * Must be called from Activity.onCreate().
1120
 */
1121
  public static void onCreate()
1122
    {
1123
    onCreate(0);
1124
    }
1125
1126
///////////////////////////////////////////////////////////////////////////////////////////////////
1127
/**
1128
 * Call this so that the Library can initialize its internal data structures.
1129
 * Must be called from Activity.onCreate().
1130
 *
1131
 * @param id id of an Activity that is using the library; anything unique so that the Library can
1132
 *           tell between Activities in case you're going to be using it from more than one.
1133
 */
1134
  public static void onCreate(long id)
1135
    {
1136
    InternalStackFrameList.onCreate(id);
1137
    }
1138
1139
///////////////////////////////////////////////////////////////////////////////////////////////////
1140
/**
1141
 * Call this so that the Library can resume its operations.
1142
 * Must be called from Activity.onResume().
1143
 */
1144
  public static void onResume()
1145
    {
1146 ee238898 Leszek Koltunski
    onResume(0);
1147 9ec374e8 Leszek Koltunski
    }
1148
1149
///////////////////////////////////////////////////////////////////////////////////////////////////
1150
/**
1151
 * Call this so that the Library can resume its operations.
1152
 * Must be called from Activity.onResume().
1153
 *
1154
 * @param id id of an Activity that is using the library; anything unique so that the Library can
1155
 *           tell between Activities in case you're going to be using it from more than one.
1156
 */
1157
  public static void onResume(long id)
1158
    {
1159
    InternalStackFrameList.onResume(id);
1160
    }
1161
1162 05ecc6fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1163
/**
1164 78e89fb5 Leszek Koltunski
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
1165 05ecc6fe Leszek Koltunski
 * Must be called from Activity.onPause().
1166
 */
1167
  public static void onPause()
1168
    {
1169 9ec374e8 Leszek Koltunski
    onPause(0);
1170
    }
1171
1172
///////////////////////////////////////////////////////////////////////////////////////////////////
1173
/**
1174
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
1175
 * Must be called from Activity.onPause().
1176
 *
1177
 * @param id id of an Activity that is using the library; anything unique so that the Library can
1178
 *           tell between Activities in case you're going to be using it from more than one.
1179
 */
1180
  public static void onPause(long id)
1181
    {
1182
    InternalStackFrameList.onPause(id);
1183 8d98b65f Leszek Koltunski
1184 30094332 Leszek Koltunski
    Dynamic.onPause();  // common for all frames
1185 8d98b65f Leszek Koltunski
    InternalOutputSurface.onPause();
1186
    Effect.onPause();
1187
    DeferredJobs.onPause();
1188
1189
    mOITCompilationAttempted = false;
1190
    mNeedsTransformFeedback  = false;
1191 c8dbce40 Leszek Koltunski
1192 bfe45b4a Leszek Koltunski
    mLinkedListSSBO[0]= -1;
1193 5f35f1cb Leszek Koltunski
    mAtomicCounter = null;
1194 fd836a41 Leszek Koltunski
1195 d99fcc9c Leszek Koltunski
    mNormalProgram     = null;
1196
    mMainOITProgram    = null;
1197
    mMainProgram       = null;
1198
    mFullProgram       = null;
1199
    mOITClearProgram   = null;
1200
    mOITBuildProgram   = null;
1201
    mOITCollapseProgram= null;
1202
    mOITRenderProgram  = null;
1203
    mBlitDepthProgram  = null;
1204
    mBlitProgram       = null;
1205 6a06a912 Leszek Koltunski
    }
1206 bfe45b4a Leszek Koltunski
1207 c8dbce40 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1208
/**
1209
 * Call this so that the Library can release its internal data structures.
1210 9ec374e8 Leszek Koltunski
 * Must be called from Activity.onDestroy().
1211 c8dbce40 Leszek Koltunski
 */
1212
  public static void onDestroy()
1213
    {
1214 9ec374e8 Leszek Koltunski
    onDestroy(0);
1215
    }
1216
1217
///////////////////////////////////////////////////////////////////////////////////////////////////
1218
/**
1219
 * Call this so that the Library can release its internal data structures.
1220
 * Must be called from Activity.onDestroy().
1221
 *
1222
 * @param id id of an Activity that is using the library; anything unique so that the Library can
1223
 *           tell between Activities in case you're going to be using it from more than one.
1224
 */
1225
  public static void onDestroy(long id)
1226
    {
1227
    if( InternalStackFrameList.isInitialized() )
1228 c8dbce40 Leszek Koltunski
      {
1229 9ec374e8 Leszek Koltunski
      InternalStackFrameList.onDestroy(id);
1230 c8dbce40 Leszek Koltunski
      }
1231
    }
1232
1233 d2dc25ad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1234
/**
1235
 * Some devices - Qualcomm's Adreno 3xx with drivers v. 84,95,100,104,140, and possibly more -
1236
 * suffer from a very slow compilation of GLSL program if said program includes Transform Feedback.
1237
 * Return true if the platform we are running on does not suffer from this problem.
1238
 */
1239
  public static boolean fastCompilationTF()
1240
    {
1241
    return mFastCompilationTF;
1242
    }
1243
1244 2bd3d0fb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1245
/**
1246
 * Return the maximum size of the texture supported by the driver.
1247
 */
1248
  public static int getMaxTextureSize()
1249
    {
1250
    return mMaxTextureSize;
1251
    }
1252
1253 97b6c85e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1254
/**
1255
 * Call this before calling onSurfaceCreated() if you want to access normal vectors in CPU.
1256
 */
1257
  public static void needTransformFeedback()
1258
    {
1259
    mNeedsTransformFeedback = true;
1260
    }
1261
1262 bfe45b4a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1263
/**
1264
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
1265
 * single (InputSurface,MeshBase) combo.
1266
 *
1267
 * @param type {@link EffectType}
1268
 * @return The maximum number of effects of a given type.
1269
 */
1270
  @SuppressWarnings("unused")
1271
  public static int getMax(EffectType type)
1272
    {
1273
    return EffectQueue.getMax(type.ordinal());
1274
    }
1275
1276
///////////////////////////////////////////////////////////////////////////////////////////////////
1277
/**
1278
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
1279
 * This can fail if:
1280
 * <ul>
1281
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
1282
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
1283 9519d1b1 Leszek Koltunski
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link DistortedLibrary#onSurfaceCreated}. After this
1284 bfe45b4a Leszek Koltunski
 *     time only decreasing the value of 'max' is permitted.
1285
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
1286
 * </ul>
1287
 *
1288
 * @param type {@link EffectType}
1289
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
1290
 *            than Byte.MAX_VALUE
1291
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
1292
 */
1293
  @SuppressWarnings("unused")
1294
  public static boolean setMax(EffectType type, int max)
1295
    {
1296
    return EffectQueue.setMax(type.ordinal(),max);
1297
    }
1298 a9d4bb9a Leszek Koltunski
1299
///////////////////////////////////////////////////////////////////////////////////////////////////
1300
/**
1301
 * Return a String defining the vendor of the graphics driver.
1302
 */
1303
  public static String getDriverVendor()
1304
    {
1305
    return mVendor;
1306
    }
1307
1308
///////////////////////////////////////////////////////////////////////////////////////////////////
1309
/**
1310
 * Return a String defining the version of the graphics driver.
1311
 */
1312
  public static String getDriverVersion()
1313
    {
1314
    return mVersion;
1315
    }
1316
1317
///////////////////////////////////////////////////////////////////////////////////////////////////
1318
/**
1319
 * Return a String defining the renderer of the graphics driver.
1320
 */
1321
  public static String getDriverRenderer()
1322
    {
1323
    return mRenderer;
1324
    }
1325 f8f6d457 leszek
  }