Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | #include "rsContext.h" |
| 18 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 19 | #include <GLES/gl.h> |
| 20 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 21 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 24 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 25 | Element::Element(Context *rsc) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 26 | { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 27 | mBits = 0; |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 28 | mAllocFile = __FILE__; |
| 29 | mAllocLine = __LINE__; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 30 | mFields = NULL; |
| 31 | mFieldCount = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | |
| 35 | Element::~Element() |
| 36 | { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 37 | for (uint32_t ct = 0; ct < mRSC->mStateElement.mElements.size(); ct++) { |
| 38 | if (mRSC->mStateElement.mElements[ct] == this) { |
| 39 | mRSC->mStateElement.mElements.removeAt(ct); |
| 40 | break; |
| 41 | } |
| 42 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 43 | clear(); |
| 44 | } |
| 45 | |
| 46 | void Element::clear() |
| 47 | { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 48 | delete [] mFields; |
| 49 | mFields = NULL; |
| 50 | mFieldCount = 0; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 52 | |
| 53 | size_t Element::getSizeBits() const |
| 54 | { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 55 | if (!mFieldCount) { |
| 56 | return mBits; |
| 57 | } |
| 58 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 59 | size_t total = 0; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 60 | for (size_t ct=0; ct < mFieldCount; ct++) { |
| 61 | total += mFields[ct].e->mBits; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 62 | } |
| 63 | return total; |
| 64 | } |
| 65 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 66 | size_t Element::getFieldOffsetBits(uint32_t componentNumber) const |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 67 | { |
| 68 | size_t offset = 0; |
| 69 | for (uint32_t ct = 0; ct < componentNumber; ct++) { |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 70 | offset += mFields[ct].e->mBits; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 71 | } |
| 72 | return offset; |
| 73 | } |
| 74 | |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 75 | void Element::dumpLOGV(const char *prefix) const |
| 76 | { |
| 77 | ObjectBase::dumpLOGV(prefix); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 78 | LOGV("%s Element: components %i, size %i", prefix, mFieldCount, mBits); |
| 79 | for (uint32_t ct = 0; ct < mFieldCount; ct++) { |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 80 | char buf[1024]; |
| 81 | sprintf(buf, "%s component %i: ", prefix, ct); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 82 | //mComponents[ct]->dumpLOGV(buf); |
Jason Sams | 3c0dfba | 2009-09-27 17:50:38 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 86 | |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 87 | const Element * Element::create(Context *rsc, RsDataType dt, RsDataKind dk, |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 88 | bool isNorm, uint32_t vecSize) |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 89 | { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 90 | // Look for an existing match. |
| 91 | for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) { |
| 92 | const Element *ee = rsc->mStateElement.mElements[ct]; |
| 93 | if (!ee->getFieldCount() && |
| 94 | (ee->getComponent().getType() == dt) && |
| 95 | (ee->getComponent().getKind() == dk) && |
| 96 | (ee->getComponent().getIsNormalized() == isNorm) && |
| 97 | (ee->getComponent().getVectorSize() == vecSize)) { |
| 98 | // Match |
| 99 | ee->incUserRef(); |
| 100 | return ee; |
| 101 | } |
| 102 | } |
| 103 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 104 | Element *e = new Element(rsc); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 105 | e->mComponent.set(dt, dk, isNorm, vecSize); |
| 106 | e->mBits = e->mComponent.getBits(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 107 | rsc->mStateElement.mElements.push(e); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 108 | return e; |
| 109 | } |
| 110 | |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 111 | const Element * Element::create(Context *rsc, size_t count, const Element **ein, |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 112 | const char **nin, const size_t * lengths) |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 113 | { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 114 | // Look for an existing match. |
| 115 | for (uint32_t ct=0; ct < rsc->mStateElement.mElements.size(); ct++) { |
| 116 | const Element *ee = rsc->mStateElement.mElements[ct]; |
| 117 | if (ee->getFieldCount() == count) { |
| 118 | bool match = true; |
| 119 | for (uint32_t i=0; i < count; i++) { |
| 120 | if ((ee->mFields[i].e.get() != ein[i]) || |
| 121 | (ee->mFields[i].name.length() != lengths[i]) || |
| 122 | (ee->mFields[i].name != nin[i])) { |
| 123 | match = false; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | if (match) { |
| 128 | ee->incUserRef(); |
| 129 | return ee; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 134 | Element *e = new Element(rsc); |
| 135 | e->mFields = new ElementField_t [count]; |
| 136 | e->mFieldCount = count; |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 137 | for (size_t ct=0; ct < count; ct++) { |
| 138 | e->mFields[ct].e.set(ein[ct]); |
| 139 | e->mFields[ct].name.setTo(nin[ct], lengths[ct]); |
| 140 | } |
| 141 | |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 142 | rsc->mStateElement.mElements.push(e); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 143 | return e; |
| 144 | } |
| 145 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 146 | String8 Element::getCStructBody(uint32_t indent) const |
| 147 | { |
| 148 | String8 si; |
| 149 | for (uint32_t ct=0; ct < indent; ct++) { |
| 150 | si.append(" "); |
| 151 | } |
| 152 | |
| 153 | String8 s(si); |
| 154 | s.append("{\n"); |
| 155 | for (uint32_t ct = 0; ct < mFieldCount; ct++) { |
| 156 | s.append(si); |
| 157 | s.append(mFields[ct].e->getCType(indent+4)); |
| 158 | s.append(" "); |
| 159 | s.append(mFields[ct].name); |
| 160 | s.append(";\n"); |
| 161 | } |
| 162 | s.append(si); |
| 163 | s.append("}"); |
| 164 | return s; |
| 165 | } |
| 166 | |
| 167 | String8 Element::getCType(uint32_t indent) const |
| 168 | { |
| 169 | String8 s; |
| 170 | for (uint32_t ct=0; ct < indent; ct++) { |
| 171 | s.append(" "); |
| 172 | } |
| 173 | |
| 174 | if (!mFieldCount) { |
| 175 | // Basic component. |
| 176 | s.append(mComponent.getCType()); |
| 177 | } else { |
| 178 | s.append("struct "); |
| 179 | s.append(getCStructBody(indent)); |
| 180 | } |
| 181 | |
| 182 | return s; |
| 183 | } |
| 184 | |
Jason Sams | e17964e | 2010-01-04 16:52:27 -0800 | [diff] [blame] | 185 | String8 Element::getGLSLType(uint32_t indent) const |
| 186 | { |
| 187 | String8 s; |
| 188 | for (uint32_t ct=0; ct < indent; ct++) { |
| 189 | s.append(" "); |
| 190 | } |
| 191 | |
| 192 | if (!mFieldCount) { |
| 193 | // Basic component. |
| 194 | s.append(mComponent.getGLSLType()); |
| 195 | } else { |
| 196 | rsAssert(0); |
| 197 | //s.append("struct "); |
| 198 | //s.append(getCStructBody(indent)); |
| 199 | } |
| 200 | |
| 201 | return s; |
| 202 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 203 | |
| 204 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 205 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 206 | ElementState::ElementState() |
| 207 | { |
| 208 | } |
| 209 | |
| 210 | ElementState::~ElementState() |
| 211 | { |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 212 | rsAssert(!mElements.size()); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 215 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 216 | ///////////////////////////////////////// |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 217 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 218 | |
| 219 | namespace android { |
| 220 | namespace renderscript { |
| 221 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 222 | RsElement rsi_ElementCreate(Context *rsc, |
| 223 | RsDataType dt, |
| 224 | RsDataKind dk, |
| 225 | bool norm, |
| 226 | uint32_t vecSize) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 227 | { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 228 | //LOGE("rsi_ElementCreate %i %i %i %i", dt, dk, norm, vecSize); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 229 | const Element *e = Element::create(rsc, dt, dk, norm, vecSize); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 230 | e->incUserRef(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 231 | return (RsElement)e; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 234 | RsElement rsi_ElementCreate2(Context *rsc, |
| 235 | size_t count, |
| 236 | const RsElement * ein, |
| 237 | const char ** names, |
| 238 | const size_t * nameLengths) |
| 239 | { |
| 240 | //LOGE("rsi_ElementCreate2 %i", count); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 241 | const Element *e = Element::create(rsc, count, (const Element **)ein, names, nameLengths); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 242 | e->incUserRef(); |
Jason Sams | e4c487a | 2010-02-17 15:38:10 -0800 | [diff] [blame] | 243 | return (RsElement)e; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 246 | |
| 247 | } |
| 248 | } |