Debian Codegraph @

rs/java/android/renderscript/Mesh.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2012 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.compat.annotation.UnsupportedAppUsage;
20 import android.os.Build;
21
22 import java.util.Vector;
23
24 /**
25 * @hide
26 * @deprecated in API 16
27 * <p>This class is a container for geometric data displayed with
28 * RenderScript. Internally, a mesh is a collection of allocations that
29 * represent vertex data (positions, normals, texture
30 * coordinates) and index data such as triangles and lines. </p>
31 * <p>
32 * Vertex data could either be interleaved within one
33 * allocation that is provided separately, as multiple allocation
34 * objects, or done as a combination of both. When a
35 * vertex channel name matches an input in the vertex program,
36 * RenderScript automatically connects the two together.
37 * </p>
38 * <p>
39 * Parts of the mesh can be rendered with either explicit
40 * index sets or primitive types.
41 * </p>
42 **/
43 @Deprecated
44 public class Mesh extends BaseObj {
45
46 /**
47 * @deprecated in API 16
48 * Describes the way mesh vertex data is interpreted when rendering
49 *
50 **/
51 public enum Primitive {
52 /**
53 * @deprecated in API 16
54 * Vertex data will be rendered as a series of points
55 */
56 @UnsupportedAppUsage
57 POINT (0),
58 /**
59 * @deprecated in API 16
60 * Vertex pairs will be rendered as lines
61 */
62 LINE (1),
63 /**
64 * @deprecated in API 16
65 * Vertex data will be rendered as a connected line strip
66 */
67 LINE_STRIP (2),
68 /**
69 * @deprecated in API 16
70 * Vertices will be rendered as individual triangles
71 */
72 @UnsupportedAppUsage
73 TRIANGLE (3),
74 /**
75 * @deprecated in API 16
76 * Vertices will be rendered as a connected triangle strip
77 * defined by the first three vertices with each additional
78 * triangle defined by a new vertex
79 */
80 TRIANGLE_STRIP (4),
81 /**
82 * @deprecated in API 16
83 * Vertices will be rendered as a sequence of triangles that all
84 * share first vertex as the origin
85 */
86 TRIANGLE_FAN (5);
87
88 int mID;
89 Primitive(int id) {
90 mID = id;
91 }
92 }
93
94 Allocation[] mVertexBuffers;
95 Allocation[] mIndexBuffers;
96 Primitive[] mPrimitives;
97
98 Mesh(long id, RenderScript rs) {
99 super(id, rs);
100 guard.open("destroy");
101 }
102
103 /**
104 * @deprecated in API 16
105 * @return number of allocations containing vertex data
106 *
107 **/
108 public int getVertexAllocationCount() {
109 if(mVertexBuffers == null) {
110 return 0;
111 }
112 return mVertexBuffers.length;
113 }
114 /**
115 * @deprecated in API 16
116 * @param slot index in the list of allocations to return
117 * @return vertex data allocation at the given index
118 *
119 **/
120 @UnsupportedAppUsage
121 public Allocation getVertexAllocation(int slot) {
122 return mVertexBuffers[slot];
123 }
124
125 /**
126 * @deprecated in API 16
127 * @return number of primitives or index sets in the mesh
128 *
129 **/
130 public int getPrimitiveCount() {
131 if(mIndexBuffers == null) {
132 return 0;
133 }
134 return mIndexBuffers.length;
135 }
136
137 /**
138 * @deprecated in API 16
139 * @param slot locaton within the list of index set allocation
140 * @return allocation containing primtive index data or null if
141 * the index data is not specified explicitly
142 *
143 **/
144 public Allocation getIndexSetAllocation(int slot) {
145 return mIndexBuffers[slot];
146 }
147 /**
148 * @deprecated in API 16
149 * @param slot locaiton within the list of index set primitives
150 * @return index set primitive type
151 *
152 **/
153 public Primitive getPrimitive(int slot) {
154 return mPrimitives[slot];
155 }
156
157 @Override
158 void updateFromNative() {
159 super.updateFromNative();
160 int vtxCount = mRS.nMeshGetVertexBufferCount(getID(mRS));
161 int idxCount = mRS.nMeshGetIndexCount(getID(mRS));
162
163 long[] vtxIDs = new long[vtxCount];
164 long[] idxIDs = new long[idxCount];
165 int[] primitives = new int[idxCount];
166
167 mRS.nMeshGetVertices(getID(mRS), vtxIDs, vtxCount);
168 mRS.nMeshGetIndices(getID(mRS), idxIDs, primitives, idxCount);
169
170 mVertexBuffers = new Allocation[vtxCount];
171 mIndexBuffers = new Allocation[idxCount];
172 mPrimitives = new Primitive[idxCount];
173
174 for(int i = 0; i < vtxCount; i ++) {
175 if(vtxIDs[i] != 0) {
176 mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
177 mVertexBuffers[i].updateFromNative();
178 }
179 }
180
181 for(int i = 0; i < idxCount; i ++) {
182 if(idxIDs[i] != 0) {
183 mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null, Allocation.USAGE_SCRIPT);
184 mIndexBuffers[i].updateFromNative();
185 }
186 mPrimitives[i] = Primitive.values()[primitives[i]];
187 }
188 }
189
190 /**
191 * @deprecated in API 16
192 * Mesh builder object. It starts empty and requires you to
193 * add the types necessary to create vertex and index
194 * allocations.
195 *
196 */
197 public static class Builder {
198 RenderScript mRS;
199 int mUsage;
200
201 class Entry {
202 Type t;
203 Element e;
204 int size;
205 Primitive prim;
206 int usage;
207 }
208
209 int mVertexTypeCount;
210 Entry[] mVertexTypes;
211 Vector mIndexTypes;
212
213 /**
214 * @deprecated in API 16
215 * Creates builder object
216 * @param rs Context to which the mesh will belong.
217 * @param usage specifies how the mesh allocations are to be
218 * handled, whether they need to be uploaded to a
219 * buffer on the gpu, maintain a cpu copy, etc
220 */
221 public Builder(RenderScript rs, int usage) {
222 mRS = rs;
223 mUsage = usage;
224 mVertexTypeCount = 0;
225 mVertexTypes = new Entry[16];
226 mIndexTypes = new Vector();
227 }
228
229 /**
230 * @deprecated in API 16
231 * @return internal index of the last vertex buffer type added to
232 * builder
233 **/
234 public int getCurrentVertexTypeIndex() {
235 return mVertexTypeCount - 1;
236 }
237
238 /**
239 * @deprecated in API 16
240 * @return internal index of the last index set added to the
241 * builder
242 **/
243 public int getCurrentIndexSetIndex() {
244 return mIndexTypes.size() - 1;
245 }
246
247 /**
248 * @deprecated in API 16
249 * Adds a vertex data type to the builder object
250 *
251 * @param t type of the vertex data allocation to be created
252 *
253 * @return this
254 **/
255 public Builder addVertexType(Type t) throws IllegalStateException {
256 if (mVertexTypeCount >= mVertexTypes.length) {
257 throw new IllegalStateException("Max vertex types exceeded.");
258 }
259
260 mVertexTypes[mVertexTypeCount] = new Entry();
261 mVertexTypes[mVertexTypeCount].t = t;
262 mVertexTypes[mVertexTypeCount].e = null;
263 mVertexTypeCount++;
264 return this;
265 }
266
267 /**
268 * @deprecated in API 16
269 * Adds a vertex data type to the builder object
270 *
271 * @param e element describing the vertex data layout
272 * @param size number of elements in the buffer
273 *
274 * @return this
275 **/
276 public Builder addVertexType(Element e, int size) throws IllegalStateException {
277 if (mVertexTypeCount >= mVertexTypes.length) {
278 throw new IllegalStateException("Max vertex types exceeded.");
279 }
280
281 mVertexTypes[mVertexTypeCount] = new Entry();
282 mVertexTypes[mVertexTypeCount].t = null;
283 mVertexTypes[mVertexTypeCount].e = e;
284 mVertexTypes[mVertexTypeCount].size = size;
285 mVertexTypeCount++;
286 return this;
287 }
288
289 /**
290 * @deprecated in API 16
291 * Adds an index set data type to the builder object
292 *
293 * @param t type of the index set data, could be null
294 * @param p primitive type
295 *
296 * @return this
297 **/
298 public Builder addIndexSetType(Type t, Primitive p) {
299 Entry indexType = new Entry();
300 indexType.t = t;
301 indexType.e = null;
302 indexType.size = 0;
303 indexType.prim = p;
304 mIndexTypes.addElement(indexType);
305 return this;
306 }
307
308 /**
309 * @deprecated in API 16
310 * Adds an index set primitive type to the builder object
311 *
312 * @param p primitive type
313 *
314 * @return this
315 **/
316 public Builder addIndexSetType(Primitive p) {
317 Entry indexType = new Entry();
318 indexType.t = null;
319 indexType.e = null;
320 indexType.size = 0;
321 indexType.prim = p;
322 mIndexTypes.addElement(indexType);
323 return this;
324 }
325
326 /**
327 * @deprecated in API 16
328 * Adds an index set data type to the builder object
329 *
330 * @param e element describing the index set data layout
331 * @param size number of elements in the buffer
332 * @param p primitive type
333 *
334 * @return this
335 **/
336 public Builder addIndexSetType(Element e, int size, Primitive p) {
337 Entry indexType = new Entry();
338 indexType.t = null;
339 indexType.e = e;
340 indexType.size = size;
341 indexType.prim = p;
342 mIndexTypes.addElement(indexType);
343 return this;
344 }
345
346 Type newType(Element e, int size) {
347 Type.Builder tb = new Type.Builder(mRS, e);
348 tb.setX(size);
349 return tb.create();
350 }
351
352 /**
353 * @deprecated in API 16
354 * Create a Mesh object from the current state of the builder
355 *
356 **/
357 public Mesh create() {
358 mRS.validate();
359 long[] vtx = new long[mVertexTypeCount];
360 long[] idx = new long[mIndexTypes.size()];
361 int[] prim = new int[mIndexTypes.size()];
362
363 Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
364 Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
365 Primitive[] primitives = new Primitive[mIndexTypes.size()];
366
367 for(int ct = 0; ct < mVertexTypeCount; ct ++) {
368 Allocation alloc = null;
369 Entry entry = mVertexTypes[ct];
370 if (entry.t != null) {
371 alloc = Allocation.createTyped(mRS, entry.t, mUsage);
372 } else if(entry.e != null) {
373 alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
374 } else {
375 // Should never happen because the builder will always set one
376 throw new IllegalStateException("Builder corrupt, no valid element in entry.");
377 }
378 vertexBuffers[ct] = alloc;
379 vtx[ct] = alloc.getID(mRS);
380 }
381
382 for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
383 Allocation alloc = null;
384 Entry entry = (Entry)mIndexTypes.elementAt(ct);
385 if (entry.t != null) {
386 alloc = Allocation.createTyped(mRS, entry.t, mUsage);
387 } else if(entry.e != null) {
388 alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
389 } else {
390 // Should never happen because the builder will always set one
391 throw new IllegalStateException("Builder corrupt, no valid element in entry.");
392 }
393 long allocID = (alloc == null) ? 0 : alloc.getID(mRS);
394 indexBuffers[ct] = alloc;
395 primitives[ct] = entry.prim;
396
397 idx[ct] = allocID;
398 prim[ct] = entry.prim.mID;
399 }
400
401 long id = mRS.nMeshCreate(vtx, idx, prim);
402 Mesh newMesh = new Mesh(id, mRS);
403 newMesh.mVertexBuffers = vertexBuffers;
404 newMesh.mIndexBuffers = indexBuffers;
405 newMesh.mPrimitives = primitives;
406
407 return newMesh;
408 }
409 }
410
411 /**
412 * @deprecated in API 16
413 * Mesh builder object. It starts empty and requires the user to
414 * add all the vertex and index allocations that comprise the
415 * mesh
416 *
417 */
418 public static class AllocationBuilder {
419 RenderScript mRS;
420
421 class Entry {
422 Allocation a;
423 Primitive prim;
424 }
425
426 int mVertexTypeCount;
427 Entry[] mVertexTypes;
428
429 Vector mIndexTypes;
430
431 /**
432 * @deprecated in API 16
433 **/
434 @UnsupportedAppUsage
435 public AllocationBuilder(RenderScript rs) {
436 mRS = rs;
437 mVertexTypeCount = 0;
438 mVertexTypes = new Entry[16];
439 mIndexTypes = new Vector();
440 }
441
442 /**
443 * @deprecated in API 16
444 * @return internal index of the last vertex buffer type added to
445 * builder
446 **/
447 public int getCurrentVertexTypeIndex() {
448 return mVertexTypeCount - 1;
449 }
450
451 /**
452 * @deprecated in API 16
453 * @return internal index of the last index set added to the
454 * builder
455 **/
456 public int getCurrentIndexSetIndex() {
457 return mIndexTypes.size() - 1;
458 }
459
460 /**
461 * @deprecated in API 16
462 * Adds an allocation containing vertex buffer data to the
463 * builder
464 *
465 * @param a vertex data allocation
466 *
467 * @return this
468 **/
469 @UnsupportedAppUsage
470 public AllocationBuilder addVertexAllocation(Allocation a) throws IllegalStateException {
471 if (mVertexTypeCount >= mVertexTypes.length) {
472 throw new IllegalStateException("Max vertex types exceeded.");
473 }
474
475 mVertexTypes[mVertexTypeCount] = new Entry();
476 mVertexTypes[mVertexTypeCount].a = a;
477 mVertexTypeCount++;
478 return this;
479 }
480
481 /**
482 * @deprecated in API 16
483 * Adds an allocation containing index buffer data and index type
484 * to the builder
485 *
486 * @param a index set data allocation, could be null
487 * @param p index set primitive type
488 *
489 * @return this
490 **/
491 @UnsupportedAppUsage
492 public AllocationBuilder addIndexSetAllocation(Allocation a, Primitive p) {
493 Entry indexType = new Entry();
494 indexType.a = a;
495 indexType.prim = p;
496 mIndexTypes.addElement(indexType);
497 return this;
498 }
499
500 /**
501 * @deprecated in API 16
502 * Adds an index set type to the builder
503 *
504 * @param p index set primitive type
505 *
506 * @return this
507 **/
508 @UnsupportedAppUsage
509 public AllocationBuilder addIndexSetType(Primitive p) {
510 Entry indexType = new Entry();
511 indexType.a = null;
512 indexType.prim = p;
513 mIndexTypes.addElement(indexType);
514 return this;
515 }
516
517 /**
518 * @deprecated in API 16
519 * Create a Mesh object from the current state of the builder
520 *
521 **/
522 @UnsupportedAppUsage
523 public Mesh create() {
524 mRS.validate();
525
526 long[] vtx = new long[mVertexTypeCount];
527 long[] idx = new long[mIndexTypes.size()];
528 int[] prim = new int[mIndexTypes.size()];
529
530 Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
531 Primitive[] primitives = new Primitive[mIndexTypes.size()];
532 Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
533
534 for(int ct = 0; ct < mVertexTypeCount; ct ++) {
535 Entry entry = mVertexTypes[ct];
536 vertexBuffers[ct] = entry.a;
537 vtx[ct] = entry.a.getID(mRS);
538 }
539
540 for(int ct = 0; ct < mIndexTypes.size(); ct ++) {
541 Entry entry = (Entry)mIndexTypes.elementAt(ct);
542 long allocID = (entry.a == null) ? 0 : entry.a.getID(mRS);
543 indexBuffers[ct] = entry.a;
544 primitives[ct] = entry.prim;
545
546 idx[ct] = allocID;
547 prim[ct] = entry.prim.mID;
548 }
549
550 long id = mRS.nMeshCreate(vtx, idx, prim);
551 Mesh newMesh = new Mesh(id, mRS);
552 newMesh.mVertexBuffers = vertexBuffers;
553 newMesh.mIndexBuffers = indexBuffers;
554 newMesh.mPrimitives = primitives;
555
556 return newMesh;
557 }
558 }
559
560 /**
561 * @deprecated in API 16
562 * Builder that allows creation of a mesh object point by point
563 * and triangle by triangle
564 *
565 **/
566 public static class TriangleMeshBuilder {
567 float mVtxData[];
568 int mVtxCount;
569 int mMaxIndex;
570 short mIndexData[];
571 int mIndexCount;
572 RenderScript mRS;
573 Element mElement;
574
575 float mNX = 0;
576 float mNY = 0;
577 float mNZ = -1;
578 float mS0 = 0;
579 float mT0 = 0;
580 float mR = 1;
581 float mG = 1;
582 float mB = 1;
583 float mA = 1;
584
585 int mVtxSize;
586 int mFlags;
587
588 /**
589 * @deprecated in API 16
590 **/
591 public static final int COLOR = 0x0001;
592 /**
593 * @deprecated in API 16
594 **/
595 public static final int NORMAL = 0x0002;
596 /**
597 * @deprecated in API 16
598 **/
599 public static final int TEXTURE_0 = 0x0100;
600
601 /**
602 * @deprecated in API 16
603 * @param rs Context to which the mesh will belong.
604 * @param vtxSize specifies whether the vertex is a float2 or
605 * float3
606 * @param flags bitfield that is a combination of COLOR, NORMAL,
607 * and TEXTURE_0 that specifies what vertex data
608 * channels are present in the mesh
609 *
610 **/
611 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
612 public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) {
613 mRS = rs;
614 mVtxCount = 0;
615 mMaxIndex = 0;
616 mIndexCount = 0;
617 mVtxData = new float[128];
618 mIndexData = new short[128];
619 mVtxSize = vtxSize;
620 mFlags = flags;
621
622 if (vtxSize < 2 || vtxSize > 3) {
623 throw new IllegalArgumentException("Vertex size out of range.");
624 }
625 }
626
627 private void makeSpace(int count) {
628 if ((mVtxCount + count) >= mVtxData.length) {
629 float t[] = new float[mVtxData.length * 2];
630 System.arraycopy(mVtxData, 0, t, 0, mVtxData.length);
631 mVtxData = t;
632 }
633 }
634
635 private void latch() {
636 if ((mFlags & COLOR) != 0) {
637 makeSpace(4);
638 mVtxData[mVtxCount++] = mR;
639 mVtxData[mVtxCount++] = mG;
640 mVtxData[mVtxCount++] = mB;
641 mVtxData[mVtxCount++] = mA;
642 }
643 if ((mFlags & TEXTURE_0) != 0) {
644 makeSpace(2);
645 mVtxData[mVtxCount++] = mS0;
646 mVtxData[mVtxCount++] = mT0;
647 }
648 if ((mFlags & NORMAL) != 0) {
649 makeSpace(4);
650 mVtxData[mVtxCount++] = mNX;
651 mVtxData[mVtxCount++] = mNY;
652 mVtxData[mVtxCount++] = mNZ;
653 mVtxData[mVtxCount++] = 0.0f;
654 }
655 mMaxIndex ++;
656 }
657
658 /**
659 * @deprecated in API 16
660 * Adds a float2 vertex to the mesh
661 *
662 * @param x position x
663 * @param y position y
664 *
665 * @return this
666 *
667 **/
668 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
669 public TriangleMeshBuilder addVertex(float x, float y) {
670 if (mVtxSize != 2) {
671 throw new IllegalStateException("add mistmatch with declared components.");
672 }
673 makeSpace(2);
674 mVtxData[mVtxCount++] = x;
675 mVtxData[mVtxCount++] = y;
676 latch();
677 return this;
678 }
679
680 /**
681 * @deprecated in API 16
682 * Adds a float3 vertex to the mesh
683 *
684 * @param x position x
685 * @param y position y
686 * @param z position z
687 *
688 * @return this
689 *
690 **/
691 public TriangleMeshBuilder addVertex(float x, float y, float z) {
692 if (mVtxSize != 3) {
693 throw new IllegalStateException("add mistmatch with declared components.");
694 }
695 makeSpace(4);
696 mVtxData[mVtxCount++] = x;
697 mVtxData[mVtxCount++] = y;
698 mVtxData[mVtxCount++] = z;
699 mVtxData[mVtxCount++] = 1.0f;
700 latch();
701 return this;
702 }
703
704 /**
705 * @deprecated in API 16
706 * Sets the texture coordinate for the vertices that are added after this method call.
707 *
708 * @param s texture coordinate s
709 * @param t texture coordinate t
710 *
711 * @return this
712 **/
713 public TriangleMeshBuilder setTexture(float s, float t) {
714 if ((mFlags & TEXTURE_0) == 0) {
715 throw new IllegalStateException("add mistmatch with declared components.");
716 }
717 mS0 = s;
718 mT0 = t;
719 return this;
720 }
721
722 /**
723 * @deprecated in API 16
724 * Sets the normal vector for the vertices that are added after this method call.
725 *
726 * @param x normal vector x
727 * @param y normal vector y
728 * @param z normal vector z
729 *
730 * @return this
731 **/
732 public TriangleMeshBuilder setNormal(float x, float y, float z) {
733 if ((mFlags & NORMAL) == 0) {
734 throw new IllegalStateException("add mistmatch with declared components.");
735 }
736 mNX = x;
737 mNY = y;
738 mNZ = z;
739 return this;
740 }
741
742 /**
743 * @deprecated in API 16
744 * Sets the color for the vertices that are added after this method call.
745 *
746 * @param r red component
747 * @param g green component
748 * @param b blue component
749 * @param a alpha component
750 *
751 * @return this
752 **/
753 public TriangleMeshBuilder setColor(float r, float g, float b, float a) {
754 if ((mFlags & COLOR) == 0) {
755 throw new IllegalStateException("add mistmatch with declared components.");
756 }
757 mR = r;
758 mG = g;
759 mB = b;
760 mA = a;
761 return this;
762 }
763
764 /**
765 * @deprecated in API 16
766 * Adds a new triangle to the mesh builder
767 *
768 * @param idx1 index of the first vertex in the triangle
769 * @param idx2 index of the second vertex in the triangle
770 * @param idx3 index of the third vertex in the triangle
771 *
772 * @return this
773 **/
774 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
775 public TriangleMeshBuilder addTriangle(int idx1, int idx2, int idx3) {
776 if((idx1 >= mMaxIndex) || (idx1 < 0) ||
777 (idx2 >= mMaxIndex) || (idx2 < 0) ||
778 (idx3 >= mMaxIndex) || (idx3 < 0)) {
779 throw new IllegalStateException("Index provided greater than vertex count.");
780 }
781 if ((mIndexCount + 3) >= mIndexData.length) {
782 short t[] = new short[mIndexData.length * 2];
783 System.arraycopy(mIndexData, 0, t, 0, mIndexData.length);
784 mIndexData = t;
785 }
786 mIndexData[mIndexCount++] = (short)idx1;
787 mIndexData[mIndexCount++] = (short)idx2;
788 mIndexData[mIndexCount++] = (short)idx3;
789 return this;
790 }
791
792 /**
793 * @deprecated in API 16
794 * Creates the mesh object from the current state of the builder
795 *
796 * @param uploadToBufferObject specifies whether the vertex data
797 * is to be uploaded into the buffer
798 * object indicating that it's likely
799 * not going to be modified and
800 * rendered many times.
801 * Alternatively, it indicates the
802 * mesh data will be updated
803 * frequently and remain in script
804 * accessible memory
805 *
806 **/
807 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
808 public Mesh create(boolean uploadToBufferObject) {
809 Element.Builder b = new Element.Builder(mRS);
810 b.add(Element.createVector(mRS,
811 Element.DataType.FLOAT_32,
812 mVtxSize), "position");
813 if ((mFlags & COLOR) != 0) {
814 b.add(Element.F32_4(mRS), "color");
815 }
816 if ((mFlags & TEXTURE_0) != 0) {
817 b.add(Element.F32_2(mRS), "texture0");
818 }
819 if ((mFlags & NORMAL) != 0) {
820 b.add(Element.F32_3(mRS), "normal");
821 }
822 mElement = b.create();
823
824 int usage = Allocation.USAGE_SCRIPT;
825 if (uploadToBufferObject) {
826 usage |= Allocation.USAGE_GRAPHICS_VERTEX;
827 }
828
829 Builder smb = new Builder(mRS, usage);
830 smb.addVertexType(mElement, mMaxIndex);
831 smb.addIndexSetType(Element.U16(mRS), mIndexCount, Primitive.TRIANGLE);
832
833 Mesh sm = smb.create();
834
835 sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mMaxIndex, mVtxData);
836 if(uploadToBufferObject) {
837 sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
838 }
839
840 sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData);
841 if (uploadToBufferObject) {
842 sm.getIndexSetAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
843 }
844
845 return sm;
846 }
847 }
848 }
849