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