commit 9ed801850838fe5ff42a5daa4a0f76435903e02a
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Mon May 22 15:45:00 2017 +0100

    Beginnings of support for multi-COLOR attachment Framebuffers.
    This will be used in OutputSurface's Postprocessing Buffer.

diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index ff77038..1286fb0 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -38,18 +38,22 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
     {
     if( mColorCreated==NOT_CREATED_YET )
       {
-      GLES30.glGenTextures(1, mColorH, 0);
-      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
-      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
-      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
-      GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
-      GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
-      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mWidth, mHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
-      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
-
+      GLES30.glGenTextures( mNumColors, mColorH, 0);
       GLES30.glGenFramebuffers(1, mFBOH, 0);
       GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
-      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
+
+      for(int i=0; i<mNumColors; i++)
+        {
+        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[i]);
+        GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
+        GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
+        GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
+        GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
+        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mWidth, mHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
+        GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0+i, GLES30.GL_TEXTURE_2D, mColorH[i], 0);
+        }
+
+      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
       GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
 
       mColorCreated = checkStatus("color");
@@ -162,9 +166,9 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
 // inside a Tree of DistortedNodes (TREE)
 // SYSTEM surfaces do not get removed in onDestroy().
 
-  DistortedFramebuffer(int depthStencil, int type, int width, int height)
+  DistortedFramebuffer(int numcolors, int depthStencil, int type, int width, int height)
     {
-    super(width,height,NOT_CREATED_YET,depthStencil,NOT_CREATED_YET, type);
+    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET, type);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -186,31 +190,32 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
- * Create a new offscreen Framebuffer.
+ * Create new offscreen Framebuffer with configurable number of COLOR, DEPTH and STENCIL attachments.
  *
- * @param width Width of the COLOR attachment.
- * @param height Height of the COLOR attachment.
+ * @param width        Width of all the COLOR attachments.
+ * @param height       Height of all the COLOR attachments.
+ * @param numcolors    How many COLOR attachments to create?
  * @param depthStencil Add DEPTH or STENCIL attachment?
  *                     Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
  */
   @SuppressWarnings("unused")
-  public DistortedFramebuffer(int width, int height, int depthStencil)
+  public DistortedFramebuffer(int width, int height, int numcolors, int depthStencil)
     {
-    super(width,height,NOT_CREATED_YET,depthStencil,NOT_CREATED_YET,TYPE_USER);
+    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET,TYPE_USER);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 /**
- * Create a new offscreen Framebuffer. No DEPTH or STENCIL buffer will be created.
+ * Create new offscreen Framebuffer with COLOR0 attachment only.
  *
- * @param width Width of the COLOR attachment.
- * @param height Height of the COLOR attachment.
+ * @param width Width of the COLOR0 attachment.
+ * @param height Height of the COLOR0 attachment.
  */
   @SuppressWarnings("unused")
   public DistortedFramebuffer(int width, int height)
     {
-    super(width,height,NOT_CREATED_YET, NO_DEPTH_NO_STENCIL,NOT_CREATED_YET,TYPE_USER);
+    super(width,height,NOT_CREATED_YET, 1, NO_DEPTH_NO_STENCIL,NOT_CREATED_YET,TYPE_USER);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/DistortedNode.java b/src/main/java/org/distorted/library/DistortedNode.java
index 4d1a2a0..c039d0a 100644
--- a/src/main/java/org/distorted/library/DistortedNode.java
+++ b/src/main/java/org/distorted/library/DistortedNode.java
@@ -248,7 +248,7 @@ public class DistortedNode implements DistortedSlave
       {
       int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
       int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
-      newData.mFBO = new DistortedFramebuffer(mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
+      newData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
       //android.util.Log.d("NODE", "creating new FBO "+newData.mFBO.getID() );
       }
 
@@ -322,7 +322,7 @@ public class DistortedNode implements DistortedSlave
         {
         int width  = mFboW <= 0 ? mSurface.getWidth()  : mFboW;
         int height = mFboH <= 0 ? mSurface.getHeight() : mFboH;
-        mData.mFBO = new DistortedFramebuffer(mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
+        mData.mFBO = new DistortedFramebuffer(1,mFboDepthStencil, DistortedSurface.TYPE_TREE, width, height);
         }
 
       mData.mFBO.setAsOutput(currTime);
@@ -451,7 +451,7 @@ public class DistortedNode implements DistortedSlave
           depthStencil = (hasStencil ? DistortedFramebuffer.BOTH_DEPTH_STENCIL:DistortedFramebuffer.DEPTH_NO_STENCIL);
           }
 
-        mSurface = new DistortedFramebuffer(depthStencil,DistortedSurface.TYPE_TREE,w,h);
+        mSurface = new DistortedFramebuffer(1,depthStencil,DistortedSurface.TYPE_TREE,w,h);
         }
       }
     if( (flags & Distorted.CLONE_CHILDREN) != 0 )
