blob: 82ad33e42c1c4e5710cb349e47a0025e014aeea8 [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
279 Type *type = new Type(rsc);
280 type->mDimX = stream->loadU32();
281 type->mDimY = stream->loadU32();
282 type->mDimZ = stream->loadU32();
283
284 uint8_t temp = stream->loadU8();
285 type->mDimLOD = temp != 0;
286
287 temp = stream->loadU8();
288 type->mFaces = temp != 0;
289
290 type->setElement(elem);
291
292 return type;
293}
294
Jason Samsef21edc2010-02-22 15:37:51 -0800295bool Type::getIsNp2() const
296{
297 uint32_t x = getDimX();
298 uint32_t y = getDimY();
299 uint32_t z = getDimZ();
300
301 if (x && (x & (x-1))) {
302 return true;
303 }
304 if (y && (y & (y-1))) {
305 return true;
306 }
307 if (z && (z & (z-1))) {
308 return true;
309 }
310 return false;
311}
312
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700313bool Type::isEqual(const Type *other) const {
314 if(other == NULL) {
315 return false;
316 }
317 if (other->getElement()->isEqual(getElement()) &&
318 other->getDimX() == mDimX &&
319 other->getDimY() == mDimY &&
320 other->getDimZ() == mDimZ &&
321 other->getDimLOD() == mDimLOD &&
322 other->getDimFaces() == mFaces) {
323 return true;
324 }
325 return false;
326}
Jason Samse12c1c52009-09-27 17:50:38 -0700327
Jason Sams96abf812010-10-05 13:32:49 -0700328Type * Type::cloneAndResize1D(Context *rsc, uint32_t dimX) const
329{
330 TypeState * stc = &rsc->mStateType;
331 for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
332 Type *t = stc->mTypes[ct];
333 if (t->getElement() != mElement.get()) continue;
334 if (t->getDimX() != dimX) continue;
335 if (t->getDimY() != mDimY) continue;
336 if (t->getDimZ() != mDimZ) continue;
337 if (t->getDimLOD() != mDimLOD) continue;
338 if (t->getDimFaces() != mFaces) continue;
339 t->incUserRef();
340 return t;
341 }
342
343 Type *nt = new Type(rsc);
344 nt->mElement.set(mElement);
345 nt->mDimX = dimX;
346 nt->mDimY = mDimY;
347 nt->mDimZ = mDimZ;
348 nt->mDimLOD = mDimLOD;
349 nt->mFaces = mFaces;
350 nt->compute();
351 return nt;
352}
353
354Type * Type::cloneAndResize2D(Context *rsc, uint32_t dimX, uint32_t dimY) const
355{
356 TypeState * stc = &rsc->mStateType;
357 for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
358 Type *t = stc->mTypes[ct];
359 if (t->getElement() != mElement.get()) continue;
360 if (t->getDimX() != dimX) continue;
361 if (t->getDimY() != dimY) continue;
362 if (t->getDimZ() != mDimZ) continue;
363 if (t->getDimLOD() != mDimLOD) continue;
364 if (t->getDimFaces() != mFaces) continue;
365 t->incUserRef();
366 return t;
367 }
368
369 Type *nt = new Type(rsc);
370 nt->mElement.set(mElement);
371 nt->mDimX = dimX;
372 nt->mDimY = dimY;
373 nt->mDimZ = mDimZ;
374 nt->mDimLOD = mDimLOD;
375 nt->mFaces = mFaces;
376 nt->compute();
377 return nt;
378}
379
380
Jason Sams326e0dd2009-05-22 14:03:28 -0700381//////////////////////////////////////////////////
Jason Samse5ffb872009-08-09 17:01:55 -0700382//
Jason Sams326e0dd2009-05-22 14:03:28 -0700383namespace android {
384namespace renderscript {
385
Alex Sakhartchouk417e6a42010-07-15 11:33:03 -0700386void rsi_TypeGetNativeData(Context *rsc, RsType type, uint32_t *typeData, uint32_t typeDataSize)
387{
388 rsAssert(typeDataSize == 6);
389 // Pack the data in the follofing way mDimX; mDimY; mDimZ;
390 // mDimLOD; mDimFaces; mElement; into typeData
391 Type *t = static_cast<Type *>(type);
392
393 (*typeData++) = t->getDimX();
394 (*typeData++) = t->getDimY();
395 (*typeData++) = t->getDimZ();
396 (*typeData++) = t->getDimLOD();
397 (*typeData++) = t->getDimFaces() ? 1 : 0;
398 (*typeData++) = (uint32_t)t->getElement();
399
400}
401
Jason Sams326e0dd2009-05-22 14:03:28 -0700402
403}
404}
405
Jason Sams225afd32010-10-21 14:06:55 -0700406RsType rsaTypeCreate(RsContext con, RsElement _e, uint32_t dimCount,
Jason Sams2353ae32010-10-14 17:48:46 -0700407 const RsDimension *dims, const uint32_t *vals)
408{
409 Context *rsc = static_cast<Context *>(con);
410 Element *e = static_cast<Element *>(_e);
411 TypeState * stc = &rsc->mStateType;
412
413 uint32_t dimX = 0;
414 uint32_t dimY = 0;
415 uint32_t dimZ = 0;
416 uint32_t dimLOD = 0;
417 uint32_t dimFaces = 0;
418
419 for (uint32_t ct=0; ct < dimCount; ct++) {
420 switch(dims[ct]) {
421 case RS_DIMENSION_X: dimX = vals[ct]; break;
422 case RS_DIMENSION_Y: dimY = vals[ct]; break;
423 case RS_DIMENSION_Z: dimZ = vals[ct]; break;
424 case RS_DIMENSION_LOD: dimLOD = vals[ct]; break;
425 case RS_DIMENSION_FACE: dimFaces = vals[ct]; break;
426
427 default:
428 LOGE("rsaTypeCreate: Bad dimension");
429 rsAssert(0);
430 }
431 }
432
Jason Sams225afd32010-10-21 14:06:55 -0700433 ObjectBase::asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700434 for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
435 Type *t = stc->mTypes[ct];
436 if (t->getElement() != e) continue;
437 if (t->getDimX() != dimX) continue;
438 if (t->getDimY() != dimY) continue;
439 if (t->getDimZ() != dimZ) continue;
440 if (t->getDimLOD() != dimLOD) continue;
441 if (t->getDimFaces() != dimFaces) continue;
Jason Sams225afd32010-10-21 14:06:55 -0700442 t->incUserRef();
443 ObjectBase::asyncUnlock();
Jason Sams2353ae32010-10-14 17:48:46 -0700444 return t;
445 }
Jason Sams225afd32010-10-21 14:06:55 -0700446 ObjectBase::asyncUnlock();
Jason Sams2353ae32010-10-14 17:48:46 -0700447
448 Type * st = new Type(rsc);
449 st->incUserRef();
450 st->setDimX(dimX);
451 st->setDimY(dimY);
452 st->setDimZ(dimZ);
453 st->setElement(e);
454 st->setDimLOD(dimLOD);
455 st->setDimFaces(dimFaces);
456 st->compute();
457
Jason Sams225afd32010-10-21 14:06:55 -0700458 ObjectBase::asyncLock();
Jason Sams2353ae32010-10-14 17:48:46 -0700459 stc->mTypes.push(st);
Jason Sams225afd32010-10-21 14:06:55 -0700460 ObjectBase::asyncUnlock();
Jason Sams2353ae32010-10-14 17:48:46 -0700461 return st;
462}
463