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