| 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 { |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 85 | if (mVertexTypeCount >= mVertexTypes.length) { |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 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 { |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 97 | if (mVertexTypeCount >= mVertexTypes.length) { |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 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; |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 137 | if (b.mIndexType.t != null) { |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 138 | indexID = b.mIndexType.t.mID; |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 139 | } else if (b.mIndexType.size != 0) { |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 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++) { |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 147 | if (b.mVertexTypes[ct].t != null) { |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 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 | |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 184 | float mNX = 0; |
| 185 | float mNY = 0; |
| 186 | float mNZ = -1; |
| 187 | float mS0 = 0; |
| 188 | float mT0 = 0; |
| 189 | float mR = 1; |
| 190 | float mG = 1; |
| 191 | float mB = 1; |
| 192 | float mA = 1; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 193 | |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 194 | int mVtxSize; |
| 195 | int mFlags; |
| 196 | |
| 197 | public static final int COLOR = 0x0001; |
| 198 | public static final int NORMAL = 0x0002; |
| 199 | public static final int TEXTURE_0 = 0x0100; |
| 200 | |
| 201 | public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) { |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 202 | mRS = rs; |
| 203 | mVtxCount = 0; |
| 204 | mIndexCount = 0; |
| 205 | mVtxData = new float[128]; |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 206 | mIndexData = new short[128]; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 207 | mVtxSize = vtxSize; |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 208 | mFlags = flags; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 209 | |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 210 | if (vtxSize < 2 || vtxSize > 3) { |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 211 | throw new IllegalArgumentException("Vertex size out of range."); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private void makeSpace(int count) { |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 216 | if ((mVtxCount + count) >= mVtxData.length) { |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 217 | float t[] = new float[mVtxData.length * 2]; |
| 218 | System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); |
| 219 | mVtxData = t; |
| 220 | } |
| 221 | } |
| 222 | |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 223 | private void latch() { |
| 224 | if ((mFlags & COLOR) != 0) { |
| 225 | makeSpace(4); |
| 226 | mVtxData[mVtxCount++] = mR; |
| 227 | mVtxData[mVtxCount++] = mG; |
| 228 | mVtxData[mVtxCount++] = mB; |
| 229 | mVtxData[mVtxCount++] = mA; |
| 230 | } |
| Jason Sams | 7299c83 | 2009-10-16 14:55:41 -0700 | [diff] [blame^] | 231 | if ((mFlags & TEXTURE_0) != 0) { |
| 232 | makeSpace(2); |
| 233 | mVtxData[mVtxCount++] = mS0; |
| 234 | mVtxData[mVtxCount++] = mT0; |
| 235 | } |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 236 | if ((mFlags & NORMAL) != 0) { |
| 237 | makeSpace(3); |
| 238 | mVtxData[mVtxCount++] = mNX; |
| 239 | mVtxData[mVtxCount++] = mNY; |
| 240 | mVtxData[mVtxCount++] = mNZ; |
| 241 | } |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | public void addVertex(float x, float y) { |
| 245 | if (mVtxSize != 2) { |
| 246 | throw new IllegalStateException("add mistmatch with declared components."); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 247 | } |
| 248 | makeSpace(2); |
| 249 | mVtxData[mVtxCount++] = x; |
| 250 | mVtxData[mVtxCount++] = y; |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 251 | latch(); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 254 | public void addVertex(float x, float y, float z) { |
| 255 | if (mVtxSize != 3) { |
| 256 | throw new IllegalStateException("add mistmatch with declared components."); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 257 | } |
| 258 | makeSpace(3); |
| 259 | mVtxData[mVtxCount++] = x; |
| 260 | mVtxData[mVtxCount++] = y; |
| 261 | mVtxData[mVtxCount++] = z; |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 262 | latch(); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 265 | public void setTexture(float s, float t) { |
| 266 | if ((mFlags & TEXTURE_0) == 0) { |
| 267 | throw new IllegalStateException("add mistmatch with declared components."); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 268 | } |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 269 | mS0 = s; |
| 270 | mT0 = t; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 273 | public void setNormal(float x, float y, float z) { |
| 274 | if ((mFlags & NORMAL) == 0) { |
| 275 | throw new IllegalStateException("add mistmatch with declared components."); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 276 | } |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 277 | mNX = x; |
| 278 | mNY = y; |
| 279 | mNZ = z; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 282 | public void setColor(float r, float g, float b, float a) { |
| 283 | if ((mFlags & COLOR) == 0) { |
| 284 | throw new IllegalStateException("add mistmatch with declared components."); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 285 | } |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 286 | mR = r; |
| 287 | mG = g; |
| 288 | mB = b; |
| 289 | mA = a; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | public void addTriangle(int idx1, int idx2, int idx3) { |
| Jason Sams | bd2197f | 2009-10-07 18:14:01 -0700 | [diff] [blame] | 293 | if((idx1 >= mVtxCount) || (idx1 < 0) || |
| 294 | (idx2 >= mVtxCount) || (idx2 < 0) || |
| 295 | (idx3 >= mVtxCount) || (idx3 < 0)) { |
| 296 | throw new IllegalStateException("Index provided greater than vertex count."); |
| 297 | } |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 298 | if ((mIndexCount + 3) >= mIndexData.length) { |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 299 | short t[] = new short[mIndexData.length * 2]; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 300 | System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); |
| 301 | mIndexData = t; |
| 302 | } |
| Jason Sams | 768bc02 | 2009-09-21 19:41:04 -0700 | [diff] [blame] | 303 | mIndexData[mIndexCount++] = (short)idx1; |
| 304 | mIndexData[mIndexCount++] = (short)idx2; |
| 305 | mIndexData[mIndexCount++] = (short)idx3; |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | public SimpleMesh create() { |
| 309 | Element.Builder b = new Element.Builder(mRS); |
| 310 | int floatCount = mVtxSize; |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 311 | if (mVtxSize == 2) { |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 312 | b.addFloatXY(); |
| 313 | } else { |
| 314 | b.addFloatXYZ(); |
| 315 | } |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 316 | if ((mFlags & COLOR) != 0) { |
| 317 | floatCount += 4; |
| 318 | b.addFloatRGBA(); |
| 319 | } |
| 320 | if ((mFlags & TEXTURE_0) != 0) { |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 321 | floatCount += 2; |
| 322 | b.addFloatST(); |
| 323 | } |
| Jason Sams | 7f04778 | 2009-10-02 18:18:35 -0700 | [diff] [blame] | 324 | if ((mFlags & NORMAL) != 0) { |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 325 | floatCount += 3; |
| 326 | b.addFloatNorm(); |
| 327 | } |
| 328 | mElement = b.create(); |
| 329 | |
| 330 | Builder smb = new Builder(mRS); |
| 331 | smb.addVertexType(mElement, mVtxCount / floatCount); |
| Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 332 | smb.setIndexType(Element.INDEX_16(mRS), mIndexCount); |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 333 | smb.setPrimitive(Primitive.TRIANGLE); |
| 334 | SimpleMesh sm = smb.create(); |
| 335 | |
| 336 | Allocation vertexAlloc = sm.createVertexAllocation(0); |
| 337 | Allocation indexAlloc = sm.createIndexAllocation(); |
| 338 | sm.bindVertexAllocation(vertexAlloc, 0); |
| 339 | sm.bindIndexAllocation(indexAlloc); |
| 340 | |
| 341 | vertexAlloc.data(mVtxData); |
| 342 | vertexAlloc.uploadToBufferObject(); |
| 343 | |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 344 | indexAlloc.data(mIndexData); |
| 345 | indexAlloc.uploadToBufferObject(); |
| 346 | |
| 347 | return sm; |
| 348 | } |
| 349 | } |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 350 | } |
| 351 | |