Jason Sams | a89371c | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 1 | /* |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Jason Sams | a89371c | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 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 | |
| 17 | #include "rsContext.h" |
Alex Sakhartchouk | 4edf030 | 2012-03-09 10:47:27 -0800 | [diff] [blame] | 18 | #include "rsMesh.h" |
Alex Sakhartchouk | e23d239 | 2012-03-09 09:24:39 -0800 | [diff] [blame] | 19 | #include "rs.h" |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 20 | |
Jason Sams | a89371c | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 21 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 24 | Mesh::Mesh(Context *rsc) : ObjectBase(rsc) { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 25 | mHal.drv = NULL; |
| 26 | mHal.state.primitives = NULL; |
| 27 | mHal.state.primitivesCount = 0; |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 28 | mHal.state.indexBuffers = NULL; |
| 29 | mHal.state.indexBuffersCount = 0; |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 30 | mHal.state.vertexBuffers = NULL; |
| 31 | mHal.state.vertexBuffersCount = 0; |
| 32 | mInitialized = false; |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 33 | |
| 34 | mVertexBuffers = NULL; |
| 35 | mIndexBuffers = NULL; |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 36 | } |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 37 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 38 | Mesh::Mesh(Context *rsc, |
| 39 | uint32_t vertexBuffersCount, |
| 40 | uint32_t primitivesCount) : ObjectBase(rsc) { |
| 41 | mHal.drv = NULL; |
| 42 | mHal.state.primitivesCount = primitivesCount; |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 43 | mHal.state.indexBuffersCount = primitivesCount; |
| 44 | mHal.state.primitives = new RsPrimitive[mHal.state.primitivesCount]; |
| 45 | mHal.state.indexBuffers = new Allocation *[mHal.state.indexBuffersCount]; |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 46 | for (uint32_t i = 0; i < mHal.state.primitivesCount; i ++) { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 47 | 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 Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 51 | } |
| 52 | mHal.state.vertexBuffersCount = vertexBuffersCount; |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 53 | 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 Sams | a89371c | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 62 | Mesh::~Mesh() { |
Alex Sakhartchouk | 77d9f4b | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 63 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 64 | mRSC->mHal.funcs.mesh.destroy(mRSC, this); |
| 65 | #endif |
| 66 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 67 | delete[] mHal.state.vertexBuffers; |
| 68 | delete[] mHal.state.primitives; |
| 69 | delete[] mHal.state.indexBuffers; |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 70 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 71 | delete[] mVertexBuffers; |
| 72 | delete[] mIndexBuffers; |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void Mesh::init() { |
| 76 | #ifndef ANDROID_RS_SERIALIZE |
| 77 | mRSC->mHal.funcs.mesh.init(mRSC, this); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 78 | #endif |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 81 | void Mesh::serialize(Context *rsc, OStream *stream) const { |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 82 | // Need to identify ourselves |
| 83 | stream->addU32((uint32_t)getClassId()); |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 84 | stream->addString(getName()); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 85 | |
| 86 | // Store number of vertex streams |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 87 | stream->addU32(mHal.state.vertexBuffersCount); |
| 88 | for (uint32_t vCount = 0; vCount < mHal.state.vertexBuffersCount; vCount ++) { |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 89 | mHal.state.vertexBuffers[vCount]->serialize(rsc, stream); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 92 | stream->addU32(mHal.state.primitivesCount); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 93 | // Store the primitives |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 94 | for (uint32_t pCount = 0; pCount < mHal.state.primitivesCount; pCount ++) { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 95 | stream->addU8((uint8_t)mHal.state.primitives[pCount]); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 96 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 97 | if (mHal.state.indexBuffers[pCount]) { |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 98 | stream->addU32(1); |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 99 | mHal.state.indexBuffers[pCount]->serialize(rsc, stream); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 100 | } else { |
| 101 | stream->addU32(0); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | Mesh *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 Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 110 | ALOGE("mesh loading skipped due to invalid class id"); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 111 | return NULL; |
| 112 | } |
| 113 | |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 114 | const char *name = stream->loadString(); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 115 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 116 | uint32_t vertexBuffersCount = stream->loadU32(); |
| 117 | ObjectBaseRef<Allocation> *vertexBuffers = NULL; |
| 118 | if (vertexBuffersCount) { |
| 119 | vertexBuffers = new ObjectBaseRef<Allocation>[vertexBuffersCount]; |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 120 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 121 | for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) { |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 122 | Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream); |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 123 | vertexBuffers[vCount].set(vertexAlloc); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 127 | 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 Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 133 | |
| 134 | // load all primitives |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 135 | for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) { |
| 136 | primitives[pCount] = (RsPrimitive)stream->loadU8(); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 137 | |
| 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 Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 142 | indexBuffers[pCount].set(indexAlloc); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 147 | Mesh *mesh = new Mesh(rsc, vertexBuffersCount, primitivesCount); |
Jason Sams | 48ecf6a | 2013-07-09 15:35:29 -0700 | [diff] [blame] | 148 | mesh->assignName(name); |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 149 | 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 Sakhartchouk | 77d9f4b | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 165 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 166 | mesh->init(); |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 167 | mesh->uploadAll(rsc); |
| 168 | #endif |
| 169 | return mesh; |
| 170 | } |
| 171 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 172 | void Mesh::render(Context *rsc) const { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 173 | for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) { |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 174 | renderPrimitive(rsc, ct); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 179 | if (primIndex >= mHal.state.primitivesCount) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 180 | ALOGE("Invalid primitive index"); |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 184 | if (mHal.state.indexBuffers[primIndex]) { |
| 185 | renderPrimitiveRange(rsc, primIndex, 0, mHal.state.indexBuffers[primIndex]->getType()->getDimX()); |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 189 | renderPrimitiveRange(rsc, primIndex, 0, mHal.state.vertexBuffers[0]->getType()->getDimX()); |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 192 | void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 193 | if (len < 1 || primIndex >= mHal.state.primitivesCount) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 194 | ALOGE("Invalid mesh or parameters"); |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 198 | mRSC->mHal.funcs.mesh.draw(mRSC, this, primIndex, start, len); |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 201 | void Mesh::uploadAll(Context *rsc) { |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 202 | for (uint32_t ct = 0; ct < mHal.state.vertexBuffersCount; ct ++) { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 203 | if (mHal.state.vertexBuffers[ct]) { |
| 204 | rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.vertexBuffers[ct]); |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 208 | for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 209 | if (mHal.state.indexBuffers[ct]) { |
| 210 | rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.indexBuffers[ct]); |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
Jason Sams | a89371c | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Jason Sams | e3150cf | 2012-07-24 18:10:20 -0700 | [diff] [blame] | 215 | void Mesh::computeBBox(Context *rsc) { |
Alex Sakhartchouk | ba4aa5c | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 216 | float *posPtr = NULL; |
| 217 | uint32_t vectorSize = 0; |
| 218 | uint32_t stride = 0; |
| 219 | uint32_t numVerts = 0; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 220 | Allocation *posAlloc = NULL; |
Alex Sakhartchouk | ba4aa5c | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 221 | // First we need to find the position ptr and stride |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 222 | for (uint32_t ct=0; ct < mHal.state.vertexBuffersCount; ct++) { |
| 223 | const Type *bufferType = mHal.state.vertexBuffers[ct]->getType(); |
Alex Sakhartchouk | ba4aa5c | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 224 | const Element *bufferElem = bufferType->getElement(); |
| 225 | |
| 226 | for (uint32_t ct=0; ct < bufferElem->getFieldCount(); ct++) { |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 227 | if (strcmp(bufferElem->getFieldName(ct), "position") == 0) { |
Alex Sakhartchouk | ba4aa5c | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 228 | vectorSize = bufferElem->getField(ct)->getComponent().getVectorSize(); |
| 229 | stride = bufferElem->getSizeBytes() / sizeof(float); |
| 230 | uint32_t offset = bufferElem->getFieldOffsetBytes(ct); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 231 | 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 Sakhartchouk | ba4aa5c | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 235 | numVerts = bufferType->getDimX(); |
| 236 | break; |
| 237 | } |
| 238 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 239 | if (posPtr) { |
Alex Sakhartchouk | ba4aa5c | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 1e6; |
| 245 | mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = -1e6; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 246 | if (!posPtr) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 247 | ALOGE("Unable to compute bounding box"); |
Alex Sakhartchouk | ba4aa5c | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 248 | mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 0.0f; |
| 249 | mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = 0.0f; |
| 250 | return; |
| 251 | } |
| 252 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 253 | for (uint32_t i = 0; i < numVerts; i ++) { |
| 254 | for (uint32_t v = 0; v < vectorSize; v ++) { |
Alex Sakhartchouk | ba4aa5c | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 255 | mBBoxMin[v] = rsMin(mBBoxMin[v], posPtr[v]); |
| 256 | mBBoxMax[v] = rsMax(mBBoxMax[v], posPtr[v]); |
| 257 | } |
| 258 | posPtr += stride; |
| 259 | } |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 260 | |
| 261 | if (posAlloc) { |
| 262 | rsc->mHal.funcs.allocation.unlock1D(rsc, posAlloc); |
| 263 | } |
Alex Sakhartchouk | ba4aa5c | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 266 | namespace android { |
| 267 | namespace renderscript { |
| 268 | |
Alex Sakhartchouk | 9003e56 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 269 | RsMesh rsi_MeshCreate(Context *rsc, |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 270 | RsAllocation * vtx, size_t vtxCount, |
| 271 | RsAllocation * idx, size_t idxCount, |
| 272 | uint32_t * primType, size_t primTypeCount) { |
Alex Sakhartchouk | 9003e56 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 273 | rsAssert(idxCount == primTypeCount); |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 274 | Mesh *sm = new Mesh(rsc, vtxCount, idxCount); |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 275 | sm->incUserRef(); |
| 276 | |
Alex Sakhartchouk | 9003e56 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 277 | for (uint32_t i = 0; i < vtxCount; i ++) { |
| 278 | sm->setVertexBuffer((Allocation*)vtx[i], i); |
| 279 | } |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 280 | |
Alex Sakhartchouk | 9003e56 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 281 | for (uint32_t i = 0; i < idxCount; i ++) { |
| 282 | sm->setPrimitive((Allocation*)idx[i], (RsPrimitive)primType[i], i); |
| 283 | } |
Alex Sakhartchouk | 4e9a7a8 | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 284 | |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 285 | sm->init(); |
Alex Sakhartchouk | 9003e56 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 286 | |
| 287 | return sm; |
Alex Sakhartchouk | 54929cc | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 290 | }} |
| 291 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 292 | void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx) { |
Alex Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 293 | Mesh *sm = static_cast<Mesh *>(mv); |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 294 | *numVtx = sm->mHal.state.vertexBuffersCount; |
Alex Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 297 | void rsaMeshGetIndexCount(RsContext con, RsMesh mv, int32_t *numIdx) { |
Alex Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 298 | Mesh *sm = static_cast<Mesh *>(mv); |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 299 | *numIdx = sm->mHal.state.primitivesCount; |
Alex Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 302 | void rsaMeshGetVertices(RsContext con, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount) { |
Alex Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 303 | Mesh *sm = static_cast<Mesh *>(mv); |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 304 | rsAssert(vtxDataCount == sm->mHal.state.vertexBuffersCount); |
Alex Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 305 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 306 | for (uint32_t ct = 0; ct < vtxDataCount; ct ++) { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 307 | vtxData[ct] = sm->mHal.state.vertexBuffers[ct]; |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 308 | sm->mHal.state.vertexBuffers[ct]->incUserRef(); |
Alex Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 312 | void rsaMeshGetIndices(RsContext con, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount) { |
Alex Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 313 | Mesh *sm = static_cast<Mesh *>(mv); |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 314 | rsAssert(idxDataCount == sm->mHal.state.primitivesCount); |
Alex Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 315 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 316 | for (uint32_t ct = 0; ct < idxDataCount; ct ++) { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 317 | 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 Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
Alex Sakhartchouk | d18c744 | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 323 | } |