rsScriptC.cpp revision 8b2c065dfc16c148e2829a19e83d2269b9bcd4cc
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 21#include "acc/acc.h" 22#include "utils/Timers.h" 23 24#include <GLES/gl.h> 25#include <GLES/glext.h> 26 27using namespace android; 28using namespace android::renderscript; 29 30#define GET_TLS() Context::ScriptTLSStruct * tls = \ 31 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \ 32 Context * rsc = tls->mContext; \ 33 ScriptC * sc = (ScriptC *) tls->mScript 34 35 36ScriptC::ScriptC() 37{ 38 mAccScript = NULL; 39 memset(&mProgram, 0, sizeof(mProgram)); 40} 41 42ScriptC::~ScriptC() 43{ 44 if (mAccScript) { 45 accDeleteScript(mAccScript); 46 } 47} 48 49 50bool ScriptC::run(Context *rsc, uint32_t launchIndex) 51{ 52 Context::ScriptTLSStruct * tls = 53 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); 54 55 if (mEnviroment.mFragmentStore.get()) { 56 rsc->setFragmentStore(mEnviroment.mFragmentStore.get()); 57 } 58 if (mEnviroment.mFragment.get()) { 59 rsc->setFragment(mEnviroment.mFragment.get()); 60 } 61 if (mEnviroment.mVertex.get()) { 62 rsc->setVertex(mEnviroment.mVertex.get()); 63 } 64 65 if (launchIndex == 0) { 66 mEnviroment.mStartTimeMillis 67 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); 68 } 69 70 bool ret = false; 71 tls->mScript = this; 72 ret = mProgram.mScript(launchIndex) != 0; 73 tls->mScript = NULL; 74 return ret; 75} 76 77ScriptCState::ScriptCState() 78{ 79 clear(); 80} 81 82ScriptCState::~ScriptCState() 83{ 84 if (mAccScript) { 85 accDeleteScript(mAccScript); 86 } 87} 88 89void ScriptCState::clear() 90{ 91 memset(&mProgram, 0, sizeof(mProgram)); 92 93 mConstantTypeCount = 0; 94 for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { 95 mConstantBufferTypes[ct].clear(); 96 } 97 98 memset(&mEnviroment, 0, sizeof(mEnviroment)); 99 mEnviroment.mClearColor[0] = 0; 100 mEnviroment.mClearColor[1] = 0; 101 mEnviroment.mClearColor[2] = 0; 102 mEnviroment.mClearColor[3] = 1; 103 mEnviroment.mClearDepth = 1; 104 mEnviroment.mClearStencil = 0; 105 mEnviroment.mIsRoot = false; 106 107 mAccScript = NULL; 108 109 mInt32Defines.clear(); 110 mFloatDefines.clear(); 111} 112 113static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name) 114{ 115 const ScriptCState::SymbolTable_t *sym = ScriptCState::lookupSymbol(name); 116 if (sym) { 117 return sym->mPtr; 118 } 119 LOGE("ScriptC sym lookup failed for %s", name); 120 return NULL; 121} 122 123void ScriptCState::runCompiler(Context *rsc) 124{ 125 mAccScript = accCreateScript(); 126 String8 tmp; 127 128 rsc->appendNameDefines(&tmp); 129 appendDecls(&tmp); 130 rsc->appendVarDefines(&tmp); 131 appendVarDefines(&tmp); 132 appendTypes(&tmp); 133 tmp.append("#line 1\n"); 134 135 const char* scriptSource[] = {tmp.string(), mProgram.mScriptText}; 136 int scriptLength[] = {tmp.length(), mProgram.mScriptTextLength} ; 137 accScriptSource(mAccScript, sizeof(scriptLength) / sizeof(int), scriptSource, scriptLength); 138 accRegisterSymbolCallback(mAccScript, symbolLookup, NULL); 139 accCompileScript(mAccScript); 140 accGetScriptLabel(mAccScript, "main", (ACCvoid**) &mProgram.mScript); 141 rsAssert(mProgram.mScript); 142 143 if (!mProgram.mScript) { 144 ACCchar buf[4096]; 145 ACCsizei len; 146 accGetScriptInfoLog(mAccScript, sizeof(buf), &len, buf); 147 LOGE(buf); 148 } 149 150 mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); 151 mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); 152 mEnviroment.mFragmentStore.set(rsc->getDefaultProgramFragmentStore()); 153 154 if (mProgram.mScript) { 155 const static int pragmaMax = 16; 156 ACCsizei pragmaCount; 157 ACCchar * str[pragmaMax]; 158 accGetPragmas(mAccScript, &pragmaCount, pragmaMax, &str[0]); 159 160 for (int ct=0; ct < pragmaCount; ct+=2) { 161 if (!strcmp(str[ct], "version")) { 162 continue; 163 } 164 165 if (!strcmp(str[ct], "stateVertex")) { 166 if (!strcmp(str[ct+1], "default")) { 167 continue; 168 } 169 if (!strcmp(str[ct+1], "parent")) { 170 mEnviroment.mVertex.clear(); 171 continue; 172 } 173 ProgramVertex * pv = (ProgramVertex *)rsc->lookupName(str[ct+1]); 174 if (pv != NULL) { 175 mEnviroment.mVertex.set(pv); 176 continue; 177 } 178 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]); 179 } 180 181 if (!strcmp(str[ct], "stateRaster")) { 182 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]); 183 } 184 185 if (!strcmp(str[ct], "stateFragment")) { 186 if (!strcmp(str[ct+1], "default")) { 187 continue; 188 } 189 if (!strcmp(str[ct+1], "parent")) { 190 mEnviroment.mFragment.clear(); 191 continue; 192 } 193 ProgramFragment * pf = (ProgramFragment *)rsc->lookupName(str[ct+1]); 194 if (pf != NULL) { 195 mEnviroment.mFragment.set(pf); 196 continue; 197 } 198 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]); 199 } 200 201 if (!strcmp(str[ct], "stateFragmentStore")) { 202 if (!strcmp(str[ct+1], "default")) { 203 continue; 204 } 205 if (!strcmp(str[ct+1], "parent")) { 206 mEnviroment.mFragmentStore.clear(); 207 continue; 208 } 209 ProgramFragmentStore * pfs = 210 (ProgramFragmentStore *)rsc->lookupName(str[ct+1]); 211 if (pfs != NULL) { 212 mEnviroment.mFragmentStore.set(pfs); 213 continue; 214 } 215 LOGE("Unreconized value %s passed to stateFragmentStore", str[ct+1]); 216 } 217 218 } 219 220 221 } else { 222 // Deal with an error. 223 } 224} 225 226 227void ScriptCState::appendVarDefines(String8 *str) 228{ 229 char buf[256]; 230 LOGD("appendVarDefines mInt32Defines.size()=%d mFloatDefines.size()=%d\n", 231 mInt32Defines.size(), mFloatDefines.size()); 232 for (size_t ct=0; ct < mInt32Defines.size(); ct++) { 233 str->append("#define "); 234 str->append(mInt32Defines.keyAt(ct)); 235 str->append(" "); 236 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct)); 237 str->append(buf); 238 } 239 for (size_t ct=0; ct < mFloatDefines.size(); ct++) { 240 str->append("#define "); 241 str->append(mFloatDefines.keyAt(ct)); 242 str->append(" "); 243 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct)); 244 str->append(buf); 245 } 246} 247 248void ScriptCState::appendTypes(String8 *str) 249{ 250 char buf[256]; 251 252 for (size_t ct=0; ct < mConstantTypeCount; ct++) { 253 const Type *t = mConstantBufferTypes[ct].get(); 254 const Element *e = t->getElement(); 255 256 if (!t->getName()) { 257 continue; 258 } 259 for (size_t ct2=0; ct2 < e->getComponentCount(); ct2++) { 260 const Component *c = e->getComponent(ct2); 261 str->append("#define OFFSETOF_"); 262 str->append(t->getName()); 263 str->append("_"); 264 str->append(c->getComponentName()); 265 sprintf(buf, " %i\n", ct2); 266 str->append(buf); 267 } 268 269 } 270 271} 272 273 274namespace android { 275namespace renderscript { 276 277void rsi_ScriptCBegin(Context * rsc) 278{ 279 ScriptCState *ss = &rsc->mScriptC; 280 ss->clear(); 281} 282 283void rsi_ScriptCAddType(Context * rsc, RsType vt) 284{ 285 ScriptCState *ss = &rsc->mScriptC; 286 const Type *t = static_cast<const Type *>(vt); 287 288 ss->mConstantBufferTypes[ss->mConstantTypeCount].set(t); 289 ss->mConstantTypeCount ++; 290} 291 292void rsi_ScriptCSetScript(Context * rsc, void *vp) 293{ 294 ScriptCState *ss = &rsc->mScriptC; 295 ss->mProgram.mScript = reinterpret_cast<ScriptC::RunScript_t>(vp); 296} 297 298void rsi_ScriptCSetRoot(Context * rsc, bool isRoot) 299{ 300 ScriptCState *ss = &rsc->mScriptC; 301 ss->mEnviroment.mIsRoot = isRoot; 302} 303 304void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) 305{ 306 ScriptCState *ss = &rsc->mScriptC; 307 ss->mProgram.mScriptText = text; 308 ss->mProgram.mScriptTextLength = len; 309} 310 311 312RsScript rsi_ScriptCCreate(Context * rsc) 313{ 314 ScriptCState *ss = &rsc->mScriptC; 315 316 ss->runCompiler(rsc); 317 318 ScriptC *s = new ScriptC(); 319 s->incRef(); 320 s->mAccScript = ss->mAccScript; 321 ss->mAccScript = NULL; 322 s->mEnviroment = ss->mEnviroment; 323 s->mProgram = ss->mProgram; 324 ss->clear(); 325 326 return s; 327} 328 329void rsi_ScriptCSetDefineF(Context *rsc, const char* name, float value) 330{ 331 ScriptCState *ss = &rsc->mScriptC; 332 ss->mFloatDefines.add(String8(name), value); 333} 334 335void rsi_ScriptCSetDefineI32(Context *rsc, const char* name, int32_t value) 336{ 337 ScriptCState *ss = &rsc->mScriptC; 338 ss->mInt32Defines.add(String8(name), value); 339} 340 341} 342} 343 344 345