commit 94f6d47251742f6979b213f6fb4c4ddd46b10237
Author: Leszek Koltunski <leszek@distoretedandroid.org>
Date:   Wed Mar 29 12:05:10 2017 +0100

    Make it more flexible; now it can run almost all apps on OpenGL 2.0 contexts; OpenGL 3.0 ( with GLSL 3.00) required for POSTPROCESSING.

diff --git a/src/main/java/org/distorted/library/Distorted.java b/src/main/java/org/distorted/library/Distorted.java
index 3663dfa..9dc833f 100644
--- a/src/main/java/org/distorted/library/Distorted.java
+++ b/src/main/java/org/distorted/library/Distorted.java
@@ -29,7 +29,9 @@ import org.distorted.library.program.*;
  */
 public class Distorted 
   {
-  static final String glslVersion="#version 300 es\n";
+  static int GLSL;
+  static String GLSL_VERSION;
+
   /**
    * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
    * backing up our DistortedTexture.
@@ -104,6 +106,9 @@ public class Distorted
   public static void onCreate(final Context context)
   throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
     {
+    GLSL = 100;
+    GLSL_VERSION= (GLSL==100 ? "#version 100\n" : "#version 300 es\n");
+
     final Resources resources = context.getResources();
     DistortedEffects.createProgram(resources);
     EffectQueuePostprocess.createProgram(resources);
diff --git a/src/main/java/org/distorted/library/DistortedEffects.java b/src/main/java/org/distorted/library/DistortedEffects.java
index 8fc92dc..e6c75b7 100644
--- a/src/main/java/org/distorted/library/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/DistortedEffects.java
@@ -110,8 +110,8 @@ public class DistortedEffects
     final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
     final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
 
-    String mainVertHeader= Distorted.glslVersion;
-    String mainFragHeader= Distorted.glslVersion;
+    String mainVertHeader= Distorted.GLSL_VERSION;
+    String mainFragHeader= Distorted.GLSL_VERSION;
 
     EffectNames name;
     EffectTypes type;
@@ -144,7 +144,7 @@ public class DistortedEffects
     //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
     //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
 
-    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader);
+    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL);
 
     int mainProgramH = mMainProgram.getProgramHandle();
     EffectQueueFragment.getUniforms(mainProgramH);
@@ -156,12 +156,12 @@ public class DistortedEffects
     final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
     final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
 
-    String blitVertHeader= (Distorted.glslVersion + "#define NUM_VERTEX 0\n"  );
-    String blitFragHeader= (Distorted.glslVersion + "#define NUM_FRAGMENT 0\n");
+    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
+    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
 
     try
       {
-      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader);
+      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
       }
     catch(Exception e)
       {
@@ -179,7 +179,7 @@ public class DistortedEffects
 
     try
       {
-      mDebugProgram = new DistortedProgram(debugVertexStream,debugFragmentStream, Distorted.glslVersion, Distorted.glslVersion);
+      mDebugProgram = new DistortedProgram(debugVertexStream,debugFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
       }
     catch(Exception e)
       {
diff --git a/src/main/java/org/distorted/library/EffectQueuePostprocess.java b/src/main/java/org/distorted/library/EffectQueuePostprocess.java
index abe5fb5..88cf8a5 100644
--- a/src/main/java/org/distorted/library/EffectQueuePostprocess.java
+++ b/src/main/java/org/distorted/library/EffectQueuePostprocess.java
@@ -123,8 +123,8 @@ class EffectQueuePostprocess extends EffectQueue
     try
       {
       mBlur1Program = new DistortedProgram(blur1VertexStream,blur1FragmentStream,
-                                          Distorted.glslVersion,
-                                          Distorted.glslVersion+"#define MAX_BLUR "+MAX_BLUR);
+                                          Distorted.GLSL_VERSION,
+                                          Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_BLUR, Distorted.GLSL);
       }
     catch(Exception e)
       {
@@ -145,13 +145,14 @@ class EffectQueuePostprocess extends EffectQueue
     try
       {
       mBlur2Program = new DistortedProgram(blur2VertexStream,blur2FragmentStream,
-                                          Distorted.glslVersion,
-                                          Distorted.glslVersion + "#define MAX_BLUR "+MAX_BLUR);
+                                          Distorted.GLSL_VERSION,
+                                          Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_BLUR, Distorted.GLSL);
       }
     catch(Exception e)
       {
       android.util.Log.e("EFFECTS", "exception trying to compile BLUR2 program: "+e.getMessage());
-      throw new RuntimeException(e.getMessage());
+      // run anyway as compiling Blur2 WILL fail on OpenGL 2.0 contexts
+      mBlur2Program = mBlur1Program;
       }
 
     int blur2ProgramH = mBlur2Program.getProgramHandle();
diff --git a/src/main/java/org/distorted/library/program/DistortedProgram.java b/src/main/java/org/distorted/library/program/DistortedProgram.java
index 249172c..825f56d 100644
--- a/src/main/java/org/distorted/library/program/DistortedProgram.java
+++ b/src/main/java/org/distorted/library/program/DistortedProgram.java
@@ -35,8 +35,10 @@ import java.io.InputStreamReader;
  */
 public class DistortedProgram
   {
-  private int mProgramHandle;
+  private String mAttributeStr;
+  private int mAttributeLen;
 
+  private int mProgramHandle;
   private int mNumAttributes;
   private String[] mAttributeName;
 
@@ -105,16 +107,16 @@ public class DistortedProgram
       if( currChar==';') break;
       }
 
-    if( semicolon<len && semicolon-whiteSpace>=4 )   // "in a;" --> 4
+    if( semicolon<len && semicolon-whiteSpace>=mAttributeLen+1 )
       {
       String subline = line.substring(whiteSpace,semicolon);
       int subLen = semicolon-whiteSpace;
 
-      if( subline.startsWith("in "))
+      if( subline.startsWith(mAttributeStr))
         {
         //android.util.Log.e("program", "GOOD LINE: " +subline+" subLen="+subLen);
 
-        for(nameBegin=subLen-1; nameBegin>1; nameBegin--)
+        for(nameBegin=subLen-1; nameBegin>mAttributeLen-2; nameBegin--)
           {
           currChar=subline.charAt(nameBegin);
 
@@ -260,9 +262,12 @@ public class DistortedProgram
    * @throws LinkingException
    */
 
-  public DistortedProgram(final InputStream vertex, final InputStream fragment, final String vertexHeader, final String fragmentHeader)
+  public DistortedProgram(final InputStream vertex, final InputStream fragment, final String vertexHeader, final String fragmentHeader, int glslVersion)
   throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
     {
+    mAttributeStr = (glslVersion == 100 ? "attribute " : "in ");
+    mAttributeLen = mAttributeStr.length();
+
     mNumAttributes = 0;
 
     final String vertexShader   = readTextFileFromRawResource(vertex  , true );
diff --git a/src/main/res/raw/blit_fragment_shader.glsl b/src/main/res/raw/blit_fragment_shader.glsl
index 30094a8..37124cc 100644
--- a/src/main/res/raw/blit_fragment_shader.glsl
+++ b/src/main/res/raw/blit_fragment_shader.glsl
@@ -19,13 +19,26 @@
 
 precision lowp float;
 
-in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
+#if __VERSION__ != 100
+#define TEXTURE texture
 out vec4 fragColor;           // The output color
+in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
+#else
+#define TEXTURE texture2D
+varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
+#endif
+
 uniform sampler2D u_Texture;  // The input texture.
 
 //////////////////////////////////////////////////////////////////////////////////////////////
 
 void main()                    		
-  {  
-  fragColor = texture(u_Texture,v_TexCoordinate);
+  {
+#if __VERSION__ != 100
+  fragColor    =
+#else
+  gl_FragColor =
+#endif
+
+  TEXTURE(u_Texture,v_TexCoordinate);
   }
\ No newline at end of file
diff --git a/src/main/res/raw/blit_vertex_shader.glsl b/src/main/res/raw/blit_vertex_shader.glsl
index f3e6cc4..7080267 100644
--- a/src/main/res/raw/blit_vertex_shader.glsl
+++ b/src/main/res/raw/blit_vertex_shader.glsl
@@ -19,9 +19,15 @@
 
 precision lowp float;
 
-uniform float u_Depth;    // distance from the near plane to render plane, in clip coords
-in vec2 a_Position;       // Per-vertex position.
-out vec2 v_TexCoordinate; //
+#if __VERSION__ != 100
+in vec2 a_Position;           // Per-vertex position.
+out vec2 v_TexCoordinate;     //
+#else
+attribute vec2 a_Position;    // Per-vertex position.
+varying vec2 v_TexCoordinate; //
+#endif
+
+uniform float u_Depth;        // distance from the near plane to render plane, in clip coords
 
 //////////////////////////////////////////////////////////////////////////////////////////////
 
diff --git a/src/main/res/raw/blur1_fragment_shader.glsl b/src/main/res/raw/blur1_fragment_shader.glsl
index fb12907..742225e 100644
--- a/src/main/res/raw/blur1_fragment_shader.glsl
+++ b/src/main/res/raw/blur1_fragment_shader.glsl
@@ -19,8 +19,15 @@
 
 precision lowp float;
 
+#if __VERSION__ != 100
+#define TEXTURE texture
 in vec2 v_TexCoordinate;
 out vec4 fragColor;
+#else
+#define TEXTURE texture2D
+varying vec2 v_TexCoordinate;
+#endif
+
 uniform sampler2D u_ColorTexture;
 uniform float u_Offsets[MAX_BLUR];
 uniform float u_Weights[MAX_BLUR];
@@ -30,13 +37,19 @@ uniform int u_Radius;
 
 void main()
   {
-  vec4 pixel= texture(u_ColorTexture,v_TexCoordinate) * u_Weights[0];
+  vec4 pixel= TEXTURE(u_ColorTexture,v_TexCoordinate) * u_Weights[0];
 
   for (int i=1; i<=u_Radius; i+=1)
     {
-    pixel += ( texture(u_ColorTexture,vec2(v_TexCoordinate.x+u_Offsets[i],v_TexCoordinate.y)) +
-               texture(u_ColorTexture,vec2(v_TexCoordinate.x-u_Offsets[i],v_TexCoordinate.y)) ) * u_Weights[i];
+    pixel += ( TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x+u_Offsets[i],v_TexCoordinate.y)) +
+               TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x-u_Offsets[i],v_TexCoordinate.y)) ) * u_Weights[i];
     }
 
-  fragColor = pixel;
+#if __VERSION__ != 100
+  fragColor    =
+#else
+  gl_FragColor =
+#endif
+
+  pixel;
   }
\ No newline at end of file
diff --git a/src/main/res/raw/blur2_fragment_shader.glsl b/src/main/res/raw/blur2_fragment_shader.glsl
index 309e8b8..35678df 100644
--- a/src/main/res/raw/blur2_fragment_shader.glsl
+++ b/src/main/res/raw/blur2_fragment_shader.glsl
@@ -19,8 +19,15 @@
 
 precision lowp float;
 
+#if __VERSION__ != 100
+#define TEXTURE texture
 in vec2 v_TexCoordinate;
 out vec4 fragColor;
+#else
+#define TEXTURE texture2D
+varying vec2 v_TexCoordinate;
+#endif
+
 uniform sampler2D u_ColorTexture;
 uniform sampler2D u_DepthTexture;
 uniform float u_Offsets[MAX_BLUR];
@@ -31,15 +38,21 @@ uniform int u_Radius;
 
 void main()
   {
-  gl_FragDepth = 0.0;//texture(u_DepthTexture,v_TexCoordinate);
+  gl_FragDepth = TEXTURE(u_DepthTexture,v_TexCoordinate).r;
 
-  vec4 pixel= texture(u_ColorTexture,v_TexCoordinate) * u_Weights[0];
+  vec4 pixel= TEXTURE(u_ColorTexture,v_TexCoordinate) * u_Weights[0];
 
   for (int i=1; i<=u_Radius; i+=1)
     {
-    pixel += ( texture(u_ColorTexture,vec2(v_TexCoordinate.x+u_Offsets[i],v_TexCoordinate.y)) +
-               texture(u_ColorTexture,vec2(v_TexCoordinate.x-u_Offsets[i],v_TexCoordinate.y)) ) * u_Weights[i];
+    pixel += ( TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x+u_Offsets[i],v_TexCoordinate.y)) +
+               TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x-u_Offsets[i],v_TexCoordinate.y)) ) * u_Weights[i];
     }
 
