Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedLibrary.java @ 8c57d77b

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