blob: 3c4bfa2a59355c303c6d8996b9461895741c0ea3 [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
21using namespace android;
22using namespace android::renderscript;
23
24
25ScriptC::ScriptC()
26{
27 mScript = NULL;
28}
29
30ScriptC::~ScriptC()
31{
32}
33
Jack Palevich55d45222009-05-26 18:58:04 -070034extern "C" void matrixLoadIdentity(void *con, rsc_Matrix *mat)
Jason Samsd19f10d2009-05-22 14:03:28 -070035{
36 Matrix *m = reinterpret_cast<Matrix *>(mat);
37 m->loadIdentity();
38}
39
Jack Palevich55d45222009-05-26 18:58:04 -070040extern "C" void matrixLoadFloat(void *con, rsc_Matrix *mat, const float *f)
Jason Samsd19f10d2009-05-22 14:03:28 -070041{
42 Matrix *m = reinterpret_cast<Matrix *>(mat);
43 m->load(f);
44}
45
Jack Palevich55d45222009-05-26 18:58:04 -070046extern "C" void matrixLoadMat(void *con, rsc_Matrix *mat, const rsc_Matrix *newmat)
Jason Samsd19f10d2009-05-22 14:03:28 -070047{
48 Matrix *m = reinterpret_cast<Matrix *>(mat);
49 m->load(reinterpret_cast<const Matrix *>(newmat));
50}
51
Jack Palevich55d45222009-05-26 18:58:04 -070052extern "C" void matrixLoadRotate(void *con, rsc_Matrix *mat, float rot, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -070053{
54 Matrix *m = reinterpret_cast<Matrix *>(mat);
55 m->loadRotate(rot, x, y, z);
56}
57
Jack Palevich55d45222009-05-26 18:58:04 -070058extern "C" void matrixLoadScale(void *con, rsc_Matrix *mat, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -070059{
60 Matrix *m = reinterpret_cast<Matrix *>(mat);
61 m->loadScale(x, y, z);
62}
63
Jack Palevich55d45222009-05-26 18:58:04 -070064extern "C" void matrixLoadTranslate(void *con, rsc_Matrix *mat, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -070065{
66 Matrix *m = reinterpret_cast<Matrix *>(mat);
67 m->loadTranslate(x, y, z);
68}
69
Jack Palevich55d45222009-05-26 18:58:04 -070070extern "C" void matrixLoadMultiply(void *con, rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
Jason Samsd19f10d2009-05-22 14:03:28 -070071{
72 Matrix *m = reinterpret_cast<Matrix *>(mat);
73 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
74 reinterpret_cast<const Matrix *>(rhs));
75}
76
Jack Palevich55d45222009-05-26 18:58:04 -070077extern "C" void matrixMultiply(void *con, rsc_Matrix *mat, const rsc_Matrix *rhs)
Jason Samsd19f10d2009-05-22 14:03:28 -070078{
79 Matrix *m = reinterpret_cast<Matrix *>(mat);
80 m->multiply(reinterpret_cast<const Matrix *>(rhs));
81}
82
Jack Palevich55d45222009-05-26 18:58:04 -070083extern "C" void matrixRotate(void *con, rsc_Matrix *mat, float rot, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -070084{
85 Matrix *m = reinterpret_cast<Matrix *>(mat);
86 m->rotate(rot, x, y, z);
87}
88
Jack Palevich55d45222009-05-26 18:58:04 -070089extern "C" void matrixScale(void *con, rsc_Matrix *mat, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -070090{
91 Matrix *m = reinterpret_cast<Matrix *>(mat);
92 m->scale(x, y, z);
93}
94
Jack Palevich55d45222009-05-26 18:58:04 -070095extern "C" void matrixTranslate(void *con, rsc_Matrix *mat, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -070096{
97 Matrix *m = reinterpret_cast<Matrix *>(mat);
98 m->translate(x, y, z);
99}
100
101
Jack Palevich55d45222009-05-26 18:58:04 -0700102extern "C" const void * loadVp(void *vp, uint32_t bank, uint32_t offset)
Jason Samsd19f10d2009-05-22 14:03:28 -0700103{
104 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
105 return &static_cast<const uint8_t *>(env->mScript->mSlots[bank]->getPtr())[offset];
106}
107
Jack Palevich55d45222009-05-26 18:58:04 -0700108extern "C" float loadF(void *vp, uint32_t bank, uint32_t offset)
Jason Samsd19f10d2009-05-22 14:03:28 -0700109{
110 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
111 //LOGE("bank %i, offset %i", bank, offset);
112 //LOGE("%p", env->mScript->mSlots[bank]->getPtr());
113 return static_cast<const float *>(env->mScript->mSlots[bank]->getPtr())[offset];
114}
115
Jack Palevich55d45222009-05-26 18:58:04 -0700116extern "C" int32_t loadI32(void *vp, uint32_t bank, uint32_t offset)
Jason Samsd19f10d2009-05-22 14:03:28 -0700117{
118 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
119 return static_cast<const int32_t *>(env->mScript->mSlots[bank]->getPtr())[offset];
120}
121
Jack Palevich55d45222009-05-26 18:58:04 -0700122extern "C" uint32_t loadU32(void *vp, uint32_t bank, uint32_t offset)
Jason Samsd19f10d2009-05-22 14:03:28 -0700123{
124 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
125 return static_cast<const uint32_t *>(env->mScript->mSlots[bank]->getPtr())[offset];
126}
127
Jack Palevich55d45222009-05-26 18:58:04 -0700128extern "C" void loadEnvVec4(void *vp, uint32_t bank, uint32_t offset, rsc_Vector4 *v)
Jason Samsd19f10d2009-05-22 14:03:28 -0700129{
130 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
131 memcpy(v, &static_cast<const float *>(env->mScript->mSlots[bank]->getPtr())[offset], sizeof(rsc_Vector4));
132}
133
Jack Palevich55d45222009-05-26 18:58:04 -0700134extern "C" void loadEnvMatrix(void *vp, uint32_t bank, uint32_t offset, rsc_Matrix *m)
Jason Samsd19f10d2009-05-22 14:03:28 -0700135{
136 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
137 memcpy(m, &static_cast<const float *>(env->mScript->mSlots[bank]->getPtr())[offset], sizeof(rsc_Matrix));
138}
139
140
Jack Palevich55d45222009-05-26 18:58:04 -0700141extern "C" void storeF(void *vp, uint32_t bank, uint32_t offset, float v)
Jason Samsd19f10d2009-05-22 14:03:28 -0700142{
143 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
144 static_cast<float *>(env->mScript->mSlots[bank]->getPtr())[offset] = v;
145}
146
Jack Palevich55d45222009-05-26 18:58:04 -0700147extern "C" void storeI32(void *vp, uint32_t bank, uint32_t offset, int32_t v)
Jason Samsd19f10d2009-05-22 14:03:28 -0700148{
149 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
150 static_cast<int32_t *>(env->mScript->mSlots[bank]->getPtr())[offset] = v;
151}
152
Jack Palevich55d45222009-05-26 18:58:04 -0700153extern "C" void storeU32(void *vp, uint32_t bank, uint32_t offset, uint32_t v)
Jason Samsd19f10d2009-05-22 14:03:28 -0700154{
155 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
156 static_cast<uint32_t *>(env->mScript->mSlots[bank]->getPtr())[offset] = v;
157}
158
Jack Palevich55d45222009-05-26 18:58:04 -0700159extern "C" void storeEnvVec4(void *vp, uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
Jason Samsd19f10d2009-05-22 14:03:28 -0700160{
161 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
162 memcpy(&static_cast<float *>(env->mScript->mSlots[bank]->getPtr())[offset], v, sizeof(rsc_Vector4));
163}
164
Jack Palevich55d45222009-05-26 18:58:04 -0700165extern "C" void storeEnvMatrix(void *vp, uint32_t bank, uint32_t offset, const rsc_Matrix *m)
Jason Samsd19f10d2009-05-22 14:03:28 -0700166{
167 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
168 memcpy(&static_cast<float *>(env->mScript->mSlots[bank]->getPtr())[offset], m, sizeof(rsc_Matrix));
169}
170
171
Jack Palevich55d45222009-05-26 18:58:04 -0700172extern "C" void color(void *vp, float r, float g, float b, float a)
Jason Samsd19f10d2009-05-22 14:03:28 -0700173{
174 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
175 glColor4f(r, g, b, a);
176}
177
Jack Palevich55d45222009-05-26 18:58:04 -0700178extern "C" void renderTriangleMesh(void *vp, RsTriangleMesh mesh)
Jason Samsd19f10d2009-05-22 14:03:28 -0700179{
180 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
181 rsi_TriangleMeshRender(env->mContext, mesh);
182}
183
Jack Palevich55d45222009-05-26 18:58:04 -0700184extern "C" void renderTriangleMeshRange(void *vp, RsTriangleMesh mesh, uint32_t start, uint32_t count)
Jason Samsd19f10d2009-05-22 14:03:28 -0700185{
186 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
187 rsi_TriangleMeshRenderRange(env->mContext, mesh, start, count);
188}
189
Jack Palevich55d45222009-05-26 18:58:04 -0700190extern "C" void materialDiffuse(void *vp, float r, float g, float b, float a)
Jason Samsd19f10d2009-05-22 14:03:28 -0700191{
192 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
193 float v[] = {r, g, b, a};
194 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, v);
195}
196
Jack Palevich55d45222009-05-26 18:58:04 -0700197extern "C" void materialSpecular(void *vp, float r, float g, float b, float a)
Jason Samsd19f10d2009-05-22 14:03:28 -0700198{
199 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
200 float v[] = {r, g, b, a};
201 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, v);
202}
203
Jack Palevich55d45222009-05-26 18:58:04 -0700204extern "C" void lightPosition(void *vp, float x, float y, float z, float w)
Jason Samsd19f10d2009-05-22 14:03:28 -0700205{
206 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
207 float v[] = {x, y, z, w};
208 glLightfv(GL_LIGHT0, GL_POSITION, v);
209}
210
Jack Palevich55d45222009-05-26 18:58:04 -0700211extern "C" void materialShininess(void *vp, float s)
Jason Samsd19f10d2009-05-22 14:03:28 -0700212{
213 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
214 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &s);
215}
216
Jack Palevich55d45222009-05-26 18:58:04 -0700217extern "C" void uploadToTexture(void *vp, RsAllocation va, uint32_t baseMipLevel)
Jason Samsd19f10d2009-05-22 14:03:28 -0700218{
219 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
220 rsi_AllocationUploadToTexture(env->mContext, va, baseMipLevel);
221}
222
Jack Palevich55d45222009-05-26 18:58:04 -0700223extern "C" void enable(void *vp, uint32_t p)
Jason Samsd19f10d2009-05-22 14:03:28 -0700224{
225 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
226 glEnable(p);
227}
228
Jack Palevich55d45222009-05-26 18:58:04 -0700229extern "C" void disable(void *vp, uint32_t p)
Jason Samsd19f10d2009-05-22 14:03:28 -0700230{
231 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
232 glDisable(p);
233}
234
Jack Palevich55d45222009-05-26 18:58:04 -0700235extern "C" uint32_t scriptRand(void *vp, uint32_t max)
Jason Samsd19f10d2009-05-22 14:03:28 -0700236{
237 return (uint32_t)(((float)rand()) * max / RAND_MAX);
238}
239
240// Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a
Jack Palevich55d45222009-05-26 18:58:04 -0700241extern "C" void drawTriangleArray(void *vp, RsAllocation alloc, uint32_t count)
Jason Samsd19f10d2009-05-22 14:03:28 -0700242{
243 const Allocation *a = (const Allocation *)alloc;
244 const uint32_t *ptr = (const uint32_t *)a->getPtr();
245
246 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
247 env->mContext->setupCheck();
248
249 glBindBuffer(GL_ARRAY_BUFFER, 0);
250 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
251
252 glEnableClientState(GL_VERTEX_ARRAY);
253 glDisableClientState(GL_NORMAL_ARRAY);
254 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
255 glEnableClientState(GL_COLOR_ARRAY);
256
257 glVertexPointer(2, GL_FIXED, 12, ptr + 1);
258 //glTexCoordPointer(2, GL_FIXED, 24, ptr + 1);
259 glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
260
261 glDrawArrays(GL_TRIANGLES, 0, count * 3);
262}
263
Jack Palevich55d45222009-05-26 18:58:04 -0700264extern "C" void pfBindTexture(void *vp, RsProgramFragment vpf, uint32_t slot, RsAllocation va)
Jason Samsd19f10d2009-05-22 14:03:28 -0700265{
266 //LOGE("pfBindTexture %p", vpf);
267 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
Jack Palevich55d45222009-05-26 18:58:04 -0700268 rsi_ProgramFragmentBindTexture(env->mContext,
Jason Samsd19f10d2009-05-22 14:03:28 -0700269 static_cast<ProgramFragment *>(vpf),
270 slot,
271 static_cast<Allocation *>(va));
272
273}
274
Jack Palevich55d45222009-05-26 18:58:04 -0700275extern "C" void pfBindSampler(void *vp, RsProgramFragment vpf, uint32_t slot, RsSampler vs)
Jason Samsd19f10d2009-05-22 14:03:28 -0700276{
277 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
Jack Palevich55d45222009-05-26 18:58:04 -0700278 rsi_ProgramFragmentBindSampler(env->mContext,
Jason Samsd19f10d2009-05-22 14:03:28 -0700279 static_cast<ProgramFragment *>(vpf),
280 slot,
281 static_cast<Sampler *>(vs));
282
283}
284
Jack Palevich55d45222009-05-26 18:58:04 -0700285extern "C" void contextBindProgramFragmentStore(void *vp, RsProgramFragmentStore pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -0700286{
287 //LOGE("contextBindProgramFragmentStore %p", pfs);
288 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
289 rsi_ContextBindProgramFragmentStore(env->mContext, pfs);
290
291}
292
Jack Palevich55d45222009-05-26 18:58:04 -0700293extern "C" void contextBindProgramFragment(void *vp, RsProgramFragment pf)
Jason Samsd19f10d2009-05-22 14:03:28 -0700294{
295 //LOGE("contextBindProgramFragment %p", pf);
296 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp);
297 rsi_ContextBindProgramFragment(env->mContext, pf);
298
299}
300
301
302static rsc_FunctionTable scriptCPtrTable = {
303 loadVp,
304 loadF,
305 loadI32,
306 loadU32,
307 loadEnvVec4,
308 loadEnvMatrix,
309
310 storeF,
311 storeI32,
312 storeU32,
313 storeEnvVec4,
314 storeEnvMatrix,
315
316 matrixLoadIdentity,
317 matrixLoadFloat,
318 matrixLoadMat,
319 matrixLoadRotate,
320 matrixLoadScale,
321 matrixLoadTranslate,
322 matrixLoadMultiply,
323 matrixMultiply,
324 matrixRotate,
325 matrixScale,
326 matrixTranslate,
327
328 color,
329 renderTriangleMesh,
330 renderTriangleMeshRange,
331
332 pfBindTexture,
333 pfBindSampler,
334
335 materialDiffuse,
336 materialSpecular,
337 lightPosition,
338 materialShininess,
339 uploadToTexture,
340 enable,
341 disable,
342
343 scriptRand,
344 drawTriangleArray,
345 contextBindProgramFragment,
346 contextBindProgramFragmentStore
347};
348
349
350void ScriptC::run(Context *rsc, uint32_t launchID)
351{
352 Env e = {rsc, this};
353 mScript(&e, &scriptCPtrTable, launchID);
354}
355
356ScriptCState::ScriptCState()
357{
358 clear();
359}
360
361ScriptCState::~ScriptCState()
362{
363}
364
365void ScriptCState::clear()
366{
367 mConstantBufferTypes.clear();
368 mClearColor[0] = 0;
369 mClearColor[1] = 0;
370 mClearColor[2] = 0;
371 mClearColor[3] = 1;
372 mClearDepth = 1;
373 mClearStencil = 0;
374 mScript = NULL;
375 mIsRoot = false;
376 mIsOrtho = true;
377}
378
379namespace android {
380namespace renderscript {
381
382void rsi_ScriptCBegin(Context * rsc)
383{
384 ScriptCState *ss = &rsc->mScriptC;
385 ss->clear();
386}
387
388void rsi_ScriptCSetClearColor(Context * rsc, float r, float g, float b, float a)
389{
390 ScriptCState *ss = &rsc->mScriptC;
391 ss->mClearColor[0] = r;
392 ss->mClearColor[1] = g;
393 ss->mClearColor[2] = b;
394 ss->mClearColor[3] = a;
395}
396
397void rsi_ScriptCSetClearDepth(Context * rsc, float v)
398{
399 ScriptCState *ss = &rsc->mScriptC;
400 ss->mClearDepth = v;
401}
402
403void rsi_ScriptCSetClearStencil(Context * rsc, uint32_t v)
404{
405 ScriptCState *ss = &rsc->mScriptC;
406 ss->mClearStencil = v;
407}
408
409void rsi_ScriptCAddType(Context * rsc, RsType vt)
410{
411 ScriptCState *ss = &rsc->mScriptC;
412 ss->mConstantBufferTypes.add(static_cast<const Type *>(vt));
413}
414
415void rsi_ScriptCSetScript(Context * rsc, void *vp)
416{
417 ScriptCState *ss = &rsc->mScriptC;
418 ss->mScript = reinterpret_cast<rsc_RunScript>(vp);
419}
420
421void rsi_ScriptCSetRoot(Context * rsc, bool isRoot)
422{
423 ScriptCState *ss = &rsc->mScriptC;
424 ss->mIsRoot = isRoot;
425}
426
427void rsi_ScriptCSetOrtho(Context * rsc, bool isOrtho)
428{
429 ScriptCState *ss = &rsc->mScriptC;
430 ss->mIsOrtho = isOrtho;
431}
432
433RsScript rsi_ScriptCCreate(Context * rsc)
434{
435 ScriptCState *ss = &rsc->mScriptC;
436
437 ScriptC *s = new ScriptC();
438 s->mScript = ss->mScript;
439 s->mClearColor[0] = ss->mClearColor[0];
440 s->mClearColor[1] = ss->mClearColor[1];
441 s->mClearColor[2] = ss->mClearColor[2];
442 s->mClearColor[3] = ss->mClearColor[3];
443 s->mClearDepth = ss->mClearDepth;
444 s->mClearStencil = ss->mClearStencil;
445 s->mIsRoot = ss->mIsRoot;
446 s->mIsOrtho = ss->mIsOrtho;
447
448 return s;
449}
450
451}
452}
453
454