-  fragColor = pixel;
+#if __VERSION__ != 100
+  fragColor    =
+#else
+  gl_FragColor =
+#endif
+
+  pixel;
   }
\ No newline at end of file
diff --git a/src/main/res/raw/blur_vertex_shader.glsl b/src/main/res/raw/blur_vertex_shader.glsl
index c3236dc..59a33c5 100644
--- a/src/main/res/raw/blur_vertex_shader.glsl
+++ b/src/main/res/raw/blur_vertex_shader.glsl
@@ -19,10 +19,17 @@
 
 precision lowp float;
 
-uniform float u_Depth;    // distance from the near plane to render plane, in clip coords
+#if __VERSION__ != 100
 in vec2 a_Position;       // Per-vertex position.
 in vec2 a_TexCoordinate;  // Per-vertex texture coordinate information we will pass in.
 out vec2 v_TexCoordinate; //
+#else
+attribute vec2 a_Position;       // Per-vertex position.
+attribute vec2 a_TexCoordinate;  // Per-vertex texture coordinate information we will pass in.
+varying vec2 v_TexCoordinate;    //
+#endif
+
+uniform float u_Depth;    // distance from the near plane to render plane, in clip coords
 
 //////////////////////////////////////////////////////////////////////////////////////////////
 
diff --git a/src/main/res/raw/main_fragment_shader.glsl b/src/main/res/raw/main_fragment_shader.glsl
index 895fa58..8a69569 100644
--- a/src/main/res/raw/main_fragment_shader.glsl
+++ b/src/main/res/raw/main_fragment_shader.glsl
@@ -18,13 +18,21 @@
 //////////////////////////////////////////////////////////////////////////////////////////////
 
 precision lowp float;
