commit 341151fc079e3283ad3d57233b080b535f3e3cf9
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Apr 27 17:53:41 2018 +0100

    Fixes ported from the 'OIT' branch:
    
    - remove counting of transparent pixels
    - various fixes for the Mali GPU

diff --git a/src/main/java/org/distorted/library/main/DistortedEffects.java b/src/main/java/org/distorted/library/main/DistortedEffects.java
index 5a52229..63a928d 100644
--- a/src/main/java/org/distorted/library/main/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/main/DistortedEffects.java
@@ -53,7 +53,6 @@ public class DistortedEffects
   /// MAIN PROGRAM ///
   private static DistortedProgram mMainProgram;
   private static int mMainTextureH;
-  private static int mCountIndexH;
 
   /// BLIT PROGRAM ///
   private static DistortedProgram mBlitProgram;
@@ -122,7 +121,6 @@ public class DistortedEffects
     EffectQueueVertex.getUniforms(mainProgramH);
     EffectQueueMatrix.getUniforms(mainProgramH);
     mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
-    mCountIndexH = GLES31.glGetUniformLocation( mainProgramH, "u_currentIndex");
 
     // BLIT PROGRAM ////////////////////////////////////
     final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
@@ -285,7 +283,6 @@ public class DistortedEffects
 
     mMainProgram.useProgram();
     GLES31.glUniform1i(mMainTextureH, 0);
-    GLES31.glUniform1i(mCountIndexH, surface.getNewCounter() );
 
     if( Distorted.GLSL >= 300 )
       {
diff --git a/src/main/java/org/distorted/library/main/DistortedFramebuffer.java b/src/main/java/org/distorted/library/main/DistortedFramebuffer.java
index c60f388..b3a158f 100644
--- a/src/main/java/org/distorted/library/main/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/main/DistortedFramebuffer.java
@@ -39,7 +39,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Must be called from a thread holding OpenGL Context
 
-  void createSurface()
+  void create()
     {
     if( mColorCreated==NOT_CREATED_YET )
       {
@@ -135,7 +135,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Must be called from a thread holding OpenGL Context
 
-  void deleteSurface()
+  void delete()
     {
     if( mColorH[0]>0 )
       {
@@ -158,7 +158,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // called from onDestroy(); mark OpenGL assets as 'not created'
 
-  void recreateSurface()
+  void recreate()
     {
     if( mColorCreated!=DONT_CREATE )
       {
diff --git a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
index c188ed0..20ccb3a 100644
--- a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
@@ -24,9 +24,6 @@ import android.opengl.Matrix;
 
 import org.distorted.library.effect.EffectQuality;
 
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.nio.IntBuffer;
 import java.util.ArrayList;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -105,38 +102,10 @@ public static final int DEBUG_FPS = 1;
                     // mWidth,mHeight are the sizes of the Viewport, those -
                     // sizes of the backing up texture.
 
-  ////////////////////////////////////////////////////////////////////////////////
-  // section dealing with Shader Storage Buffer Object (for counting transparency)
-  private static final int FRAME_DELAY = 3;
-  private static int mBufferSize       =10;
-  private static DistortedObjectCounter mSurfaceCounter = new DistortedObjectCounter();
-  private static int[] mSSBO = new int[1];
-  private static IntBuffer mIntBuffer;
-
-  static
-    {
-    mSSBO[0]= -1;
-    }
-
-  private int mSurfaceID;
-  private int mLastIndex;
-  private int[] mLastValue = new int[FRAME_DELAY];
-  private int mLastDiff = -1;
-
-  private static final int RUNNING_AVERAGE = 10;
-  private int[] mRunningAvg = new int[RUNNING_AVERAGE];
-  private int mAvgIndex;
-  private int mAvgSum;
-  // end section
-  ////////////////////////////////////////////////////////////////////////////////
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   abstract void prepareDebug(long time);
   abstract void renderDebug(long time);
-  abstract void createSurface();
-  abstract void deleteSurface();
-  abstract void recreateSurface();
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -175,78 +144,6 @@ public static final int DEBUG_FPS = 1;
     createProjection();
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// Must be called from a thread holding OpenGL Context
-
-  void create()
-    {
-    if( mSSBO[0]<0 )
-      {
-      GLES31.glGenBuffers(1,mSSBO,0);
-
-      // here bind the new SSBO and map it
-      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mSSBO[0]);
-      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, FRAME_DELAY *mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
-      ByteBuffer buf = (ByteBuffer) GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, FRAME_DELAY *mBufferSize*4, GLES31.GL_MAP_READ_BIT );
-      mIntBuffer = buf.order(ByteOrder.nativeOrder()).asIntBuffer();
-      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER,0, mSSBO[0]);
-      }
-
-    mSurfaceID = mSurfaceCounter.returnNext();
-
-    if( mSurfaceID>=mBufferSize )
-      {
-      // increase size of the Buffer, copy over old values, remap.
-      int[] tmp = new int[FRAME_DELAY *mBufferSize];
-      for(int i = 0; i< FRAME_DELAY *mBufferSize; i++) tmp[i] = mIntBuffer.get(i);
-
-      mBufferSize*= 2;
-
-      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 0, 0);
-      GLES31.glUnmapBuffer(GLES31.GL_SHADER_STORAGE_BUFFER);
-      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER,0);
-
-      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mSSBO[0]);
-      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, FRAME_DELAY *mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
-      ByteBuffer buf = (ByteBuffer) GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, FRAME_DELAY *mBufferSize*4, GLES31.GL_MAP_READ_BIT );
-      mIntBuffer = buf.order(ByteOrder.nativeOrder()).asIntBuffer();
-      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER,0, mSSBO[0]);
-
-      for(int i=0;i<tmp.length;i++) mIntBuffer.put(i,tmp[i]);
-      }
-
-    createSurface();
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// Must be called from a thread holding OpenGL Context
-
-  void delete()
-    {
-    mSurfaceCounter.release(mSurfaceID);
-    deleteSurface();
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void recreate()
-    {
-    mSSBO[0]  = -1;
-    mLastDiff = -1;
-
-    for(int i = 0; i< FRAME_DELAY; i++) mLastValue[i] = 0;
-    mSurfaceCounter.releaseAll();
-
-    recreateSurface();
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  int getNewCounter()
-    {
-    return FRAME_DELAY*mSurfaceID + mLastIndex;
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private void createProjection()
@@ -363,8 +260,8 @@ public static final int DEBUG_FPS = 1;
           surface.mRealWidth = maxw;
           surface.mRealHeight = maxh;
 
-          surface.recreateSurface();
-          surface.createSurface();
+          surface.recreate();
+          surface.create();
           }
         }
       }
