blob: 3343db51af93e8746d10b6e2c525de1a4328bdb4 [file] [log] [blame]
Jason Sams326e0dd2009-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 Palevich1ef8b802009-05-28 15:53:04 -070021#include "acc/acc.h"
Jason Samsa4a54e42009-06-10 18:39:40 -070022#include "utils/String8.h"
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070023#include "utils/Timers.h"
Jack Palevich1ef8b802009-05-28 15:53:04 -070024
Jason Sams1aa5a4e2009-06-22 17:15:15 -070025#include <GLES/gl.h>
26#include <GLES/glext.h>
27
Jason Sams326e0dd2009-05-22 14:03:28 -070028using namespace android;
29using namespace android::renderscript;
30
Jason Samse5769102009-06-19 16:03:18 -070031#define GET_TLS() Context::ScriptTLSStruct * tls = \
32 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
33 Context * rsc = tls->mContext; \
34 ScriptC * sc = (ScriptC *) tls->mScript
35
Jason Sams326e0dd2009-05-22 14:03:28 -070036
37ScriptC::ScriptC()
38{
Jack Palevich1ef8b802009-05-28 15:53:04 -070039 mAccScript = NULL;
Jason Samsefb8de12009-06-08 15:20:31 -070040 memset(&mProgram, 0, sizeof(mProgram));
Jason Sams326e0dd2009-05-22 14:03:28 -070041}
42
43ScriptC::~ScriptC()
44{
Jack Palevich1ef8b802009-05-28 15:53:04 -070045 if (mAccScript) {
46 accDeleteScript(mAccScript);
47 }
Jason Sams326e0dd2009-05-22 14:03:28 -070048}
49
Jason Sams326e0dd2009-05-22 14:03:28 -070050
Jason Samse5769102009-06-19 16:03:18 -070051bool ScriptC::run(Context *rsc, uint32_t launchIndex)
Jason Sams326e0dd2009-05-22 14:03:28 -070052{
Jason Samsd34b7252009-08-04 16:58:20 -070053 Context::ScriptTLSStruct * tls =
Jason Samse5769102009-06-19 16:03:18 -070054 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey);
Jason Samsa0a1b6f2009-06-10 15:04:38 -070055
56 if (mEnviroment.mFragmentStore.get()) {
57 rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
58 }
59 if (mEnviroment.mFragment.get()) {
60 rsc->setFragment(mEnviroment.mFragment.get());
61 }
Jason Sams8ce125b2009-06-17 16:52:59 -070062 if (mEnviroment.mVertex.get()) {
63 rsc->setVertex(mEnviroment.mVertex.get());
64 }
Jason Samsa0a1b6f2009-06-10 15:04:38 -070065
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070066 if (launchIndex == 0) {
67 mEnviroment.mStartTimeMillis
68 = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
69 }
70
Jason Samse45ac6e2009-07-20 14:31:06 -070071 bool ret = false;
Jason Samse5769102009-06-19 16:03:18 -070072 tls->mScript = this;
Jason Samse45ac6e2009-07-20 14:31:06 -070073 ret = mProgram.mScript(launchIndex) != 0;
Jason Samse5769102009-06-19 16:03:18 -070074 tls->mScript = NULL;
Jason Samse45ac6e2009-07-20 14:31:06 -070075 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -070076}
77
78ScriptCState::ScriptCState()
79{
80 clear();
81}
82
83ScriptCState::~ScriptCState()
84{
Jack Palevich1ef8b802009-05-28 15:53:04 -070085 if (mAccScript) {
86 accDeleteScript(mAccScript);
87 }
Jason Sams326e0dd2009-05-22 14:03:28 -070088}
89
90void ScriptCState::clear()
91{
Jason Samsefb8de12009-06-08 15:20:31 -070092 memset(&mProgram, 0, sizeof(mProgram));
93
Jason Sams326e0dd2009-05-22 14:03:28 -070094 mConstantBufferTypes.clear();
Jason Samsefb8de12009-06-08 15:20:31 -070095
96 memset(&mEnviroment, 0, sizeof(mEnviroment));
97 mEnviroment.mClearColor[0] = 0;
98 mEnviroment.mClearColor[1] = 0;
99 mEnviroment.mClearColor[2] = 0;
100 mEnviroment.mClearColor[3] = 1;
101 mEnviroment.mClearDepth = 1;
102 mEnviroment.mClearStencil = 0;
103 mEnviroment.mIsRoot = false;
Jason Samsefb8de12009-06-08 15:20:31 -0700104
Jack Palevich1ef8b802009-05-28 15:53:04 -0700105 mAccScript = NULL;
Jason Samsefb8de12009-06-08 15:20:31 -0700106
Joe Onorato57b79ce2009-08-09 22:57:44 -0700107 mInt32Defines.clear();
108 mFloatDefines.clear();
Jason Sams1f526332009-06-05 17:35:09 -0700109}
110
Jason Samsd34b7252009-08-04 16:58:20 -0700111static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name)
Jason Sams29df66f2009-07-16 15:08:06 -0700112{
113 const ScriptCState::SymbolTable_t *sym = ScriptCState::lookupSymbol(name);
Jason Sams29df66f2009-07-16 15:08:06 -0700114 if (sym) {
115 return sym->mPtr;
116 }
Jason Sams29df66f2009-07-16 15:08:06 -0700117 LOGE("ScriptC sym lookup failed for %s", name);
Jason Sams29df66f2009-07-16 15:08:06 -0700118 return NULL;
119}
Jason Samsa4a54e42009-06-10 18:39:40 -0700120
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700121void ScriptCState::runCompiler(Context *rsc)
Jason Sams1f526332009-06-05 17:35:09 -0700122{
123 mAccScript = accCreateScript();
Jason Samsa4a54e42009-06-10 18:39:40 -0700124 String8 tmp;
Jason Sams1f526332009-06-05 17:35:09 -0700125
Jason Samsa4a54e42009-06-10 18:39:40 -0700126 rsc->appendNameDefines(&tmp);
Jason Samsf1685042009-07-16 17:47:40 -0700127 appendDecls(&tmp);
Joe Onorato57b79ce2009-08-09 22:57:44 -0700128 rsc->appendVarDefines(&tmp);
129 appendVarDefines(&tmp);
Jason Samsb5909ce2009-07-21 12:20:54 -0700130 tmp.append("#line 1\n");
Jason Samsa4a54e42009-06-10 18:39:40 -0700131
132 const char* scriptSource[] = {tmp.string(), mProgram.mScriptText};
133 int scriptLength[] = {tmp.length(), mProgram.mScriptTextLength} ;
134 accScriptSource(mAccScript, sizeof(scriptLength) / sizeof(int), scriptSource, scriptLength);
Jason Sams29df66f2009-07-16 15:08:06 -0700135 accRegisterSymbolCallback(mAccScript, symbolLookup, NULL);
Jason Sams1f526332009-06-05 17:35:09 -0700136 accCompileScript(mAccScript);
Jason Samsefb8de12009-06-08 15:20:31 -0700137 accGetScriptLabel(mAccScript, "main", (ACCvoid**) &mProgram.mScript);
Jason Sams10308932009-06-09 12:15:30 -0700138 rsAssert(mProgram.mScript);
139
Jason Samsf1685042009-07-16 17:47:40 -0700140 if (!mProgram.mScript) {
141 ACCchar buf[4096];
142 ACCsizei len;
143 accGetScriptInfoLog(mAccScript, sizeof(buf), &len, buf);
144 LOGE(buf);
Jason Samsf1685042009-07-16 17:47:40 -0700145 }
146
Jason Sams8ce125b2009-06-17 16:52:59 -0700147 mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
148 mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
149 mEnviroment.mFragmentStore.set(rsc->getDefaultProgramFragmentStore());
Jason Samsa4a54e42009-06-10 18:39:40 -0700150
Jason Sams10308932009-06-09 12:15:30 -0700151 if (mProgram.mScript) {
152 const static int pragmaMax = 16;
153 ACCsizei pragmaCount;
154 ACCchar * str[pragmaMax];
155 accGetPragmas(mAccScript, &pragmaCount, pragmaMax, &str[0]);
156
Jason Sams10308932009-06-09 12:15:30 -0700157 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Sams10308932009-06-09 12:15:30 -0700158 if (!strcmp(str[ct], "version")) {
159 continue;
Jason Sams10308932009-06-09 12:15:30 -0700160 }
161
Jason Sams10308932009-06-09 12:15:30 -0700162 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700163 if (!strcmp(str[ct+1], "default")) {
164 continue;
165 }
166 if (!strcmp(str[ct+1], "parent")) {
167 mEnviroment.mVertex.clear();
168 continue;
169 }
170 ProgramVertex * pv = (ProgramVertex *)rsc->lookupName(str[ct+1]);
171 if (pv != NULL) {
172 mEnviroment.mVertex.set(pv);
173 continue;
174 }
Jason Sams10308932009-06-09 12:15:30 -0700175 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
176 }
177
178 if (!strcmp(str[ct], "stateRaster")) {
Jason Sams10308932009-06-09 12:15:30 -0700179 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
180 }
181
182 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700183 if (!strcmp(str[ct+1], "default")) {
184 continue;
185 }
186 if (!strcmp(str[ct+1], "parent")) {
187 mEnviroment.mFragment.clear();
188 continue;
189 }
190 ProgramFragment * pf = (ProgramFragment *)rsc->lookupName(str[ct+1]);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700191 if (pf != NULL) {
192 mEnviroment.mFragment.set(pf);
Jason Sams10308932009-06-09 12:15:30 -0700193 continue;
194 }
195 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
196 }
197
198 if (!strcmp(str[ct], "stateFragmentStore")) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700199 if (!strcmp(str[ct+1], "default")) {
200 continue;
201 }
202 if (!strcmp(str[ct+1], "parent")) {
203 mEnviroment.mFragmentStore.clear();
204 continue;
205 }
Jason Samsd34b7252009-08-04 16:58:20 -0700206 ProgramFragmentStore * pfs =
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700207 (ProgramFragmentStore *)rsc->lookupName(str[ct+1]);
208 if (pfs != NULL) {
209 mEnviroment.mFragmentStore.set(pfs);
Jason Sams10308932009-06-09 12:15:30 -0700210 continue;
211 }
Jason Sams10308932009-06-09 12:15:30 -0700212 LOGE("Unreconized value %s passed to stateFragmentStore", str[ct+1]);
213 }
214
215 }
216
Jason Samsd34b7252009-08-04 16:58:20 -0700217
Jason Sams10308932009-06-09 12:15:30 -0700218 } else {
219 // Deal with an error.
220 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700221}
222
Joe Onorato57b79ce2009-08-09 22:57:44 -0700223
224void ScriptCState::appendVarDefines(String8 *str)
225{
226 char buf[256];
227 LOGD("appendVarDefines mInt32Defines.size()=%d mFloatDefines.size()=%d\n",
228 mInt32Defines.size(), mFloatDefines.size());
229 for (size_t ct=0; ct < mInt32Defines.size(); ct++) {
230 str->append("#define ");
231 str->append(mInt32Defines.keyAt(ct));
232 str->append(" ");
233 sprintf(buf, "%i\n", (int)mInt32Defines.valueAt(ct));
234 str->append(buf);
235 }
236 for (size_t ct=0; ct < mFloatDefines.size(); ct++) {
237 str->append("#define ");
238 str->append(mFloatDefines.keyAt(ct));
239 str->append(" ");
240 sprintf(buf, "%ff\n", mFloatDefines.valueAt(ct));
241 str->append(buf);
242 }
243}
244
245
Jason Sams326e0dd2009-05-22 14:03:28 -0700246namespace android {
247namespace renderscript {
248
249void rsi_ScriptCBegin(Context * rsc)
250{
251 ScriptCState *ss = &rsc->mScriptC;
252 ss->clear();
253}
254
Jason Sams326e0dd2009-05-22 14:03:28 -0700255void rsi_ScriptCAddType(Context * rsc, RsType vt)
256{
257 ScriptCState *ss = &rsc->mScriptC;
258 ss->mConstantBufferTypes.add(static_cast<const Type *>(vt));
259}
260
Jason Samsefb8de12009-06-08 15:20:31 -0700261void rsi_ScriptCSetScript(Context * rsc, void *vp)
Jason Sams326e0dd2009-05-22 14:03:28 -0700262{
263 ScriptCState *ss = &rsc->mScriptC;
Jason Samse45ac6e2009-07-20 14:31:06 -0700264 ss->mProgram.mScript = reinterpret_cast<ScriptC::RunScript_t>(vp);
Jason Sams326e0dd2009-05-22 14:03:28 -0700265}
266
267void rsi_ScriptCSetRoot(Context * rsc, bool isRoot)
268{
269 ScriptCState *ss = &rsc->mScriptC;
Jason Samsefb8de12009-06-08 15:20:31 -0700270 ss->mEnviroment.mIsRoot = isRoot;
Jason Sams326e0dd2009-05-22 14:03:28 -0700271}
272
Jason Sams1f526332009-06-05 17:35:09 -0700273void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
274{
275 ScriptCState *ss = &rsc->mScriptC;
Jason Samsefb8de12009-06-08 15:20:31 -0700276 ss->mProgram.mScriptText = text;
277 ss->mProgram.mScriptTextLength = len;
Jason Sams1f526332009-06-05 17:35:09 -0700278}
279
280
Jason Sams326e0dd2009-05-22 14:03:28 -0700281RsScript rsi_ScriptCCreate(Context * rsc)
282{
283 ScriptCState *ss = &rsc->mScriptC;
284
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700285 ss->runCompiler(rsc);
Jason Sams1f526332009-06-05 17:35:09 -0700286
Jason Sams326e0dd2009-05-22 14:03:28 -0700287 ScriptC *s = new ScriptC();
Jason Samsefb8de12009-06-08 15:20:31 -0700288 s->incRef();
Jack Palevich1ef8b802009-05-28 15:53:04 -0700289 s->mAccScript = ss->mAccScript;
290 ss->mAccScript = NULL;
Jason Samsefb8de12009-06-08 15:20:31 -0700291 s->mEnviroment = ss->mEnviroment;
292 s->mProgram = ss->mProgram;
293 ss->clear();
Jason Sams10308932009-06-09 12:15:30 -0700294
Jason Sams326e0dd2009-05-22 14:03:28 -0700295 return s;
296}
297
Joe Onorato57b79ce2009-08-09 22:57:44 -0700298void rsi_ScriptCSetDefineF(Context *rsc, const char* name, float value)
299{
300 ScriptCState *ss = &rsc->mScriptC;
301 ss->mFloatDefines.add(String8(name), value);
302}
303
304void rsi_ScriptCSetDefineI32(Context *rsc, const char* name, int32_t value)
305{
306 ScriptCState *ss = &rsc->mScriptC;
307 ss->mInt32Defines.add(String8(name), value);
308}
309
Jason Sams326e0dd2009-05-22 14:03:28 -0700310}
311}
312
313