diff options
26 files changed, 853 insertions, 508 deletions
diff --git a/graphics/java/android/renderscript/FileA3D.java b/graphics/java/android/renderscript/FileA3D.java index fb36f1f..3b3711b 100644 --- a/graphics/java/android/renderscript/FileA3D.java +++ b/graphics/java/android/renderscript/FileA3D.java @@ -36,7 +36,6 @@ public class FileA3D extends BaseObj { UNKNOWN, MESH, - SIMPLE_MESH, TYPE, ELEMENT, ALLOCATION, @@ -89,10 +88,7 @@ public class FileA3D extends BaseObj { switch (mClassID) { case MESH: - mLoadedObj = null; - break; - case SIMPLE_MESH: - mLoadedObj = new SimpleMesh(objectID, mRS); + mLoadedObj = new Mesh(objectID, mRS); break; case TYPE: mLoadedObj = new Type(objectID, mRS); diff --git a/graphics/java/android/renderscript/Mesh.java b/graphics/java/android/renderscript/Mesh.java new file mode 100644 index 0000000..5a53878 --- /dev/null +++ b/graphics/java/android/renderscript/Mesh.java @@ -0,0 +1,447 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.renderscript; + +import java.util.Vector; + +import android.util.Config; +import android.util.Log; + +/** + * @hide + * + **/ +public class Mesh extends BaseObj { + + Allocation[] mVertexBuffers; + Allocation[] mIndexBuffers; + Primitive[] mPrimitives; + + Mesh(int id, RenderScript rs) { + super(rs); + mID = id; + } + + public int getVertexAllocationCount() { + if(mVertexBuffers == null) { + return 0; + } + return mVertexBuffers.length; + } + public Allocation getVertexAllocation(int slot) { + return mVertexBuffers[slot]; + } + + public int getPrimitiveCount() { + if(mIndexBuffers == null) { + return 0; + } + return mIndexBuffers.length; + } + public Allocation getIndexAllocation(int slot) { + return mIndexBuffers[slot]; + } + public Primitive getPrimitive(int slot) { + return mPrimitives[slot]; + } + + public static class Builder { + RenderScript mRS; + + class Entry { + Type t; + Element e; + int size; + Primitive prim; + } + + int mVertexTypeCount; + Entry[] mVertexTypes; + Vector mIndexTypes; + + public Builder(RenderScript rs) { + mRS = rs; + mVertexTypeCount = 0; + mVertexTypes = new Entry[16]; + mIndexTypes = new Vector(); + } + + public int addVertexType(Type t) throws IllegalStateException { + if (mVertexTypeCount >= mVertexTypes.length) { + throw new IllegalStateException("Max vertex types exceeded."); + } + + int addedIndex = mVertexTypeCount; + mVertexTypes[mVertexTypeCount] = new Entry(); + mVertexTypes[mVertexTypeCount].t = t; + mVertexTypes[mVertexTypeCount].e = null; + mVertexTypeCount++; + return addedIndex; + } + + public int addVertexType(Element e, int size) throws IllegalStateException { + if (mVertexTypeCount >= mVertexTypes.length) { + throw new IllegalStateException("Max vertex types exceeded."); + } + + int addedIndex = mVertexTypeCount; + mVertexTypes[mVertexTypeCount] = new Entry(); + mVertexTypes[mVertexTypeCount].t = null; + mVertexTypes[mVertexTypeCount].e = e; + mVertexTypes[mVertexTypeCount].size = size; + mVertexTypeCount++; + return addedIndex; + } + + public int addIndexType(Type t, Primitive p) { + int addedIndex = mIndexTypes.size(); + Entry indexType = new Entry(); + indexType.t = t; + indexType.e = null; + indexType.size = 0; + indexType.prim = p; + mIndexTypes.addElement(indexType); + return addedIndex; + } + + public int addIndexType(Primitive p) { + int addedIndex = mIndexTypes.size(); + Entry indexType = new Entry(); + indexType.t = null; + indexType.e = null; + indexType.size = 0; + indexType.prim = p; + mIndexTypes.addElement(indexType); + return addedIndex; + } + + public int addIndexType(Element e, int size, Primitive p) { + int addedIndex = mIndexTypes.size(); + Entry indexType = new Entry(); + indexType.t = null; + indexType.e = e; + indexType.size = size; + indexType.prim = p; + mIndexTypes.addElement(indexType); + return addedIndex; + } + + Type newType(Element e, int size) { + Type.Builder tb = new Type.Builder(mRS, e); + tb.add(Dimension.X, size); + return tb.create(); + } + + static synchronized Mesh internalCreate(RenderScript rs, Builder b) { + + int id = rs.nMeshCreate(b.mVertexTypeCount, b.mIndexTypes.size()); + Mesh newMesh = new Mesh(id, rs); + newMesh.mIndexBuffers = new Allocation[b.mIndexTypes.size()]; + newMesh.mPrimitives = new Primitive[b.mIndexTypes.size()]; + newMesh.mVertexBuffers = new Allocation[b.mVertexTypeCount]; + + for(int ct = 0; ct < b.mIndexTypes.size(); ct ++) { + Allocation alloc = null; + Entry entry = (Entry)b.mIndexTypes.elementAt(ct); + if (entry.t != null) { + alloc = Allocation.createTyped(rs, entry.t); + } + else if(entry.e != null) { + alloc = Allocation.createSized(rs, entry.e, entry.size); + } + int allocID = (alloc == null) ? 0 : alloc.getID(); + rs.nMeshBindIndex(id, allocID, entry.prim.mID, ct); + newMesh.mIndexBuffers[ct] = alloc; + newMesh.mPrimitives[ct] = entry.prim; + } + + for(int ct = 0; ct < b.mVertexTypeCount; ct ++) { + Allocation alloc = null; + Entry entry = b.mVertexTypes[ct]; + if (entry.t != null) { + alloc = Allocation.createTyped(rs, entry.t); + } else if(entry.e != null) { + alloc = Allocation.createSized(rs, entry.e, entry.size); + } + rs.nMeshBindVertex(id, alloc.getID(), ct); + newMesh.mVertexBuffers[ct] = alloc; + } + + return newMesh; + } + + public Mesh create() { + mRS.validate(); + Mesh sm = internalCreate(mRS, this); + return sm; + } + } + + public static class AllocationBuilder { + RenderScript mRS; + + class Entry { + Allocation a; + Primitive prim; + } + + int mVertexTypeCount; + Entry[] mVertexTypes; + + Vector mIndexTypes; + + public AllocationBuilder(RenderScript rs) { + mRS = rs; + mVertexTypeCount = 0; + mVertexTypes = new Entry[16]; + mIndexTypes = new Vector(); + } + + public int addVertexAllocation(Allocation a) throws IllegalStateException { + if (mVertexTypeCount >= mVertexTypes.length) { + throw new IllegalStateException("Max vertex types exceeded."); + } + + int addedIndex = mVertexTypeCount; + mVertexTypes[mVertexTypeCount] = new Entry(); + mVertexTypes[mVertexTypeCount].a = a; + mVertexTypeCount++; + return addedIndex; + } + + public int addIndexAllocation(Allocation a, Primitive p) { + int addedIndex = mIndexTypes.size(); + Entry indexType = new Entry(); + indexType.a = a; + indexType.prim = p; + mIndexTypes.addElement(indexType); + return addedIndex; + } + + public int addIndexType(Primitive p) { + int addedIndex = mIndexTypes.size(); + Entry indexType = new Entry(); + indexType.a = null; + indexType.prim = p; + mIndexTypes.addElement(indexType); + return addedIndex; + } + + static synchronized Mesh internalCreate(RenderScript rs, AllocationBuilder b) { + + int id = rs.nMeshCreate(b.mVertexTypeCount, b.mIndexTypes.size()); + Mesh newMesh = new Mesh(id, rs); + newMesh.mIndexBuffers = new Allocation[b.mIndexTypes.size()]; + newMesh.mPrimitives = new Primitive[b.mIndexTypes.size()]; + newMesh.mVertexBuffers = new Allocation[b.mVertexTypeCount]; + + for(int ct = 0; ct < b.mIndexTypes.size(); ct ++) { + Entry entry = (Entry)b.mIndexTypes.elementAt(ct); + int allocID = (entry.a == null) ? 0 : entry.a.getID(); + rs.nMeshBindIndex(id, allocID, entry.prim.mID, ct); + newMesh.mIndexBuffers[ct] = entry.a; + newMesh.mPrimitives[ct] = entry.prim; + } + + for(int ct = 0; ct < b.mVertexTypeCount; ct ++) { + Entry entry = b.mVertexTypes[ct]; + rs.nMeshBindVertex(id, entry.a.mID, ct); + newMesh.mVertexBuffers[ct] = entry.a; + } + + return newMesh; + } + + public Mesh create() { + mRS.validate(); + Mesh sm = internalCreate(mRS, this); + return sm; + } + } + + + public static class TriangleMeshBuilder { + float mVtxData[]; + int mVtxCount; + short mIndexData[]; + int mIndexCount; + RenderScript mRS; + Element mElement; + + float mNX = 0; + float mNY = 0; + float mNZ = -1; + float mS0 = 0; + float mT0 = 0; + float mR = 1; + float mG = 1; + float mB = 1; + float mA = 1; + + int mVtxSize; + int mFlags; + + public static final int COLOR = 0x0001; + public static final int NORMAL = 0x0002; + public static final int TEXTURE_0 = 0x0100; + + public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) { + mRS = rs; + mVtxCount = 0; + mIndexCount = 0; + mVtxData = new float[128]; + mIndexData = new short[128]; + mVtxSize = vtxSize; + mFlags = flags; + + 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; + } + } + + private void latch() { + if ((mFlags & COLOR) != 0) { + makeSpace(4); + mVtxData[mVtxCount++] = mR; + mVtxData[mVtxCount++] = mG; + mVtxData[mVtxCount++] = mB; + mVtxData[mVtxCount++] = mA; + } + if ((mFlags & TEXTURE_0) != 0) { + makeSpace(2); + mVtxData[mVtxCount++] = mS0; + mVtxData[mVtxCount++] = mT0; + } + if ((mFlags & NORMAL) != 0) { + makeSpace(3); + mVtxData[mVtxCount++] = mNX; + mVtxData[mVtxCount++] = mNY; + mVtxData[mVtxCount++] = mNZ; + } + } + + public void addVertex(float x, float y) { + if (mVtxSize != 2) { + throw new IllegalStateException("add mistmatch with declared components."); + } + makeSpace(2); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + latch(); + } + + public void addVertex(float x, float y, float z) { + if (mVtxSize != 3) { + throw new IllegalStateException("add mistmatch with declared components."); + } + makeSpace(3); + mVtxData[mVtxCount++] = x; + mVtxData[mVtxCount++] = y; + mVtxData[mVtxCount++] = z; + latch(); + } + + public void setTexture(float s, float t) { + if ((mFlags & TEXTURE_0) == 0) { + throw new IllegalStateException("add mistmatch with declared components."); + } + mS0 = s; + mT0 = t; + } + + public void setNormal(float x, float y, float z) { + if ((mFlags & NORMAL) == 0) { + throw new IllegalStateException("add mistmatch with declared components."); + } + mNX = x; + mNY = y; + mNZ = z; + } + + public void setColor(float r, float g, float b, float a) { + if ((mFlags & COLOR) == 0) { + throw new IllegalStateException("add mistmatch with declared components."); + } + mR = r; + mG = g; + mB = b; + mA = a; + } + + public void addTriangle(int idx1, int idx2, int idx3) { + if((idx1 >= mVtxCount) || (idx1 < 0) || + (idx2 >= mVtxCount) || (idx2 < 0) || + (idx3 >= mVtxCount) || (idx3 < 0)) { + throw new IllegalStateException("Index provided greater than vertex count."); + } + if ((mIndexCount + 3) >= mIndexData.length) { + short t[] = new short[mIndexData.length * 2]; + System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); + mIndexData = t; + } + mIndexData[mIndexCount++] = (short)idx1; + mIndexData[mIndexCount++] = (short)idx2; + mIndexData[mIndexCount++] = (short)idx3; + } + + public Mesh create(boolean uploadToBufferObject) { + Element.Builder b = new Element.Builder(mRS); + int floatCount = mVtxSize; + b.add(Element.createVector(mRS, + Element.DataType.FLOAT_32, + mVtxSize), "position"); + if ((mFlags & COLOR) != 0) { + floatCount += 4; + b.add(Element.F32_4(mRS), "color"); + } + if ((mFlags & TEXTURE_0) != 0) { + floatCount += 2; + b.add(Element.F32_2(mRS), "texture0"); + } + if ((mFlags & NORMAL) != 0) { + floatCount += 3; + b.add(Element.F32_3(mRS), "normal"); + } + mElement = b.create(); + + Builder smb = new Builder(mRS); + smb.addVertexType(mElement, mVtxCount / floatCount); + smb.addIndexType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE); + + Mesh sm = smb.create(); + + sm.getVertexAllocation(0).data(mVtxData); + if(uploadToBufferObject) { + sm.getVertexAllocation(0).uploadToBufferObject(); + } + + sm.getIndexAllocation(0).data(mIndexData); + sm.getIndexAllocation(0).uploadToBufferObject(); + + return sm; + } + } +} + diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index b2e5b02..c7e8ca5 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -186,6 +186,10 @@ public class RenderScript { native void nLightSetColor(int l, float r, float g, float b); native void nLightSetPosition(int l, float x, float y, float z); + native int nMeshCreate(int vtxCount, int indexCount); + native void nMeshBindVertex(int id, int alloc, int slot); + native void nMeshBindIndex(int id, int alloc, int prim, int slot); + native int nSimpleMeshCreate(int batchID, int idxID, int[] vtxID, int prim); native void nSimpleMeshBindVertex(int id, int alloc, int slot); native void nSimpleMeshBindIndex(int id, int alloc); diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp index c61a215..a6d2489 100644 --- a/graphics/jni/android_renderscript_RenderScript.cpp +++ b/graphics/jni/android_renderscript_RenderScript.cpp @@ -1351,6 +1351,33 @@ nLightSetPosition(JNIEnv *_env, jobject _this, jint light, float x, float y, flo // --------------------------------------------------------------------------- static jint +nMeshCreate(JNIEnv *_env, jobject _this, jint vtxCount, jint idxCount) +{ + RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); + LOG_API("nMeshCreate, con(%p), vtxCount(%i), idxCount(%i)", con, vtxCount, idxCount); + int id = (int)rsMeshCreate(con, vtxCount, idxCount); + return id; +} + +static void +nMeshBindVertex(JNIEnv *_env, jobject _this, jint s, jint alloc, jint slot) +{ + RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); + LOG_API("nMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)s, (RsAllocation)alloc, slot); + rsMeshBindVertex(con, (RsMesh)s, (RsAllocation)alloc, slot); +} + +static void +nMeshBindIndex(JNIEnv *_env, jobject _this, jint s, jint alloc, jint primID, jint slot) +{ + RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); + LOG_API("nMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)s, (RsAllocation)alloc); + rsMeshBindIndex(con, (RsMesh)s, (RsAllocation)alloc, primID, slot); +} + +// --------------------------------------------------------------------------- + +static jint nSimpleMeshCreate(JNIEnv *_env, jobject _this, jint batchID, jint indexID, jintArray vtxIDs, jint primID) { RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); @@ -1367,16 +1394,16 @@ static void nSimpleMeshBindVertex(JNIEnv *_env, jobject _this, jint s, jint alloc, jint slot) { RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); - LOG_API("nSimpleMeshBindVertex, con(%p), SimpleMesh(%p), Alloc(%p), slot(%i)", con, (RsSimpleMesh)s, (RsAllocation)alloc, slot); - rsSimpleMeshBindVertex(con, (RsSimpleMesh)s, (RsAllocation)alloc, slot); + LOG_API("nSimpleMeshBindVertex, con(%p), Mesh(%p), Alloc(%p), slot(%i)", con, (RsMesh)s, (RsAllocation)alloc, slot); + rsSimpleMeshBindVertex(con, (RsMesh)s, (RsAllocation)alloc, slot); } static void nSimpleMeshBindIndex(JNIEnv *_env, jobject _this, jint s, jint alloc) { RsContext con = (RsContext)(_env->GetIntField(_this, gContextId)); - LOG_API("nSimpleMeshBindIndex, con(%p), SimpleMesh(%p), Alloc(%p)", con, (RsSimpleMesh)s, (RsAllocation)alloc); - rsSimpleMeshBindIndex(con, (RsSimpleMesh)s, (RsAllocation)alloc); + LOG_API("nSimpleMeshBindIndex, con(%p), Mesh(%p), Alloc(%p)", con, (RsMesh)s, (RsAllocation)alloc); + rsSimpleMeshBindIndex(con, (RsMesh)s, (RsAllocation)alloc); } // --------------------------------------------------------------------------- @@ -1513,6 +1540,10 @@ static JNINativeMethod methods[] = { {"nSimpleMeshBindVertex", "(III)V", (void*)nSimpleMeshBindVertex }, {"nSimpleMeshBindIndex", "(II)V", (void*)nSimpleMeshBindIndex }, +{"nMeshCreate", "(II)I", (void*)nMeshCreate }, +{"nMeshBindVertex", "(III)V", (void*)nMeshBindVertex }, +{"nMeshBindIndex", "(IIII)V", (void*)nMeshBindIndex }, + }; static int registerFuncs(JNIEnv *_env) diff --git a/libs/rs/Android.mk b/libs/rs/Android.mk index 7afc4f2..37c418b 100644 --- a/libs/rs/Android.mk +++ b/libs/rs/Android.mk @@ -102,7 +102,6 @@ LOCAL_SRC_FILES:= \ rsScriptC_LibGL.cpp \ rsShaderCache.cpp \ rsSignal.cpp \ - rsSimpleMesh.cpp \ rsStream.cpp \ rsThreadIO.cpp \ rsType.cpp \ diff --git a/libs/rs/RenderScript.h b/libs/rs/RenderScript.h index 1428318..8e6b5c6 100644 --- a/libs/rs/RenderScript.h +++ b/libs/rs/RenderScript.h @@ -38,7 +38,7 @@ typedef void * RsFile; typedef void * RsFont; typedef void * RsSampler; typedef void * RsScript; -typedef void * RsSimpleMesh; +typedef void * RsMesh; typedef void * RsType; typedef void * RsLight; typedef void * RsObjectBase; @@ -229,7 +229,6 @@ enum RsAnimationEdge { enum RsA3DClassID { RS_A3D_CLASS_ID_UNKNOWN, RS_A3D_CLASS_ID_MESH, - RS_A3D_CLASS_ID_SIMPLE_MESH, RS_A3D_CLASS_ID_TYPE, RS_A3D_CLASS_ID_ELEMENT, RS_A3D_CLASS_ID_ALLOCATION, diff --git a/libs/rs/RenderScriptEnv.h b/libs/rs/RenderScriptEnv.h index 144e539..9225904 100644 --- a/libs/rs/RenderScriptEnv.h +++ b/libs/rs/RenderScriptEnv.h @@ -9,7 +9,7 @@ typedef void * RsDevice; typedef void * RsElement; typedef void * RsSampler; typedef void * RsScript; -typedef void * RsSimpleMesh; +typedef void * RsMesh; typedef void * RsType; typedef void * RsProgramFragment; typedef void * RsProgramStore; diff --git a/libs/rs/java/Fountain/res/raw/fountain.rs b/libs/rs/java/Fountain/res/raw/fountain.rs index 2ecb391..c8c10cd 100644 --- a/libs/rs/java/Fountain/res/raw/fountain.rs +++ b/libs/rs/java/Fountain/res/raw/fountain.rs @@ -35,7 +35,7 @@ int root() { p++; } - rsgDrawSimpleMesh(partMesh); + rsgDrawMesh(partMesh); return 1; } diff --git a/libs/rs/java/Fountain/res/raw/fountain_bc.bc b/libs/rs/java/Fountain/res/raw/fountain_bc.bc Binary files differindex 822f30a..2c8ce8b 100644 --- a/libs/rs/java/Fountain/res/raw/fountain_bc.bc +++ b/libs/rs/java/Fountain/res/raw/fountain_bc.bc 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 c001e84..cfe8e27 100644 --- a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java +++ b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java @@ -36,11 +36,10 @@ public class FountainRS { ScriptField_Point points = new ScriptField_Point(mRS, PART_COUNT); - SimpleMesh.Builder smb = new SimpleMesh.Builder(mRS); - int vtxSlot = smb.addVertexType(points.getType()); - smb.setPrimitive(Primitive.POINT); - SimpleMesh sm = smb.create(); - sm.bindVertexAllocation(points.getAllocation(), vtxSlot); + Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS); + smb.addVertexAllocation(points.getAllocation()); + smb.addIndexType(Primitive.POINT); + Mesh sm = smb.create(); mScript = new ScriptC_Fountain(mRS, mRes, R.raw.fountain_bc, true); mScript.set_partMesh(sm); diff --git a/libs/rs/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java b/libs/rs/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java index 95ce1ab..0ec0009 100644 --- a/libs/rs/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java +++ b/libs/rs/java/Fountain/src/com/android/fountain/ScriptC_Fountain.java @@ -27,13 +27,13 @@ public class ScriptC_Fountain extends ScriptC { } private final static int mExportVarIdx_partMesh = 0; - private SimpleMesh mExportVar_partMesh; - public void set_partMesh(SimpleMesh v) { + private Mesh mExportVar_partMesh; + public void set_partMesh(Mesh v) { mExportVar_partMesh = v; setVar(mExportVarIdx_partMesh, (v == null) ? 0 : v.getID()); } - public SimpleMesh get_partMesh() { + public Mesh get_partMesh() { return mExportVar_partMesh; } diff --git a/libs/rs/java/ModelViewer/res/raw/modelviewer.rs b/libs/rs/java/ModelViewer/res/raw/modelviewer.rs index 91194e8..559bf48 100644 --- a/libs/rs/java/ModelViewer/res/raw/modelviewer.rs +++ b/libs/rs/java/ModelViewer/res/raw/modelviewer.rs @@ -62,7 +62,7 @@ int root(int launchID) { rsMatrixRotate(&matrix, gRotate, 0.0f, 1.0f, 0.0f); rsgProgramVertexLoadModelMatrix(&matrix); - rsgDrawSimpleMesh(gTestMesh); + rsgDrawMesh(gTestMesh); color(0.3f, 0.3f, 0.3f, 1.0f); rsgDrawText("Renderscript model test", 30, 695); diff --git a/libs/rs/java/ModelViewer/res/raw/modelviewer_bc.bc b/libs/rs/java/ModelViewer/res/raw/modelviewer_bc.bc Binary files differindex a64e725..fb85028 100644 --- a/libs/rs/java/ModelViewer/res/raw/modelviewer_bc.bc +++ b/libs/rs/java/ModelViewer/res/raw/modelviewer_bc.bc diff --git a/libs/rs/java/ModelViewer/res/raw/robot.a3d b/libs/rs/java/ModelViewer/res/raw/robot.a3d Binary files differindex c0c66ae..430fe95 100644 --- a/libs/rs/java/ModelViewer/res/raw/robot.a3d +++ b/libs/rs/java/ModelViewer/res/raw/robot.a3d diff --git a/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewerRS.java b/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewerRS.java index b6485dc..37eb9c1 100644 --- a/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewerRS.java +++ b/libs/rs/java/ModelViewer/src/com/android/modelviewer/ModelViewerRS.java @@ -55,7 +55,7 @@ public class ModelViewerRS { private Allocation mGridImage; private Allocation mAllocPV; - private SimpleMesh mMesh; + private Mesh mMesh; private Font mItalic; private Allocation mTextAlloc; @@ -149,15 +149,15 @@ public class ModelViewerRS { FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.robot); FileA3D.IndexEntry entry = model.getIndexEntry(0); - if(entry == null || entry.getClassID() != FileA3D.ClassID.SIMPLE_MESH) { + if(entry == null || entry.getClassID() != FileA3D.ClassID.MESH) { Log.e("rs", "could not load model"); } else { - mMesh = (SimpleMesh)entry.getObject(); + mMesh = (Mesh)entry.getObject(); mScript.set_gTestMesh(mMesh); } - mItalic = Font.create(mRS, mRes, "DroidSerif-Italic.ttf", 10); + mItalic = Font.create(mRS, mRes, "DroidSerif-Italic.ttf", 8); mScript.set_gItalic(mItalic); initTextAllocation(); diff --git a/libs/rs/java/ModelViewer/src/com/android/modelviewer/ScriptC_Modelviewer.java b/libs/rs/java/ModelViewer/src/com/android/modelviewer/ScriptC_Modelviewer.java index d3a2a29..06c10ab 100644 --- a/libs/rs/java/ModelViewer/src/com/android/modelviewer/ScriptC_Modelviewer.java +++ b/libs/rs/java/ModelViewer/src/com/android/modelviewer/ScriptC_Modelviewer.java @@ -60,13 +60,13 @@ public class ScriptC_Modelviewer extends ScriptC { } private final static int mExportVarIdx_gTestMesh = 3; - private SimpleMesh mExportVar_gTestMesh; - public void set_gTestMesh(SimpleMesh v) { + private Mesh mExportVar_gTestMesh; + public void set_gTestMesh(Mesh v) { mExportVar_gTestMesh = v; setVar(mExportVarIdx_gTestMesh, (v == null) ? 0 : v.getID()); } - public SimpleMesh get_gTestMesh() { + public Mesh get_gTestMesh() { return mExportVar_gTestMesh; } diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec index 3694b65..172ba66 100644 --- a/libs/rs/rs.spec +++ b/libs/rs/rs.spec @@ -478,8 +478,34 @@ FontCreateFromFile { ret RsFont } +MeshCreate { + ret RsMesh + param uint32_t vtxCount + param uint32_t idxCount + } + +MeshBindIndex { + param RsMesh mesh + param RsAllocation idx + param uint32_t primType + param uint32_t slot + } + +MeshBindPrimitive { + param RsMesh mesh + param RsAllocation prim + param uint32_t primType + param uint32_t slot + } + +MeshBindVertex { + param RsMesh mesh + param RsAllocation vtx + param uint32_t slot + } + SimpleMeshCreate { - ret RsSimpleMesh + ret RsMesh param RsAllocation prim param RsAllocation index param RsAllocation *vtx @@ -489,17 +515,17 @@ SimpleMeshCreate { SimpleMeshBindIndex { - param RsSimpleMesh mesh + param RsMesh mesh param RsAllocation idx } SimpleMeshBindPrimitive { - param RsSimpleMesh mesh + param RsMesh mesh param RsAllocation prim } SimpleMeshBindVertex { - param RsSimpleMesh mesh + param RsMesh mesh param RsAllocation vtx param uint32_t slot } diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h index 73f478a..06433a1 100644 --- a/libs/rs/rsContext.h +++ b/libs/rs/rsContext.h @@ -24,7 +24,6 @@ #include "rsType.h" #include "rsMatrix.h" #include "rsAllocation.h" -#include "rsSimpleMesh.h" #include "rsMesh.h" #include "rsDevice.h" #include "rsScriptC.h" diff --git a/libs/rs/rsContextHostStub.h b/libs/rs/rsContextHostStub.h index be1fff6..c437606 100644 --- a/libs/rs/rsContextHostStub.h +++ b/libs/rs/rsContextHostStub.h @@ -24,7 +24,6 @@ #include "rsType.h" #include "rsMatrix.h" #include "rsAllocation.h" -#include "rsSimpleMesh.h" #include "rsMesh.h" //#include "rsDevice.h" #include "rsScriptC.h" diff --git a/libs/rs/rsFileA3D.cpp b/libs/rs/rsFileA3D.cpp index 4fac421..5709f2a 100644 --- a/libs/rs/rsFileA3D.cpp +++ b/libs/rs/rsFileA3D.cpp @@ -258,9 +258,6 @@ ObjectBase *FileA3D::initializeFromEntry(size_t index) { case RS_A3D_CLASS_ID_MESH: entry->mRsObj = Mesh::createFromStream(mRSC, mReadStream); break; - case RS_A3D_CLASS_ID_SIMPLE_MESH: - entry->mRsObj = SimpleMesh::createFromStream(mRSC, mReadStream); - break; case RS_A3D_CLASS_ID_TYPE: entry->mRsObj = Type::createFromStream(mRSC, mReadStream); break; diff --git a/libs/rs/rsMesh.cpp b/libs/rs/rsMesh.cpp index bd9cd27..4f90f36 100644 --- a/libs/rs/rsMesh.cpp +++ b/libs/rs/rsMesh.cpp @@ -35,43 +35,138 @@ Mesh::Mesh(Context *rsc) : ObjectBase(rsc) { mAllocFile = __FILE__; mAllocLine = __LINE__; - mVerticies = NULL; - mVerticiesCount = 0; mPrimitives = NULL; mPrimitivesCount = 0; + mVertexBuffers = NULL; + mVertexTypes = NULL; + mVertexBufferCount = 0; } Mesh::~Mesh() { + if(mVertexTypes) { + delete[] mVertexTypes; + } + + if(mVertexBuffers) { + delete[] mVertexBuffers; + } + + if(mPrimitives) { + for(uint32_t i = 0; i < mPrimitivesCount; i ++) { + delete mPrimitives[i]; + } + delete[] mPrimitives; + } } -void Mesh::serialize(OStream *stream) const +void Mesh::render(Context *rsc) const { - // Need to identify ourselves - stream->addU32((uint32_t)getClassId()); + for(uint32_t ct = 0; ct < mPrimitivesCount; ct ++) { + renderPrimitive(rsc, ct); + } +} - String8 name(getName()); - stream->addString(&name); +void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const { + if (primIndex >= mPrimitivesCount) { + LOGE("Invalid primitive index"); + return; + } - stream->addU32(mVerticiesCount); + Primitive_t *prim = mPrimitives[primIndex]; - for(uint32_t vCount = 0; vCount < mVerticiesCount; vCount ++) { - Verticies_t *verts = mVerticies[vCount]; + if (prim->mIndexBuffer.get()) { + renderPrimitiveRange(rsc, primIndex, 0, prim->mIndexBuffer->getType()->getDimX()); + return; + } - stream->addU32(verts->mAllocationCount); + if (prim->mPrimitiveBuffer.get()) { + renderPrimitiveRange(rsc, primIndex, 0, prim->mPrimitiveBuffer->getType()->getDimX()); + return; + } + + renderPrimitiveRange(rsc, primIndex, 0, mVertexBuffers[0]->getType()->getDimX()); +} - for (uint32_t aCount = 0; aCount < verts->mAllocationCount; aCount++) { - verts->mAllocations[aCount]->serialize(stream); +void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const +{ + if (len < 1 || primIndex >= mPrimitivesCount) { + return; + } + + rsc->checkError("Mesh::renderPrimitiveRange 1"); + VertexArray va; + for (uint32_t ct=0; ct < mVertexBufferCount; ct++) { + mVertexBuffers[ct]->uploadCheck(rsc); + if (mVertexBuffers[ct]->getIsBufferObject()) { + va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID()); + } else { + va.setActiveBuffer(mVertexBuffers[ct]->getPtr()); } - stream->addU32(verts->mVertexDataSize); + mVertexBuffers[ct]->getType()->enableGLVertexBuffer(&va); + } + va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); + + rsc->checkError("Mesh::renderPrimitiveRange 2"); + Primitive_t *prim = mPrimitives[primIndex]; + if (prim->mIndexBuffer.get()) { + prim->mIndexBuffer->uploadCheck(rsc); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prim->mIndexBuffer->getBufferObjectID()); + glDrawElements(prim->mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2)); + } else { + glDrawArrays(prim->mGLPrimitive, start, len); + } + + rsc->checkError("Mesh::renderPrimitiveRange"); +} - stream->addU32(verts->mOffsetCoord); - stream->addU32(verts->mOffsetTex); - stream->addU32(verts->mOffsetNorm); - stream->addU32(verts->mSizeCoord); - stream->addU32(verts->mSizeTex); - stream->addU32(verts->mSizeNorm ); +void Mesh::uploadAll(Context *rsc) +{ + for (uint32_t ct = 0; ct < mVertexBufferCount; ct ++) { + if (mVertexBuffers[ct].get()) { + mVertexBuffers[ct]->deferedUploadToBufferObject(rsc); + } + } + + for (uint32_t ct = 0; ct < mPrimitivesCount; ct ++) { + if (mPrimitives[ct]->mIndexBuffer.get()) { + mPrimitives[ct]->mIndexBuffer->deferedUploadToBufferObject(rsc); + } + if (mPrimitives[ct]->mPrimitiveBuffer.get()) { + mPrimitives[ct]->mPrimitiveBuffer->deferedUploadToBufferObject(rsc); + } + } + + rsc->checkError("Mesh::uploadAll"); +} + +void Mesh::updateGLPrimitives() +{ + for(uint32_t i = 0; i < mPrimitivesCount; i ++) { + switch(mPrimitives[i]->mPrimitive) { + case RS_PRIMITIVE_POINT: mPrimitives[i]->mGLPrimitive = GL_POINTS; break; + case RS_PRIMITIVE_LINE: mPrimitives[i]->mGLPrimitive = GL_LINES; break; + case RS_PRIMITIVE_LINE_STRIP: mPrimitives[i]->mGLPrimitive = GL_LINE_STRIP; break; + case RS_PRIMITIVE_TRIANGLE: mPrimitives[i]->mGLPrimitive = GL_TRIANGLES; break; + case RS_PRIMITIVE_TRIANGLE_STRIP: mPrimitives[i]->mGLPrimitive = GL_TRIANGLE_STRIP; break; + case RS_PRIMITIVE_TRIANGLE_FAN: mPrimitives[i]->mGLPrimitive = GL_TRIANGLE_FAN; break; + } + } +} + +void Mesh::serialize(OStream *stream) const +{ + // Need to identify ourselves + stream->addU32((uint32_t)getClassId()); + + String8 name(getName()); + stream->addString(&name); + + // Store number of vertex streams + stream->addU32(mVertexBufferCount); + for(uint32_t vCount = 0; vCount < mVertexBufferCount; vCount ++) { + mVertexBuffers[vCount]->serialize(stream); } stream->addU32(mPrimitivesCount); @@ -79,27 +174,22 @@ void Mesh::serialize(OStream *stream) const for (uint32_t pCount = 0; pCount < mPrimitivesCount; pCount ++) { Primitive_t * prim = mPrimitives[pCount]; - stream->addU8((uint8_t)prim->mType); + stream->addU8((uint8_t)prim->mPrimitive); - // We store the index to the vertices - // So iterate over our vertices to find which one we point to - uint32_t vertexIndex = 0; - for(uint32_t vCount = 0; vCount < mVerticiesCount; vCount ++) { - if(prim->mVerticies == mVerticies[vCount]) { - vertexIndex = vCount; - break; - } + if(prim->mIndexBuffer.get()) { + stream->addU32(1); + prim->mIndexBuffer->serialize(stream); } - stream->addU32(vertexIndex); - - stream->addU32(prim->mIndexCount); - for (uint32_t ct = 0; ct < prim->mIndexCount; ct++) { - stream->addU16(prim->mIndicies[ct]); + else { + stream->addU32(0); } - stream->addU32(prim->mRestartCounts); - for (uint32_t ct = 0; ct < prim->mRestartCounts; ct++) { - stream->addU16(prim->mRestarts[ct]); + if(prim->mPrimitiveBuffer.get()) { + stream->addU32(1); + prim->mPrimitiveBuffer->serialize(stream); + } + else { + stream->addU32(0); } } } @@ -119,86 +209,46 @@ Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) stream->loadString(&name); mesh->setName(name.string(), name.size()); - mesh->mVerticiesCount = stream->loadU32(); - if(mesh->mVerticiesCount) { - mesh->mVerticies = new Verticies_t *[mesh->mVerticiesCount]; - } - else { - mesh->mVerticies = NULL; - } - - for(uint32_t vCount = 0; vCount < mesh->mVerticiesCount; vCount ++) { - Verticies_t *verts = new Verticies_t(); - // Store our vertices one the mesh - mesh->mVerticies[vCount] = verts; - - verts->mAllocationCount = stream->loadU32(); - verts->mAllocations = new Allocation *[verts->mAllocationCount]; + mesh->mVertexBufferCount = stream->loadU32(); + if(mesh->mVertexBufferCount) { + mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[mesh->mVertexBufferCount]; - LOGE("processChunk_Verticies count %i", verts->mAllocationCount); - for (uint32_t aCount = 0; aCount < verts->mAllocationCount; aCount++) { - verts->mAllocations[aCount] = Allocation::createFromStream(rsc, stream); + for(uint32_t vCount = 0; vCount < mesh->mVertexBufferCount; vCount ++) { + Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream); + mesh->mVertexBuffers[vCount].set(vertexAlloc); } - verts->mVertexDataSize = stream->loadU32(); - - verts->mOffsetCoord = stream->loadU32(); - verts->mOffsetTex = stream->loadU32(); - verts->mOffsetNorm = stream->loadU32(); - - verts->mSizeCoord = stream->loadU32(); - verts->mSizeTex = stream->loadU32(); - verts->mSizeNorm = stream->loadU32(); } mesh->mPrimitivesCount = stream->loadU32(); if(mesh->mPrimitivesCount) { mesh->mPrimitives = new Primitive_t *[mesh->mPrimitivesCount]; - } - else { - mesh->mPrimitives = NULL; - } - // load all primitives - for (uint32_t pCount = 0; pCount < mesh->mPrimitivesCount; pCount ++) { - Primitive_t * prim = new Primitive_t; - mesh->mPrimitives[pCount] = prim; + // load all primitives + for (uint32_t pCount = 0; pCount < mesh->mPrimitivesCount; pCount ++) { + Primitive_t * prim = new Primitive_t; + mesh->mPrimitives[pCount] = prim; - prim->mType = (RsPrimitive)stream->loadU8(); - - // We store the index to the vertices - uint32_t vertexIndex = stream->loadU32(); - if(vertexIndex < mesh->mVerticiesCount) { - prim->mVerticies = mesh->mVerticies[vertexIndex]; - } - else { - prim->mVerticies = NULL; - } + prim->mPrimitive = (RsPrimitive)stream->loadU8(); - prim->mIndexCount = stream->loadU32(); - if(prim->mIndexCount){ - prim->mIndicies = new uint16_t[prim->mIndexCount]; - for (uint32_t ct = 0; ct < prim->mIndexCount; ct++) { - prim->mIndicies[ct] = stream->loadU16(); + // Check to see if the index buffer was stored + uint32_t isIndexPresent = stream->loadU32(); + if(isIndexPresent) { + Allocation *indexAlloc = Allocation::createFromStream(rsc, stream); + prim->mIndexBuffer.set(indexAlloc); } - } - else { - prim->mIndicies = NULL; - } - - prim->mRestartCounts = stream->loadU32(); - if (prim->mRestartCounts) { - prim->mRestarts = new uint16_t[prim->mRestartCounts]; - for (uint32_t ct = 0; ct < prim->mRestartCounts; ct++) { - prim->mRestarts[ct] = stream->loadU16(); + // Check to see if the primitive buffer was stored + uint32_t isPrimitivePresent = stream->loadU32(); + if(isPrimitivePresent) { + Allocation *primitiveAlloc = Allocation::createFromStream(rsc, stream); + prim->mPrimitiveBuffer.set(primitiveAlloc); } } - else { - prim->mRestarts = NULL; - } - } + mesh->updateGLPrimitives(); + mesh->uploadAll(rsc); + return mesh; } @@ -211,3 +261,103 @@ MeshContext::~MeshContext() { } +namespace android { +namespace renderscript { + +RsMesh rsi_MeshCreate(Context *rsc, uint32_t vtxCount, uint32_t idxCount) +{ + Mesh *sm = new Mesh(rsc); + sm->incUserRef(); + + sm->mPrimitivesCount = idxCount; + sm->mPrimitives = new Mesh::Primitive_t *[sm->mPrimitivesCount]; + for(uint32_t ct = 0; ct < idxCount; ct ++) { + sm->mPrimitives[ct] = new Mesh::Primitive_t; + } + + sm->mVertexBufferCount = vtxCount; + sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount]; + sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount]; + + return sm; +} + +void rsi_MeshBindVertex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t slot) +{ + Mesh *sm = static_cast<Mesh *>(mv); + rsAssert(slot < sm->mVertexBufferCount); + + sm->mVertexBuffers[slot].set((Allocation *)va); +} + +void rsi_MeshBindIndex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t primType, uint32_t slot) +{ + Mesh *sm = static_cast<Mesh *>(mv); + rsAssert(slot < sm->mPrimitivesCount); + + sm->mPrimitives[slot]->mIndexBuffer.set((Allocation *)va); + sm->mPrimitives[slot]->mPrimitive = (RsPrimitive)primType; + sm->updateGLPrimitives(); +} + +void rsi_MeshBindPrimitive(Context *rsc, RsMesh mv, RsAllocation va, uint32_t primType, uint32_t slot) +{ + Mesh *sm = static_cast<Mesh *>(mv); + rsAssert(slot < sm->mPrimitivesCount); + + sm->mPrimitives[slot]->mPrimitiveBuffer.set((Allocation *)va); + sm->mPrimitives[slot]->mPrimitive = (RsPrimitive)primType; + sm->updateGLPrimitives(); +} + + +// Route all the simple mesh through mesh + +RsMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType) +{ + Mesh *sm = new Mesh(rsc); + sm->incUserRef(); + + sm->mPrimitivesCount = 1; + sm->mPrimitives = new Mesh::Primitive_t *[sm->mPrimitivesCount]; + sm->mPrimitives[0] = new Mesh::Primitive_t; + + sm->mPrimitives[0]->mIndexType.set((const Type *)idx); + sm->mPrimitives[0]->mPrimitiveType.set((const Type *)prim); + sm->mPrimitives[0]->mPrimitive = (RsPrimitive)primType; + sm->updateGLPrimitives(); + + sm->mVertexBufferCount = vtxCount; + sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount]; + sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount]; + for (uint32_t ct=0; ct < vtxCount; ct++) { + sm->mVertexTypes[ct].set((const Type *)vtx[ct]); + } + + return sm; +} + +void rsi_SimpleMeshBindVertex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t slot) +{ + Mesh *sm = static_cast<Mesh *>(mv); + rsAssert(slot < sm->mVertexBufferCount); + + sm->mVertexBuffers[slot].set((Allocation *)va); +} + +void rsi_SimpleMeshBindIndex(Context *rsc, RsMesh mv, RsAllocation va) +{ + Mesh *sm = static_cast<Mesh *>(mv); + sm->mPrimitives[0]->mIndexBuffer.set((Allocation *)va); +} + +void rsi_SimpleMeshBindPrimitive(Context *rsc, RsMesh mv, RsAllocation va) +{ + Mesh *sm = static_cast<Mesh *>(mv); + sm->mPrimitives[0]->mPrimitiveBuffer.set((Allocation *)va); +} + + + + +}} diff --git a/libs/rs/rsMesh.h b/libs/rs/rsMesh.h index 8c7e8a4..66174b7 100644 --- a/libs/rs/rsMesh.h +++ b/libs/rs/rsMesh.h @@ -32,43 +32,35 @@ public: Mesh(Context *); ~Mesh(); - struct Verticies_t - { - Allocation ** mAllocations; - uint32_t mAllocationCount; - - size_t mVertexDataSize; - - size_t mOffsetCoord; - size_t mOffsetTex; - size_t mOffsetNorm; - - size_t mSizeCoord; - size_t mSizeTex; - size_t mSizeNorm; - - uint32_t mBufferObject; - }; - + // Contains vertex data + // Position, normal, texcoord, etc could either be strided in one allocation + // of provided separetely in multiple ones + ObjectBaseRef<Allocation> *mVertexBuffers; + ObjectBaseRef<const Type> *mVertexTypes; + uint32_t mVertexBufferCount; + + // Either mIndexBuffer, mPrimitiveBuffer or both could have a NULL reference + // If both are null, mPrimitive only would be used to render the mesh struct Primitive_t { - RsPrimitive mType; - Verticies_t *mVerticies; - - uint32_t mIndexCount; - uint16_t *mIndicies; + ObjectBaseRef<Allocation> mIndexBuffer; + ObjectBaseRef<Allocation> mPrimitiveBuffer; + ObjectBaseRef<const Type> mIndexType; + ObjectBaseRef<const Type> mPrimitiveType; - uint32_t mRestartCounts; - uint16_t *mRestarts; + RsPrimitive mPrimitive; + uint32_t mGLPrimitive; }; - Verticies_t ** mVerticies; - uint32_t mVerticiesCount; - Primitive_t ** mPrimitives; uint32_t mPrimitivesCount; - void analyzeElement(); + void render(Context *) const; + void renderPrimitive(Context *, uint32_t primIndex) const; + void renderPrimitiveRange(Context *, uint32_t primIndex, uint32_t start, uint32_t len) const; + void uploadAll(Context *); + void updateGLPrimitives(); + virtual void serialize(OStream *stream) const; virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_MESH; } static Mesh *createFromStream(Context *rsc, IStream *stream); @@ -90,3 +82,4 @@ public: #endif //ANDROID_RS_TRIANGLE_MESH_H + diff --git a/libs/rs/rsScriptC_LibGL.cpp b/libs/rs/rsScriptC_LibGL.cpp index dbd398e..7185009 100644 --- a/libs/rs/rsScriptC_LibGL.cpp +++ b/libs/rs/rsScriptC_LibGL.cpp @@ -43,10 +43,10 @@ using namespace android::renderscript; // IO routines ////////////////////////////////////////////////////////////////////////////// -static void SC_updateSimpleMesh(RsSimpleMesh mesh) +static void SC_updateSimpleMesh(RsMesh mesh) { GET_TLS(); - SimpleMesh *sm = static_cast<SimpleMesh *>(mesh); + Mesh *sm = static_cast<Mesh *>(mesh); sm->uploadAll(rsc); } @@ -220,24 +220,54 @@ static void SC_drawRect(float x1, float y1, x1, y1, z); } -static void SC_drawSimpleMesh(RsSimpleMesh vsm) +static void SC_drawSimpleMesh(RsMesh vsm) { GET_TLS(); - SimpleMesh *sm = static_cast<SimpleMesh *>(vsm); + Mesh *sm = static_cast<Mesh *>(vsm); + if (!rsc->setupCheck()) { + return; + } + sm->renderPrimitive(rsc, 0); +} + +static void SC_drawSimpleMeshRange(RsMesh vsm, uint32_t start, uint32_t len) +{ + GET_TLS(); + Mesh *sm = static_cast<Mesh *>(vsm); + if (!rsc->setupCheck()) { + return; + } + sm->renderPrimitiveRange(rsc, 0, start, len); +} + +static void SC_drawMesh(RsMesh vsm) +{ + GET_TLS(); + Mesh *sm = static_cast<Mesh *>(vsm); if (!rsc->setupCheck()) { return; } sm->render(rsc); } -static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len) +static void SC_drawMeshPrimitive(RsMesh vsm, uint32_t primIndex) { GET_TLS(); - SimpleMesh *sm = static_cast<SimpleMesh *>(vsm); + Mesh *sm = static_cast<Mesh *>(vsm); if (!rsc->setupCheck()) { return; } - sm->renderRange(rsc, start, len); + sm->renderPrimitive(rsc, primIndex); +} + +static void SC_drawMeshPrimitiveRange(RsMesh vsm, uint32_t primIndex, uint32_t start, uint32_t len) +{ + GET_TLS(); + Mesh *sm = static_cast<Mesh *>(vsm); + if (!rsc->setupCheck()) { + return; + } + sm->renderPrimitiveRange(rsc, primIndex, start, len); } @@ -375,6 +405,10 @@ static ScriptCState::SymbolTable_t gSyms[] = { { "_Z17rsgDrawSimpleMesh7rs_mesh", (void *)&SC_drawSimpleMesh }, { "_Z17rsgDrawSimpleMesh7rs_meshii", (void *)&SC_drawSimpleMeshRange }, + { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh }, + { "_Z11rsgDrawMesh7rs_meshi", (void *)&SC_drawMeshPrimitive }, + { "_Z11rsgDrawMesh7rs_meshiii", (void *)&SC_drawMeshPrimitiveRange }, + { "rsgClearColor", (void *)&SC_ClearColor }, { "rsgClearDepth", (void *)&SC_ClearDepth }, diff --git a/libs/rs/rsSimpleMesh.cpp b/libs/rs/rsSimpleMesh.cpp deleted file mode 100644 index e5c2eb5..0000000 --- a/libs/rs/rsSimpleMesh.cpp +++ /dev/null @@ -1,259 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ANDROID_RS_BUILD_FOR_HOST -#include "rsContext.h" - -#include <GLES/gl.h> -#include <GLES2/gl2.h> -#include <GLES/glext.h> -#else -#include "rsContextHostStub.h" - -#include <OpenGL/gl.h> -#include <OpenGl/glext.h> -#endif - -using namespace android; -using namespace android::renderscript; - - - - -SimpleMesh::SimpleMesh(Context *rsc) : ObjectBase(rsc) -{ - mAllocFile = __FILE__; - mAllocLine = __LINE__; -} - -SimpleMesh::~SimpleMesh() -{ - delete[] mVertexTypes; - delete[] mVertexBuffers; -} - -void SimpleMesh::render(Context *rsc) const -{ - if (mPrimitiveType.get()) { - renderRange(rsc, 0, mPrimitiveType->getDimX()); - return; - } - - if (mIndexType.get()) { - renderRange(rsc, 0, mIndexType->getDimX()); - return; - } - - renderRange(rsc, 0, mVertexTypes[0]->getDimX()); -} - -void SimpleMesh::renderRange(Context *rsc, uint32_t start, uint32_t len) const -{ - if (len < 1) { - return; - } - - rsc->checkError("SimpleMesh::renderRange 1"); - VertexArray va; - for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { - mVertexBuffers[ct]->uploadCheck(rsc); - if (mVertexBuffers[ct]->getIsBufferObject()) { - va.setActiveBuffer(mVertexBuffers[ct]->getBufferObjectID()); - } else { - va.setActiveBuffer(mVertexBuffers[ct]->getPtr()); - } - mVertexTypes[ct]->enableGLVertexBuffer(&va); - } - va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); - - rsc->checkError("SimpleMesh::renderRange 2"); - if (mIndexType.get()) { - mIndexBuffer->uploadCheck(rsc); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID()); - glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2)); - } else { - glDrawArrays(mGLPrimitive, start, len); - } - - rsc->checkError("SimpleMesh::renderRange"); -} - -void SimpleMesh::uploadAll(Context *rsc) -{ - for (uint32_t ct=0; ct < mVertexTypeCount; ct++) { - if (mVertexBuffers[ct].get()) { - mVertexBuffers[ct]->deferedUploadToBufferObject(rsc); - } - } - if (mIndexBuffer.get()) { - mIndexBuffer->deferedUploadToBufferObject(rsc); - } - if (mPrimitiveBuffer.get()) { - mPrimitiveBuffer->deferedUploadToBufferObject(rsc); - } - rsc->checkError("SimpleMesh::uploadAll"); -} - -void SimpleMesh::updateGLPrimitive() -{ - switch(mPrimitive) { - case RS_PRIMITIVE_POINT: mGLPrimitive = GL_POINTS; break; - case RS_PRIMITIVE_LINE: mGLPrimitive = GL_LINES; break; - case RS_PRIMITIVE_LINE_STRIP: mGLPrimitive = GL_LINE_STRIP; break; - case RS_PRIMITIVE_TRIANGLE: mGLPrimitive = GL_TRIANGLES; break; - case RS_PRIMITIVE_TRIANGLE_STRIP: mGLPrimitive = GL_TRIANGLE_STRIP; break; - case RS_PRIMITIVE_TRIANGLE_FAN: mGLPrimitive = GL_TRIANGLE_FAN; break; - } -} - -void SimpleMesh::serialize(OStream *stream) const -{ - // Need to identify ourselves - stream->addU32((uint32_t)getClassId()); - - String8 name(getName()); - stream->addString(&name); - - // Add primitive type - stream->addU8((uint8_t)mPrimitive); - - // And now serialize the allocations - mIndexBuffer->serialize(stream); - - // We need to indicate if the primitive buffer is present - if(mPrimitiveBuffer.get() != NULL) { - // Write if the primitive buffer is present - stream->addU32(1); - mPrimitiveBuffer->serialize(stream); - } - else { - // No buffer present, will need this when we read - stream->addU32(0); - } - - // Store number of vertex streams - stream->addU32(mVertexTypeCount); - for(uint32_t vCount = 0; vCount < mVertexTypeCount; vCount ++) { - mVertexBuffers[vCount]->serialize(stream); - } -} - -SimpleMesh *SimpleMesh::createFromStream(Context *rsc, IStream *stream) -{ - // First make sure we are reading the correct object - RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); - if(classID != RS_A3D_CLASS_ID_SIMPLE_MESH) { - LOGE("simple mesh loading skipped due to invalid class id"); - return NULL; - } - - SimpleMesh * mesh = new SimpleMesh(rsc); - - String8 name; - stream->loadString(&name); - mesh->setName(name.string(), name.size()); - - mesh->mPrimitive = (RsPrimitive)stream->loadU8(); - mesh->updateGLPrimitive(); - - Allocation *indexAlloc = Allocation::createFromStream(rsc, stream); - const Type *indexType = indexAlloc->getType(); - mesh->mIndexBuffer.set(indexAlloc); - mesh->mIndexType.set(indexType); - - bool isPrimitivePresent = stream->loadU32() != 0; - if(isPrimitivePresent) { - mesh->mPrimitiveBuffer.set(Allocation::createFromStream(rsc, stream)); - mesh->mPrimitiveType.set(mesh->mPrimitiveBuffer->getType()); - } - - mesh->mVertexTypeCount = stream->loadU32(); - if(mesh->mVertexTypeCount) { - mesh->mVertexTypes = new ObjectBaseRef<const Type>[mesh->mVertexTypeCount]; - mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[mesh->mVertexTypeCount]; - - for(uint32_t vCount = 0; vCount < mesh->mVertexTypeCount; vCount ++) { - Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream); - const Type *vertexType = vertexAlloc->getType(); - mesh->mVertexBuffers[vCount].set(vertexAlloc); - mesh->mVertexTypes[vCount].set(vertexType); - } - } - - mesh->uploadAll(rsc); - - return mesh; -} - - -SimpleMeshContext::SimpleMeshContext() -{ -} - -SimpleMeshContext::~SimpleMeshContext() -{ -} - - -namespace android { -namespace renderscript { - - -RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType) -{ - SimpleMesh *sm = new SimpleMesh(rsc); - sm->incUserRef(); - - sm->mIndexType.set((const Type *)idx); - sm->mPrimitiveType.set((const Type *)prim); - - sm->mVertexTypeCount = vtxCount; - sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount]; - sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount]; - for (uint32_t ct=0; ct < vtxCount; ct++) { - sm->mVertexTypes[ct].set((const Type *)vtx[ct]); - } - - sm->mPrimitive = (RsPrimitive)primType; - sm->updateGLPrimitive(); - return sm; -} - -void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot) -{ - SimpleMesh *sm = static_cast<SimpleMesh *>(mv); - rsAssert(slot < sm->mVertexTypeCount); - - sm->mVertexBuffers[slot].set((Allocation *)va); -} - -void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va) -{ - SimpleMesh *sm = static_cast<SimpleMesh *>(mv); - sm->mIndexBuffer.set((Allocation *)va); -} - -void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va) -{ - SimpleMesh *sm = static_cast<SimpleMesh *>(mv); - sm->mPrimitiveBuffer.set((Allocation *)va); -} - - - - -}} - diff --git a/libs/rs/rsSimpleMesh.h b/libs/rs/rsSimpleMesh.h deleted file mode 100644 index 362c7fb..0000000 --- a/libs/rs/rsSimpleMesh.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ANDROID_RS_SIMPLE_MESH_H -#define ANDROID_RS_SIMPLE_MESH_H - - -#include "RenderScript.h" - -// --------------------------------------------------------------------------- -namespace android { -namespace renderscript { - - -// An element is a group of Components that occupies one cell in a structure. -class SimpleMesh : public ObjectBase -{ -public: - SimpleMesh(Context *); - ~SimpleMesh(); - - ObjectBaseRef<const Type> mIndexType; - ObjectBaseRef<const Type> mPrimitiveType; - ObjectBaseRef<const Type> *mVertexTypes; - uint32_t mVertexTypeCount; - - ObjectBaseRef<Allocation> mIndexBuffer; - ObjectBaseRef<Allocation> mPrimitiveBuffer; - ObjectBaseRef<Allocation> *mVertexBuffers; - - RsPrimitive mPrimitive; - uint32_t mGLPrimitive; - - - void render(Context *) const; - void renderRange(Context *, uint32_t start, uint32_t len) const; - void uploadAll(Context *); - void updateGLPrimitive(); - - virtual void serialize(OStream *stream) const; - virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_SIMPLE_MESH; } - static SimpleMesh *createFromStream(Context *rsc, IStream *stream); - -protected: -}; - -class SimpleMeshContext -{ -public: - SimpleMeshContext(); - ~SimpleMeshContext(); - - -}; - - -} -} -#endif //ANDROID_RS_SIMPLE_MESH_H - diff --git a/libs/rs/scriptc/rs_graphics.rsh b/libs/rs/scriptc/rs_graphics.rsh index fba3d6d..1f1f1ac 100644 --- a/libs/rs/scriptc/rs_graphics.rsh +++ b/libs/rs/scriptc/rs_graphics.rsh @@ -32,6 +32,10 @@ extern void rsgDrawSpriteScreenspace(float x, float y, float z, float w, float h extern void __attribute__((overloadable)) rsgDrawSimpleMesh(rs_mesh ism); extern void __attribute__((overloadable)) rsgDrawSimpleMesh(rs_mesh ism, int start, int len); +extern void __attribute__((overloadable)) rsgDrawMesh(rs_mesh ism); +extern void __attribute__((overloadable)) rsgDrawMesh(rs_mesh ism, int primitiveIndex); +extern void __attribute__((overloadable)) rsgDrawMesh(rs_mesh ism, int primitiveIndex, int start, int len); + extern void rsgClearColor(float, float, float, float); extern void rsgClearDepth(float); |