blob: 8decfc5d0a82cc808edf928ebc3ad7044a69b0a0 [file] [log] [blame]
Jason Samsa89371c2009-06-30 14:13:04 -07001/*
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Samsa89371c2009-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 Sakhartchouk4edf0302012-03-09 10:47:27 -080018#include "rsMesh.h"
Alex Sakhartchouke23d2392012-03-09 09:24:39 -080019#include "rs.h"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070020
Jason Samsa89371c2009-06-30 14:13:04 -070021using namespace android;
22using namespace android::renderscript;
23
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080024Mesh::Mesh(Context *rsc) : ObjectBase(rsc) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070025 mHal.drv = NULL;
26 mHal.state.primitives = NULL;
27 mHal.state.primitivesCount = 0;
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070028 mHal.state.indexBuffers = NULL;
29 mHal.state.indexBuffersCount = 0;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070030 mHal.state.vertexBuffers = NULL;
31 mHal.state.vertexBuffersCount = 0;
32 mInitialized = false;
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070033
34 mVertexBuffers = NULL;
35 mIndexBuffers = NULL;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070036}
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080037
Alex Sakhartchouka04e30d2011-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 Sakhartchouk064aa7e2011-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 Sakhartchouka04e30d2011-04-29 16:49:08 -070046 for (uint32_t i = 0; i < mHal.state.primitivesCount; i ++) {
Alex Sakhartchouk064aa7e2011-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 Sakhartchouka04e30d2011-04-29 16:49:08 -070051 }
52 mHal.state.vertexBuffersCount = vertexBuffersCount;
Alex Sakhartchouk064aa7e2011-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 Samsa89371c2009-06-30 14:13:04 -070060}
61
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080062Mesh::~Mesh() {
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080063#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070064 mRSC->mHal.funcs.mesh.destroy(mRSC, this);
65#endif
66
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070067 delete[] mHal.state.vertexBuffers;
68 delete[] mHal.state.primitives;
69 delete[] mHal.state.indexBuffers;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070070
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070071 delete[] mVertexBuffers;
72 delete[] mIndexBuffers;
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070073}
74
75void Mesh::init() {
76#ifndef ANDROID_RS_SERIALIZE
77 mRSC->mHal.funcs.mesh.init(mRSC, this);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080078#endif
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -080079}
80
Jason Samse3150cf2012-07-24 18:10:20 -070081void Mesh::serialize(Context *rsc, OStream *stream) const {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080082 // Need to identify ourselves
83 stream->addU32((uint32_t)getClassId());
Jason Sams48ecf6a2013-07-09 15:35:29 -070084 stream->addString(getName());
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080085
86 // Store number of vertex streams
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070087 stream->addU32(mHal.state.vertexBuffersCount);
88 for (uint32_t vCount = 0; vCount < mHal.state.vertexBuffersCount; vCount ++) {
Jason Samse3150cf2012-07-24 18:10:20 -070089 mHal.state.vertexBuffers[vCount]->serialize(rsc, stream);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080090 }
91
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070092 stream->addU32(mHal.state.primitivesCount);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080093 // Store the primitives
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -070094 for (uint32_t pCount = 0; pCount < mHal.state.primitivesCount; pCount ++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070095 stream->addU8((uint8_t)mHal.state.primitives[pCount]);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080096
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -070097 if (mHal.state.indexBuffers[pCount]) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -080098 stream->addU32(1);
Jason Samse3150cf2012-07-24 18:10:20 -070099 mHal.state.indexBuffers[pCount]->serialize(rsc, stream);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800100 } else {
101 stream->addU32(0);
102 }
103 }
104}
105
106Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) {
107 // First make sure we are reading the correct object
108 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
109 if (classID != RS_A3D_CLASS_ID_MESH) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000110 ALOGE("mesh loading skipped due to invalid class id");
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800111 return NULL;
112 }
113
Jason Sams48ecf6a2013-07-09 15:35:29 -0700114 const char *name = stream->loadString();
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800115
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700116 uint32_t vertexBuffersCount = stream->loadU32();
117 ObjectBaseRef<Allocation> *vertexBuffers = NULL;
118 if (vertexBuffersCount) {
119 vertexBuffers = new ObjectBaseRef<Allocation>[vertexBuffersCount];
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800120
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700121 for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) {
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800122 Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700123 vertexBuffers[vCount].set(vertexAlloc);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800124 }
125 }
126
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700127 uint32_t primitivesCount = stream->loadU32();
128 ObjectBaseRef<Allocation> *indexBuffers = NULL;
129 RsPrimitive *primitives = NULL;
130 if (primitivesCount) {
131 indexBuffers = new ObjectBaseRef<Allocation>[primitivesCount];
132 primitives = new RsPrimitive[primitivesCount];
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800133
134 // load all primitives
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700135 for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) {
136 primitives[pCount] = (RsPrimitive)stream->loadU8();
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800137
138 // Check to see if the index buffer was stored
139 uint32_t isIndexPresent = stream->loadU32();
140 if (isIndexPresent) {
141 Allocation *indexAlloc = Allocation::createFromStream(rsc, stream);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700142 indexBuffers[pCount].set(indexAlloc);
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800143 }
144 }
145 }
146
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700147 Mesh *mesh = new Mesh(rsc, vertexBuffersCount, primitivesCount);
Jason Sams48ecf6a2013-07-09 15:35:29 -0700148 mesh->assignName(name);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700149 for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) {
150 mesh->setVertexBuffer(vertexBuffers[vCount].get(), vCount);
151 }
152 for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) {
153 mesh->setPrimitive(indexBuffers[pCount].get(), primitives[pCount], pCount);
154 }
155
156 // Cleanup
157 if (vertexBuffersCount) {
158 delete[] vertexBuffers;
159 }
160 if (primitivesCount) {
161 delete[] indexBuffers;
162 delete[] primitives;
163 }
164
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800165#ifndef ANDROID_RS_SERIALIZE
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700166 mesh->init();
Alex Sakhartchouk099d7d32011-01-28 09:31:47 -0800167 mesh->uploadAll(rsc);
168#endif
169 return mesh;
170}
171
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800172void Mesh::render(Context *rsc) const {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700173 for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) {
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700174 renderPrimitive(rsc, ct);
175 }
176}
177
178void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700179 if (primIndex >= mHal.state.primitivesCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000180 ALOGE("Invalid primitive index");
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700181 return;
182 }
183
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700184 if (mHal.state.indexBuffers[primIndex]) {
185 renderPrimitiveRange(rsc, primIndex, 0, mHal.state.indexBuffers[primIndex]->getType()->getDimX());
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700186 return;
187 }
188
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700189 renderPrimitiveRange(rsc, primIndex, 0, mHal.state.vertexBuffers[0]->getType()->getDimX());
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700190}
191
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800192void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700193 if (len < 1 || primIndex >= mHal.state.primitivesCount) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000194 ALOGE("Invalid mesh or parameters");
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700195 return;
196 }
197
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700198 mRSC->mHal.funcs.mesh.draw(mRSC, this, primIndex, start, len);
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700199}
200
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800201void Mesh::uploadAll(Context *rsc) {
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700202 for (uint32_t ct = 0; ct < mHal.state.vertexBuffersCount; ct ++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700203 if (mHal.state.vertexBuffers[ct]) {
204 rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.vertexBuffers[ct]);
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700205 }
206 }
207
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700208 for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700209 if (mHal.state.indexBuffers[ct]) {
210 rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.indexBuffers[ct]);
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700211 }
212 }
Jason Samsa89371c2009-06-30 14:13:04 -0700213}
214
Jason Samse3150cf2012-07-24 18:10:20 -0700215void Mesh::computeBBox(Context *rsc) {
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700216 float *posPtr = NULL;
217 uint32_t vectorSize = 0;
218 uint32_t stride = 0;
219 uint32_t numVerts = 0;
Jason Sams807fdc42012-07-25 17:55:39 -0700220 Allocation *posAlloc = NULL;
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700221 // First we need to find the position ptr and stride
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700222 for (uint32_t ct=0; ct < mHal.state.vertexBuffersCount; ct++) {
223 const Type *bufferType = mHal.state.vertexBuffers[ct]->getType();
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700224 const Element *bufferElem = bufferType->getElement();
225
226 for (uint32_t ct=0; ct < bufferElem->getFieldCount(); ct++) {
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800227 if (strcmp(bufferElem->getFieldName(ct), "position") == 0) {
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700228 vectorSize = bufferElem->getField(ct)->getComponent().getVectorSize();
229 stride = bufferElem->getSizeBytes() / sizeof(float);
230 uint32_t offset = bufferElem->getFieldOffsetBytes(ct);
Jason Sams807fdc42012-07-25 17:55:39 -0700231 posAlloc = mHal.state.vertexBuffers[ct];
232 const uint8_t *bp = (const uint8_t *)rsc->mHal.funcs.allocation.lock1D(
233 rsc, posAlloc);
234 posPtr = (float*)(bp + offset);
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700235 numVerts = bufferType->getDimX();
236 break;
237 }
238 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800239 if (posPtr) {
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700240 break;
241 }
242 }
243
244 mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 1e6;
245 mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = -1e6;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800246 if (!posPtr) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000247 ALOGE("Unable to compute bounding box");
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700248 mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 0.0f;
249 mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = 0.0f;
250 return;
251 }
252
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800253 for (uint32_t i = 0; i < numVerts; i ++) {
254 for (uint32_t v = 0; v < vectorSize; v ++) {
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700255 mBBoxMin[v] = rsMin(mBBoxMin[v], posPtr[v]);
256 mBBoxMax[v] = rsMax(mBBoxMax[v], posPtr[v]);
257 }
258 posPtr += stride;
259 }
Jason Sams807fdc42012-07-25 17:55:39 -0700260
261 if (posAlloc) {
262 rsc->mHal.funcs.allocation.unlock1D(rsc, posAlloc);
263 }
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700264}
265
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700266namespace android {
267namespace renderscript {
268
Alex Sakhartchouk9003e562011-05-12 10:38:03 -0700269RsMesh rsi_MeshCreate(Context *rsc,
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700270 RsAllocation * vtx, size_t vtxCount,
271 RsAllocation * idx, size_t idxCount,
272 uint32_t * primType, size_t primTypeCount) {
Alex Sakhartchouk9003e562011-05-12 10:38:03 -0700273 rsAssert(idxCount == primTypeCount);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700274 Mesh *sm = new Mesh(rsc, vtxCount, idxCount);
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700275 sm->incUserRef();
276
Alex Sakhartchouk9003e562011-05-12 10:38:03 -0700277 for (uint32_t i = 0; i < vtxCount; i ++) {
278 sm->setVertexBuffer((Allocation*)vtx[i], i);
279 }
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700280
Alex Sakhartchouk9003e562011-05-12 10:38:03 -0700281 for (uint32_t i = 0; i < idxCount; i ++) {
282 sm->setPrimitive((Allocation*)idx[i], (RsPrimitive)primType[i], i);
283 }
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700284
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700285 sm->init();
Alex Sakhartchouk9003e562011-05-12 10:38:03 -0700286
287 return sm;
Alex Sakhartchouk54929cc2010-11-08 15:10:52 -0800288}
289
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700290}}
291
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800292void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700293 Mesh *sm = static_cast<Mesh *>(mv);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700294 *numVtx = sm->mHal.state.vertexBuffersCount;
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700295}
296
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800297void rsaMeshGetIndexCount(RsContext con, RsMesh mv, int32_t *numIdx) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700298 Mesh *sm = static_cast<Mesh *>(mv);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700299 *numIdx = sm->mHal.state.primitivesCount;
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700300}
301
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800302void rsaMeshGetVertices(RsContext con, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700303 Mesh *sm = static_cast<Mesh *>(mv);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700304 rsAssert(vtxDataCount == sm->mHal.state.vertexBuffersCount);
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700305
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800306 for (uint32_t ct = 0; ct < vtxDataCount; ct ++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700307 vtxData[ct] = sm->mHal.state.vertexBuffers[ct];
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700308 sm->mHal.state.vertexBuffers[ct]->incUserRef();
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700309 }
310}
311
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800312void rsaMeshGetIndices(RsContext con, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount) {
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700313 Mesh *sm = static_cast<Mesh *>(mv);
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700314 rsAssert(idxDataCount == sm->mHal.state.primitivesCount);
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700315
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800316 for (uint32_t ct = 0; ct < idxDataCount; ct ++) {
Alex Sakhartchouk064aa7e2011-10-18 10:54:29 -0700317 va[ct] = sm->mHal.state.indexBuffers[ct];
318 primType[ct] = sm->mHal.state.primitives[ct];
319 if (sm->mHal.state.indexBuffers[ct]) {
320 sm->mHal.state.indexBuffers[ct]->incUserRef();
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700321 }
322 }
Alex Sakhartchoukd18c7442010-07-12 15:50:32 -0700323}