Implement reflecting Java objects into the ACC enviroment.
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index ca35a40..50d39b7 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -16,6 +16,8 @@
 
 package android.renderscript;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Array;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -33,9 +35,12 @@
  *
  **/
 public class Allocation extends BaseObj {
-    Allocation(int id, RenderScript rs) {
+    Type mType;
+
+    Allocation(int id, RenderScript rs, Type t) {
         super(rs);
         mID = id;
+        mType = t;
     }
 
     public void uploadToTexture(int baseMipLevel) {
@@ -82,6 +87,10 @@
         mRS.nAllocationRead(mID, d);
     }
 
+    public void data(Object o) {
+        mRS.nAllocationDataFromObject(mID, mType, o);
+    }
+
 
     public class Adapter1D extends BaseObj {
         Adapter1D(int id, RenderScript rs) {
@@ -179,7 +188,7 @@
             throw new IllegalStateException("Bad Type");
         }
         int id = rs.nAllocationCreateTyped(type.mID);
-        return new Allocation(id, rs);
+        return new Allocation(id, rs, type);
     }
 
     static public Allocation createSized(RenderScript rs, Element e, int count)
@@ -194,7 +203,7 @@
                 throw new IllegalStateException("Bad element.");
             }
         }
-        return new Allocation(id, rs);
+        return new Allocation(id, rs, null);
     }
 
     static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
@@ -204,7 +213,7 @@
         }
 
         int id = rs.nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b);
-        return new Allocation(id, rs);
+        return new Allocation(id, rs, null);
     }
 
     static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
@@ -214,7 +223,7 @@
         }
 
         int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b);
-        return new Allocation(id, rs);
+        return new Allocation(id, rs, null);
     }
 
     static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
@@ -230,8 +239,20 @@
         Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
         return createFromBitmapBoxed(rs, b, dstFmt, genMips);
     }
-
-
+/*
+    public static Allocation createFromObject(RenderScript rs, Object o) {
+        Class c = o.getClass();
+        Type t;
+        if(c.isArray()) {
+            t = Type.createFromClass(rs, c, Array.getLength(o));
+        } else {
+            t = Type.createFromClass(rs, c, 1);
+        }
+        Allocation alloc = createTyped(rs, t);
+        t.destroy();
+        return alloc;
+    } 
+*/
 }
 
 
diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java
index 14d9115..9155da8 100644
--- a/graphics/java/android/renderscript/Element.java
+++ b/graphics/java/android/renderscript/Element.java
@@ -16,6 +16,10 @@
 
 package android.renderscript;
 
