Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 21 | #include "acc/acc.h" |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 22 | #include "utils/Timers.h" |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 23 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 24 | #include <GLES/gl.h> |
| 25 | #include <GLES/glext.h> |
| 26 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | using namespace android; |
| 28 | using namespace android::renderscript; |
| 29 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 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 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 35 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 36 | ScriptC::ScriptC(Context *rsc) : Script(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 38 | mAllocFile = __FILE__; |
| 39 | mAllocLine = __LINE__; |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 40 | mAccScript = NULL; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 41 | memset(&mProgram, 0, sizeof(mProgram)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | ScriptC::~ScriptC() |
| 45 | { |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 46 | if (mAccScript) { |
| 47 | accDeleteScript(mAccScript); |
| 48 | } |
Jason Sams | 9d5e03d | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 49 | free(mEnviroment.mScriptText); |
| 50 | mEnviroment.mScriptText = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Jason Sams | e60446b | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 53 | void ScriptC::setupScript() |
| 54 | { |
| 55 | for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { |
| 56 | if (mProgram.mSlotPointers[ct]) { |
| 57 | *mProgram.mSlotPointers[ct] = mSlots[ct]->getPtr(); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 62 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 63 | uint32_t ScriptC::run(Context *rsc, uint32_t launchIndex) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 64 | { |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 65 | if (mProgram.mScript == NULL) { |
| 66 | rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script"); |
| 67 | return 0; |
| 68 | } |
| 69 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 70 | Context::ScriptTLSStruct * tls = |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 71 | (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); |
Jason Sams | 996db8d | 2009-10-06 17:16:55 -0700 | [diff] [blame] | 72 | rsAssert(tls); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 73 | |
| 74 | if (mEnviroment.mFragmentStore.get()) { |
| 75 | rsc->setFragmentStore(mEnviroment.mFragmentStore.get()); |
| 76 | } |
| 77 | if (mEnviroment.mFragment.get()) { |
| 78 | rsc->setFragment(mEnviroment.mFragment.get()); |
| 79 | } |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 80 | if (mEnviroment.mVertex.get()) { |
| 81 | rsc->setVertex(mEnviroment.mVertex.get()); |
| 82 | } |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 83 | if (mEnviroment.mRaster.get()) { |
| 84 | rsc->setRaster(mEnviroment.mRaster.get()); |
| 85 | } |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 86 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 87 | if (launchIndex == 0) { |
| 88 | mEnviroment.mStartTimeMillis |
| 89 | = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 90 | } |
Jason Sams | e60446b | 2009-09-24 14:55:38 -0700 | [diff] [blame] | 91 | setupScript(); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 92 | |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 93 | uint32_t ret = 0; |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 94 | tls->mScript = this; |
Jason Sams | b9d5c57 | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 95 | ret = mProgram.mScript(launchIndex); |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 96 | tls->mScript = NULL; |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 97 | return ret; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | ScriptCState::ScriptCState() |
| 101 | { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 102 | mScript = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 103 | clear(); |
| 104 | } |
| 105 | |
| 106 | ScriptCState::~ScriptCState() |
| 107 | { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 108 | delete mScript; |
| 109 | mScript = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void ScriptCState::clear() |
| 113 | { |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 114 | for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { |
| 115 | mConstantBufferTypes[ct].clear(); |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 116 | mSlotNames[ct].setTo(""); |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 117 | mInvokableNames[ct].setTo(""); |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 118 | mSlotWritable[ct] = false; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 119 | } |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 120 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 121 | delete mScript; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 122 | mScript = new ScriptC(NULL); |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 123 | |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 124 | mInt32Defines.clear(); |
| 125 | mFloatDefines.clear(); |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 128 | static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name) |
Jason Sams | 54440a0 | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 129 | { |
| 130 | const ScriptCState::SymbolTable_t *sym = ScriptCState::lookupSymbol(name); |
Jason Sams | 54440a0 | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 131 | if (sym) { |
| 132 | return sym->mPtr; |
| 133 | } |
Jason Sams | 54440a0 | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 134 | LOGE("ScriptC sym lookup failed for %s", name); |
Jason Sams | 54440a0 | 2009-07-16 15:08:06 -0700 | [diff] [blame] | 135 | return NULL; |
| 136 | } |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 137 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 138 | void ScriptCState::runCompiler(Context *rsc, ScriptC *s) |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 139 | { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 140 | s->mAccScript = accCreateScript(); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 141 | String8 tmp; |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 142 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 143 | rsc->appendNameDefines(&tmp); |
Jason Sams | 764205c | 2009-07-16 17:47:40 -0700 | [diff] [blame] | 144 | appendDecls(&tmp); |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 145 | appendVarDefines(rsc, &tmp); |
| 146 | appendTypes(rsc, &tmp); |
Jason Sams | ee41112 | 2009-07-21 12:20:54 -0700 | [diff] [blame] | 147 | tmp.append("#line 1\n"); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 148 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 149 | const char* scriptSource[] = {tmp.string(), s->mEnviroment.mScriptText}; |
| 150 | int scriptLength[] = {tmp.length(), s->mEnviroment.mScriptTextLength} ; |
| 151 | accScriptSource(s->mAccScript, sizeof(scriptLength) / sizeof(int), scriptSource, scriptLength); |
| 152 | accRegisterSymbolCallback(s->mAccScript, symbolLookup, NULL); |
| 153 | accCompileScript(s->mAccScript); |
| 154 | accGetScriptLabel(s->mAccScript, "main", (ACCvoid**) &s->mProgram.mScript); |
| 155 | accGetScriptLabel(s->mAccScript, "init", (ACCvoid**) &s->mProgram.mInit); |
| 156 | rsAssert(s->mProgram.mScript); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 157 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 158 | if (!s->mProgram.mScript) { |
Jason Sams | 764205c | 2009-07-16 17:47:40 -0700 | [diff] [blame] | 159 | ACCchar buf[4096]; |
| 160 | ACCsizei len; |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 161 | accGetScriptInfoLog(s->mAccScript, sizeof(buf), &len, buf); |
Jason Sams | 156cce6 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 162 | LOGE(buf); |
| 163 | rsc->setError(RS_ERROR_BAD_SCRIPT, "Error compiling user script."); |
| 164 | return; |
Jason Sams | 764205c | 2009-07-16 17:47:40 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 167 | if (s->mProgram.mInit) { |
| 168 | s->mProgram.mInit(); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { |
| 172 | if (mSlotNames[ct].length() > 0) { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 173 | accGetScriptLabel(s->mAccScript, |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 174 | mSlotNames[ct].string(), |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 175 | (ACCvoid**) &s->mProgram.mSlotPointers[ct]); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 179 | for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { |
| 180 | if (mInvokableNames[ct].length() > 0) { |
| 181 | accGetScriptLabel(s->mAccScript, |
| 182 | mInvokableNames[ct].string(), |
| 183 | (ACCvoid**) &s->mEnviroment.mInvokables[ct]); |
| 184 | } |
| 185 | } |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 186 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 187 | s->mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); |
| 188 | s->mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); |
| 189 | s->mEnviroment.mFragmentStore.set(rsc->getDefaultProgramFragmentStore()); |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 190 | s->mEnviroment.mRaster.set(rsc->getDefaultProgramRaster()); |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 191 | |
| 192 | if (s->mProgram.mScript) { |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 193 | const static int pragmaMax = 16; |
| 194 | ACCsizei pragmaCount; |
| 195 | ACCchar * str[pragmaMax]; |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 196 | accGetPragmas(s->mAccScript, &pragmaCount, pragmaMax, &str[0]); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 197 | |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 198 | for (int ct=0; ct < pragmaCount; ct+=2) { |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 199 | if (!strcmp(str[ct], "version")) { |
| 200 | continue; |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 203 | if (!strcmp(str[ct], "stateVertex")) { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 204 | if (!strcmp(str[ct+1], "default")) { |
| 205 | continue; |
| 206 | } |
| 207 | if (!strcmp(str[ct+1], "parent")) { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 208 | s->mEnviroment.mVertex.clear(); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 209 | continue; |
| 210 | } |
| 211 | ProgramVertex * pv = (ProgramVertex *)rsc->lookupName(str[ct+1]); |
| 212 | if (pv != NULL) { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 213 | s->mEnviroment.mVertex.set(pv); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 214 | continue; |
| 215 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 216 | LOGE("Unreconized value %s passed to stateVertex", str[ct+1]); |
| 217 | } |
| 218 | |
| 219 | if (!strcmp(str[ct], "stateRaster")) { |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 220 | if (!strcmp(str[ct+1], "default")) { |
| 221 | continue; |
| 222 | } |
| 223 | if (!strcmp(str[ct+1], "parent")) { |
| 224 | s->mEnviroment.mRaster.clear(); |
| 225 | continue; |
| 226 | } |
| 227 | ProgramRaster * pr = (ProgramRaster *)rsc->lookupName(str[ct+1]); |
| 228 | if (pr != NULL) { |
| 229 | s->mEnviroment.mRaster.set(pr); |
| 230 | continue; |
| 231 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 232 | LOGE("Unreconized value %s passed to stateRaster", str[ct+1]); |
| 233 | } |
| 234 | |
| 235 | if (!strcmp(str[ct], "stateFragment")) { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 236 | if (!strcmp(str[ct+1], "default")) { |
| 237 | continue; |
| 238 | } |
| 239 | if (!strcmp(str[ct+1], "parent")) { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 240 | s->mEnviroment.mFragment.clear(); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 241 | continue; |
| 242 | } |
| 243 | ProgramFragment * pf = (ProgramFragment *)rsc->lookupName(str[ct+1]); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 244 | if (pf != NULL) { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 245 | s->mEnviroment.mFragment.set(pf); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 246 | continue; |
| 247 | } |
| 248 | LOGE("Unreconized value %s passed to stateFragment", str[ct+1]); |
| 249 | } |
| 250 | |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 251 | if (!strcmp(str[ct], "stateStore")) { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 252 | if (!strcmp(str[ct+1], "default")) { |
| 253 | continue; |
| 254 | } |
| 255 | if (!strcmp(str[ct+1], "parent")) { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 256 | s->mEnviroment.mFragmentStore.clear(); |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 257 | continue; |
| 258 | } |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 259 | ProgramFragmentStore * pfs = |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 260 | (ProgramFragmentStore *)rsc->lookupName(str[ct+1]); |
| 261 | if (pfs != NULL) { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 262 | s->mEnviroment.mFragmentStore.set(pfs); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 263 | continue; |
| 264 | } |
Jason Sams | 5235cf3 | 2009-09-28 18:12:56 -0700 | [diff] [blame] | 265 | LOGE("Unreconized value %s passed to stateStore", str[ct+1]); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | } |
| 269 | |
Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 270 | |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 271 | } else { |
| 272 | // Deal with an error. |
| 273 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 276 | static void appendElementBody(String8 *s, const Element *e) |
| 277 | { |
| 278 | s->append(" {\n"); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 279 | for (size_t ct2=0; ct2 < e->getFieldCount(); ct2++) { |
| 280 | const Element *c = e->getField(ct2); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 281 | s->append(" "); |
| 282 | s->append(c->getCType()); |
| 283 | s->append(" "); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 284 | s->append(e->getFieldName(ct2)); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 285 | s->append(";\n"); |
| 286 | } |
| 287 | s->append("}"); |
| 288 | } |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 289 | |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 290 | void ScriptCState::appendVarDefines(const Context *rsc, String8 *str) |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 291 | { |
| 292 | char buf[256]; |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 293 | if (rsc->props.mLogScripts) { |
| 294 | LOGD("appendVarDefines mInt32Defines.size()=%d mFloatDefines.size()=%d\n", |
| 295 | mInt32Defines.size(), mFloatDefines.size()); |
| 296 | } |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 297 | for (size_t ct=0; ct < mInt32Defines.size(); ct++) { |
| 298 | str->append("#define "); |
| 299 | str->append(mInt32Defines.keyAt(ct)); |
| 300 | str->append(" "); |
| 301 | sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct)); |
| 302 | str->append(buf); |
| 303 | } |
| 304 | for (size_t ct=0; ct < mFloatDefines.size(); ct++) { |
| 305 | str->append("#define "); |
| 306 | str->append(mFloatDefines.keyAt(ct)); |
| 307 | str->append(" "); |
| 308 | sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct)); |
| 309 | str->append(buf); |
| 310 | } |
| 311 | } |
| 312 | |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 313 | |
| 314 | |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 315 | void ScriptCState::appendTypes(const Context *rsc, String8 *str) |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 316 | { |
| 317 | char buf[256]; |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 318 | String8 tmp; |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 319 | |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 320 | str->append("struct vecF32_2_s {float x; float y;};\n"); |
| 321 | str->append("struct vecF32_3_s {float x; float y; float z;};\n"); |
| 322 | str->append("struct vecF32_4_s {float x; float y; float z; float w;};\n"); |
| 323 | str->append("struct vecU8_4_s {char r; char g; char b; char a;};\n"); |
| 324 | str->append("#define vecF32_2_t struct vecF32_2_s\n"); |
| 325 | str->append("#define vecF32_3_t struct vecF32_3_s\n"); |
| 326 | str->append("#define vecF32_4_t struct vecF32_4_s\n"); |
| 327 | str->append("#define vecU8_4_t struct vecU8_4_s\n"); |
| 328 | str->append("#define vecI8_4_t struct vecU8_4_s\n"); |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 329 | |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 330 | for (size_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) { |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 331 | const Type *t = mConstantBufferTypes[ct].get(); |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 332 | if (!t) { |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 333 | continue; |
| 334 | } |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 335 | const Element *e = t->getElement(); |
Jason Sams | 0011bcf | 2009-12-15 12:58:36 -0800 | [diff] [blame] | 336 | if (e->getName() && (e->getFieldCount() > 1)) { |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 337 | String8 s("struct struct_"); |
| 338 | s.append(e->getName()); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 339 | s.append(e->getCStructBody()); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 340 | s.append(";\n"); |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 341 | |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 342 | s.append("#define "); |
| 343 | s.append(e->getName()); |
| 344 | s.append("_t struct struct_"); |
| 345 | s.append(e->getName()); |
| 346 | s.append("\n\n"); |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 347 | if (rsc->props.mLogScripts) { |
| 348 | LOGV(s); |
| 349 | } |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 350 | str->append(s); |
| 351 | } |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 352 | |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 353 | if (mSlotNames[ct].length() > 0) { |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 354 | String8 s; |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 355 | if (e->getName()) { |
| 356 | // Use the named struct |
| 357 | s.setTo(e->getName()); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 358 | } else { |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 359 | // create an struct named from the slot. |
| 360 | s.setTo("struct "); |
| 361 | s.append(mSlotNames[ct]); |
| 362 | s.append("_s"); |
| 363 | s.append(e->getCStructBody()); |
| 364 | //appendElementBody(&s, e); |
| 365 | s.append(";\n"); |
| 366 | s.append("struct "); |
| 367 | s.append(mSlotNames[ct]); |
| 368 | s.append("_s"); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 369 | } |
Jason Sams | 718cd1f | 2009-12-23 14:35:29 -0800 | [diff] [blame] | 370 | |
| 371 | s.append(" * "); |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 372 | s.append(mSlotNames[ct]); |
| 373 | s.append(";\n"); |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 374 | if (rsc->props.mLogScripts) { |
| 375 | LOGV(s); |
| 376 | } |
Jason Sams | 2525a81 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 377 | str->append(s); |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 378 | } |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 379 | } |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 382 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 383 | namespace android { |
| 384 | namespace renderscript { |
| 385 | |
| 386 | void rsi_ScriptCBegin(Context * rsc) |
| 387 | { |
| 388 | ScriptCState *ss = &rsc->mScriptC; |
| 389 | ss->clear(); |
| 390 | } |
| 391 | |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 392 | void rsi_ScriptCSetScript(Context * rsc, void *vp) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 393 | { |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 394 | rsAssert(0); |
| 395 | //ScriptCState *ss = &rsc->mScriptC; |
| 396 | //ss->mProgram.mScript = reinterpret_cast<ScriptC::RunScript_t>(vp); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 399 | void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) |
| 400 | { |
| 401 | ScriptCState *ss = &rsc->mScriptC; |
Jason Sams | 9d5e03d | 2009-11-03 11:25:42 -0800 | [diff] [blame] | 402 | |
| 403 | char *t = (char *)malloc(len + 1); |
| 404 | memcpy(t, text, len); |
| 405 | t[len] = 0; |
| 406 | ss->mScript->mEnviroment.mScriptText = t; |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 407 | ss->mScript->mEnviroment.mScriptTextLength = len; |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 411 | RsScript rsi_ScriptCCreate(Context * rsc) |
| 412 | { |
| 413 | ScriptCState *ss = &rsc->mScriptC; |
| 414 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 415 | ScriptC *s = ss->mScript; |
| 416 | ss->mScript = NULL; |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 417 | |
Jason Sams | be2e841 | 2009-09-16 15:04:38 -0700 | [diff] [blame] | 418 | ss->runCompiler(rsc, s); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 419 | s->incUserRef(); |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 420 | s->setContext(rsc); |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 421 | for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) { |
| 422 | s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get()); |
| 423 | s->mSlotNames[ct] = ss->mSlotNames[ct]; |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 424 | s->mSlotWritable[ct] = ss->mSlotWritable[ct]; |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 425 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 426 | |
Jason Sams | fbf0b9e | 2009-08-13 12:59:04 -0700 | [diff] [blame] | 427 | ss->clear(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 428 | return s; |
| 429 | } |
| 430 | |
Joe Onorato | d7b3774 | 2009-08-09 22:57:44 -0700 | [diff] [blame] | 431 | void rsi_ScriptCSetDefineF(Context *rsc, const char* name, float value) |
| 432 | { |
| 433 | ScriptCState *ss = &rsc->mScriptC; |
| 434 | ss->mFloatDefines.add(String8(name), value); |
| 435 | } |
| 436 | |
| 437 | void rsi_ScriptCSetDefineI32(Context *rsc, const char* name, int32_t value) |
| 438 | { |
| 439 | ScriptCState *ss = &rsc->mScriptC; |
| 440 | ss->mInt32Defines.add(String8(name), value); |
| 441 | } |
| 442 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | |