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