Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
| 24 | |
| 25 | ScriptC::ScriptC() |
| 26 | { |
| 27 | mScript = NULL; |
| 28 | } |
| 29 | |
| 30 | ScriptC::~ScriptC() |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | static void matrixLoadIdentity(void *con, rsc_Matrix *mat) |
| 35 | { |
| 36 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 37 | m->loadIdentity(); |
| 38 | } |
| 39 | |
| 40 | static void matrixLoadFloat(void *con, rsc_Matrix *mat, const float *f) |
| 41 | { |
| 42 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 43 | m->load(f); |
| 44 | } |
| 45 | |
| 46 | static 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 | |
| 52 | static 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 | |
| 58 | static 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 | |
| 64 | static 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 | |
| 70 | static 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 | |
| 77 | static 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 | |
| 83 | static 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 | |
| 89 | static 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 | |
| 95 | static 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 | |
| 102 | static 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 | |
| 108 | static 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 | |
| 116 | static 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 | |
| 122 | static 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 | |
| 128 | static 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 | |
| 134 | static 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 | |
| 141 | static 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 | |
| 147 | static 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 | |
| 153 | static 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 | |
| 159 | static 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 | |
| 165 | static 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 | |
| 172 | static 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 | |
| 178 | static void renderTriangleMesh(void *vp, RsTriangleMesh mesh) |
| 179 | { |
| 180 | ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); |
| 181 | rsi_TriangleMeshRender(env->mContext, mesh); |
| 182 | } |
| 183 | |
| 184 | static 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 | |
| 190 | static 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 | |
| 197 | static 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 | |
| 204 | static 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 | |
| 211 | static 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 | |
| 217 | static 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 | |
| 223 | static void enable(void *vp, uint32_t p) |
| 224 | { |
| 225 | ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); |
| 226 | glEnable(p); |
| 227 | } |
| 228 | |
| 229 | static void disable(void *vp, uint32_t p) |
| 230 | { |
| 231 | ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); |
| 232 | glDisable(p); |
| 233 | } |
| 234 | |
| 235 | static 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 |
| 241 | static 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 | |
| 264 | static void pfBindTexture(void *vp, RsProgramFragment vpf, uint32_t slot, RsAllocation va) |
| 265 | { |
| 266 | //LOGE("pfBindTexture %p", vpf); |
| 267 | ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); |
| 268 | rsi_ProgramFragmentBindTexture(env->mContext, |
| 269 | static_cast<ProgramFragment *>(vpf), |
| 270 | slot, |
| 271 | static_cast<Allocation *>(va)); |
| 272 | |
| 273 | } |
| 274 | |
| 275 | static void pfBindSampler(void *vp, RsProgramFragment vpf, uint32_t slot, RsSampler vs) |
| 276 | { |
| 277 | ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); |
| 278 | rsi_ProgramFragmentBindSampler(env->mContext, |
| 279 | static_cast<ProgramFragment *>(vpf), |
| 280 | slot, |
| 281 | static_cast<Sampler *>(vs)); |
| 282 | |
| 283 | } |
| 284 | |
| 285 | static void contextBindProgramFragmentStore(void *vp, RsProgramFragmentStore pfs) |
| 286 | { |
| 287 | //LOGE("contextBindProgramFragmentStore %p", pfs); |
| 288 | ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); |
| 289 | rsi_ContextBindProgramFragmentStore(env->mContext, pfs); |
| 290 | |
| 291 | } |
| 292 | |
| 293 | static void contextBindProgramFragment(void *vp, RsProgramFragment pf) |
| 294 | { |
| 295 | //LOGE("contextBindProgramFragment %p", pf); |
| 296 | ScriptC::Env * env = static_cast<ScriptC::Env *>(vp); |
| 297 | rsi_ContextBindProgramFragment(env->mContext, pf); |
| 298 | |
| 299 | } |
| 300 | |
| 301 | |
| 302 | static 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 | |
| 350 | void ScriptC::run(Context *rsc, uint32_t launchID) |
| 351 | { |
| 352 | Env e = {rsc, this}; |
| 353 | mScript(&e, &scriptCPtrTable, launchID); |
| 354 | } |
| 355 | |
| 356 | ScriptCState::ScriptCState() |
| 357 | { |
| 358 | clear(); |
| 359 | } |
| 360 | |
| 361 | ScriptCState::~ScriptCState() |
| 362 | { |
| 363 | } |
| 364 | |
| 365 | void 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 | |
| 379 | namespace android { |
| 380 | namespace renderscript { |
| 381 | |
| 382 | void rsi_ScriptCBegin(Context * rsc) |
| 383 | { |
| 384 | ScriptCState *ss = &rsc->mScriptC; |
| 385 | ss->clear(); |
| 386 | } |
| 387 | |
| 388 | void 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 | |
| 397 | void rsi_ScriptCSetClearDepth(Context * rsc, float v) |
| 398 | { |
| 399 | ScriptCState *ss = &rsc->mScriptC; |
| 400 | ss->mClearDepth = v; |
| 401 | } |
| 402 | |
| 403 | void rsi_ScriptCSetClearStencil(Context * rsc, uint32_t v) |
| 404 | { |
| 405 | ScriptCState *ss = &rsc->mScriptC; |
| 406 | ss->mClearStencil = v; |
| 407 | } |
| 408 | |
| 409 | void rsi_ScriptCAddType(Context * rsc, RsType vt) |
| 410 | { |
| 411 | ScriptCState *ss = &rsc->mScriptC; |
| 412 | ss->mConstantBufferTypes.add(static_cast<const Type *>(vt)); |
| 413 | } |
| 414 | |
| 415 | void rsi_ScriptCSetScript(Context * rsc, void *vp) |
| 416 | { |
| 417 | ScriptCState *ss = &rsc->mScriptC; |
| 418 | ss->mScript = reinterpret_cast<rsc_RunScript>(vp); |
| 419 | } |
| 420 | |
| 421 | void rsi_ScriptCSetRoot(Context * rsc, bool isRoot) |
| 422 | { |
| 423 | ScriptCState *ss = &rsc->mScriptC; |
| 424 | ss->mIsRoot = isRoot; |
| 425 | } |
| 426 | |
| 427 | void rsi_ScriptCSetOrtho(Context * rsc, bool isOrtho) |
| 428 | { |
| 429 | ScriptCState *ss = &rsc->mScriptC; |
| 430 | ss->mIsOrtho = isOrtho; |
| 431 | } |
| 432 | |
| 433 | RsScript 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 | |