rsType.cpp revision 032f3590daa7c255a0c20decf0c3b2bee949d8b8
1/* 2 * Copyright (C) 2009 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 17#ifndef ANDROID_RS_BUILD_FOR_HOST 18#include "rsContext.h" 19#include <GLES/gl.h> 20#else 21#include "rsContextHostStub.h" 22#include <OpenGL/gl.h> 23#endif 24 25using namespace android; 26using namespace android::renderscript; 27 28Type::Type(Context *rsc) : ObjectBase(rsc) 29{ 30 mAllocFile = __FILE__; 31 mAllocLine = __LINE__; 32 mLODs = 0; 33 mLODCount = 0; 34 clear(); 35} 36 37Type::~Type() 38{ 39 for (uint32_t ct = 0; ct < mRSC->mStateType.mTypes.size(); ct++) { 40 if (mRSC->mStateType.mTypes[ct] == this) { 41 mRSC->mStateType.mTypes.removeAt(ct); 42 break; 43 } 44 } 45 if (mLODs) { 46 delete [] mLODs; 47 } 48} 49 50void Type::clear() 51{ 52 if (mLODs) { 53 delete [] mLODs; 54 mLODs = NULL; 55 } 56 mDimX = 0; 57 mDimY = 0; 58 mDimZ = 0; 59 mDimLOD = 0; 60 mFaces = false; 61 mElement.clear(); 62} 63 64TypeState::TypeState() 65{ 66} 67 68TypeState::~TypeState() 69{ 70} 71 72size_t Type::getOffsetForFace(uint32_t face) const 73{ 74 rsAssert(mFaces); 75 return 0; 76} 77 78void Type::compute() 79{ 80 uint32_t oldLODCount = mLODCount; 81 if (mDimLOD) { 82 uint32_t l2x = rsFindHighBit(mDimX) + 1; 83 uint32_t l2y = rsFindHighBit(mDimY) + 1; 84 uint32_t l2z = rsFindHighBit(mDimZ) + 1; 85 86 mLODCount = rsMax(l2x, l2y); 87 mLODCount = rsMax(mLODCount, l2z); 88 } else { 89 mLODCount = 1; 90 } 91 if (mLODCount != oldLODCount) { 92 if(mLODs){ 93 delete [] mLODs; 94 } 95 mLODs = new LOD[mLODCount]; 96 } 97 98 uint32_t tx = mDimX; 99 uint32_t ty = mDimY; 100 uint32_t tz = mDimZ; 101 size_t offset = 0; 102 for (uint32_t lod=0; lod < mLODCount; lod++) { 103 mLODs[lod].mX = tx; 104 mLODs[lod].mY = ty; 105 mLODs[lod].mZ = tz; 106 mLODs[lod].mOffset = offset; 107 offset += tx * rsMax(ty, 1u) * rsMax(tz, 1u) * mElement->getSizeBytes(); 108 if (tx > 1) tx >>= 1; 109 if (ty > 1) ty >>= 1; 110 if (tz > 1) tz >>= 1; 111 } 112 113 // At this point the offset is the size of a mipmap chain; 114 mMipChainSizeBytes = offset; 115 116 if (mFaces) { 117 offset *= 6; 118 } 119 mTotalSizeBytes = offset; 120 121 makeGLComponents(); 122} 123 124uint32_t Type::getLODOffset(uint32_t lod, uint32_t x) const 125{ 126 uint32_t offset = mLODs[lod].mOffset; 127 offset += x * mElement->getSizeBytes(); 128 return offset; 129} 130 131uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y) const 132{ 133 uint32_t offset = mLODs[lod].mOffset; 134 offset += (x + y * mLODs[lod].mX) * mElement->getSizeBytes(); 135 return offset; 136} 137 138uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y, uint32_t z) const 139{ 140 uint32_t offset = mLODs[lod].mOffset; 141 offset += (x + y*mLODs[lod].mX + z*mLODs[lod].mX*mLODs[lod].mY) * mElement->getSizeBytes(); 142 return offset; 143} 144 145 146void Type::makeGLComponents() 147{ 148 if(getElement()->getFieldCount() >= RS_MAX_ATTRIBS) { 149 return; 150 } 151 152 uint32_t userNum = 0; 153 154 for (uint32_t ct=0; ct < getElement()->getFieldCount(); ct++) { 155 const Component &c = getElement()->getField(ct)->getComponent(); 156 157 mAttribs[userNum].size = c.getVectorSize(); 158 mAttribs[userNum].offset = mElement->getFieldOffsetBytes(ct); 159 mAttribs[userNum].type = c.getGLType(); 160 mAttribs[userNum].normalized = c.getType() != RS_TYPE_FLOAT_32;//c.getIsNormalized(); 161 mAttribs[userNum].name.setTo(getElement()->getFieldName(ct)); 162 userNum ++; 163 } 164} 165 166 167void Type::enableGLVertexBuffer(VertexArray *va) const 168{ 169 uint32_t stride = mElement->getSizeBytes(); 170 for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) { 171 if (mAttribs[ct].size) { 172 va->add(mAttribs[ct], stride); 173 } 174 } 175} 176 177 178 179void Type::dumpLOGV(const char *prefix) const 180{ 181 char buf[1024]; 182 ObjectBase::dumpLOGV(prefix); 183 LOGV("%s Type: x=%i y=%i z=%i mip=%i face=%i", prefix, mDimX, mDimY, mDimZ, mDimLOD, mFaces); 184 sprintf(buf, "%s element: ", prefix); 185 mElement->dumpLOGV(buf); 186} 187 188void Type::serialize(OStream *stream) const 189{ 190 // Need to identify ourselves 191 stream->addU32((uint32_t)getClassId()); 192 193 String8 name(getName()); 194 stream->addString(&name); 195 196 mElement->serialize(stream); 197 198 stream->addU32(mDimX); 199 stream->addU32(mDimY); 200 stream->addU32(mDimZ); 201 202 stream->addU8((uint8_t)(mDimLOD ? 1 : 0)); 203 stream->addU8((uint8_t)(mFaces ? 1 : 0)); 204} 205 206Type *Type::createFromStream(Context *rsc, IStream *stream) 207{ 208 // First make sure we are reading the correct object 209 RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); 210 if(classID != RS_A3D_CLASS_ID_TYPE) { 211 LOGE("type loading skipped due to invalid class id\n"); 212 return NULL; 213 } 214 215 String8 name; 216 stream->loadString(&name); 217 218 Element *elem = Element::createFromStream(rsc, stream); 219 if(!elem) { 220 return NULL; 221 } 222 223 Type *type = new Type(rsc); 224 type->mDimX = stream->loadU32(); 225 type->mDimY = stream->loadU32(); 226 type->mDimZ = stream->loadU32(); 227 228 uint8_t temp = stream->loadU8(); 229 type->mDimLOD = temp != 0; 230 231 temp = stream->loadU8(); 232 type->mFaces = temp != 0; 233 234 type->setElement(elem); 235 236 return type; 237} 238 239bool Type::getIsNp2() const 240{ 241 uint32_t x = getDimX(); 242 uint32_t y = getDimY(); 243 uint32_t z = getDimZ(); 244 245 if (x && (x & (x-1))) { 246 return true; 247 } 248 if (y && (y & (y-1))) { 249 return true; 250 } 251 if (z && (z & (z-1))) { 252 return true; 253 } 254 return false; 255} 256 257 258////////////////////////////////////////////////// 259// 260namespace android { 261namespace renderscript { 262 263void rsi_TypeBegin(Context *rsc, RsElement vse) 264{ 265 TypeState * stc = &rsc->mStateType; 266 267 stc->mX = 0; 268 stc->mY = 0; 269 stc->mZ = 0; 270 stc->mLOD = false; 271 stc->mFaces = false; 272 stc->mElement.set(static_cast<const Element *>(vse)); 273} 274 275void rsi_TypeAdd(Context *rsc, RsDimension dim, size_t value) 276{ 277 TypeState * stc = &rsc->mStateType; 278 279 if (dim < 0) { 280 //error 281 return; 282 } 283 284 285 switch (dim) { 286 case RS_DIMENSION_X: 287 stc->mX = value; 288 return; 289 case RS_DIMENSION_Y: 290 stc->mY = value; 291 return; 292 case RS_DIMENSION_Z: 293 stc->mZ = value; 294 return; 295 case RS_DIMENSION_FACE: 296 stc->mFaces = (value != 0); 297 return; 298 case RS_DIMENSION_LOD: 299 stc->mLOD = (value != 0); 300 return; 301 default: 302 break; 303 } 304 305 306 int32_t arrayNum = dim - RS_DIMENSION_ARRAY_0; 307 if ((dim < 0) || (dim > RS_DIMENSION_MAX)) { 308 LOGE("rsTypeAdd: Bad dimension"); 309 //error 310 return; 311 } 312 313 // todo: implement array support 314 315} 316 317RsType rsi_TypeCreate(Context *rsc) 318{ 319 TypeState * stc = &rsc->mStateType; 320 321 for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) { 322 Type *t = stc->mTypes[ct]; 323 if (t->getElement() != stc->mElement.get()) continue; 324 if (t->getDimX() != stc->mX) continue; 325 if (t->getDimY() != stc->mY) continue; 326 if (t->getDimZ() != stc->mZ) continue; 327 if (t->getDimLOD() != stc->mLOD) continue; 328 if (t->getDimFaces() != stc->mFaces) continue; 329 t->incUserRef(); 330 return t; 331 } 332 333 Type * st = new Type(rsc); 334 st->incUserRef(); 335 st->setDimX(stc->mX); 336 st->setDimY(stc->mY); 337 st->setDimZ(stc->mZ); 338 st->setElement(stc->mElement.get()); 339 st->setDimLOD(stc->mLOD); 340 st->setDimFaces(stc->mFaces); 341 st->compute(); 342 stc->mElement.clear(); 343 stc->mTypes.push(st); 344 return st; 345} 346 347void rsi_TypeGetNativeData(Context *rsc, RsType type, uint32_t *typeData, uint32_t typeDataSize) 348{ 349 rsAssert(typeDataSize == 6); 350 // Pack the data in the follofing way mDimX; mDimY; mDimZ; 351 // mDimLOD; mDimFaces; mElement; into typeData 352 Type *t = static_cast<Type *>(type); 353 354 (*typeData++) = t->getDimX(); 355 (*typeData++) = t->getDimY(); 356 (*typeData++) = t->getDimZ(); 357 (*typeData++) = t->getDimLOD(); 358 (*typeData++) = t->getDimFaces() ? 1 : 0; 359 (*typeData++) = (uint32_t)t->getElement(); 360 361} 362 363 364} 365} 366 367