program: Remove OPCODE_KIL_NV.
[mesa.git] / src / mesa / program / prog_execute.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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 "c99_math.h"
39 #include "main/glheader.h"
40 #include "main/macros.h"
41 #include "prog_execute.h"
42 #include "prog_instruction.h"
43 #include "prog_parameter.h"
44 #include "prog_print.h"
45 #include "prog_noise.h"
46
47
48 /* debug predicate */
49 #define DEBUG_PROG 0
50
51
52 /**
53 * Set x to positive or negative infinity.
54 */
55 #define SET_POS_INFINITY(x) \
56 do { \
57 fi_type fi; \
58 fi.i = 0x7F800000; \
59 x = fi.f; \
60 } while (0)
61 #define SET_NEG_INFINITY(x) \
62 do { \
63 fi_type fi; \
64 fi.i = 0xFF800000; \
65 x = fi.f; \
66 } while (0)
67
68 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
69
70
71 static const GLfloat ZeroVec[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
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 >= VARYING_SLOT_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_STATE_VAR:
117 /* Fallthrough */
118 case PROGRAM_CONSTANT:
119 /* Fallthrough */
120 case PROGRAM_UNIFORM:
121 if (reg >= (GLint) prog->Parameters->NumParameters)
122 return ZeroVec;
123 return (GLfloat *) prog->Parameters->ParameterValues[reg];
124
125 case PROGRAM_SYSTEM_VALUE:
126 assert(reg < (GLint) ARRAY_SIZE(machine->SystemValues));
127 return machine->SystemValues[reg];
128
129 default:
130 _mesa_problem(NULL,
131 "Invalid src register file %d in get_src_register_pointer()",
132 source->File);
133 return ZeroVec;
134 }
135 }
136
137
138 /**
139 * Return a pointer to the 4-element float vector specified by the given
140 * destination register.
141 */
142 static inline GLfloat *
143 get_dst_register_pointer(const struct prog_dst_register *dest,
144 struct gl_program_machine *machine)
145 {
146 static GLfloat dummyReg[4];
147 GLint reg = dest->Index;
148
149 if (dest->RelAddr) {
150 /* add address register value to src index/offset */
151 reg += machine->AddressReg[0][0];
152 if (reg < 0) {
153 return dummyReg;
154 }
155 }
156
157 switch (dest->File) {
158 case PROGRAM_TEMPORARY:
159 if (reg >= MAX_PROGRAM_TEMPS)
160 return dummyReg;
161 return machine->Temporaries[reg];
162
163 case PROGRAM_OUTPUT:
164 if (reg >= MAX_PROGRAM_OUTPUTS)
165 return dummyReg;
166 return machine->Outputs[reg];
167
168 default:
169 _mesa_problem(NULL,
170 "Invalid dest register file %d in get_dst_register_pointer()",
171 dest->File);
172 return dummyReg;
173 }
174 }
175
176
177
178 /**
179 * Fetch a 4-element float vector from the given source register.
180 * Apply swizzling and negating as needed.
181 */
182 static void
183 fetch_vector4(const struct prog_src_register *source,
184 const struct gl_program_machine *machine, GLfloat result[4])
185 {
186 const GLfloat *src = get_src_register_pointer(source, machine);
187
188 if (source->Swizzle == SWIZZLE_NOOP) {
189 /* no swizzling */
190 COPY_4V(result, src);
191 }
192 else {
193 assert(GET_SWZ(source->Swizzle, 0) <= 3);
194 assert(GET_SWZ(source->Swizzle, 1) <= 3);
195 assert(GET_SWZ(source->Swizzle, 2) <= 3);
196 assert(GET_SWZ(source->Swizzle, 3) <= 3);
197 result[0] = src[GET_SWZ(source->Swizzle, 0)];
198 result[1] = src[GET_SWZ(source->Swizzle, 1)];
199 result[2] = src[GET_SWZ(source->Swizzle, 2)];
200 result[3] = src[GET_SWZ(source->Swizzle, 3)];
201 }
202
203 if (source->Abs) {
204 result[0] = fabsf(result[0]);
205 result[1] = fabsf(result[1]);
206 result[2] = fabsf(result[2]);
207 result[3] = fabsf(result[3]);
208 }
209 if (source->Negate) {
210 assert(source->Negate == NEGATE_XYZW);
211 result[0] = -result[0];
212 result[1] = -result[1];
213 result[2] = -result[2];
214 result[3] = -result[3];
215 }
216
217 #ifdef NAN_CHECK
218 assert(!IS_INF_OR_NAN(result[0]));
219 assert(!IS_INF_OR_NAN(result[0]));
220 assert(!IS_INF_OR_NAN(result[0]));
221 assert(!IS_INF_OR_NAN(result[0]));
222 #endif
223 }
224
225
226 /**
227 * Fetch the derivative with respect to X or Y for the given register.
228 * XXX this currently only works for fragment program input attribs.
229 */
230 static void
231 fetch_vector4_deriv(struct gl_context * ctx,
232 const struct prog_src_register *source,
233 const struct gl_program_machine *machine,
234 char xOrY, GLfloat result[4])
235 {
236 if (source->File == PROGRAM_INPUT &&
237 source->Index < (GLint) machine->NumDeriv) {
238 const GLint col = machine->CurElement;
239 const GLfloat w = machine->Attribs[VARYING_SLOT_POS][col][3];
240 const GLfloat invQ = 1.0f / w;
241 GLfloat deriv[4];
242
243 if (xOrY == 'X') {
244 deriv[0] = machine->DerivX[source->Index][0] * invQ;
245 deriv[1] = machine->DerivX[source->Index][1] * invQ;
246 deriv[2] = machine->DerivX[source->Index][2] * invQ;
247 deriv[3] = machine->DerivX[source->Index][3] * invQ;
248 }
249 else {
250 deriv[0] = machine->DerivY[source->Index][0] * invQ;
251 deriv[1] = machine->DerivY[source->Index][1] * invQ;
252 deriv[2] = machine->DerivY[source->Index][2] * invQ;
253 deriv[3] = machine->DerivY[source->Index][3] * invQ;
254 }
255
256 result[0] = deriv[GET_SWZ(source->Swizzle, 0)];
257 result[1] = deriv[GET_SWZ(source->Swizzle, 1)];
258 result[2] = deriv[GET_SWZ(source->Swizzle, 2)];
259 result[3] = deriv[GET_SWZ(source->Swizzle, 3)];
260
261 if (source->Abs) {
262 result[0] = fabsf(result[0]);
263 result[1] = fabsf(result[1]);
264 result[2] = fabsf(result[2]);
265 result[3] = fabsf(result[3]);
266 }
267 if (source->Negate) {
268 assert(source->Negate == NEGATE_XYZW);
269 result[0] = -result[0];
270 result[1] = -result[1];
271 result[2] = -result[2];
272 result[3] = -result[3];
273 }
274 }
275 else {
276 ASSIGN_4V(result, 0.0, 0.0, 0.0, 0.0);
277 }
278 }
279
280
281 /**
282 * As above, but only return result[0] element.
283 */
284 static void
285 fetch_vector1(const struct prog_src_register *source,
286 const struct gl_program_machine *machine, GLfloat result[4])
287 {
288 const GLfloat *src = get_src_register_pointer(source, machine);
289
290 result[0] = src[GET_SWZ(source->Swizzle, 0)];
291
292 if (source->Abs) {
293 result[0] = fabsf(result[0]);
294 }
295 if (source->Negate) {
296 result[0] = -result[0];
297 }
298 }
299
300
301 /**
302 * Fetch texel from texture. Use partial derivatives when possible.
303 */
304 static inline void
305 fetch_texel(struct gl_context *ctx,
306 const struct gl_program_machine *machine,
307 const struct prog_instruction *inst,
308 const GLfloat texcoord[4], GLfloat lodBias,
309 GLfloat color[4])
310 {
311 const GLuint unit = machine->Samplers[inst->TexSrcUnit];
312
313 /* Note: we only have the right derivatives for fragment input attribs.
314 */
315 if (machine->NumDeriv > 0 &&
316 inst->SrcReg[0].File == PROGRAM_INPUT &&
317 inst->SrcReg[0].Index == VARYING_SLOT_TEX0 + inst->TexSrcUnit) {
318 /* simple texture fetch for which we should have derivatives */
319 GLuint attr = inst->SrcReg[0].Index;
320 machine->FetchTexelDeriv(ctx, texcoord,
321 machine->DerivX[attr],
322 machine->DerivY[attr],
323 lodBias, unit, color);
324 }
325 else {
326 machine->FetchTexelLod(ctx, texcoord, lodBias, unit, color);
327 }
328 }
329
330
331 /**
332 * Test value against zero and return GT, LT, EQ or UN if NaN.
333 */
334 static inline GLuint
335 generate_cc(float value)
336 {
337 if (value != value)
338 return COND_UN; /* NaN */
339 if (value > 0.0F)
340 return COND_GT;
341 if (value < 0.0F)
342 return COND_LT;
343 return COND_EQ;
344 }
345
346
347 /**
348 * Test if the ccMaskRule is satisfied by the given condition code.
349 * Used to mask destination writes according to the current condition code.
350 */
351 static inline GLboolean
352 test_cc(GLuint condCode, GLuint ccMaskRule)
353 {
354 switch (ccMaskRule) {
355 case COND_EQ: return (condCode == COND_EQ);
356 case COND_NE: return (condCode != COND_EQ);
357 case COND_LT: return (condCode == COND_LT);
358 case COND_GE: return (condCode == COND_GT || condCode == COND_EQ);
359 case COND_LE: return (condCode == COND_LT || condCode == COND_EQ);
360 case COND_GT: return (condCode == COND_GT);
361 case COND_TR: return GL_TRUE;
362 case COND_FL: return GL_FALSE;
363 default: return GL_TRUE;
364 }
365 }
366
367
368 /**
369 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
370 * or GL_FALSE to indicate result.
371 */
372 static inline GLboolean
373 eval_condition(const struct gl_program_machine *machine,
374 const struct prog_instruction *inst)
375 {
376 const GLuint swizzle = inst->DstReg.CondSwizzle;
377 const GLuint condMask = inst->DstReg.CondMask;
378 if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
379 test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
380 test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
381 test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
382 return GL_TRUE;
383 }
384 else {
385 return GL_FALSE;
386 }
387 }
388
389
390
391 /**
392 * Store 4 floats into a register. Observe the instructions saturate and
393 * set-condition-code flags.
394 */
395 static void
396 store_vector4(const struct prog_instruction *inst,
397 struct gl_program_machine *machine, const GLfloat value[4])
398 {
399 const struct prog_dst_register *dstReg = &(inst->DstReg);
400 const GLboolean clamp = inst->Saturate;
401 GLuint writeMask = dstReg->WriteMask;
402 GLfloat clampedValue[4];
403 GLfloat *dst = get_dst_register_pointer(dstReg, machine);
404
405 #if 0
406 if (value[0] > 1.0e10 ||
407 IS_INF_OR_NAN(value[0]) ||
408 IS_INF_OR_NAN(value[1]) ||
409 IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3]))
410 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
411 #endif
412
413 if (clamp) {
414 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
415 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
416 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
417 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
418 value = clampedValue;
419 }
420
421 if (dstReg->CondMask != COND_TR) {
422 /* condition codes may turn off some writes */
423 if (writeMask & WRITEMASK_X) {
424 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
425 dstReg->CondMask))
426 writeMask &= ~WRITEMASK_X;
427 }
428 if (writeMask & WRITEMASK_Y) {
429 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
430 dstReg->CondMask))
431 writeMask &= ~WRITEMASK_Y;
432 }
433 if (writeMask & WRITEMASK_Z) {
434 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
435 dstReg->CondMask))
436 writeMask &= ~WRITEMASK_Z;
437 }
438 if (writeMask & WRITEMASK_W) {
439 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
440 dstReg->CondMask))
441 writeMask &= ~WRITEMASK_W;
442 }
443 }
444
445 #ifdef NAN_CHECK
446 assert(!IS_INF_OR_NAN(value[0]));
447 assert(!IS_INF_OR_NAN(value[0]));
448 assert(!IS_INF_OR_NAN(value[0]));
449 assert(!IS_INF_OR_NAN(value[0]));
450 #endif
451
452 if (writeMask & WRITEMASK_X)
453 dst[0] = value[0];
454 if (writeMask & WRITEMASK_Y)
455 dst[1] = value[1];
456 if (writeMask & WRITEMASK_Z)
457 dst[2] = value[2];
458 if (writeMask & WRITEMASK_W)
459 dst[3] = value[3];
460
461 if (inst->CondUpdate) {
462 if (writeMask & WRITEMASK_X)
463 machine->CondCodes[0] = generate_cc(value[0]);
464 if (writeMask & WRITEMASK_Y)
465 machine->CondCodes[1] = generate_cc(value[1]);
466 if (writeMask & WRITEMASK_Z)
467 machine->CondCodes[2] = generate_cc(value[2]);
468 if (writeMask & WRITEMASK_W)
469 machine->CondCodes[3] = generate_cc(value[3]);
470 #if DEBUG_PROG
471 printf("CondCodes=(%s,%s,%s,%s) for:\n",
472 _mesa_condcode_string(machine->CondCodes[0]),
473 _mesa_condcode_string(machine->CondCodes[1]),
474 _mesa_condcode_string(machine->CondCodes[2]),
475 _mesa_condcode_string(machine->CondCodes[3]));
476 #endif
477 }
478 }
479
480
481 /**
482 * Execute the given vertex/fragment program.
483 *
484 * \param ctx rendering context
485 * \param program the program to execute
486 * \param machine machine state (must be initialized)
487 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
488 */
489 GLboolean
490 _mesa_execute_program(struct gl_context * ctx,
491 const struct gl_program *program,
492 struct gl_program_machine *machine)
493 {
494 const GLuint numInst = program->NumInstructions;
495 const GLuint maxExec = 65536;
496 GLuint pc, numExec = 0;
497
498 machine->CurProgram = program;
499
500 if (DEBUG_PROG) {
501 printf("execute program %u --------------------\n", program->Id);
502 }
503
504 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
505 machine->EnvParams = ctx->VertexProgram.Parameters;
506 }
507 else {
508 machine->EnvParams = ctx->FragmentProgram.Parameters;
509 }
510
511 for (pc = 0; pc < numInst; pc++) {
512 const struct prog_instruction *inst = program->Instructions + pc;
513
514 if (DEBUG_PROG) {
515 _mesa_print_instruction(inst);
516 }
517
518 switch (inst->Opcode) {
519 case OPCODE_ABS:
520 {
521 GLfloat a[4], result[4];
522 fetch_vector4(&inst->SrcReg[0], machine, a);
523 result[0] = fabsf(a[0]);
524 result[1] = fabsf(a[1]);
525 result[2] = fabsf(a[2]);
526 result[3] = fabsf(a[3]);
527 store_vector4(inst, machine, result);
528 }
529 break;
530 case OPCODE_ADD:
531 {
532 GLfloat a[4], b[4], result[4];
533 fetch_vector4(&inst->SrcReg[0], machine, a);
534 fetch_vector4(&inst->SrcReg[1], machine, b);
535 result[0] = a[0] + b[0];
536 result[1] = a[1] + b[1];
537 result[2] = a[2] + b[2];
538 result[3] = a[3] + b[3];
539 store_vector4(inst, machine, result);
540 if (DEBUG_PROG) {
541 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
542 result[0], result[1], result[2], result[3],
543 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
544 }
545 }
546 break;
547 case OPCODE_ARL:
548 {
549 GLfloat t[4];
550 fetch_vector4(&inst->SrcReg[0], machine, t);
551 machine->AddressReg[0][0] = IFLOOR(t[0]);
552 if (DEBUG_PROG) {
553 printf("ARL %d\n", machine->AddressReg[0][0]);
554 }
555 }
556 break;
557 case OPCODE_BGNLOOP:
558 /* no-op */
559 assert(program->Instructions[inst->BranchTarget].Opcode
560 == OPCODE_ENDLOOP);
561 break;
562 case OPCODE_ENDLOOP:
563 /* subtract 1 here since pc is incremented by for(pc) loop */
564 assert(program->Instructions[inst->BranchTarget].Opcode
565 == OPCODE_BGNLOOP);
566 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
567 break;
568 case OPCODE_BGNSUB: /* begin subroutine */
569 break;
570 case OPCODE_ENDSUB: /* end subroutine */
571 break;
572 case OPCODE_BRK: /* break out of loop (conditional) */
573 assert(program->Instructions[inst->BranchTarget].Opcode
574 == OPCODE_ENDLOOP);
575 if (eval_condition(machine, inst)) {
576 /* break out of loop */
577 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
578 pc = inst->BranchTarget;
579 }
580 break;
581 case OPCODE_CONT: /* continue loop (conditional) */
582 assert(program->Instructions[inst->BranchTarget].Opcode
583 == OPCODE_ENDLOOP);
584 if (eval_condition(machine, inst)) {
585 /* continue at ENDLOOP */
586 /* Subtract 1 here since we'll do pc++ at end of for-loop */
587 pc = inst->BranchTarget - 1;
588 }
589 break;
590 case OPCODE_CAL: /* Call subroutine (conditional) */
591 if (eval_condition(machine, inst)) {
592 /* call the subroutine */
593 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
594 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
595 }
596 machine->CallStack[machine->StackDepth++] = pc + 1; /* next inst */
597 /* Subtract 1 here since we'll do pc++ at end of for-loop */
598 pc = inst->BranchTarget - 1;
599 }
600 break;
601 case OPCODE_CMP:
602 {
603 GLfloat a[4], b[4], c[4], result[4];
604 fetch_vector4(&inst->SrcReg[0], machine, a);
605 fetch_vector4(&inst->SrcReg[1], machine, b);
606 fetch_vector4(&inst->SrcReg[2], machine, c);
607 result[0] = a[0] < 0.0F ? b[0] : c[0];
608 result[1] = a[1] < 0.0F ? b[1] : c[1];
609 result[2] = a[2] < 0.0F ? b[2] : c[2];
610 result[3] = a[3] < 0.0F ? b[3] : c[3];
611 store_vector4(inst, machine, result);
612 if (DEBUG_PROG) {
613 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
614 result[0], result[1], result[2], result[3],
615 a[0], a[1], a[2], a[3],
616 b[0], b[1], b[2], b[3],
617 c[0], c[1], c[2], c[3]);
618 }
619 }
620 break;
621 case OPCODE_COS:
622 {
623 GLfloat a[4], result[4];
624 fetch_vector1(&inst->SrcReg[0], machine, a);
625 result[0] = result[1] = result[2] = result[3]
626 = cosf(a[0]);
627 store_vector4(inst, machine, result);
628 }
629 break;
630 case OPCODE_DDX: /* Partial derivative with respect to X */
631 {
632 GLfloat result[4];
633 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
634 'X', result);
635 store_vector4(inst, machine, result);
636 }
637 break;
638 case OPCODE_DDY: /* Partial derivative with respect to Y */
639 {
640 GLfloat result[4];
641 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
642 'Y', result);
643 store_vector4(inst, machine, result);
644 }
645 break;
646 case OPCODE_DP2:
647 {
648 GLfloat a[4], b[4], result[4];
649 fetch_vector4(&inst->SrcReg[0], machine, a);
650 fetch_vector4(&inst->SrcReg[1], machine, b);
651 result[0] = result[1] = result[2] = result[3] = DOT2(a, b);
652 store_vector4(inst, machine, result);
653 if (DEBUG_PROG) {
654 printf("DP2 %g = (%g %g) . (%g %g)\n",
655 result[0], a[0], a[1], b[0], b[1]);
656 }
657 }
658 break;
659 case OPCODE_DP3:
660 {
661 GLfloat a[4], b[4], result[4];
662 fetch_vector4(&inst->SrcReg[0], machine, a);
663 fetch_vector4(&inst->SrcReg[1], machine, b);
664 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
665 store_vector4(inst, machine, result);
666 if (DEBUG_PROG) {
667 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
668 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
669 }
670 }
671 break;
672 case OPCODE_DP4:
673 {
674 GLfloat a[4], b[4], result[4];
675 fetch_vector4(&inst->SrcReg[0], machine, a);
676 fetch_vector4(&inst->SrcReg[1], machine, b);
677 result[0] = result[1] = result[2] = result[3] = DOT4(a, b);
678 store_vector4(inst, machine, result);
679 if (DEBUG_PROG) {
680 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
681 result[0], a[0], a[1], a[2], a[3],
682 b[0], b[1], b[2], b[3]);
683 }
684 }
685 break;
686 case OPCODE_DPH:
687 {
688 GLfloat a[4], b[4], result[4];
689 fetch_vector4(&inst->SrcReg[0], machine, a);
690 fetch_vector4(&inst->SrcReg[1], machine, b);
691 result[0] = result[1] = result[2] = result[3] = DOT3(a, b) + b[3];
692 store_vector4(inst, machine, result);
693 }
694 break;
695 case OPCODE_DST: /* Distance vector */
696 {
697 GLfloat a[4], b[4], result[4];
698 fetch_vector4(&inst->SrcReg[0], machine, a);
699 fetch_vector4(&inst->SrcReg[1], machine, b);
700 result[0] = 1.0F;
701 result[1] = a[1] * b[1];
702 result[2] = a[2];
703 result[3] = b[3];
704 store_vector4(inst, machine, result);
705 }
706 break;
707 case OPCODE_EXP:
708 {
709 GLfloat t[4], q[4], floor_t0;
710 fetch_vector1(&inst->SrcReg[0], machine, t);
711 floor_t0 = floorf(t[0]);
712 if (floor_t0 > FLT_MAX_EXP) {
713 SET_POS_INFINITY(q[0]);
714 SET_POS_INFINITY(q[2]);
715 }
716 else if (floor_t0 < FLT_MIN_EXP) {
717 q[0] = 0.0F;
718 q[2] = 0.0F;
719 }
720 else {
721 q[0] = ldexpf(1.0, (int) floor_t0);
722 /* Note: GL_NV_vertex_program expects
723 * result.z = result.x * APPX(result.y)
724 * We do what the ARB extension says.
725 */
726 q[2] = exp2f(t[0]);
727 }
728 q[1] = t[0] - floor_t0;
729 q[3] = 1.0F;
730 store_vector4( inst, machine, q );
731 }
732 break;
733 case OPCODE_EX2: /* Exponential base 2 */
734 {
735 GLfloat a[4], result[4], val;
736 fetch_vector1(&inst->SrcReg[0], machine, a);
737 val = exp2f(a[0]);
738 /*
739 if (IS_INF_OR_NAN(val))
740 val = 1.0e10;
741 */
742 result[0] = result[1] = result[2] = result[3] = val;
743 store_vector4(inst, machine, result);
744 }
745 break;
746 case OPCODE_FLR:
747 {
748 GLfloat a[4], result[4];
749 fetch_vector4(&inst->SrcReg[0], machine, a);
750 result[0] = floorf(a[0]);
751 result[1] = floorf(a[1]);
752 result[2] = floorf(a[2]);
753 result[3] = floorf(a[3]);
754 store_vector4(inst, machine, result);
755 }
756 break;
757 case OPCODE_FRC:
758 {
759 GLfloat a[4], result[4];
760 fetch_vector4(&inst->SrcReg[0], machine, a);
761 result[0] = a[0] - floorf(a[0]);
762 result[1] = a[1] - floorf(a[1]);
763 result[2] = a[2] - floorf(a[2]);
764 result[3] = a[3] - floorf(a[3]);
765 store_vector4(inst, machine, result);
766 }
767 break;
768 case OPCODE_IF:
769 {
770 GLboolean cond;
771 assert(program->Instructions[inst->BranchTarget].Opcode
772 == OPCODE_ELSE ||
773 program->Instructions[inst->BranchTarget].Opcode
774 == OPCODE_ENDIF);
775 /* eval condition */
776 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
777 GLfloat a[4];
778 fetch_vector1(&inst->SrcReg[0], machine, a);
779 cond = (a[0] != 0.0F);
780 }
781 else {
782 cond = eval_condition(machine, inst);
783 }
784 if (DEBUG_PROG) {
785 printf("IF: %d\n", cond);
786 }
787 /* do if/else */
788 if (cond) {
789 /* do if-clause (just continue execution) */
790 }
791 else {
792 /* go to the instruction after ELSE or ENDIF */
793 assert(inst->BranchTarget >= 0);
794 pc = inst->BranchTarget;
795 }
796 }
797 break;
798 case OPCODE_ELSE:
799 /* goto ENDIF */
800 assert(program->Instructions[inst->BranchTarget].Opcode
801 == OPCODE_ENDIF);
802 assert(inst->BranchTarget >= 0);
803 pc = inst->BranchTarget;
804 break;
805 case OPCODE_ENDIF:
806 /* nothing */
807 break;
808 case OPCODE_KIL: /* ARB_f_p only */
809 {
810 GLfloat a[4];
811 fetch_vector4(&inst->SrcReg[0], machine, a);
812 if (DEBUG_PROG) {
813 printf("KIL if (%g %g %g %g) <= 0.0\n",
814 a[0], a[1], a[2], a[3]);
815 }
816
817 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
818 return GL_FALSE;
819 }
820 }
821 break;
822 case OPCODE_LG2: /* log base 2 */
823 {
824 GLfloat a[4], result[4], val;
825 fetch_vector1(&inst->SrcReg[0], machine, a);
826 /* The fast LOG2 macro doesn't meet the precision requirements.
827 */
828 if (a[0] == 0.0F) {
829 val = -FLT_MAX;
830 }
831 else {
832 val = logf(a[0]) * 1.442695F;
833 }
834 result[0] = result[1] = result[2] = result[3] = val;
835 store_vector4(inst, machine, result);
836 }
837 break;
838 case OPCODE_LIT:
839 {
840 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
841 GLfloat a[4], result[4];
842 fetch_vector4(&inst->SrcReg[0], machine, a);
843 a[0] = MAX2(a[0], 0.0F);
844 a[1] = MAX2(a[1], 0.0F);
845 /* XXX ARB version clamps a[3], NV version doesn't */
846 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
847 result[0] = 1.0F;
848 result[1] = a[0];
849 /* XXX we could probably just use pow() here */
850 if (a[0] > 0.0F) {
851 if (a[1] == 0.0F && a[3] == 0.0F)
852 result[2] = 1.0F;
853 else
854 result[2] = powf(a[1], a[3]);
855 }
856 else {
857 result[2] = 0.0F;
858 }
859 result[3] = 1.0F;
860 store_vector4(inst, machine, result);
861 if (DEBUG_PROG) {
862 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
863 result[0], result[1], result[2], result[3],
864 a[0], a[1], a[2], a[3]);
865 }
866 }
867 break;
868 case OPCODE_LOG:
869 {
870 GLfloat t[4], q[4], abs_t0;
871 fetch_vector1(&inst->SrcReg[0], machine, t);
872 abs_t0 = fabsf(t[0]);
873 if (abs_t0 != 0.0F) {
874 if (IS_INF_OR_NAN(abs_t0))
875 {
876 SET_POS_INFINITY(q[0]);
877 q[1] = 1.0F;
878 SET_POS_INFINITY(q[2]);
879 }
880 else {
881 int exponent;
882 GLfloat mantissa = frexpf(t[0], &exponent);
883 q[0] = (GLfloat) (exponent - 1);
884 q[1] = 2.0F * mantissa; /* map [.5, 1) -> [1, 2) */
885
886 /* The fast LOG2 macro doesn't meet the precision
887 * requirements.
888 */
889 q[2] = logf(t[0]) * 1.442695F;
890 }
891 }
892 else {
893 SET_NEG_INFINITY(q[0]);
894 q[1] = 1.0F;
895 SET_NEG_INFINITY(q[2]);
896 }
897 q[3] = 1.0;
898 store_vector4(inst, machine, q);
899 }
900 break;
901 case OPCODE_LRP:
902 {
903 GLfloat a[4], b[4], c[4], result[4];
904 fetch_vector4(&inst->SrcReg[0], machine, a);
905 fetch_vector4(&inst->SrcReg[1], machine, b);
906 fetch_vector4(&inst->SrcReg[2], machine, c);
907 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
908 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
909 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
910 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
911 store_vector4(inst, machine, result);
912 if (DEBUG_PROG) {
913 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
914 "(%g %g %g %g), (%g %g %g %g)\n",
915 result[0], result[1], result[2], result[3],
916 a[0], a[1], a[2], a[3],
917 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
918 }
919 }
920 break;
921 case OPCODE_MAD:
922 {
923 GLfloat a[4], b[4], c[4], result[4];
924 fetch_vector4(&inst->SrcReg[0], machine, a);
925 fetch_vector4(&inst->SrcReg[1], machine, b);
926 fetch_vector4(&inst->SrcReg[2], machine, c);
927 result[0] = a[0] * b[0] + c[0];
928 result[1] = a[1] * b[1] + c[1];
929 result[2] = a[2] * b[2] + c[2];
930 result[3] = a[3] * b[3] + c[3];
931 store_vector4(inst, machine, result);
932 if (DEBUG_PROG) {
933 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
934 "(%g %g %g %g) + (%g %g %g %g)\n",
935 result[0], result[1], result[2], result[3],
936 a[0], a[1], a[2], a[3],
937 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
938 }
939 }
940 break;
941 case OPCODE_MAX:
942 {
943 GLfloat a[4], b[4], result[4];
944 fetch_vector4(&inst->SrcReg[0], machine, a);
945 fetch_vector4(&inst->SrcReg[1], machine, b);
946 result[0] = MAX2(a[0], b[0]);
947 result[1] = MAX2(a[1], b[1]);
948 result[2] = MAX2(a[2], b[2]);
949 result[3] = MAX2(a[3], b[3]);
950 store_vector4(inst, machine, result);
951 if (DEBUG_PROG) {
952 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
953 result[0], result[1], result[2], result[3],
954 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
955 }
956 }
957 break;
958 case OPCODE_MIN:
959 {
960 GLfloat a[4], b[4], result[4];
961 fetch_vector4(&inst->SrcReg[0], machine, a);
962 fetch_vector4(&inst->SrcReg[1], machine, b);
963 result[0] = MIN2(a[0], b[0]);
964 result[1] = MIN2(a[1], b[1]);
965 result[2] = MIN2(a[2], b[2]);
966 result[3] = MIN2(a[3], b[3]);
967 store_vector4(inst, machine, result);
968 }
969 break;
970 case OPCODE_MOV:
971 {
972 GLfloat result[4];
973 fetch_vector4(&inst->SrcReg[0], machine, result);
974 store_vector4(inst, machine, result);
975 if (DEBUG_PROG) {
976 printf("MOV (%g %g %g %g)\n",
977 result[0], result[1], result[2], result[3]);
978 }
979 }
980 break;
981 case OPCODE_MUL:
982 {
983 GLfloat a[4], b[4], result[4];
984 fetch_vector4(&inst->SrcReg[0], machine, a);
985 fetch_vector4(&inst->SrcReg[1], machine, b);
986 result[0] = a[0] * b[0];
987 result[1] = a[1] * b[1];
988 result[2] = a[2] * b[2];
989 result[3] = a[3] * b[3];
990 store_vector4(inst, machine, result);
991 if (DEBUG_PROG) {
992 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
993 result[0], result[1], result[2], result[3],
994 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
995 }
996 }
997 break;
998 case OPCODE_NOISE1:
999 {
1000 GLfloat a[4], result[4];
1001 fetch_vector1(&inst->SrcReg[0], machine, a);
1002 result[0] =
1003 result[1] =
1004 result[2] =
1005 result[3] = _mesa_noise1(a[0]);
1006 store_vector4(inst, machine, result);
1007 }
1008 break;
1009 case OPCODE_NOISE2:
1010 {
1011 GLfloat a[4], result[4];
1012 fetch_vector4(&inst->SrcReg[0], machine, a);
1013 result[0] =
1014 result[1] =
1015 result[2] = result[3] = _mesa_noise2(a[0], a[1]);
1016 store_vector4(inst, machine, result);
1017 }
1018 break;
1019 case OPCODE_NOISE3:
1020 {
1021 GLfloat a[4], result[4];
1022 fetch_vector4(&inst->SrcReg[0], machine, a);
1023 result[0] =
1024 result[1] =
1025 result[2] =
1026 result[3] = _mesa_noise3(a[0], a[1], a[2]);
1027 store_vector4(inst, machine, result);
1028 }
1029 break;
1030 case OPCODE_NOISE4:
1031 {
1032 GLfloat a[4], result[4];
1033 fetch_vector4(&inst->SrcReg[0], machine, a);
1034 result[0] =
1035 result[1] =
1036 result[2] =
1037 result[3] = _mesa_noise4(a[0], a[1], a[2], a[3]);
1038 store_vector4(inst, machine, result);
1039 }
1040 break;
1041 case OPCODE_NOP:
1042 break;
1043 case OPCODE_POW:
1044 {
1045 GLfloat a[4], b[4], result[4];
1046 fetch_vector1(&inst->SrcReg[0], machine, a);
1047 fetch_vector1(&inst->SrcReg[1], machine, b);
1048 result[0] = result[1] = result[2] = result[3]
1049 = powf(a[0], b[0]);
1050 store_vector4(inst, machine, result);
1051 }
1052 break;
1053
1054 case OPCODE_RCP:
1055 {
1056 GLfloat a[4], result[4];
1057 fetch_vector1(&inst->SrcReg[0], machine, a);
1058 if (DEBUG_PROG) {
1059 if (a[0] == 0)
1060 printf("RCP(0)\n");
1061 else if (IS_INF_OR_NAN(a[0]))
1062 printf("RCP(inf)\n");
1063 }
1064 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1065 store_vector4(inst, machine, result);
1066 }
1067 break;
1068 case OPCODE_RET: /* return from subroutine (conditional) */
1069 if (eval_condition(machine, inst)) {
1070 if (machine->StackDepth == 0) {
1071 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
1072 }
1073 /* subtract one because of pc++ in the for loop */
1074 pc = machine->CallStack[--machine->StackDepth] - 1;
1075 }
1076 break;
1077 case OPCODE_RSQ: /* 1 / sqrt() */
1078 {
1079 GLfloat a[4], result[4];
1080 fetch_vector1(&inst->SrcReg[0], machine, a);
1081 a[0] = fabsf(a[0]);
1082 result[0] = result[1] = result[2] = result[3] = 1.0f / sqrtf(a[0]);
1083 store_vector4(inst, machine, result);
1084 if (DEBUG_PROG) {
1085 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
1086 }
1087 }
1088 break;
1089 case OPCODE_SCS: /* sine and cos */
1090 {
1091 GLfloat a[4], result[4];
1092 fetch_vector1(&inst->SrcReg[0], machine, a);
1093 result[0] = cosf(a[0]);
1094 result[1] = sinf(a[0]);
1095 result[2] = 0.0F; /* undefined! */
1096 result[3] = 0.0F; /* undefined! */
1097 store_vector4(inst, machine, result);
1098 }
1099 break;
1100 case OPCODE_SEQ: /* set on equal */
1101 {
1102 GLfloat a[4], b[4], result[4];
1103 fetch_vector4(&inst->SrcReg[0], machine, a);
1104 fetch_vector4(&inst->SrcReg[1], machine, b);
1105 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1106 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1107 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1108 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1109 store_vector4(inst, machine, result);
1110 if (DEBUG_PROG) {
1111 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1112 result[0], result[1], result[2], result[3],
1113 a[0], a[1], a[2], a[3],
1114 b[0], b[1], b[2], b[3]);
1115 }
1116 }
1117 break;
1118 case OPCODE_SGE: /* set on greater or equal */
1119 {
1120 GLfloat a[4], b[4], result[4];
1121 fetch_vector4(&inst->SrcReg[0], machine, a);
1122 fetch_vector4(&inst->SrcReg[1], machine, b);
1123 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1124 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1125 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1126 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1127 store_vector4(inst, machine, result);
1128 if (DEBUG_PROG) {
1129 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1130 result[0], result[1], result[2], result[3],
1131 a[0], a[1], a[2], a[3],
1132 b[0], b[1], b[2], b[3]);
1133 }
1134 }
1135 break;
1136 case OPCODE_SGT: /* set on greater */
1137 {
1138 GLfloat a[4], b[4], result[4];
1139 fetch_vector4(&inst->SrcReg[0], machine, a);
1140 fetch_vector4(&inst->SrcReg[1], machine, b);
1141 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1142 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1143 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1144 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1145 store_vector4(inst, machine, result);
1146 if (DEBUG_PROG) {
1147 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1148 result[0], result[1], result[2], result[3],
1149 a[0], a[1], a[2], a[3],
1150 b[0], b[1], b[2], b[3]);
1151 }
1152 }
1153 break;
1154 case OPCODE_SIN:
1155 {
1156 GLfloat a[4], result[4];
1157 fetch_vector1(&inst->SrcReg[0], machine, a);
1158 result[0] = result[1] = result[2] = result[3]
1159 = sinf(a[0]);
1160 store_vector4(inst, machine, result);
1161 }
1162 break;
1163 case OPCODE_SLE: /* set on less or equal */
1164 {
1165 GLfloat a[4], b[4], result[4];
1166 fetch_vector4(&inst->SrcReg[0], machine, a);
1167 fetch_vector4(&inst->SrcReg[1], machine, b);
1168 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1169 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1170 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1171 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1172 store_vector4(inst, machine, result);
1173 if (DEBUG_PROG) {
1174 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1175 result[0], result[1], result[2], result[3],
1176 a[0], a[1], a[2], a[3],
1177 b[0], b[1], b[2], b[3]);
1178 }
1179 }
1180 break;
1181 case OPCODE_SLT: /* set on less */
1182 {
1183 GLfloat a[4], b[4], result[4];
1184 fetch_vector4(&inst->SrcReg[0], machine, a);
1185 fetch_vector4(&inst->SrcReg[1], machine, b);
1186 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1187 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1188 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1189 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1190 store_vector4(inst, machine, result);
1191 if (DEBUG_PROG) {
1192 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1193 result[0], result[1], result[2], result[3],
1194 a[0], a[1], a[2], a[3],
1195 b[0], b[1], b[2], b[3]);
1196 }
1197 }
1198 break;
1199 case OPCODE_SNE: /* set on not equal */
1200 {
1201 GLfloat a[4], b[4], result[4];
1202 fetch_vector4(&inst->SrcReg[0], machine, a);
1203 fetch_vector4(&inst->SrcReg[1], machine, b);
1204 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1205 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1206 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1207 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1208 store_vector4(inst, machine, result);
1209 if (DEBUG_PROG) {
1210 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1211 result[0], result[1], result[2], result[3],
1212 a[0], a[1], a[2], a[3],
1213 b[0], b[1], b[2], b[3]);
1214 }
1215 }
1216 break;
1217 case OPCODE_SSG: /* set sign (-1, 0 or +1) */
1218 {
1219 GLfloat a[4], result[4];
1220 fetch_vector4(&inst->SrcReg[0], machine, a);
1221 result[0] = (GLfloat) ((a[0] > 0.0F) - (a[0] < 0.0F));
1222 result[1] = (GLfloat) ((a[1] > 0.0F) - (a[1] < 0.0F));
1223 result[2] = (GLfloat) ((a[2] > 0.0F) - (a[2] < 0.0F));
1224 result[3] = (GLfloat) ((a[3] > 0.0F) - (a[3] < 0.0F));
1225 store_vector4(inst, machine, result);
1226 }
1227 break;
1228 case OPCODE_SUB:
1229 {
1230 GLfloat a[4], b[4], result[4];
1231 fetch_vector4(&inst->SrcReg[0], machine, a);
1232 fetch_vector4(&inst->SrcReg[1], machine, b);
1233 result[0] = a[0] - b[0];
1234 result[1] = a[1] - b[1];
1235 result[2] = a[2] - b[2];
1236 result[3] = a[3] - b[3];
1237 store_vector4(inst, machine, result);
1238 if (DEBUG_PROG) {
1239 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1240 result[0], result[1], result[2], result[3],
1241 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
1242 }
1243 }
1244 break;
1245 case OPCODE_SWZ: /* extended swizzle */
1246 {
1247 const struct prog_src_register *source = &inst->SrcReg[0];
1248 const GLfloat *src = get_src_register_pointer(source, machine);
1249 GLfloat result[4];
1250 GLuint i;
1251 for (i = 0; i < 4; i++) {
1252 const GLuint swz = GET_SWZ(source->Swizzle, i);
1253 if (swz == SWIZZLE_ZERO)
1254 result[i] = 0.0;
1255 else if (swz == SWIZZLE_ONE)
1256 result[i] = 1.0;
1257 else {
1258 assert(swz <= 3);
1259 result[i] = src[swz];
1260 }
1261 if (source->Negate & (1 << i))
1262 result[i] = -result[i];
1263 }
1264 store_vector4(inst, machine, result);
1265 }
1266 break;
1267 case OPCODE_TEX: /* Both ARB and NV frag prog */
1268 /* Simple texel lookup */
1269 {
1270 GLfloat texcoord[4], color[4];
1271 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1272
1273 /* For TEX, texcoord.Q should not be used and its value should not
1274 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1275 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1276 * which is effectively what happens when the texcoord swizzle
1277 * is .xyzz
1278 */
1279 texcoord[3] = 1.0f;
1280
1281 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1282
1283 if (DEBUG_PROG) {
1284 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1285 color[0], color[1], color[2], color[3],
1286 inst->TexSrcUnit,
1287 texcoord[0], texcoord[1], texcoord[2], texcoord[3]);
1288 }
1289 store_vector4(inst, machine, color);
1290 }
1291 break;
1292 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1293 /* Texel lookup with LOD bias */
1294 {
1295 GLfloat texcoord[4], color[4], lodBias;
1296
1297 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1298
1299 /* texcoord[3] is the bias to add to lambda */
1300 lodBias = texcoord[3];
1301
1302 fetch_texel(ctx, machine, inst, texcoord, lodBias, color);
1303
1304 if (DEBUG_PROG) {
1305 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1306 " bias %g\n",
1307 color[0], color[1], color[2], color[3],
1308 inst->TexSrcUnit,
1309 texcoord[0],
1310 texcoord[1],
1311 texcoord[2],
1312 texcoord[3],
1313 lodBias);
1314 }
1315
1316 store_vector4(inst, machine, color);
1317 }
1318 break;
1319 case OPCODE_TXD: /* GL_NV_fragment_program only */
1320 /* Texture lookup w/ partial derivatives for LOD */
1321 {
1322 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
1323 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1324 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1325 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
1326 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
1327 0.0, /* lodBias */
1328 inst->TexSrcUnit, color);
1329 store_vector4(inst, machine, color);
1330 }
1331 break;
1332 case OPCODE_TXL:
1333 /* Texel lookup with explicit LOD */
1334 {
1335 GLfloat texcoord[4], color[4], lod;
1336
1337 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1338
1339 /* texcoord[3] is the LOD */
1340 lod = texcoord[3];
1341
1342 machine->FetchTexelLod(ctx, texcoord, lod,
1343 machine->Samplers[inst->TexSrcUnit], color);
1344
1345 store_vector4(inst, machine, color);
1346 }
1347 break;
1348 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1349 /* Texture lookup w/ projective divide */
1350 {
1351 GLfloat texcoord[4], color[4];
1352
1353 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1354 /* Not so sure about this test - if texcoord[3] is
1355 * zero, we'd probably be fine except for an assert in
1356 * IROUND_POS() which gets triggered by the inf values created.
1357 */
1358 if (texcoord[3] != 0.0F) {
1359 texcoord[0] /= texcoord[3];
1360 texcoord[1] /= texcoord[3];
1361 texcoord[2] /= texcoord[3];
1362 }
1363
1364 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1365
1366 store_vector4(inst, machine, color);
1367 }
1368 break;
1369 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
1370 /* Texture lookup w/ projective divide, as above, but do not
1371 * do the divide by w if sampling from a cube map.
1372 */
1373 {
1374 GLfloat texcoord[4], color[4];
1375
1376 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1377 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1378 texcoord[3] != 0.0F) {
1379 texcoord[0] /= texcoord[3];
1380 texcoord[1] /= texcoord[3];
1381 texcoord[2] /= texcoord[3];
1382 }
1383
1384 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1385
1386 store_vector4(inst, machine, color);
1387 }
1388 break;
1389 case OPCODE_TRUNC: /* truncate toward zero */
1390 {
1391 GLfloat a[4], result[4];
1392 fetch_vector4(&inst->SrcReg[0], machine, a);
1393 result[0] = (GLfloat) (GLint) a[0];
1394 result[1] = (GLfloat) (GLint) a[1];
1395 result[2] = (GLfloat) (GLint) a[2];
1396 result[3] = (GLfloat) (GLint) a[3];
1397 store_vector4(inst, machine, result);
1398 }
1399 break;
1400 case OPCODE_XPD: /* cross product */
1401 {
1402 GLfloat a[4], b[4], result[4];
1403 fetch_vector4(&inst->SrcReg[0], machine, a);
1404 fetch_vector4(&inst->SrcReg[1], machine, b);
1405 result[0] = a[1] * b[2] - a[2] * b[1];
1406 result[1] = a[2] * b[0] - a[0] * b[2];
1407 result[2] = a[0] * b[1] - a[1] * b[0];
1408 result[3] = 1.0;
1409 store_vector4(inst, machine, result);
1410 if (DEBUG_PROG) {
1411 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1412 result[0], result[1], result[2], result[3],
1413 a[0], a[1], a[2], b[0], b[1], b[2]);
1414 }
1415 }
1416 break;
1417 case OPCODE_END:
1418 return GL_TRUE;
1419 default:
1420 _mesa_problem(ctx, "Bad opcode %d in _mesa_execute_program",
1421 inst->Opcode);
1422 return GL_TRUE; /* return value doesn't matter */
1423 }
1424
1425 numExec++;
1426 if (numExec > maxExec) {
1427 static GLboolean reported = GL_FALSE;
1428 if (!reported) {
1429 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1430 reported = GL_TRUE;
1431 }
1432 return GL_TRUE;
1433 }
1434
1435 } /* for pc */
1436
1437 return GL_TRUE;
1438 }