blob: baf4c5319074bcbafd443073f3e985b2885a50a1 [file] [log] [blame]
Jason Samsa89371c2009-06-30 14:13:04 -07001/*
2 * Copyright (C) 2009 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
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsa89371c2009-06-30 14:13:04 -070018#include "rsContext.h"
19
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070020#include <GLES/gl.h>
21#include <GLES2/gl2.h>
22#include <GLES/glext.h>
23#else
24#include "rsContextHostStub.h"
25
26#include <OpenGL/gl.h>
27#include <OpenGl/glext.h>
28#endif
29
Jason Samsa89371c2009-06-30 14:13:04 -070030using namespace android;
31using namespace android::renderscript;
32
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080033Mesh::Mesh(Context *rsc) : ObjectBase(rsc) {
Jason Samsa89371c2009-06-30 14:13:04 -070034 mPrimitives = NULL;
Jason Samsa5597fc2009-07-08 18:01:53 -070035 mPrimitivesCount = 0;
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -070036 mVertexBuffers = NULL;
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -070037 mVertexBufferCount = 0;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080038 mAttribs = NULL;
39 mAttribAllocationIndex = NULL;
40
41 mAttribCount = 0;
Jason Samsa89371c2009-06-30 14:13:04 -070042}
43
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080044Mesh::~Mesh() {
45 if (mVertexBuffers) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -070046 delete[] mVertexBuffers;
47 }
48
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080049 if (mPrimitives) {
50 for (uint32_t i = 0; i < mPrimitivesCount; i ++) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -070051 delete mPrimitives[i];
52 }
53 delete[] mPrimitives;
54 }
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080055
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080056 if (mAttribs) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080057 delete[] mAttribs;
58 delete[] mAttribAllocationIndex;
59 }
60}
61
62bool Mesh::isValidGLComponent(const Element *elem, uint32_t fieldIdx) {
63 // Do not create attribs for padding
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080064 if (elem->getFieldName(fieldIdx)[0] == '#') {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080065 return false;
66 }
67
68 // Only GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FIXED, GL_FLOAT are accepted.
69 // Filter rs types accordingly
70 RsDataType dt = elem->getField(fieldIdx)->getComponent().getType();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080071 if (dt != RS_TYPE_FLOAT_32 && dt != RS_TYPE_UNSIGNED_8 &&
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080072 dt != RS_TYPE_UNSIGNED_16 && dt != RS_TYPE_SIGNED_8 &&
73 dt != RS_TYPE_SIGNED_16) {
74 return false;
75 }
76
77 // Now make sure they are not arrays
78 uint32_t arraySize = elem->getFieldArraySize(fieldIdx);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080079 if (arraySize != 1) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080080 return false;
81 }
82
83 return true;
84}
85
86void Mesh::initVertexAttribs() {
87 // Count the number of gl attrs to initialize
88 mAttribCount = 0;
89 for (uint32_t ct=0; ct < mVertexBufferCount; ct++) {
90 const Element *elem = mVertexBuffers[ct]->getType()->getElement();
91 for (uint32_t ct=0; ct < elem->getFieldCount(); ct++) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080092 if (isValidGLComponent(elem, ct)) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080093 mAttribCount ++;
94 }
95 }
96 }
97
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080098 if (mAttribs) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080099 delete [] mAttribs;
100 delete [] mAttribAllocationIndex;
101 mAttribs = NULL;
102 mAttribAllocationIndex = NULL;
103 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800104 if (!mAttribCount) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800105 return;
106 }
107
108 mAttribs = new VertexArray::Attrib[mAttribCount];
109 mAttribAllocationIndex = new uint32_t[mAttribCount];
110
111 uint32_t userNum = 0;
112 for (uint32_t ct=0; ct < mVertexBufferCount; ct++) {
113 const Element *elem = mVertexBuffers[ct]->getType()->getElement();
114 uint32_t stride = elem->getSizeBytes();
115 for (uint32_t fieldI=0; fieldI < elem->getFieldCount(); fieldI++) {
116 const Component &c = elem->getField(fieldI)->getComponent();
117
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800118 if (!isValidGLComponent(elem, fieldI)) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800119 continue;
120 }
121
122 mAttribs[userNum].size = c.getVectorSize();
123 mAttribs[userNum].offset = elem->getFieldOffsetBytes(fieldI);
124 mAttribs[userNum].type = c.getGLType();
125 mAttribs[userNum].normalized = c.getType() != RS_TYPE_FLOAT_32;//c.getIsNormalized();
126 mAttribs[userNum].stride = stride;
127 String8 tmp(RS_SHADER_ATTR);
128 tmp.append(elem->getFieldName(fieldI));
129 mAttribs[userNum].name.setTo(tmp.string());
130
131 // Remember which allocation this attribute came from
132 mAttribAllocationIndex[userNum] = ct;
133 userNum ++;
134 }
135 }
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700136}
137
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138void Mesh::render(Context *rsc) const {
139 for (uint32_t ct = 0; ct < mPrimitivesCount; ct ++) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700140 renderPrimitive(rsc, ct);
141 }
142}
143
144void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const {
145 if (primIndex >= mPrimitivesCount) {
146 LOGE("Invalid primitive index");
147 return;
148 }
149
150 Primitive_t *prim = mPrimitives[primIndex];
151
152 if (prim->mIndexBuffer.get()) {
153 renderPrimitiveRange(rsc, primIndex, 0, prim->mIndexBuffer->getType()->getDimX());
154 return;
155 }
156
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700157 renderPrimitiveRange(rsc, primIndex, 0, mVertexBuffers[0]->getType()->getDimX());
158}
159
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800160void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800161 if (len < 1 || primIndex >= mPrimitivesCount || mAttribCount == 0) {
162 LOGE("Invalid mesh or parameters");
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700163 return;
164 }
165
166 rsc->checkError("Mesh::renderPrimitiveRange 1");
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700167 for (uint32_t ct=0; ct < mVertexBufferCount; ct++) {
168 mVertexBuffers[ct]->uploadCheck(rsc);
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700169 }
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800170 // update attributes with either buffer information or data ptr based on their current state
171 for (uint32_t ct=0; ct < mAttribCount; ct++) {
172 uint32_t allocIndex = mAttribAllocationIndex[ct];
173 Allocation *alloc = mVertexBuffers[allocIndex].get();
174 if (alloc->getIsBufferObject()) {
175 mAttribs[ct].buffer = alloc->getBufferObjectID();
176 mAttribs[ct].ptr = NULL;
177 } else {
178 mAttribs[ct].buffer = 0;
179 mAttribs[ct].ptr = (const uint8_t*)alloc->getPtr();
180 }
181 }
182
183 VertexArray va(mAttribs, mAttribCount);
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700184 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
185
186 rsc->checkError("Mesh::renderPrimitiveRange 2");
187 Primitive_t *prim = mPrimitives[primIndex];
188 if (prim->mIndexBuffer.get()) {
189 prim->mIndexBuffer->uploadCheck(rsc);
190 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prim->mIndexBuffer->getBufferObjectID());
191 glDrawElements(prim->mGLPrimitive, len, GL_UNSIGNED_SHORT, (uint16_t *)(start * 2));
192 } else {
193 glDrawArrays(prim->mGLPrimitive, start, len);
194 }
195
196 rsc->checkError("Mesh::renderPrimitiveRange");
197}
198
199
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800200void Mesh::uploadAll(Context *rsc) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700201 for (uint32_t ct = 0; ct < mVertexBufferCount; ct ++) {
202 if (mVertexBuffers[ct].get()) {
203 mVertexBuffers[ct]->deferedUploadToBufferObject(rsc);
204 }
205 }
206
207 for (uint32_t ct = 0; ct < mPrimitivesCount; ct ++) {
208 if (mPrimitives[ct]->mIndexBuffer.get()) {
209 mPrimitives[ct]->mIndexBuffer->deferedUploadToBufferObject(rsc);
210 }
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700211 }
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700212}
213
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800214void Mesh::updateGLPrimitives() {
215 for (uint32_t i = 0; i < mPrimitivesCount; i ++) {
216 switch (mPrimitives[i]->mPrimitive) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700217 case RS_PRIMITIVE_POINT: mPrimitives[i]->mGLPrimitive = GL_POINTS; break;
218 case RS_PRIMITIVE_LINE: mPrimitives[i]->mGLPrimitive = GL_LINES; break;
219 case RS_PRIMITIVE_LINE_STRIP: mPrimitives[i]->mGLPrimitive = GL_LINE_STRIP; break;
220 case RS_PRIMITIVE_TRIANGLE: mPrimitives[i]->mGLPrimitive = GL_TRIANGLES; break;
221 case RS_PRIMITIVE_TRIANGLE_STRIP: mPrimitives[i]->mGLPrimitive = GL_TRIANGLE_STRIP; break;
222 case RS_PRIMITIVE_TRIANGLE_FAN: mPrimitives[i]->mGLPrimitive = GL_TRIANGLE_FAN; break;
223 }
224 }
Jason Samsa89371c2009-06-30 14:13:04 -0700225}
226
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800227void Mesh::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700228 // Need to identify ourselves
229 stream->addU32((uint32_t)getClassId());
230
231 String8 name(getName());
232 stream->addString(&name);
233
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700234 // Store number of vertex streams
235 stream->addU32(mVertexBufferCount);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800236 for (uint32_t vCount = 0; vCount < mVertexBufferCount; vCount ++) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700237 mVertexBuffers[vCount]->serialize(stream);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700238 }
239
240 stream->addU32(mPrimitivesCount);
241 // Store the primitives
242 for (uint32_t pCount = 0; pCount < mPrimitivesCount; pCount ++) {
243 Primitive_t * prim = mPrimitives[pCount];
244
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700245 stream->addU8((uint8_t)prim->mPrimitive);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700246
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800247 if (prim->mIndexBuffer.get()) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700248 stream->addU32(1);
249 prim->mIndexBuffer->serialize(stream);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800250 } else {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700251 stream->addU32(0);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700252 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700253 }
254}
255
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800256Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700257 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700258 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800259 if (classID != RS_A3D_CLASS_ID_MESH) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700260 LOGE("mesh loading skipped due to invalid class id");
261 return NULL;
262 }
263
264 Mesh * mesh = new Mesh(rsc);
265
266 String8 name;
267 stream->loadString(&name);
268 mesh->setName(name.string(), name.size());
269
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700270 mesh->mVertexBufferCount = stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800271 if (mesh->mVertexBufferCount) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700272 mesh->mVertexBuffers = new ObjectBaseRef<Allocation>[mesh->mVertexBufferCount];
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700273
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800274 for (uint32_t vCount = 0; vCount < mesh->mVertexBufferCount; vCount ++) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700275 Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream);
276 mesh->mVertexBuffers[vCount].set(vertexAlloc);
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700277 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700278 }
279
280 mesh->mPrimitivesCount = stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800281 if (mesh->mPrimitivesCount) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700282 mesh->mPrimitives = new Primitive_t *[mesh->mPrimitivesCount];
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700283
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700284 // load all primitives
285 for (uint32_t pCount = 0; pCount < mesh->mPrimitivesCount; pCount ++) {
286 Primitive_t * prim = new Primitive_t;
287 mesh->mPrimitives[pCount] = prim;
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700288
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700289 prim->mPrimitive = (RsPrimitive)stream->loadU8();
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700290
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700291 // Check to see if the index buffer was stored
292 uint32_t isIndexPresent = stream->loadU32();
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800293 if (isIndexPresent) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700294 Allocation *indexAlloc = Allocation::createFromStream(rsc, stream);
295 prim->mIndexBuffer.set(indexAlloc);
296 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700297 }
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700298 }
299
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700300 mesh->updateGLPrimitives();
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800301 mesh->initVertexAttribs();
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700302 mesh->uploadAll(rsc);
303
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700304 return mesh;
305}
Jason Samsa89371c2009-06-30 14:13:04 -0700306
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700307void Mesh::computeBBox() {
308 float *posPtr = NULL;
309 uint32_t vectorSize = 0;
310 uint32_t stride = 0;
311 uint32_t numVerts = 0;
312 // First we need to find the position ptr and stride
313 for (uint32_t ct=0; ct < mVertexBufferCount; ct++) {
314 const Type *bufferType = mVertexBuffers[ct]->getType();
315 const Element *bufferElem = bufferType->getElement();
316
317 for (uint32_t ct=0; ct < bufferElem->getFieldCount(); ct++) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800318 if (strcmp(bufferElem->getFieldName(ct), "position") == 0) {
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700319 vectorSize = bufferElem->getField(ct)->getComponent().getVectorSize();
320 stride = bufferElem->getSizeBytes() / sizeof(float);
321 uint32_t offset = bufferElem->getFieldOffsetBytes(ct);
322 posPtr = (float*)((uint8_t*)mVertexBuffers[ct]->getPtr() + offset);
323 numVerts = bufferType->getDimX();
324 break;
325 }
326 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800327 if (posPtr) {
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700328 break;
329 }
330 }
331
332 mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 1e6;
333 mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = -1e6;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800334 if (!posPtr) {
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700335 LOGE("Unable to compute bounding box");
336 mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 0.0f;
337 mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = 0.0f;
338 return;
339 }
340
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800341 for (uint32_t i = 0; i < numVerts; i ++) {
342 for (uint32_t v = 0; v < vectorSize; v ++) {
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700343 mBBoxMin[v] = rsMin(mBBoxMin[v], posPtr[v]);
344 mBBoxMax[v] = rsMax(mBBoxMax[v], posPtr[v]);
345 }
346 posPtr += stride;
347 }
348}
349
Jason Samsa89371c2009-06-30 14:13:04 -0700350
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800351MeshContext::MeshContext() {
Jason Samsa89371c2009-06-30 14:13:04 -0700352}
353
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800354MeshContext::~MeshContext() {
Jason Samsa89371c2009-06-30 14:13:04 -0700355}
356
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700357namespace android {
358namespace renderscript {
359
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800360RsMesh rsi_MeshCreate(Context *rsc, uint32_t vtxCount, uint32_t idxCount) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700361 Mesh *sm = new Mesh(rsc);
362 sm->incUserRef();
363
364 sm->mPrimitivesCount = idxCount;
365 sm->mPrimitives = new Mesh::Primitive_t *[sm->mPrimitivesCount];
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800366 for (uint32_t ct = 0; ct < idxCount; ct ++) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700367 sm->mPrimitives[ct] = new Mesh::Primitive_t;
368 }
369
370 sm->mVertexBufferCount = vtxCount;
371 sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount];
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700372
373 return sm;
374}
375
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800376void rsi_MeshBindVertex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t slot) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700377 Mesh *sm = static_cast<Mesh *>(mv);
378 rsAssert(slot < sm->mVertexBufferCount);
379
380 sm->mVertexBuffers[slot].set((Allocation *)va);
381}
382
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800383void rsi_MeshBindIndex(Context *rsc, RsMesh mv, RsAllocation va, uint32_t primType, uint32_t slot) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700384 Mesh *sm = static_cast<Mesh *>(mv);
385 rsAssert(slot < sm->mPrimitivesCount);
386
387 sm->mPrimitives[slot]->mIndexBuffer.set((Allocation *)va);
388 sm->mPrimitives[slot]->mPrimitive = (RsPrimitive)primType;
389 sm->updateGLPrimitives();
390}
391
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800392void rsi_MeshInitVertexAttribs(Context *rsc, RsMesh mv) {
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800393 Mesh *sm = static_cast<Mesh *>(mv);
394 sm->initVertexAttribs();
395}
396
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700397}}
398
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800399void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700400 Mesh *sm = static_cast<Mesh *>(mv);
401 *numVtx = sm->mVertexBufferCount;
402}
403
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800404void rsaMeshGetIndexCount(RsContext con, RsMesh mv, int32_t *numIdx) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700405 Mesh *sm = static_cast<Mesh *>(mv);
406 *numIdx = sm->mPrimitivesCount;
407}
408
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800409void rsaMeshGetVertices(RsContext con, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700410 Mesh *sm = static_cast<Mesh *>(mv);
411 rsAssert(vtxDataCount == sm->mVertexBufferCount);
412
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800413 for (uint32_t ct = 0; ct < vtxDataCount; ct ++) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700414 vtxData[ct] = sm->mVertexBuffers[ct].get();
415 sm->mVertexBuffers[ct]->incUserRef();
416 }
417}
418
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800419void rsaMeshGetIndices(RsContext con, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700420 Mesh *sm = static_cast<Mesh *>(mv);
421 rsAssert(idxDataCount == sm->mPrimitivesCount);
422
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800423 for (uint32_t ct = 0; ct < idxDataCount; ct ++) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700424 va[ct] = sm->mPrimitives[ct]->mIndexBuffer.get();
425 primType[ct] = sm->mPrimitives[ct]->mPrimitive;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800426 if (sm->mPrimitives[ct]->mIndexBuffer.get()) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700427 sm->mPrimitives[ct]->mIndexBuffer->incUserRef();
428 }
429 }
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700430}