diff options
Diffstat (limited to 'graphics/java/android/renderscript')
-rw-r--r-- | graphics/java/android/renderscript/Allocation.java | 60 | ||||
-rw-r--r-- | graphics/java/android/renderscript/Element.java | 122 | ||||
-rw-r--r-- | graphics/java/android/renderscript/RenderScript.java | 13 | ||||
-rw-r--r-- | graphics/java/android/renderscript/SimpleMesh.java | 145 |
4 files changed, 287 insertions, 53 deletions
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java index 81848b9..3001c743 100644 --- a/graphics/java/android/renderscript/Allocation.java +++ b/graphics/java/android/renderscript/Allocation.java @@ -47,28 +47,62 @@ public class Allocation extends BaseObj { mRS.nAllocationUploadToTexture(mID, baseMipLevel); } + public void uploadToBufferObject() { + mRS.nAllocationUploadToBufferObject(mID); + } + public void data(int[] d) { - mRS.nAllocationData(mID, d); + int size = 0; + if(mType != null && mType.mElement != null) { + size = mType.mElement.mSize; + for(int ct=0; ct < mType.mValues.length; ct++) { + if(mType.mValues[ct] != 0) { + size *= mType.mValues[ct]; + } + } + if((d.length * 4) < size) { + throw new IllegalArgumentException("Array too small for allocation type."); + } + Log.e("rs", "Alloc data size=" + size); + mRS.nAllocationData(mID, d, size); + return; + } + mRS.nAllocationData(mID, d, d.length * 4); } public void data(float[] d) { - mRS.nAllocationData(mID, d); + int size = 0; + if(mType != null && mType.mElement != null) { + size = mType.mElement.mSize; + for(int ct=0; ct < mType.mValues.length; ct++) { + if(mType.mValues[ct] != 0) { + size *= mType.mValues[ct]; + } + } + if((d.length * 4) < size) { + throw new IllegalArgumentException("Array too small for allocation type."); + } + Log.e("rs", "Alloc data size=" + size); + mRS.nAllocationData(mID, d, size); + return; + } + mRS.nAllocationData(mID, d, d.length * 4); } public void subData1D(int off, int count, int[] d) { - mRS.nAllocationSubData1D(mID, off, count, d); + mRS.nAllocationSubData1D(mID, off, count, d, count * 4); } public void subData1D(int off, int count, float[] d) { - mRS.nAllocationSubData1D(mID, off, count, d); + mRS.nAllocationSubData1D(mID, off, count, d, d.length * 4); } public void subData2D(int xoff, int yoff, int w, int h, int[] d) { - mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d); + mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4); } public void subData2D(int xoff, int yoff, int w, int h, float[] d) { - mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d); + mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4); } public void readData(int[] d) { @@ -221,20 +255,6 @@ public class Allocation extends BaseObj { 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 aeec739..0ca112c 100644 --- a/graphics/java/android/renderscript/Element.java +++ b/graphics/java/android/renderscript/Element.java @@ -25,30 +25,31 @@ import java.lang.reflect.Field; public class Element extends BaseObj { final int mPredefinedID; final boolean mIsPredefined; - - public static final Element USER_U8 = new Element(0); - public static final Element USER_I8 = new Element(1); - public static final Element USER_U16 = new Element(2); - public static final Element USER_I16 = new Element(3); - public static final Element USER_U32 = new Element(4); - public static final Element USER_I32 = new Element(5); - public static final Element USER_FLOAT = new Element(6); - - public static final Element A_8 = new Element(7); - public static final Element RGB_565 = new Element(8); - public static final Element RGB_888 = new Element(11); - public static final Element RGBA_5551 = new Element(9); - public static final Element RGBA_4444 = new Element(10); - public static final Element RGBA_8888 = new Element(12); - - public static final Element INDEX_16 = new Element(13); - public static final Element INDEX_32 = new Element(14); - public static final Element XY_F32 = new Element(15); - public static final Element XYZ_F32 = new Element(16); - public static final Element ST_XY_F32 = new Element(17); - public static final Element ST_XYZ_F32 = new Element(18); - public static final Element NORM_XYZ_F32 = new Element(19); - public static final Element NORM_ST_XYZ_F32 = new Element(20); + final int mSize; + + public static final Element USER_U8 = new Element(0, 1); + public static final Element USER_I8 = new Element(1, 1); + public static final Element USER_U16 = new Element(2, 2); + public static final Element USER_I16 = new Element(3, 2); + public static final Element USER_U32 = new Element(4, 4); + public static final Element USER_I32 = new Element(5, 4); + public static final Element USER_FLOAT = new Element(6, 4); + + public static final Element A_8 = new Element(7, 1); + public static final Element RGB_565 = new Element(8, 2); + public static final Element RGB_888 = new Element(11, 2); + public static final Element RGBA_5551 = new Element(9, 2); + public static final Element RGBA_4444 = new Element(10, 2); + public static final Element RGBA_8888 = new Element(12, 4); + + public static final Element INDEX_16 = new Element(13, 2); + public static final Element INDEX_32 = new Element(14, 2); + public static final Element XY_F32 = new Element(15, 8); + public static final Element XYZ_F32 = new Element(16, 12); + public static final Element ST_XY_F32 = new Element(17, 16); + public static final Element ST_XYZ_F32 = new Element(18, 20); + public static final Element NORM_XYZ_F32 = new Element(19, 24); + public static final Element NORM_ST_XYZ_F32 = new Element(20, 32); void initPredef(RenderScript rs) { mID = rs.nElementGetPredefined(mPredefinedID); @@ -121,18 +122,20 @@ public class Element extends BaseObj { } - Element(int predef) { + Element(int predef, int size) { super(null); mID = 0; mPredefinedID = predef; mIsPredefined = true; + mSize = size; } - Element(int id, RenderScript rs) { + Element(int id, RenderScript rs, int size) { super(rs); mID = id; mPredefinedID = 0; mIsPredefined = false; + mSize = size; } public void destroy() throws IllegalStateException { @@ -168,6 +171,7 @@ public class Element extends BaseObj { RenderScript mRS; Entry[] mEntries; int mEntryCount; + int mSizeBits; private class Entry { Element mElement; @@ -182,6 +186,7 @@ public class Element extends BaseObj { mRS = rs; mEntryCount = 0; mEntries = new Entry[8]; + mSizeBits = 0; } void addEntry(Entry e) { @@ -201,6 +206,7 @@ public class Element extends BaseObj { Entry en = new Entry(); en.mElement = e; addEntry(en); + mSizeBits += e.mSize * 8; return this; } @@ -211,6 +217,7 @@ public class Element extends BaseObj { en.mIsNormalized = isNormalized; en.mBits = bits; en.mName = name; + mSizeBits += bits; addEntry(en); return this; } @@ -236,6 +243,12 @@ public class Element extends BaseObj { return this; } + public Builder addFloatXY(String prefix) { + add(DataType.FLOAT, DataKind.X, false, 32, prefix + "X"); + add(DataType.FLOAT, DataKind.Y, false, 32, prefix + "Y"); + return this; + } + public Builder addFloatXYZ() { add(DataType.FLOAT, DataKind.X, false, 32, null); add(DataType.FLOAT, DataKind.Y, false, 32, null); @@ -243,17 +256,49 @@ public class Element extends BaseObj { return this; } + public Builder addFloatXYZ(String prefix) { + add(DataType.FLOAT, DataKind.X, false, 32, prefix + "X"); + add(DataType.FLOAT, DataKind.Y, false, 32, prefix + "Y"); + add(DataType.FLOAT, DataKind.Z, false, 32, prefix + "Z"); + return this; + } + public Builder addFloatST() { add(DataType.FLOAT, DataKind.S, false, 32, null); add(DataType.FLOAT, DataKind.T, false, 32, null); return this; } + public Builder addFloatST(String prefix) { + add(DataType.FLOAT, DataKind.S, false, 32, prefix + "S"); + add(DataType.FLOAT, DataKind.T, false, 32, prefix + "T"); + return this; + } + + public Builder addFloatNorm() { + add(DataType.FLOAT, DataKind.NX, false, 32, null); + add(DataType.FLOAT, DataKind.NY, false, 32, null); + add(DataType.FLOAT, DataKind.NZ, false, 32, null); + return this; + } + + public Builder addFloatNorm(String prefix) { + add(DataType.FLOAT, DataKind.NX, false, 32, prefix + "NX"); + add(DataType.FLOAT, DataKind.NY, false, 32, prefix + "NY"); + add(DataType.FLOAT, DataKind.NZ, false, 32, prefix + "NZ"); + return this; + } + public Builder addFloatPointSize() { add(DataType.FLOAT, DataKind.POINT_SIZE, false, 32, null); return this; } + public Builder addFloatPointSize(String name) { + add(DataType.FLOAT, DataKind.POINT_SIZE, false, 32, name); + return this; + } + public Builder addFloatRGB() { add(DataType.FLOAT, DataKind.RED, false, 32, null); add(DataType.FLOAT, DataKind.GREEN, false, 32, null); @@ -261,6 +306,13 @@ public class Element extends BaseObj { return this; } + public Builder addFloatRGB(String prefix) { + add(DataType.FLOAT, DataKind.RED, false, 32, prefix + "R"); + add(DataType.FLOAT, DataKind.GREEN, false, 32, prefix + "G"); + add(DataType.FLOAT, DataKind.BLUE, false, 32, prefix + "B"); + return this; + } + public Builder addFloatRGBA() { add(DataType.FLOAT, DataKind.RED, false, 32, null); add(DataType.FLOAT, DataKind.GREEN, false, 32, null); @@ -269,6 +321,14 @@ public class Element extends BaseObj { return this; } + public Builder addFloatRGBA(String prefix) { + add(DataType.FLOAT, DataKind.RED, false, 32, prefix + "R"); + add(DataType.FLOAT, DataKind.GREEN, false, 32, prefix + "G"); + add(DataType.FLOAT, DataKind.BLUE, false, 32, prefix + "B"); + add(DataType.FLOAT, DataKind.ALPHA, false, 32, prefix + "A"); + return this; + } + public Builder addUNorm8RGBA() { add(DataType.UNSIGNED, DataKind.RED, true, 8, null); add(DataType.UNSIGNED, DataKind.GREEN, true, 8, null); @@ -277,6 +337,14 @@ public class Element extends BaseObj { return this; } + public Builder addUNorm8RGBA(String prefix) { + add(DataType.UNSIGNED, DataKind.RED, true, 8, prefix + "R"); + add(DataType.UNSIGNED, DataKind.GREEN, true, 8, prefix + "G"); + add(DataType.UNSIGNED, DataKind.BLUE, true, 8, prefix + "B"); + add(DataType.UNSIGNED, DataKind.ALPHA, true, 8, prefix + "A"); + return this; + } + static synchronized Element internalCreate(RenderScript rs, Builder b) { rs.nElementBegin(); for (int ct=0; ct < b.mEntryCount; ct++) { @@ -292,7 +360,7 @@ public class Element extends BaseObj { } } int id = rs.nElementCreate(); - return new Element(id, rs); + return new Element(id, rs, (b.mSizeBits + 7) >> 3); } public Element create() { diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index 84890038..ee7b702 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -97,12 +97,13 @@ public class RenderScript { native int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp); native void nAllocationUploadToTexture(int alloc, int baseMioLevel); - native void nAllocationData(int id, int[] d); - native void nAllocationData(int id, float[] d); - native void nAllocationSubData1D(int id, int off, int count, int[] d); - native void nAllocationSubData1D(int id, int off, int count, float[] d); - native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d); - native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d); + native void nAllocationUploadToBufferObject(int alloc); + native void nAllocationData(int id, int[] d, int sizeBytes); + native void nAllocationData(int id, float[] d, int sizeBytes); + native void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes); + native void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes); + native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes); + native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes); native void nAllocationRead(int id, int[] d); native void nAllocationRead(int id, float[] d); native void nAllocationDataFromObject(int id, Type t, Object o); diff --git a/graphics/java/android/renderscript/SimpleMesh.java b/graphics/java/android/renderscript/SimpleMesh.java index d80551e..e66fb8a 100644 --- a/graphics/java/android/renderscript/SimpleMesh.java +++ b/graphics/java/android/renderscript/SimpleMesh.java @@ -167,5 +167,150 @@ public class SimpleMesh extends BaseObj { } } + public static class TriangleMeshBuilder { + float mVtxData[]; + int mVtxCount; + int mIndexData[]; + int mIndexCount; + RenderScript mRS; + Element mElement; + + int mVtxSize; + boolean mNorm; + boolean mTex; + + public TriangleMeshBuilder(RenderScript rs, int vtxSize, boolean norm, boolean tex) { + mRS = rs; + mVtxCount = 0; + mIndexCount = 0; + mVtxData = new float[128]; + mIndexData = new int[128]; + mVtxSize = vtxSize; + mNorm = norm; + mTex = tex; + + if(vtxSize < 2 || vtxSize > 3) { + throw new IllegalArgumentException("Vertex size out of range."); + } + } + + private void makeSpace(int count) { + if((mVtxCount + count) >= mVtxData.length) { + float t[] = new float[mVtxData.length * 2]; + System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); + mVtxData = t; + } + } + + public void add_XY(float x, float y) { + if((mVtxSize != 2) || mNorm || mTex) { + throw new IllegalStateException("add mistmatch with declaired components."); + } + makeSpace(2); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + } + + public void add_XYZ(float x, float y, float z) { + if((mVtxSize != 3) || mNorm || mTex) { + throw new IllegalStateException("add mistmatch with declaired components."); + } + makeSpace(3); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + mVtxData[mVtxCount++] = z; + } + + public void add_XY_ST(float x, float y, float s, float t) { + if((mVtxSize != 2) || mNorm || !mTex) { + throw new IllegalStateException("add mistmatch with declaired components."); + } + makeSpace(4); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + mVtxData[mVtxCount++] = s; + mVtxData[mVtxCount++] = t; + } + + public void add_XYZ_ST(float x, float y, float z, float s, float t) { + if((mVtxSize != 3) || mNorm || !mTex) { + throw new IllegalStateException("add mistmatch with declaired components."); + } + makeSpace(5); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + mVtxData[mVtxCount++] = z; + mVtxData[mVtxCount++] = s; + mVtxData[mVtxCount++] = t; + } + + public void add_XYZ_ST_NORM(float x, float y, float z, float s, float t, float nx, float ny, float nz) { + if((mVtxSize != 3) || !mNorm || !mTex) { + throw new IllegalStateException("add mistmatch with declaired components."); + } + makeSpace(8); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + mVtxData[mVtxCount++] = z; + mVtxData[mVtxCount++] = s; + mVtxData[mVtxCount++] = t; + mVtxData[mVtxCount++] = nx; + mVtxData[mVtxCount++] = ny; + mVtxData[mVtxCount++] = nz; + } + + public void addTriangle(int idx1, int idx2, int idx3) { + if((mIndexCount + 3) >= mIndexData.length) { + int t[] = new int[mIndexData.length * 2]; + System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); + mIndexData = t; + } + mIndexData[mIndexCount++] = idx1; + mIndexData[mIndexCount++] = idx2; + mIndexData[mIndexCount++] = idx3; + } + + public SimpleMesh create() { + Element.Builder b = new Element.Builder(mRS); + int floatCount = mVtxSize; + if(mVtxSize == 2) { + b.addFloatXY(); + } else { + b.addFloatXYZ(); + } + if(mTex) { + floatCount += 2; + b.addFloatST(); + } + if(mNorm) { + floatCount += 3; + b.addFloatNorm(); + } + mElement = b.create(); + + Builder smb = new Builder(mRS); + smb.addVertexType(mElement, mVtxCount / floatCount); + smb.setIndexType(Element.INDEX_16, mIndexCount); + smb.setPrimitive(Primitive.TRIANGLE); + SimpleMesh sm = smb.create(); + + Allocation vertexAlloc = sm.createVertexAllocation(0); + Allocation indexAlloc = sm.createIndexAllocation(); + sm.bindVertexAllocation(vertexAlloc, 0); + sm.bindIndexAllocation(indexAlloc); + + vertexAlloc.data(mVtxData); + vertexAlloc.uploadToBufferObject(); + + // This is safe because length is a pow2 + for(int ct=0; ct < (mIndexCount+1); ct += 2) { + mIndexData[ct >> 1] = mIndexData[ct] | (mIndexData[ct+1] << 16); + } + indexAlloc.data(mIndexData); + indexAlloc.uploadToBufferObject(); + + return sm; + } + } } |