rsType.cpp revision d3e0ad43dc758c409fc23d1893dab67b18520c24
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    uint32_t userNum = 0;
149
150    for (uint32_t ct=0; ct < getElement()->getFieldCount(); ct++) {
151        const Component &c = getElement()->getField(ct)->getComponent();
152
153        mAttribs[userNum].size = c.getVectorSize();
154        mAttribs[userNum].offset = mElement->getFieldOffsetBytes(ct);
155        mAttribs[userNum].type = c.getGLType();
156        mAttribs[userNum].normalized = c.getType() != RS_TYPE_FLOAT_32;//c.getIsNormalized();
157        mAttribs[userNum].name.setTo(getElement()->getFieldName(ct));
158        userNum ++;
159    }
160}
161
162
163void Type::enableGLVertexBuffer(VertexArray *va) const
164{
165    uint32_t stride = mElement->getSizeBytes();
166    for (uint32_t ct=0; ct < RS_MAX_ATTRIBS; ct++) {
167        if (mAttribs[ct].size) {
168            va->add(mAttribs[ct], stride);
169        }
170    }
171}
172
173
174
175void Type::dumpLOGV(const char *prefix) const
176{
177    char buf[1024];
178    ObjectBase::dumpLOGV(prefix);
179    LOGV("%s   Type: x=%i y=%i z=%i mip=%i face=%i", prefix, mDimX, mDimY, mDimZ, mDimLOD, mFaces);
180    sprintf(buf, "%s element: ", prefix);
181    mElement->dumpLOGV(buf);
182}
183
184void Type::serialize(OStream *stream) const
185{
186    // Need to identify ourselves
187    stream->addU32((uint32_t)getClassId());
188
189    String8 name(getName());
190    stream->addString(&name);
191
192    mElement->serialize(stream);
193
194    stream->addU32(mDimX);
195    stream->addU32(mDimY);
196    stream->addU32(mDimZ);
197
198    stream->addU8((uint8_t)(mDimLOD ? 1 : 0));
199    stream->addU8((uint8_t)(mFaces ? 1 : 0));
200}
201
202Type *Type::createFromStream(Context *rsc, IStream *stream)
203{
204    // First make sure we are reading the correct object
205    RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
206    if(classID != RS_A3D_CLASS_ID_TYPE) {
207        LOGE("type loading skipped due to invalid class id\n");
208        return NULL;
209    }
210
211    String8 name;
212    stream->loadString(&name);
213
214    Element *elem = Element::createFromStream(rsc, stream);
215    if(!elem) {
216        return NULL;
217    }
218
219    Type *type = new Type(rsc);
220    type->mDimX = stream->loadU32();
221    type->mDimY = stream->loadU32();
222    type->mDimZ = stream->loadU32();
223
224    uint8_t temp = stream->loadU8();
225    type->mDimLOD = temp != 0;
226
227    temp = stream->loadU8();
228    type->mFaces = temp != 0;
229
230    type->setElement(elem);
231
232    return type;
233}
234
235bool Type::getIsNp2() const
236{
237    uint32_t x = getDimX();
238    uint32_t y = getDimY();
239    uint32_t z = getDimZ();
240
241    if (x && (x & (x-1))) {
242        return true;
243    }
244    if (y && (y & (y-1))) {
245        return true;
246    }
247    if (z && (z & (z-1))) {
248        return true;
249    }
250    return false;
251}
252
253
254//////////////////////////////////////////////////
255//
256namespace android {
257namespace renderscript {
258
259void rsi_TypeBegin(Context *rsc, RsElement vse)
260{
261    TypeState * stc = &rsc->mStateType;
262
263    stc->mX = 0;
264    stc->mY = 0;
265    stc->mZ = 0;
266    stc->mLOD = false;
267    stc->mFaces = false;
268    stc->mElement.set(static_cast<const Element *>(vse));
269}
270
271void rsi_TypeAdd(Context *rsc, RsDimension dim, size_t value)
272{
273    TypeState * stc = &rsc->mStateType;
274
275    if (dim < 0) {
276        //error
277        return;
278    }
279
280
281    switch (dim) {
282    case RS_DIMENSION_X:
283        stc->mX = value;
284        return;
285    case RS_DIMENSION_Y:
286        stc->mY = value;
287        return;
288    case RS_DIMENSION_Z:
289        stc->mZ = value;
290        return;
291    case RS_DIMENSION_FACE:
292        stc->mFaces = (value != 0);
293        return;
294    case RS_DIMENSION_LOD:
295        stc->mLOD = (value != 0);
296        return;
297    default:
298        break;
299    }
300
301
302    int32_t arrayNum = dim - RS_DIMENSION_ARRAY_0;
303    if ((dim < 0) || (dim > RS_DIMENSION_MAX)) {
304        LOGE("rsTypeAdd: Bad dimension");
305        //error
306        return;
307    }
308
309    // todo: implement array support
310
311}
312
313RsType rsi_TypeCreate(Context *rsc)
314{
315    TypeState * stc = &rsc->mStateType;
316
317    for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
318        Type *t = stc->mTypes[ct];
319        if (t->getElement() != stc->mElement.get()) continue;
320        if (t->getDimX() != stc->mX) continue;
321        if (t->getDimY() != stc->mY) continue;
322        if (t->getDimZ() != stc->mZ) continue;
323        if (t->getDimLOD() != stc->mLOD) continue;
324        if (t->getDimFaces() != stc->mFaces) continue;
325        t->incUserRef();
326        return t;
327    }
328
329    Type * st = new Type(rsc);
330    st->incUserRef();
331    st->setDimX(stc->mX);
332    st->setDimY(stc->mY);
333    st->setDimZ(stc->mZ);
334    st->setElement(stc->mElement.get());
335    st->setDimLOD(stc->mLOD);
336    st->setDimFaces(stc->mFaces);
337    st->compute();
338    stc->mElement.clear();
339    stc->mTypes.push(st);
340    return st;
341}
342
343
344}
345}
346
347