Project

General

Profile

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

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

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 eff6e3d3 Leszek Koltunski
  private static boolean mBuggyUBOs;
120 78ff6ea9 Leszek Koltunski
121
  private static int mMaxTextureSize         = Integer.MAX_VALUE;
122
  private static int mMaxNumberOfVerUniforms = Integer.MAX_VALUE;
123
  private static int mMaxNumberOfFraUniforms = Integer.MAX_VALUE;
124 6a06a912 Leszek Koltunski
125 bfe45b4a Leszek Koltunski
  //////////////////////////////////////////////////////////////////////////////////////////////
126
  /// MAIN PROGRAM ///
127
  private static DistortedProgram mMainProgram;
128
  private static int mMainTextureH;
129 62c869ad Leszek Koltunski
  private static int mTransformFeedbackH;
130 bfe45b4a Leszek Koltunski
131
  /// NORMAL PROGRAM /////
132
  private static DistortedProgram mNormalProgram;
133 62c869ad Leszek Koltunski
  private static int mNormalProjectionH;
134 bfe45b4a Leszek Koltunski
135
  /// MAIN OIT PROGRAM ///
136
  private static DistortedProgram mMainOITProgram;
137
  private static int mMainOITTextureH;
138
  private static int mMainOITSizeH;
139
  private static int mMainOITNumRecordsH;
140
141
  /// BLIT PROGRAM ///
142
  private static DistortedProgram mBlitProgram;
143
  private static int mBlitTextureH;
144
  private static int mBlitDepthH;
145
  private static final FloatBuffer mQuadPositions;
146
147 f046b159 Leszek Koltunski
  /// FULL PROGRAM ///
148
  private static DistortedProgram mFullProgram;
149
150 bfe45b4a Leszek Koltunski
  static
151
    {
152
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
153
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
154
    mQuadPositions.put(positionData).position(0);
155
    }
156
157
  /// BLIT DEPTH PROGRAM ///
158
  private static DistortedProgram mBlitDepthProgram;
159
  private static int mBlitDepthTextureH;
160
  private static int mBlitDepthDepthTextureH;
161
  private static int mBlitDepthDepthH;
162
  private static int mBlitDepthTexCorrH;
163
164 0bd9f644 Leszek Koltunski
  /// Program Handles ///
165
  private static int mMainProgramH, mFullProgramH, mMainOITProgramH;
166
167 bfe45b4a Leszek Koltunski
  /// OIT SSBO BUFFER ///
168
  private static int[] mLinkedListSSBO = new int[1];
169 5f35f1cb Leszek Koltunski
  private static int[] mAtomicCounter;
170 bfe45b4a Leszek Koltunski
  private static int   mCurrBuffer;
171
172
  static
173
    {
174
    mLinkedListSSBO[0]= -1;
175
    mCurrBuffer       =  0;
176
    }
177
178
  ///////////////////////////////////////////////////////////////
179
  // meaning: allocate 1.0 screenful of places for transparent
180
  // fragments in the SSBO backing up the OIT render method.
181
  private static float mBufferSize=1.0f;
182
183
  /// OIT CLEAR PROGRAM ///
184
  private static DistortedProgram mOITClearProgram;
185
  private static int mOITClearDepthH;
186
  private static int mOITClearTexCorrH;
187
  private static int mOITClearSizeH;
188
189
  /// OIT BUILD PROGRAM ///
190
  private static DistortedProgram mOITBuildProgram;
191
  private static int mOITBuildTextureH;
192
  private static int mOITBuildDepthTextureH;
193
  private static int mOITBuildDepthH;
194
  private static int mOITBuildTexCorrH;
195
  private static int mOITBuildSizeH;
196
  private static int mOITBuildNumRecordsH;
197
198
  /// OIT COLLAPSE PROGRAM ///
199
  private static DistortedProgram mOITCollapseProgram;
200
  private static int mOITCollapseDepthTextureH;
201
  private static int mOITCollapseDepthH;
202
  private static int mOITCollapseTexCorrH;
203
  private static int mOITCollapseSizeH;
204
205
  /// OIT RENDER PROGRAM ///
206
  private static DistortedProgram mOITRenderProgram;
207
  private static int mOITRenderDepthH;
208
  private static int mOITRenderTexCorrH;
209
  private static int mOITRenderSizeH;
210
211
  /// END PROGRAMS //////
212
213 d99fcc9c Leszek Koltunski
  /**
214
   * Every application using the library must implement this interface so that the library can send
215
   * it exceptions that arise. The exceptions may come at any time, for example the library will
216
   * compile its OIT problem only on the first attempt to use the OIT
217
   * Those will mainly be hardware-related: shaders do not compile on particular hardware, the required
218
   * OpenGL ES 3.0 is not supported, etc.
219
   */
220
  public interface ExceptionListener
221
    {
222
    void distortedException(Exception ex);
223
    }
224
225
  private static ExceptionListener mListener;
226
  private static Resources mResources;
