Jason Sams | 7c878f3 | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 1 | /* |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Jason Sams | 7c878f3 | 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 | bedc023 | 2012-03-09 10:47:27 -0800 | [diff] [blame] | 18 | #include "rsMesh.h" |
Alex Sakhartchouk | 137ccbb | 2012-03-09 09:24:39 -0800 | [diff] [blame] | 19 | #include "rs.h" |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 20 | |
Jason Sams | 7c878f3 | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 21 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 24 | Mesh::Mesh(Context *rsc) : ObjectBase(rsc) { |
Alex Sakhartchouk | 4a36b45 | 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 | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 28 | mHal.state.indexBuffers = NULL; |
| 29 | mHal.state.indexBuffersCount = 0; |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 30 | mHal.state.vertexBuffers = NULL; |
| 31 | mHal.state.vertexBuffersCount = 0; |
| 32 | mInitialized = false; |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 33 | |
| 34 | mVertexBuffers = NULL; |
| 35 | mIndexBuffers = NULL; |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 36 | } |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 37 | |
Alex Sakhartchouk | 4a36b45 | 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 | 5ef2f53 | 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 | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 46 | for (uint32_t i = 0; i < mHal.state.primitivesCount; i ++) { |
Alex Sakhartchouk | 5ef2f53 | 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 | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 51 | } |
| 52 | mHal.state.vertexBuffersCount = vertexBuffersCount; |
Alex Sakhartchouk | 5ef2f53 | 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 | 7c878f3 | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 62 | Mesh::~Mesh() { |
Alex Sakhartchouk | d0f5bd1 | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 63 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 64 | mRSC->mHal.funcs.mesh.destroy(mRSC, this); |
| 65 | #endif |
| 66 | |
Alex Sakhartchouk | 5ef2f53 | 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 | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 70 | |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 71 | delete[] mVertexBuffers; |
| 72 | delete[] mIndexBuffers; |
Alex Sakhartchouk | 4a36b45 | 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 | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 78 | #endif |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 81 | void 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 Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 89 | stream->addU32(mHal.state.vertexBuffersCount); |
| 90 | for (uint32_t vCount = 0; vCount < mHal.state.vertexBuffersCount; vCount ++) { |
| 91 | mHal.state.vertexBuffers[vCount]->serialize(stream); |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 94 | stream->addU32(mHal.state.primitivesCount); |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 95 | // Store the primitives |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 96 | for (uint32_t pCount = 0; pCount < mHal.state.primitivesCount; pCount ++) { |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 97 | stream->addU8((uint8_t)mHal.state.primitives[pCount]); |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 98 | |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 99 | if (mHal.state.indexBuffers[pCount]) { |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 100 | stream->addU32(1); |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 101 | mHal.state.indexBuffers[pCount]->serialize(stream); |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 102 | } else { |
| 103 | stream->addU32(0); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | Mesh *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 Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 112 | ALOGE("mesh loading skipped due to invalid class id"); |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 113 | return NULL; |
| 114 | } |
| 115 | |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 116 | String8 name; |
| 117 | stream->loadString(&name); |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 118 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 119 | uint32_t vertexBuffersCount = stream->loadU32(); |
| 120 | ObjectBaseRef<Allocation> *vertexBuffers = NULL; |
| 121 | if (vertexBuffersCount) { |
| 122 | vertexBuffers = new ObjectBaseRef<Allocation>[vertexBuffersCount]; |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 123 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 124 | for (uint32_t vCount = 0; vCount < vertexBuffersCount; vCount ++) { |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 125 | Allocation *vertexAlloc = Allocation::createFromStream(rsc, stream); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 126 | vertexBuffers[vCount].set(vertexAlloc); |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 130 | 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 Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 136 | |
| 137 | // load all primitives |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 138 | for (uint32_t pCount = 0; pCount < primitivesCount; pCount ++) { |
| 139 | primitives[pCount] = (RsPrimitive)stream->loadU8(); |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 140 | |
| 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 Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 145 | indexBuffers[pCount].set(indexAlloc); |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 150 | 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 Sakhartchouk | d0f5bd1 | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 168 | #ifndef ANDROID_RS_SERIALIZE |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 169 | mesh->init(); |
Alex Sakhartchouk | a3b5960 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 170 | mesh->uploadAll(rsc); |
| 171 | #endif |
| 172 | return mesh; |
| 173 | } |
| 174 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 175 | void Mesh::render(Context *rsc) const { |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 176 | for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) { |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 177 | renderPrimitive(rsc, ct); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void Mesh::renderPrimitive(Context *rsc, uint32_t primIndex) const { |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 182 | if (primIndex >= mHal.state.primitivesCount) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 183 | ALOGE("Invalid primitive index"); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 187 | if (mHal.state.indexBuffers[primIndex]) { |
| 188 | renderPrimitiveRange(rsc, primIndex, 0, mHal.state.indexBuffers[primIndex]->getType()->getDimX()); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 192 | renderPrimitiveRange(rsc, primIndex, 0, mHal.state.vertexBuffers[0]->getType()->getDimX()); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 195 | void Mesh::renderPrimitiveRange(Context *rsc, uint32_t primIndex, uint32_t start, uint32_t len) const { |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 196 | if (len < 1 || primIndex >= mHal.state.primitivesCount) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 197 | ALOGE("Invalid mesh or parameters"); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 201 | mRSC->mHal.funcs.mesh.draw(mRSC, this, primIndex, start, len); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 204 | void Mesh::uploadAll(Context *rsc) { |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 205 | for (uint32_t ct = 0; ct < mHal.state.vertexBuffersCount; ct ++) { |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 206 | if (mHal.state.vertexBuffers[ct]) { |
| 207 | rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.vertexBuffers[ct]); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 211 | for (uint32_t ct = 0; ct < mHal.state.primitivesCount; ct ++) { |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 212 | if (mHal.state.indexBuffers[ct]) { |
| 213 | rsc->mHal.funcs.allocation.markDirty(rsc, mHal.state.indexBuffers[ct]); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
Jason Sams | 7c878f3 | 2009-06-30 14:13:04 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Alex Sakhartchouk | a80145d | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 218 | void 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 Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 224 | for (uint32_t ct=0; ct < mHal.state.vertexBuffersCount; ct++) { |
| 225 | const Type *bufferType = mHal.state.vertexBuffers[ct]->getType(); |
Alex Sakhartchouk | a80145d | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 226 | const Element *bufferElem = bufferType->getElement(); |
| 227 | |
| 228 | for (uint32_t ct=0; ct < bufferElem->getFieldCount(); ct++) { |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 229 | if (strcmp(bufferElem->getFieldName(ct), "position") == 0) { |
Alex Sakhartchouk | a80145d | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 230 | vectorSize = bufferElem->getField(ct)->getComponent().getVectorSize(); |
| 231 | stride = bufferElem->getSizeBytes() / sizeof(float); |
| 232 | uint32_t offset = bufferElem->getFieldOffsetBytes(ct); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 233 | posPtr = (float*)((uint8_t*)mHal.state.vertexBuffers[ct]->getPtr() + offset); |
Alex Sakhartchouk | a80145d | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 234 | numVerts = bufferType->getDimX(); |
| 235 | break; |
| 236 | } |
| 237 | } |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 238 | if (posPtr) { |
Alex Sakhartchouk | a80145d | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 1e6; |
| 244 | mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = -1e6; |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 245 | if (!posPtr) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 246 | ALOGE("Unable to compute bounding box"); |
Alex Sakhartchouk | a80145d | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 247 | mBBoxMin[0] = mBBoxMin[1] = mBBoxMin[2] = 0.0f; |
| 248 | mBBoxMax[0] = mBBoxMax[1] = mBBoxMax[2] = 0.0f; |
| 249 | return; |
| 250 | } |
| 251 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 252 | for (uint32_t i = 0; i < numVerts; i ++) { |
| 253 | for (uint32_t v = 0; v < vectorSize; v ++) { |
Alex Sakhartchouk | a80145d | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 254 | mBBoxMin[v] = rsMin(mBBoxMin[v], posPtr[v]); |
| 255 | mBBoxMax[v] = rsMax(mBBoxMax[v], posPtr[v]); |
| 256 | } |
| 257 | posPtr += stride; |
| 258 | } |
| 259 | } |
| 260 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 261 | namespace android { |
| 262 | namespace renderscript { |
| 263 | |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 264 | RsMesh rsi_MeshCreate(Context *rsc, |
Alex Sakhartchouk | 17a8a19 | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 265 | RsAllocation * vtx, size_t vtxCount, |
| 266 | RsAllocation * idx, size_t idxCount, |
| 267 | uint32_t * primType, size_t primTypeCount) { |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 268 | rsAssert(idxCount == primTypeCount); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 269 | Mesh *sm = new Mesh(rsc, vtxCount, idxCount); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 270 | sm->incUserRef(); |
| 271 | |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 272 | for (uint32_t i = 0; i < vtxCount; i ++) { |
| 273 | sm->setVertexBuffer((Allocation*)vtx[i], i); |
| 274 | } |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 275 | |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 276 | for (uint32_t i = 0; i < idxCount; i ++) { |
| 277 | sm->setPrimitive((Allocation*)idx[i], (RsPrimitive)primType[i], i); |
| 278 | } |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 279 | |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 280 | sm->init(); |
Alex Sakhartchouk | 25999a0 | 2011-05-12 10:38:03 -0700 | [diff] [blame] | 281 | |
| 282 | return sm; |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Alex Sakhartchouk | 581cc64 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 285 | }} |
| 286 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 287 | void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx) { |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 288 | Mesh *sm = static_cast<Mesh *>(mv); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 289 | *numVtx = sm->mHal.state.vertexBuffersCount; |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 292 | void rsaMeshGetIndexCount(RsContext con, RsMesh mv, int32_t *numIdx) { |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 293 | Mesh *sm = static_cast<Mesh *>(mv); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 294 | *numIdx = sm->mHal.state.primitivesCount; |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 297 | void rsaMeshGetVertices(RsContext con, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount) { |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 298 | Mesh *sm = static_cast<Mesh *>(mv); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 299 | rsAssert(vtxDataCount == sm->mHal.state.vertexBuffersCount); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 300 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 301 | for (uint32_t ct = 0; ct < vtxDataCount; ct ++) { |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 302 | vtxData[ct] = sm->mHal.state.vertexBuffers[ct]; |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 303 | sm->mHal.state.vertexBuffers[ct]->incUserRef(); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 307 | void rsaMeshGetIndices(RsContext con, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount) { |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 308 | Mesh *sm = static_cast<Mesh *>(mv); |
Alex Sakhartchouk | 4a36b45 | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 309 | rsAssert(idxDataCount == sm->mHal.state.primitivesCount); |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 310 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 311 | for (uint32_t ct = 0; ct < idxDataCount; ct ++) { |
Alex Sakhartchouk | 5ef2f53 | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 312 | 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 Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
Alex Sakhartchouk | 80a4c2c | 2010-07-12 15:50:32 -0700 | [diff] [blame] | 318 | } |