| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | import android.util.Config; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | /** |
| 23 | * @hide |
| 24 | * |
| 25 | **/ |
| 26 | public class SimpleMesh extends BaseObj { |
| 27 | Type[] mVertexTypes; |
| 28 | Type mIndexType; |
| 29 | //Type mBatcheType; |
| 30 | Primitive mPrimitive; |
| 31 | |
| 32 | SimpleMesh(int id, RenderScript rs) { |
| 33 | super(rs); |
| 34 | mID = id; |
| 35 | } |
| 36 | |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 37 | public void bindVertexAllocation(Allocation a, int slot) { |
| 38 | mRS.nSimpleMeshBindVertex(mID, a.mID, slot); |
| 39 | } |
| 40 | |
| 41 | public void bindIndexAllocation(Allocation a) { |
| 42 | mRS.nSimpleMeshBindIndex(mID, a.mID); |
| 43 | } |
| 44 | |
| 45 | public Allocation createVertexAllocation(int slot) { |
| 46 | return Allocation.createTyped(mRS, mVertexTypes[slot]); |
| 47 | } |
| 48 | |
| 49 | public Allocation createIndexAllocation() { |
| 50 | return Allocation.createTyped(mRS, mIndexType); |
| 51 | } |
| 52 | |
| Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 53 | public Type getVertexType(int slot) { |
| 54 | return mVertexTypes[slot]; |
| 55 | } |
| 56 | |
| 57 | public Type getIndexType() { |
| 58 | return mIndexType; |
| 59 | } |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 60 | |
| 61 | public static class Builder { |
| 62 | RenderScript mRS; |
| 63 | |
| 64 | class Entry { |
| 65 | Type t; |
| 66 | Element e; |
| 67 | int size; |
| 68 | } |
| 69 | |
| 70 | int mVertexTypeCount; |
| 71 | Entry[] mVertexTypes; |
| 72 | Entry mIndexType; |
| 73 | //Entry mBatchType; |
| 74 | Primitive mPrimitive; |
| 75 | |
| 76 | |
| 77 | public Builder(RenderScript rs) { |
| 78 | mRS = rs; |
| 79 | mVertexTypeCount = 0; |
| 80 | mVertexTypes = new Entry[16]; |
| 81 | mIndexType = new Entry(); |
| 82 | } |
| 83 | |
| 84 | public int addVertexType(Type t) throws IllegalStateException { |
| 85 | if(mVertexTypeCount >= mVertexTypes.length) { |
| 86 | throw new IllegalStateException("Max vertex types exceeded."); |
| 87 | } |
| 88 | |
| 89 | int addedIndex = mVertexTypeCount; |
| 90 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 91 | mVertexTypes[mVertexTypeCount].t = t; |
| 92 | mVertexTypeCount++; |
| 93 | return addedIndex; |
| 94 | } |
| 95 | |
| 96 | public int addVertexType(Element e, int size) throws IllegalStateException { |
| 97 | if(mVertexTypeCount >= mVertexTypes.length) { |
| 98 | throw new IllegalStateException("Max vertex types exceeded."); |
| 99 | } |
| 100 | |
| 101 | int addedIndex = mVertexTypeCount; |
| 102 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 103 | mVertexTypes[mVertexTypeCount].e = e; |
| 104 | mVertexTypes[mVertexTypeCount].size = size; |
| 105 | mVertexTypeCount++; |
| 106 | return addedIndex; |
| 107 | } |
| 108 | |
| 109 | public void setIndexType(Type t) { |
| 110 | mIndexType.t = t; |
| 111 | mIndexType.e = null; |
| 112 | mIndexType.size = 0; |
| 113 | } |
| 114 | |
| 115 | public void setIndexType(Element e, int size) { |
| 116 | mIndexType.t = null; |
| 117 | mIndexType.e = e; |
| 118 | mIndexType.size = size; |
| 119 | } |
| 120 | |
| 121 | public void setPrimitive(Primitive p) { |
| 122 | mPrimitive = p; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | Type newType(Element e, int size) { |
| 127 | Type.Builder tb = new Type.Builder(mRS, e); |
| 128 | tb.add(Dimension.X, size); |
| 129 | return tb.create(); |
| 130 | } |
| 131 | |
| 132 | static synchronized SimpleMesh internalCreate(RenderScript rs, Builder b) { |
| 133 | Type[] toDestroy = new Type[18]; |
| 134 | int toDestroyCount = 0; |
| 135 | |
| 136 | int indexID = 0; |
| 137 | if(b.mIndexType.t != null) { |
| 138 | indexID = b.mIndexType.t.mID; |
| 139 | } else if(b.mIndexType.size != 0) { |
| 140 | b.mIndexType.t = b.newType(b.mIndexType.e, b.mIndexType.size); |
| 141 | indexID = b.mIndexType.t.mID; |
| 142 | toDestroy[toDestroyCount++] = b.mIndexType.t; |
| 143 | } |
| 144 | |
| 145 | int[] IDs = new int[b.mVertexTypeCount]; |
| 146 | for(int ct=0; ct < b.mVertexTypeCount; ct++) { |
| 147 | if(b.mVertexTypes[ct].t != null) { |
| 148 | IDs[ct] = b.mVertexTypes[ct].t.mID; |
| 149 | } else { |
| 150 | b.mVertexTypes[ct].t = b.newType(b.mVertexTypes[ct].e, b.mVertexTypes[ct].size); |
| 151 | IDs[ct] = b.mVertexTypes[ct].t.mID; |
| 152 | toDestroy[toDestroyCount++] = b.mVertexTypes[ct].t; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | int id = rs.nSimpleMeshCreate(0, indexID, IDs, b.mPrimitive.mID); |
| 157 | for(int ct=0; ct < toDestroyCount; ct++) { |
| 158 | toDestroy[ct].destroy(); |
| 159 | } |
| 160 | |
| 161 | return new SimpleMesh(id, rs); |
| 162 | } |
| 163 | |
| 164 | public SimpleMesh create() { |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 165 | SimpleMesh sm = internalCreate(mRS, this); |
| 166 | sm.mVertexTypes = new Type[mVertexTypeCount]; |
| 167 | for(int ct=0; ct < mVertexTypeCount; ct++) { |
| 168 | sm.mVertexTypes[ct] = mVertexTypes[ct].t; |
| 169 | } |
| 170 | sm.mIndexType = mIndexType.t; |
| 171 | sm.mPrimitive = mPrimitive; |
| 172 | return sm; |
| 173 | } |
| 174 | } |
| 175 | |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 176 | public static class TriangleMeshBuilder { |
| 177 | float mVtxData[]; |
| 178 | int mVtxCount; |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 179 | short mIndexData[]; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 180 | int mIndexCount; |
| 181 | RenderScript mRS; |
| 182 | Element mElement; |
| 183 | |
| 184 | int mVtxSize; |
| 185 | boolean mNorm; |
| 186 | boolean mTex; |
| 187 | |
| 188 | public TriangleMeshBuilder(RenderScript rs, int vtxSize, boolean norm, boolean tex) { |
| 189 | mRS = rs; |
| 190 | mVtxCount = 0; |
| 191 | mIndexCount = 0; |
| 192 | mVtxData = new float[128]; |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 193 | mIndexData = new short[128]; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 194 | mVtxSize = vtxSize; |
| 195 | mNorm = norm; |
| 196 | mTex = tex; |
| 197 | |
| 198 | if(vtxSize < 2 || vtxSize > 3) { |
| 199 | throw new IllegalArgumentException("Vertex size out of range."); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | private void makeSpace(int count) { |
| 204 | if((mVtxCount + count) >= mVtxData.length) { |
| 205 | float t[] = new float[mVtxData.length * 2]; |
| 206 | System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); |
| 207 | mVtxData = t; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | public void add_XY(float x, float y) { |
| 212 | if((mVtxSize != 2) || mNorm || mTex) { |
| 213 | throw new IllegalStateException("add mistmatch with declaired components."); |
| 214 | } |
| 215 | makeSpace(2); |
| 216 | mVtxData[mVtxCount++] = x; |
| 217 | mVtxData[mVtxCount++] = y; |
| 218 | } |
| 219 | |
| 220 | public void add_XYZ(float x, float y, float z) { |
| 221 | if((mVtxSize != 3) || mNorm || mTex) { |
| 222 | throw new IllegalStateException("add mistmatch with declaired components."); |
| 223 | } |
| 224 | makeSpace(3); |
| 225 | mVtxData[mVtxCount++] = x; |
| 226 | mVtxData[mVtxCount++] = y; |
| 227 | mVtxData[mVtxCount++] = z; |
| 228 | } |
| 229 | |
| 230 | public void add_XY_ST(float x, float y, float s, float t) { |
| 231 | if((mVtxSize != 2) || mNorm || !mTex) { |
| 232 | throw new IllegalStateException("add mistmatch with declaired components."); |
| 233 | } |
| 234 | makeSpace(4); |
| 235 | mVtxData[mVtxCount++] = x; |
| 236 | mVtxData[mVtxCount++] = y; |
| 237 | mVtxData[mVtxCount++] = s; |
| 238 | mVtxData[mVtxCount++] = t; |
| 239 | } |
| 240 | |
| 241 | public void add_XYZ_ST(float x, float y, float z, float s, float t) { |
| 242 | if((mVtxSize != 3) || mNorm || !mTex) { |
| 243 | throw new IllegalStateException("add mistmatch with declaired components."); |
| 244 | } |
| 245 | makeSpace(5); |
| 246 | mVtxData[mVtxCount++] = x; |
| 247 | mVtxData[mVtxCount++] = y; |
| 248 | mVtxData[mVtxCount++] = z; |
| 249 | mVtxData[mVtxCount++] = s; |
| 250 | mVtxData[mVtxCount++] = t; |
| 251 | } |
| 252 | |
| 253 | public void add_XYZ_ST_NORM(float x, float y, float z, float s, float t, float nx, float ny, float nz) { |
| 254 | if((mVtxSize != 3) || !mNorm || !mTex) { |
| 255 | throw new IllegalStateException("add mistmatch with declaired components."); |
| 256 | } |
| 257 | makeSpace(8); |
| 258 | mVtxData[mVtxCount++] = x; |
| 259 | mVtxData[mVtxCount++] = y; |
| 260 | mVtxData[mVtxCount++] = z; |
| 261 | mVtxData[mVtxCount++] = s; |
| 262 | mVtxData[mVtxCount++] = t; |
| 263 | mVtxData[mVtxCount++] = nx; |
| 264 | mVtxData[mVtxCount++] = ny; |
| 265 | mVtxData[mVtxCount++] = nz; |
| 266 | } |
| 267 | |
| 268 | public void addTriangle(int idx1, int idx2, int idx3) { |
| 269 | if((mIndexCount + 3) >= mIndexData.length) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 270 | short t[] = new short[mIndexData.length * 2]; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 271 | System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); |
| 272 | mIndexData = t; |
| 273 | } |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 274 | mIndexData[mIndexCount++] = (short)idx1; |
| 275 | mIndexData[mIndexCount++] = (short)idx2; |
| 276 | mIndexData[mIndexCount++] = (short)idx3; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | public SimpleMesh create() { |
| 280 | Element.Builder b = new Element.Builder(mRS); |
| 281 | int floatCount = mVtxSize; |
| 282 | if(mVtxSize == 2) { |
| 283 | b.addFloatXY(); |
| 284 | } else { |
| 285 | b.addFloatXYZ(); |
| 286 | } |
| 287 | if(mTex) { |
| 288 | floatCount += 2; |
| 289 | b.addFloatST(); |
| 290 | } |
| 291 | if(mNorm) { |
| 292 | floatCount += 3; |
| 293 | b.addFloatNorm(); |
| 294 | } |
| 295 | mElement = b.create(); |
| 296 | |
| 297 | Builder smb = new Builder(mRS); |
| 298 | smb.addVertexType(mElement, mVtxCount / floatCount); |
| Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 299 | smb.setIndexType(Element.INDEX_16(mRS), mIndexCount); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 300 | smb.setPrimitive(Primitive.TRIANGLE); |
| 301 | SimpleMesh sm = smb.create(); |
| 302 | |
| 303 | Allocation vertexAlloc = sm.createVertexAllocation(0); |
| 304 | Allocation indexAlloc = sm.createIndexAllocation(); |
| 305 | sm.bindVertexAllocation(vertexAlloc, 0); |
| 306 | sm.bindIndexAllocation(indexAlloc); |
| 307 | |
| 308 | vertexAlloc.data(mVtxData); |
| 309 | vertexAlloc.uploadToBufferObject(); |
| 310 | |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 311 | indexAlloc.data(mIndexData); |
| 312 | indexAlloc.uploadToBufferObject(); |
| 313 | |
| 314 | return sm; |
| 315 | } |
| 316 | } |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 317 | } |
| 318 | |