227
228 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
229 c638c1b0 Leszek Koltunski
// private: hide this from Javadoc
230 6a06a912 Leszek Koltunski
231 7602a827 Leszek Koltunski
  private DistortedLibrary()
232 6a06a912 Leszek Koltunski
    {
233 8eccf334 Leszek Koltunski
234 6a06a912 Leszek Koltunski
    }
235
236 bfe45b4a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238 d99fcc9c Leszek Koltunski
  private static void createMainProgram()
239 bfe45b4a Leszek Koltunski
    {
240
    // MAIN PROGRAM ////////////////////////////////////
241 d99fcc9c Leszek Koltunski
    final InputStream mainVertStream = mResources.openRawResource(R.raw.main_vertex_shader);
242
    final InputStream mainFragStream = mResources.openRawResource(R.raw.main_fragment_shader);
243 bfe45b4a Leszek Koltunski
244
    int numF = FragmentEffect.getNumEnabled();
245
    int numV = VertexEffect.getNumEnabled();
246
247 b7074bc6 Leszek Koltunski
    String mainVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
248
    String mainFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
249 36d65d88 Leszek Koltunski
250 e8925fcd Leszek Koltunski
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
251 46d463a4 Leszek Koltunski
    if( MeshBase.getUseCenters() ) mainVertHeader += "#define COMP_CENTERS\n";
252 eff6e3d3 Leszek Koltunski
    if( mBuggyUBOs )               mainVertHeader += "#define BUGGY_UBOS\n";
253 36d65d88 Leszek Koltunski
254 bfe45b4a Leszek Koltunski
    String enabledEffectV= VertexEffect.getGLSL();
255
    String enabledEffectF= FragmentEffect.getGLSL();
256
257
    String[] feedback = { "v_Position", "v_endPosition" };
258
259
    try
260
      {
261 97b6c85e Leszek Koltunski
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader,
262
                                          mainFragHeader, enabledEffectV, enabledEffectF,
263
                                          mGLSL, mNeedsTransformFeedback ? feedback : null );
264 bfe45b4a Leszek Koltunski
      }
265
    catch(Exception e)
266
      {
267
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
268
      throw new RuntimeException(e.getMessage());
269
      }
270
271 0bd9f644 Leszek Koltunski
    mMainProgramH = mMainProgram.getProgramHandle();
272
    EffectQueue.getUniforms(mMainProgramH,0);
273 9f9924f8 Leszek Koltunski
    MeshBase.getUniforms(mMainProgramH,0);
274 0bd9f644 Leszek Koltunski
    mMainTextureH= GLES30.glGetUniformLocation( mMainProgramH, "u_Texture");
275
    mTransformFeedbackH= GLES30.glGetUniformLocation( mMainProgramH, "u_TransformFeedback");
276 bfe45b4a Leszek Koltunski
277
    // BLIT PROGRAM ////////////////////////////////////
278 d99fcc9c Leszek Koltunski
    final InputStream blitVertStream = mResources.openRawResource(R.raw.blit_vertex_shader);
279
    final InputStream blitFragStream = mResources.openRawResource(R.raw.blit_fragment_shader);
280 bfe45b4a Leszek Koltunski
281
    try
282
      {
283 b7074bc6 Leszek Koltunski
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
284 bfe45b4a Leszek Koltunski
      }
285
    catch(Exception e)
286
      {
287
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
288
      throw new RuntimeException(e.getMessage());
289
      }
290
291
    int blitProgramH = mBlitProgram.getProgramHandle();
292 b7074bc6 Leszek Koltunski
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
293
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
294 bfe45b4a Leszek Koltunski
295
    // BLIT DEPTH PROGRAM ////////////////////////////////////
296 d99fcc9c Leszek Koltunski
    final InputStream blitDepthVertStream = mResources.openRawResource(R.raw.blit_depth_vertex_shader);
297
    final InputStream blitDepthFragStream = mResources.openRawResource(R.raw.blit_depth_fragment_shader);
298 bfe45b4a Leszek Koltunski
299
    try
300
      {
301 b7074bc6 Leszek Koltunski
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
302 bfe45b4a Leszek Koltunski
      }
303
    catch(Exception e)
304
      {
305
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
306
      throw new RuntimeException(e.getMessage());
307
      }
308
309
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
310 b7074bc6 Leszek Koltunski
    mBlitDepthTextureH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Texture");
311
    mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
312
    mBlitDepthDepthH        = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
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 bfe45b4a Leszek Koltunski
    }
533
534 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
535 0bd9f644 Leszek Koltunski
/**
536
 * Execute all VertexEffects and adjust all vertices
537
 *
538
 * @y.exclude
539
 */
540 07206c71 Leszek Koltunski
  public static void adjustVertices(MeshBase mesh, EffectQueueVertex queue)
