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