Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// 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
// Distorted is distributed in the hope that it will be useful,                                  //
12
// 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
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.main;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.opengl.GLES30;
27
import android.opengl.GLES31;
28
import android.util.Log;
29

    
30
import org.distorted.library.R;
31
import org.distorted.library.effect.Effect;
32
import org.distorted.library.effectqueue.EffectQueue;
33
import org.distorted.library.effectqueue.EffectQueuePostprocess;
34
import org.distorted.library.effect.EffectType;
35
import org.distorted.library.effect.FragmentEffect;
36
import org.distorted.library.effect.PostprocessEffect;
37
import org.distorted.library.effect.VertexEffect;
38
import org.distorted.library.mesh.MeshBase;
39
import org.distorted.library.message.EffectMessageSender;
40
import org.distorted.library.program.DistortedProgram;
41
import org.distorted.library.program.VertexCompilationException;
42

    
43
import java.io.InputStream;
44
import java.nio.ByteBuffer;
45
import java.nio.ByteOrder;
46
import java.nio.FloatBuffer;
47
import java.nio.IntBuffer;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
/**
51
 * A singleton class used to control various global dialog_settings.
52
 */
53
public class DistortedLibrary
54
  {
55
  private static int mGLSL;
56
  private static String mGLSL_VERSION;
57
  private static boolean mOITCompilationSuccessful;
58
  /**
59
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
60
   * backing up our DistortedTexture.
61
   * <p>
62
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
63
   * apply different effects. Used in the copy constructor.
64
   */
65
  public static final int CLONE_SURFACE = 0x1;
66
  /**
67
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
68
   * <p>
69
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
70
   */
71
  public static final int CLONE_MATRIX = 0x2;
72
  /**
73
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
74
   * <p>
75
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
76
   */
77
  public static final int CLONE_VERTEX  = 0x4;
78
  /**
79
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
80
   * <p>
81
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
82
   */
83
  public static final int CLONE_FRAGMENT= 0x8;
84
   /**
85
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
86
   * <p>
87
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
88
   */
89
  public static final int CLONE_POSTPROCESS= 0x10;
90
  /**
91
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
92
   * <p>
93
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
94
   * on the screen with (optionally) different Effects.
95
   */
96
  public static final int CLONE_CHILDREN= 0x20;
97

    
98
  /**
99
   * Work around bugs in ARM Mali driver by, instead to a single FBO, rendering to a circular queue
100
   * of FBO_QUEUE_SIZE FBOs. (otherwise we sometimes get a 'full pipeline flush' and the end result
101
   * might be missing part of the Objects)
102
   *
103
   * This bug only exists on Mali driver r12.
104
   *
105
   * https://community.arm.com/graphics/f/discussions/10285/opengl-es-3-1-on-mali-t880-flashes
106
   */
107
  static final int FBO_QUEUE_SIZE = 4;
108

    
109
  private static boolean mInitialized=false;
110

    
111
  //////////////////////////////////////////////////////////////////////////////////////////////
112
  /// MAIN PROGRAM ///
113
  private static DistortedProgram mMainProgram;
114
  private static int mMainTextureH;
115

    
116
  /// NORMAL PROGRAM /////
117
  private static DistortedProgram mNormalProgram;
118
  private static int mNormalMVPMatrixH;
119

    
120
  /// MAIN OIT PROGRAM ///
121
  private static DistortedProgram mMainOITProgram;
122
  private static int mMainOITTextureH;
123
  private static int mMainOITSizeH;
124
  private static int mMainOITNumRecordsH;
125

    
126
  /// BLIT PROGRAM ///
127
  private static DistortedProgram mBlitProgram;
128
  private static int mBlitTextureH;
129
  private static int mBlitDepthH;
130
  private static final FloatBuffer mQuadPositions;
131

    
132
  /// FULL PROGRAM ///
133
  private static DistortedProgram mFullProgram;
134

    
135
  static
136
    {
137
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
138
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
139
    mQuadPositions.put(positionData).position(0);
140
    }
141

    
142
  /// BLIT DEPTH PROGRAM ///
143
  private static DistortedProgram mBlitDepthProgram;
144
  private static int mBlitDepthTextureH;
145
  private static int mBlitDepthDepthTextureH;
146
  private static int mBlitDepthDepthH;
147
  private static int mBlitDepthTexCorrH;
148

    
149
  /// OIT SSBO BUFFER ///
150
  private static int[] mLinkedListSSBO = new int[1];
151
  private static int[] mAtomicCounter = new int[DistortedLibrary.FBO_QUEUE_SIZE];
152
  private static int   mCurrBuffer;
153

    
154
  static
155
    {
156
    mLinkedListSSBO[0]= -1;
157
    mCurrBuffer       =  0;
158

    
159
    for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++)  mAtomicCounter[i] = -1;
160
    }
