blob: 399a52ba248ba98ae262f2125f366318453aa70a [file] [log] [blame]
Jason Sams7c878f32009-06-30 14:13:04 -07001/*
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Sams7c878f32009-06-30 14:13:04 -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
17#include "rsContext.h"
Alex Sakhartchoukbedc0232012-03-09 10:47:27 -080018#include "rsMesh.h"
Alex Sakhartchouk137ccbb2012-03-09 09:24:39 -080019#include "rs.h"
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070020
Jason Sams7c878f32009-06-30 14:13:04 -070021using namespace android;
22using namespace android::renderscript;
23
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080024Mesh::Mesh(Context *rsc) : ObjectBase(rsc) {
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070025 mHal.drv = NULL;
26 mHal.state.primitives = NULL;
27 mHal.state.primitivesCount = 0;
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070028 mHal.state.indexBuffers = NULL;
29 mHal.state.indexBuffersCount = 0;
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070030 mHal.state.vertexBuffers = NULL;
31 mHal.state.vertexBuffersCount = 0;
32 mInitialized = false;
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070033
34 mVertexBuffers = NULL;
35 mIndexBuffers = NULL;
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070036}
Alex Sakhartchouka3b59602011-01-28 09:31:47 -080037
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070038Mesh::Mesh(Context *rsc,
39 uint32_t vertexBuffersCount,
40 uint32_t primitivesCount) : ObjectBase(rsc) {
41 mHal.drv = NULL;
42 mHal.state.primitivesCount = primitivesCount;
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070043 mHal.state.indexBuffersCount = primitivesCount;
44 mHal.state.primitives = new RsPrimitive[mHal.state.primitivesCount];
45 mHal.state.indexBuffers = new Allocation *[mHal.state.indexBuffersCount];
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070046 for (uint32_t i = 0; i < mHal.state.primitivesCount; i ++) {
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070047 mHal.state.primitives[i] = RS_PRIMITIVE_POINT;
48 }
49 for (uint32_t i = 0; i < mHal.state.indexBuffersCount; i ++) {
50 mHal.state.indexBuffers[i] = NULL;
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070051 }
52 mHal.state.vertexBuffersCount = vertexBuffersCount;
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070053 mHal.state.vertexBuffers = new Allocation *[mHal.state.vertexBuffersCount];
54 for (uint32_t i = 0; i < mHal.state.vertexBuffersCount; i ++) {
55 mHal.state.vertexBuffers[i] = NULL;
56 }
57
58 mVertexBuffers = new ObjectBaseRef<Allocation>[mHal.state.vertexBuffersCount];
59 mIndexBuffers = new ObjectBaseRef<Allocation>[mHal.state.primitivesCount];
Jason Sams7c878f32009-06-30 14:13:04 -070060}
61
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080062Mesh::~Mesh() {
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080063#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070064 mRSC->mHal.funcs.mesh.destroy(mRSC, this);
65#endif
66
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070067 delete[] mHal.state.vertexBuffers;
68 delete[] mHal.state.primitives;
69 delete[] mHal.state.indexBuffers;
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070070
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070071 delete[] mVertexBuffers;
72 delete[] mIndexBuffers;
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070073}
74
75void Mesh::init() {
76#ifndef ANDROID_RS_SERIALIZE
77 mRSC->mHal.funcs.mesh.init(mRSC, this);
Alex Sakhartchouka3b59602011-01-28 09:31:47 -080078#endif
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -080079}
80
Alex Sakhartchouka3b59602011-01-28 09:31:47 -080081void Mesh::serialize(OStream *stream) const {
82 // Need to identify ourselves
83 stream->addU32((uint32_t)getClassId());
84
85 String8 name(getName());
86 stream->addString(&name);
87
88 // Store number of vertex streams
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070089 stream->addU32(mHal.state.vertexBuffersCount);
90 for (uint32_t vCount = 0; vCount < mHal.state.vertexBuffersCount; vCount ++) {
91 mHal.state.vertexBuffers[vCount]->serialize(stream);
Alex Sakhartchouka3b59602011-01-28 09:31:47 -080092 }
93
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070094 stream->addU32(mHal.state.primitivesCount);
Alex Sakhartchouka3b59602011-01-28 09:31:47 -080095 // Store the primitives
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070096 for (uint32_t pCount = 0; pCount < mHal.state.primitivesCount; pCount ++) {
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070097 stream->addU8((uint8_t)mHal.state.primitives[pCount]);
Alex Sakhartchouka3b59602011-01-28 09:31:47 -080098
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -070099 if (mHal.state.indexBuffers[pCount]) {
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800100 stream->addU32(1);
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700101 mHal.state.indexBuffers[pCount]->serialize(stream);
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800102 } else {
103 stream->addU32(0);
104 }
105 }
106}
107
108Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) {
109 // First make sure we are reading the correct object
110 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
111 if (classID != RS_A3D_CLASS_ID_MESH) {
Steve Block3762c312012-01-06 19:20:56 +0000112 ALOGE("mesh loading skipped due to invalid class id");
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800113 return NULL;
114 }
115
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800116 String8 name;
117 stream->loadString(&name);
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800118
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700119 uint32_t vertexBuffersCount = stream->loadU32();
120 ObjectBaseRef<Allocation> *vertexBuffers = NULL;
121 if (vertexBuffersCount) {
122 vertexBuffers = new ObjectBaseRef<Allocation>[vertexBuffersCount];
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800123
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700124 for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) {
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800125 Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700126 vertexBuffers[vCount].set(vertexAlloc);
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800127 }
128 }
129
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700130 uint32_t primitivesCount = stream->loadU32();
131 ObjectBaseRef<Allocation> *indexBuffers = NULL;
132 RsPrimitive *primitives = NULL;
133 if (primitivesCount) {
134 indexBuffers = new ObjectBaseRef<Allocation>[primitivesCount];
135 primitives = new RsPrimitive[primitivesCount];
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800136
137 // load all primitives
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700138 for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) {
139 primitives[pCount] = (RsPrimitive)stream->loadU8();
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800140
141 // Check to see if the index buffer was stored
142 uint32_t isIndexPresent = stream->loadU32();
143 if (isIndexPresent) {
144 Allocation *indexAlloc = Allocation::createFromStream(rsc, stream);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700145 indexBuffers[pCount].set(indexAlloc);
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800146 }
147 }
148 }
149
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700150 Mesh *mesh = new Mesh(rsc, vertexBuffersCount, primitivesCount);
151 mesh->setName(name.string(), name.size());
152 for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) {
153 mesh->setVertexBuffer(vertexBuffers[vCount].get(), vCount);
154 }
155 for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) {
156 mesh->setPrimitive(indexBuffers[pCount].get(), primitives[pCount], pCount);
157 }
158
159 // Cleanup
160 if (vertexBuffersCount) {
161 delete[] vertexBuffers;
162 }
163 if (primitivesCount) {
164 delete[] indexBuffers;
165 delete[] primitives;
166 }
167
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800168#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700169 mesh->init();
Alex Sakhartchouka3b59602011-01-28 09:31:47 -0800170 mesh->uploadAll(rsc);
171#endif
172 return mesh;
173}
174
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800175void Mesh::render(Context *rsc) const {
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700176 for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) {
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700177 renderPrimitive(rsc, ct);
178 }
179}
180
181void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const {
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700182 if (primIndex >= mHal.state.primitivesCount) {
Steve Block3762c312012-01-06 19:20:56 +0000183 ALOGE("Invalid primitive index");
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700184 return;
185 }
186
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700187 if (mHal.state.indexBuffers[primIndex]) {
188 renderPrimitiveRange(rsc, primIndex, 0, mHal.state.indexBuffers[primIndex]->getType()->getDimX());
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700189 return;
190 }
191
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700192 renderPrimitiveRange(rsc, primIndex, 0, mHal.state.vertexBuffers[0]->getType()->getDimX());
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700193}
194
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800195void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const {
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700196 if (len < 1 || primIndex >= mHal.state.primitivesCount) {
Steve Block3762c312012-01-06 19:20:56 +0000197 ALOGE("Invalid mesh or parameters");
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700198 return;
199 }
200
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700201 mRSC->mHal.funcs.mesh.draw(mRSC, this, primIndex, start, len);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700202}
203
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800204void Mesh::uploadAll(Context *rsc) {
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700205 for (uint32_t ct = 0; ct < mHal.state.vertexBuffersCount; ct ++) {
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700206 if (mHal.state.vertexBuffers[ct]) {
207 rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.vertexBuffers[ct]);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700208 }
209 }
210
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700211 for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) {
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700212 if (mHal.state.indexBuffers[ct]) {
213 rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.indexBuffers[ct]);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700214 }
215 }
Jason Sams7c878f32009-06-30 14:13:04 -0700216}
217
Alex Sakhartchouka80145d2010-08-13 14:32:23 -0700218void Mesh::computeBBox() {
219 float *posPtr = NULL;
220 uint32_t vectorSize = 0;
221 uint32_t stride = 0;
222 uint32_t numVerts = 0;
223 // First we need to find the position ptr and stride
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700224 for (uint32_t ct=0; ct < mHal.state.vertexBuffersCount; ct++) {
225 const Type *bufferType = mHal.state.vertexBuffers[ct]->getType();
Alex Sakhartchouka80145d2010-08-13 14:32:23 -0700226 const Element *bufferElem = bufferType->getElement();
227
228 for (uint32_t ct=0; ct < bufferElem->getFieldCount(); ct++) {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800229 if (strcmp(bufferElem->getFieldName(ct), "position") == 0) {
Alex Sakhartchouka80145d2010-08-13 14:32:23 -0700230 vectorSize = bufferElem->getField(ct)->getComponent().getVectorSize();
231 stride = bufferElem->getSizeBytes() / sizeof(float);
232 uint32_t offset = bufferElem->getFieldOffsetBytes(ct);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700233 posPtr = (float*)((uint8_t*)mHal.state.vertexBuffers[ct]->getPtr() + offset);
Alex Sakhartchouka80145d2010-08-13 14:32:23 -0700234 numVerts = bufferType->getDimX();
235 break;
236 }
237 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800238 if (posPtr) {
Alex Sakhartchouka80145d2010-08-13 14:32:23 -0700239 break;
240 }
241 }
242
243 mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 1e6;
244 mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = -1e6;
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800245 if (!posPtr) {
Steve Block3762c312012-01-06 19:20:56 +0000246 ALOGE("Unable to compute bounding box");
Alex Sakhartchouka80145d2010-08-13 14:32:23 -0700247 mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 0.0f;
248 mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = 0.0f;
249 return;
250 }
251
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800252 for (uint32_t i = 0; i < numVerts; i ++) {
253 for (uint32_t v = 0; v < vectorSize; v ++) {
Alex Sakhartchouka80145d2010-08-13 14:32:23 -0700254 mBBoxMin[v] = rsMin(mBBoxMin[v], posPtr[v]);
255 mBBoxMax[v] = rsMax(mBBoxMax[v], posPtr[v]);
256 }
257 posPtr += stride;
258 }
259}
260
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700261namespace android {
262namespace renderscript {
263
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700264RsMesh rsi_MeshCreate(Context *rsc,
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700265 RsAllocation * vtx, size_t vtxCount,
266 RsAllocation * idx, size_t idxCount,
267 uint32_t * primType, size_t primTypeCount) {
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700268 rsAssert(idxCount == primTypeCount);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700269 Mesh *sm = new Mesh(rsc, vtxCount, idxCount);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700270 sm->incUserRef();
271
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700272 for (uint32_t i = 0; i < vtxCount; i ++) {
273 sm->setVertexBuffer((Allocation*)vtx[i], i);
274 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700275
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700276 for (uint32_t i = 0; i < idxCount; i ++) {
277 sm->setPrimitive((Allocation*)idx[i], (RsPrimitive)primType[i], i);
278 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700279
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700280 sm->init();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700281
282 return sm;
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800283}
284
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700285}}
286
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800287void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700288 Mesh *sm = static_cast<Mesh *>(mv);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700289 *numVtx = sm->mHal.state.vertexBuffersCount;
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700290}
291
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800292void rsaMeshGetIndexCount(RsContext con, RsMesh mv, int32_t *numIdx) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700293 Mesh *sm = static_cast<Mesh *>(mv);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700294 *numIdx = sm->mHal.state.primitivesCount;
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700295}
296
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800297void rsaMeshGetVertices(RsContext con, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700298 Mesh *sm = static_cast<Mesh *>(mv);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700299 rsAssert(vtxDataCount == sm->mHal.state.vertexBuffersCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700300
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800301 for (uint32_t ct = 0; ct < vtxDataCount; ct ++) {
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700302 vtxData[ct] = sm->mHal.state.vertexBuffers[ct];
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700303 sm->mHal.state.vertexBuffers[ct]->incUserRef();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700304 }
305}
306
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800307void rsaMeshGetIndices(RsContext con, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount) {
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700308 Mesh *sm = static_cast<Mesh *>(mv);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700309 rsAssert(idxDataCount == sm->mHal.state.primitivesCount);
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700310
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800311 for (uint32_t ct = 0; ct < idxDataCount; ct ++) {
Alex Sakhartchouk5ef2f532011-10-18 10:54:29 -0700312 va[ct] = sm->mHal.state.indexBuffers[ct];
313 primType[ct] = sm->mHal.state.primitives[ct];
314 if (sm->mHal.state.indexBuffers[ct]) {
315 sm->mHal.state.indexBuffers[ct]->incUserRef();
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700316 }
317 }
Alex Sakhartchouk80a4c2c2010-07-12 15:50:32 -0700318}