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 | |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 21 | #include "acc/acc.h" |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 22 | #include "utils/String8.h" |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 23 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 24 | using namespace android; |
| 25 | using namespace android::renderscript; |
| 26 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 27 | #define GET_TLS() Context::ScriptTLSStruct * tls = \ |
| 28 | (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \ |
| 29 | Context * rsc = tls->mContext; \ |
| 30 | ScriptC * sc = (ScriptC *) tls->mScript |
| 31 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 32 | |
| 33 | ScriptC::ScriptC() |
| 34 | { |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 35 | mAccScript = NULL; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 36 | memset(&mProgram, 0, sizeof(mProgram)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | ScriptC::~ScriptC() |
| 40 | { |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 41 | if (mAccScript) { |
| 42 | accDeleteScript(mAccScript); |
| 43 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Jason Sams | af49c74 | 2009-06-19 18:33:44 -0700 | [diff] [blame^] | 46 | extern "C" float fixedToFloat(int32_t f) |
| 47 | { |
| 48 | return ((float)f) / 0x10000; |
| 49 | } |
| 50 | |
| 51 | extern "C" float intToFloat(int32_t f) |
| 52 | { |
| 53 | return (float)f; |
| 54 | } |
| 55 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 56 | extern "C" void matrixLoadIdentity(rsc_Matrix *mat) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 57 | { |
| 58 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 59 | m->loadIdentity(); |
| 60 | } |
| 61 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 62 | extern "C" void matrixLoadFloat(rsc_Matrix *mat, const float *f) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 63 | { |
| 64 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 65 | m->load(f); |
| 66 | } |
| 67 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 68 | extern "C" void matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 69 | { |
| 70 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 71 | m->load(reinterpret_cast<const Matrix *>(newmat)); |
| 72 | } |
| 73 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 74 | extern "C" void matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 75 | { |
| 76 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 77 | m->loadRotate(rot, x, y, z); |
| 78 | } |
| 79 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 80 | extern "C" void matrixLoadScale(rsc_Matrix *mat, float x, float y, float z) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 81 | { |
| 82 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 83 | m->loadScale(x, y, z); |
| 84 | } |
| 85 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 86 | extern "C" void matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 87 | { |
| 88 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 89 | m->loadTranslate(x, y, z); |
| 90 | } |
| 91 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 92 | extern "C" void matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 93 | { |
| 94 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 95 | m->loadMultiply(reinterpret_cast<const Matrix *>(lhs), |
| 96 | reinterpret_cast<const Matrix *>(rhs)); |
| 97 | } |
| 98 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 99 | extern "C" void matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 100 | { |
| 101 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 102 | m->multiply(reinterpret_cast<const Matrix *>(rhs)); |
| 103 | } |
| 104 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 105 | extern "C" void matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 106 | { |
| 107 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 108 | m->rotate(rot, x, y, z); |
| 109 | } |
| 110 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 111 | extern "C" void matrixScale(rsc_Matrix *mat, float x, float y, float z) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 112 | { |
| 113 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 114 | m->scale(x, y, z); |
| 115 | } |
| 116 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 117 | extern "C" void matrixTranslate(rsc_Matrix *mat, float x, float y, float z) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 118 | { |
| 119 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 120 | m->translate(x, y, z); |
| 121 | } |
| 122 | |
| 123 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 124 | extern "C" const void * loadVp(uint32_t bank, uint32_t offset) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 125 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 126 | GET_TLS(); |
| 127 | return &static_cast<const uint8_t *>(sc->mSlots[bank]->getPtr())[offset]; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 130 | extern "C" float loadF(uint32_t bank, uint32_t offset) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 131 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 132 | GET_TLS(); |
| 133 | return static_cast<const float *>(sc->mSlots[bank]->getPtr())[offset]; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 136 | extern "C" int32_t loadI32(uint32_t bank, uint32_t offset) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 137 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 138 | GET_TLS(); |
| 139 | return static_cast<const int32_t *>(sc->mSlots[bank]->getPtr())[offset]; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 142 | extern "C" uint32_t loadU32(uint32_t bank, uint32_t offset) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 143 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 144 | GET_TLS(); |
| 145 | return static_cast<const uint32_t *>(sc->mSlots[bank]->getPtr())[offset]; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 148 | extern "C" void loadEnvVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 149 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 150 | GET_TLS(); |
| 151 | memcpy(v, &static_cast<const float *>(sc->mSlots[bank]->getPtr())[offset], sizeof(rsc_Vector4)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 154 | extern "C" void loadEnvMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 155 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 156 | GET_TLS(); |
| 157 | memcpy(m, &static_cast<const float *>(sc->mSlots[bank]->getPtr())[offset], sizeof(rsc_Matrix)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 161 | extern "C" void storeF(uint32_t bank, uint32_t offset, float v) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 162 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 163 | GET_TLS(); |
| 164 | static_cast<float *>(sc->mSlots[bank]->getPtr())[offset] = v; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 167 | extern "C" void storeI32(uint32_t bank, uint32_t offset, int32_t v) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 168 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 169 | GET_TLS(); |
| 170 | static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 173 | extern "C" void storeU32(uint32_t bank, uint32_t offset, uint32_t v) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 174 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 175 | GET_TLS(); |
| 176 | static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 179 | extern "C" void storeEnvVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 180 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 181 | GET_TLS(); |
| 182 | memcpy(&static_cast<float *>(sc->mSlots[bank]->getPtr())[offset], v, sizeof(rsc_Vector4)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 185 | extern "C" void storeEnvMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 186 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 187 | GET_TLS(); |
| 188 | memcpy(&static_cast<float *>(sc->mSlots[bank]->getPtr())[offset], m, sizeof(rsc_Matrix)); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 192 | extern "C" void color(float r, float g, float b, float a) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 193 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 194 | glColor4f(r, g, b, a); |
| 195 | } |
| 196 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 197 | extern "C" void renderTriangleMesh(RsTriangleMesh mesh) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 198 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 199 | GET_TLS(); |
| 200 | rsi_TriangleMeshRender(rsc, mesh); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 203 | extern "C" void renderTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 204 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 205 | GET_TLS(); |
| 206 | rsi_TriangleMeshRenderRange(rsc, mesh, start, count); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 209 | extern "C" void materialDiffuse(float r, float g, float b, float a) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 210 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 211 | float v[] = {r, g, b, a}; |
| 212 | glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, v); |
| 213 | } |
| 214 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 215 | extern "C" void materialSpecular(float r, float g, float b, float a) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 216 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 217 | float v[] = {r, g, b, a}; |
| 218 | glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, v); |
| 219 | } |
| 220 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 221 | extern "C" void lightPosition(float x, float y, float z, float w) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 222 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 223 | float v[] = {x, y, z, w}; |
| 224 | glLightfv(GL_LIGHT0, GL_POSITION, v); |
| 225 | } |
| 226 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 227 | extern "C" void materialShininess(float s) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 228 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 229 | glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &s); |
| 230 | } |
| 231 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 232 | extern "C" void uploadToTexture(RsAllocation va, uint32_t baseMipLevel) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 233 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 234 | GET_TLS(); |
| 235 | rsi_AllocationUploadToTexture(rsc, va, baseMipLevel); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 238 | extern "C" void enable(uint32_t p) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 239 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 240 | glEnable(p); |
| 241 | } |
| 242 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 243 | extern "C" void disable(uint32_t p) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 244 | { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 245 | glDisable(p); |
| 246 | } |
| 247 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 248 | extern "C" uint32_t scriptRand(uint32_t max) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 249 | { |
| 250 | return (uint32_t)(((float)rand()) * max / RAND_MAX); |
| 251 | } |
| 252 | |
| 253 | // Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 254 | extern "C" void drawTriangleArray(RsAllocation alloc, uint32_t count) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 255 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 256 | GET_TLS(); |
| 257 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 258 | const Allocation *a = (const Allocation *)alloc; |
| 259 | const uint32_t *ptr = (const uint32_t *)a->getPtr(); |
| 260 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 261 | rsc->setupCheck(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 262 | |
| 263 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 264 | //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]); |
| 265 | |
| 266 | glEnableClientState(GL_VERTEX_ARRAY); |
| 267 | glDisableClientState(GL_NORMAL_ARRAY); |
| 268 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 269 | glEnableClientState(GL_COLOR_ARRAY); |
| 270 | |
| 271 | glVertexPointer(2, GL_FIXED, 12, ptr + 1); |
| 272 | //glTexCoordPointer(2, GL_FIXED, 24, ptr + 1); |
| 273 | glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr); |
| 274 | |
| 275 | glDrawArrays(GL_TRIANGLES, 0, count * 3); |
| 276 | } |
| 277 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 278 | extern "C" void drawRect(int32_t x1, int32_t x2, int32_t y1, int32_t y2) |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 279 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 280 | GET_TLS(); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 281 | x1 = (x1 << 16); |
| 282 | x2 = (x2 << 16); |
| 283 | y1 = (y1 << 16); |
| 284 | y2 = (y2 << 16); |
| 285 | |
| 286 | int32_t vtx[] = {x1,y1, x1,y2, x2,y1, x2,y2}; |
| 287 | static const int32_t tex[] = {0,0, 0,0x10000, 0x10000,0, 0x10000,0x10000}; |
| 288 | |
| 289 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 290 | rsc->setupCheck(); |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 291 | |
| 292 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 293 | //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]); |
| 294 | |
| 295 | glEnableClientState(GL_VERTEX_ARRAY); |
| 296 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 297 | glDisableClientState(GL_NORMAL_ARRAY); |
| 298 | glDisableClientState(GL_COLOR_ARRAY); |
| 299 | |
| 300 | glVertexPointer(2, GL_FIXED, 8, vtx); |
| 301 | glTexCoordPointer(2, GL_FIXED, 8, tex); |
| 302 | //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr); |
| 303 | |
| 304 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 305 | } |
| 306 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 307 | extern "C" void pfBindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 308 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 309 | GET_TLS(); |
| 310 | rsi_ProgramFragmentBindTexture(rsc, |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 311 | static_cast<ProgramFragment *>(vpf), |
| 312 | slot, |
| 313 | static_cast<Allocation *>(va)); |
| 314 | |
| 315 | } |
| 316 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 317 | extern "C" void pfBindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 318 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 319 | GET_TLS(); |
| 320 | rsi_ProgramFragmentBindSampler(rsc, |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 321 | static_cast<ProgramFragment *>(vpf), |
| 322 | slot, |
| 323 | static_cast<Sampler *>(vs)); |
| 324 | |
| 325 | } |
| 326 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 327 | extern "C" void contextBindProgramFragmentStore(RsProgramFragmentStore pfs) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 328 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 329 | GET_TLS(); |
| 330 | rsi_ContextBindProgramFragmentStore(rsc, pfs); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 331 | |
| 332 | } |
| 333 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 334 | extern "C" void contextBindProgramFragment(RsProgramFragment pf) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 335 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 336 | GET_TLS(); |
| 337 | rsi_ContextBindProgramFragment(rsc, pf); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 338 | |
| 339 | } |
| 340 | |
| 341 | |
| 342 | static rsc_FunctionTable scriptCPtrTable = { |
| 343 | loadVp, |
| 344 | loadF, |
| 345 | loadI32, |
| 346 | loadU32, |
| 347 | loadEnvVec4, |
| 348 | loadEnvMatrix, |
| 349 | |
| 350 | storeF, |
| 351 | storeI32, |
| 352 | storeU32, |
| 353 | storeEnvVec4, |
| 354 | storeEnvMatrix, |
| 355 | |
| 356 | matrixLoadIdentity, |
| 357 | matrixLoadFloat, |
| 358 | matrixLoadMat, |
| 359 | matrixLoadRotate, |
| 360 | matrixLoadScale, |
| 361 | matrixLoadTranslate, |
| 362 | matrixLoadMultiply, |
| 363 | matrixMultiply, |
| 364 | matrixRotate, |
| 365 | matrixScale, |
| 366 | matrixTranslate, |
| 367 | |
| 368 | color, |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 369 | |
| 370 | pfBindTexture, |
| 371 | pfBindSampler, |
| 372 | |
| 373 | materialDiffuse, |
| 374 | materialSpecular, |
| 375 | lightPosition, |
| 376 | materialShininess, |
| 377 | uploadToTexture, |
| 378 | enable, |
| 379 | disable, |
| 380 | |
| 381 | scriptRand, |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 382 | contextBindProgramFragment, |
Jason Sams | fe08d99 | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 383 | contextBindProgramFragmentStore, |
| 384 | |
| 385 | |
| 386 | renderTriangleMesh, |
| 387 | renderTriangleMeshRange, |
| 388 | |
| 389 | drawTriangleArray, |
| 390 | drawRect |
| 391 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 392 | }; |
| 393 | |
| 394 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 395 | bool ScriptC::run(Context *rsc, uint32_t launchIndex) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 396 | { |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 397 | Context::ScriptTLSStruct * tls = |
| 398 | (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 399 | |
| 400 | if (mEnviroment.mFragmentStore.get()) { |
| 401 | rsc->setFragmentStore(mEnviroment.mFragmentStore.get()); |
| 402 | } |
| 403 | if (mEnviroment.mFragment.get()) { |
| 404 | rsc->setFragment(mEnviroment.mFragment.get()); |
| 405 | } |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 406 | if (mEnviroment.mVertex.get()) { |
| 407 | rsc->setVertex(mEnviroment.mVertex.get()); |
| 408 | } |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 409 | |
Jason Sams | 462d11b | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 410 | tls->mScript = this; |
| 411 | return mProgram.mScript(launchIndex, &scriptCPtrTable) != 0; |
| 412 | tls->mScript = NULL; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | ScriptCState::ScriptCState() |
| 416 | { |
| 417 | clear(); |
| 418 | } |
| 419 | |
| 420 | ScriptCState::~ScriptCState() |
| 421 | { |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 422 | if (mAccScript) { |
| 423 | accDeleteScript(mAccScript); |
| 424 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void ScriptCState::clear() |
| 428 | { |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 429 | memset(&mProgram, 0, sizeof(mProgram)); |
| 430 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 431 | mConstantBufferTypes.clear(); |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 432 | |
| 433 | memset(&mEnviroment, 0, sizeof(mEnviroment)); |
| 434 | mEnviroment.mClearColor[0] = 0; |
| 435 | mEnviroment.mClearColor[1] = 0; |
| 436 | mEnviroment.mClearColor[2] = 0; |
| 437 | mEnviroment.mClearColor[3] = 1; |
| 438 | mEnviroment.mClearDepth = 1; |
| 439 | mEnviroment.mClearStencil = 0; |
| 440 | mEnviroment.mIsRoot = false; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 441 | |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 442 | mAccScript = NULL; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 443 | |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 446 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 447 | void ScriptCState::runCompiler(Context *rsc) |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 448 | { |
| 449 | mAccScript = accCreateScript(); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 450 | String8 tmp; |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 451 | |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 452 | rsc->appendNameDefines(&tmp); |
| 453 | |
| 454 | const char* scriptSource[] = {tmp.string(), mProgram.mScriptText}; |
| 455 | int scriptLength[] = {tmp.length(), mProgram.mScriptTextLength} ; |
| 456 | accScriptSource(mAccScript, sizeof(scriptLength) / sizeof(int), scriptSource, scriptLength); |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 457 | accCompileScript(mAccScript); |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 458 | accGetScriptLabel(mAccScript, "main", (ACCvoid**) &mProgram.mScript); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 459 | rsAssert(mProgram.mScript); |
| 460 | |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 461 | mEnviroment.mFragment.set(rsc->getDefaultProgramFragment()); |
| 462 | mEnviroment.mVertex.set(rsc->getDefaultProgramVertex()); |
| 463 | mEnviroment.mFragmentStore.set(rsc->getDefaultProgramFragmentStore()); |
Jason Sams | d5680f9 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 464 | |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 465 | if (mProgram.mScript) { |
| 466 | const static int pragmaMax = 16; |
| 467 | ACCsizei pragmaCount; |
| 468 | ACCchar * str[pragmaMax]; |
| 469 | accGetPragmas(mAccScript, &pragmaCount, pragmaMax, &str[0]); |
| 470 | |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 471 | for (int ct=0; ct < pragmaCount; ct+=2) { |
| 472 | LOGE("pragma %i %s %s", ct, str[ct], str[ct+1]); |
| 473 | |
| 474 | if (!strcmp(str[ct], "version")) { |
| 475 | continue; |
| 476 | |
| 477 | } |
| 478 | |
| 479 | |
| 480 | if (!strcmp(str[ct], "stateVertex")) { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 481 | if (!strcmp(str[ct+1], "default")) { |
| 482 | continue; |
| 483 | } |
| 484 | if (!strcmp(str[ct+1], "parent")) { |
| 485 | mEnviroment.mVertex.clear(); |
| 486 | continue; |
| 487 | } |
| 488 | ProgramVertex * pv = (ProgramVertex *)rsc->lookupName(str[ct+1]); |
| 489 | if (pv != NULL) { |
| 490 | mEnviroment.mVertex.set(pv); |
| 491 | continue; |
| 492 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 493 | LOGE("Unreconized value %s passed to stateVertex", str[ct+1]); |
| 494 | } |
| 495 | |
| 496 | if (!strcmp(str[ct], "stateRaster")) { |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 497 | LOGE("Unreconized value %s passed to stateRaster", str[ct+1]); |
| 498 | } |
| 499 | |
| 500 | if (!strcmp(str[ct], "stateFragment")) { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 501 | if (!strcmp(str[ct+1], "default")) { |
| 502 | continue; |
| 503 | } |
| 504 | if (!strcmp(str[ct+1], "parent")) { |
| 505 | mEnviroment.mFragment.clear(); |
| 506 | continue; |
| 507 | } |
| 508 | ProgramFragment * pf = (ProgramFragment *)rsc->lookupName(str[ct+1]); |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 509 | if (pf != NULL) { |
| 510 | mEnviroment.mFragment.set(pf); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 511 | continue; |
| 512 | } |
| 513 | LOGE("Unreconized value %s passed to stateFragment", str[ct+1]); |
| 514 | } |
| 515 | |
| 516 | if (!strcmp(str[ct], "stateFragmentStore")) { |
Jason Sams | 9c54bdb | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 517 | if (!strcmp(str[ct+1], "default")) { |
| 518 | continue; |
| 519 | } |
| 520 | if (!strcmp(str[ct+1], "parent")) { |
| 521 | mEnviroment.mFragmentStore.clear(); |
| 522 | continue; |
| 523 | } |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 524 | ProgramFragmentStore * pfs = |
| 525 | (ProgramFragmentStore *)rsc->lookupName(str[ct+1]); |
| 526 | if (pfs != NULL) { |
| 527 | mEnviroment.mFragmentStore.set(pfs); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 528 | continue; |
| 529 | } |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 530 | LOGE("Unreconized value %s passed to stateFragmentStore", str[ct+1]); |
| 531 | } |
| 532 | |
| 533 | } |
| 534 | |
| 535 | |
| 536 | } else { |
| 537 | // Deal with an error. |
| 538 | } |
| 539 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | namespace android { |
| 543 | namespace renderscript { |
| 544 | |
| 545 | void rsi_ScriptCBegin(Context * rsc) |
| 546 | { |
| 547 | ScriptCState *ss = &rsc->mScriptC; |
| 548 | ss->clear(); |
| 549 | } |
| 550 | |
| 551 | void rsi_ScriptCSetClearColor(Context * rsc, float r, float g, float b, float a) |
| 552 | { |
| 553 | ScriptCState *ss = &rsc->mScriptC; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 554 | ss->mEnviroment.mClearColor[0] = r; |
| 555 | ss->mEnviroment.mClearColor[1] = g; |
| 556 | ss->mEnviroment.mClearColor[2] = b; |
| 557 | ss->mEnviroment.mClearColor[3] = a; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | void rsi_ScriptCSetClearDepth(Context * rsc, float v) |
| 561 | { |
| 562 | ScriptCState *ss = &rsc->mScriptC; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 563 | ss->mEnviroment.mClearDepth = v; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | void rsi_ScriptCSetClearStencil(Context * rsc, uint32_t v) |
| 567 | { |
| 568 | ScriptCState *ss = &rsc->mScriptC; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 569 | ss->mEnviroment.mClearStencil = v; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void rsi_ScriptCAddType(Context * rsc, RsType vt) |
| 573 | { |
| 574 | ScriptCState *ss = &rsc->mScriptC; |
| 575 | ss->mConstantBufferTypes.add(static_cast<const Type *>(vt)); |
| 576 | } |
| 577 | |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 578 | void rsi_ScriptCSetScript(Context * rsc, void *vp) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 579 | { |
| 580 | ScriptCState *ss = &rsc->mScriptC; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 581 | ss->mProgram.mScript = reinterpret_cast<rsc_RunScript>(vp); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void rsi_ScriptCSetRoot(Context * rsc, bool isRoot) |
| 585 | { |
| 586 | ScriptCState *ss = &rsc->mScriptC; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 587 | ss->mEnviroment.mIsRoot = isRoot; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 590 | void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len) |
| 591 | { |
| 592 | ScriptCState *ss = &rsc->mScriptC; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 593 | ss->mProgram.mScriptText = text; |
| 594 | ss->mProgram.mScriptTextLength = len; |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 598 | RsScript rsi_ScriptCCreate(Context * rsc) |
| 599 | { |
| 600 | ScriptCState *ss = &rsc->mScriptC; |
| 601 | |
Jason Sams | 3eaa338 | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 602 | ss->runCompiler(rsc); |
Jason Sams | 39ddc950 | 2009-06-05 17:35:09 -0700 | [diff] [blame] | 603 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 604 | ScriptC *s = new ScriptC(); |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 605 | s->incRef(); |
Jack Palevich | ec5a20b | 2009-05-28 15:53:04 -0700 | [diff] [blame] | 606 | s->mAccScript = ss->mAccScript; |
| 607 | ss->mAccScript = NULL; |
Jason Sams | 3a833d8 | 2009-06-08 15:20:31 -0700 | [diff] [blame] | 608 | s->mEnviroment = ss->mEnviroment; |
| 609 | s->mProgram = ss->mProgram; |
| 610 | ss->clear(); |
Jason Sams | da423d8 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 611 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 612 | return s; |
| 613 | } |
| 614 | |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | |