Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -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 java.util.Vector; |
| 20 | |
| 21 | import android.util.Config; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | /** |
| 25 | * @hide |
| 26 | * |
| 27 | **/ |
| 28 | public class Mesh extends BaseObj { |
| 29 | |
| 30 | Allocation[] mVertexBuffers; |
| 31 | Allocation[] mIndexBuffers; |
| 32 | Primitive[] mPrimitives; |
| 33 | |
| 34 | Mesh(int id, RenderScript rs) { |
| 35 | super(rs); |
| 36 | mID = id; |
| 37 | } |
| 38 | |
| 39 | public int getVertexAllocationCount() { |
| 40 | if(mVertexBuffers == null) { |
| 41 | return 0; |
| 42 | } |
| 43 | return mVertexBuffers.length; |
| 44 | } |
| 45 | public Allocation getVertexAllocation(int slot) { |
| 46 | return mVertexBuffers[slot]; |
| 47 | } |
| 48 | |
| 49 | public int getPrimitiveCount() { |
| 50 | if(mIndexBuffers == null) { |
| 51 | return 0; |
| 52 | } |
| 53 | return mIndexBuffers.length; |
| 54 | } |
| 55 | public Allocation getIndexAllocation(int slot) { |
| 56 | return mIndexBuffers[slot]; |
| 57 | } |
| 58 | public Primitive getPrimitive(int slot) { |
| 59 | return mPrimitives[slot]; |
| 60 | } |
| 61 | |
| 62 | public static class Builder { |
| 63 | RenderScript mRS; |
| 64 | |
| 65 | class Entry { |
| 66 | Type t; |
| 67 | Element e; |
| 68 | int size; |
| 69 | Primitive prim; |
| 70 | } |
| 71 | |
| 72 | int mVertexTypeCount; |
| 73 | Entry[] mVertexTypes; |
| 74 | Vector mIndexTypes; |
| 75 | |
| 76 | public Builder(RenderScript rs) { |
| 77 | mRS = rs; |
| 78 | mVertexTypeCount = 0; |
| 79 | mVertexTypes = new Entry[16]; |
| 80 | mIndexTypes = new Vector(); |
| 81 | } |
| 82 | |
| 83 | public int addVertexType(Type t) throws IllegalStateException { |
| 84 | if (mVertexTypeCount >= mVertexTypes.length) { |
| 85 | throw new IllegalStateException("Max vertex types exceeded."); |
| 86 | } |
| 87 | |
| 88 | int addedIndex = mVertexTypeCount; |
| 89 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 90 | mVertexTypes[mVertexTypeCount].t = t; |
| 91 | mVertexTypes[mVertexTypeCount].e = null; |
| 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].t = null; |
| 104 | mVertexTypes[mVertexTypeCount].e = e; |
| 105 | mVertexTypes[mVertexTypeCount].size = size; |
| 106 | mVertexTypeCount++; |
| 107 | return addedIndex; |
| 108 | } |
| 109 | |
| 110 | public int addIndexType(Type t, Primitive p) { |
| 111 | int addedIndex = mIndexTypes.size(); |
| 112 | Entry indexType = new Entry(); |
| 113 | indexType.t = t; |
| 114 | indexType.e = null; |
| 115 | indexType.size = 0; |
| 116 | indexType.prim = p; |
| 117 | mIndexTypes.addElement(indexType); |
| 118 | return addedIndex; |
| 119 | } |
| 120 | |
| 121 | public int addIndexType(Primitive p) { |
| 122 | int addedIndex = mIndexTypes.size(); |
| 123 | Entry indexType = new Entry(); |
| 124 | indexType.t = null; |
| 125 | indexType.e = null; |
| 126 | indexType.size = 0; |
| 127 | indexType.prim = p; |
| 128 | mIndexTypes.addElement(indexType); |
| 129 | return addedIndex; |
| 130 | } |
| 131 | |
| 132 | public int addIndexType(Element e, int size, Primitive p) { |
| 133 | int addedIndex = mIndexTypes.size(); |
| 134 | Entry indexType = new Entry(); |
| 135 | indexType.t = null; |
| 136 | indexType.e = e; |
| 137 | indexType.size = size; |
| 138 | indexType.prim = p; |
| 139 | mIndexTypes.addElement(indexType); |
| 140 | return addedIndex; |
| 141 | } |
| 142 | |
| 143 | Type newType(Element e, int size) { |
| 144 | Type.Builder tb = new Type.Builder(mRS, e); |
| 145 | tb.add(Dimension.X, size); |
| 146 | return tb.create(); |
| 147 | } |
| 148 | |
| 149 | static synchronized Mesh internalCreate(RenderScript rs, Builder b) { |
| 150 | |
| 151 | int id = rs.nMeshCreate(b.mVertexTypeCount, b.mIndexTypes.size()); |
| 152 | Mesh newMesh = new Mesh(id, rs); |
| 153 | newMesh.mIndexBuffers = new Allocation[b.mIndexTypes.size()]; |
| 154 | newMesh.mPrimitives = new Primitive[b.mIndexTypes.size()]; |
| 155 | newMesh.mVertexBuffers = new Allocation[b.mVertexTypeCount]; |
| 156 | |
| 157 | for(int ct = 0; ct < b.mIndexTypes.size(); ct ++) { |
| 158 | Allocation alloc = null; |
| 159 | Entry entry = (Entry)b.mIndexTypes.elementAt(ct); |
| 160 | if (entry.t != null) { |
| 161 | alloc = Allocation.createTyped(rs, entry.t); |
| 162 | } |
| 163 | else if(entry.e != null) { |
| 164 | alloc = Allocation.createSized(rs, entry.e, entry.size); |
| 165 | } |
| 166 | int allocID = (alloc == null) ? 0 : alloc.getID(); |
| 167 | rs.nMeshBindIndex(id, allocID, entry.prim.mID, ct); |
| 168 | newMesh.mIndexBuffers[ct] = alloc; |
| 169 | newMesh.mPrimitives[ct] = entry.prim; |
| 170 | } |
| 171 | |
| 172 | for(int ct = 0; ct < b.mVertexTypeCount; ct ++) { |
| 173 | Allocation alloc = null; |
| 174 | Entry entry = b.mVertexTypes[ct]; |
| 175 | if (entry.t != null) { |
| 176 | alloc = Allocation.createTyped(rs, entry.t); |
| 177 | } else if(entry.e != null) { |
| 178 | alloc = Allocation.createSized(rs, entry.e, entry.size); |
| 179 | } |
| 180 | rs.nMeshBindVertex(id, alloc.getID(), ct); |
| 181 | newMesh.mVertexBuffers[ct] = alloc; |
| 182 | } |
| 183 | |
| 184 | return newMesh; |
| 185 | } |
| 186 | |
| 187 | public Mesh create() { |
| 188 | mRS.validate(); |
| 189 | Mesh sm = internalCreate(mRS, this); |
| 190 | return sm; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | public static class AllocationBuilder { |
| 195 | RenderScript mRS; |
| 196 | |
| 197 | class Entry { |
| 198 | Allocation a; |
| 199 | Primitive prim; |
| 200 | } |
| 201 | |
| 202 | int mVertexTypeCount; |
| 203 | Entry[] mVertexTypes; |
| 204 | |
| 205 | Vector mIndexTypes; |
| 206 | |
| 207 | public AllocationBuilder(RenderScript rs) { |
| 208 | mRS = rs; |
| 209 | mVertexTypeCount = 0; |
| 210 | mVertexTypes = new Entry[16]; |
| 211 | mIndexTypes = new Vector(); |
| 212 | } |
| 213 | |
| 214 | public int addVertexAllocation(Allocation a) throws IllegalStateException { |
| 215 | if (mVertexTypeCount >= mVertexTypes.length) { |
| 216 | throw new IllegalStateException("Max vertex types exceeded."); |
| 217 | } |
| 218 | |
| 219 | int addedIndex = mVertexTypeCount; |
| 220 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 221 | mVertexTypes[mVertexTypeCount].a = a; |
| 222 | mVertexTypeCount++; |
| 223 | return addedIndex; |
| 224 | } |
| 225 | |
| 226 | public int addIndexAllocation(Allocation a, Primitive p) { |
| 227 | int addedIndex = mIndexTypes.size(); |
| 228 | Entry indexType = new Entry(); |
| 229 | indexType.a = a; |
| 230 | indexType.prim = p; |
| 231 | mIndexTypes.addElement(indexType); |
| 232 | return addedIndex; |
| 233 | } |
| 234 | |
| 235 | public int addIndexType(Primitive p) { |
| 236 | int addedIndex = mIndexTypes.size(); |
| 237 | Entry indexType = new Entry(); |
| 238 | indexType.a = null; |
| 239 | indexType.prim = p; |
| 240 | mIndexTypes.addElement(indexType); |
| 241 | return addedIndex; |
| 242 | } |
| 243 | |
| 244 | static synchronized Mesh internalCreate(RenderScript rs, AllocationBuilder b) { |
| 245 | |
| 246 | int id = rs.nMeshCreate(b.mVertexTypeCount, b.mIndexTypes.size()); |
| 247 | Mesh newMesh = new Mesh(id, rs); |
| 248 | newMesh.mIndexBuffers = new Allocation[b.mIndexTypes.size()]; |
| 249 | newMesh.mPrimitives = new Primitive[b.mIndexTypes.size()]; |
| 250 | newMesh.mVertexBuffers = new Allocation[b.mVertexTypeCount]; |
| 251 | |
| 252 | for(int ct = 0; ct < b.mIndexTypes.size(); ct ++) { |
| 253 | Entry entry = (Entry)b.mIndexTypes.elementAt(ct); |
| 254 | int allocID = (entry.a == null) ? 0 : entry.a.getID(); |
| 255 | rs.nMeshBindIndex(id, allocID, entry.prim.mID, ct); |
| 256 | newMesh.mIndexBuffers[ct] = entry.a; |
| 257 | newMesh.mPrimitives[ct] = entry.prim; |
| 258 | } |
| 259 | |
| 260 | for(int ct = 0; ct < b.mVertexTypeCount; ct ++) { |
| 261 | Entry entry = b.mVertexTypes[ct]; |
| 262 | rs.nMeshBindVertex(id, entry.a.mID, ct); |
| 263 | newMesh.mVertexBuffers[ct] = entry.a; |
| 264 | } |
| 265 | |
| 266 | return newMesh; |
| 267 | } |
| 268 | |
| 269 | public Mesh create() { |
| 270 | mRS.validate(); |
| 271 | Mesh sm = internalCreate(mRS, this); |
| 272 | return sm; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | |
| 277 | public static class TriangleMeshBuilder { |
| 278 | float mVtxData[]; |
| 279 | int mVtxCount; |
| 280 | short mIndexData[]; |
| 281 | int mIndexCount; |
| 282 | RenderScript mRS; |
| 283 | Element mElement; |
| 284 | |
| 285 | float mNX = 0; |
| 286 | float mNY = 0; |
| 287 | float mNZ = -1; |
| 288 | float mS0 = 0; |
| 289 | float mT0 = 0; |
| 290 | float mR = 1; |
| 291 | float mG = 1; |
| 292 | float mB = 1; |
| 293 | float mA = 1; |
| 294 | |
| 295 | int mVtxSize; |
| 296 | int mFlags; |
| 297 | |
| 298 | public static final int COLOR = 0x0001; |
| 299 | public static final int NORMAL = 0x0002; |
| 300 | public static final int TEXTURE_0 = 0x0100; |
| 301 | |
| 302 | public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) { |
| 303 | mRS = rs; |
| 304 | mVtxCount = 0; |
| 305 | mIndexCount = 0; |
| 306 | mVtxData = new float[128]; |
| 307 | mIndexData = new short[128]; |
| 308 | mVtxSize = vtxSize; |
| 309 | mFlags = flags; |
| 310 | |
| 311 | if (vtxSize < 2 || vtxSize > 3) { |
| 312 | throw new IllegalArgumentException("Vertex size out of range."); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | private void makeSpace(int count) { |
| 317 | if ((mVtxCount + count) >= mVtxData.length) { |
| 318 | float t[] = new float[mVtxData.length * 2]; |
| 319 | System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); |
| 320 | mVtxData = t; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | private void latch() { |
| 325 | if ((mFlags & COLOR) != 0) { |
| 326 | makeSpace(4); |
| 327 | mVtxData[mVtxCount++] = mR; |
| 328 | mVtxData[mVtxCount++] = mG; |
| 329 | mVtxData[mVtxCount++] = mB; |
| 330 | mVtxData[mVtxCount++] = mA; |
| 331 | } |
| 332 | if ((mFlags & TEXTURE_0) != 0) { |
| 333 | makeSpace(2); |
| 334 | mVtxData[mVtxCount++] = mS0; |
| 335 | mVtxData[mVtxCount++] = mT0; |
| 336 | } |
| 337 | if ((mFlags & NORMAL) != 0) { |
| 338 | makeSpace(3); |
| 339 | mVtxData[mVtxCount++] = mNX; |
| 340 | mVtxData[mVtxCount++] = mNY; |
| 341 | mVtxData[mVtxCount++] = mNZ; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | public void addVertex(float x, float y) { |
| 346 | if (mVtxSize != 2) { |
| 347 | throw new IllegalStateException("add mistmatch with declared components."); |
| 348 | } |
| 349 | makeSpace(2); |
| 350 | mVtxData[mVtxCount++] = x; |
| 351 | mVtxData[mVtxCount++] = y; |
| 352 | latch(); |
| 353 | } |
| 354 | |
| 355 | public void addVertex(float x, float y, float z) { |
| 356 | if (mVtxSize != 3) { |
| 357 | throw new IllegalStateException("add mistmatch with declared components."); |
| 358 | } |
| 359 | makeSpace(3); |
| 360 | mVtxData[mVtxCount++] = x; |
| 361 | mVtxData[mVtxCount++] = y; |
| 362 | mVtxData[mVtxCount++] = z; |
| 363 | latch(); |
| 364 | } |
| 365 | |
| 366 | public void setTexture(float s, float t) { |
| 367 | if ((mFlags & TEXTURE_0) == 0) { |
| 368 | throw new IllegalStateException("add mistmatch with declared components."); |
| 369 | } |
| 370 | mS0 = s; |
| 371 | mT0 = t; |
| 372 | } |
| 373 | |
| 374 | public void setNormal(float x, float y, float z) { |
| 375 | if ((mFlags & NORMAL) == 0) { |
| 376 | throw new IllegalStateException("add mistmatch with declared components."); |
| 377 | } |
| 378 | mNX = x; |
| 379 | mNY = y; |
| 380 | mNZ = z; |
| 381 | } |
| 382 | |
| 383 | public void setColor(float r, float g, float b, float a) { |
| 384 | if ((mFlags & COLOR) == 0) { |
| 385 | throw new IllegalStateException("add mistmatch with declared components."); |
| 386 | } |
| 387 | mR = r; |
| 388 | mG = g; |
| 389 | mB = b; |
| 390 | mA = a; |
| 391 | } |
| 392 | |
| 393 | public void addTriangle(int idx1, int idx2, int idx3) { |
| 394 | if((idx1 >= mVtxCount) || (idx1 < 0) || |
| 395 | (idx2 >= mVtxCount) || (idx2 < 0) || |
| 396 | (idx3 >= mVtxCount) || (idx3 < 0)) { |
| 397 | throw new IllegalStateException("Index provided greater than vertex count."); |
| 398 | } |
| 399 | if ((mIndexCount + 3) >= mIndexData.length) { |
| 400 | short t[] = new short[mIndexData.length * 2]; |
| 401 | System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); |
| 402 | mIndexData = t; |
| 403 | } |
| 404 | mIndexData[mIndexCount++] = (short)idx1; |
| 405 | mIndexData[mIndexCount++] = (short)idx2; |
| 406 | mIndexData[mIndexCount++] = (short)idx3; |
| 407 | } |
| 408 | |
| 409 | public Mesh create(boolean uploadToBufferObject) { |
| 410 | Element.Builder b = new Element.Builder(mRS); |
| 411 | int floatCount = mVtxSize; |
| 412 | b.add(Element.createVector(mRS, |
| 413 | Element.DataType.FLOAT_32, |
| 414 | mVtxSize), "position"); |
| 415 | if ((mFlags & COLOR) != 0) { |
| 416 | floatCount += 4; |
| 417 | b.add(Element.F32_4(mRS), "color"); |
| 418 | } |
| 419 | if ((mFlags & TEXTURE_0) != 0) { |
| 420 | floatCount += 2; |
| 421 | b.add(Element.F32_2(mRS), "texture0"); |
| 422 | } |
| 423 | if ((mFlags & NORMAL) != 0) { |
| 424 | floatCount += 3; |
| 425 | b.add(Element.F32_3(mRS), "normal"); |
| 426 | } |
| 427 | mElement = b.create(); |
| 428 | |
| 429 | Builder smb = new Builder(mRS); |
| 430 | smb.addVertexType(mElement, mVtxCount / floatCount); |
| 431 | smb.addIndexType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE); |
| 432 | |
| 433 | Mesh sm = smb.create(); |
| 434 | |
| 435 | sm.getVertexAllocation(0).data(mVtxData); |
| 436 | if(uploadToBufferObject) { |
| 437 | sm.getVertexAllocation(0).uploadToBufferObject(); |
| 438 | } |
| 439 | |
| 440 | sm.getIndexAllocation(0).data(mIndexData); |
| 441 | sm.getIndexAllocation(0).uploadToBufferObject(); |
| 442 | |
| 443 | return sm; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |