Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
| 3 | * Version: 6.5.3 |
| 4 | * |
| 5 | * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included |
| 15 | * in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * \file prog_execute.c |
| 27 | * Software interpreter for vertex/fragment programs. |
| 28 | * \author Brian Paul |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * NOTE: we do everything in single-precision floating point; we don't |
| 33 | * currently observe the single/half/fixed-precision qualifiers. |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | |
| 38 | #include "glheader.h" |
| 39 | #include "colormac.h" |
| 40 | #include "context.h" |
| 41 | #include "program.h" |
| 42 | #include "prog_execute.h" |
| 43 | #include "prog_instruction.h" |
| 44 | #include "prog_parameter.h" |
| 45 | #include "prog_print.h" |
| 46 | #include "slang_library_noise.h" |
| 47 | |
| 48 | |
| 49 | /* See comments below for info about this */ |
| 50 | #define LAMBDA_ZERO 1 |
| 51 | |
| 52 | /* debug predicate */ |
| 53 | #define DEBUG_PROG 0 |
| 54 | |
| 55 | |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Set x to positive or negative infinity. |
| 58 | */ |
| 59 | #if defined(USE_IEEE) || defined(_WIN32) |
| 60 | #define SET_POS_INFINITY(x) ( *((GLuint *) (void *)&x) = 0x7F800000 ) |
| 61 | #define SET_NEG_INFINITY(x) ( *((GLuint *) (void *)&x) = 0xFF800000 ) |
| 62 | #elif defined(VMS) |
| 63 | #define SET_POS_INFINITY(x) x = __MAXFLOAT |
| 64 | #define SET_NEG_INFINITY(x) x = -__MAXFLOAT |
| 65 | #else |
| 66 | #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL |
| 67 | #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL |
| 68 | #endif |
| 69 | |
| 70 | #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits |
| 71 | |
| 72 | |
| 73 | static const GLfloat ZeroVec[4] = { 0.0F, 0.0F, 0.0F, 0.0F }; |
| 74 | |
| 75 | |
| 76 | |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Return a pointer to the 4-element float vector specified by the given |
| 79 | * source register. |
| 80 | */ |
| 81 | static INLINE const GLfloat * |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 82 | get_register_pointer(const struct prog_src_register *source, |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 83 | const struct gl_program_machine *machine) |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 84 | { |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 85 | if (source->RelAddr) { |
| 86 | const GLint reg = source->Index + machine->AddressReg[0][0]; |
Brian | 313d50e | 2007-02-25 19:01:16 -0700 | [diff] [blame] | 87 | if (source->File == PROGRAM_ENV_PARAM) |
| 88 | if (reg < 0 || reg >= MAX_PROGRAM_ENV_PARAMS) |
| 89 | return ZeroVec; |
| 90 | else |
| 91 | return machine->EnvParams[reg]; |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 92 | else { |
Brian | 313d50e | 2007-02-25 19:01:16 -0700 | [diff] [blame] | 93 | const struct gl_program_parameter_list *params; |
Brian | 761728a | 2007-02-24 11:14:57 -0700 | [diff] [blame] | 94 | ASSERT(source->File == PROGRAM_LOCAL_PARAM || |
| 95 | source->File == PROGRAM_STATE_VAR); |
Brian | 313d50e | 2007-02-25 19:01:16 -0700 | [diff] [blame] | 96 | params = machine->CurProgram->Parameters; |
| 97 | if (reg < 0 || reg >= params->NumParameters) |
| 98 | return ZeroVec; |
| 99 | else |
| 100 | return params->ParameterValues[reg]; |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 104 | switch (source->File) { |
| 105 | case PROGRAM_TEMPORARY: |
| 106 | ASSERT(source->Index < MAX_PROGRAM_TEMPS); |
| 107 | return machine->Temporaries[source->Index]; |
| 108 | |
| 109 | case PROGRAM_INPUT: |
| 110 | if (machine->CurProgram->Target == GL_VERTEX_PROGRAM_ARB) { |
| 111 | ASSERT(source->Index < VERT_ATTRIB_MAX); |
| 112 | return machine->VertAttribs[source->Index]; |
| 113 | } |
| 114 | else { |
| 115 | ASSERT(source->Index < FRAG_ATTRIB_MAX); |
| 116 | return machine->Attribs[source->Index][machine->CurElement]; |
| 117 | } |
| 118 | |
| 119 | case PROGRAM_OUTPUT: |
Brian | 292a804 | 2007-02-24 15:49:54 -0700 | [diff] [blame] | 120 | ASSERT(source->Index < MAX_PROGRAM_OUTPUTS); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 121 | return machine->Outputs[source->Index]; |
| 122 | |
| 123 | case PROGRAM_LOCAL_PARAM: |
| 124 | ASSERT(source->Index < MAX_PROGRAM_LOCAL_PARAMS); |
| 125 | return machine->CurProgram->LocalParams[source->Index]; |
| 126 | |
| 127 | case PROGRAM_ENV_PARAM: |
| 128 | ASSERT(source->Index < MAX_PROGRAM_ENV_PARAMS); |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 129 | return machine->EnvParams[source->Index]; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 130 | |
| 131 | case PROGRAM_STATE_VAR: |
| 132 | /* Fallthrough */ |
| 133 | case PROGRAM_CONSTANT: |
| 134 | /* Fallthrough */ |
| 135 | case PROGRAM_UNIFORM: |
| 136 | /* Fallthrough */ |
| 137 | case PROGRAM_NAMED_PARAM: |
| 138 | ASSERT(source->Index < |
| 139 | (GLint) machine->CurProgram->Parameters->NumParameters); |
| 140 | return machine->CurProgram->Parameters->ParameterValues[source->Index]; |
| 141 | |
| 142 | default: |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 143 | _mesa_problem(NULL, |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 144 | "Invalid input register file %d in get_register_pointer()", |
| 145 | source->File); |
| 146 | return NULL; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
Brian | 6774f32 | 2007-02-25 18:39:46 -0700 | [diff] [blame] | 151 | #if FEATURE_MESA_program_debug |
| 152 | static struct gl_program_machine *CurrentMachine = NULL; |
| 153 | |
| 154 | /** |
| 155 | * For GL_MESA_program_debug. |
| 156 | * Return current value (4*GLfloat) of a program register. |
| 157 | * Called via ctx->Driver.GetProgramRegister(). |
| 158 | */ |
| 159 | void |
| 160 | _mesa_get_program_register(GLcontext *ctx, enum register_file file, |
| 161 | GLuint index, GLfloat val[4]) |
| 162 | { |
| 163 | if (CurrentMachine) { |
| 164 | struct prog_src_register src; |
| 165 | const GLfloat *reg; |
| 166 | src.File = file; |
| 167 | src.Index = index; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 168 | reg = get_register_pointer(&src, CurrentMachine); |
Brian | 6774f32 | 2007-02-25 18:39:46 -0700 | [diff] [blame] | 169 | COPY_4V(val, reg); |
| 170 | } |
| 171 | } |
| 172 | #endif /* FEATURE_MESA_program_debug */ |
| 173 | |
| 174 | |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Fetch a 4-element float vector from the given source register. |
| 177 | * Apply swizzling and negating as needed. |
| 178 | */ |
| 179 | static void |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 180 | fetch_vector4(const struct prog_src_register *source, |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 181 | const struct gl_program_machine *machine, GLfloat result[4]) |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 182 | { |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 183 | const GLfloat *src = get_register_pointer(source, machine); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 184 | ASSERT(src); |
| 185 | |
| 186 | if (source->Swizzle == SWIZZLE_NOOP) { |
| 187 | /* no swizzling */ |
| 188 | COPY_4V(result, src); |
| 189 | } |
| 190 | else { |
| 191 | ASSERT(GET_SWZ(source->Swizzle, 0) <= 3); |
| 192 | ASSERT(GET_SWZ(source->Swizzle, 1) <= 3); |
| 193 | ASSERT(GET_SWZ(source->Swizzle, 2) <= 3); |
| 194 | ASSERT(GET_SWZ(source->Swizzle, 3) <= 3); |
| 195 | result[0] = src[GET_SWZ(source->Swizzle, 0)]; |
| 196 | result[1] = src[GET_SWZ(source->Swizzle, 1)]; |
| 197 | result[2] = src[GET_SWZ(source->Swizzle, 2)]; |
| 198 | result[3] = src[GET_SWZ(source->Swizzle, 3)]; |
| 199 | } |
| 200 | |
| 201 | if (source->NegateBase) { |
| 202 | result[0] = -result[0]; |
| 203 | result[1] = -result[1]; |
| 204 | result[2] = -result[2]; |
| 205 | result[3] = -result[3]; |
| 206 | } |
| 207 | if (source->Abs) { |
| 208 | result[0] = FABSF(result[0]); |
| 209 | result[1] = FABSF(result[1]); |
| 210 | result[2] = FABSF(result[2]); |
| 211 | result[3] = FABSF(result[3]); |
| 212 | } |
| 213 | if (source->NegateAbs) { |
| 214 | result[0] = -result[0]; |
| 215 | result[1] = -result[1]; |
| 216 | result[2] = -result[2]; |
| 217 | result[3] = -result[3]; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | #if 0 |
| 222 | /** |
| 223 | * Fetch the derivative with respect to X for the given register. |
| 224 | * \return GL_TRUE if it was easily computed or GL_FALSE if we |
| 225 | * need to execute another instance of the program (ugh)! |
| 226 | */ |
| 227 | static GLboolean |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 228 | fetch_vector4_deriv(GLcontext * ctx, |
| 229 | const struct prog_src_register *source, |
| 230 | const SWspan * span, |
| 231 | char xOrY, GLint column, GLfloat result[4]) |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 232 | { |
| 233 | GLfloat src[4]; |
| 234 | |
| 235 | ASSERT(xOrY == 'X' || xOrY == 'Y'); |
| 236 | |
| 237 | switch (source->Index) { |
| 238 | case FRAG_ATTRIB_WPOS: |
| 239 | if (xOrY == 'X') { |
| 240 | src[0] = 1.0; |
| 241 | src[1] = 0.0; |
| 242 | src[2] = span->attrStepX[FRAG_ATTRIB_WPOS][2] |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 243 | / ctx->DrawBuffer->_DepthMaxF; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 244 | src[3] = span->attrStepX[FRAG_ATTRIB_WPOS][3]; |
| 245 | } |
| 246 | else { |
| 247 | src[0] = 0.0; |
| 248 | src[1] = 1.0; |
| 249 | src[2] = span->attrStepY[FRAG_ATTRIB_WPOS][2] |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 250 | / ctx->DrawBuffer->_DepthMaxF; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 251 | src[3] = span->attrStepY[FRAG_ATTRIB_WPOS][3]; |
| 252 | } |
| 253 | break; |
| 254 | case FRAG_ATTRIB_COL0: |
| 255 | case FRAG_ATTRIB_COL1: |
| 256 | if (xOrY == 'X') { |
| 257 | src[0] = span->attrStepX[source->Index][0] * (1.0F / CHAN_MAXF); |
| 258 | src[1] = span->attrStepX[source->Index][1] * (1.0F / CHAN_MAXF); |
| 259 | src[2] = span->attrStepX[source->Index][2] * (1.0F / CHAN_MAXF); |
| 260 | src[3] = span->attrStepX[source->Index][3] * (1.0F / CHAN_MAXF); |
| 261 | } |
| 262 | else { |
| 263 | src[0] = span->attrStepY[source->Index][0] * (1.0F / CHAN_MAXF); |
| 264 | src[1] = span->attrStepY[source->Index][1] * (1.0F / CHAN_MAXF); |
| 265 | src[2] = span->attrStepY[source->Index][2] * (1.0F / CHAN_MAXF); |
| 266 | src[3] = span->attrStepY[source->Index][3] * (1.0F / CHAN_MAXF); |
| 267 | } |
| 268 | break; |
| 269 | case FRAG_ATTRIB_FOGC: |
| 270 | if (xOrY == 'X') { |
| 271 | src[0] = span->attrStepX[FRAG_ATTRIB_FOGC][0] * (1.0F / CHAN_MAXF); |
| 272 | src[1] = 0.0; |
| 273 | src[2] = 0.0; |
| 274 | src[3] = 0.0; |
| 275 | } |
| 276 | else { |
| 277 | src[0] = span->attrStepY[FRAG_ATTRIB_FOGC][0] * (1.0F / CHAN_MAXF); |
| 278 | src[1] = 0.0; |
| 279 | src[2] = 0.0; |
| 280 | src[3] = 0.0; |
| 281 | } |
| 282 | break; |
| 283 | default: |
| 284 | assert(source->Index < FRAG_ATTRIB_MAX); |
| 285 | /* texcoord or varying */ |
| 286 | if (xOrY == 'X') { |
| 287 | /* this is a little tricky - I think I've got it right */ |
| 288 | const GLfloat invQ = 1.0f / (span->attrStart[source->Index][3] |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 289 | + |
| 290 | span->attrStepX[source->Index][3] * |
| 291 | column); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 292 | src[0] = span->attrStepX[source->Index][0] * invQ; |
| 293 | src[1] = span->attrStepX[source->Index][1] * invQ; |
| 294 | src[2] = span->attrStepX[source->Index][2] * invQ; |
| 295 | src[3] = span->attrStepX[source->Index][3] * invQ; |
| 296 | } |
| 297 | else { |
| 298 | /* Tricky, as above, but in Y direction */ |
| 299 | const GLfloat invQ = 1.0f / (span->attrStart[source->Index][3] |
| 300 | + span->attrStepY[source->Index][3]); |
| 301 | src[0] = span->attrStepY[source->Index][0] * invQ; |
| 302 | src[1] = span->attrStepY[source->Index][1] * invQ; |
| 303 | src[2] = span->attrStepY[source->Index][2] * invQ; |
| 304 | src[3] = span->attrStepY[source->Index][3] * invQ; |
| 305 | } |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | result[0] = src[GET_SWZ(source->Swizzle, 0)]; |
| 310 | result[1] = src[GET_SWZ(source->Swizzle, 1)]; |
| 311 | result[2] = src[GET_SWZ(source->Swizzle, 2)]; |
| 312 | result[3] = src[GET_SWZ(source->Swizzle, 3)]; |
| 313 | |
| 314 | if (source->NegateBase) { |
| 315 | result[0] = -result[0]; |
| 316 | result[1] = -result[1]; |
| 317 | result[2] = -result[2]; |
| 318 | result[3] = -result[3]; |
| 319 | } |
| 320 | if (source->Abs) { |
| 321 | result[0] = FABSF(result[0]); |
| 322 | result[1] = FABSF(result[1]); |
| 323 | result[2] = FABSF(result[2]); |
| 324 | result[3] = FABSF(result[3]); |
| 325 | } |
| 326 | if (source->NegateAbs) { |
| 327 | result[0] = -result[0]; |
| 328 | result[1] = -result[1]; |
| 329 | result[2] = -result[2]; |
| 330 | result[3] = -result[3]; |
| 331 | } |
| 332 | return GL_TRUE; |
| 333 | } |
| 334 | #endif |
| 335 | |
| 336 | |
| 337 | /** |
| 338 | * As above, but only return result[0] element. |
| 339 | */ |
| 340 | static void |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 341 | fetch_vector1(const struct prog_src_register *source, |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 342 | const struct gl_program_machine *machine, GLfloat result[4]) |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 343 | { |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 344 | const GLfloat *src = get_register_pointer(source, machine); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 345 | ASSERT(src); |
| 346 | |
| 347 | result[0] = src[GET_SWZ(source->Swizzle, 0)]; |
| 348 | |
| 349 | if (source->NegateBase) { |
| 350 | result[0] = -result[0]; |
| 351 | } |
| 352 | if (source->Abs) { |
| 353 | result[0] = FABSF(result[0]); |
| 354 | } |
| 355 | if (source->NegateAbs) { |
| 356 | result[0] = -result[0]; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /** |
| 362 | * Test value against zero and return GT, LT, EQ or UN if NaN. |
| 363 | */ |
| 364 | static INLINE GLuint |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 365 | generate_cc(float value) |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 366 | { |
| 367 | if (value != value) |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 368 | return COND_UN; /* NaN */ |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 369 | if (value > 0.0F) |
| 370 | return COND_GT; |
| 371 | if (value < 0.0F) |
| 372 | return COND_LT; |
| 373 | return COND_EQ; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | /** |
| 378 | * Test if the ccMaskRule is satisfied by the given condition code. |
| 379 | * Used to mask destination writes according to the current condition code. |
| 380 | */ |
| 381 | static INLINE GLboolean |
| 382 | test_cc(GLuint condCode, GLuint ccMaskRule) |
| 383 | { |
| 384 | switch (ccMaskRule) { |
| 385 | case COND_EQ: return (condCode == COND_EQ); |
| 386 | case COND_NE: return (condCode != COND_EQ); |
| 387 | case COND_LT: return (condCode == COND_LT); |
| 388 | case COND_GE: return (condCode == COND_GT || condCode == COND_EQ); |
| 389 | case COND_LE: return (condCode == COND_LT || condCode == COND_EQ); |
| 390 | case COND_GT: return (condCode == COND_GT); |
| 391 | case COND_TR: return GL_TRUE; |
| 392 | case COND_FL: return GL_FALSE; |
| 393 | default: return GL_TRUE; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /** |
| 399 | * Evaluate the 4 condition codes against a predicate and return GL_TRUE |
| 400 | * or GL_FALSE to indicate result. |
| 401 | */ |
| 402 | static INLINE GLboolean |
| 403 | eval_condition(const struct gl_program_machine *machine, |
| 404 | const struct prog_instruction *inst) |
| 405 | { |
| 406 | const GLuint swizzle = inst->DstReg.CondSwizzle; |
| 407 | const GLuint condMask = inst->DstReg.CondMask; |
| 408 | if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) || |
| 409 | test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) || |
| 410 | test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) || |
| 411 | test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) { |
| 412 | return GL_TRUE; |
| 413 | } |
| 414 | else { |
| 415 | return GL_FALSE; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | |
| 420 | |
| 421 | /** |
| 422 | * Store 4 floats into a register. Observe the instructions saturate and |
| 423 | * set-condition-code flags. |
| 424 | */ |
| 425 | static void |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 426 | store_vector4(const struct prog_instruction *inst, |
| 427 | struct gl_program_machine *machine, const GLfloat value[4]) |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 428 | { |
| 429 | const struct prog_dst_register *dest = &(inst->DstReg); |
| 430 | const GLboolean clamp = inst->SaturateMode == SATURATE_ZERO_ONE; |
| 431 | GLfloat *dstReg; |
| 432 | GLfloat dummyReg[4]; |
| 433 | GLfloat clampedValue[4]; |
| 434 | GLuint writeMask = dest->WriteMask; |
| 435 | |
| 436 | switch (dest->File) { |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 437 | case PROGRAM_OUTPUT: |
Brian | 292a804 | 2007-02-24 15:49:54 -0700 | [diff] [blame] | 438 | ASSERT(dest->Index < MAX_PROGRAM_OUTPUTS); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 439 | dstReg = machine->Outputs[dest->Index]; |
| 440 | break; |
| 441 | case PROGRAM_TEMPORARY: |
Brian | 292a804 | 2007-02-24 15:49:54 -0700 | [diff] [blame] | 442 | ASSERT(dest->Index < MAX_PROGRAM_TEMPS); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 443 | dstReg = machine->Temporaries[dest->Index]; |
| 444 | break; |
| 445 | case PROGRAM_WRITE_ONLY: |
| 446 | dstReg = dummyReg; |
| 447 | return; |
| 448 | default: |
| 449 | _mesa_problem(NULL, "bad register file in store_vector4(fp)"); |
| 450 | return; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | #if 0 |
| 454 | if (value[0] > 1.0e10 || |
| 455 | IS_INF_OR_NAN(value[0]) || |
| 456 | IS_INF_OR_NAN(value[1]) || |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 457 | IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3])) |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 458 | printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]); |
| 459 | #endif |
| 460 | |
| 461 | if (clamp) { |
| 462 | clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F); |
| 463 | clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F); |
| 464 | clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F); |
| 465 | clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F); |
| 466 | value = clampedValue; |
| 467 | } |
| 468 | |
| 469 | if (dest->CondMask != COND_TR) { |
| 470 | /* condition codes may turn off some writes */ |
| 471 | if (writeMask & WRITEMASK_X) { |
| 472 | if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 0)], |
| 473 | dest->CondMask)) |
| 474 | writeMask &= ~WRITEMASK_X; |
| 475 | } |
| 476 | if (writeMask & WRITEMASK_Y) { |
| 477 | if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 1)], |
| 478 | dest->CondMask)) |
| 479 | writeMask &= ~WRITEMASK_Y; |
| 480 | } |
| 481 | if (writeMask & WRITEMASK_Z) { |
| 482 | if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 2)], |
| 483 | dest->CondMask)) |
| 484 | writeMask &= ~WRITEMASK_Z; |
| 485 | } |
| 486 | if (writeMask & WRITEMASK_W) { |
| 487 | if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 3)], |
| 488 | dest->CondMask)) |
| 489 | writeMask &= ~WRITEMASK_W; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (writeMask & WRITEMASK_X) |
| 494 | dstReg[0] = value[0]; |
| 495 | if (writeMask & WRITEMASK_Y) |
| 496 | dstReg[1] = value[1]; |
| 497 | if (writeMask & WRITEMASK_Z) |
| 498 | dstReg[2] = value[2]; |
| 499 | if (writeMask & WRITEMASK_W) |
| 500 | dstReg[3] = value[3]; |
| 501 | |
| 502 | if (inst->CondUpdate) { |
| 503 | if (writeMask & WRITEMASK_X) |
| 504 | machine->CondCodes[0] = generate_cc(value[0]); |
| 505 | if (writeMask & WRITEMASK_Y) |
| 506 | machine->CondCodes[1] = generate_cc(value[1]); |
| 507 | if (writeMask & WRITEMASK_Z) |
| 508 | machine->CondCodes[2] = generate_cc(value[2]); |
| 509 | if (writeMask & WRITEMASK_W) |
| 510 | machine->CondCodes[3] = generate_cc(value[3]); |
Brian | a01616e | 2007-03-28 11:01:28 -0600 | [diff] [blame] | 511 | #if DEBUG_PROG |
| 512 | printf("CondCodes=(%s,%s,%s,%s) for:\n", |
| 513 | _mesa_condcode_string(machine->CondCodes[0]), |
| 514 | _mesa_condcode_string(machine->CondCodes[1]), |
| 515 | _mesa_condcode_string(machine->CondCodes[2]), |
| 516 | _mesa_condcode_string(machine->CondCodes[3])); |
| 517 | #endif |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | |
| 522 | #if 0 |
| 523 | /** |
| 524 | * Initialize a new machine state instance from an existing one, adding |
| 525 | * the partial derivatives onto the input registers. |
| 526 | * Used to implement DDX and DDY instructions in non-trivial cases. |
| 527 | */ |
| 528 | static void |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 529 | init_machine_deriv(GLcontext * ctx, |
| 530 | const struct gl_program_machine *machine, |
| 531 | const struct gl_fragment_program *program, |
| 532 | const SWspan * span, char xOrY, |
| 533 | struct gl_program_machine *dMachine) |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 534 | { |
| 535 | GLuint attr; |
| 536 | |
| 537 | ASSERT(xOrY == 'X' || xOrY == 'Y'); |
| 538 | |
| 539 | /* copy existing machine */ |
| 540 | _mesa_memcpy(dMachine, machine, sizeof(struct gl_program_machine)); |
| 541 | |
| 542 | if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) { |
| 543 | /* XXX also need to do this when using valgrind */ |
| 544 | /* Clear temporary registers (undefined for ARB_f_p) */ |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 545 | _mesa_bzero((void *) machine->Temporaries, |
| 546 | MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat)); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* Add derivatives */ |
| 550 | if (program->Base.InputsRead & FRAG_BIT_WPOS) { |
| 551 | GLfloat *wpos = machine->Attribs[FRAG_ATTRIB_WPOS][machine->CurElement]; |
| 552 | if (xOrY == 'X') { |
| 553 | wpos[0] += 1.0F; |
| 554 | wpos[1] += 0.0F; |
| 555 | wpos[2] += span->attrStepX[FRAG_ATTRIB_WPOS][2]; |
| 556 | wpos[3] += span->attrStepX[FRAG_ATTRIB_WPOS][3]; |
| 557 | } |
| 558 | else { |
| 559 | wpos[0] += 0.0F; |
| 560 | wpos[1] += 1.0F; |
| 561 | wpos[2] += span->attrStepY[FRAG_ATTRIB_WPOS][2]; |
| 562 | wpos[3] += span->attrStepY[FRAG_ATTRIB_WPOS][3]; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /* primary, secondary colors */ |
| 567 | for (attr = FRAG_ATTRIB_COL0; attr <= FRAG_ATTRIB_COL1; attr++) { |
| 568 | if (program->Base.InputsRead & (1 << attr)) { |
| 569 | GLfloat *col = machine->Attribs[attr][machine->CurElement]; |
| 570 | if (xOrY == 'X') { |
| 571 | col[0] += span->attrStepX[attr][0] * (1.0F / CHAN_MAXF); |
| 572 | col[1] += span->attrStepX[attr][1] * (1.0F / CHAN_MAXF); |
| 573 | col[2] += span->attrStepX[attr][2] * (1.0F / CHAN_MAXF); |
| 574 | col[3] += span->attrStepX[attr][3] * (1.0F / CHAN_MAXF); |
| 575 | } |
| 576 | else { |
| 577 | col[0] += span->attrStepY[attr][0] * (1.0F / CHAN_MAXF); |
| 578 | col[1] += span->attrStepY[attr][1] * (1.0F / CHAN_MAXF); |
| 579 | col[2] += span->attrStepY[attr][2] * (1.0F / CHAN_MAXF); |
| 580 | col[3] += span->attrStepY[attr][3] * (1.0F / CHAN_MAXF); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | if (program->Base.InputsRead & FRAG_BIT_FOGC) { |
| 585 | GLfloat *fogc = machine->Attribs[FRAG_ATTRIB_FOGC][machine->CurElement]; |
| 586 | if (xOrY == 'X') { |
| 587 | fogc[0] += span->attrStepX[FRAG_ATTRIB_FOGC][0]; |
| 588 | } |
| 589 | else { |
| 590 | fogc[0] += span->attrStepY[FRAG_ATTRIB_FOGC][0]; |
| 591 | } |
| 592 | } |
| 593 | /* texcoord and varying vars */ |
| 594 | for (attr = FRAG_ATTRIB_TEX0; attr < FRAG_ATTRIB_MAX; attr++) { |
| 595 | if (program->Base.InputsRead & (1 << attr)) { |
| 596 | GLfloat *val = machine->Attribs[attr][machine->CurElement]; |
| 597 | /* XXX perspective-correct interpolation */ |
| 598 | if (xOrY == 'X') { |
| 599 | val[0] += span->attrStepX[attr][0]; |
| 600 | val[1] += span->attrStepX[attr][1]; |
| 601 | val[2] += span->attrStepX[attr][2]; |
| 602 | val[3] += span->attrStepX[attr][3]; |
| 603 | } |
| 604 | else { |
| 605 | val[0] += span->attrStepY[attr][0]; |
| 606 | val[1] += span->attrStepY[attr][1]; |
| 607 | val[2] += span->attrStepY[attr][2]; |
| 608 | val[3] += span->attrStepY[attr][3]; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* init condition codes */ |
| 614 | dMachine->CondCodes[0] = COND_EQ; |
| 615 | dMachine->CondCodes[1] = COND_EQ; |
| 616 | dMachine->CondCodes[2] = COND_EQ; |
| 617 | dMachine->CondCodes[3] = COND_EQ; |
| 618 | } |
| 619 | #endif |
| 620 | |
| 621 | |
| 622 | /** |
| 623 | * Execute the given vertex/fragment program. |
| 624 | * |
Brian | 3c1c999 | 2007-02-25 19:11:44 -0700 | [diff] [blame] | 625 | * \param ctx rendering context |
| 626 | * \param program the program to execute |
| 627 | * \param machine machine state (must be initialized) |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 628 | * \return GL_TRUE if program completed or GL_FALSE if program executed KIL. |
| 629 | */ |
| 630 | GLboolean |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 631 | _mesa_execute_program(GLcontext * ctx, |
Brian | 8b34b7d | 2007-02-25 18:26:50 -0700 | [diff] [blame] | 632 | const struct gl_program *program, |
Brian | 085d7d5 | 2007-02-25 18:23:37 -0700 | [diff] [blame] | 633 | struct gl_program_machine *machine) |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 634 | { |
Brian | 8b34b7d | 2007-02-25 18:26:50 -0700 | [diff] [blame] | 635 | const GLuint numInst = program->NumInstructions; |
Brian | cfd0011 | 2007-02-25 18:30:45 -0700 | [diff] [blame] | 636 | const GLuint maxExec = 10000; |
| 637 | GLint pc, numExec = 0; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 638 | |
| 639 | machine->CurProgram = program; |
| 640 | |
| 641 | if (DEBUG_PROG) { |
| 642 | printf("execute program %u --------------------\n", program->Id); |
| 643 | } |
| 644 | |
| 645 | #if FEATURE_MESA_program_debug |
| 646 | CurrentMachine = machine; |
| 647 | #endif |
| 648 | |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 649 | if (program->Target == GL_VERTEX_PROGRAM_ARB) { |
| 650 | machine->EnvParams = ctx->VertexProgram.Parameters; |
| 651 | } |
| 652 | else { |
| 653 | machine->EnvParams = ctx->FragmentProgram.Parameters; |
| 654 | } |
| 655 | |
Brian | 8b34b7d | 2007-02-25 18:26:50 -0700 | [diff] [blame] | 656 | for (pc = 0; pc < numInst; pc++) { |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 657 | const struct prog_instruction *inst = program->Instructions + pc; |
| 658 | |
| 659 | #if FEATURE_MESA_program_debug |
| 660 | if (ctx->FragmentProgram.CallbackEnabled && |
| 661 | ctx->FragmentProgram.Callback) { |
| 662 | ctx->FragmentProgram.CurrentPosition = inst->StringPos; |
| 663 | ctx->FragmentProgram.Callback(program->Target, |
| 664 | ctx->FragmentProgram.CallbackData); |
| 665 | } |
| 666 | #endif |
| 667 | |
| 668 | if (DEBUG_PROG) { |
| 669 | _mesa_print_instruction(inst); |
| 670 | } |
| 671 | |
| 672 | switch (inst->Opcode) { |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 673 | case OPCODE_ABS: |
| 674 | { |
| 675 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 676 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 677 | result[0] = FABSF(a[0]); |
| 678 | result[1] = FABSF(a[1]); |
| 679 | result[2] = FABSF(a[2]); |
| 680 | result[3] = FABSF(a[3]); |
| 681 | store_vector4(inst, machine, result); |
| 682 | } |
| 683 | break; |
| 684 | case OPCODE_ADD: |
| 685 | { |
| 686 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 687 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 688 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 689 | result[0] = a[0] + b[0]; |
| 690 | result[1] = a[1] + b[1]; |
| 691 | result[2] = a[2] + b[2]; |
| 692 | result[3] = a[3] + b[3]; |
| 693 | store_vector4(inst, machine, result); |
| 694 | if (DEBUG_PROG) { |
| 695 | printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n", |
| 696 | result[0], result[1], result[2], result[3], |
| 697 | a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 698 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 699 | } |
| 700 | break; |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 701 | case OPCODE_ARL: |
| 702 | { |
| 703 | GLfloat t[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 704 | fetch_vector4(&inst->SrcReg[0], machine, t); |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 705 | machine->AddressReg[0][0] = (GLint) FLOORF(t[0]); |
| 706 | } |
| 707 | break; |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 708 | case OPCODE_BGNLOOP: |
| 709 | /* no-op */ |
| 710 | break; |
| 711 | case OPCODE_ENDLOOP: |
| 712 | /* subtract 1 here since pc is incremented by for(pc) loop */ |
| 713 | pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */ |
| 714 | break; |
| 715 | case OPCODE_BGNSUB: /* begin subroutine */ |
| 716 | break; |
| 717 | case OPCODE_ENDSUB: /* end subroutine */ |
| 718 | break; |
| 719 | case OPCODE_BRA: /* branch (conditional) */ |
| 720 | /* fall-through */ |
| 721 | case OPCODE_BRK: /* break out of loop (conditional) */ |
| 722 | /* fall-through */ |
| 723 | case OPCODE_CONT: /* continue loop (conditional) */ |
| 724 | if (eval_condition(machine, inst)) { |
| 725 | /* take branch */ |
| 726 | /* Subtract 1 here since we'll do pc++ at end of for-loop */ |
| 727 | pc = inst->BranchTarget - 1; |
| 728 | } |
| 729 | break; |
| 730 | case OPCODE_CAL: /* Call subroutine (conditional) */ |
| 731 | if (eval_condition(machine, inst)) { |
| 732 | /* call the subroutine */ |
| 733 | if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) { |
| 734 | return GL_TRUE; /* Per GL_NV_vertex_program2 spec */ |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 735 | } |
Brian | a0275b0 | 2007-03-27 11:02:20 -0600 | [diff] [blame] | 736 | machine->CallStack[machine->StackDepth++] = pc + 1; /* next inst */ |
Brian | 31dc7a3 | 2007-03-27 15:21:35 -0600 | [diff] [blame] | 737 | /* Subtract 1 here since we'll do pc++ at end of for-loop */ |
| 738 | pc = inst->BranchTarget - 1; |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 739 | } |
| 740 | break; |
| 741 | case OPCODE_CMP: |
| 742 | { |
| 743 | GLfloat a[4], b[4], c[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 744 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 745 | fetch_vector4(&inst->SrcReg[1], machine, b); |
| 746 | fetch_vector4(&inst->SrcReg[2], machine, c); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 747 | result[0] = a[0] < 0.0F ? b[0] : c[0]; |
| 748 | result[1] = a[1] < 0.0F ? b[1] : c[1]; |
| 749 | result[2] = a[2] < 0.0F ? b[2] : c[2]; |
| 750 | result[3] = a[3] < 0.0F ? b[3] : c[3]; |
| 751 | store_vector4(inst, machine, result); |
| 752 | } |
| 753 | break; |
| 754 | case OPCODE_COS: |
| 755 | { |
| 756 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 757 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 758 | result[0] = result[1] = result[2] = result[3] |
| 759 | = (GLfloat) _mesa_cos(a[0]); |
| 760 | store_vector4(inst, machine, result); |
| 761 | } |
| 762 | break; |
| 763 | case OPCODE_DDX: /* Partial derivative with respect to X */ |
| 764 | { |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 765 | #if 0 |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 766 | GLfloat a[4], aNext[4], result[4]; |
| 767 | struct gl_program_machine dMachine; |
| 768 | if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'X', |
| 769 | column, result)) { |
| 770 | /* This is tricky. Make a copy of the current machine state, |
| 771 | * increment the input registers by the dx or dy partial |
| 772 | * derivatives, then re-execute the program up to the |
| 773 | * preceeding instruction, then fetch the source register. |
| 774 | * Finally, find the difference in the register values for |
| 775 | * the original and derivative runs. |
| 776 | */ |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 777 | fetch_vector4(&inst->SrcReg[0], machine, program, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 778 | init_machine_deriv(ctx, machine, program, span, |
| 779 | 'X', &dMachine); |
| 780 | execute_program(ctx, program, pc, &dMachine, span, column); |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 781 | fetch_vector4(&inst->SrcReg[0], &dMachine, program, |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 782 | aNext); |
| 783 | result[0] = aNext[0] - a[0]; |
| 784 | result[1] = aNext[1] - a[1]; |
| 785 | result[2] = aNext[2] - a[2]; |
| 786 | result[3] = aNext[3] - a[3]; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 787 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 788 | store_vector4(inst, machine, result); |
| 789 | #else |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 790 | store_vector4(inst, machine, ZeroVec); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 791 | #endif |
| 792 | } |
| 793 | break; |
| 794 | case OPCODE_DDY: /* Partial derivative with respect to Y */ |
| 795 | { |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 796 | #if 0 |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 797 | GLfloat a[4], aNext[4], result[4]; |
| 798 | struct gl_program_machine dMachine; |
| 799 | if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'Y', |
| 800 | column, result)) { |
| 801 | init_machine_deriv(ctx, machine, program, span, |
| 802 | 'Y', &dMachine); |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 803 | fetch_vector4(&inst->SrcReg[0], machine, program, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 804 | execute_program(ctx, program, pc, &dMachine, span, column); |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 805 | fetch_vector4(&inst->SrcReg[0], &dMachine, program, |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 806 | aNext); |
| 807 | result[0] = aNext[0] - a[0]; |
| 808 | result[1] = aNext[1] - a[1]; |
| 809 | result[2] = aNext[2] - a[2]; |
| 810 | result[3] = aNext[3] - a[3]; |
| 811 | } |
| 812 | store_vector4(inst, machine, result); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 813 | #else |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 814 | store_vector4(inst, machine, ZeroVec); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 815 | #endif |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 816 | } |
| 817 | break; |
| 818 | case OPCODE_DP3: |
| 819 | { |
| 820 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 821 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 822 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 823 | result[0] = result[1] = result[2] = result[3] = DOT3(a, b); |
| 824 | store_vector4(inst, machine, result); |
| 825 | if (DEBUG_PROG) { |
| 826 | printf("DP3 %g = (%g %g %g) . (%g %g %g)\n", |
| 827 | result[0], a[0], a[1], a[2], b[0], b[1], b[2]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 828 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 829 | } |
| 830 | break; |
| 831 | case OPCODE_DP4: |
| 832 | { |
| 833 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 834 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 835 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 836 | result[0] = result[1] = result[2] = result[3] = DOT4(a, b); |
| 837 | store_vector4(inst, machine, result); |
| 838 | if (DEBUG_PROG) { |
| 839 | printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n", |
| 840 | result[0], a[0], a[1], a[2], a[3], |
| 841 | b[0], b[1], b[2], b[3]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 842 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 843 | } |
| 844 | break; |
| 845 | case OPCODE_DPH: |
| 846 | { |
| 847 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 848 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 849 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 850 | result[0] = result[1] = result[2] = result[3] = |
| 851 | a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + b[3]; |
| 852 | store_vector4(inst, machine, result); |
| 853 | } |
| 854 | break; |
| 855 | case OPCODE_DST: /* Distance vector */ |
| 856 | { |
| 857 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 858 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 859 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 860 | result[0] = 1.0F; |
| 861 | result[1] = a[1] * b[1]; |
| 862 | result[2] = a[2]; |
| 863 | result[3] = b[3]; |
| 864 | store_vector4(inst, machine, result); |
| 865 | } |
| 866 | break; |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 867 | case OPCODE_EXP: |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 868 | { |
| 869 | GLfloat t[4], q[4], floor_t0; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 870 | fetch_vector1(&inst->SrcReg[0], machine, t); |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 871 | floor_t0 = FLOORF(t[0]); |
| 872 | if (floor_t0 > FLT_MAX_EXP) { |
| 873 | SET_POS_INFINITY(q[0]); |
| 874 | SET_POS_INFINITY(q[2]); |
| 875 | } |
| 876 | else if (floor_t0 < FLT_MIN_EXP) { |
| 877 | q[0] = 0.0F; |
| 878 | q[2] = 0.0F; |
| 879 | } |
| 880 | else { |
Brian | 761728a | 2007-02-24 11:14:57 -0700 | [diff] [blame] | 881 | q[0] = LDEXPF(1.0, (int) floor_t0); |
| 882 | /* Note: GL_NV_vertex_program expects |
| 883 | * result.z = result.x * APPX(result.y) |
| 884 | * We do what the ARB extension says. |
| 885 | */ |
| 886 | q[2] = pow(2.0, t[0]); |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 887 | } |
| 888 | q[1] = t[0] - floor_t0; |
| 889 | q[3] = 1.0F; |
| 890 | store_vector4( inst, machine, q ); |
| 891 | } |
| 892 | break; |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 893 | case OPCODE_EX2: /* Exponential base 2 */ |
| 894 | { |
| 895 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 896 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 897 | result[0] = result[1] = result[2] = result[3] = |
| 898 | (GLfloat) _mesa_pow(2.0, a[0]); |
| 899 | store_vector4(inst, machine, result); |
| 900 | } |
| 901 | break; |
| 902 | case OPCODE_FLR: |
| 903 | { |
| 904 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 905 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 906 | result[0] = FLOORF(a[0]); |
| 907 | result[1] = FLOORF(a[1]); |
| 908 | result[2] = FLOORF(a[2]); |
| 909 | result[3] = FLOORF(a[3]); |
| 910 | store_vector4(inst, machine, result); |
| 911 | } |
| 912 | break; |
| 913 | case OPCODE_FRC: |
| 914 | { |
| 915 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 916 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 917 | result[0] = a[0] - FLOORF(a[0]); |
| 918 | result[1] = a[1] - FLOORF(a[1]); |
| 919 | result[2] = a[2] - FLOORF(a[2]); |
| 920 | result[3] = a[3] - FLOORF(a[3]); |
| 921 | store_vector4(inst, machine, result); |
| 922 | } |
| 923 | break; |
| 924 | case OPCODE_IF: |
Brian | 63556fa | 2007-03-23 14:47:46 -0600 | [diff] [blame] | 925 | { |
| 926 | GLboolean cond; |
| 927 | /* eval condition */ |
| 928 | if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) { |
| 929 | GLfloat a[4]; |
| 930 | fetch_vector1(&inst->SrcReg[0], machine, a); |
| 931 | cond = (a[0] != 0.0); |
| 932 | } |
| 933 | else { |
| 934 | cond = eval_condition(machine, inst); |
| 935 | } |
Brian | a7f7366 | 2007-04-20 08:11:51 -0600 | [diff] [blame^] | 936 | if (DEBUG_PROG) { |
| 937 | printf("IF: %d\n", cond); |
| 938 | } |
Brian | 63556fa | 2007-03-23 14:47:46 -0600 | [diff] [blame] | 939 | /* do if/else */ |
| 940 | if (cond) { |
| 941 | /* do if-clause (just continue execution) */ |
| 942 | } |
| 943 | else { |
| 944 | /* go to the instruction after ELSE or ENDIF */ |
| 945 | assert(inst->BranchTarget >= 0); |
| 946 | pc = inst->BranchTarget - 1; |
| 947 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 948 | } |
| 949 | break; |
| 950 | case OPCODE_ELSE: |
| 951 | /* goto ENDIF */ |
| 952 | assert(inst->BranchTarget >= 0); |
| 953 | pc = inst->BranchTarget - 1; |
| 954 | break; |
| 955 | case OPCODE_ENDIF: |
| 956 | /* nothing */ |
| 957 | break; |
| 958 | case OPCODE_INT: /* float to int */ |
| 959 | { |
| 960 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 961 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 962 | result[0] = (GLfloat) (GLint) a[0]; |
| 963 | result[1] = (GLfloat) (GLint) a[1]; |
| 964 | result[2] = (GLfloat) (GLint) a[2]; |
| 965 | result[3] = (GLfloat) (GLint) a[3]; |
| 966 | store_vector4(inst, machine, result); |
| 967 | } |
| 968 | break; |
| 969 | case OPCODE_KIL_NV: /* NV_f_p only (conditional) */ |
| 970 | if (eval_condition(machine, inst)) { |
| 971 | return GL_FALSE; |
| 972 | } |
| 973 | break; |
| 974 | case OPCODE_KIL: /* ARB_f_p only */ |
| 975 | { |
| 976 | GLfloat a[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 977 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 978 | if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) { |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 979 | return GL_FALSE; |
| 980 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 981 | } |
| 982 | break; |
| 983 | case OPCODE_LG2: /* log base 2 */ |
| 984 | { |
| 985 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 986 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 987 | result[0] = result[1] = result[2] = result[3] = LOG2(a[0]); |
| 988 | store_vector4(inst, machine, result); |
| 989 | } |
| 990 | break; |
| 991 | case OPCODE_LIT: |
| 992 | { |
| 993 | const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */ |
| 994 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 995 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 996 | a[0] = MAX2(a[0], 0.0F); |
| 997 | a[1] = MAX2(a[1], 0.0F); |
| 998 | /* XXX ARB version clamps a[3], NV version doesn't */ |
| 999 | a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon)); |
| 1000 | result[0] = 1.0F; |
| 1001 | result[1] = a[0]; |
| 1002 | /* XXX we could probably just use pow() here */ |
| 1003 | if (a[0] > 0.0F) { |
| 1004 | if (a[1] == 0.0 && a[3] == 0.0) |
| 1005 | result[2] = 1.0; |
| 1006 | else |
| 1007 | result[2] = EXPF(a[3] * LOGF(a[1])); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1008 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1009 | else { |
| 1010 | result[2] = 0.0; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1011 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1012 | result[3] = 1.0F; |
| 1013 | store_vector4(inst, machine, result); |
| 1014 | if (DEBUG_PROG) { |
| 1015 | printf("LIT (%g %g %g %g) : (%g %g %g %g)\n", |
| 1016 | result[0], result[1], result[2], result[3], |
| 1017 | a[0], a[1], a[2], a[3]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1018 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1019 | } |
| 1020 | break; |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 1021 | case OPCODE_LOG: |
| 1022 | { |
| 1023 | GLfloat t[4], q[4], abs_t0; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1024 | fetch_vector1(&inst->SrcReg[0], machine, t); |
Brian | f183a2d | 2007-02-23 17:14:30 -0700 | [diff] [blame] | 1025 | abs_t0 = FABSF(t[0]); |
| 1026 | if (abs_t0 != 0.0F) { |
| 1027 | /* Since we really can't handle infinite values on VMS |
| 1028 | * like other OSes we'll use __MAXFLOAT to represent |
| 1029 | * infinity. This may need some tweaking. |
| 1030 | */ |
| 1031 | #ifdef VMS |
| 1032 | if (abs_t0 == __MAXFLOAT) |
| 1033 | #else |
| 1034 | if (IS_INF_OR_NAN(abs_t0)) |
| 1035 | #endif |
| 1036 | { |
| 1037 | SET_POS_INFINITY(q[0]); |
| 1038 | q[1] = 1.0F; |
| 1039 | SET_POS_INFINITY(q[2]); |
| 1040 | } |
| 1041 | else { |
| 1042 | int exponent; |
| 1043 | GLfloat mantissa = FREXPF(t[0], &exponent); |
| 1044 | q[0] = (GLfloat) (exponent - 1); |
| 1045 | q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */ |
| 1046 | q[2] = (GLfloat) (q[0] + LOG2(q[1])); |
| 1047 | } |
| 1048 | } |
| 1049 | else { |
| 1050 | SET_NEG_INFINITY(q[0]); |
| 1051 | q[1] = 1.0F; |
| 1052 | SET_NEG_INFINITY(q[2]); |
| 1053 | } |
| 1054 | q[3] = 1.0; |
| 1055 | store_vector4(inst, machine, q); |
| 1056 | } |
| 1057 | break; |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1058 | case OPCODE_LRP: |
| 1059 | { |
| 1060 | GLfloat a[4], b[4], c[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1061 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1062 | fetch_vector4(&inst->SrcReg[1], machine, b); |
| 1063 | fetch_vector4(&inst->SrcReg[2], machine, c); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1064 | result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0]; |
| 1065 | result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1]; |
| 1066 | result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2]; |
| 1067 | result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3]; |
| 1068 | store_vector4(inst, machine, result); |
| 1069 | if (DEBUG_PROG) { |
| 1070 | printf("LRP (%g %g %g %g) = (%g %g %g %g), " |
| 1071 | "(%g %g %g %g), (%g %g %g %g)\n", |
| 1072 | result[0], result[1], result[2], result[3], |
| 1073 | a[0], a[1], a[2], a[3], |
| 1074 | b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1075 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1076 | } |
| 1077 | break; |
| 1078 | case OPCODE_MAD: |
| 1079 | { |
| 1080 | GLfloat a[4], b[4], c[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1081 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1082 | fetch_vector4(&inst->SrcReg[1], machine, b); |
| 1083 | fetch_vector4(&inst->SrcReg[2], machine, c); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1084 | result[0] = a[0] * b[0] + c[0]; |
| 1085 | result[1] = a[1] * b[1] + c[1]; |
| 1086 | result[2] = a[2] * b[2] + c[2]; |
| 1087 | result[3] = a[3] * b[3] + c[3]; |
| 1088 | store_vector4(inst, machine, result); |
| 1089 | if (DEBUG_PROG) { |
| 1090 | printf("MAD (%g %g %g %g) = (%g %g %g %g) * " |
| 1091 | "(%g %g %g %g) + (%g %g %g %g)\n", |
| 1092 | result[0], result[1], result[2], result[3], |
| 1093 | a[0], a[1], a[2], a[3], |
| 1094 | b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1095 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1096 | } |
| 1097 | break; |
| 1098 | case OPCODE_MAX: |
| 1099 | { |
| 1100 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1101 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1102 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1103 | result[0] = MAX2(a[0], b[0]); |
| 1104 | result[1] = MAX2(a[1], b[1]); |
| 1105 | result[2] = MAX2(a[2], b[2]); |
| 1106 | result[3] = MAX2(a[3], b[3]); |
| 1107 | store_vector4(inst, machine, result); |
| 1108 | if (DEBUG_PROG) { |
| 1109 | printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n", |
| 1110 | result[0], result[1], result[2], result[3], |
| 1111 | a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1112 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1113 | } |
| 1114 | break; |
| 1115 | case OPCODE_MIN: |
| 1116 | { |
| 1117 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1118 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1119 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1120 | result[0] = MIN2(a[0], b[0]); |
| 1121 | result[1] = MIN2(a[1], b[1]); |
| 1122 | result[2] = MIN2(a[2], b[2]); |
| 1123 | result[3] = MIN2(a[3], b[3]); |
| 1124 | store_vector4(inst, machine, result); |
| 1125 | } |
| 1126 | break; |
| 1127 | case OPCODE_MOV: |
| 1128 | { |
| 1129 | GLfloat result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1130 | fetch_vector4(&inst->SrcReg[0], machine, result); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1131 | store_vector4(inst, machine, result); |
| 1132 | if (DEBUG_PROG) { |
| 1133 | printf("MOV (%g %g %g %g)\n", |
| 1134 | result[0], result[1], result[2], result[3]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1135 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1136 | } |
| 1137 | break; |
| 1138 | case OPCODE_MUL: |
| 1139 | { |
| 1140 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1141 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1142 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1143 | result[0] = a[0] * b[0]; |
| 1144 | result[1] = a[1] * b[1]; |
| 1145 | result[2] = a[2] * b[2]; |
| 1146 | result[3] = a[3] * b[3]; |
| 1147 | store_vector4(inst, machine, result); |
| 1148 | if (DEBUG_PROG) { |
| 1149 | printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n", |
| 1150 | result[0], result[1], result[2], result[3], |
| 1151 | a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1152 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1153 | } |
| 1154 | break; |
| 1155 | case OPCODE_NOISE1: |
| 1156 | { |
| 1157 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1158 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1159 | result[0] = |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1160 | result[1] = |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1161 | result[2] = result[3] = _slang_library_noise1(a[0]); |
| 1162 | store_vector4(inst, machine, result); |
| 1163 | } |
| 1164 | break; |
| 1165 | case OPCODE_NOISE2: |
| 1166 | { |
| 1167 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1168 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1169 | result[0] = |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1170 | result[1] = |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1171 | result[2] = result[3] = _slang_library_noise2(a[0], a[1]); |
| 1172 | store_vector4(inst, machine, result); |
| 1173 | } |
| 1174 | break; |
| 1175 | case OPCODE_NOISE3: |
| 1176 | { |
| 1177 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1178 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1179 | result[0] = |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1180 | result[1] = |
| 1181 | result[2] = |
| 1182 | result[3] = _slang_library_noise3(a[0], a[1], a[2]); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1183 | store_vector4(inst, machine, result); |
| 1184 | } |
| 1185 | break; |
| 1186 | case OPCODE_NOISE4: |
| 1187 | { |
| 1188 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1189 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1190 | result[0] = |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1191 | result[1] = |
| 1192 | result[2] = |
| 1193 | result[3] = _slang_library_noise4(a[0], a[1], a[2], a[3]); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1194 | store_vector4(inst, machine, result); |
| 1195 | } |
| 1196 | break; |
| 1197 | case OPCODE_NOP: |
| 1198 | break; |
| 1199 | case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */ |
| 1200 | { |
| 1201 | GLfloat a[4], result[4]; |
| 1202 | GLhalfNV hx, hy; |
| 1203 | GLuint *rawResult = (GLuint *) result; |
| 1204 | GLuint twoHalves; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1205 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1206 | hx = _mesa_float_to_half(a[0]); |
| 1207 | hy = _mesa_float_to_half(a[1]); |
| 1208 | twoHalves = hx | (hy << 16); |
| 1209 | rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3] |
| 1210 | = twoHalves; |
| 1211 | store_vector4(inst, machine, result); |
| 1212 | } |
| 1213 | break; |
| 1214 | case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */ |
| 1215 | { |
| 1216 | GLfloat a[4], result[4]; |
| 1217 | GLuint usx, usy, *rawResult = (GLuint *) result; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1218 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1219 | a[0] = CLAMP(a[0], 0.0F, 1.0F); |
| 1220 | a[1] = CLAMP(a[1], 0.0F, 1.0F); |
| 1221 | usx = IROUND(a[0] * 65535.0F); |
| 1222 | usy = IROUND(a[1] * 65535.0F); |
| 1223 | rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3] |
| 1224 | = usx | (usy << 16); |
| 1225 | store_vector4(inst, machine, result); |
| 1226 | } |
| 1227 | break; |
| 1228 | case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */ |
| 1229 | { |
| 1230 | GLfloat a[4], result[4]; |
| 1231 | GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1232 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1233 | a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F); |
| 1234 | a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F); |
| 1235 | a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F); |
| 1236 | a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F); |
| 1237 | ubx = IROUND(127.0F * a[0] + 128.0F); |
| 1238 | uby = IROUND(127.0F * a[1] + 128.0F); |
| 1239 | ubz = IROUND(127.0F * a[2] + 128.0F); |
| 1240 | ubw = IROUND(127.0F * a[3] + 128.0F); |
| 1241 | rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3] |
| 1242 | = ubx | (uby << 8) | (ubz << 16) | (ubw << 24); |
| 1243 | store_vector4(inst, machine, result); |
| 1244 | } |
| 1245 | break; |
| 1246 | case OPCODE_PK4UB: /* pack four GLubytes into one 32-bit float */ |
| 1247 | { |
| 1248 | GLfloat a[4], result[4]; |
| 1249 | GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1250 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1251 | a[0] = CLAMP(a[0], 0.0F, 1.0F); |
| 1252 | a[1] = CLAMP(a[1], 0.0F, 1.0F); |
| 1253 | a[2] = CLAMP(a[2], 0.0F, 1.0F); |
| 1254 | a[3] = CLAMP(a[3], 0.0F, 1.0F); |
| 1255 | ubx = IROUND(255.0F * a[0]); |
| 1256 | uby = IROUND(255.0F * a[1]); |
| 1257 | ubz = IROUND(255.0F * a[2]); |
| 1258 | ubw = IROUND(255.0F * a[3]); |
| 1259 | rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3] |
| 1260 | = ubx | (uby << 8) | (ubz << 16) | (ubw << 24); |
| 1261 | store_vector4(inst, machine, result); |
| 1262 | } |
| 1263 | break; |
| 1264 | case OPCODE_POW: |
| 1265 | { |
| 1266 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1267 | fetch_vector1(&inst->SrcReg[0], machine, a); |
| 1268 | fetch_vector1(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1269 | result[0] = result[1] = result[2] = result[3] |
| 1270 | = (GLfloat) _mesa_pow(a[0], b[0]); |
| 1271 | store_vector4(inst, machine, result); |
| 1272 | } |
| 1273 | break; |
| 1274 | case OPCODE_RCP: |
| 1275 | { |
| 1276 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1277 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1278 | if (DEBUG_PROG) { |
| 1279 | if (a[0] == 0) |
| 1280 | printf("RCP(0)\n"); |
| 1281 | else if (IS_INF_OR_NAN(a[0])) |
| 1282 | printf("RCP(inf)\n"); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1283 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1284 | result[0] = result[1] = result[2] = result[3] = 1.0F / a[0]; |
| 1285 | store_vector4(inst, machine, result); |
| 1286 | } |
| 1287 | break; |
| 1288 | case OPCODE_RET: /* return from subroutine (conditional) */ |
| 1289 | if (eval_condition(machine, inst)) { |
| 1290 | if (machine->StackDepth == 0) { |
| 1291 | return GL_TRUE; /* Per GL_NV_vertex_program2 spec */ |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1292 | } |
Brian | a0275b0 | 2007-03-27 11:02:20 -0600 | [diff] [blame] | 1293 | /* subtract one because of pc++ in the for loop */ |
| 1294 | pc = machine->CallStack[--machine->StackDepth] - 1; |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1295 | } |
| 1296 | break; |
| 1297 | case OPCODE_RFL: /* reflection vector */ |
| 1298 | { |
| 1299 | GLfloat axis[4], dir[4], result[4], tmpX, tmpW; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1300 | fetch_vector4(&inst->SrcReg[0], machine, axis); |
| 1301 | fetch_vector4(&inst->SrcReg[1], machine, dir); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1302 | tmpW = DOT3(axis, axis); |
| 1303 | tmpX = (2.0F * DOT3(axis, dir)) / tmpW; |
| 1304 | result[0] = tmpX * axis[0] - dir[0]; |
| 1305 | result[1] = tmpX * axis[1] - dir[1]; |
| 1306 | result[2] = tmpX * axis[2] - dir[2]; |
| 1307 | /* result[3] is never written! XXX enforce in parser! */ |
| 1308 | store_vector4(inst, machine, result); |
| 1309 | } |
| 1310 | break; |
| 1311 | case OPCODE_RSQ: /* 1 / sqrt() */ |
| 1312 | { |
| 1313 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1314 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1315 | a[0] = FABSF(a[0]); |
| 1316 | result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]); |
| 1317 | store_vector4(inst, machine, result); |
| 1318 | if (DEBUG_PROG) { |
| 1319 | printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1320 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1321 | } |
| 1322 | break; |
| 1323 | case OPCODE_SCS: /* sine and cos */ |
| 1324 | { |
| 1325 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1326 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1327 | result[0] = (GLfloat) _mesa_cos(a[0]); |
| 1328 | result[1] = (GLfloat) _mesa_sin(a[0]); |
| 1329 | result[2] = 0.0; /* undefined! */ |
| 1330 | result[3] = 0.0; /* undefined! */ |
| 1331 | store_vector4(inst, machine, result); |
| 1332 | } |
| 1333 | break; |
| 1334 | case OPCODE_SEQ: /* set on equal */ |
| 1335 | { |
| 1336 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1337 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1338 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1339 | result[0] = (a[0] == b[0]) ? 1.0F : 0.0F; |
| 1340 | result[1] = (a[1] == b[1]) ? 1.0F : 0.0F; |
| 1341 | result[2] = (a[2] == b[2]) ? 1.0F : 0.0F; |
| 1342 | result[3] = (a[3] == b[3]) ? 1.0F : 0.0F; |
| 1343 | store_vector4(inst, machine, result); |
Brian | 28ab112 | 2007-03-06 12:15:30 -0700 | [diff] [blame] | 1344 | if (DEBUG_PROG) { |
| 1345 | printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n", |
| 1346 | result[0], result[1], result[2], result[3], |
| 1347 | a[0], a[1], a[2], a[3], |
| 1348 | b[0], b[1], b[2], b[3]); |
| 1349 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1350 | } |
| 1351 | break; |
| 1352 | case OPCODE_SFL: /* set false, operands ignored */ |
| 1353 | { |
| 1354 | static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F }; |
| 1355 | store_vector4(inst, machine, result); |
| 1356 | } |
| 1357 | break; |
| 1358 | case OPCODE_SGE: /* set on greater or equal */ |
| 1359 | { |
| 1360 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1361 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1362 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1363 | result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F; |
| 1364 | result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F; |
| 1365 | result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F; |
| 1366 | result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F; |
| 1367 | store_vector4(inst, machine, result); |
Brian | 28ab112 | 2007-03-06 12:15:30 -0700 | [diff] [blame] | 1368 | if (DEBUG_PROG) { |
| 1369 | printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n", |
| 1370 | result[0], result[1], result[2], result[3], |
| 1371 | a[0], a[1], a[2], a[3], |
| 1372 | b[0], b[1], b[2], b[3]); |
| 1373 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1374 | } |
| 1375 | break; |
| 1376 | case OPCODE_SGT: /* set on greater */ |
| 1377 | { |
| 1378 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1379 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1380 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1381 | result[0] = (a[0] > b[0]) ? 1.0F : 0.0F; |
| 1382 | result[1] = (a[1] > b[1]) ? 1.0F : 0.0F; |
| 1383 | result[2] = (a[2] > b[2]) ? 1.0F : 0.0F; |
| 1384 | result[3] = (a[3] > b[3]) ? 1.0F : 0.0F; |
| 1385 | store_vector4(inst, machine, result); |
| 1386 | if (DEBUG_PROG) { |
Brian | 28ab112 | 2007-03-06 12:15:30 -0700 | [diff] [blame] | 1387 | printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n", |
| 1388 | result[0], result[1], result[2], result[3], |
| 1389 | a[0], a[1], a[2], a[3], |
| 1390 | b[0], b[1], b[2], b[3]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1391 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1392 | } |
| 1393 | break; |
| 1394 | case OPCODE_SIN: |
| 1395 | { |
| 1396 | GLfloat a[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1397 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1398 | result[0] = result[1] = result[2] = result[3] |
| 1399 | = (GLfloat) _mesa_sin(a[0]); |
| 1400 | store_vector4(inst, machine, result); |
| 1401 | } |
| 1402 | break; |
| 1403 | case OPCODE_SLE: /* set on less or equal */ |
| 1404 | { |
| 1405 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1406 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1407 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1408 | result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F; |
| 1409 | result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F; |
| 1410 | result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F; |
| 1411 | result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F; |
| 1412 | store_vector4(inst, machine, result); |
Brian | 28ab112 | 2007-03-06 12:15:30 -0700 | [diff] [blame] | 1413 | if (DEBUG_PROG) { |
| 1414 | printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n", |
| 1415 | result[0], result[1], result[2], result[3], |
| 1416 | a[0], a[1], a[2], a[3], |
| 1417 | b[0], b[1], b[2], b[3]); |
| 1418 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1419 | } |
| 1420 | break; |
| 1421 | case OPCODE_SLT: /* set on less */ |
| 1422 | { |
| 1423 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1424 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1425 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1426 | result[0] = (a[0] < b[0]) ? 1.0F : 0.0F; |
| 1427 | result[1] = (a[1] < b[1]) ? 1.0F : 0.0F; |
| 1428 | result[2] = (a[2] < b[2]) ? 1.0F : 0.0F; |
| 1429 | result[3] = (a[3] < b[3]) ? 1.0F : 0.0F; |
| 1430 | store_vector4(inst, machine, result); |
Brian | 28ab112 | 2007-03-06 12:15:30 -0700 | [diff] [blame] | 1431 | if (DEBUG_PROG) { |
| 1432 | printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n", |
| 1433 | result[0], result[1], result[2], result[3], |
| 1434 | a[0], a[1], a[2], a[3], |
| 1435 | b[0], b[1], b[2], b[3]); |
| 1436 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1437 | } |
| 1438 | break; |
| 1439 | case OPCODE_SNE: /* set on not equal */ |
| 1440 | { |
| 1441 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1442 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1443 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1444 | result[0] = (a[0] != b[0]) ? 1.0F : 0.0F; |
| 1445 | result[1] = (a[1] != b[1]) ? 1.0F : 0.0F; |
| 1446 | result[2] = (a[2] != b[2]) ? 1.0F : 0.0F; |
| 1447 | result[3] = (a[3] != b[3]) ? 1.0F : 0.0F; |
| 1448 | store_vector4(inst, machine, result); |
Brian | 28ab112 | 2007-03-06 12:15:30 -0700 | [diff] [blame] | 1449 | if (DEBUG_PROG) { |
| 1450 | printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n", |
| 1451 | result[0], result[1], result[2], result[3], |
| 1452 | a[0], a[1], a[2], a[3], |
| 1453 | b[0], b[1], b[2], b[3]); |
| 1454 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1455 | } |
| 1456 | break; |
| 1457 | case OPCODE_STR: /* set true, operands ignored */ |
| 1458 | { |
| 1459 | static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F }; |
| 1460 | store_vector4(inst, machine, result); |
| 1461 | } |
| 1462 | break; |
| 1463 | case OPCODE_SUB: |
| 1464 | { |
| 1465 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1466 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1467 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1468 | result[0] = a[0] - b[0]; |
| 1469 | result[1] = a[1] - b[1]; |
| 1470 | result[2] = a[2] - b[2]; |
| 1471 | result[3] = a[3] - b[3]; |
| 1472 | store_vector4(inst, machine, result); |
| 1473 | if (DEBUG_PROG) { |
| 1474 | printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n", |
| 1475 | result[0], result[1], result[2], result[3], |
| 1476 | a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]); |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1477 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1478 | } |
| 1479 | break; |
| 1480 | case OPCODE_SWZ: /* extended swizzle */ |
| 1481 | { |
| 1482 | const struct prog_src_register *source = &inst->SrcReg[0]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1483 | const GLfloat *src = get_register_pointer(source, machine); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1484 | GLfloat result[4]; |
| 1485 | GLuint i; |
| 1486 | for (i = 0; i < 4; i++) { |
| 1487 | const GLuint swz = GET_SWZ(source->Swizzle, i); |
| 1488 | if (swz == SWIZZLE_ZERO) |
| 1489 | result[i] = 0.0; |
| 1490 | else if (swz == SWIZZLE_ONE) |
| 1491 | result[i] = 1.0; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1492 | else { |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1493 | ASSERT(swz >= 0); |
| 1494 | ASSERT(swz <= 3); |
| 1495 | result[i] = src[swz]; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1496 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1497 | if (source->NegateBase & (1 << i)) |
| 1498 | result[i] = -result[i]; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1499 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1500 | store_vector4(inst, machine, result); |
| 1501 | } |
| 1502 | break; |
| 1503 | case OPCODE_TEX: /* Both ARB and NV frag prog */ |
| 1504 | /* Texel lookup */ |
| 1505 | { |
| 1506 | /* Note: only use the precomputed lambda value when we're |
| 1507 | * sampling texture unit [K] with texcoord[K]. |
| 1508 | * Otherwise, the lambda value may have no relation to the |
| 1509 | * instruction's texcoord or texture image. Using the wrong |
| 1510 | * lambda is usually bad news. |
| 1511 | * The rest of the time, just use zero (until we get a more |
| 1512 | * sophisticated way of computing lambda). |
| 1513 | */ |
| 1514 | GLfloat coord[4], color[4], lambda; |
| 1515 | #if 0 |
| 1516 | if (inst->SrcReg[0].File == PROGRAM_INPUT && |
| 1517 | inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit) |
| 1518 | lambda = span->array->lambda[inst->TexSrcUnit][column]; |
| 1519 | else |
| 1520 | #endif |
| 1521 | lambda = 0.0; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1522 | fetch_vector4(&inst->SrcReg[0], machine, coord); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1523 | machine->FetchTexelLod(ctx, coord, lambda, inst->TexSrcUnit, |
| 1524 | color); |
| 1525 | if (DEBUG_PROG) { |
| 1526 | printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g], " |
| 1527 | "lod %f\n", |
| 1528 | color[0], color[1], color[2], color[3], |
| 1529 | inst->TexSrcUnit, |
| 1530 | coord[0], coord[1], coord[2], coord[3], lambda); |
| 1531 | } |
| 1532 | store_vector4(inst, machine, color); |
| 1533 | } |
| 1534 | break; |
| 1535 | case OPCODE_TXB: /* GL_ARB_fragment_program only */ |
| 1536 | /* Texel lookup with LOD bias */ |
| 1537 | { |
| 1538 | const struct gl_texture_unit *texUnit |
| 1539 | = &ctx->Texture.Unit[inst->TexSrcUnit]; |
| 1540 | GLfloat coord[4], color[4], lambda, bias; |
| 1541 | #if 0 |
| 1542 | if (inst->SrcReg[0].File == PROGRAM_INPUT && |
| 1543 | inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit) |
| 1544 | lambda = span->array->lambda[inst->TexSrcUnit][column]; |
| 1545 | else |
| 1546 | #endif |
| 1547 | lambda = 0.0; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1548 | fetch_vector4(&inst->SrcReg[0], machine, coord); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1549 | /* coord[3] is the bias to add to lambda */ |
| 1550 | bias = texUnit->LodBias + coord[3]; |
| 1551 | if (texUnit->_Current) |
| 1552 | bias += texUnit->_Current->LodBias; |
| 1553 | machine->FetchTexelLod(ctx, coord, lambda + bias, |
| 1554 | inst->TexSrcUnit, color); |
| 1555 | store_vector4(inst, machine, color); |
| 1556 | } |
| 1557 | break; |
| 1558 | case OPCODE_TXD: /* GL_NV_fragment_program only */ |
| 1559 | /* Texture lookup w/ partial derivatives for LOD */ |
| 1560 | { |
| 1561 | GLfloat texcoord[4], dtdx[4], dtdy[4], color[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1562 | fetch_vector4(&inst->SrcReg[0], machine, texcoord); |
| 1563 | fetch_vector4(&inst->SrcReg[1], machine, dtdx); |
| 1564 | fetch_vector4(&inst->SrcReg[2], machine, dtdy); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1565 | machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy, |
| 1566 | inst->TexSrcUnit, color); |
| 1567 | store_vector4(inst, machine, color); |
| 1568 | } |
| 1569 | break; |
| 1570 | case OPCODE_TXP: /* GL_ARB_fragment_program only */ |
| 1571 | /* Texture lookup w/ projective divide */ |
| 1572 | { |
| 1573 | GLfloat texcoord[4], color[4], lambda; |
| 1574 | #if 0 |
| 1575 | if (inst->SrcReg[0].File == PROGRAM_INPUT && |
| 1576 | inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit) |
| 1577 | lambda = span->array->lambda[inst->TexSrcUnit][column]; |
| 1578 | else |
| 1579 | #endif |
| 1580 | lambda = 0.0; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1581 | fetch_vector4(&inst->SrcReg[0], machine, texcoord); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1582 | /* Not so sure about this test - if texcoord[3] is |
| 1583 | * zero, we'd probably be fine except for an ASSERT in |
| 1584 | * IROUND_POS() which gets triggered by the inf values created. |
| 1585 | */ |
| 1586 | if (texcoord[3] != 0.0) { |
| 1587 | texcoord[0] /= texcoord[3]; |
| 1588 | texcoord[1] /= texcoord[3]; |
| 1589 | texcoord[2] /= texcoord[3]; |
| 1590 | } |
| 1591 | machine->FetchTexelLod(ctx, texcoord, lambda, |
| 1592 | inst->TexSrcUnit, color); |
| 1593 | store_vector4(inst, machine, color); |
| 1594 | } |
| 1595 | break; |
| 1596 | case OPCODE_TXP_NV: /* GL_NV_fragment_program only */ |
| 1597 | /* Texture lookup w/ projective divide */ |
| 1598 | { |
| 1599 | GLfloat texcoord[4], color[4], lambda; |
| 1600 | #if 0 |
| 1601 | if (inst->SrcReg[0].File == PROGRAM_INPUT && |
| 1602 | inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit) |
| 1603 | lambda = span->array->lambda[inst->TexSrcUnit][column]; |
| 1604 | else |
| 1605 | #endif |
| 1606 | lambda = 0.0; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1607 | fetch_vector4(&inst->SrcReg[0], machine, texcoord); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1608 | if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX && |
| 1609 | texcoord[3] != 0.0) { |
| 1610 | texcoord[0] /= texcoord[3]; |
| 1611 | texcoord[1] /= texcoord[3]; |
| 1612 | texcoord[2] /= texcoord[3]; |
| 1613 | } |
| 1614 | machine->FetchTexelLod(ctx, texcoord, lambda, |
| 1615 | inst->TexSrcUnit, color); |
| 1616 | store_vector4(inst, machine, color); |
| 1617 | } |
| 1618 | break; |
| 1619 | case OPCODE_UP2H: /* unpack two 16-bit floats */ |
| 1620 | { |
| 1621 | GLfloat a[4], result[4]; |
| 1622 | const GLuint *rawBits = (const GLuint *) a; |
| 1623 | GLhalfNV hx, hy; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1624 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1625 | hx = rawBits[0] & 0xffff; |
| 1626 | hy = rawBits[0] >> 16; |
| 1627 | result[0] = result[2] = _mesa_half_to_float(hx); |
| 1628 | result[1] = result[3] = _mesa_half_to_float(hy); |
| 1629 | store_vector4(inst, machine, result); |
| 1630 | } |
| 1631 | break; |
| 1632 | case OPCODE_UP2US: /* unpack two GLushorts */ |
| 1633 | { |
| 1634 | GLfloat a[4], result[4]; |
| 1635 | const GLuint *rawBits = (const GLuint *) a; |
| 1636 | GLushort usx, usy; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1637 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1638 | usx = rawBits[0] & 0xffff; |
| 1639 | usy = rawBits[0] >> 16; |
| 1640 | result[0] = result[2] = usx * (1.0f / 65535.0f); |
| 1641 | result[1] = result[3] = usy * (1.0f / 65535.0f); |
| 1642 | store_vector4(inst, machine, result); |
| 1643 | } |
| 1644 | break; |
| 1645 | case OPCODE_UP4B: /* unpack four GLbytes */ |
| 1646 | { |
| 1647 | GLfloat a[4], result[4]; |
| 1648 | const GLuint *rawBits = (const GLuint *) a; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1649 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1650 | result[0] = (((rawBits[0] >> 0) & 0xff) - 128) / 127.0F; |
| 1651 | result[1] = (((rawBits[0] >> 8) & 0xff) - 128) / 127.0F; |
| 1652 | result[2] = (((rawBits[0] >> 16) & 0xff) - 128) / 127.0F; |
| 1653 | result[3] = (((rawBits[0] >> 24) & 0xff) - 128) / 127.0F; |
| 1654 | store_vector4(inst, machine, result); |
| 1655 | } |
| 1656 | break; |
| 1657 | case OPCODE_UP4UB: /* unpack four GLubytes */ |
| 1658 | { |
| 1659 | GLfloat a[4], result[4]; |
| 1660 | const GLuint *rawBits = (const GLuint *) a; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1661 | fetch_vector1(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1662 | result[0] = ((rawBits[0] >> 0) & 0xff) / 255.0F; |
| 1663 | result[1] = ((rawBits[0] >> 8) & 0xff) / 255.0F; |
| 1664 | result[2] = ((rawBits[0] >> 16) & 0xff) / 255.0F; |
| 1665 | result[3] = ((rawBits[0] >> 24) & 0xff) / 255.0F; |
| 1666 | store_vector4(inst, machine, result); |
| 1667 | } |
| 1668 | break; |
| 1669 | case OPCODE_XPD: /* cross product */ |
| 1670 | { |
| 1671 | GLfloat a[4], b[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1672 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1673 | fetch_vector4(&inst->SrcReg[1], machine, b); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1674 | result[0] = a[1] * b[2] - a[2] * b[1]; |
| 1675 | result[1] = a[2] * b[0] - a[0] * b[2]; |
| 1676 | result[2] = a[0] * b[1] - a[1] * b[0]; |
| 1677 | result[3] = 1.0; |
| 1678 | store_vector4(inst, machine, result); |
Brian | 9637c96 | 2007-03-07 17:40:57 -0700 | [diff] [blame] | 1679 | if (DEBUG_PROG) { |
| 1680 | printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n", |
| 1681 | result[0], result[1], result[2], result[3], |
| 1682 | a[0], a[1], a[2], b[0], b[1], b[2]); |
| 1683 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1684 | } |
| 1685 | break; |
| 1686 | case OPCODE_X2D: /* 2-D matrix transform */ |
| 1687 | { |
| 1688 | GLfloat a[4], b[4], c[4], result[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1689 | fetch_vector4(&inst->SrcReg[0], machine, a); |
| 1690 | fetch_vector4(&inst->SrcReg[1], machine, b); |
| 1691 | fetch_vector4(&inst->SrcReg[2], machine, c); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1692 | result[0] = a[0] + b[0] * c[0] + b[1] * c[1]; |
| 1693 | result[1] = a[1] + b[0] * c[2] + b[1] * c[3]; |
| 1694 | result[2] = a[2] + b[0] * c[0] + b[1] * c[1]; |
| 1695 | result[3] = a[3] + b[0] * c[2] + b[1] * c[3]; |
| 1696 | store_vector4(inst, machine, result); |
| 1697 | } |
| 1698 | break; |
| 1699 | case OPCODE_PRINT: |
| 1700 | { |
| 1701 | if (inst->SrcReg[0].File != -1) { |
| 1702 | GLfloat a[4]; |
Brian | 33eac56 | 2007-02-25 18:52:41 -0700 | [diff] [blame] | 1703 | fetch_vector4(&inst->SrcReg[0], machine, a); |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1704 | _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst->Data, |
| 1705 | a[0], a[1], a[2], a[3]); |
| 1706 | } |
| 1707 | else { |
| 1708 | _mesa_printf("%s\n", (const char *) inst->Data); |
| 1709 | } |
| 1710 | } |
| 1711 | break; |
| 1712 | case OPCODE_END: |
| 1713 | return GL_TRUE; |
| 1714 | default: |
| 1715 | _mesa_problem(ctx, "Bad opcode %d in _mesa_exec_fragment_program", |
| 1716 | inst->Opcode); |
| 1717 | return GL_TRUE; /* return value doesn't matter */ |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1718 | |
| 1719 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1720 | |
Brian | cfd0011 | 2007-02-25 18:30:45 -0700 | [diff] [blame] | 1721 | numExec++; |
| 1722 | if (numExec > maxExec) { |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1723 | _mesa_problem(ctx, "Infinite loop detected in fragment program"); |
| 1724 | return GL_TRUE; |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1725 | } |
Brian | e80d901 | 2007-02-23 16:53:24 -0700 | [diff] [blame] | 1726 | |
| 1727 | } /* for pc */ |
Brian | 13e3b21 | 2007-02-22 16:09:40 -0700 | [diff] [blame] | 1728 | |
| 1729 | #if FEATURE_MESA_program_debug |
| 1730 | CurrentMachine = NULL; |
| 1731 | #endif |
| 1732 | |
| 1733 | return GL_TRUE; |
| 1734 | } |