Project

General

Profile

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

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

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
  static
133
    {
134
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
135
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
136
    mQuadPositions.put(positionData).position(0);
137
    }
138

    
139
  /// BLIT DEPTH PROGRAM ///
140
  private static DistortedProgram mBlitDepthProgram;
141
  private static int mBlitDepthTextureH;
142
  private static int mBlitDepthDepthTextureH;
143
  private static int mBlitDepthDepthH;
144
  private static int mBlitDepthTexCorrH;
145

    
146
  /// OIT SSBO BUFFER ///
147
  private static int[] mLinkedListSSBO = new int[1];
148
  private static int[] mAtomicCounter = new int[DistortedLibrary.FBO_QUEUE_SIZE];
149
  private static int   mCurrBuffer;
150

    
151
  static
152
    {
153
    mLinkedListSSBO[0]= -1;
154
    mCurrBuffer       =  0;
155

    
156
    for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++)  mAtomicCounter[i] = -1;
157
    }
158

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

    
164
  /// OIT CLEAR PROGRAM ///
165
  private static DistortedProgram mOITClearProgram;
166
  private static int mOITClearDepthH;
167
  private static int mOITClearTexCorrH;
168
  private static int mOITClearSizeH;
169

    
170
  /// OIT BUILD PROGRAM ///
171
  private static DistortedProgram mOITBuildProgram;
172
  private static int mOITBuildTextureH;
173
  private static int mOITBuildDepthTextureH;
174
  private static int mOITBuildDepthH;
175
  private static int mOITBuildTexCorrH;
176
  private static int mOITBuildSizeH;
177
  private static int mOITBuildNumRecordsH;
178

    
179
  /// OIT COLLAPSE PROGRAM ///
180
  private static DistortedProgram mOITCollapseProgram;
181
  private static int mOITCollapseDepthTextureH;
182
  private static int mOITCollapseDepthH;
183
  private static int mOITCollapseTexCorrH;
184
  private static int mOITCollapseSizeH;
185

    
186
  /// OIT RENDER PROGRAM ///
187
  private static DistortedProgram mOITRenderProgram;
188
  private static int mOITRenderDepthH;
189
  private static int mOITRenderTexCorrH;
190
  private static int mOITRenderSizeH;
191

    
192
  /// END PROGRAMS //////
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
// private: hide this from Javadoc
196

    
197
  private DistortedLibrary()
198
    {
199

    
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

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

    
210
    int numF = FragmentEffect.getNumEnabled();
211
    int numV = VertexEffect.getNumEnabled();
212

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

    
218
    String[] feedback = { "v_Position", "v_endPosition" };
219

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

    
231
    int mainProgramH = mMainProgram.getProgramHandle();
232
    EffectQueue.getUniforms(mainProgramH,0);
233
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
234

    
235
    // BLIT PROGRAM ////////////////////////////////////
236
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
237
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
238

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

    
249
    int blitProgramH = mBlitProgram.getProgramHandle();
250
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
251
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
252

    
253
    // BLIT DEPTH PROGRAM ////////////////////////////////////
254
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
255
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
256

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

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

    
273
    // NORMAL PROGRAM //////////////////////////////////////
274
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
275
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
276

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

    
287
    int normalProgramH = mNormalProgram.getProgramHandle();
288
    mNormalMVPMatrixH  = GLES30.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  private static void createProgramsOIT(Resources resources)
294
    {
295
    // MAIN OIT PROGRAM ////////////////////////////////
296
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
297
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
298

    
299
    int numF = FragmentEffect.getNumEnabled();
300
    int numV = VertexEffect.getNumEnabled();
301

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

    
305
    String enabledEffectV= VertexEffect.getGLSL();
306
    String enabledEffectF= FragmentEffect.getGLSL();
307

    
308
    try
309
      {
310
      mMainOITProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
311
                                             enabledEffectV, enabledEffectF, mGLSL, null);
312
      }
313
    catch(Exception e)
314
      {
315
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN OIT program: "+e.getMessage());
316
      throw new RuntimeException(e.getMessage());
317
      }
318

    
319
    int mainOITProgramH = mMainOITProgram.getProgramHandle();
320
    EffectQueue.getUniforms(mainOITProgramH,1);
321
    mMainOITTextureH    = GLES30.glGetUniformLocation( mainOITProgramH, "u_Texture");
322
    mMainOITSizeH       = GLES30.glGetUniformLocation( mainOITProgramH, "u_Size");
323
    mMainOITNumRecordsH = GLES30.glGetUniformLocation( mainOITProgramH, "u_numRecords");
324

    
325
    // OIT CLEAR PROGRAM ////////////////////////////////////
326
    final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
327
    final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader);