541 f046b159 Leszek Koltunski
    {
542 d99fcc9c Leszek Koltunski
    if( mFullProgram==null )
543 f046b159 Leszek Koltunski
      {
544 d99fcc9c Leszek Koltunski
      try
545
        {
546
        createFullProgram(mResources);
547
        }
548
      catch(Exception ex)
549
        {
550
        mListener.distortedException(ex);
551
        return;
552
        }
553 f046b159 Leszek Koltunski
      }
554 d99fcc9c Leszek Koltunski
555
    int num = mesh.getNumVertices();
556
    int tfo = mesh.getTFO();
557
558
    mFullProgram.useProgram();
559
    mesh.bindVertexAttribs(mFullProgram);
560
    queue.compute(1);
561 78ff6ea9 Leszek Koltunski
    queue.send(0.0f,mFullProgramH,3);
562 9f9924f8 Leszek Koltunski
    mesh.send(mFullProgramH,3);
563 d99fcc9c Leszek Koltunski
564
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
565
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
566
    InternalRenderState.switchOffDrawing();
567
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
568
    InternalRenderState.restoreDrawing();
569
    GLES30.glEndTransformFeedback();
570
    mesh.copyTransformToVertex();
571
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
572 f046b159 Leszek Koltunski
    }
573
574 bfe45b4a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
575
576 c90aca24 Leszek Koltunski
  static void drawPrivOIT(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
577 bfe45b4a Leszek Koltunski
    {
578 06ddd60d Leszek Koltunski
    if( mMainOITProgram!=null )
579
      {
580
      EffectQueue[] queues = effects.getQueues();
581 c90aca24 Leszek Koltunski
582 06ddd60d Leszek Koltunski
      EffectQueue.compute(queues, currTime);
583
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
584 bfe45b4a Leszek Koltunski
585 06ddd60d Leszek Koltunski
      mMainOITProgram.useProgram();
586
      GLES30.glUniform1i(mMainOITTextureH, 0);
587
      GLES30.glUniform2ui(mMainOITSizeH, surface.mWidth, surface.mHeight);
588
      GLES30.glUniform1ui(mMainOITNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
589
      mesh.bindVertexAttribs(mMainOITProgram);
590 9f9924f8 Leszek Koltunski
      mesh.send(mMainOITProgramH,1);
591 bfe45b4a Leszek Koltunski
592 06ddd60d Leszek Koltunski
      float inflate     = mesh.getInflate();
593
      float distance    = surface.mDistance;
594
      float mipmap      = surface.mMipmap;
595
      float[] projection= surface.mProjectionMatrix;
596 bfe45b4a Leszek Koltunski
597 78ff6ea9 Leszek Koltunski
      EffectQueue.send(queues, mMainOITProgramH, distance, mipmap, projection, inflate, 1 );
598 06ddd60d Leszek Koltunski
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
599 bfe45b4a Leszek Koltunski
600 06ddd60d Leszek Koltunski
      if( mesh.getShowNormals() )
601
        {
602
        mMainProgram.useProgram();
603 9f9924f8 Leszek Koltunski
        mesh.send(mMainProgramH,0);
604 78ff6ea9 Leszek Koltunski
        EffectQueue.send(queues, mMainProgramH, distance, mipmap, projection, inflate, 0 );
605 62c869ad Leszek Koltunski
        displayNormals(projection,mesh);
606 06ddd60d Leszek Koltunski
        }
607 bfe45b4a Leszek Koltunski
      }
608
    }
609
610
///////////////////////////////////////////////////////////////////////////////////////////////////
611
612 c90aca24 Leszek Koltunski
  static void drawPriv(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
613 bfe45b4a Leszek Koltunski
    {
614 f046b159 Leszek Koltunski
    if( mMainProgram!=null )
615 f482efa3 Leszek Koltunski
      {
616
      EffectQueue[] queues = effects.getQueues();
617 bfe45b4a Leszek Koltunski
618 f482efa3 Leszek Koltunski
      EffectQueue.compute(queues, currTime);
619
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
620 46b572b5 Leszek Koltunski
621 f482efa3 Leszek Koltunski
      mMainProgram.useProgram();
622
      GLES30.glUniform1i(DistortedLibrary.mMainTextureH, 0);
623
      mesh.bindVertexAttribs(DistortedLibrary.mMainProgram);
624 9f9924f8 Leszek Koltunski
      mesh.send(mMainProgramH,0);
625 809dcae3 Leszek Koltunski
626 f482efa3 Leszek Koltunski
      float inflate     = mesh.getInflate();
627
      float distance    = surface.mDistance;
628
      float mipmap      = surface.mMipmap;
629
      float[] projection= surface.mProjectionMatrix;
630 809dcae3 Leszek Koltunski
631 78ff6ea9 Leszek Koltunski
      EffectQueue.send(queues, mMainProgramH, distance, mipmap, projection, inflate, 0 );
632 f482efa3 Leszek Koltunski
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
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 bfe45b4a Leszek Koltunski
646 f482efa3 Leszek Koltunski
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
647
      GLES30.glUniform1i(mBlitTextureH, 0);
648
      GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
649
      GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
650
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
651
      }
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
662
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
663
      GLES30.glUniform1i(mBlitDepthTextureH, 0);
664
      GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
665
      GLES30.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
666
      GLES30.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
667
      GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
668
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
669
      }
670 bfe45b4a Leszek Koltunski
    }
671
672
///////////////////////////////////////////////////////////////////////////////////////////////////
673 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
674 bfe45b4a Leszek Koltunski
675
  private static int printPreviousBuffer()
676
    {
677
    int counter = 0;
678
679 b7074bc6 Leszek Koltunski
    ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
680
                                                                GLES30.GL_MAP_READ_BIT);
681 bfe45b4a Leszek Koltunski
    if( atomicBuf!=null )
682
      {
683
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
684
      counter = atomicIntBuf.get(0);
685
      }
686
    else
687
      {
688 b7074bc6 Leszek Koltunski
      Log.e("effects", "print: failed to map atomic buffer");
689 bfe45b4a Leszek Koltunski
      }
690
691 b7074bc6 Leszek Koltunski
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
692 bfe45b4a Leszek Koltunski
693
    return counter;
694
    }
695
696
///////////////////////////////////////////////////////////////////////////////////////////////////
697
698
  private static void zeroBuffer()
699
    {
700 b7074bc6 Leszek Koltunski
    ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
701 fd836a41 Leszek Koltunski
                                                                GLES30.GL_MAP_WRITE_BIT|GLES30.GL_MAP_INVALIDATE_BUFFER_BIT);
702 bfe45b4a Leszek Koltunski
    if( atomicBuf!=null )
703
      {
704
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
705
      atomicIntBuf.put(0,0);
706
      }
707
    else
708
      {
709 b7074bc6 Leszek Koltunski
      Log.e("effects", "zero: failed to map atomic buffer");
710 bfe45b4a Leszek Koltunski
      }
711
712 b7074bc6 Leszek Koltunski
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
713 bfe45b4a Leszek Koltunski
    }