161

    
162
  ///////////////////////////////////////////////////////////////
163
  // meaning: allocate 1.0 screenful of places for transparent
164
  // fragments in the SSBO backing up the OIT render method.
165
  private static float mBufferSize=1.0f;
166

    
167
  /// OIT CLEAR PROGRAM ///
168
  private static DistortedProgram mOITClearProgram;
169
  private static int mOITClearDepthH;
170
  private static int mOITClearTexCorrH;
171
  private static int mOITClearSizeH;
172

    
173
  /// OIT BUILD PROGRAM ///
174
  private static DistortedProgram mOITBuildProgram;
175
  private static int mOITBuildTextureH;
176
  private static int mOITBuildDepthTextureH;
177
  private static int mOITBuildDepthH;
178
  private static int mOITBuildTexCorrH;
179
  private static int mOITBuildSizeH;
180
  private static int mOITBuildNumRecordsH;
181

    
182
  /// OIT COLLAPSE PROGRAM ///
183
  private static DistortedProgram mOITCollapseProgram;
184
  private static int mOITCollapseDepthTextureH;
185
  private static int mOITCollapseDepthH;
186
  private static int mOITCollapseTexCorrH;
187
  private static int mOITCollapseSizeH;
188

    
189
  /// OIT RENDER PROGRAM ///
190
  private static DistortedProgram mOITRenderProgram;
191
  private static int mOITRenderDepthH;
192
  private static int mOITRenderTexCorrH;
193
  private static int mOITRenderSizeH;
194

    
195
  /// END PROGRAMS //////
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
// private: hide this from Javadoc
199

    
200
  private DistortedLibrary()