328

    
329
    try
330
      {
331
      mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
332
      }
333
    catch(Exception e)
334
      {
335
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage());
336
      throw new RuntimeException(e.getMessage());
337
      }
338

    
339
    int oitClearProgramH   = mOITClearProgram.getProgramHandle();
340
    mOITClearDepthH        = GLES30.glGetUniformLocation( oitClearProgramH, "u_Depth");
341
    mOITClearTexCorrH      = GLES30.glGetUniformLocation( oitClearProgramH, "u_TexCorr");
342
    mOITClearSizeH         = GLES30.glGetUniformLocation( oitClearProgramH, "u_Size");
343

    
344
    // OIT BUILD PROGRAM ////////////////////////////////////
345
    final InputStream oitBuildVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
346
    final InputStream oitBuildFragStream = resources.openRawResource(R.raw.oit_build_fragment_shader);
347

    
348
    try
349
      {
350
      mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
351
      }
352
    catch(Exception e)
353
      {
354
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage());
355
      throw new RuntimeException(e.getMessage());
356
      }
357

    
358
    int oitBuildProgramH   = mOITBuildProgram.getProgramHandle();
359
    mOITBuildTextureH      = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Texture");
360
    mOITBuildDepthTextureH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture");
361
    mOITBuildDepthH        = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Depth");
362
    mOITBuildTexCorrH      = GLES30.glGetUniformLocation( oitBuildProgramH, "u_TexCorr");
363
    mOITBuildSizeH         = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Size");
364
    mOITBuildNumRecordsH   = GLES30.glGetUniformLocation( oitBuildProgramH, "u_numRecords");
365

    
366
    // OIT COLLAPSE PROGRAM ///////////////////////////
367
    final InputStream oitCollapseVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
368
    final InputStream oitCollapseFragStream = resources.openRawResource(R.raw.oit_collapse_fragment_shader);
369

    
370
    try
371
      {
372
      mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
373
      }
374
    catch(Exception e)
375
      {
376
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage());
377
      throw new RuntimeException(e.getMessage());
378
      }
379

    
380
    int oitCollapseProgramH   = mOITCollapseProgram.getProgramHandle();
381
    mOITCollapseDepthTextureH = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture");
382
    mOITCollapseDepthH        = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Depth");
383
    mOITCollapseTexCorrH      = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr");
384
    mOITCollapseSizeH         = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Size");
385

    
386
    // OIT RENDER PROGRAM ///////////////////////////
387
    final InputStream oitRenderVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
388
    final InputStream oitRenderFragStream = resources.openRawResource(R.raw.oit_render_fragment_shader);
389

    
390
    try
391
      {
392
      mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
393
      }
394
    catch(Exception e)
395
      {
396
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage());
397
      throw new RuntimeException(e.getMessage());
398
      }
399

    
400
    int oitRenderProgramH   = mOITRenderProgram.getProgramHandle();
401
    mOITRenderDepthH        = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Depth");
402
    mOITRenderTexCorrH      = GLES30.glGetUniformLocation( oitRenderProgramH, "u_TexCorr");
