commit 66a0fa172a14481cf53953aa14544f8bca10cf37
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Fri May 11 13:53:09 2018 +0100

    OIT: some progress

diff --git a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
index 0d5ac51..6de2cd8 100644
--- a/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/main/DistortedOutputSurface.java
@@ -218,70 +218,6 @@ public static final int DEBUG_FPS = 1;
       }
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private static void initializeOIT(DistortedOutputSurface surface)
-    {
-    if( mBufferOIT==null )
-      {
-      mBufferOIT = new DistortedFramebuffer(1, BOTH_DEPTH_STENCIL, TYPE_SYST, surface.mWidth, surface.mHeight);
-      mBufferOIT.mMipmap = 1.0f;
-      mBufferOIT.mNear = surface.mNear;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
-      mBufferOIT.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
-
-      DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
-
-      mBufferOIT.setAsOutput();
-      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBufferOIT.mColorH[0], 0);
-      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT | GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_STENCIL_BUFFER_BIT);
-      }
-
-    if( mBufferOIT.mWidth != surface.mWidth || mBufferOIT.mHeight != surface.mHeight )
-      {
-      mBufferOIT.mWidth  = (int)(surface.mWidth *mBufferOIT.mMipmap);
-      mBufferOIT.mHeight = (int)(surface.mHeight*mBufferOIT.mMipmap);
-
-      mBufferOIT.mNear   = surface.mNear;  // Near plane is independent of the mipmap level
-
-      //android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
-
-      mBufferOIT.createProjection();
-
-      int maxw = mBufferOIT.mWidth  > mBufferOIT.mRealWidth  ? mBufferOIT.mWidth  : mBufferOIT.mRealWidth;
-      int maxh = mBufferOIT.mHeight > mBufferOIT.mRealHeight ? mBufferOIT.mHeight : mBufferOIT.mRealHeight;
-
-      if (maxw > mBufferOIT.mRealWidth || maxh > mBufferOIT.mRealHeight)
-        {
-        mBufferOIT.mRealWidth = maxw;
-        mBufferOIT.mRealHeight = maxh;
-
-        mBufferOIT.recreate();
-        mBufferOIT.create();
-        }
-      }
-
-    if( mBufferOIT.mNear != surface.mNear || mBufferOIT.mFOV != surface.mFOV )
-      {
-      mBufferOIT.mNear = surface.mNear;
-      mBufferOIT.mFOV  = surface.mFOV;
-      mBufferOIT.createProjection();
-      }
-
-    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mBufferOIT.mFBOH[0]);
-
-    DistortedRenderState.colorDepthStencilOn();
-    GLES31.glClearColor(surface.mClearR, surface.mClearG, surface.mClearB, surface.mClearA);
-    GLES31.glClearDepthf(surface.mClearDepth);
-    GLES31.glClearStencil(surface.mClearStencil);
-    GLES31.glClear(surface.mClear);
-    DistortedRenderState.colorDepthStencilRestore();
-
-    DistortedEffects.zeroOutAtomic();
-    DistortedEffects.oitClear(surface);
-
-    GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   static synchronized void onDestroy()
@@ -334,6 +270,65 @@ public static final int DEBUG_FPS = 1;
       }
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  private static void oitClear(DistortedOutputSurface buffer)
+    {
+    if( mBufferOIT==null )
+      {
+      mBufferOIT = new DistortedFramebuffer(1, BOTH_DEPTH_STENCIL, TYPE_SYST, buffer.mWidth, buffer.mHeight);
+      mBufferOIT.mMipmap = 1.0f;
+      mBufferOIT.mNear = buffer.mNear;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
+      mBufferOIT.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
+
+      DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
+      }
+
+    if( mBufferOIT.mWidth != buffer.mWidth || mBufferOIT.mHeight != buffer.mHeight )
+      {
+      mBufferOIT.mWidth  = (int)(buffer.mWidth *mBufferOIT.mMipmap);
+      mBufferOIT.mHeight = (int)(buffer.mHeight*mBufferOIT.mMipmap);
+
+      mBufferOIT.mNear   = buffer.mNear;  // Near plane is independent of the mipmap level
+
+      //android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
+
+      mBufferOIT.createProjection();
+
+      int maxw = mBufferOIT.mWidth  > mBufferOIT.mRealWidth  ? mBufferOIT.mWidth  : mBufferOIT.mRealWidth;
+      int maxh = mBufferOIT.mHeight > mBufferOIT.mRealHeight ? mBufferOIT.mHeight : mBufferOIT.mRealHeight;
+
+      if (maxw > mBufferOIT.mRealWidth || maxh > mBufferOIT.mRealHeight)
+        {
+        mBufferOIT.mRealWidth = maxw;
+        mBufferOIT.mRealHeight = maxh;
+
+        mBufferOIT.recreate();
+        mBufferOIT.create();
+        }
+      }
+
+    if( mBufferOIT.mNear != buffer.mNear || mBufferOIT.mFOV != buffer.mFOV )
+      {
+      mBufferOIT.mNear = buffer.mNear;
+      mBufferOIT.mFOV  = buffer.mFOV;
+      mBufferOIT.createProjection();
+      }
+
+    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mBufferOIT.mFBOH[0]);
+    DistortedRenderState.colorDepthStencilOn();
+    GLES31.glClearColor(buffer.mClearR, buffer.mClearG, buffer.mClearB, buffer.mClearA);
+    GLES31.glClearDepthf(buffer.mClearDepth);
+    GLES31.glClearStencil(buffer.mClearStencil);
+    GLES31.glClear(buffer.mClear);
+    DistortedRenderState.colorDepthStencilRestore();
+
+    DistortedEffects.zeroOutAtomic();
+    DistortedEffects.oitClear(buffer);
+
+    GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   private int oitBuild(DistortedOutputSurface buffer)
@@ -387,7 +382,7 @@ public static final int DEBUG_FPS = 1;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private void clearBuffer(DistortedOutputSurface buffer)
+  private static void clearBuffer(DistortedOutputSurface buffer)
     {
     GLES31.glStencilMask(0xff);
     GLES31.glDepthMask(true);
@@ -397,10 +392,14 @@ public static final int DEBUG_FPS = 1;
     GLES31.glClearStencil(0);
 
     buffer.setAsOutput();
-    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[1], 0);
+    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[buffer.mNumColors-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[0], 0);
-    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
+
+    for(int i=buffer.mNumColors-2; i>=0; i--)
+      {
+      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[i], 0);
+      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -415,7 +414,7 @@ public static final int DEBUG_FPS = 1;
     EffectQueuePostprocess lastQueue=null, currQueue;
     long lastBucket=0, currBucket=0;
 
-    initializeOIT(this);
+    oitClear(this);
 
     for(int i=0; i<numChildren; i++)
       {