201
    {
202

    
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  private static void createPrograms(Resources resources)
208
    {
209
    // MAIN PROGRAM ////////////////////////////////////
210
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
211
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
212

    
213
    int numF = FragmentEffect.getNumEnabled();
214
    int numV = VertexEffect.getNumEnabled();
215

    
216
    String mainVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
217
    String mainFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
218
    String enabledEffectV= VertexEffect.getGLSL();
219
    String enabledEffectF= FragmentEffect.getGLSL();
220

    
221
    String[] feedback = { "v_Position", "v_endPosition" };
222

    
223
    try
224
      {
225
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
226
                                          enabledEffectV, enabledEffectF, mGLSL, feedback);
227
      }
228
    catch(Exception e)
229
      {
230
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
231
      throw new RuntimeException(e.getMessage());
232
      }
233

    
234
    int mainProgramH = mMainProgram.getProgramHandle();
235
    EffectQueue.getUniforms(mainProgramH,0);
236
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
237

    
238
    // BLIT PROGRAM ////////////////////////////////////
239
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
240
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
241

    
242
    try
243
      {
244
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
245
      }
246
    catch(Exception e)
247
      {
248
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
249
      throw new RuntimeException(e.getMessage());
250
      }
251

    
252
    int blitProgramH = mBlitProgram.getProgramHandle();
253
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
254
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
255

    
256
    // BLIT DEPTH PROGRAM ////////////////////////////////////
257
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
258
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
259

    
260
    try
261
      {
262
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
263
      }
264
    catch(Exception e)
265
      {
266
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
267
      throw new RuntimeException(e.getMessage());
268
      }
269

    
270
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
271
    mBlitDepthTextureH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Texture");
272
    mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
273
    mBlitDepthDepthH        = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
274
    mBlitDepthTexCorrH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
275

    
276
    // NORMAL PROGRAM //////////////////////////////////////
277
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
278
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
279

    
280
    try
281
      {
282
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
283
      }
284
    catch(Exception e)
285
      {
286
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
287
      throw new RuntimeException(e.getMessage());
288
      }
289

    
290
    int normalProgramH = mNormalProgram.getProgramHandle();
291
    mNormalMVPMatrixH  = GLES30.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  private static void createFullProgram(Resources resources)
297
    {
298
    final InputStream fullVertStream = resources.openRawResource(R.raw.main_vertex_shader);
299
    final InputStream fullFragStream = resources.openRawResource(R.raw.main_fragment_shader);
300

    
301
    int numV = VertexEffect.getAllEnabled();
302

    
303
    String fullVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n");
304
    String fullFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " +                                         0   + "\n");
305
    String enabledEffectV= VertexEffect.getAllGLSL();
306
    String enabledEffectF= "{}";
307

    
308
    fullVertHeader += "#define PREAPPLY\n";
309

    
310
    String[] feedback = { "v_Position", "v_endPosition", "v_Inflate" };
311

    
312
    try
313
      {
314
      mFullProgram = new DistortedProgram(fullVertStream, fullFragStream, fullVertHeader, fullFragHeader,
315
                                          enabledEffectV, enabledEffectF, mGLSL, feedback);
316
      }
317
    catch(Exception e)
318
      {
319
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile FULL program: "+e.getMessage());
320
      throw new RuntimeException(e.getMessage());
321
      }
322

    
323
    int fullProgramH = mFullProgram.getProgramHandle();
324
    EffectQueue.getUniforms(fullProgramH,3);
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  private static void createProgramsOIT(Resources resources)
330
    {
331
    // MAIN OIT PROGRAM ////////////////////////////////
332
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
333
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
334

    
335
    int numF = FragmentEffect.getNumEnabled();
336
    int numV = VertexEffect.getNumEnabled();
337

    
338
    String mainVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n") + ("#define OIT\n");
339
    String mainFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n") + ("#define OIT\n");
340

    
341
    String enabledEffectV= VertexEffect.getGLSL();
342
    String enabledEffectF= FragmentEffect.getGLSL();
343

    
344
    try
345
      {
346
      mMainOITProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
347
                                             enabledEffectV, enabledEffectF, mGLSL, null);
348
      }
349
    catch(Exception e)
350
      {
351
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN OIT program: "+e.getMessage());
352
      throw new RuntimeException(e.getMessage());
353
      }
354

    
355
    int mainOITProgramH = mMainOITProgram.getProgramHandle();
356
    EffectQueue.getUniforms(mainOITProgramH,1);
357
    mMainOITTextureH    = GLES30.glGetUniformLocation( mainOITProgramH, "u_Texture");
358
    mMainOITSizeH       = GLES30.glGetUniformLocation( mainOITProgramH, "u_Size");
359
    mMainOITNumRecordsH = GLES30.glGetUniformLocation( mainOITProgramH, "u_numRecords");
360

    
361
    // OIT CLEAR PROGRAM ////////////////////////////////////
362
    final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
363
    final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader);
364

    
365
    try
366
      {
367
      mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
368
      }
369
    catch(Exception e)
370
      {
371
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage());
372
      throw new RuntimeException(e.getMessage());
373
      }
374

    
375
    int oitClearProgramH   = mOITClearProgram.getProgramHandle();
376
    mOITClearDepthH        = GLES30.glGetUniformLocation( oitClearProgramH, "u_Depth");
377
    mOITClearTexCorrH      = GLES30.glGetUniformLocation( oitClearProgramH, "u_TexCorr");
378
    mOITClearSizeH         = GLES30.glGetUniformLocation( oitClearProgramH, "u_Size");
379

    
380
    // OIT BUILD PROGRAM ////////////////////////////////////
381
    final InputStream oitBuildVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
