commit 97020807d7f1e8aaa030d7474db66b130f24eebe
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri Jun 29 12:25:29 2018 +0100

    Start merging master and OIT.

diff --git a/src/main/java/org/distorted/library/main/DistortedEffects.java b/src/main/java/org/distorted/library/main/DistortedEffects.java
index d7c417f..fe9e865 100644
--- a/src/main/java/org/distorted/library/main/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/main/DistortedEffects.java
@@ -63,6 +63,13 @@ public class DistortedEffects
     mQuadPositions.put(positionData).position(0);
     }
 
+  /// BLIT DEPTH PROGRAM ///
+  private static DistortedProgram mBlitDepthProgram;
+  private static int mBlitDepthTextureH;
+  private static int mBlitDepthDepthTextureH;
+  private static int mBlitDepthDepthH;
+  private static int mBlitDepthTexCorrH;
+
   /// OIT SSBO BUFFER ///
   private static int[] mLinkedListSSBO = new int[1];
   private static int[] mAtomicCounter = new int[1];
@@ -179,6 +186,26 @@ public class DistortedEffects
     mBlitTextureH  = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
     mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
 
+    // 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);
+
+    try
+      {
+      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
+      }
+    catch(Exception e)
+      {
+      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
+      throw new RuntimeException(e.getMessage());
+      }
+
+    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");
+
     // OIT CLEAR PROGRAM ////////////////////////////////////
     final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
     final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader);
@@ -430,6 +457,21 @@ public class DistortedEffects
     GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
+    {
+    mBlitDepthProgram.useProgram();
+
+    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
+    GLES31.glUniform1i(mBlitDepthTextureH, 0);
+    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
+    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
+    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);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // reset atomic counter to 0
 
diff --git a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
index f2d92f4..ad5bf80 100644
--- a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
@@ -261,6 +261,43 @@ public abstract class DistortedOutputSurface extends DistortedSurface implements
       }
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private int blitWithDepth(long currTime, DistortedOutputSurface buffer,int fbo)
+    {
+    GLES31.glViewport(0, 0, mWidth, mHeight);
+    setAsOutputFBO(currTime,fbo);
+    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
+    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo]);
+    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
+    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[fbo]);
+
+    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
+    GLES31.glStencilMask(0x00);
+
+    DistortedEffects.blitDepthPriv(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
+    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
+    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
+    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
+    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
+
+    // clear buffers
+    GLES31.glStencilMask(0xff);
+    GLES31.glDepthMask(true);
+    GLES31.glColorMask(true,true,true,true);
+    GLES31.glClearColor(0.0f,0.0f,0.0f,0.0f);
+    GLES31.glClearDepthf(1.0f);
+    GLES31.glClearStencil(0);
+
+    buffer.setAsOutputFBO(fbo);
+    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo+1], 0);
+    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
+    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[2*fbo  ], 0);
+    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
+
+    return 1;
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private static void oitClear(DistortedOutputSurface buffer)
diff --git a/src/main/res/raw/blit_depth_fragment_shader.glsl b/src/main/res/raw/blit_depth_fragment_shader.glsl
new file mode 100644
index 0000000..889ccef
--- /dev/null
+++ b/src/main/res/raw/blit_depth_fragment_shader.glsl
@@ -0,0 +1,34 @@
+//////////////////////////////////////////////////////////////////////////////////////////////
+// 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/>.                       //
+//////////////////////////////////////////////////////////////////////////////////////////////
+
+precision highp float;
+
+out vec4 fragColor;           // The output color
+in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
+
+uniform sampler2D u_Texture;
+uniform sampler2D u_DepthTexture;
+
+//////////////////////////////////////////////////////////////////////////////////////////////
+
+void main()                    		
+  {
+  gl_FragDepth = texture(u_DepthTexture,v_TexCoordinate).r;
+  fragColor    = texture(u_Texture     ,v_TexCoordinate);
+  }
\ 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
new file mode 100644
index 0000000..4047179
--- /dev/null
+++ b/src/main/res/raw/blit_depth_vertex_shader.glsl
@@ -0,0 +1,37 @@
+//////////////////////////////////////////////////////////////////////////////////////////////
+// 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/>.                       //
+//////////////////////////////////////////////////////////////////////////////////////////////
+
+precision highp float;
+
+in vec2 a_Position;           // Per-vertex position.
+out vec2 v_TexCoordinate;     //
+
+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.
+
+//////////////////////////////////////////////////////////////////////////////////////////////
+
+void main()
+  {
+  v_TexCoordinate = (a_Position + 0.5) * u_TexCorr;
+  gl_Position     = vec4(2.0*a_Position,u_Depth,1.0);
+  }
