rsMesh.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (C) 2011 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. #include "rsContext.h"
  17. #include "rsMesh.h"
  18. #include "rs.h"
  19. namespace android {
  20. namespace renderscript {
  21. Mesh::Mesh(Context *rsc) : ObjectBase(rsc) {
  22. mHal.drv = nullptr;
  23. mHal.state.primitives = nullptr;
  24. mHal.state.primitivesCount = 0;
  25. mHal.state.indexBuffers = nullptr;
  26. mHal.state.indexBuffersCount = 0;
  27. mHal.state.vertexBuffers = nullptr;
  28. mHal.state.vertexBuffersCount = 0;
  29. mInitialized = false;
  30. mVertexBuffers = nullptr;
  31. mIndexBuffers = nullptr;
  32. }
  33. Mesh::Mesh(Context *rsc,
  34. uint32_t vertexBuffersCount,
  35. uint32_t primitivesCount) : ObjectBase(rsc) {
  36. mHal.drv = nullptr;
  37. mHal.state.primitivesCount = primitivesCount;
  38. mHal.state.indexBuffersCount = primitivesCount;
  39. mHal.state.primitives = new RsPrimitive[mHal.state.primitivesCount];
  40. mHal.state.indexBuffers = new Allocation *[mHal.state.indexBuffersCount];
  41. for (uint32_t i = 0; i < mHal.state.primitivesCount; i ++) {
  42. mHal.state.primitives[i] = RS_PRIMITIVE_POINT;
  43. }
  44. for (uint32_t i = 0; i < mHal.state.indexBuffersCount; i ++) {
  45. mHal.state.indexBuffers[i] = nullptr;
  46. }
  47. mHal.state.vertexBuffersCount = vertexBuffersCount;
  48. mHal.state.vertexBuffers = new Allocation *[mHal.state.vertexBuffersCount];
  49. for (uint32_t i = 0; i < mHal.state.vertexBuffersCount; i ++) {
  50. mHal.state.vertexBuffers[i] = nullptr;
  51. }
  52. mVertexBuffers = new ObjectBaseRef<Allocation>[mHal.state.vertexBuffersCount];
  53. mIndexBuffers = new ObjectBaseRef<Allocation>[mHal.state.primitivesCount];
  54. }
  55. Mesh::~Mesh() {
  56. #ifndef ANDROID_RS_SERIALIZE
  57. mRSC->mHal.funcs.mesh.destroy(mRSC, this);
  58. #endif
  59. delete[] mHal.state.vertexBuffers;
  60. delete[] mHal.state.primitives;
  61. delete[] mHal.state.indexBuffers;
  62. delete[] mVertexBuffers;
  63. delete[] mIndexBuffers;
  64. }
  65. void Mesh::init() {
  66. #ifndef ANDROID_RS_SERIALIZE
  67. mRSC->mHal.funcs.mesh.init(mRSC, this);
  68. #endif
  69. }
  70. void Mesh::serialize(Context *rsc, OStream *stream) const {
  71. // Need to identify ourselves
  72. stream->addU32((uint32_t)getClassId());
  73. stream->addString(getName());
  74. // Store number of vertex streams
  75. stream->addU32(mHal.state.vertexBuffersCount);
  76. for (uint32_t vCount = 0; vCount < mHal.state.vertexBuffersCount; vCount ++) {
  77. mHal.state.vertexBuffers[vCount]->serialize(rsc, stream);
  78. }
  79. stream->addU32(mHal.state.primitivesCount);
  80. // Store the primitives
  81. for (uint32_t pCount = 0; pCount < mHal.state.primitivesCount; pCount ++) {
  82. stream->addU8((uint8_t)mHal.state.primitives[pCount]);
  83. if (mHal.state.indexBuffers[pCount]) {
  84. stream->addU32(1);
  85. mHal.state.indexBuffers[pCount]->serialize(rsc, stream);
  86. } else {
  87. stream->addU32(0);
  88. }
  89. }
  90. }
  91. Mesh *Mesh::createFromStream(Context *rsc, IStream *stream) {
  92. // First make sure we are reading the correct object
  93. RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
  94. if (classID != RS_A3D_CLASS_ID_MESH) {
  95. ALOGE("mesh loading skipped due to invalid class id");
  96. return nullptr;
  97. }
  98. const char *name = stream->loadString();
  99. uint32_t vertexBuffersCount = stream->loadU32();
  100. ObjectBaseRef<Allocation> *vertexBuffers = nullptr;
  101. if (vertexBuffersCount) {
  102. vertexBuffers = new ObjectBaseRef<Allocation>[vertexBuffersCount];
  103. for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) {
  104. Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream);
  105. vertexBuffers[vCount].set(vertexAlloc);
  106. }
  107. }
  108. uint32_t primitivesCount = stream->loadU32();
  109. ObjectBaseRef<Allocation> *indexBuffers = nullptr;
  110. RsPrimitive *primitives = nullptr;
  111. if (primitivesCount) {
  112. indexBuffers = new ObjectBaseRef<Allocation>[primitivesCount];
  113. primitives = new RsPrimitive[primitivesCount];
  114. // load all primitives
  115. for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) {
  116. primitives[pCount] = (RsPrimitive)stream->loadU8();
  117. // Check to see if the index buffer was stored
  118. uint32_t isIndexPresent = stream->loadU32();
  119. if (isIndexPresent) {
  120. Allocation *indexAlloc = Allocation::createFromStream(rsc, stream);
  121. indexBuffers[pCount].set(indexAlloc);
  122. }
  123. }
  124. }
  125. Mesh *mesh = new Mesh(rsc, vertexBuffersCount, primitivesCount);
  126. mesh->assignName(name);
  127. for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) {
  128. mesh->setVertexBuffer(vertexBuffers[vCount].get(), vCount);
  129. }
  130. for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) {
  131. mesh->setPrimitive(indexBuffers[pCount].get(), primitives[pCount], pCount);
  132. }
  133. // Cleanup
  134. if (vertexBuffersCount) {
  135. delete[] vertexBuffers;
  136. }
  137. if (primitivesCount) {
  138. delete[] indexBuffers;
  139. delete[] primitives;
  140. }
  141. #ifndef ANDROID_RS_SERIALIZE
  142. mesh->init();
  143. mesh->uploadAll(rsc);
  144. #endif
  145. return mesh;
  146. }
  147. void Mesh::render(Context *rsc) const {
  148. for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) {
  149. renderPrimitive(rsc, ct);
  150. }
  151. }
  152. void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const {
  153. if (primIndex >= mHal.state.primitivesCount) {
  154. ALOGE("Invalid primitive index");
  155. return;
  156. }
  157. if (mHal.state.indexBuffers[primIndex]) {
  158. renderPrimitiveRange(rsc, primIndex, 0, mHal.state.indexBuffers[primIndex]->getType()->getDimX());
  159. return;
  160. }
  161. renderPrimitiveRange(rsc, primIndex, 0, mHal.state.vertexBuffers[0]->getType()->getDimX());
  162. }
  163. void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const {
  164. if (len < 1 || primIndex >= mHal.state.primitivesCount) {
  165. ALOGE("Invalid mesh or parameters");
  166. return;
  167. }
  168. mRSC->mHal.funcs.mesh.draw(mRSC, this, primIndex, start, len);
  169. }
  170. void Mesh::uploadAll(Context *rsc) {
  171. for (uint32_t ct = 0; ct < mHal.state.vertexBuffersCount; ct ++) {
  172. if (mHal.state.vertexBuffers[ct]) {
  173. rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.vertexBuffers[ct]);
  174. }
  175. }
  176. for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) {
  177. if (mHal.state.indexBuffers[ct]) {
  178. rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.indexBuffers[ct]);
  179. }
  180. }
  181. }
  182. void Mesh::computeBBox(Context *rsc) {
  183. float *posPtr = nullptr;
  184. uint32_t vectorSize = 0;
  185. uint32_t stride = 0;
  186. uint32_t numVerts = 0;
  187. Allocation *posAlloc = nullptr;
  188. // First we need to find the position ptr and stride
  189. for (uint32_t ct=0; ct < mHal.state.vertexBuffersCount; ct++) {
  190. const Type *bufferType = mHal.state.vertexBuffers[ct]->getType();
  191. const Element *bufferElem = bufferType->getElement();
  192. for (uint32_t ct=0; ct < bufferElem->getFieldCount(); ct++) {
  193. if (strcmp(bufferElem->getFieldName(ct), "position") == 0) {
  194. vectorSize = bufferElem->getField(ct)->getComponent().getVectorSize();
  195. stride = bufferElem->getSizeBytes() / sizeof(float);
  196. uint32_t offset = bufferElem->getFieldOffsetBytes(ct);
  197. posAlloc = mHal.state.vertexBuffers[ct];
  198. const uint8_t *bp = (const uint8_t *)rsc->mHal.funcs.allocation.lock1D(
  199. rsc, posAlloc);
  200. posPtr = (float*)(bp + offset);
  201. numVerts = bufferType->getDimX();
  202. break;
  203. }
  204. }
  205. if (posPtr) {
  206. break;
  207. }
  208. }
  209. mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 1e6;
  210. mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = -1e6;
  211. if (!posPtr) {
  212. ALOGE("Unable to compute bounding box");
  213. mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 0.0f;
  214. mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = 0.0f;
  215. return;
  216. }
  217. for (uint32_t i = 0; i < numVerts; i ++) {
  218. for (uint32_t v = 0; v < vectorSize; v ++) {
  219. mBBoxMin[v] = rsMin(mBBoxMin[v], posPtr[v]);
  220. mBBoxMax[v] = rsMax(mBBoxMax[v], posPtr[v]);
  221. }
  222. posPtr += stride;
  223. }
  224. if (posAlloc) {
  225. rsc->mHal.funcs.allocation.unlock1D(rsc, posAlloc);
  226. }
  227. }
  228. RsMesh rsi_MeshCreate(Context *rsc,
  229. RsAllocation * vtx, size_t vtxCount,
  230. RsAllocation * idx, size_t idxCount,
  231. uint32_t * primType, size_t primTypeCount) {
  232. rsAssert(idxCount == primTypeCount);
  233. Mesh *sm = new Mesh(rsc, vtxCount, idxCount);
  234. sm->incUserRef();
  235. for (uint32_t i = 0; i < vtxCount; i ++) {
  236. sm->setVertexBuffer((Allocation*)vtx[i], i);
  237. }
  238. for (uint32_t i = 0; i < idxCount; i ++) {
  239. sm->setPrimitive((Allocation*)idx[i], (RsPrimitive)primType[i], i);
  240. }
  241. sm->init();
  242. return sm;
  243. }
  244. } // namespace renderscript
  245. } // namespace android