blob: 5b4cadb6c98eb633aaad2735b3aa6fad9aa46af6 [file] [log] [blame]
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07001/*
Jason Samse619de62012-05-08 18:40:58 -07002 * Copyright (C) 2008-2012 The Android Open Source Project
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -07003 *
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 java.util.Vector;
20
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070021/**
Tim Murraya9084222013-04-05 22:06:43 +000022 * @hide
Jason Samsd4ca9912012-05-08 19:02:07 -070023 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -080024 * <p>This class is a container for geometric data displayed with
Tim Murrayc11e25c2013-04-09 11:01:01 -070025 * RenderScript. Internally, a mesh is a collection of allocations that
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080026 * represent vertex data (positions, normals, texture
Robert Ly11518ac2011-02-09 13:57:06 -080027 * coordinates) and index data such as triangles and lines. </p>
28 * <p>
29 * Vertex data could either be interleaved within one
30 * allocation that is provided separately, as multiple allocation
31 * objects, or done as a combination of both. When a
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080032 * vertex channel name matches an input in the vertex program,
Tim Murrayc11e25c2013-04-09 11:01:01 -070033 * RenderScript automatically connects the two together.
Robert Ly11518ac2011-02-09 13:57:06 -080034 * </p>
35 * <p>
36 * Parts of the mesh can be rendered with either explicit
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080037 * index sets or primitive types.
Robert Ly11518ac2011-02-09 13:57:06 -080038 * </p>
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070039 **/
40public class Mesh extends BaseObj {
41
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070042 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070043 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080044 * Describes the way mesh vertex data is interpreted when rendering
45 *
46 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080047 public enum Primitive {
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070048 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070049 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080050 * Vertex data will be rendered as a series of points
51 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080052 POINT (0),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070053 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070054 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080055 * Vertex pairs will be rendered as lines
56 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080057 LINE (1),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070058 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070059 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080060 * Vertex data will be rendered as a connected line strip
61 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080062 LINE_STRIP (2),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070063 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070064 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080065 * Vertices will be rendered as individual triangles
66 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080067 TRIANGLE (3),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070068 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070069 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080070 * Vertices will be rendered as a connected triangle strip
71 * defined by the first three vertices with each additional
72 * triangle defined by a new vertex
73 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080074 TRIANGLE_STRIP (4),
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070075 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070076 * @deprecated in API 16
Alex Sakhartchoukec0d3352011-01-17 15:23:22 -080077 * Vertices will be rendered as a sequence of triangles that all
78 * share first vertex as the origin
79 */
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -080080 TRIANGLE_FAN (5);
81
82 int mID;
83 Primitive(int id) {
84 mID = id;
85 }
86 }
87
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070088 Allocation[] mVertexBuffers;
89 Allocation[] mIndexBuffers;
90 Primitive[] mPrimitives;
91
Tim Murray460a0492013-11-19 12:45:54 -080092 Mesh(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070093 super(id, rs);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -070094 }
95
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070096 /**
Jason Samsd4ca9912012-05-08 19:02:07 -070097 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -080098 * @return number of allocations containing vertex data
99 *
100 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700101 public int getVertexAllocationCount() {
102 if(mVertexBuffers == null) {
103 return 0;
104 }
105 return mVertexBuffers.length;
106 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700107 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700108 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800109 * @param slot index in the list of allocations to return
110 * @return vertex data allocation at the given index
111 *
112 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700113 public Allocation getVertexAllocation(int slot) {
114 return mVertexBuffers[slot];
115 }
116
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700117 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700118 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800119 * @return number of primitives or index sets in the mesh
120 *
121 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700122 public int getPrimitiveCount() {
123 if(mIndexBuffers == null) {
124 return 0;
125 }
126 return mIndexBuffers.length;
127 }
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800128
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700129 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700130 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800131 * @param slot locaton within the list of index set allocation
132 * @return allocation containing primtive index data or null if
133 * the index data is not specified explicitly
134 *
135 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800136 public Allocation getIndexSetAllocation(int slot) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700137 return mIndexBuffers[slot];
138 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700139 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700140 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800141 * @param slot locaiton within the list of index set primitives
142 * @return index set primitive type
143 *
144 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700145 public Primitive getPrimitive(int slot) {
146 return mPrimitives[slot];
147 }
148
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700149 @Override
150 void updateFromNative() {
Jason Sams06d69de2010-11-09 17:11:40 -0800151 super.updateFromNative();
Jason Samse07694b2012-04-03 15:36:36 -0700152 int vtxCount = mRS.nMeshGetVertexBufferCount(getID(mRS));
153 int idxCount = mRS.nMeshGetIndexCount(getID(mRS));
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700154
Ashok Bhat98071552014-02-12 09:54:43 +0000155 long[] vtxIDs = new long[vtxCount];
156 long[] idxIDs = new long[idxCount];
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700157 int[] primitives = new int[idxCount];
158
Jason Samse07694b2012-04-03 15:36:36 -0700159 mRS.nMeshGetVertices(getID(mRS), vtxIDs, vtxCount);
160 mRS.nMeshGetIndices(getID(mRS), idxIDs, primitives, idxCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700161
162 mVertexBuffers = new Allocation[vtxCount];
163 mIndexBuffers = new Allocation[idxCount];
164 mPrimitives = new Primitive[idxCount];
165
166 for(int i = 0; i < vtxCount; i ++) {
167 if(vtxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800168 mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700169 mVertexBuffers[i].updateFromNative();
170 }
171 }
172
173 for(int i = 0; i < idxCount; i ++) {
174 if(idxIDs[i] != 0) {
Jason Samsd4b23b52010-12-13 15:32:35 -0800175 mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700176 mIndexBuffers[i].updateFromNative();
177 }
178 mPrimitives[i] = Primitive.values()[primitives[i]];
179 }
180 }
181
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700182 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700183 * @deprecated in API 16
Robert Ly11518ac2011-02-09 13:57:06 -0800184 * Mesh builder object. It starts empty and requires you to
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800185 * add the types necessary to create vertex and index
Robert Ly11518ac2011-02-09 13:57:06 -0800186 * allocations.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800187 *
188 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700189 public static class Builder {
190 RenderScript mRS;
Jason Samsd1952402010-12-20 12:55:28 -0800191 int mUsage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700192
193 class Entry {
194 Type t;
195 Element e;
196 int size;
197 Primitive prim;
Jason Samsd1952402010-12-20 12:55:28 -0800198 int usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700199 }
200
201 int mVertexTypeCount;
202 Entry[] mVertexTypes;
203 Vector mIndexTypes;
204
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700205 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700206 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800207 * Creates builder object
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800208 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800209 * @param usage specifies how the mesh allocations are to be
210 * handled, whether they need to be uploaded to a
211 * buffer on the gpu, maintain a cpu copy, etc
212 */
Jason Samsd1952402010-12-20 12:55:28 -0800213 public Builder(RenderScript rs, int usage) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700214 mRS = rs;
Jason Samsd1952402010-12-20 12:55:28 -0800215 mUsage = usage;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700216 mVertexTypeCount = 0;
217 mVertexTypes = new Entry[16];
218 mIndexTypes = new Vector();
219 }
220
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700221 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700222 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800223 * @return internal index of the last vertex buffer type added to
224 * builder
225 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800226 public int getCurrentVertexTypeIndex() {
227 return mVertexTypeCount - 1;
228 }
229
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700230 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700231 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800232 * @return internal index of the last index set added to the
233 * builder
234 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800235 public int getCurrentIndexSetIndex() {
236 return mIndexTypes.size() - 1;
237 }
238
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700239 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700240 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800241 * Adds a vertex data type to the builder object
242 *
Stephen Hinesb11e3d22011-01-11 19:30:58 -0800243 * @param t type of the vertex data allocation to be created
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800244 *
245 * @return this
246 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800247 public Builder addVertexType(Type t) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700248 if (mVertexTypeCount >= mVertexTypes.length) {
249 throw new IllegalStateException("Max vertex types exceeded.");
250 }
251
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700252 mVertexTypes[mVertexTypeCount] = new Entry();
253 mVertexTypes[mVertexTypeCount].t = t;
254 mVertexTypes[mVertexTypeCount].e = null;
255 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800256 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700257 }
258
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700259 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700260 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800261 * Adds a vertex data type to the builder object
262 *
263 * @param e element describing the vertex data layout
264 * @param size number of elements in the buffer
265 *
266 * @return this
267 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800268 public Builder addVertexType(Element e, int size) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700269 if (mVertexTypeCount >= mVertexTypes.length) {
270 throw new IllegalStateException("Max vertex types exceeded.");
271 }
272
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700273 mVertexTypes[mVertexTypeCount] = new Entry();
274 mVertexTypes[mVertexTypeCount].t = null;
275 mVertexTypes[mVertexTypeCount].e = e;
276 mVertexTypes[mVertexTypeCount].size = size;
277 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800278 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700279 }
280
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700281 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700282 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800283 * Adds an index set data type to the builder object
284 *
285 * @param t type of the index set data, could be null
286 * @param p primitive type
287 *
288 * @return this
289 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800290 public Builder addIndexSetType(Type t, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700291 Entry indexType = new Entry();
292 indexType.t = t;
293 indexType.e = null;
294 indexType.size = 0;
295 indexType.prim = p;
296 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800297 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700298 }
299
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700300 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700301 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800302 * Adds an index set primitive type to the builder object
303 *
304 * @param p primitive type
305 *
306 * @return this
307 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800308 public Builder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700309 Entry indexType = new Entry();
310 indexType.t = null;
311 indexType.e = null;
312 indexType.size = 0;
313 indexType.prim = p;
314 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800315 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700316 }
317
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700318 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700319 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800320 * Adds an index set data type to the builder object
321 *
322 * @param e element describing the index set data layout
323 * @param size number of elements in the buffer
324 * @param p primitive type
325 *
326 * @return this
327 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800328 public Builder addIndexSetType(Element e, int size, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700329 Entry indexType = new Entry();
330 indexType.t = null;
331 indexType.e = e;
332 indexType.size = size;
333 indexType.prim = p;
334 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800335 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700336 }
337
338 Type newType(Element e, int size) {
339 Type.Builder tb = new Type.Builder(mRS, e);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800340 tb.setX(size);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700341 return tb.create();
342 }
343
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700344 /**
Jason Samsd4ca9912012-05-08 19:02:07 -0700345 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800346 * Create a Mesh object from the current state of the builder
347 *
348 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700349 public Mesh create() {
350 mRS.validate();
Ashok Bhat98071552014-02-12 09:54:43 +0000351 long[] vtx = new long[mVertexTypeCount];
352 long[] idx = new long[mIndexTypes.size()];
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700353 int[] prim = new int[mIndexTypes.size()];
354
355 Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
356 Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
357 Primitive[] primitives = new Primitive[mIndexTypes.size()];
358
359 for(int ct = 0; ct < mVertexTypeCount; ct ++) {
360 Allocation alloc = null;
361 Entry entry = mVertexTypes[ct];
362 if (entry.t != null) {
363 alloc = Allocation.createTyped(mRS, entry.t, mUsage);
364 } else if(entry.e != null) {
365 alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
366 }
367 vertexBuffers[ct] = alloc;
Ashok Bhat98071552014-02-12 09:54:43 +0000368 vtx[ct] = alloc.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700369 }
370
371 for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
372 Allocation alloc = null;
373 Entry entry = (Entry)mIndexTypes.elementAt(ct);
374 if (entry.t != null) {
375 alloc = Allocation.createTyped(mRS, entry.t, mUsage);
376 } else if(entry.e != null) {
377 alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
378 }
Tim Murray460a0492013-11-19 12:45:54 -0800379 long allocID = (alloc == null) ? 0 : alloc.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700380 indexBuffers[ct] = alloc;
381 primitives[ct] = entry.prim;
382
Ashok Bhat98071552014-02-12 09:54:43 +0000383 idx[ct] = allocID;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700384 prim[ct] = entry.prim.mID;
385 }
386
Tim Murray460a0492013-11-19 12:45:54 -0800387 long id = mRS.nMeshCreate(vtx, idx, prim);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700388 Mesh newMesh = new Mesh(id, mRS);
389 newMesh.mVertexBuffers = vertexBuffers;
390 newMesh.mIndexBuffers = indexBuffers;
391 newMesh.mPrimitives = primitives;
392
393 return newMesh;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700394 }
395 }
396
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700397 /**
Jason Samse619de62012-05-08 18:40:58 -0700398 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800399 * Mesh builder object. It starts empty and requires the user to
400 * add all the vertex and index allocations that comprise the
401 * mesh
402 *
403 */
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700404 public static class AllocationBuilder {
405 RenderScript mRS;
406
407 class Entry {
408 Allocation a;
409 Primitive prim;
410 }
411
412 int mVertexTypeCount;
413 Entry[] mVertexTypes;
414
415 Vector mIndexTypes;
416
Jason Samse619de62012-05-08 18:40:58 -0700417 /**
418 * @deprecated in API 16
419 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700420 public AllocationBuilder(RenderScript rs) {
421 mRS = rs;
422 mVertexTypeCount = 0;
423 mVertexTypes = new Entry[16];
424 mIndexTypes = new Vector();
425 }
426
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700427 /**
Jason Samse619de62012-05-08 18:40:58 -0700428 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800429 * @return internal index of the last vertex buffer type added to
430 * builder
431 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800432 public int getCurrentVertexTypeIndex() {
433 return mVertexTypeCount - 1;
434 }
435
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700436 /**
Jason Samse619de62012-05-08 18:40:58 -0700437 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800438 * @return internal index of the last index set added to the
439 * builder
440 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800441 public int getCurrentIndexSetIndex() {
442 return mIndexTypes.size() - 1;
443 }
444
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700445 /**
Jason Samse619de62012-05-08 18:40:58 -0700446 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800447 * Adds an allocation containing vertex buffer data to the
448 * builder
449 *
450 * @param a vertex data allocation
451 *
452 * @return this
453 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800454 public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700455 if (mVertexTypeCount >= mVertexTypes.length) {
456 throw new IllegalStateException("Max vertex types exceeded.");
457 }
458
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700459 mVertexTypes[mVertexTypeCount] = new Entry();
460 mVertexTypes[mVertexTypeCount].a = a;
461 mVertexTypeCount++;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800462 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700463 }
464
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700465 /**
Jason Samse619de62012-05-08 18:40:58 -0700466 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800467 * Adds an allocation containing index buffer data and index type
468 * to the builder
469 *
470 * @param a index set data allocation, could be null
471 * @param p index set primitive type
472 *
473 * @return this
474 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800475 public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700476 Entry indexType = new Entry();
477 indexType.a = a;
478 indexType.prim = p;
479 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800480 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700481 }
482
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700483 /**
Jason Samse619de62012-05-08 18:40:58 -0700484 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800485 * Adds an index set type to the builder
486 *
487 * @param p index set primitive type
488 *
489 * @return this
490 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800491 public AllocationBuilder addIndexSetType(Primitive p) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700492 Entry indexType = new Entry();
493 indexType.a = null;
494 indexType.prim = p;
495 mIndexTypes.addElement(indexType);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800496 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700497 }
498
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700499 /**
Jason Samse619de62012-05-08 18:40:58 -0700500 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800501 * Create a Mesh object from the current state of the builder
502 *
503 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700504 public Mesh create() {
505 mRS.validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700506
Ashok Bhat98071552014-02-12 09:54:43 +0000507 long[] vtx = new long[mVertexTypeCount];
508 long[] idx = new long[mIndexTypes.size()];
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700509 int[] prim = new int[mIndexTypes.size()];
510
511 Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
512 Primitive[] primitives = new Primitive[mIndexTypes.size()];
513 Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
514
515 for(int ct = 0; ct < mVertexTypeCount; ct ++) {
516 Entry entry = mVertexTypes[ct];
517 vertexBuffers[ct] = entry.a;
Ashok Bhat98071552014-02-12 09:54:43 +0000518 vtx[ct] = entry.a.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700519 }
520
521 for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
522 Entry entry = (Entry)mIndexTypes.elementAt(ct);
Tim Murray460a0492013-11-19 12:45:54 -0800523 long allocID = (entry.a == null) ? 0 : entry.a.getID(mRS);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700524 indexBuffers[ct] = entry.a;
525 primitives[ct] = entry.prim;
526
Ashok Bhat98071552014-02-12 09:54:43 +0000527 idx[ct] = allocID;
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700528 prim[ct] = entry.prim.mID;
529 }
530
Tim Murray460a0492013-11-19 12:45:54 -0800531 long id = mRS.nMeshCreate(vtx, idx, prim);
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700532 Mesh newMesh = new Mesh(id, mRS);
533 newMesh.mVertexBuffers = vertexBuffers;
534 newMesh.mIndexBuffers = indexBuffers;
535 newMesh.mPrimitives = primitives;
536
537 return newMesh;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700538 }
539 }
540
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700541 /**
Jason Samse619de62012-05-08 18:40:58 -0700542 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800543 * Builder that allows creation of a mesh object point by point
544 * and triangle by triangle
545 *
546 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700547 public static class TriangleMeshBuilder {
548 float mVtxData[];
549 int mVtxCount;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800550 int mMaxIndex;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700551 short mIndexData[];
552 int mIndexCount;
553 RenderScript mRS;
554 Element mElement;
555
556 float mNX = 0;
557 float mNY = 0;
558 float mNZ = -1;
559 float mS0 = 0;
560 float mT0 = 0;
561 float mR = 1;
562 float mG = 1;
563 float mB = 1;
564 float mA = 1;
565
566 int mVtxSize;
567 int mFlags;
568
Jason Samse619de62012-05-08 18:40:58 -0700569 /**
570 * @deprecated in API 16
571 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700572 public static final int COLOR = 0x0001;
Jason Samse619de62012-05-08 18:40:58 -0700573 /**
574 * @deprecated in API 16
575 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700576 public static final int NORMAL = 0x0002;
Jason Samse619de62012-05-08 18:40:58 -0700577 /**
578 * @deprecated in API 16
579 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700580 public static final int TEXTURE_0 = 0x0100;
581
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700582 /**
Jason Samse619de62012-05-08 18:40:58 -0700583 * @deprecated in API 16
Alex Sakhartchoukf5c876e2011-01-13 14:53:43 -0800584 * @param rs Context to which the mesh will belong.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800585 * @param vtxSize specifies whether the vertex is a float2 or
586 * float3
587 * @param flags bitfield that is a combination of COLOR, NORMAL,
588 * and TEXTURE_0 that specifies what vertex data
589 * channels are present in the mesh
590 *
591 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700592 public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) {
593 mRS = rs;
594 mVtxCount = 0;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800595 mMaxIndex = 0;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700596 mIndexCount = 0;
597 mVtxData = new float[128];
598 mIndexData = new short[128];
599 mVtxSize = vtxSize;
600 mFlags = flags;
601
602 if (vtxSize < 2 || vtxSize > 3) {
603 throw new IllegalArgumentException("Vertex size out of range.");
604 }
605 }
606
607 private void makeSpace(int count) {
608 if ((mVtxCount + count) >= mVtxData.length) {
609 float t[] = new float[mVtxData.length * 2];
610 System.arraycopy(mVtxData, 0, t, 0, mVtxData.length);
611 mVtxData = t;
612 }
613 }
614
615 private void latch() {
616 if ((mFlags & COLOR) != 0) {
617 makeSpace(4);
618 mVtxData[mVtxCount++] = mR;
619 mVtxData[mVtxCount++] = mG;
620 mVtxData[mVtxCount++] = mB;
621 mVtxData[mVtxCount++] = mA;
622 }
623 if ((mFlags & TEXTURE_0) != 0) {
624 makeSpace(2);
625 mVtxData[mVtxCount++] = mS0;
626 mVtxData[mVtxCount++] = mT0;
627 }
628 if ((mFlags & NORMAL) != 0) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800629 makeSpace(4);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700630 mVtxData[mVtxCount++] = mNX;
631 mVtxData[mVtxCount++] = mNY;
632 mVtxData[mVtxCount++] = mNZ;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800633 mVtxData[mVtxCount++] = 0.0f;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700634 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800635 mMaxIndex ++;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700636 }
637
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700638 /**
Jason Samse619de62012-05-08 18:40:58 -0700639 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800640 * Adds a float2 vertex to the mesh
641 *
642 * @param x position x
643 * @param y position y
644 *
645 * @return this
646 *
647 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800648 public TriangleMeshBuilder addVertex(float x, float y) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700649 if (mVtxSize != 2) {
650 throw new IllegalStateException("add mistmatch with declared components.");
651 }
652 makeSpace(2);
653 mVtxData[mVtxCount++] = x;
654 mVtxData[mVtxCount++] = y;
655 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800656 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700657 }
658
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700659 /**
Jason Samse619de62012-05-08 18:40:58 -0700660 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800661 * Adds a float3 vertex to the mesh
662 *
663 * @param x position x
664 * @param y position y
665 * @param z position z
666 *
667 * @return this
668 *
669 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800670 public TriangleMeshBuilder addVertex(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700671 if (mVtxSize != 3) {
672 throw new IllegalStateException("add mistmatch with declared components.");
673 }
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800674 makeSpace(4);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700675 mVtxData[mVtxCount++] = x;
676 mVtxData[mVtxCount++] = y;
677 mVtxData[mVtxCount++] = z;
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800678 mVtxData[mVtxCount++] = 1.0f;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700679 latch();
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800680 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700681 }
682
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700683 /**
Jason Samse619de62012-05-08 18:40:58 -0700684 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800685 * Sets the texture coordinate for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800686 *
687 * @param s texture coordinate s
688 * @param t texture coordinate t
689 *
690 * @return this
691 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800692 public TriangleMeshBuilder setTexture(float s, float t) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700693 if ((mFlags & TEXTURE_0) == 0) {
694 throw new IllegalStateException("add mistmatch with declared components.");
695 }
696 mS0 = s;
697 mT0 = t;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800698 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700699 }
700
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700701 /**
Jason Samse619de62012-05-08 18:40:58 -0700702 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800703 * Sets the normal vector for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800704 *
705 * @param x normal vector x
706 * @param y normal vector y
707 * @param z normal vector z
708 *
709 * @return this
710 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800711 public TriangleMeshBuilder setNormal(float x, float y, float z) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700712 if ((mFlags & NORMAL) == 0) {
713 throw new IllegalStateException("add mistmatch with declared components.");
714 }
715 mNX = x;
716 mNY = y;
717 mNZ = z;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800718 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700719 }
720
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700721 /**
Jason Samse619de62012-05-08 18:40:58 -0700722 * @deprecated in API 16
Robert Lyf11ffc112012-02-22 10:59:12 -0800723 * Sets the color for the vertices that are added after this method call.
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800724 *
725 * @param r red component
726 * @param g green component
727 * @param b blue component
728 * @param a alpha component
729 *
730 * @return this
731 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800732 public TriangleMeshBuilder setColor(float r, float g, float b, float a) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700733 if ((mFlags & COLOR) == 0) {
734 throw new IllegalStateException("add mistmatch with declared components.");
735 }
736 mR = r;
737 mG = g;
738 mB = b;
739 mA = a;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800740 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700741 }
742
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700743 /**
Jason Samse619de62012-05-08 18:40:58 -0700744 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800745 * Adds a new triangle to the mesh builder
746 *
747 * @param idx1 index of the first vertex in the triangle
748 * @param idx2 index of the second vertex in the triangle
749 * @param idx3 index of the third vertex in the triangle
750 *
751 * @return this
752 **/
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800753 public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) {
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800754 if((idx1 >= mMaxIndex) || (idx1 < 0) ||
755 (idx2 >= mMaxIndex) || (idx2 < 0) ||
756 (idx3 >= mMaxIndex) || (idx3 < 0)) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700757 throw new IllegalStateException("Index provided greater than vertex count.");
758 }
759 if ((mIndexCount + 3) >= mIndexData.length) {
760 short t[] = new short[mIndexData.length * 2];
761 System.arraycopy(mIndexData, 0, t, 0, mIndexData.length);
762 mIndexData = t;
763 }
764 mIndexData[mIndexCount++] = (short)idx1;
765 mIndexData[mIndexCount++] = (short)idx2;
766 mIndexData[mIndexCount++] = (short)idx3;
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800767 return this;
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700768 }
769
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700770 /**
Jason Samse619de62012-05-08 18:40:58 -0700771 * @deprecated in API 16
Alex Sakhartchoukdf272022011-01-09 11:34:03 -0800772 * Creates the mesh object from the current state of the builder
773 *
774 * @param uploadToBufferObject specifies whether the vertex data
775 * is to be uploaded into the buffer
776 * object indicating that it's likely
777 * not going to be modified and
778 * rendered many times.
779 * Alternatively, it indicates the
780 * mesh data will be updated
781 * frequently and remain in script
782 * accessible memory
783 *
784 **/
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700785 public Mesh create(boolean uploadToBufferObject) {
786 Element.Builder b = new Element.Builder(mRS);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700787 b.add(Element.createVector(mRS,
788 Element.DataType.FLOAT_32,
789 mVtxSize), "position");
790 if ((mFlags & COLOR) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700791 b.add(Element.F32_4(mRS), "color");
792 }
793 if ((mFlags & TEXTURE_0) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700794 b.add(Element.F32_2(mRS), "texture0");
795 }
796 if ((mFlags & NORMAL) != 0) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700797 b.add(Element.F32_3(mRS), "normal");
798 }
799 mElement = b.create();
800
Jason Samsd1952402010-12-20 12:55:28 -0800801 int usage = Allocation.USAGE_SCRIPT;
802 if (uploadToBufferObject) {
803 usage |= Allocation.USAGE_GRAPHICS_VERTEX;
804 }
805
806 Builder smb = new Builder(mRS, usage);
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800807 smb.addVertexType(mElement, mMaxIndex);
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800808 smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700809
810 Mesh sm = smb.create();
811
Alex Sakhartchouke60149d2011-11-15 15:15:21 -0800812 sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mMaxIndex, mVtxData);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700813 if(uploadToBufferObject) {
Andreas Gampe16720c12015-03-17 19:10:14 -0700814 sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700815 }
816
Jason Samsb97b2512011-01-16 15:04:08 -0800817 sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData);
Jason Samsd1952402010-12-20 12:55:28 -0800818 if (uploadToBufferObject) {
Alex Sakhartchoukb4d7bb62010-12-21 14:42:26 -0800819 sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
Jason Samsd1952402010-12-20 12:55:28 -0800820 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700821
822 return sm;
823 }
824 }
825}
826