382
    final InputStream oitBuildFragStream = resources.openRawResource(R.raw.oit_build_fragment_shader);
383

    
384
    try
385
      {
386
      mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
387
      }
388
    catch(Exception e)
389
      {
390
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage());
391
      throw new RuntimeException(e.getMessage());
392
      }
393

    
394
    int oitBuildProgramH   = mOITBuildProgram.getProgramHandle();
395
    mOITBuildTextureH      = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Texture");
396
    mOITBuildDepthTextureH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture");
397
    mOITBuildDepthH        = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Depth");
398
    mOITBuildTexCorrH      = GLES30.glGetUniformLocation( oitBuildProgramH, "u_TexCorr");
399
    mOITBuildSizeH         = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Size");
400
    mOITBuildNumRecordsH   = GLES30.glGetUniformLocation( oitBuildProgramH, "u_numRecords");
401

    
402
    // OIT COLLAPSE PROGRAM ///////////////////////////
403
    final InputStream oitCollapseVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
404
    final InputStream oitCollapseFragStream = resources.openRawResource(R.raw.oit_collapse_fragment_shader);
405

    
406
    try
407
      {
408
      mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
409
      }
410
    catch(Exception e)
411
      {
412
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage());
413
      throw new RuntimeException(e.getMessage());
414
      }
415

    
416
    int oitCollapseProgramH   = mOITCollapseProgram.getProgramHandle();
417
    mOITCollapseDepthTextureH = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture");
418
    mOITCollapseDepthH        = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Depth");
419
    mOITCollapseTexCorrH      = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr");
420
    mOITCollapseSizeH         = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Size");
421

    
422
    // OIT RENDER PROGRAM ///////////////////////////
423
    final InputStream oitRenderVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
424
    final InputStream oitRenderFragStream = resources.openRawResource(R.raw.oit_render_fragment_shader);
425

    
426
    try
427
      {
428
      mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
429
      }
430
    catch(Exception e)
431
      {
432
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage());
433
      throw new RuntimeException(e.getMessage());
434
      }
435

    
436
    int oitRenderProgramH   = mOITRenderProgram.getProgramHandle();
437
    mOITRenderDepthH        = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Depth");
438
    mOITRenderTexCorrH      = GLES30.glGetUniformLocation( oitRenderProgramH, "u_TexCorr");
439
    mOITRenderSizeH         = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Size");
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
  private static void displayNormals(EffectQueue[] queues, MeshBase mesh)
445
    {
446
    int num = mesh.getNumVertices();
447
    int tfo = mesh.getTFO();
448

    
449
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
450
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
451
    InternalRenderState.switchOffDrawing();
452
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
453
    InternalRenderState.restoreDrawing();
454
    GLES30.glEndTransformFeedback();
455
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
456

    
457
    mNormalProgram.useProgram();
458
    GLES30.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, EffectQueue.getMVP(queues) , 0);
459
    mesh.bindTransformAttribs(mNormalProgram);
460
    GLES30.glLineWidth(8.0f);
461
    GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*num);
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465
// execute all VertexEffects and adjust all vertices
466

    
467
  public static void adjustVertices(MeshBase mesh)
468
    {
469
    if( mFullProgram!=null )
470
      {
471
      GLES30.glViewport(0, 0, 500, 500 ); // TODO ???
472

    
473
      int num = mesh.getNumVertices();
474
      int tfo = mesh.getTFO();
475

    
476
      mFullProgram.useProgram();
477
      mesh.bindVertexAttribs(mFullProgram);
478
      mesh.computeQueue();
479
      mesh.sendQueue();
480

    
481
      GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
482
      GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
483
      InternalRenderState.switchOffDrawing();
484
      GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
485
      InternalRenderState.restoreDrawing();
486
      GLES30.glEndTransformFeedback();
487
      mesh.copyTransformToVertex();
488
      GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
489
      }
490
    }
