rsScriptC.cpp revision e45ac6e91864f2a6617b6a2c0aa87cdb62cf58fd
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/String8.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 bool ret = false; 66 tls->mScript = this; 67 ret = mProgram.mScript(launchIndex) != 0; 68 tls->mScript = NULL; 69 return ret; 70} 71 72ScriptCState::ScriptCState() 73{ 74 clear(); 75} 76 77ScriptCState::~ScriptCState() 78{ 79 if (mAccScript) { 80 accDeleteScript(mAccScript); 81 } 82} 83 84void ScriptCState::clear() 85{ 86 memset(&mProgram, 0, sizeof(mProgram)); 87 88 mConstantBufferTypes.clear(); 89 90 memset(&mEnviroment, 0, sizeof(mEnviroment)); 91 mEnviroment.mClearColor[0] = 0; 92 mEnviroment.mClearColor[1] = 0; 93 mEnviroment.mClearColor[2] = 0; 94 mEnviroment.mClearColor[3] = 1; 95 mEnviroment.mClearDepth = 1; 96 mEnviroment.mClearStencil = 0; 97 mEnviroment.mIsRoot = false; 98 99 mAccScript = NULL; 100 101} 102 103static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name) 104{ 105 const ScriptCState::SymbolTable_t *sym = ScriptCState::lookupSymbol(name); 106 107 if (sym) { 108 return sym->mPtr; 109 } 110 111 LOGE("ScriptC sym lookup failed for %s", name); 112 113 // Default to calling dlsym to allow any global symbol: 114 return NULL; 115} 116 117void ScriptCState::runCompiler(Context *rsc) 118{ 119 mAccScript = accCreateScript(); 120 String8 tmp; 121 122 rsc->appendNameDefines(&tmp); 123 appendDecls(&tmp); 124 //tmp.append("#line 1\n"); 125 126 const char* scriptSource[] = {tmp.string(), mProgram.mScriptText}; 127 int scriptLength[] = {tmp.length(), mProgram.mScriptTextLength} ; 128 accScriptSource(mAccScript, sizeof(scriptLength) / sizeof(int), scriptSource, scriptLength); 129 accRegisterSymbolCallback(mAccScript, symbolLookup, NULL); 130 accCompileScript(mAccScript); 131 accGetScriptLabel(mAccScript, "main", (ACCvoid**) &mProgram.mScript); 132 rsAssert(mProgram.mScript); 133 134 if (!mProgram.mScript) { 135 ACCchar buf[4096]; 136 ACCsizei len; 137 accGetScriptInfoLog(mAccScript, sizeof(buf), &len, buf); 138 LOGE(buf); 139 140 } 141 142 mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); 143 mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); 144 mEnviroment.mFragmentStore.set(rsc->getDefaultProgramFragmentStore()); 145 146 if (mProgram.mScript) { 147 const static int pragmaMax = 16; 148 ACCsizei pragmaCount; 149 ACCchar * str[pragmaMax]; 150 accGetPragmas(mAccScript, &pragmaCount, pragmaMax, &str[0]); 151 152 for (int ct=0; ct < pragmaCount; ct+=2) { 153 if (!strcmp(str[ct], "version")) { 154 continue; 155 } 156 157 if (!strcmp(str[ct], "stateVertex")) { 158 if (!strcmp(str[ct+1], "default")) { 159 continue; 160 } 161 if (!strcmp(str[ct+1], "parent")) { 162 mEnviroment.mVertex.clear(); 163 continue; 164 } 165 ProgramVertex * pv = (ProgramVertex *)rsc->lookupName(str[ct+1]); 166 if (pv != NULL) { 167 mEnviroment.mVertex.set(pv); 168 continue; 169 } 170 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]); 171 } 172 173 if (!strcmp(str[ct], "stateRaster")) { 174 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]); 175 } 176 177 if (!strcmp(str[ct], "stateFragment")) { 178 if (!strcmp(str[ct+1], "default")) { 179 continue; 180 } 181 if (!strcmp(str[ct+1], "parent")) { 182 mEnviroment.mFragment.clear(); 183 continue; 184 } 185 ProgramFragment * pf = (ProgramFragment *)rsc->lookupName(str[ct+1]); 186 if (pf != NULL) { 187 mEnviroment.mFragment.set(pf); 188 continue; 189 } 190 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]); 191 } 192 193 if (!strcmp(str[ct], "stateFragmentStore")) { 194 if (!strcmp(str[ct+1], "default")) { 195 continue; 196 } 197 if (!strcmp(str[ct+1], "parent")) { 198 mEnviroment.mFragmentStore.clear(); 199 continue; 200 } 201 ProgramFragmentStore * pfs = 202 (ProgramFragmentStore *)rsc->lookupName(str[ct+1]); 203 if (pfs != NULL) { 204 mEnviroment.mFragmentStore.set(pfs); 205 continue; 206 } 207 LOGE("Unreconized value %s passed to stateFragmentStore", str[ct+1]); 208 } 209 210 } 211 212 213 } else { 214 // Deal with an error. 215 } 216 217} 218 219namespace android { 220namespace renderscript { 221 222void rsi_ScriptCBegin(Context * rsc) 223{ 224 ScriptCState *ss = &rsc->mScriptC; 225 ss->clear(); 226} 227 228void rsi_ScriptCSetClearColor(Context * rsc, float r, float g, float b, float a) 229{ 230 ScriptCState *ss = &rsc->mScriptC; 231 ss->mEnviroment.mClearColor[0] = r; 232 ss->mEnviroment.mClearColor[1] = g; 233 ss->mEnviroment.mClearColor[2] = b; 234 ss->mEnviroment.mClearColor[3] = a; 235} 236 237void rsi_ScriptCSetClearDepth(Context * rsc, float v) 238{ 239 ScriptCState *ss = &rsc->mScriptC; 240 ss->mEnviroment.mClearDepth = v; 241} 242 243void rsi_ScriptCSetClearStencil(Context * rsc, uint32_t v) 244{ 245 ScriptCState *ss = &rsc->mScriptC; 246 ss->mEnviroment.mClearStencil = v; 247} 248 249void rsi_ScriptCAddType(Context * rsc, RsType vt) 250{ 251 ScriptCState *ss = &rsc->mScriptC; 252 ss->mConstantBufferTypes.add(static_cast<const Type *>(vt)); 253} 254 255void rsi_ScriptCSetScript(Context * rsc, void *vp) 256{ 257 ScriptCState *ss = &rsc->mScriptC; 258 ss->mProgram.mScript = reinterpret_cast<ScriptC::RunScript_t>(vp); 259} 260 261void rsi_ScriptCSetRoot(Context * rsc, bool isRoot) 262{ 263 ScriptCState *ss = &rsc->mScriptC; 264 ss->mEnviroment.mIsRoot = isRoot; 265} 266 267void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) 268{ 269 ScriptCState *ss = &rsc->mScriptC; 270 ss->mProgram.mScriptText = text; 271 ss->mProgram.mScriptTextLength = len; 272} 273 274 275RsScript rsi_ScriptCCreate(Context * rsc) 276{ 277 ScriptCState *ss = &rsc->mScriptC; 278 279 ss->runCompiler(rsc); 280 281 ScriptC *s = new ScriptC(); 282 s->incRef(); 283 s->mAccScript = ss->mAccScript; 284 ss->mAccScript = NULL; 285 s->mEnviroment = ss->mEnviroment; 286 s->mProgram = ss->mProgram; 287 ss->clear(); 288 289 return s; 290} 291 292} 293} 294 295 296