blob: caaa9f557de47a4e09007298f1dd4a332b16c99a [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -070018#include "rsContext.h"
Jason Samse5ffb872009-08-09 17:01:55 -070019#include <GLES/gl.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070020#else
21#include "rsContextHostStub.h"
22#include <OpenGL/gl.h>
23#endif
Jason Sams326e0dd2009-05-22 14:03:28 -070024
25using namespace android;
26using namespace android::renderscript;
27
Jason Samse514b452009-09-25 14:51:22 -070028Type::Type(Context *rsc) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070029{
30 mLODs = 0;
31 mLODCount = 0;
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070032 mAttribs = NULL;
33 mAttribsSize = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070034 clear();
35}
36
Jason Sams225afd32010-10-21 14:06:55 -070037void Type::preDestroy()
Jason Sams326e0dd2009-05-22 14:03:28 -070038{
Jason Sams81549542010-02-17 15:38:10 -080039 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 }
Jason Sams225afd32010-10-21 14:06:55 -070045}
46
47Type::~Type()
48{
Jason Sams326e0dd2009-05-22 14:03:28 -070049 if (mLODs) {
50 delete [] mLODs;
Alex Sakhartchouk700ba382010-10-08 15:00:05 -070051 mLODs = NULL;
52 }
53 if(mAttribs) {
54 delete [] mAttribs;
55 mAttribs = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070056 }
57}
58
59void Type::clear()
60{
61 if (mLODs) {
62 delete [] mLODs;
63 mLODs = NULL;
64 }
65 mDimX = 0;
66 mDimY = 0;
67 mDimZ = 0;
68 mDimLOD = 0;
69 mFaces = false;
70 mElement.clear();
71}
72
73TypeState::TypeState()
74{
75}
76
77TypeState::~TypeState()
78{
79}
80
81size_t Type::getOffsetForFace(uint32_t face) const
82{
83 rsAssert(mFaces);
84 return 0;
85}
86
87void Type::compute()
88{
Jason Sams326e0dd2009-05-22 14:03:28 -070089 uint32_t oldLODCount = mLODCount;
90 if (mDimLOD) {
91 uint32_t l2x = rsFindHighBit(mDimX) + 1;
92 uint32_t l2y = rsFindHighBit(mDimY) + 1;
93 uint32_t l2z = rsFindHighBit(mDimZ) + 1;
94
95 mLODCount = rsMax(l2x, l2y);
96 mLODCount = rsMax(mLODCount, l2z);
97 } else {
98 mLODCount = 1;
99 }
100 if (mLODCount != oldLODCount) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700101 if(mLODs){
102 delete [] mLODs;
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700103 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700104 mLODs = new LOD[mLODCount];
105 }
106
Jason Sams326e0dd2009-05-22 14:03:28 -0700107 uint32_t tx = mDimX;
108 uint32_t ty = mDimY;
109 uint32_t tz = mDimZ;
110 size_t offset = 0;
111 for (uint32_t lod=0; lod < mLODCount; lod++) {
112 mLODs[lod].mX = tx;
113 mLODs[lod].mY = ty;
114 mLODs[lod].mZ = tz;
115 mLODs[lod].mOffset = offset;
Jason Sams326e0dd2009-05-22 14:03:28 -0700116 offset += tx * rsMax(ty, 1u) * rsMax(tz, 1u) * mElement->getSizeBytes();
Jason Sams7c528982010-01-07 16:25:08 -0800117 if (tx > 1) tx >>= 1;
118 if (ty > 1) ty >>= 1;
119 if (tz > 1) tz >>= 1;
Jason Sams326e0dd2009-05-22 14:03:28 -0700120 }
121
Jason Sams326e0dd2009-05-22 14:03:28 -0700122 // At this point the offset is the size of a mipmap chain;
123 mMipChainSizeBytes = offset;
124
125 if (mFaces) {
126 offset *= 6;
127 }
128 mTotalSizeBytes = offset;
129
Jason Samse5ffb872009-08-09 17:01:55 -0700130 makeGLComponents();
Jason Sams326e0dd2009-05-22 14:03:28 -0700131}
132
133uint32_t Type::getLODOffset(uint32_t lod, uint32_t x) const
134{
135 uint32_t offset = mLODs[lod].mOffset;
136 offset += x * mElement->getSizeBytes();
137 return offset;
138}
139
140uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y) const
141{
142 uint32_t offset = mLODs[lod].mOffset;
143 offset += (x + y * mLODs[lod].mX) * mElement->getSizeBytes();
144 return offset;
145}
146
147uint32_t Type::getLODOffset(uint32_t lod, uint32_t x, uint32_t y, uint32_t z) const
148{
149 uint32_t offset = mLODs[lod].mOffset;
150 offset += (x + y*mLODs[lod].mX + z*mLODs[lod].mX*mLODs[lod].mY) * mElement->getSizeBytes();
151 return offset;
152}
153
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700154bool Type::isValidGLComponent(uint32_t fieldIdx) {
155 // Do not create attribs for padding
156 if(mElement->getFieldName(fieldIdx)[0] == '#') {
157 return false;
158 }
159
160 // Only GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FIXED, GL_FLOAT are accepted.
161 // Filter rs types accordingly
162 RsDataType dt = mElement->getField(fieldIdx)->getComponent().getType();
163 if(dt != RS_TYPE_FLOAT_32 && dt != RS_TYPE_UNSIGNED_8 &&
164 dt != RS_TYPE_UNSIGNED_16 && dt != RS_TYPE_SIGNED_8 &&
165 dt != RS_TYPE_SIGNED_16) {
166 return false;
167 }
168
169 // Now make sure they are not arrays
170 uint32_t arraySize = mElement->getFieldArraySize(fieldIdx);
171 if(arraySize != 1) {
172 return false;
173 }
174
175 return true;
176}
Jason Sams326e0dd2009-05-22 14:03:28 -0700177
Jason Samse5ffb872009-08-09 17:01:55 -0700178void Type::makeGLComponents()
179{
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700180 // Count the number of gl attrs to initialize
181 mAttribsSize = 0;
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700182
183 for (uint32_t ct=0; ct < mElement->getFieldCount(); ct++) {
184 if(isValidGLComponent(ct)) {
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700185 mAttribsSize ++;
186 }
187 }
188 if(mAttribs) {
189 delete [] mAttribs;
190 mAttribs = NULL;
191 }
192 if(mAttribsSize) {
193 mAttribs = new VertexArray::Attrib[mAttribsSize];
194 }
195
Jason Sams433eca32010-01-06 11:57:52 -0800196 uint32_t userNum = 0;
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700197 for (uint32_t ct=0; ct < mElement->getFieldCount(); ct++) {
198 const Component &c = mElement->getField(ct)->getComponent();
Jason Samse5ffb872009-08-09 17:01:55 -0700199
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700200 if(!isValidGLComponent(ct)) {
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700201 continue;
202 }
203
Jason Sams79f52df2010-06-01 15:47:01 -0700204 mAttribs[userNum].size = c.getVectorSize();
205 mAttribs[userNum].offset = mElement->getFieldOffsetBytes(ct);
206 mAttribs[userNum].type = c.getGLType();
207 mAttribs[userNum].normalized = c.getType() != RS_TYPE_FLOAT_32;//c.getIsNormalized();
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700208 String8 tmp(RS_SHADER_ATTR);
Alex Sakhartchouk39f2ef62010-10-11 12:35:15 -0700209 tmp.append(mElement->getFieldName(ct));
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700210 mAttribs[userNum].name.setTo(tmp.string());
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700211
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700212 userNum ++;
Jason Samse5ffb872009-08-09 17:01:55 -0700213 }
214}
215
Jason Sams79f52df2010-06-01 15:47:01 -0700216
Jason Samsc460e552009-11-25 13:22:07 -0800217void Type::enableGLVertexBuffer(VertexArray *va) const
Jason Samse5ffb872009-08-09 17:01:55 -0700218{
Jason Sams433eca32010-01-06 11:57:52 -0800219 uint32_t stride = mElement->getSizeBytes();
Alex Sakhartchouk700ba382010-10-08 15:00:05 -0700220 for (uint32_t ct=0; ct < mAttribsSize; ct++) {
221 // Load up to RS_MAX_ATTRIBS inputs
222 // TODO: grow vertexarray dynamically
223 if(ct >= RS_MAX_ATTRIBS) {
224 LOGE("More GL attributes than we can handle");
225 break;
226 }
Jason Sams79f52df2010-06-01 15:47:01 -0700227 if (mAttribs[ct].size) {
228 va->add(mAttribs[ct], stride);
Jason Sams433eca32010-01-06 11:57:52 -0800229 }
230 }
231}
232
233
Jason Samse5ffb872009-08-09 17:01:55 -0700234
Jason Samse12c1c52009-09-27 17:50:38 -0700235void Type::dumpLOGV(const char *prefix) const
236{
237 char buf[1024];
238 ObjectBase::dumpLOGV(prefix);
239 LOGV("%s Type: x=%i y=%i z=%i mip=%i face=%i", prefix, mDimX, mDimY, mDimZ, mDimLOD, mFaces);
240 sprintf(buf, "%s element: ", prefix);
241 mElement->dumpLOGV(buf);
242}
243
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700244void Type::serialize(OStream *stream) const
245{
246 // Need to identify ourselves
247 stream->addU32((uint32_t)getClassId());
248
249 String8 name(getName());
250 stream->addString(&name);
251
252 mElement->serialize(stream);
253
254 stream->addU32(mDimX);
255 stream->addU32(mDimY);
256 stream->addU32(mDimZ);
257
258 stream->addU8((uint8_t)(mDimLOD ? 1 : 0));
259 stream->addU8((uint8_t)(mFaces ? 1 : 0));
260}
261
262Type *Type::createFromStream(Context *rsc, IStream *stream)
263{
264 // First make sure we are reading the correct object
Alex Sakhartchoukb825f672010-06-04 10:06:50 -0700265 RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
266 if(classID != RS_A3D_CLASS_ID_TYPE) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700267 LOGE("type loading skipped due to invalid class id\n");
268 return NULL;
269 }
270
271 String8 name;
272 stream->loadString(&name);
273
274 Element *elem = Element::createFromStream(rsc, stream);
275 if(!elem) {
276 return NULL;
277 }
278
Jason Samsf0c1df42010-10-26 13:09:17 -0700279 uint32_t x = stream->loadU32();
280 uint32_t y = stream->loadU32();
281 uint32_t z = stream->loadU32();
282 uint8_t lod = stream->loadU8();
283 uint8_t faces = stream->loadU8();
284 return Type::getType(rsc, elem, x, y, z, lod != 0, faces !=0 );
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700285}
286
Jason Samsef21edc2010-02-22 15:37:51 -0800287bool Type::getIsNp2() const
288{
289 uint32_t x = getDimX();
290 uint32_t y = getDimY();
291 uint32_t z = getDimZ();
292
293 if (x && (x & (x-1))) {
294 return true;
295 }
296 if (y && (y & (y-1))) {
297 return true;
298 }
299 if (z && (z & (z-1))) {
300 return true;
301 }
302 return false;
303}
304
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700305bool Type::isEqual(const Type *other) const {
306 if(other == NULL) {
307 return false;
308 }
309 if (other->getElement()->isEqual(getElement()) &&
310 other->getDimX() == mDimX &&
311 other->getDimY() == mDimY &&
312 other->getDimZ() == mDimZ &&
313 other->getDimLOD() == mDimLOD &&
314 other->getDimFaces() == mFaces) {
315 return true;
316 }
317 return false;
318}
Jason Samse12c1c52009-09-27 17:50:38 -0700319
Jason Samsf0c1df42010-10-26 13:09:17 -0700320Type * Type::getType(Context *rsc, const Element *e,
321 uint32_t dimX, uint32_t dimY, uint32_t dimZ,
322 bool dimLOD, bool dimFaces)
Jason Sams96abf812010-10-05 13:32:49 -0700323{
324 TypeState * stc = &rsc->mStateType;
Jason Samsf0c1df42010-10-26 13:09:17 -0700325
326 ObjectBase::asyncLock();
Jason Sams96abf812010-10-05 13:32:49 -0700327 for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
328 Type *t = stc->mTypes[ct];
Jason Samsf0c1df42010-10-26 13:09:17 -0700329 if (t->getElement() != e) continue;
Jason Sams96abf812010-10-05 13:32:49 -0700330 if (t->getDimX() != dimX) continue;
Jason Samsf0c1df42010-10-26 13:09:17 -0700331 if (t->getDimY() != dimY) continue;
332 if (t->getDimZ() != dimZ) continue;
333 if (t->getDimLOD() != dimLOD) continue;
334 if (t->getDimFaces() != dimFaces) continue;
Jason Sams96abf812010-10-05 13:32:49 -0700335 t->incUserRef();
Jason Samsf0c1df42010-10-26 13:09:17 -0700336 ObjectBase::asyncUnlock();
Jason Sams96abf812010-10-05 13:32:49 -0700337 return t;
338 }
Jason Samsf0c1df42010-10-26 13:09:17 -0700339 ObjectBase::asyncUnlock();
340
Jason Sams96abf812010-10-05 13:32:49 -0700341
342 Type *nt = new Type(rsc);
Jason Samsf0c1df42010-10-26 13:09:17 -0700343 nt->mElement.set(e);
Jason Sams96abf812010-10-05 13:32:49 -0700344 nt->mDimX = dimX;
Jason Samsf0c1df42010-10-26 13:09:17 -0700345 nt->mDimY = dimY;
346 nt->mDimZ = dimZ;
347 nt->mDimLOD = dimLOD;
348 nt->mFaces = dimFaces;
Jason Sams96abf812010-10-05 13:32:49 -0700349 nt->compute();
Jason Samsf0c1df42010-10-26 13:09:17 -0700350 nt->incUserRef();
351
352 ObjectBase::asyncLock();
353 stc->mTypes.push(nt);
354 ObjectBase::asyncUnlock();
355
Jason Sams96abf812010-10-05 13:32:49 -0700356 return nt;
357}
358
Jason Samsf0c1df42010-10-26 13:09:17 -0700359Type * Type::cloneAndResize1D(Context *rsc, uint32_t dimX) const
360{
361 return getType(rsc, mElement.get(), dimX,
362 mDimY, mDimZ, mDimLOD, mFaces);
363}
364
Jason Sams96abf812010-10-05 13:32:49 -0700365Type * Type::cloneAndResize2D(Context *rsc, uint32_t dimX, uint32_t dimY) const
366{
Jason Samsf0c1df42010-10-26 13:09:17 -0700367 return getType(rsc, mElement.get(), dimX, dimY,
368 mDimZ, mDimLOD, mFaces);
Jason Sams96abf812010-10-05 13:32:49 -0700369}
370
371
Jason Sams326e0dd2009-05-22 14:03:28 -0700372//////////////////////////////////////////////////
Jason Samse5ffb872009-08-09 17:01:55 -0700373//
Jason Sams326e0dd2009-05-22 14:03:28 -0700374namespace android {
375namespace renderscript {
376
Jason Sams326e0dd2009-05-22 14:03:28 -0700377}
378}
379
Jason Sams225afd32010-10-21 14:06:55 -0700380RsType rsaTypeCreate(RsContext con, RsElement _e, uint32_t dimCount,
Jason Sams2353ae32010-10-14 17:48:46 -0700381 const RsDimension *dims, const uint32_t *vals)
382{
383 Context *rsc = static_cast<Context *>(con);
384 Element *e = static_cast<Element *>(_e);
385 TypeState * stc = &rsc->mStateType;
386
387 uint32_t dimX = 0;
388 uint32_t dimY = 0;
389 uint32_t dimZ = 0;
390 uint32_t dimLOD = 0;
391 uint32_t dimFaces = 0;
392
393 for (uint32_t ct=0; ct < dimCount; ct++) {
394 switch(dims[ct]) {
395 case RS_DIMENSION_X: dimX = vals[ct]; break;
396 case RS_DIMENSION_Y: dimY = vals[ct]; break;
397 case RS_DIMENSION_Z: dimZ = vals[ct]; break;
398 case RS_DIMENSION_LOD: dimLOD = vals[ct]; break;
399 case RS_DIMENSION_FACE: dimFaces = vals[ct]; break;
400
401 default:
402 LOGE("rsaTypeCreate: Bad dimension");
403 rsAssert(0);
404 }
405 }
406
Jason Samsf0c1df42010-10-26 13:09:17 -0700407 return Type::getType(rsc, e, dimX, dimY, dimZ, dimLOD, dimFaces);
Jason Sams2353ae32010-10-14 17:48:46 -0700408}
409
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700410void rsaTypeGetNativeData(RsContext con, RsType type, uint32_t *typeData, uint32_t typeDataSize)
411{
412 rsAssert(typeDataSize == 6);
413 // Pack the data in the follofing way mDimX; mDimY; mDimZ;
414 // mDimLOD; mDimFaces; mElement; into typeData
415 Type *t = static_cast<Type *>(type);
416
417 (*typeData++) = t->getDimX();
418 (*typeData++) = t->getDimY();
419 (*typeData++) = t->getDimZ();
420 (*typeData++) = t->getDimLOD();
421 (*typeData++) = t->getDimFaces() ? 1 : 0;
422 (*typeData++) = (uint32_t)t->getElement();
423 t->getElement()->incUserRef();
424}