-  
-uniform sampler2D u_Texture;            // The input texture.
-    
+
+#if __VERSION__ != 100
+#define TEXTURE texture
 in vec3 v_Position;                     // Interpolated position for this fragment.
 in vec3 v_Normal;                       // Interpolated normal for this fragment.
 in vec2 v_TexCoordinate;                // Interpolated texture coordinate per fragment.
 out vec4 fragColor;                     // The output color
+#else
+#define TEXTURE texture2D
+varying vec3 v_Position;                // Interpolated position for this fragment.
+varying vec3 v_Normal;                  // Interpolated normal for this fragment.
+varying vec2 v_TexCoordinate;           // Interpolated texture coordinate per fragment.
+#endif
+
+uniform sampler2D u_Texture;            // The input texture.
 
 #if NUM_FRAGMENT>0
 uniform int fNumEffects;                // total number of fragment effects
@@ -93,7 +101,7 @@ void saturation(float degree, int effect, inout vec4 color)
 
 void main()                    		
   {  
-  vec4 pixel = texture(u_Texture,v_TexCoordinate);
+  vec4 pixel = TEXTURE(u_Texture,v_TexCoordinate);
 
 #if NUM_FRAGMENT>0
   vec2 diff;
@@ -141,5 +149,11 @@ void main()
     }
 #endif
 
