rsScriptC.cpp revision e7b4b86cf8ff443ce4f7bdf7050ab9e3cf39a6d5
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"
20#include "../../compile/libbcc/include/bcc/bcc.h"
21#include "utils/Timers.h"
22
23#include <GLES/gl.h>
24#include <GLES/glext.h>
25
26using namespace android;
27using namespace android::renderscript;
28
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
34
35ScriptC::ScriptC(Context *rsc) : Script(rsc)
36{
37    mAllocFile = __FILE__;
38    mAllocLine = __LINE__;
39    mBccScript = NULL;
40    memset(&mProgram, 0, sizeof(mProgram));
41}
42
43ScriptC::~ScriptC()
44{
45    if (mBccScript) {
46        bccDeleteScript(mBccScript);
47    }
48    free(mEnviroment.mScriptText);
49    mEnviroment.mScriptText = NULL;
50}
51
52void ScriptC::setupScript(Context *rsc)
53{
54    setupGLState(rsc);
55    mEnviroment.mStartTimeMillis
56                = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
57
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.");
74        }
75    }
76}
77
78const 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
94Script * ScriptC::setTLS(Script *sc)
95{
96    Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *)
97                                  pthread_getspecific(Context::gThreadTLSKey);
98    rsAssert(tls);
99    Script *old = tls->mScript;
100    tls->mScript = sc;
101    return old;
102}
103
104
105void ScriptC::setupGLState(Context *rsc)
106{
107    if (mEnviroment.mFragmentStore.get()) {
108        rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
109    }
110    if (mEnviroment.mFragment.get()) {
111        rsc->setFragment(mEnviroment.mFragment.get());
112    }
113    if (mEnviroment.mVertex.get()) {
114        rsc->setVertex(mEnviroment.mVertex.get());
115    }
116    if (mEnviroment.mRaster.get()) {
117        rsc->setRaster(mEnviroment.mRaster.get());
118    }
119}
120
121uint32_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;
126    }
127
128    setupScript(rsc);
129
130    uint32_t ret = 0;
131    Script * oldTLS = setTLS(this);
132    //LOGE("ScriptC::run %p", mProgram.mRoot);
133    ret = mProgram.mRoot();
134    setTLS(oldTLS);
135    //LOGE("ScriptC::run ret %i", ret);
136    return ret;
137}
138
139
140typedef 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;
169typedef int (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
170
171static void wc_xy(void *usr, uint32_t idx)
172{
173    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
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
201void ScriptC::runForEach(Context *rsc,
202                         const Allocation * ain,
203                         Allocation * aout,
204                         const void * usr,
205                         const RsScriptCall *sc)
206{
207    MTLaunchStruct mtls;
208    memset(&mtls, 0, sizeof(mtls));
209
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    }
224
225    if (!sc || (sc->xEnd == 0)) {
226        mtls.xEnd = mtls.dimX;
227    } else {
228        rsAssert(sc->xStart < mtls.dimX);
229        rsAssert(sc->xEnd <= mtls.dimX);
230        rsAssert(sc->xStart < sc->xEnd);
231        mtls.xStart = rsMin(mtls.dimX, sc->xStart);
232        mtls.xEnd = rsMin(mtls.dimX, sc->xEnd);
233        if (mtls.xStart >= mtls.xEnd) return;
234    }
235
236    if (!sc || (sc->yEnd == 0)) {
237        mtls.yEnd = mtls.dimY;
238    } else {
239        rsAssert(sc->yStart < mtls.dimY);
240        rsAssert(sc->yEnd <= mtls.dimY);
241        rsAssert(sc->yStart < sc->yEnd);
242        mtls.yStart = rsMin(mtls.dimY, sc->yStart);
243        mtls.yEnd = rsMin(mtls.dimY, sc->yEnd);
244        if (mtls.yStart >= mtls.yEnd) return;
245    }
246
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);
251
252    rsAssert(ain->getType()->getDimZ() == 0);
253
254    setupScript(rsc);
255    Script * oldTLS = setTLS(this);
256
257
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;
265
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();
271    }
272
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
281    if ((rsc->getWorkerPoolSize() > 1) && mEnviroment.mIsThreadable &&
282        ((mtls.dimY * mtls.dimZ * mtls.dimArray) > 1)) {
283
284        //LOGE("launch 1");
285        rsc->launchThreads(wc_xy, &mtls);
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);
296
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                    }
302                }
303            }
304        }
305    }
306
307    setTLS(oldTLS);
308}
309
310void 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    }
318    setupScript(rsc);
319    Script * oldTLS = setTLS(this);
320
321    ((void (*)(const void *, uint32_t))
322        mEnviroment.mInvokeFunctions[slot])(data, len);
323
324    setTLS(oldTLS);
325}
326
327ScriptCState::ScriptCState()
328{
329    mScript = NULL;
330    clear();
331}
332
333ScriptCState::~ScriptCState()
334{
335    delete mScript;
336    mScript = NULL;
337}
338
339void ScriptCState::clear()
340{
341    for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
342        mConstantBufferTypes[ct].clear();
343        mSlotWritable[ct] = false;
344    }
345
346    delete mScript;
347    mScript = new ScriptC(NULL);
348}
349
350static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name)
351{
352    const ScriptCState::SymbolTable_t *sym;
353    ScriptC *s = (ScriptC *)pContext;
354    sym = ScriptCState::lookupSymbol(name);
355    if (sym) {
356        return sym->mPtr;
357    }
358    s->mEnviroment.mIsThreadable = false;
359    sym = ScriptCState::lookupSymbolCL(name);
360    if (sym) {
361        return sym->mPtr;
362    }
363    sym = ScriptCState::lookupSymbolGL(name);
364    if (sym) {
365        return sym->mPtr;
366    }
367    LOGE("ScriptC sym lookup failed for %s", name);
368    return NULL;
369}
370
371void ScriptCState::runCompiler(Context *rsc, ScriptC *s)
372{
373    LOGV("ScriptCState::runCompiler ");
374
375    s->mBccScript = bccCreateScript();
376    s->mEnviroment.mIsThreadable = true;
377    bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength);
378    bccRegisterSymbolCallback(s->mBccScript, symbolLookup, s);
379    bccCompileScript(s->mBccScript);
380    bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot);
381    bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit);
382    LOGV("root %p,  init %p", s->mProgram.mRoot, s->mProgram.mInit);
383
384    if (s->mProgram.mInit) {
385        s->mProgram.mInit();
386    }
387
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);
394    }
395
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    }
403    //for (int ct2=0; ct2 < s->mEnviroment.mFieldCount; ct2++ ) {
404        //LOGE("Script field %i = %p", ct2, s->mEnviroment.mFieldAddress[ct2]);
405    //}
406
407    s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
408    s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
409    s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
410    s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
411
412    if (s->mProgram.mRoot) {
413        const static int pragmaMax = 16;
414        BCCsizei pragmaCount;
415        BCCchar * str[pragmaMax];
416        bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]);
417
418        for (int ct=0; ct < pragmaCount; ct+=2) {
419            //LOGE("pragme %s %s", str[ct], str[ct+1]);
420            if (!strcmp(str[ct], "version")) {
421                continue;
422            }
423
424            if (!strcmp(str[ct], "stateVertex")) {
425                if (!strcmp(str[ct+1], "default")) {
426                    continue;
427                }
428                if (!strcmp(str[ct+1], "parent")) {
429                    s->mEnviroment.mVertex.clear();
430                    continue;
431                }
432                LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
433            }
434
435            if (!strcmp(str[ct], "stateRaster")) {
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                }
443                LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
444            }
445
446            if (!strcmp(str[ct], "stateFragment")) {
447                if (!strcmp(str[ct+1], "default")) {
448                    continue;
449                }
450                if (!strcmp(str[ct+1], "parent")) {
451                    s->mEnviroment.mFragment.clear();
452                    continue;
453                }
454                LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
455            }
456
457            if (!strcmp(str[ct], "stateStore")) {
458                if (!strcmp(str[ct+1], "default")) {
459                    continue;
460                }
461                if (!strcmp(str[ct+1], "parent")) {
462                    s->mEnviroment.mFragmentStore.clear();
463                    continue;
464                }
465                LOGE("Unreconized value %s passed to stateStore", str[ct+1]);
466            }
467
468        }
469
470
471    } else {
472        // Deal with an error.
473    }
474}
475
476
477
478namespace android {
479namespace renderscript {
480
481void rsi_ScriptCBegin(Context * rsc)
482{
483    ScriptCState *ss = &rsc->mScriptC;
484    ss->clear();
485}
486
487void rsi_ScriptCSetScript(Context * rsc, void *vp)
488{
489    rsAssert(0);
490    //ScriptCState *ss = &rsc->mScriptC;
491    //ss->mProgram.mScript = reinterpret_cast<ScriptC::RunScript_t>(vp);
492}
493
494void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
495{
496    ScriptCState *ss = &rsc->mScriptC;
497
498    char *t = (char *)malloc(len + 1);
499    memcpy(t, text, len);
500    t[len] = 0;
501    ss->mScript->mEnviroment.mScriptText = t;
502    ss->mScript->mEnviroment.mScriptTextLength = len;
503}
504
505
506RsScript rsi_ScriptCCreate(Context * rsc)
507{
508    ScriptCState *ss = &rsc->mScriptC;
509
510    ScriptC *s = ss->mScript;
511    ss->mScript = NULL;
512
513    ss->runCompiler(rsc, s);
514    s->incUserRef();
515    s->setContext(rsc);
516    for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
517        s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get());
518        s->mSlotWritable[ct] = ss->mSlotWritable[ct];
519    }
520
521    ss->clear();
522    return s;
523}
524
525}
526}
527
528
529