491

    
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493

    
494
  static void drawPrivOIT(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
495
    {
496
    EffectQueue[] queues = effects.getQueues();
497

    
498
    EffectQueue.compute(queues, currTime);
499
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
500

    
501
    DistortedLibrary.mMainOITProgram.useProgram();
502
    GLES30.glUniform1i(DistortedLibrary.mMainOITTextureH, 0);
503
    GLES30.glUniform2ui(DistortedLibrary.mMainOITSizeH, surface.mWidth, surface.mHeight);
504
    GLES30.glUniform1ui(DistortedLibrary.mMainOITNumRecordsH, (int)(DistortedLibrary.mBufferSize*surface.mWidth*surface.mHeight) );
505
    mesh.bindVertexAttribs(DistortedLibrary.mMainOITProgram);
506

    
507
    float inflate     = mesh.getInflate();
508
    float distance    = surface.mDistance;
509
    float mipmap      = surface.mMipmap;
510
    float[] projection= surface.mProjectionMatrix;
511

    
512
    EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 1 );
513
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
514

    
515
    if( mesh.getShowNormals() )
516
      {
517
      DistortedLibrary.mMainProgram.useProgram();
518
      EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 0 );
519
      displayNormals(queues,mesh);
520
      }
521
    }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
  static void drawPriv(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
526
    {
527
    if( mMainProgram!=null )
528
      {
529
      EffectQueue[] queues = effects.getQueues();
530

    
531
      EffectQueue.compute(queues, currTime);
532
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
533

    
534
      mMainProgram.useProgram();
535
      GLES30.glUniform1i(DistortedLibrary.mMainTextureH, 0);
536
      mesh.bindVertexAttribs(DistortedLibrary.mMainProgram);
537

    
538
      float inflate     = mesh.getInflate();
539
      float distance    = surface.mDistance;
540
      float mipmap      = surface.mMipmap;
541
      float[] projection= surface.mProjectionMatrix;
542

    
543
      EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 0 );
544
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
545

    
546
      if( mesh.getShowNormals() ) displayNormals(queues,mesh);
547
      }
548
    }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551

    
552
  static void blitPriv(InternalOutputSurface surface)
553
    {
554
    if( mBlitProgram!=null )
555
      {
556
      mBlitProgram.useProgram();
557

    
558
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
559
      GLES30.glUniform1i(mBlitTextureH, 0);
560
      GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
561
      GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
562
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
563
      }
564
    }
565

    
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567

    
568
  static void blitDepthPriv(InternalOutputSurface surface, float corrW, float corrH)
569
    {
570
    if( mBlitDepthProgram!=null )
571
      {
572
      mBlitDepthProgram.useProgram();
573

    
574
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
575
      GLES30.glUniform1i(mBlitDepthTextureH, 0);
576
      GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
577
      GLES30.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
578
      GLES30.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
579
      GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
580
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
581
      }
582
    }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585
// yes it is safe to be mixing 3.0 and 3.1 like that, senior members of the OpenGL discussions forum assert
586

    
587
  private static int printPreviousBuffer()
588
    {
589
    int counter = 0;
590

    
591
    ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
592
                                                                GLES30.GL_MAP_READ_BIT);
593
    if( atomicBuf!=null )
594
      {
595
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
596
      counter = atomicIntBuf.get(0);
597
      }
598
    else
599
      {
600
      Log.e("effects", "print: failed to map atomic buffer");
601
      }
602

    
603
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
604

    
605
    return counter;
606
    }
607

    
608
///////////////////////////////////////////////////////////////////////////////////////////////////
609

    
610
  private static void zeroBuffer()
611
    {
612
    ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
613
        GLES30.GL_MAP_WRITE_BIT|GLES30.GL_MAP_INVALIDATE_BUFFER_BIT);
614
    if( atomicBuf!=null )
615
      {
616
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
617
      atomicIntBuf.put(0,0);
618
      }
619
    else
620
      {
621
      Log.e("effects", "zero: failed to map atomic buffer");
622
      }
623

    
624
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
625
    }
626

    
627
///////////////////////////////////////////////////////////////////////////////////////////////////
628
// reset atomic counter to 0
629

    
630
  static int zeroOutAtomic()
