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