714
715
///////////////////////////////////////////////////////////////////////////////////////////////////
716
// reset atomic counter to 0
717
718
  static int zeroOutAtomic()
719
    {
720
    int counter = 0;
721
722 5f35f1cb Leszek Koltunski
    if( mAtomicCounter==null )
723 bfe45b4a Leszek Koltunski
      {
724 5f35f1cb Leszek Koltunski
      mAtomicCounter = new int[mFBOQueueSize];
725
726
      GLES30.glGenBuffers(mFBOQueueSize,mAtomicCounter,0);
727 bfe45b4a Leszek Koltunski
728 5f35f1cb Leszek Koltunski
      for(int i=0; i<mFBOQueueSize; i++)
729 bfe45b4a Leszek Koltunski
        {
730 b7074bc6 Leszek Koltunski
        GLES30.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]);
731
        GLES30.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES30.GL_DYNAMIC_DRAW);
732 bfe45b4a Leszek Koltunski
        zeroBuffer();
733
        }
734
      }
735
736
    // reading the value of the buffer on every frame would slow down rendering by
737 d99fcc9c Leszek Koltunski
    // about 3%; doing it only once every 5 frames affects speed by less than 1%.
738 bfe45b4a Leszek Koltunski
    if( mCurrBuffer==0 )
739
      {
740 b7074bc6 Leszek Koltunski
      GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
741 bfe45b4a Leszek Koltunski
      counter = printPreviousBuffer();
742
      }
743
744 5f35f1cb Leszek Koltunski
    if( ++mCurrBuffer>=mFBOQueueSize ) mCurrBuffer = 0;
745 bfe45b4a Leszek Koltunski
746 b7074bc6 Leszek Koltunski
    GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
747 bfe45b4a Leszek Koltunski
    zeroBuffer();
748
749
    return counter;
750
    }
751
752
///////////////////////////////////////////////////////////////////////////////////////////////////
753 809dcae3 Leszek Koltunski
// Pass1 of the OIT algorithm. Clear per-pixel head-pointers.
754 bfe45b4a Leszek Koltunski
755 7602a827 Leszek Koltunski
  static void oitClear(InternalOutputSurface surface, int counter)
