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