631
    {
632
    int counter = 0;
633

    
634
    if( mAtomicCounter[0]<0 )
635
      {
636
      GLES30.glGenBuffers(DistortedLibrary.FBO_QUEUE_SIZE,mAtomicCounter,0);
637

    
638
      for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++)
639
        {
640
        GLES30.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]);
641
        GLES30.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES30.GL_DYNAMIC_DRAW);
642
        zeroBuffer();
643
        }
644
      }
645

    
646
    // reading the value of the buffer on every frame would slow down rendering by
647
    // dialog_about 3%; doing it only once every 5 frames affects speed by less than 1%.
648
    if( mCurrBuffer==0 )
649
      {
650
      GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
651
      counter = printPreviousBuffer();
652
      }
653

    
654
    if( ++mCurrBuffer>= DistortedLibrary.FBO_QUEUE_SIZE ) mCurrBuffer = 0;
655

    
656
    GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
657
    zeroBuffer();
658

    
659
    return counter;
660
    }
661

    
662
///////////////////////////////////////////////////////////////////////////////////////////////////
663
// Pass1 of the OIT algorithm. Clear per-pixel head-pointers.
664

    
665
  static void oitClear(InternalOutputSurface surface, int counter)
666
    {
667
    if( mOITClearProgram!=null )
668
      {
669
      if( mLinkedListSSBO[0]<0 )
670
        {
671
        GLES30.glGenBuffers(1,mLinkedListSSBO,0);
672

    
673
        int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
674
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
675
        GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
676
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
677

    
678
        GLES30.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
679
        }
680

    
681
      // See if we have overflown the SSBO in one of the previous frames.
682
      // If yes, assume we need to make the SSBO larger.
683
      float overflow = counter/(mBufferSize*surface.mWidth*surface.mHeight);
684

    
685
      if( overflow>1.0f )
686
        {
687
        //android.util.Log.e("effects", "previous frame rendered "+counter+
688
        //                   " fragments, but there was only "+(mBufferSize*surface.mWidth*surface.mHeight)+" space");
689

    
690
        mBufferSize *= (int)(overflow+1.0f);
691
        int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
692
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
693
        GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
694
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
695
        }
696

    
697
      mOITClearProgram.useProgram();
698

    
699
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
700
      GLES30.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f );   // corrections do not really matter here - only present because of common vertex shader.
701
      GLES30.glUniform1f( mOITClearDepthH , 1.0f);          // likewise depth
702
      GLES30.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight);
703
      GLES30.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
704
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
705
      }
706
    }
707

    
708
///////////////////////////////////////////////////////////////////////////////////////////////////
709
// Pass2 of the OIT algorithm - build per-pixel linked lists.
710

    
711
  static void oitBuild(InternalOutputSurface surface, float corrW, float corrH)
712
    {
713
    if( mOITBuildProgram!=null )
714
      {
715
      mOITBuildProgram.useProgram();
716

    
717
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
718
      GLES30.glUniform1i(mOITBuildTextureH, 0);
719
      GLES30.glUniform1i(mOITBuildDepthTextureH, 1);
720
      GLES30.glUniform2f(mOITBuildTexCorrH, corrW, corrH );
721
      GLES30.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight);
722
      GLES30.glUniform1ui(mOITBuildNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
723
      GLES30.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear);
724
      GLES30.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
725
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
726
      }
727
    }
728

    
729
///////////////////////////////////////////////////////////////////////////////////////////////////
730
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list.
731

    
732
  static void oitCollapse(InternalOutputSurface surface, float corrW, float corrH)
733
    {
734
    if( mOITCollapseProgram!=null )
735
      {
736
      mOITCollapseProgram.useProgram();
737

    
738
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
739
      GLES30.glUniform1i(mOITCollapseDepthTextureH, 1);
740
      GLES30.glUniform2f(mOITCollapseTexCorrH, corrW, corrH );
741
      GLES30.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight);
742
      GLES30.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear);
743
      GLES30.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
744
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
745
      }
746
    }