756 bfe45b4a Leszek Koltunski
    {
757 d99fcc9c Leszek Koltunski
    if( mOITClearProgram==null )
758 bfe45b4a Leszek Koltunski
      {
759 d99fcc9c Leszek Koltunski
      if( mGLSL>=310 && !mOITCompilationAttempted )
760 f482efa3 Leszek Koltunski
        {
761 d99fcc9c Leszek Koltunski
        mOITCompilationAttempted = true;
762 bfe45b4a Leszek Koltunski
763 d99fcc9c Leszek Koltunski
        try
764
          {
765
          createOITProgram(mResources);
766
          }
767
        catch(Exception ex)
768
          {
769
          mListener.distortedException(ex);
770
          return;
771
          }
772 f482efa3 Leszek Koltunski
        }
773 d99fcc9c Leszek Koltunski
      else
774 f482efa3 Leszek Koltunski
        {
775 d99fcc9c Leszek Koltunski
        return;
776 f482efa3 Leszek Koltunski
        }
777 d99fcc9c Leszek Koltunski
      }
778 bfe45b4a Leszek Koltunski
779 d99fcc9c Leszek Koltunski
    if( mLinkedListSSBO[0]<0 )
780
      {
781
      GLES30.glGenBuffers(1,mLinkedListSSBO,0);
782 bfe45b4a Leszek Koltunski
783 d99fcc9c Leszek Koltunski
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
784
      GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
785
      GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
786
      GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
787
788
      GLES30.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
789
      }
790
791
    // See if we have overflown the SSBO in one of the previous frames.
792
    // If yes, assume we need to make the SSBO larger.
793
    float overflow = counter/(mBufferSize*surface.mWidth*surface.mHeight);
794
795
    if( overflow>1.0f )
796
      {
797
      mBufferSize *= (int)(overflow+1.0f);
798
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
799
      GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
800
      GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
801
      GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
802 f482efa3 Leszek Koltunski
      }
803 d99fcc9c Leszek Koltunski
804
    mOITClearProgram.useProgram();
805
806
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
807
    GLES30.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f );   // corrections do not really matter here - only present because of common vertex shader.
808
    GLES30.glUniform1f( mOITClearDepthH , 1.0f);          // likewise depth
809
    GLES30.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight);
810
    GLES30.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
811
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
812 bfe45b4a Leszek Koltunski
    }
813
814
///////////////////////////////////////////////////////////////////////////////////////////////////
815
// Pass2 of the OIT algorithm - build per-pixel linked lists.
816
817 7602a827 Leszek Koltunski
  static void oitBuild(InternalOutputSurface surface, float corrW, float corrH)
818 bfe45b4a Leszek Koltunski
    {
819 f482efa3 Leszek Koltunski
    if( mOITBuildProgram!=null )
820
      {
821
      mOITBuildProgram.useProgram();
822
823
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
824
      GLES30.glUniform1i(mOITBuildTextureH, 0);
825
      GLES30.glUniform1i(mOITBuildDepthTextureH, 1);
826
      GLES30.glUniform2f(mOITBuildTexCorrH, corrW, corrH );
827
      GLES30.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight);
828
      GLES30.glUniform1ui(mOITBuildNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
829
      GLES30.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear);
830
      GLES30.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
831
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
832
      }
833 bfe45b4a Leszek Koltunski
    }
834
835
///////////////////////////////////////////////////////////////////////////////////////////////////
836
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list.
837
838 7602a827 Leszek Koltunski
  static void oitCollapse(InternalOutputSurface surface, float corrW, float corrH)
839 bfe45b4a Leszek Koltunski
    {
840 f482efa3 Leszek Koltunski
    if( mOITCollapseProgram!=null )
841
      {
842
      mOITCollapseProgram.useProgram();
843
844
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
845
      GLES30.glUniform1i(mOITCollapseDepthTextureH, 1);
846
      GLES30.glUniform2f(mOITCollapseTexCorrH, corrW, corrH );
847
      GLES30.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight);
848
      GLES30.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear);
849
      GLES30.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
850
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
851
      }
852 bfe45b4a Leszek Koltunski
    }
853
854
///////////////////////////////////////////////////////////////////////////////////////////////////
855
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists.
856
857 7602a827 Leszek Koltunski
  static void oitRender(InternalOutputSurface surface, float corrW, float corrH)
858 bfe45b4a Leszek Koltunski
    {
859 f482efa3 Leszek Koltunski
    if( mOITRenderProgram!=null )
860
      {
861
      mOITRenderProgram.useProgram();
862
863
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
864
      GLES30.glUniform2f(mOITRenderTexCorrH, corrW, corrH );
865
      GLES30.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight);
866
      GLES30.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear);
867
      GLES30.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
868
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
869
      }
870 bfe45b4a Leszek Koltunski
    }
871
872
///////////////////////////////////////////////////////////////////////////////////////////////////
873
874
  static void setSSBOSize(float size)
875
    {
876
    mBufferSize = size;
877
    }
878
879 5f35f1cb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
880
881
  static int getQueueSize()
882
    {
883
    return mFBOQueueSize;
884
    }
885
886 edea9cf3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
887
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
888 b7074bc6 Leszek Koltunski
// PowerVR GE8100 / GE8300 compiler fails to compile OIT programs.
889 edea9cf3 Leszek Koltunski
890 5f35f1cb Leszek Koltunski
  private static void detectBuggyDriversAndSetQueueSize(int queueSize)