403
    mOITRenderSizeH         = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Size");
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  private static void displayNormals(EffectQueue[] queues, MeshBase mesh)
409
    {
410
    int num = mesh.getNumVertices();
411
    int tfo = mesh.getTFO();
412

    
413
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
414
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
415
    InternalRenderState.switchOffDrawing();
416
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
417
    InternalRenderState.restoreDrawing();
418
    GLES30.glEndTransformFeedback();
419
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
420

    
421
    DistortedLibrary.mNormalProgram.useProgram();
422
    GLES30.glUniformMatrix4fv(DistortedLibrary.mNormalMVPMatrixH, 1, false, EffectQueue.getMVP(queues) , 0);
423
    mesh.bindTransformAttribs(DistortedLibrary.mNormalProgram);
424
    GLES30.glLineWidth(8.0f);
425
    GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*num);
426
    }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
  static void drawPrivOIT(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
431
    {
432
    EffectQueue[] queues = effects.getQueues();
433

    
434
    EffectQueue.compute(queues, currTime);
435
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
436

    
437
    DistortedLibrary.mMainOITProgram.useProgram();
438
    GLES30.glUniform1i(DistortedLibrary.mMainOITTextureH, 0);
439
    GLES30.glUniform2ui(DistortedLibrary.mMainOITSizeH, surface.mWidth, surface.mHeight);
440
    GLES30.glUniform1ui(DistortedLibrary.mMainOITNumRecordsH, (int)(DistortedLibrary.mBufferSize*surface.mWidth*surface.mHeight) );
441
    mesh.bindVertexAttribs(DistortedLibrary.mMainOITProgram);
442

    
443
    float inflate     = mesh.getInflate();
444
    float distance    = surface.mDistance;
445
    float mipmap      = surface.mMipmap;
446
    float[] projection= surface.mProjectionMatrix;
447

    
448
    EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 1 );
449
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
450

    
451
    if( mesh.getShowNormals() )
452
      {
453
      DistortedLibrary.mMainProgram.useProgram();
454
      EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 0 );
455
      displayNormals(queues,mesh);
456
      }
457
    }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

    
461
  static void drawPriv(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
462
    {
463
    if( mMainOITProgram!=null )
464
      {
465
      EffectQueue[] queues = effects.getQueues();
466

    
467
      EffectQueue.compute(queues, currTime);
468
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
469

    
470
      mMainProgram.useProgram();
471
      GLES30.glUniform1i(DistortedLibrary.mMainTextureH, 0);
472
      mesh.bindVertexAttribs(DistortedLibrary.mMainProgram);
473

    
474
      float inflate     = mesh.getInflate();
475
      float distance    = surface.mDistance;
476
      float mipmap      = surface.mMipmap;
477
      float[] projection= surface.mProjectionMatrix;
478

    
479
      EffectQueue.send(queues, distance, mipmap, projection, inflate, mesh, 0 );
480
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
481

    
482
      if( mesh.getShowNormals() ) displayNormals(queues,mesh);
483
      }
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  static void blitPriv(InternalOutputSurface surface)
489
    {
490
    if( mBlitProgram!=null )
491
      {
492
      mBlitProgram.useProgram();
493

    
494
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
495
      GLES30.glUniform1i(mBlitTextureH, 0);
496
      GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
497
      GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
498
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
499
      }
500
    }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

    
504
  static void blitDepthPriv(InternalOutputSurface surface, float corrW, float corrH)
505
    {
506
    if( mBlitDepthProgram!=null )
507
      {
508
      mBlitDepthProgram.useProgram();
509

    
510
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
511
      GLES30.glUniform1i(mBlitDepthTextureH, 0);
512
      GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
513
      GLES30.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
514
      GLES30.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
515
      GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
516
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
517
      }
518
    }
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521
// yes it is safe to be mixing 3.0 and 3.1 like that, senior members of the OpenGL discussions forum assert
522

    
523
  private static int printPreviousBuffer()
524
    {
525
    int counter = 0;
526

    
527
    ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
528
                                                                GLES30.GL_MAP_READ_BIT);
529
    if( atomicBuf!=null )
530
      {
531
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
532
      counter = atomicIntBuf.get(0);
533
      }
534
    else
535
      {
536
      Log.e("effects", "print: failed to map atomic buffer");
537
      }
538

    
539
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
540

    
541
    return counter;
542
    }
543

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545

    
546
  private static void zeroBuffer()
547
    {
548
    ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
549
        GLES30.GL_MAP_WRITE_BIT|GLES30.GL_MAP_INVALIDATE_BUFFER_BIT);
550
    if( atomicBuf!=null )
551
      {
552
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
553
      atomicIntBuf.put(0,0);
554
      }
555
    else
556
      {
557
      Log.e("effects", "zero: failed to map atomic buffer");
558
      }
559

    
560
    GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564
// reset atomic counter to 0
565

    
566
  static int zeroOutAtomic()
567
    {
568
    int counter = 0;
569

    
570
    if( mAtomicCounter[0]<0 )
571
      {
572
      GLES30.glGenBuffers(DistortedLibrary.FBO_QUEUE_SIZE,mAtomicCounter,0);
573

    
574
      for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++)
575
        {
576
        GLES30.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]);
577
        GLES30.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES30.GL_DYNAMIC_DRAW);