747

    
748
///////////////////////////////////////////////////////////////////////////////////////////////////
749
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists.
750

    
751
  static void oitRender(InternalOutputSurface surface, float corrW, float corrH)
752
    {
753
    if( mOITRenderProgram!=null )
754
      {
755
      mOITRenderProgram.useProgram();
756

    
757
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
758
      GLES30.glUniform2f(mOITRenderTexCorrH, corrW, corrH );
759
      GLES30.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight);
760
      GLES30.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear);
761
      GLES30.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
762
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
763
      }
764
    }
765

    
766
///////////////////////////////////////////////////////////////////////////////////////////////////
767

    
768
  static void setSSBOSize(float size)
769
    {
770
    mBufferSize = size;
771
    }
772

    
773
///////////////////////////////////////////////////////////////////////////////////////////////////
774
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
775
// PowerVR GE8100 / GE8300 compiler fails to compile OIT programs.
776

    
777
  private static void detectBuggyDrivers()
778
    {
779
    String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
780
    String version = GLES30.glGetString(GLES30.GL_VERSION);
781
    String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
782

    
783
    /*
784
    android.util.Log.e("DISTORTED", "GLSL Version "+GLES30.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
785
    android.util.Log.e("DISTORTED", "GL Version "  +GLES30.glGetString(GLES31.GL_VERSION));
786
    android.util.Log.e("DISTORTED", "GL Vendor "   +GLES30.glGetString(GLES31.GL_VENDOR));
787
    android.util.Log.e("DISTORTED", "GL Renderer " +GLES30.glGetString(GLES31.GL_RENDERER));
788
    */
789

    
790
    if( vendor.contains("ARM") )
791
      {
792
      if( version.contains("r12") )
793
        {
794
        Log.e("DISTORTED", "You are running this on a ARM Mali driver r12.\nThis is a buggy driver, please update to r22. Problems with flashing expected.");
795
        }
796
      }
797
    else if( vendor.contains("Imagination") )
798
      {
799
      if( renderer.contains("GE8") )
800
        {
801
        Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
802
        Log.e("DISTORTED", "GLSL Version "+GLES30.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
803
        }
804
      }
805
    }
806

    
807
///////////////////////////////////////////////////////////////////////////////////////////////////
808
/**
809
 * Return OpenGL ES version supported by the hardware we are running on.
810
 * There are only two possibilities: 300 (OpenGL ES 3.0) or 310 (at least OpenGL ES 3.1)
811
 */
812
  static int getGLSL()
813
    {
814
    return mGLSL;
815
    }
816

    
817
///////////////////////////////////////////////////////////////////////////////////////////////////
818
/**
819
 * Have we successfully managed to compile the OIT programs?
820
 * Some hardware ( Imagination GE8100 / 8300, driver build 1.8@4490469 present in my HTC phone and,
821
 * sadly, Amazon Fire Stick 4K ) fails to compile the shaders. See relevant thread in Imagination's
822
 * support forum:
823
 *
824
 * https://forums.imgtec.com/t/ge8100-in-htc-desire-12-android-7-1-1-fragment-shader-fails-to-compile/2708
825
 */
826
  public static boolean OITCompilationSuccessful()
827
    {
828
    return mOITCompilationSuccessful;
829
    }
830

    
831
///////////////////////////////////////////////////////////////////////////////////////////////////
832
/**
833
 * Have we called onCreate yet, ie have we initialized the library?
834
 * @return <code>true</code> if the library is initialized and ready for action.
835
 */
836
  public static boolean isInitialized()
837
    {
838
    return mInitialized;
839
    }
840

    
841
///////////////////////////////////////////////////////////////////////////////////////////////////
842
/**
843
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
844
 * I.e. best called from GLSurfaceView.onCreate().
845
 * <p>
846
 * Needs to be called from a thread holding the OpenGL context.
847
 *   
848
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
849
 */
850
  public static void onCreate(final Context context) throws Exception
851
    {
852
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
853
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
854

    
855
    int glESversion = configurationInfo.reqGlEsVersion;
856
    int major = glESversion >> 16;
857
    int minor = glESversion & 0xff;
858

    
859
    if( major< 3 )
860
      {
861
      mGLSL = 100*major + 10*minor;
862
      throw new VertexCompilationException("at least OpenGL ES 3.0 required, this device supports only "+major+"."+minor);
863
      }
864
    else
865
      {
866
      mGLSL = (major==3 && minor==0) ? 300 : 310;
867
      }
868

    
869
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+major+"."+minor);
870
    mGLSL_VERSION = "#version "+mGLSL+" es\n";
871

    
872
    mInitialized = true;
873
    mOITCompilationSuccessful = false;
874

    
875
    detectBuggyDrivers();
876

    
877
    EffectMessageSender.startSending();
878

    
879
    final Resources resources = context.getResources();
880

    
881
    Exception exception=null;
882

    
883
    try
884
      {
885
      createPrograms(resources);
886
      }
887
    catch(Exception ex)
888
      {
889
      exception = ex;
890
      }
891

    
892
    try
893
      {
894
      createFullProgram(resources);
895
      }
896
    catch(Exception ex)
897
      {
898
      exception = ex;
899
      }
900

    
901
    if( mGLSL>=310)
902
      {
903
      try
904
        {
905
        createProgramsOIT(resources);
906
        mOITCompilationSuccessful = true;
907
        }
908
      catch(Exception ex)
909
        {
910
        exception = ex;
911
        }
912
      }
913

    
914
    try
915
      {
916
      EffectQueuePostprocess.createPrograms(resources, mGLSL);
917
      }
918
    catch(Exception ex)
919
      {
920
      exception = ex;
921
      }
922

    
923
    try
924
      {
925
      PostprocessEffect.createPrograms(mGLSL);
926
      }
927
    catch(Exception ex)
928
      {
929
      exception = ex;
930
      }
931

    
932
    if( exception!=null)
933
      {
934
      throw exception;
935
      }
936
    }