891 edea9cf3 Leszek Koltunski
    {
892 b7074bc6 Leszek Koltunski
    String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
893
    String version = GLES30.glGetString(GLES30.GL_VERSION);
894
    String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
895 edea9cf3 Leszek Koltunski
896 5f35f1cb Leszek Koltunski
    mFBOQueueSize = 1;
897 edea9cf3 Leszek Koltunski
898
    if( vendor.contains("ARM") )
899
      {
900 5f35f1cb Leszek Koltunski
      try
901 edea9cf3 Leszek Koltunski
        {
902 5f35f1cb Leszek Koltunski
        String regex = ".*r(\\d+)p\\d.*";
903
        Pattern pattern = Pattern.compile(regex);
904
        Matcher matcher = pattern.matcher(version);
905
906
        if( matcher.find() )
907
          {
908
          String driverVersion = matcher.group(1);
909
910
          if( driverVersion!=null )
911
            {
912
            int drvVersion = Integer.parseInt(driverVersion);
913
914
            if( drvVersion<22 )
915
              {
916
              Log.e("DISTORTED", "You are running this on a ARM Mali driver r"+driverVersion+".\n" +
917
                    "This is a buggy driver, please update to r22. Inserting workaround which uses a lot of memory.");
918
919
              mFBOQueueSize = queueSize;
920
              }
921
            }
922
          }
923
        }
924
      catch(Exception ex)
925
        {
926
        android.util.Log.e("library", "exception trying to pattern match version: "+ex.toString());
927 edea9cf3 Leszek Koltunski
        }
928
      }
929
    else if( vendor.contains("Imagination") )
930
      {
931
      if( renderer.contains("GE8") )
932
        {
933 b7074bc6 Leszek Koltunski
        Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
934 a79650a2 Leszek Koltunski
        Log.e("DISTORTED", "GLSL Version "+GLES30.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
935 edea9cf3 Leszek Koltunski
        }
936
      }
937 97b6c85e Leszek Koltunski
    else if( vendor.contains("Qualcomm"))
938
      {
939
      if( version.contains("V@331.0") )
940
        {
941
        Log.e("DISTORTED", "You are running this on an Adreno 3xx driver version 331.\nStrange shit might happen.");
942 eff6e3d3 Leszek Koltunski
        mBuggyUBOs = true;
943 97b6c85e Leszek Koltunski
        }
944
      }
945 edea9cf3 Leszek Koltunski
    }
946
947 b7074bc6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
948
/**
949
 * Return OpenGL ES version supported by the hardware we are running on.
950 60fdf71d Leszek Koltunski
 * There are only three possibilities: 300 (OpenGL ES 3.0) or 310 (at least OpenGL ES 3.1)
951
 * or 200 (OpenGL ES 2.0)
952 b7074bc6 Leszek Koltunski
 */
953 60fdf71d Leszek Koltunski
  public static int getGLSL()
954 b7074bc6 Leszek Koltunski
    {
955
    return mGLSL;
956
    }
957
958 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
959
/**
960 c43abe6c Leszek Koltunski
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
961 9519d1b1 Leszek Koltunski
 * I.e. best called from GLSurfaceView.Renderer.onSurfaceCreated().
962 6a06a912 Leszek Koltunski
 * <p>
963 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
964 5f35f1cb Leszek Koltunski
 *
965 9ec374e8 Leszek Koltunski
 * @param context  Context of the App using the library - used to open up Resources and read Shader code.
966 d99fcc9c Leszek Koltunski
 * @param listener The library will send all (asynchronous!) exceptions there.
967 6a06a912 Leszek Koltunski
 */
968 9519d1b1 Leszek Koltunski
  public static void onSurfaceCreated(final Context context, final ExceptionListener listener)
969 5f35f1cb Leszek Koltunski
    {
970 9519d1b1 Leszek Koltunski
    onSurfaceCreated(context,listener,4);
971 5f35f1cb Leszek Koltunski
    }
972
973
///////////////////////////////////////////////////////////////////////////////////////////////////
974
/**
975
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
976 9519d1b1 Leszek Koltunski
 * I.e. best called from GLSurfaceView.Renderer.onSurfaceCreated().
977 5f35f1cb Leszek Koltunski
 * <p>
978
 * Needs to be called from a thread holding the OpenGL context.
979
 *   
980 9ec374e8 Leszek Koltunski
 * @param context   Context of the App using the library - used to open up Resources and read Shader code.
981
 * @param listener  The library will send all (asynchronous!) exceptions there.
982 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
983 5f35f1cb Leszek Koltunski
 */
984 9519d1b1 Leszek Koltunski
  public static void onSurfaceCreated(final Context context, final ExceptionListener listener, int queueSize)
985 d6e94c84 Leszek Koltunski
    {
986 30beb34f Leszek Koltunski
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
987
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
988 b7074bc6 Leszek Koltunski
989
    int glESversion = configurationInfo.reqGlEsVersion;
990
    int major = glESversion >> 16;
991
    int minor = glESversion & 0xff;
992
993 d99fcc9c Leszek Koltunski
    mListener = listener;
994
995 b7074bc6 Leszek Koltunski
    if( major< 3 )
996
      {
997
      mGLSL = 100*major + 10*minor;
998 d99fcc9c Leszek Koltunski
      VertexCompilationException ex = new VertexCompilationException("at least OpenGL ES 3.0 required, this device supports only "+major+"."+minor);
999
      mListener.distortedException(ex);
1000 b7074bc6 Leszek Koltunski
      }
1001
    else
1002
      {
1003
      mGLSL = (major==3 && minor==0) ? 300 : 310;
1004
      }
1005
1006 78ff6ea9 Leszek Koltunski
    int[] tmp = new int[1];
1007
    GLES30.glGetIntegerv(GLES30.GL_MAX_TEXTURE_SIZE, tmp, 0);
1008
    mMaxTextureSize = tmp[0];
1009
    GLES30.glGetIntegerv(GLES30.GL_MAX_VERTEX_UNIFORM_VECTORS  , tmp, 0);
1010
    mMaxNumberOfVerUniforms = tmp[0];
1011
    GLES30.glGetIntegerv(GLES30.GL_MAX_FRAGMENT_UNIFORM_VECTORS, tmp, 0);
1012
    mMaxNumberOfFraUniforms = tmp[0];
1013 2bd3d0fb Leszek Koltunski
1014 b7074bc6 Leszek Koltunski
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+major+"."+minor);
1015 8f011621 Leszek Koltunski
/*
1016 78ff6ea9 Leszek Koltunski
    android.util.Log.e("DISTORTED", "max texture size: "+mMaxTextureSize);
1017
    android.util.Log.e("DISTORTED", "max num vert: "+mMaxNumberOfVerUniforms);
1018
    android.util.Log.e("DISTORTED", "max num frag: "+mMaxNumberOfFraUniforms);
1019 8f011621 Leszek Koltunski
*/
1020 b7074bc6 Leszek Koltunski
    mGLSL_VERSION = "#version "+mGLSL+" es\n";
1021 edea9cf3 Leszek Koltunski
1022 9ec374e8 Leszek Koltunski
    InternalStackFrameList.setInitialized(true);
1023 d99fcc9c Leszek Koltunski
    mOITCompilationAttempted = false;
1024 edea9cf3 Leszek Koltunski
1025 5f35f1cb Leszek Koltunski
    detectBuggyDriversAndSetQueueSize(queueSize);
1026 0e924ba5 Leszek Koltunski
    EffectMessageSender.startSending();
1027
1028 d99fcc9c Leszek Koltunski
    mResources = context.getResources();
1029 edea9cf3 Leszek Koltunski
1030
    try
1031
      {
1032 d99fcc9c Leszek Koltunski
      createMainProgram();
1033 edea9cf3 Leszek Koltunski
      }
1034
    catch(Exception ex)
1035
      {
1036 d99fcc9c Leszek Koltunski
      mListener.distortedException(ex);
1037 edea9cf3 Leszek Koltunski
      }
1038
1039 f046b159 Leszek Koltunski
    try
1040
      {
1041 d99fcc9c Leszek Koltunski
      EffectQueuePostprocess.createPrograms(mResources, mGLSL);
1042 f046b159 Leszek Koltunski
      }
1043
    catch(Exception ex)
1044
      {
1045 d99fcc9c Leszek Koltunski
      mListener.distortedException(ex);
1046 a13dde77 Leszek Koltunski
      }
1047
1048 edea9cf3 Leszek Koltunski
    try
1049
      {
1050 b7074bc6 Leszek Koltunski
      PostprocessEffect.createPrograms(mGLSL);
1051 edea9cf3 Leszek Koltunski
      }
1052
    catch(Exception ex)
1053
      {
1054 d99fcc9c Leszek Koltunski
      mListener.distortedException(ex);
1055 edea9cf3 Leszek Koltunski
      }
1056 6a06a912 Leszek Koltunski
    }