+import android.util.Config;
+import android.util.Log;
+
+import java.lang.reflect.Field;
 
 /**
  * @hide
@@ -144,7 +148,26 @@
         mRS.nElementDestroy(mID);
     }
 
+    public static Element createFromClass(RenderScript rs, Class c) {
+        Field[] fields = c.getFields();
+        Builder b = new Builder(rs);
 
+        for(Field f: fields) {
+            Class fc = f.getType();
+            if(fc == int.class) {
+                b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 32, f.getName());
+            } else if(fc == short.class) {
+                b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 16, f.getName());
+            } else if(fc == byte.class) {
+                b.add(Element.DataType.SIGNED, Element.DataKind.USER, false, 8, f.getName());
+            } else if(fc == float.class) {
+                b.add(Element.DataType.FLOAT, Element.DataKind.USER, false, 32, f.getName());
+            } else {
+                throw new IllegalArgumentException("Unkown field type");
+            }
+        }
+        return b.create();
+    }
 
 
     public static class Builder {
@@ -158,6 +181,7 @@
             Element.DataKind mKind;
             boolean mIsNormalized;
             int mBits;
+            String mName;
         }
 
         public Builder(RenderScript rs) {
@@ -188,16 +212,22 @@
             return this;
         }
 
-        public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
+        public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits, String name) {
             Entry en = new Entry();
             en.mType = dt;
             en.mKind = dk;
             en.mIsNormalized = isNormalized;
             en.mBits = bits;
+            en.mName = name;
             addEntry(en);
             return this;
         }
 
+        public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
+            add(dt, dk, isNormalized, bits, null);
+            return this;
+        }
+
         static synchronized Element internalCreate(RenderScript rs, Builder b) {
             rs.nElementBegin();
             for (int ct=0; ct < b.mEntryCount; ct++) {
@@ -209,7 +239,7 @@
                     if (en.mIsNormalized) {
                         norm = 1;
                     }
-                    rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits);
+                    rs.nElementAdd(en.mKind.mID, en.mType.mID, norm, en.mBits, en.mName);
                 }
             }
             int id = rs.nElementCreate();
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index 77486b1..31bfc57 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -18,9 +18,11 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Field;
 
 import android.content.res.Resources;
 import android.graphics.Bitmap;
+import android.renderscript.Type;
 import android.util.Config;
 import android.util.Log;
 import android.view.Surface;
@@ -76,7 +78,7 @@
 
     native void nElementBegin();
     native void nElementAddPredefined(int predef);
-    native void nElementAdd(int kind, int type, int norm, int bits);
+    native void nElementAdd(int kind, int type, int norm, int bits, String s);
     native int  nElementCreate();
     native int  nElementGetPredefined(int predef);
     native void nElementDestroy(int obj);
@@ -85,6 +87,8 @@
     native void nTypeAdd(int dim, int val);
     native int  nTypeCreate();
     native void nTypeDestroy(int id);
+    native void nTypeFinalDestroy(Type t);
+    native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
 
     native int  nAllocationCreateTyped(int type);
     native int  nAllocationCreatePredefSized(int predef, int count);
@@ -102,6 +106,7 @@
     native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d);
     native void nAllocationRead(int id, int[] d);
     native void nAllocationRead(int id, float[] d);
+    native void nAllocationDataFromObject(int id, Type t, Object o);
 
     native void nTriangleMeshDestroy(int id);
     native void nTriangleMeshBegin(int vertex, int index);
diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java
index 1c7b32b..42c58ce 100644
--- a/graphics/java/android/renderscript/Script.java
+++ b/graphics/java/android/renderscript/Script.java
@@ -21,6 +21,7 @@
  **/
 public class Script extends BaseObj {
     boolean mIsRoot;
+    Type[] mTypes;
 
     Script(int id, RenderScript rs) {
         super(rs);
@@ -62,21 +63,40 @@
     public static class Builder {
         RenderScript mRS;
         boolean mIsRoot = false;
+        Type[] mTypes;
+        int mTypeCount;
 
         Builder(RenderScript rs) {
             mRS = rs;
+            mTypes = new Type[4];
+            mTypeCount = 0;
         }
 
         public void addType(Type t) {
-            mRS.nScriptCAddType(t.mID);
+            if(mTypeCount >= mTypes.length) {
+                Type[] nt = new Type[mTypeCount * 2];
+                for(int ct=0; ct < mTypeCount; ct++) {
+                    nt[ct] = mTypes[ct];
+                }
+                mTypes = nt;
+            }
+            mTypes[mTypeCount] = t;
+            mTypeCount++;
         }
 
         void transferCreate() {
             mRS.nScriptCSetRoot(mIsRoot);
+            for(int ct=0; ct < mTypeCount; ct++) {
+                mRS.nScriptCAddType(mTypes[ct].mID);
+            }
         }
 
         void transferObject(Script s) {
             s.mIsRoot = mIsRoot;
+            s.mTypes = new Type[mTypeCount];
+            for(int ct=0; ct < mTypeCount; ct++) {
+                s.mTypes[ct] = mTypes[ct];
+            }
         }
 
         public void setRoot(boolean r) {
diff --git a/graphics/java/android/renderscript/Type.java b/graphics/java/android/renderscript/Type.java
index a5fc603..2ee164f 100644
--- a/graphics/java/android/renderscript/Type.java
+++ b/graphics/java/android/renderscript/Type.java
@@ -16,10 +16,12 @@
 
 package android.renderscript;
 
+import java.lang.reflect.Field;
+
+import android.renderscript.Element;
 import android.util.Config;
 import android.util.Log;
 
-
 /**
  * @hide
  *
@@ -28,11 +30,22 @@
     Dimension[] mDimensions;
     int[] mValues;
     Element mElement;
+    private int mNativeCache;
+    Class mJavaClass;
 
 
     Type(int id, RenderScript rs) {
         super(rs);
         mID = id;
+        mNativeCache = 0;
+    }
+
+    protected void finalize() throws Throwable {
+        if(mNativeCache) {
+            mRS.nTypeFinalDestroy(this);
+            mNativeCache = 0;
+        }
+        super.finalize();
     }
 
     public void destroy() {
@@ -43,6 +56,45 @@
         mRS.nTypeDestroy(mID);
     }
 
+    public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
+        Element e = Element.createFromClass(rs, c);
+        Builder b = new Builder(rs, e);
+        b.add(Dimension.X, size);
+        Type t = b.create();
+        e.destroy();
+
+        // native fields
+        {
+            Field[] fields = c.getFields();
+            int[] arTypes = new int[fields.length];
+            int[] arBits = new int[fields.length];
+
+            for(int ct=0; ct < fields.length; ct++) {
+                Field f = fields[ct];
+                Class fc = f.getType();
+                if(fc == int.class) {
+                    arTypes[ct] = Element.DataType.SIGNED.mID;
+                    arBits[ct] = 32;
+                } else if(fc == short.class) {
+                    arTypes[ct] = Element.DataType.SIGNED.mID;
+                    arBits[ct] = 16;
+                } else if(fc == byte.class) {
+                    arTypes[ct] = Element.DataType.SIGNED.mID;
+                    arBits[ct] = 8;
+                } else if(fc == float.class) {
+                    arTypes[ct] = Element.DataType.FLOAT.mID;
+                    arBits[ct] = 32;
+                } else {
+                    throw new IllegalArgumentException("Unkown field type");
+                }
+            }
+            rs.nTypeSetupFields(t, arTypes, arBits, fields);
+        }
+        t.mJavaClass = c;
+        t.setName(scriptName);
+        return t;
+    }
+
     public static class Builder {
         RenderScript mRS;
         Entry[] mEntries;
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index 24bbea5..ef0cf68 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -50,6 +50,7 @@
 
 static jfieldID gContextId = 0;
 static jfieldID gNativeBitmapID = 0;
+static jfieldID gTypeNativeCache = 0;
 
 static void _nInit(JNIEnv *_env, jclass _this)
 {
@@ -57,6 +58,9 @@
 
     jclass bitmapClass = _env->FindClass("android/graphics/Bitmap");
     gNativeBitmapID = _env->GetFieldID(bitmapClass, "mNativeBitmap", "I");
+
+    jclass typeClass = _env->FindClass("android/renderscript/Type");
+    gTypeNativeCache = _env->GetFieldID(typeClass, "mNativeCache", "I");
 }
 
 
@@ -149,11 +153,18 @@
 }
 
 static void
-nElementAdd(JNIEnv *_env, jobject _this, jint kind, jint type, jint norm, jint bits)
+nElementAdd(JNIEnv *_env, jobject _this, jint kind, jint type, jint norm, jint bits, jstring name)
 {
     RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    const char* n = NULL;
+    if (name) {
+        n = _env->GetStringUTFChars(name, NULL);
+    }
     LOG_API("nElementAdd, con(%p), kind(%i), type(%i), norm(%i), bits(%i)", con, kind, type, norm, bits);
-    rsElementAdd((RsDataKind)kind, (RsDataType)type, norm != 0, (size_t)bits);
+    rsElementAdd((RsDataKind)kind, (RsDataType)type, norm != 0, (size_t)bits, n);
+    if (n) {
+        _env->ReleaseStringUTFChars(name, n);
+    }
 }
 
 static jint
@@ -214,6 +225,91 @@
     rsTypeDestroy((RsType)eID);
 }
 
+static void * SF_LoadInt(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
+{
+    ((int32_t *)buffer)[0] = _env->GetIntField(_obj, _field);
+    return ((uint8_t *)buffer) + 4;
+}
+
+static void * SF_LoadShort(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
+{
+    ((int16_t *)buffer)[0] = _env->GetShortField(_obj, _field);
+    return ((uint8_t *)buffer) + 2;
+}
+
+static void * SF_LoadByte(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
+{
+    ((int8_t *)buffer)[0] = _env->GetByteField(_obj, _field);
+    return ((uint8_t *)buffer) + 1;
+}
+
+static void * SF_LoadFloat(JNIEnv *_env, jobject _obj, jfieldID _field, void *buffer)
+{
+    ((float *)buffer)[0] = _env->GetFloatField(_obj, _field);
+    return ((uint8_t *)buffer) + 4;
+}
+
+struct TypeFieldCache {
+    jfieldID field;
+    int bits;
+    void * (*ptr)(JNIEnv *, jobject, jfieldID, void *buffer);
+};
+
+struct TypeCache {
+    int fieldCount;
+    int size;
+    TypeFieldCache fields[1];
+};
+
+//{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
+static void
+nTypeFinalDestroy(JNIEnv *_env, jobject _this, jobject _type)
+{
+    TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
+    free(tc);
+}
+
+// native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
+static void
+nTypeSetupFields(JNIEnv *_env, jobject _this, jobject _type, jintArray _types, jintArray _bits, jobjectArray _IDs)
+{
+    int fieldCount = _env->GetArrayLength(_types);
+    size_t structSize = sizeof(TypeCache) + (sizeof(TypeFieldCache) * (fieldCount-1));
+    TypeCache *tc = (TypeCache *)malloc(structSize);
+    memset(tc, 0, structSize);
+
+    TypeFieldCache *tfc = &tc->fields[0];
+    tc->fieldCount = fieldCount;
+    _env->SetIntField(_type, gTypeNativeCache, (jint)tc);
+
+    jint *fType = _env->GetIntArrayElements(_types, NULL);
+    jint *fBits = _env->GetIntArrayElements(_bits, NULL);
+    for (int ct=0; ct < fieldCount; ct++) {
+        jobject field = _env->GetObjectArrayElement(_IDs, ct);
+        tfc[ct].field = _env->FromReflectedField(field);
+        tfc[ct].bits = fBits[ct];
+
+        switch(fType[ct]) {
+        case RS_TYPE_FLOAT:
+            tfc[ct].ptr = SF_LoadFloat;
+            break;
+        case RS_TYPE_UNSIGNED:
+        case RS_TYPE_SIGNED:
+            switch(tfc[ct].bits) {
+            case 32:    tfc[ct].ptr = SF_LoadInt;   break;
+            case 16:    tfc[ct].ptr = SF_LoadShort; break;
+            case 8:     tfc[ct].ptr = SF_LoadByte;  break;
+            }
+            break;
+        }
+        tc->size += 4;
+    }
+
+    _env->ReleaseIntArrayElements(_types, fType, JNI_ABORT);
+    _env->ReleaseIntArrayElements(_bits, fBits, JNI_ABORT);
+}
+
+
 // -----------------------------------
 
 static jint
@@ -412,6 +508,26 @@
 }
 
 
+//{"nAllocationDataFromObject",      "(ILandroid/renderscript/Type;Ljava/lang/Object;)V",   (void*)nAllocationDataFromObject },
+static void
+nAllocationDataFromObject(JNIEnv *_env, jobject _this, jint alloc, jobject _type, jobject _o)
+{
+    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
+    LOG_API("nAllocationDataFromObject con(%p), alloc(%p)", con, (RsAllocation)alloc);
+
+    const TypeCache *tc = (TypeCache *)_env->GetIntField(_type, gTypeNativeCache);
+
+    void * bufAlloc = malloc(tc->size);
+    void * buf = bufAlloc;
+    for (int ct=0; ct < tc->fieldCount; ct++) {
+        const TypeFieldCache *tfc = &tc->fields[ct];
+        buf = tfc->ptr(_env, _o, tfc->field, buf);
+    }
+    rsAllocationData((RsAllocation)alloc, bufAlloc);
+    const uint32_t * tmp = (const uint32_t *)bufAlloc;
+    free(bufAlloc);
+}
+
 // -----------------------------------
 
 static void
@@ -1195,7 +1311,7 @@
 
 {"nElementBegin",                  "()V",                                  (void*)nElementBegin },
 {"nElementAddPredefined",          "(I)V",                                 (void*)nElementAddPredefined },
-{"nElementAdd",                    "(IIII)V",                              (void*)nElementAdd },
+{"nElementAdd",                    "(IIIILjava/lang/String;)V",            (void*)nElementAdd },
 {"nElementCreate",                 "()I",                                  (void*)nElementCreate },
 {"nElementGetPredefined",          "(I)I",                                 (void*)nElementGetPredefined },
 {"nElementDestroy",                "(I)V",                                 (void*)nElementDestroy },
@@ -1204,6 +1320,8 @@
 {"nTypeAdd",                       "(II)V",                                (void*)nTypeAdd },
 {"nTypeCreate",                    "()I",                                  (void*)nTypeCreate },
 {"nTypeDestroy",                   "(I)V",                                 (void*)nTypeDestroy },
+{"nTypeFinalDestroy",              "(Landroid/renderscript/Type;)V",       (void*)nTypeFinalDestroy },
+{"nTypeSetupFields",               "(Landroid/renderscript/Type;[I[I[Ljava/lang/reflect/Field;)V", (void*)nTypeSetupFields },
 
 {"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
 {"nAllocationCreatePredefSized",   "(II)I",                                (void*)nAllocationCreatePredefSized },
@@ -1220,6 +1338,7 @@
 {"nAllocationSubData2D",           "(IIIII[F)V",                           (void*)nAllocationSubData2D_f },
 {"nAllocationRead",                "(I[I)V",                               (void*)nAllocationRead_i },
 {"nAllocationRead",                "(I[F)V",                               (void*)nAllocationRead_f },
+{"nAllocationDataFromObject",      "(ILandroid/renderscript/Type;Ljava/lang/Object;)V",   (void*)nAllocationDataFromObject },
 
 {"nTriangleMeshDestroy",           "(I)V",                                 (void*)nTriangleMeshDestroy },
 {"nTriangleMeshBegin",             "(II)V",                                (void*)nTriangleMeshBegin },
diff --git a/libs/rs/java/Fountain/res/raw/fountain.c b/libs/rs/java/Fountain/res/raw/fountain.c
index 5ed9ed51..6e6afcd 100644
--- a/libs/rs/java/Fountain/res/raw/fountain.c
+++ b/libs/rs/java/Fountain/res/raw/fountain.c
@@ -7,38 +7,23 @@
 
 
 int main(int launchID) {
-    int count, touch, x, y, rate;
-    int ct, ct2;
-    int newPart;
-    int drawCount;
-    int idx;
-    float dx, dy;
-    float posx,posy;
-    int c;
-    int srcIdx;
-    int dstIdx;
-
-    count = loadI32(0, 1);
-    touch = loadI32(0, 2);
-    x = loadI32(0, 3);
-    y = loadI32(0, 4);
-
-    rate = 4;
+    int ct;
+    int count = loadI32(0, OFFSETOF_SomeData_count);
+    int touch = loadI32(0, OFFSETOF_SomeData_touch);
+    int rate = 4;
     int maxLife = (count / rate) - 1;
 
     if (touch) {
-        newPart = loadI32(2, 0);
-        for (ct2=0; ct2<rate; ct2++) {
-            dx = randf(1.f) - 0.5f;
-            dy = randf(1.f) - 0.5f;
-
-            idx = newPart * 5 + 1;
-            storeF(2, idx, dx);
-            storeF(2, idx + 1, dy);
+        int x = loadI32(0, OFFSETOF_SomeData_x);
+        int y = loadI32(0, OFFSETOF_SomeData_y);
+        int newPart = loadI32(2, 0);
+        for (ct=0; ct<rate; ct++) {
+            int idx = newPart * 5 + 1;
+            storeF(2, idx, randf(1.f) - 0.5f);
+            storeF(2, idx + 1, randf(1.f) - 0.5f);
             storeI32(2, idx + 2, maxLife);
             storeF(2, idx + 3, x);
             storeF(2, idx + 4, y);
-
             newPart++;
             if (newPart >= count) {
                 newPart = 0;
@@ -47,21 +32,21 @@
         storeI32(2, 0, newPart);
     }
 
-    drawCount = 0;
+    int drawCount = 0;
     float height = getHeight();
     for (ct=0; ct < count; ct++) {
-        srcIdx = ct * 5 + 1;
+        int srcIdx = ct * 5 + 1;
 
-        dx = loadF(2, srcIdx);
-        dy = loadF(2, srcIdx + 1);
         int life = loadI32(2, srcIdx + 2);
-        posx = loadF(2, srcIdx + 3);
-        posy = loadF(2, srcIdx + 4);
 
         if (life) {
+            float posx = loadF(2, srcIdx + 3);
+            float posy = loadF(2, srcIdx + 4);
+            float dx = loadF(2, srcIdx);
+            float dy = loadF(2, srcIdx + 1);
             if (posy < height) {
-                dstIdx = drawCount * 9;
-                c = 0xcfcfcfcf;
+                int dstIdx = drawCount * 9;
+                int c = 0xcfcfcfcf;
 
                 storeI32(1, dstIdx, c);
                 storeF(1, dstIdx + 1, posx);
diff --git a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java
index ad4f949..3e680e35 100644
--- a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java
+++ b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java
@@ -23,24 +23,21 @@
 import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
+import android.renderscript.*;
 import android.util.Log;
 
-import android.renderscript.RenderScript;
-import android.renderscript.ProgramVertex;
-import android.renderscript.Element;
-import android.renderscript.Allocation;
-import android.renderscript.Script;
-import android.renderscript.ScriptC;
-import android.renderscript.ProgramFragment;
-import android.renderscript.ProgramStore;
-import android.renderscript.SimpleMesh;
-import android.renderscript.Type;
-import android.renderscript.Primitive;
-
 
 public class FountainRS {
     public static final int PART_COUNT = 4000;
 
+    static class SomeData {
+        public int x;
+        public int y;
+        public int touch;
+        public int rate;
+        public int count;
+    }
+
     public FountainRS() {
     }
 
@@ -51,10 +48,10 @@
     }
 
     public void newTouchPosition(int x, int y) {
-        mParams[0] = 1;
-        mParams[1] = x;
-        mParams[2] = y;
-        mIntAlloc.subData1D(2, 3, mParams);
+        mSD.touch = 1;
+        mSD.x = x;
+        mSD.y = y;
+        mIntAlloc.data(mSD);
     }
 
 
@@ -73,10 +70,13 @@
 
     private Bitmap mBackground;
 
-    int mParams[] = new int[10];
+    SomeData mSD = new SomeData();
+    private Type mSDType;
 
     private void initRS() {
-        mIntAlloc = Allocation.createSized(mRS, Element.USER_I32, 10);
+        mSD = new SomeData();
+        mSDType = Type.createFromClass(mRS, SomeData.class, 1, "SomeData");
+        mIntAlloc = Allocation.createTyped(mRS, mSDType);
         mVertAlloc = Allocation.createSized(mRS, Element.USER_I32, PART_COUNT * 5 + 1);
 
         ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
@@ -91,14 +91,8 @@
         mPF = bf.create();
         mPF.setName("PgmFragParts");
 
-        mParams[0] = 0;
-        mParams[1] = PART_COUNT;
-        mParams[2] = 0;
-        mParams[3] = 0;
-        mParams[4] = 0;
-        mParams[5] = 0;
-        mParams[6] = 0;
-        mIntAlloc.data(mParams);
+        mSD.count = PART_COUNT;
+        mIntAlloc.data(mSD);
 
         Element.Builder eb = new Element.Builder(mRS);
         eb.add(Element.DataType.UNSIGNED, Element.DataKind.RED, true, 8);
@@ -124,6 +118,7 @@
         ScriptC.Builder sb = new ScriptC.Builder(mRS);
         sb.setScript(mRes, R.raw.fountain);
         sb.setRoot(true);
+        sb.addType(mSDType);
         mScript = sb.create();
         mScript.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 
diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec
index 7dbf412..d4fbf6b 100644
--- a/libs/rs/rs.spec
+++ b/libs/rs/rs.spec
@@ -44,6 +44,7 @@
 	param RsDataType dataType
 	param bool isNormalized
 	param size_t bits
+	param const char * name
 	}
 
 ElementCreate {
diff --git a/libs/rs/rsComponent.cpp b/libs/rs/rsComponent.cpp
index fde62a0..831580b 100644
--- a/libs/rs/rsComponent.cpp
+++ b/libs/rs/rsComponent.cpp
@@ -31,12 +31,15 @@
 
 Component::Component(
     DataKind dk, DataType dt,
-    bool isNormalized, uint32_t bits)
+    bool isNormalized, uint32_t bits, const char * name)
 {
     mType = dt;
     mKind = dk;
     mIsNormalized = isNormalized;
     mBits = bits;
+    if (name) {
+        mName = name;
+    }
 }
 
 Component::~Component()
diff --git a/libs/rs/rsComponent.h b/libs/rs/rsComponent.h
index 9db107f..5ee95c7 100644
--- a/libs/rs/rsComponent.h
+++ b/libs/rs/rsComponent.h
@@ -44,7 +44,7 @@
     };
 
 
-    Component(DataKind dk, DataType dt, bool isNormalized, uint32_t bits);
+    Component(DataKind dk, DataType dt, bool isNorm, uint32_t bits, const char *);
     virtual ~Component();
 
     DataType getType() const {return mType;}
@@ -54,12 +54,15 @@
 
     uint32_t getGLType() const;
 
+    const char * getComponentName() const {return mName.string();}
+
 protected:
 
     DataType mType;
     bool mIsNormalized;
     DataKind mKind;
     uint32_t mBits;
+    String8 mName;
 
 private:
     Component();
diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp
index 9de23b3..2860452 100644
--- a/libs/rs/rsContext.cpp
+++ b/libs/rs/rsContext.cpp
@@ -17,7 +17,6 @@
 #include "rsDevice.h"
 #include "rsContext.h"
 #include "rsThreadIO.h"
-#include "utils/String8.h"
 #include <ui/FramebufferNativeWindow.h>
 
 #include <GLES/gl.h>
diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h
index 54c555f..60a526b 100644
--- a/libs/rs/rsContext.h
+++ b/libs/rs/rsContext.h
@@ -19,9 +19,6 @@
 
 #include "rsUtils.h"
 
-#include <utils/KeyedVector.h>
-#include <utils/String8.h>
-#include <utils/Vector.h>
 #include <ui/Surface.h>
 
 #include "rsType.h"
diff --git a/libs/rs/rsElement.cpp b/libs/rs/rsElement.cpp
index 0c5cb18..97b18c0 100644
--- a/libs/rs/rsElement.cpp
+++ b/libs/rs/rsElement.cpp
@@ -23,45 +23,45 @@
 
 void ElementState::initPredefined()
 {
-    Component * u_8  = new Component(Component::USER,   Component::UNSIGNED,  true,  8);
-    Component * i_8  = new Component(Component::USER,   Component::SIGNED,    true,  8);
-    Component * u_16 = new Component(Component::USER,   Component::UNSIGNED,  true,  16);
-    Component * i_16 = new Component(Component::USER,   Component::SIGNED,    true,  16);
-    Component * u_32 = new Component(Component::USER,   Component::UNSIGNED,  true,  32);
-    Component * i_32 = new Component(Component::USER,   Component::SIGNED,    true,  32);
-    Component * f_32 = new Component(Component::USER,   Component::FLOAT,     true,  32);
+    Component * u_8  = new Component(Component::USER,   Component::UNSIGNED,  true,  8, 0);
+    Component * i_8  = new Component(Component::USER,   Component::SIGNED,    true,  8, 0);
+    Component * u_16 = new Component(Component::USER,   Component::UNSIGNED,  true,  16, 0);
+    Component * i_16 = new Component(Component::USER,   Component::SIGNED,    true,  16, 0);
+    Component * u_32 = new Component(Component::USER,   Component::UNSIGNED,  true,  32, 0);
+    Component * i_32 = new Component(Component::USER,   Component::SIGNED,    true,  32, 0);
+    Component * f_32 = new Component(Component::USER,   Component::FLOAT,     true,  32, 0);
 
 
-    Component * r_4  = new Component(Component::RED,    Component::UNSIGNED,  true,  4);
-    Component * r_5  = new Component(Component::RED,    Component::UNSIGNED,  true,  5);
-    Component * r_8  = new Component(Component::RED,    Component::UNSIGNED,  true,  8);
+    Component * r_4  = new Component(Component::RED,    Component::UNSIGNED,  true,  4, 0);
+    Component * r_5  = new Component(Component::RED,    Component::UNSIGNED,  true,  5, 0);
+    Component * r_8  = new Component(Component::RED,    Component::UNSIGNED,  true,  8, 0);
 
-    Component * g_4  = new Component(Component::GREEN,  Component::UNSIGNED,  true,  4);
-    Component * g_5  = new Component(Component::GREEN,  Component::UNSIGNED,  true,  5);
-    Component * g_6  = new Component(Component::GREEN,  Component::UNSIGNED,  true,  6);
-    Component * g_8  = new Component(Component::GREEN,  Component::UNSIGNED,  true,  8);
+    Component * g_4  = new Component(Component::GREEN,  Component::UNSIGNED,  true,  4, 0);
+    Component * g_5  = new Component(Component::GREEN,  Component::UNSIGNED,  true,  5, 0);
+    Component * g_6  = new Component(Component::GREEN,  Component::UNSIGNED,  true,  6, 0);
+    Component * g_8  = new Component(Component::GREEN,  Component::UNSIGNED,  true,  8, 0);
 
-    Component * b_4  = new Component(Component::BLUE,   Component::UNSIGNED,  true,  4);
-    Component * b_5  = new Component(Component::BLUE,   Component::UNSIGNED,  true,  5);
-    Component * b_8  = new Component(Component::BLUE,   Component::UNSIGNED,  true,  8);
+    Component * b_4  = new Component(Component::BLUE,   Component::UNSIGNED,  true,  4, 0);
+    Component * b_5  = new Component(Component::BLUE,   Component::UNSIGNED,  true,  5, 0);
+    Component * b_8  = new Component(Component::BLUE,   Component::UNSIGNED,  true,  8, 0);
 
-    Component * a_1  = new Component(Component::ALPHA,  Component::UNSIGNED,  true,  1);
-    Component * a_4  = new Component(Component::ALPHA,  Component::UNSIGNED,  true,  4);
-    Component * a_8  = new Component(Component::ALPHA,  Component::UNSIGNED,  true,  8);
+    Component * a_1  = new Component(Component::ALPHA,  Component::UNSIGNED,  true,  1, 0);
+    Component * a_4  = new Component(Component::ALPHA,  Component::UNSIGNED,  true,  4, 0);
+    Component * a_8  = new Component(Component::ALPHA,  Component::UNSIGNED,  true,  8, 0);
 
-    Component * idx_16 = new Component(Component::INDEX,  Component::UNSIGNED,  false, 16);
-    Component * idx_32 = new Component(Component::INDEX,  Component::UNSIGNED,  false, 32);
+    Component * idx_16 = new Component(Component::INDEX,  Component::UNSIGNED,  false, 16, 0);
+    Component * idx_32 = new Component(Component::INDEX,  Component::UNSIGNED,  false, 32, 0);
 
-    Component * x    = new Component(Component::X,      Component::FLOAT,     false, 32);
-    Component * y    = new Component(Component::Y,      Component::FLOAT,     false, 32);
-    Component * z    = new Component(Component::Z,      Component::FLOAT,     false, 32);
+    Component * x    = new Component(Component::X,      Component::FLOAT,     false, 32, 0);
+    Component * y    = new Component(Component::Y,      Component::FLOAT,     false, 32, 0);
+    Component * z    = new Component(Component::Z,      Component::FLOAT,     false, 32, 0);
 
-    Component * nx   = new Component(Component::NX,     Component::FLOAT,     false, 32);
-    Component * ny   = new Component(Component::NY,     Component::FLOAT,     false, 32);
-    Component * nz   = new Component(Component::NZ,     Component::FLOAT,     false, 32);
+    Component * nx   = new Component(Component::NX,     Component::FLOAT,     false, 32, 0);
+    Component * ny   = new Component(Component::NY,     Component::FLOAT,     false, 32, 0);
+    Component * nz   = new Component(Component::NZ,     Component::FLOAT,     false, 32, 0);
 
-    Component * s    = new Component(Component::S,      Component::FLOAT,     false, 32);
-    Component * t    = new Component(Component::T,      Component::FLOAT,     false, 32);
+    Component * s    = new Component(Component::S,      Component::FLOAT,     false, 32, 0);
+    Component * t    = new Component(Component::T,      Component::FLOAT,     false, 32, 0);
 
     Element * e;
 
@@ -391,14 +391,14 @@
     return e;
 }
 
-void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits)
+void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits, const char *name)
 {
     ElementState * sec = &rsc->mStateElement;
-
     Component *c = new Component(static_cast<Component::DataKind>(dk),
                                  static_cast<Component::DataType>(dt),
                                  isNormalized,
-                                 bits);
+                                 bits,
+                                 name);
     sec->mComponentBuildList.add(c);
 }
 
diff --git a/libs/rs/rsFileA3D.cpp b/libs/rs/rsFileA3D.cpp
index 86d294b..347ef23 100644
--- a/libs/rs/rsFileA3D.cpp
+++ b/libs/rs/rsFileA3D.cpp
@@ -334,7 +334,7 @@
         uint32_t bits = io->loadU8();
         bool isNorm = io->loadU8() != 0;
         LOGE("  %i %i %i %i", dk, dt, bits, isNorm);
-        rsi_ElementAdd(rsc, dk, dt, isNorm, bits);
+        rsi_ElementAdd(rsc, dk, dt, isNorm, bits, 0);
     }
     LOGE("processChunk_Element create");
     ie->mRsObj = rsi_ElementCreate(rsc);
diff --git a/libs/rs/rsScript.h b/libs/rs/rsScript.h
index a8e04a6..6983ef4 100644
--- a/libs/rs/rsScript.h
+++ b/libs/rs/rsScript.h
@@ -29,6 +29,8 @@
 class ProgramRaster;
 class ProgramFragmentStore;
 
+#define MAX_SCRIPT_BANKS 16
+
 class Script : public ObjectBase
 {
 public:
@@ -57,7 +59,8 @@
     const Type * mConstantBufferTypes;
     uint32_t mCounstantBufferCount;
 
-    ObjectBaseRef<Allocation> mSlots[16];
+    ObjectBaseRef<Allocation> mSlots[MAX_SCRIPT_BANKS];
+    ObjectBaseRef<Type> mTypes[MAX_SCRIPT_BANKS];
 
     virtual bool run(Context *, uint32_t launchID) = 0;
 };
diff --git a/libs/rs/rsScriptC.cpp b/libs/rs/rsScriptC.cpp
index 3343db5..652283d 100644
--- a/libs/rs/rsScriptC.cpp
+++ b/libs/rs/rsScriptC.cpp
@@ -19,7 +19,6 @@
 #include "rsMatrix.h"
 
 #include "acc/acc.h"
-#include "utils/String8.h"
 #include "utils/Timers.h"
 
 #include <GLES/gl.h>
@@ -91,7 +90,10 @@
 {
     memset(&mProgram, 0, sizeof(mProgram));
 
-    mConstantBufferTypes.clear();
+    mConstantTypeCount = 0;
+    for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
+        mConstantBufferTypes[ct].clear();
+    }
 
     memset(&mEnviroment, 0, sizeof(mEnviroment));
     mEnviroment.mClearColor[0] = 0;
@@ -127,6 +129,7 @@
     appendDecls(&tmp);
     rsc->appendVarDefines(&tmp);
     appendVarDefines(&tmp);
+    appendTypes(&tmp);
     tmp.append("#line 1\n");
 
     const char* scriptSource[] = {tmp.string(), mProgram.mScriptText};
@@ -242,6 +245,31 @@
     }
 }
 
+void ScriptCState::appendTypes(String8 *str)
+{
+    char buf[256];
+
+    for (size_t ct=0; ct < mConstantTypeCount; ct++) {
+        const Type *t = mConstantBufferTypes[ct].get();
+        const Element *e = t->getElement();
+
+        if (!t->getName()) {
+            continue;
+        }
+        for (size_t ct2=0; ct2 < e->getComponentCount(); ct2++) {
+            const Component *c = e->getComponent(ct2);
+            str->append("#define OFFSETOF_");
+            str->append(t->getName());
+            str->append("_");
+            str->append(c->getComponentName());
+            sprintf(buf, " %i\n", ct2);
+            str->append(buf);
+        }
+
+    }
+
+}
+
 
 namespace android {
 namespace renderscript {
@@ -255,7 +283,10 @@
 void rsi_ScriptCAddType(Context * rsc, RsType vt)
 {
     ScriptCState *ss = &rsc->mScriptC;
-    ss->mConstantBufferTypes.add(static_cast<const Type *>(vt));
+    const Type *t = static_cast<const Type *>(vt);
+
+    ss->mConstantBufferTypes[ss->mConstantTypeCount].set(t);
+    ss->mConstantTypeCount ++;
 }
 
 void rsi_ScriptCSetScript(Context * rsc, void *vp)
diff --git a/libs/rs/rsScriptC.h b/libs/rs/rsScriptC.h
index ad0e3ee..60a6fba 100644
--- a/libs/rs/rsScriptC.h
+++ b/libs/rs/rsScriptC.h
@@ -68,11 +68,13 @@
     ScriptC::Program_t mProgram;
     Script::Enviroment_t mEnviroment;
 
-    Vector<const Type *> mConstantBufferTypes;
+    ObjectBaseRef<const Type> mConstantBufferTypes[MAX_SCRIPT_BANKS];
+    uint32_t mConstantTypeCount;
 
     void clear();
     void runCompiler(Context *rsc);
     void appendVarDefines(String8 *str);
+    void appendTypes(String8 *str);
 
     struct SymbolTable_t {
         const char * mName;
diff --git a/libs/rs/rsScriptC_Lib.cpp b/libs/rs/rsScriptC_Lib.cpp
index 4ee112d..c13894b 100644
--- a/libs/rs/rsScriptC_Lib.cpp
+++ b/libs/rs/rsScriptC_Lib.cpp
@@ -20,7 +20,6 @@
 #include "rsNoise.h"
 
 #include "acc/acc.h"
-#include "utils/String8.h"
 #include "utils/Timers.h"
 
 #include <GLES/gl.h>
diff --git a/libs/rs/rsUtils.h b/libs/rs/rsUtils.h
index f0e4750..ec928db 100644
--- a/libs/rs/rsUtils.h
+++ b/libs/rs/rsUtils.h
@@ -21,6 +21,8 @@
 #define LOG_TAG "rs"
 #include <utils/Log.h>
 #include <utils/Vector.h>
+#include <utils/KeyedVector.h>
+#include <utils/String8.h>
 #include <stdlib.h>
 #include <pthread.h>
 #include <time.h>