commit cee0369af6518529260065d40ff04247fde5a6c9
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Wed Apr 25 16:26:18 2018 +0100

    Order Independent Transparency: debugging

diff --git a/src/main/java/org/distorted/library/main/DistortedEffects.java b/src/main/java/org/distorted/library/main/DistortedEffects.java
index 22abeb1..5e401b1 100644
--- a/src/main/java/org/distorted/library/main/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/main/DistortedEffects.java
@@ -88,8 +88,12 @@ public class DistortedEffects
     }
 
   private static int mBufferSize=(0x1<<23);  // 8 million entries
+
   private static IntBuffer mIntBuffer;
 
+private static ByteBuffer mBuf;
+private static IntBuffer mIntBuf;
+
   /// BLIT DEPTH RENDER PROGRAM ///
   private static DistortedProgram mBlitDepthRenderProgram;
   private static int mBlitDepthRenderDepthTextureH;
@@ -118,6 +122,8 @@ public class DistortedEffects
   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);
 
@@ -136,8 +142,16 @@ public class DistortedEffects
 
     String[] feedback = { "v_Position", "v_endPosition" };
 
-    mMainProgram = new DistortedProgram( mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
-                                         enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
+    try
+      {
+      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
+                                          enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
+      }
+    catch(Exception e)
+      {
+      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
+      throw new RuntimeException(e.getMessage());
+      }
 
     int mainProgramH = mMainProgram.getProgramHandle();
     EffectQueueFragment.getUniforms(mainProgramH);
@@ -147,6 +161,8 @@ public class DistortedEffects
     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);
 
@@ -159,7 +175,7 @@ public class DistortedEffects
       }
     catch(Exception e)
       {
-      Log.e("EFFECTS", "exception trying to compile BLIT program: "+e.getMessage());
+      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
       throw new RuntimeException(e.getMessage());
       }
 
@@ -168,6 +184,8 @@ public class DistortedEffects
     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);
 
@@ -177,7 +195,7 @@ public class DistortedEffects
       }
     catch(Exception e)
       {
-      Log.e("EFFECTS", "exception trying to compile BLIT DEPTH program: "+e.getMessage());
+      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
       throw new RuntimeException(e.getMessage());
       }
 
@@ -196,7 +214,11 @@ public class DistortedEffects
       {
       GLES31.glGenBuffers(1,mLinkedListSSBO,0);
       GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
-      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
+      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
+
+      mBuf = (ByteBuffer)GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, mBufferSize*4, GLES31.GL_MAP_READ_BIT);
+      mIntBuf = mBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
+
       GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
       }
 
@@ -209,6 +231,8 @@ public class DistortedEffects
       }
 
     // 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);
 
@@ -218,7 +242,7 @@ public class DistortedEffects
       }
     catch(Exception e)
       {
-      Log.e("EFFECTS", "exception trying to compile BLIT DEPTH RENDER program: "+e.getMessage());
+      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH RENDER program: "+e.getMessage());
       throw new RuntimeException(e.getMessage());
       }
 
@@ -229,6 +253,8 @@ public class DistortedEffects
     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);
 
@@ -238,7 +264,7 @@ public class DistortedEffects
       }
     catch(Exception e)
       {
-      Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
+      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
       throw new RuntimeException(e.getMessage());
       }
 