1057
1058 9ec374e8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1059
/**
1060
 * Call this so that the Library can initialize its internal data structures.
1061
 * Must be called from Activity.onCreate().
1062
 */
1063
  public static void onCreate()
1064
    {
1065
    onCreate(0);
1066
    }
1067
1068
///////////////////////////////////////////////////////////////////////////////////////////////////
1069
/**
1070
 * Call this so that the Library can initialize its internal data structures.
1071
 * Must be called from Activity.onCreate().
1072
 *
1073
 * @param id id of an Activity that is using the library; anything unique so that the Library can
1074
 *           tell between Activities in case you're going to be using it from more than one.
1075
 */
1076
  public static void onCreate(long id)
1077
    {
1078
    InternalStackFrameList.onCreate(id);
1079
    }
1080
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082
/**
1083
 * Call this so that the Library can resume its operations.
1084
 * Must be called from Activity.onResume().
1085
 */
1086
  public static void onResume()
1087
    {
1088
    onCreate(0);
1089
    }
1090
1091
///////////////////////////////////////////////////////////////////////////////////////////////////
1092
/**
1093
 * Call this so that the Library can resume its operations.
1094
 * Must be called from Activity.onResume().
1095
 *
1096
 * @param id id of an Activity that is using the library; anything unique so that the Library can
1097
 *           tell between Activities in case you're going to be using it from more than one.
1098
 */
1099
  public static void onResume(long id)
1100
    {
1101
    InternalStackFrameList.onResume(id);
1102
    }
