| 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 | |
| 53 | |
| 54 | public static class Builder { |
| 55 | RenderScript mRS; |
| 56 | |
| 57 | class Entry { |
| 58 | Type t; |
| 59 | Element e; |
| 60 | int size; |
| 61 | } |
| 62 | |
| 63 | int mVertexTypeCount; |
| 64 | Entry[] mVertexTypes; |
| 65 | Entry mIndexType; |
| 66 | //Entry mBatchType; |
| 67 | Primitive mPrimitive; |
| 68 | |
| 69 | |
| 70 | public Builder(RenderScript rs) { |
| 71 | mRS = rs; |
| 72 | mVertexTypeCount = 0; |
| 73 | mVertexTypes = new Entry[16]; |
| 74 | mIndexType = new Entry(); |
| 75 | } |
| 76 | |
| 77 | public int addVertexType(Type t) throws IllegalStateException { |
| 78 | if(mVertexTypeCount >= mVertexTypes.length) { |
| 79 | throw new IllegalStateException("Max vertex types exceeded."); |
| 80 | } |
| 81 | |
| 82 | int addedIndex = mVertexTypeCount; |
| 83 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 84 | mVertexTypes[mVertexTypeCount].t = t; |
| 85 | mVertexTypeCount++; |
| 86 | return addedIndex; |
| 87 | } |
| 88 | |
| 89 | public int addVertexType(Element e, int size) throws IllegalStateException { |
| 90 | if(mVertexTypeCount >= mVertexTypes.length) { |
| 91 | throw new IllegalStateException("Max vertex types exceeded."); |
| 92 | } |
| 93 | |
| 94 | int addedIndex = mVertexTypeCount; |
| 95 | mVertexTypes[mVertexTypeCount] = new Entry(); |
| 96 | mVertexTypes[mVertexTypeCount].e = e; |
| 97 | mVertexTypes[mVertexTypeCount].size = size; |
| 98 | mVertexTypeCount++; |
| 99 | return addedIndex; |
| 100 | } |
| 101 | |
| 102 | public void setIndexType(Type t) { |
| 103 | mIndexType.t = t; |
| 104 | mIndexType.e = null; |
| 105 | mIndexType.size = 0; |
| 106 | } |
| 107 | |
| 108 | public void setIndexType(Element e, int size) { |
| 109 | mIndexType.t = null; |
| 110 | mIndexType.e = e; |
| 111 | mIndexType.size = size; |
| 112 | } |
| 113 | |
| 114 | public void setPrimitive(Primitive p) { |
| 115 | mPrimitive = p; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | Type newType(Element e, int size) { |
| 120 | Type.Builder tb = new Type.Builder(mRS, e); |
| 121 | tb.add(Dimension.X, size); |
| 122 | return tb.create(); |
| 123 | } |
| 124 | |
| 125 | static synchronized SimpleMesh internalCreate(RenderScript rs, Builder b) { |
| 126 | Type[] toDestroy = new Type[18]; |
| 127 | int toDestroyCount = 0; |
| 128 | |
| 129 | int indexID = 0; |
| 130 | if(b.mIndexType.t != null) { |
| 131 | indexID = b.mIndexType.t.mID; |
| 132 | } else if(b.mIndexType.size != 0) { |
| 133 | b.mIndexType.t = b.newType(b.mIndexType.e, b.mIndexType.size); |
| 134 | indexID = b.mIndexType.t.mID; |
| 135 | toDestroy[toDestroyCount++] = b.mIndexType.t; |
| 136 | } |
| 137 | |
| 138 | int[] IDs = new int[b.mVertexTypeCount]; |
| 139 | for(int ct=0; ct < b.mVertexTypeCount; ct++) { |
| 140 | if(b.mVertexTypes[ct].t != null) { |
| 141 | IDs[ct] = b.mVertexTypes[ct].t.mID; |
| 142 | } else { |
| 143 | b.mVertexTypes[ct].t = b.newType(b.mVertexTypes[ct].e, b.mVertexTypes[ct].size); |
| 144 | IDs[ct] = b.mVertexTypes[ct].t.mID; |
| 145 | toDestroy[toDestroyCount++] = b.mVertexTypes[ct].t; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | int id = rs.nSimpleMeshCreate(0, indexID, IDs, b.mPrimitive.mID); |
| 150 | for(int ct=0; ct < toDestroyCount; ct++) { |
| 151 | toDestroy[ct].destroy(); |
| 152 | } |
| 153 | |
| 154 | return new SimpleMesh(id, rs); |
| 155 | } |
| 156 | |
| 157 | public SimpleMesh create() { |
| 158 | Log.e("rs", "SimpleMesh create"); |
| 159 | SimpleMesh sm = internalCreate(mRS, this); |
| 160 | sm.mVertexTypes = new Type[mVertexTypeCount]; |
| 161 | for(int ct=0; ct < mVertexTypeCount; ct++) { |
| 162 | sm.mVertexTypes[ct] = mVertexTypes[ct].t; |
| 163 | } |
| 164 | sm.mIndexType = mIndexType.t; |
| 165 | sm.mPrimitive = mPrimitive; |
| 166 | return sm; |
| 167 | } |
| 168 | } |
| 169 | |
| Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 170 | public static class TriangleMeshBuilder { |
| 171 | float mVtxData[]; |
| 172 | int mVtxCount; |
| 173 | int mIndexData[]; |
| 174 | int mIndexCount; |
| 175 | RenderScript mRS; |
| 176 | Element mElement; |
| 177 | |
| 178 | int mVtxSize; |
| 179 | boolean mNorm; |
| 180 | boolean mTex; |
| 181 | |
| 182 | public TriangleMeshBuilder(RenderScript rs, int vtxSize, boolean norm, boolean tex) { |
| 183 | mRS = rs; |
| 184 | mVtxCount = 0; |
| 185 | mIndexCount = 0; |
| 186 | mVtxData = new float[128]; |
| 187 | mIndexData = new int[128]; |
| 188 | mVtxSize = vtxSize; |
| 189 | mNorm = norm; |
| 190 | mTex = tex; |
| 191 | |
| 192 | if(vtxSize < 2 || vtxSize > 3) { |
| 193 | throw new IllegalArgumentException("Vertex size out of range."); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private void makeSpace(int count) { |
| 198 | if((mVtxCount + count) >= mVtxData.length) { |
| 199 | float t[] = new float[mVtxData.length * 2]; |
| 200 | System.arraycopy(mVtxData, 0, t, 0, mVtxData.length); |
| 201 | mVtxData = t; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | public void add_XY(float x, float y) { |
| 206 | if((mVtxSize != 2) || mNorm || mTex) { |
| 207 | throw new IllegalStateException("add mistmatch with declaired components."); |
| 208 | } |
| 209 | makeSpace(2); |
| 210 | mVtxData[mVtxCount++] = x; |
| 211 | mVtxData[mVtxCount++] = y; |
| 212 | } |
| 213 | |
| 214 | public void add_XYZ(float x, float y, float z) { |
| 215 | if((mVtxSize != 3) || mNorm || mTex) { |
| 216 | throw new IllegalStateException("add mistmatch with declaired components."); |
| 217 | } |
| 218 | makeSpace(3); |
| 219 | mVtxData[mVtxCount++] = x; |
| 220 | mVtxData[mVtxCount++] = y; |
| 221 | mVtxData[mVtxCount++] = z; |
| 222 | } |
| 223 | |
| 224 | public void add_XY_ST(float x, float y, float s, float t) { |
| 225 | if((mVtxSize != 2) || mNorm || !mTex) { |
| 226 | throw new IllegalStateException("add mistmatch with declaired components."); |
| 227 | } |
| 228 | makeSpace(4); |
| 229 | mVtxData[mVtxCount++] = x; |
| 230 | mVtxData[mVtxCount++] = y; |
| 231 | mVtxData[mVtxCount++] = s; |
| 232 | mVtxData[mVtxCount++] = t; |
| 233 | } |
| 234 | |
| 235 | public void add_XYZ_ST(float x, float y, float z, float s, float t) { |
| 236 | if((mVtxSize != 3) || mNorm || !mTex) { |
| 237 | throw new IllegalStateException("add mistmatch with declaired components."); |
| 238 | } |
| 239 | makeSpace(5); |
| 240 | mVtxData[mVtxCount++] = x; |
| 241 | mVtxData[mVtxCount++] = y; |
| 242 | mVtxData[mVtxCount++] = z; |
| 243 | mVtxData[mVtxCount++] = s; |
| 244 | mVtxData[mVtxCount++] = t; |
| 245 | } |
| 246 | |
| 247 | public void add_XYZ_ST_NORM(float x, float y, float z, float s, float t, float nx, float ny, float nz) { |
| 248 | if((mVtxSize != 3) || !mNorm || !mTex) { |
| 249 | throw new IllegalStateException("add mistmatch with declaired components."); |
| 250 | } |
| 251 | makeSpace(8); |
| 252 | mVtxData[mVtxCount++] = x; |
| 253 | mVtxData[mVtxCount++] = y; |
| 254 | mVtxData[mVtxCount++] = z; |
| 255 | mVtxData[mVtxCount++] = s; |
| 256 | mVtxData[mVtxCount++] = t; |
| 257 | mVtxData[mVtxCount++] = nx; |
| 258 | mVtxData[mVtxCount++] = ny; |
| 259 | mVtxData[mVtxCount++] = nz; |
| 260 | } |
| 261 | |
| 262 | public void addTriangle(int idx1, int idx2, int idx3) { |
| 263 | if((mIndexCount + 3) >= mIndexData.length) { |
| 264 | int t[] = new int[mIndexData.length * 2]; |
| 265 | System.arraycopy(mIndexData, 0, t, 0, mIndexData.length); |
| 266 | mIndexData = t; |
| 267 | } |
| 268 | mIndexData[mIndexCount++] = idx1; |
| 269 | mIndexData[mIndexCount++] = idx2; |
| 270 | mIndexData[mIndexCount++] = idx3; |
| 271 | } |
| 272 | |
| 273 | public SimpleMesh create() { |
| 274 | Element.Builder b = new Element.Builder(mRS); |
| 275 | int floatCount = mVtxSize; |
| 276 | if(mVtxSize == 2) { |
| 277 | b.addFloatXY(); |
| 278 | } else { |
| 279 | b.addFloatXYZ(); |
| 280 | } |
| 281 | if(mTex) { |
| 282 | floatCount += 2; |
| 283 | b.addFloatST(); |
| 284 | } |
| 285 | if(mNorm) { |
| 286 | floatCount += 3; |
| 287 | b.addFloatNorm(); |
| 288 | } |
| 289 | mElement = b.create(); |
| 290 | |
| 291 | Builder smb = new Builder(mRS); |
| 292 | smb.addVertexType(mElement, mVtxCount / floatCount); |
| 293 | smb.setIndexType(Element.INDEX_16, mIndexCount); |
| 294 | smb.setPrimitive(Primitive.TRIANGLE); |
| 295 | SimpleMesh sm = smb.create(); |
| 296 | |
| 297 | Allocation vertexAlloc = sm.createVertexAllocation(0); |
| 298 | Allocation indexAlloc = sm.createIndexAllocation(); |
| 299 | sm.bindVertexAllocation(vertexAlloc, 0); |
| 300 | sm.bindIndexAllocation(indexAlloc); |
| 301 | |
| 302 | vertexAlloc.data(mVtxData); |
| 303 | vertexAlloc.uploadToBufferObject(); |
| 304 | |
| 305 | // This is safe because length is a pow2 |
| 306 | for(int ct=0; ct < (mIndexCount+1); ct += 2) { |
| 307 | mIndexData[ct >> 1] = mIndexData[ct] | (mIndexData[ct+1] << 16); |
| 308 | } |
| 309 | indexAlloc.data(mIndexData); |
| 310 | indexAlloc.uploadToBufferObject(); |
| 311 | |
| 312 | return sm; |
| 313 | } |
| 314 | } |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 315 | } |
| 316 | |