blob: ced25163288686285db58e9832731ef4acd25cd6 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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 Palevichec5a20b2009-05-28 15:53:04 -070021#include "acc/acc.h"
Joe Onorato3370ec92009-08-09 11:39:02 -070022#include "utils/Timers.h"
Jack Palevichec5a20b2009-05-28 15:53:04 -070023
Jason Sams4b962e52009-06-22 17:15:15 -070024#include <GLES/gl.h>
25#include <GLES/glext.h>
26
Jason Samsd19f10d2009-05-22 14:03:28 -070027using namespace android;
28using namespace android::renderscript;
29
Jason Sams462d11b2009-06-19 16:03:18 -070030#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 Samsd19f10d2009-05-22 14:03:28 -070035
36ScriptC::ScriptC()
37{
Jack Palevichec5a20b2009-05-28 15:53:04 -070038 mAccScript = NULL;
Jason Sams3a833d82009-06-08 15:20:31 -070039 memset(&mProgram, 0, sizeof(mProgram));
Jason Samsd19f10d2009-05-22 14:03:28 -070040}
41
42ScriptC::~ScriptC()
43{
Jack Palevichec5a20b2009-05-28 15:53:04 -070044 if (mAccScript) {
45 accDeleteScript(mAccScript);
46 }
Jason Samsd19f10d2009-05-22 14:03:28 -070047}
48
Jason Samsd19f10d2009-05-22 14:03:28 -070049
Jason Sams462d11b2009-06-19 16:03:18 -070050bool ScriptC::run(Context *rsc, uint32_t launchIndex)
Jason Samsd19f10d2009-05-22 14:03:28 -070051{
Jason Sams22534172009-08-04 16:58:20 -070052 Context::ScriptTLSStruct * tls =
Jason Sams462d11b2009-06-19 16:03:18 -070053 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey);
Jason Sams3eaa3382009-06-10 15:04:38 -070054
55 if (mEnviroment.mFragmentStore.get()) {
56 rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
57 }
58 if (mEnviroment.mFragment.get()) {
59 rsc->setFragment(mEnviroment.mFragment.get());
60 }
Jason Sams9c54bdb2009-06-17 16:52:59 -070061 if (mEnviroment.mVertex.get()) {
62 rsc->setVertex(mEnviroment.mVertex.get());
63 }
Jason Sams3eaa3382009-06-10 15:04:38 -070064
Joe Onorato3370ec92009-08-09 11:39:02 -070065 if (launchIndex == 0) {
66 mEnviroment.mStartTimeMillis
67 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
68 }
69
Jason Samsc97bb882009-07-20 14:31:06 -070070 bool ret = false;
Jason Sams462d11b2009-06-19 16:03:18 -070071 tls->mScript = this;
Jason Samsc97bb882009-07-20 14:31:06 -070072 ret = mProgram.mScript(launchIndex) != 0;
Jason Sams462d11b2009-06-19 16:03:18 -070073 tls->mScript = NULL;
Jason Samsc97bb882009-07-20 14:31:06 -070074 return ret;
Jason Samsd19f10d2009-05-22 14:03:28 -070075}
76
77ScriptCState::ScriptCState()
78{
79 clear();
80}
81
82ScriptCState::~ScriptCState()
83{
Jack Palevichec5a20b2009-05-28 15:53:04 -070084 if (mAccScript) {
85 accDeleteScript(mAccScript);
86 }
Jason Samsd19f10d2009-05-22 14:03:28 -070087}
88
89void ScriptCState::clear()
90{
Jason Sams3a833d82009-06-08 15:20:31 -070091 memset(&mProgram, 0, sizeof(mProgram));
92
Jason Sams43ee06852009-08-12 17:54:11 -070093 for (uint32_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
94 mConstantBufferTypes[ct].clear();
Jason Samsfbf0b9e2009-08-13 12:59:04 -070095 mSlotNames[ct].setTo("");
Jason Sams43ee06852009-08-12 17:54:11 -070096 }
Jason Sams3a833d82009-06-08 15:20:31 -070097
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;
Jason Sams3a833d82009-06-08 15:20:31 -0700106
Jack Palevichec5a20b2009-05-28 15:53:04 -0700107 mAccScript = NULL;
Jason Sams3a833d82009-06-08 15:20:31 -0700108
Joe Onoratod7b37742009-08-09 22:57:44 -0700109 mInt32Defines.clear();
110 mFloatDefines.clear();
Jason Sams39ddc9502009-06-05 17:35:09 -0700111}
112
Jason Sams22534172009-08-04 16:58:20 -0700113static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name)
Jason Sams54440a02009-07-16 15:08:06 -0700114{
115 const ScriptCState::SymbolTable_t *sym = ScriptCState::lookupSymbol(name);
Jason Sams54440a02009-07-16 15:08:06 -0700116 if (sym) {
117 return sym->mPtr;
118 }
Jason Sams54440a02009-07-16 15:08:06 -0700119 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams54440a02009-07-16 15:08:06 -0700120 return NULL;
121}
Jason Samsd5680f92009-06-10 18:39:40 -0700122
Jason Sams3eaa3382009-06-10 15:04:38 -0700123void ScriptCState::runCompiler(Context *rsc)
Jason Sams39ddc9502009-06-05 17:35:09 -0700124{
125 mAccScript = accCreateScript();
Jason Samsd5680f92009-06-10 18:39:40 -0700126 String8 tmp;
Jason Sams39ddc9502009-06-05 17:35:09 -0700127
Jason Samsd5680f92009-06-10 18:39:40 -0700128 rsc->appendNameDefines(&tmp);
Jason Sams764205c2009-07-16 17:47:40 -0700129 appendDecls(&tmp);
Joe Onoratod7b37742009-08-09 22:57:44 -0700130 rsc->appendVarDefines(&tmp);
131 appendVarDefines(&tmp);
Jason Sams43ee06852009-08-12 17:54:11 -0700132 appendTypes(&tmp);
Jason Samsee411122009-07-21 12:20:54 -0700133 tmp.append("#line 1\n");
Jason Samsd5680f92009-06-10 18:39:40 -0700134
135 const char* scriptSource[] = {tmp.string(), mProgram.mScriptText};
136 int scriptLength[] = {tmp.length(), mProgram.mScriptTextLength} ;
137 accScriptSource(mAccScript, sizeof(scriptLength) / sizeof(int), scriptSource, scriptLength);
Jason Sams54440a02009-07-16 15:08:06 -0700138 accRegisterSymbolCallback(mAccScript, symbolLookup, NULL);
Jason Sams39ddc9502009-06-05 17:35:09 -0700139 accCompileScript(mAccScript);
Jason Sams3a833d82009-06-08 15:20:31 -0700140 accGetScriptLabel(mAccScript, "main", (ACCvoid**) &mProgram.mScript);
Jason Samsda423d82009-06-09 12:15:30 -0700141 rsAssert(mProgram.mScript);
142
Jason Sams764205c2009-07-16 17:47:40 -0700143 if (!mProgram.mScript) {
144 ACCchar buf[4096];
145 ACCsizei len;
146 accGetScriptInfoLog(mAccScript, sizeof(buf), &len, buf);
147 LOGE(buf);
Jason Sams764205c2009-07-16 17:47:40 -0700148 }
149
Jason Sams9c54bdb2009-06-17 16:52:59 -0700150 mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
151 mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
152 mEnviroment.mFragmentStore.set(rsc->getDefaultProgramFragmentStore());
Jason Samsd5680f92009-06-10 18:39:40 -0700153
Jason Samsda423d82009-06-09 12:15:30 -0700154 if (mProgram.mScript) {
155 const static int pragmaMax = 16;
156 ACCsizei pragmaCount;
157 ACCchar * str[pragmaMax];
158 accGetPragmas(mAccScript, &pragmaCount, pragmaMax, &str[0]);
159
Jason Samsda423d82009-06-09 12:15:30 -0700160 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsda423d82009-06-09 12:15:30 -0700161 if (!strcmp(str[ct], "version")) {
162 continue;
Jason Samsda423d82009-06-09 12:15:30 -0700163 }
164
Jason Samsda423d82009-06-09 12:15:30 -0700165 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700166 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 }
Jason Samsda423d82009-06-09 12:15:30 -0700178 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
179 }
180
181 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsda423d82009-06-09 12:15:30 -0700182 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
183 }
184
185 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700186 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]);
Jason Sams3eaa3382009-06-10 15:04:38 -0700194 if (pf != NULL) {
195 mEnviroment.mFragment.set(pf);
Jason Samsda423d82009-06-09 12:15:30 -0700196 continue;
197 }
198 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
199 }
200
201 if (!strcmp(str[ct], "stateFragmentStore")) {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700202 if (!strcmp(str[ct+1], "default")) {
203 continue;
204 }
205 if (!strcmp(str[ct+1], "parent")) {
206 mEnviroment.mFragmentStore.clear();
207 continue;
208 }
Jason Sams22534172009-08-04 16:58:20 -0700209 ProgramFragmentStore * pfs =
Jason Sams3eaa3382009-06-10 15:04:38 -0700210 (ProgramFragmentStore *)rsc->lookupName(str[ct+1]);
211 if (pfs != NULL) {
212 mEnviroment.mFragmentStore.set(pfs);
Jason Samsda423d82009-06-09 12:15:30 -0700213 continue;
214 }
Jason Samsda423d82009-06-09 12:15:30 -0700215 LOGE("Unreconized value %s passed to stateFragmentStore", str[ct+1]);
216 }
217
218 }
219
Jason Sams22534172009-08-04 16:58:20 -0700220
Jason Samsda423d82009-06-09 12:15:30 -0700221 } else {
222 // Deal with an error.
223 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700224}
225
Joe Onoratod7b37742009-08-09 22:57:44 -0700226
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
Jason Sams43ee06852009-08-12 17:54:11 -0700248void ScriptCState::appendTypes(String8 *str)
249{
250 char buf[256];
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700251 String8 tmp;
Jason Sams43ee06852009-08-12 17:54:11 -0700252
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700253 for (size_t ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
Jason Sams43ee06852009-08-12 17:54:11 -0700254 const Type *t = mConstantBufferTypes[ct].get();
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700255 if (!t) {
Jason Sams43ee06852009-08-12 17:54:11 -0700256 continue;
257 }
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700258 const Element *e = t->getElement();
259
260 if (t->getName()) {
261 for (size_t ct2=0; ct2 < e->getComponentCount(); ct2++) {
262 const Component *c = e->getComponent(ct2);
263 tmp.setTo("#define OFFSETOF_");
264 tmp.append(t->getName());
265 tmp.append("_");
266 tmp.append(c->getComponentName());
267 sprintf(buf, " %i\n", ct2);
268 tmp.append(buf);
269 LOGD(tmp);
270 str->append(tmp);
271 }
Jason Sams43ee06852009-08-12 17:54:11 -0700272 }
273
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700274 if (mSlotNames[ct].length() > 0) {
275 for (size_t ct2=0; ct2 < e->getComponentCount(); ct2++) {
276 const Component *c = e->getComponent(ct2);
277 tmp.setTo("#define ");
278 tmp.append(mSlotNames[ct]);
279 tmp.append("_");
280 tmp.append(c->getComponentName());
281 switch (c->getType()) {
282 case Component::FLOAT:
283 tmp.append(" loadF(");
284 break;
285 case Component::SIGNED:
286 sprintf(buf, " loadI%i(", c->getBits());
287 tmp.append(buf);
288 break;
289 case Component::UNSIGNED:
290 sprintf(buf, " loadU%i(", c->getBits());
291 tmp.append(buf);
292 break;
293 }
294 sprintf(buf, "%i, %i)\n", ct, ct2);
295 tmp.append(buf);
296
297 LOGD(tmp);
298 str->append(tmp);
299 }
300 }
Jason Sams43ee06852009-08-12 17:54:11 -0700301 }
302
303}
304
Joe Onoratod7b37742009-08-09 22:57:44 -0700305
Jason Samsd19f10d2009-05-22 14:03:28 -0700306namespace android {
307namespace renderscript {
308
309void rsi_ScriptCBegin(Context * rsc)
310{
311 ScriptCState *ss = &rsc->mScriptC;
312 ss->clear();
313}
314
Jason Sams3a833d82009-06-08 15:20:31 -0700315void rsi_ScriptCSetScript(Context * rsc, void *vp)
Jason Samsd19f10d2009-05-22 14:03:28 -0700316{
317 ScriptCState *ss = &rsc->mScriptC;
Jason Samsc97bb882009-07-20 14:31:06 -0700318 ss->mProgram.mScript = reinterpret_cast<ScriptC::RunScript_t>(vp);
Jason Samsd19f10d2009-05-22 14:03:28 -0700319}
320
Jason Sams39ddc9502009-06-05 17:35:09 -0700321void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
322{
323 ScriptCState *ss = &rsc->mScriptC;
Jason Sams3a833d82009-06-08 15:20:31 -0700324 ss->mProgram.mScriptText = text;
325 ss->mProgram.mScriptTextLength = len;
Jason Sams39ddc9502009-06-05 17:35:09 -0700326}
327
328
Jason Samsd19f10d2009-05-22 14:03:28 -0700329RsScript rsi_ScriptCCreate(Context * rsc)
330{
331 ScriptCState *ss = &rsc->mScriptC;
332
Jason Sams3eaa3382009-06-10 15:04:38 -0700333 ss->runCompiler(rsc);
Jason Sams39ddc9502009-06-05 17:35:09 -0700334
Jason Samsd19f10d2009-05-22 14:03:28 -0700335 ScriptC *s = new ScriptC();
Jason Sams3a833d82009-06-08 15:20:31 -0700336 s->incRef();
Jack Palevichec5a20b2009-05-28 15:53:04 -0700337 s->mAccScript = ss->mAccScript;
338 ss->mAccScript = NULL;
Jason Sams3a833d82009-06-08 15:20:31 -0700339 s->mEnviroment = ss->mEnviroment;
340 s->mProgram = ss->mProgram;
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700341 for (int ct=0; ct < MAX_SCRIPT_BANKS; ct++) {
342 s->mTypes[ct].set(ss->mConstantBufferTypes[ct].get());
343 s->mSlotNames[ct] = ss->mSlotNames[ct];
344 }
Jason Samsda423d82009-06-09 12:15:30 -0700345
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700346 ss->clear();
Jason Samsd19f10d2009-05-22 14:03:28 -0700347 return s;
348}
349
Joe Onoratod7b37742009-08-09 22:57:44 -0700350void rsi_ScriptCSetDefineF(Context *rsc, const char* name, float value)
351{
352 ScriptCState *ss = &rsc->mScriptC;
353 ss->mFloatDefines.add(String8(name), value);
354}
355
356void rsi_ScriptCSetDefineI32(Context *rsc, const char* name, int32_t value)
357{
358 ScriptCState *ss = &rsc->mScriptC;
359 ss->mInt32Defines.add(String8(name), value);
360}
361
Jason Samsd19f10d2009-05-22 14:03:28 -0700362}
363}
364
365