Project

General

Profile

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

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

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