-  fragColor = vec4(pixel.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, pixel.a);
+#if __VERSION__ != 100
+  fragColor    =
+#else
+  gl_FragColor =
+#endif
+
+  vec4(pixel.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, pixel.a);
   }
\ No newline at end of file
diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 0cb708f..b5bc402 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -19,6 +19,22 @@
 
 precision lowp float;
 
+#if __VERSION__ != 100
+in vec3 a_Position;                  // Per-vertex position.
+in vec3 a_Normal;                    // Per-vertex normal vector.
+in vec2 a_TexCoordinate;             // Per-vertex texture coordinate.
+out vec3 v_Position;                 //
+out vec3 v_Normal;                   //
+out vec2 v_TexCoordinate;            //
+#else
+attribute vec3 a_Position;           // Per-vertex position.
+attribute vec3 a_Normal;             // Per-vertex normal vector.
+attribute vec2 a_TexCoordinate;      // Per-vertex texture coordinate.
+varying vec3 v_Position;             //
+varying vec3 v_Normal;               //
+varying vec2 v_TexCoordinate;        //
+#endif
+
 uniform vec3 u_objD;                 // half of object width x half of object height X half the depth;
                                      // point (0,0,0) is the center of the object
 
@@ -26,17 +42,9 @@ uniform float u_Depth;               // max absolute value of v.z ; beyond that
                                      // I read OpenGL ES has a built-in uniform variable gl_DepthRange.near = n,
                                      // .far = f, .diff = f-n so maybe u_Depth is redundant
                                      // Update: this struct is only available in fragment shaders