1103
1104 05ecc6fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1105
/**
1106 78e89fb5 Leszek Koltunski
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
1107 05ecc6fe Leszek Koltunski
 * Must be called from Activity.onPause().
1108
 */
1109
  public static void onPause()
1110
    {
1111 9ec374e8 Leszek Koltunski
    onPause(0);
1112
    }
1113
1114
///////////////////////////////////////////////////////////////////////////////////////////////////
1115
/**
1116
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
1117
 * Must be called from Activity.onPause().
1118
 *
1119
 * @param id id of an Activity that is using the library; anything unique so that the Library can
1120
 *           tell between Activities in case you're going to be using it from more than one.
1121
 */
1122
  public static void onPause(long id)
1123
    {
1124
    InternalStackFrameList.onPause(id);
1125 30094332 Leszek Koltunski
    Dynamic.onPause();  // common for all frames
1126 c8dbce40 Leszek Koltunski
1127 bfe45b4a Leszek Koltunski
    mLinkedListSSBO[0]= -1;
1128 5f35f1cb Leszek Koltunski
    mAtomicCounter = null;
1129 fd836a41 Leszek Koltunski
1130 d99fcc9c Leszek Koltunski
    mNormalProgram     = null;
1131
    mMainOITProgram    = null;
1132
    mMainProgram       = null;
1133
    mFullProgram       = null;
1134
    mOITClearProgram   = null;
1135
    mOITBuildProgram   = null;
1136
    mOITCollapseProgram= null;
1137
    mOITRenderProgram  = null;
1138
    mBlitDepthProgram  = null;
1139
    mBlitProgram       = null;
1140 6a06a912 Leszek Koltunski
    }
1141 bfe45b4a Leszek Koltunski
1142 c8dbce40 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1143
/**
1144
 * Call this so that the Library can release its internal data structures.
1145 9ec374e8 Leszek Koltunski
 * Must be called from Activity.onDestroy().
1146 c8dbce40 Leszek Koltunski
 */
1147
  public static void onDestroy()
1148
    {
1149 9ec374e8 Leszek Koltunski
    onDestroy(0);
1150
    }
1151
1152
///////////////////////////////////////////////////////////////////////////////////////////////////
1153
/**
1154
 * Call this so that the Library can release its internal data structures.
1155
 * Must be called from Activity.onDestroy().
1156
 *
1157
 * @param id id of an Activity that is using the library; anything unique so that the Library can
1158
 *           tell between Activities in case you're going to be using it from more than one.
1159
 */
1160
  public static void onDestroy(long id)
1161
    {
1162
    if( InternalStackFrameList.isInitialized() )
1163 c8dbce40 Leszek Koltunski
      {
1164 9ec374e8 Leszek Koltunski
      InternalStackFrameList.onDestroy(id);
1165 3bbe4d67 Leszek Koltunski
1166
      InternalOutputSurface.onDestroy(); // those three really destroy
1167
      Effect.onDestroy();                // static data that does not
1168
      DeferredJobs.onDestroy();          // need to be part of a frame
1169 30094332 Leszek Koltunski
1170
      mOITCompilationAttempted = false;
1171 c8dbce40 Leszek Koltunski
      }
1172
    }
1173
1174 2bd3d0fb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1175
/**
1176
 * Return the maximum size of the texture supported by the driver.
1177
 */
1178
  public static int getMaxTextureSize()
1179
    {
1180
    return mMaxTextureSize;
1181
    }
1182
1183 97b6c85e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1184
/**
1185
 * Call this before calling onSurfaceCreated() if you want to access normal vectors in CPU.
1186
 */
1187
  public static void needTransformFeedback()
1188
    {
1189
    mNeedsTransformFeedback = true;
1190
    }
1191
1192 bfe45b4a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1193
/**
1194
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
1195
 * single (InputSurface,MeshBase) combo.
1196
 *
1197
 * @param type {@link EffectType}
1198
 * @return The maximum number of effects of a given type.
1199
 */
1200
  @SuppressWarnings("unused")
1201
  public static int getMax(EffectType type)
1202
    {
1203
    return EffectQueue.getMax(type.ordinal());
1204
    }
1205
1206
///////////////////////////////////////////////////////////////////////////////////////////////////
1207
/**
1208
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
1209
 * This can fail if:
1210
 * <ul>
1211
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
1212
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
1213 9519d1b1 Leszek Koltunski
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link DistortedLibrary#onSurfaceCreated}. After this
1214 bfe45b4a Leszek Koltunski
 *     time only decreasing the value of 'max' is permitted.
1215
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
1216
 * </ul>
1217
 *
1218
 * @param type {@link EffectType}
1219
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
1220
 *            than Byte.MAX_VALUE
1221
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
1222
 */
1223
  @SuppressWarnings("unused")
1224
  public static boolean setMax(EffectType type, int max)
1225
    {
1226
    return EffectQueue.setMax(type.ordinal(),max);
1227
    }
1228 f8f6d457 leszek
  }