Add sampler support
diff --git a/java/Fountain/src/com/android/fountain/FountainView.java b/java/Fountain/src/com/android/fountain/FountainView.java
index bc34080..3381525 100644
--- a/java/Fountain/src/com/android/fountain/FountainView.java
+++ b/java/Fountain/src/com/android/fountain/FountainView.java
@@ -52,6 +52,7 @@
     private RenderScript.ProgramFragment mPF;
     private RenderScript.ProgramFragment mPF2;
     private RenderScript.Allocation mTexture;
+    private RenderScript.Sampler mSampler;
 
     private Bitmap mBackground;
 
@@ -83,6 +84,12 @@
         mPFS = mRS.programFragmentStoreCreate();
         mRS.contextBindProgramFragmentStore(mPFS);
 
+        mRS.samplerBegin();
+        mRS.samplerSet(RenderScript.SamplerParam.FILTER_MAG, RenderScript.SamplerValue.LINEAR);
+        mRS.samplerSet(RenderScript.SamplerParam.FILTER_MIN, RenderScript.SamplerValue.LINEAR);
+        mSampler = mRS.samplerCreate();
+
+
         mRS.programFragmentBegin(null, null);
         mPF = mRS.programFragmentCreate();
         //mRS.contextBindProgramFragment(mPF);
@@ -92,6 +99,7 @@
         mPF2 = mRS.programFragmentCreate();
         mRS.contextBindProgramFragment(mPF2);
         mPF2.bindTexture(mTexture, 0);
+        mPF2.bindSampler(mSampler, 0);
 
         mParams[0] = 0;
         mParams[1] = partCount;
diff --git a/java/Fountain/src/com/android/fountain/RenderScript.java b/java/Fountain/src/com/android/fountain/RenderScript.java
index 739f9ae..cf16cec 100644
--- a/java/Fountain/src/com/android/fountain/RenderScript.java
+++ b/java/Fountain/src/com/android/fountain/RenderScript.java
@@ -38,7 +38,7 @@
 
 
 
-       /*
+     /*
      * We use a class initializer to allow the native code to cache some
      * field offsets.
      */
@@ -49,9 +49,7 @@
         sInitialized = false;
         try {
             System.loadLibrary("RS_jni");
-            Log.e(LOG_TAG, "*** Renderscript INIT");
             _nInit();
-            Log.e(LOG_TAG, "*** Renderscript INIT 3");
             sInitialized = true;
         } catch (UnsatisfiedLinkError e) {
             Log.d(LOG_TAG, "RenderScript JNI library not found!");
@@ -126,6 +124,10 @@
     native private void nScriptCSetScript(byte[] script, int offset, int length);
     native private int  nScriptCCreate();
 
+    native private void nSamplerDestroy(int sampler);
+    native private void nSamplerBegin();
+    native private void nSamplerSet(int param, int value);
+    native private int  nSamplerCreate();
 
     native private void nProgramFragmentStoreBegin(int in, int out);
     native private void nProgramFragmentStoreDepthFunc(int func);
@@ -307,6 +309,34 @@
         }
     }
 
+    public enum SamplerParam {
+        FILTER_MIN (0),
+        FILTER_MAG (1),
+        WRAP_MODE_S (2),
+        WRAP_MODE_T (3),
+        WRAP_MODE_R (4);
+
+        int mID;
+        SamplerParam(int id) {
+            mID = id;
+        }
+    }
+
+    public enum SamplerValue {
+        NEAREST (0),
+        LINEAR (1),
+        LINEAR_MIP_LINEAR (2),
+        WRAP (3),
+        CLAMP (4);
+
+        int mID;
+        SamplerValue(int id) {
+            mID = id;
+        }
+    }
+
+
+
     public class Element extends BaseObj {
         Element(int id) {
             mID = id;
@@ -727,9 +757,9 @@
             nProgramFragmentBindTexture(mID, slot, va.mID);
         }
 
-        //public void bindSampler(Sampler vs, int slot) {
-            //nProgramFragmentBindSampler(mID, slot, vs.mID);
-        //}
+        public void bindSampler(Sampler vs, int slot) {
+            nProgramFragmentBindSampler(mID, slot, vs.mID);
+        }
     }
 
     public void programFragmentBegin(Element in, Element out) {
@@ -761,6 +791,33 @@
         return new ProgramFragment(id);
     }
 
+    //////////////////////////////////////////////////////////////////////////////////
+    // Sampler
+
+    public class Sampler extends BaseObj {
+        Sampler(int id) {
+            mID = id;
+        }
+
+        public void destroy() {
+            nSamplerDestroy(mID);
+            mID = 0;
+        }
+    }
+
+    public void samplerBegin() {
+        nSamplerBegin();
+    }
+
+    public void samplerSet(SamplerParam p, SamplerValue v) {
+        nSamplerSet(p.mID, v.mID);
+    }
+
+    public Sampler samplerCreate() {
+        int id = nSamplerCreate();
+        return new Sampler(id);
+    }
+
 
     ///////////////////////////////////////////////////////////////////////////////////
     // Root state
diff --git a/jni/RenderScript_jni.cpp b/jni/RenderScript_jni.cpp
index 50849af..4af58a6 100644
--- a/jni/RenderScript_jni.cpp
+++ b/jni/RenderScript_jni.cpp
@@ -718,14 +718,6 @@
 }
 
 static void
