blob: 5d876549eef40e5565d6bb60ea3d8fa14784e845 [file] [log] [blame]
Jason Sams1bada8c2009-08-09 17:01:55 -07001/*
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
17package android.renderscript;
18
19import android.util.Config;
20import android.util.Log;
21
22/**
23 * @hide
24 *
25 **/
26public 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 Sams1bada8c2009-08-09 17:01:55 -070037 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 Sams2525a812009-09-03 15:43:13 -070053 public Type getVertexType(int slot) {
54 return mVertexTypes[slot];
55 }
56
57 public Type getIndexType() {
58 return mIndexType;
59 }
Jason Sams1bada8c2009-08-09 17:01:55 -070060
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() {
165 Log.e("rs", "SimpleMesh create");
166 SimpleMesh sm = internalCreate(mRS, this);
167 sm.mVertexTypes = new Type[mVertexTypeCount];
168 for(int ct=0; ct < mVertexTypeCount; ct++) {
169 sm.mVertexTypes[ct] = mVertexTypes[ct].t;
170 }
171 sm.mIndexType = mIndexType.t;
172 sm.mPrimitive = mPrimitive;
173 return sm;
174 }
175 }
176
Jason Sams07ae4062009-08-27 20:23:34 -0700177 public static class TriangleMeshBuilder {
178 float mVtxData[];
179 int mVtxCount;
180 int mIndexData[];
181 int mIndexCount;
182 RenderScript mRS;
183 Element mElement;
184
185 int mVtxSize;
186 boolean mNorm;
187 boolean mTex;
188
189 public TriangleMeshBuilder(RenderScript rs, int vtxSize, boolean norm, boolean tex) {
190 mRS = rs;
191 mVtxCount = 0;
192 mIndexCount = 0;
193 mVtxData = new float[128];
194 mIndexData = new int[128];
195 mVtxSize = vtxSize;
196 mNorm = norm;
197 mTex = tex;
198
199 if(vtxSize < 2 || vtxSize > 3) {
200 throw new IllegalArgumentException("Vertex size out of range.");
201 }
202 }
203
204 private void makeSpace(int count) {
205 if((mVtxCount + count) >= mVtxData.length) {
206 float t[] = new float[mVtxData.length * 2];
207 System.arraycopy(mVtxData, 0, t, 0, mVtxData.length);
208 mVtxData = t;
209 }
210 }
211
212 public void add_XY(float x, float y) {
213 if((mVtxSize != 2) || mNorm || mTex) {
214 throw new IllegalStateException("add mistmatch with declaired components.");
215 }
216 makeSpace(2);
217 mVtxData[mVtxCount++] = x;
218 mVtxData[mVtxCount++] = y;
219 }
220
221 public void add_XYZ(float x, float y, float z) {
222 if((mVtxSize != 3) || mNorm || mTex) {
223 throw new IllegalStateException("add mistmatch with declaired components.");
224 }
225 makeSpace(3);
226 mVtxData[mVtxCount++] = x;
227 mVtxData[mVtxCount++] = y;
228 mVtxData[mVtxCount++] = z;
229 }
230
231 public void add_XY_ST(float x, float y, float s, float t) {
232 if((mVtxSize != 2) || mNorm || !mTex) {
233 throw new IllegalStateException("add mistmatch with declaired components.");
234 }
235 makeSpace(4);
236 mVtxData[mVtxCount++] = x;
237 mVtxData[mVtxCount++] = y;
238 mVtxData[mVtxCount++] = s;
239 mVtxData[mVtxCount++] = t;
240 }
241
242 public void add_XYZ_ST(float x, float y, float z, float s, float t) {
243 if((mVtxSize != 3) || mNorm || !mTex) {
244 throw new IllegalStateException("add mistmatch with declaired components.");
245 }
246 makeSpace(5);
247 mVtxData[mVtxCount++] = x;
248 mVtxData[mVtxCount++] = y;
249 mVtxData[mVtxCount++] = z;
250 mVtxData[mVtxCount++] = s;
251 mVtxData[mVtxCount++] = t;
252 }
253
254 public void add_XYZ_ST_NORM(float x, float y, float z, float s, float t, float nx, float ny, float nz) {
255 if((mVtxSize != 3) || !mNorm || !mTex) {
256 throw new IllegalStateException("add mistmatch with declaired components.");
257 }
258 makeSpace(8);
259 mVtxData[mVtxCount++] = x;
260 mVtxData[mVtxCount++] = y;
261 mVtxData[mVtxCount++] = z;
262 mVtxData[mVtxCount++] = s;
263 mVtxData[mVtxCount++] = t;
264 mVtxData[mVtxCount++] = nx;
265 mVtxData[mVtxCount++] = ny;
266 mVtxData[mVtxCount++] = nz;
267 }
268
269 public void addTriangle(int idx1, int idx2, int idx3) {
270 if((mIndexCount + 3) >= mIndexData.length) {
271 int t[] = new int[mIndexData.length * 2];
272 System.arraycopy(mIndexData, 0, t, 0, mIndexData.length);
273 mIndexData = t;
274 }
275 mIndexData[mIndexCount++] = idx1;
276 mIndexData[mIndexCount++] = idx2;
277 mIndexData[mIndexCount++] = idx3;
278 }
279
280 public SimpleMesh create() {
281 Element.Builder b = new Element.Builder(mRS);
282 int floatCount = mVtxSize;
283 if(mVtxSize == 2) {
284 b.addFloatXY();
285 } else {
286 b.addFloatXYZ();
287 }
288 if(mTex) {
289 floatCount += 2;
290 b.addFloatST();
291 }
292 if(mNorm) {
293 floatCount += 3;
294 b.addFloatNorm();
295 }
296 mElement = b.create();
297
298 Builder smb = new Builder(mRS);
299 smb.addVertexType(mElement, mVtxCount / floatCount);
300 smb.setIndexType(Element.INDEX_16, mIndexCount);
301 smb.setPrimitive(Primitive.TRIANGLE);
302 SimpleMesh sm = smb.create();
303
304 Allocation vertexAlloc = sm.createVertexAllocation(0);
305 Allocation indexAlloc = sm.createIndexAllocation();
306 sm.bindVertexAllocation(vertexAlloc, 0);
307 sm.bindIndexAllocation(indexAlloc);
308
309 vertexAlloc.data(mVtxData);
310 vertexAlloc.uploadToBufferObject();
311
312 // This is safe because length is a pow2
313 for(int ct=0; ct < (mIndexCount+1); ct += 2) {
314 mIndexData[ct >> 1] = mIndexData[ct] | (mIndexData[ct+1] << 16);
315 }
316 indexAlloc.data(mIndexData);
317 indexAlloc.uploadToBufferObject();
318
319 return sm;
320 }
321 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700322}
323