commit 2f35828cb7960959c6b2944c8d6aa39da58d39cf
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Thu Apr 26 12:22:06 2018 +0100

    - remove counting of transparent pixels from main fragment shader
    - remove debugs and tidy up
    - some 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 5e401b1..2724d8e 100644
--- a/src/main/java/org/distorted/library/main/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/main/DistortedEffects.java
@@ -54,7 +54,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,8 +121,6 @@ private static IntBuffer mIntBuf;
   throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
     {
     // MAIN PROGRAM ////////////////////////////////////
-    android.util.Log.e("Effects", "creating main program");
-
     final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
     final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
 
@@ -158,11 +155,8 @@ private static IntBuffer mIntBuf;
     EffectQueueVertex.getUniforms(mainProgramH);
     EffectQueueMatrix.getUniforms(mainProgramH);
     mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
-    mCountIndexH = GLES31.glGetUniformLocation( mainProgramH, "u_currentIndex");
 
     // BLIT PROGRAM ////////////////////////////////////
-    android.util.Log.e("Effects", "creating blit program");
-
     final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
     final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
 
@@ -184,8 +178,6 @@ private static IntBuffer mIntBuf;
     mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
 
     // BLIT DEPTH PROGRAM ////////////////////////////////////
-    android.util.Log.e("Effects", "creating blit depth program");
-
     final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
     final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
 
@@ -231,8 +223,6 @@ private static IntBuffer mIntBuf;
       }
 
     // BLIT DEPTH RENDER PROGRAM ///////////////////////////
-    android.util.Log.e("Effects", "creating blit depth render program");
-
     final InputStream blitDepthRenderVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
     final InputStream blitDepthRenderFragStream = resources.openRawResource(R.raw.blit_depth_render_fragment_shader);
 
@@ -253,8 +243,6 @@ private static IntBuffer mIntBuf;
     mBlitDepthRenderSizeH         = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size");
 
     // NORMAL PROGRAM //////////////////////////////////////
-    android.util.Log.e("Effects", "creating normal program");
-
     final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
     final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
 
@@ -374,7 +362,6 @@ private static IntBuffer mIntBuf;
 
     mMainProgram.useProgram();
     GLES31.glUniform1i(mMainTextureH, 0);
-    GLES31.glUniform1i(mCountIndexH, surface.getNewCounter() );
 
     GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
     GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
@@ -423,10 +410,6 @@ private static IntBuffer mIntBuf;
     GLES31.glUniform1f(mBlitDepthDepthH , 1.0f-surface.mNear);
     GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
     GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
-
-
-    //android.util.Log.e("effects", "width="+surface.mWidth+" height="+surface.mHeight+
-    //    " bufferSize: "+mBufferSize+" numRecords: "+((mBufferSize-surface.mWidth*surface.mHeight)/3) );
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
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/DistortedObjectCounter.java b/src/main/java/org/distorted/library/main/DistortedObjectCounter.java
deleted file mode 100644
index 7276c6d..0000000
--- a/src/main/java/org/distorted/library/main/DistortedObjectCounter.java
+++ /dev/null
@@ -1,97 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// Copyright 2016 Leszek Koltunski                                                               //
-//                                                                                               //
-// This file is part of Distorted.                                                               //
-//                                                                                               //
-// Distorted is free software: you can redistribute it and/or modify                             //
-// it under the terms of the GNU General Public License as published by                          //
-// the Free Software Foundation, either version 2 of the License, or                             //
-// (at your option) any later version.                                                           //
-//                                                                                               //
-// Distorted is distributed in the hope that it will be useful,                                  //
-// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
-// GNU General Public License for more details.                                                  //
-//                                                                                               //
-// You should have received a copy of the GNU General Public License                             //
-// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-package org.distorted.library.main;
-
-import java.util.ArrayList;
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Return unique IDs of objects in a lazy way. When we create a new Object, return the lowest unused
- * integer. When an objects gets deleted, its ID then gets marked unused. We do not try to 'collapse'
- * unused holes.
- */
-class DistortedObjectCounter
-  {
-  private ArrayList<Integer> mUsed;
-  private int mFirstUnused;
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  DistortedObjectCounter()
-    {
-    mUsed        = new ArrayList<>();
-    mFirstUnused = 0;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  int returnNext()
-    {
-    int size = mUsed.size();
-
-    if( mFirstUnused<size )
-      {
-      int ret = mFirstUnused;
-
-      mUsed.set(mFirstUnused,1);
-
-      int found=-1;
-
-      for(int i=mFirstUnused+1; i<size; i++ )
-        {
-        if( mUsed.get(i)==0 )
-          {
-          found = i;
-          break;
-          }
-        }
-
-      mFirstUnused = found<0 ? size : found;
-
-      return ret;
-      }
-
-    mUsed.add(1);
-    mFirstUnused++;
-
-    return size;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void release(int objID)
-    {
-    int size = mUsed.size();
-
-    if( objID<size && objID>=0 && mUsed.get(objID)==1 )
-      {
-      mUsed.set(objID,0);
-      if( objID<mFirstUnused ) mFirstUnused = objID;
-      }
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void releaseAll()
-    {
-    mUsed.clear();
-    mFirstUnused = 0;
-    }
-  }
diff --git a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
index 41a1664..895643a 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();
           }
         }
       }
@@ -634,31 +531,6 @@ public static final int DEBUG_FPS = 1;
       GLES31.glClearStencil(mClearStencil);
       GLES31.glClear(mClear);
       DistortedRenderState.colorDepthStencilRestore();
-/*
-      if( mSSBO[0]>=0 )
-        {
-        // yes, this DOES keep on working when 'value' overflows into negative territory.
-        int value = mIntBuffer.get(FRAME_DELAY*mSurfaceID+mLastIndex);
-
-        if( value-mLastValue[mLastIndex]!=mLastDiff )
-          {
-          android.util.Log.d("surface", "id " + mSurfaceID +
-                             (mType == TYPE_USER ? " USER" : (mType == TYPE_SYST ? " SYST" : " TREE")) +
-                             " viewport: (" + mWidth + "x" + mHeight + ") last frame: " + (value - mLastValue[mLastIndex])
-                             + " avg: " + (mAvgSum/RUNNING_AVERAGE)
-                            );
-          }
-
-        mLastDiff = value-mLastValue[mLastIndex];
-        mLastValue[mLastIndex] = value;
-
-        mAvgSum += (mLastDiff-mRunningAvg[mAvgIndex]);
-        mRunningAvg[mAvgIndex] = mLastDiff;
-        if( ++mAvgIndex>=RUNNING_AVERAGE ) mAvgIndex =0;
-        }
-
-      if( ++mLastIndex >= FRAME_DELAY ) mLastIndex=0;
-*/
       }
     }
 
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/res/raw/main_fragment_shader.glsl b/src/main/res/raw/main_fragment_shader.glsl
index 8052bd4..52af919 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 mediump 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..e5e152a 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 mediump float;
 
 #if __VERSION__ != 100
 in vec3 a_Position;                  // Per-vertex position.
