rsScriptC.cpp revision 6678e9b2568ad041429a2477177133fe4932159f
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 21using namespace android; 22using namespace android::renderscript; 23 24 25ScriptC::ScriptC() 26{ 27 mScript = NULL; 28} 29 30ScriptC::~ScriptC() 31{ 32} 33 34extern "C" void matrixLoadIdentity(void *con, rsc_Matrix *mat) 35{ 36 Matrix *m = reinterpret_cast<Matrix *>(mat); 37 m->loadIdentity(); 38} 39 40extern "C" void matrixLoadFloat(void *con, rsc_Matrix *mat, const float *f) 41{ 42 Matrix *m = reinterpret_cast<Matrix *>(mat); 43 m->load(f); 44} 45 46extern "C" void matrixLoadMat(void *con, rsc_Matrix *mat, const rsc_Matrix *newmat) 47{ 48 Matrix *m = reinterpret_cast<Matrix *>(mat); 49 m->load(reinterpret_cast<const Matrix *>(newmat)); 50} 51 52extern "C" void matrixLoadRotate(void *con, rsc_Matrix *mat, float rot, float x, float y, float z) 53{ 54 Matrix *m = reinterpret_cast<Matrix *>(mat); 55 m->loadRotate(rot, x, y, z); 56} 57 58extern "C" void matrixLoadScale(void *con, rsc_Matrix *mat, float x, float y, float z) 59{ 60 Matrix *m = reinterpret_cast<Matrix *>(mat); 61 m->loadScale(x, y, z); 62} 63 64extern "C" void matrixLoadTranslate(void *con, rsc_Matrix *mat, float x, float y, float z) 65{ 66 Matrix *m = reinterpret_cast<Matrix *>(mat); 67 m->loadTranslate(x, y, z); 68} 69 70extern "C" void matrixLoadMultiply(void *con, rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs) 71{ 72 Matrix *m = reinterpret_cast<Matrix *>(mat); 73 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs), 74 reinterpret_cast<const Matrix *>(rhs)); 75} 76 77extern "C" void matrixMultiply(void *con, rsc_Matrix *mat, const rsc_Matrix *rhs) 78{ 79 Matrix *m = reinterpret_cast<Matrix *>(mat); 80 m->multiply(reinterpret_cast<const Matrix *>(rhs)); 81} 82 83extern "C" void matrixRotate(void *con, rsc_Matrix *mat, float rot, float x, float y, float z) 84{ 85 Matrix *m = reinterpret_cast<Matrix *>(mat); 86 m->rotate(rot, x, y, z); 87} 88 89extern "C" void matrixScale(void *con, rsc_Matrix *mat, float x, float y, float z) 90{ 91 Matrix *m = reinterpret_cast<Matrix *>(mat); 92 m->scale(x, y, z); 93} 94 95extern "C" void matrixTranslate(void *con, rsc_Matrix *mat, float x, float y, float z) 96{ 97 Matrix *m = reinterpret_cast<Matrix *>(mat); 98 m->translate(x, y, z); 99} 100 101 102extern "C" const void * loadVp(void *vp, uint32_t bank, uint32_t offset) 103{ 104 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 105 return &static_cast<const uint8_t *>(env->mScript->mSlots[bank]->getPtr())[offset]; 106} 107 108extern "C" float loadF(void *vp, uint32_t bank, uint32_t offset) 109{ 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 116extern "C" int32_t loadI32(void *vp, uint32_t bank, uint32_t offset) 117{ 118 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 119 return static_cast<const int32_t *>(env->mScript->mSlots[bank]->getPtr())[offset]; 120} 121 122extern "C" uint32_t loadU32(void *vp, uint32_t bank, uint32_t offset) 123{ 124 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 125 return static_cast<const uint32_t *>(env->mScript->mSlots[bank]->getPtr())[offset]; 126} 127 128extern "C" void loadEnvVec4(void *vp, uint32_t bank, uint32_t offset, rsc_Vector4 *v) 129{ 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 134extern "C" void loadEnvMatrix(void *vp, uint32_t bank, uint32_t offset, rsc_Matrix *m) 135{ 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 141extern "C" void storeF(void *vp, uint32_t bank, uint32_t offset, float v) 142{ 143 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 144 static_cast<float *>(env->mScript->mSlots[bank]->getPtr())[offset] = v; 145} 146 147extern "C" void storeI32(void *vp, uint32_t bank, uint32_t offset, int32_t v) 148{ 149 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 150 static_cast<int32_t *>(env->mScript->mSlots[bank]->getPtr())[offset] = v; 151} 152 153extern "C" void storeU32(void *vp, uint32_t bank, uint32_t offset, uint32_t v) 154{ 155 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 156 static_cast<uint32_t *>(env->mScript->mSlots[bank]->getPtr())[offset] = v; 157} 158 159extern "C" void storeEnvVec4(void *vp, uint32_t bank, uint32_t offset, const rsc_Vector4 *v) 160{ 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 165extern "C" void storeEnvMatrix(void *vp, uint32_t bank, uint32_t offset, const rsc_Matrix *m) 166{ 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 172extern "C" void color(void *vp, float r, float g, float b, float a) 173{ 174 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 175 glColor4f(r, g, b, a); 176} 177 178extern "C" void renderTriangleMesh(void *vp, RsTriangleMesh mesh) 179{ 180 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 181 rsi_TriangleMeshRender(env->mContext, mesh); 182} 183 184extern "C" void renderTriangleMeshRange(void *vp, RsTriangleMesh mesh, uint32_t start, uint32_t count) 185{ 186 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 187 rsi_TriangleMeshRenderRange(env->mContext, mesh, start, count); 188} 189 190extern "C" void materialDiffuse(void *vp, float r, float g, float b, float a) 191{ 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 197extern "C" void materialSpecular(void *vp, float r, float g, float b, float a) 198{ 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 204extern "C" void lightPosition(void *vp, float x, float y, float z, float w) 205{ 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 211extern "C" void materialShininess(void *vp, float s) 212{ 213 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 214 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &s); 215} 216 217extern "C" void uploadToTexture(void *vp, RsAllocation va, uint32_t baseMipLevel) 218{ 219 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 220 rsi_AllocationUploadToTexture(env->mContext, va, baseMipLevel); 221} 222 223extern "C" void enable(void *vp, uint32_t p) 224{ 225 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 226 glEnable(p); 227} 228 229extern "C" void disable(void *vp, uint32_t p) 230{ 231 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 232 glDisable(p); 233} 234 235extern "C" uint32_t scriptRand(void *vp, uint32_t max) 236{ 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 241extern "C" void drawTriangleArray(void *vp, RsAllocation alloc, uint32_t count) 242{ 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 264extern "C" void drawRect(void *vp, int32_t x1, int32_t x2, int32_t y1, int32_t y2) 265{ 266 x1 = (x1 << 16); 267 x2 = (x2 << 16); 268 y1 = (y1 << 16); 269 y2 = (y2 << 16); 270 271 int32_t vtx[] = {x1,y1, x1,y2, x2,y1, x2,y2}; 272 static const int32_t tex[] = {0,0, 0,0x10000, 0x10000,0, 0x10000,0x10000}; 273 274 275 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 276 env->mContext->setupCheck(); 277 278 glBindBuffer(GL_ARRAY_BUFFER, 0); 279 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]); 280 281 glEnableClientState(GL_VERTEX_ARRAY); 282 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 283 glDisableClientState(GL_NORMAL_ARRAY); 284 glDisableClientState(GL_COLOR_ARRAY); 285 286 glVertexPointer(2, GL_FIXED, 8, vtx); 287 glTexCoordPointer(2, GL_FIXED, 8, tex); 288 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr); 289 290 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 291} 292 293extern "C" void pfBindTexture(void *vp, RsProgramFragment vpf, uint32_t slot, RsAllocation va) 294{ 295 //LOGE("pfBindTexture %p", vpf); 296 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 297 rsi_ProgramFragmentBindTexture(env->mContext, 298 static_cast<ProgramFragment *>(vpf), 299 slot, 300 static_cast<Allocation *>(va)); 301 302} 303 304extern "C" void pfBindSampler(void *vp, RsProgramFragment vpf, uint32_t slot, RsSampler vs) 305{ 306 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 307 rsi_ProgramFragmentBindSampler(env->mContext, 308 static_cast<ProgramFragment *>(vpf), 309 slot, 310 static_cast<Sampler *>(vs)); 311 312} 313 314extern "C" void contextBindProgramFragmentStore(void *vp, RsProgramFragmentStore pfs) 315{ 316 //LOGE("contextBindProgramFragmentStore %p", pfs); 317 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 318 rsi_ContextBindProgramFragmentStore(env->mContext, pfs); 319 320} 321 322extern "C" void contextBindProgramFragment(void *vp, RsProgramFragment pf) 323{ 324 //LOGE("contextBindProgramFragment %p", pf); 325 ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); 326 rsi_ContextBindProgramFragment(env->mContext, pf); 327 328} 329 330 331static rsc_FunctionTable scriptCPtrTable = { 332 loadVp, 333 loadF, 334 loadI32, 335 loadU32, 336 loadEnvVec4, 337 loadEnvMatrix, 338 339 storeF, 340 storeI32, 341 storeU32, 342 storeEnvVec4, 343 storeEnvMatrix, 344 345 matrixLoadIdentity, 346 matrixLoadFloat, 347 matrixLoadMat, 348 matrixLoadRotate, 349 matrixLoadScale, 350 matrixLoadTranslate, 351 matrixLoadMultiply, 352 matrixMultiply, 353 matrixRotate, 354 matrixScale, 355 matrixTranslate, 356 357 color, 358 359 pfBindTexture, 360 pfBindSampler, 361 362 materialDiffuse, 363 materialSpecular, 364 lightPosition, 365 materialShininess, 366 uploadToTexture, 367 enable, 368 disable, 369 370 scriptRand, 371 contextBindProgramFragment, 372 contextBindProgramFragmentStore, 373 374 375 renderTriangleMesh, 376 renderTriangleMeshRange, 377 378 drawTriangleArray, 379 drawRect 380 381}; 382 383 384void ScriptC::run(Context *rsc, uint32_t launchID) 385{ 386 Env e = {rsc, this}; 387 mScript(&e, &scriptCPtrTable, launchID); 388} 389 390ScriptCState::ScriptCState() 391{ 392 clear(); 393} 394 395ScriptCState::~ScriptCState() 396{ 397} 398 399void ScriptCState::clear() 400{ 401 mConstantBufferTypes.clear(); 402 mClearColor[0] = 0; 403 mClearColor[1] = 0; 404 mClearColor[2] = 0; 405 mClearColor[3] = 1; 406 mClearDepth = 1; 407 mClearStencil = 0; 408 mScript = NULL; 409 mIsRoot = false; 410 mIsOrtho = true; 411} 412 413namespace android { 414namespace renderscript { 415 416void rsi_ScriptCBegin(Context * rsc) 417{ 418 ScriptCState *ss = &rsc->mScriptC; 419 ss->clear(); 420} 421 422void rsi_ScriptCSetClearColor(Context * rsc, float r, float g, float b, float a) 423{ 424 ScriptCState *ss = &rsc->mScriptC; 425 ss->mClearColor[0] = r; 426 ss->mClearColor[1] = g; 427 ss->mClearColor[2] = b; 428 ss->mClearColor[3] = a; 429} 430 431void rsi_ScriptCSetClearDepth(Context * rsc, float v) 432{ 433 ScriptCState *ss = &rsc->mScriptC; 434 ss->mClearDepth = v; 435} 436 437void rsi_ScriptCSetClearStencil(Context * rsc, uint32_t v) 438{ 439 ScriptCState *ss = &rsc->mScriptC; 440 ss->mClearStencil = v; 441} 442 443void rsi_ScriptCAddType(Context * rsc, RsType vt) 444{ 445 ScriptCState *ss = &rsc->mScriptC; 446 ss->mConstantBufferTypes.add(static_cast<const Type *>(vt)); 447} 448 449void rsi_ScriptCSetScript(Context * rsc, void *vp) 450{ 451 ScriptCState *ss = &rsc->mScriptC; 452 ss->mScript = reinterpret_cast<rsc_RunScript>(vp); 453} 454 455void rsi_ScriptCSetRoot(Context * rsc, bool isRoot) 456{ 457 ScriptCState *ss = &rsc->mScriptC; 458 ss->mIsRoot = isRoot; 459} 460 461void rsi_ScriptCSetOrtho(Context * rsc, bool isOrtho) 462{ 463 ScriptCState *ss = &rsc->mScriptC; 464 ss->mIsOrtho = isOrtho; 465} 466 467RsScript rsi_ScriptCCreate(Context * rsc) 468{ 469 ScriptCState *ss = &rsc->mScriptC; 470 471 ScriptC *s = new ScriptC(); 472 s->mScript = ss->mScript; 473 s->mClearColor[0] = ss->mClearColor[0]; 474 s->mClearColor[1] = ss->mClearColor[1]; 475 s->mClearColor[2] = ss->mClearColor[2]; 476 s->mClearColor[3] = ss->mClearColor[3]; 477 s->mClearDepth = ss->mClearDepth; 478 s->mClearStencil = ss->mClearStencil; 479 s->mIsRoot = ss->mIsRoot; 480 s->mIsOrtho = ss->mIsOrtho; 481 482 return s; 483} 484 485} 486} 487 488 489