@@ -411,6 +437,8 @@ public class DistortedEffects
     {
     mBlitDepthRenderProgram.useProgram();
 
+    analyzeBuffer(surface.mWidth, surface.mHeight);
+
     GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
     GLES31.glUniform1i(mBlitDepthRenderDepthTextureH, 1);
     GLES31.glUniform2f(mBlitDepthRenderTexCorrH, corrW, corrH );
@@ -426,7 +454,52 @@ public class DistortedEffects
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-   
+
+
+  private static void analyzeBuffer(int w, int h)
+    {
+    int ptr;
+    final int GREEN = (255<<16) + 255;
+    final int RED   = (255<<24) + 255;
+
+    int left1=0, left2=0;
+    int right1=0, right2=0;
+    int overflow = 0;
+
+    for(int row=0; row<h; row++)
+      for(int col=0; col<w; col++)
+        {
+        ptr = mIntBuf.get( col+row*w );
+
+        if( ptr!=0 )
+          {
+          if( ptr>0 && ptr<mBufferSize )
+            {
+            ptr = mIntBuf.get(ptr);
+
+            if ((2*col>w) && ptr != RED)
+              {
+              if (ptr==GREEN) right1++;
+              else            right2++;
+              }
+            if ((2*col<=w) && ptr != GREEN)
+              {
+              if (ptr==RED) left1++;
+              else          left2++;
+              }
+            }
+          else
+            {
+            overflow++;
+            }
+          }
+        }
+
+    android.util.Log.e("surface", " left1: "+left1+" left2: "+left2+" right1: "+right1+" right2: "+right2+" overflow: "+overflow);
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
   private void releasePriv()
     {
     if( !matrixCloned   )   mM.abortAll(false);
diff --git a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
index 53459da..41a1664 100644
--- a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
@@ -488,8 +488,12 @@ public static final int DEBUG_FPS = 1;
 
           numRenders += currQueue.postprocess(mBuffer);
           numRenders += blitWithDepth(time, mBuffer[quality]);
+          GLES31.glMemoryBarrier(GLES31.GL_ALL_BARRIER_BITS);
+          //GLES31.glFinish();
           numRenders += blitWithDepthRender(time,mBuffer[quality]);  // merge the OIT linked list
           clearBuffer(mBuffer[quality]);
+          GLES31.glMemoryBarrier(GLES31.GL_ALL_BARRIER_BITS);
+          //GLES31.glFlush();
           }
         }
 
diff --git a/src/main/res/raw/blit_depth_fragment_shader.glsl b/src/main/res/raw/blit_depth_fragment_shader.glsl
index 2e39264..f8cbd15 100644
--- a/src/main/res/raw/blit_depth_fragment_shader.glsl
+++ b/src/main/res/raw/blit_depth_fragment_shader.glsl
@@ -70,7 +70,7 @@ void insert2( vec2 ij, uint depth, uint rgba )
     {
     ptr = ptr /*uint(ij.x + ij.y * float(u_Size.x))*/ + uint(u_Size.x*u_Size.y);
 
-    u_Records[ptr   ] = (255u<<( (4*int(ij.x) > u_Size.x) ? 24u:16u)) + (255u);
+    u_Records[ptr   ] = (255u<<( (2*int(ij.x) > u_Size.x) ? 24u:16u)) + (255u);
     //u_Records[ptr+1u] = depth;
     //u_Records[ptr+2u] = rgba;
 
diff --git a/src/main/res/raw/blit_depth_render_fragment_shader.glsl b/src/main/res/raw/blit_depth_render_fragment_shader.glsl
index b44426f..4f607c0 100644
--- a/src/main/res/raw/blit_depth_render_fragment_shader.glsl
+++ b/src/main/res/raw/blit_depth_render_fragment_shader.glsl
@@ -74,8 +74,8 @@ void main()
     u_Records[index] = 0u;
     vec4 color = convert(u_Records[ptr]);
 
-         if( (4*int(v_Pixel.x) > u_Size.x) && color.r==1.0 ) FRAG_COLOR = color;
-    else if( (4*int(v_Pixel.x) <=u_Size.x) && color.g==1.0 ) FRAG_COLOR = color;
+         if( (2*int(v_Pixel.x) > u_Size.x) && color.r==1.0 ) FRAG_COLOR = color;
+    else if( (2*int(v_Pixel.x) <=u_Size.x) && color.g==1.0 ) FRAG_COLOR = color;
     else FRAG_COLOR = vec4(0.0,0.0,1.0,1.0);
     //FRAG_COLOR = convert(u_Records[index]);
     //u_Records[index] = 0u;
