program: Remove condition-code and precision support.
[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 * Store 4 floats into a register. Observe the instructions saturate and
333 * set-condition-code flags.
334 */
335 static void
336 store_vector4(const struct prog_instruction *inst,
337 struct gl_program_machine *machine, const GLfloat value[4])
338 {
339 const struct prog_dst_register *dstReg = &(inst->DstReg);
340 const GLboolean clamp = inst->Saturate;
341 GLuint writeMask = dstReg->WriteMask;
342 GLfloat clampedValue[4];
343 GLfloat *dst = get_dst_register_pointer(dstReg, machine);
344
345 #if 0
346 if (value[0] > 1.0e10 ||
347 IS_INF_OR_NAN(value[0]) ||
348 IS_INF_OR_NAN(value[1]) ||
349 IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3]))
350 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
351 #endif
352
353 if (clamp) {
354 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
355 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
356 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
357 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
358 value = clampedValue;
359 }
360
361 #ifdef NAN_CHECK
362 assert(!IS_INF_OR_NAN(value[0]));
363 assert(!IS_INF_OR_NAN(value[0]));
364 assert(!IS_INF_OR_NAN(value[0]));
365 assert(!IS_INF_OR_NAN(value[0]));
366 #endif
367
368 if (writeMask & WRITEMASK_X)
369 dst[0] = value[0];
370 if (writeMask & WRITEMASK_Y)
371 dst[1] = value[1];
372 if (writeMask & WRITEMASK_Z)
373 dst[2] = value[2];
374 if (writeMask & WRITEMASK_W)
375 dst[3] = value[3];
376 }
377
378
379 /**
380 * Execute the given vertex/fragment program.
381 *
382 * \param ctx rendering context
383 * \param program the program to execute
384 * \param machine machine state (must be initialized)
385 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
386 */
387 GLboolean
388 _mesa_execute_program(struct gl_context * ctx,
389 const struct gl_program *program,
390 struct gl_program_machine *machine)
391 {
392 const GLuint numInst = program->NumInstructions;
393 const GLuint maxExec = 65536;
394 GLuint pc, numExec = 0;
395
396 machine->CurProgram = program;
397
398 if (DEBUG_PROG) {
399 printf("execute program %u --------------------\n", program->Id);
400 }
401
402 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
403 machine->EnvParams = ctx->VertexProgram.Parameters;
404 }
405 else {
406 machine->EnvParams = ctx->FragmentProgram.Parameters;
407 }
408
409 for (pc = 0; pc < numInst; pc++) {
410 const struct prog_instruction *inst = program->Instructions + pc;
411
412 if (DEBUG_PROG) {
413 _mesa_print_instruction(inst);
414 }
415
416 switch (inst->Opcode) {
417 case OPCODE_ABS:
418 {
419 GLfloat a[4], result[4];
420 fetch_vector4(&inst->SrcReg[0], machine, a);
421 result[0] = fabsf(a[0]);
422 result[1] = fabsf(a[1]);
423 result[2] = fabsf(a[2]);
424 result[3] = fabsf(a[3]);
425 store_vector4(inst, machine, result);
426 }
427 break;
428 case OPCODE_ADD:
429 {
430 GLfloat a[4], b[4], result[4];
431 fetch_vector4(&inst->SrcReg[0], machine, a);
432 fetch_vector4(&inst->SrcReg[1], machine, b);
433 result[0] = a[0] + b[0];
434 result[1] = a[1] + b[1];
435 result[2] = a[2] + b[2];
436 result[3] = a[3] + b[3];
437 store_vector4(inst, machine, result);
438 if (DEBUG_PROG) {
439 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
440 result[0], result[1], result[2], result[3],
441 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
442 }
443 }
444 break;
445 case OPCODE_ARL:
446 {
447 GLfloat t[4];
448 fetch_vector4(&inst->SrcReg[0], machine, t);
449 machine->AddressReg[0][0] = IFLOOR(t[0]);
450 if (DEBUG_PROG) {
451 printf("ARL %d\n", machine->AddressReg[0][0]);
452 }
453 }
454 break;
455 case OPCODE_BGNLOOP:
456 /* no-op */
457 assert(program->Instructions[inst->BranchTarget].Opcode
458 == OPCODE_ENDLOOP);
459 break;
460 case OPCODE_ENDLOOP:
461 /* subtract 1 here since pc is incremented by for(pc) loop */
462 assert(program->Instructions[inst->BranchTarget].Opcode
463 == OPCODE_BGNLOOP);
464 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
465 break;
466 case OPCODE_BGNSUB: /* begin subroutine */
467 break;
468 case OPCODE_ENDSUB: /* end subroutine */
469 break;
470 case OPCODE_BRK: /* break out of loop (conditional) */
471 assert(program->Instructions[inst->BranchTarget].Opcode
472 == OPCODE_ENDLOOP);
473 /* break out of loop */
474 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
475 pc = inst->BranchTarget;
476 break;
477 case OPCODE_CONT: /* continue loop (conditional) */
478 assert(program->Instructions[inst->BranchTarget].Opcode
479 == OPCODE_ENDLOOP);
480 /* continue at ENDLOOP */
481 /* Subtract 1 here since we'll do pc++ at end of for-loop */
482 pc = inst->BranchTarget - 1;
483 break;
484 case OPCODE_CAL: /* Call subroutine (conditional) */
485 /* call the subroutine */
486 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
487 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
488 }
489 machine->CallStack[machine->StackDepth++] = pc + 1; /* next inst */
490 /* Subtract 1 here since we'll do pc++ at end of for-loop */
491 pc = inst->BranchTarget - 1;
492 break;
493 case OPCODE_CMP:
494 {
495 GLfloat a[4], b[4], c[4], result[4];
496 fetch_vector4(&inst->SrcReg[0], machine, a);
497 fetch_vector4(&inst->SrcReg[1], machine, b);
498 fetch_vector4(&inst->SrcReg[2], machine, c);
499 result[0] = a[0] < 0.0F ? b[0] : c[0];
500 result[1] = a[1] < 0.0F ? b[1] : c[1];
501 result[2] = a[2] < 0.0F ? b[2] : c[2];
502 result[3] = a[3] < 0.0F ? b[3] : c[3];
503 store_vector4(inst, machine, result);
504 if (DEBUG_PROG) {
505 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
506 result[0], result[1], result[2], result[3],
507 a[0], a[1], a[2], a[3],
508 b[0], b[1], b[2], b[3],
509 c[0], c[1], c[2], c[3]);
510 }
511 }
512 break;
513 case OPCODE_COS:
514 {
515 GLfloat a[4], result[4];
516 fetch_vector1(&inst->SrcReg[0], machine, a);
517 result[0] = result[1] = result[2] = result[3]
518 = cosf(a[0]);
519 store_vector4(inst, machine, result);
520 }
521 break;
522 case OPCODE_DDX: /* Partial derivative with respect to X */
523 {
524 GLfloat result[4];
525 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
526 'X', result);
527 store_vector4(inst, machine, result);
528 }
529 break;
530 case OPCODE_DDY: /* Partial derivative with respect to Y */
531 {
532 GLfloat result[4];
533 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
534 'Y', result);
535 store_vector4(inst, machine, result);
536 }
537 break;
538 case OPCODE_DP2:
539 {
540 GLfloat a[4], b[4], result[4];
541 fetch_vector4(&inst->SrcReg[0], machine, a);
542 fetch_vector4(&inst->SrcReg[1], machine, b);
543 result[0] = result[1] = result[2] = result[3] = DOT2(a, b);
544 store_vector4(inst, machine, result);
545 if (DEBUG_PROG) {
546 printf("DP2 %g = (%g %g) . (%g %g)\n",
547 result[0], a[0], a[1], b[0], b[1]);
548 }
549 }
550 break;
551 case OPCODE_DP3:
552 {
553 GLfloat a[4], b[4], result[4];
554 fetch_vector4(&inst->SrcReg[0], machine, a);
555 fetch_vector4(&inst->SrcReg[1], machine, b);
556 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
557 store_vector4(inst, machine, result);
558 if (DEBUG_PROG) {
559 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
560 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
561 }
562 }
563 break;
564 case OPCODE_DP4:
565 {
566 GLfloat a[4], b[4], result[4];
567 fetch_vector4(&inst->SrcReg[0], machine, a);
568 fetch_vector4(&inst->SrcReg[1], machine, b);
569 result[0] = result[1] = result[2] = result[3] = DOT4(a, b);
570 store_vector4(inst, machine, result);
571 if (DEBUG_PROG) {
572 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
573 result[0], a[0], a[1], a[2], a[3],
574 b[0], b[1], b[2], b[3]);
575 }
576 }
577 break;
578 case OPCODE_DPH:
579 {
580 GLfloat a[4], b[4], result[4];
581 fetch_vector4(&inst->SrcReg[0], machine, a);
582 fetch_vector4(&inst->SrcReg[1], machine, b);
583 result[0] = result[1] = result[2] = result[3] = DOT3(a, b) + b[3];
584 store_vector4(inst, machine, result);
585 }
586 break;
587 case OPCODE_DST: /* Distance vector */
588 {
589 GLfloat a[4], b[4], result[4];
590 fetch_vector4(&inst->SrcReg[0], machine, a);
591 fetch_vector4(&inst->SrcReg[1], machine, b);
592 result[0] = 1.0F;
593 result[1] = a[1] * b[1];
594 result[2] = a[2];
595 result[3] = b[3];
596 store_vector4(inst, machine, result);
597 }
598 break;
599 case OPCODE_EXP:
600 {
601 GLfloat t[4], q[4], floor_t0;
602 fetch_vector1(&inst->SrcReg[0], machine, t);
603 floor_t0 = floorf(t[0]);
604 if (floor_t0 > FLT_MAX_EXP) {
605 SET_POS_INFINITY(q[0]);
606 SET_POS_INFINITY(q[2]);
607 }
608 else if (floor_t0 < FLT_MIN_EXP) {
609 q[0] = 0.0F;
610 q[2] = 0.0F;
611 }
612 else {
613 q[0] = ldexpf(1.0, (int) floor_t0);
614 /* Note: GL_NV_vertex_program expects
615 * result.z = result.x * APPX(result.y)
616 * We do what the ARB extension says.
617 */
618 q[2] = exp2f(t[0]);
619 }
620 q[1] = t[0] - floor_t0;
621 q[3] = 1.0F;
622 store_vector4( inst, machine, q );
623 }
624 break;
625 case OPCODE_EX2: /* Exponential base 2 */
626 {
627 GLfloat a[4], result[4], val;
628 fetch_vector1(&inst->SrcReg[0], machine, a);
629 val = exp2f(a[0]);
630 /*
631 if (IS_INF_OR_NAN(val))
632 val = 1.0e10;
633 */
634 result[0] = result[1] = result[2] = result[3] = val;
635 store_vector4(inst, machine, result);
636 }
637 break;
638 case OPCODE_FLR:
639 {
640 GLfloat a[4], result[4];
641 fetch_vector4(&inst->SrcReg[0], machine, a);
642 result[0] = floorf(a[0]);
643 result[1] = floorf(a[1]);
644 result[2] = floorf(a[2]);
645 result[3] = floorf(a[3]);
646 store_vector4(inst, machine, result);
647 }
648 break;
649 case OPCODE_FRC:
650 {
651 GLfloat a[4], result[4];
652 fetch_vector4(&inst->SrcReg[0], machine, a);
653 result[0] = a[0] - floorf(a[0]);
654 result[1] = a[1] - floorf(a[1]);
655 result[2] = a[2] - floorf(a[2]);
656 result[3] = a[3] - floorf(a[3]);
657 store_vector4(inst, machine, result);
658 }
659 break;
660 case OPCODE_IF:
661 {
662 GLboolean cond;
663 assert(program->Instructions[inst->BranchTarget].Opcode
664 == OPCODE_ELSE ||
665 program->Instructions[inst->BranchTarget].Opcode
666 == OPCODE_ENDIF);
667 /* eval condition */
668 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
669 GLfloat a[4];
670 fetch_vector1(&inst->SrcReg[0], machine, a);
671 cond = (a[0] != 0.0F);
672 }
673 if (DEBUG_PROG) {
674 printf("IF: %d\n", cond);
675 }
676 /* do if/else */
677 if (cond) {
678 /* do if-clause (just continue execution) */
679 }
680 else {
681 /* go to the instruction after ELSE or ENDIF */
682 assert(inst->BranchTarget >= 0);
683 pc = inst->BranchTarget;
684 }
685 }
686 break;
687 case OPCODE_ELSE:
688 /* goto ENDIF */
689 assert(program->Instructions[inst->BranchTarget].Opcode
690 == OPCODE_ENDIF);
691 assert(inst->BranchTarget >= 0);
692 pc = inst->BranchTarget;
693 break;
694 case OPCODE_ENDIF:
695 /* nothing */
696 break;
697 case OPCODE_KIL: /* ARB_f_p only */
698 {
699 GLfloat a[4];
700 fetch_vector4(&inst->SrcReg[0], machine, a);
701 if (DEBUG_PROG) {
702 printf("KIL if (%g %g %g %g) <= 0.0\n",
703 a[0], a[1], a[2], a[3]);
704 }
705
706 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
707 return GL_FALSE;
708 }
709 }
710 break;
711 case OPCODE_LG2: /* log base 2 */
712 {
713 GLfloat a[4], result[4], val;
714 fetch_vector1(&inst->SrcReg[0], machine, a);
715 /* The fast LOG2 macro doesn't meet the precision requirements.
716 */
717 if (a[0] == 0.0F) {
718 val = -FLT_MAX;
719 }
720 else {
721 val = logf(a[0]) * 1.442695F;
722 }
723 result[0] = result[1] = result[2] = result[3] = val;
724 store_vector4(inst, machine, result);
725 }
726 break;
727 case OPCODE_LIT:
728 {
729 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
730 GLfloat a[4], result[4];
731 fetch_vector4(&inst->SrcReg[0], machine, a);
732 a[0] = MAX2(a[0], 0.0F);
733 a[1] = MAX2(a[1], 0.0F);
734 /* XXX ARB version clamps a[3], NV version doesn't */
735 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
736 result[0] = 1.0F;
737 result[1] = a[0];
738 /* XXX we could probably just use pow() here */
739 if (a[0] > 0.0F) {
740 if (a[1] == 0.0F && a[3] == 0.0F)
741 result[2] = 1.0F;
742 else
743 result[2] = powf(a[1], a[3]);
744 }
745 else {
746 result[2] = 0.0F;
747 }
748 result[3] = 1.0F;
749 store_vector4(inst, machine, result);
750 if (DEBUG_PROG) {
751 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
752 result[0], result[1], result[2], result[3],
753 a[0], a[1], a[2], a[3]);
754 }
755 }
756 break;
757 case OPCODE_LOG:
758 {
759 GLfloat t[4], q[4], abs_t0;
760 fetch_vector1(&inst->SrcReg[0], machine, t);
761 abs_t0 = fabsf(t[0]);
762 if (abs_t0 != 0.0F) {
763 if (IS_INF_OR_NAN(abs_t0))
764 {
765 SET_POS_INFINITY(q[0]);
766 q[1] = 1.0F;
767 SET_POS_INFINITY(q[2]);
768 }
769 else {
770 int exponent;
771 GLfloat mantissa = frexpf(t[0], &exponent);
772 q[0] = (GLfloat) (exponent - 1);
773 q[1] = 2.0F * mantissa; /* map [.5, 1) -> [1, 2) */
774
775 /* The fast LOG2 macro doesn't meet the precision
776 * requirements.
777 */
778 q[2] = logf(t[0]) * 1.442695F;
779 }
780 }
781 else {
782 SET_NEG_INFINITY(q[0]);
783 q[1] = 1.0F;
784 SET_NEG_INFINITY(q[2]);
785 }
786 q[3] = 1.0;
787 store_vector4(inst, machine, q);
788 }
789 break;
790 case OPCODE_LRP:
791 {
792 GLfloat a[4], b[4], c[4], result[4];
793 fetch_vector4(&inst->SrcReg[0], machine, a);
794 fetch_vector4(&inst->SrcReg[1], machine, b);
795 fetch_vector4(&inst->SrcReg[2], machine, c);
796 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
797 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
798 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
799 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
800 store_vector4(inst, machine, result);
801 if (DEBUG_PROG) {
802 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
803 "(%g %g %g %g), (%g %g %g %g)\n",
804 result[0], result[1], result[2], result[3],
805 a[0], a[1], a[2], a[3],
806 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
807 }
808 }
809 break;
810 case OPCODE_MAD:
811 {
812 GLfloat a[4], b[4], c[4], result[4];
813 fetch_vector4(&inst->SrcReg[0], machine, a);
814 fetch_vector4(&inst->SrcReg[1], machine, b);
815 fetch_vector4(&inst->SrcReg[2], machine, c);
816 result[0] = a[0] * b[0] + c[0];
817 result[1] = a[1] * b[1] + c[1];
818 result[2] = a[2] * b[2] + c[2];
819 result[3] = a[3] * b[3] + c[3];
820 store_vector4(inst, machine, result);
821 if (DEBUG_PROG) {
822 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
823 "(%g %g %g %g) + (%g %g %g %g)\n",
824 result[0], result[1], result[2], result[3],
825 a[0], a[1], a[2], a[3],
826 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
827 }
828 }
829 break;
830 case OPCODE_MAX:
831 {
832 GLfloat a[4], b[4], result[4];
833 fetch_vector4(&inst->SrcReg[0], machine, a);
834 fetch_vector4(&inst->SrcReg[1], machine, b);
835 result[0] = MAX2(a[0], b[0]);
836 result[1] = MAX2(a[1], b[1]);
837 result[2] = MAX2(a[2], b[2]);
838 result[3] = MAX2(a[3], b[3]);
839 store_vector4(inst, machine, result);
840 if (DEBUG_PROG) {
841 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
842 result[0], result[1], result[2], result[3],
843 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
844 }
845 }
846 break;
847 case OPCODE_MIN:
848 {
849 GLfloat a[4], b[4], result[4];
850 fetch_vector4(&inst->SrcReg[0], machine, a);
851 fetch_vector4(&inst->SrcReg[1], machine, b);
852 result[0] = MIN2(a[0], b[0]);
853 result[1] = MIN2(a[1], b[1]);
854 result[2] = MIN2(a[2], b[2]);
855 result[3] = MIN2(a[3], b[3]);
856 store_vector4(inst, machine, result);
857 }
858 break;
859 case OPCODE_MOV:
860 {
861 GLfloat result[4];
862 fetch_vector4(&inst->SrcReg[0], machine, result);
863 store_vector4(inst, machine, result);
864 if (DEBUG_PROG) {
865 printf("MOV (%g %g %g %g)\n",
866 result[0], result[1], result[2], result[3]);
867 }
868 }
869 break;
870 case OPCODE_MUL:
871 {
872 GLfloat a[4], b[4], result[4];
873 fetch_vector4(&inst->SrcReg[0], machine, a);
874 fetch_vector4(&inst->SrcReg[1], machine, b);
875 result[0] = a[0] * b[0];
876 result[1] = a[1] * b[1];
877 result[2] = a[2] * b[2];
878 result[3] = a[3] * b[3];
879 store_vector4(inst, machine, result);
880 if (DEBUG_PROG) {
881 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
882 result[0], result[1], result[2], result[3],
883 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
884 }
885 }
886 break;
887 case OPCODE_NOISE1:
888 {
889 GLfloat a[4], result[4];
890 fetch_vector1(&inst->SrcReg[0], machine, a);
891 result[0] =
892 result[1] =
893 result[2] =
894 result[3] = _mesa_noise1(a[0]);
895 store_vector4(inst, machine, result);
896 }
897 break;
898 case OPCODE_NOISE2:
899 {
900 GLfloat a[4], result[4];
901 fetch_vector4(&inst->SrcReg[0], machine, a);
902 result[0] =
903 result[1] =
904 result[2] = result[3] = _mesa_noise2(a[0], a[1]);
905 store_vector4(inst, machine, result);
906 }
907 break;
908 case OPCODE_NOISE3:
909 {
910 GLfloat a[4], result[4];
911 fetch_vector4(&inst->SrcReg[0], machine, a);
912 result[0] =
913 result[1] =
914 result[2] =
915 result[3] = _mesa_noise3(a[0], a[1], a[2]);
916 store_vector4(inst, machine, result);
917 }
918 break;
919 case OPCODE_NOISE4:
920 {
921 GLfloat a[4], result[4];
922 fetch_vector4(&inst->SrcReg[0], machine, a);
923 result[0] =
924 result[1] =
925 result[2] =
926 result[3] = _mesa_noise4(a[0], a[1], a[2], a[3]);
927 store_vector4(inst, machine, result);
928 }
929 break;
930 case OPCODE_NOP:
931 break;
932 case OPCODE_POW:
933 {
934 GLfloat a[4], b[4], result[4];
935 fetch_vector1(&inst->SrcReg[0], machine, a);
936 fetch_vector1(&inst->SrcReg[1], machine, b);
937 result[0] = result[1] = result[2] = result[3]
938 = powf(a[0], b[0]);
939 store_vector4(inst, machine, result);
940 }
941 break;
942
943 case OPCODE_RCP:
944 {
945 GLfloat a[4], result[4];
946 fetch_vector1(&inst->SrcReg[0], machine, a);
947 if (DEBUG_PROG) {
948 if (a[0] == 0)
949 printf("RCP(0)\n");
950 else if (IS_INF_OR_NAN(a[0]))
951 printf("RCP(inf)\n");
952 }
953 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
954 store_vector4(inst, machine, result);
955 }
956 break;
957 case OPCODE_RET: /* return from subroutine (conditional) */
958 if (machine->StackDepth == 0) {
959 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
960 }
961 /* subtract one because of pc++ in the for loop */
962 pc = machine->CallStack[--machine->StackDepth] - 1;
963 break;
964 case OPCODE_RSQ: /* 1 / sqrt() */
965 {
966 GLfloat a[4], result[4];
967 fetch_vector1(&inst->SrcReg[0], machine, a);
968 a[0] = fabsf(a[0]);
969 result[0] = result[1] = result[2] = result[3] = 1.0f / sqrtf(a[0]);
970 store_vector4(inst, machine, result);
971 if (DEBUG_PROG) {
972 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
973 }
974 }
975 break;
976 case OPCODE_SCS: /* sine and cos */
977 {
978 GLfloat a[4], result[4];
979 fetch_vector1(&inst->SrcReg[0], machine, a);
980 result[0] = cosf(a[0]);
981 result[1] = sinf(a[0]);
982 result[2] = 0.0F; /* undefined! */
983 result[3] = 0.0F; /* undefined! */
984 store_vector4(inst, machine, result);
985 }
986 break;
987 case OPCODE_SEQ: /* set on equal */
988 {
989 GLfloat a[4], b[4], result[4];
990 fetch_vector4(&inst->SrcReg[0], machine, a);
991 fetch_vector4(&inst->SrcReg[1], machine, b);
992 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
993 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
994 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
995 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
996 store_vector4(inst, machine, result);
997 if (DEBUG_PROG) {
998 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
999 result[0], result[1], result[2], result[3],
1000 a[0], a[1], a[2], a[3],
1001 b[0], b[1], b[2], b[3]);
1002 }
1003 }
1004 break;
1005 case OPCODE_SGE: /* set on greater or equal */
1006 {
1007 GLfloat a[4], b[4], result[4];
1008 fetch_vector4(&inst->SrcReg[0], machine, a);
1009 fetch_vector4(&inst->SrcReg[1], machine, b);
1010 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1011 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1012 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1013 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1014 store_vector4(inst, machine, result);
1015 if (DEBUG_PROG) {
1016 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1017 result[0], result[1], result[2], result[3],
1018 a[0], a[1], a[2], a[3],
1019 b[0], b[1], b[2], b[3]);
1020 }
1021 }
1022 break;
1023 case OPCODE_SGT: /* set on greater */
1024 {
1025 GLfloat a[4], b[4], result[4];
1026 fetch_vector4(&inst->SrcReg[0], machine, a);
1027 fetch_vector4(&inst->SrcReg[1], machine, b);
1028 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1029 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1030 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1031 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1032 store_vector4(inst, machine, result);
1033 if (DEBUG_PROG) {
1034 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1035 result[0], result[1], result[2], result[3],
1036 a[0], a[1], a[2], a[3],
1037 b[0], b[1], b[2], b[3]);
1038 }
1039 }
1040 break;
1041 case OPCODE_SIN:
1042 {
1043 GLfloat a[4], result[4];
1044 fetch_vector1(&inst->SrcReg[0], machine, a);
1045 result[0] = result[1] = result[2] = result[3]
1046 = sinf(a[0]);
1047 store_vector4(inst, machine, result);
1048 }
1049 break;
1050 case OPCODE_SLE: /* set on less or equal */
1051 {
1052 GLfloat a[4], b[4], result[4];
1053 fetch_vector4(&inst->SrcReg[0], machine, a);
1054 fetch_vector4(&inst->SrcReg[1], machine, b);
1055 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1056 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1057 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1058 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1059 store_vector4(inst, machine, result);
1060 if (DEBUG_PROG) {
1061 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1062 result[0], result[1], result[2], result[3],
1063 a[0], a[1], a[2], a[3],
1064 b[0], b[1], b[2], b[3]);
1065 }
1066 }
1067 break;
1068 case OPCODE_SLT: /* set on less */
1069 {
1070 GLfloat a[4], b[4], result[4];
1071 fetch_vector4(&inst->SrcReg[0], machine, a);
1072 fetch_vector4(&inst->SrcReg[1], machine, b);
1073 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1074 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1075 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1076 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1077 store_vector4(inst, machine, result);
1078 if (DEBUG_PROG) {
1079 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1080 result[0], result[1], result[2], result[3],
1081 a[0], a[1], a[2], a[3],
1082 b[0], b[1], b[2], b[3]);
1083 }
1084 }
1085 break;
1086 case OPCODE_SNE: /* set on not equal */
1087 {
1088 GLfloat a[4], b[4], result[4];
1089 fetch_vector4(&inst->SrcReg[0], machine, a);
1090 fetch_vector4(&inst->SrcReg[1], machine, b);
1091 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1092 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1093 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1094 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1095 store_vector4(inst, machine, result);
1096 if (DEBUG_PROG) {
1097 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1098 result[0], result[1], result[2], result[3],
1099 a[0], a[1], a[2], a[3],
1100 b[0], b[1], b[2], b[3]);
1101 }
1102 }
1103 break;
1104 case OPCODE_SSG: /* set sign (-1, 0 or +1) */
1105 {
1106 GLfloat a[4], result[4];
1107 fetch_vector4(&inst->SrcReg[0], machine, a);
1108 result[0] = (GLfloat) ((a[0] > 0.0F) - (a[0] < 0.0F));
1109 result[1] = (GLfloat) ((a[1] > 0.0F) - (a[1] < 0.0F));
1110 result[2] = (GLfloat) ((a[2] > 0.0F) - (a[2] < 0.0F));
1111 result[3] = (GLfloat) ((a[3] > 0.0F) - (a[3] < 0.0F));
1112 store_vector4(inst, machine, result);
1113 }
1114 break;
1115 case OPCODE_SUB:
1116 {
1117 GLfloat a[4], b[4], result[4];
1118 fetch_vector4(&inst->SrcReg[0], machine, a);
1119 fetch_vector4(&inst->SrcReg[1], machine, b);
1120 result[0] = a[0] - b[0];
1121 result[1] = a[1] - b[1];
1122 result[2] = a[2] - b[2];
1123 result[3] = a[3] - b[3];
1124 store_vector4(inst, machine, result);
1125 if (DEBUG_PROG) {
1126 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1127 result[0], result[1], result[2], result[3],
1128 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
1129 }
1130 }
1131 break;
1132 case OPCODE_SWZ: /* extended swizzle */
1133 {
1134 const struct prog_src_register *source = &inst->SrcReg[0];
1135 const GLfloat *src = get_src_register_pointer(source, machine);
1136 GLfloat result[4];
1137 GLuint i;
1138 for (i = 0; i < 4; i++) {
1139 const GLuint swz = GET_SWZ(source->Swizzle, i);
1140 if (swz == SWIZZLE_ZERO)
1141 result[i] = 0.0;
1142 else if (swz == SWIZZLE_ONE)
1143 result[i] = 1.0;
1144 else {
1145 assert(swz <= 3);
1146 result[i] = src[swz];
1147 }
1148 if (source->Negate & (1 << i))
1149 result[i] = -result[i];
1150 }
1151 store_vector4(inst, machine, result);
1152 }
1153 break;
1154 case OPCODE_TEX: /* Both ARB and NV frag prog */
1155 /* Simple texel lookup */
1156 {
1157 GLfloat texcoord[4], color[4];
1158 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1159
1160 /* For TEX, texcoord.Q should not be used and its value should not
1161 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1162 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1163 * which is effectively what happens when the texcoord swizzle
1164 * is .xyzz
1165 */
1166 texcoord[3] = 1.0f;
1167
1168 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1169
1170 if (DEBUG_PROG) {
1171 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1172 color[0], color[1], color[2], color[3],
1173 inst->TexSrcUnit,
1174 texcoord[0], texcoord[1], texcoord[2], texcoord[3]);
1175 }
1176 store_vector4(inst, machine, color);
1177 }
1178 break;
1179 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1180 /* Texel lookup with LOD bias */
1181 {
1182 GLfloat texcoord[4], color[4], lodBias;
1183
1184 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1185
1186 /* texcoord[3] is the bias to add to lambda */
1187 lodBias = texcoord[3];
1188
1189 fetch_texel(ctx, machine, inst, texcoord, lodBias, color);
1190
1191 if (DEBUG_PROG) {
1192 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1193 " bias %g\n",
1194 color[0], color[1], color[2], color[3],
1195 inst->TexSrcUnit,
1196 texcoord[0],
1197 texcoord[1],
1198 texcoord[2],
1199 texcoord[3],
1200 lodBias);
1201 }
1202
1203 store_vector4(inst, machine, color);
1204 }
1205 break;
1206 case OPCODE_TXD: /* GL_NV_fragment_program only */
1207 /* Texture lookup w/ partial derivatives for LOD */
1208 {
1209 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
1210 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1211 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1212 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
1213 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
1214 0.0, /* lodBias */
1215 inst->TexSrcUnit, color);
1216 store_vector4(inst, machine, color);
1217 }
1218 break;
1219 case OPCODE_TXL:
1220 /* Texel lookup with explicit LOD */
1221 {
1222 GLfloat texcoord[4], color[4], lod;
1223
1224 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1225
1226 /* texcoord[3] is the LOD */
1227 lod = texcoord[3];
1228
1229 machine->FetchTexelLod(ctx, texcoord, lod,
1230 machine->Samplers[inst->TexSrcUnit], color);
1231
1232 store_vector4(inst, machine, color);
1233 }
1234 break;
1235 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1236 /* Texture lookup w/ projective divide */
1237 {
1238 GLfloat texcoord[4], color[4];
1239
1240 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1241 /* Not so sure about this test - if texcoord[3] is
1242 * zero, we'd probably be fine except for an assert in
1243 * IROUND_POS() which gets triggered by the inf values created.
1244 */
1245 if (texcoord[3] != 0.0F) {
1246 texcoord[0] /= texcoord[3];
1247 texcoord[1] /= texcoord[3];
1248 texcoord[2] /= texcoord[3];
1249 }
1250
1251 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1252
1253 store_vector4(inst, machine, color);
1254 }
1255 break;
1256 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
1257 /* Texture lookup w/ projective divide, as above, but do not
1258 * do the divide by w if sampling from a cube map.
1259 */
1260 {
1261 GLfloat texcoord[4], color[4];
1262
1263 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1264 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1265 texcoord[3] != 0.0F) {
1266 texcoord[0] /= texcoord[3];
1267 texcoord[1] /= texcoord[3];
1268 texcoord[2] /= texcoord[3];
1269 }
1270
1271 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1272
1273 store_vector4(inst, machine, color);
1274 }
1275 break;
1276 case OPCODE_TRUNC: /* truncate toward zero */
1277 {
1278 GLfloat a[4], result[4];
1279 fetch_vector4(&inst->SrcReg[0], machine, a);
1280 result[0] = (GLfloat) (GLint) a[0];
1281 result[1] = (GLfloat) (GLint) a[1];
1282 result[2] = (GLfloat) (GLint) a[2];
1283 result[3] = (GLfloat) (GLint) a[3];
1284 store_vector4(inst, machine, result);
1285 }
1286 break;
1287 case OPCODE_XPD: /* cross product */
1288 {
1289 GLfloat a[4], b[4], result[4];
1290 fetch_vector4(&inst->SrcReg[0], machine, a);
1291 fetch_vector4(&inst->SrcReg[1], machine, b);
1292 result[0] = a[1] * b[2] - a[2] * b[1];
1293 result[1] = a[2] * b[0] - a[0] * b[2];
1294 result[2] = a[0] * b[1] - a[1] * b[0];
1295 result[3] = 1.0;
1296 store_vector4(inst, machine, result);
1297 if (DEBUG_PROG) {
1298 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1299 result[0], result[1], result[2], result[3],
1300 a[0], a[1], a[2], b[0], b[1], b[2]);
1301 }
1302 }
1303 break;
1304 case OPCODE_END:
1305 return GL_TRUE;
1306 default:
1307 _mesa_problem(ctx, "Bad opcode %d in _mesa_execute_program",
1308 inst->Opcode);
1309 return GL_TRUE; /* return value doesn't matter */
1310 }
1311
1312 numExec++;
1313 if (numExec > maxExec) {
1314 static GLboolean reported = GL_FALSE;
1315 if (!reported) {
1316 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1317 reported = GL_TRUE;
1318 }
1319 return GL_TRUE;
1320 }
1321
1322 } /* for pc */
1323
1324 return GL_TRUE;
1325 }