Jason Sams | 326e0dd | 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 | #include "rsScriptC.h" |
| 19 | #include "rsMatrix.h" |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 20 | #include "../../../external/llvm/libbcc/include/bcc/bcc.h" |
Joe Onorato | 9c4e4ca | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 21 | #include "utils/Timers.h" |
Jack Palevich | 1ef8b80 | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 22 | |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 23 | #include <GLES/gl.h> |
| 24 | #include <GLES/glext.h> |
| 25 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 26 | using namespace android; |
| 27 | using namespace android::renderscript; |
| 28 | |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 29 | #define GET_TLS() Context::ScriptTLSStruct * tls = \ |
| 30 | (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \ |
| 31 | Context * rsc = tls->mContext; \ |
| 32 | ScriptC * sc = (ScriptC *) tls->mScript |
| 33 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 35 | ScriptC::ScriptC(Context *rsc) : Script(rsc) |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 36 | { |
Jason Sams | f2649a9 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 37 | mAllocFile = __FILE__; |
| 38 | mAllocLine = __LINE__; |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 39 | mBccScript = NULL; |
Jason Sams | efb8de1 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 40 | memset(&mProgram, 0, sizeof(mProgram)); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ScriptC::~ScriptC() |
| 44 | { |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 45 | if (mBccScript) { |
| 46 | bccDeleteScript(mBccScript); |
Jack Palevich | 1ef8b80 | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 47 | } |
Jason Sams | e402ed3 | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 48 | free(mEnviroment.mScriptText); |
| 49 | mEnviroment.mScriptText = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 52 | void ScriptC::setupScript(Context *rsc) |
Jason Sams | ada7f27 | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 53 | { |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 54 | setupGLState(rsc); |
| 55 | mEnviroment.mStartTimeMillis |
| 56 | = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 57 | |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 58 | for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { |
| 59 | if (!mSlots[ct].get()) |
| 60 | continue; |
| 61 | void *ptr = mSlots[ct]->getPtr(); |
| 62 | void **dest = ((void ***)mEnviroment.mFieldAddress)[ct]; |
| 63 | //LOGE("setupScript %i %p = %p %p %i", ct, dest, ptr, mSlots[ct]->getType(), mSlots[ct]->getType()->getDimX()); |
| 64 | |
| 65 | //const uint32_t *p32 = (const uint32_t *)ptr; |
| 66 | //for (uint32_t ct2=0; ct2 < mSlots[ct]->getType()->getDimX(); ct2++) { |
| 67 | //LOGE(" %i = 0x%08x ", ct2, p32[ct2]); |
| 68 | //} |
| 69 | |
| 70 | if (dest) { |
| 71 | *dest = ptr; |
| 72 | } else { |
| 73 | LOGE("ScriptC::setupScript, NULL var binding address."); |
Jason Sams | ada7f27 | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Jason Sams | ce92d4b | 2010-05-17 14:55:34 -0700 | [diff] [blame] | 78 | const Allocation *ScriptC::ptrToAllocation(const void *ptr) const |
| 79 | { |
| 80 | if (!ptr) { |
| 81 | return NULL; |
| 82 | } |
| 83 | for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { |
| 84 | if (!mSlots[ct].get()) |
| 85 | continue; |
| 86 | if (mSlots[ct]->getPtr() == ptr) { |
| 87 | return mSlots[ct].get(); |
| 88 | } |
| 89 | } |
| 90 | LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 94 | Script * ScriptC::setTLS(Script *sc) |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 95 | { |
| 96 | Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *) |
| 97 | pthread_getspecific(Context::gThreadTLSKey); |
| 98 | rsAssert(tls); |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 99 | Script *old = tls->mScript; |
| 100 | tls->mScript = sc; |
| 101 | return old; |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 104 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 105 | void ScriptC::setupGLState(Context *rsc) |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 106 | { |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 107 | if (mEnviroment.mFragmentStore.get()) { |
| 108 | rsc->setFragmentStore(mEnviroment.mFragmentStore.get()); |
| 109 | } |
| 110 | if (mEnviroment.mFragment.get()) { |
| 111 | rsc->setFragment(mEnviroment.mFragment.get()); |
| 112 | } |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 113 | if (mEnviroment.mVertex.get()) { |
| 114 | rsc->setVertex(mEnviroment.mVertex.get()); |
| 115 | } |
Jason Sams | b681c8a | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 116 | if (mEnviroment.mRaster.get()) { |
| 117 | rsc->setRaster(mEnviroment.mRaster.get()); |
| 118 | } |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 119 | } |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 120 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 121 | uint32_t ScriptC::run(Context *rsc) |
| 122 | { |
| 123 | if (mProgram.mRoot == NULL) { |
| 124 | rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script"); |
| 125 | return 0; |
Joe Onorato | 9c4e4ca | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 126 | } |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 127 | |
| 128 | setupScript(rsc); |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 129 | |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 130 | uint32_t ret = 0; |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 131 | Script * oldTLS = setTLS(this); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 132 | //LOGE("ScriptC::run %p", mProgram.mRoot); |
| 133 | ret = mProgram.mRoot(); |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 134 | setTLS(oldTLS); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 135 | //LOGE("ScriptC::run ret %i", ret); |
Jason Sams | e45ac6e | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 136 | return ret; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 139 | |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 140 | typedef struct { |
| 141 | Context *rsc; |
| 142 | ScriptC *script; |
| 143 | const Allocation * ain; |
| 144 | Allocation * aout; |
| 145 | const void * usr; |
| 146 | |
| 147 | uint32_t mSliceSize; |
| 148 | volatile int mSliceNum; |
| 149 | |
| 150 | const uint8_t *ptrIn; |
| 151 | uint32_t eStrideIn; |
| 152 | uint8_t *ptrOut; |
| 153 | uint32_t eStrideOut; |
| 154 | |
| 155 | uint32_t xStart; |
| 156 | uint32_t xEnd; |
| 157 | uint32_t yStart; |
| 158 | uint32_t yEnd; |
| 159 | uint32_t zStart; |
| 160 | uint32_t zEnd; |
| 161 | uint32_t arrayStart; |
| 162 | uint32_t arrayEnd; |
| 163 | |
| 164 | uint32_t dimX; |
| 165 | uint32_t dimY; |
| 166 | uint32_t dimZ; |
| 167 | uint32_t dimArray; |
| 168 | } MTLaunchStruct; |
| 169 | typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t); |
| 170 | |
| 171 | static void wc_xy(void *usr, uint32_t idx) |
| 172 | { |
| 173 | MTLaunchStruct *mtls = (MTLaunchStruct *)usr; |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 174 | |
| 175 | while (1) { |
| 176 | uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum); |
| 177 | uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize; |
| 178 | uint32_t yEnd = yStart + mtls->mSliceSize; |
| 179 | yEnd = rsMin(yEnd, mtls->yEnd); |
| 180 | if (yEnd <= yStart) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd); |
| 185 | |
| 186 | for (uint32_t y = yStart; y < yEnd; y++) { |
| 187 | uint32_t offset = mtls->dimX * y; |
| 188 | uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset); |
| 189 | const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset); |
| 190 | |
| 191 | for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) { |
| 192 | ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0); |
| 193 | xPtrIn += mtls->eStrideIn; |
| 194 | xPtrOut += mtls->eStrideOut; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 201 | void ScriptC::runForEach(Context *rsc, |
| 202 | const Allocation * ain, |
| 203 | Allocation * aout, |
| 204 | const void * usr, |
| 205 | const RsScriptCall *sc) |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 206 | { |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 207 | MTLaunchStruct mtls; |
| 208 | memset(&mtls, 0, sizeof(mtls)); |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 209 | |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 210 | if (ain) { |
| 211 | mtls.dimX = ain->getType()->getDimX(); |
| 212 | mtls.dimY = ain->getType()->getDimY(); |
| 213 | mtls.dimZ = ain->getType()->getDimZ(); |
| 214 | //mtls.dimArray = ain->getType()->getDimArray(); |
| 215 | } else if (aout) { |
| 216 | mtls.dimX = aout->getType()->getDimX(); |
| 217 | mtls.dimY = aout->getType()->getDimY(); |
| 218 | mtls.dimZ = aout->getType()->getDimZ(); |
| 219 | //mtls.dimArray = aout->getType()->getDimArray(); |
| 220 | } else { |
| 221 | rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations"); |
| 222 | return; |
| 223 | } |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 224 | |
| 225 | if (!sc || (sc->xEnd == 0)) { |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 226 | mtls.xEnd = mtls.dimX; |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 227 | } else { |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 228 | rsAssert(sc->xStart < mtls.dimX); |
| 229 | rsAssert(sc->xEnd <= mtls.dimX); |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 230 | rsAssert(sc->xStart < sc->xEnd); |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 231 | mtls.xStart = rsMin(mtls.dimX, sc->xStart); |
| 232 | mtls.xEnd = rsMin(mtls.dimX, sc->xEnd); |
| 233 | if (mtls.xStart >= mtls.xEnd) return; |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | if (!sc || (sc->yEnd == 0)) { |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 237 | mtls.yEnd = mtls.dimY; |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 238 | } else { |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 239 | rsAssert(sc->yStart < mtls.dimY); |
| 240 | rsAssert(sc->yEnd <= mtls.dimY); |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 241 | rsAssert(sc->yStart < sc->yEnd); |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 242 | mtls.yStart = rsMin(mtls.dimY, sc->yStart); |
| 243 | mtls.yEnd = rsMin(mtls.dimY, sc->yEnd); |
| 244 | if (mtls.yStart >= mtls.yEnd) return; |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 247 | mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd); |
| 248 | mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd); |
| 249 | mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd); |
| 250 | mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd); |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 251 | |
| 252 | rsAssert(ain->getType()->getDimZ() == 0); |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 253 | |
| 254 | setupScript(rsc); |
| 255 | Script * oldTLS = setTLS(this); |
| 256 | |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 257 | |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 258 | mtls.rsc = rsc; |
| 259 | mtls.ain = ain; |
| 260 | mtls.aout = aout; |
| 261 | mtls.script = this; |
| 262 | mtls.usr = usr; |
| 263 | mtls.mSliceSize = 10; |
| 264 | mtls.mSliceNum = 0; |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 265 | |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 266 | mtls.ptrIn = NULL; |
| 267 | mtls.eStrideIn = 0; |
| 268 | if (ain) { |
| 269 | mtls.ptrIn = (const uint8_t *)ain->getPtr(); |
| 270 | mtls.eStrideIn = ain->getType()->getElementSizeBytes(); |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 273 | mtls.ptrOut = NULL; |
| 274 | mtls.eStrideOut = 0; |
| 275 | if (aout) { |
| 276 | mtls.ptrOut = (uint8_t *)aout->getPtr(); |
| 277 | mtls.eStrideOut = aout->getType()->getElementSizeBytes(); |
| 278 | } |
| 279 | |
| 280 | |
Jason Sams | dd663fa | 2010-08-11 13:26:28 -0700 | [diff] [blame] | 281 | if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable && |
Jason Sams | 1813340 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 282 | ((mtls.dimY * mtls.dimZ * mtls.dimArray) > 1)) { |
| 283 | |
| 284 | //LOGE("launch 1"); |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 285 | rsc->launchThreads(wc_xy, &mtls); |
Jason Sams | 1813340 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 286 | //LOGE("launch 2"); |
| 287 | } else { |
| 288 | for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) { |
| 289 | for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) { |
| 290 | for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) { |
| 291 | uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar + |
| 292 | mtls.dimX * mtls.dimY * z + |
| 293 | mtls.dimX * y; |
| 294 | uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset); |
| 295 | const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset); |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 296 | |
Jason Sams | 1813340 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 297 | for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) { |
| 298 | ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar); |
| 299 | xPtrIn += mtls.eStrideIn; |
| 300 | xPtrOut += mtls.eStrideOut; |
| 301 | } |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 305 | } |
Jason Sams | 1813340 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 306 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 307 | setTLS(oldTLS); |
| 308 | } |
| 309 | |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 310 | void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) |
| 311 | { |
| 312 | //LOGE("rsi_ScriptInvoke %i", slot); |
| 313 | if ((slot >= mEnviroment.mInvokeFunctionCount) || |
| 314 | (mEnviroment.mInvokeFunctions[slot] == NULL)) { |
| 315 | rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script"); |
| 316 | return; |
| 317 | } |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 318 | setupScript(rsc); |
| 319 | Script * oldTLS = setTLS(this); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 320 | |
Jason Sams | 2a63bf6 | 2010-06-08 15:40:48 -0700 | [diff] [blame] | 321 | ((void (*)(const void *, uint32_t)) |
| 322 | mEnviroment.mInvokeFunctions[slot])(data, len); |
| 323 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 324 | setTLS(oldTLS); |
Jason Sams | 22fa371 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 327 | ScriptCState::ScriptCState() |
| 328 | { |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 329 | mScript = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 330 | clear(); |
| 331 | } |
| 332 | |
| 333 | ScriptCState::~ScriptCState() |
| 334 | { |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 335 | delete mScript; |
| 336 | mScript = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void ScriptCState::clear() |
| 340 | { |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 341 | for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { |
| 342 | mConstantBufferTypes[ct].clear(); |
Jason Sams | 90b36a8 | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 343 | mSlotWritable[ct] = false; |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 344 | } |
Jason Sams | efb8de1 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 345 | |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 346 | delete mScript; |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 347 | mScript = new ScriptC(NULL); |
Jason Sams | 1f52633 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 350 | static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) |
Jason Sams | 29df66f | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 351 | { |
Jason Sams | aeb094b | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 352 | const ScriptCState::SymbolTable_t *sym; |
Jason Sams | dd663fa | 2010-08-11 13:26:28 -0700 | [diff] [blame] | 353 | ScriptC *s = (ScriptC *)pContext; |
Jason Sams | aeb094b | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 354 | sym = ScriptCState::lookupSymbol(name); |
| 355 | if (sym) { |
| 356 | return sym->mPtr; |
| 357 | } |
Jason Sams | dd663fa | 2010-08-11 13:26:28 -0700 | [diff] [blame] | 358 | s->mEnviroment.mIsThreadable = false; |
Jason Sams | aeb094b | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 359 | sym = ScriptCState::lookupSymbolCL(name); |
| 360 | if (sym) { |
| 361 | return sym->mPtr; |
| 362 | } |
| 363 | sym = ScriptCState::lookupSymbolGL(name); |
Jason Sams | 29df66f | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 364 | if (sym) { |
| 365 | return sym->mPtr; |
| 366 | } |
Jason Sams | 29df66f | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 367 | LOGE("ScriptC sym lookup failed for %s", name); |
Jason Sams | 29df66f | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 368 | return NULL; |
| 369 | } |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 370 | |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 371 | void ScriptCState::runCompiler(Context *rsc, ScriptC *s) |
Jason Sams | 1f52633 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 372 | { |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 373 | LOGV("ScriptCState::runCompiler "); |
Jason Sams | 1f52633 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 374 | |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 375 | s->mBccScript = bccCreateScript(); |
Jason Sams | dd663fa | 2010-08-11 13:26:28 -0700 | [diff] [blame] | 376 | s->mEnviroment.mIsThreadable = true; |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 377 | bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength); |
Jason Sams | dd663fa | 2010-08-11 13:26:28 -0700 | [diff] [blame] | 378 | bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 379 | bccCompileScript(s->mBccScript); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 380 | bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot); |
| 381 | bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit); |
Jason Sams | ace3e01 | 2010-07-15 17:11:13 -0700 | [diff] [blame] | 382 | LOGV("root %p, init %p", s->mProgram.mRoot, s->mProgram.mInit); |
Jason Sams | f168504 | 2009-07-16 17:47:40 -0700 | [diff] [blame] | 383 | |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 384 | if (s->mProgram.mInit) { |
| 385 | s->mProgram.mInit(); |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Jason Sams | 8c88090 | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 388 | bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL); |
| 389 | if(s->mEnviroment.mInvokeFunctionCount <= 0) |
| 390 | s->mEnviroment.mInvokeFunctions = NULL; |
| 391 | else { |
| 392 | s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t)); |
| 393 | bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions); |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Shih-wei Liao | a226b16 | 2010-07-20 16:43:25 -0700 | [diff] [blame] | 396 | bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL); |
| 397 | if(s->mEnviroment.mFieldCount <= 0) |
| 398 | s->mEnviroment.mFieldAddress = NULL; |
| 399 | else { |
| 400 | s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *)); |
| 401 | bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress); |
| 402 | } |
Jason Sams | 1813340 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 403 | //for (int ct2=0; ct2 < s->mEnviroment.mFieldCount; ct2++ ) { |
| 404 | //LOGE("Script field %i = %p", ct2, s->mEnviroment.mFieldAddress[ct2]); |
| 405 | //} |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 406 | |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 407 | s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); |
| 408 | s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); |
Jason Sams | ccc010b | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 409 | s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore()); |
Jason Sams | b681c8a | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 410 | s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster()); |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 411 | |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 412 | if (s->mProgram.mRoot) { |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 413 | const static int pragmaMax = 16; |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 414 | BCCsizei pragmaCount; |
| 415 | BCCchar * str[pragmaMax]; |
| 416 | bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]); |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 417 | |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 418 | for (int ct=0; ct < pragmaCount; ct+=2) { |
Jason Sams | aeb094b | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 419 | //LOGE("pragme %s %s", str[ct], str[ct+1]); |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 420 | if (!strcmp(str[ct], "version")) { |
| 421 | continue; |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 424 | if (!strcmp(str[ct], "stateVertex")) { |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 425 | if (!strcmp(str[ct+1], "default")) { |
| 426 | continue; |
| 427 | } |
| 428 | if (!strcmp(str[ct+1], "parent")) { |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 429 | s->mEnviroment.mVertex.clear(); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 430 | continue; |
| 431 | } |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 432 | LOGE("Unreconized value %s passed to stateVertex", str[ct+1]); |
| 433 | } |
| 434 | |
| 435 | if (!strcmp(str[ct], "stateRaster")) { |
Jason Sams | b681c8a | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 436 | if (!strcmp(str[ct+1], "default")) { |
| 437 | continue; |
| 438 | } |
| 439 | if (!strcmp(str[ct+1], "parent")) { |
| 440 | s->mEnviroment.mRaster.clear(); |
| 441 | continue; |
| 442 | } |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 443 | LOGE("Unreconized value %s passed to stateRaster", str[ct+1]); |
| 444 | } |
| 445 | |
| 446 | if (!strcmp(str[ct], "stateFragment")) { |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 447 | if (!strcmp(str[ct+1], "default")) { |
| 448 | continue; |
| 449 | } |
| 450 | if (!strcmp(str[ct+1], "parent")) { |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 451 | s->mEnviroment.mFragment.clear(); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 452 | continue; |
| 453 | } |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 454 | LOGE("Unreconized value %s passed to stateFragment", str[ct+1]); |
| 455 | } |
| 456 | |
Jason Sams | b681c8a | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 457 | if (!strcmp(str[ct], "stateStore")) { |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 458 | if (!strcmp(str[ct+1], "default")) { |
| 459 | continue; |
| 460 | } |
| 461 | if (!strcmp(str[ct+1], "parent")) { |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 462 | s->mEnviroment.mFragmentStore.clear(); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 463 | continue; |
| 464 | } |
Jason Sams | b681c8a | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 465 | LOGE("Unreconized value %s passed to stateStore", str[ct+1]); |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | } |
| 469 | |
Jason Sams | d34b725 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 470 | |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 471 | } else { |
| 472 | // Deal with an error. |
| 473 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 476 | |
Joe Onorato | 57b79ce | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 477 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 478 | namespace android { |
| 479 | namespace renderscript { |
| 480 | |
| 481 | void rsi_ScriptCBegin(Context * rsc) |
| 482 | { |
| 483 | ScriptCState *ss = &rsc->mScriptC; |
| 484 | ss->clear(); |
| 485 | } |
| 486 | |
Jason Sams | efb8de1 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 487 | void rsi_ScriptCSetScript(Context * rsc, void *vp) |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 488 | { |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 489 | rsAssert(0); |
| 490 | //ScriptCState *ss = &rsc->mScriptC; |
| 491 | //ss->mProgram.mScript = reinterpret_cast<ScriptC::RunScript_t>(vp); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Jason Sams | 1f52633 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 494 | void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) |
| 495 | { |
| 496 | ScriptCState *ss = &rsc->mScriptC; |
Jason Sams | e402ed3 | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 497 | |
| 498 | char *t = (char *)malloc(len + 1); |
| 499 | memcpy(t, text, len); |
| 500 | t[len] = 0; |
| 501 | ss->mScript->mEnviroment.mScriptText = t; |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 502 | ss->mScript->mEnviroment.mScriptTextLength = len; |
Jason Sams | 1f52633 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 506 | RsScript rsi_ScriptCCreate(Context * rsc) |
| 507 | { |
| 508 | ScriptCState *ss = &rsc->mScriptC; |
| 509 | |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 510 | ScriptC *s = ss->mScript; |
| 511 | ss->mScript = NULL; |
Jason Sams | 1f52633 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 512 | |
Jason Sams | 8c6bc69 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 513 | ss->runCompiler(rsc, s); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 514 | s->incUserRef(); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 515 | s->setContext(rsc); |
Jason Sams | fa51719 | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 516 | for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { |
| 517 | s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get()); |
Jason Sams | 90b36a8 | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 518 | s->mSlotWritable[ct] = ss->mSlotWritable[ct]; |
Jason Sams | fa51719 | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 519 | } |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 520 | |
Jason Sams | fa51719 | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 521 | ss->clear(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 522 | return s; |
| 523 | } |
| 524 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | |