commit e029600f8559d43f5989b785a5102924aaecb34e
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Apr 27 17:40:39 2018 +0100

    still debugging the OIT

diff --git a/src/main/java/org/distorted/library/main/DistortedEffects.java b/src/main/java/org/distorted/library/main/DistortedEffects.java
index 2724d8e..32d7e5f 100644
--- a/src/main/java/org/distorted/library/main/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/main/DistortedEffects.java
@@ -70,12 +70,7 @@ public class DistortedEffects
 
   /// BLIT DEPTH PROGRAM ///
   private static DistortedProgram mBlitDepthProgram;
-  private static int mBlitDepthTextureH;
-  private static int mBlitDepthDepthTextureH;
-  private static int mBlitDepthDepthH;
-  private static int mBlitDepthTexCorrH;
   private static int mBlitDepthSizeH;
-  private static int mBlitDepthNumRecordsH;
 
   private static int[] mLinkedListSSBO = new int[1];
   private static int[] mAtomicCounter = new int[1];
@@ -95,9 +90,6 @@ private static IntBuffer mIntBuf;
 
   /// BLIT DEPTH RENDER PROGRAM ///
   private static DistortedProgram mBlitDepthRenderProgram;
-  private static int mBlitDepthRenderDepthTextureH;
-  private static int mBlitDepthRenderDepthH;
-  private static int mBlitDepthRenderTexCorrH;
   private static int mBlitDepthRenderSizeH;
 
   /// NORMAL PROGRAM /////
@@ -192,12 +184,7 @@ private static IntBuffer mIntBuf;
       }
 
     int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
-    mBlitDepthTextureH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
-    mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
-    mBlitDepthDepthH        = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
-    mBlitDepthTexCorrH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
     mBlitDepthSizeH         = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Size");
-    mBlitDepthNumRecordsH   = GLES31.glGetUniformLocation( blitDepthProgramH, "u_numRecords");
 
     mIntBuffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
     mIntBuffer.put(0,0);
@@ -237,9 +224,6 @@ private static IntBuffer mIntBuf;
       }
 
     int blitDepthRenderProgramH   = mBlitDepthRenderProgram.getProgramHandle();
-    mBlitDepthRenderDepthTextureH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_DepthTexture");
-    mBlitDepthRenderDepthH        = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Depth");
-    mBlitDepthRenderTexCorrH      = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_TexCorr");
     mBlitDepthRenderSizeH         = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size");
 
     // NORMAL PROGRAM //////////////////////////////////////
@@ -402,12 +386,7 @@ private static IntBuffer mIntBuf;
     mBlitDepthProgram.useProgram();
 
     GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
-    GLES31.glUniform1i(mBlitDepthTextureH, 0);
-    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
-    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
-    GLES31.glUniform2i(mBlitDepthSizeH, surface.mWidth, surface.mHeight);
-    GLES31.glUniform1ui(mBlitDepthNumRecordsH, (mBufferSize-surface.mWidth*surface.mHeight)/3 );  // see the fragment shader
-    GLES31.glUniform1f(mBlitDepthDepthH , 1.0f-surface.mNear);
+    GLES31.glUniform2f(mBlitDepthSizeH, surface.mWidth, surface.mHeight);
     GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
     GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
     }