937

    
938
///////////////////////////////////////////////////////////////////////////////////////////////////
939
/**
940
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
941
 * Must be called from Activity.onPause().
942
 */
943
  public static void onPause()
944
    {
945
    InternalObject.onPause();
946

    
947
    mLinkedListSSBO[0]= -1;
948

    
949
    for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++) mAtomicCounter[i] = -1;
950
    }
951

    
952
///////////////////////////////////////////////////////////////////////////////////////////////////
953
/**
954
 * Call this so that the Library can release its internal data structures.
955
 * Must be called from Activity.onDestroy(). 
956
 */
957
  public static void onDestroy()
958
    {
959
    InternalObject.onDestroy();
960
    InternalNodeData.onDestroy();
961
    InternalMaster.onDestroy();
962
    InternalOutputSurface.onDestroy();
963
    DistortedEffects.onDestroy();
964
    EffectQueue.onDestroy();
965
    Effect.onDestroy();
966
    EffectMessageSender.stopSending();
967

    
968
    mInitialized = false;
969
    }
970

    
971
///////////////////////////////////////////////////////////////////////////////////////////////////
972
/**
973
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
974
 * single (InputSurface,MeshBase) combo.
975
 *
976
 * @param type {@link EffectType}
977
 * @return The maximum number of effects of a given type.
978
 */
979
  @SuppressWarnings("unused")
980
  public static int getMax(EffectType type)
981
    {
982
    return EffectQueue.getMax(type.ordinal());
983
    }
984

    
985
///////////////////////////////////////////////////////////////////////////////////////////////////
986
/**
987
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
988
 * This can fail if:
989
 * <ul>
990
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
991
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
992
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link DistortedLibrary#onCreate}. After this
993
 *     time only decreasing the value of 'max' is permitted.
994
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
995
 * </ul>
996
 *
997
 * @param type {@link EffectType}
998
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
999
 *            than Byte.MAX_VALUE
1000
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
1001
 */
1002
  @SuppressWarnings("unused")
1003
  public static boolean setMax(EffectType type, int max)
1004
    {
1005
    return EffectQueue.setMax(type.ordinal(),max);
1006
    }
1007
  }
(3-3/14)