578
        zeroBuffer();
579
        }
580
      }
581

    
582
    // reading the value of the buffer on every frame would slow down rendering by
583
    // dialog_about 3%; doing it only once every 5 frames affects speed by less than 1%.
584
    if( mCurrBuffer==0 )
585
      {
586
      GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
587
      counter = printPreviousBuffer();
588
      }
589

    
590
    if( ++mCurrBuffer>= DistortedLibrary.FBO_QUEUE_SIZE ) mCurrBuffer = 0;
591

    
592
    GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
593
    zeroBuffer();
594

    
595
    return counter;
596
    }
597

    
598
///////////////////////////////////////////////////////////////////////////////////////////////////
599
// Pass1 of the OIT algorithm. Clear per-pixel head-pointers.
600

    
601
  static void oitClear(InternalOutputSurface surface, int counter)
602
    {
603
    if( mOITClearProgram!=null )
604
      {
605
      if( mLinkedListSSBO[0]<0 )
606
        {
607
        GLES30.glGenBuffers(1,mLinkedListSSBO,0);
608

    
609
        int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
610
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
611
        GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
612
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
613

    
614
        GLES30.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
615
        }
616

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

    
621
      if( overflow>1.0f )
622
        {
623
        //android.util.Log.e("effects", "previous frame rendered "+counter+
624
        //                   " fragments, but there was only "+(mBufferSize*surface.mWidth*surface.mHeight)+" space");
625

    
626
        mBufferSize *= (int)(overflow+1.0f);
627
        int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
628
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
629
        GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
630
        GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
631
        }
632

    
633
      mOITClearProgram.useProgram();
634

    
635
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
636
      GLES30.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f );   // corrections do not really matter here - only present because of common vertex shader.
637
      GLES30.glUniform1f( mOITClearDepthH , 1.0f);          // likewise depth
638
      GLES30.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight);
639
      GLES30.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
640
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
641
      }
642
    }
643

    
644
///////////////////////////////////////////////////////////////////////////////////////////////////
645
// Pass2 of the OIT algorithm - build per-pixel linked lists.
646

    
647
  static void oitBuild(InternalOutputSurface surface, float corrW, float corrH)
648
    {
649
    if( mOITBuildProgram!=null )
650
      {
651
      mOITBuildProgram.useProgram();
652

    
653
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
654
      GLES30.glUniform1i(mOITBuildTextureH, 0);
655
      GLES30.glUniform1i(mOITBuildDepthTextureH, 1);
656
      GLES30.glUniform2f(mOITBuildTexCorrH, corrW, corrH );
657
      GLES30.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight);
658
      GLES30.glUniform1ui(mOITBuildNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
659
      GLES30.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear);
660
      GLES30.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
661
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
662
      }
663
    }
664

    
665
///////////////////////////////////////////////////////////////////////////////////////////////////
666
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list.
667

    
668
  static void oitCollapse(InternalOutputSurface surface, float corrW, float corrH)
