Jason Sams | 536923d | 2010-05-18 13:35:45 -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" |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 20 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 21 | #include "utils/Timers.h" |
| 22 | |
| 23 | #define GL_GLEXT_PROTOTYPES |
| 24 | |
| 25 | #include <GLES/gl.h> |
| 26 | #include <GLES/glext.h> |
| 27 | #include <GLES2/gl2.h> |
| 28 | #include <GLES2/gl2ext.h> |
| 29 | |
| 30 | #include <time.h> |
| 31 | |
| 32 | using namespace android; |
| 33 | using namespace android::renderscript; |
| 34 | |
| 35 | #define GET_TLS() Context::ScriptTLSStruct * tls = \ |
| 36 | (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \ |
| 37 | Context * rsc = tls->mContext; \ |
| 38 | ScriptC * sc = (ScriptC *) tls->mScript |
| 39 | |
| 40 | |
| 41 | ////////////////////////////////////////////////////////////////////////////// |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 42 | // Context |
| 43 | ////////////////////////////////////////////////////////////////////////////// |
| 44 | |
| 45 | static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va) |
| 46 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 47 | CHECK_OBJ_OR_NULL(va); |
| 48 | CHECK_OBJ(vpf); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 49 | GET_TLS(); |
| 50 | rsi_ProgramBindTexture(rsc, |
| 51 | static_cast<ProgramFragment *>(vpf), |
| 52 | slot, |
| 53 | static_cast<Allocation *>(va)); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs) |
| 58 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 59 | CHECK_OBJ_OR_NULL(vs); |
| 60 | CHECK_OBJ(vpf); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 61 | GET_TLS(); |
| 62 | rsi_ProgramBindSampler(rsc, |
| 63 | static_cast<ProgramFragment *>(vpf), |
| 64 | slot, |
| 65 | static_cast<Sampler *>(vs)); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | static void SC_bindProgramStore(RsProgramStore pfs) |
| 70 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 71 | CHECK_OBJ_OR_NULL(pfs); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 72 | GET_TLS(); |
| 73 | rsi_ContextBindProgramStore(rsc, pfs); |
| 74 | } |
| 75 | |
| 76 | static void SC_bindProgramFragment(RsProgramFragment pf) |
| 77 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 78 | CHECK_OBJ_OR_NULL(pf); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 79 | GET_TLS(); |
| 80 | rsi_ContextBindProgramFragment(rsc, pf); |
| 81 | } |
| 82 | |
| 83 | static void SC_bindProgramVertex(RsProgramVertex pv) |
| 84 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 85 | CHECK_OBJ_OR_NULL(pv); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 86 | GET_TLS(); |
| 87 | rsi_ContextBindProgramVertex(rsc, pv); |
| 88 | } |
| 89 | |
| 90 | static void SC_bindProgramRaster(RsProgramRaster pv) |
| 91 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 92 | CHECK_OBJ_OR_NULL(pv); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 93 | GET_TLS(); |
| 94 | rsi_ContextBindProgramRaster(rsc, pv); |
| 95 | } |
| 96 | |
| 97 | ////////////////////////////////////////////////////////////////////////////// |
| 98 | // VP |
| 99 | ////////////////////////////////////////////////////////////////////////////// |
| 100 | |
Jim Miller | d8e7620 | 2010-07-28 14:46:22 -0700 | [diff] [blame] | 101 | static void SC_vpLoadProjectionMatrix(const rsc_Matrix *m) |
| 102 | { |
| 103 | GET_TLS(); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 104 | rsc->getVertex()->setProjectionMatrix(rsc, m); |
Jim Miller | d8e7620 | 2010-07-28 14:46:22 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 107 | static void SC_vpLoadModelMatrix(const rsc_Matrix *m) |
| 108 | { |
| 109 | GET_TLS(); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 110 | rsc->getVertex()->setModelviewMatrix(rsc, m); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void SC_vpLoadTextureMatrix(const rsc_Matrix *m) |
| 114 | { |
| 115 | GET_TLS(); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 116 | rsc->getVertex()->setTextureMatrix(rsc, m); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
Jason Sams | 442a647 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 120 | static void SC_pfConstantColor(RsProgramFragment vpf, float r, float g, float b, float a) |
| 121 | { |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 122 | GET_TLS(); |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 123 | CHECK_OBJ(vpf); |
Jason Sams | 442a647 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 124 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 125 | pf->setConstantColor(rsc, r, g, b, a); |
Jason Sams | 442a647 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Alex Sakhartchouk | cbed752 | 2010-08-16 17:40:10 -0700 | [diff] [blame] | 128 | static void SC_vpGetProjectionMatrix(rsc_Matrix *m) |
| 129 | { |
| 130 | GET_TLS(); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 131 | rsc->getVertex()->getProjectionMatrix(rsc, m); |
Alex Sakhartchouk | cbed752 | 2010-08-16 17:40:10 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 134 | |
| 135 | ////////////////////////////////////////////////////////////////////////////// |
| 136 | // Drawing |
| 137 | ////////////////////////////////////////////////////////////////////////////// |
| 138 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 139 | static void SC_drawQuadTexCoords(float x1, float y1, float z1, |
| 140 | float u1, float v1, |
| 141 | float x2, float y2, float z2, |
| 142 | float u2, float v2, |
| 143 | float x3, float y3, float z3, |
| 144 | float u3, float v3, |
| 145 | float x4, float y4, float z4, |
| 146 | float u4, float v4) |
| 147 | { |
| 148 | GET_TLS(); |
| 149 | if (!rsc->setupCheck()) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | //LOGE("Quad"); |
| 154 | //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1); |
| 155 | //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2); |
| 156 | //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3); |
| 157 | //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4); |
| 158 | |
| 159 | float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4}; |
| 160 | const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4}; |
| 161 | |
Alex Sakhartchouk | 9d71e21 | 2010-11-08 15:10:52 -0800 | [diff] [blame] | 162 | VertexArray::Attrib attribs[2]; |
| 163 | attribs[0].set(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "ATTRIB_position"); |
| 164 | attribs[1].set(GL_FLOAT, 2, 8, false, (uint32_t)tex, "ATTRIB_texture0"); |
| 165 | |
| 166 | VertexArray va(attribs, 2); |
Jason Sams | 8cb39de | 2010-06-01 15:47:01 -0700 | [diff] [blame] | 167 | va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 168 | |
| 169 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 170 | } |
| 171 | |
| 172 | static void SC_drawQuad(float x1, float y1, float z1, |
| 173 | float x2, float y2, float z2, |
| 174 | float x3, float y3, float z3, |
| 175 | float x4, float y4, float z4) |
| 176 | { |
| 177 | SC_drawQuadTexCoords(x1, y1, z1, 0, 1, |
| 178 | x2, y2, z2, 1, 1, |
| 179 | x3, y3, z3, 1, 0, |
| 180 | x4, y4, z4, 0, 0); |
| 181 | } |
| 182 | |
| 183 | static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h) |
| 184 | { |
| 185 | GET_TLS(); |
| 186 | ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex()); |
| 187 | rsc->setVertex(rsc->getDefaultProgramVertex()); |
| 188 | //rsc->setupCheck(); |
| 189 | |
| 190 | //GLint crop[4] = {0, h, w, -h}; |
| 191 | |
| 192 | float sh = rsc->getHeight(); |
| 193 | |
| 194 | SC_drawQuad(x, sh - y, z, |
| 195 | x+w, sh - y, z, |
| 196 | x+w, sh - (y+h), z, |
| 197 | x, sh - (y+h), z); |
| 198 | rsc->setVertex((ProgramVertex *)tmp.get()); |
| 199 | } |
Jason Sams | 6d1cf41 | 2010-06-17 18:05:38 -0700 | [diff] [blame] | 200 | /* |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 201 | static void SC_drawSprite(float x, float y, float z, float w, float h) |
| 202 | { |
| 203 | GET_TLS(); |
| 204 | float vin[3] = {x, y, z}; |
| 205 | float vout[4]; |
| 206 | |
| 207 | //LOGE("ds in %f %f %f", x, y, z); |
| 208 | rsc->getVertex()->transformToScreen(rsc, vout, vin); |
| 209 | //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]); |
| 210 | vout[0] /= vout[3]; |
| 211 | vout[1] /= vout[3]; |
| 212 | vout[2] /= vout[3]; |
| 213 | |
| 214 | vout[0] *= rsc->getWidth() / 2; |
| 215 | vout[1] *= rsc->getHeight() / 2; |
| 216 | vout[0] += rsc->getWidth() / 2; |
| 217 | vout[1] += rsc->getHeight() / 2; |
| 218 | |
| 219 | vout[0] -= w/2; |
| 220 | vout[1] -= h/2; |
| 221 | |
| 222 | //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]); |
| 223 | |
| 224 | // U, V, W, H |
| 225 | SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w); |
| 226 | //rsc->setupCheck(); |
| 227 | } |
Jason Sams | 6d1cf41 | 2010-06-17 18:05:38 -0700 | [diff] [blame] | 228 | */ |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 229 | |
| 230 | static void SC_drawRect(float x1, float y1, |
| 231 | float x2, float y2, float z) |
| 232 | { |
| 233 | //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z); |
| 234 | SC_drawQuad(x1, y2, z, |
| 235 | x2, y2, z, |
| 236 | x2, y1, z, |
| 237 | x1, y1, z); |
| 238 | } |
| 239 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 240 | static void SC_drawMesh(RsMesh vsm) |
| 241 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 242 | CHECK_OBJ(vsm); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 243 | GET_TLS(); |
| 244 | Mesh *sm = static_cast<Mesh *>(vsm); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 245 | if (!rsc->setupCheck()) { |
| 246 | return; |
| 247 | } |
| 248 | sm->render(rsc); |
| 249 | } |
| 250 | |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 251 | static void SC_drawMeshPrimitive(RsMesh vsm, uint32_t primIndex) |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 252 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 253 | CHECK_OBJ(vsm); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 254 | GET_TLS(); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 255 | Mesh *sm = static_cast<Mesh *>(vsm); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 256 | if (!rsc->setupCheck()) { |
| 257 | return; |
| 258 | } |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 259 | sm->renderPrimitive(rsc, primIndex); |
| 260 | } |
| 261 | |
| 262 | static void SC_drawMeshPrimitiveRange(RsMesh vsm, uint32_t primIndex, uint32_t start, uint32_t len) |
| 263 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 264 | CHECK_OBJ(vsm); |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 265 | GET_TLS(); |
| 266 | Mesh *sm = static_cast<Mesh *>(vsm); |
| 267 | if (!rsc->setupCheck()) { |
| 268 | return; |
| 269 | } |
| 270 | sm->renderPrimitiveRange(rsc, primIndex, start, len); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Alex Sakhartchouk | a80145d | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 273 | static void SC_meshComputeBoundingBox(RsMesh vsm, float *minX, float *minY, float *minZ, |
| 274 | float *maxX, float *maxY, float *maxZ) |
| 275 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 276 | CHECK_OBJ(vsm); |
Alex Sakhartchouk | a80145d | 2010-08-13 14:32:23 -0700 | [diff] [blame] | 277 | GET_TLS(); |
| 278 | Mesh *sm = static_cast<Mesh *>(vsm); |
| 279 | sm->computeBBox(); |
| 280 | *minX = sm->mBBoxMin[0]; |
| 281 | *minY = sm->mBBoxMin[1]; |
| 282 | *minZ = sm->mBBoxMin[2]; |
| 283 | *maxX = sm->mBBoxMax[0]; |
| 284 | *maxY = sm->mBBoxMax[1]; |
| 285 | *maxZ = sm->mBBoxMax[2]; |
| 286 | } |
| 287 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 288 | |
| 289 | ////////////////////////////////////////////////////////////////////////////// |
| 290 | // |
| 291 | ////////////////////////////////////////////////////////////////////////////// |
| 292 | |
| 293 | |
| 294 | static void SC_color(float r, float g, float b, float a) |
| 295 | { |
| 296 | GET_TLS(); |
Jason Sams | 442a647 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 297 | ProgramFragment *pf = (ProgramFragment *)rsc->getFragment(); |
Alex Sakhartchouk | b89aaac | 2010-09-23 16:16:33 -0700 | [diff] [blame] | 298 | pf->setConstantColor(rsc, r, g, b, a); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 301 | static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel) |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 302 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 303 | CHECK_OBJ(va); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 304 | GET_TLS(); |
| 305 | rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel); |
| 306 | } |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 307 | static void SC_uploadToTexture(RsAllocation va) |
| 308 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 309 | CHECK_OBJ(va); |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 310 | GET_TLS(); |
| 311 | rsi_AllocationUploadToTexture(rsc, va, false, 0); |
| 312 | } |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 313 | |
| 314 | static void SC_uploadToBufferObject(RsAllocation va) |
| 315 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 316 | CHECK_OBJ(va); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 317 | GET_TLS(); |
| 318 | rsi_AllocationUploadToBufferObject(rsc, va); |
| 319 | } |
| 320 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 321 | static void SC_ClearColor(float r, float g, float b, float a) |
| 322 | { |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 323 | GET_TLS(); |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 324 | rsc->setupProgramStore(); |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 325 | |
| 326 | glClearColor(r, g, b, a); |
| 327 | glClear(GL_COLOR_BUFFER_BIT); |
| 328 | } |
| 329 | |
| 330 | static void SC_ClearDepth(float v) |
| 331 | { |
| 332 | GET_TLS(); |
Alex Sakhartchouk | feede2a | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 333 | rsc->setupProgramStore(); |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 334 | |
| 335 | glClearDepthf(v); |
| 336 | glClear(GL_DEPTH_BUFFER_BIT); |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static uint32_t SC_getWidth() |
| 340 | { |
| 341 | GET_TLS(); |
| 342 | return rsc->getWidth(); |
| 343 | } |
| 344 | |
| 345 | static uint32_t SC_getHeight() |
| 346 | { |
| 347 | GET_TLS(); |
| 348 | return rsc->getHeight(); |
| 349 | } |
| 350 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 351 | static void SC_DrawTextAlloc(RsAllocation va, int x, int y) |
| 352 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 353 | CHECK_OBJ(va); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 354 | GET_TLS(); |
| 355 | Allocation *alloc = static_cast<Allocation *>(va); |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 356 | const char *text = (const char *)alloc->getPtr(); |
| 357 | size_t allocSize = alloc->getType()->getSizeBytes(); |
| 358 | rsc->mStateFont.renderText(text, allocSize, x, y); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static void SC_DrawText(const char *text, int x, int y) |
| 362 | { |
| 363 | GET_TLS(); |
Alex Sakhartchouk | 10825a0 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 364 | size_t textLen = strlen(text); |
| 365 | rsc->mStateFont.renderText(text, textLen, x, y); |
| 366 | } |
| 367 | |
| 368 | static void SC_setMetrics(Font::Rect *metrics, |
| 369 | int32_t *left, int32_t *right, |
| 370 | int32_t *top, int32_t *bottom) |
| 371 | { |
| 372 | if(left) { |
| 373 | *left = metrics->left; |
| 374 | } |
| 375 | if(right) { |
| 376 | *right = metrics->right; |
| 377 | } |
| 378 | if(top) { |
| 379 | *top = metrics->top; |
| 380 | } |
| 381 | if(bottom) { |
| 382 | *bottom = metrics->bottom; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static void SC_MeasureTextAlloc(RsAllocation va, |
| 387 | int32_t *left, int32_t *right, |
| 388 | int32_t *top, int32_t *bottom) |
| 389 | { |
| 390 | CHECK_OBJ(va); |
| 391 | GET_TLS(); |
| 392 | Allocation *alloc = static_cast<Allocation *>(va); |
| 393 | const char *text = (const char *)alloc->getPtr(); |
| 394 | size_t textLen = alloc->getType()->getSizeBytes(); |
| 395 | Font::Rect metrics; |
| 396 | rsc->mStateFont.measureText(text, textLen, &metrics); |
| 397 | SC_setMetrics(&metrics, left, right, top, bottom); |
| 398 | } |
| 399 | |
| 400 | static void SC_MeasureText(const char *text, |
| 401 | int32_t *left, int32_t *right, |
| 402 | int32_t *top, int32_t *bottom) |
| 403 | { |
| 404 | GET_TLS(); |
| 405 | size_t textLen = strlen(text); |
| 406 | Font::Rect metrics; |
| 407 | rsc->mStateFont.measureText(text, textLen, &metrics); |
| 408 | SC_setMetrics(&metrics, left, right, top, bottom); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static void SC_BindFont(RsFont font) |
| 412 | { |
Jason Sams | f166d9b | 2010-09-30 18:15:52 -0700 | [diff] [blame] | 413 | CHECK_OBJ(font); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 414 | GET_TLS(); |
| 415 | rsi_ContextBindFont(rsc, font); |
| 416 | } |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 417 | |
Alex Sakhartchouk | fb10c16 | 2010-08-04 14:45:48 -0700 | [diff] [blame] | 418 | static void SC_FontColor(float r, float g, float b, float a) |
| 419 | { |
| 420 | GET_TLS(); |
| 421 | rsc->mStateFont.setFontColor(r, g, b, a); |
| 422 | } |
| 423 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 424 | ////////////////////////////////////////////////////////////////////////////// |
| 425 | // Class implementation |
| 426 | ////////////////////////////////////////////////////////////////////////////// |
| 427 | |
| 428 | // llvm name mangling ref |
| 429 | // <builtin-type> ::= v # void |
| 430 | // ::= b # bool |
| 431 | // ::= c # char |
| 432 | // ::= a # signed char |
| 433 | // ::= h # unsigned char |
| 434 | // ::= s # short |
| 435 | // ::= t # unsigned short |
| 436 | // ::= i # int |
| 437 | // ::= j # unsigned int |
| 438 | // ::= l # long |
| 439 | // ::= m # unsigned long |
| 440 | // ::= x # long long, __int64 |
| 441 | // ::= y # unsigned long long, __int64 |
| 442 | // ::= f # float |
| 443 | // ::= d # double |
| 444 | |
| 445 | static ScriptCState::SymbolTable_t gSyms[] = { |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 446 | { "_Z22rsgBindProgramFragment19rs_program_fragment", (void *)&SC_bindProgramFragment, false }, |
| 447 | { "_Z19rsgBindProgramStore16rs_program_store", (void *)&SC_bindProgramStore, false }, |
| 448 | { "_Z20rsgBindProgramVertex17rs_program_vertex", (void *)&SC_bindProgramVertex, false }, |
| 449 | { "_Z20rsgBindProgramRaster17rs_program_raster", (void *)&SC_bindProgramRaster, false }, |
| 450 | { "_Z14rsgBindSampler19rs_program_fragmentj10rs_sampler", (void *)&SC_bindSampler, false }, |
| 451 | { "_Z14rsgBindTexture19rs_program_fragmentj13rs_allocation", (void *)&SC_bindTexture, false }, |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 452 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 453 | { "_Z36rsgProgramVertexLoadProjectionMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadProjectionMatrix, false }, |
| 454 | { "_Z31rsgProgramVertexLoadModelMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadModelMatrix, false }, |
| 455 | { "_Z33rsgProgramVertexLoadTextureMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadTextureMatrix, false }, |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 456 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 457 | { "_Z35rsgProgramVertexGetProjectionMatrixP12rs_matrix4x4", (void *)&SC_vpGetProjectionMatrix, false }, |
Alex Sakhartchouk | cbed752 | 2010-08-16 17:40:10 -0700 | [diff] [blame] | 458 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 459 | { "_Z31rsgProgramFragmentConstantColor19rs_program_fragmentffff", (void *)&SC_pfConstantColor, false }, |
Jason Sams | 442a647 | 2010-08-04 17:50:20 -0700 | [diff] [blame] | 460 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 461 | { "_Z11rsgGetWidthv", (void *)&SC_getWidth, false }, |
| 462 | { "_Z12rsgGetHeightv", (void *)&SC_getHeight, false }, |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 463 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 464 | { "_Z18rsgUploadToTexture13rs_allocationj", (void *)&SC_uploadToTexture2, false }, |
| 465 | { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture, false }, |
| 466 | { "_Z23rsgUploadToBufferObject13rs_allocation", (void *)&SC_uploadToBufferObject, false }, |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 467 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 468 | { "_Z11rsgDrawRectfffff", (void *)&SC_drawRect, false }, |
| 469 | { "_Z11rsgDrawQuadffffffffffff", (void *)&SC_drawQuad, false }, |
| 470 | { "_Z20rsgDrawQuadTexCoordsffffffffffffffffffff", (void *)&SC_drawQuadTexCoords, false }, |
| 471 | { "_Z24rsgDrawSpriteScreenspacefffff", (void *)&SC_drawSpriteScreenspace, false }, |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 472 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 473 | { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh, false }, |
| 474 | { "_Z11rsgDrawMesh7rs_meshj", (void *)&SC_drawMeshPrimitive, false }, |
| 475 | { "_Z11rsgDrawMesh7rs_meshjjj", (void *)&SC_drawMeshPrimitiveRange, false }, |
| 476 | { "_Z25rsgMeshComputeBoundingBox7rs_meshPfS0_S0_S0_S0_S0_", (void *)&SC_meshComputeBoundingBox, false }, |
Alex Sakhartchouk | 164aaed | 2010-07-01 16:14:06 -0700 | [diff] [blame] | 477 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 478 | { "_Z13rsgClearColorffff", (void *)&SC_ClearColor, false }, |
| 479 | { "_Z13rsgClearDepthf", (void *)&SC_ClearDepth, false }, |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 480 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 481 | { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText, false }, |
| 482 | { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc, false }, |
| 483 | { "_Z14rsgMeasureTextPKcPiS1_S1_S1_", (void *)&SC_MeasureText, false }, |
| 484 | { "_Z14rsgMeasureText13rs_allocationPiS0_S0_S0_", (void *)&SC_MeasureTextAlloc, false }, |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 485 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 486 | { "_Z11rsgBindFont7rs_font", (void *)&SC_BindFont, false }, |
| 487 | { "_Z12rsgFontColorffff", (void *)&SC_FontColor, false }, |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 488 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 489 | // misc |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 490 | { "_Z5colorffff", (void *)&SC_color, false }, |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 491 | |
Jason Sams | 8f0adba | 2010-11-01 14:26:30 -0700 | [diff] [blame] | 492 | { NULL, NULL, false } |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 493 | }; |
| 494 | |
| 495 | const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym) |
| 496 | { |
| 497 | ScriptCState::SymbolTable_t *syms = gSyms; |
| 498 | |
| 499 | while (syms->mPtr) { |
| 500 | if (!strcmp(syms->mName, sym)) { |
| 501 | return syms; |
| 502 | } |
| 503 | syms++; |
| 504 | } |
| 505 | return NULL; |
| 506 | } |
| 507 | |