diff --git a/src/main/java/org/distorted/library/DistortedOutputSurface.java b/src/main/java/org/distorted/library/DistortedOutputSurface.java
index 8038a97..3003eb4 100644
--- a/src/main/java/org/distorted/library/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/DistortedOutputSurface.java
@@ -84,9 +84,9 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  DistortedOutputSurface(int width, int height, int createColor, int depthStencil, int fbo, int type)
+  DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
     {
-    super(width,height,createColor,type);
+    super(width,height,createColor,numcolors,type);
 
     mProjectionMatrix = new float[16];
 
@@ -191,8 +191,8 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
 
           for(int j=0; j<EffectQuality.LENGTH; j++)
             {
-            mBuffer1[j] = new DistortedFramebuffer(BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
-            mBuffer2[j] = new DistortedFramebuffer(BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
+            mBuffer1[j] = new DistortedFramebuffer(1,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
+            mBuffer2[j] = new DistortedFramebuffer(1,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
             mBuffer1[j].mMipmap = mipmap;
             mBuffer2[j].mMipmap = mipmap;
             mipmap *= EffectQuality.MULTIPLIER;
diff --git a/src/main/java/org/distorted/library/DistortedRenderState.java b/src/main/java/org/distorted/library/DistortedRenderState.java
index 6711768..57fba06 100644
--- a/src/main/java/org/distorted/library/DistortedRenderState.java
+++ b/src/main/java/org/distorted/library/DistortedRenderState.java
@@ -119,6 +119,10 @@ class DistortedRenderState
     {
     if( cState.colorMaskR!=1 || cState.colorMaskG!=1 || cState.colorMaskB!=1 || cState.colorMaskA!=1 )
       {
+      sState.colorMaskR = cState.colorMaskR;
+      sState.colorMaskG = cState.colorMaskG;
+      sState.colorMaskB = cState.colorMaskB;
+      sState.colorMaskA = cState.colorMaskA;
       cState.colorMaskR = 1;
       cState.colorMaskG = 1;
       cState.colorMaskB = 1;
@@ -127,11 +131,13 @@ class DistortedRenderState
       }
     if( cState.depthMask!=1 )
       {
+      sState.depthMask = cState.depthMask;
       cState.depthMask = 1;
       GLES30.glDepthMask(true);
       }
     if( cState.stencilMask!= STENCIL_MASK )
       {
+      sState.stencilMask = cState.stencilMask;
       cState.stencilMask = STENCIL_MASK;
       GLES30.glStencilMask(cState.stencilMask);
       }
diff --git a/src/main/java/org/distorted/library/DistortedScreen.java b/src/main/java/org/distorted/library/DistortedScreen.java
index 05f2609..9c5d0e3 100644
--- a/src/main/java/org/distorted/library/DistortedScreen.java
+++ b/src/main/java/org/distorted/library/DistortedScreen.java
@@ -53,7 +53,7 @@ public class DistortedScreen extends DistortedOutputSurface
     {
     // set color to 'DONT_CREATE' so that Screens will not get added to the Surface lists
     // set depth to 'CREATED' so that depth will be, by default, on.
-    super(0,0,DONT_CREATE,DEPTH_NO_STENCIL,0,TYPE_USER);
+    super(0,0,DONT_CREATE,1,DEPTH_NO_STENCIL,0,TYPE_USER);
 
     if( view!=null )
       {
diff --git a/src/main/java/org/distorted/library/DistortedSurface.java b/src/main/java/org/distorted/library/DistortedSurface.java
index 54c8da5..0aa0f23 100644
--- a/src/main/java/org/distorted/library/DistortedSurface.java
+++ b/src/main/java/org/distorted/library/DistortedSurface.java
@@ -24,19 +24,26 @@ package org.distorted.library;
 abstract class DistortedSurface extends DistortedObject
 {
   int mColorCreated;
-  int[] mColorH = new int[1];
+  int mNumColors;
+  int[] mColorH;
   int mWidth, mHeight;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  DistortedSurface(int width, int height, int create, int type)
+  DistortedSurface(int width, int height, int create, int numcolors, int type)
     {
     super(create,type);
 
+    mNumColors    = numcolors;
     mWidth        = width ;
     mHeight       = height;
     mColorCreated = create;
-    mColorH[0]    = 0;
+
+    if( mNumColors>0 )
+      {
+      mColorH = new int[mNumColors];
+      for( int i=0; i<mNumColors; i++ )  mColorH[i] = 0;
+      }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/DistortedTexture.java b/src/main/java/org/distorted/library/DistortedTexture.java
index 06b0e20..fa4b903 100644
--- a/src/main/java/org/distorted/library/DistortedTexture.java
+++ b/src/main/java/org/distorted/library/DistortedTexture.java
@@ -115,7 +115,7 @@ public class DistortedTexture extends DistortedSurface implements DistortedInput
 
   public DistortedTexture(int width, int height, int type)
     {
-    super(width,height,NOT_CREATED_YET,type);
+    super(width,height,NOT_CREATED_YET,1,type);
     mBmp= null;
     }
 
@@ -127,7 +127,7 @@ public class DistortedTexture extends DistortedSurface implements DistortedInput
  */
   public DistortedTexture(int width, int height)
     {
-    super(width,height,NOT_CREATED_YET,TYPE_USER);
+    super(width,height,NOT_CREATED_YET,1,TYPE_USER);
     mBmp= null;
     }
 