669
    {
670
    if( mOITCollapseProgram!=null )
671
      {
672
      mOITCollapseProgram.useProgram();
673

    
674
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
675
      GLES30.glUniform1i(mOITCollapseDepthTextureH, 1);
676
      GLES30.glUniform2f(mOITCollapseTexCorrH, corrW, corrH );
677
      GLES30.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight);
678
      GLES30.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear);
679
      GLES30.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
680
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
681
      }
682
    }
683

    
684
///////////////////////////////////////////////////////////////////////////////////////////////////
685
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists.
686

    
687
  static void oitRender(InternalOutputSurface surface, float corrW, float corrH)
688
    {
689
    if( mOITRenderProgram!=null )
690
      {
691
      mOITRenderProgram.useProgram();
692

    
693
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
694
      GLES30.glUniform2f(mOITRenderTexCorrH, corrW, corrH );
695
      GLES30.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight);
696
      GLES30.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear);
697
      GLES30.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
698
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
699
      }
700
    }
701

    
702
///////////////////////////////////////////////////////////////////////////////////////////////////
703

    
704
  static void setSSBOSize(float size)
705
    {
706
    mBufferSize = size;
707
    }
708

    
709
///////////////////////////////////////////////////////////////////////////////////////////////////
710
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
711
// PowerVR GE8100 / GE8300 compiler fails to compile OIT programs.
712

    
713
  private static void detectBuggyDrivers()
714
    {
715
    String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
716
    String version = GLES30.glGetString(GLES30.GL_VERSION);
717
    String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
718

    
719
    /*
720
    android.util.Log.e("DISTORTED", "GLSL Version "+GLES30.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
721
    android.util.Log.e("DISTORTED", "GL Version "  +GLES30.glGetString(GLES31.GL_VERSION));
722
    android.util.Log.e("DISTORTED", "GL Vendor "   +GLES30.glGetString(GLES31.GL_VENDOR));
723
    android.util.Log.e("DISTORTED", "GL Renderer " +GLES30.glGetString(GLES31.GL_RENDERER));
724
    */
725

    
726
    if( vendor.contains("ARM") )
727
      {
728
      if( version.contains("r12") )
729
        {
730
        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.");
731
        }
732
      }
733
    else if( vendor.contains("Imagination") )
734
      {
735
      if( renderer.contains("GE8") )
736
        {
737
        Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
738
        }
739
      }
740
    }
741

    
742
///////////////////////////////////////////////////////////////////////////////////////////////////
743
/**
744
 * Return OpenGL ES version supported by the hardware we are running on.
745
 * There are only two possibilities: 300 (OpenGL ES 3.0) or 310 (at least OpenGL ES 3.1)
746
 */
747
  static int getGLSL()
748
    {
749
    return mGLSL;
750
    }
751

    
752
///////////////////////////////////////////////////////////////////////////////////////////////////
753
/**
754
 * Have we successfully managed to compile the OIT programs?
755
 * Some hardware ( Imagination GE8100 / 8300, driver build 1.8@4490469 present in my HTC phone and,
756
 * sadly, Amazon Fire Stick 4K ) fails to compile the shaders. See relevant thread in Imagination's
757
 * support forum:
758
 *
759
 * https://forums.imgtec.com/t/ge8100-in-htc-desire-12-android-7-1-1-fragment-shader-fails-to-compile/2708
760
 */
761
  public static boolean OITCompilationSuccessful()
762
    {
763
    return mOITCompilationSuccessful;
764
    }
765

    
766
///////////////////////////////////////////////////////////////////////////////////////////////////
767
/**
768
 * Have we called onCreate yet, ie have we initialized the library?
769
 * @return <code>true</code> if the library is initialized and ready for action.
770
 */
771
  public static boolean isInitialized()
772
    {
773
    return mInitialized;
774
    }
775

    
776
///////////////////////////////////////////////////////////////////////////////////////////////////
777
/**
778
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
779
 * I.e. best called from GLSurfaceView.onCreate().
780
 * <p>
781
 * Needs to be called from a thread holding the OpenGL context.
782
 *   
783
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
784
 */
785
  public static void onCreate(final Context context) throws Exception
