-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsMesh.cpp
288 lines (245 loc) · 9.35 KB
/
rsMesh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "rsContext.h"
#include "rsMesh.h"
#include "rs.h"
namespace android {
namespace renderscript {
Mesh::Mesh(Context *rsc) : ObjectBase(rsc) {
mHal.drv = nullptr;
mHal.state.primitives = nullptr;
mHal.state.primitivesCount = 0;
mHal.state.indexBuffers = nullptr;
mHal.state.indexBuffersCount = 0;
mHal.state.vertexBuffers = nullptr;
mHal.state.vertexBuffersCount = 0;
mInitialized = false;
mVertexBuffers = nullptr;
mIndexBuffers = nullptr;
}
Mesh::Mesh(Context *rsc,
uint32_t vertexBuffersCount,
uint32_t primitivesCount) : ObjectBase(rsc) {
mHal.drv = nullptr;
mHal.state.primitivesCount = primitivesCount;
mHal.state.indexBuffersCount = primitivesCount;
mHal.state.primitives = new RsPrimitive[mHal.state.primitivesCount];
mHal.state.indexBuffers = new Allocation *[mHal.state.indexBuffersCount];
for (uint32_t i = 0; i < mHal.state.primitivesCount; i ++) {
mHal.state.primitives[i] = RS_PRIMITIVE_POINT;
}
for (uint32_t i = 0; i < mHal.state.indexBuffersCount; i ++) {
mHal.state.indexBuffers[i] = nullptr;
}
mHal.state.vertexBuffersCount = vertexBuffersCount;
mHal.state.vertexBuffers = new Allocation *[mHal.state.vertexBuffersCount];
for (uint32_t i = 0; i < mHal.state.vertexBuffersCount; i ++) {
mHal.state.vertexBuffers[i] = nullptr;
}
mVertexBuffers = new ObjectBaseRef<Allocation>[mHal.state.vertexBuffersCount];
mIndexBuffers = new ObjectBaseRef<Allocation>[mHal.state.primitivesCount];
}
Mesh::~Mesh() {
#ifndef ANDROID_RS_SERIALIZE
mRSC->mHal.funcs.mesh.destroy(mRSC, this);
#endif
delete[] mHal.state.vertexBuffers;
delete[] mHal.state.primitives;
delete[] mHal.state.indexBuffers;
delete[] mVertexBuffers;
delete[] mIndexBuffers;
}
void Mesh::init() {
#ifndef ANDROID_RS_SERIALIZE
mRSC->mHal.funcs.mesh.init(mRSC, this);
#endif
}
void Mesh::serialize(Context *rsc, OStream *stream) const {
// Need to identify ourselves
stream->addU32((uint32_t)getClassId());
stream->addString(getName());
// Store number of vertex streams
stream->addU32(mHal.state.vertexBuffersCount);
for (uint32_t vCount = 0; vCount < mHal.state.vertexBuffersCount; vCount ++) {
mHal.state.vertexBuffers[vCount]->serialize(rsc, stream);
}
stream->addU32(mHal.state.primitivesCount);
// Store the primitives
for (uint32_t pCount = 0; pCount < mHal.state.primitivesCount; pCount ++) {
stream->addU8((uint8_t)mHal.state.primitives[pCount]);
if (mHal.state.indexBuffers[pCount]) {
stream->addU32(1);
mHal.state.indexBuffers[pCount]->serialize(rsc, stream);
} else {
stream->addU32(0);
}
}
}
Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) {
// First make sure we are reading the correct object
RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
if (classID != RS_A3D_CLASS_ID_MESH) {
ALOGE("mesh loading skipped due to invalid class id");
return nullptr;
}
const char *name = stream->loadString();
uint32_t vertexBuffersCount = stream->loadU32();
ObjectBaseRef<Allocation> *vertexBuffers = nullptr;
if (vertexBuffersCount) {
vertexBuffers = new ObjectBaseRef<Allocation>[vertexBuffersCount];
for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) {
Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream);
vertexBuffers[vCount].set(vertexAlloc);
}
}
uint32_t primitivesCount = stream->loadU32();
ObjectBaseRef<Allocation> *indexBuffers = nullptr;
RsPrimitive *primitives = nullptr;
if (primitivesCount) {
indexBuffers = new ObjectBaseRef<Allocation>[primitivesCount];
primitives = new RsPrimitive[primitivesCount];
// load all primitives
for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) {
primitives[pCount] = (RsPrimitive)stream->loadU8();
// Check to see if the index buffer was stored
uint32_t isIndexPresent = stream->loadU32();
if (isIndexPresent) {
Allocation *indexAlloc = Allocation::createFromStream(rsc, stream);
indexBuffers[pCount].set(indexAlloc);
}
}
}
Mesh *mesh = new Mesh(rsc, vertexBuffersCount, primitivesCount);
mesh->assignName(name);
for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) {
mesh->setVertexBuffer(vertexBuffers[vCount].get(), vCount);
}
for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) {
mesh->setPrimitive(indexBuffers[pCount].get(), primitives[pCount], pCount);
}
// Cleanup
if (vertexBuffersCount) {
delete[] vertexBuffers;
}
if (primitivesCount) {
delete[] indexBuffers;
delete[] primitives;
}
#ifndef ANDROID_RS_SERIALIZE
mesh->init();
mesh->uploadAll(rsc);
#endif
return mesh;
}
void Mesh::render(Context *rsc) const {
for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) {
renderPrimitive(rsc, ct);
}
}
void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const {
if (primIndex >= mHal.state.primitivesCount) {
ALOGE("Invalid primitive index");
return;
}
if (mHal.state.indexBuffers[primIndex]) {
renderPrimitiveRange(rsc, primIndex, 0, mHal.state.indexBuffers[primIndex]->getType()->getDimX());
return;
}
renderPrimitiveRange(rsc, primIndex, 0, mHal.state.vertexBuffers[0]->getType()->getDimX());
}
void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const {
if (len < 1 || primIndex >= mHal.state.primitivesCount) {
ALOGE("Invalid mesh or parameters");
return;
}
mRSC->mHal.funcs.mesh.draw(mRSC, this, primIndex, start, len);
}
void Mesh::uploadAll(Context *rsc) {
for (uint32_t ct = 0; ct < mHal.state.vertexBuffersCount; ct ++) {
if (mHal.state.vertexBuffers[ct]) {
rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.vertexBuffers[ct]);
}
}
for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) {
if (mHal.state.indexBuffers[ct]) {
rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.indexBuffers[ct]);
}
}
}
void Mesh::computeBBox(Context *rsc) {
float *posPtr = nullptr;
uint32_t vectorSize = 0;
uint32_t stride = 0;
uint32_t numVerts = 0;
Allocation *posAlloc = nullptr;
// First we need to find the position ptr and stride
for (uint32_t ct=0; ct < mHal.state.vertexBuffersCount; ct++) {
const Type *bufferType = mHal.state.vertexBuffers[ct]->getType();
const Element *bufferElem = bufferType->getElement();
for (uint32_t ct=0; ct < bufferElem->getFieldCount(); ct++) {
if (strcmp(bufferElem->getFieldName(ct), "position") == 0) {
vectorSize = bufferElem->getField(ct)->getComponent().getVectorSize();
stride = bufferElem->getSizeBytes() / sizeof(float);
uint32_t offset = bufferElem->getFieldOffsetBytes(ct);
posAlloc = mHal.state.vertexBuffers[ct];
const uint8_t *bp = (const uint8_t *)rsc->mHal.funcs.allocation.lock1D(
rsc, posAlloc);
posPtr = (float*)(bp + offset);
numVerts = bufferType->getDimX();
break;
}
}
if (posPtr) {
break;
}
}
mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 1e6;
mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = -1e6;
if (!posPtr) {
ALOGE("Unable to compute bounding box");
mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 0.0f;
mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = 0.0f;
return;
}
for (uint32_t i = 0; i < numVerts; i ++) {
for (uint32_t v = 0; v < vectorSize; v ++) {
mBBoxMin[v] = rsMin(mBBoxMin[v], posPtr[v]);
mBBoxMax[v] = rsMax(mBBoxMax[v], posPtr[v]);
}
posPtr += stride;
}
if (posAlloc) {
rsc->mHal.funcs.allocation.unlock1D(rsc, posAlloc);
}
}
RsMesh rsi_MeshCreate(Context *rsc,
RsAllocation * vtx, size_t vtxCount,
RsAllocation * idx, size_t idxCount,
uint32_t * primType, size_t primTypeCount) {
rsAssert(idxCount == primTypeCount);
Mesh *sm = new Mesh(rsc, vtxCount, idxCount);
sm->incUserRef();
for (uint32_t i = 0; i < vtxCount; i ++) {
sm->setVertexBuffer((Allocation*)vtx[i], i);
}
for (uint32_t i = 0; i < idxCount; i ++) {
sm->setPrimitive((Allocation*)idx[i], (RsPrimitive)primType[i], i);
}
sm->init();
return sm;
}
} // namespace renderscript
} // namespace android