-nContextBindSampler(JNIEnv *_env, jobject _this, jint sampler, jint slot)
-{
-    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
-    LOG_API("nContextBindSampler, con(%p), sampler(%p), slot(%i)", con, (RsSampler)sampler, slot);
-    rsContextBindSampler(slot, (RsSampler)sampler);
-}
-
-static void
 nContextBindProgramFragmentStore(JNIEnv *_env, jobject _this, jint pfs)
 {
     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
@@ -741,6 +733,40 @@
     rsContextBindProgramFragment((RsProgramFragment)pf);
 }
 
+// ---------------------------------------------------------------------------
+
+static void
+nSamplerDestroy(JNIEnv *_env, jobject _this, jint s)
+{
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    LOG_API("nSamplerDestroy, con(%p), sampler(%p)", con, (RsSampler)s);
+    rsSamplerDestroy((RsSampler)s);
+}
+
+static void
+nSamplerBegin(JNIEnv *_env, jobject _this)
+{
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    LOG_API("nSamplerBegin, con(%p)", con);
+    rsSamplerBegin();
+}
+
+static void
+nSamplerSet(JNIEnv *_env, jobject _this, jint p, jint v)
+{
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    LOG_API("nSamplerSet, con(%p), param(%i), value(%i)", con, p, v);
+    rsSamplerSet((RsSamplerParam)p, (RsSamplerValue)v);
+}
+
+static jint
+nSamplerCreate(JNIEnv *_env, jobject _this)
+{
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    LOG_API("nSamplerCreate, con(%p), script(%p)", con, (RsScript)script);
+    return (jint)rsSamplerCreate();
+}
+
 
 // ---------------------------------------------------------------------------
 
@@ -825,10 +851,14 @@
 {"nProgramFragmentCreate",         "()I",                                  (void*)nProgramFragmentCreate },
 
 {"nContextBindRootScript",         "(I)V",                                 (void*)nContextBindRootScript },
-//{"nContextBindSampler",          "(II)V",                                (void*)nContextBindSampler },
 {"nContextBindProgramFragmentStore","(I)V",                                (void*)nContextBindProgramFragmentStore },
 {"nContextBindProgramFragment",    "(I)V",                                 (void*)nContextBindProgramFragment },
 
+{"nSamplerDestroy",                "(I)V",                                 (void*)nSamplerDestroy },
+{"nSamplerBegin",                  "()V",                                  (void*)nSamplerBegin },
+{"nSamplerSet",                    "(II)V",                                (void*)nSamplerSet },
+{"nSamplerCreate",                 "()I",                                  (void*)nSamplerCreate },
+
 };
 
 static int registerFuncs(JNIEnv *_env)
diff --git a/rs.spec b/rs.spec
index 98c7008..090be32 100644
--- a/rs.spec
+++ b/rs.spec
@@ -1,10 +1,5 @@
 
 
-ContextBindSampler {
-	param uint32_t slot
-	param RsSampler sampler
-	}
-
 ContextBindRootScript {
 	param RsScript sampler
 	}
@@ -212,6 +207,9 @@
 	ret RsSampler
 	}
 
+SamplerDestroy {
+	param RsSampler s
+	}
 
 TriangleMeshBegin {
 	param RsElement vertex
diff --git a/rsProgramFragment.cpp b/rsProgramFragment.cpp
index 1a6e2c7..3d316ea 100644
--- a/rsProgramFragment.cpp
+++ b/rsProgramFragment.cpp
@@ -62,14 +62,14 @@
             break;
         }
 
-//        if (mSamplers[ct].get()) {
-            //mSamplers[ct]->setupGL();
-//        } else {
+        if (mSamplers[ct].get()) {
+            mSamplers[ct]->setupGL();
+        } else {
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-        //}
+        }
     }
     glActiveTexture(GL_TEXTURE0);
 }
diff --git a/rsSampler.cpp b/rsSampler.cpp
index 3c008c9..ca407db 100644
--- a/rsSampler.cpp
+++ b/rsSampler.cpp
@@ -53,7 +53,21 @@
 
 void Sampler::setupGL()
 {
+    GLenum translate[] = {
+        GL_NEAREST, //RS_SAMPLER_NEAREST,
+        GL_LINEAR, //RS_SAMPLER_LINEAR,
+        GL_LINEAR_MIP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
+        GL_WRAP, //RS_SAMPLER_WRAP,
+        GL_CLAMP_TO_EDGS, //RS_SAMPLER_CLAMP
+
+    }
+
+
     //LOGE("setup gl");
+    switch(mMagFilter) {
+    case RS_SAMPLER_
+    }
+
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
@@ -76,7 +90,7 @@
 
 void SamplerState::setupGL()
 {
-    for (uint32_t ct=0; ct < 1/*RS_MAX_SAMPLER_SLOT*/; ct++) {
+    for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
         Sampler *s = mSamplers[ct].get();
         if (s) {
             s->setupGL();
@@ -140,4 +154,12 @@
     return s;
 }
 
+void rsi_SamplerDestroy(Context *rsc, RsSampler vs)
+{
+    Sampler * s = static_cast<Sampler *>(vs);
+    s->decRef();
+
+}
+
+
 }}