diff options
Diffstat (limited to 'libs/rs/rsScriptC.cpp')
-rw-r--r-- | libs/rs/rsScriptC.cpp | 389 |
1 files changed, 177 insertions, 212 deletions
diff --git a/libs/rs/rsScriptC.cpp b/libs/rs/rsScriptC.cpp index f4d2451..975b704 100644 --- a/libs/rs/rsScriptC.cpp +++ b/libs/rs/rsScriptC.cpp @@ -17,8 +17,7 @@ #include "rsContext.h" #include "rsScriptC.h" #include "rsMatrix.h" - -#include "acc/acc.h" +#include "../../../external/llvm/libbcc/include/bcc/bcc.h" #include "utils/Timers.h" #include <GLES/gl.h> @@ -37,40 +36,74 @@ ScriptC::ScriptC(Context *rsc) : Script(rsc) { mAllocFile = __FILE__; mAllocLine = __LINE__; - mAccScript = NULL; + mBccScript = NULL; memset(&mProgram, 0, sizeof(mProgram)); } ScriptC::~ScriptC() { - if (mAccScript) { - accDeleteScript(mAccScript); + if (mBccScript) { + bccDeleteScript(mBccScript); } free(mEnviroment.mScriptText); mEnviroment.mScriptText = NULL; } -void ScriptC::setupScript() +void ScriptC::setupScript(Context *rsc) { - for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { - if (mProgram.mSlotPointers[ct]) { - *mProgram.mSlotPointers[ct] = mSlots[ct]->getPtr(); + setupGLState(rsc); + mEnviroment.mStartTimeMillis + = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); + + for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { + if (!mSlots[ct].get()) + continue; + void *ptr = mSlots[ct]->getPtr(); + void **dest = ((void ***)mEnviroment.mFieldAddress)[ct]; + //LOGE("setupScript %i %p = %p %p %i", ct, dest, ptr, mSlots[ct]->getType(), mSlots[ct]->getType()->getDimX()); + + //const uint32_t *p32 = (const uint32_t *)ptr; + //for (uint32_t ct2=0; ct2 < mSlots[ct]->getType()->getDimX(); ct2++) { + //LOGE(" %i = 0x%08x ", ct2, p32[ct2]); + //} + + if (dest) { + *dest = ptr; + } else { + LOGE("ScriptC::setupScript, NULL var binding address."); } } } - -uint32_t ScriptC::run(Context *rsc, uint32_t launchIndex) +const Allocation *ScriptC::ptrToAllocation(const void *ptr) const { - if (mProgram.mScript == NULL) { - rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script"); - return 0; + if (!ptr) { + return NULL; + } + for (uint32_t ct=0; ct < mEnviroment.mFieldCount; ct++) { + if (!mSlots[ct].get()) + continue; + if (mSlots[ct]->getPtr() == ptr) { + return mSlots[ct].get(); + } } + LOGE("ScriptC::ptrToAllocation, failed to find %p", ptr); + return NULL; +} - Context::ScriptTLSStruct * tls = - (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); +Script * ScriptC::setTLS(Script *sc) +{ + Context::ScriptTLSStruct * tls = (Context::ScriptTLSStruct *) + pthread_getspecific(Context::gThreadTLSKey); rsAssert(tls); + Script *old = tls->mScript; + tls->mScript = sc; + return old; +} + +void ScriptC::setupGLState(Context *rsc) +{ if (mEnviroment.mFragmentStore.get()) { rsc->setFragmentStore(mEnviroment.mFragmentStore.get()); } @@ -83,20 +116,92 @@ uint32_t ScriptC::run(Context *rsc, uint32_t launchIndex) if (mEnviroment.mRaster.get()) { rsc->setRaster(mEnviroment.mRaster.get()); } +} - if (launchIndex == 0) { - mEnviroment.mStartTimeMillis - = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); +uint32_t ScriptC::run(Context *rsc) +{ + if (mProgram.mRoot == NULL) { + rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script"); + return 0; } - setupScript(); + + setupScript(rsc); uint32_t ret = 0; - tls->mScript = this; - ret = mProgram.mScript(launchIndex); - tls->mScript = NULL; + Script * oldTLS = setTLS(this); + //LOGE("ScriptC::run %p", mProgram.mRoot); + ret = mProgram.mRoot(); + setTLS(oldTLS); + //LOGE("ScriptC::run ret %i", ret); return ret; } +void ScriptC::runForEach(Context *rsc, const Allocation *ain, Allocation *aout, + uint32_t xStart, uint32_t yStart, uint32_t xEnd, uint32_t yEnd) +{ + LOGE("ScriptC::runForEach not implemented"); +} + +void ScriptC::runForEach(Context *rsc, const Allocation *ain, Allocation *aout, uint32_t xStart, uint32_t xEnd) +{ + uint32_t dimX = ain->getType()->getDimX(); + rsAssert(xStart < dimX); + rsAssert(xEnd <= dimX); + rsAssert(ain->getType()->getDimY() == 0); + rsAssert(ain->getType()->getDimZ() == 0); + + if (xStart >= dimX) xStart = dimX - 1; + if (xEnd >= dimX) xEnd = dimX - 1; + if (xStart > xEnd) return; + + setupScript(rsc); + Script * oldTLS = setTLS(this); + + typedef int (*rs_t)(const void *, void *, uint32_t); + const uint8_t *ptrIn = (const uint8_t *)ain->getPtr(); + uint32_t strideIn = ain->getType()->getElementSizeBytes(); + + uint8_t *ptrOut = NULL; + uint32_t strideOut = 0; + if (aout) { + ptrOut = (uint8_t *)aout->getPtr(); + strideOut = aout->getType()->getElementSizeBytes(); + } + + for (uint32_t ct=xStart; ct < xEnd; ct++) { + ((rs_t)mProgram.mRoot) (ptrIn + (strideIn * ct), ptrOut + (strideOut * ct), ct); + } + + setTLS(oldTLS); +} + +void ScriptC::runForEach(Context *rsc, const Allocation *ain, Allocation *aout) +{ + if (ain->getType()->getDimY()) { + runForEach(rsc, ain, aout, 0, 0, 0xffffffff, 0xffffffff); + } else { + runForEach(rsc, ain, aout, 0, 0xffffffff); + } +} + + +void ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, uint32_t len) +{ + //LOGE("rsi_ScriptInvoke %i", slot); + if ((slot >= mEnviroment.mInvokeFunctionCount) || + (mEnviroment.mInvokeFunctions[slot] == NULL)) { + rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script"); + return; + } + setupScript(rsc); + Script * oldTLS = setTLS(this); + + ((void (*)(const void *, uint32_t)) + mEnviroment.mInvokeFunctions[slot])(data, len); + + setTLS(oldTLS); +} + ScriptCState::ScriptCState() { mScript = NULL; @@ -113,21 +218,25 @@ void ScriptCState::clear() { for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { mConstantBufferTypes[ct].clear(); - mSlotNames[ct].setTo(""); - mInvokableNames[ct].setTo(""); mSlotWritable[ct] = false; } delete mScript; mScript = new ScriptC(NULL); - - mInt32Defines.clear(); - mFloatDefines.clear(); } -static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name) +static BCCvoid* symbolLookup(BCCvoid* pContext, const BCCchar* name) { - const ScriptCState::SymbolTable_t *sym = ScriptCState::lookupSymbol(name); + const ScriptCState::SymbolTable_t *sym; + sym = ScriptCState::lookupSymbol(name); + if (sym) { + return sym->mPtr; + } + sym = ScriptCState::lookupSymbolCL(name); + if (sym) { + return sym->mPtr; + } + sym = ScriptCState::lookupSymbolGL(name); if (sym) { return sym->mPtr; } @@ -137,65 +246,60 @@ static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name) void ScriptCState::runCompiler(Context *rsc, ScriptC *s) { - s->mAccScript = accCreateScript(); - String8 tmp; - - rsc->appendNameDefines(&tmp); - appendDecls(&tmp); - appendVarDefines(rsc, &tmp); - appendTypes(rsc, &tmp); - tmp.append("#line 1\n"); - - const char* scriptSource[] = {tmp.string(), s->mEnviroment.mScriptText}; - int scriptLength[] = {tmp.length(), s->mEnviroment.mScriptTextLength} ; - accScriptSource(s->mAccScript, sizeof(scriptLength) / sizeof(int), scriptSource, scriptLength); - accRegisterSymbolCallback(s->mAccScript, symbolLookup, NULL); - accCompileScript(s->mAccScript); - accGetScriptLabel(s->mAccScript, "main", (ACCvoid**) &s->mProgram.mScript); - accGetScriptLabel(s->mAccScript, "init", (ACCvoid**) &s->mProgram.mInit); - rsAssert(s->mProgram.mScript); - - if (!s->mProgram.mScript) { - ACCchar buf[4096]; - ACCsizei len; - accGetScriptInfoLog(s->mAccScript, sizeof(buf), &len, buf); - LOGE("%s", buf); - rsc->setError(RS_ERROR_BAD_SCRIPT, "Error compiling user script."); - return; - } + LOGE("ScriptCState::runCompiler "); + + s->mBccScript = bccCreateScript(); + bccScriptBitcode(s->mBccScript, s->mEnviroment.mScriptText, s->mEnviroment.mScriptTextLength); + bccRegisterSymbolCallback(s->mBccScript, symbolLookup, NULL); + bccCompileScript(s->mBccScript); + bccGetScriptLabel(s->mBccScript, "root", (BCCvoid**) &s->mProgram.mRoot); + bccGetScriptLabel(s->mBccScript, "init", (BCCvoid**) &s->mProgram.mInit); + LOGE("root %p, init %p", s->mProgram.mRoot, s->mProgram.mInit); if (s->mProgram.mInit) { s->mProgram.mInit(); } - for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { - if (mSlotNames[ct].length() > 0) { - accGetScriptLabel(s->mAccScript, - mSlotNames[ct].string(), - (ACCvoid**) &s->mProgram.mSlotPointers[ct]); - } + bccGetExportFuncs(s->mBccScript, (BCCsizei*) &s->mEnviroment.mInvokeFunctionCount, 0, NULL); + if(s->mEnviroment.mInvokeFunctionCount <= 0) + s->mEnviroment.mInvokeFunctions = NULL; + else { + s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t*) calloc(s->mEnviroment.mInvokeFunctionCount, sizeof(Script::InvokeFunc_t)); + bccGetExportFuncs(s->mBccScript, NULL, s->mEnviroment.mInvokeFunctionCount, (BCCvoid **) s->mEnviroment.mInvokeFunctions); } - for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { - if (mInvokableNames[ct].length() > 0) { - accGetScriptLabel(s->mAccScript, - mInvokableNames[ct].string(), - (ACCvoid**) &s->mEnviroment.mInvokables[ct]); - } +// s->mEnviroment.mInvokeFunctions = (Script::InvokeFunc_t *)calloc(100, sizeof(void *)); +// BCCchar **labels = new char*[100]; +// bccGetFunctions(s->mBccScript, (BCCsizei *)&s->mEnviroment.mInvokeFunctionCount, +// 100, (BCCchar **)labels); + //LOGE("func count %i", s->mEnviroment.mInvokeFunctionCount); +// for (uint32_t i=0; i < s->mEnviroment.mInvokeFunctionCount; i++) { +// BCCsizei length; +// bccGetFunctionBinary(s->mBccScript, labels[i], (BCCvoid **)&(s->mEnviroment.mInvokeFunctions[i]), &length); + //LOGE("func %i %p", i, s->mEnviroment.mInvokeFunctions[i]); + // } + + s->mEnviroment.mFieldAddress = (void **)calloc(100, sizeof(void *)); + bccGetExportVars(s->mBccScript, (BCCsizei *)&s->mEnviroment.mFieldCount, + 100, s->mEnviroment.mFieldAddress); + //LOGE("var count %i", s->mEnviroment.mFieldCount); + for (uint32_t i=0; i < s->mEnviroment.mFieldCount; i++) { + //LOGE("var %i %p", i, s->mEnviroment.mFieldAddress[i]); } s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); - s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramFragmentStore()); + s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore()); s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster()); - if (s->mProgram.mScript) { + if (s->mProgram.mRoot) { const static int pragmaMax = 16; - ACCsizei pragmaCount; - ACCchar * str[pragmaMax]; - accGetPragmas(s->mAccScript, &pragmaCount, pragmaMax, &str[0]); + BCCsizei pragmaCount; + BCCchar * str[pragmaMax]; + bccGetPragmas(s->mBccScript, &pragmaCount, pragmaMax, &str[0]); for (int ct=0; ct < pragmaCount; ct+=2) { + //LOGE("pragme %s %s", str[ct], str[ct+1]); if (!strcmp(str[ct], "version")) { continue; } @@ -208,11 +312,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) s->mEnviroment.mVertex.clear(); continue; } - ProgramVertex * pv = (ProgramVertex *)rsc->lookupName(str[ct+1]); - if (pv != NULL) { - s->mEnviroment.mVertex.set(pv); - continue; - } LOGE("Unreconized value %s passed to stateVertex", str[ct+1]); } @@ -224,11 +323,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) s->mEnviroment.mRaster.clear(); continue; } - ProgramRaster * pr = (ProgramRaster *)rsc->lookupName(str[ct+1]); - if (pr != NULL) { - s->mEnviroment.mRaster.set(pr); - continue; - } LOGE("Unreconized value %s passed to stateRaster", str[ct+1]); } @@ -240,11 +334,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) s->mEnviroment.mFragment.clear(); continue; } - ProgramFragment * pf = (ProgramFragment *)rsc->lookupName(str[ct+1]); - if (pf != NULL) { - s->mEnviroment.mFragment.set(pf); - continue; - } LOGE("Unreconized value %s passed to stateFragment", str[ct+1]); } @@ -256,12 +345,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) s->mEnviroment.mFragmentStore.clear(); continue; } - ProgramFragmentStore * pfs = - (ProgramFragmentStore *)rsc->lookupName(str[ct+1]); - if (pfs != NULL) { - s->mEnviroment.mFragmentStore.set(pfs); - continue; - } LOGE("Unreconized value %s passed to stateStore", str[ct+1]); } @@ -273,111 +356,6 @@ void ScriptCState::runCompiler(Context *rsc, ScriptC *s) } } -static void appendElementBody(String8 *s, const Element *e) -{ - s->append(" {\n"); - for (size_t ct2=0; ct2 < e->getFieldCount(); ct2++) { - const Element *c = e->getField(ct2); - s->append(" "); - s->append(c->getCType()); - s->append(" "); - s->append(e->getFieldName(ct2)); - s->append(";\n"); - } - s->append("}"); -} - -void ScriptCState::appendVarDefines(const Context *rsc, String8 *str) -{ - char buf[256]; - if (rsc->props.mLogScripts) { - LOGD("appendVarDefines mInt32Defines.size()=%d mFloatDefines.size()=%d\n", - mInt32Defines.size(), mFloatDefines.size()); - } - for (size_t ct=0; ct < mInt32Defines.size(); ct++) { - str->append("#define "); - str->append(mInt32Defines.keyAt(ct)); - str->append(" "); - sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct)); - str->append(buf); - } - for (size_t ct=0; ct < mFloatDefines.size(); ct++) { - str->append("#define "); - str->append(mFloatDefines.keyAt(ct)); - str->append(" "); - sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct)); - str->append(buf); - } -} - - - -void ScriptCState::appendTypes(const Context *rsc, String8 *str) -{ - char buf[256]; - String8 tmp; - - str->append("struct vecF32_2_s {float x; float y;};\n"); - str->append("struct vecF32_3_s {float x; float y; float z;};\n"); - str->append("struct vecF32_4_s {float x; float y; float z; float w;};\n"); - str->append("struct vecU8_4_s {char r; char g; char b; char a;};\n"); - str->append("#define vecF32_2_t struct vecF32_2_s\n"); - str->append("#define vecF32_3_t struct vecF32_3_s\n"); - str->append("#define vecF32_4_t struct vecF32_4_s\n"); - str->append("#define vecU8_4_t struct vecU8_4_s\n"); - str->append("#define vecI8_4_t struct vecU8_4_s\n"); - - for (size_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { - const Type *t = mConstantBufferTypes[ct].get(); - if (!t) { - continue; - } - const Element *e = t->getElement(); - if (e->getName() && (e->getFieldCount() > 1)) { - String8 s("struct struct_"); - s.append(e->getName()); - s.append(e->getCStructBody()); - s.append(";\n"); - - s.append("#define "); - s.append(e->getName()); - s.append("_t struct struct_"); - s.append(e->getName()); - s.append("\n\n"); - if (rsc->props.mLogScripts) { - LOGV("%s", static_cast<const char*>(s)); - } - str->append(s); - } - - if (mSlotNames[ct].length() > 0) { - String8 s; - if (e->getName()) { - // Use the named struct - s.setTo(e->getName()); - } else { - // create an struct named from the slot. - s.setTo("struct "); - s.append(mSlotNames[ct]); - s.append("_s"); - s.append(e->getCStructBody()); - //appendElementBody(&s, e); - s.append(";\n"); - s.append("struct "); - s.append(mSlotNames[ct]); - s.append("_s"); - } - - s.append(" * "); - s.append(mSlotNames[ct]); - s.append(";\n"); - if (rsc->props.mLogScripts) { - LOGV("%s", static_cast<const char*>(s)); - } - str->append(s); - } - } -} namespace android { @@ -420,7 +398,6 @@ RsScript rsi_ScriptCCreate(Context * rsc) s->setContext(rsc); for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get()); - s->mSlotNames[ct] = ss->mSlotNames[ct]; s->mSlotWritable[ct] = ss->mSlotWritable[ct]; } @@ -428,18 +405,6 @@ RsScript rsi_ScriptCCreate(Context * rsc) return s; } -void rsi_ScriptCSetDefineF(Context *rsc, const char* name, float value) -{ - ScriptCState *ss = &rsc->mScriptC; - ss->mFloatDefines.add(String8(name), value); -} - -void rsi_ScriptCSetDefineI32(Context *rsc, const char* name, int32_t value) -{ - ScriptCState *ss = &rsc->mScriptC; - ss->mInt32Defines.add(String8(name), value); -} - } } |