-                                
+
 uniform mat4 u_MVPMatrix;            // the combined model/view/projection matrix.
 uniform mat4 u_MVMatrix;             // the combined model/view matrix.
-		 
-in vec3 a_Position;                  // Per-vertex position.
-in vec3 a_Normal;                    // Per-vertex normal vector.
-in vec2 a_TexCoordinate;             // Per-vertex texture coordinate.
-		  
-out vec3 v_Position;                 //
-out vec3 v_Normal;                   //
-out vec2 v_TexCoordinate;            //
 
 #if NUM_VERTEX>0
 uniform int vNumEffects;             // total number of vertex effects
diff --git a/src/main/res/raw/test_fragment_shader.glsl b/src/main/res/raw/test_fragment_shader.glsl
index c920cea..b0b0af1 100644
--- a/src/main/res/raw/test_fragment_shader.glsl
+++ b/src/main/res/raw/test_fragment_shader.glsl
@@ -19,11 +19,19 @@
 
 precision lowp float;
 
+#if __VERSION__ != 100
 out vec4 fragColor;
+#endif
 
 //////////////////////////////////////////////////////////////////////////////////////////////
 
 void main()
   {
-  fragColor = vec4(1.0,0.0,0.0,0.2);
+#if __VERSION__ != 100
+  fragColor    =
+#else
+  gl_FragColor =
+#endif
+
+  vec4(1.0,0.0,0.0,0.2);
   }
\ No newline at end of file
diff --git a/src/main/res/raw/test_vertex_shader.glsl b/src/main/res/raw/test_vertex_shader.glsl
index f55c0ef..bf52c1d 100644
--- a/src/main/res/raw/test_vertex_shader.glsl
+++ b/src/main/res/raw/test_vertex_shader.glsl
@@ -19,9 +19,14 @@
 
 precision lowp float;
 
+#if __VERSION__ != 100
+in vec2 a_Position;        // Per-vertex position information we will pass in.
+#else
+attribute vec2 a_Position; // Per-vertex position information we will pass in.
+#endif
+
 uniform vec2 u_objD;       // object width X object height.
 uniform mat4 u_MVPMatrix;  // the combined model/view/projection matrix.
-in vec2 a_Position;        // Per-vertex position information we will pass in.
 
 //////////////////////////////////////////////////////////////////////////////////////////////
 
