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