blob: cbc5df95aa05bc31b80343c90cda87f716db2191 [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
17#include "rsContext.h"
18#include "rsScriptC.h"
19#include "rsMatrix.h"
Anatol Pomazaue7b4b862010-09-07 17:33:01 -070020#include "../../compile/libbcc/include/bcc/bcc.h"
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070021#include "utils/Timers.h"
Jack Palevich1ef8b802009-05-28 15:53:04 -070022
Jason Sams1aa5a4e2009-06-22 17:15:15 -070023#include <GLES/gl.h>
24#include <GLES/glext.h>
25
Jason Sams326e0dd2009-05-22 14:03:28 -070026using namespace android;
27using namespace android::renderscript;
28
Jason Samse5769102009-06-19 16:03:18 -070029#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 Sams326e0dd2009-05-22 14:03:28 -070034
Jason Samse514b452009-09-25 14:51:22 -070035ScriptC::ScriptC(Context *rsc) : Script(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070036{
Jason Samsf2649a92009-09-25 16:37:33 -070037 mAllocFile = __FILE__;
38 mAllocLine = __LINE__;
Jason Samsbe36bf32010-05-11 14:03:58 -070039 mBccScript = NULL;
Jason Samsefb8de12009-06-08 15:20:31 -070040 memset(&mProgram, 0, sizeof(mProgram));
Jason Sams326e0dd2009-05-22 14:03:28 -070041}
42
43ScriptC::~ScriptC()
44{
Jason Samsbe36bf32010-05-11 14:03:58 -070045 if (mBccScript) {
46 bccDeleteScript(mBccScript);
Jack Palevich1ef8b802009-05-28 15:53:04 -070047 }
Jason Samse402ed32009-11-03 11:25:42 -080048 free(mEnviroment.mScriptText);
49 mEnviroment.mScriptText = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -070050}
51
Jason Samsc61346b2010-05-28 18:23:22 -070052void ScriptC::setupScript(Context *rsc)
Jason Samsada7f272009-09-24 14:55:38 -070053{
Jason Samsc61346b2010-05-28 18:23:22 -070054 setupGLState(rsc);
55 mEnviroment.mStartTimeMillis
56 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
57
Jason Samsbe36bf32010-05-11 14:03:58 -070058 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
Jason Sams900f1612010-09-16 18:18:29 -070059 if (mSlots[ct].get() && !mTypes[ct].get()) {
60 mTypes[ct].set(mSlots[ct]->getType());
61 }
62
63 if (!mTypes[ct].get())
Jason Samsbe36bf32010-05-11 14:03:58 -070064 continue;
Jason Sams900f1612010-09-16 18:18:29 -070065 void *ptr = NULL;
66 if (mSlots[ct].get()) {
67 ptr = mSlots[ct]->getPtr();
68 }
Jason Samsbe36bf32010-05-11 14:03:58 -070069 void **dest = ((void ***)mEnviroment.mFieldAddress)[ct];
70 //LOGE("setupScript %i %p = %p %p %i", ct, dest, ptr, mSlots[ct]->getType(), mSlots[ct]->getType()->getDimX());
71
72 //const uint32_t *p32 = (const uint32_t *)ptr;
73 //for (uint32_t ct2=0; ct2 < mSlots[ct]->getType()->getDimX(); ct2++) {
74 //LOGE(" %i = 0x%08x ", ct2, p32[ct2]);
75 //}
76
77 if (dest) {
78 *dest = ptr;
79 } else {
80 LOGE("ScriptC::setupScript, NULL var binding address.");
Jason Samsada7f272009-09-24 14:55:38 -070081 }
82 }
83}
84
Jason Samsce92d4b2010-05-17 14:55:34 -070085const Allocation *ScriptC::ptrToAllocation(const void *ptr) const
86{
87 if (!ptr) {
88 return NULL;
89 }
90 for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) {
91 if (!mSlots[ct].get())
92 continue;
93 if (mSlots[ct]->getPtr() == ptr) {
94 return mSlots[ct].get();
95 }
96 }
97 LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr);
98 return NULL;
99}
100
Jason Samsc61346b2010-05-28 18:23:22 -0700101Script * ScriptC::setTLS(Script *sc)
Jason Sams22fa3712010-05-19 17:22:57 -0700102{
103 Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
104 pthread_getspecific(Context::gThreadTLSKey);
105 rsAssert(tls);
Jason Samsc61346b2010-05-28 18:23:22 -0700106 Script *old = tls->mScript;
107 tls->mScript = sc;
108 return old;
Jason Sams22fa3712010-05-19 17:22:57 -0700109}
110
Jason Sams326e0dd2009-05-22 14:03:28 -0700111
Jason Samsc61346b2010-05-28 18:23:22 -0700112void ScriptC::setupGLState(Context *rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700113{
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700114 if (mEnviroment.mFragmentStore.get()) {
115 rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
116 }
117 if (mEnviroment.mFragment.get()) {
118 rsc->setFragment(mEnviroment.mFragment.get());
119 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700120 if (mEnviroment.mVertex.get()) {
121 rsc->setVertex(mEnviroment.mVertex.get());
122 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700123 if (mEnviroment.mRaster.get()) {
124 rsc->setRaster(mEnviroment.mRaster.get());
125 }
Jason Samsc61346b2010-05-28 18:23:22 -0700126}
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700127
Jason Samsc61346b2010-05-28 18:23:22 -0700128uint32_t ScriptC::run(Context *rsc)
129{
130 if (mProgram.mRoot == NULL) {
131 rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
132 return 0;
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700133 }
Jason Samsc61346b2010-05-28 18:23:22 -0700134
135 setupScript(rsc);
Jason Sams1d54f102009-09-03 15:43:13 -0700136
Jason Sams2dca84d2009-12-09 11:05:45 -0800137 uint32_t ret = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700138 Script * oldTLS = setTLS(this);
Jason Samsbe36bf32010-05-11 14:03:58 -0700139 //LOGE("ScriptC::run %p", mProgram.mRoot);
140 ret = mProgram.mRoot();
Jason Samsc61346b2010-05-28 18:23:22 -0700141 setTLS(oldTLS);
Jason Samsbe36bf32010-05-11 14:03:58 -0700142 //LOGE("ScriptC::run ret %i", ret);
Jason Samse45ac6e2009-07-20 14:31:06 -0700143 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700144}
145
Jason Samsc61346b2010-05-28 18:23:22 -0700146
Jason Sams7bf29dd2010-07-19 15:38:19 -0700147typedef struct {
148 Context *rsc;
149 ScriptC *script;
150 const Allocation * ain;
151 Allocation * aout;
152 const void * usr;
153
154 uint32_t mSliceSize;
155 volatile int mSliceNum;
156
157 const uint8_t *ptrIn;
158 uint32_t eStrideIn;
159 uint8_t *ptrOut;
160 uint32_t eStrideOut;
161
162 uint32_t xStart;
163 uint32_t xEnd;
164 uint32_t yStart;
165 uint32_t yEnd;
166 uint32_t zStart;
167 uint32_t zEnd;
168 uint32_t arrayStart;
169 uint32_t arrayEnd;
170
171 uint32_t dimX;
172 uint32_t dimY;
173 uint32_t dimZ;
174 uint32_t dimArray;
175} MTLaunchStruct;
176typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
177
178static void wc_xy(void *usr, uint32_t idx)
179{
180 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700181
182 while (1) {
183 uint32_t slice = (uint32_t)android_atomic_inc(&mtls->mSliceNum);
184 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
185 uint32_t yEnd = yStart + mtls->mSliceSize;
186 yEnd = rsMin(yEnd, mtls->yEnd);
187 if (yEnd <= yStart) {
188 return;
189 }
190
191 //LOGE("usr idx %i, x %i,%i y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
192
193 for (uint32_t y = yStart; y < yEnd; y++) {
194 uint32_t offset = mtls->dimX * y;
195 uint8_t *xPtrOut = mtls->ptrOut + (mtls->eStrideOut * offset);
196 const uint8_t *xPtrIn = mtls->ptrIn + (mtls->eStrideIn * offset);
197
198 for (uint32_t x = mtls->xStart; x < mtls->xEnd; x++) {
199 ((rs_t)mtls->script->mProgram.mRoot) (xPtrIn, xPtrOut, mtls->usr, x, y, 0, 0);
200 xPtrIn += mtls->eStrideIn;
201 xPtrOut += mtls->eStrideOut;
202 }
203 }
204 }
205
206}
207
Jason Samsace3e012010-07-15 17:11:13 -0700208void ScriptC::runForEach(Context *rsc,
209 const Allocation * ain,
210 Allocation * aout,
211 const void * usr,
212 const RsScriptCall *sc)
Jason Samsc61346b2010-05-28 18:23:22 -0700213{
Jason Sams7bf29dd2010-07-19 15:38:19 -0700214 MTLaunchStruct mtls;
215 memset(&mtls, 0, sizeof(mtls));
Jason Samsc61346b2010-05-28 18:23:22 -0700216
Jason Sams7bf29dd2010-07-19 15:38:19 -0700217 if (ain) {
218 mtls.dimX = ain->getType()->getDimX();
219 mtls.dimY = ain->getType()->getDimY();
220 mtls.dimZ = ain->getType()->getDimZ();
221 //mtls.dimArray = ain->getType()->getDimArray();
222 } else if (aout) {
223 mtls.dimX = aout->getType()->getDimX();
224 mtls.dimY = aout->getType()->getDimY();
225 mtls.dimZ = aout->getType()->getDimZ();
226 //mtls.dimArray = aout->getType()->getDimArray();
227 } else {
228 rsc->setError(RS_ERROR_BAD_SCRIPT, "rsForEach called with null allocations");
229 return;
230 }
Jason Samsace3e012010-07-15 17:11:13 -0700231
232 if (!sc || (sc->xEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700233 mtls.xEnd = mtls.dimX;
Jason Samsace3e012010-07-15 17:11:13 -0700234 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700235 rsAssert(sc->xStart < mtls.dimX);
236 rsAssert(sc->xEnd <= mtls.dimX);
Jason Samsace3e012010-07-15 17:11:13 -0700237 rsAssert(sc->xStart < sc->xEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700238 mtls.xStart = rsMin(mtls.dimX, sc->xStart);
239 mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
240 if (mtls.xStart >= mtls.xEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700241 }
242
243 if (!sc || (sc->yEnd == 0)) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700244 mtls.yEnd = mtls.dimY;
Jason Samsace3e012010-07-15 17:11:13 -0700245 } else {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700246 rsAssert(sc->yStart < mtls.dimY);
247 rsAssert(sc->yEnd <= mtls.dimY);
Jason Samsace3e012010-07-15 17:11:13 -0700248 rsAssert(sc->yStart < sc->yEnd);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700249 mtls.yStart = rsMin(mtls.dimY, sc->yStart);
250 mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
251 if (mtls.yStart >= mtls.yEnd) return;
Jason Samsace3e012010-07-15 17:11:13 -0700252 }
253
Jason Sams7bf29dd2010-07-19 15:38:19 -0700254 mtls.xEnd = rsMax((uint32_t)1, mtls.xEnd);
255 mtls.yEnd = rsMax((uint32_t)1, mtls.yEnd);
256 mtls.zEnd = rsMax((uint32_t)1, mtls.zEnd);
257 mtls.arrayEnd = rsMax((uint32_t)1, mtls.arrayEnd);
Jason Samsace3e012010-07-15 17:11:13 -0700258
259 rsAssert(ain->getType()->getDimZ() == 0);
Jason Samsc61346b2010-05-28 18:23:22 -0700260
261 setupScript(rsc);
262 Script * oldTLS = setTLS(this);
263
Jason Samsace3e012010-07-15 17:11:13 -0700264
Jason Sams7bf29dd2010-07-19 15:38:19 -0700265 mtls.rsc = rsc;
266 mtls.ain = ain;
267 mtls.aout = aout;
268 mtls.script = this;
269 mtls.usr = usr;
270 mtls.mSliceSize = 10;
271 mtls.mSliceNum = 0;
Jason Samsc61346b2010-05-28 18:23:22 -0700272
Jason Sams7bf29dd2010-07-19 15:38:19 -0700273 mtls.ptrIn = NULL;
274 mtls.eStrideIn = 0;
275 if (ain) {
276 mtls.ptrIn = (const uint8_t *)ain->getPtr();
277 mtls.eStrideIn = ain->getType()->getElementSizeBytes();
Jason Samsc61346b2010-05-28 18:23:22 -0700278 }
279
Jason Sams7bf29dd2010-07-19 15:38:19 -0700280 mtls.ptrOut = NULL;
281 mtls.eStrideOut = 0;
282 if (aout) {
283 mtls.ptrOut = (uint8_t *)aout->getPtr();
284 mtls.eStrideOut = aout->getType()->getElementSizeBytes();
285 }
286
287
Jason Samsdd663fa2010-08-11 13:26:28 -0700288 if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable &&
Jason Sams18133402010-07-20 15:09:00 -0700289 ((mtls.dimY * mtls.dimZ * mtls.dimArray) > 1)) {
290
291 //LOGE("launch 1");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700292 rsc->launchThreads(wc_xy, &mtls);
Jason Sams18133402010-07-20 15:09:00 -0700293 //LOGE("launch 2");
294 } else {
295 for (uint32_t ar = mtls.arrayStart; ar < mtls.arrayEnd; ar++) {
296 for (uint32_t z = mtls.zStart; z < mtls.zEnd; z++) {
297 for (uint32_t y = mtls.yStart; y < mtls.yEnd; y++) {
298 uint32_t offset = mtls.dimX * mtls.dimY * mtls.dimZ * ar +
299 mtls.dimX * mtls.dimY * z +
300 mtls.dimX * y;
301 uint8_t *xPtrOut = mtls.ptrOut + (mtls.eStrideOut * offset);
302 const uint8_t *xPtrIn = mtls.ptrIn + (mtls.eStrideIn * offset);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700303
Jason Sams18133402010-07-20 15:09:00 -0700304 for (uint32_t x = mtls.xStart; x < mtls.xEnd; x++) {
305 ((rs_t)mProgram.mRoot) (xPtrIn, xPtrOut, usr, x, y, z, ar);
306 xPtrIn += mtls.eStrideIn;
307 xPtrOut += mtls.eStrideOut;
308 }
Jason Samsace3e012010-07-15 17:11:13 -0700309 }
310 }
311 }
Jason Samsc61346b2010-05-28 18:23:22 -0700312 }
Jason Sams18133402010-07-20 15:09:00 -0700313
Jason Samsc61346b2010-05-28 18:23:22 -0700314 setTLS(oldTLS);
315}
316
Jason Sams22fa3712010-05-19 17:22:57 -0700317void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len)
318{
319 //LOGE("rsi_ScriptInvoke %i", slot);
320 if ((slot >= mEnviroment.mInvokeFunctionCount) ||
321 (mEnviroment.mInvokeFunctions[slot] == NULL)) {
322 rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
323 return;
324 }
Jason Samsc61346b2010-05-28 18:23:22 -0700325 setupScript(rsc);
326 Script * oldTLS = setTLS(this);
Jason Sams22fa3712010-05-19 17:22:57 -0700327
Jason Sams2a63bf62010-06-08 15:40:48 -0700328 ((void (*)(const void *, uint32_t))
329 mEnviroment.mInvokeFunctions[slot])(data, len);
330
Jason Samsc61346b2010-05-28 18:23:22 -0700331 setTLS(oldTLS);
Jason Sams22fa3712010-05-19 17:22:57 -0700332}
333
Jason Sams326e0dd2009-05-22 14:03:28 -0700334ScriptCState::ScriptCState()
335{
Jason Sams8c6bc692009-09-16 15:04:38 -0700336 mScript = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700337 clear();
338}
339
340ScriptCState::~ScriptCState()
341{
Jason Sams8c6bc692009-09-16 15:04:38 -0700342 delete mScript;
343 mScript = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700344}
345
346void ScriptCState::clear()
347{
Jason Sams8b2c0652009-08-12 17:54:11 -0700348 for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
349 mConstantBufferTypes[ct].clear();
Jason Sams90b36a82009-08-17 13:56:09 -0700350 mSlotWritable[ct] = false;
Jason Sams8b2c0652009-08-12 17:54:11 -0700351 }
Jason Samsefb8de12009-06-08 15:20:31 -0700352
Jason Sams8c6bc692009-09-16 15:04:38 -0700353 delete mScript;
Jason Samse514b452009-09-25 14:51:22 -0700354 mScript = new ScriptC(NULL);
Jason Sams1f526332009-06-05 17:35:09 -0700355}
356
Jason Samsbe36bf32010-05-11 14:03:58 -0700357static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name)
Jason Sams29df66f2009-07-16 15:08:06 -0700358{
Jason Samsaeb094b2010-05-18 13:35:45 -0700359 const ScriptCState::SymbolTable_t *sym;
Jason Samsdd663fa2010-08-11 13:26:28 -0700360 ScriptC *s = (ScriptC *)pContext;
Jason Samsaeb094b2010-05-18 13:35:45 -0700361 sym = ScriptCState::lookupSymbol(name);
362 if (sym) {
363 return sym->mPtr;
364 }
Jason Samsdd663fa2010-08-11 13:26:28 -0700365 s->mEnviroment.mIsThreadable = false;
Jason Samsaeb094b2010-05-18 13:35:45 -0700366 sym = ScriptCState::lookupSymbolCL(name);
367 if (sym) {
368 return sym->mPtr;
369 }
370 sym = ScriptCState::lookupSymbolGL(name);
Jason Sams29df66f2009-07-16 15:08:06 -0700371 if (sym) {
372 return sym->mPtr;
373 }
Jason Sams29df66f2009-07-16 15:08:06 -0700374 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700375 return NULL;
376}
Jason Samsa4a54e42009-06-10 18:39:40 -0700377
Jason Sams8c6bc692009-09-16 15:04:38 -0700378void ScriptCState::runCompiler(Context *rsc, ScriptC *s)
Jason Sams1f526332009-06-05 17:35:09 -0700379{
Jason Samsace3e012010-07-15 17:11:13 -0700380 LOGV("ScriptCState::runCompiler ");
Jason Sams1f526332009-06-05 17:35:09 -0700381
Jason Samsbe36bf32010-05-11 14:03:58 -0700382 s->mBccScript = bccCreateScript();
Jason Samsdd663fa2010-08-11 13:26:28 -0700383 s->mEnviroment.mIsThreadable = true;
Jason Samsbe36bf32010-05-11 14:03:58 -0700384 bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength);
Jason Samsdd663fa2010-08-11 13:26:28 -0700385 bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
Jason Samsbe36bf32010-05-11 14:03:58 -0700386 bccCompileScript(s->mBccScript);
Jason Samsbe36bf32010-05-11 14:03:58 -0700387 bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
388 bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
Jason Samsace3e012010-07-15 17:11:13 -0700389 LOGV("root %p, init %p", s->mProgram.mRoot, s->mProgram.mInit);
Jason Samsf1685042009-07-16 17:47:40 -0700390
Jason Sams8c6bc692009-09-16 15:04:38 -0700391 if (s->mProgram.mInit) {
392 s->mProgram.mInit();
Jason Sams1d54f102009-09-03 15:43:13 -0700393 }
394
Jason Sams8c880902010-06-15 12:15:57 -0700395 bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL);
396 if(s->mEnviroment.mInvokeFunctionCount <= 0)
397 s->mEnviroment.mInvokeFunctions = NULL;
398 else {
399 s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t));
400 bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions);
Jason Sams1d54f102009-09-03 15:43:13 -0700401 }
402
Shih-wei Liaoa226b162010-07-20 16:43:25 -0700403 bccGetExportVars(s->mBccScript, (BCCsizei*) &s->mEnviroment.mFieldCount, 0, NULL);
404 if(s->mEnviroment.mFieldCount <= 0)
405 s->mEnviroment.mFieldAddress = NULL;
406 else {
407 s->mEnviroment.mFieldAddress = (void **) calloc(s->mEnviroment.mFieldCount, sizeof(void *));
408 bccGetExportVars(s->mBccScript, NULL, s->mEnviroment.mFieldCount, (BCCvoid **) s->mEnviroment.mFieldAddress);
409 }
Jason Sams18133402010-07-20 15:09:00 -0700410 //for (int ct2=0; ct2 < s->mEnviroment.mFieldCount; ct2++ ) {
411 //LOGE("Script field %i = %p", ct2, s->mEnviroment.mFieldAddress[ct2]);
412 //}
Jason Samsa4a54e42009-06-10 18:39:40 -0700413
Jason Sams8c6bc692009-09-16 15:04:38 -0700414 s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
415 s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
Jason Samsccc010b2010-05-13 18:30:11 -0700416 s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
Jason Samsb681c8a2009-09-28 18:12:56 -0700417 s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
Jason Sams8c6bc692009-09-16 15:04:38 -0700418
Jason Samsbe36bf32010-05-11 14:03:58 -0700419 if (s->mProgram.mRoot) {
Jason Sams10308932009-06-09 12:15:30 -0700420 const static int pragmaMax = 16;
Jason Samsbe36bf32010-05-11 14:03:58 -0700421 BCCsizei pragmaCount;
422 BCCchar * str[pragmaMax];
423 bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
Jason Sams10308932009-06-09 12:15:30 -0700424
Jason Sams10308932009-06-09 12:15:30 -0700425 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700426 //LOGE("pragme %s %s", str[ct], str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700427 if (!strcmp(str[ct], "version")) {
428 continue;
Jason Sams10308932009-06-09 12:15:30 -0700429 }
430
Jason Sams10308932009-06-09 12:15:30 -0700431 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700432 if (!strcmp(str[ct+1], "default")) {
433 continue;
434 }
435 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700436 s->mEnviroment.mVertex.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700437 continue;
438 }
Jason Sams10308932009-06-09 12:15:30 -0700439 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
440 }
441
442 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsb681c8a2009-09-28 18:12:56 -0700443 if (!strcmp(str[ct+1], "default")) {
444 continue;
445 }
446 if (!strcmp(str[ct+1], "parent")) {
447 s->mEnviroment.mRaster.clear();
448 continue;
449 }
Jason Sams10308932009-06-09 12:15:30 -0700450 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
451 }
452
453 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700454 if (!strcmp(str[ct+1], "default")) {
455 continue;
456 }
457 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700458 s->mEnviroment.mFragment.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700459 continue;
460 }
Jason Sams10308932009-06-09 12:15:30 -0700461 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
462 }
463
Jason Samsb681c8a2009-09-28 18:12:56 -0700464 if (!strcmp(str[ct], "stateStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700465 if (!strcmp(str[ct+1], "default")) {
466 continue;
467 }
468 if (!strcmp(str[ct+1], "parent")) {
Jason Sams8c6bc692009-09-16 15:04:38 -0700469 s->mEnviroment.mFragmentStore.clear();
Jason Sams8ce125b2009-06-17 16:52:59 -0700470 continue;
471 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700472 LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
Jason Sams10308932009-06-09 12:15:30 -0700473 }
474
475 }
476
Jason Samsd34b7252009-08-04 16:58:20 -0700477
Jason Sams10308932009-06-09 12:15:30 -0700478 } else {
479 // Deal with an error.
480 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700481}
482
Jason Sams8b2c0652009-08-12 17:54:11 -0700483
Joe Onorato57b79ce2009-08-09 22:57:44 -0700484
Jason Sams326e0dd2009-05-22 14:03:28 -0700485namespace android {
486namespace renderscript {
487
488void rsi_ScriptCBegin(Context * rsc)
489{
490 ScriptCState *ss = &rsc->mScriptC;
491 ss->clear();
492}
493
Jason Samsefb8de12009-06-08 15:20:31 -0700494void rsi_ScriptCSetScript(Context * rsc, void *vp)
Jason Sams326e0dd2009-05-22 14:03:28 -0700495{
Jason Sams8c6bc692009-09-16 15:04:38 -0700496 rsAssert(0);
497 //ScriptCState *ss = &rsc->mScriptC;
498 //ss->mProgram.mScript = reinterpret_cast<ScriptC::RunScript_t>(vp);
Jason Sams326e0dd2009-05-22 14:03:28 -0700499}
500
Jason Sams1f526332009-06-05 17:35:09 -0700501void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
502{
503 ScriptCState *ss = &rsc->mScriptC;
Jason Samse402ed32009-11-03 11:25:42 -0800504
505 char *t = (char *)malloc(len + 1);
506 memcpy(t, text, len);
507 t[len] = 0;
508 ss->mScript->mEnviroment.mScriptText = t;
Jason Sams8c6bc692009-09-16 15:04:38 -0700509 ss->mScript->mEnviroment.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700510}
511
512
Jason Sams326e0dd2009-05-22 14:03:28 -0700513RsScript rsi_ScriptCCreate(Context * rsc)
514{
515 ScriptCState *ss = &rsc->mScriptC;
516
Jason Sams8c6bc692009-09-16 15:04:38 -0700517 ScriptC *s = ss->mScript;
518 ss->mScript = NULL;
Jason Sams1f526332009-06-05 17:35:09 -0700519
Jason Sams8c6bc692009-09-16 15:04:38 -0700520 ss->runCompiler(rsc, s);
Jason Sams9397e302009-08-27 20:23:34 -0700521 s->incUserRef();
Jason Samse514b452009-09-25 14:51:22 -0700522 s->setContext(rsc);
Jason Samsfa517192009-08-13 12:59:04 -0700523 for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
524 s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get());
Jason Sams90b36a82009-08-17 13:56:09 -0700525 s->mSlotWritable[ct] = ss->mSlotWritable[ct];
Jason Samsfa517192009-08-13 12:59:04 -0700526 }
Jason Sams10308932009-06-09 12:15:30 -0700527
Jason Samsfa517192009-08-13 12:59:04 -0700528 ss->clear();
Jason Sams326e0dd2009-05-22 14:03:28 -0700529 return s;
530}
531
Jason Sams326e0dd2009-05-22 14:03:28 -0700532}
533}
534
535