@@ -420,13 +399,10 @@ private static IntBuffer mIntBuf;
     {
     mBlitDepthRenderProgram.useProgram();
 
-    analyzeBuffer(surface.mWidth, surface.mHeight);
+    //analyzeBuffer(surface.mWidth, surface.mHeight);
 
     GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
-    GLES31.glUniform1i(mBlitDepthRenderDepthTextureH, 1);
-    GLES31.glUniform2f(mBlitDepthRenderTexCorrH, corrW, corrH );
-    GLES31.glUniform2i(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight);
-    GLES31.glUniform1f( mBlitDepthRenderDepthH , 1.0f-surface.mNear);
+    GLES31.glUniform2f(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight);
     GLES31.glVertexAttribPointer(mBlitDepthRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
     GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
 
@@ -442,8 +418,8 @@ private static IntBuffer mIntBuf;
   private static void analyzeBuffer(int w, int h)
     {
     int ptr;
-    final int GREEN = (255<<16) + 255;
-    final int RED   = (255<<24) + 255;
+    final int GREEN = 1;
+    final int RED   = 2;
 
     int left1=0, left2=0;
     int right1=0, right2=0;
diff --git a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
index 895643a..73742f6 100644
--- a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
@@ -386,11 +386,9 @@ 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();
+          //GLES31.glFlush();
           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 f8cbd15..4ea3289 100644
--- a/src/main/res/raw/blit_depth_fragment_shader.glsl
+++ b/src/main/res/raw/blit_depth_fragment_shader.glsl
@@ -20,143 +20,28 @@
 precision highp float;
 precision highp int;
 
-#if __VERSION__ != 100
-out vec4 fragColor;           // The output color
-in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
-in vec2 v_Pixel;              // location of the current fragment, in pixels
-#define TEXTURE texture
-#define FRAG_COLOR fragColor
-#else
-varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
-varying vec2 v_Pixel;         // location of the current fragment, in pixels
-#define TEXTURE texture2D
-#define FRAG_COLOR gl_FragColor
-#endif
+out vec4 fragColor;
+in vec2 v_TexCoordinate;
 
-uniform sampler2D u_Texture;
-uniform sampler2D u_DepthTexture;
+uniform vec2 u_Size;
 
-//////////////////////////////////////////////////////////////////////////////////////////////
-// per-pixel linked list. Order Independent Transparency.
-
-uniform ivec2 u_Size;
-uniform uint u_numRecords;
-
-layout (binding=0, offset=0) uniform atomic_uint u_Counter;  // initialize to 0
-
-layout (std430,binding=1) buffer linkedlist  // first (u_Size.x*u_Size.y) uints - head pointers,
-  {                                          // one for each pixel in the Output rectangle.
-  uint u_Records[];                          //
-  };                                         // Next 3*u_numRecords uints - actual linked list, i.e.
-                                             // triplets of (pointer,depth,rgba).
-
-//////////////////////////////////////////////////////////////////////////////////////////////
-// Concurrent insert to a linked list. Tim Harris, 'pragmatic implementation of non-blocking
-// linked-lists', 2001.
-// This arranges fragments by decreasing 'depth', so one would think - from back to front, but
-// in main() below the depth is mapped with S*(1-depth)/2, so it is really front to back.
-
-void insert2( vec2 ij, uint depth, uint rgba )
-  {
-/*
-  uint index = uint(ij.x + ij.y * float(u_Size.x));
-  u_Records[index] = (255u<<24u)+ uint( (int(255*int(ij.y)/u_Size.y))<<16u ) + (255u);
-  discard;
-*/
-
-  uint ptr = atomicCounterIncrement(u_Counter);
-
-  if( ptr<u_numRecords )
-    {
-    ptr = ptr /*uint(ij.x + ij.y * float(u_Size.x))*/ + uint(u_Size.x*u_Size.y);
-
-    u_Records[ptr   ] = (255u<<( (2*int(ij.x) > u_Size.x) ? 24u:16u)) + (255u);
-    //u_Records[ptr+1u] = depth;
-    //u_Records[ptr+2u] = rgba;
-
-    uint index = uint(ij.x + ij.y * float(u_Size.x));
-    u_Records[index] = ptr;
-    discard;
-    }
-  }
-
-void insert( vec2 ij, uint depth, uint rgba )
-  {
-  uint ptr = atomicCounterIncrement(u_Counter);
+layout (binding=0, offset=0) uniform atomic_uint u_Counter;
 
-  if( ptr<u_numRecords )
-    {
-    ptr = 3u*ptr + uint(u_Size.x*u_Size.y);
-
-    u_Records[ptr   ] = 0u;
-    u_Records[ptr+1u] = depth;
-    u_Records[ptr+2u] = rgba;
-
-    uint index = uint(ij.x + ij.y * float(u_Size.x));
-
-    u_Records[index] = ptr;
-    }
-/*
-  uint ptr = atomicCounterIncrement(u_Counter)-1u;
-
-  if( ptr<u_numRecords )
-    {
-    ptr = 3u*ptr + uint(u_Size.x*u_Size.y);
-
-    u_Records[ptr+1u] = depth;
-    u_Records[ptr+2u] = rgba;
-
-    memoryBarrier();
-
-    uint prev = uint(ij.x + ij.y * float(u_Size.x));
-    uint curr = u_Records[prev];
-/*
-    while (true)
-      {
-      if ( curr==0u || depth > u_Records[curr+1u] )  // need to insert here
-        {
-
-        u_Records[ptr] = curr; // next of new record is curr
-        memoryBarrier();
-        uint res = atomicCompSwap( u_Records[prev], curr, ptr );
-/*
-        if (res==curr) break;      // done!
-        else           curr = res; // could not insert! retry from same place in list
-        }
-      else // advance in list
-        {
-        prev = curr;
-        curr = u_Records[prev];
-        }
-      }
-*/
-//    }
-
-
-  }
-
-//////////////////////////////////////////////////////////////////////////////////////////////
-
-uint convert(vec4 c)
+layout (std430,binding=1) buffer linkedlist
   {
-  return ((uint(255.0*c.r))<<24u) + ((uint(255.0*c.g))<<16u) + ((uint(255.0*c.b))<<8u) + uint(255.0*c.a);
-  }
+  uint u_Records[];
+  };
 
 //////////////////////////////////////////////////////////////////////////////////////////////
 
 void main()                    		
   {
-  vec4 frag  = TEXTURE(u_Texture     , v_TexCoordinate);
-  float depth= TEXTURE(u_DepthTexture, v_TexCoordinate).r;
-/*
-  if( frag.a > 0.95 )
-    {
-    gl_FragDepth = depth;
-    FRAG_COLOR   = frag;
-    }
-  else if( frag.a > 0.0 )
-    {*/
-    const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough.
-    insert2(v_Pixel, uint(S*(1.0-depth)/2.0), convert(frag) );
-    //}
+  uint pixelX = uint(v_TexCoordinate.x * u_Size.x);
+  uint pixelY = uint(v_TexCoordinate.y * u_Size.y);
+  uint index = pixelX + pixelY * uint(u_Size.x);
+
+  uint ptr = atomicCounterIncrement(u_Counter) + uint(u_Size.x*u_Size.y);
+  u_Records[ptr  ] = (v_TexCoordinate.x>0.5?2u:1u);
+  u_Records[index] = ptr;
+  discard;
   }
\ No newline at end of file
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 4f607c0..5b9a7ca 100644
--- a/src/main/res/raw/blit_depth_render_fragment_shader.glsl
+++ b/src/main/res/raw/blit_depth_render_fragment_shader.glsl
@@ -20,97 +20,27 @@
 precision highp float;
 precision highp int;
 
-#if __VERSION__ != 100
-out vec4 fragColor;           // The output color
-in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
-in vec2 v_Pixel;              // location of the current fragment, in pixels
-#define TEXTURE texture
-#define FRAG_COLOR fragColor
-#else
-varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
-varying vec2 v_Pixel;         // location of the current fragment, in pixels
-#define TEXTURE texture2D
-#define FRAG_COLOR gl_FragColor
-#endif
+out vec4 fragColor;
+in vec2 v_TexCoordinate;
 
-uniform sampler2D u_DepthTexture;
+uniform vec2 u_Size;
 
-//////////////////////////////////////////////////////////////////////////////////////////////
-// per-pixel linked list. Order Independent Transparency.
-
-uniform ivec2 u_Size;
-
-layout (std430,binding=1) buffer linkedlist  // first (u_Size.x*u_Size.y) uints - head pointers,
-  {                                          // one for each pixel in the Output rectangle.
-  uint u_Records[];                          //
-  };                                         // Next 3*u_numRecords uints - actual linked list, i.e.
-                                             // triplets of (pointer,depth,rgba).
-
-//////////////////////////////////////////////////////////////////////////////////////////////
-
-vec4 convert(uint rgba)
+layout (std430,binding=1) buffer linkedlist
   {
-  return vec4( float((rgba>>24u)&255u),float((rgba>>16u)&255u),float((rgba>>8u)&255u),float(rgba&255u) ) / 255.0;
-  }
-
-//////////////////////////////////////////////////////////////////////////////////////////////
-// https://en.wikipedia.org/wiki/Alpha_compositing (premultiplied)
-
-vec4 blend(vec4 clr,vec4 srf)
-  {
-  return clr + (1.0 - clr.a) * vec4(srf.rgb * srf.a , srf.a);
-  }
+  uint u_Records[];
+  };
 
 //////////////////////////////////////////////////////////////////////////////////////////////
 
 void main()                    		
   {
-  uint index = uint(v_Pixel.x + v_Pixel.y * float(u_Size.x));
-  uint ptr = u_Records[index];
+  uint pixelX = uint(v_TexCoordinate.x * u_Size.x);
+  uint pixelY = uint(v_TexCoordinate.y * u_Size.y);
+  uint index  = pixelX + pixelY * uint(u_Size.x);
 
-  if( ptr == 0u ) discard;
-  else
-    {
-    u_Records[index] = 0u;
-    vec4 color = convert(u_Records[ptr]);
-
-         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;
-    }
-
-  /*
-  uint index = uint(v_Pixel.x + v_Pixel.y * float(u_Size.x));
-  uint curr = u_Records[index];
-
-  if (curr == 0u) discard;
-
-  vec4 color= vec4(0.0,0.0,0.0,0.0);
-  u_Records[index] = 0u;
-
-  while (curr > 0u)                                      // keep walking the linked list
-    {                                                    // and blending the colors in
-    curr = u_Records[curr];                              //
-    //color= blend( color, convert(u_Records[curr+2u]) );  //
-
-    color = convert(u_Records[curr+2u]);
-
-    if( color.a == 0.0 ) color = vec4(0.0,1.0,0.0,1.0);
-    }
-
-
-  if( curr>0u )
-    {
-    curr = u_Records[curr];
-    color= convert(u_Records[curr+2u]);
-    }
-
-  gl_FragDepth = TEXTURE(u_DepthTexture, v_TexCoordinate).r;
-  FRAG_COLOR = color;
+  uint ptr = u_Records[index];
 
-//else
-//  FRAG_COLOR = vec4(1.0,0.0,0.0,1.0);
-*/
+       if( u_Records[ptr]==2u ) fragColor = vec4(1.0,0.0,0.0,1.0);
+  else if( u_Records[ptr]==1u ) fragColor = vec4(0.0,1.0,0.0,1.0);
+  else                          fragColor = vec4(0.0,0.0,1.0,1.0);
   }
\ No newline at end of file
diff --git a/src/main/res/raw/blit_depth_vertex_shader.glsl b/src/main/res/raw/blit_depth_vertex_shader.glsl
index f1f5d57..f5311ca 100644
--- a/src/main/res/raw/blit_depth_vertex_shader.glsl
+++ b/src/main/res/raw/blit_depth_vertex_shader.glsl
@@ -20,29 +20,13 @@
 precision highp float;
 precision highp int;
 
-#if __VERSION__ != 100
-in vec2 a_Position;           // Per-vertex position.
-out vec2 v_TexCoordinate;     //
-out vec2 v_Pixel;             //
-#else
-attribute vec2 a_Position;    // Per-vertex position.
-varying vec2 v_TexCoordinate; //
-varying vec2 v_Pixel;         //
-#endif
-
-uniform float u_Depth;        // distance from the near plane to render plane, in clip coords
-uniform vec2  u_TexCorr;      // when we blit from postprocessing buffers, the buffers can be
-                              // larger than necessary (there is just one static set being
-                              // reused!) so we need to compensate here by adjusting the texture
-                              // coords.
-
-uniform ivec2 u_Size;         // size of the output surface, in pixels.
+in vec2 a_Position;
+out vec2 v_TexCoordinate;
 
 //////////////////////////////////////////////////////////////////////////////////////////////
 
 void main()
   {
-  v_TexCoordinate = (a_Position + 0.5) * u_TexCorr;
-  v_Pixel         = v_TexCoordinate * vec2(u_Size);
-  gl_Position     = vec4(2.0*a_Position,u_Depth,1.0);
+  v_TexCoordinate = a_Position + 0.5;
+  gl_Position     = vec4(2.0*a_Position,1.0,1.0);
   }
diff --git a/src/main/res/raw/main_fragment_shader.glsl b/src/main/res/raw/main_fragment_shader.glsl
index 52af919..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 mediump float;
+precision highp float;
 
 #if __VERSION__ != 100
 in vec3 v_Position;                     // Interpolated position for this fragment.
diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index e5e152a..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 mediump float;
+precision highp float;
 
 #if __VERSION__ != 100
 in vec3 a_Position;                  // Per-vertex position.
