From 164aaedf7f24827c3da84acc733325ae985930d6 Mon Sep 17 00:00:00 2001 From: Alex Sakhartchouk Date: Thu, 1 Jul 2010 16:14:06 -0700 Subject: [PATCH] Start of mesh API cleanup. Switched all native code to go through Mesh class. Removed SimpleMesh Added java Mesh class Will need to port all existing code to use java Mesh, then remove java SimpleMesh. Change-Id: Idb9c03d0b06b4ef87db28dffcffa1881d39120e5 --- .../java/android/renderscript/FileA3D.java | 6 +- graphics/java/android/renderscript/Mesh.java | 447 ++++++++++++++++++ .../android/renderscript/RenderScript.java | 4 + .../jni/android_renderscript_RenderScript.cpp | 39 +- libs/rs/Android.mk | 1 - libs/rs/RenderScript.h | 3 +- libs/rs/RenderScriptEnv.h | 2 +- libs/rs/java/Fountain/res/raw/fountain.rs | 2 +- libs/rs/java/Fountain/res/raw/fountain_bc.bc | Bin 3000 -> 2996 bytes .../src/com/android/fountain/FountainRS.java | 9 +- .../android/fountain/ScriptC_Fountain.java | 6 +- .../java/ModelViewer/res/raw/modelviewer.rs | 2 +- .../ModelViewer/res/raw/modelviewer_bc.bc | Bin 6220 -> 6216 bytes libs/rs/java/ModelViewer/res/raw/robot.a3d | Bin 144508 -> 144504 bytes .../android/modelviewer/ModelViewerRS.java | 8 +- .../modelviewer/ScriptC_Modelviewer.java | 6 +- libs/rs/rs.spec | 34 +- libs/rs/rsContext.h | 1 - libs/rs/rsContextHostStub.h | 1 - libs/rs/rsFileA3D.cpp | 3 - libs/rs/rsMesh.cpp | 356 ++++++++++---- libs/rs/rsMesh.h | 49 +- libs/rs/rsScriptC_LibGL.cpp | 48 +- libs/rs/rsSimpleMesh.cpp | 259 ---------- libs/rs/rsSimpleMesh.h | 73 --- libs/rs/scriptc/rs_graphics.rsh | 4 + 26 files changed, 854 insertions(+), 509 deletions(-) create mode 100644 graphics/java/android/renderscript/Mesh.java delete mode 100644 libs/rs/rsSimpleMesh.cpp delete mode 100644 libs/rs/rsSimpleMesh.h diff --git a/graphics/java/android/renderscript/FileA3D.java b/graphics/java/android/renderscript/FileA3D.java index fb36f1faa401a..3b3711bb320f9 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 0000000000000..5a538787d6015 --- /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 b2e5b02cadec7..c7e8ca5929bce 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 c61a215950844..a6d2489c29448 100644 --- a/graphics/jni/android_renderscript_RenderScript.cpp +++ b/graphics/jni/android_renderscript_RenderScript.cpp @@ -1350,6 +1350,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) { @@ -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 7afc4f24c59ce..37c418bec7e97 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 14283183e970b..8e6b5c67e3d87 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 144e5392a1d05..92259041b73f1 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 2ecb3916cb720..c8c10cd0388f0 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 index 822f30ad9c28048353072bf4b023d31e60ea34c0..2c8ce8b9c6fb729d9822a495f0ca5d57de1b41eb 100644 GIT binary patch delta 183 zcmdlXzD1nV$@#!NMIHu*R~tF+b1@oDe#xa*uNw0|=K92x62XL-EhTRzelcz-k#P8E z;bAO!NZ3=sF!S^yjsqUC?0b12)p1i>7Xu)K;gMZE@~5O985rrZv}ro z>skaQ^4_w)UahjUVL<^l0 kHaZ6UbJXD%{CmJ)iGzvaAt#Sx3;t}D^VGh;!^!{x0O7wyC;$Ke 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 c001e842ba853..cfe8e27aeecb7 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 95ce1ab32a99d..0ec00096f525e 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 91194e8279127..559bf4856472c 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 index a64e7258b50bf5737f74e64f2cd6e423e8d40f7c..fb8502860f7204ac699d20de03b30831a3283a31 100644 GIT binary patch delta 628 zcmX?OaKeDo$@#!NMIHtQ{*9c)qKq3S*NT?ZOI)|VUaBG)%MzicC|j-amq$@{nd9H$ zSocX6T^p_nU7s?EscD8&(v$|1tsKq~M?^f2wV60eS-P!aaU!mX%zYE<-mr0W30j62M>q?6vTYY|IAYl?JoC8Z zpW?Od^^-V(N@rd%Qas}F_t`#xaJ&-Pm7|4 zn|I9QZ907k@_8T5o5^~r8MvMZwK|Xxbh^ccd)l=!pf`Xbt??CJo9wgyvl+4dkGVZD zg(lxm6o;o l#bjnniNr~u6I)DLZ38Bz delta 632 zcmX?MaK?bs$@#!NMIHtQfsLHSqKum+*NT?ZOI@+QUaBG)%Mzhm=&Pxz(4nc~#_18< zZP4QCGchH{!+6QWWEY0Rets?y?oxk>&$>_I^bstY%4e|V9E*=j)a{91j1Pz?iq}1K z4HPkZAo22eUc&;3iO#0YrX~|p3b+|v=4`f@SUKmQ{q-u9nLw4(J#~dY=|sRZ)&q5F zp8Us_=sIbm;@JsqeS(Z<#2ML~O(rwS9DC(+z(t}dY0DAQ%+rrJzCC37pL2O)3R7@P z%u;3^wXHrUHYa4}x?eia02KMrx?JV==5P55$sf*dmGx9Ja6J)fbzs7^GL^qPKz3_< zh1aJ2o)%y)0SyckOq5CZaK5x&*3(U#G0ZzAxYqp=zfQL@gYY90jwGhuiFMKn8g5Sl z&L)TgP5cTm@hH~;rzJqs?U{jL@K8KXq%~Ef3FtprnKlbXZY7Dv#4QOeJY}1J4!sU{ zXfBJ%B__0>is1Zoeg}(daAG}=%M**qy)yr6xcfq8cu4#yj)jEq#v@aZ+&)R<*#(zL z2X$ElN{<-JmVI=cG!YzB-Y&w0z_^%k&Ory{<5c1F59iI`zE|LLF;*0whA?MGCv88@;~PN#FP@jgpC}Zby_?< nj>~!4JA1TG1UlW@BlrfhsOPaZ6K5$)Hx>hZLEj^+3}65Nb{hE= diff --git a/libs/rs/java/ModelViewer/res/raw/robot.a3d b/libs/rs/java/ModelViewer/res/raw/robot.a3d index c0c66aed5e560e82a675b687a7087207fff7ae37..430fe950ffd64517785cb24927d0fd8ef7b6785b 100644 GIT binary patch delta 210 zcmezKg5$>v4qnH+l%o916l0h8v^1TGyyCuG3=9k|Afe!l#DY`?21Xzs1Pn}=z!<~^ ziD6U60+Iv*CJ4pAJo#&}JQp*R$HXvMu0#x(Ylp&Zjw{(7SHdWFzzAeMNEe6(nF+Eh aCowlEwW!1>*d57ypg0I{GHjgWR}294w;;&? delta 264 zcmezIg5%E%4qnH+l%o916l0h8v^0~6yyEqO3=9kf`8k!rrA29p$*I8^i3O>~KrtpD z1_2!tCNKuEL1I|dGcz!-0_C`XI43bTDYdA?DA=8W0VoUxEKr&gsE85Do@NPVY_~qh zXj?1_5(Sw60&rau3+26FTA6@$!z9>%it_S{auahH7~t|OjBt4lpnOScMM-HmRsObj = 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 bd9cd275999c9..4f90f36b1c400 100644 --- a/libs/rs/rsMesh.cpp +++ b/libs/rs/rsMesh.cpp @@ -35,14 +35,124 @@ 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::render(Context *rsc) const +{ + for(uint32_t ct = 0; ct < mPrimitivesCount; ct ++) { + renderPrimitive(rsc, ct); + } +} + +void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const { + if (primIndex >= mPrimitivesCount) { + LOGE("Invalid primitive index"); + return; + } + + Primitive_t *prim = mPrimitives[primIndex]; + + if (prim->mIndexBuffer.get()) { + renderPrimitiveRange(rsc, primIndex, 0, prim->mIndexBuffer->getType()->getDimX()); + return; + } + + if (prim->mPrimitiveBuffer.get()) { + renderPrimitiveRange(rsc, primIndex, 0, prim->mPrimitiveBuffer->getType()->getDimX()); + return; + } + + renderPrimitiveRange(rsc, primIndex, 0, mVertexBuffers[0]->getType()->getDimX()); +} + +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()); + } + 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"); +} + + +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 @@ -53,25 +163,10 @@ void Mesh::serialize(OStream *stream) const String8 name(getName()); stream->addString(&name); - stream->addU32(mVerticiesCount); - - for(uint32_t vCount = 0; vCount < mVerticiesCount; vCount ++) { - Verticies_t *verts = mVerticies[vCount]; - - stream->addU32(verts->mAllocationCount); - - for (uint32_t aCount = 0; aCount < verts->mAllocationCount; aCount++) { - verts->mAllocations[aCount]->serialize(stream); - } - stream->addU32(verts->mVertexDataSize); - - stream->addU32(verts->mOffsetCoord); - stream->addU32(verts->mOffsetTex); - stream->addU32(verts->mOffsetNorm); - - stream->addU32(verts->mSizeCoord); - stream->addU32(verts->mSizeTex); - stream->addU32(verts->mSizeNorm ); + // 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; - } + mesh->mVertexBufferCount = stream->loadU32(); + if(mesh->mVertexBufferCount) { + mesh->mVertexBuffers = new ObjectBaseRef[mesh->mVertexBufferCount]; - 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]; - - 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(); + prim->mPrimitive = (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; - } + // 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); + } - 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 primitive buffer was stored + uint32_t isPrimitivePresent = stream->loadU32(); + if(isPrimitivePresent) { + Allocation *primitiveAlloc = Allocation::createFromStream(rsc, stream); + prim->mPrimitiveBuffer.set(primitiveAlloc); } } - 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(); - - } - } - 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[vtxCount]; + sm->mVertexTypes = new ObjectBaseRef[vtxCount]; + + return sm; +} + +void rsi_MeshBindVertex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t slot) +{ + Mesh *sm = static_cast(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(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(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[vtxCount]; + sm->mVertexBuffers = new ObjectBaseRef[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(mv); + rsAssert(slot < sm->mVertexBufferCount); + + sm->mVertexBuffers[slot].set((Allocation *)va); +} + +void rsi_SimpleMeshBindIndex(Context *rsc, RsMesh mv, RsAllocation va) +{ + Mesh *sm = static_cast(mv); + sm->mPrimitives[0]->mIndexBuffer.set((Allocation *)va); +} + +void rsi_SimpleMeshBindPrimitive(Context *rsc, RsMesh mv, RsAllocation va) +{ + Mesh *sm = static_cast(mv); + sm->mPrimitives[0]->mPrimitiveBuffer.set((Allocation *)va); +} + + + + +}} diff --git a/libs/rs/rsMesh.h b/libs/rs/rsMesh.h index 8c7e8a4427c67..66174b7255818 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 *mVertexBuffers; + ObjectBaseRef *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; + ObjectBaseRef mIndexBuffer; + ObjectBaseRef mPrimitiveBuffer; + ObjectBaseRef mIndexType; + ObjectBaseRef mPrimitiveType; - uint32_t mIndexCount; - uint16_t *mIndicies; - - 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 dbd398e34c42b..7185009b7a34f 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(mesh); + Mesh *sm = static_cast(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(vsm); + Mesh *sm = static_cast(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(vsm); + if (!rsc->setupCheck()) { + return; + } + sm->renderPrimitiveRange(rsc, 0, start, len); +} + +static void SC_drawMesh(RsMesh vsm) +{ + GET_TLS(); + Mesh *sm = static_cast(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(vsm); + Mesh *sm = static_cast(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(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 e5c2eb5afa194..0000000000000 --- 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 -#include -#include -#else -#include "rsContextHostStub.h" - -#include -#include -#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[mesh->mVertexTypeCount]; - mesh->mVertexBuffers = new ObjectBaseRef[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[vtxCount]; - sm->mVertexBuffers = new ObjectBaseRef[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(mv); - rsAssert(slot < sm->mVertexTypeCount); - - sm->mVertexBuffers[slot].set((Allocation *)va); -} - -void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va) -{ - SimpleMesh *sm = static_cast(mv); - sm->mIndexBuffer.set((Allocation *)va); -} - -void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va) -{ - SimpleMesh *sm = static_cast(mv); - sm->mPrimitiveBuffer.set((Allocation *)va); -} - - - - -}} - diff --git a/libs/rs/rsSimpleMesh.h b/libs/rs/rsSimpleMesh.h deleted file mode 100644 index 362c7fbac2d4c..0000000000000 --- 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 mIndexType; - ObjectBaseRef mPrimitiveType; - ObjectBaseRef *mVertexTypes; - uint32_t mVertexTypeCount; - - ObjectBaseRef mIndexBuffer; - ObjectBaseRef mPrimitiveBuffer; - ObjectBaseRef *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 fba3d6d0acd00..1f1f1ac02a5ee 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);