diff --git a/src/main/java/org/distorted/library/main/DistortedScreen.java b/src/main/java/org/distorted/library/main/DistortedScreen.java
index 530d3d5..e74025c 100644
--- a/src/main/java/org/distorted/library/main/DistortedScreen.java
+++ b/src/main/java/org/distorted/library/main/DistortedScreen.java
@@ -56,9 +56,9 @@ public class DistortedScreen extends DistortedOutputSurface
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // here we don't manage underlying OpenGL assets ourselves
 
-  void createSurface()   {}
-  void deleteSurface()   {}
-  void recreateSurface() {}
+  void create()   {}
+  void delete()   {}
+  void recreate() {}
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
diff --git a/src/main/java/org/distorted/library/program/DistortedProgram.java b/src/main/java/org/distorted/library/program/DistortedProgram.java
index 505f808..e520403 100644
--- a/src/main/java/org/distorted/library/program/DistortedProgram.java
+++ b/src/main/java/org/distorted/library/program/DistortedProgram.java
@@ -292,24 +292,20 @@ public class DistortedProgram
       final int[] compileStatus = new int[1];
       GLES31.glGetShaderiv(shaderHandle, GLES31.GL_COMPILE_STATUS, compileStatus, 0);
 
-      if (compileStatus[0] != GLES31.GL_TRUE )
+      if (compileStatus[0] != GLES31.GL_TRUE)
         {
-        GLES31.glDeleteShader(shaderHandle);
-        shaderHandle = 0;
-        }
-      }
+        String error = GLES31.glGetShaderInfoLog(shaderHandle);
 
-    if (shaderHandle == 0)
-      {
-      String error = GLES31.glGetShaderInfoLog(shaderHandle);
+        //android.util.Log.e("Program", "error compiling :" + error);
 
-      //android.util.Log.e("Program", "error compiling :"+error);
+        GLES31.glDeleteShader(shaderHandle);
 
-      switch(shaderType)
-        {
-        case GLES31.GL_VERTEX_SHADER  : throw new VertexCompilationException(error);
-        case GLES31.GL_FRAGMENT_SHADER: throw new FragmentCompilationException(error);
-        default                       : throw new RuntimeException(error);
+        switch (shaderType)
+          {
+          case GLES31.GL_VERTEX_SHADER:   throw new VertexCompilationException(error);
+          case GLES31.GL_FRAGMENT_SHADER: throw new FragmentCompilationException(error);
+          default:                        throw new RuntimeException(error);
+          }
         }
       }
 
diff --git a/src/main/res/raw/main_fragment_shader.glsl b/src/main/res/raw/main_fragment_shader.glsl
index 8052bd4..084f7f5 100644
--- a/src/main/res/raw/main_fragment_shader.glsl
+++ b/src/main/res/raw/main_fragment_shader.glsl
@@ -17,7 +17,7 @@
 // along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
 //////////////////////////////////////////////////////////////////////////////////////////////
 
-precision lowp float;
+precision highp float;
 
 #if __VERSION__ != 100
 in vec3 v_Position;                     // Interpolated position for this fragment.
@@ -43,19 +43,6 @@ uniform vec4 fUniforms[2*NUM_FRAGMENT]; // i-th effect is 2 consecutive vec4's:
                                         // next describes the Region, i.e. area over which the effect is active.
 #endif    // NUM_FRAGMENT>0
 
-layout (std430,binding=0) buffer SSBO   // PER-SURFACE count of transparent fragments.
-  {                                     // Can be buffered, i.e. if we are for example
-  int ssbocount[];                      // triple-buffered, then surfaceID=N uses 3
-  };                                    // consecutive counts (N,N+1,N+2) during 3
-                                        // consecutive frames.
-                                        // Cannot be an 'uint' because the Java application that's
-                                        // reading this does not have unsigned types so it's reading
-                                        // this into a signed int... Doesn't matter as things keep on
-                                        // working even when this overflows into negative territory :)
-
-uniform int u_currentIndex;             // Index into the count[] array we are supposed to be using
-                                        // during this very invocation.
-
 //////////////////////////////////////////////////////////////////////////////////////////////
 
 void main()                    		
@@ -78,7 +65,5 @@ void main()
     }
 #endif
 
-  if( color.a < 1.0 && color.a > 0.0 ) atomicAdd(ssbocount[u_currentIndex],1);
-
   FRAG_COLOR = vec4(color.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, color.a);
   }
\ No newline at end of file
diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 3e92fda..78f2267 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -17,7 +17,7 @@
 // along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
 //////////////////////////////////////////////////////////////////////////////////////////////
 
-precision lowp float;
+precision highp float;
 
 #if __VERSION__ != 100
 in vec3 a_Position;                  // Per-vertex position.