786
    {
787
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
788
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
789

    
790
    int glESversion = configurationInfo.reqGlEsVersion;
791
    int major = glESversion >> 16;
792
    int minor = glESversion & 0xff;
793

    
794
    if( major< 3 )
795
      {
796
      mGLSL = 100*major + 10*minor;
797
      throw new VertexCompilationException("at least OpenGL ES 3.0 required, this device supports only "+major+"."+minor);
798
      }
799
    else
800
      {
801
      mGLSL = (major==3 && minor==0) ? 300 : 310;
802
      }
803

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

    
807
    mInitialized = true;
808
    mOITCompilationSuccessful = false;
809

    
810
    detectBuggyDrivers();
811

    
812
    EffectMessageSender.startSending();
813

    
814
    final Resources resources = context.getResources();
815

    
816
    Exception exception=null;
817

    
818
    try
819
      {
820
      createPrograms(resources);
821
      }
822
    catch(Exception ex)
823
      {
824
      exception = ex;
825
      }
826

    
827
    if( mGLSL>=310)
828
      {
829
      try
830
        {
831
        createProgramsOIT(resources);
832
        mOITCompilationSuccessful = true;
833
        }
834
      catch(Exception ex)
835
        {
836
        exception = ex;
837
        }
838
      }
839

    
840
    try
841
      {
842
      EffectQueuePostprocess.createPrograms(resources, mGLSL);
843
      }
844
    catch(Exception ex)
845
      {
846
      exception = ex;
847
      }
848

    
849
    try
850
      {
851
      PostprocessEffect.createPrograms(mGLSL);
852
      }
853
    catch(Exception ex)
854
      {
855
      exception = ex;
856
      }
857

    
858
    if( exception!=null)
859
      {
860
      throw exception;
861
      }
862
    }
863

    
864
///////////////////////////////////////////////////////////////////////////////////////////////////
865
/**
866
 * Call this so that the Library can release the OpenGL related data that needs to be recreated.
867
 * Must be called from Activity.onPause().
868
 */
869
  public static void onPause()
870
    {
871
    InternalObject.onPause();
872

    
873
    mLinkedListSSBO[0]= -1;
874

    
875
    for(int i = 0; i< DistortedLibrary.FBO_QUEUE_SIZE; i++) mAtomicCounter[i] = -1;
876
    }
877

    
878
///////////////////////////////////////////////////////////////////////////////////////////////////
879
/**
880
 * Call this so that the Library can release its internal data structures.
881
 * Must be called from Activity.onDestroy(). 
882
 */
883
  public static void onDestroy()
884
    {
885
    InternalObject.onDestroy();
886
    InternalNodeData.onDestroy();
887
    InternalMaster.onDestroy();
888
    InternalOutputSurface.onDestroy();
889
    DistortedEffects.onDestroy();
890
    EffectQueue.onDestroy();
891
    Effect.onDestroy();
892
    EffectMessageSender.stopSending();
893

    
894
    mInitialized = false;
895
    }
896

    
897
///////////////////////////////////////////////////////////////////////////////////////////////////
898
/**
899
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
900
 * single (InputSurface,MeshBase) combo.
901
 *
902
 * @param type {@link EffectType}
903
 * @return The maximum number of effects of a given type.
904
 */
905
  @SuppressWarnings("unused")
906
  public static int getMax(EffectType type)
907
    {
908
    return EffectQueue.getMax(type.ordinal());
909
    }
910

    
911
///////////////////////////////////////////////////////////////////////////////////////////////////
912
/**
913
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
914
 * This can fail if:
915
 * <ul>
916
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
917
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
918
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link DistortedLibrary#onCreate}. After this
919
 *     time only decreasing the value of 'max' is permitted.
920
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
921
 * </ul>
922
 *
923
 * @param type {@link EffectType}
924
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
925
 *            than Byte.MAX_VALUE
926
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
927
 */
928
  @SuppressWarnings("unused")
929
  public static boolean setMax(EffectType type, int max)
930
    {
931
    return EffectQueue.setMax(type.ordinal(),max);
932
    }
933
  }
(3-3/14)